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"
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 "h/smp_lock.h"
21 int afs_osicred_initialized=0;
22 struct AFS_UCRED afs_osi_cred;
23 afs_lock_t afs_xosi; /* lock is for tvattr */
24 extern struct osi_dev cacheDev;
25 extern struct super_block *afs_cacheSBp;
27 void *osi_UFSOpen(afs_int32 ainode)
30 register struct osi_file *afile = NULL;
31 extern int cacheDiskType;
34 struct inode *tip = NULL;
35 struct file *filp = NULL;
36 AFS_STATCNT(osi_UFSOpen);
37 if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
38 osi_Panic("UFSOpen called for non-UFS cache\n");
40 if (!afs_osicred_initialized) {
41 /* valid for alpha_osf, SunOS, Ultrix */
42 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
43 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
44 afs_osicred_initialized = 1;
46 afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
49 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
50 sizeof(struct osi_file));
52 memset(afile, 0, sizeof(struct osi_file));
54 filp->f_dentry = &afile->dentry;
55 tip = iget(afs_cacheSBp, (u_long)ainode);
57 osi_Panic("Can't get inode %d\n", ainode);
58 FILE_INODE(filp) = tip;
59 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
60 filp->f_flags = O_RDWR;
61 #if defined(AFS_LINUX24_ENV)
62 filp->f_op = fops_get(tip->i_fop);
64 filp->f_op = tip->i_op->default_file_ops;
66 if (filp->f_op && filp->f_op->open)
67 code = filp->f_op->open(tip, filp);
69 osi_Panic("Can't open inode %d\n", ainode);
70 afile->size = tip->i_size;
73 afile->proc = (int (*)()) 0;
74 afile->inum = ainode; /* for hint validity checking */
78 int afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
80 register afs_int32 code;
81 AFS_STATCNT(osi_Stat);
82 MObtainWriteLock(&afs_xosi,320);
83 astat->size = FILE_INODE(&afile->file)->i_size;
84 astat->blksize = FILE_INODE(&afile->file)->i_blksize;
85 astat->mtime = FILE_INODE(&afile->file)->i_mtime;
86 astat->atime = FILE_INODE(&afile->file)->i_atime;
88 MReleaseWriteLock(&afs_xosi);
92 int osi_UFSClose(register struct osi_file *afile)
94 AFS_STATCNT(osi_Close);
96 if (FILE_INODE(&afile->file)) {
97 struct file *filp = &afile->file;
98 if (filp->f_op && filp->f_op->release)
99 filp->f_op->release(FILE_INODE(filp), filp);
100 iput(FILE_INODE(filp));
104 osi_FreeSmallSpace(afile);
108 int osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
110 struct AFS_UCRED *oldCred;
111 register afs_int32 code;
112 struct osi_stat tstat;
113 struct iattr newattrs;
114 struct inode *inode = FILE_INODE(&afile->file);
115 AFS_STATCNT(osi_Truncate);
117 /* This routine only shrinks files, and most systems
118 * have very slow truncates, even when the file is already
119 * small enough. Check now and save some time.
121 code = afs_osi_Stat(afile, &tstat);
122 if (code || tstat.size <= asize) return code;
123 MObtainWriteLock(&afs_xosi,321);
126 inode->i_size = newattrs.ia_size = asize;
127 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
128 #if defined(AFS_LINUX24_ENV)
129 newattrs.ia_ctime = CURRENT_TIME;
131 /* avoid notify_change() since it wants to update dentry->d_parent */
133 code = inode_change_ok(inode, &newattrs);
135 #ifdef INODE_SETATTR_NOT_VOID
136 code = inode_setattr(inode, &newattrs);
138 inode_setattr(inode, &newattrs);
142 truncate_inode_pages(&inode->i_data, asize);
144 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
145 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
148 truncate_inode_pages(inode, asize);
149 if (inode->i_op && inode->i_op->truncate)
150 inode->i_op->truncate(inode);
156 MReleaseWriteLock(&afs_xosi);
161 /* Generic read interface */
162 int afs_osi_Read(register struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
164 struct AFS_UCRED *oldCred;
166 register afs_int32 code;
167 register afs_int32 cnt1=0;
168 AFS_STATCNT(osi_Read);
171 * If the osi_file passed in is NULL, panic only if AFS is not shutting
172 * down. No point in crashing when we are already shutting down
175 if ( !afs_shuttingdown )
176 osi_Panic("osi_Read called with null param");
181 if (offset != -1) afile->offset = offset;
183 code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
186 code = asize - resid;
187 afile->offset += code;
190 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
191 ICL_TYPE_INT32, code);
197 /* Generic write interface */
198 int afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
200 struct AFS_UCRED *oldCred;
202 register afs_int32 code;
203 AFS_STATCNT(osi_Write);
205 osi_Panic("afs_osi_Write called with null param");
206 if (offset != -1) afile->offset = offset;
208 code = osi_rdwr(UIO_WRITE, afile, (caddr_t)aptr, asize, &resid);
211 code = asize - resid;
212 afile->offset += code;
215 if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
219 (*afile->proc)(afile, code);
225 /* This work should be handled by physstrat in ca/machdep.c.
226 This routine written from the RT NFS port strategy routine.
227 It has been generalized a bit, but should still be pretty clear. */
228 int afs_osi_MapStrategy(int (*aproc)(struct buf *bp), register struct buf *bp)
230 afs_int32 returnCode;
232 AFS_STATCNT(osi_MapStrategy);
233 returnCode = (*aproc) (bp);
238 void shutdown_osifile(void)
240 extern int afs_cold_shutdown;
242 AFS_STATCNT(shutdown_osifile);
243 if (afs_cold_shutdown) {
244 afs_osicred_initialized = 0;