conditionals-for-linux-ac-kernels-20010420
[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 #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 afs_osi_Read(afile, offset, aptr, asize)
163     register struct osi_file *afile;
164     int offset;
165     char *aptr;
166     afs_int32 asize; {
167     struct AFS_UCRED *oldCred;
168     size_t resid;
169     register afs_int32 code;
170     register afs_int32 cnt1=0;
171     AFS_STATCNT(osi_Read);
172
173     /**
174       * If the osi_file passed in is NULL, panic only if AFS is not shutting
175       * down. No point in crashing when we are already shutting down
176       */
177     if ( !afile ) {
178         if ( !afs_shuttingdown )
179             osi_Panic("osi_Read called with null param");
180         else
181             return EIO;
182     }
183
184     if (offset != -1) afile->offset = offset;
185     AFS_GUNLOCK();
186     code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
187     AFS_GLOCK();
188     if (code == 0) {
189         code = asize - resid;
190         afile->offset += code;
191     }
192     else {
193         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
194                  ICL_TYPE_INT32, code);
195         code = -1;
196     }
197     return code;
198 }
199
200 /* Generic write interface */
201 afs_osi_Write(afile, offset, aptr, asize)
202     register struct osi_file *afile;
203     char *aptr;
204     afs_int32 offset;
205     afs_int32 asize; {
206     struct AFS_UCRED *oldCred;
207     size_t resid;
208     register afs_int32 code;
209     AFS_STATCNT(osi_Write);
210     if ( !afile )
211         osi_Panic("afs_osi_Write called with null param");
212     if (offset != -1) afile->offset = offset;
213     AFS_GUNLOCK();
214     code = osi_rdwr(UIO_WRITE, afile, (caddr_t)aptr, asize, &resid);
215     AFS_GLOCK();
216     if (code == 0) {
217         code = asize - resid;
218         afile->offset += code;
219     }
220     else {
221         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
222         code = -1;
223     }
224     if (afile->proc) {
225         (*afile->proc)(afile, code);
226     }
227     return code;
228 }
229
230
231 /*  This work should be handled by physstrat in ca/machdep.c.
232     This routine written from the RT NFS port strategy routine.
233     It has been generalized a bit, but should still be pretty clear. */
234 int afs_osi_MapStrategy(aproc, bp)
235     int (*aproc)();
236     register struct buf *bp;
237 {
238     afs_int32 returnCode;
239
240     AFS_STATCNT(osi_MapStrategy);
241     returnCode = (*aproc) (bp);
242
243     return returnCode;
244 }
245
246 void
247 shutdown_osifile()
248 {
249   extern int afs_cold_shutdown;
250
251   AFS_STATCNT(shutdown_osifile);
252   if (afs_cold_shutdown) {
253     afs_osicred_initialized = 0;
254   }
255 }
256