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