Update copyright dates
[xfstests-dev.git] / src / loggen.c
1 /*
2  * Copyright (c) 2000-2001 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 <libxfs.h>
43 #include <malloc.h>
44 #include <xfs_log.h>
45 #include <xfs_log_priv.h>
46
47 void
48 usage()
49 {
50     fprintf(stderr,"Usage: loggen\n"
51                    "           set up parameters before writing record(s):\n"
52                    "               -f f     - set format\n"
53                    "               -u u     - set uuid\n"
54                    "               -c c     - set cycle\n"
55                    "               -b b     - set block\n"
56                    "               -C c     - set tail cycle\n"
57                    "               -B b     - set tail block\n"
58                    "           write log record(s):\n"
59                    "               -z n     - write n zero block(s)     (1BB)\n"
60                    "               -e n     - write n empty record(s)   (2BB)\n"
61                    "               -m n     - write n unmount record(s) (2BB)\n"
62                    "\n"
63                    "            redirect stdout to external log partition, or pipe to\n"
64                    "            dd with appropriate parameters to stuff into internal log.\n"
65     );
66     exit(1);
67 }
68
69 int         bufblocks            = 0;
70 void        *buf                 = NULL;
71 int         param_cycle          = 1;
72 int         param_block          = 0;
73 int         param_tail_cycle     = 1;
74 int         param_tail_block     = 0;
75 int         param_fmt            = XLOG_FMT;
76 uuid_t      param_uuid           = {0};
77
78 void
79 loggen_alloc(int blocks)
80 {
81     if (!(buf=realloc(buf, blocks*BBSIZE))) {
82         fprintf(stderr,"failed to allocate %d block(s)\n", blocks);
83         exit(1);
84     }
85     memset(buf, 0, blocks*BBSIZE);
86     bufblocks=blocks;
87 }
88
89 void
90 loggen_write()
91 {         
92     if (!buf) {
93         fprintf(stderr,"no buffer allocated\n");
94         exit(1);
95     }
96
97     if (fwrite(buf, BBSIZE, bufblocks, stdout) != bufblocks) {
98         perror("fwrite");
99         exit(1);
100     }
101
102 }
103
104 void
105 loggen_zero(int count)
106 {
107     if (!count) count=1;
108     
109     fprintf(stderr,"   *** zero block (1BB) x %d\n", count);
110     loggen_alloc(1);
111     while (count--)
112         loggen_write(count);
113 }      
114       
115 void
116 loggen_unmount(int count)
117 {
118     xlog_rec_header_t       *head;
119     xlog_op_header_t        *op;
120     /* the data section must be 32 bit size aligned */
121     struct {
122         __uint16_t magic;
123         __uint16_t pad1;
124         __uint32_t pad2; /* may as well make it 64 bits */
125     } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
126     
127     if (!count) count=1;
128     
129     fprintf(stderr,"   *** unmount record (2BB) x %d\n", count);
130     loggen_alloc(2);
131     
132     head = (xlog_rec_header_t *)buf;
133     op   = (xlog_op_header_t  *)(((char*)buf)+BBSIZE);
134
135     /* note that oh_tid actually contains the cycle number
136      * and the tid is stored in h_cycle_data[0] - that's the
137      * way things end up on disk.
138      */
139
140     INT_SET(head->h_magicno,        ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
141     INT_SET(head->h_cycle,          ARCH_CONVERT, param_cycle);
142     INT_SET(head->h_version,        ARCH_CONVERT, 1);
143     INT_SET(head->h_len,            ARCH_CONVERT, 20);
144     INT_SET(head->h_chksum,         ARCH_CONVERT, 0);
145     INT_SET(head->h_prev_block,     ARCH_CONVERT, -1);
146     INT_SET(head->h_num_logops,     ARCH_CONVERT, 1);
147     INT_SET(head->h_cycle_data[0],  ARCH_CONVERT, 0xb0c0d0d0);
148     INT_SET(head->h_fmt,            ARCH_CONVERT, param_fmt);
149     
150     ASSIGN_ANY_LSN(head->h_tail_lsn,    
151             param_tail_cycle, param_tail_block, ARCH_CONVERT);
152
153     memcpy(head->h_fs_uuid,  param_uuid, sizeof(uuid_t));
154
155     /* now a log unmount op */
156     INT_SET(op->oh_tid,             ARCH_CONVERT, param_cycle);
157     INT_SET(op->oh_len,             ARCH_CONVERT, sizeof(magic));
158     INT_SET(op->oh_clientid,        ARCH_CONVERT, XFS_LOG);
159     INT_SET(op->oh_flags,           ARCH_CONVERT, XLOG_UNMOUNT_TRANS);
160     INT_SET(op->oh_res2,            ARCH_CONVERT, 0);
161
162     /* and the data for this op */
163
164     memcpy(op+1, &magic, sizeof(magic));
165     
166     while (count--) {
167         ASSIGN_ANY_LSN(head->h_lsn,         
168                 param_cycle, param_block++, ARCH_CONVERT);
169         
170         loggen_write(count);
171     }
172
173   
174 void
175 loggen_empty(int count)
176 {
177     xlog_rec_header_t       *head;
178     xlog_op_header_t        *op1, *op2, *op3, *op4, *op5;
179     xfs_trans_header_t      *trans;
180     xfs_buf_log_format_t    *blf;
181     int                     *data;
182     char                    *p;
183     
184     if (!count) count=1;
185     
186     fprintf(stderr,"   *** empty record (2BB) x %d\n", count);
187     loggen_alloc(2);
188     
189     p=(char*)buf;
190     head  = (xlog_rec_header_t   *)p;         p+=BBSIZE;
191     op1   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
192     op2   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
193     trans = (xfs_trans_header_t  *)p;         p+=sizeof(xfs_trans_header_t);
194     op3   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
195     blf   = (xfs_buf_log_format_t*)p;         p+=sizeof(xfs_buf_log_format_t);
196     op4   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
197     data  = (int                 *)p;         p+=sizeof(int);
198     op5   = (xlog_op_header_t    *)p;         p+=sizeof(xlog_op_header_t);
199
200     /* note that oh_tid actually contains the cycle number
201      * and the tid is stored in h_cycle_data[0] - that's the
202      * way things end up on disk.
203      */
204
205     INT_SET(head->h_magicno,        ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
206     INT_SET(head->h_cycle,          ARCH_CONVERT, param_cycle);
207     INT_SET(head->h_version,        ARCH_CONVERT, 1);
208     INT_SET(head->h_len,            ARCH_CONVERT, 5*sizeof(xlog_op_header_t) +
209                                                     sizeof(xfs_trans_header_t)+
210                                                     sizeof(xfs_buf_log_format_t)+
211                                                     sizeof(int));
212     INT_SET(head->h_chksum,         ARCH_CONVERT, 0);
213     INT_SET(head->h_prev_block,     ARCH_CONVERT, -1);
214     INT_SET(head->h_num_logops,     ARCH_CONVERT, 5);
215     INT_SET(head->h_cycle_data[0],  ARCH_CONVERT, 0xb0c0d0d0);
216     INT_SET(head->h_fmt,            ARCH_CONVERT, param_fmt);
217     
218     ASSIGN_ANY_LSN(head->h_tail_lsn,    
219             param_tail_cycle, param_tail_block, ARCH_CONVERT);
220
221     memcpy(head->h_fs_uuid,  param_uuid, sizeof(uuid_t));
222
223     /* start */
224     INT_SET(op1->oh_tid,            ARCH_CONVERT, 1);
225     INT_SET(op1->oh_len,            ARCH_CONVERT, 0);
226     INT_SET(op1->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
227     INT_SET(op1->oh_flags,          ARCH_CONVERT, XLOG_START_TRANS);
228     INT_SET(op1->oh_res2,           ARCH_CONVERT, 0);
229     /* dummy */
230     INT_SET(op2->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
231     INT_SET(op2->oh_len,            ARCH_CONVERT, sizeof(xfs_trans_header_t));
232     INT_SET(op2->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
233     INT_SET(op2->oh_flags,          ARCH_CONVERT, 0);
234     INT_SET(op2->oh_res2,           ARCH_CONVERT, 0);
235     /* dummy transaction - this stuff doesn't get endian converted */
236     trans->th_magic     = XFS_TRANS_MAGIC;
237     trans->th_type      = XFS_TRANS_DUMMY1;
238     trans->th_tid       = 0;
239     trans->th_num_items = 1;
240     /* buffer */
241     INT_SET(op3->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
242     INT_SET(op3->oh_len,            ARCH_CONVERT, sizeof(xfs_buf_log_format_t));
243     INT_SET(op3->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
244     INT_SET(op3->oh_flags,          ARCH_CONVERT, 0);
245     INT_SET(op3->oh_res2,           ARCH_CONVERT, 0);
246     /* an empty buffer too */
247     blf->blf_type       = XFS_LI_BUF;
248     blf->blf_size       = 2;
249     blf->blf_flags      = XFS_BLI_CANCEL;
250     blf->blf_blkno      = 1;
251     blf->blf_map_size   = 1;
252     blf->blf_data_map[0]= 0;
253     /* commit */
254     INT_SET(op4->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
255     INT_SET(op4->oh_len,            ARCH_CONVERT, sizeof(int));
256     INT_SET(op4->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
257     INT_SET(op4->oh_flags,          ARCH_CONVERT, 0);
258     INT_SET(op4->oh_res2,           ARCH_CONVERT, 0);
259     /* and the data */
260     *data=*(int*)(char*)"FISH"; /* this won't get written (I hope) */
261     /* commit */
262     INT_SET(op5->oh_tid,            ARCH_CONVERT, 0xb0c0d0d0);
263     INT_SET(op5->oh_len,            ARCH_CONVERT, 0);
264     INT_SET(op5->oh_clientid,       ARCH_CONVERT, XFS_TRANSACTION);
265     INT_SET(op5->oh_flags,          ARCH_CONVERT, XLOG_COMMIT_TRANS);
266     INT_SET(op5->oh_res2,           ARCH_CONVERT, 0);
267
268     while (count--) {
269         ASSIGN_ANY_LSN(head->h_lsn,         
270                 param_cycle, param_block++, ARCH_CONVERT);
271         
272         loggen_write(count);
273     }
274 }   
275
276 int
277 main(int argc, char *argv[])
278 {
279     int c;
280     
281     fprintf(stderr,"*** loggen\n");
282     
283     if (argc<2) usage();
284     
285     while ((c = getopt(argc, argv, "f:u:c:b:C:B:z:e:m:")) != -1) {
286         switch (c) {
287             case 'f':
288                 param_fmt=atoi(optarg);
289                 break;
290             case 'u':
291                 memset(param_uuid, atoi(optarg), sizeof(param_uuid));
292                 break;
293             case 'c':
294                 param_cycle=atoi(optarg);
295                 break;
296             case 'b':
297                 param_block=atoi(optarg);
298                 break;
299             case 'C':
300                 param_tail_cycle=atoi(optarg);
301                 break;
302             case 'B':
303                 param_tail_block=atoi(optarg);
304                 break;
305                 
306             case 'z':
307                 loggen_zero(atoi(optarg));
308                 break;
309             case 'e':
310                 loggen_empty(atoi(optarg));
311                 break;
312             case 'm':
313                 loggen_unmount(atoi(optarg));
314                 break;
315                 
316             default:
317                 fprintf(stderr, "unknown option\n");
318                 usage();
319         }
320     }
321     return 0;   
322 }
323
324