e211c055fac9114b319e6579e7ccff43f0f36c40
[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 #ifdef HAVE_LINUX_SETATTR_PREPARE
187     code = setattr_prepare(file_dentry(afile->filp), &newattrs);
188 #else
189     code = inode_change_ok(inode, &newattrs);
190 #endif
191     if (!code)
192         code = afs_inode_setattr(afile, &newattrs);
193     if (!code)
194         truncate_inode_pages(&inode->i_data, asize);
195     code = -code;
196 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
197     up_write(&inode->i_alloc_sem);
198 #endif
199     afs_linux_unlock_inode(inode);
200     AFS_GLOCK();
201     ReleaseWriteLock(&afs_xosi);
202     return code;
203 }
204
205
206 /* Generic read interface */
207 int
208 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
209              afs_int32 asize)
210 {
211     struct uio auio;
212     struct iovec iov;
213     afs_int32 code;
214
215     memset(&auio, 0, sizeof(auio));
216     memset(&iov, 0, sizeof(iov));
217
218     AFS_STATCNT(osi_Read);
219
220     /*
221      * If the osi_file passed in is NULL, panic only if AFS is not shutting
222      * down. No point in crashing when we are already shutting down
223      */
224     if (!afile) {
225         if (afs_shuttingdown == AFS_RUNNING)
226             osi_Panic("osi_Read called with null param");
227         else
228             return -EIO;
229     }
230
231     if (offset != -1)
232         afile->offset = offset;
233     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
234     AFS_GUNLOCK();
235     code = osi_rdwr(afile, &auio, UIO_READ);
236     AFS_GLOCK();
237     if (code == 0) {
238         code = asize - auio.uio_resid;
239         afile->offset += code;
240     } else {
241         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
242                    ICL_TYPE_INT32, code);
243         if (code > 0) {
244             code = -code;
245         }
246     }
247     return code;
248 }
249
250 /* Generic write interface */
251 int
252 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
253               afs_int32 asize)
254 {
255     struct uio auio;
256     struct iovec iov;
257     afs_int32 code;
258
259     memset(&auio, 0, sizeof(auio));
260     memset(&iov, 0, sizeof(iov));
261
262     AFS_STATCNT(osi_Write);
263
264     if (!afile) {
265         if (afs_shuttingdown == AFS_RUNNING)
266             osi_Panic("afs_osi_Write called with null param");
267         else
268             return -EIO;
269     }
270
271     if (offset != -1)
272         afile->offset = offset;
273     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
274     AFS_GUNLOCK();
275     code = osi_rdwr(afile, &auio, UIO_WRITE);
276     AFS_GLOCK();
277     if (code == 0) {
278         code = asize - auio.uio_resid;
279         afile->offset += code;
280     } else {
281         if (code == ENOSPC)
282             afs_warnuser
283                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
284         if (code > 0) {
285             code = -code;
286         }
287     }
288
289     if (afile->proc)
290         (*afile->proc)(afile, code);
291
292     return code;
293 }
294
295
296 /*  This work should be handled by physstrat in ca/machdep.c.
297     This routine written from the RT NFS port strategy routine.
298     It has been generalized a bit, but should still be pretty clear. */
299 int
300 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
301 {
302     afs_int32 returnCode;
303
304     AFS_STATCNT(osi_MapStrategy);
305     returnCode = (*aproc) (bp);
306
307     return returnCode;
308 }
309
310 void
311 shutdown_osifile(void)
312 {
313     AFS_STATCNT(shutdown_osifile);
314     if (afs_cold_shutdown) {
315         afs_osicred_initialized = 0;
316     }
317 }
318
319 /* Intialize cache device info and fragment size for disk cache partition. */
320 int
321 osi_InitCacheInfo(char *aname)
322 {
323     int code;
324     extern afs_dcache_id_t cacheInode;
325     struct dentry *dp;
326     extern struct osi_dev cacheDev;
327     extern afs_int32 afs_fsfragsize;
328     extern struct super_block *afs_cacheSBp;
329     extern struct vfsmount *afs_cacheMnt;
330     code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
331     if (code)
332         return ENOENT;
333
334     osi_get_fh(dp, &cacheInode.ufs);
335     cacheDev.dev = dp->d_inode->i_sb->s_dev;
336     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
337     afs_cacheSBp = dp->d_inode->i_sb;
338
339     dput(dp);
340
341     afs_init_sb_export_ops(afs_cacheSBp);
342
343     return 0;
344 }
345
346
347 /* osi_rdwr
348  * seek, then read or write to an open inode. addrp points to data in
349  * kernel space.
350  */
351 int
352 osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
353 {
354     struct file *filp = osifile->filp;
355     mm_segment_t old_fs = {0};
356     int code = 0;
357     struct iovec *iov;
358     size_t count;
359     unsigned long savelim;
360     loff_t pos;
361
362     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
363     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
364
365     if (uiop->uio_seg == AFS_UIOSYS) {
366         /* Switch into user space */
367         old_fs = get_fs();
368         set_fs(get_ds());
369     }
370
371     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
372         iov = uiop->uio_iov;
373         count = iov->iov_len;
374         if (count == 0) {
375             uiop->uio_iov++;
376             uiop->uio_iovcnt--;
377             continue;
378         }
379
380         pos = uiop->uio_offset;
381         if (rw == UIO_READ)
382             code = afs_file_read(filp, iov->iov_base, count, &pos);
383         else
384             code = afs_file_write(filp, iov->iov_base, count, &pos);
385
386         if (code < 0) {
387             code = -code;
388             break;
389         } else if (code == 0) {
390             /*
391              * This is bad -- we can't read any more data from the
392              * file, but we have no good way of signaling a partial
393              * read either.
394              */
395             code = EIO;
396             break;
397         }
398
399         iov->iov_base += code;
400         iov->iov_len -= code;
401         uiop->uio_resid -= code;
402         uiop->uio_offset += code;
403         code = 0;
404     }
405
406     if (uiop->uio_seg == AFS_UIOSYS) {
407         /* Switch back into kernel space */
408         set_fs(old_fs);
409     }
410
411     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
412
413     return code;
414 }
415
416 /* setup_uio 
417  * Setup a uio struct.
418  */
419 void
420 setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
421           int count, uio_flag_t flag, uio_seg_t seg)
422 {
423     iovecp->iov_base = (char *)buf;
424     iovecp->iov_len = count;
425     uiop->uio_iov = iovecp;
426     uiop->uio_iovcnt = 1;
427     uiop->uio_offset = pos;
428     uiop->uio_seg = seg;
429     uiop->uio_resid = count;
430     uiop->uio_flag = flag;
431 }
432
433
434 /* uiomove
435  * UIO_READ : dp -> uio
436  * UIO_WRITE : uio -> dp
437  */
438 int
439 uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
440 {
441     int count;
442     struct iovec *iov;
443     int code;
444
445     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
446         iov = uiop->uio_iov;
447         count = iov->iov_len;
448
449         if (!count) {
450             uiop->uio_iov++;
451             uiop->uio_iovcnt--;
452             continue;
453         }
454
455         if (count > length)
456             count = length;
457
458         switch (uiop->uio_seg) {
459         case AFS_UIOSYS:
460             switch (rw) {
461             case UIO_READ:
462                 memcpy(iov->iov_base, dp, count);
463                 break;
464             case UIO_WRITE:
465                 memcpy(dp, iov->iov_base, count);
466                 break;
467             default:
468                 printf("uiomove: Bad rw = %d\n", rw);
469                 return -EINVAL;
470             }
471             break;
472         case AFS_UIOUSER:
473             switch (rw) {
474             case UIO_READ:
475                 AFS_COPYOUT(dp, iov->iov_base, count, code);
476                 break;
477             case UIO_WRITE:
478                 AFS_COPYIN(iov->iov_base, dp, count, code);
479                 break;
480             default:
481                 printf("uiomove: Bad rw = %d\n", rw);
482                 return -EINVAL;
483             }
484             break;
485         default:
486             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
487             return -EINVAL;
488         }
489
490         dp += count;
491         length -= count;
492         iov->iov_base += count;
493         iov->iov_len -= count;
494         uiop->uio_offset += count;
495         uiop->uio_resid -= count;
496     }
497     return 0;
498 }
499