7f3ed0ffc01782e3305610b6d3246dffd38e87fb
[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 #if defined(AFS_LINUX24_ENV)
108 int osi_notify_change(struct dentry * dentry, struct iattr * attr)
109 {
110     struct inode *inode = dentry->d_inode;
111     int error;
112     time_t now = CURRENT_TIME;
113     unsigned int ia_valid = attr->ia_valid;
114
115     attr->ia_ctime = now;
116     if (!(ia_valid & ATTR_ATIME_SET))
117         attr->ia_atime = now;
118     if (!(ia_valid & ATTR_MTIME_SET))
119         attr->ia_mtime = now;
120
121     lock_kernel();
122     if (inode && inode->i_op && inode->i_op->setattr)
123         error = inode->i_op->setattr(dentry, attr);
124     else {
125         error = inode_change_ok(inode, attr);
126         if (!error)
127             inode_setattr(inode, attr);
128     }
129     unlock_kernel();
130     return error;
131 }
132 #endif
133
134 osi_UFSTruncate(afile, asize)
135     register struct osi_file *afile;
136     afs_int32 asize; {
137     struct AFS_UCRED *oldCred;
138     register afs_int32 code;
139     struct osi_stat tstat;
140     struct iattr newattrs;
141     struct inode *inode = FILE_INODE(&afile->file);
142     AFS_STATCNT(osi_Truncate);
143
144     /* This routine only shrinks files, and most systems
145      * have very slow truncates, even when the file is already
146      * small enough.  Check now and save some time.
147      */
148     code = afs_osi_Stat(afile, &tstat);
149     if (code || tstat.size <= asize) return code;
150     MObtainWriteLock(&afs_xosi,321);    
151     AFS_GUNLOCK();
152     down(&inode->i_sem);
153 #if defined(AFS_LINUX24_ENV)
154     newattrs.ia_size = asize;
155     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
156     code = osi_notify_change(&afile->dentry, &newattrs);
157 #else
158     inode->i_size = newattrs.ia_size = asize;
159     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
160     if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
161         code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
162     }
163     if (!code) {
164         truncate_inode_pages(inode, asize);
165         if (inode->i_op && inode->i_op->truncate)
166             inode->i_op->truncate(inode);
167     }
168 #endif
169     code = -code;
170     up(&inode->i_sem);
171     AFS_GLOCK();
172     MReleaseWriteLock(&afs_xosi);
173     return code;
174 }
175
176
177 /* Generic read interface */
178 afs_osi_Read(afile, offset, aptr, asize)
179     register struct osi_file *afile;
180     int offset;
181     char *aptr;
182     afs_int32 asize; {
183     struct AFS_UCRED *oldCred;
184     size_t resid;
185     register afs_int32 code;
186     register afs_int32 cnt1=0;
187     AFS_STATCNT(osi_Read);
188
189     /**
190       * If the osi_file passed in is NULL, panic only if AFS is not shutting
191       * down. No point in crashing when we are already shutting down
192       */
193     if ( !afile ) {
194         if ( !afs_shuttingdown )
195             osi_Panic("osi_Read called with null param");
196         else
197             return EIO;
198     }
199
200     if (offset != -1) afile->offset = offset;
201     AFS_GUNLOCK();
202     code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
203     AFS_GLOCK();
204     if (code == 0) {
205         code = asize - resid;
206         afile->offset += code;
207     }
208     else {
209         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
210                  ICL_TYPE_INT32, code);
211         code = -1;
212     }
213     return code;
214 }
215
216 /* Generic write interface */
217 afs_osi_Write(afile, offset, aptr, asize)
218     register struct osi_file *afile;
219     char *aptr;
220     afs_int32 offset;
221     afs_int32 asize; {
222     struct AFS_UCRED *oldCred;
223     size_t resid;
224     register afs_int32 code;
225     AFS_STATCNT(osi_Write);
226     if ( !afile )
227         osi_Panic("afs_osi_Write called with null param");
228     if (offset != -1) afile->offset = offset;
229     AFS_GUNLOCK();
230     code = osi_rdwr(UIO_WRITE, afile, (caddr_t)aptr, asize, &resid);
231     AFS_GLOCK();
232     if (code == 0) {
233         code = asize - resid;
234         afile->offset += code;
235     }
236     else {
237         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
238         code = -1;
239     }
240     if (afile->proc) {
241         (*afile->proc)(afile, code);
242     }
243     return code;
244 }
245
246
247 /*  This work should be handled by physstrat in ca/machdep.c.
248     This routine written from the RT NFS port strategy routine.
249     It has been generalized a bit, but should still be pretty clear. */
250 int afs_osi_MapStrategy(aproc, bp)
251     int (*aproc)();
252     register struct buf *bp;
253 {
254     afs_int32 returnCode;
255
256     AFS_STATCNT(osi_MapStrategy);
257     returnCode = (*aproc) (bp);
258
259     return returnCode;
260 }
261
262 void
263 shutdown_osifile()
264 {
265   extern int afs_cold_shutdown;
266
267   AFS_STATCNT(shutdown_osifile);
268   if (afs_cold_shutdown) {
269     afs_osicred_initialized = 0;
270   }
271 }
272