afs: do not allow two shutdown sequences in parallel
[openafs.git] / src / afs / LINUX / osi_file.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 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include <linux/module.h> /* early to avoid printf->printk mapping */
15 #include "afs/sysincludes.h"    /* Standard vendor system headers */
16 #include "afsincludes.h"        /* Afs-based standard headers */
17 #include "afs/afs_stats.h"      /* afs statistics */
18 #include <linux/namei.h>
19
20 #if defined(HAVE_LINUX_EXPORTFS_H)
21 #include <linux/exportfs.h>
22 #endif
23 #include "osi_compat.h"
24
25 int cache_fh_type = -1;
26 int cache_fh_len = -1;
27
28 afs_lock_t afs_xosi;            /* lock is for tvattr */
29 extern struct osi_dev cacheDev;
30 extern struct vfsmount *afs_cacheMnt;
31 extern struct super_block *afs_cacheSBp;
32 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
33 extern struct cred *cache_creds;
34 #endif
35
36 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
37 int
38 afs_fh_acceptable(void *context, struct dentry *dp)
39 {
40     return 1;
41 }
42
43 struct file *
44 afs_linux_raw_open(afs_dcache_id_t *ainode)
45 {
46     struct inode *tip = NULL;
47     struct dentry *dp = NULL;
48     struct file* filp;
49
50     dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
51                 afs_fh_acceptable);
52     if ((!dp) || IS_ERR(dp))
53            osi_Panic("Can't get dentry\n");
54     tip = dp->d_inode;
55     tip->i_flags |= S_NOATIME;  /* Disable updating access times. */
56
57 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
58     /* Use stashed credentials - prevent selinux/apparmor problems  */
59     filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, cache_creds);
60     if (IS_ERR(filp))
61         filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, current_cred());
62 #else
63     filp = dentry_open(dget(dp), mntget(afs_cacheMnt), O_RDWR);
64 #endif
65     if (IS_ERR(filp))
66         osi_Panic("Can't open file: %d\n", (int) PTR_ERR(filp));
67
68     dput(dp);
69
70     return filp;
71 }
72
73 void *
74 osi_UFSOpen(afs_dcache_id_t *ainode)
75 {
76     struct osi_file *afile = NULL;
77     extern int cacheDiskType;
78
79     AFS_STATCNT(osi_UFSOpen);
80     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
81         osi_Panic("UFSOpen called for non-UFS cache\n");
82     }
83     if (!afs_osicred_initialized) {
84         /* valid for alpha_osf, SunOS, Ultrix */
85         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
86         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
87         afs_osicred_initialized = 1;
88     }
89     AFS_GUNLOCK();
90     afile = kmalloc(sizeof(struct osi_file), GFP_NOFS);
91     if (!afile) {
92         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
93                   (int)sizeof(struct osi_file));
94     }
95     memset(afile, 0, sizeof(struct osi_file));
96
97     afile->filp = afs_linux_raw_open(ainode);
98     afile->size = i_size_read(FILE_INODE(afile->filp));
99     AFS_GLOCK();
100     afile->offset = 0;
101     afile->proc = (int (*)())0;
102     return (void *)afile;
103 }
104
105 /*
106  * Given a dentry, return the file handle as encoded by the filesystem.
107  * We can't assume anything about the length (words, not bytes).
108  * The cache has to live on a single filesystem with uniform file 
109  * handles, otherwise we panic.
110  */
111 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
112     int max_len;
113     int type;
114
115     if (cache_fh_len > 0)
116         max_len = cache_fh_len;
117     else
118         max_len = MAX_FH_LEN;
119     type = afs_get_fh_from_dentry(dp, ainode, &max_len);
120     if (type == 255) {
121         osi_Panic("File handle encoding failed\n");
122     }
123     if (cache_fh_type < 0)
124         cache_fh_type = type;
125     if (cache_fh_len < 0) {
126         cache_fh_len = max_len;
127     }
128     if (type != cache_fh_type || max_len != cache_fh_len) {
129         osi_Panic("Inconsistent file handles within cache\n");
130     }
131 }
132
133 int
134 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
135 {
136     AFS_STATCNT(osi_Stat);
137     ObtainWriteLock(&afs_xosi, 320);
138     astat->size = i_size_read(OSIFILE_INODE(afile));
139     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
140     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
141
142     ReleaseWriteLock(&afs_xosi);
143     return 0;
144 }
145
146 int
147 osi_UFSClose(struct osi_file *afile)
148 {
149     AFS_STATCNT(osi_Close);
150     if (afile) {
151         if (OSIFILE_INODE(afile)) {
152             filp_close(afile->filp, NULL);
153         }
154     }
155     kfree(afile);
156     return 0;
157 }
158
159 int
160 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
161 {
162     afs_int32 code;
163     struct osi_stat tstat;
164     struct iattr newattrs;
165     struct inode *inode = OSIFILE_INODE(afile);
166     AFS_STATCNT(osi_Truncate);
167
168     /* This routine only shrinks files, and most systems
169      * have very slow truncates, even when the file is already
170      * small enough.  Check now and save some time.
171      */
172     code = afs_osi_Stat(afile, &tstat);
173     if (code || tstat.size <= asize)
174         return code;
175     ObtainWriteLock(&afs_xosi, 321);
176     AFS_GUNLOCK();
177     afs_linux_lock_inode(inode);
178 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
179     down_write(&inode->i_alloc_sem);
180 #endif
181     newattrs.ia_size = asize;
182     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
183     newattrs.ia_ctime = CURRENT_TIME;
184
185     /* avoid notify_change() since it wants to update dentry->d_parent */
186     code = inode_change_ok(inode, &newattrs);
187     if (!code)
188         code = afs_inode_setattr(afile, &newattrs);
189     if (!code)
190         truncate_inode_pages(&inode->i_data, asize);
191     code = -code;
192 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
193     up_write(&inode->i_alloc_sem);
194 #endif
195     afs_linux_unlock_inode(inode);
196     AFS_GLOCK();
197     ReleaseWriteLock(&afs_xosi);
198     return code;
199 }
200
201
202 /* Generic read interface */
203 int
204 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
205              afs_int32 asize)
206 {
207     struct uio auio;
208     struct iovec iov;
209     afs_int32 code;
210
211     memset(&auio, 0, sizeof(auio));
212     memset(&iov, 0, sizeof(iov));
213
214     AFS_STATCNT(osi_Read);
215
216     /*
217      * If the osi_file passed in is NULL, panic only if AFS is not shutting
218      * down. No point in crashing when we are already shutting down
219      */
220     if (!afile) {
221         if (afs_shuttingdown == AFS_RUNNING)
222             osi_Panic("osi_Read called with null param");
223         else
224             return -EIO;
225     }
226
227     if (offset != -1)
228         afile->offset = offset;
229     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
230     AFS_GUNLOCK();
231     code = osi_rdwr(afile, &auio, UIO_READ);
232     AFS_GLOCK();
233     if (code == 0) {
234         code = asize - auio.uio_resid;
235         afile->offset += code;
236     } else {
237         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
238                    ICL_TYPE_INT32, code);
239         if (code > 0) {
240             code = -code;
241         }
242     }
243     return code;
244 }
245
246 /* Generic write interface */
247 int
248 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
249               afs_int32 asize)
250 {
251     struct uio auio;
252     struct iovec iov;
253     afs_int32 code;
254
255     memset(&auio, 0, sizeof(auio));
256     memset(&iov, 0, sizeof(iov));
257
258     AFS_STATCNT(osi_Write);
259
260     if (!afile) {
261         if (afs_shuttingdown == AFS_RUNNING)
262             osi_Panic("afs_osi_Write called with null param");
263         else
264             return -EIO;
265     }
266
267     if (offset != -1)
268         afile->offset = offset;
269     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
270     AFS_GUNLOCK();
271     code = osi_rdwr(afile, &auio, UIO_WRITE);
272     AFS_GLOCK();
273     if (code == 0) {
274         code = asize - auio.uio_resid;
275         afile->offset += code;
276     } else {
277         if (code == ENOSPC)
278             afs_warnuser
279                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
280         if (code > 0) {
281             code = -code;
282         }
283     }
284
285     if (afile->proc)
286         (*afile->proc)(afile, code);
287
288     return code;
289 }
290
291
292 /*  This work should be handled by physstrat in ca/machdep.c.
293     This routine written from the RT NFS port strategy routine.
294     It has been generalized a bit, but should still be pretty clear. */
295 int
296 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
297 {
298     afs_int32 returnCode;
299
300     AFS_STATCNT(osi_MapStrategy);
301     returnCode = (*aproc) (bp);
302
303     return returnCode;
304 }
305
306 void
307 shutdown_osifile(void)
308 {
309     AFS_STATCNT(shutdown_osifile);
310     if (afs_cold_shutdown) {
311         afs_osicred_initialized = 0;
312     }
313 }
314
315 /* Intialize cache device info and fragment size for disk cache partition. */
316 int
317 osi_InitCacheInfo(char *aname)
318 {
319     int code;
320     extern afs_dcache_id_t cacheInode;
321     struct dentry *dp;
322     extern struct osi_dev cacheDev;
323     extern afs_int32 afs_fsfragsize;
324     extern struct super_block *afs_cacheSBp;
325     extern struct vfsmount *afs_cacheMnt;
326     code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
327     if (code)
328         return ENOENT;
329
330     osi_get_fh(dp, &cacheInode.ufs);
331     cacheDev.dev = dp->d_inode->i_sb->s_dev;
332     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
333     afs_cacheSBp = dp->d_inode->i_sb;
334
335     dput(dp);
336
337     afs_init_sb_export_ops(afs_cacheSBp);
338
339     return 0;
340 }
341
342
343 /* osi_rdwr
344  * seek, then read or write to an open inode. addrp points to data in
345  * kernel space.
346  */
347 int
348 osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
349 {
350     struct file *filp = osifile->filp;
351     mm_segment_t old_fs = {0};
352     int code = 0;
353     struct iovec *iov;
354     size_t count;
355     unsigned long savelim;
356     loff_t pos;
357
358     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
359     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
360
361     if (uiop->uio_seg == AFS_UIOSYS) {
362         /* Switch into user space */
363         old_fs = get_fs();
364         set_fs(get_ds());
365     }
366
367     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
368         iov = uiop->uio_iov;
369         count = iov->iov_len;
370         if (count == 0) {
371             uiop->uio_iov++;
372             uiop->uio_iovcnt--;
373             continue;
374         }
375
376         pos = uiop->uio_offset;
377         if (rw == UIO_READ)
378             code = afs_file_read(filp, iov->iov_base, count, &pos);
379         else
380             code = afs_file_write(filp, iov->iov_base, count, &pos);
381
382         if (code < 0) {
383             code = -code;
384             break;
385         } else if (code == 0) {
386             /*
387              * This is bad -- we can't read any more data from the
388              * file, but we have no good way of signaling a partial
389              * read either.
390              */
391             code = EIO;
392             break;
393         }
394
395         iov->iov_base += code;
396         iov->iov_len -= code;
397         uiop->uio_resid -= code;
398         uiop->uio_offset += code;
399         code = 0;
400     }
401
402     if (uiop->uio_seg == AFS_UIOSYS) {
403         /* Switch back into kernel space */
404         set_fs(old_fs);
405     }
406
407     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
408
409     return code;
410 }
411
412 /* setup_uio 
413  * Setup a uio struct.
414  */
415 void
416 setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
417           int count, uio_flag_t flag, uio_seg_t seg)
418 {
419     iovecp->iov_base = (char *)buf;
420     iovecp->iov_len = count;
421     uiop->uio_iov = iovecp;
422     uiop->uio_iovcnt = 1;
423     uiop->uio_offset = pos;
424     uiop->uio_seg = seg;
425     uiop->uio_resid = count;
426     uiop->uio_flag = flag;
427 }
428
429
430 /* uiomove
431  * UIO_READ : dp -> uio
432  * UIO_WRITE : uio -> dp
433  */
434 int
435 uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
436 {
437     int count;
438     struct iovec *iov;
439     int code;
440
441     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
442         iov = uiop->uio_iov;
443         count = iov->iov_len;
444
445         if (!count) {
446             uiop->uio_iov++;
447             uiop->uio_iovcnt--;
448             continue;
449         }
450
451         if (count > length)
452             count = length;
453
454         switch (uiop->uio_seg) {
455         case AFS_UIOSYS:
456             switch (rw) {
457             case UIO_READ:
458                 memcpy(iov->iov_base, dp, count);
459                 break;
460             case UIO_WRITE:
461                 memcpy(dp, iov->iov_base, count);
462                 break;
463             default:
464                 printf("uiomove: Bad rw = %d\n", rw);
465                 return -EINVAL;
466             }
467             break;
468         case AFS_UIOUSER:
469             switch (rw) {
470             case UIO_READ:
471                 AFS_COPYOUT(dp, iov->iov_base, count, code);
472                 break;
473             case UIO_WRITE:
474                 AFS_COPYIN(iov->iov_base, dp, count, code);
475                 break;
476             default:
477                 printf("uiomove: Bad rw = %d\n", rw);
478                 return -EINVAL;
479             }
480             break;
481         default:
482             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
483             return -EINVAL;
484         }
485
486         dp += count;
487         length -= count;
488         iov->iov_base += count;
489         iov->iov_len -= count;
490         uiop->uio_offset += count;
491         uiop->uio_resid -= count;
492     }
493     return 0;
494 }
495