afs: Negate codes using a clear, standard method
[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
14 #include <linux/module.h> /* early to avoid printf->printk mapping */
15 #include "afs/sysincludes.h"    /* Standard vendor system headers */
16 #include "afsincludes.h"        /* Afs-based standard headers */
17 #include "afs/afs_stats.h"      /* afs statistics */
18 #include <linux/namei.h>
19
20 #if defined(HAVE_LINUX_EXPORTFS_H)
21 #include <linux/exportfs.h>
22 #endif
23 #include "osi_compat.h"
24
25 int cache_fh_type = -1;
26 int cache_fh_len = -1;
27
28 afs_lock_t afs_xosi;            /* lock is for tvattr */
29 extern struct osi_dev cacheDev;
30 extern struct vfsmount *afs_cacheMnt;
31 extern struct super_block *afs_cacheSBp;
32 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
33 extern struct cred *cache_creds;
34 #endif
35
36 /* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
37 int
38 afs_fh_acceptable(void *context, struct dentry *dp)
39 {
40     return 1;
41 }
42
43 struct file *
44 afs_linux_raw_open(afs_dcache_id_t *ainode)
45 {
46     struct inode *tip = NULL;
47     struct dentry *dp = NULL;
48     struct file* filp;
49
50     dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
51                 afs_fh_acceptable);
52     if ((!dp) || IS_ERR(dp))
53            osi_Panic("Can't get dentry\n");
54     tip = dp->d_inode;
55     tip->i_flags |= S_NOATIME;  /* Disable updating access times. */
56
57 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
58     /* Use stashed credentials - prevent selinux/apparmor problems  */
59     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, cache_creds);
60     if (IS_ERR(filp))
61         filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
62 #else
63     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
64 #endif
65     if (IS_ERR(filp))
66         osi_Panic("Can't open file: %d\n", (int) PTR_ERR(filp));
67     return filp;
68 }
69
70 void *
71 osi_UFSOpen(afs_dcache_id_t *ainode)
72 {
73     struct osi_file *afile = NULL;
74     extern int cacheDiskType;
75
76     AFS_STATCNT(osi_UFSOpen);
77     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
78         osi_Panic("UFSOpen called for non-UFS cache\n");
79     }
80     if (!afs_osicred_initialized) {
81         /* valid for alpha_osf, SunOS, Ultrix */
82         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
83         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
84         afs_osicred_initialized = 1;
85     }
86     AFS_GUNLOCK();
87     afile = kmalloc(sizeof(struct osi_file), GFP_NOFS);
88     if (!afile) {
89         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
90                   (int)sizeof(struct osi_file));
91     }
92     memset(afile, 0, sizeof(struct osi_file));
93
94     afile->filp = afs_linux_raw_open(ainode);
95     afile->size = i_size_read(FILE_INODE(afile->filp));
96     AFS_GLOCK();
97     afile->offset = 0;
98     afile->proc = (int (*)())0;
99     return (void *)afile;
100 }
101
102 /*
103  * Given a dentry, return the file handle as encoded by the filesystem.
104  * We can't assume anything about the length (words, not bytes).
105  * The cache has to live on a single filesystem with uniform file 
106  * handles, otherwise we panic.
107  */
108 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
109     int max_len;
110     int type;
111
112     if (cache_fh_len > 0)
113         max_len = cache_fh_len;
114     else
115         max_len = MAX_FH_LEN;
116     type = afs_get_fh_from_dentry(dp, ainode, &max_len);
117     if (type == 255) {
118         osi_Panic("File handle encoding failed\n");
119     }
120     if (cache_fh_type < 0)
121         cache_fh_type = type;
122     if (cache_fh_len < 0) {
123         cache_fh_len = max_len;
124     }
125     if (type != cache_fh_type || max_len != cache_fh_len) {
126         osi_Panic("Inconsistent file handles within cache\n");
127     }
128 }
129
130 int
131 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
132 {
133     AFS_STATCNT(osi_Stat);
134     ObtainWriteLock(&afs_xosi, 320);
135     astat->size = i_size_read(OSIFILE_INODE(afile));
136     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
137     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
138
139     ReleaseWriteLock(&afs_xosi);
140     return 0;
141 }
142
143 int
144 osi_UFSClose(struct osi_file *afile)
145 {
146     AFS_STATCNT(osi_Close);
147     if (afile) {
148         if (OSIFILE_INODE(afile)) {
149             filp_close(afile->filp, NULL);
150         }
151     }
152     kfree(afile);
153     return 0;
154 }
155
156 int
157 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
158 {
159     afs_int32 code;
160     struct osi_stat tstat;
161     struct iattr newattrs;
162     struct inode *inode = OSIFILE_INODE(afile);
163     AFS_STATCNT(osi_Truncate);
164
165     /* This routine only shrinks files, and most systems
166      * have very slow truncates, even when the file is already
167      * small enough.  Check now and save some time.
168      */
169     code = afs_osi_Stat(afile, &tstat);
170     if (code || tstat.size <= asize)
171         return code;
172     ObtainWriteLock(&afs_xosi, 321);
173     AFS_GUNLOCK();
174     afs_linux_lock_inode(inode);
175 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
176     down_write(&inode->i_alloc_sem);
177 #endif
178     newattrs.ia_size = asize;
179     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
180     newattrs.ia_ctime = CURRENT_TIME;
181
182     /* avoid notify_change() since it wants to update dentry->d_parent */
183     code = inode_change_ok(inode, &newattrs);
184     if (!code)
185         code = afs_inode_setattr(afile, &newattrs);
186     if (!code)
187         truncate_inode_pages(&inode->i_data, asize);
188     code = -code;
189 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
190     up_write(&inode->i_alloc_sem);
191 #endif
192     afs_linux_unlock_inode(inode);
193     AFS_GLOCK();
194     ReleaseWriteLock(&afs_xosi);
195     return code;
196 }
197
198
199 /* Generic read interface */
200 int
201 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
202              afs_int32 asize)
203 {
204     struct uio auio;
205     struct iovec iov;
206     afs_int32 code;
207
208     AFS_STATCNT(osi_Read);
209
210     /*
211      * If the osi_file passed in is NULL, panic only if AFS is not shutting
212      * down. No point in crashing when we are already shutting down
213      */
214     if (!afile) {
215         if (!afs_shuttingdown)
216             osi_Panic("osi_Read called with null param");
217         else
218             return -EIO;
219     }
220
221     if (offset != -1)
222         afile->offset = offset;
223     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
224     AFS_GUNLOCK();
225     code = osi_rdwr(afile, &auio, UIO_READ);
226     AFS_GLOCK();
227     if (code == 0) {
228         code = asize - auio.uio_resid;
229         afile->offset += code;
230     } else {
231         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
232                    ICL_TYPE_INT32, code);
233         if (code > 0) {
234             code = -code;
235         }
236     }
237     return code;
238 }
239
240 /* Generic write interface */
241 int
242 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
243               afs_int32 asize)
244 {
245     struct uio auio;
246     struct iovec iov;
247     afs_int32 code;
248
249     AFS_STATCNT(osi_Write);
250
251     if (!afile) {
252         if (!afs_shuttingdown)
253             osi_Panic("afs_osi_Write called with null param");
254         else
255             return -EIO;
256     }
257
258     if (offset != -1)
259         afile->offset = offset;
260     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
261     AFS_GUNLOCK();
262     code = osi_rdwr(afile, &auio, UIO_WRITE);
263     AFS_GLOCK();
264     if (code == 0) {
265         code = asize - auio.uio_resid;
266         afile->offset += code;
267     } else {
268         if (code == ENOSPC)
269             afs_warnuser
270                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
271         if (code > 0) {
272             code = -code;
273         }
274     }
275
276     if (afile->proc)
277         (*afile->proc)(afile, code);
278
279     return code;
280 }
281
282
283 /*  This work should be handled by physstrat in ca/machdep.c.
284     This routine written from the RT NFS port strategy routine.
285     It has been generalized a bit, but should still be pretty clear. */
286 int
287 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
288 {
289     afs_int32 returnCode;
290
291     AFS_STATCNT(osi_MapStrategy);
292     returnCode = (*aproc) (bp);
293
294     return returnCode;
295 }
296
297 void
298 shutdown_osifile(void)
299 {
300     AFS_STATCNT(shutdown_osifile);
301     if (afs_cold_shutdown) {
302         afs_osicred_initialized = 0;
303     }
304 }
305
306 /* Intialize cache device info and fragment size for disk cache partition. */
307 int
308 osi_InitCacheInfo(char *aname)
309 {
310     int code;
311     extern afs_dcache_id_t cacheInode;
312     struct dentry *dp;
313     extern struct osi_dev cacheDev;
314     extern afs_int32 afs_fsfragsize;
315     extern struct super_block *afs_cacheSBp;
316     extern struct vfsmount *afs_cacheMnt;
317     code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
318     if (code)
319         return ENOENT;
320
321     osi_get_fh(dp, &cacheInode.ufs);
322     cacheDev.dev = dp->d_inode->i_sb->s_dev;
323     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
324     afs_cacheSBp = dp->d_inode->i_sb;
325
326     dput(dp);
327
328     afs_init_sb_export_ops(afs_cacheSBp);
329
330     return 0;
331 }
332
333
334 /* osi_rdwr
335  * seek, then read or write to an open inode. addrp points to data in
336  * kernel space.
337  */
338 int
339 osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
340 {
341     struct file *filp = osifile->filp;
342     mm_segment_t old_fs = {0};
343     int code = 0;
344     struct iovec *iov;
345     size_t count;
346     unsigned long savelim;
347     loff_t pos;
348
349     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
350     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
351
352     if (uiop->uio_seg == AFS_UIOSYS) {
353         /* Switch into user space */
354         old_fs = get_fs();
355         set_fs(get_ds());
356     }
357
358     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
359         iov = uiop->uio_iov;
360         count = iov->iov_len;
361         if (count == 0) {
362             uiop->uio_iov++;
363             uiop->uio_iovcnt--;
364             continue;
365         }
366
367         pos = uiop->uio_offset;
368         if (rw == UIO_READ)
369             code = filp->f_op->read(filp, iov->iov_base, count, &pos);
370         else
371             code = filp->f_op->write(filp, iov->iov_base, count, &pos);
372
373         if (code < 0) {
374             code = -code;
375             break;
376         } else if (code == 0) {
377             /*
378              * This is bad -- we can't read any more data from the
379              * file, but we have no good way of signaling a partial
380              * read either.
381              */
382             code = EIO;
383             break;
384         }
385
386         iov->iov_base += code;
387         iov->iov_len -= code;
388         uiop->uio_resid -= code;
389         uiop->uio_offset += code;
390         code = 0;
391     }
392
393     if (uiop->uio_seg == AFS_UIOSYS) {
394         /* Switch back into kernel space */
395         set_fs(old_fs);
396     }
397
398     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
399
400     return code;
401 }
402
403 /* setup_uio 
404  * Setup a uio struct.
405  */
406 void
407 setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
408           int count, uio_flag_t flag, uio_seg_t seg)
409 {
410     iovecp->iov_base = (char *)buf;
411     iovecp->iov_len = count;
412     uiop->uio_iov = iovecp;
413     uiop->uio_iovcnt = 1;
414     uiop->uio_offset = pos;
415     uiop->uio_seg = seg;
416     uiop->uio_resid = count;
417     uiop->uio_flag = flag;
418 }
419
420
421 /* uiomove
422  * UIO_READ : dp -> uio
423  * UIO_WRITE : uio -> dp
424  */
425 int
426 uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
427 {
428     int count;
429     struct iovec *iov;
430     int code;
431
432     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
433         iov = uiop->uio_iov;
434         count = iov->iov_len;
435
436         if (!count) {
437             uiop->uio_iov++;
438             uiop->uio_iovcnt--;
439             continue;
440         }
441
442         if (count > length)
443             count = length;
444
445         switch (uiop->uio_seg) {
446         case AFS_UIOSYS:
447             switch (rw) {
448             case UIO_READ:
449                 memcpy(iov->iov_base, dp, count);
450                 break;
451             case UIO_WRITE:
452                 memcpy(dp, iov->iov_base, count);
453                 break;
454             default:
455                 printf("uiomove: Bad rw = %d\n", rw);
456                 return -EINVAL;
457             }
458             break;
459         case AFS_UIOUSER:
460             switch (rw) {
461             case UIO_READ:
462                 AFS_COPYOUT(dp, iov->iov_base, count, code);
463                 break;
464             case UIO_WRITE:
465                 AFS_COPYIN(iov->iov_base, dp, count, code);
466                 break;
467             default:
468                 printf("uiomove: Bad rw = %d\n", rw);
469                 return -EINVAL;
470             }
471             break;
472         default:
473             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
474             return -EINVAL;
475         }
476
477         dp += count;
478         length -= count;
479         iov->iov_base += count;
480         iov->iov_len -= count;
481         uiop->uio_offset += count;
482         uiop->uio_resid -= count;
483     }
484     return 0;
485 }
486