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