2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
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>
20 #if defined(HAVE_LINUX_EXPORTFS_H)
21 #include <linux/exportfs.h>
23 #include "osi_compat.h"
26 # if defined(HAVE_LINUX_KTIME_GET_COARSE_REAL_TS64)
27 # define AFS_CURRENT_TIME(x) (ktime_get_coarse_real_ts64((x)))
29 # ifdef IATTR_TAKES_64BIT_TIME
30 # define AFS_CURRENT_TIME(x) do {*(x) = current_kernel_time64();} while (0)
32 # define AFS_CURRENT_TIME(x) do {*(x) = current_kernel_time();} while (0)
36 # define AFS_CURRENT_TIME(x) do {*(x) = CURRENT_TIME;} while(0)
39 int cache_fh_type = -1;
40 int cache_fh_len = -1;
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;
49 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
51 afs_fh_acceptable(void *context, struct dentry *dp)
57 afs_linux_raw_open(afs_dcache_id_t *ainode)
59 struct inode *tip = NULL;
60 struct dentry *dp = NULL;
62 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
63 const struct cred *cur_cred;
66 dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
68 if ((!dp) || IS_ERR(dp))
69 osi_Panic("Can't get dentry\n");
71 tip->i_flags |= S_NOATIME; /* Disable updating access times. */
73 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
74 cur_cred = override_creds(cache_creds);
76 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
77 /* Use stashed credentials - prevent selinux/apparmor problems */
78 filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, cache_creds);
80 filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, current_cred());
82 filp = dentry_open(dget(dp), mntget(afs_cacheMnt), O_RDWR);
84 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
85 revert_creds(cur_cred);
88 afs_warn("afs: Cannot open cache file (code %d). Trying to continue, "
89 "but AFS accesses may return errors or panic the system\n",
100 osi_UFSOpen(afs_dcache_id_t *ainode)
102 struct osi_file *afile = NULL;
103 extern int cacheDiskType;
105 AFS_STATCNT(osi_UFSOpen);
106 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
107 osi_Panic("UFSOpen called for non-UFS cache\n");
109 if (!afs_osicred_initialized) {
110 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
111 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
112 afs_osicred_initialized = 1;
115 afile = kmalloc(sizeof(struct osi_file), GFP_NOFS);
117 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
118 (int)sizeof(struct osi_file));
120 memset(afile, 0, sizeof(struct osi_file));
122 afile->filp = afs_linux_raw_open(ainode);
124 afile->size = i_size_read(FILE_INODE(afile->filp));
129 osi_FreeLargeSpace(afile);
134 afile->proc = (int (*)())0;
135 return (void *)afile;
139 * Given a dentry, return the file handle as encoded by the filesystem.
140 * We can't assume anything about the length (words, not bytes).
141 * The cache has to live on a single filesystem with uniform file
142 * handles, otherwise we panic.
144 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
148 if (cache_fh_len > 0)
149 max_len = cache_fh_len;
151 max_len = MAX_FH_LEN;
152 type = afs_get_fh_from_dentry(dp, ainode, &max_len);
154 osi_Panic("File handle encoding failed\n");
156 if (cache_fh_type < 0)
157 cache_fh_type = type;
158 if (cache_fh_len < 0) {
159 cache_fh_len = max_len;
161 if (type != cache_fh_type || max_len != cache_fh_len) {
162 osi_Panic("Inconsistent file handles within cache\n");
167 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
169 AFS_STATCNT(osi_Stat);
170 astat->size = i_size_read(OSIFILE_INODE(afile));
171 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
172 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
178 osi_UFSClose(struct osi_file *afile)
180 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
181 const struct cred *cur_cred;
184 AFS_STATCNT(osi_Close);
186 if (OSIFILE_INODE(afile)) {
187 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
188 cur_cred = override_creds(cache_creds);
190 filp_close(afile->filp, NULL);
191 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
192 revert_creds(cur_cred);
201 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
204 struct osi_stat tstat;
205 struct iattr newattrs;
206 struct inode *inode = OSIFILE_INODE(afile);
207 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
208 const struct cred *cur_cred;
210 AFS_STATCNT(osi_Truncate);
212 /* This routine only shrinks files, and most systems
213 * have very slow truncates, even when the file is already
214 * small enough. Check now and save some time.
216 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
217 cur_cred = override_creds(cache_creds);
219 code = afs_osi_Stat(afile, &tstat);
220 if (code || tstat.size <= asize)
223 afs_linux_lock_inode(inode);
224 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
225 down_write(&inode->i_alloc_sem);
227 newattrs.ia_size = asize;
228 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
229 AFS_CURRENT_TIME(&newattrs.ia_ctime);
231 /* avoid notify_change() since it wants to update dentry->d_parent */
232 #ifdef HAVE_LINUX_SETATTR_PREPARE
233 code = setattr_prepare(file_dentry(afile->filp), &newattrs);
235 code = inode_change_ok(inode, &newattrs);
238 code = afs_inode_setattr(afile, &newattrs);
240 truncate_inode_pages(&inode->i_data, asize);
242 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
243 up_write(&inode->i_alloc_sem);
245 afs_linux_unlock_inode(inode);
248 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
249 revert_creds(cur_cred);
255 /* Generic read interface */
257 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
264 memset(&auio, 0, sizeof(auio));
265 memset(&iov, 0, sizeof(iov));
267 AFS_STATCNT(osi_Read);
270 * If the osi_file passed in is NULL, panic only if AFS is not shutting
271 * down. No point in crashing when we are already shutting down
274 if (afs_shuttingdown == AFS_RUNNING)
275 osi_Panic("osi_Read called with null param");
281 afile->offset = offset;
282 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
284 code = osi_rdwr(afile, &auio, UIO_READ);
287 code = asize - auio.uio_resid;
288 afile->offset += code;
290 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
291 ICL_TYPE_INT32, code);
299 /* Generic write interface */
301 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
308 memset(&auio, 0, sizeof(auio));
309 memset(&iov, 0, sizeof(iov));
311 AFS_STATCNT(osi_Write);
314 if (afs_shuttingdown == AFS_RUNNING)
315 osi_Panic("afs_osi_Write called with null param");
321 afile->offset = offset;
322 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
324 code = osi_rdwr(afile, &auio, UIO_WRITE);
327 code = asize - auio.uio_resid;
328 afile->offset += code;
338 (*afile->proc)(afile, code);
344 /* This work should be handled by physstrat in ca/machdep.c.
345 This routine written from the RT NFS port strategy routine.
346 It has been generalized a bit, but should still be pretty clear. */
348 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
350 afs_int32 returnCode;
352 AFS_STATCNT(osi_MapStrategy);
353 returnCode = (*aproc) (bp);
359 shutdown_osifile(void)
361 AFS_STATCNT(shutdown_osifile);
362 if (afs_cold_shutdown) {
363 afs_osicred_initialized = 0;
367 /* Intialize cache device info and fragment size for disk cache partition. */
369 osi_InitCacheInfo(char *aname)
372 extern afs_dcache_id_t cacheInode;
374 extern struct osi_dev cacheDev;
375 extern afs_int32 afs_fsfragsize;
376 extern struct super_block *afs_cacheSBp;
377 extern struct vfsmount *afs_cacheMnt;
378 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
382 osi_get_fh(dp, &cacheInode.ufs);
383 cacheDev.dev = dp->d_inode->i_sb->s_dev;
384 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
385 afs_cacheSBp = dp->d_inode->i_sb;
389 afs_init_sb_export_ops(afs_cacheSBp);
396 * seek, then read or write to an open inode. addrp points to data in
400 osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
402 struct file *filp = osifile->filp;
403 #ifdef AFS_FILE_NEEDS_SET_FS
404 mm_segment_t old_fs = {0};
405 #endif /* AFS_FILE_NEEDS_SET_FS */
409 unsigned long savelim;
411 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
412 const struct cred *cur_cred = override_creds(cache_creds);
415 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
416 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
418 #ifdef AFS_FILE_NEEDS_SET_FS
419 if (uiop->uio_seg == AFS_UIOSYS) {
420 /* Switch into user space */
424 #endif /* AFS_FILE_NEEDS_SET_FS */
426 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
428 count = iov->iov_len;
435 pos = uiop->uio_offset;
437 code = afs_file_read(filp, iov->iov_base, count, &pos);
439 code = afs_file_write(filp, iov->iov_base, count, &pos);
444 } else if (code == 0) {
446 * This is bad -- we can't read any more data from the
447 * file, but we have no good way of signaling a partial
454 iov->iov_base += code;
455 iov->iov_len -= code;
456 uiop->uio_resid -= code;
457 uiop->uio_offset += code;
461 #ifdef AFS_FILE_NEEDS_SET_FS
462 if (uiop->uio_seg == AFS_UIOSYS) {
463 /* Switch back into kernel space */
466 #endif /* AFS_FILE_NEEDS_SET_FS */
468 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
469 #if defined(HAVE_LINUX_OVERRIDE_CREDS)
470 revert_creds(cur_cred);
477 * Setup a uio struct.
480 setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
481 int count, uio_flag_t flag, uio_seg_t seg)
483 iovecp->iov_base = (char *)buf;
484 iovecp->iov_len = count;
485 uiop->uio_iov = iovecp;
486 uiop->uio_iovcnt = 1;
487 uiop->uio_offset = pos;
489 uiop->uio_resid = count;
490 uiop->uio_flag = flag;
495 * UIO_READ : dp -> uio
496 * UIO_WRITE : uio -> dp
499 uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
505 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
507 count = iov->iov_len;
518 switch (uiop->uio_seg) {
522 memcpy(iov->iov_base, dp, count);
525 memcpy(dp, iov->iov_base, count);
528 printf("uiomove: Bad rw = %d\n", rw);
535 AFS_COPYOUT(dp, iov->iov_base, count, code);
538 AFS_COPYIN(iov->iov_base, dp, count, code);
541 printf("uiomove: Bad rw = %d\n", rw);
546 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
552 iov->iov_base += count;
553 iov->iov_len -= count;
554 uiop->uio_offset += count;
555 uiop->uio_resid -= count;