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 #define afs_open open64
71 #else /* !O_LARGEFILE */
73 #endif /* !O_LARGEFILE */
75 /*@printflike@*/ extern void Log(const char *format, ...);
77 void AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext);
78 void AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext);
79 void ClearVolumeStats(register VolumeDiskData * vol);
80 void ClearVolumeStats_r(register VolumeDiskData * vol);
83 #define nFILES (sizeof (stuff)/sizeof(struct stuff))
85 /* Note: the volume creation functions herein leave the destroyMe flag in the
86 volume header ON: this means that the volumes will not be attached by the
87 file server and WILL BE DESTROYED the next time a system salvage is performed */
90 RemoveInodes(Device dev, VolumeId vid)
95 /* This relies on the fact that IDEC only needs the device and NT only
96 * needs the dev and vid to decrement volume special files.
98 IH_INIT(handle, dev, vid, -1);
99 for (i = 0; i < nFILES; i++) {
100 Inode inode = *stuff[i].inode;
101 if (VALID_INO(inode))
102 IH_DEC(handle, inode, vid);
108 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
109 { /* Should be the same as volumeId if there is
113 retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
119 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
120 { /* Should be the same as volumeId if there is
124 char headerName[32], volumePath[64];
126 struct DiskPartition *partition;
127 struct VolumeDiskHeader diskHeader;
133 memset(&vol, 0, sizeof(vol));
135 vol.parentId = parentId;
136 vol.copyDate = time(0); /* The only date which really means when this
137 * @i(instance) of this volume was created.
138 * Creation date does not mean this */
140 /* Initialize handle for error case below. */
143 /* Verify that the parition is valid before writing to it. */
144 if (!(partition = VGetPartition_r(partname, 0))) {
145 Log("VCreateVolume: partition %s is not in service.\n", partname);
149 #if defined(NEARINODE_HINT)
150 nearInodeHash(volumeId, nearInode);
151 nearInode %= partition->f_files;
153 VLockPartition_r(partname);
154 memset(&tempHeader, 0, sizeof(tempHeader));
155 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
156 tempHeader.stamp.version = VOLUMEHEADERVERSION;
157 tempHeader.id = vol.id;
158 tempHeader.parent = vol.parentId;
159 vol.stamp.magic = VOLUMEINFOMAGIC;
160 vol.stamp.version = VOLUMEINFOVERSION;
161 vol.destroyMe = DESTROY_ME;
162 (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, vol.id);
163 (void)afs_snprintf(volumePath, sizeof volumePath, "%s/%s",
164 VPartitionPath(partition), headerName);
165 fd = afs_open(volumePath, O_CREAT | O_EXCL | O_WRONLY, 0600);
167 if (errno == EEXIST) {
168 Log("VCreateVolume: Header file %s already exists!\n",
172 Log("VCreateVolume: Couldn't create header file %s for volume %u\n", volumePath, vol.id);
177 device = partition->device;
179 for (i = 0; i < nFILES; i++) {
180 register struct stuff *p = &stuff[i];
185 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
186 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
187 INODESPECIAL, p->inodeType, vol.parentId);
188 if (!(VALID_INO(*(p->inode)))) {
189 if (errno == EEXIST) {
190 /* Increment the reference count instead. */
195 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
197 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
199 IH_INIT(lh, device, parentId, *(p->inode));
205 code = IH_INC(lh, *(p->inode), parentId);
206 FDH_REALLYCLOSE(fdP);
215 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
216 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
219 if (!VALID_INO(*(p->inode))) {
220 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
224 RemoveInodes(device, vol.id);
229 IH_INIT(handle, device, vol.parentId, *(p->inode));
230 fdP = IH_OPEN(handle);
232 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
233 PrintInode(NULL, *(p->inode)), errno);
236 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
237 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
238 PrintInode(NULL, *(p->inode)), errno);
239 FDH_REALLYCLOSE(fdP);
242 if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
244 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
245 PrintInode(NULL, *(p->inode)), errno);
246 FDH_REALLYCLOSE(fdP);
249 FDH_REALLYCLOSE(fdP);
251 nearInode = *(p->inode);
254 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
255 fdP = IH_OPEN(handle);
257 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
258 PrintInode(NULL, tempHeader.volumeInfo), errno);
262 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
263 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
264 PrintInode(NULL, tempHeader.volumeInfo), errno);
265 FDH_REALLYCLOSE(fdP);
269 if (FDH_WRITE(fdP, (char *)&vol, sizeof(vol)) != sizeof(vol)) {
270 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
271 PrintInode(NULL, tempHeader.volumeInfo), errno);
272 FDH_REALLYCLOSE(fdP);
279 VolumeHeaderToDisk(&diskHeader, &tempHeader);
280 if (write(fd, &diskHeader, sizeof(diskHeader)) != sizeof(diskHeader)) {
281 Log("VCreateVolume: Unable to write volume header %s; volume %u not created\n", volumePath, vol.id);
287 return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
292 AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext)
295 AssignVolumeName_r(vol, name, ext);
300 AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext)
303 strncpy(vol->name, name, VNAMESIZE - 1);
304 vol->name[VNAMESIZE - 1] = '\0';
305 dot = strrchr(vol->name, '.');
306 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
309 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
313 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
315 /* The id and parentId fields are not copied; these are inviolate--the to volume
316 * is assumed to have already been created. The id's cannot be changed once
317 * creation has taken place, since they are embedded in the various inodes associated
318 * with the volume. The copydate is also inviolate--it always reflects the time
319 * this volume was created (compare with the creation date--the creation date of
320 * a backup volume is the creation date of the original parent, because the backup
321 * is used to backup the parent volume). */
325 parent = to->parentId;
326 copydate = to->copyDate;
327 memcpy(to, from, sizeof(*from));
329 to->parentId = parent;
330 to->copyDate = copydate;
331 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
332 to->stamp.magic = VOLUMEINFOMAGIC;
333 to->stamp.version = VOLUMEINFOVERSION;
338 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
343 code = CopyVolumeHeader_r(from, to);
349 ClearVolumeStats(register VolumeDiskData * vol)
352 ClearVolumeStats_r(vol);
357 ClearVolumeStats_r(register VolumeDiskData * vol)
359 memset(vol->weekUse, 0, sizeof(vol->weekUse));