fix endianess cleanups fallout in xfstests
[xfstests-dev.git] / src / loggen.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32  
33 /*
34  *
35  * loggen: Generate log entries. Very much incomplete. The empty log
36  *         record is a bit of a misnomer since we need to jump through
37  *         hoops to get a log record that parses ok yet does nothing.
38  *
39  *                                                  - dxm 29/09/00
40  */
41
42 #include <xfs/libxfs.h>
43 #include <xfs/xfs_log.h>
44 #include <xfs/xfs_log_priv.h>
45
46 void
47 usage(void)
48 {
49     fprintf(stderr,"Usage: loggen\n"
50                    "           set up parameters before writing record(s):\n"
51                    "               -f f     - set format\n"
52                    "               -u u     - set uuid\n"
53                    "               -c c     - set cycle\n"
54                    "               -b b     - set block\n"
55                    "               -C c     - set tail cycle\n"
56                    "               -B b     - set tail block\n"
57                    "           write log record(s):\n"
58                    "               -z n     - write n zero block(s)     (1BB)\n"
59                    "               -e n     - write n empty record(s)   (2BB)\n"
60                    "               -m n     - write n unmount record(s) (2BB)\n"
61                    "\n"
62                    "            redirect stdout to external log partition, or pipe to\n"
63                    "            dd with appropriate parameters to stuff into internal log.\n"
64     );
65     exit(1);
66 }
67
68 int         bufblocks            = 0;
69 void        *buf                 = NULL;
70 int         param_cycle          = 1;
71 int         param_block          = 0;
72 int         param_tail_cycle     = 1;
73 int         param_tail_block     = 0;
74 int         param_fmt            = XLOG_FMT;
75 uuid_t      param_uuid           = {0};
76
77 void
78 loggen_alloc(int blocks)
79 {
80     if (!(buf=realloc(buf, blocks*BBSIZE))) {
81         fprintf(stderr,"failed to allocate %d block(s)\n", blocks);
82         exit(1);
83     }
84     memset(buf, 0, blocks*BBSIZE);
85     bufblocks=blocks;
86 }
87
88 void
89 loggen_write(void)
90 {         
91     if (!buf) {
92         fprintf(stderr,"no buffer allocated\n");
93         exit(1);
94     }
95
96     if (fwrite(buf, BBSIZE, bufblocks, stdout) != bufblocks) {
97         perror("fwrite");
98         exit(1);
99     }
100
101 }
102
103 void
104 loggen_zero(int count)
105 {
106     if (!count) count=1;
107     
108     fprintf(stderr,"   *** zero block (1BB) x %d\n", count);
109     loggen_alloc(1);
110     while (count--)
111         loggen_write();
112 }      
113       
114 void
115 loggen_unmount(int count)
116 {
117     xlog_rec_header_t       *head;
118     xlog_op_header_t        *op;
119     /* the data section must be 32 bit size aligned */
120     struct {
121         __uint16_t magic;
122         __uint16_t pad1;
123         __uint32_t pad2; /* may as well make it 64 bits */
124     } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
125     
126     if (!count) count=1;
127     
128     fprintf(stderr,"   *** unmount record (2BB) x %d\n", count);
129     loggen_alloc(2);
130     
131     head = (xlog_rec_header_t *)buf;
132     op   = (xlog_op_header_t  *)(((char*)buf)+BBSIZE);
133
134     /* note that oh_tid actually contains the cycle number
135      * and the tid is stored in h_cycle_data[0] - that's the
136      * way things end up on disk.
137      */
138
139     INT_SET(head->h_magicno,        ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
140     INT_SET(head->h_cycle,          ARCH_CONVERT, param_cycle);
141     INT_SET(head->h_version,        ARCH_CONVERT, 1);
142     INT_SET(head->h_len,            ARCH_CONVERT, 20);
143     INT_SET(head->h_chksum,         ARCH_CONVERT, 0);
144     INT_SET(head->h_prev_block,     ARCH_CONVERT, -1);
145     INT_SET(head->h_num_logops,     ARCH_CONVERT, 1);
146     INT_SET(head->h_cycle_data[0],  ARCH_CONVERT, 0xb0c0d0d0);
147     INT_SET(head->h_fmt,            ARCH_CONVERT, param_fmt);
148     
149     ASSIGN_ANY_LSN_DISK(head->h_tail_lsn, param_tail_cycle, param_tail_block);
150
151     memcpy(head->h_fs_uuid,  param_uuid, sizeof(uuid_t));
152
153     /* now a log unmount op */
154     INT_SET(op->oh_tid,             ARCH_CONVERT, param_cycle);
155     INT_SET(op->oh_len,             ARCH_CONVERT, sizeof(magic));
156     INT_SET(op->oh_clientid,        ARCH_CONVERT, XFS_LOG);
157     INT_SET(op->oh_flags,           ARCH_CONVERT, XLOG_UNMOUNT_TRANS);
158     INT_SET(op->oh_res2,            ARCH_CONVERT, 0);
159
160     /* and the data for this op */
161
162     memcpy(op+1, &magic, sizeof(magic));
163     
164     while (count--) {
165         ASSIGN_ANY_LSN_DISK(head->h_lsn,         
166                 param_cycle, param_block++);
167         
168         loggen_write();
169     }
170
171   
172 void
173 loggen_empty(int count)
174 {
175     xlog_rec_header_t       *head;
176     xlog_op_header_t        *op1, *op2, *op3, *op4, *op5;
177     xfs_trans_header_t      *trans;
178     xfs_buf_log_format_t    *blf;
179     int                     *data;
180     char                    *p;
181     
182     if (!count) count=1;
183     
184     fprintf(stderr,"   *** empty record (2BB) x %d\n", count);
185     loggen_alloc(2);
186     
187     p=(char*)buf;
188     head  = (xlog_rec_header_t   *)p;         p+=BBSIZE;
189     op1   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
190     op2   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
191     trans = (xfs_trans_header_t  *)p;         p+=sizeof(xfs_trans_header_t);
192     op3   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
193     blf   = (xfs_buf_log_format_t*)p;         p+=sizeof(xfs_buf_log_format_t);
194     op4   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
195     data  = (int                 *)p;         p+=sizeof(int);
196     op5   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
197
198     /* note that oh_tid actually contains the cycle number
199      * and the tid is stored in h_cycle_data[0] - that's the
200      * way things end up on disk.
201      */
202
203     INT_SET(head->h_magicno,        ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
204     INT_SET(head->h_cycle,          ARCH_CONVERT, param_cycle);
205     INT_SET(head->h_version,        ARCH_CONVERT, 1);
206     INT_SET(head->h_len,            ARCH_CONVERT, 5*sizeof(xlog_op_header_t) +
207                                                     sizeof(xfs_trans_header_t)+
208                                                     sizeof(xfs_buf_log_format_t)+
209                                                     sizeof(int));
210     INT_SET(head->h_chksum,         ARCH_CONVERT, 0);
211     INT_SET(head->h_prev_block,     ARCH_CONVERT, -1);
212     INT_SET(head->h_num_logops,     ARCH_CONVERT, 5);
213     INT_SET(head->h_cycle_data[0],  ARCH_CONVERT, 0xb0c0d0d0);
214     INT_SET(head->h_fmt,            ARCH_CONVERT, param_fmt);
215     
216     ASSIGN_ANY_LSN_DISK(head->h_tail_lsn,    
217             param_tail_cycle, param_tail_block);
218
219     memcpy(head->h_fs_uuid,  param_uuid, sizeof(uuid_t));
220
221     /* start */
222     INT_SET(op1->oh_tid,            ARCH_CONVERT, 1);
223     INT_SET(op1->oh_len,            ARCH_CONVERT, 0);
224     INT_SET(op1->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
225     INT_SET(op1->oh_flags,          ARCH_CONVERT, XLOG_START_TRANS);
226     INT_SET(op1->oh_res2,           ARCH_CONVERT, 0);
227     /* dummy */
228     INT_SET(op2->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
229     INT_SET(op2->oh_len,            ARCH_CONVERT, sizeof(xfs_trans_header_t));
230     INT_SET(op2->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
231     INT_SET(op2->oh_flags,          ARCH_CONVERT, 0);
232     INT_SET(op2->oh_res2,           ARCH_CONVERT, 0);
233     /* dummy transaction - this stuff doesn't get endian converted */
234     trans->th_magic     = XFS_TRANS_MAGIC;
235     trans->th_type      = XFS_TRANS_DUMMY1;
236     trans->th_tid       = 0;
237     trans->th_num_items = 1;
238     /* buffer */
239     INT_SET(op3->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
240     INT_SET(op3->oh_len,            ARCH_CONVERT, sizeof(xfs_buf_log_format_t));
241     INT_SET(op3->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
242     INT_SET(op3->oh_flags,          ARCH_CONVERT, 0);
243     INT_SET(op3->oh_res2,           ARCH_CONVERT, 0);
244     /* an empty buffer too */
245     blf->blf_type       = XFS_LI_BUF;
246     blf->blf_size       = 2;
247     blf->blf_flags      = XFS_BLI_CANCEL;
248     blf->blf_blkno      = 1;
249     blf->blf_map_size   = 1;
250     blf->blf_data_map[0]= 0;
251     /* commit */
252     INT_SET(op4->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
253     INT_SET(op4->oh_len,            ARCH_CONVERT, sizeof(int));
254     INT_SET(op4->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
255     INT_SET(op4->oh_flags,          ARCH_CONVERT, 0);
256     INT_SET(op4->oh_res2,           ARCH_CONVERT, 0);
257     /* and the data */
258     *data=*(int*)(char*)"FISH"; /* this won't get written (I hope) */
259     /* commit */
260     INT_SET(op5->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
261     INT_SET(op5->oh_len,            ARCH_CONVERT, 0);
262     INT_SET(op5->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
263     INT_SET(op5->oh_flags,          ARCH_CONVERT, XLOG_COMMIT_TRANS);
264     INT_SET(op5->oh_res2,           ARCH_CONVERT, 0);
265
266     while (count--) {
267         ASSIGN_ANY_LSN_DISK(head->h_lsn,         
268                 param_cycle, param_block++);
269         
270         loggen_write();
271     }
272 }   
273
274 int
275 main(int argc, char *argv[])
276 {
277     int c;
278     
279     fprintf(stderr,"*** loggen\n");
280     
281     if (argc<2) usage();
282     
283     while ((c = getopt(argc, argv, "f:u:c:b:C:B:z:e:m:")) != -1) {
284         switch (c) {
285             case 'f':
286                 param_fmt=atoi(optarg);
287                 break;
288             case 'u':
289                 memset(param_uuid, atoi(optarg), sizeof(param_uuid));
290                 break;
291             case 'c':
292                 param_cycle=atoi(optarg);
293                 break;
294             case 'b':
295                 param_block=atoi(optarg);
296                 break;
297             case 'C':
298                 param_tail_cycle=atoi(optarg);
299                 break;
300             case 'B':
301                 param_tail_block=atoi(optarg);
302                 break;
303                 
304             case 'z':
305                 loggen_zero(atoi(optarg));
306                 break;
307             case 'e':
308                 loggen_empty(atoi(optarg));
309                 break;
310             case 'm':
311                 loggen_unmount(atoi(optarg));
312                 break;
313                 
314             default:
315                 fprintf(stderr, "unknown option\n");
316                 usage();
317         }
318     }
319     return 0;   
320 }