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
112 VOL_LOCK retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
113 VOL_UNLOCK return retVal;
117 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
118 { /* Should be the same as volumeId if there is
122 char headerName[32], volumePath[64];
124 struct DiskPartition *partition;
125 struct VolumeDiskHeader diskHeader;
131 memset(&vol, 0, sizeof(vol));
133 vol.parentId = parentId;
134 vol.copyDate = time(0); /* The only date which really means when this
135 * @i(instance) of this volume was created.
136 * Creation date does not mean this */
138 /* Initialize handle for error case below. */
141 /* Verify that the parition is valid before writing to it. */
142 if (!(partition = VGetPartition_r(partname, 0))) {
143 Log("VCreateVolume: partition %s is not in service.\n", partname);
147 #if defined(NEARINODE_HINT)
148 nearInodeHash(volumeId, nearInode);
149 nearInode %= partition->f_files;
151 VLockPartition_r(partname);
152 memset(&tempHeader, 0, sizeof(tempHeader));
153 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
154 tempHeader.stamp.version = VOLUMEHEADERVERSION;
155 tempHeader.id = vol.id;
156 tempHeader.parent = vol.parentId;
157 vol.stamp.magic = VOLUMEINFOMAGIC;
158 vol.stamp.version = VOLUMEINFOVERSION;
159 vol.destroyMe = DESTROY_ME;
160 (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, vol.id);
161 (void)afs_snprintf(volumePath, sizeof volumePath, "%s/%s",
162 VPartitionPath(partition), headerName);
163 fd = afs_open(volumePath, O_CREAT | O_EXCL | O_WRONLY, 0600);
165 if (errno == EEXIST) {
166 Log("VCreateVolume: Header file %s already exists!\n",
170 Log("VCreateVolume: Couldn't create header file %s for volume %u\n", volumePath, vol.id);
175 device = partition->device;
177 for (i = 0; i < nFILES; i++) {
178 register struct stuff *p = &stuff[i];
183 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
184 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
185 INODESPECIAL, p->inodeType, vol.parentId);
186 if (!(VALID_INO(*(p->inode)))) {
187 if (errno == EEXIST) {
188 /* Increment the reference count instead. */
193 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
195 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
197 IH_INIT(lh, device, parentId, *(p->inode));
203 code = IH_INC(lh, *(p->inode), parentId);
204 FDH_REALLYCLOSE(fdP);
213 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
214 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
217 if (!VALID_INO(*(p->inode))) {
218 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
222 RemoveInodes(device, vol.id);
227 IH_INIT(handle, device, vol.parentId, *(p->inode));
228 fdP = IH_OPEN(handle);
230 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
231 PrintInode(NULL, *(p->inode)), errno);
234 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
235 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
236 PrintInode(NULL, *(p->inode)), errno);
237 FDH_REALLYCLOSE(fdP);
240 if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
242 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
243 PrintInode(NULL, *(p->inode)), errno);
244 FDH_REALLYCLOSE(fdP);
247 FDH_REALLYCLOSE(fdP);
249 nearInode = *(p->inode);
252 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
253 fdP = IH_OPEN(handle);
255 Log("VCreateVolume: Problem iopen inode %llu (err=%d)\n",
256 (afs_uintmax_t) tempHeader.volumeInfo, errno);
260 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
261 Log("VCreateVolume: Problem lseek inode %llu (err=%d)\n",
262 (afs_uintmax_t) tempHeader.volumeInfo, errno);
263 FDH_REALLYCLOSE(fdP);
267 if (FDH_WRITE(fdP, (char *)&vol, sizeof(vol)) != sizeof(vol)) {
268 Log("VCreateVolume: Problem writing to inode %llu (err=%d)\n",
269 (afs_uintmax_t) tempHeader.volumeInfo, errno);
270 FDH_REALLYCLOSE(fdP);
277 VolumeHeaderToDisk(&diskHeader, &tempHeader);
278 if (write(fd, &diskHeader, sizeof(diskHeader)) != sizeof(diskHeader)) {
279 Log("VCreateVolume: Unable to write volume header %s; volume %u not created\n", volumePath, vol.id);
285 return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
290 AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext)
292 VOL_LOCK AssignVolumeName_r(vol, name, ext);
296 AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext)
299 strncpy(vol->name, name, VNAMESIZE - 1);
300 vol->name[VNAMESIZE - 1] = '\0';
301 dot = strrchr(vol->name, '.');
302 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
305 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
309 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
311 /* The id and parentId fields are not copied; these are inviolate--the to volume
312 * is assumed to have already been created. The id's cannot be changed once
313 * creation has taken place, since they are embedded in the various inodes associated
314 * with the volume. The copydate is also inviolate--it always reflects the time
315 * this volume was created (compare with the creation date--the creation date of
316 * a backup volume is the creation date of the original parent, because the backup
317 * is used to backup the parent volume). */
321 parent = to->parentId;
322 copydate = to->copyDate;
323 memcpy(to, from, sizeof(*from));
325 to->parentId = parent;
326 to->copyDate = copydate;
327 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
328 to->stamp.magic = VOLUMEINFOMAGIC;
329 to->stamp.version = VOLUMEINFOVERSION;
334 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
338 VOL_LOCK code = CopyVolumeHeader_r(from, to);
339 VOL_UNLOCK return (code);
343 ClearVolumeStats(register VolumeDiskData * vol)
345 VOL_LOCK ClearVolumeStats_r(vol);
349 ClearVolumeStats_r(register VolumeDiskData * vol)
351 memset(vol->weekUse, 0, sizeof(vol->weekUse));