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>
22 #include <sys/types.h>
33 #ifdef AFS_PTHREAD_ENV
35 #else /* AFS_PTHREAD_ENV */
36 #include <afs/assert.h>
37 #endif /* AFS_PTHREAD_ENV */
40 #include <afs/afsint.h>
42 #include <afs/errors.h>
45 #include <afs/afssyscalls.h>
47 #include <afs/afsutil.h>
54 #include "partition.h"
55 #include "viceinode.h"
57 #include "volinodes.h"
58 #include "vol_prototypes.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 #define nFILES (sizeof (stuff)/sizeof(struct stuff))
79 /* Note: the volume creation functions herein leave the destroyMe flag in the
80 volume header ON: this means that the volumes will not be attached by the
81 file server and WILL BE DESTROYED the next time a system salvage is performed */
84 RemoveInodes(Device dev, VolumeId vid)
89 /* This relies on the fact that IDEC only needs the device and NT only
90 * needs the dev and vid to decrement volume special files.
92 IH_INIT(handle, dev, vid, -1);
93 for (i = 0; i < nFILES; i++) {
94 Inode inode = *stuff[i].inode;
96 IH_DEC(handle, inode, vid);
102 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
103 { /* Should be the same as volumeId if there is
107 retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
113 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
114 { /* Should be the same as volumeId if there is
118 char headerName[32], volumePath[64];
120 struct DiskPartition64 *partition;
121 struct VolumeDiskHeader diskHeader;
128 memset(&vol, 0, sizeof(vol));
130 vol.parentId = parentId;
131 vol.copyDate = time(0); /* The only date which really means when this
132 * @i(instance) of this volume was created.
133 * Creation date does not mean this */
135 /* Initialize handle for error case below. */
138 /* Verify that the parition is valid before writing to it. */
139 if (!(partition = VGetPartition_r(partname, 0))) {
140 Log("VCreateVolume: partition %s is not in service.\n", partname);
144 #if defined(NEARINODE_HINT)
145 nearInodeHash(volumeId, nearInode);
146 nearInode %= partition->f_files;
148 VGetVolumePath(ec, vol.id, &part, &name);
149 if (*ec == VNOVOL || !strcmp(partition->name, part)) {
150 /* this case is ok */
152 /* return EXDEV if it's a clone to an alternate partition
153 * otherwise assume it's a move */
154 if (vol.parentId != vol.id) {
159 VLockPartition_r(partname);
160 memset(&tempHeader, 0, sizeof(tempHeader));
161 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
162 tempHeader.stamp.version = VOLUMEHEADERVERSION;
163 tempHeader.id = vol.id;
164 tempHeader.parent = vol.parentId;
165 vol.stamp.magic = VOLUMEINFOMAGIC;
166 vol.stamp.version = VOLUMEINFOVERSION;
167 vol.destroyMe = DESTROY_ME;
168 (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(vol.id));
169 (void)afs_snprintf(volumePath, sizeof volumePath, "%s/%s",
170 VPartitionPath(partition), headerName);
171 fd = afs_open(volumePath, O_CREAT | O_EXCL | O_WRONLY, 0600);
173 if (errno == EEXIST) {
174 Log("VCreateVolume: Header file %s already exists!\n",
178 Log("VCreateVolume: Couldn't create header file %s for volume %u\n", volumePath, vol.id);
183 device = partition->device;
185 for (i = 0; i < nFILES; i++) {
186 register struct stuff *p = &stuff[i];
191 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
192 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
193 INODESPECIAL, p->inodeType, vol.parentId);
194 if (!(VALID_INO(*(p->inode)))) {
195 if (errno == EEXIST) {
196 /* Increment the reference count instead. */
201 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
203 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
205 IH_INIT(lh, device, parentId, *(p->inode));
211 code = IH_INC(lh, *(p->inode), parentId);
212 FDH_REALLYCLOSE(fdP);
221 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
222 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
225 if (!VALID_INO(*(p->inode))) {
226 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
230 RemoveInodes(device, vol.id);
235 IH_INIT(handle, device, vol.parentId, *(p->inode));
236 fdP = IH_OPEN(handle);
238 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
239 PrintInode(NULL, *(p->inode)), errno);
242 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
243 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
244 PrintInode(NULL, *(p->inode)), errno);
245 FDH_REALLYCLOSE(fdP);
248 if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
250 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
251 PrintInode(NULL, *(p->inode)), errno);
252 FDH_REALLYCLOSE(fdP);
255 FDH_REALLYCLOSE(fdP);
257 nearInode = *(p->inode);
260 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
261 fdP = IH_OPEN(handle);
263 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
264 PrintInode(NULL, tempHeader.volumeInfo), errno);
268 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
269 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
270 PrintInode(NULL, tempHeader.volumeInfo), errno);
271 FDH_REALLYCLOSE(fdP);
275 if (FDH_WRITE(fdP, (char *)&vol, sizeof(vol)) != sizeof(vol)) {
276 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
277 PrintInode(NULL, tempHeader.volumeInfo), errno);
278 FDH_REALLYCLOSE(fdP);
285 VolumeHeaderToDisk(&diskHeader, &tempHeader);
286 if (write(fd, &diskHeader, sizeof(diskHeader)) != sizeof(diskHeader)) {
287 Log("VCreateVolume: Unable to write volume header %s; volume %u not created\n", volumePath, vol.id);
293 return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
298 AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext)
301 AssignVolumeName_r(vol, name, ext);
306 AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext)
309 strncpy(vol->name, name, VNAMESIZE - 1);
310 vol->name[VNAMESIZE - 1] = '\0';
311 dot = strrchr(vol->name, '.');
312 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
315 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
319 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
321 /* The id and parentId fields are not copied; these are inviolate--the to volume
322 * is assumed to have already been created. The id's cannot be changed once
323 * creation has taken place, since they are embedded in the various inodes associated
324 * with the volume. The copydate is also inviolate--it always reflects the time
325 * this volume was created (compare with the creation date--the creation date of
326 * a backup volume is the creation date of the original parent, because the backup
327 * is used to backup the parent volume). */
331 parent = to->parentId;
332 copydate = to->copyDate;
333 memcpy(to, from, sizeof(*from));
335 to->parentId = parent;
336 to->copyDate = copydate;
337 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
338 to->stamp.magic = VOLUMEINFOMAGIC;
339 to->stamp.version = VOLUMEINFOVERSION;
344 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
349 code = CopyVolumeHeader_r(from, to);
355 ClearVolumeStats(register VolumeDiskData * vol)
358 ClearVolumeStats_r(vol);
363 ClearVolumeStats_r(register VolumeDiskData * vol)
365 memset(vol->weekUse, 0, sizeof(vol->weekUse));