2 * Copyright 2000, International Business Machines Corporation and others.
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
13 Institution: The Information Technology Center, Carnegie-Mellon University
17 #include <afsconfig.h>
18 #include <afs/param.h>
24 #include <sys/types.h>
35 #ifdef AFS_PTHREAD_ENV
37 #else /* AFS_PTHREAD_ENV */
38 #include <afs/assert.h>
39 #endif /* AFS_PTHREAD_ENV */
42 #include <afs/afsint.h>
44 #include <afs/errors.h>
47 #include <afs/afssyscalls.h>
49 #include <afs/afsutil.h>
56 #include "partition.h"
57 #include "viceinode.h"
59 #include "volinodes.h"
61 #include <sys/lockf.h>
63 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
70 /*@printflike@*/ extern void Log(const char *format, ...);
72 void AssignVolumeName();
73 void AssignVolumeName_r();
74 void ClearVolumeStats();
75 void ClearVolumeStats_r();
78 #define nFILES (sizeof (stuff)/sizeof(struct stuff))
80 /* Note: the volume creation functions herein leave the destroyMe flag in the
81 volume header ON: this means that the volumes will not be attached by the
82 file server and WILL BE DESTROYED the next time a system salvage is performed */
85 RemoveInodes(Device dev, VolumeId vid)
90 /* This relies on the fact that IDEC only needs the device and NT only
91 * needs the dev and vid to decrement volume special files.
93 IH_INIT(handle, dev, vid, -1);
94 for (i = 0; i < nFILES; i++) {
95 Inode inode = *stuff[i].inode;
97 IH_DEC(handle, inode, vid);
103 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
104 { /* Should be the same as volumeId if there is
107 VOL_LOCK retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
108 VOL_UNLOCK return retVal;
112 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
113 { /* Should be the same as volumeId if there is
117 char headerName[32], volumePath[64];
119 struct DiskPartition *partition;
120 struct VolumeDiskHeader diskHeader;
126 memset(&vol, 0, sizeof(vol));
128 vol.parentId = parentId;
129 vol.copyDate = time(0); /* The only date which really means when this
130 * @i(instance) of this volume was created.
131 * Creation date does not mean this */
133 /* Initialize handle for error case below. */
136 /* Verify that the parition is valid before writing to it. */
137 if (!(partition = VGetPartition(partname, 0))) {
138 Log("VCreateVolume: partition %s is not in service.\n", partname);
142 #if defined(NEARINODE_HINT)
143 nearInodeHash(volumeId, nearInode);
144 nearInode %= partition->f_files;
146 VLockPartition(partname);
147 memset(&tempHeader, 0, sizeof(tempHeader));
148 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
149 tempHeader.stamp.version = VOLUMEHEADERVERSION;
150 tempHeader.id = vol.id;
151 tempHeader.parent = vol.parentId;
152 vol.stamp.magic = VOLUMEINFOMAGIC;
153 vol.stamp.version = VOLUMEINFOVERSION;
154 vol.destroyMe = DESTROY_ME;
155 (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, vol.id);
156 (void)afs_snprintf(volumePath, sizeof volumePath, "%s/%s",
157 VPartitionPath(partition), headerName);
158 fd = open(volumePath, O_CREAT | O_EXCL | O_WRONLY, 0600);
160 if (errno == EEXIST) {
161 Log("VCreateVolume: Header file %s already exists!\n",
165 Log("VCreateVolume: Couldn't create header file %s for volume %u\n", volumePath, vol.id);
170 device = partition->device;
172 for (i = 0; i < nFILES; i++) {
173 register struct stuff *p = &stuff[i];
178 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
179 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
180 INODESPECIAL, p->inodeType, vol.parentId);
181 if (!(VALID_INO(*(p->inode)))) {
182 if (errno == EEXIST) {
183 /* Increment the reference count instead. */
188 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
190 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
192 IH_INIT(lh, device, parentId, *(p->inode));
198 code = IH_INC(lh, *(p->inode), parentId);
199 FDH_REALLYCLOSE(fdP);
208 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
209 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
212 if (!VALID_INO(*(p->inode))) {
213 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
217 RemoveInodes(device, vol.id);
222 IH_INIT(handle, device, vol.parentId, *(p->inode));
223 fdP = IH_OPEN(handle);
225 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
226 PrintInode(NULL, *(p->inode)), errno);
229 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
230 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
231 PrintInode(NULL, *(p->inode)), errno);
232 FDH_REALLYCLOSE(fdP);
235 if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
237 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
238 PrintInode(NULL, *(p->inode)), errno);
239 FDH_REALLYCLOSE(fdP);
242 FDH_REALLYCLOSE(fdP);
244 nearInode = *(p->inode);
247 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
248 fdP = IH_OPEN(handle);
250 Log("VCreateVolume: Problem iopen inode %llu (err=%d)\n",
251 (afs_uintmax_t) tempHeader.volumeInfo, errno);
255 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
256 Log("VCreateVolume: Problem lseek inode %llu (err=%d)\n",
257 (afs_uintmax_t) tempHeader.volumeInfo, errno);
258 FDH_REALLYCLOSE(fdP);
262 if (FDH_WRITE(fdP, (char *)&vol, sizeof(vol)) != sizeof(vol)) {
263 Log("VCreateVolume: Problem writing to inode %llu (err=%d)\n",
264 (afs_uintmax_t) tempHeader.volumeInfo, errno);
265 FDH_REALLYCLOSE(fdP);
272 VolumeHeaderToDisk(&diskHeader, &tempHeader);
273 if (write(fd, &diskHeader, sizeof(diskHeader)) != sizeof(diskHeader)) {
274 Log("VCreateVolume: Unable to write volume header %s; volume %u not created\n", volumePath, vol.id);
280 return (VAttachVolumeByName(ec, partname, headerName, V_SECRETLY));
285 AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext)
287 VOL_LOCK AssignVolumeName_r(vol, name, ext);
291 AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext)
294 strncpy(vol->name, name, VNAMESIZE - 1);
295 vol->name[VNAMESIZE - 1] = '\0';
296 dot = strrchr(vol->name, '.');
297 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
300 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
304 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
306 /* The id and parentId fields are not copied; these are inviolate--the to volume
307 * is assumed to have already been created. The id's cannot be changed once
308 * creation has taken place, since they are embedded in the various inodes associated
309 * with the volume. The copydate is also inviolate--it always reflects the time
310 * this volume was created (compare with the creation date--the creation date of
311 * a backup volume is the creation date of the original parent, because the backup
312 * is used to backup the parent volume). */
316 parent = to->parentId;
317 copydate = to->copyDate;
318 memcpy(to, from, sizeof(*from));
320 to->parentId = parent;
321 to->copyDate = copydate;
322 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
323 to->stamp.magic = VOLUMEINFOMAGIC;
324 to->stamp.version = VOLUMEINFOVERSION;
329 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
333 VOL_LOCK code = CopyVolumeHeader_r(from, to);
334 VOL_UNLOCK return (code);
338 ClearVolumeStats(register VolumeDiskData * vol)
340 VOL_LOCK ClearVolumeStats_r(vol);
344 ClearVolumeStats_r(register VolumeDiskData * vol)
346 memset(vol->weekUse, 0, sizeof(vol->weekUse));