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