Minimise crref() and add a fastpath for cache hits
[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 #ifdef AFS_LINUX24_ENV
15 #include "h/module.h" /* early to avoid printf->printk mapping */
16 #endif
17 #include "afs/sysincludes.h"    /* Standard vendor system headers */
18 #include "afsincludes.h"        /* Afs-based standard headers */
19 #include "afs/afs_stats.h"      /* afs statistics */
20 #include "h/smp_lock.h"
21 #if defined(AFS_LINUX26_ENV)
22 #include "h/namei.h"
23 #endif
24 #if defined(LINUX_USE_FH)
25 #include "h/exportfs.h"
26 int cache_fh_type = -1;
27 int cache_fh_len = -1;
28 #endif
29
30 afs_lock_t afs_xosi;            /* lock is for tvattr */
31 extern struct osi_dev cacheDev;
32 #if defined(AFS_LINUX24_ENV)
33 extern struct vfsmount *afs_cacheMnt;
34 #endif
35 extern struct super_block *afs_cacheSBp;
36
37 #if defined(AFS_LINUX26_ENV) 
38 struct file *
39 afs_linux_raw_open(afs_dcache_id_t *ainode, ino_t *hint)
40 {
41     struct inode *tip = NULL;
42     struct dentry *dp = NULL;
43     struct file* filp;
44
45 #if !defined(LINUX_USE_FH)
46     tip = iget(afs_cacheSBp, ainode->ufs);
47     if (!tip)
48         osi_Panic("Can't get inode %d\n", (int) ainode->ufs);
49
50     dp = d_alloc_anon(tip);
51 #else
52     dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
53                                                  cache_fh_len, cache_fh_type);
54     if (!dp)
55            osi_Panic("Can't get dentry\n");
56     tip = dp->d_inode;
57 #endif
58     tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
59
60 #if defined(STRUCT_TASK_HAS_CRED)
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 #if defined(LINUX_USE_FH)
67         osi_Panic("Can't open file\n");
68 #else
69         osi_Panic("Can't open inode %d\n", (int) ainode->ufs);
70 #endif
71     if (hint)
72         *hint = tip->i_ino;
73     return filp;
74 }
75
76 void *
77 osi_UFSOpen(afs_dcache_id_t *ainode)
78 {
79     struct osi_file *afile = NULL;
80     extern int cacheDiskType;
81     struct file *filp = NULL;
82     AFS_STATCNT(osi_UFSOpen);
83     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
84         osi_Panic("UFSOpen called for non-UFS cache\n");
85     }
86     if (!afs_osicred_initialized) {
87         /* valid for alpha_osf, SunOS, Ultrix */
88         memset((char *)&afs_osi_cred, 0, sizeof(AFS_UCRED));
89         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
90         afs_osicred_initialized = 1;
91     }
92     afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
93     AFS_GUNLOCK();
94     if (!afile) {
95         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
96                   (int)sizeof(struct osi_file));
97     }
98     memset(afile, 0, sizeof(struct osi_file));
99
100     afile->filp = afs_linux_raw_open(ainode, &afile->inum);
101     afile->size = i_size_read(FILE_INODE(afile->filp));
102     AFS_GLOCK();
103     afile->offset = 0;
104     afile->proc = (int (*)())0;
105     return (void *)afile;
106 }
107 #else
108 void *
109 osi_UFSOpen(afs_dcache_id_t *ainode)
110 {
111     register struct osi_file *afile = NULL;
112     extern int cacheDiskType;
113     afs_int32 code = 0;
114     struct inode *tip = NULL;
115     struct file *filp = NULL;
116     AFS_STATCNT(osi_UFSOpen);
117     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
118         osi_Panic("UFSOpen called for non-UFS cache\n");
119     }
120     if (!afs_osicred_initialized) {
121         /* valid for alpha_osf, SunOS, Ultrix */
122         memset((char *)&afs_osi_cred, 0, sizeof(AFS_UCRED));
123         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
124         afs_osicred_initialized = 1;
125     }
126     afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
127     AFS_GUNLOCK();
128     if (!afile) {
129         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
130                   sizeof(struct osi_file));
131     }
132     memset(afile, 0, sizeof(struct osi_file));
133     filp = &afile->file;
134     filp->f_dentry = &afile->dentry;
135     tip = iget(afs_cacheSBp, ainode->ufs);
136     if (!tip)
137         osi_Panic("Can't get inode %d\n", ainode->ufs);
138     FILE_INODE(filp) = tip;
139     tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
140     filp->f_flags = O_RDWR;
141 #if defined(AFS_LINUX24_ENV)
142     filp->f_mode = FMODE_READ|FMODE_WRITE;
143     filp->f_op = fops_get(tip->i_fop);
144 #else
145     filp->f_op = tip->i_op->default_file_ops;
146 #endif
147     if (filp->f_op && filp->f_op->open)
148         code = filp->f_op->open(tip, filp);
149     if (code)
150         osi_Panic("Can't open inode %d\n", ainode->ufs);
151     afile->size = i_size_read(tip);
152     AFS_GLOCK();
153     afile->offset = 0;
154     afile->proc = (int (*)())0;
155     afile->inum = ainode->ufs;  /* for hint validity checking */
156     return (void *)afile;
157 }
158 #endif
159
160 #if defined(LINUX_USE_FH)
161 /*
162  * Given a dentry, return the file handle as encoded by the filesystem.
163  * We can't assume anything about the length (words, not bytes).
164  * The cache has to live on a single filesystem with uniform file 
165  * handles, otherwise we panic.
166  */
167 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
168     int max_len;
169     int type;
170
171     if (cache_fh_len > 0)
172         max_len = cache_fh_len;
173     else
174         max_len = MAX_FH_LEN;
175     if (dp->d_sb->s_export_op->encode_fh) {
176         type = dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], &max_len, 0);
177         if (type == 255) {
178            osi_Panic("File handle encoding failed\n");
179         }
180         if (cache_fh_type < 0)
181             cache_fh_type = type;
182         if (cache_fh_len < 0) {
183             cache_fh_len = max_len;
184         }
185         if (type != cache_fh_type || max_len != cache_fh_len) {
186            osi_Panic("Inconsistent file handles within cache\n");
187         }
188     } else {
189          /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
190         if (cache_fh_type < 0)
191             cache_fh_type = FILEID_INO32_GEN;
192         if (cache_fh_len < 0)
193             cache_fh_len = sizeof(struct fid)/4;
194         ainode->fh.i32.ino = dp->d_inode->i_ino;
195         ainode->fh.i32.gen = dp->d_inode->i_generation;
196     }
197 }
198 #else
199 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
200     *ainode = dp->d_inode->i_ino;
201 }
202 #endif
203
204 int
205 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
206 {
207     register afs_int32 code;
208     AFS_STATCNT(osi_Stat);
209     MObtainWriteLock(&afs_xosi, 320);
210     astat->size = i_size_read(OSIFILE_INODE(afile));
211 #if defined(AFS_LINUX26_ENV)
212     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
213     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
214 #else
215     astat->mtime = OSIFILE_INODE(afile)->i_mtime;
216     astat->atime = OSIFILE_INODE(afile)->i_atime;
217 #endif
218     code = 0;
219     MReleaseWriteLock(&afs_xosi);
220     return code;
221 }
222
223 #ifdef AFS_LINUX26_ENV
224 int
225 osi_UFSClose(register struct osi_file *afile)
226 {
227     AFS_STATCNT(osi_Close);
228     if (afile) {
229         if (OSIFILE_INODE(afile)) {
230             filp_close(afile->filp, NULL);
231         }
232     }
233
234     osi_FreeLargeSpace(afile);
235     return 0;
236 }
237 #else
238 int
239 osi_UFSClose(register struct osi_file *afile)
240 {
241     AFS_STATCNT(osi_Close);
242     if (afile) {
243         if (FILE_INODE(&afile->file)) {
244             struct file *filp = &afile->file;
245             if (filp->f_op && filp->f_op->release)
246                 filp->f_op->release(FILE_INODE(filp), filp);
247             iput(FILE_INODE(filp));
248         }
249     }
250
251     osi_FreeLargeSpace(afile);
252     return 0;
253 }
254 #endif
255
256 int
257 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
258 {
259     register afs_int32 code;
260     struct osi_stat tstat;
261     struct iattr newattrs;
262     struct inode *inode = OSIFILE_INODE(afile);
263     AFS_STATCNT(osi_Truncate);
264
265     /* This routine only shrinks files, and most systems
266      * have very slow truncates, even when the file is already
267      * small enough.  Check now and save some time.
268      */
269     code = afs_osi_Stat(afile, &tstat);
270     if (code || tstat.size <= asize)
271         return code;
272     MObtainWriteLock(&afs_xosi, 321);
273     AFS_GUNLOCK();
274 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
275     down_write(&inode->i_alloc_sem);
276 #endif
277 #ifdef STRUCT_INODE_HAS_I_MUTEX
278     mutex_lock(&inode->i_mutex);
279 #else
280     down(&inode->i_sem);
281 #endif
282     newattrs.ia_size = asize;
283     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
284 #if defined(AFS_LINUX24_ENV)
285     newattrs.ia_ctime = CURRENT_TIME;
286
287     /* avoid notify_change() since it wants to update dentry->d_parent */
288     lock_kernel();
289     code = inode_change_ok(inode, &newattrs);
290     if (!code) {
291 #ifdef INODE_SETATTR_NOT_VOID
292 #if defined(AFS_LINUX26_ENV)
293         if (inode->i_op && inode->i_op->setattr)
294             code = inode->i_op->setattr(afile->filp->f_dentry, &newattrs);
295         else
296 #endif
297             code = inode_setattr(inode, &newattrs);
298 #else
299         inode_setattr(inode, &newattrs);
300 #endif
301     }
302     unlock_kernel();
303     if (!code)
304         truncate_inode_pages(&inode->i_data, asize);
305 #else
306     i_size_write(inode, asize);
307     if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
308         code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
309     }
310     if (!code) {
311         truncate_inode_pages(inode, asize);
312         if (inode->i_op && inode->i_op->truncate)
313             inode->i_op->truncate(inode);
314     }
315 #endif
316     code = -code;
317 #ifdef STRUCT_INODE_HAS_I_MUTEX
318     mutex_unlock(&inode->i_mutex);
319 #else
320     up(&inode->i_sem);
321 #endif
322 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
323     up_write(&inode->i_alloc_sem);
324 #endif
325     AFS_GLOCK();
326     MReleaseWriteLock(&afs_xosi);
327     return code;
328 }
329
330
331 /* Generic read interface */
332 int
333 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
334              afs_int32 asize)
335 {
336     struct uio auio;
337     struct iovec iov;
338     afs_int32 code;
339
340     AFS_STATCNT(osi_Read);
341
342     /*
343      * If the osi_file passed in is NULL, panic only if AFS is not shutting
344      * down. No point in crashing when we are already shutting down
345      */
346     if (!afile) {
347         if (!afs_shuttingdown)
348             osi_Panic("osi_Read called with null param");
349         else
350             return EIO;
351     }
352
353     if (offset != -1)
354         afile->offset = offset;
355     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
356     AFS_GUNLOCK();
357     code = osi_rdwr(afile, &auio, UIO_READ);
358     AFS_GLOCK();
359     if (code == 0) {
360         code = asize - auio.uio_resid;
361         afile->offset += code;
362     } else {
363         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
364                    ICL_TYPE_INT32, code);
365         code = -1;
366     }
367     return code;
368 }
369
370 /* Generic write interface */
371 int
372 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
373               afs_int32 asize)
374 {
375     struct uio auio;
376     struct iovec iov;
377     afs_int32 code;
378
379     AFS_STATCNT(osi_Write);
380
381     if (!afile) {
382         if (!afs_shuttingdown)
383             osi_Panic("afs_osi_Write called with null param");
384         else
385             return EIO;
386     }
387
388     if (offset != -1)
389         afile->offset = offset;
390     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
391     AFS_GUNLOCK();
392     code = osi_rdwr(afile, &auio, UIO_WRITE);
393     AFS_GLOCK();
394     if (code == 0) {
395         code = asize - auio.uio_resid;
396         afile->offset += code;
397     } else {
398         if (code == ENOSPC)
399             afs_warnuser
400                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
401         code = -1;
402     }
403
404     if (afile->proc)
405         (*afile->proc)(afile, code);
406
407     return code;
408 }
409
410
411 /*  This work should be handled by physstrat in ca/machdep.c.
412     This routine written from the RT NFS port strategy routine.
413     It has been generalized a bit, but should still be pretty clear. */
414 int
415 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
416 {
417     afs_int32 returnCode;
418
419     AFS_STATCNT(osi_MapStrategy);
420     returnCode = (*aproc) (bp);
421
422     return returnCode;
423 }
424
425 void
426 shutdown_osifile(void)
427 {
428     AFS_STATCNT(shutdown_osifile);
429     if (afs_cold_shutdown) {
430         afs_osicred_initialized = 0;
431     }
432 }
433
434 /* Intialize cache device info and fragment size for disk cache partition. */
435 int
436 osi_InitCacheInfo(char *aname)
437 {
438     int code;
439     extern afs_dcache_id_t cacheInode;
440     struct dentry *dp;
441     extern struct osi_dev cacheDev;
442     extern afs_int32 afs_fsfragsize;
443     extern struct super_block *afs_cacheSBp;
444     extern struct vfsmount *afs_cacheMnt;
445     code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
446     if (code)
447         return ENOENT;
448
449     osi_get_fh(dp, &cacheInode.ufs);
450     cacheDev.dev = dp->d_inode->i_sb->s_dev;
451     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
452     afs_cacheSBp = dp->d_inode->i_sb;
453
454     dput(dp);
455
456     return 0;
457 }
458
459
460 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
461 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
462
463 /* osi_rdwr
464  * seek, then read or write to an open inode. addrp points to data in
465  * kernel space.
466  */
467 int
468 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
469 {
470 #ifdef AFS_LINUX26_ENV
471     struct file *filp = osifile->filp;
472 #else
473     struct file *filp = &osifile->file;
474 #endif
475     KERNEL_SPACE_DECL;
476     int code = 0;
477     struct iovec *iov;
478     afs_size_t count;
479     unsigned long savelim;
480
481     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
482     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
483
484     if (uiop->uio_seg == AFS_UIOSYS)
485         TO_USER_SPACE();
486
487     /* seek to the desired position. Return -1 on error. */
488     if (filp->f_op->llseek) {
489         if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
490             return -1;
491     } else
492         filp->f_pos = uiop->uio_offset;
493
494     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
495         iov = uiop->uio_iov;
496         count = iov->iov_len;
497         if (count == 0) {
498             uiop->uio_iov++;
499             uiop->uio_iovcnt--;
500             continue;
501         }
502
503         if (rw == UIO_READ)
504             code = FOP_READ(filp, iov->iov_base, count);
505         else
506             code = FOP_WRITE(filp, iov->iov_base, count);
507
508         if (code < 0) {
509             code = -code;
510             break;
511         } else if (code == 0) {
512             /*
513              * This is bad -- we can't read any more data from the
514              * file, but we have no good way of signaling a partial
515              * read either.
516              */
517             code = EIO;
518             break;
519         }
520
521         iov->iov_base += code;
522         iov->iov_len -= code;
523         uiop->uio_resid -= code;
524         uiop->uio_offset += code;
525         code = 0;
526     }
527
528     if (uiop->uio_seg == AFS_UIOSYS)
529         TO_KERNEL_SPACE();
530
531     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
532
533     return code;
534 }
535
536 /* setup_uio 
537  * Setup a uio struct.
538  */
539 void
540 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
541           int count, uio_flag_t flag, uio_seg_t seg)
542 {
543     iovecp->iov_base = (char *)buf;
544     iovecp->iov_len = count;
545     uiop->uio_iov = iovecp;
546     uiop->uio_iovcnt = 1;
547     uiop->uio_offset = pos;
548     uiop->uio_seg = seg;
549     uiop->uio_resid = count;
550     uiop->uio_flag = flag;
551 }
552
553
554 /* uiomove
555  * UIO_READ : dp -> uio
556  * UIO_WRITE : uio -> dp
557  */
558 int
559 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
560 {
561     int count;
562     struct iovec *iov;
563     int code;
564
565     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
566         iov = uiop->uio_iov;
567         count = iov->iov_len;
568
569         if (!count) {
570             uiop->uio_iov++;
571             uiop->uio_iovcnt--;
572             continue;
573         }
574
575         if (count > length)
576             count = length;
577
578         switch (uiop->uio_seg) {
579         case AFS_UIOSYS:
580             switch (rw) {
581             case UIO_READ:
582                 memcpy(iov->iov_base, dp, count);
583                 break;
584             case UIO_WRITE:
585                 memcpy(dp, iov->iov_base, count);
586                 break;
587             default:
588                 printf("uiomove: Bad rw = %d\n", rw);
589                 return -EINVAL;
590             }
591             break;
592         case AFS_UIOUSER:
593             switch (rw) {
594             case UIO_READ:
595                 AFS_COPYOUT(dp, iov->iov_base, count, code);
596                 break;
597             case UIO_WRITE:
598                 AFS_COPYIN(iov->iov_base, dp, count, code);
599                 break;
600             default:
601                 printf("uiomove: Bad rw = %d\n", rw);
602                 return -EINVAL;
603             }
604             break;
605         default:
606             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
607             return -EINVAL;
608         }
609
610         dp += count;
611         length -= count;
612         iov->iov_base += count;
613         iov->iov_len -= count;
614         uiop->uio_offset += count;
615         uiop->uio_resid -= count;
616     }
617     return 0;
618 }
619