Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/file.c |
| 3 | * |
| 4 | * Copyright (C) 1992 Rick Sladkey |
| 5 | */ |
| 6 | #include <linux/nfs_fs.h> |
| 7 | #include "internal.h" |
David Howells | a4ff146 | 2012-12-05 16:31:49 +0000 | [diff] [blame] | 8 | #include "fscache.h" |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 9 | #include "pnfs.h" |
| 10 | |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 11 | #ifdef CONFIG_NFS_V4_2 |
| 12 | #include "nfs42.h" |
| 13 | #endif |
| 14 | |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 15 | #define NFSDBG_FACILITY NFSDBG_FILE |
| 16 | |
| 17 | static int |
| 18 | nfs4_file_open(struct inode *inode, struct file *filp) |
| 19 | { |
| 20 | struct nfs_open_context *ctx; |
| 21 | struct dentry *dentry = filp->f_path.dentry; |
| 22 | struct dentry *parent = NULL; |
| 23 | struct inode *dir; |
| 24 | unsigned openflags = filp->f_flags; |
| 25 | struct iattr attr; |
Trond Myklebust | 5bc2afc | 2013-09-23 18:01:28 -0400 | [diff] [blame] | 26 | int opened = 0; |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 27 | int err; |
| 28 | |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 29 | /* |
| 30 | * If no cached dentry exists or if it's negative, NFSv4 handled the |
| 31 | * opens in ->lookup() or ->create(). |
| 32 | * |
| 33 | * We only get this far for a cached positive dentry. We skipped |
| 34 | * revalidation, so handle it here by dropping the dentry and returning |
| 35 | * -EOPENSTALE. The VFS will retry the lookup/create/open. |
| 36 | */ |
| 37 | |
Al Viro | 6de1472 | 2013-09-16 10:53:17 -0400 | [diff] [blame] | 38 | dprintk("NFS: open file(%pd2)\n", dentry); |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 39 | |
| 40 | if ((openflags & O_ACCMODE) == 3) |
| 41 | openflags--; |
| 42 | |
| 43 | /* We can't create new files here */ |
| 44 | openflags &= ~(O_CREAT|O_EXCL); |
| 45 | |
| 46 | parent = dget_parent(dentry); |
| 47 | dir = parent->d_inode; |
| 48 | |
| 49 | ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode); |
| 50 | err = PTR_ERR(ctx); |
| 51 | if (IS_ERR(ctx)) |
| 52 | goto out; |
| 53 | |
| 54 | attr.ia_valid = ATTR_OPEN; |
| 55 | if (openflags & O_TRUNC) { |
| 56 | attr.ia_valid |= ATTR_SIZE; |
| 57 | attr.ia_size = 0; |
| 58 | nfs_wb_all(inode); |
| 59 | } |
| 60 | |
Trond Myklebust | 5bc2afc | 2013-09-23 18:01:28 -0400 | [diff] [blame] | 61 | inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened); |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 62 | if (IS_ERR(inode)) { |
| 63 | err = PTR_ERR(inode); |
| 64 | switch (err) { |
| 65 | case -EPERM: |
| 66 | case -EACCES: |
| 67 | case -EDQUOT: |
| 68 | case -ENOSPC: |
| 69 | case -EROFS: |
| 70 | goto out_put_ctx; |
| 71 | default: |
| 72 | goto out_drop; |
| 73 | } |
| 74 | } |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 75 | if (inode != dentry->d_inode) |
| 76 | goto out_drop; |
| 77 | |
| 78 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 79 | nfs_file_set_open_context(filp, ctx); |
David Howells | f1fe29b | 2013-09-27 11:20:03 +0100 | [diff] [blame] | 80 | nfs_fscache_open_file(inode, filp); |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 81 | err = 0; |
| 82 | |
| 83 | out_put_ctx: |
| 84 | put_nfs_open_context(ctx); |
| 85 | out: |
| 86 | dput(parent); |
| 87 | return err; |
| 88 | |
| 89 | out_drop: |
| 90 | d_drop(dentry); |
| 91 | err = -EOPENSTALE; |
| 92 | goto out_put_ctx; |
| 93 | } |
| 94 | |
| 95 | static int |
| 96 | nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 97 | { |
| 98 | int ret; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 99 | struct inode *inode = file_inode(file); |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 100 | |
Trond Myklebust | 05990d1 | 2012-09-11 16:01:22 -0400 | [diff] [blame] | 101 | do { |
| 102 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 103 | if (ret != 0) |
| 104 | break; |
| 105 | mutex_lock(&inode->i_mutex); |
| 106 | ret = nfs_file_fsync_commit(file, start, end, datasync); |
Christoph Hellwig | 1b33809 | 2014-04-21 10:29:17 -0700 | [diff] [blame] | 107 | if (!ret) |
Trond Myklebust | 05990d1 | 2012-09-11 16:01:22 -0400 | [diff] [blame] | 108 | ret = pnfs_layoutcommit_inode(inode, true); |
| 109 | mutex_unlock(&inode->i_mutex); |
Trond Myklebust | dcfc4f2 | 2012-09-11 16:19:38 -0400 | [diff] [blame] | 110 | /* |
| 111 | * If nfs_file_fsync_commit detected a server reboot, then |
| 112 | * resend all dirty pages that might have been covered by |
| 113 | * the NFS_CONTEXT_RESEND_WRITES flag |
| 114 | */ |
| 115 | start = 0; |
| 116 | end = LLONG_MAX; |
Trond Myklebust | 05990d1 | 2012-09-11 16:01:22 -0400 | [diff] [blame] | 117 | } while (ret == -EAGAIN); |
| 118 | |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 119 | return ret; |
| 120 | } |
| 121 | |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 122 | #ifdef CONFIG_NFS_V4_2 |
| 123 | static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence) |
| 124 | { |
| 125 | loff_t ret; |
| 126 | |
| 127 | switch (whence) { |
| 128 | case SEEK_HOLE: |
| 129 | case SEEK_DATA: |
| 130 | ret = nfs42_proc_llseek(filep, offset, whence); |
| 131 | if (ret != -ENOTSUPP) |
| 132 | return ret; |
| 133 | default: |
| 134 | return nfs_file_llseek(filep, offset, whence); |
| 135 | } |
| 136 | } |
| 137 | #endif /* CONFIG_NFS_V4_2 */ |
| 138 | |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 139 | const struct file_operations nfs4_file_operations = { |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 140 | #ifdef CONFIG_NFS_V4_2 |
| 141 | .llseek = nfs4_file_llseek, |
| 142 | #else |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 143 | .llseek = nfs_file_llseek, |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 144 | #endif |
Al Viro | 3aa2d19 | 2014-04-02 20:14:12 -0400 | [diff] [blame] | 145 | .read = new_sync_read, |
Al Viro | edaf436 | 2014-04-03 14:07:25 -0400 | [diff] [blame] | 146 | .write = new_sync_write, |
Al Viro | 3aa2d19 | 2014-04-02 20:14:12 -0400 | [diff] [blame] | 147 | .read_iter = nfs_file_read, |
Al Viro | edaf436 | 2014-04-03 14:07:25 -0400 | [diff] [blame] | 148 | .write_iter = nfs_file_write, |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 149 | .mmap = nfs_file_mmap, |
| 150 | .open = nfs4_file_open, |
| 151 | .flush = nfs_file_flush, |
| 152 | .release = nfs_file_release, |
| 153 | .fsync = nfs4_file_fsync, |
| 154 | .lock = nfs_lock, |
| 155 | .flock = nfs_flock, |
| 156 | .splice_read = nfs_file_splice_read, |
Al Viro | 4da54c2 | 2014-04-05 04:37:17 -0400 | [diff] [blame] | 157 | .splice_write = iter_file_splice_write, |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 158 | .check_flags = nfs_check_flags, |
Jeff Layton | 1c994a0 | 2014-08-27 06:49:41 -0400 | [diff] [blame] | 159 | .setlease = simple_nosetlease, |
Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 160 | }; |