reindent-20030715
[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 RCSID
21     ("$Header$");
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #ifdef AFS_NT40_ENV
27 #include <time.h>
28 #include <fcntl.h>
29 #else
30 #include <sys/time.h>
31 #include <sys/file.h>
32 #include <unistd.h>
33 #endif
34 #include <sys/stat.h>
35 #ifdef AFS_PTHREAD_ENV
36 #include <assert.h>
37 #else /* AFS_PTHREAD_ENV */
38 #include <afs/assert.h>
39 #endif /* AFS_PTHREAD_ENV */
40
41 #include <rx/xdr.h>
42 #include <afs/afsint.h>
43 #include "nfs.h"
44 #include <afs/errors.h>
45 #include "lock.h"
46 #include "lwp.h"
47 #include <afs/afssyscalls.h>
48 #include "ihandle.h"
49 #include <afs/afsutil.h>
50 #ifdef AFS_NT40_ENV
51 #include "ntops.h"
52 #include <io.h>
53 #endif
54 #include "vnode.h"
55 #include "volume.h"
56 #include "partition.h"
57 #include "viceinode.h"
58
59 #include "volinodes.h"
60 #ifdef  AFS_AIX_ENV
61 #include <sys/lockf.h>
62 #endif
63 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
64 #include <string.h>
65 #else
66 #include <strings.h>
67 #endif
68
69
70 /*@printflike@*/ extern void Log(const char *format, ...);
71
72 void AssignVolumeName();
73 void AssignVolumeName_r();
74 void ClearVolumeStats();
75 void ClearVolumeStats_r();
76
77
78 #define nFILES  (sizeof (stuff)/sizeof(struct stuff))
79
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 */
83
84 static void
85 RemoveInodes(Device dev, VolumeId vid)
86 {
87     register int i;
88     IHandle_t *handle;
89
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.
92      */
93     IH_INIT(handle, dev, vid, -1);
94     for (i = 0; i < nFILES; i++) {
95         Inode inode = *stuff[i].inode;
96         if (VALID_INO(inode))
97             IH_DEC(handle, inode, vid);
98     }
99     IH_RELEASE(handle);
100 }
101
102 Volume *
103 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
104 {                               /* Should be the same as volumeId if there is
105                                  * no parent */
106     Volume *retVal;
107     VOL_LOCK retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
108     VOL_UNLOCK return retVal;
109 }
110
111 Volume *
112 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
113 {                               /* Should be the same as volumeId if there is
114                                  * no parent */
115     VolumeDiskData vol;
116     int fd, i;
117     char headerName[32], volumePath[64];
118     Device device;
119     struct DiskPartition *partition;
120     struct VolumeDiskHeader diskHeader;
121     IHandle_t *handle;
122     FdHandle_t *fdP;
123     Inode nearInode = 0;
124
125     *ec = 0;
126     memset(&vol, 0, sizeof(vol));
127     vol.id = volumeId;
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 */
132
133     /* Initialize handle for error case below. */
134     handle = NULL;
135
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);
139         *ec = VNOVOL;
140         return NULL;
141     }
142 #if     defined(NEARINODE_HINT)
143     nearInodeHash(volumeId, nearInode);
144     nearInode %= partition->f_files;
145 #endif
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);
159     if (fd == -1) {
160         if (errno == EEXIST) {
161             Log("VCreateVolume: Header file %s already exists!\n",
162                 volumePath);
163             *ec = VVOLEXISTS;
164         } else {
165             Log("VCreateVolume: Couldn't create header file %s for volume %u\n", volumePath, vol.id);
166             *ec = VNOVOL;
167         }
168         return NULL;
169     }
170     device = partition->device;
171
172     for (i = 0; i < nFILES; i++) {
173         register struct stuff *p = &stuff[i];
174         if (p->obsolete)
175             continue;
176 #ifdef AFS_NAMEI_ENV
177         *(p->inode) =
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. */
184                 IHandle_t *lh;
185                 int code;
186
187 #ifdef AFS_NT40_ENV
188                 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
189 #else
190                 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
191 #endif
192                 IH_INIT(lh, device, parentId, *(p->inode));
193                 fdP = IH_OPEN(lh);
194                 if (fdP == NULL) {
195                     IH_RELEASE(lh);
196                     goto bad;
197                 }
198                 code = IH_INC(lh, *(p->inode), parentId);
199                 FDH_REALLYCLOSE(fdP);
200                 IH_RELEASE(lh);
201                 if (code < 0)
202                     goto bad;
203                 continue;
204             }
205         }
206 #else
207         *(p->inode) =
208             IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
209                       vol.id, INODESPECIAL, p->inodeType, vol.parentId);
210 #endif
211
212         if (!VALID_INO(*(p->inode))) {
213             Log("VCreateVolume:  Problem creating %s file associated with volume header %s\n", p->description, volumePath);
214           bad:
215             if (handle)
216                 IH_RELEASE(handle);
217             RemoveInodes(device, vol.id);
218             *ec = VNOVOL;
219             close(fd);
220             return NULL;
221         }
222         IH_INIT(handle, device, vol.parentId, *(p->inode));
223         fdP = IH_OPEN(handle);
224         if (fdP == NULL) {
225             Log("VCreateVolume:  Problem iopen inode %s (err=%d)\n",
226                 PrintInode(NULL, *(p->inode)), errno);
227             goto bad;
228         }
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);
233             goto bad;
234         }
235         if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
236             sizeof(p->stamp)) {
237             Log("VCreateVolume:  Problem writing to  inode %s (err=%d)\n",
238                 PrintInode(NULL, *(p->inode)), errno);
239             FDH_REALLYCLOSE(fdP);
240             goto bad;
241         }
242         FDH_REALLYCLOSE(fdP);
243         IH_RELEASE(handle);
244         nearInode = *(p->inode);
245     }
246
247     IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
248     fdP = IH_OPEN(handle);
249     if (fdP == NULL) {
250         Log("VCreateVolume:  Problem iopen inode %llu (err=%d)\n",
251             (afs_uintmax_t) tempHeader.volumeInfo, errno);
252         unlink(volumePath);
253         goto bad;
254     }
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);
259         unlink(volumePath);
260         goto bad;
261     }
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);
266         unlink(volumePath);
267         goto bad;
268     }
269     FDH_CLOSE(fdP);
270     IH_RELEASE(handle);
271
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);
275         unlink(volumePath);
276         goto bad;
277     }
278     fsync(fd);
279     close(fd);
280     return (VAttachVolumeByName(ec, partname, headerName, V_SECRETLY));
281 }
282
283
284 void
285 AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext)
286 {
287     VOL_LOCK AssignVolumeName_r(vol, name, ext);
288 VOL_UNLOCK}
289
290 void
291 AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext)
292 {
293     register char *dot;
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))
298         *dot = 0;
299     if (ext)
300         strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
301 }
302
303 afs_int32
304 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
305 {
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). */
313     Date copydate;
314     VolumeId id, parent;
315     id = to->id;
316     parent = to->parentId;
317     copydate = to->copyDate;
318     memcpy(to, from, sizeof(*from));
319     to->id = id;
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;
325     return 0;
326 }
327
328 afs_int32
329 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
330 {
331     afs_int32 code;
332
333     VOL_LOCK code = CopyVolumeHeader_r(from, to);
334     VOL_UNLOCK return (code);
335 }
336
337 void
338 ClearVolumeStats(register VolumeDiskData * vol)
339 {
340     VOL_LOCK ClearVolumeStats_r(vol);
341 VOL_UNLOCK}
342
343 void
344 ClearVolumeStats_r(register VolumeDiskData * vol)
345 {
346     memset(vol->weekUse, 0, sizeof(vol->weekUse));
347     vol->dayUse = 0;
348     vol->dayUseDate = 0;
349 }