linux26-dont-fix-inode-size-ourselves-20041208
[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 "h/module.h" /* early to avoid printf->printk mapping */
17 #include "afs/sysincludes.h"    /* Standard vendor system headers */
18 #include "afsincludes.h"        /* Afs-based standard headers */
19 #include "afs/afs_stats.h"      /* afs statistics */
20 #include "h/smp_lock.h"
21
22
23 int afs_osicred_initialized = 0;
24 struct AFS_UCRED afs_osi_cred;
25 afs_lock_t afs_xosi;            /* lock is for tvattr */
26 extern struct osi_dev cacheDev;
27 extern struct super_block *afs_cacheSBp;
28
29 void *
30 osi_UFSOpen(afs_int32 ainode)
31 {
32     register struct osi_file *afile = NULL;
33     extern int cacheDiskType;
34     afs_int32 code = 0;
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         memset((char *)&afs_osi_cred, 0, 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_AllocLargeSpace(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_LINUX26_ENV)
63     filp->f_mapping = tip->i_mapping;
64 #endif
65 #if defined(AFS_LINUX24_ENV)
66     filp->f_op = fops_get(tip->i_fop);
67 #else
68     filp->f_op = tip->i_op->default_file_ops;
69 #endif
70     if (filp->f_op && filp->f_op->open)
71         code = filp->f_op->open(tip, filp);
72     if (code)
73         osi_Panic("Can't open inode %d\n", ainode);
74     afile->size = tip->i_size;
75     AFS_GLOCK();
76     afile->offset = 0;
77     afile->proc = (int (*)())0;
78     afile->inum = ainode;       /* for hint validity checking */
79     return (void *)afile;
80 }
81
82 int
83 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
84 {
85     register afs_int32 code;
86     AFS_STATCNT(osi_Stat);
87     MObtainWriteLock(&afs_xosi, 320);
88     astat->size = FILE_INODE(&afile->file)->i_size;
89     astat->blksize = FILE_INODE(&afile->file)->i_blksize;
90 #if defined(AFS_LINUX26_ENV)
91     astat->mtime = FILE_INODE(&afile->file)->i_mtime.tv_sec;
92     astat->atime = FILE_INODE(&afile->file)->i_atime.tv_sec;
93 #else
94     astat->mtime = FILE_INODE(&afile->file)->i_mtime;
95     astat->atime = FILE_INODE(&afile->file)->i_atime;
96 #endif
97     code = 0;
98     MReleaseWriteLock(&afs_xosi);
99     return code;
100 }
101
102 int
103 osi_UFSClose(register struct osi_file *afile)
104 {
105     AFS_STATCNT(osi_Close);
106     if (afile) {
107         if (FILE_INODE(&afile->file)) {
108             struct file *filp = &afile->file;
109             if (filp->f_op && filp->f_op->release)
110                 filp->f_op->release(FILE_INODE(filp), filp);
111             iput(FILE_INODE(filp));
112         }
113     }
114
115     osi_FreeLargeSpace(afile);
116     return 0;
117 }
118
119 int
120 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
121 {
122     register afs_int32 code;
123     struct osi_stat tstat;
124     struct iattr newattrs;
125     struct inode *inode = FILE_INODE(&afile->file);
126     AFS_STATCNT(osi_Truncate);
127
128     /* This routine only shrinks files, and most systems
129      * have very slow truncates, even when the file is already
130      * small enough.  Check now and save some time.
131      */
132     code = afs_osi_Stat(afile, &tstat);
133     if (code || tstat.size <= asize)
134         return code;
135     MObtainWriteLock(&afs_xosi, 321);
136     AFS_GUNLOCK();
137 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
138     down_write(&inode->i_alloc_sem);
139 #endif
140     down(&inode->i_sem);
141     newattrs.ia_size = asize;
142     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
143 #if defined(AFS_LINUX24_ENV)
144     newattrs.ia_ctime = CURRENT_TIME;
145
146     /* avoid notify_change() since it wants to update dentry->d_parent */
147     lock_kernel();
148     code = inode_change_ok(inode, &newattrs);
149     if (!code)
150 #ifdef INODE_SETATTR_NOT_VOID
151         code = inode_setattr(inode, &newattrs);
152 #else
153         inode_setattr(inode, &newattrs);
154 #endif
155     unlock_kernel();
156     if (!code)
157         truncate_inode_pages(&inode->i_data, asize);
158 #else
159     inode->i_size = asize;
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 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
172     up_write(&inode->i_alloc_sem);
173 #endif
174     AFS_GLOCK();
175     MReleaseWriteLock(&afs_xosi);
176     return code;
177 }
178
179
180 /* Generic read interface */
181 int
182 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
183              afs_int32 asize)
184 {
185     size_t resid;
186     register afs_int32 code;
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)
201         afile->offset = offset;
202     AFS_GUNLOCK();
203     code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
204     AFS_GLOCK();
205     if (code == 0) {
206         code = asize - resid;
207         afile->offset += code;
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 int
218 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
219               afs_int32 asize)
220 {
221     size_t resid;
222     register afs_int32 code;
223     AFS_STATCNT(osi_Write);
224     if (!afile) {
225         if (!afs_shuttingdown)
226             osi_Panic("afs_osi_Write called with null param");
227         else
228             return EIO;
229     }
230     if (offset != -1)
231         afile->offset = offset;
232     AFS_GUNLOCK();
233     code = osi_rdwr(UIO_WRITE, afile, (caddr_t) aptr, asize, &resid);
234     AFS_GLOCK();
235     if (code == 0) {
236         code = asize - resid;
237         afile->offset += code;
238     } else {
239         if (code == ENOSPC)
240             afs_warnuser
241                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
242         code = -1;
243     }
244     if (afile->proc) {
245         (*afile->proc) (afile, code);
246     }
247     return code;
248 }
249
250
251 /*  This work should be handled by physstrat in ca/machdep.c.
252     This routine written from the RT NFS port strategy routine.
253     It has been generalized a bit, but should still be pretty clear. */
254 int
255 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
256 {
257     afs_int32 returnCode;
258
259     AFS_STATCNT(osi_MapStrategy);
260     returnCode = (*aproc) (bp);
261
262     return returnCode;
263 }
264
265 void
266 shutdown_osifile(void)
267 {
268     extern int afs_cold_shutdown;
269
270     AFS_STATCNT(shutdown_osifile);
271     if (afs_cold_shutdown) {
272         afs_osicred_initialized = 0;
273     }
274 }