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