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