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/smp_lock.h>
19 #include <linux/namei.h>
21 #if defined(HAVE_LINUX_EXPORTFS_H)
22 #include <linux/exportfs.h>
24 #include "osi_compat.h"
26 int cache_fh_type = -1;
27 int cache_fh_len = -1;
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;
37 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
39 afs_fh_acceptable(void *context, struct dentry *dp)
45 afs_linux_raw_open(afs_dcache_id_t *ainode)
47 struct inode *tip = NULL;
48 struct dentry *dp = NULL;
51 dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
54 osi_Panic("Can't get dentry\n");
56 tip->i_flags |= S_NOATIME; /* Disable updating access times. */
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);
62 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
64 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
67 osi_Panic("Can't open file: %d\n", (int) PTR_ERR(filp));
72 osi_UFSOpen(afs_dcache_id_t *ainode)
74 struct osi_file *afile = NULL;
75 extern int cacheDiskType;
77 AFS_STATCNT(osi_UFSOpen);
78 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
79 osi_Panic("UFSOpen called for non-UFS cache\n");
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;
87 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
90 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
91 (int)sizeof(struct osi_file));
93 memset(afile, 0, sizeof(struct osi_file));
95 afile->filp = afs_linux_raw_open(ainode);
96 afile->size = i_size_read(FILE_INODE(afile->filp));
99 afile->proc = (int (*)())0;
100 return (void *)afile;
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.
109 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
113 if (cache_fh_len > 0)
114 max_len = cache_fh_len;
116 max_len = MAX_FH_LEN;
117 type = afs_get_fh_from_dentry(dp, ainode, &max_len);
119 osi_Panic("File handle encoding failed\n");
121 if (cache_fh_type < 0)
122 cache_fh_type = type;
123 if (cache_fh_len < 0) {
124 cache_fh_len = max_len;
126 if (type != cache_fh_type || max_len != cache_fh_len) {
127 osi_Panic("Inconsistent file handles within cache\n");
132 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
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;
140 ReleaseWriteLock(&afs_xosi);
145 osi_UFSClose(register struct osi_file *afile)
147 AFS_STATCNT(osi_Close);
149 if (OSIFILE_INODE(afile)) {
150 filp_close(afile->filp, NULL);
154 osi_FreeLargeSpace(afile);
159 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
161 register afs_int32 code;
162 struct osi_stat tstat;
163 struct iattr newattrs;
164 struct inode *inode = OSIFILE_INODE(afile);
165 AFS_STATCNT(osi_Truncate);
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.
171 code = afs_osi_Stat(afile, &tstat);
172 if (code || tstat.size <= asize)
174 ObtainWriteLock(&afs_xosi, 321);
176 #ifdef STRUCT_INODE_HAS_I_MUTEX
177 mutex_lock(&inode->i_mutex);
181 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
182 down_write(&inode->i_alloc_sem);
184 newattrs.ia_size = asize;
185 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
186 newattrs.ia_ctime = CURRENT_TIME;
188 /* avoid notify_change() since it wants to update dentry->d_parent */
189 code = inode_change_ok(inode, &newattrs);
191 #ifdef INODE_SETATTR_NOT_VOID
192 if (inode->i_op && inode->i_op->setattr)
193 code = inode->i_op->setattr(afile->filp->f_dentry, &newattrs);
195 code = inode_setattr(inode, &newattrs);
197 inode_setattr(inode, &newattrs);
201 truncate_inode_pages(&inode->i_data, asize);
203 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
204 up_write(&inode->i_alloc_sem);
206 #ifdef STRUCT_INODE_HAS_I_MUTEX
207 mutex_unlock(&inode->i_mutex);
212 ReleaseWriteLock(&afs_xosi);
217 /* Generic read interface */
219 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
226 AFS_STATCNT(osi_Read);
229 * If the osi_file passed in is NULL, panic only if AFS is not shutting
230 * down. No point in crashing when we are already shutting down
233 if (!afs_shuttingdown)
234 osi_Panic("osi_Read called with null param");
240 afile->offset = offset;
241 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
243 code = osi_rdwr(afile, &auio, UIO_READ);
246 code = asize - auio.uio_resid;
247 afile->offset += code;
249 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
250 ICL_TYPE_INT32, code);
256 /* Generic write interface */
258 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
265 AFS_STATCNT(osi_Write);
268 if (!afs_shuttingdown)
269 osi_Panic("afs_osi_Write called with null param");
275 afile->offset = offset;
276 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
278 code = osi_rdwr(afile, &auio, UIO_WRITE);
281 code = asize - auio.uio_resid;
282 afile->offset += code;
286 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
291 (*afile->proc)(afile, code);
297 /* This work should be handled by physstrat in ca/machdep.c.
298 This routine written from the RT NFS port strategy routine.
299 It has been generalized a bit, but should still be pretty clear. */
301 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
303 afs_int32 returnCode;
305 AFS_STATCNT(osi_MapStrategy);
306 returnCode = (*aproc) (bp);
312 shutdown_osifile(void)
314 AFS_STATCNT(shutdown_osifile);
315 if (afs_cold_shutdown) {
316 afs_osicred_initialized = 0;
320 /* Intialize cache device info and fragment size for disk cache partition. */
322 osi_InitCacheInfo(char *aname)
325 extern afs_dcache_id_t cacheInode;
327 extern struct osi_dev cacheDev;
328 extern afs_int32 afs_fsfragsize;
329 extern struct super_block *afs_cacheSBp;
330 extern struct vfsmount *afs_cacheMnt;
331 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
335 osi_get_fh(dp, &cacheInode.ufs);
336 cacheDev.dev = dp->d_inode->i_sb->s_dev;
337 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
338 afs_cacheSBp = dp->d_inode->i_sb;
342 afs_init_sb_export_ops(afs_cacheSBp);
349 * seek, then read or write to an open inode. addrp points to data in
353 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
355 struct file *filp = osifile->filp;
356 mm_segment_t old_fs = {0};
360 unsigned long savelim;
363 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
364 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
366 if (uiop->uio_seg == AFS_UIOSYS) {
367 /* Switch into user space */
372 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
374 count = iov->iov_len;
381 pos = uiop->uio_offset;
383 code = filp->f_op->read(filp, iov->iov_base, count, &pos);
385 code = filp->f_op->write(filp, iov->iov_base, count, &pos);
390 } else if (code == 0) {
392 * This is bad -- we can't read any more data from the
393 * file, but we have no good way of signaling a partial
400 iov->iov_base += code;
401 iov->iov_len -= code;
402 uiop->uio_resid -= code;
403 uiop->uio_offset += code;
407 if (uiop->uio_seg == AFS_UIOSYS) {
408 /* Switch back into kernel space */
412 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
418 * Setup a uio struct.
421 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
422 int count, uio_flag_t flag, uio_seg_t seg)
424 iovecp->iov_base = (char *)buf;
425 iovecp->iov_len = count;
426 uiop->uio_iov = iovecp;
427 uiop->uio_iovcnt = 1;
428 uiop->uio_offset = pos;
430 uiop->uio_resid = count;
431 uiop->uio_flag = flag;
436 * UIO_READ : dp -> uio
437 * UIO_WRITE : uio -> dp
440 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
446 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
448 count = iov->iov_len;
459 switch (uiop->uio_seg) {
463 memcpy(iov->iov_base, dp, count);
466 memcpy(dp, iov->iov_base, count);
469 printf("uiomove: Bad rw = %d\n", rw);
476 AFS_COPYOUT(dp, iov->iov_base, count, code);
479 AFS_COPYIN(iov->iov_base, dp, count, code);
482 printf("uiomove: Bad rw = %d\n", rw);
487 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
493 iov->iov_base += count;
494 iov->iov_len -= count;
495 uiop->uio_offset += count;
496 uiop->uio_resid -= count;