vol: dead initialization in VWalkVolumeHeaders()
[openafs.git] / src / vol / vutil.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 /*
11         System:         VICE-TWO
12         Module:         vutil.c
13         Institution:    The Information Technology Center, Carnegie-Mellon University
14
15  */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 #include <roken.h>
21
22 #ifdef HAVE_SYS_FILE_H
23 #include <sys/file.h>
24 #endif
25
26 #ifdef HAVE_SYS_LOCKF_H
27 #include <sys/lockf.h>
28 #endif
29
30 #include <rx/xdr.h>
31 #include <afs/afsint.h>
32 #include "nfs.h"
33 #include <afs/errors.h>
34 #include "lock.h"
35 #include "lwp.h"
36 #include <afs/afssyscalls.h>
37 #include "ihandle.h"
38 #include <afs/afsutil.h>
39 #ifdef AFS_NT40_ENV
40 #include "ntops.h"
41 #endif
42 #include "vnode.h"
43 #include "volume.h"
44 #include "volume_inline.h"
45 #include "partition.h"
46 #include "viceinode.h"
47
48 #include "volinodes.h"
49 #include "vol_prototypes.h"
50 #include "common.h"
51
52 #ifndef AFS_NT40_ENV
53 # ifdef O_LARGEFILE
54 #  define AFS_SETLKW   F_SETLKW64
55 #  define AFS_SETLK    F_SETLK64
56 #  define afs_st_flock flock64
57 # else
58 #  define AFS_SETLKW   F_SETLKW
59 #  define AFS_SETLK    F_SETLK
60 #  define afs_st_flock flock
61 # endif
62 #endif
63
64 /* Note:  the volume creation functions herein leave the destroyMe flag in the
65    volume header ON:  this means that the volumes will not be attached by the
66    file server and WILL BE DESTROYED the next time a system salvage is performed */
67
68 #ifdef FSSYNC_BUILD_CLIENT
69 static void
70 RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId parent,
71              VolumeId vid)
72 {
73     int i;
74     IHandle_t *handle;
75
76     /* This relies on the fact that IDEC only needs the device and NT only
77      * needs the dev and vid to decrement volume special files.
78      */
79     IH_INIT(handle, dev, parent, -1);
80     for (i = 0; i < MAXINODETYPE; i++) {
81         Inode inode = *stuff[i].inode;
82         if (VALID_INO(inode)) {
83             if (stuff[i].inodeType == VI_LINKTABLE) {
84                 IH_DEC(handle, inode, parent);
85             } else {
86                 IH_DEC(handle, inode, vid);
87             }
88         }
89     }
90     IH_RELEASE(handle);
91 }
92
93 Volume *
94 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
95 {                               /* Should be the same as volumeId if there is
96                                  * no parent */
97     Volume *retVal;
98     VOL_LOCK;
99     retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
100     VOL_UNLOCK;
101     return retVal;
102 }
103
104 Volume *
105 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
106 {                               /* Should be the same as volumeId if there is
107                                  * no parent */
108     VolumeDiskData vol;
109     int i, rc;
110     char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN];
111     Device device;
112     struct DiskPartition64 *partition;
113     struct VolumeDiskHeader diskHeader;
114     IHandle_t *handle;
115     FdHandle_t *fdP;
116     Inode nearInode AFS_UNUSED = 0;
117     char *part, *name;
118     struct stat st;
119     struct VolumeHeader tempHeader;
120     struct afs_inode_info stuff[MAXINODETYPE];
121     afs_ino_str_t stmp;
122 # ifdef AFS_DEMAND_ATTACH_FS
123     int locktype = 0;
124 # endif /* AFS_DEMAND_ATTACH_FS */
125
126     init_inode_info(&tempHeader, stuff);
127
128     *ec = 0;
129     memset(&vol, 0, sizeof(vol));
130     vol.id = volumeId;
131     vol.parentId = parentId;
132     vol.copyDate = time(0);     /* The only date which really means when this
133                                  * @i(instance) of this volume was created.
134                                  * Creation date does not mean this */
135
136     /* Initialize handle for error case below. */
137     handle = NULL;
138
139     /* Verify that the parition is valid before writing to it. */
140     if (!(partition = VGetPartition_r(partname, 0))) {
141         Log("VCreateVolume: partition %s is not in service.\n", partname);
142         *ec = VNOVOL;
143         return NULL;
144     }
145 #if     defined(NEARINODE_HINT)
146     nearInodeHash(volumeId, nearInode);
147     nearInode %= partition->f_files;
148 #endif
149     VGetVolumePath(ec, vol.id, &part, &name);
150     if (*ec == VNOVOL || !strcmp(partition->name, part)) {
151         /* this case is ok */
152     } else {
153         /* return EXDEV if it's a clone to an alternate partition
154          * otherwise assume it's a move */
155         if (vol.parentId != vol.id) {
156             *ec = EXDEV;
157             return NULL;
158         }
159     }
160     *ec = 0;
161
162 # ifdef AFS_DEMAND_ATTACH_FS
163     /* volume doesn't exist yet, but we must lock it to try to prevent something
164      * else from reading it when we're e.g. half way through creating it (or
165      * something tries to create the same volume at the same time) */
166     locktype = VVolLockType(V_VOLUPD, 1);
167     rc = VLockVolumeByIdNB(volumeId, partition, locktype);
168     if (rc) {
169         Log("VCreateVolume: vol %lu already locked by someone else\n",
170             afs_printable_uint32_lu(volumeId));
171         *ec = VNOVOL;
172         return NULL;
173     }
174 # else /* AFS_DEMAND_ATTACH_FS */
175     VLockPartition_r(partname);
176 # endif /* !AFS_DEMAND_ATTACH_FS */
177
178     memset(&tempHeader, 0, sizeof(tempHeader));
179     tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
180     tempHeader.stamp.version = VOLUMEHEADERVERSION;
181     tempHeader.id = vol.id;
182     tempHeader.parent = vol.parentId;
183     vol.stamp.magic = VOLUMEINFOMAGIC;
184     vol.stamp.version = VOLUMEINFOVERSION;
185     vol.destroyMe = DESTROY_ME;
186     snprintf(headerName, sizeof headerName, VFORMAT,
187              afs_printable_uint32_lu(vol.id));
188     snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
189              VPartitionPath(partition), headerName);
190     rc = stat(volumePath, &st);
191     if (rc == 0 || errno != ENOENT) {
192         if (rc == 0) {
193             Log("VCreateVolume: Header file %s already exists!\n",
194                 volumePath);
195             *ec = VVOLEXISTS;
196         } else {
197             Log("VCreateVolume: Error %d trying to stat header file %s\n",
198                 errno, volumePath);
199             *ec = VNOVOL;
200         }
201         goto bad_noheader;
202     }
203     device = partition->device;
204
205     for (i = 0; i < MAXINODETYPE; i++) {
206         struct afs_inode_info *p = &stuff[i];
207         if (p->obsolete)
208             continue;
209 #ifdef AFS_NAMEI_ENV
210         *(p->inode) =
211             IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
212                       (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
213                       INODESPECIAL, p->inodeType, vol.parentId);
214         if (!(VALID_INO(*(p->inode)))) {
215             if (errno == EEXIST && (p->inodeType == VI_LINKTABLE)) {
216                 /* Increment the reference count instead. */
217                 IHandle_t *lh;
218                 int code;
219
220                 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
221                 IH_INIT(lh, device, parentId, *(p->inode));
222                 fdP = IH_OPEN(lh);
223                 if (fdP == NULL) {
224                     IH_RELEASE(lh);
225                     goto bad;
226                 }
227                 code = IH_INC(lh, *(p->inode), parentId);
228                 FDH_REALLYCLOSE(fdP);
229                 IH_RELEASE(lh);
230                 if (code < 0)
231                     goto bad;
232                 continue;
233             }
234         }
235 #else
236         *(p->inode) =
237             IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
238                       vol.id, INODESPECIAL, p->inodeType, vol.parentId);
239 #endif
240
241         if (!VALID_INO(*(p->inode))) {
242             Log("VCreateVolume:  Problem creating %s file associated with volume header %s\n", p->description, volumePath);
243           bad:
244             if (handle)
245                 IH_RELEASE(handle);
246             RemoveInodes(stuff, device, vol.parentId, vol.id);
247             if (!*ec) {
248                 *ec = VNOVOL;
249             }
250             VDestroyVolumeDiskHeader(partition, volumeId, parentId);
251           bad_noheader:
252 # ifdef AFS_DEMAND_ATTACH_FS
253             if (locktype) {
254                 VUnlockVolumeById(volumeId, partition);
255             }
256 # endif /* AFS_DEMAND_ATTACH_FS */
257             return NULL;
258         }
259         IH_INIT(handle, device, vol.parentId, *(p->inode));
260         fdP = IH_OPEN(handle);
261         if (fdP == NULL) {
262             Log("VCreateVolume:  Problem iopen inode %s (err=%d)\n",
263                 PrintInode(stmp, *(p->inode)), errno);
264             goto bad;
265         }
266         if (FDH_PWRITE(fdP, (char *)&p->stamp, sizeof(p->stamp), 0) !=
267             sizeof(p->stamp)) {
268             Log("VCreateVolume:  Problem writing to inode %s (err=%d)\n",
269                 PrintInode(stmp, *(p->inode)), errno);
270             FDH_REALLYCLOSE(fdP);
271             goto bad;
272         }
273         FDH_REALLYCLOSE(fdP);
274         IH_RELEASE(handle);
275         nearInode = *(p->inode);
276     }
277
278     IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
279     fdP = IH_OPEN(handle);
280     if (fdP == NULL) {
281         Log("VCreateVolume:  Problem iopen inode %s (err=%d)\n",
282             PrintInode(stmp, tempHeader.volumeInfo), errno);
283         goto bad;
284     }
285     if (FDH_PWRITE(fdP, (char *)&vol, sizeof(vol), 0) != sizeof(vol)) {
286         Log("VCreateVolume:  Problem writing to  inode %s (err=%d)\n",
287             PrintInode(stmp, tempHeader.volumeInfo), errno);
288         FDH_REALLYCLOSE(fdP);
289         goto bad;
290     }
291     FDH_CLOSE(fdP);
292     IH_RELEASE(handle);
293
294     VolumeHeaderToDisk(&diskHeader, &tempHeader);
295     rc = VCreateVolumeDiskHeader(&diskHeader, partition);
296     if (rc) {
297         Log("VCreateVolume: Error %d trying to write volume header for "
298             "volume %u on partition %s; volume not created\n", rc,
299             vol.id, VPartitionPath(partition));
300         if (rc == EEXIST) {
301             *ec = VVOLEXISTS;
302         }
303         goto bad;
304     }
305
306 # ifdef AFS_DEMAND_ATTACH_FS
307     if (locktype) {
308         VUnlockVolumeById(volumeId, partition);
309     }
310 # endif /* AFS_DEMAND_ATTACH_FS */
311     return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
312 }
313 #endif /* FSSYNC_BUILD_CLIENT */
314
315
316 void
317 AssignVolumeName(VolumeDiskData * vol, char *name, char *ext)
318 {
319     VOL_LOCK;
320     AssignVolumeName_r(vol, name, ext);
321     VOL_UNLOCK;
322 }
323
324 void
325 AssignVolumeName_r(VolumeDiskData * vol, char *name, char *ext)
326 {
327     char *dot;
328     strncpy(vol->name, name, VNAMESIZE - 1);
329     vol->name[VNAMESIZE - 1] = '\0';
330     dot = strrchr(vol->name, '.');
331     if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
332         *dot = 0;
333     if (ext)
334         strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
335 }
336
337 afs_int32
338 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
339 {
340     /* The id and parentId fields are not copied; these are inviolate--the to volume
341      * is assumed to have already been created.  The id's cannot be changed once
342      * creation has taken place, since they are embedded in the various inodes associated
343      * with the volume.  The copydate is also inviolate--it always reflects the time
344      * this volume was created (compare with the creation date--the creation date of
345      * a backup volume is the creation date of the original parent, because the backup
346      * is used to backup the parent volume). */
347     Date copydate;
348     VolumeId id, parent;
349     id = to->id;
350     parent = to->parentId;
351     copydate = to->copyDate;
352     memcpy(to, from, sizeof(*from));
353     to->id = id;
354     to->parentId = parent;
355     to->copyDate = copydate;
356     to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
357     to->stamp.magic = VOLUMEINFOMAGIC;
358     to->stamp.version = VOLUMEINFOVERSION;
359     return 0;
360 }
361
362 afs_int32
363 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
364 {
365     afs_int32 code;
366
367     VOL_LOCK;
368     code = CopyVolumeHeader_r(from, to);
369     VOL_UNLOCK;
370     return (code);
371 }
372
373 void
374 ClearVolumeStats(VolumeDiskData * vol)
375 {
376     VOL_LOCK;
377     ClearVolumeStats_r(vol);
378     VOL_UNLOCK;
379 }
380
381 void
382 ClearVolumeStats_r(VolumeDiskData * vol)
383 {
384     memset(vol->weekUse, 0, sizeof(vol->weekUse));
385     vol->dayUse = 0;
386     vol->dayUseDate = 0;
387 }
388
389 void
390 CopyVolumeStats_r(VolumeDiskData * from, VolumeDiskData * to)
391 {
392     memcpy(to->weekUse, from->weekUse, sizeof(to->weekUse));
393     to->dayUse = from->dayUse;
394     to->dayUseDate = from->dayUseDate;
395     if (from->stat_initialized) {
396         memcpy(to->stat_reads, from->stat_reads, sizeof(to->stat_reads));
397         memcpy(to->stat_writes, from->stat_writes, sizeof(to->stat_writes));
398         memcpy(to->stat_fileSameAuthor, from->stat_fileSameAuthor,
399                sizeof(to->stat_fileSameAuthor));
400         memcpy(to->stat_fileDiffAuthor, from->stat_fileDiffAuthor,
401                sizeof(to->stat_fileDiffAuthor));
402         memcpy(to->stat_dirSameAuthor, from->stat_dirSameAuthor,
403                sizeof(to->stat_dirSameAuthor));
404         memcpy(to->stat_dirDiffAuthor, from->stat_dirDiffAuthor,
405                sizeof(to->stat_dirDiffAuthor));
406     }
407 }
408
409 void
410 CopyVolumeStats(VolumeDiskData * from, VolumeDiskData * to)
411 {
412     VOL_LOCK;
413     CopyVolumeStats_r(from, to);
414     VOL_UNLOCK;
415 }
416
417 /**
418  * read an existing volume disk header.
419  *
420  * @param[in]  volid  volume id
421  * @param[in]  dp     disk partition object
422  * @param[out] hdr    volume disk header or NULL
423  *
424  * @note if hdr is NULL, this is essentially an existence test for the vol
425  *       header
426  *
427  * @return operation status
428  *    @retval 0 success
429  *    @retval -1 volume header doesn't exist
430  *    @retval EIO failed to read volume header
431  *
432  * @internal
433  */
434 afs_int32
435 VReadVolumeDiskHeader(VolumeId volid,
436                       struct DiskPartition64 * dp,
437                       VolumeDiskHeader_t * hdr)
438 {
439     afs_int32 code = 0;
440     int fd;
441     char path[MAXPATHLEN];
442
443     snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
444              VPartitionPath(dp), afs_printable_uint32_lu(volid));
445     fd = open(path, O_RDONLY);
446     if (fd < 0) {
447         Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu (errno %d).\n",
448             afs_printable_uint32_lu(volid), errno);
449         code = -1;
450
451     } else if (hdr && read(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
452         Log("VReadVolumeDiskHeader: Couldn't read header for volume %lu.\n",
453             afs_printable_uint32_lu(volid));
454         code = EIO;
455     }
456
457     if (fd >= 0) {
458         close(fd);
459     }
460     return code;
461 }
462
463 #ifdef FSSYNC_BUILD_CLIENT
464 /**
465  * write an existing volume disk header.
466  *
467  * @param[in] hdr   volume disk header
468  * @param[in] dp    disk partition object
469  * @param[in] cr    assert if O_CREAT | O_EXCL should be passed to open()
470  *
471  * @return operation status
472  *    @retval 0 success
473  *    @retval -1 volume header doesn't exist
474  *    @retval EIO failed to write volume header
475  *
476  * @internal
477  */
478 static afs_int32
479 _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
480                         struct DiskPartition64 * dp,
481                         int flags)
482 {
483     afs_int32 code = 0;
484     int fd;
485     char path[MAXPATHLEN];
486
487 #ifdef AFS_DEMAND_ATTACH_FS
488     /* prevent racing with VGC scanners reading the vol header while we are
489      * writing it */
490     code = VPartHeaderLock(dp, READ_LOCK);
491     if (code) {
492         return EIO;
493     }
494 #endif /* AFS_DEMAND_ATTACH_FS */
495
496     flags |= O_RDWR;
497
498     snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
499              VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
500     fd = open(path, flags, 0644);
501     if (fd < 0) {
502         code = errno;
503         Log("_VWriteVolumeDiskHeader: Couldn't open header for volume %lu, "
504             "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
505     } else if (write(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
506         Log("_VWriteVolumeDiskHeader: Couldn't write header for volume %lu, "
507             "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
508         code = EIO;
509     }
510
511     if (fd >= 0) {
512         if (close(fd) != 0) {
513             Log("_VWriteVolumeDiskHeader: Error closing header for volume "
514                 "%lu, errno %d\n", afs_printable_uint32_lu(hdr->id), errno);
515         }
516     }
517
518 #ifdef AFS_DEMAND_ATTACH_FS
519     VPartHeaderUnlock(dp, READ_LOCK);
520 #endif /* AFS_DEMAND_ATTACH_FS */
521
522     return code;
523 }
524
525 /**
526  * write an existing volume disk header.
527  *
528  * @param[in] hdr   volume disk header
529  * @param[in] dp    disk partition object
530  *
531  * @return operation status
532  *    @retval 0 success
533  *    @retval ENOENT volume header doesn't exist
534  *    @retval EIO failed to write volume header
535  */
536 afs_int32
537 VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
538                        struct DiskPartition64 * dp)
539 {
540     afs_int32 code;
541
542 #ifdef AFS_DEMAND_ATTACH_FS
543     VolumeDiskHeader_t oldhdr;
544     int delvgc = 0, addvgc = 0;
545     SYNC_response res;
546
547     /* first, see if anything with the volume IDs have changed; if so, we
548      * need to update the VGC */
549
550     code = VReadVolumeDiskHeader(hdr->id, dp, &oldhdr);
551     if (code == 0 && (oldhdr.id != hdr->id || oldhdr.parent != hdr->parent)) {
552         /* the vol id or parent vol id changed; need to delete the VGC entry
553          * for the old vol id/parent, and add the new one */
554         delvgc = 1;
555         addvgc = 1;
556
557     } else if (code) {
558         /* couldn't get the old header info; add the new header info to the
559          * VGC in case it hasn't been added yet */
560         addvgc = 1;
561     }
562
563 #endif /* AFS_DEMAND_ATTACH_FS */
564
565     code = _VWriteVolumeDiskHeader(hdr, dp, 0);
566     if (code) {
567         goto done;
568     }
569
570 #ifdef AFS_DEMAND_ATTACH_FS
571     if (delvgc) {
572         memset(&res, 0, sizeof(res));
573         code = FSYNC_VGCDel(dp->name, oldhdr.parent, oldhdr.id, FSYNC_WHATEVER, &res);
574
575         /* unknown vol id is okay; it just further suggests the old header
576          * data was bogus, which is fine since we're trying to fix it */
577         if (code && res.hdr.reason != FSYNC_UNKNOWN_VOLID) {
578             Log("VWriteVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) "
579                 "failed with code %ld reason %ld\n", dp->name,
580                 afs_printable_uint32_lu(oldhdr.parent),
581                 afs_printable_uint32_lu(oldhdr.id),
582                 afs_printable_int32_ld(code),
583                 afs_printable_int32_ld(res.hdr.reason));
584         }
585
586     }
587     if (addvgc) {
588         memset(&res, 0, sizeof(res));
589         code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
590         if (code) {
591             Log("VWriteVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) "
592                 "failed with code %ld reason %ld\n", dp->name,
593                 afs_printable_uint32_lu(hdr->parent),
594                 afs_printable_uint32_lu(hdr->id),
595                 afs_printable_int32_ld(code),
596                 afs_printable_int32_ld(res.hdr.reason));
597         }
598     }
599
600 #endif /* AFS_DEMAND_ATTACH_FS */
601
602  done:
603     return code;
604 }
605
606 /**
607  * create and write a volume disk header to disk.
608  *
609  * @param[in] hdr   volume disk header
610  * @param[in] dp    disk partition object
611  *
612  * @return operation status
613  *    @retval 0 success
614  *    @retval EEXIST volume header already exists
615  *    @retval EIO failed to write volume header
616  *
617  * @internal
618  */
619 afs_int32
620 VCreateVolumeDiskHeader(VolumeDiskHeader_t * hdr,
621                         struct DiskPartition64 * dp)
622 {
623     afs_int32 code = 0;
624 #ifdef AFS_DEMAND_ATTACH_FS
625     SYNC_response res;
626 #endif /* AFS_DEMAND_ATTACH_FS */
627
628     code = _VWriteVolumeDiskHeader(hdr, dp, O_CREAT | O_EXCL);
629     if (code) {
630         goto done;
631     }
632
633 #ifdef AFS_DEMAND_ATTACH_FS
634     memset(&res, 0, sizeof(res));
635     code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
636     if (code) {
637         Log("VCreateVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) failed "
638             "with code %ld reason %ld\n", dp->name,
639             afs_printable_uint32_lu(hdr->parent),
640             afs_printable_uint32_lu(hdr->id),
641             afs_printable_int32_ld(code),
642             afs_printable_int32_ld(res.hdr.reason));
643     }
644 #endif /* AFS_DEMAND_ATTACH_FS */
645
646  done:
647     return code;
648 }
649
650
651 /**
652  * destroy a volume disk header.
653  *
654  * @param[in] dp      disk partition object
655  * @param[in] volid   volume id
656  * @param[in] parent  parent's volume id, 0 if unknown
657  *
658  * @return operation status
659  *    @retval 0 success
660  *
661  * @note if parent is 0, the parent volume ID will be looked up from the
662  * fileserver
663  *
664  * @note for non-DAFS, parent is currently ignored
665  */
666 afs_int32
667 VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
668                          VolumeId volid,
669                          VolumeId parent)
670 {
671     afs_int32 code = 0;
672     char path[MAXPATHLEN];
673 #ifdef AFS_DEMAND_ATTACH_FS
674     SYNC_response res;
675 #endif /* AFS_DEMAND_ATTACH_FS */
676
677     snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
678              VPartitionPath(dp), afs_printable_uint32_lu(volid));
679     code = unlink(path);
680     if (code) {
681         Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
682         goto done;
683     }
684
685 #ifdef AFS_DEMAND_ATTACH_FS
686     memset(&res, 0, sizeof(res));
687     if (!parent) {
688         FSSYNC_VGQry_response_t q_res;
689
690         code = FSYNC_VGCQuery(dp->name, volid, &q_res, &res);
691         if (code) {
692             Log("VDestroyVolumeDiskHeader: FSYNC_VGCQuery(%s, %lu) failed "
693                 "with code %ld, reason %ld\n", dp->name,
694                 afs_printable_uint32_lu(volid), afs_printable_int32_ld(code),
695                 afs_printable_int32_ld(res.hdr.reason));
696             goto done;
697         }
698
699         parent = q_res.rw;
700
701     }
702     code = FSYNC_VGCDel(dp->name, parent, volid, FSYNC_WHATEVER, &res);
703     if (code) {
704         Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) failed "
705             "with code %ld reason %ld\n", dp->name,
706             afs_printable_uint32_lu(parent),
707             afs_printable_uint32_lu(volid),
708             afs_printable_int32_ld(code),
709             afs_printable_int32_ld(res.hdr.reason));
710     }
711 #endif /* AFS_DEMAND_ATTACH_FS */
712
713  done:
714     return code;
715 }
716 #endif /* FSSYNC_BUILD_CLIENT */
717
718 /**
719  * handle a single vol header as part of VWalkVolumeHeaders.
720  *
721  * @param[in] dp      disk partition
722  * @param[in] volfunc function to call when a vol header is successfully read
723  * @param[in] name    full path name to the .vol header
724  * @param[out] hdr    header data read in from the .vol header
725  * @param[in] locked  1 if the partition headers are locked, 0 otherwise
726  * @param[in] rock    the rock to pass to volfunc
727  *
728  * @return operation status
729  *  @retval 0  success
730  *  @retval -1 fatal error, stop scanning
731  *  @retval 1  failed to read header
732  *  @retval 2  volfunc callback indicated error after header read
733  */
734 static int
735 _VHandleVolumeHeader(struct DiskPartition64 *dp, VWalkVolFunc volfunc,
736                      const char *name, struct VolumeDiskHeader *hdr,
737                      int locked, void *rock)
738 {
739     int error = 0;
740     FD_t fd;
741
742     if ((fd = OS_OPEN(name, O_RDONLY, 0)) == INVALID_FD
743         || OS_READ(fd, hdr, sizeof(*hdr))
744         != sizeof(*hdr)
745         || hdr->stamp.magic != VOLUMEHEADERMAGIC) {
746         error = 1;
747     }
748
749     if (fd != INVALID_FD) {
750         OS_CLOSE(fd);
751     }
752
753 #ifdef AFSFS_DEMAND_ATTACH_FS
754     if (locked) {
755         VPartHeaderUnlock(dp);
756     }
757 #endif /* AFS_DEMAND_ATTACH_FS */
758
759     if (!error && volfunc) {
760         /* the volume header seems fine; call the caller-supplied
761          * 'we-found-a-volume-header' function */
762         int last = 1;
763
764 #ifdef AFS_DEMAND_ATTACH_FS
765         if (!locked) {
766             last = 0;
767         }
768 #endif /* AFS_DEMAND_ATTACH_FS */
769
770         error = (*volfunc) (dp, name, hdr, last, rock);
771         if (error < 0) {
772             return -1;
773         }
774         if (error) {
775             error = 2;
776         }
777     }
778
779 #ifdef AFS_DEMAND_ATTACH_FS
780     if (error && !locked) {
781         int code;
782         /* retry reading the volume header under the partition
783          * header lock, just to be safe and ensure we're not
784          * racing something rewriting the vol header */
785         code = VPartHeaderLock(dp, WRITE_LOCK);
786         if (code) {
787             Log("Error acquiring partition write lock when "
788                 "looking at header %s\n", name);
789             return -1;
790         }
791
792         return _VHandleVolumeHeader(dp, volfunc, name, hdr, 1, rock);
793     }
794 #endif /* AFS_DEMAND_ATTACH_FS */
795
796     return error;
797 }
798
799 /**
800  * walk through the list of volume headers on a partition.
801  *
802  * This function looks through all of the .vol headers on a partition, reads in
803  * each header, and calls the supplied volfunc function on each one. If the
804  * header cannot be read (or volfunc returns a positive error code), DAFS will
805  * VPartHeaderExLock() and retry. If that fails, or if we are non-DAFS, errfunc
806  * will be called (which typically will unlink the problem volume header).
807  *
808  * If volfunc returns a negative error code, walking the partition will stop
809  * and we will return an error immediately.
810  *
811  * @param[in] dp       partition to walk
812  * @param[in] partpath the path opendir()
813  * @param[in] volfunc  the function to call when a header is encountered, or
814  *                     NULL to just skip over valid headers
815  * @param[in] errfunc  the function to call when a problematic header is
816  *                     encountered, or NULL to just skip over bad headers
817  * @param[in] rock     rock for volfunc and errfunc
818  *
819  * @see VWalkVolFunc
820  * @see VWalkErrFunc
821  *
822  * @return operation status
823  *  @retval 0 success
824  *  @retval negative fatal error, walk did not finish
825  */
826 int
827 VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
828                    VWalkVolFunc volfunc, VWalkErrFunc errfunc, void *rock)
829 {
830     DIR *dirp;
831     struct dirent *dentry;
832     int code = 0;
833     struct VolumeDiskHeader diskHeader;
834
835     dirp = opendir(partpath);
836     if (!dirp) {
837         Log("VWalkVolumeHeaders: cannot open directory %s\n", partpath);
838         code = -1;
839         goto done;
840     }
841
842     while ((dentry = readdir(dirp)) != NULL) {
843         char *p;
844         p = strrchr(dentry->d_name, '.');
845         if (p != NULL && strcmp(p, VHDREXT) == 0) {
846             char name[VMAXPATHLEN];
847
848             sprintf(name, "%s" OS_DIRSEP "%s", partpath, dentry->d_name);
849
850             code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
851             if (code < 0) {
852                 /* fatal error, stop walking */
853                 goto done;
854             }
855             if (code && errfunc) {
856                 /* error with header; call the caller-supplied vol error
857                  * function */
858
859                 struct VolumeDiskHeader *hdr = &diskHeader;
860                 if (code == 1) {
861                     /* we failed to read the header at all, so don't pass in
862                      * the header ptr */
863                     hdr = NULL;
864                 }
865                 (*errfunc) (dp, name, hdr, rock);
866             }
867             code = 0;
868         }
869     }
870  done:
871     if (dirp) {
872         closedir(dirp);
873         dirp = NULL;
874     }
875
876     return code;
877 }
878
879 /**
880  * initialize a struct VLockFile.
881  *
882  * @param[in] lf   struct VLockFile to initialize
883  * @param[in] path Full path to the file to use for locks. The string contents
884  *                 are copied.
885  */
886 void
887 VLockFileInit(struct VLockFile *lf, const char *path)
888 {
889     memset(lf, 0, sizeof(*lf));
890     lf->path = strdup(path);
891     lf->fd = INVALID_FD;
892     MUTEX_INIT(&lf->mutex, "vlockfile", MUTEX_DEFAULT, 0);
893 }
894
895 #ifdef AFS_NT40_ENV
896 static_inline FD_t
897 _VOpenPath(const char *path)
898 {
899     HANDLE handle;
900
901     handle = CreateFile(path,
902                         GENERIC_READ | GENERIC_WRITE,
903                         FILE_SHARE_READ | FILE_SHARE_WRITE,
904                         NULL,
905                         OPEN_ALWAYS,
906                         FILE_ATTRIBUTE_HIDDEN,
907                         NULL);
908     if (handle == INVALID_HANDLE_VALUE) {
909         return INVALID_FD;
910     }
911
912     return handle;
913 }
914
915 static_inline int
916 _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock)
917 {
918     DWORD flags = 0;
919     OVERLAPPED lap;
920
921     if (locktype == WRITE_LOCK) {
922         flags |= LOCKFILE_EXCLUSIVE_LOCK;
923     }
924     if (nonblock) {
925         flags |= LOCKFILE_FAIL_IMMEDIATELY;
926     }
927
928     memset(&lap, 0, sizeof(lap));
929     lap.Offset = offset;
930
931     if (!LockFileEx(handle, flags, 0, 1, 0, &lap)) {
932         if (GetLastError() == ERROR_LOCK_VIOLATION) {
933             return EBUSY;
934         }
935         return EIO;
936     }
937
938     return 0;
939 }
940
941 static_inline void
942 _VUnlockFd(FD_t handle, afs_uint32 offset)
943 {
944     OVERLAPPED lap;
945
946     memset(&lap, 0, sizeof(lap));
947     lap.Offset = offset;
948
949     UnlockFileEx(handle, 0, 1, 0, &lap);
950 }
951
952 static_inline void
953 _VCloseFd(FD_t handle)
954 {
955     CloseHandle(handle);
956 }
957
958 #else /* !AFS_NT40_ENV */
959
960 /**
961  * open a file on the local filesystem suitable for locking
962  *
963  * @param[in] path  abs path of the file to open
964  *
965  * @return file descriptor
966  *  @retval INVALID_FD failure opening file
967  */
968 static_inline FD_t
969 _VOpenPath(const char *path)
970 {
971     int fd;
972
973     fd = open(path, O_RDWR | O_CREAT, 0660);
974     if (fd < 0) {
975         return INVALID_FD;
976     }
977     return fd;
978 }
979
980 /**
981  * lock an offset in a file descriptor.
982  *
983  * @param[in] fd       file descriptor to lock
984  * @param[in] offset   offset in file to lock
985  * @param[in] locktype READ_LOCK or WRITE_LOCK
986  * @param[in] nonblock 1 to fail immediately, 0 to wait to acquire lock
987  *
988  * @return operation status
989  *  @retval 0 success
990  *  @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
991  *                specified
992  *  @retval EIO   error acquiring file lock
993  */
994 static_inline int
995 _VLockFd(FD_t fd, afs_uint32 offset, int locktype, int nonblock)
996 {
997     int l_type = F_WRLCK;
998     int cmd = AFS_SETLKW;
999     struct afs_st_flock sf;
1000
1001     if (locktype == READ_LOCK) {
1002         l_type = F_RDLCK;
1003     }
1004     if (nonblock) {
1005         cmd = AFS_SETLK;
1006     }
1007
1008     sf.l_start = offset;
1009     sf.l_len = 1;
1010     sf.l_type = l_type;
1011     sf.l_whence = SEEK_SET;
1012
1013     if (fcntl(fd, cmd, &sf)) {
1014         if (nonblock && (errno == EACCES || errno == EAGAIN)) {
1015             /* We asked for a nonblocking lock, and it was already locked */
1016             sf.l_pid = 0;
1017             if (fcntl(fd, F_GETLK, &sf) != 0 || sf.l_pid == 0) {
1018                 Log("_VLockFd: fcntl failed with error %d when trying to "
1019                     "query the conflicting lock for fd %d (locktype=%d, "
1020                     "offset=%lu)\n", errno, fd, locktype,
1021                     afs_printable_uint32_lu(offset));
1022             } else {
1023                 Log("_VLockFd: conflicting lock held on fd %d, offset %lu by "
1024                     "pid %ld (locktype=%d)\n", fd,
1025                     afs_printable_uint32_lu(offset), (long int)sf.l_pid,
1026                     locktype);
1027             }
1028             return EBUSY;
1029         }
1030         Log("_VLockFd: fcntl failed with error %d when trying to lock "
1031             "fd %d (locktype=%d, offset=%lu)\n", errno, fd, locktype,
1032             afs_printable_uint32_lu(offset));
1033         return EIO;
1034     }
1035
1036     return 0;
1037 }
1038
1039 /**
1040  * close a file descriptor used for file locking.
1041  *
1042  * @param[in] fd file descriptor to close
1043  */
1044 static_inline void
1045 _VCloseFd(FD_t fd)
1046 {
1047     if (close(fd)) {
1048         Log("_VCloseFd: error %d closing fd %d\n",
1049             errno, fd);
1050     }
1051 }
1052
1053 /**
1054  * unlock a file offset in a file descriptor.
1055  *
1056  * @param[in] fd file descriptor to unlock
1057  * @param[in] offset offset to unlock
1058  */
1059 static_inline void
1060 _VUnlockFd(FD_t fd, afs_uint32 offset)
1061 {
1062     struct afs_st_flock sf;
1063
1064     sf.l_start = offset;
1065     sf.l_len = 1;
1066     sf.l_type = F_UNLCK;
1067     sf.l_whence = SEEK_SET;
1068
1069     if (fcntl(fd, AFS_SETLK, &sf)) {
1070         Log("_VUnlockFd: fcntl failed with error %d when trying to unlock "
1071             "fd %d\n", errno, fd);
1072     }
1073 }
1074 #endif /* !AFS_NT40_ENV */
1075
1076 /**
1077  * reinitialize a struct VLockFile.
1078  *
1079  * Use this to close the lock file (unlocking any locks in it), and effectively
1080  * restore lf to the state it was in when it was initialized. This is the same
1081  * as unlocking all of the locks on the file, without having to remember what
1082  * all of the locks were. Do not unlock previously held locks after calling
1083  * this.
1084  *
1085  * @param[in] lf  struct VLockFile to reinit
1086  *
1087  * @pre nobody is waiting for a lock on this lockfile or otherwise using
1088  *      this lockfile at all
1089  */
1090 void
1091 VLockFileReinit(struct VLockFile *lf)
1092 {
1093     MUTEX_ENTER(&lf->mutex);
1094
1095     if (lf->fd != INVALID_FD) {
1096         _VCloseFd(lf->fd);
1097         lf->fd = INVALID_FD;
1098     }
1099
1100     lf->refcount = 0;
1101
1102     MUTEX_EXIT(&lf->mutex);
1103 }
1104
1105 /**
1106  * lock a file on disk for the process.
1107  *
1108  * @param[in] lf       the struct VLockFile representing the file to lock
1109  * @param[in] offset   the offset in the file to lock
1110  * @param[in] locktype READ_LOCK or WRITE_LOCK
1111  * @param[in] nonblock 0 to wait for conflicting locks to clear before
1112  *                     obtaining the lock; 1 to fail immediately if a
1113  *                     conflicting lock is held by someone else
1114  *
1115  * @return operation status
1116  *  @retval 0 success
1117  *  @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1118  *                specified
1119  *  @retval EIO   error acquiring file lock
1120  *
1121  * @note DAFS only
1122  *
1123  * @note do not try to lock/unlock the same offset in the same file from
1124  * different threads; use VGetDiskLock to protect threads from each other in
1125  * addition to other processes
1126  */
1127 int
1128 VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonblock)
1129 {
1130     int code;
1131
1132     osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1133
1134     MUTEX_ENTER(&lf->mutex);
1135
1136     if (lf->fd == INVALID_FD) {
1137         lf->fd = _VOpenPath(lf->path);
1138         if (lf->fd == INVALID_FD) {
1139             MUTEX_EXIT(&lf->mutex);
1140             return EIO;
1141         }
1142     }
1143
1144     lf->refcount++;
1145
1146     MUTEX_EXIT(&lf->mutex);
1147
1148     code = _VLockFd(lf->fd, offset, locktype, nonblock);
1149
1150     if (code) {
1151         MUTEX_ENTER(&lf->mutex);
1152         if (--lf->refcount < 1) {
1153             _VCloseFd(lf->fd);
1154             lf->fd = INVALID_FD;
1155         }
1156         MUTEX_EXIT(&lf->mutex);
1157     }
1158
1159     return code;
1160 }
1161
1162 void
1163 VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
1164 {
1165     MUTEX_ENTER(&lf->mutex);
1166
1167     osi_Assert(lf->fd != INVALID_FD);
1168
1169     if (--lf->refcount < 1) {
1170         _VCloseFd(lf->fd);
1171         lf->fd = INVALID_FD;
1172     } else {
1173         _VUnlockFd(lf->fd, offset);
1174     }
1175
1176     MUTEX_EXIT(&lf->mutex);
1177 }
1178
1179 #ifdef AFS_DEMAND_ATTACH_FS
1180
1181 /**
1182  * initialize a struct VDiskLock.
1183  *
1184  * @param[in] dl struct VDiskLock to initialize
1185  * @param[in] lf the struct VLockFile to associate with this disk lock
1186  */
1187 void
1188 VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset)
1189 {
1190     osi_Assert(lf);
1191     memset(dl, 0, sizeof(*dl));
1192     Lock_Init(&dl->rwlock);
1193     MUTEX_INIT(&dl->mutex, "disklock", MUTEX_DEFAULT, 0);
1194     CV_INIT(&dl->cv, "disklock cv", CV_DEFAULT, 0);
1195     dl->lockfile = lf;
1196     dl->offset = offset;
1197 }
1198
1199 /**
1200  * acquire a lock on a file on local disk.
1201  *
1202  * @param[in] dl       the VDiskLock structure corresponding to the file on disk
1203  * @param[in] locktype READ_LOCK if you want a read lock, or WRITE_LOCK if
1204  *                     you want a write lock
1205  * @param[in] nonblock 0 to wait for conflicting locks to clear before
1206  *                     obtaining the lock; 1 to fail immediately if a
1207  *                     conflicting lock is held by someone else
1208  *
1209  * @return operation status
1210  *  @retval 0 success
1211  *  @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1212  *                specified
1213  *  @retval EIO   error acquiring file lock
1214  *
1215  * @note DAFS only
1216  *
1217  * @note while normal fcntl-y locks on Unix systems generally only work per-
1218  * process, this interface also deals with locks between threads in the
1219  * process in addition to different processes acquiring the lock
1220  */
1221 int
1222 VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
1223 {
1224     int code = 0;
1225     osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1226
1227     if (nonblock) {
1228         if (locktype == READ_LOCK) {
1229             ObtainReadLockNoBlock(&dl->rwlock, code);
1230         } else {
1231             ObtainWriteLockNoBlock(&dl->rwlock, code);
1232         }
1233
1234         if (code) {
1235             return EBUSY;
1236         }
1237
1238     } else if (locktype == READ_LOCK) {
1239         ObtainReadLock(&dl->rwlock);
1240     } else {
1241         ObtainWriteLock(&dl->rwlock);
1242     }
1243
1244     MUTEX_ENTER(&dl->mutex);
1245
1246     if ((dl->flags & VDISKLOCK_ACQUIRING)) {
1247         /* Some other thread is waiting to acquire an fs lock. If nonblock=1,
1248          * we can return immediately, since we know we'll need to wait to
1249          * acquire. Otherwise, wait for the other thread to finish acquiring
1250          * the fs lock */
1251         if (nonblock) {
1252             code = EBUSY;
1253         } else {
1254             while ((dl->flags & VDISKLOCK_ACQUIRING)) {
1255                 CV_WAIT(&dl->cv, &dl->mutex);
1256             }
1257         }
1258     }
1259
1260     if (code == 0 && !(dl->flags & VDISKLOCK_ACQUIRED)) {
1261         /* no other thread holds the lock on the actual file; so grab one */
1262
1263         /* first try, don't block on the lock to see if we can get it without
1264          * waiting */
1265         code = VLockFileLock(dl->lockfile, dl->offset, locktype, 1);
1266
1267         if (code == EBUSY && !nonblock) {
1268
1269             /* mark that we are waiting on the fs lock */
1270             dl->flags |= VDISKLOCK_ACQUIRING;
1271
1272             MUTEX_EXIT(&dl->mutex);
1273             code = VLockFileLock(dl->lockfile, dl->offset, locktype, nonblock);
1274             MUTEX_ENTER(&dl->mutex);
1275
1276             dl->flags &= ~VDISKLOCK_ACQUIRING;
1277
1278             if (code == 0) {
1279                 dl->flags |= VDISKLOCK_ACQUIRED;
1280             }
1281
1282             CV_BROADCAST(&dl->cv);
1283         }
1284     }
1285
1286     if (code) {
1287         if (locktype == READ_LOCK) {
1288             ReleaseReadLock(&dl->rwlock);
1289         } else {
1290             ReleaseWriteLock(&dl->rwlock);
1291         }
1292     } else {
1293         /* successfully got the lock, so inc the number of unlocks we need
1294          * to do before we can unlock the actual file */
1295         ++dl->lockers;
1296     }
1297
1298     MUTEX_EXIT(&dl->mutex);
1299
1300     return code;
1301 }
1302
1303 /**
1304  * release a lock on a file on local disk.
1305  *
1306  * @param[in] dl the struct VDiskLock to release
1307  * @param[in] locktype READ_LOCK if you are unlocking a read lock, or
1308  *                     WRITE_LOCK if you are unlocking a write lock
1309  *
1310  * @return operation status
1311  *  @retval 0 success
1312  */
1313 void
1314 VReleaseDiskLock(struct VDiskLock *dl, int locktype)
1315 {
1316     osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1317
1318     MUTEX_ENTER(&dl->mutex);
1319     osi_Assert(dl->lockers > 0);
1320
1321     if (--dl->lockers < 1) {
1322         /* no threads are holding this lock anymore, so we can release the
1323          * actual disk lock */
1324         VLockFileUnlock(dl->lockfile, dl->offset);
1325         dl->flags &= ~VDISKLOCK_ACQUIRED;
1326     }
1327
1328     MUTEX_EXIT(&dl->mutex);
1329
1330     if (locktype == READ_LOCK) {
1331         ReleaseReadLock(&dl->rwlock);
1332     } else {
1333         ReleaseWriteLock(&dl->rwlock);
1334     }
1335 }
1336
1337 #endif /* AFS_DEMAND_ATTACH_FS */