Remove references to SunOS 4
[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 # if defined(HAVE_LINUX_KTIME_GET_COARSE_REAL_TS64)
27 #   define AFS_CURRENT_TIME(x) (ktime_get_coarse_real_ts64((x)))
28 # else
29 #  ifdef IATTR_TAKES_64BIT_TIME
30 #    define AFS_CURRENT_TIME(x) do {*(x) = current_kernel_time64();} while (0)
31 #  else
32 #    define AFS_CURRENT_TIME(x) do {*(x) = current_kernel_time();} while (0)
33 #  endif
34 # endif
35 #else
36 # define AFS_CURRENT_TIME(x) do {*(x) = CURRENT_TIME;} while(0)
37 #endif
38
39 int cache_fh_type = -1;
40 int cache_fh_len = -1;
41
42 extern struct osi_dev cacheDev;
43 extern struct vfsmount *afs_cacheMnt;
44 extern struct super_block *afs_cacheSBp;
45 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
46 extern struct cred *cache_creds;
47 #endif
48
49 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
50 int
51 afs_fh_acceptable(void *context, struct dentry *dp)
52 {
53     return 1;
54 }
55
56 struct file *
57 afs_linux_raw_open(afs_dcache_id_t *ainode)
58 {
59     struct inode *tip = NULL;
60     struct dentry *dp = NULL;
61     struct file* filp;
62
63     dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
64                 afs_fh_acceptable);
65     if ((!dp) || IS_ERR(dp))
66            osi_Panic("Can't get dentry\n");
67     tip = dp->d_inode;
68     tip->i_flags |= S_NOATIME;  /* Disable updating access times. */
69
70 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
71     /* Use stashed credentials - prevent selinux/apparmor problems  */
72     filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, cache_creds);
73     if (IS_ERR(filp))
74         filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, current_cred());
75 #else
76     filp = dentry_open(dget(dp), mntget(afs_cacheMnt), O_RDWR);
77 #endif
78     if (IS_ERR(filp)) {
79         afs_warn("afs: Cannot open cache file (code %d). Trying to continue, "
80                  "but AFS accesses may return errors or panic the system\n",
81                  (int) PTR_ERR(filp));
82         filp = NULL;
83     }
84
85     dput(dp);
86
87     return filp;
88 }
89
90 void *
91 osi_UFSOpen(afs_dcache_id_t *ainode)
92 {
93     struct osi_file *afile = NULL;
94     extern int cacheDiskType;
95
96     AFS_STATCNT(osi_UFSOpen);
97     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
98         osi_Panic("UFSOpen called for non-UFS cache\n");
99     }
100     if (!afs_osicred_initialized) {
101         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
102         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
103         afs_osicred_initialized = 1;
104     }
105     AFS_GUNLOCK();
106     afile = kmalloc(sizeof(struct osi_file), GFP_NOFS);
107     if (!afile) {
108         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
109                   (int)sizeof(struct osi_file));
110     }
111     memset(afile, 0, sizeof(struct osi_file));
112
113     afile->filp = afs_linux_raw_open(ainode);
114     if (afile->filp) {
115         afile->size = i_size_read(FILE_INODE(afile->filp));
116     }
117     AFS_GLOCK();
118
119     if (!afile->filp) {
120         osi_FreeLargeSpace(afile);
121         return NULL;
122     }
123
124     afile->offset = 0;
125     afile->proc = (int (*)())0;
126     return (void *)afile;
127 }
128
129 /*
130  * Given a dentry, return the file handle as encoded by the filesystem.
131  * We can't assume anything about the length (words, not bytes).
132  * The cache has to live on a single filesystem with uniform file 
133  * handles, otherwise we panic.
134  */
135 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
136     int max_len;
137     int type;
138
139     if (cache_fh_len > 0)
140         max_len = cache_fh_len;
141     else
142         max_len = MAX_FH_LEN;
143     type = afs_get_fh_from_dentry(dp, ainode, &max_len);
144     if (type == 255) {
145         osi_Panic("File handle encoding failed\n");
146     }
147     if (cache_fh_type < 0)
148         cache_fh_type = type;
149     if (cache_fh_len < 0) {
150         cache_fh_len = max_len;
151     }
152     if (type != cache_fh_type || max_len != cache_fh_len) {
153         osi_Panic("Inconsistent file handles within cache\n");
154     }
155 }
156
157 int
158 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
159 {
160     AFS_STATCNT(osi_Stat);
161     astat->size = i_size_read(OSIFILE_INODE(afile));
162     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
163     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
164
165     return 0;
166 }
167
168 int
169 osi_UFSClose(struct osi_file *afile)
170 {
171     AFS_STATCNT(osi_Close);
172     if (afile) {
173         if (OSIFILE_INODE(afile)) {
174             filp_close(afile->filp, NULL);
175         }
176     }
177     kfree(afile);
178     return 0;
179 }
180
181 int
182 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
183 {
184     afs_int32 code;
185     struct osi_stat tstat;
186     struct iattr newattrs;
187     struct inode *inode = OSIFILE_INODE(afile);
188     AFS_STATCNT(osi_Truncate);
189
190     /* This routine only shrinks files, and most systems
191      * have very slow truncates, even when the file is already
192      * small enough.  Check now and save some time.
193      */
194     code = afs_osi_Stat(afile, &tstat);
195     if (code || tstat.size <= asize)
196         return code;
197     AFS_GUNLOCK();
198     afs_linux_lock_inode(inode);
199 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
200     down_write(&inode->i_alloc_sem);
201 #endif
202     newattrs.ia_size = asize;
203     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
204     AFS_CURRENT_TIME(&newattrs.ia_ctime);
205
206     /* avoid notify_change() since it wants to update dentry->d_parent */
207 #ifdef HAVE_LINUX_SETATTR_PREPARE
208     code = setattr_prepare(file_dentry(afile->filp), &newattrs);
209 #else
210     code = inode_change_ok(inode, &newattrs);
211 #endif
212     if (!code)
213         code = afs_inode_setattr(afile, &newattrs);
214     if (!code)
215         truncate_inode_pages(&inode->i_data, asize);
216     code = -code;
217 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
218     up_write(&inode->i_alloc_sem);
219 #endif
220     afs_linux_unlock_inode(inode);
221     AFS_GLOCK();
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_WarnENOSPC();
303         if (code > 0) {
304             code = -code;
305         }
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), 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     afs_init_sb_export_ops(afs_cacheSBp);
361
362     return 0;
363 }
364
365
366 /* osi_rdwr
367  * seek, then read or write to an open inode. addrp points to data in
368  * kernel space.
369  */
370 int
371 osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
372 {
373     struct file *filp = osifile->filp;
374 #ifdef AFS_FILE_NEEDS_SET_FS
375     mm_segment_t old_fs = {0};
376 #endif /* AFS_FILE_NEEDS_SET_FS */
377     int code = 0;
378     struct iovec *iov;
379     size_t count;
380     unsigned long savelim;
381     loff_t pos;
382
383     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
384     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
385
386 #ifdef AFS_FILE_NEEDS_SET_FS
387     if (uiop->uio_seg == AFS_UIOSYS) {
388         /* Switch into user space */
389         old_fs = get_fs();
390         set_fs(get_ds());
391     }
392 #endif /* AFS_FILE_NEEDS_SET_FS */
393
394     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
395         iov = uiop->uio_iov;
396         count = iov->iov_len;
397         if (count == 0) {
398             uiop->uio_iov++;
399             uiop->uio_iovcnt--;
400             continue;
401         }
402
403         pos = uiop->uio_offset;
404         if (rw == UIO_READ)
405             code = afs_file_read(filp, iov->iov_base, count, &pos);
406         else
407             code = afs_file_write(filp, iov->iov_base, count, &pos);
408
409         if (code < 0) {
410             code = -code;
411             break;
412         } else if (code == 0) {
413             /*
414              * This is bad -- we can't read any more data from the
415              * file, but we have no good way of signaling a partial
416              * read either.
417              */
418             code = EIO;
419             break;
420         }
421
422         iov->iov_base += code;
423         iov->iov_len -= code;
424         uiop->uio_resid -= code;
425         uiop->uio_offset += code;
426         code = 0;
427     }
428
429 #ifdef AFS_FILE_NEEDS_SET_FS
430     if (uiop->uio_seg == AFS_UIOSYS) {
431         /* Switch back into kernel space */
432         set_fs(old_fs);
433     }
434 #endif /* AFS_FILE_NEEDS_SET_FS */
435
436     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
437
438     return code;
439 }
440
441 /* setup_uio 
442  * Setup a uio struct.
443  */
444 void
445 setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
446           int count, uio_flag_t flag, uio_seg_t seg)
447 {
448     iovecp->iov_base = (char *)buf;
449     iovecp->iov_len = count;
450     uiop->uio_iov = iovecp;
451     uiop->uio_iovcnt = 1;
452     uiop->uio_offset = pos;
453     uiop->uio_seg = seg;
454     uiop->uio_resid = count;
455     uiop->uio_flag = flag;
456 }
457
458
459 /* uiomove
460  * UIO_READ : dp -> uio
461  * UIO_WRITE : uio -> dp
462  */
463 int
464 uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
465 {
466     int count;
467     struct iovec *iov;
468     int code;
469
470     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
471         iov = uiop->uio_iov;
472         count = iov->iov_len;
473
474         if (!count) {
475             uiop->uio_iov++;
476             uiop->uio_iovcnt--;
477             continue;
478         }
479
480         if (count > length)
481             count = length;
482
483         switch (uiop->uio_seg) {
484         case AFS_UIOSYS:
485             switch (rw) {
486             case UIO_READ:
487                 memcpy(iov->iov_base, dp, count);
488                 break;
489             case UIO_WRITE:
490                 memcpy(dp, iov->iov_base, count);
491                 break;
492             default:
493                 printf("uiomove: Bad rw = %d\n", rw);
494                 return -EINVAL;
495             }
496             break;
497         case AFS_UIOUSER:
498             switch (rw) {
499             case UIO_READ:
500                 AFS_COPYOUT(dp, iov->iov_base, count, code);
501                 break;
502             case UIO_WRITE:
503                 AFS_COPYIN(iov->iov_base, dp, count, code);
504                 break;
505             default:
506                 printf("uiomove: Bad rw = %d\n", rw);
507                 return -EINVAL;
508             }
509             break;
510         default:
511             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
512             return -EINVAL;
513         }
514
515         dp += count;
516         length -= count;
517         iov->iov_base += count;
518         iov->iov_len -= count;
519         uiop->uio_offset += count;
520         uiop->uio_resid -= count;
521     }
522     return 0;
523 }
524