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