Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com> |
| 3 | */ |
| 4 | #include <linux/fs.h> |
| 5 | #include <linux/sunrpc/sched.h> |
| 6 | #include <linux/nfs.h> |
| 7 | #include <linux/nfs3.h> |
| 8 | #include <linux/nfs4.h> |
| 9 | #include <linux/nfs_xdr.h> |
| 10 | #include <linux/nfs_fs.h> |
| 11 | #include "nfs4_fs.h" |
| 12 | #include "nfs42.h" |
| 13 | |
| 14 | static int nfs42_set_rw_stateid(nfs4_stateid *dst, struct file *file, |
| 15 | fmode_t fmode) |
| 16 | { |
| 17 | struct nfs_open_context *open; |
| 18 | struct nfs_lock_context *lock; |
| 19 | int ret; |
| 20 | |
| 21 | open = get_nfs_open_context(nfs_file_open_context(file)); |
| 22 | lock = nfs_get_lock_context(open); |
| 23 | if (IS_ERR(lock)) { |
| 24 | put_nfs_open_context(open); |
| 25 | return PTR_ERR(lock); |
| 26 | } |
| 27 | |
| 28 | ret = nfs4_set_rw_stateid(dst, open, lock, fmode); |
| 29 | |
| 30 | nfs_put_lock_context(lock); |
| 31 | put_nfs_open_context(open); |
| 32 | return ret; |
| 33 | } |
| 34 | |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 35 | static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
| 36 | loff_t offset, loff_t len) |
| 37 | { |
| 38 | struct inode *inode = file_inode(filep); |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 39 | struct nfs_server *server = NFS_SERVER(inode); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 40 | struct nfs42_falloc_args args = { |
| 41 | .falloc_fh = NFS_FH(inode), |
| 42 | .falloc_offset = offset, |
| 43 | .falloc_length = len, |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 44 | .falloc_bitmask = server->cache_consistency_bitmask, |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 45 | }; |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 46 | struct nfs42_falloc_res res = { |
| 47 | .falloc_server = server, |
| 48 | }; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 49 | int status; |
| 50 | |
| 51 | msg->rpc_argp = &args; |
| 52 | msg->rpc_resp = &res; |
| 53 | |
| 54 | status = nfs42_set_rw_stateid(&args.falloc_stateid, filep, FMODE_WRITE); |
| 55 | if (status) |
| 56 | return status; |
| 57 | |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 58 | res.falloc_fattr = nfs_alloc_fattr(); |
| 59 | if (!res.falloc_fattr) |
| 60 | return -ENOMEM; |
| 61 | |
| 62 | status = nfs4_call_sync(server->client, server, msg, |
| 63 | &args.seq_args, &res.seq_res, 0); |
| 64 | if (status == 0) |
| 65 | status = nfs_post_op_update_inode(inode, res.falloc_fattr); |
| 66 | |
| 67 | kfree(res.falloc_fattr); |
| 68 | return status; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
| 72 | loff_t offset, loff_t len) |
| 73 | { |
| 74 | struct nfs_server *server = NFS_SERVER(file_inode(filep)); |
| 75 | struct nfs4_exception exception = { }; |
| 76 | int err; |
| 77 | |
| 78 | do { |
| 79 | err = _nfs42_proc_fallocate(msg, filep, offset, len); |
| 80 | if (err == -ENOTSUPP) |
| 81 | return -EOPNOTSUPP; |
| 82 | err = nfs4_handle_exception(server, err, &exception); |
| 83 | } while (exception.retry); |
| 84 | |
| 85 | return err; |
| 86 | } |
| 87 | |
| 88 | int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len) |
| 89 | { |
| 90 | struct rpc_message msg = { |
| 91 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE], |
| 92 | }; |
| 93 | struct inode *inode = file_inode(filep); |
| 94 | int err; |
| 95 | |
| 96 | if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE)) |
| 97 | return -EOPNOTSUPP; |
| 98 | |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 99 | mutex_lock(&inode->i_mutex); |
| 100 | |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 101 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
| 102 | if (err == -EOPNOTSUPP) |
| 103 | NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE; |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 104 | |
| 105 | mutex_unlock(&inode->i_mutex); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 106 | return err; |
| 107 | } |
| 108 | |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 109 | int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len) |
| 110 | { |
| 111 | struct rpc_message msg = { |
| 112 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE], |
| 113 | }; |
| 114 | struct inode *inode = file_inode(filep); |
| 115 | int err; |
| 116 | |
| 117 | if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE)) |
| 118 | return -EOPNOTSUPP; |
| 119 | |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 120 | nfs_wb_all(inode); |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 121 | mutex_lock(&inode->i_mutex); |
| 122 | |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 123 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 124 | if (err == 0) |
| 125 | truncate_pagecache_range(inode, offset, (offset + len) -1); |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 126 | if (err == -EOPNOTSUPP) |
| 127 | NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE; |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 128 | |
| 129 | mutex_unlock(&inode->i_mutex); |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 130 | return err; |
| 131 | } |
| 132 | |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 133 | loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence) |
| 134 | { |
| 135 | struct inode *inode = file_inode(filep); |
| 136 | struct nfs42_seek_args args = { |
| 137 | .sa_fh = NFS_FH(inode), |
| 138 | .sa_offset = offset, |
| 139 | .sa_what = (whence == SEEK_HOLE) ? |
| 140 | NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA, |
| 141 | }; |
| 142 | struct nfs42_seek_res res; |
| 143 | struct rpc_message msg = { |
| 144 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK], |
| 145 | .rpc_argp = &args, |
| 146 | .rpc_resp = &res, |
| 147 | }; |
| 148 | struct nfs_server *server = NFS_SERVER(inode); |
| 149 | int status; |
| 150 | |
Anna Schumaker | 878ffa9 | 2014-10-23 14:00:54 -0400 | [diff] [blame] | 151 | if (!nfs_server_capable(inode, NFS_CAP_SEEK)) |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 152 | return -ENOTSUPP; |
| 153 | |
| 154 | status = nfs42_set_rw_stateid(&args.sa_stateid, filep, FMODE_READ); |
| 155 | if (status) |
| 156 | return status; |
| 157 | |
| 158 | nfs_wb_all(inode); |
| 159 | status = nfs4_call_sync(server->client, server, &msg, |
| 160 | &args.seq_args, &res.seq_res, 0); |
| 161 | if (status == -ENOTSUPP) |
| 162 | server->caps &= ~NFS_CAP_SEEK; |
| 163 | if (status) |
| 164 | return status; |
| 165 | |
| 166 | return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes); |
| 167 | } |