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