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 # ifdef IATTR_TAKES_64BIT_TIME
27 # define CURRENT_TIME (current_kernel_time64())
29 # define CURRENT_TIME (current_kernel_time())
33 int cache_fh_type = -1;
34 int cache_fh_len = -1;
36 extern struct osi_dev cacheDev;
37 extern struct vfsmount *afs_cacheMnt;
38 extern struct super_block *afs_cacheSBp;
39 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
40 extern struct cred *cache_creds;
43 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
45 afs_fh_acceptable(void *context, struct dentry *dp)
51 afs_linux_raw_open(afs_dcache_id_t *ainode)
53 struct inode *tip = NULL;
54 struct dentry *dp = NULL;
57 dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
59 if ((!dp) || IS_ERR(dp))
60 osi_Panic("Can't get dentry\n");
62 tip->i_flags |= S_NOATIME; /* Disable updating access times. */
64 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
65 /* Use stashed credentials - prevent selinux/apparmor problems */
66 filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, cache_creds);
68 filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, current_cred());
70 filp = dentry_open(dget(dp), mntget(afs_cacheMnt), O_RDWR);
73 afs_warn("afs: Cannot open cache file (code %d). Trying to continue, "
74 "but AFS accesses may return errors or panic the system\n",
85 osi_UFSOpen(afs_dcache_id_t *ainode)
87 struct osi_file *afile = NULL;
88 extern int cacheDiskType;
90 AFS_STATCNT(osi_UFSOpen);
91 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
92 osi_Panic("UFSOpen called for non-UFS cache\n");
94 if (!afs_osicred_initialized) {
95 /* valid for SunOS, Ultrix */
96 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
97 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
98 afs_osicred_initialized = 1;
101 afile = kmalloc(sizeof(struct osi_file), GFP_NOFS);
103 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
104 (int)sizeof(struct osi_file));
106 memset(afile, 0, sizeof(struct osi_file));
108 afile->filp = afs_linux_raw_open(ainode);
110 afile->size = i_size_read(FILE_INODE(afile->filp));
115 osi_FreeLargeSpace(afile);
120 afile->proc = (int (*)())0;
121 return (void *)afile;
125 * Given a dentry, return the file handle as encoded by the filesystem.
126 * We can't assume anything about the length (words, not bytes).
127 * The cache has to live on a single filesystem with uniform file
128 * handles, otherwise we panic.
130 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
134 if (cache_fh_len > 0)
135 max_len = cache_fh_len;
137 max_len = MAX_FH_LEN;
138 type = afs_get_fh_from_dentry(dp, ainode, &max_len);
140 osi_Panic("File handle encoding failed\n");
142 if (cache_fh_type < 0)
143 cache_fh_type = type;
144 if (cache_fh_len < 0) {
145 cache_fh_len = max_len;
147 if (type != cache_fh_type || max_len != cache_fh_len) {
148 osi_Panic("Inconsistent file handles within cache\n");
153 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
155 AFS_STATCNT(osi_Stat);
156 astat->size = i_size_read(OSIFILE_INODE(afile));
157 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
158 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
164 osi_UFSClose(struct osi_file *afile)
166 AFS_STATCNT(osi_Close);
168 if (OSIFILE_INODE(afile)) {
169 filp_close(afile->filp, NULL);
177 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
180 struct osi_stat tstat;
181 struct iattr newattrs;
182 struct inode *inode = OSIFILE_INODE(afile);
183 AFS_STATCNT(osi_Truncate);
185 /* This routine only shrinks files, and most systems
186 * have very slow truncates, even when the file is already
187 * small enough. Check now and save some time.
189 code = afs_osi_Stat(afile, &tstat);
190 if (code || tstat.size <= asize)
193 afs_linux_lock_inode(inode);
194 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
195 down_write(&inode->i_alloc_sem);
197 newattrs.ia_size = asize;
198 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
199 newattrs.ia_ctime = CURRENT_TIME;
201 /* avoid notify_change() since it wants to update dentry->d_parent */
202 #ifdef HAVE_LINUX_SETATTR_PREPARE
203 code = setattr_prepare(file_dentry(afile->filp), &newattrs);
205 code = inode_change_ok(inode, &newattrs);
208 code = afs_inode_setattr(afile, &newattrs);
210 truncate_inode_pages(&inode->i_data, asize);
212 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
213 up_write(&inode->i_alloc_sem);
215 afs_linux_unlock_inode(inode);
221 /* Generic read interface */
223 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
230 memset(&auio, 0, sizeof(auio));
231 memset(&iov, 0, sizeof(iov));
233 AFS_STATCNT(osi_Read);
236 * If the osi_file passed in is NULL, panic only if AFS is not shutting
237 * down. No point in crashing when we are already shutting down
240 if (afs_shuttingdown == AFS_RUNNING)
241 osi_Panic("osi_Read called with null param");
247 afile->offset = offset;
248 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
250 code = osi_rdwr(afile, &auio, UIO_READ);
253 code = asize - auio.uio_resid;
254 afile->offset += code;
256 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
257 ICL_TYPE_INT32, code);
265 /* Generic write interface */
267 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
274 memset(&auio, 0, sizeof(auio));
275 memset(&iov, 0, sizeof(iov));
277 AFS_STATCNT(osi_Write);
280 if (afs_shuttingdown == AFS_RUNNING)
281 osi_Panic("afs_osi_Write called with null param");
287 afile->offset = offset;
288 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
290 code = osi_rdwr(afile, &auio, UIO_WRITE);
293 code = asize - auio.uio_resid;
294 afile->offset += code;
298 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
305 (*afile->proc)(afile, code);
311 /* This work should be handled by physstrat in ca/machdep.c.
312 This routine written from the RT NFS port strategy routine.
313 It has been generalized a bit, but should still be pretty clear. */
315 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
317 afs_int32 returnCode;
319 AFS_STATCNT(osi_MapStrategy);
320 returnCode = (*aproc) (bp);
326 shutdown_osifile(void)
328 AFS_STATCNT(shutdown_osifile);
329 if (afs_cold_shutdown) {
330 afs_osicred_initialized = 0;
334 /* Intialize cache device info and fragment size for disk cache partition. */
336 osi_InitCacheInfo(char *aname)
339 extern afs_dcache_id_t cacheInode;
341 extern struct osi_dev cacheDev;
342 extern afs_int32 afs_fsfragsize;
343 extern struct super_block *afs_cacheSBp;
344 extern struct vfsmount *afs_cacheMnt;
345 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
349 osi_get_fh(dp, &cacheInode.ufs);
350 cacheDev.dev = dp->d_inode->i_sb->s_dev;
351 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
352 afs_cacheSBp = dp->d_inode->i_sb;
356 afs_init_sb_export_ops(afs_cacheSBp);
363 * seek, then read or write to an open inode. addrp points to data in
367 osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
369 struct file *filp = osifile->filp;
370 #ifdef AFS_FILE_NEEDS_SET_FS
371 mm_segment_t old_fs = {0};
372 #endif /* AFS_FILE_NEEDS_SET_FS */
376 unsigned long savelim;
379 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
380 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
382 #ifdef AFS_FILE_NEEDS_SET_FS
383 if (uiop->uio_seg == AFS_UIOSYS) {
384 /* Switch into user space */
388 #endif /* AFS_FILE_NEEDS_SET_FS */
390 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
392 count = iov->iov_len;
399 pos = uiop->uio_offset;
401 code = afs_file_read(filp, iov->iov_base, count, &pos);
403 code = afs_file_write(filp, iov->iov_base, count, &pos);
408 } else if (code == 0) {
410 * This is bad -- we can't read any more data from the
411 * file, but we have no good way of signaling a partial
418 iov->iov_base += code;
419 iov->iov_len -= code;
420 uiop->uio_resid -= code;
421 uiop->uio_offset += code;
425 #ifdef AFS_FILE_NEEDS_SET_FS
426 if (uiop->uio_seg == AFS_UIOSYS) {
427 /* Switch back into kernel space */
430 #endif /* AFS_FILE_NEEDS_SET_FS */
432 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
438 * Setup a uio struct.
441 setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
442 int count, uio_flag_t flag, uio_seg_t seg)
444 iovecp->iov_base = (char *)buf;
445 iovecp->iov_len = count;
446 uiop->uio_iov = iovecp;
447 uiop->uio_iovcnt = 1;
448 uiop->uio_offset = pos;
450 uiop->uio_resid = count;
451 uiop->uio_flag = flag;
456 * UIO_READ : dp -> uio
457 * UIO_WRITE : uio -> dp
460 uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
466 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
468 count = iov->iov_len;
479 switch (uiop->uio_seg) {
483 memcpy(iov->iov_base, dp, count);
486 memcpy(dp, iov->iov_base, count);
489 printf("uiomove: Bad rw = %d\n", rw);
496 AFS_COPYOUT(dp, iov->iov_base, count, code);
499 AFS_COPYIN(iov->iov_base, dp, count, code);
502 printf("uiomove: Bad rw = %d\n", rw);
507 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
513 iov->iov_base += count;
514 iov->iov_len -= count;
515 uiop->uio_offset += count;
516 uiop->uio_resid -= count;