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