death to register
[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
21 #if defined(HAVE_LINUX_EXPORTFS_H)
22 #include <linux/exportfs.h>
23 #endif
24 #include "osi_compat.h"
25
26 int cache_fh_type = -1;
27 int cache_fh_len = -1;
28
29 afs_lock_t afs_xosi;            /* lock is for tvattr */
30 extern struct osi_dev cacheDev;
31 extern struct vfsmount *afs_cacheMnt;
32 extern struct super_block *afs_cacheSBp;
33 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
34 extern struct cred *cache_creds;
35 #endif
36
37 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
38 int
39 afs_fh_acceptable(void *context, struct dentry *dp)
40 {
41     return 1;
42 }
43
44 struct file *
45 afs_linux_raw_open(afs_dcache_id_t *ainode)
46 {
47     struct inode *tip = NULL;
48     struct dentry *dp = NULL;
49     struct file* filp;
50
51     dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
52                 afs_fh_acceptable);
53     if (!dp)
54            osi_Panic("Can't get dentry\n");
55     tip = dp->d_inode;
56     tip->i_flags |= S_NOATIME;  /* Disable updating access times. */
57
58 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
59     /* Use stashed credentials - prevent selinux/apparmor problems  */
60     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, cache_creds);
61     if (IS_ERR(filp))
62         filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
63 #else
64     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
65 #endif
66     if (IS_ERR(filp))
67         osi_Panic("Can't open file: %d\n", (int) PTR_ERR(filp));
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 /*
104  * Given a dentry, return the file handle as encoded by the filesystem.
105  * We can't assume anything about the length (words, not bytes).
106  * The cache has to live on a single filesystem with uniform file 
107  * handles, otherwise we panic.
108  */
109 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
110     int max_len;
111     int type;
112
113     if (cache_fh_len > 0)
114         max_len = cache_fh_len;
115     else
116         max_len = MAX_FH_LEN;
117     type = afs_get_fh_from_dentry(dp, ainode, &max_len);
118     if (type == 255) {
119         osi_Panic("File handle encoding failed\n");
120     }
121     if (cache_fh_type < 0)
122         cache_fh_type = type;
123     if (cache_fh_len < 0) {
124         cache_fh_len = max_len;
125     }
126     if (type != cache_fh_type || max_len != cache_fh_len) {
127         osi_Panic("Inconsistent file handles within cache\n");
128     }
129 }
130
131 int
132 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
133 {
134     AFS_STATCNT(osi_Stat);
135     ObtainWriteLock(&afs_xosi, 320);
136     astat->size = i_size_read(OSIFILE_INODE(afile));
137     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
138     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
139
140     ReleaseWriteLock(&afs_xosi);
141     return 0;
142 }
143
144 int
145 osi_UFSClose(struct osi_file *afile)
146 {
147     AFS_STATCNT(osi_Close);
148     if (afile) {
149         if (OSIFILE_INODE(afile)) {
150             filp_close(afile->filp, NULL);
151         }
152     }
153
154     osi_FreeLargeSpace(afile);
155     return 0;
156 }
157
158 int
159 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
160 {
161     afs_int32 code;
162     struct osi_stat tstat;
163     struct iattr newattrs;
164     struct inode *inode = OSIFILE_INODE(afile);
165     AFS_STATCNT(osi_Truncate);
166
167     /* This routine only shrinks files, and most systems
168      * have very slow truncates, even when the file is already
169      * small enough.  Check now and save some time.
170      */
171     code = afs_osi_Stat(afile, &tstat);
172     if (code || tstat.size <= asize)
173         return code;
174     ObtainWriteLock(&afs_xosi, 321);
175     AFS_GUNLOCK();
176     afs_linux_lock_inode(inode);
177 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
178     down_write(&inode->i_alloc_sem);
179 #endif
180     newattrs.ia_size = asize;
181     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
182     newattrs.ia_ctime = CURRENT_TIME;
183
184     /* avoid notify_change() since it wants to update dentry->d_parent */
185     code = inode_change_ok(inode, &newattrs);
186     if (!code)
187         code = afs_inode_setattr(afile, &newattrs);
188     if (!code)
189         truncate_inode_pages(&inode->i_data, asize);
190     code = -code;
191 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
192     up_write(&inode->i_alloc_sem);
193 #endif
194     afs_linux_unlock_inode(inode);
195     AFS_GLOCK();
196     ReleaseWriteLock(&afs_xosi);
197     return code;
198 }
199
200
201 /* Generic read interface */
202 int
203 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
204              afs_int32 asize)
205 {
206     struct uio auio;
207     struct iovec iov;
208     afs_int32 code;
209
210     AFS_STATCNT(osi_Read);
211
212     /*
213      * If the osi_file passed in is NULL, panic only if AFS is not shutting
214      * down. No point in crashing when we are already shutting down
215      */
216     if (!afile) {
217         if (!afs_shuttingdown)
218             osi_Panic("osi_Read called with null param");
219         else
220             return EIO;
221     }
222
223     if (offset != -1)
224         afile->offset = offset;
225     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
226     AFS_GUNLOCK();
227     code = osi_rdwr(afile, &auio, UIO_READ);
228     AFS_GLOCK();
229     if (code == 0) {
230         code = asize - auio.uio_resid;
231         afile->offset += code;
232     } else {
233         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
234                    ICL_TYPE_INT32, code);
235         code = -1;
236     }
237     return code;
238 }
239
240 /* Generic write interface */
241 int
242 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
243               afs_int32 asize)
244 {
245     struct uio auio;
246     struct iovec iov;
247     afs_int32 code;
248
249     AFS_STATCNT(osi_Write);
250
251     if (!afile) {
252         if (!afs_shuttingdown)
253             osi_Panic("afs_osi_Write called with null param");
254         else
255             return EIO;
256     }
257
258     if (offset != -1)
259         afile->offset = offset;
260     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
261     AFS_GUNLOCK();
262     code = osi_rdwr(afile, &auio, UIO_WRITE);
263     AFS_GLOCK();
264     if (code == 0) {
265         code = asize - auio.uio_resid;
266         afile->offset += code;
267     } else {
268         if (code == ENOSPC)
269             afs_warnuser
270                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
271         code = -1;
272     }
273
274     if (afile->proc)
275         (*afile->proc)(afile, code);
276
277     return code;
278 }
279
280
281 /*  This work should be handled by physstrat in ca/machdep.c.
282     This routine written from the RT NFS port strategy routine.
283     It has been generalized a bit, but should still be pretty clear. */
284 int
285 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
286 {
287     afs_int32 returnCode;
288
289     AFS_STATCNT(osi_MapStrategy);
290     returnCode = (*aproc) (bp);
291
292     return returnCode;
293 }
294
295 void
296 shutdown_osifile(void)
297 {
298     AFS_STATCNT(shutdown_osifile);
299     if (afs_cold_shutdown) {
300         afs_osicred_initialized = 0;
301     }
302 }
303
304 /* Intialize cache device info and fragment size for disk cache partition. */
305 int
306 osi_InitCacheInfo(char *aname)
307 {
308     int code;
309     extern afs_dcache_id_t cacheInode;
310     struct dentry *dp;
311     extern struct osi_dev cacheDev;
312     extern afs_int32 afs_fsfragsize;
313     extern struct super_block *afs_cacheSBp;
314     extern struct vfsmount *afs_cacheMnt;
315     code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
316     if (code)
317         return ENOENT;
318
319     osi_get_fh(dp, &cacheInode.ufs);
320     cacheDev.dev = dp->d_inode->i_sb->s_dev;
321     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
322     afs_cacheSBp = dp->d_inode->i_sb;
323
324     dput(dp);
325
326     afs_init_sb_export_ops(afs_cacheSBp);
327
328     return 0;
329 }
330
331
332 /* osi_rdwr
333  * seek, then read or write to an open inode. addrp points to data in
334  * kernel space.
335  */
336 int
337 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
338 {
339     struct file *filp = osifile->filp;
340     mm_segment_t old_fs = {0};
341     int code = 0;
342     struct iovec *iov;
343     size_t count;
344     unsigned long savelim;
345     loff_t pos;
346
347     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
348     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
349
350     if (uiop->uio_seg == AFS_UIOSYS) {
351         /* Switch into user space */
352         old_fs = get_fs();
353         set_fs(get_ds());
354     }
355
356     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
357         iov = uiop->uio_iov;
358         count = iov->iov_len;
359         if (count == 0) {
360             uiop->uio_iov++;
361             uiop->uio_iovcnt--;
362             continue;
363         }
364
365         pos = uiop->uio_offset;
366         if (rw == UIO_READ)
367             code = filp->f_op->read(filp, iov->iov_base, count, &pos);
368         else
369             code = filp->f_op->write(filp, iov->iov_base, count, &pos);
370
371         if (code < 0) {
372             code = -code;
373             break;
374         } else if (code == 0) {
375             /*
376              * This is bad -- we can't read any more data from the
377              * file, but we have no good way of signaling a partial
378              * read either.
379              */
380             code = EIO;
381             break;
382         }
383
384         iov->iov_base += code;
385         iov->iov_len -= code;
386         uiop->uio_resid -= code;
387         uiop->uio_offset += code;
388         code = 0;
389     }
390
391     if (uiop->uio_seg == AFS_UIOSYS) {
392         /* Switch back into kernel space */
393         set_fs(old_fs);
394     }
395
396     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
397
398     return code;
399 }
400
401 /* setup_uio 
402  * Setup a uio struct.
403  */
404 void
405 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
406           int count, uio_flag_t flag, uio_seg_t seg)
407 {
408     iovecp->iov_base = (char *)buf;
409     iovecp->iov_len = count;
410     uiop->uio_iov = iovecp;
411     uiop->uio_iovcnt = 1;
412     uiop->uio_offset = pos;
413     uiop->uio_seg = seg;
414     uiop->uio_resid = count;
415     uiop->uio_flag = flag;
416 }
417
418
419 /* uiomove
420  * UIO_READ : dp -> uio
421  * UIO_WRITE : uio -> dp
422  */
423 int
424 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
425 {
426     int count;
427     struct iovec *iov;
428     int code;
429
430     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
431         iov = uiop->uio_iov;
432         count = iov->iov_len;
433
434         if (!count) {
435             uiop->uio_iov++;
436             uiop->uio_iovcnt--;
437             continue;
438         }
439
440         if (count > length)
441             count = length;
442
443         switch (uiop->uio_seg) {
444         case AFS_UIOSYS:
445             switch (rw) {
446             case UIO_READ:
447                 memcpy(iov->iov_base, dp, count);
448                 break;
449             case UIO_WRITE:
450                 memcpy(dp, iov->iov_base, count);
451                 break;
452             default:
453                 printf("uiomove: Bad rw = %d\n", rw);
454                 return -EINVAL;
455             }
456             break;
457         case AFS_UIOUSER:
458             switch (rw) {
459             case UIO_READ:
460                 AFS_COPYOUT(dp, iov->iov_base, count, code);
461                 break;
462             case UIO_WRITE:
463                 AFS_COPYIN(iov->iov_base, dp, count, code);
464                 break;
465             default:
466                 printf("uiomove: Bad rw = %d\n", rw);
467                 return -EINVAL;
468             }
469             break;
470         default:
471             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
472             return -EINVAL;
473         }
474
475         dp += count;
476         length -= count;
477         iov->iov_base += count;
478         iov->iov_len -= count;
479         uiop->uio_offset += count;
480         uiop->uio_resid -= count;
481     }
482     return 0;
483 }
484