initial-linux24-support-20001105
[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 int osi_notify_change(struct dentry * dentry, struct iattr * attr)
108 {
109     struct inode *inode = dentry->d_inode;
110     int error;
111     time_t now = CURRENT_TIME;
112     unsigned int ia_valid = attr->ia_valid;
113
114     attr->ia_ctime = now;
115     if (!(ia_valid & ATTR_ATIME_SET))
116         attr->ia_atime = now;
117     if (!(ia_valid & ATTR_MTIME_SET))
118         attr->ia_mtime = now;
119
120     lock_kernel();
121     if (inode && inode->i_op && inode->i_op->setattr)
122         error = inode->i_op->setattr(dentry, attr);
123     else {
124         error = inode_change_ok(inode, attr);
125         if (!error)
126             inode_setattr(inode, attr);
127     }
128     unlock_kernel();
129     return error;
130 }
131
132 osi_UFSTruncate(afile, asize)
133     register struct osi_file *afile;
134     afs_int32 asize; {
135     struct AFS_UCRED *oldCred;
136     register afs_int32 code;
137     struct osi_stat tstat;
138     struct iattr newattrs;
139     struct inode *inode = FILE_INODE(&afile->file);
140     AFS_STATCNT(osi_Truncate);
141
142     /* This routine only shrinks files, and most systems
143      * have very slow truncates, even when the file is already
144      * small enough.  Check now and save some time.
145      */
146     code = afs_osi_Stat(afile, &tstat);
147     if (code || tstat.size <= asize) return code;
148     MObtainWriteLock(&afs_xosi,321);    
149     AFS_GUNLOCK();
150     down(&inode->i_sem);
151 #if defined(AFS_LINUX24_ENV)
152     newattrs.ia_size = asize;
153     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
154     code = osi_notify_change(&afile->dentry, &newattrs);
155 #else
156     inode->i_size = newattrs.ia_size = asize;
157     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
158     if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
159         code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
160     }
161     if (!code) {
162         truncate_inode_pages(inode, asize);
163         if (inode->i_op && inode->i_op->truncate)
164             inode->i_op->truncate(inode);
165     }
166 #endif
167     code = -code;
168     up(&inode->i_sem);
169     AFS_GLOCK();
170     MReleaseWriteLock(&afs_xosi);
171     return code;
172 }
173
174
175 /* Generic read interface */
176 afs_osi_Read(afile, offset, aptr, asize)
177     register struct osi_file *afile;
178     int offset;
179     char *aptr;
180     afs_int32 asize; {
181     struct AFS_UCRED *oldCred;
182     unsigned int resid;
183     register afs_int32 code;
184     register afs_int32 cnt1=0;
185     AFS_STATCNT(osi_Read);
186
187     /**
188       * If the osi_file passed in is NULL, panic only if AFS is not shutting
189       * down. No point in crashing when we are already shutting down
190       */
191     if ( !afile ) {
192         if ( !afs_shuttingdown )
193             osi_Panic("osi_Read called with null param");
194         else
195             return EIO;
196     }
197
198     if (offset != -1) afile->offset = offset;
199     AFS_GUNLOCK();
200     code = osi_rdwr(UIO_READ, afile, (caddr_t) aptr, asize, &resid);
201     AFS_GLOCK();
202     if (code == 0) {
203         code = asize - resid;
204         afile->offset += code;
205     }
206     else {
207         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
208                  ICL_TYPE_INT32, code);
209         code = -1;
210     }
211     return code;
212 }
213
214 /* Generic write interface */
215 afs_osi_Write(afile, offset, aptr, asize)
216     register struct osi_file *afile;
217     char *aptr;
218     afs_int32 offset;
219     afs_int32 asize; {
220     struct AFS_UCRED *oldCred;
221     unsigned int resid;
222     register afs_int32 code;
223     AFS_STATCNT(osi_Write);
224     if ( !afile )
225         osi_Panic("afs_osi_Write called with null param");
226     if (offset != -1) afile->offset = offset;
227     AFS_GUNLOCK();
228     code = osi_rdwr(UIO_WRITE, afile, (caddr_t)aptr, asize, &resid);
229     AFS_GLOCK();
230     if (code == 0) {
231         code = asize - resid;
232         afile->offset += code;
233     }
234     else {
235         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
236         code = -1;
237     }
238     if (afile->proc) {
239         (*afile->proc)(afile, code);
240     }
241     return code;
242 }
243
244
245 /*  This work should be handled by physstrat in ca/machdep.c.
246     This routine written from the RT NFS port strategy routine.
247     It has been generalized a bit, but should still be pretty clear. */
248 int afs_osi_MapStrategy(aproc, bp)
249     int (*aproc)();
250     register struct buf *bp;
251 {
252     afs_int32 returnCode;
253
254     AFS_STATCNT(osi_MapStrategy);
255     returnCode = (*aproc) (bp);
256
257     return returnCode;
258 }
259
260 void
261 shutdown_osifile()
262 {
263   extern int afs_cold_shutdown;
264
265   AFS_STATCNT(shutdown_osifile);
266   if (afs_cold_shutdown) {
267     afs_osicred_initialized = 0;
268   }
269 }
270