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