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