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