more-prototyping-20030513
[openafs.git] / src / afs / LINUX / osi_file.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID("$Header$");
14
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"
19
20
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;
26
27 void *osi_UFSOpen(afs_int32 ainode)
28 {
29     struct inode *ip;
30     register struct osi_file *afile = NULL;
31     extern int cacheDiskType;
32     afs_int32 code = 0;
33     int dummy;
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");
39     }
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;
45     }
46     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
47     AFS_GUNLOCK();
48     if (!afile) {
49         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
50                    sizeof(struct osi_file));
51     }
52     memset(afile, 0, sizeof(struct osi_file));
53     filp = &afile->file;
54     filp->f_dentry = &afile->dentry;
55     tip = iget(afs_cacheSBp, (u_long)ainode);
56     if (!tip)
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);
63 #else
64     filp->f_op = tip->i_op->default_file_ops;
65 #endif
66     if (filp->f_op && filp->f_op->open)
67         code = filp->f_op->open(tip, filp);
68     if (code)
69         osi_Panic("Can't open inode %d\n", ainode);
70     afile->size = tip->i_size;
71     AFS_GLOCK();
72     afile->offset = 0;
73     afile->proc = (int (*)()) 0;
74     afile->inum = ainode;        /* for hint validity checking */
75     return (void *)afile;
76 }
77
78 int afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
79 {
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;
87     code = 0;
88     MReleaseWriteLock(&afs_xosi);
89     return code;
90 }
91
92 int osi_UFSClose(register struct osi_file *afile)
93   {
94       AFS_STATCNT(osi_Close);
95       if (afile) {
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));
101           }
102       }
103       
104       osi_FreeSmallSpace(afile);
105       return 0;
106   }
107
108 int osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
109 {
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);
116
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.
120      */
121     code = afs_osi_Stat(afile, &tstat);
122     if (code || tstat.size <= asize) return code;
123     MObtainWriteLock(&afs_xosi,321);    
124     AFS_GUNLOCK();
125     down(&inode->i_sem);
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;
130
131     /* avoid notify_change() since it wants to update dentry->d_parent */
132     lock_kernel();
133     code = inode_change_ok(inode, &newattrs);
134     if (!code)
135 #ifdef INODE_SETATTR_NOT_VOID
136         code = inode_setattr(inode, &newattrs);
137 #else
138         inode_setattr(inode, &newattrs);
139 #endif
140     unlock_kernel();
141     if (!code)
142         truncate_inode_pages(&inode->i_data, asize);
143 #else
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);
146     }
147     if (!code) {
148         truncate_inode_pages(inode, asize);
149         if (inode->i_op && inode->i_op->truncate)
150             inode->i_op->truncate(inode);
151     }
152 #endif
153     code = -code;
154     up(&inode->i_sem);
155     AFS_GLOCK();
156     MReleaseWriteLock(&afs_xosi);
157     return code;
158 }
159
160
161 /* Generic read interface */
162 int afs_osi_Read(register struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
163 {
164     struct AFS_UCRED *oldCred;
165     size_t resid;
166     register afs_int32 code;
167     register afs_int32 cnt1=0;
168     AFS_STATCNT(osi_Read);
169
170     /**
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
173       */
174     if ( !afile ) {
175         if ( !afs_shuttingdown )
176             osi_Panic("osi_Read called with null param");
177         else
178             return EIO;
179     }
180
181     if (offset != -1) afile->offset = offset;
182     AFS_GUNLOCK();
183     code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
184     AFS_GLOCK();
185     if (code == 0) {
186         code = asize - resid;
187         afile->offset += code;
188     }
189     else {
190         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
191                  ICL_TYPE_INT32, code);
192         code = -1;
193     }
194     return code;
195 }
196
197 /* Generic write interface */
198 int afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
199 {
200     struct AFS_UCRED *oldCred;
201     size_t resid;
202     register afs_int32 code;
203     AFS_STATCNT(osi_Write);
204     if ( !afile )
205         osi_Panic("afs_osi_Write called with null param");
206     if (offset != -1) afile->offset = offset;
207     AFS_GUNLOCK();
208     code = osi_rdwr(UIO_WRITE, afile, (caddr_t)aptr, asize, &resid);
209     AFS_GLOCK();
210     if (code == 0) {
211         code = asize - resid;
212         afile->offset += code;
213     }
214     else {
215         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
216         code = -1;
217     }
218     if (afile->proc) {
219         (*afile->proc)(afile, code);
220     }
221     return code;
222 }
223
224
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)
229 {
230     afs_int32 returnCode;
231
232     AFS_STATCNT(osi_MapStrategy);
233     returnCode = (*aproc) (bp);
234
235     return returnCode;
236 }
237
238 void shutdown_osifile(void)
239 {
240   extern int afs_cold_shutdown;
241
242   AFS_STATCNT(shutdown_osifile);
243   if (afs_cold_shutdown) {
244     afs_osicred_initialized = 0;
245   }
246 }
247