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