Update copyright dates (again)
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_allocinfo.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 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 #include <lib/hsm.h>
41
42 #include <string.h>
43
44 /*---------------------------------------------------------------------------
45
46 Test program used to test the DMAPI function dm_get_allocinfo().  The
47 command line is:
48
49         get_allocinfo [-D] [-n nelem] [-o offp] [-s sid] pathname
50
51 where pathname is the name of a file, 'offp' is a byte offset from the
52 beginning of the file where you want to start dumping, and 'nelem' allows
53 you to specify how many extent structures to use in each dm_get_allocinfo
54 call.
55
56 The code checks the returned extents as much as possible for errors.
57 It detects bad ex_type values, verifies that there is always a trailing
58 hole at the end of the file, that the ex_offset of each extent matches the
59 ex_offset+ex_length of the previous extent, and that ex_offset+ex_length
60 is always an even multiple of 512.  It verifies that all ex_offset values
61 after the first fall on a 512-byte boundary.  It verifies that the '*offp'
62 value returned by dm_get_allocinfo() is 0 at the end of the file, and
63 equals ex_offset+ex_length of the last extent if not at the end of the file.
64 Any error is reported to stderr, and the program then terminates with a
65 non-zero exit status.
66
67 The program produces output similar to xfs_bmap in order to make comparison
68 easier.  Here is some sample output.
69
70 f1: offset 1
71         rc 0, nelemp 17
72         0: [0..127]: resv       [1..511]
73
74 Line 1 gives the name of the file and the byte offset within the file where
75 the dump started.  Line 2 appears once for each dm_get_allocinfo() call,
76 giving the return value (rc) and the number of extents which were returned.
77 Line 3 is repeated once for each extent.  The first field "0:" is the extent
78 number.  The second field "[0..127]:" give the starting and ending block for
79 the extent in 512-byte units.  The third field is either "resv" to indicate
80 allocated space or "hole" if the extent is a hole.  The fourth field
81 "[1..511]" only appears if the dump did not start with byte zero of the
82 first block.  In that case, the first number shows the actual byte offset
83 within the block (1 in this case).  The second number should always be
84 511 since we always dump to the end of a block.
85
86 Possible tests
87 --------------
88
89 Dump some holey files and compare the output of this program with xfs_bmap.
90
91 Produce a file with holes, and perform the following tests using just one
92 extent (-e 1).
93         Dump extents from beginning of the file.
94         Dump from byte 1 of the file.
95         Dump from the last byte of the first extent.
96         Dump from the first byte of the second extent.
97         Dump from the middle of the second extent.
98         Dump the first byte of the last extent.
99         Dump the last byte of the last extent.
100         Dump the first byte after the last extent.
101         Dump starting at an offset way past the end of the file.
102
103 Produce a fragmented file with many adjacent DM_EXTENT_RES extents.
104         Repeat the above tests.
105
106 Produce a GRIO file with holes.
107         Repeat the above tests.
108
109 Run the following shell script.
110
111 #!/bin/ksh
112
113 #       Dump the same holey file $max times, each time using one less extent
114 #       structure than the previous time.  The grep and diff code
115 #       checks to make sure that you get the same answer each time, no matter
116 #       how many extents you use.  If all is okay, The only messages you will
117 #       see are the "trial $num" messages.
118
119 max=20          # should be bigger than the number extents in the file
120
121 num=$max
122 while [ $num -gt 0 ]
123 do
124         echo "trial $num"
125         ./test_alloc -e $num f1 | grep '\[' > x.$num
126         if [ $num -lt $max ]
127         then
128                 diff x.$num x.$max
129         fi
130         num=`expr $num - 1`
131 done
132
133 ----------------------------------------------------------------------------*/
134
135 #ifndef linux
136 extern  char    *sys_errlist[];
137 #endif
138 extern  int     optind;
139 extern  char    *optarg;
140
141 static int print_alloc(dm_sessid_t sid, void* hanp, size_t hlen,
142         char *pathname, dm_off_t startoff, u_int nelem);
143
144 char    *Progname;
145 int     Dflag = 0;
146
147
148 static void
149 usage(void)
150 {
151         fprintf(stderr, "usage:\t%s [-D] [-n nelem] [-o off] [-s sid] "
152                 "pathname\n", Progname);
153         exit(1);
154 }
155
156
157 int
158 main(
159         int     argc, 
160         char    **argv)
161 {
162         dm_sessid_t      sid = DM_NO_SESSION;
163         dm_off_t        startoff = 0;           /* starting offset */
164         u_int           nelem = 100;
165         char            *pathname;
166         void            *hanp;
167         size_t           hlen;
168         dm_stat_t       sbuf;
169         char            *name;
170         int             opt;
171
172         if (Progname = strrchr(argv[0], '/')) {
173                 Progname++;
174         } else {
175                 Progname = argv[0];
176         }
177
178         /* Crack and validate the command line options. */
179
180         while ((opt = getopt(argc, argv, "Dn:o:s:")) != EOF) {
181                 switch(opt) {
182                 case 'D':
183                         Dflag++;
184                         break;
185                 case 'n':
186                         nelem = atol(optarg);
187                         break;
188                 case 'o':
189                         startoff = atoll(optarg);
190                         break;
191                 case 's':
192                         sid = atol(optarg);
193                         break;
194                 case '?':
195                         usage();
196                 }
197         }
198         if (optind + 1 != argc)
199                 usage();
200         pathname = argv[optind];
201
202         if (dm_init_service(&name)) {
203                 fprintf(stderr, "dm_init_service failed, %s\n",
204                         strerror(errno));
205                 exit(1);
206         }
207
208         if (sid == DM_NO_SESSION)
209                 find_test_session(&sid);
210
211         /* Get the file's handle and verify that it is a regular file. */
212
213         if (dm_path_to_handle(pathname, &hanp, &hlen)) {
214                 fprintf(stderr, "can't get handle for %s\n", pathname);
215                 exit(1);
216         }
217         if (dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN, DM_AT_STAT, &sbuf)) {
218                 fprintf(stderr, "dm_get_fileattr failed\n");
219                 exit(1);
220         }
221         if (!S_ISREG(sbuf.dt_mode)) {
222                 fprintf(stderr, "%s is not a regular file\n", pathname);
223                 exit(1);
224         }
225
226         /* Print the allocation. */
227
228         if (print_alloc(sid, hanp, hlen, pathname, startoff, nelem))
229                 exit(1);
230
231         dm_handle_free(hanp, hlen);
232         exit(0);
233 }
234
235
236 static int
237 print_alloc(
238         dm_sessid_t     sid,
239         void            *hanp,
240         size_t          hlen,
241         char            *pathname,
242         dm_off_t        startoff,
243         u_int           nelem)
244 {
245         dm_off_t        endoff;
246         dm_extent_t     *extent;
247         u_int           nelemp;
248         u_int           num = 0;
249         u_int           i;
250         char            *type = NULL;
251         int             rc;
252
253         fprintf(stdout, "%s: starting offset %lld\n", pathname, startoff);
254
255         /* Allocate space for the number of extents requested by the user. */
256
257         if ((extent = malloc(nelem * sizeof(*extent))) == NULL) {
258                 fprintf(stderr, "can't malloc extent structures\n");
259                 return(1);
260         }
261
262         rc = 1;
263         endoff = startoff;
264
265         while (rc != 0) {
266                 rc = dm_get_allocinfo(sid, hanp, hlen, DM_NO_TOKEN, &startoff,
267                         nelem, extent, &nelemp);
268
269                 if (rc < 0) {
270                         fprintf(stderr, "dm_get_allocinfo failed, %s\n",
271                                 strerror(errno));
272                         return(1);
273                 }
274
275                 fprintf(stdout, "\treturned %d, nelemp %d\n", rc, nelemp);
276                 if (Dflag && nelemp)
277                         fprintf(stdout, " ex_type    ex_offset       ex_length\n");
278
279                 /* Note: it is possible for nelemp to be zero! */
280
281                 for (i = 0; i < nelemp; i++) {
282                         /* The extent must either be reserved space or a hole.
283                         */
284
285                         switch (extent[i].ex_type) {
286                         case DM_EXTENT_RES:
287                                 type = "resv";
288                                 break;
289                         case DM_EXTENT_HOLE:
290                                 type = "hole";
291                                 break;
292                         default:
293                                 fprintf(stderr, "invalid extent type %d\n",
294                                         extent[i].ex_type);
295                                 return(1);
296                         }
297
298                         if (!Dflag) {
299                                 fprintf(stdout, "\t%d: [%lld..%lld]: %s", num,
300                                         extent[i].ex_offset / 512,
301                                         (extent[i].ex_offset +
302                                         extent[i].ex_length - 1) / 512, type);
303                                 if ((extent[i].ex_offset % 512 != 0) ||
304                                     (endoff % 512 != 0)) {
305                                         fprintf(stdout, "\t[%lld..%lld]\n",
306                                                 extent[i].ex_offset % 512,
307                                                 (endoff-1) % 512);
308                                 } else {
309                                         fprintf(stdout, "\n");
310                                 }
311                         } else {
312                                 fprintf(stdout, "%5s    %13lld  %13lld\n",
313                                         type, extent[i].ex_offset,
314                                         extent[i].ex_length);
315                         }
316
317                         /* The ex_offset of the first extent should match the
318                            'startoff' specified by the caller.  The ex_offset
319                            in subsequent extents should always match
320                            (ex_offset + ex_length) of the previous extent,
321                            and should always start on a 512 byte boundary.
322                         */
323
324                         if (extent[i].ex_offset != endoff) {
325                                 fprintf(stderr, "new extent (%lld)is not "
326                                         "adjacent to previous one (%lld)\n",
327                                         extent[i].ex_offset, endoff);
328                                 return(1);
329                         }
330                         if (num && (extent[i].ex_offset % 512) != 0) {
331                                 fprintf(stderr, "non-initial ex_offset (%lld) "
332                                         "is not a 512-byte multiple\n",
333                                         extent[i].ex_offset);
334                                 return(1);
335                         }
336
337                         /* Non-initial extents should have ex_length values
338                            that are an even multiple of 512.  The initial
339                            extent should be a multiple of 512 less the offset
340                            into the starting 512-byte block.
341                         */
342
343                         if (((extent[i].ex_offset % 512) + extent[i].ex_length) % 512 != 0) { 
344                                 fprintf(stderr, "ex_length is incorrect based "
345                                         "upon the ex_offset\n");
346                                 return(1);
347                         }
348
349                         endoff = extent[i].ex_offset + extent[i].ex_length;
350                         num++;          /* count of extents printed */
351                 }
352
353                 /* If not yet at end of file, the startoff parameter should
354                    match the ex_offset plus ex_length of the last extent
355                    retrieved.
356                 */
357
358                 if (rc && startoff != endoff) {
359                         fprintf(stderr, "startoff is %lld, should be %lld\n",
360                                 startoff, endoff);
361                         return(1);
362                 }
363
364                 /* If we are at end of file, the last extent should be a
365                    hole.
366                 */
367
368                 if (!rc && type && strcmp(type, "hole")) {
369                         fprintf(stderr, "file didn't end with a hole\n");
370                         return(1);
371                 }
372         }
373
374         /* At end of file, startoff should always equal zero. */
375
376         if (startoff != 0) {
377                 fprintf(stderr, "ERROR: startoff was not zero at end of file\n");
378                 return(1);
379         }
380         return(0);
381 }