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