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