8571795cc8c99d58ee52321f0d19171587e45579
[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 "../afs/param.h"       /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h"  /* afs statistics */
14 #include "../h/smp_lock.h"
15
16
17 int afs_osicred_initialized=0;
18 struct  AFS_UCRED afs_osi_cred;
19 afs_lock_t afs_xosi;            /* lock is for tvattr */
20 extern struct osi_dev cacheDev;
21 extern struct super_block *afs_cacheSBp;
22
23 void *osi_UFSOpen(ainode)
24     afs_int32 ainode;
25 {
26     struct inode *ip;
27     register struct osi_file *afile = NULL;
28     extern int cacheDiskType;
29     afs_int32 code = 0;
30     int dummy;
31     struct inode *tip = NULL;
32     struct file *filp = NULL;
33     AFS_STATCNT(osi_UFSOpen);
34     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
35         osi_Panic("UFSOpen called for non-UFS cache\n");
36     }
37     if (!afs_osicred_initialized) {
38         /* valid for alpha_osf, SunOS, Ultrix */
39         bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
40         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
41         afs_osicred_initialized = 1;
42     }
43     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
44     AFS_GUNLOCK();
45     if (!afile) {
46         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
47                    sizeof(struct osi_file));
48     }
49     memset(afile, 0, sizeof(struct osi_file));
50     filp = &afile->file;
51     filp->f_dentry = &afile->dentry;
52     tip = iget(afs_cacheSBp, (u_long)ainode);
53     if (!tip)
54         osi_Panic("Can't get inode %d\n", ainode);
55     FILE_INODE(filp) = tip;
56     tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
57     filp->f_flags = O_RDWR;
58 #if defined(AFS_LINUX24_ENV)
59     filp->f_op = fops_get(tip->i_fop);
60 #else
61     filp->f_op = tip->i_op->default_file_ops;
62 #endif
63     if (filp->f_op && filp->f_op->open)
64         code = filp->f_op->open(tip, filp);
65     if (code)
66         osi_Panic("Can't open inode %d\n", ainode);
67     afile->size = tip->i_size;
68     AFS_GLOCK();
69     afile->offset = 0;
70     afile->proc = (int (*)()) 0;
71     afile->inum = ainode;        /* for hint validity checking */
72     return (void *)afile;
73 }
74
75 afs_osi_Stat(afile, astat)
76     register struct osi_file *afile;
77     register struct osi_stat *astat; {
78     register afs_int32 code;
79     AFS_STATCNT(osi_Stat);
80     MObtainWriteLock(&afs_xosi,320);
81     astat->size = FILE_INODE(&afile->file)->i_size;
82     astat->blksize = FILE_INODE(&afile->file)->i_blksize;
83     astat->mtime = FILE_INODE(&afile->file)->i_mtime;
84     astat->atime = FILE_INODE(&afile->file)->i_atime;
85     code = 0;
86     MReleaseWriteLock(&afs_xosi);
87     return code;
88 }
89
90 osi_UFSClose(afile)
91      register struct osi_file *afile;
92   {
93       AFS_STATCNT(osi_Close);
94       if (afile) {
95           if (FILE_INODE(&afile->file)) {
96               struct file *filp = &afile->file;
97               if (filp->f_op && filp->f_op->release)
98                   filp->f_op->release(FILE_INODE(filp), filp);
99               iput(FILE_INODE(filp));
100           }
101       }
102       
103       osi_FreeSmallSpace(afile);
104       return 0;
105   }
106
107 osi_UFSTruncate(afile, asize)
108     register struct osi_file *afile;
109     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);
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         inode_setattr(inode, &newattrs);
136     unlock_kernel();
137 #else
138     if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
139         code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
140     }
141     if (!code) {
142         truncate_inode_pages(inode, asize);
143         if (inode->i_op && inode->i_op->truncate)
144             inode->i_op->truncate(inode);
145     }
146 #endif
147     code = -code;
148     up(&inode->i_sem);
149     AFS_GLOCK();
150     MReleaseWriteLock(&afs_xosi);
151     return code;
152 }
153
154
155 /* Generic read interface */
156 afs_osi_Read(afile, offset, aptr, asize)
157     register struct osi_file *afile;
158     int offset;
159     char *aptr;
160     afs_int32 asize; {
161     struct AFS_UCRED *oldCred;
162     size_t resid;
163     register afs_int32 code;
164     register afs_int32 cnt1=0;
165     AFS_STATCNT(osi_Read);
166
167     /**
168       * If the osi_file passed in is NULL, panic only if AFS is not shutting
169       * down. No point in crashing when we are already shutting down
170       */
171     if ( !afile ) {
172         if ( !afs_shuttingdown )
173             osi_Panic("osi_Read called with null param");
174         else
175             return EIO;
176     }
177
178     if (offset != -1) afile->offset = offset;
179     AFS_GUNLOCK();
180     code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
181     AFS_GLOCK();
182     if (code == 0) {
183         code = asize - resid;
184         afile->offset += code;
185     }
186     else {
187         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
188                  ICL_TYPE_INT32, code);
189         code = -1;
190     }
191     return code;
192 }
193
194 /* Generic write interface */
195 afs_osi_Write(afile, offset, aptr, asize)
196     register struct osi_file *afile;
197     char *aptr;
198     afs_int32 offset;
199     afs_int32 asize; {
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(aproc, bp)
229     int (*aproc)();
230     register struct buf *bp;
231 {
232     afs_int32 returnCode;
233
234     AFS_STATCNT(osi_MapStrategy);
235     returnCode = (*aproc) (bp);
236
237     return returnCode;
238 }
239
240 void
241 shutdown_osifile()
242 {
243   extern int afs_cold_shutdown;
244
245   AFS_STATCNT(shutdown_osifile);
246   if (afs_cold_shutdown) {
247     afs_osicred_initialized = 0;
248   }
249 }
250