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