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