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