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