openafs-string-header-cleanup-20071030
[openafs.git] / src / vol / listinodes.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11
12         System:         VICE-TWO
13         Module:         listinodes.c
14         Institution:    The Information Technology Center, Carnegie-Mellon University
15
16  */
17
18 #define ITC                     /* Required by inode.h */
19
20 #include <afsconfig.h>
21 #include <afs/param.h>
22
23 #include <string.h>
24
25 RCSID
26     ("$Header$");
27
28 #ifndef AFS_NAMEI_ENV
29 #if defined(AFS_LINUX20_ENV) || defined(AFS_SUN4_ENV)
30 /* ListViceInodes
31  *
32  * Return codes:
33  * 0 - success
34  * -1 - Unable to read the inodes.
35  * -2 - Unable to completely write temp file. Produces warning message in log.
36  */
37 int
38 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
39                int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
40                char *wpath, void *rock)
41 {
42     Log("ListViceInodes not implemented for this platform!\n");
43     return -1;
44 }
45 #else
46 #include <ctype.h>
47 #include <sys/param.h>
48 #if defined(AFS_SGI_ENV)
49 #else
50 #ifdef  AFS_OSF_ENV
51 #include <ufs/fs.h>
52 #else /* AFS_OSF_ENV */
53 #ifdef AFS_VFSINCL_ENV
54 #define VFS
55 #ifdef    AFS_SUN5_ENV
56 #include <sys/fs/ufs_fs.h>
57 #else
58 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
59 #include <ufs/ufs/dinode.h>
60 #include <ufs/ffs/fs.h>
61 #define itod ino_to_fsba
62 #else
63 #include <ufs/fs.h>
64 #endif
65 #endif
66 #else /* AFS_VFSINCL_ENV */
67 #ifdef  AFS_AIX_ENV
68 #include <sys/filsys.h>
69 #else
70 #include <sys/fs.h>
71 #endif
72 #endif /* AFS_VFSINCL_ENV */
73 #endif /* AFS_OSF_ENV */
74 #include <sys/time.h>
75 #ifdef AFS_VFSINCL_ENV
76 #include <sys/vnode.h>
77 #ifdef    AFS_SUN5_ENV
78 #include <sys/fs/ufs_inode.h>
79 #else
80 #if !defined(AFS_DARWIN_ENV)
81 #include <ufs/inode.h>
82 #endif
83 #endif
84 #else /* AFS_VFSINCL_ENV */
85 #ifdef  AFS_OSF_ENV
86 #include <ufs/inode.h>
87 #else /* AFS_OSF_ENV */
88 #include <sys/inode.h>
89 #endif
90 #endif /* AFS_VFSINCL_ENV */
91 #endif /* AFS_SGI_ENV */
92 #include <afs/osi_inode.h>
93 #include <sys/file.h>
94 #include <stdio.h>
95 #include <rx/xdr.h>
96 #include <afs/afsint.h>
97 #include "nfs.h"
98 #include <afs/afssyscalls.h>
99 #include "viceinode.h"
100 #include <sys/stat.h>
101 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV)
102 #include <sys/ino.h>
103 #endif
104 #ifdef AFS_PTHREAD_ENV
105 #include <assert.h>
106 #else /* AFS_PTHREAD_ENV */
107 #include <afs/assert.h>
108 #endif /* AFS_PTHREAD_ENV */
109 #if defined(AFS_HPUX101_ENV)
110 #include <unistd.h>
111 #endif
112 #include "partition.h"
113
114 /*@+fcnmacros +macrofcndecl@*/
115 #ifdef O_LARGEFILE
116 #ifdef S_SPLINT_S
117 extern off64_t afs_lseek(int FD, off64_t O, int F);
118 #endif /*S_SPLINT_S */
119 #define afs_lseek(FD, O, F)   lseek64(FD, (off64_t) (O), F)
120 #define afs_stat      stat64
121 #define afs_fstat     fstat64
122 #define afs_open      open64
123 #define afs_fopen     fopen64
124 #else /* !O_LARGEFILE */
125 #ifdef S_SPLINT_S
126 extern off_t afs_lseek(int FD, off_t O, int F);
127 #endif /*S_SPLINT_S */
128 #define afs_lseek(FD, O, F)   lseek(FD, (off_t) (O), F)
129 #define afs_stat      stat
130 #define afs_fstat     fstat
131 #define afs_open      open
132 #define afs_fopen     fopen
133 #endif /* !O_LARGEFILE */
134 /*@=fcnmacros =macrofcndecl@*/
135
136 /* Notice:  parts of this module have been cribbed from vfsck.c */
137
138 #define ROOTINODE       2
139 static char *partition;
140 int Testing=0;
141 int pfd;
142
143 #ifdef  AFS_AIX32_ENV
144 #include <jfs/filsys.h>
145
146 #ifndef FSBSIZE
147 #define FSBSIZE         (4096)  /* filesystem block size        */
148 #define FSBSHIFT        (12)    /* log2(FSBSIZE)                */
149 #define FSBMASK         (FSBSIZE - 1)   /* FSBSIZE mask                 */
150
151 #define MIN_FSIZE       DISKMAP_B       /* minimum fs size (FSblocks)   */
152 #define LAST_RSVD_I     15      /* last reserved inode          */
153 #endif
154
155 #ifndef INOPB
156 /*
157  * This will hopefully eventually make it into the system include files
158  */
159 #define INOPB           (FSBSIZE / sizeof (struct dinode))
160 #endif
161
162 #ifdef AFS_AIX41_ENV
163 int fragsize;
164 int iagsize;
165 int ag512;
166 int agblocks;
167 #endif /* AFS_AIX41_ENV */
168
169 /*
170  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
171  XX This was lifted from some `com/cmd/fs/fshlpr_aix3/Fs.h', which indicated X
172  XX a longing to see it make it into a readily accessible include file. XXXXXX
173  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
174  *
175  * itoo - inode number to offset within disk block
176  */
177 #undef itoo
178 #define itoo(x)         (int) ((unsigned)(x) % INOPB)
179
180 int Bsize = FSBSIZE;            /* block size for this system                   */
181 daddr_t fmax;                   /* total number of blocks n file system         */
182 ino_t imax, inum;               /* total number of I-nodes in file system       */
183
184 static struct superblock fs;
185 struct dinode *ginode();
186
187
188 int
189 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
190                int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
191                char *wpath, void *rock)
192 {
193     FILE *inodeFile = NULL;
194     char dev[50], rdev[51];
195     struct stat status;
196     struct dinode *p;
197     struct ViceInodeInfo info;
198     struct stat root_inode;
199     int ninodes = 0, err = 0;
200
201     pfd = -1;                   /* initialize so we don't close on error output below. */
202     *forcep = 0;
203     sync();
204     sleep(1);                   /* simulate operator    */
205     sync();
206     sleep(1);
207     sync();
208     sleep(1);
209
210     partition = mountedOn;
211     sprintf(dev, "/dev/%s", devname);
212     sprintf(rdev, "/dev/r%s", devname);
213
214     if (stat(mountedOn, &root_inode) < 0) {
215         Log("cannot stat: %s\n", mountedOn);
216         return -1;
217     }
218
219     if (root_inode.st_ino != ROOTDIR_I) {
220         Log("%s is not root of a filesystem\n", mountedOn);
221         return -1;
222     }
223
224
225     /*
226      * done with the superblock, now try to read the raw device.
227      */
228     if (ReadSuper(&fs, dev) < 0)
229         return -1;
230
231     switch (fs.s_fmod) {
232     default:
233     case FM_CLEAN:              /* clean and unmounted                  */
234         Log("Most peculiar - Super blk in FM_CLEAN state!\n");
235         goto out;
236     case FM_MOUNT:              /* mounted cleanly                      */
237         break;
238
239     case FM_MDIRTY:             /* dirty when mounted or commit fail    */
240     case FM_LOGREDO:            /* log redo attempted but failed        */
241         Log("File system %s is in a bad state.\n", rdev);
242         Log("Call your IBM representative.\n");
243         return -1;
244     }
245 #ifdef AFS_AIX42_ENV
246     if (IsBigFilesFileSystem(&fs, (char *)0)) {
247         Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
248         return -1;
249     }
250 #else
251     if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
252 #ifdef  AFS_AIX41_ENV
253         if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
254             || (fs.s_version != fsv3pvers)) {
255             Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
256             return -1;
257         }
258 #else
259         Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
260             fs.s_magic, fsv3magic);
261         return -1;
262 #endif
263     }
264 #endif
265
266 #ifdef AFS_AIX41_ENV
267     fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
268     iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
269     ag512 = fragsize * fs.s_agsize / 512;
270     agblocks = fragsize * fs.s_agsize >> BSHIFT;
271 #endif /* AFS_AIX41_ENV */
272
273     fmax = fs.s_fsize / (FSBSIZE / 512);        /* first invalid blk num */
274
275     pfd = afs_open(rdev, O_RDONLY);
276     if (pfd < 0) {
277         Log("Unable to open `%s' inode for reading\n", rdev);
278         return -1;
279     }
280
281     inodeFile = fopen(resultFile, "w");
282     if (inodeFile == NULL) {
283         Log("Unable to create inode description file %s\n", resultFile);
284         goto out;
285     }
286
287     /*
288      * calculate the maximum number of inodes possible
289      */
290 #ifdef AFS_AIX41_ENV
291     imax = iagsize * (fs.s_fsize / ag512) - 1;
292 #else /* AFS_AIX41_ENV */
293     imax =
294         ((fmax / fs.s_agsize +
295           ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
296          * fs.s_agsize) - 1;
297 #endif /* AFS_AIX41_ENV */
298
299     /*
300      * check for "FORCESALVAGE" equivalent:
301      *      LAST_RSVD_I is a vice inode, with dead beef, and
302      *      di_nlink == 2 to indicate the FORCE.
303      */
304     assert(p = ginode(LAST_RSVD_I));
305
306     if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
307         && p->di_nlink == 2) {
308         *forcep = 1;
309         idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
310     }
311
312     for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
313         if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
314             || (p->di_mode & IFMT) != IFREG)
315             continue;
316
317         info.inodeNumber = inum;
318         info.byteCount = p->di_size;
319         info.linkCount = p->di_nlink;
320         info.u.param[0] = p->di_vicep1;
321         info.u.param[1] = p->di_vicep2;
322         info.u.param[2] = p->di_vicep3;
323         info.u.param[3] = p->di_vicep4;
324
325         if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
326             continue;
327
328         if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
329             Log("Error writing inode file for partition %s\n", partition);
330             goto out;
331         }
332         ++ninodes;
333     }
334
335     if (fflush(inodeFile) == EOF) {
336         Log("Unable to successfully flush inode file for %s\n", partition);
337         err = -2;
338         goto out1;
339     }
340     if (fsync(fileno(inodeFile)) == -1) {
341         Log("Unable to successfully fsync inode file for %s\n", partition);
342         err = -2;
343         goto out1;
344     }
345     if (fclose(inodeFile) == EOF) {
346         Log("Unable to successfully close inode file for %s\n", partition);
347         err = -2;
348         goto out1;
349     }
350
351     /*
352      * Paranoia:  check that the file is really the right size
353      */
354     if (stat(resultFile, &status) == -1) {
355         Log("Unable to successfully stat inode file for %s\n", partition);
356         err = -2;
357         goto out1;
358     }
359     if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
360         Log("Wrong size (%d instead of %d) in inode file for %s\n",
361             status.st_size, ninodes * sizeof(struct ViceInodeInfo),
362             partition);
363         err = -2;
364         goto out1;
365     }
366     close(pfd);
367     return 0;
368
369   out:
370     err = -1;
371   out1:
372     if (pfd >= 0)
373         close(pfd);
374     if (inodeFile)
375         fclose(inodeFile);
376
377     return err;
378 }
379
380 /* Read in the superblock for devName */
381 int
382 ReadSuper(struct superblock *fs, char *devName)
383 {
384     int pfd;
385
386     pfd = afs_open(devName, O_RDONLY);
387     if (pfd < 0) {
388         Log("Unable to open inode on %s for reading superblock.\n", devName);
389         return -1;
390     }
391
392     if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
393         Log("Unable to read superblock on %s.\n", devName);
394         return -1;
395     }
396     close(pfd);
397     return (0);
398 }
399
400 #ifdef AFS_AIX42_ENV
401 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
402 int
403 IsBigFilesFileSystem(struct superblock *sb)
404 {
405     if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
406         && (sb->s_version == fsbigfile)
407         && (sb->s_bigexp))
408         return 1;
409     else
410         return 0;
411 }
412 #endif
413
414 struct dinode *
415 ginode(inum)
416 {
417     int ag;
418     daddr_t pblk;
419     struct dinode *dp;
420     static char buf[FSBSIZE];
421     static daddr_t last_blk = -1;
422
423 #ifdef AFS_AIX41_ENV
424     ag = inum / iagsize;
425     pblk =
426         (ag ==
427          0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
428                                                          ag * iagsize) /
429         INOPB;
430 #else /* AFS_AIX41_ENV */
431     ag = inum / fs.s_agsize;
432     pblk =
433         (ag ==
434          0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
435                                                             ag *
436                                                             fs.s_agsize) /
437         INOPB;
438 #endif /* AFS_AIX41_ENV */
439
440     if (last_blk != pblk) {
441         if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
442             last_blk = -1;
443             return 0;
444         }
445         last_blk = pblk;
446     }
447
448     dp = (struct dinode *)buf;
449     dp += itoo(inum);
450     return (dp);
451 }
452
453 #else /* !AFS_AIX31_ENV       */
454
455 #if defined(AFS_SGI_ENV)
456
457 /* libefs.h includes <assert.h>, which we don't want */
458 #define __ASSERT_H__
459
460 #ifdef AFS_SGI_XFS_IOPS_ENV
461 #include <dirent.h>
462 #include <afs/xfsattrs.h>
463 /* xfs_ListViceInodes
464  *
465  * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
466  * the inode information. The name is required for the idec operation to work. 
467  * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
468  * changes, the algorithm will need to change. 
469  * 1) If the parent inode number does not match the directory's inod number,
470  *    change it in the attribute.
471  * 2) If the unqifier in the attribute does not match the name, rename the
472  *    file. This is done by doing an exclusive open, incrementing the tag
473  *    number until a file can be created. If the tag changes, then the
474  *    attribute will need updating.
475  * 3) If the tag in the attribute does not match the name, change the
476  *    attribute.
477  * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
478  *
479  */
480
481 /* xfs_VerifyInode
482  * 
483  * Does the verifications listed above.
484  * We can't change the names until the readdir is complete, so we set the
485  * rename flag if the file needs renaming.
486  */
487 int
488 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
489                 int *rename)
490 {
491     char path[1024];
492     int vno;
493     int update_pino = 0;
494     int update_tag = 0;
495     int update_chown = 0;
496     int retCode = 0;
497     char tmpName[32];
498     b64_string_t stmp;
499     int tag;
500
501     *rename = 0;
502     (void)sprintf(path, "%s/%s", dir, name);
503     /* Verify uid and gid fields */
504     if (info->ili_magic != XFS_VICEMAGIC) {
505         Log("%s  magic for %s/%s (inode %s) from %d to %d\n",
506             Testing ? "Would have changed" : "Changing", dir, name,
507             PrintInode(NULL, info->ili_info.inodeNumber), info->ili_magic,
508             XFS_VICEMAGIC);
509         if (!Testing)
510             update_chown = 1;
511     }
512
513     vno = info->ili_info.param[0];
514     if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
515         Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
516             Testing ? "Would have changed" : "Changing", dir, name,
517             PrintInode(NULL, info->ili_info.inodeNumber), info->ili_vno,
518             AFS_XFS_VNO_CLIP(vno));
519         if (!Testing)
520             update_chown = 1;
521     }
522
523     if (update_chown) {
524         if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
525             Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
526                 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
527             retCode = -1;
528         }
529     }
530
531     /* Check Parent inode number. */
532     if (info->ili_pino != pino) {
533         afs_ino_str_t sino, sipino, spino;
534         (void)PrintInode(sino, info->ili_info.inodeNumber);
535         (void)PrintInode(sipino, info->ili_pino);
536         (void)PrintInode(spino, pino);
537         Log("%s parent ino for %s (inode %s) from %s to %s.\n",
538             Testing ? "Would have changed" : "Changing", path, sino, sipino,
539             spino);
540         if (!Testing)
541             update_pino = 1;
542     }
543
544     /* Verify the file name. */
545     (void)strcpy(tmpName, ".");
546     (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
547     if (strncmp(name, tmpName, strlen(tmpName))) {
548         Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
549             Testing ? "Would have returned bad" : "Bad", name,
550             PrintInode(NULL, info->ili_info.inodeNumber), dir,
551             info->ili_info.param[2], info->ili_tag);
552         if (!Testing)
553             *rename = 1;
554     }
555
556     if (!*rename) {
557         /* update the tag? */
558         (void)strcat(tmpName, ".");
559         (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
560         if (strcmp(name, tmpName)) {
561             char *p;
562             (void)strcpy(tmpName, name);
563             p = strchr(tmpName + 1, '.');
564             if (!p) {
565                 Log("No tag found on name %s (inode %s)in directory, %s.\n",
566                     name, PrintInode(NULL, info->ili_info.inodeNumber), dir,
567                     Testing ? "would have renamed" : "will rename");
568                 if (!Testing)
569                     *rename = 1;
570             } else {
571                 tag = base64_to_int(p + 1);
572                 Log("%s the tag for %s (inode %s) from %d to %d.\n",
573                     Testing ? "Would have changed" : "Will change", path,
574                     PrintInode(NULL, info->ili_info.inodeNumber), dir, tag,
575                     info->ili_tag);
576                 if (!Testing)
577                     update_tag = 1;
578             }
579         }
580     }
581
582     if (update_pino || update_tag) {
583         afs_xfs_attr_t attrs;
584         int length;
585
586         length = SIZEOF_XFS_ATTR_T;
587         if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
588             0) {
589             Log("Can't get AFS attribute for %s\n", path);
590             return -1;
591         }
592         if (update_pino)
593             attrs.at_pino = pino;
594         if (update_tag)
595             attrs.at_tag = tag;
596         if (attr_set
597             (path, AFS_XFS_ATTR, (char *)&attrs, length,
598              ATTR_ROOT | ATTR_REPLACE) < 0) {
599             Log("Can't set AFS attribute into %s\n", path);
600             retCode = -1;
601         }
602     }
603
604     return retCode;
605 }
606
607 typedef struct {
608     int uniq;
609     char name[28];
610 } xfs_Rename_t;
611
612 int
613 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
614 {
615     int i, j;
616     char opath[128], nbase[128], npath[128];
617     afs_xfs_attr_t attrs;
618     int length = SIZEOF_XFS_ATTR_T;
619     b64_string_t stmp;
620     int tag;
621     int fd;
622
623     for (i = 0; i < n_renames; i++) {
624         (void)sprintf(opath, "%s/%s", dir, renames[i].name);
625         (void)sprintf(nbase, "%s/.%s", dir,
626                       int_to_base64(stmp, renames[i].uniq));
627         for (tag = 2, j = 0; j < 64; tag++, j++) {
628             (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
629             fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
630             if (fd > 0) {
631                 close(fd);
632                 break;
633             }
634         }
635         if (j != 64) {
636             Log("Can't find a new name for %s\n", opath);
637             return -1;
638         }
639         if (rename(opath, npath) < 0) {
640             Log("Can't rename %s to %s\n", opath, npath);
641             return -1;
642         }
643         Log("Renamed %s to %s\n", opath, npath);
644         return 0;
645     }
646 }
647
648
649 int
650 xfs_ListViceInodes(char *devname, char *mountedOn, char *resultFile,
651                    int (*judgeInode) (), int judgeParam, int *forcep,
652                    int forceR, char *wpath, void *rock)
653 {
654     FILE *inodeFile = NULL;
655     i_list_inode_t info;
656     int info_size = sizeof(i_list_inode_t);
657     int fd;
658     DIR *top_dirp;
659     dirent64_t *top_direntp;
660     DIR *vol_dirp;
661     dirent64_t *vol_direntp;
662     struct stat64 sdirbuf;
663     struct stat64 sfilebuf;
664     afs_xfs_attr_t attrs;
665     afs_xfs_dattr_t dattrs;
666     int length;
667     char vol_dirname[1024];
668     int ninodes = 0;
669     int code = 0;
670     xfs_Rename_t *renames = (xfs_Rename_t *) 0;
671     int rename;
672 #define N_RENAME_STEP 64
673     int n_renames = 0;
674     int n_avail = 0;
675     uint64_t pino;
676     struct stat status;
677     int errors = 0;
678
679     *forcep = 0;
680
681     if (stat64(mountedOn, &sdirbuf) < 0) {
682         perror("xfs_ListViceInodes: stat64");
683         return -1;
684     }
685
686     inodeFile = fopen(resultFile, "w");
687     if (inodeFile == NULL) {
688         Log("Unable to create inode description file %s\n", resultFile);
689         return -1;
690     }
691
692     if ((top_dirp = opendir(mountedOn)) == NULL) {
693         Log("Can't open directory %s to read inodes.\n", mountedOn);
694         return -1;
695     }
696
697     while (top_direntp = readdir64(top_dirp)) {
698         /* Only descend directories with the AFSDIR attribute set.
699          * Could also verify the contents of the atribute, but currently
700          * they are not used.
701          * Performance could be improved for single volume salvages by
702          * only going through the directory containing the volume's inodes.
703          * But I'm being complete as a first pass.
704          */
705         (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
706         length = SIZEOF_XFS_DATTR_T;
707         if (attr_get
708             (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
709             continue;
710
711         if ((vol_dirp = opendir(vol_dirname)) == NULL) {
712             if (errno == ENOTDIR)
713                 continue;
714             Log("Can't open directory %s to read inodes.\n", vol_dirname);
715             goto err1_exit;
716         }
717
718         pino = top_direntp->d_ino;
719         n_renames = 0;
720         while (vol_direntp = readdir64(vol_dirp)) {
721             if (vol_direntp->d_name[1] == '\0'
722                 || vol_direntp->d_name[1] == '.')
723                 continue;
724
725             info.ili_version = AFS_XFS_ILI_VERSION;
726             info_size = sizeof(i_list_inode_t);
727             code =
728                 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
729                              &info_size);
730             if (code) {
731                 /* Where possible, give more explicit messages. */
732                 switch (errno) {
733                 case ENXIO:
734                 case ENOSYS:
735                     Log("%s (device id %d) is not on an XFS filesystem.\n",
736                         vol_dirname, sdirbuf.st_dev);
737                     goto err1_exit;
738                     break;
739                 case EINVAL:
740                 case E2BIG:
741                     if (info_size != sizeof(i_list_inode_t)
742                         || info.ili_version != AFS_XFS_ILI_VERSION) {
743                         Log("Version skew between kernel and salvager.\n");
744                         goto err1_exit;
745                     }
746                     break;
747                 }
748                 /* Continue, so we collect all the errors in the first pass. */
749                 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
750                     vol_direntp->d_name, strerror(errno));
751                 errors++;
752                 continue;
753             }
754
755             if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
756                 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
757                 goto err1_exit;
758             }
759
760             if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
761                 continue;
762
763             rename = 0;
764             if (xfs_VerifyInode
765                 (vol_dirname, pino, vol_direntp->d_name, &info,
766                  &rename) < 0) {
767                 errors++;
768             }
769
770             if (rename) {
771                 /* Add this name to the list of items to rename. */
772                 if (n_renames >= n_avail) {
773                     n_avail += N_RENAME_STEP;
774                     if (n_avail == N_RENAME_STEP)
775                         renames = (xfs_Rename_t *)
776                             malloc(n_avail * sizeof(xfs_Rename_t));
777                     else
778                         renames = (xfs_Rename_t *)
779                             realloc((char *)renames,
780                                     n_avail * sizeof(xfs_Rename_t));
781                     if (!renames) {
782                         Log("Can't %salloc %lu bytes for rename list.\n",
783                             (n_avail == N_RENAME_STEP) ? "m" : "re",
784                             n_avail * sizeof(xfs_Rename_t));
785                         goto err1_exit;
786                     }
787                 }
788                 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
789                 renames[n_renames].uniq = info.ili_info.param[2];
790                 n_renames++;
791             }
792
793             if (fwrite
794                 (&info.ili_info, sizeof(vice_inode_info_t), 1, inodeFile)
795                 != 1) {
796                 Log("Error writing inode file for partition %s\n", mountedOn);
797                 goto err1_exit;
798             }
799             ninodes++;
800
801         }                       /* end while vol_direntp */
802
803         closedir(vol_dirp);
804         vol_dirp = (DIR *) 0;
805         if (n_renames) {
806             Log("Renaming files.\n");
807             if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
808                 goto err1_exit;
809             }
810         }
811     }
812
813     closedir(top_dirp);
814     if (renames)
815         free((char *)renames);
816     if (fflush(inodeFile) == EOF) {
817         ("Unable to successfully flush inode file for %s\n", mountedOn);
818         fclose(inodeFile);
819         return errors ? -1 : -2;
820     }
821     if (fsync(fileno(inodeFile)) == -1) {
822         Log("Unable to successfully fsync inode file for %s\n", mountedOn);
823         fclose(inodeFile);
824         return errors ? -1 : -2;
825     }
826     if (fclose(inodeFile) == EOF) {
827         Log("Unable to successfully close inode file for %s\n", mountedOn);
828         return errors ? -1 : -2;
829     }
830     /*
831      * Paranoia:  check that the file is really the right size
832      */
833     if (stat(resultFile, &status) == -1) {
834         Log("Unable to successfully stat inode file for %s\n", partition);
835         return errors ? -1 : -2;
836     }
837     if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
838         Log("Wrong size (%d instead of %d) in inode file for %s\n",
839             status.st_size, ninodes * sizeof(struct ViceInodeInfo),
840             partition);
841         return errors ? -1 : -2;
842     }
843
844     if (errors) {
845         Log("Errors encontered listing inodes, not salvaging partition.\n");
846         return -1;
847     }
848
849     return 0;
850
851   err1_exit:
852     if (vol_dirp)
853         closedir(vol_dirp);
854     if (top_dirp)
855         closedir(top_dirp);
856     if (renames)
857         free((char *)renames);
858     if (inodeFile)
859         fclose(inodeFile);
860     return -1;
861 }
862
863 #endif
864
865 int
866 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
867                int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
868                char *wpath, void *rock)
869 {
870     FILE *inodeFile = NULL;
871     char dev[50], rdev[51];
872     struct stat status;
873     struct efs_dinode *p;
874     struct ViceInodeInfo info;
875     struct stat root_inode;
876     int ninodes = 0, err = 0;
877     struct efs_dinode *dinodeBuf = NULL;
878     int last_cgno;
879     ino_t imax, inum;           /* total number of I-nodes in file system */
880
881     *forcep = 0;
882     sync();
883     sleep(1);                   /* simulate operator    */
884     sync();
885     sleep(1);
886     sync();
887     sleep(1);
888
889     if (stat(mountedOn, &root_inode) < 0) {
890         Log("cannot stat: %s\n", mountedOn);
891         return -1;
892     }
893 #ifdef AFS_SGI_XFS_IOPS_ENV
894     if (!strcmp("xfs", root_inode.st_fstype)) {
895         return xfs_ListViceInodes(devname, mountedOn, resultFile, judgeInode,
896                                   judgeParam, forcep, forceR, wpath, rock);
897     } else
898 #endif
899     {
900         Log("%s is not root of a filesystem\n", mountedOn);
901         return -1;
902     }
903 }
904
905 #else /* AFS_SGI_ENV */
906
907 #ifdef AFS_HPUX_ENV
908 #define SPERB   (MAXBSIZE / sizeof(short))
909 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
910
911 struct bufarea {
912     struct bufarea *b_next;     /* must be first */
913     daddr_t b_bno;
914     int b_size;
915     union {
916         char b_buf[MAXBSIZE];   /* buffer space */
917         short b_lnks[SPERB];    /* link counts */
918         daddr_t b_indir[MAXNINDIR];     /* indirect block */
919         struct fs b_fs;         /* super block */
920         struct cg b_cg;         /* cylinder group */
921     } b_un;
922     char b_dirty;
923 };
924 typedef struct bufarea BUFAREA;
925
926 BUFAREA sblk;
927 #define sblock sblk.b_un.b_fs
928 #endif /* AFS_HPUX_ENV */
929
930 extern char *afs_rawname();
931 int
932 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
933                int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
934                char *wpath, void *rock)
935 {
936     union {
937 #ifdef  AFS_AIX_ENV
938         struct filsys fs;
939         char block[BSIZE];
940 #else                           /* !AFS_AIX_ENV */
941         struct fs fs;
942         char block[SBSIZE];
943 #endif
944     } super;
945     int i, c, e, bufsize, code, err = 0;
946     FILE *inodeFile = NULL;
947     char dev[50], rdev[100], err1[512], *ptr1;
948     struct dinode *inodes = NULL, *einodes, *dptr;
949     struct stat status;
950     int ninodes = 0;
951     struct dinode *p;
952     struct ViceInodeInfo info;
953
954     *forcep = 0;
955     partition = mountedOn;
956     sprintf(rdev, "%s/%s", wpath, devname);
957     ptr1 = afs_rawname(rdev);
958     strcpy(rdev, ptr1);
959
960     sync();
961     /* Bletch:  this is terrible;  is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
962 #ifdef  AFS_AIX_ENV
963     sleep(5);                   /* Trying a smaller one for aix */
964 #else
965     sleep(10);
966 #endif
967
968     pfd = afs_open(rdev, O_RDONLY);
969     if (pfd <= 0) {
970         sprintf(err1, "Could not open device %s to get inode list\n", rdev);
971         perror(err1);
972         return -1;
973     }
974 #ifdef  AFS_AIX_ENV
975     if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
976 #else
977 #ifdef AFS_HPUX_ENV
978     if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
979 #else
980     if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
981 #endif /* AFS_HPUX_ENV */
982 #endif
983         Log("Unable to read superblock, partition %s\n", partition);
984         goto out;
985     }
986
987     inodeFile = fopen(resultFile, "w");
988     if (inodeFile == NULL) {
989         Log("Unable to create inode description file %s\n", resultFile);
990         goto out;
991     }
992 #ifdef  AFS_AIX_ENV
993     /*
994      * char *FSlabel(), *fslabel=0;
995      * fslabel = FSlabel(&super.fs);
996      */
997     if (super.fs.s_bsize == 0)
998         super.fs.s_bsize = 512;
999     if (super.fs.s_bsize != BSIZE) {
1000         Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
1001         goto out;
1002     }
1003     fmax = super.fs.s_fsize;    /* first invalid blk num */
1004     imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
1005     if (imax == 0) {
1006         Log("Size check: imax==0!\n");
1007         goto out;
1008     }
1009     if (GetAuxInodeFile(partition, &status) == 0) {
1010         Log("Can't access Aux inode file for partition %s, aborting\n",
1011             partition);
1012         goto out;
1013     }
1014     for (inum = 1; inum <= imax; inum++) {
1015         struct dauxinode *auxp;
1016         if ((auxp = IsAfsInode(inum)) == NULL) {
1017             /* Not an afs inode, keep going */
1018             continue;
1019         }
1020         if ((p = ginode(inum)) == NULL)
1021             continue;
1022         /* deleted/non-existent inode when di_mode == 0 */
1023         if (!p->di_mode)
1024             continue;
1025         info.inodeNumber = (int)inum;
1026         info.byteCount = p->di_size;
1027         info.linkCount = p->di_nlink;
1028         info.u.param[0] = auxp->aux_param1;
1029         info.u.param[1] = auxp->aux_param2;
1030         info.u.param[2] = auxp->aux_param3;
1031         info.u.param[3] = auxp->aux_param4;
1032         if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1033             continue;
1034         if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1035             Log("Error writing inode file for partition %s\n", partition);
1036             goto out;
1037         }
1038         ninodes++;
1039     }
1040 #else
1041     /*
1042      * run a few consistency checks of the superblock
1043      * (Cribbed from vfsck)
1044      */
1045 #ifdef AFS_HPUX_ENV
1046 #if defined(FD_FSMAGIC)
1047     if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
1048         && (sblock.fs_magic != FD_FSMAGIC)
1049 #if     defined(AFS_HPUX101_ENV)
1050         && (sblock.fs_magic != FD_FSMAGIC_2)
1051 #endif
1052         ) {
1053 #else
1054     if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
1055 #endif
1056         Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
1057         goto out;
1058     }
1059     if (sblock.fs_ncg < 1) {
1060         Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
1061         goto out;
1062     }
1063     if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
1064         Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
1065         goto out;
1066     }
1067     if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
1068         || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
1069         Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
1070         goto out;
1071     }
1072     if (sblock.fs_sbsize > SBSIZE) {
1073         Log("There's something wrong with the superblock for partition %s; bsize too large (%d vs. %d) run vfsck\n", partition, sblock.fs_sbsize, sblock.fs_bsize);
1074         goto out;
1075     }
1076 #else
1077     if ((super.fs.fs_magic != FS_MAGIC)
1078         || (super.fs.fs_ncg < 1)
1079 #if     defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1080         || (super.fs.fs_cpg < 1)
1081 #else
1082         || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1083 #endif
1084         || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1085             || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1086         || (super.fs.fs_sbsize > SBSIZE)) {
1087         Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1088         goto out;
1089     }
1090 #endif /* AFS_HPUX_ENV */
1091
1092 #ifdef AFS_HPUX_ENV
1093     bufsize = sblock.fs_ipg * sizeof(struct dinode);
1094 #else
1095     bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1096 #endif /* AFS_HPUX_ENV */
1097     inodes = (struct dinode *)malloc(bufsize);
1098     einodes = (struct dinode *)(((char *)inodes) + bufsize);
1099     if (inodes == NULL) {
1100         Log("Unable to allocate enough memory to scan inodes; help!\n");
1101         goto out;
1102     }
1103     Log("Scanning inodes on device %s...\n", rdev);
1104 #ifdef AFS_HPUX_ENV
1105     for (c = 0; c < sblock.fs_ncg; c++) {
1106         i = c * sblock.fs_ipg;
1107         e = i + sblock.fs_ipg;
1108 #if     defined(AFS_HPUX102_ENV)
1109         if (afs_lseek(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1110             -1) {
1111 #else
1112         if (afs_lseek(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1113             -1) {
1114 #endif
1115 #else
1116     for (c = 0; c < super.fs.fs_ncg; c++) {
1117         daddr_t dblk1;
1118 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1119         daddr_t f1;
1120 #if defined(AFS_DARWIN_ENV)
1121 #define offset_t off_t
1122 #define llseek lseek
1123 #endif
1124         offset_t off;
1125 #endif /* AFS_SUN5_ENV */
1126         i = c * super.fs.fs_ipg;
1127         e = i + super.fs.fs_ipg;
1128 #ifdef  AFS_OSF_ENV
1129         dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
1130         if (afs_lseek(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
1131 #else
1132 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1133         f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1134         off = (offset_t) f1 << DEV_BSHIFT;
1135         if (llseek(pfd, off, L_SET) == -1) {
1136 #else
1137         if (afs_lseek(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1138             == -1) {
1139 #endif /* AFS_SUN5_ENV */
1140 #endif /* AFS_OSF_ENV */
1141 #endif /* AFS_HPUX_ENV */
1142             Log("Error reading inodes for partition %s; run vfsck\n",
1143                 partition);
1144             goto out;
1145         }
1146         while (i < e) {
1147             if (!forceR) {
1148                 if (read(pfd, inodes, bufsize) != bufsize) {
1149                     Log("Error reading inodes for partition %s; run vfsck\n",
1150                         partition);
1151                     goto out;
1152                 }
1153             } else {
1154                 register int bj, bk;
1155                 dptr = inodes;
1156                 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1157                     if ((code = read(pfd, dptr, 512)) != 512) {
1158                         Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1159                         if (afs_lseek(pfd, 512, L_SET) == -1) {
1160                             Log("Lseek failed\n");
1161                             goto out;
1162                         }
1163                         dptr->di_mode = 0;
1164                         dptr++;
1165                         dptr->di_mode = 0;
1166                         dptr++;
1167                         dptr->di_mode = 0;
1168                         dptr++;
1169                         dptr->di_mode = 0;
1170                         dptr++;
1171                     } else
1172                         dptr += 4;
1173                 }
1174             }
1175             for (p = inodes; p < einodes && i < e; i++, p++) {
1176 #ifdef notdef
1177                 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1178                     i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1179                     p->di_size, p->di_nlink);
1180                 printf
1181                     ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1182                      i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1183                      p->di_size, p->di_nlink);
1184 #endif
1185 #ifdef AFS_OSF_ENV
1186 #ifdef AFS_3DISPARES
1187                 /* Check to see if this inode is a pre-"OSF1 4.0D" inode */
1188                 if ((p->di_uid || p->di_gid)
1189                     && !(p->di_flags & (IC_XUID | IC_XGID))) {
1190                     Log("Found unconverted inode %d: Use 'fs_conv_dux40D convert' on partition %s\n", i, partition);
1191                     goto out;
1192                 }
1193 #else
1194                 assert(0);      /* define AFS_3DISPARES in param.h */
1195 #endif
1196 #endif
1197 #if     defined(AFS_SUN56_ENV)
1198                 /* if this is a pre-sol2.6 unconverted inode, bail out */
1199                 {
1200                     afs_uint32 p1, p2, p3, p4;
1201                     int p5;
1202                     quad *q;
1203
1204                     q = (quad *) & (p->di_ic.ic_lsize);
1205                     p1 = p->di_gen;
1206                     p2 = p->di_ic.ic_flags;
1207                     p3 = q->val[0];
1208                     p4 = p->di_ic.ic_uid;
1209                     p5 = p->di_ic.ic_gid;
1210
1211                     if ((p2 || p3) && !p4 && (p5 == -2)) {
1212                         Log("Found unconverted inode %d\n", i);
1213                         Log("You should run the AFS file conversion utility\n");
1214                         goto out;
1215                     }
1216                 }
1217 #endif
1218                 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1219                     afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1220
1221                     info.u.param[0] = p->di_vicep1;
1222 #ifdef  AFS_3DISPARES
1223                     if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1224                         info.u.param[1] = INODESPECIAL;
1225                         info.u.param[2] = p3;
1226                         info.u.param[3] = p2 & 0x3;
1227                     } else {
1228                         info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1229                         info.u.param[2] = (p2 & 0x3fffff);
1230                         info.u.param[3] =
1231                             (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1232                     }
1233 #else
1234                     info.u.param[1] = p->di_vicep2;
1235                     info.u.param[2] = DI_VICEP3(p);
1236                     info.u.param[3] = p->di_vicep4;
1237 #endif
1238                     info.inodeNumber = i;
1239                     info.byteCount = p->di_size;
1240                     info.linkCount = p->di_nlink;
1241                     if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1242                         continue;
1243                     if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1244                         Log("Error writing inode file for partition %s\n",
1245                             partition);
1246                         goto out;
1247                     }
1248                     ninodes++;
1249                 }
1250             }
1251         }
1252     }
1253     if (inodes)
1254         free(inodes);
1255 #endif
1256     if (fflush(inodeFile) == EOF) {
1257         Log("Unable to successfully flush inode file for %s\n", partition);
1258         err = -2;
1259         goto out1;
1260     }
1261     if (fsync(fileno(inodeFile)) == -1) {
1262         Log("Unable to successfully fsync inode file for %s\n", partition);
1263         err = -2;
1264         goto out1;
1265     }
1266     if (fclose(inodeFile) == EOF) {
1267         Log("Unable to successfully close inode file for %s\n", partition);
1268         err = -2;
1269         goto out1;
1270     }
1271
1272     /*
1273      * Paranoia:  check that the file is really the right size
1274      */
1275     if (stat(resultFile, &status) == -1) {
1276         Log("Unable to successfully stat inode file for %s\n", partition);
1277         err = -2;
1278         goto out1;
1279     }
1280     if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1281         Log("Wrong size (%d instead of %d) in inode file for %s\n",
1282             status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1283             partition);
1284         err = -2;
1285         goto out1;
1286     }
1287     close(pfd);
1288     return 0;
1289
1290   out:
1291     err = -1;
1292   out1:
1293     close(pfd);
1294     if (inodeFile)
1295         fclose(inodeFile);
1296     if (inodes)
1297         free(inodes);
1298     return err;
1299 }
1300 #endif /* !AFS_SGI_ENV */
1301 #endif /* !AFS_AIX31_ENV       */
1302
1303 #ifdef AFS_DARWIN_ENV
1304 #undef dbtob
1305 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1306 #endif
1307
1308 int
1309 bread(int fd, char *buf, daddr_t blk, afs_int32 size)
1310 {
1311 #ifdef  AFS_AIX_ENV
1312 #ifdef  AFS_AIX41_ENV
1313     offset_t off = (offset_t) blk << FSBSHIFT;
1314     if (llseek(fd, off, 0) < 0) {
1315         Log("Unable to seek to offset %llu for block %u\n", off, blk);
1316         return -1;
1317     }
1318 #else /* AFS_AIX41_ENV */
1319     if (afs_lseek(fd, blk * Bsize, 0) < 0) {
1320         Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1321     }
1322 #endif /* AFS_AIX41_ENV */
1323 #else
1324     if (afs_lseek(fd, (off_t) dbtob(blk), L_SET) < 0) {
1325         Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1326     }
1327 #endif
1328     if (read(fd, buf, size) != size) {
1329         Log("Unable to read block %d, partition %s\n", blk, partition);
1330         return -1;
1331     }
1332     return 0;
1333 }
1334
1335 #endif /* AFS_LINUX20_ENV */
1336 #endif /* AFS_NAMEI_ENV */