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