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"
25 int cache_fh_type = -1;
26 int cache_fh_len = -1;
28 afs_lock_t afs_xosi; /* lock is for tvattr */
29 extern struct osi_dev cacheDev;
30 extern struct vfsmount *afs_cacheMnt;
31 extern struct super_block *afs_cacheSBp;
32 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
33 extern struct cred *cache_creds;
36 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
38 afs_fh_acceptable(void *context, struct dentry *dp)
44 afs_linux_raw_open(afs_dcache_id_t *ainode)
46 struct inode *tip = NULL;
47 struct dentry *dp = NULL;
50 dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
52 if ((!dp) || IS_ERR(dp))
53 osi_Panic("Can't get dentry\n");
55 tip->i_flags |= S_NOATIME; /* Disable updating access times. */
57 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
58 /* Use stashed credentials - prevent selinux/apparmor problems */
59 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, cache_creds);
61 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
63 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
66 osi_Panic("Can't open file: %d\n", (int) PTR_ERR(filp));
71 osi_UFSOpen(afs_dcache_id_t *ainode)
73 struct osi_file *afile = NULL;
74 extern int cacheDiskType;
76 AFS_STATCNT(osi_UFSOpen);
77 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
78 osi_Panic("UFSOpen called for non-UFS cache\n");
80 if (!afs_osicred_initialized) {
81 /* valid for alpha_osf, SunOS, Ultrix */
82 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
83 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
84 afs_osicred_initialized = 1;
87 afile = kmalloc(sizeof(struct osi_file), GFP_NOFS);
89 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
90 (int)sizeof(struct osi_file));
92 memset(afile, 0, sizeof(struct osi_file));
94 afile->filp = afs_linux_raw_open(ainode);
95 afile->size = i_size_read(FILE_INODE(afile->filp));
98 afile->proc = (int (*)())0;
103 * Given a dentry, return the file handle as encoded by the filesystem.
104 * We can't assume anything about the length (words, not bytes).
105 * The cache has to live on a single filesystem with uniform file
106 * handles, otherwise we panic.
108 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
112 if (cache_fh_len > 0)
113 max_len = cache_fh_len;
115 max_len = MAX_FH_LEN;
116 type = afs_get_fh_from_dentry(dp, ainode, &max_len);
118 osi_Panic("File handle encoding failed\n");
120 if (cache_fh_type < 0)
121 cache_fh_type = type;
122 if (cache_fh_len < 0) {
123 cache_fh_len = max_len;
125 if (type != cache_fh_type || max_len != cache_fh_len) {
126 osi_Panic("Inconsistent file handles within cache\n");
131 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
133 AFS_STATCNT(osi_Stat);
134 ObtainWriteLock(&afs_xosi, 320);
135 astat->size = i_size_read(OSIFILE_INODE(afile));
136 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
137 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
139 ReleaseWriteLock(&afs_xosi);
144 osi_UFSClose(struct osi_file *afile)
146 AFS_STATCNT(osi_Close);
148 if (OSIFILE_INODE(afile)) {
149 filp_close(afile->filp, NULL);
157 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
160 struct osi_stat tstat;
161 struct iattr newattrs;
162 struct inode *inode = OSIFILE_INODE(afile);
163 AFS_STATCNT(osi_Truncate);
165 /* This routine only shrinks files, and most systems
166 * have very slow truncates, even when the file is already
167 * small enough. Check now and save some time.
169 code = afs_osi_Stat(afile, &tstat);
170 if (code || tstat.size <= asize)
172 ObtainWriteLock(&afs_xosi, 321);
174 afs_linux_lock_inode(inode);
175 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
176 down_write(&inode->i_alloc_sem);
178 newattrs.ia_size = asize;
179 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
180 newattrs.ia_ctime = CURRENT_TIME;
182 /* avoid notify_change() since it wants to update dentry->d_parent */
183 code = inode_change_ok(inode, &newattrs);
185 code = afs_inode_setattr(afile, &newattrs);
187 truncate_inode_pages(&inode->i_data, asize);
189 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
190 up_write(&inode->i_alloc_sem);
192 afs_linux_unlock_inode(inode);
194 ReleaseWriteLock(&afs_xosi);
199 /* Generic read interface */
201 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
208 AFS_STATCNT(osi_Read);
211 * If the osi_file passed in is NULL, panic only if AFS is not shutting
212 * down. No point in crashing when we are already shutting down
215 if (!afs_shuttingdown)
216 osi_Panic("osi_Read called with null param");
222 afile->offset = offset;
223 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
225 code = osi_rdwr(afile, &auio, UIO_READ);
228 code = asize - auio.uio_resid;
229 afile->offset += code;
231 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
232 ICL_TYPE_INT32, code);
238 /* Generic write interface */
240 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
247 AFS_STATCNT(osi_Write);
250 if (!afs_shuttingdown)
251 osi_Panic("afs_osi_Write called with null param");
257 afile->offset = offset;
258 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
260 code = osi_rdwr(afile, &auio, UIO_WRITE);
263 code = asize - auio.uio_resid;
264 afile->offset += code;
268 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
273 (*afile->proc)(afile, code);
279 /* This work should be handled by physstrat in ca/machdep.c.
280 This routine written from the RT NFS port strategy routine.
281 It has been generalized a bit, but should still be pretty clear. */
283 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
285 afs_int32 returnCode;
287 AFS_STATCNT(osi_MapStrategy);
288 returnCode = (*aproc) (bp);
294 shutdown_osifile(void)
296 AFS_STATCNT(shutdown_osifile);
297 if (afs_cold_shutdown) {
298 afs_osicred_initialized = 0;
302 /* Intialize cache device info and fragment size for disk cache partition. */
304 osi_InitCacheInfo(char *aname)
307 extern afs_dcache_id_t cacheInode;
309 extern struct osi_dev cacheDev;
310 extern afs_int32 afs_fsfragsize;
311 extern struct super_block *afs_cacheSBp;
312 extern struct vfsmount *afs_cacheMnt;
313 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
317 osi_get_fh(dp, &cacheInode.ufs);
318 cacheDev.dev = dp->d_inode->i_sb->s_dev;
319 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
320 afs_cacheSBp = dp->d_inode->i_sb;
324 afs_init_sb_export_ops(afs_cacheSBp);
331 * seek, then read or write to an open inode. addrp points to data in
335 osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
337 struct file *filp = osifile->filp;
338 mm_segment_t old_fs = {0};
342 unsigned long savelim;
345 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
346 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
348 if (uiop->uio_seg == AFS_UIOSYS) {
349 /* Switch into user space */
354 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
356 count = iov->iov_len;
363 pos = uiop->uio_offset;
365 code = filp->f_op->read(filp, iov->iov_base, count, &pos);
367 code = filp->f_op->write(filp, iov->iov_base, count, &pos);
372 } else if (code == 0) {
374 * This is bad -- we can't read any more data from the
375 * file, but we have no good way of signaling a partial
382 iov->iov_base += code;
383 iov->iov_len -= code;
384 uiop->uio_resid -= code;
385 uiop->uio_offset += code;
389 if (uiop->uio_seg == AFS_UIOSYS) {
390 /* Switch back into kernel space */
394 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
400 * Setup a uio struct.
403 setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
404 int count, uio_flag_t flag, uio_seg_t seg)
406 iovecp->iov_base = (char *)buf;
407 iovecp->iov_len = count;
408 uiop->uio_iov = iovecp;
409 uiop->uio_iovcnt = 1;
410 uiop->uio_offset = pos;
412 uiop->uio_resid = count;
413 uiop->uio_flag = flag;
418 * UIO_READ : dp -> uio
419 * UIO_WRITE : uio -> dp
422 uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
428 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
430 count = iov->iov_len;
441 switch (uiop->uio_seg) {
445 memcpy(iov->iov_base, dp, count);
448 memcpy(dp, iov->iov_base, count);
451 printf("uiomove: Bad rw = %d\n", rw);
458 AFS_COPYOUT(dp, iov->iov_base, count, code);
461 AFS_COPYIN(iov->iov_base, dp, count, code);
464 printf("uiomove: Bad rw = %d\n", rw);
469 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
475 iov->iov_base += count;
476 iov->iov_len -= count;
477 uiop->uio_offset += count;
478 uiop->uio_resid -= count;