d4fa1922230305cd5fa475b6cff8a3c66826a7d5
[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     int tmp;
173         
174     namei_HandleToVolDir(name, ih);
175
176     if (vno == NAMEI_VNODESPECIAL) {
177         (void) strcpy(name->n_dir1, NAMEI_SPECDIR);
178         addtoname(name, name->n_dir1);
179         name->n_dir2[0] = '\0';
180     }
181     else {
182         (void) int32_to_flipbase64(str, VNO_DIR1(vno));
183         (void) strcpy(name->n_dir1, str);
184         addtoname(name, name->n_dir1);
185         (void) int32_to_flipbase64(str, VNO_DIR2(vno));
186         (void) strcpy(name->n_dir2, str);
187         addtoname(name, name->n_dir2);
188     }
189     (void) int64_to_flipbase64(str, (int64_t)ih->ih_ino);
190     (void) strcpy(name->n_inode, str);
191     addtoname(name, name->n_inode);
192 }
193
194 /* The following is a warning to tell sys-admins to not muck about in this
195  * name space.
196  */
197 #define VICE_README "These files and directories are a part of the AFS \
198 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
199 int namei_ViceREADME(char *partition)
200 {
201    char filename[32];
202    int fd;
203
204    sprintf(filename, "%s/%s/README", partition, INODEDIR);
205    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
206    if (fd >= 0) {
207       write(fd, VICE_README, strlen(VICE_README));
208       close(fd);
209    }
210    return(errno);
211 }
212
213
214 #define create_dir() \
215 do { \
216     if (mkdir(tmp, 0700)<0) { \
217         if (errno != EEXIST) \
218             return -1; \
219     } \
220     else { \
221         *created = 1; \
222     } \
223 } while (0) 
224
225 #define create_nextdir(A) \
226 do { \
227          strcat(tmp, "/"); strcat(tmp, A); create_dir();  \
228 } while(0)
229
230 /* namei_CreateDataDirectories
231  *
232  * If creating the file failed because of ENOENT or ENOTDIR, try
233  * creating all the directories first.
234  */
235 static int namei_CreateDataDirectories(namei_t *name, int *created)
236 {
237     char tmp[256];
238     char *s;
239     int i;
240
241     *created = 0;
242
243     (void) strcpy(tmp, name->n_base);
244     create_dir();
245
246     create_nextdir(name->n_voldir1);
247     create_nextdir(name->n_voldir2);
248     create_nextdir(name->n_dir1);
249     if (name->n_dir2[0]) {
250         create_nextdir(name->n_dir2);
251     }
252     return 0;
253 }       
254
255 /* delTree(): Deletes an entire tree of directories (no files)
256  * Input:
257  *   root : Full path to the subtree. Should be big enough for PATH_MAX
258  *   tree : the subtree to be deleted is rooted here. Specifies only the
259  *          subtree beginning at tree (not the entire path). It should be
260  *          a pointer into the "root" buffer.
261  * Output:
262  *  errp : errno of the first error encountered during the directory cleanup.
263  *         *errp should have been initialized to 0.
264  * 
265  * Return Values:
266  *  -1  : If errors were encountered during cleanup and error is set to 
267  *        the first errno.
268  *   0  : Success.
269  *
270  * If there are errors, we try to work around them and delete as many
271  * directories as possible. We don't attempt to remove directories that still
272  * have non-dir entries in them.
273  */
274 static int
275 delTree(char *root, char *tree, int *errp)
276 {
277   char *cp;
278   DIR *ds;
279   struct dirent *dirp;
280   struct stat st;
281   int er;
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     int tmp;
426
427     *parm = status->st_uid | (status->st_gid << 15);
428     *parm |= (status->st_mode & 0x18) << 27;
429     *tag = status->st_mode & 0x7;
430 }
431
432 static int GetOGM(int fd, int *parm, int *tag)
433 {
434     struct stat status;
435     if (fstat(fd, &status)<0) 
436         return -1;
437
438     GetOGMFromStat(&status, parm, tag);
439     return 0;
440 }
441
442 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
443
444 /* Derive the name and create it O_EXCL. If that fails we have an error.
445  * Get the tag from a free column in the link table.
446  */
447 Inode namei_icreate(IHandle_t *lh, char *part, int p1, int p2, int p3, int p4)
448 {
449     namei_t name;
450     namei_ogm_t ogm;
451     b32_string_t str1;
452     char *p;
453     int i;
454     int fd = -1;
455     int code = 0;
456     int created_dir = 0;
457     IHandle_t tmp;
458     FdHandle_t *fdP;
459     FdHandle_t tfd;
460     int save_errno;
461     int tag;
462     int ogm_parm;
463     
464
465     memset((void*)&tmp, 0, sizeof(IHandle_t));
466
467
468     tmp.ih_dev = volutil_GetPartitionID(part);
469     if (tmp.ih_dev == -1) {
470         errno = EINVAL;
471         return -1;
472     }
473
474     if (p2 == -1 ) {
475         /* Parameters for special file:
476          * p1 - volume id - goes into owner/group/mode
477          * p2 - vnode == -1
478          * p3 - type
479          * p4 - parent volume id
480          */
481         ogm_parm = p1;
482         tag = p3;
483
484         tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
485         tmp.ih_ino = namei_MakeSpecIno(p1, p3);
486     }
487     else {
488         int vno = p2 & NAMEI_VNODEMASK; 
489         /* Parameters for regular file:
490          * p1 - volume id
491          * p2 - vnode
492          * p3 - uniq
493          * p4 - dv
494          */
495
496         if (vno != p2) {
497             big_vno ++;
498             errno = EINVAL;
499             return -1;
500         }
501         /* If GetFreeTag succeeds, it atomically sets link count to 1. */
502         tag = GetFreeTag(lh, p2);
503         if (tag < 0)
504             goto bad;
505
506         /* name is <uniq(p3)><tag><vno(p2)> */
507         tmp.ih_vid = p1;
508         tmp.ih_ino = (Inode)p2;
509         tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
510         tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
511
512         ogm_parm = p4;
513     }
514
515     namei_HandleToName(&name, &tmp);
516     fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
517     if (fd < 0) {
518         if (errno == ENOTDIR || errno == ENOENT) {
519             if (namei_CreateDataDirectories(&name, &created_dir)<0)
520                 goto bad;
521             fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
522             if (fd < 0)
523                 goto bad;
524         }
525         else {
526             goto bad;
527         }
528     }
529     if (SetOGM(fd, ogm_parm, tag)<0) {
530         close(fd);
531         fd = -1;
532         goto bad;
533     }
534
535     if (p2 == -1 && p3 == VI_LINKTABLE) {
536         /* hack at tmp to setup for set link count call. */
537         tfd.fd_fd = fd;
538         code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
539     }
540
541 bad:
542     if (fd >= 0)
543         close(fd);
544
545
546     if (code || (fd < 0)) {
547         if (p2 != -1) {
548             fdP = IH_OPEN(lh);
549             if (fdP) {
550                 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
551                 FDH_CLOSE(fdP);
552             }
553         }
554     }
555     return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
556 }
557
558
559 /* namei_iopen */
560 int namei_iopen(IHandle_t *h)
561 {
562     int fd;
563     namei_t name;
564
565     /* Convert handle to file name. */
566     namei_HandleToName(&name, h);
567     fd = open(name.n_path, O_RDWR, 0666);
568     return fd;
569 }
570
571 /* Need to detect vol special file and just unlink. In those cases, the
572  * handle passed in _is_ for the inode. We only check p1 for the special
573  * files.
574  */
575 int namei_dec(IHandle_t *ih, Inode ino, int p1)
576 {
577     int count = 0;
578     namei_t name;
579     int code = 0;
580     FdHandle_t *fdP;
581
582     if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
583         IHandle_t *tmp;
584         int was_closed = 0;
585         int inode_p1, tag;
586         int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
587
588         /* Verify this is the right file. */
589         IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
590
591         fdP = IH_OPEN(tmp);
592         if (fdP == NULL) {
593             IH_RELEASE(tmp);
594             errno = EINVAL;
595             return -1;
596         }
597
598         if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
599             FDH_REALLYCLOSE(fdP);
600             IH_RELEASE(tmp);
601             errno = EINVAL;
602             return -1;
603         }
604         
605         /* If it's the link table itself, decrement the link count. */
606         if (type == VI_LINKTABLE) {
607             if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
608                 FDH_REALLYCLOSE(fdP);
609                 IH_RELEASE(tmp);
610                 return -1;
611             }
612
613             count --;
614             if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
615                 FDH_REALLYCLOSE(fdP);
616                 IH_RELEASE(tmp);
617                 return -1;
618             }
619
620             if (count>0) {
621                 FDH_REALLYCLOSE(fdP);
622                 IH_RELEASE(tmp);
623                 return 0;
624             }
625         }
626         
627         namei_HandleToName(&name, tmp);
628         if ((code = unlink(name.n_path)) == 0) {
629             if (type == VI_LINKTABLE) {
630                 /* Try to remove directory. If it fails, that's ok.
631                  * Salvage will clean up.
632                  */
633                 (void) namei_RemoveDataDirectories(&name);
634             }
635         }
636         FDH_REALLYCLOSE(fdP);
637         IH_RELEASE(tmp);
638     }
639     else {
640         /* Get a file descriptor handle for this Inode */
641         fdP = IH_OPEN(ih);
642         if (fdP == NULL) {
643             return -1;
644         }
645
646         if ((count = namei_GetLinkCount(fdP, ino,  1))<0) {
647             FDH_REALLYCLOSE(fdP);
648             return -1;
649         }
650
651         count --;
652         if (count >= 0) {
653             if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
654                 FDH_REALLYCLOSE(fdP);
655                 return -1;
656             }
657         }
658         if (count == 0 ) {
659             IHandle_t th = *ih;
660             th.ih_ino = ino;
661             namei_HandleToName(&name, &th);
662             code = unlink(name.n_path);
663         }
664         FDH_CLOSE(fdP);
665     }
666
667     return code;
668 }
669
670 int namei_inc(IHandle_t *h, Inode ino, int p1)
671 {
672     int count;
673     int code = 0;
674     FdHandle_t *fdP;
675
676     if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
677         int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
678         if (type != VI_LINKTABLE)
679             return 0;
680         ino = (Inode)0;
681     }
682
683     /* Get a file descriptor handle for this Inode */
684     fdP = IH_OPEN(h);
685     if (fdP == NULL) {
686         return -1;
687     }
688
689     if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
690         code = -1;
691     else {
692         count ++;
693         if (count > 7) {
694             errno = EINVAL;
695             code = -1;
696             count = 7;
697         }
698         if (namei_SetLinkCount(fdP, ino, count, 1)<0)
699             code = -1;
700     }
701     if (code) {
702         FDH_REALLYCLOSE(fdP);
703     } else {
704         FDH_CLOSE(fdP);
705     }
706     return code;
707 }
708
709 /************************************************************************
710  * File Name Structure
711  ************************************************************************
712  *
713  * Each AFS file needs a unique name and it needs to be findable with
714  * minimal lookup time. Note that the constraint on the number of files and
715  * directories in a volume is the size of the vnode index files and the
716  * max file size AFS supports (for internal files) of 2^31. Since a record
717  * in the small vnode index file is 64 bytes long, we can have at most
718  * (2^31)/64 or 33554432 files. A record in the large index file is
719  * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
720  * Another layout parameter is that there is roughly a 16 to 1 ratio between
721  * the number of files and the number of directories.
722  *
723  * Using this information we can see that a layout of 256 directories, each
724  * with 512 subdirectories and each of those having 512 files gives us
725  * 256*512*512 = 67108864 AFS files and directories. 
726  *
727  * The volume, vnode, uniquifier and data version, as well as the tag
728  * are required, either for finding the file or for salvaging. It's best to 
729  * restrict the name to something that can be mapped into 64 bits so the
730  * "Inode" is easily comparable (using "==") to other "Inodes". The tag
731  * is used to distinguish between different versions of the same file
732  * which are currently in the RW and clones of a volume. See "Link Table
733  * Organization" below for more information on the tag. The tag is 
734  * required in the name of the file to ensure a unique name. 
735  *
736  * We can store data in the uid, gid and mode bits of the files, provided
737  * the directories have root only access. This gives us 15 bits for each
738  * of uid and gid (GNU chown considers 65535 to mean "don't change").
739  * There are 9 available mode bits. Adn we need to store a total of 
740  * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
741  * or 131 bits somewhere. 
742  *
743  * The format of a file name for a regular file is:
744  * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
745  * V1 - low 8 bits of RW volume id
746  * V2 - all bits of RW volume id
747  * AA - high 8 bits of vnode number.
748  * BB - next 9 bits of vnode number.
749  * <tag><uniq><vno> - file name
750  *
751  * Volume special files are stored in a separate directory:
752  * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
753  *
754  *
755  * The vnode is hashed into the directory using the high bits of the
756  * vnode number. This is so that consecutively created vnodes are in
757  * roughly the same area on the disk. This will at least be optimal if
758  * the user is creating many files in the same AFS directory. The name
759  * should be formed so that the leading characters are different as quickly
760  * as possible, leading to faster discards of incorrect matches in the
761  * lookup code.
762  * 
763  */
764
765
766 /************************************************************************
767  *  Link Table Organization
768  ************************************************************************
769  *
770  * The link table volume special file is used to hold the link counts that
771  * are held in the inodes in inode based AFS vice filesystems. For user
772  * space access, the link counts are being kept in a separate
773  * volume special file. The file begins with the usual version stamp
774  * information and is then followed by one row per vnode number. vnode 0
775  * is used to hold the link count of the link table itself. That is because
776  * the same link table is shared among all the volumes of the volume group
777  * and is deleted only when the last volume of a volume group is deleted.
778  *
779  * Within each row, the columns are 3 bits wide. They can each hold a 0 based
780  * link count from 0 through 7. Each colume represents a unique instance of
781  * that vnode. Say we have a file shared between the RW and a RO and a
782  * different version of the file (or a different uniquifer) for the BU volume.
783  * Then one column would be holding the link count of 2 for the RW and RO
784  * and a different column would hold the link count of 1 for the BU volume.
785  * Note that we allow only 5 volumes per file, giving 15 bits used in the
786  * short.
787  */
788 #define LINKTABLE_WIDTH 2
789 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
790
791 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
792 {
793     int toff = (int) (ino & NAMEI_VNODEMASK);
794     int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
795
796     *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
797     *index = (tindex << 1) + tindex;
798 }
799
800
801 /* namei_GetLinkCount
802  * If lockit is set, lock the file and leave it locked upon a successful
803  * return.
804  */
805 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
806 {
807     unsigned short row = 0;
808     int junk;
809     int offset, index;
810
811     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
812
813     if (lockit) {
814 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
815         if (lockf(h->fd_fd, F_LOCK, 0) < 0)
816 #else
817         if (flock(h->fd_fd, LOCK_EX)<0)
818 #endif
819             return -1;
820     }
821
822     if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
823         goto bad_getLinkByte;
824     
825     if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
826         goto bad_getLinkByte;
827     }
828         
829     return (int) ((row >> index) & NAMEI_TAGMASK);
830
831  bad_getLinkByte:
832     if (lockit)
833 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
834         lockf(h->fd_fd, F_ULOCK, 0);
835 #else
836         flock(h->fd_fd, LOCK_UN);
837 #endif
838     return -1;
839 }
840
841 /* Return a free column index for this vnode. */
842 static int GetFreeTag(IHandle_t *ih, int vno)
843 {
844     FdHandle_t *fdP;
845     int offset;
846     int col;
847     int coldata;
848     short row;
849     int code;
850
851
852     fdP = IH_OPEN(ih);
853     if (fdP == NULL)
854         return -1;
855
856     /* Only one manipulates at a time. */
857 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
858     if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
859 #else
860     if (flock(fdP->fd_fd, LOCK_EX)<0) {
861 #endif
862         FDH_REALLYCLOSE(fdP);
863         return -1;
864     }
865     
866     offset = (vno <<  LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
867     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
868         goto badGetFreeTag;
869     }
870         
871     code = read(fdP->fd_fd, (char*)&row, sizeof(row));
872     if (code != sizeof(row)) {
873         if (code != 0)
874             goto badGetFreeTag;
875         row = 0;
876     }
877         
878     /* Now find a free column in this row and claim it. */
879     coldata = 0x7;
880     for (col = 0; col<NAMEI_MAXVOLS; col++) {
881         coldata <<= col * 3;
882         if ((row & coldata) == 0)
883             break;
884     }
885     if (col >= NAMEI_MAXVOLS)
886         goto badGetFreeTag;
887
888     coldata = 1 << (col * 3);
889     row |= coldata;
890
891     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
892         goto badGetFreeTag;
893     }
894     if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
895         goto badGetFreeTag;
896     }
897     FDH_SYNC(fdP);
898 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
899     lockf(fdP->fd_fd, F_ULOCK, 0);
900 #else
901     flock(fdP->fd_fd, LOCK_UN);
902 #endif
903     FDH_REALLYCLOSE(fdP);
904     return col;;
905
906  badGetFreeTag:
907 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
908     lockf(fdP->fd_fd, F_ULOCK, 0);
909 #else
910     flock(fdP->fd_fd, LOCK_UN);
911 #endif
912     FDH_REALLYCLOSE(fdP);
913     return -1;
914 }
915
916
917
918 /* namei_SetLinkCount
919  * If locked is set, assume file is locked. Otherwise, lock file before
920  * proceeding to modify it.
921  */
922 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
923 {
924     int offset, index;
925     unsigned short row;
926     int junk;
927     int code = -1;
928
929     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
930
931     if (!locked) {
932 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
933         if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
934 #else
935         if (flock(fdP->fd_fd, LOCK_EX)<0) {
936 #endif
937             return -1;
938         }
939     }
940     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
941         errno = EBADF;
942         goto bad_SetLinkCount;
943     }
944
945     
946     code = read(fdP->fd_fd, (char*)&row, sizeof(row));
947     if (code != sizeof(row)) {
948         if (code != 0) {
949             errno = EBADF;
950             goto bad_SetLinkCount;
951         }
952         row = 0;
953     }
954     
955     junk = 7 << index;
956     count <<= index;
957     row &= (unsigned short)~junk;
958     row |= (unsigned short)count;
959
960     if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
961         errno =  EBADF;
962         goto bad_SetLinkCount;
963     }
964
965     if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
966         errno = EBADF;
967         goto bad_SetLinkCount;
968     }
969     FDH_SYNC(fdP);
970
971     code = 0;
972
973     
974 bad_SetLinkCount:
975 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
976     lockf(fdP->fd_fd, F_ULOCK, 0);
977 #else
978     flock(fdP->fd_fd, LOCK_UN);
979 #endif
980
981     return code;
982 }
983
984
985 /* ListViceInodes - write inode data to a results file. */
986 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
987                        int volid);
988 static int DecodeVolumeName(char *name, int *vid);
989 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
990                              int (*write_fun)(FILE *, struct ViceInodeInfo *,
991                                               char *, char *),
992                              FILE *fp,
993                              int (*judgeFun)(struct ViceInodeInfo *, int vid),
994                              int singleVolumeNumber);
995
996
997 /* WriteInodeInfo
998  *
999  * Write the inode data to the results file. 
1000  *
1001  * Returns -2 on error, 0 on success.
1002  *
1003  * This is written as a callback simply so that other listing routines
1004  * can use the same inode reading code.
1005  */
1006 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
1007                           char *name)
1008 {
1009     int n;
1010     n = fwrite(info, sizeof(*info), 1, fp);
1011     return (n == 1) ? 0 : -2;
1012 }
1013
1014
1015 int mode_errors; /* Number of errors found in mode bits on directories. */
1016 void VerifyDirPerms(char *path)
1017 {
1018     struct stat status;
1019
1020     if (stat(path, &status)<0) {
1021         Log("Unable to stat %s. Please manually verify mode bits for this"
1022             " directory\n", path);
1023     }
1024     else {
1025         if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1026             mode_errors ++;
1027     }
1028 }
1029
1030 /* ListViceInodes
1031  * Fill the results file with the requested inode information.
1032  *
1033  * Return values:
1034  *  0 - success
1035  * -1 - complete failure, salvage should terminate.
1036  * -2 - not enough space on partition, salvager has error message for this.
1037  *
1038  * This code optimizes single volume salvages by just looking at that one
1039  * volume's directory. 
1040  *
1041  * If the resultFile is NULL, then don't call the write routine.
1042  */
1043 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1044                    int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1045                    int singleVolumeNumber, int *forcep,
1046                    int forceR, char *wpath)
1047 {
1048     FILE *fp = (FILE*)-1;
1049     int ninodes;
1050     struct stat status;
1051
1052     if (resultFile) {
1053         fp = fopen(resultFile, "w");
1054         if (!fp) {
1055             Log("Unable to create inode description file %s\n", resultFile);
1056             return -1;
1057         }
1058     }
1059
1060     /* Verify protections on directories. */
1061     mode_errors = 0;
1062     VerifyDirPerms(mountedOn);
1063
1064     ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1065                            judgeInode, singleVolumeNumber);
1066
1067     if (!resultFile)
1068         return ninodes;
1069
1070     if (ninodes < 0) {
1071         fclose(fp);
1072         return ninodes;
1073     }
1074
1075     if (fflush(fp) == EOF) {
1076         Log("Unable to successfully flush inode file for %s\n", mountedOn);
1077         fclose(fp);
1078         return -2;
1079     }
1080     if (fsync(fileno(fp)) == -1) {
1081         Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1082         fclose(fp);
1083         return -2;
1084     }
1085     if (fclose(fp) == EOF) {
1086         Log("Unable to successfully close inode file for %s\n", mountedOn);
1087         return -2;
1088     }
1089
1090     /*
1091      * Paranoia:  check that the file is really the right size
1092      */
1093     if (stat(resultFile, &status) == -1) {
1094         Log("Unable to successfully stat inode file for %s\n", mountedOn);
1095         return -2;
1096     }
1097     if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1098         Log("Wrong size (%d instead of %d) in inode file for %s\n", 
1099             status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1100             mountedOn);
1101         return -2;
1102     }
1103     return 0;
1104 }
1105
1106
1107 /* namei_ListAFSFiles
1108  *
1109  * Collect all the matching AFS files on the drive.
1110  * If singleVolumeNumber is non-zero, just return files for that volume.
1111  *
1112  * Returns <0 on error, else number of files found to match.
1113  */
1114 int namei_ListAFSFiles(char *dev,
1115                     int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1116                                      char *),
1117                     FILE *fp,
1118                     int (*judgeFun)(struct ViceInodeInfo *, int),
1119                     int singleVolumeNumber)
1120 {
1121     IHandle_t ih;
1122     namei_t name;
1123     int ninodes = 0;
1124     DIR *dirp1, *dirp2;
1125     struct dirent *dp1, *dp2;
1126     char path2[512];
1127 #ifdef DELETE_ZLC
1128     static void FreeZLCList(void);
1129 #endif
1130
1131     memset((void*)&ih, 0, sizeof(IHandle_t));
1132     ih.ih_dev = volutil_GetPartitionID(dev);
1133
1134     if (singleVolumeNumber) {
1135         ih.ih_vid = singleVolumeNumber;
1136         namei_HandleToVolDir(&name, &ih);
1137         ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1138                                  judgeFun, singleVolumeNumber);
1139         if (ninodes < 0)
1140             return ninodes;
1141     }
1142     else {
1143         /* Find all volume data directories and descend through them. */
1144         namei_HandleToInodeDir(&name, &ih);
1145         ninodes = 0;
1146         dirp1 = opendir(name.n_path);
1147         if (!dirp1)
1148             return 0;
1149         while (dp1 = readdir(dirp1)) {
1150             if (*dp1->d_name == '.') continue;
1151             (void) strcpy(path2, name.n_path);
1152             (void) strcat(path2, "/");
1153             (void) strcat(path2, dp1->d_name);
1154             dirp2 = opendir(path2);
1155             if (dirp2) {
1156                 while (dp2 = readdir(dirp2)) {
1157                     if (*dp2->d_name == '.') continue;
1158                     if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1159                         ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1160                                                        judgeFun, 0);
1161                     }
1162                 }
1163                 closedir(dirp2);
1164             }
1165         }
1166         closedir(dirp1);
1167     }
1168 #ifdef DELETE_ZLC
1169     FreeZLCList();
1170 #endif
1171     return ninodes;
1172 }
1173
1174
1175
1176 /* namei_ListAFSSubDirs
1177  *
1178  *
1179  * Return values:
1180  * < 0 - an error
1181  * > = 0 - number of AFS files found.
1182  */
1183 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1184                              int (*writeFun)(FILE *, struct ViceInodeInfo *,
1185                                               char *, char *),
1186                              FILE *fp,
1187                              int (*judgeFun)(struct ViceInodeInfo *, int),
1188                              int singleVolumeNumber)
1189 {
1190     int i;
1191     IHandle_t myIH = *dirIH;
1192     namei_t name;
1193     char path1[512], path2[512], path3[512];
1194     DIR *dirp1, *dirp2, *dirp3;
1195     struct dirent *dp1, *dp2, *dp3;
1196     char *s;
1197     struct ViceInodeInfo info;
1198     int tag, vno;
1199     FdHandle_t linkHandle;
1200     int ninodes = 0;
1201 #ifdef DELETE_ZLC
1202     static void AddToZLCDeleteList(char dir, char *name);
1203     static void DeleteZLCFiles(char *path);
1204 #endif
1205
1206     namei_HandleToVolDir(&name, &myIH);
1207     (void) strcpy(path1, name.n_path);
1208
1209     /* Do the directory containing the special files first to pick up link
1210      * counts.
1211      */
1212     (void) strcat(path1, "/");
1213     (void) strcat(path1, NAMEI_SPECDIR);
1214
1215     linkHandle.fd_fd = -1;
1216     dirp1 = opendir(path1);
1217     if (dirp1) {
1218         while (dp1 = readdir(dirp1)) {
1219             if (*dp1->d_name == '.') continue;
1220             if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1221                 continue;
1222             if (info.u.param[2] != VI_LINKTABLE) {
1223                 info.linkCount = 1;
1224             }
1225             else {
1226                 /* Open this handle */
1227                 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1228                 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1229                 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1230             }
1231             if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1232                 continue;
1233
1234             if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1235                 if (linkHandle.fd_fd >= 0)
1236                     close(linkHandle.fd_fd);
1237                 closedir(dirp1);
1238                 return -1;
1239             }
1240             ninodes ++;
1241         }
1242         closedir(dirp1);
1243     }
1244
1245     /* Now run through all the other subdirs */
1246     namei_HandleToVolDir(&name, &myIH);
1247     (void) strcpy(path1, name.n_path);
1248     
1249     dirp1 = opendir(path1);
1250     if (dirp1) {
1251         while (dp1 = readdir(dirp1)) {
1252             if (*dp1->d_name == '.') continue;
1253             if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1254                 continue;
1255             
1256             /* Now we've got a next level subdir. */
1257             (void) strcpy(path2, path1);
1258             (void) strcat(path2, "/");
1259             (void) strcat(path2, dp1->d_name);
1260             dirp2 = opendir(path2);
1261             if (dirp2) {
1262                 while (dp2 = readdir(dirp2)) {
1263                     if (*dp2->d_name == '.') continue;
1264                     
1265                     /* Now we've got to the actual data */
1266                     (void) strcpy(path3, path2);
1267                     (void) strcat(path3, "/");
1268                     (void) strcat(path3, dp2->d_name);
1269                     dirp3 = opendir(path3);
1270                     if (dirp3) {
1271                         while (dp3 = readdir(dirp3)) {
1272                             if (*dp3->d_name == '.') continue;
1273                             if (DecodeInode(path3, dp3->d_name, &info,
1274                                             myIH.ih_vid)<0)
1275                                 continue;
1276                             info.linkCount = namei_GetLinkCount(&linkHandle,
1277                                                                info.inodeNumber, 0);
1278                             if (info.linkCount == 0) {
1279 #ifdef DELETE_ZLC
1280                                 Log("Found 0 link count file %s/%s, deleting it.\n",
1281                                     path3, dp3->d_name);
1282                                 AddToZLCDeleteList((char)i, dp3->d_name);
1283 #else
1284                                 Log("Found 0 link count file %s/%s.\n",
1285                                     path3, dp3->d_name);
1286 #endif
1287                                 continue;
1288                             }
1289                             if (judgeFun
1290                                 && !(*judgeFun)(&info, singleVolumeNumber))
1291                                 continue;
1292
1293                             if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1294                                 close(linkHandle.fd_fd);
1295                                 closedir(dirp3);
1296                                 closedir(dirp2);
1297                                 closedir(dirp1);
1298                                 return -1;
1299                             }
1300                             ninodes ++;
1301                         }
1302                         closedir(dirp3);
1303                     }
1304                 }
1305                 closedir(dirp2);
1306             }
1307         }
1308         closedir(dirp1);
1309     }
1310
1311     if (linkHandle.fd_fd >= 0)
1312         close(linkHandle.fd_fd);
1313     if (!ninodes) {
1314         /* Then why does this directory exist? Blow it away. */
1315         namei_HandleToVolDir(&name, dirIH);
1316         namei_RemoveDataDirectories(&name);
1317     }
1318
1319     return ninodes;
1320 }
1321
1322 static int DecodeVolumeName(char *name, int *vid)
1323 {
1324     if (strlen(name) <= 2)
1325         return -1;
1326     *vid = (int) flipbase64_to_int64(name);
1327     return 0;
1328 }
1329
1330
1331 /* DecodeInode
1332  *
1333  * Get the inode number from the name.
1334  * Get
1335  */
1336 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1337                        int volid)
1338 {
1339     char fpath[512];
1340     struct stat status;
1341     int parm, tag;
1342
1343     (void) strcpy(fpath, dpath);
1344     (void) strcat(fpath, "/");
1345     (void) strcat(fpath, name);
1346
1347     if (stat(fpath, &status)<0) {
1348         return -1;
1349     }
1350
1351     info->byteCount = status.st_size;
1352     info->inodeNumber = flipbase64_to_int64(name);
1353     
1354     GetOGMFromStat(&status, &parm, &tag);
1355     if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1356         /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1357         info->u.param[0] = parm;
1358         info->u.param[1] = -1;
1359         info->u.param[2] = tag;
1360         info->u.param[3] = volid;
1361     }
1362     else {
1363         /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1364         info->u.param[0] = volid;
1365         info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1366         info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1367                                  & (Inode)NAMEI_UNIQMASK);
1368         info->u.param[3] = parm;
1369     }
1370     return 0;
1371 }
1372
1373
1374 /* PrintInode
1375  *
1376  * returns a static string used to print either 32 or 64 bit inode numbers.
1377  */
1378 char * PrintInode(char *s, Inode ino)
1379 {
1380     static afs_ino_str_t result; 
1381     if (!s)
1382         s = result;
1383
1384     (void) sprintf((char*)s, "%Lu", ino);
1385
1386     return (char*)s;
1387 }
1388
1389
1390 #ifdef DELETE_ZLC
1391 /* Routines to facilitate removing zero link count files. */
1392 #define MAX_ZLC_NAMES 32
1393 #define MAX_ZLC_NAMELEN 16
1394 typedef struct zlcList_s {
1395     struct zlcList_s *zlc_next;
1396     int zlc_n;
1397     char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1398 } zlcList_t;
1399
1400 static zlcList_t *zlcAnchor = NULL;
1401 static zlcList_t *zlcCur = NULL;
1402
1403 static void AddToZLCDeleteList(char dir, char *name)
1404 {
1405     assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1406
1407     if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1408         if (zlcCur && zlcCur->zlc_next)
1409             zlcCur = zlcCur->zlc_next;
1410         else {
1411             zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1412             if (!tmp)
1413                 return;
1414             if (!zlcAnchor) {
1415                 zlcAnchor = tmp;
1416             }
1417             else {
1418                 zlcCur->zlc_next = tmp;
1419             }
1420             zlcCur = tmp;
1421             zlcCur->zlc_n = 0;
1422             zlcCur->zlc_next = NULL;
1423         }
1424     }
1425
1426     (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1427     zlcCur->zlc_n ++;
1428 }
1429
1430 static void DeleteZLCFiles(char *path)
1431 {
1432     zlcList_t *z;
1433     int i;
1434     char fname[1024];
1435
1436     for (z = zlcAnchor; z; z = z->zlc_next) {
1437         for (i=0; i < z->zlc_n; i++) {
1438             (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1439             if (namei_unlink(fname)<0) {
1440                 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1441                        GetLastError());
1442             }
1443         }
1444         z->zlc_n = 0; /* Can reuse space. */
1445     }
1446     zlcCur = zlcAnchor;
1447 }
1448
1449 static void FreeZLCList(void)
1450 {
1451     zlcList_t *tnext;
1452     zlcList_t *i;
1453
1454     i = zlcAnchor;
1455     while (i) {
1456         tnext = i->zlc_next;
1457         free(i);
1458         i = tnext;
1459     }
1460     zlcCur = zlcAnchor = NULL;
1461 }
1462 #endif
1463
1464 #endif /* AFS_NAMEI_ENV */
1465