afsconfig-and-rcsid-all-around-20010705
[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 <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/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(ainode)
28     afs_int32 ainode;
29 {
30     struct inode *ip;
31     register struct osi_file *afile = NULL;
32     extern int cacheDiskType;
33     afs_int32 code = 0;
34     int dummy;
35     struct inode *tip = NULL;
36     struct file *filp = NULL;
37     AFS_STATCNT(osi_UFSOpen);
38     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
39         osi_Panic("UFSOpen called for non-UFS cache\n");
40     }
41     if (!afs_osicred_initialized) {
42         /* valid for alpha_osf, SunOS, Ultrix */
43         bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
44         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
45         afs_osicred_initialized = 1;
46     }
47     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
48     AFS_GUNLOCK();
49     if (!afile) {
50         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
51                    sizeof(struct osi_file));
52     }
53     memset(afile, 0, sizeof(struct osi_file));
54     filp = &afile->file;
55     filp->f_dentry = &afile->dentry;
56     tip = iget(afs_cacheSBp, (u_long)ainode);
57     if (!tip)
58         osi_Panic("Can't get inode %d\n", ainode);
59     FILE_INODE(filp) = tip;
60     tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
61     filp->f_flags = O_RDWR;
62 #if defined(AFS_LINUX24_ENV)
63     filp->f_op = fops_get(tip->i_fop);
64 #else
65     filp->f_op = tip->i_op->default_file_ops;
66 #endif
67     if (filp->f_op && filp->f_op->open)
68         code = filp->f_op->open(tip, filp);
69     if (code)
70         osi_Panic("Can't open inode %d\n", ainode);
71     afile->size = tip->i_size;
72     AFS_GLOCK();
73     afile->offset = 0;
74     afile->proc = (int (*)()) 0;
75     afile->inum = ainode;        /* for hint validity checking */
76     return (void *)afile;
77 }
78
79 afs_osi_Stat(afile, astat)
80     register struct osi_file *afile;
81     register struct osi_stat *astat; {
82     register afs_int32 code;
83     AFS_STATCNT(osi_Stat);
84     MObtainWriteLock(&afs_xosi,320);
85     astat->size = FILE_INODE(&afile->file)->i_size;
86     astat->blksize = FILE_INODE(&afile->file)->i_blksize;
87     astat->mtime = FILE_INODE(&afile->file)->i_mtime;
88     astat->atime = FILE_INODE(&afile->file)->i_atime;
89     code = 0;
90     MReleaseWriteLock(&afs_xosi);
91     return code;
92 }
93
94 osi_UFSClose(afile)
95      register struct osi_file *afile;
96   {
97       AFS_STATCNT(osi_Close);
98       if (afile) {
99           if (FILE_INODE(&afile->file)) {
100               struct file *filp = &afile->file;
101               if (filp->f_op && filp->f_op->release)
102                   filp->f_op->release(FILE_INODE(filp), filp);
103               iput(FILE_INODE(filp));
104           }
105       }
106       
107       osi_FreeSmallSpace(afile);
108       return 0;
109   }
110
111 osi_UFSTruncate(afile, asize)
112     register struct osi_file *afile;
113     afs_int32 asize; {
114     struct AFS_UCRED *oldCred;
115     register afs_int32 code;
116     struct osi_stat tstat;
117     struct iattr newattrs;
118     struct inode *inode = FILE_INODE(&afile->file);
119     AFS_STATCNT(osi_Truncate);
120
121     /* This routine only shrinks files, and most systems
122      * have very slow truncates, even when the file is already
123      * small enough.  Check now and save some time.
124      */
125     code = afs_osi_Stat(afile, &tstat);
126     if (code || tstat.size <= asize) return code;
127     MObtainWriteLock(&afs_xosi,321);    
128     AFS_GUNLOCK();
129     down(&inode->i_sem);
130     inode->i_size = newattrs.ia_size = asize;
131     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
132 #if defined(AFS_LINUX24_ENV)
133     newattrs.ia_ctime = CURRENT_TIME;
134
135     /* avoid notify_change() since it wants to update dentry->d_parent */
136     lock_kernel();
137     code = inode_change_ok(inode, &newattrs);
138     if (!code)
139 #ifdef INODE_SETATTR_NOT_VOID
140         code = inode_setattr(inode, &newattrs);
141 #else
142         inode_setattr(inode, &newattrs);
143 #endif
144     unlock_kernel();
145     if (!code)
146         truncate_inode_pages(&inode->i_data, asize);
147 #else
148     if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
149         code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
150     }
151     if (!code) {
152         truncate_inode_pages(inode, asize);
153         if (inode->i_op && inode->i_op->truncate)
154             inode->i_op->truncate(inode);
155     }
156 #endif
157     code = -code;
158     up(&inode->i_sem);
159     AFS_GLOCK();
160     MReleaseWriteLock(&afs_xosi);
161     return code;
162 }
163
164
165 /* Generic read interface */
166 afs_osi_Read(afile, offset, aptr, asize)
167     register struct osi_file *afile;
168     int offset;
169     char *aptr;
170     afs_int32 asize; {
171     struct AFS_UCRED *oldCred;
172     size_t resid;
173     register afs_int32 code;
174     register afs_int32 cnt1=0;
175     AFS_STATCNT(osi_Read);
176
177     /**
178       * If the osi_file passed in is NULL, panic only if AFS is not shutting
179       * down. No point in crashing when we are already shutting down
180       */
181     if ( !afile ) {
182         if ( !afs_shuttingdown )
183             osi_Panic("osi_Read called with null param");
184         else
185             return EIO;
186     }
187
188     if (offset != -1) afile->offset = offset;
189     AFS_GUNLOCK();
190     code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
191     AFS_GLOCK();
192     if (code == 0) {
193         code = asize - resid;
194         afile->offset += code;
195     }
196     else {
197         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
198                  ICL_TYPE_INT32, code);
199         code = -1;
200     }
201     return code;
202 }
203
204 /* Generic write interface */
205 afs_osi_Write(afile, offset, aptr, asize)
206     register struct osi_file *afile;
207     char *aptr;
208     afs_int32 offset;
209     afs_int32 asize; {
210     struct AFS_UCRED *oldCred;
211     size_t resid;
212     register afs_int32 code;
213     AFS_STATCNT(osi_Write);
214     if ( !afile )
215         osi_Panic("afs_osi_Write called with null param");
216     if (offset != -1) afile->offset = offset;
217     AFS_GUNLOCK();
218     code = osi_rdwr(UIO_WRITE, afile, (caddr_t)aptr, asize, &resid);
219     AFS_GLOCK();
220     if (code == 0) {
221         code = asize - resid;
222         afile->offset += code;
223     }
224     else {
225         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
226         code = -1;
227     }
228     if (afile->proc) {
229         (*afile->proc)(afile, code);
230     }
231     return code;
232 }
233
234
235 /*  This work should be handled by physstrat in ca/machdep.c.
236     This routine written from the RT NFS port strategy routine.
237     It has been generalized a bit, but should still be pretty clear. */
238 int afs_osi_MapStrategy(aproc, bp)
239     int (*aproc)();
240     register struct buf *bp;
241 {
242     afs_int32 returnCode;
243
244     AFS_STATCNT(osi_MapStrategy);
245     returnCode = (*aproc) (bp);
246
247     return returnCode;
248 }
249
250 void
251 shutdown_osifile()
252 {
253   extern int afs_cold_shutdown;
254
255   AFS_STATCNT(shutdown_osifile);
256   if (afs_cold_shutdown) {
257     afs_osicred_initialized = 0;
258   }
259 }
260