Linux: Don't pass f_pos down to the filesystem
[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/smp_lock.h>
19 #include <linux/namei.h>
20 #if defined(LINUX_USE_FH)
21 #include <linux/exportfs.h>
22 int cache_fh_type = -1;
23 int cache_fh_len = -1;
24 #endif
25
26 afs_lock_t afs_xosi;            /* lock is for tvattr */
27 extern struct osi_dev cacheDev;
28 extern struct vfsmount *afs_cacheMnt;
29 extern struct super_block *afs_cacheSBp;
30 #if defined(STRUCT_TASK_HAS_CRED)
31 extern struct cred *cache_creds;
32 #endif
33
34 struct file *
35 afs_linux_raw_open(afs_dcache_id_t *ainode)
36 {
37     struct inode *tip = NULL;
38     struct dentry *dp = NULL;
39     struct file* filp;
40
41 #if !defined(LINUX_USE_FH)
42     tip = iget(afs_cacheSBp, ainode->ufs);
43     if (!tip)
44         osi_Panic("Can't get inode %d\n", (int) ainode->ufs);
45
46     dp = d_alloc_anon(tip);
47 #else
48     dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
49                                                  cache_fh_len, cache_fh_type);
50     if (!dp)
51            osi_Panic("Can't get dentry\n");
52     tip = dp->d_inode;
53 #endif
54     tip->i_flags |= S_NOATIME;  /* Disable updating access times. */
55
56 #if defined(STRUCT_TASK_HAS_CRED)
57     /* Use stashed credentials - prevent selinux/apparmor problems  */
58     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, cache_creds);
59     if (IS_ERR(filp))
60         filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
61 #else
62     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
63 #endif
64     if (IS_ERR(filp))
65 #if defined(LINUX_USE_FH)
66         osi_Panic("Can't open file: %d\n", (int) PTR_ERR(filp));
67 #else
68         osi_Panic("Can't open inode %d\n", (int) ainode->ufs);
69 #endif
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     afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
90     AFS_GUNLOCK();
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 #if defined(LINUX_USE_FH)
106 /*
107  * Given a dentry, return the file handle as encoded by the filesystem.
108  * We can't assume anything about the length (words, not bytes).
109  * The cache has to live on a single filesystem with uniform file 
110  * handles, otherwise we panic.
111  */
112 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
113     int max_len;
114     int type;
115
116     if (cache_fh_len > 0)
117         max_len = cache_fh_len;
118     else
119         max_len = MAX_FH_LEN;
120     if (dp->d_sb->s_export_op->encode_fh) {
121         type = dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], &max_len, 0);
122         if (type == 255) {
123            osi_Panic("File handle encoding failed\n");
124         }
125         if (cache_fh_type < 0)
126             cache_fh_type = type;
127         if (cache_fh_len < 0) {
128             cache_fh_len = max_len;
129         }
130         if (type != cache_fh_type || max_len != cache_fh_len) {
131            osi_Panic("Inconsistent file handles within cache\n");
132         }
133     } else {
134          /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
135         if (cache_fh_type < 0)
136             cache_fh_type = FILEID_INO32_GEN;
137         if (cache_fh_len < 0)
138             cache_fh_len = sizeof(struct fid)/4;
139         ainode->fh.i32.ino = dp->d_inode->i_ino;
140         ainode->fh.i32.gen = dp->d_inode->i_generation;
141     }
142 }
143 #else
144 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
145     *ainode = dp->d_inode->i_ino;
146 }
147 #endif
148
149 int
150 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
151 {
152     AFS_STATCNT(osi_Stat);
153     ObtainWriteLock(&afs_xosi, 320);
154     astat->size = i_size_read(OSIFILE_INODE(afile));
155     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
156     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
157
158     ReleaseWriteLock(&afs_xosi);
159     return 0;
160 }
161
162 int
163 osi_UFSClose(register struct osi_file *afile)
164 {
165     AFS_STATCNT(osi_Close);
166     if (afile) {
167         if (OSIFILE_INODE(afile)) {
168             filp_close(afile->filp, NULL);
169         }
170     }
171
172     osi_FreeLargeSpace(afile);
173     return 0;
174 }
175
176 int
177 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
178 {
179     register 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     ObtainWriteLock(&afs_xosi, 321);
193     AFS_GUNLOCK();
194 #ifdef STRUCT_INODE_HAS_I_MUTEX
195     mutex_lock(&inode->i_mutex);
196 #else
197     down(&inode->i_sem);
198 #endif
199 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
200     down_write(&inode->i_alloc_sem);
201 #endif
202     newattrs.ia_size = asize;
203     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
204     newattrs.ia_ctime = CURRENT_TIME;
205
206     /* avoid notify_change() since it wants to update dentry->d_parent */
207     lock_kernel();
208     code = inode_change_ok(inode, &newattrs);
209     if (!code) {
210 #ifdef INODE_SETATTR_NOT_VOID
211         if (inode->i_op && inode->i_op->setattr)
212             code = inode->i_op->setattr(afile->filp->f_dentry, &newattrs);
213         else
214             code = inode_setattr(inode, &newattrs);
215 #else
216         inode_setattr(inode, &newattrs);
217 #endif
218     }
219     unlock_kernel();
220     if (!code)
221         truncate_inode_pages(&inode->i_data, asize);
222     code = -code;
223 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
224     up_write(&inode->i_alloc_sem);
225 #endif
226 #ifdef STRUCT_INODE_HAS_I_MUTEX
227     mutex_unlock(&inode->i_mutex);
228 #else
229     up(&inode->i_sem);
230 #endif
231     AFS_GLOCK();
232     ReleaseWriteLock(&afs_xosi);
233     return code;
234 }
235
236
237 /* Generic read interface */
238 int
239 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
240              afs_int32 asize)
241 {
242     struct uio auio;
243     struct iovec iov;
244     afs_int32 code;
245
246     AFS_STATCNT(osi_Read);
247
248     /*
249      * If the osi_file passed in is NULL, panic only if AFS is not shutting
250      * down. No point in crashing when we are already shutting down
251      */
252     if (!afile) {
253         if (!afs_shuttingdown)
254             osi_Panic("osi_Read called with null param");
255         else
256             return EIO;
257     }
258
259     if (offset != -1)
260         afile->offset = offset;
261     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
262     AFS_GUNLOCK();
263     code = osi_rdwr(afile, &auio, UIO_READ);
264     AFS_GLOCK();
265     if (code == 0) {
266         code = asize - auio.uio_resid;
267         afile->offset += code;
268     } else {
269         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
270                    ICL_TYPE_INT32, code);
271         code = -1;
272     }
273     return code;
274 }
275
276 /* Generic write interface */
277 int
278 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
279               afs_int32 asize)
280 {
281     struct uio auio;
282     struct iovec iov;
283     afs_int32 code;
284
285     AFS_STATCNT(osi_Write);
286
287     if (!afile) {
288         if (!afs_shuttingdown)
289             osi_Panic("afs_osi_Write called with null param");
290         else
291             return EIO;
292     }
293
294     if (offset != -1)
295         afile->offset = offset;
296     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
297     AFS_GUNLOCK();
298     code = osi_rdwr(afile, &auio, UIO_WRITE);
299     AFS_GLOCK();
300     if (code == 0) {
301         code = asize - auio.uio_resid;
302         afile->offset += code;
303     } else {
304         if (code == ENOSPC)
305             afs_warnuser
306                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
307         code = -1;
308     }
309
310     if (afile->proc)
311         (*afile->proc)(afile, code);
312
313     return code;
314 }
315
316
317 /*  This work should be handled by physstrat in ca/machdep.c.
318     This routine written from the RT NFS port strategy routine.
319     It has been generalized a bit, but should still be pretty clear. */
320 int
321 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
322 {
323     afs_int32 returnCode;
324
325     AFS_STATCNT(osi_MapStrategy);
326     returnCode = (*aproc) (bp);
327
328     return returnCode;
329 }
330
331 void
332 shutdown_osifile(void)
333 {
334     AFS_STATCNT(shutdown_osifile);
335     if (afs_cold_shutdown) {
336         afs_osicred_initialized = 0;
337     }
338 }
339
340 /* Intialize cache device info and fragment size for disk cache partition. */
341 int
342 osi_InitCacheInfo(char *aname)
343 {
344     int code;
345     extern afs_dcache_id_t cacheInode;
346     struct dentry *dp;
347     extern struct osi_dev cacheDev;
348     extern afs_int32 afs_fsfragsize;
349     extern struct super_block *afs_cacheSBp;
350     extern struct vfsmount *afs_cacheMnt;
351     code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
352     if (code)
353         return ENOENT;
354
355     osi_get_fh(dp, &cacheInode.ufs);
356     cacheDev.dev = dp->d_inode->i_sb->s_dev;
357     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
358     afs_cacheSBp = dp->d_inode->i_sb;
359
360     dput(dp);
361
362     return 0;
363 }
364
365
366 /* osi_rdwr
367  * seek, then read or write to an open inode. addrp points to data in
368  * kernel space.
369  */
370 int
371 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
372 {
373     struct file *filp = osifile->filp;
374     KERNEL_SPACE_DECL;
375     int code = 0;
376     struct iovec *iov;
377     size_t count;
378     unsigned long savelim;
379     loff_t pos;
380
381     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
382     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
383
384     if (uiop->uio_seg == AFS_UIOSYS)
385         TO_USER_SPACE();
386
387     /* seek to the desired position. Return -1 on error. */
388     if (vfs_llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset) {
389         code = -1;
390         goto out;
391     }
392
393     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
394         iov = uiop->uio_iov;
395         count = iov->iov_len;
396         if (count == 0) {
397             uiop->uio_iov++;
398             uiop->uio_iovcnt--;
399             continue;
400         }
401
402         pos = filp->f_pos;
403         if (rw == UIO_READ)
404             code = filp->f_op->read(filp, iov->iov_base, count, &pos);
405         else
406             code = filp->f_op->write(filp, iov->iov_base, count, &pos);
407         filp->f_pos = pos;
408
409         if (code < 0) {
410             code = -code;
411             break;
412         } else if (code == 0) {
413             /*
414              * This is bad -- we can't read any more data from the
415              * file, but we have no good way of signaling a partial
416              * read either.
417              */
418             code = EIO;
419             break;
420         }
421
422         iov->iov_base += code;
423         iov->iov_len -= code;
424         uiop->uio_resid -= code;
425         uiop->uio_offset += code;
426         code = 0;
427     }
428
429 out:
430     if (uiop->uio_seg == AFS_UIOSYS)
431         TO_KERNEL_SPACE();
432
433     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
434
435     return code;
436 }
437
438 /* setup_uio 
439  * Setup a uio struct.
440  */
441 void
442 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
443           int count, uio_flag_t flag, uio_seg_t seg)
444 {
445     iovecp->iov_base = (char *)buf;
446     iovecp->iov_len = count;
447     uiop->uio_iov = iovecp;
448     uiop->uio_iovcnt = 1;
449     uiop->uio_offset = pos;
450     uiop->uio_seg = seg;
451     uiop->uio_resid = count;
452     uiop->uio_flag = flag;
453 }
454
455
456 /* uiomove
457  * UIO_READ : dp -> uio
458  * UIO_WRITE : uio -> dp
459  */
460 int
461 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
462 {
463     int count;
464     struct iovec *iov;
465     int code;
466
467     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
468         iov = uiop->uio_iov;
469         count = iov->iov_len;
470
471         if (!count) {
472             uiop->uio_iov++;
473             uiop->uio_iovcnt--;
474             continue;
475         }
476
477         if (count > length)
478             count = length;
479
480         switch (uiop->uio_seg) {
481         case AFS_UIOSYS:
482             switch (rw) {
483             case UIO_READ:
484                 memcpy(iov->iov_base, dp, count);
485                 break;
486             case UIO_WRITE:
487                 memcpy(dp, iov->iov_base, count);
488                 break;
489             default:
490                 printf("uiomove: Bad rw = %d\n", rw);
491                 return -EINVAL;
492             }
493             break;
494         case AFS_UIOUSER:
495             switch (rw) {
496             case UIO_READ:
497                 AFS_COPYOUT(dp, iov->iov_base, count, code);
498                 break;
499             case UIO_WRITE:
500                 AFS_COPYIN(iov->iov_base, dp, count, code);
501                 break;
502             default:
503                 printf("uiomove: Bad rw = %d\n", rw);
504                 return -EINVAL;
505             }
506             break;
507         default:
508             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
509             return -EINVAL;
510         }
511
512         dp += count;
513         length -= count;
514         iov->iov_base += count;
515         iov->iov_len -= count;
516         uiop->uio_offset += count;
517         uiop->uio_resid -= count;
518     }
519     return 0;
520 }
521