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