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