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