3432719e2129761fa5dbb0581d029f4f1877d8ca
[openafs.git] / src / vol / namei_ops.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 /* I/O operations for the Unix open by name (namei) interface. */
11
12 #include <afs/param.h>
13 #ifdef AFS_NAMEI_ENV
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <afs/assert.h>
21 #include <string.h>
22 #include <sys/file.h>
23 #include <sys/param.h>
24 #include <lock.h>
25 #include <afs/afsutil.h>
26 #include <lwp.h>
27 #include "nfs.h"
28 #include <afs/afsint.h>
29 #include "ihandle.h"
30 #include "vnode.h"
31 #include "volume.h"
32 #include "viceinode.h"
33 #include "voldefs.h"
34 #include "partition.h"
35
36 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
37
38 int namei_iread(IHandle_t *h, int offset, char *buf, int size)
39 {
40     int nBytes;
41     FdHandle_t *fdP;
42
43     fdP = IH_OPEN(h);
44     if (fdP == NULL)
45         return -1;
46
47     if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
48         FDH_REALLYCLOSE(fdP);
49         return -1;
50     }
51
52     nBytes = FDH_READ(fdP, buf, size);
53     FDH_CLOSE(fdP);
54     return nBytes;
55 }
56
57 int namei_iwrite(IHandle_t *h, int offset, char *buf, int size)
58 {
59     int nBytes;
60     FdHandle_t *fdP;
61
62     fdP = IH_OPEN(h);
63     if (fdP == NULL)
64         return -1;
65
66     if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
67         FDH_REALLYCLOSE(fdP);
68         return -1;
69     }
70     nBytes = FDH_WRITE(fdP, buf, size);
71     FDH_CLOSE(fdP);
72     return nBytes;
73 }
74
75
76
77 /* Inode number format:
78  * low 26 bits - vnode number - all 1's if volume special file.
79  * next 3 bits - tag 
80  * next 3 bits spare (0's)
81  * high 32 bits - uniquifier (regular) or type if spare
82  */
83 #define NAMEI_VNODEMASK    0x003ffffff
84 #define NAMEI_TAGMASK      0x7
85 #define NAMEI_TAGSHIFT     26
86 #define NAMEI_UNIQMASK     0xffffffff
87 #define NAMEI_UNIQSHIFT    32
88 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
89 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
90
91 /* dir1 is the high 8 bits of the 26 bit vnode */
92 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
93 /* dir2 is the next 9 bits */
94 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
95 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
96
97 #define NAMEI_SPECDIR "special"
98 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
99
100 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
101                       * group, not counting temporary (move) volumes.
102                       * This is the number of separate files, all having
103                       * the same vnode number, which can occur in a volume
104                       * group at once.
105                       */
106
107                        
108 typedef struct {
109     int ogm_owner;
110     int ogm_group;
111     int ogm_mode;
112 } namei_ogm_t;
113
114 int namei_SetLinkCount(FdHandle_t *h, Inode ino, int count, int locked);
115 static int GetFreeTag(IHandle_t *ih, int vno);
116
117 /* namei_HandleToInodeDir
118  *
119  * Construct the path name of the directory holding the inode data.
120  * Format: /<vicepx>/INODEDIR
121  *
122  */
123 #define PNAME_BLEN 64
124 static void namei_HandleToInodeDir(namei_t *name, IHandle_t *ih)
125 {
126     char *tmp = name->n_base;
127
128     memset(name, '\0', sizeof(*name));
129
130     (void) volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
131     tmp += VICE_PREFIX_SIZE;
132     tmp += ih->ih_dev > 25 ? 2 : 1;
133     *tmp = '/'; tmp ++;
134     (void) strcpy(tmp, INODEDIR);
135     (void) strcpy(name->n_path, name->n_base);
136 }
137
138 #define addtoname(N, C) \
139 do { \
140          strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
141 } while(0)
142
143
144 static void namei_HandleToVolDir(namei_t *name, IHandle_t *ih)
145 {
146     lb64_string_t tmp;
147
148     namei_HandleToInodeDir(name, ih);
149     (void) int32_to_flipbase64(tmp, (int64_t)(ih->ih_vid & 0xff));
150     (void) strcpy(name->n_voldir1, tmp);
151     addtoname(name, name->n_voldir1);
152     (void) int32_to_flipbase64(tmp, (int64_t)ih->ih_vid);
153     (void) strcpy(name->n_voldir2, tmp);
154     addtoname(name, name->n_voldir2);
155 }
156
157 /* namei_HandleToName
158  *
159  * Constructs a file name for the fully qualified handle.
160  * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
161  */
162 void namei_HandleToName(namei_t *name, IHandle_t *ih)
163 {
164     lb64_string_t str;
165     int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
166     int tmp;
167         
168     namei_HandleToVolDir(name, ih);
169
170     if (vno == NAMEI_VNODESPECIAL) {
171         (void) strcpy(name->n_dir1, NAMEI_SPECDIR);
172         addtoname(name, name->n_dir1);
173         name->n_dir2[0] = '\0';
174     }
175     else {
176         (void) int32_to_flipbase64(str, VNO_DIR1(vno));
177         (void) strcpy(name->n_dir1, str);
178         addtoname(name, name->n_dir1);
179         (void) int32_to_flipbase64(str, VNO_DIR2(vno));
180         (void) strcpy(name->n_dir2, str);
181         addtoname(name, name->n_dir2);
182     }
183     (void) int64_to_flipbase64(str, (int64_t)ih->ih_ino);
184     (void) strcpy(name->n_inode, str);
185     addtoname(name, name->n_inode);
186 }
187
188 /* The following is a warning to tell sys-admins to not muck about in this
189  * name space.
190  */
191 #define VICE_README "These files and directories are a part of the AFS \
192 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
193 int namei_ViceREADME(char *partition)
194 {
195    char filename[32];
196    int fd;
197
198    sprintf(filename, "%s/%s/README", partition, INODEDIR);
199    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
200    if (fd >= 0) {
201       write(fd, VICE_README, strlen(VICE_README));
202       close(fd);
203    }
204    return(errno);
205 }
206
207
208 #define create_dir() \
209 do { \
210     if (mkdir(tmp, 0700)<0) { \
211         if (errno != EEXIST) \
212             return -1; \
213     } \
214     else { \
215         *created = 1; \
216     } \
217 } while (0) 
218
219 #define create_nextdir(A) \
220 do { \
221          strcat(tmp, "/"); strcat(tmp, A); create_dir();  \
222 } while(0)
223
224 /* namei_CreateDataDirectories
225  *
226  * If creating the file failed because of ENOENT or ENOTDIR, try
227  * creating all the directories first.
228  */
229 static int namei_CreateDataDirectories(namei_t *name, int *created)
230 {
231     char tmp[256];
232     char *s;
233     int i;
234
235     *created = 0;
236
237     (void) strcpy(tmp, name->n_base);
238     create_dir();
239
240     create_nextdir(name->n_voldir1);
241     create_nextdir(name->n_voldir2);
242     create_nextdir(name->n_dir1);
243     if (name->n_dir2[0]) {
244         create_nextdir(name->n_dir2);
245     }
246     return 0;
247 }       
248
249 /* delTree(): Deletes an entire tree of directories (no files)
250  * Input:
251  *   root : Full path to the subtree. Should be big enough for PATH_MAX
252  *   tree : the subtree to be deleted is rooted here. Specifies only the
253  *          subtree beginning at tree (not the entire path). It should be
254  *          a pointer into the "root" buffer.
255  * Output:
256  *  errp : errno of the first error encountered during the directory cleanup.
257  *         *errp should have been initialized to 0.
258  * 
259  * Return Values:
260  *  -1  : If errors were encountered during cleanup and error is set to 
261  *        the first errno.
262  *   0  : Success.
263  *
264  * If there are errors, we try to work around them and delete as many
265  * directories as possible. We don't attempt to remove directories that still
266  * have non-dir entries in them.
267  */
268 static int
269 delTree(char *root, char *tree, int *errp)
270 {
271   char *cp;
272   DIR *ds;
273   struct dirent *dirp;
274   struct stat st;
275   int er;
276
277   if (*tree) {
278     /* delete the children first */
279     cp = strchr(tree, '/');
280     if (cp) {
281       delTree(root, cp+1, errp);
282       *cp = '\0';
283     }
284     else
285       cp = tree + strlen(tree);  /* move cp to the end of string tree */
286     
287     /* now delete all entries in this dir */
288     if ( (ds = opendir(root)) != (DIR *)NULL) {
289       errno = 0;
290       while (dirp = readdir(ds)) {
291         /* ignore . and .. */
292         if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
293           continue;
294         /* since root is big enough, we reuse the space to
295          * concatenate the dirname to the current tree 
296          */
297         strcat(root, "/");
298         strcat(root, dirp->d_name);
299         if (  stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
300           /* delete this subtree */
301           delTree(root, cp+1, errp); 
302         } else
303           *errp = *errp ? *errp : errno;
304           
305         /* recover path to our cur tree by truncating it to 
306          * its original len 
307          */
308         *cp = 0; 
309       }
310       if (!errno)
311         closedir(ds);
312     } 
313     
314     /* finally axe the current dir */
315     if (rmdir(root))
316       *errp = *errp ? *errp : errno;
317
318 #ifndef AFS_PTHREAD_ENV   /* let rx get some work done */
319     IOMGR_Poll();
320 #endif /* !AFS_PTHREAD_ENV */
321
322   } /* if valid tree */
323   
324   /* if we encountered errors during cleanup, we return a -1 */
325   if (*errp)
326     return -1;
327
328   return 0;
329   
330 }
331
332 /* namei_RemoveDataDirectories
333  * Return Values:
334  * Returns 0 on success.
335  * Returns -1 on error. Typically, callers ignore this error bcause we
336  * can continue running if the removes fail. The salvage process will
337  * finish tidying up for us. We only use the n_base and n_voldir1 entries
338  * and only do rmdir's.
339  */
340
341 static int namei_RemoveDataDirectories(namei_t *name)
342 {
343       char pbuf[MAXPATHLEN], *path = pbuf;
344       int prefixlen = strlen(name->n_base), err = 0;
345       
346       strcpy(path, name->n_path);
347
348       /* move past the prefix */
349       path = path+prefixlen+1; /* skip over the trailing / */
350
351       /* now delete all dirs upto path */
352       return delTree(pbuf, path, &err);
353       
354 }       
355
356 /* Create the file in the name space.
357  *
358  * Parameters stored as follows:
359  * Regular files:
360  * p1 - volid - implied in containing directory.
361  * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
362  * p3 - uniq -- bits 4-0 are in mode bits 4-0
363  * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
364  * Special files:
365  * p1 - volid - creation time - dwHighDateTime
366  * p2 - vnode - -1 means special, file goes in "S" subdirectory.
367  * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
368  * p4 - parid - parent volume id - implied in containing directory.
369  *
370  * Return value is the inode number or (Inode)-1 if error.
371  * We "know" there is only one link table, so return EEXIST if there already
372  * is a link table. It's up to the calling code to test errno and increment
373  * the link count.
374  */
375
376 /* namei_MakeSpecIno
377  *
378  * This function is called by VCreateVolume to hide the implementation
379  * details of the inode numbers. This only allows for 7 volume special
380  * types, but if we get that far, this could should be dead by then.
381  */
382 Inode namei_MakeSpecIno(int volid, int type)
383 {
384     Inode ino;
385     ino = NAMEI_INODESPECIAL;
386     type &= NAMEI_TAGMASK;
387     ino |= ((Inode)type) << NAMEI_TAGSHIFT;
388     ino |= ((Inode)volid) << NAMEI_UNIQSHIFT;
389     return ino;
390 }
391
392 /* SetOGM - set owner group and mode bits from parm and tag
393  *
394  * owner - low 15 bits of parm.
395  * group - next 15 bits of parm.
396  * mode - 2 bits of parm, then lowest = 3 bits of tag.
397  */
398 static int SetOGM(int fd, int parm, int tag)
399 {
400     int owner, group, mode;
401
402     owner = parm & 0x7fff;
403     group = (parm >> 15) & 0x7fff;
404     if (fchown(fd, owner, group)<0)
405         return -1;
406
407     mode = (parm >> 27) & 0x18;
408     mode |= tag & 0x7;
409     if (fchmod(fd, mode)<0)
410         return -1;
411
412     return 0;
413
414 }
415
416 /* GetOGM - get parm and tag from owner, group and mode bits. */
417 static void GetOGMFromStat(struct stat *status, int *parm, int *tag)
418 {
419     int tmp;
420
421     *parm = status->st_uid | (status->st_gid << 15);
422     *parm |= (status->st_mode & 0x18) << 27;
423     *tag = status->st_mode & 0x7;
424 }
425
426 static int GetOGM(int fd, int *parm, int *tag)
427 {
428     struct stat status;
429     if (fstat(fd, &status)<0) 
430         return -1;
431
432     GetOGMFromStat(&status, parm, tag);
433     return 0;
434 }
435
436 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
437
438 /* Derive the name and create it O_EXCL. If that fails we have an error.
439  * Get the tag from a free column in the link table.
440  */
441 Inode namei_icreate(IHandle_t *lh, char *part, int p1, int p2, int p3, int p4)
442 {
443     namei_t name;
444     namei_ogm_t ogm;
445     b32_string_t str1;
446     char *p;
447     int i;
448     int fd = -1;
449     int code = 0;
450     int created_dir = 0;
451     IHandle_t tmp;
452     FdHandle_t *fdP;
453     FdHandle_t tfd;
454     int save_errno;
455     int tag;
456     int ogm_parm;
457     
458
459     memset((void*)&tmp, 0, sizeof(IHandle_t));
460
461
462     tmp.ih_dev = volutil_GetPartitionID(part);
463     if (tmp.ih_dev == -1) {
464         errno = EINVAL;
465         return -1;
466     }
467
468     if (p2 == -1 ) {
469         /* Parameters for special file:
470          * p1 - volume id - goes into owner/group/mode
471          * p2 - vnode == -1
472          * p3 - type
473          * p4 - parent volume id
474          */
475         ogm_parm = p1;
476         tag = p3;
477
478         tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
479         tmp.ih_ino = namei_MakeSpecIno(p1, p3);
480     }
481     else {
482         int vno = p2 & NAMEI_VNODEMASK; 
483         /* Parameters for regular file:
484          * p1 - volume id
485          * p2 - vnode
486          * p3 - uniq
487          * p4 - dv
488          */
489
490         if (vno != p2) {
491             big_vno ++;
492             errno = EINVAL;
493             return -1;
494         }
495         /* If GetFreeTag succeeds, it atomically sets link count to 1. */
496         tag = GetFreeTag(lh, p2);
497         if (tag < 0)
498             goto bad;
499
500         /* name is <uniq(p3)><tag><vno(p2)> */
501         tmp.ih_vid = p1;
502         tmp.ih_ino = (Inode)p2;
503         tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
504         tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
505
506         ogm_parm = p4;
507     }
508
509     namei_HandleToName(&name, &tmp);
510     fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
511     if (fd < 0) {
512         if (errno == ENOTDIR || errno == ENOENT) {
513             if (namei_CreateDataDirectories(&name, &created_dir)<0)
514                 goto bad;
515             fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
516             if (fd < 0)
517                 goto bad;
518         }
519         else {
520             goto bad;
521         }
522     }
523     if (SetOGM(fd, ogm_parm, tag)<0) {
524         close(fd);
525         fd = -1;
526         goto bad;
527     }
528
529     if (p2 == -1 && p3 == VI_LINKTABLE) {
530         /* hack at tmp to setup for set link count call. */
531         tfd.fd_fd = fd;
532         code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
533     }
534
535 bad:
536     if (fd >= 0)
537         close(fd);
538
539
540     if (code || (fd < 0)) {
541         if (p2 != -1) {
542             fdP = IH_OPEN(lh);
543             if (fdP) {
544                 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
545                 FDH_CLOSE(fdP);
546             }
547         }
548     }
549     return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
550 }
551
552
553 /* namei_iopen */
554 int namei_iopen(IHandle_t *h)
555 {
556     int fd;
557     namei_t name;
558
559     /* Convert handle to file name. */
560     namei_HandleToName(&name, h);
561     fd = open(name.n_path, O_RDWR, 0666);
562     return fd;
563 }
564
565 /* Need to detect vol special file and just unlink. In those cases, the
566  * handle passed in _is_ for the inode. We only check p1 for the special
567  * files.
568  */
569 int namei_dec(IHandle_t *ih, Inode ino, int p1)
570 {
571     int count = 0;
572     namei_t name;
573     int code = 0;
574     FdHandle_t *fdP;
575
576     if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
577         IHandle_t *tmp;
578         int was_closed = 0;
579         int inode_p1, tag;
580         int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
581
582         /* Verify this is the right file. */
583         IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
584
585         fdP = IH_OPEN(tmp);
586         if (fdP == NULL) {
587             IH_RELEASE(tmp);
588             errno = EINVAL;
589             return -1;
590         }
591
592         if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
593             FDH_REALLYCLOSE(fdP);
594             IH_RELEASE(tmp);
595             errno = EINVAL;
596             return -1;
597         }
598         
599         /* If it's the link table itself, decrement the link count. */
600         if (type == VI_LINKTABLE) {
601             if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
602                 FDH_REALLYCLOSE(fdP);
603                 IH_RELEASE(tmp);
604                 return -1;
605             }
606
607             count --;
608             if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
609                 FDH_REALLYCLOSE(fdP);
610                 IH_RELEASE(tmp);
611                 return -1;
612             }
613
614             if (count>0) {
615                 FDH_REALLYCLOSE(fdP);
616                 IH_RELEASE(tmp);
617                 return 0;
618             }
619         }
620         
621         namei_HandleToName(&name, tmp);
622         if ((code = unlink(name.n_path)) == 0) {
623             if (type == VI_LINKTABLE) {
624                 /* Try to remove directory. If it fails, that's ok.
625                  * Salvage will clean up.
626                  */
627                 (void) namei_RemoveDataDirectories(&name);
628             }
629         }
630         FDH_REALLYCLOSE(fdP);
631         IH_RELEASE(tmp);
632     }
633     else {
634         /* Get a file descriptor handle for this Inode */
635         fdP = IH_OPEN(ih);
636         if (fdP == NULL) {
637             return -1;
638         }
639
640         if ((count = namei_GetLinkCount(fdP, ino,  1))<0) {
641             FDH_REALLYCLOSE(fdP);
642             return -1;
643         }
644
645         count --;
646         if (count >= 0) {
647             if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
648                 FDH_REALLYCLOSE(fdP);
649                 return -1;
650             }
651         }
652         if (count == 0 ) {
653             IHandle_t th = *ih;
654             th.ih_ino = ino;
655             namei_HandleToName(&name, &th);
656             code = unlink(name.n_path);
657         }
658         FDH_CLOSE(fdP);
659     }
660
661     return code;
662 }
663
664 int namei_inc(IHandle_t *h, Inode ino, int p1)
665 {
666     int count;
667     int code = 0;
668     FdHandle_t *fdP;
669
670     if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
671         int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
672         if (type != VI_LINKTABLE)
673             return 0;
674         ino = (Inode)0;
675     }
676
677     /* Get a file descriptor handle for this Inode */
678     fdP = IH_OPEN(h);
679     if (fdP == NULL) {
680         return -1;
681     }
682
683     if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
684         code = -1;
685     else {
686         count ++;
687         if (count > 7) {
688             errno = EINVAL;
689             code = -1;
690             count = 7;
691         }
692         if (namei_SetLinkCount(fdP, ino, count, 1)<0)
693             code = -1;
694     }
695     if (code) {
696         FDH_REALLYCLOSE(fdP);
697     } else {
698         FDH_CLOSE(fdP);
699     }
700     return code;
701 }
702
703 /************************************************************************
704  * File Name Structure
705  ************************************************************************
706  *
707  * Each AFS file needs a unique name and it needs to be findable with
708  * minimal lookup time. Note that the constraint on the number of files and
709  * directories in a volume is the size of the vnode index files and the
710  * max file size AFS supports (for internal files) of 2^31. Since a record
711  * in the small vnode index file is 64 bytes long, we can have at most
712  * (2^31)/64 or 33554432 files. A record in the large index file is
713  * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
714  * Another layout parameter is that there is roughly a 16 to 1 ratio between
715  * the number of files and the number of directories.
716  *
717  * Using this information we can see that a layout of 256 directories, each
718  * with 512 subdirectories and each of those having 512 files gives us
719  * 256*512*512 = 67108864 AFS files and directories. 
720  *
721  * The volume, vnode, uniquifier and data version, as well as the tag
722  * are required, either for finding the file or for salvaging. It's best to 
723  * restrict the name to something that can be mapped into 64 bits so the
724  * "Inode" is easily comparable (using "==") to other "Inodes". The tag
725  * is used to distinguish between different versions of the same file
726  * which are currently in the RW and clones of a volume. See "Link Table
727  * Organization" below for more information on the tag. The tag is 
728  * required in the name of the file to ensure a unique name. 
729  *
730  * We can store data in the uid, gid and mode bits of the files, provided
731  * the directories have root only access. This gives us 15 bits for each
732  * of uid and gid (GNU chown considers 65535 to mean "don't change").
733  * There are 9 available mode bits. Adn we need to store a total of 
734  * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
735  * or 131 bits somewhere. 
736  *
737  * The format of a file name for a regular file is:
738  * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
739  * V1 - low 8 bits of RW volume id
740  * V2 - all bits of RW volume id
741  * AA - high 8 bits of vnode number.
742  * BB - next 9 bits of vnode number.
743  * <tag><uniq><vno> - file name
744  *
745  * Volume special files are stored in a separate directory:
746  * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
747  *
748  *
749  * The vnode is hashed into the directory using the high bits of the
750  * vnode number. This is so that consecutively created vnodes are in
751  * roughly the same area on the disk. This will at least be optimal if
752  * the user is creating many files in the same AFS directory. The name
753  * should be formed so that the leading characters are different as quickly
754  * as possible, leading to faster discards of incorrect matches in the
755  * lookup code.
756  * 
757  */
758
759
760 /************************************************************************
761  *  Link Table Organization
762  ************************************************************************
763  *
764  * The link table volume special file is used to hold the link counts that
765  * are held in the inodes in inode based AFS vice filesystems. For user
766  * space access, the link counts are being kept in a separate
767  * volume special file. The file begins with the usual version stamp
768  * information and is then followed by one row per vnode number. vnode 0
769  * is used to hold the link count of the link table itself. That is because
770  * the same link table is shared among all the volumes of the volume group
771  * and is deleted only when the last volume of a volume group is deleted.
772  *
773  * Within each row, the columns are 3 bits wide. They can each hold a 0 based
774  * link count from 0 through 7. Each colume represents a unique instance of
775  * that vnode. Say we have a file shared between the RW and a RO and a
776  * different version of the file (or a different uniquifer) for the BU volume.
777  * Then one column would be holding the link count of 2 for the RW and RO
778  * and a different column would hold the link count of 1 for the BU volume.
779  * Note that we allow only 5 volumes per file, giving 15 bits used in the
780  * short.
781  */
782 #define LINKTABLE_WIDTH 2
783 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
784
785 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
786 {
787     int toff = (int) (ino & NAMEI_VNODEMASK);
788     int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
789
790     *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
791     *index = (tindex << 1) + tindex;
792 }
793
794
795 /* namei_GetLinkCount
796  * If lockit is set, lock the file and leave it locked upon a successful
797  * return.
798  */
799 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
800 {
801     unsigned short row = 0;
802     int junk;
803     int offset, index;
804
805     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
806
807     if (lockit) {
808         if (flock(h->fd_fd, LOCK_EX)<0)
809             return -1;
810     }
811
812     if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
813         goto bad_getLinkByte;
814     
815     if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
816         goto bad_getLinkByte;
817     }
818         
819     return (int) ((row >> index) & NAMEI_TAGMASK);
820
821  bad_getLinkByte:
822     if (lockit)
823         flock(h->fd_fd, LOCK_UN);
824     return -1;
825 }
826
827 /* Return a free column index for this vnode. */
828 static int GetFreeTag(IHandle_t *ih, int vno)
829 {
830     FdHandle_t *fdP;
831     int offset;
832     int col;
833     int coldata;
834     short row;
835     int code;
836
837
838     fdP = IH_OPEN(ih);
839     if (fdP == NULL)
840         return -1;
841
842     /* Only one manipulates at a time. */
843     if (flock(fdP->fd_fd, LOCK_EX)<0) {
844         FDH_REALLYCLOSE(fdP);
845         return -1;
846     }
847     
848     offset = (vno <<  LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
849     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
850         goto badGetFreeTag;
851     }
852         
853     code = read(fdP->fd_fd, (char*)&row, sizeof(row));
854     if (code != sizeof(row)) {
855         if (code != 0)
856             goto badGetFreeTag;
857         row = 0;
858     }
859         
860     /* Now find a free column in this row and claim it. */
861     coldata = 0x7;
862     for (col = 0; col<NAMEI_MAXVOLS; col++) {
863         coldata <<= col * 3;
864         if ((row & coldata) == 0)
865             break;
866     }
867     if (col >= NAMEI_MAXVOLS)
868         goto badGetFreeTag;
869
870     coldata = 1 << (col * 3);
871     row |= coldata;
872
873     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
874         goto badGetFreeTag;
875     }
876     if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
877         goto badGetFreeTag;
878     }
879     FDH_SYNC(fdP);
880     flock(fdP->fd_fd, LOCK_UN);
881     FDH_REALLYCLOSE(fdP);
882     return col;;
883
884  badGetFreeTag:
885     flock(fdP->fd_fd, LOCK_UN);
886     FDH_REALLYCLOSE(fdP);
887     return -1;
888 }
889
890
891
892 /* namei_SetLinkCount
893  * If locked is set, assume file is locked. Otherwise, lock file before
894  * proceeding to modify it.
895  */
896 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
897 {
898     int offset, index;
899     unsigned short row;
900     int junk;
901     int code = -1;
902
903     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
904
905
906     if (!locked) {
907         if (flock(fdP->fd_fd, LOCK_EX)<0) {
908             return -1;
909         }
910     }
911     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
912         errno = EBADF;
913         goto bad_SetLinkCount;
914     }
915
916     
917     code = read(fdP->fd_fd, (char*)&row, sizeof(row));
918     if (code != sizeof(row)) {
919         if (code != 0) {
920             errno = EBADF;
921             goto bad_SetLinkCount;
922         }
923         row = 0;
924     }
925     
926     junk = 7 << index;
927     count <<= index;
928     row &= (unsigned short)~junk;
929     row |= (unsigned short)count;
930
931     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
932         errno =  EBADF;
933         goto bad_SetLinkCount;
934     }
935
936     if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
937         errno = EBADF;
938         goto bad_SetLinkCount;
939     }
940     FDH_SYNC(fdP);
941
942     code = 0;
943
944     
945 bad_SetLinkCount:
946     flock(fdP->fd_fd, LOCK_UN);
947
948     return code;
949 }
950
951
952 /* ListViceInodes - write inode data to a results file. */
953 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
954                        int volid);
955 static int DecodeVolumeName(char *name, int *vid);
956 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
957                              int (*write_fun)(FILE *, struct ViceInodeInfo *,
958                                               char *, char *),
959                              FILE *fp,
960                              int (*judgeFun)(struct ViceInodeInfo *, int vid),
961                              int singleVolumeNumber);
962
963
964 /* WriteInodeInfo
965  *
966  * Write the inode data to the results file. 
967  *
968  * Returns -2 on error, 0 on success.
969  *
970  * This is written as a callback simply so that other listing routines
971  * can use the same inode reading code.
972  */
973 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
974                           char *name)
975 {
976     int n;
977     n = fwrite(info, sizeof(*info), 1, fp);
978     return (n == 1) ? 0 : -2;
979 }
980
981
982 int mode_errors; /* Number of errors found in mode bits on directories. */
983 void VerifyDirPerms(char *path)
984 {
985     struct stat status;
986
987     if (stat(path, &status)<0) {
988         Log("Unable to stat %s. Please manually verify mode bits for this"
989             " directory\n", path);
990     }
991     else {
992         if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
993             mode_errors ++;
994     }
995 }
996
997 /* ListViceInodes
998  * Fill the results file with the requested inode information.
999  *
1000  * Return values:
1001  *  0 - success
1002  * -1 - complete failure, salvage should terminate.
1003  * -2 - not enough space on partition, salvager has error message for this.
1004  *
1005  * This code optimizes single volume salvages by just looking at that one
1006  * volume's directory. 
1007  *
1008  * If the resultFile is NULL, then don't call the write routine.
1009  */
1010 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1011                    int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1012                    int singleVolumeNumber, int *forcep,
1013                    int forceR, char *wpath)
1014 {
1015     FILE *fp = (FILE*)-1;
1016     int ninodes;
1017     struct stat status;
1018
1019     if (resultFile) {
1020         fp = fopen(resultFile, "w");
1021         if (!fp) {
1022             Log("Unable to create inode description file %s\n", resultFile);
1023             return -1;
1024         }
1025     }
1026
1027     /* Verify protections on directories. */
1028     mode_errors = 0;
1029     VerifyDirPerms(mountedOn);
1030
1031     ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1032                            judgeInode, singleVolumeNumber);
1033
1034     if (!resultFile)
1035         return ninodes;
1036
1037     if (ninodes < 0) {
1038         fclose(fp);
1039         return ninodes;
1040     }
1041
1042     if (fflush(fp) == EOF) {
1043         Log("Unable to successfully flush inode file for %s\n", mountedOn);
1044         fclose(fp);
1045         return -2;
1046     }
1047     if (fsync(fileno(fp)) == -1) {
1048         Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1049         fclose(fp);
1050         return -2;
1051     }
1052     if (fclose(fp) == EOF) {
1053         Log("Unable to successfully close inode file for %s\n", mountedOn);
1054         return -2;
1055     }
1056
1057     /*
1058      * Paranoia:  check that the file is really the right size
1059      */
1060     if (stat(resultFile, &status) == -1) {
1061         Log("Unable to successfully stat inode file for %s\n", mountedOn);
1062         return -2;
1063     }
1064     if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1065         Log("Wrong size (%d instead of %d) in inode file for %s\n", 
1066             status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1067             mountedOn);
1068         return -2;
1069     }
1070     return 0;
1071 }
1072
1073
1074 /* namei_ListAFSFiles
1075  *
1076  * Collect all the matching AFS files on the drive.
1077  * If singleVolumeNumber is non-zero, just return files for that volume.
1078  *
1079  * Returns <0 on error, else number of files found to match.
1080  */
1081 int namei_ListAFSFiles(char *dev,
1082                     int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1083                                      char *),
1084                     FILE *fp,
1085                     int (*judgeFun)(struct ViceInodeInfo *, int),
1086                     int singleVolumeNumber)
1087 {
1088     IHandle_t ih;
1089     namei_t name;
1090     int ninodes = 0;
1091     DIR *dirp1, *dirp2;
1092     struct dirent *dp1, *dp2;
1093     char path2[512];
1094 #ifdef DELETE_ZLC
1095     static void FreeZLCList(void);
1096 #endif
1097
1098     memset((void*)&ih, 0, sizeof(IHandle_t));
1099     ih.ih_dev = volutil_GetPartitionID(dev);
1100
1101     if (singleVolumeNumber) {
1102         ih.ih_vid = singleVolumeNumber;
1103         namei_HandleToVolDir(&name, &ih);
1104         ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1105                                  judgeFun, singleVolumeNumber);
1106         if (ninodes < 0)
1107             return ninodes;
1108     }
1109     else {
1110         /* Find all volume data directories and descend through them. */
1111         namei_HandleToInodeDir(&name, &ih);
1112         ninodes = 0;
1113         dirp1 = opendir(name.n_path);
1114         if (!dirp1)
1115             return 0;
1116         while (dp1 = readdir(dirp1)) {
1117             if (*dp1->d_name == '.') continue;
1118             (void) strcpy(path2, name.n_path);
1119             (void) strcat(path2, "/");
1120             (void) strcat(path2, dp1->d_name);
1121             dirp2 = opendir(path2);
1122             if (dirp2) {
1123                 while (dp2 = readdir(dirp2)) {
1124                     if (*dp2->d_name == '.') continue;
1125                     if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1126                         ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1127                                                        judgeFun, 0);
1128                     }
1129                 }
1130                 closedir(dirp2);
1131             }
1132         }
1133         closedir(dirp1);
1134     }
1135 #ifdef DELETE_ZLC
1136     FreeZLCList();
1137 #endif
1138     return ninodes;
1139 }
1140
1141
1142
1143 /* namei_ListAFSSubDirs
1144  *
1145  *
1146  * Return values:
1147  * < 0 - an error
1148  * > = 0 - number of AFS files found.
1149  */
1150 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1151                              int (*writeFun)(FILE *, struct ViceInodeInfo *,
1152                                               char *, char *),
1153                              FILE *fp,
1154                              int (*judgeFun)(struct ViceInodeInfo *, int),
1155                              int singleVolumeNumber)
1156 {
1157     int i;
1158     IHandle_t myIH = *dirIH;
1159     namei_t name;
1160     char path1[512], path2[512], path3[512];
1161     DIR *dirp1, *dirp2, *dirp3;
1162     struct dirent *dp1, *dp2, *dp3;
1163     char *s;
1164     struct ViceInodeInfo info;
1165     int tag, vno;
1166     FdHandle_t linkHandle;
1167     int ninodes = 0;
1168 #ifdef DELETE_ZLC
1169     static void AddToZLCDeleteList(char dir, char *name);
1170     static void DeleteZLCFiles(char *path);
1171 #endif
1172
1173     namei_HandleToVolDir(&name, &myIH);
1174     (void) strcpy(path1, name.n_path);
1175
1176     /* Do the directory containing the special files first to pick up link
1177      * counts.
1178      */
1179     (void) strcat(path1, "/");
1180     (void) strcat(path1, NAMEI_SPECDIR);
1181
1182     linkHandle.fd_fd = -1;
1183     dirp1 = opendir(path1);
1184     if (dirp1) {
1185         while (dp1 = readdir(dirp1)) {
1186             if (*dp1->d_name == '.') continue;
1187             if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1188                 continue;
1189             if (info.u.param[2] != VI_LINKTABLE) {
1190                 info.linkCount = 1;
1191             }
1192             else {
1193                 /* Open this handle */
1194                 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1195                 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1196                 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1197             }
1198             if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1199                 continue;
1200
1201             if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1202                 if (linkHandle.fd_fd >= 0)
1203                     close(linkHandle.fd_fd);
1204                 closedir(dirp1);
1205                 return -1;
1206             }
1207             ninodes ++;
1208         }
1209         closedir(dirp1);
1210     }
1211
1212     /* Now run through all the other subdirs */
1213     namei_HandleToVolDir(&name, &myIH);
1214     (void) strcpy(path1, name.n_path);
1215     
1216     dirp1 = opendir(path1);
1217     if (dirp1) {
1218         while (dp1 = readdir(dirp1)) {
1219             if (*dp1->d_name == '.') continue;
1220             if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1221                 continue;
1222             
1223             /* Now we've got a next level subdir. */
1224             (void) strcpy(path2, path1);
1225             (void) strcat(path2, "/");
1226             (void) strcat(path2, dp1->d_name);
1227             dirp2 = opendir(path2);
1228             if (dirp2) {
1229                 while (dp2 = readdir(dirp2)) {
1230                     if (*dp2->d_name == '.') continue;
1231                     
1232                     /* Now we've got to the actual data */
1233                     (void) strcpy(path3, path2);
1234                     (void) strcat(path3, "/");
1235                     (void) strcat(path3, dp2->d_name);
1236                     dirp3 = opendir(path3);
1237                     if (dirp3) {
1238                         while (dp3 = readdir(dirp3)) {
1239                             if (*dp3->d_name == '.') continue;
1240                             if (DecodeInode(path3, dp3->d_name, &info,
1241                                             myIH.ih_vid)<0)
1242                                 continue;
1243                             info.linkCount = namei_GetLinkCount(&linkHandle,
1244                                                                info.inodeNumber, 0);
1245                             if (info.linkCount == 0) {
1246 #ifdef DELETE_ZLC
1247                                 Log("Found 0 link count file %s/%s, deleting it.\n",
1248                                     path3, dp3->d_name);
1249                                 AddToZLCDeleteList((char)i, dp3->d_name);
1250 #else
1251                                 Log("Found 0 link count file %s/%s.\n",
1252                                     path3, dp3->d_name);
1253 #endif
1254                                 continue;
1255                             }
1256                             if (judgeFun
1257                                 && !(*judgeFun)(&info, singleVolumeNumber))
1258                                 continue;
1259
1260                             if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1261                                 close(linkHandle.fd_fd);
1262                                 closedir(dirp3);
1263                                 closedir(dirp2);
1264                                 closedir(dirp1);
1265                                 return -1;
1266                             }
1267                             ninodes ++;
1268                         }
1269                         closedir(dirp3);
1270                     }
1271                 }
1272                 closedir(dirp2);
1273             }
1274         }
1275         closedir(dirp1);
1276     }
1277
1278     if (linkHandle.fd_fd >= 0)
1279         close(linkHandle.fd_fd);
1280     if (!ninodes) {
1281         /* Then why does this directory exist? Blow it away. */
1282         namei_HandleToVolDir(&name, dirIH);
1283         namei_RemoveDataDirectories(&name);
1284     }
1285
1286     return ninodes;
1287 }
1288
1289 static int DecodeVolumeName(char *name, int *vid)
1290 {
1291     if (strlen(name) <= 2)
1292         return -1;
1293     *vid = (int) flipbase64_to_int64(name);
1294     return 0;
1295 }
1296
1297
1298 /* DecodeInode
1299  *
1300  * Get the inode number from the name.
1301  * Get
1302  */
1303 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1304                        int volid)
1305 {
1306     char fpath[512];
1307     struct stat status;
1308     int parm, tag;
1309
1310     (void) strcpy(fpath, dpath);
1311     (void) strcat(fpath, "/");
1312     (void) strcat(fpath, name);
1313
1314     if (stat(fpath, &status)<0) {
1315         return -1;
1316     }
1317
1318     info->byteCount = status.st_size;
1319     info->inodeNumber = flipbase64_to_int64(name);
1320     
1321     GetOGMFromStat(&status, &parm, &tag);
1322     if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1323         /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1324         info->u.param[0] = parm;
1325         info->u.param[1] = -1;
1326         info->u.param[2] = tag;
1327         info->u.param[3] = volid;
1328     }
1329     else {
1330         /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1331         info->u.param[0] = volid;
1332         info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1333         info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1334                                  & (Inode)NAMEI_UNIQMASK);
1335         info->u.param[3] = parm;
1336     }
1337     return 0;
1338 }
1339
1340
1341 /* PrintInode
1342  *
1343  * returns a static string used to print either 32 or 64 bit inode numbers.
1344  */
1345 char * PrintInode(char *s, Inode ino)
1346 {
1347     static afs_ino_str_t result; 
1348     if (!s)
1349         s = result;
1350
1351     (void) sprintf((char*)s, "%Lu", ino);
1352
1353     return (char*)s;
1354 }
1355
1356
1357 #ifdef DELETE_ZLC
1358 /* Routines to facilitate removing zero link count files. */
1359 #define MAX_ZLC_NAMES 32
1360 #define MAX_ZLC_NAMELEN 16
1361 typedef struct zlcList_s {
1362     struct zlcList_s *zlc_next;
1363     int zlc_n;
1364     char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1365 } zlcList_t;
1366
1367 static zlcList_t *zlcAnchor = NULL;
1368 static zlcList_t *zlcCur = NULL;
1369
1370 static void AddToZLCDeleteList(char dir, char *name)
1371 {
1372     assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1373
1374     if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1375         if (zlcCur && zlcCur->zlc_next)
1376             zlcCur = zlcCur->zlc_next;
1377         else {
1378             zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1379             if (!tmp)
1380                 return;
1381             if (!zlcAnchor) {
1382                 zlcAnchor = tmp;
1383             }
1384             else {
1385                 zlcCur->zlc_next = tmp;
1386             }
1387             zlcCur = tmp;
1388             zlcCur->zlc_n = 0;
1389             zlcCur->zlc_next = NULL;
1390         }
1391     }
1392
1393     (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1394     zlcCur->zlc_n ++;
1395 }
1396
1397 static void DeleteZLCFiles(char *path)
1398 {
1399     zlcList_t *z;
1400     int i;
1401     char fname[1024];
1402
1403     for (z = zlcAnchor; z; z = z->zlc_next) {
1404         for (i=0; i < z->zlc_n; i++) {
1405             (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1406             if (namei_unlink(fname)<0) {
1407                 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1408                        GetLastError());
1409             }
1410         }
1411         z->zlc_n = 0; /* Can reuse space. */
1412     }
1413     zlcCur = zlcAnchor;
1414 }
1415
1416 static void FreeZLCList(void)
1417 {
1418     zlcList_t *tnext;
1419     zlcList_t *i;
1420
1421     i = zlcAnchor;
1422     while (i) {
1423         tnext = i->zlc_next;
1424         free(i);
1425         i = tnext;
1426     }
1427     zlcCur = zlcAnchor = NULL;
1428 }
1429 #endif
1430
1431 #endif /* AFS_NAMEI_ENV */
1432