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