large-file-support-20030808
[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 #ifdef O_LARGEFILE
70 #define afs_open        open64
71 #else /* !O_LARGEFILE */
72 #define afs_open        open
73 #endif /* !O_LARGEFILE */
74
75 /*@printflike@*/ extern void Log(const char *format, ...);
76
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);
81
82
83 #define nFILES  (sizeof (stuff)/sizeof(struct stuff))
84
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 */
88
89 static void
90 RemoveInodes(Device dev, VolumeId vid)
91 {
92     register int i;
93     IHandle_t *handle;
94
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.
97      */
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);
103     }
104     IH_RELEASE(handle);
105 }
106
107 Volume *
108 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
109 {                               /* Should be the same as volumeId if there is
110                                  * no parent */
111     Volume *retVal;
112     VOL_LOCK retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
113     VOL_UNLOCK return retVal;
114 }
115
116 Volume *
117 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
118 {                               /* Should be the same as volumeId if there is
119                                  * no parent */
120     VolumeDiskData vol;
121     int fd, i;
122     char headerName[32], volumePath[64];
123     Device device;
124     struct DiskPartition *partition;
125     struct VolumeDiskHeader diskHeader;
126     IHandle_t *handle;
127     FdHandle_t *fdP;
128     Inode nearInode = 0;
129
130     *ec = 0;
131     memset(&vol, 0, sizeof(vol));
132     vol.id = volumeId;
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 */
137
138     /* Initialize handle for error case below. */
139     handle = NULL;
140
141     /* Verify that the parition is valid before writing to it. */
142     if (!(partition = VGetPartition(partname, 0))) {
143         Log("VCreateVolume: partition %s is not in service.\n", partname);
144         *ec = VNOVOL;
145         return NULL;
146     }
147 #if     defined(NEARINODE_HINT)
148     nearInodeHash(volumeId, nearInode);
149     nearInode %= partition->f_files;
150 #endif
151     VLockPartition(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);
164     if (fd == -1) {
165         if (errno == EEXIST) {
166             Log("VCreateVolume: Header file %s already exists!\n",
167                 volumePath);
168             *ec = VVOLEXISTS;
169         } else {
170             Log("VCreateVolume: Couldn't create header file %s for volume %u\n", volumePath, vol.id);
171             *ec = VNOVOL;
172         }
173         return NULL;
174     }
175     device = partition->device;
176
177     for (i = 0; i < nFILES; i++) {
178         register struct stuff *p = &stuff[i];
179         if (p->obsolete)
180             continue;
181 #ifdef AFS_NAMEI_ENV
182         *(p->inode) =
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. */
189                 IHandle_t *lh;
190                 int code;
191
192 #ifdef AFS_NT40_ENV
193                 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
194 #else
195                 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
196 #endif
197                 IH_INIT(lh, device, parentId, *(p->inode));
198                 fdP = IH_OPEN(lh);
199                 if (fdP == NULL) {
200                     IH_RELEASE(lh);
201                     goto bad;
202                 }
203                 code = IH_INC(lh, *(p->inode), parentId);
204                 FDH_REALLYCLOSE(fdP);
205                 IH_RELEASE(lh);
206                 if (code < 0)
207                     goto bad;
208                 continue;
209             }
210         }
211 #else
212         *(p->inode) =
213             IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
214                       vol.id, INODESPECIAL, p->inodeType, vol.parentId);
215 #endif
216
217         if (!VALID_INO(*(p->inode))) {
218             Log("VCreateVolume:  Problem creating %s file associated with volume header %s\n", p->description, volumePath);
219           bad:
220             if (handle)
221                 IH_RELEASE(handle);
222             RemoveInodes(device, vol.id);
223             *ec = VNOVOL;
224             close(fd);
225             return NULL;
226         }
227         IH_INIT(handle, device, vol.parentId, *(p->inode));
228         fdP = IH_OPEN(handle);
229         if (fdP == NULL) {
230             Log("VCreateVolume:  Problem iopen inode %s (err=%d)\n",
231                 PrintInode(NULL, *(p->inode)), errno);
232             goto bad;
233         }
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);
238             goto bad;
239         }
240         if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
241             sizeof(p->stamp)) {
242             Log("VCreateVolume:  Problem writing to  inode %s (err=%d)\n",
243                 PrintInode(NULL, *(p->inode)), errno);
244             FDH_REALLYCLOSE(fdP);
245             goto bad;
246         }
247         FDH_REALLYCLOSE(fdP);
248         IH_RELEASE(handle);
249         nearInode = *(p->inode);
250     }
251
252     IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
253     fdP = IH_OPEN(handle);
254     if (fdP == NULL) {
255         Log("VCreateVolume:  Problem iopen inode %llu (err=%d)\n",
256             (afs_uintmax_t) tempHeader.volumeInfo, errno);
257         unlink(volumePath);
258         goto bad;
259     }
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);
264         unlink(volumePath);
265         goto bad;
266     }
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);
271         unlink(volumePath);
272         goto bad;
273     }
274     FDH_CLOSE(fdP);
275     IH_RELEASE(handle);
276
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);
280         unlink(volumePath);
281         goto bad;
282     }
283     fsync(fd);
284     close(fd);
285     return (VAttachVolumeByName(ec, partname, headerName, V_SECRETLY));
286 }
287
288
289 void
290 AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext)
291 {
292     VOL_LOCK AssignVolumeName_r(vol, name, ext);
293 VOL_UNLOCK}
294
295 void
296 AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext)
297 {
298     register char *dot;
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))
303         *dot = 0;
304     if (ext)
305         strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
306 }
307
308 afs_int32
309 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
310 {
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). */
318     Date copydate;
319     VolumeId id, parent;
320     id = to->id;
321     parent = to->parentId;
322     copydate = to->copyDate;
323     memcpy(to, from, sizeof(*from));
324     to->id = id;
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;
330     return 0;
331 }
332
333 afs_int32
334 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
335 {
336     afs_int32 code;
337
338     VOL_LOCK code = CopyVolumeHeader_r(from, to);
339     VOL_UNLOCK return (code);
340 }
341
342 void
343 ClearVolumeStats(register VolumeDiskData * vol)
344 {
345     VOL_LOCK ClearVolumeStats_r(vol);
346 VOL_UNLOCK}
347
348 void
349 ClearVolumeStats_r(register VolumeDiskData * vol)
350 {
351     memset(vol->weekUse, 0, sizeof(vol->weekUse));
352     vol->dayUse = 0;
353     vol->dayUseDate = 0;
354 }