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