xref: /openbmc/linux/fs/nfs/nfs42proc.c (revision 2e72448b)
11c6dcbe5SAnna Schumaker /*
21c6dcbe5SAnna Schumaker  * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
31c6dcbe5SAnna Schumaker  */
41c6dcbe5SAnna Schumaker #include <linux/fs.h>
51c6dcbe5SAnna Schumaker #include <linux/sunrpc/sched.h>
61c6dcbe5SAnna Schumaker #include <linux/nfs.h>
71c6dcbe5SAnna Schumaker #include <linux/nfs3.h>
81c6dcbe5SAnna Schumaker #include <linux/nfs4.h>
91c6dcbe5SAnna Schumaker #include <linux/nfs_xdr.h>
101c6dcbe5SAnna Schumaker #include <linux/nfs_fs.h>
111c6dcbe5SAnna Schumaker #include "nfs4_fs.h"
121c6dcbe5SAnna Schumaker #include "nfs42.h"
131b4a4bd8SPeng Tao #include "iostat.h"
141b4a4bd8SPeng Tao #include "pnfs.h"
151b4a4bd8SPeng Tao #include "internal.h"
161b4a4bd8SPeng Tao 
17291e1b94SAnna Schumaker #define NFSDBG_FACILITY NFSDBG_PROC
181c6dcbe5SAnna Schumaker 
19f4ac1674SAnna Schumaker static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
204bdf87ebSChristoph Hellwig 		struct nfs_lock_context *lock, loff_t offset, loff_t len)
21f4ac1674SAnna Schumaker {
22f4ac1674SAnna Schumaker 	struct inode *inode = file_inode(filep);
239a51940bSAnna Schumaker 	struct nfs_server *server = NFS_SERVER(inode);
24f4ac1674SAnna Schumaker 	struct nfs42_falloc_args args = {
25f4ac1674SAnna Schumaker 		.falloc_fh	= NFS_FH(inode),
26f4ac1674SAnna Schumaker 		.falloc_offset	= offset,
27f4ac1674SAnna Schumaker 		.falloc_length	= len,
289a51940bSAnna Schumaker 		.falloc_bitmask	= server->cache_consistency_bitmask,
29f4ac1674SAnna Schumaker 	};
309a51940bSAnna Schumaker 	struct nfs42_falloc_res res = {
319a51940bSAnna Schumaker 		.falloc_server	= server,
329a51940bSAnna Schumaker 	};
33f4ac1674SAnna Schumaker 	int status;
34f4ac1674SAnna Schumaker 
35f4ac1674SAnna Schumaker 	msg->rpc_argp = &args;
36f4ac1674SAnna Schumaker 	msg->rpc_resp = &res;
37f4ac1674SAnna Schumaker 
384bdf87ebSChristoph Hellwig 	status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
394bdf87ebSChristoph Hellwig 			lock, FMODE_WRITE);
40f4ac1674SAnna Schumaker 	if (status)
41f4ac1674SAnna Schumaker 		return status;
42f4ac1674SAnna Schumaker 
439a51940bSAnna Schumaker 	res.falloc_fattr = nfs_alloc_fattr();
449a51940bSAnna Schumaker 	if (!res.falloc_fattr)
459a51940bSAnna Schumaker 		return -ENOMEM;
469a51940bSAnna Schumaker 
479a51940bSAnna Schumaker 	status = nfs4_call_sync(server->client, server, msg,
48f4ac1674SAnna Schumaker 				&args.seq_args, &res.seq_res, 0);
499a51940bSAnna Schumaker 	if (status == 0)
509a51940bSAnna Schumaker 		status = nfs_post_op_update_inode(inode, res.falloc_fattr);
519a51940bSAnna Schumaker 
529a51940bSAnna Schumaker 	kfree(res.falloc_fattr);
539a51940bSAnna Schumaker 	return status;
54f4ac1674SAnna Schumaker }
55f4ac1674SAnna Schumaker 
56f4ac1674SAnna Schumaker static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
57f4ac1674SAnna Schumaker 				loff_t offset, loff_t len)
58f4ac1674SAnna Schumaker {
59f4ac1674SAnna Schumaker 	struct nfs_server *server = NFS_SERVER(file_inode(filep));
60f4ac1674SAnna Schumaker 	struct nfs4_exception exception = { };
614bdf87ebSChristoph Hellwig 	struct nfs_lock_context *lock;
62f4ac1674SAnna Schumaker 	int err;
63f4ac1674SAnna Schumaker 
644bdf87ebSChristoph Hellwig 	lock = nfs_get_lock_context(nfs_file_open_context(filep));
654bdf87ebSChristoph Hellwig 	if (IS_ERR(lock))
664bdf87ebSChristoph Hellwig 		return PTR_ERR(lock);
674bdf87ebSChristoph Hellwig 
684bdf87ebSChristoph Hellwig 	exception.inode = file_inode(filep);
694bdf87ebSChristoph Hellwig 	exception.state = lock->open_context->state;
704bdf87ebSChristoph Hellwig 
71f4ac1674SAnna Schumaker 	do {
724bdf87ebSChristoph Hellwig 		err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
734bdf87ebSChristoph Hellwig 		if (err == -ENOTSUPP) {
744bdf87ebSChristoph Hellwig 			err = -EOPNOTSUPP;
754bdf87ebSChristoph Hellwig 			break;
764bdf87ebSChristoph Hellwig 		}
77f4ac1674SAnna Schumaker 		err = nfs4_handle_exception(server, err, &exception);
78f4ac1674SAnna Schumaker 	} while (exception.retry);
79f4ac1674SAnna Schumaker 
804bdf87ebSChristoph Hellwig 	nfs_put_lock_context(lock);
81f4ac1674SAnna Schumaker 	return err;
82f4ac1674SAnna Schumaker }
83f4ac1674SAnna Schumaker 
84f4ac1674SAnna Schumaker int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
85f4ac1674SAnna Schumaker {
86f4ac1674SAnna Schumaker 	struct rpc_message msg = {
87f4ac1674SAnna Schumaker 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
88f4ac1674SAnna Schumaker 	};
89f4ac1674SAnna Schumaker 	struct inode *inode = file_inode(filep);
90f4ac1674SAnna Schumaker 	int err;
91f4ac1674SAnna Schumaker 
92f4ac1674SAnna Schumaker 	if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
93f4ac1674SAnna Schumaker 		return -EOPNOTSUPP;
94f4ac1674SAnna Schumaker 
955955102cSAl Viro 	inode_lock(inode);
96f830f7ddSAnna Schumaker 
97f4ac1674SAnna Schumaker 	err = nfs42_proc_fallocate(&msg, filep, offset, len);
98f4ac1674SAnna Schumaker 	if (err == -EOPNOTSUPP)
99f4ac1674SAnna Schumaker 		NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
100f830f7ddSAnna Schumaker 
1015955102cSAl Viro 	inode_unlock(inode);
102f4ac1674SAnna Schumaker 	return err;
103f4ac1674SAnna Schumaker }
104f4ac1674SAnna Schumaker 
105624bd5b7SAnna Schumaker int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
106624bd5b7SAnna Schumaker {
107624bd5b7SAnna Schumaker 	struct rpc_message msg = {
108624bd5b7SAnna Schumaker 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
109624bd5b7SAnna Schumaker 	};
110624bd5b7SAnna Schumaker 	struct inode *inode = file_inode(filep);
111624bd5b7SAnna Schumaker 	int err;
112624bd5b7SAnna Schumaker 
113624bd5b7SAnna Schumaker 	if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
114624bd5b7SAnna Schumaker 		return -EOPNOTSUPP;
115624bd5b7SAnna Schumaker 
1169a51940bSAnna Schumaker 	nfs_wb_all(inode);
1175955102cSAl Viro 	inode_lock(inode);
118f830f7ddSAnna Schumaker 
119624bd5b7SAnna Schumaker 	err = nfs42_proc_fallocate(&msg, filep, offset, len);
1209a51940bSAnna Schumaker 	if (err == 0)
1219a51940bSAnna Schumaker 		truncate_pagecache_range(inode, offset, (offset + len) -1);
122624bd5b7SAnna Schumaker 	if (err == -EOPNOTSUPP)
123624bd5b7SAnna Schumaker 		NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
124f830f7ddSAnna Schumaker 
1255955102cSAl Viro 	inode_unlock(inode);
126624bd5b7SAnna Schumaker 	return err;
127624bd5b7SAnna Schumaker }
128624bd5b7SAnna Schumaker 
1292e72448bSAnna Schumaker static ssize_t _nfs42_proc_copy(struct file *src, loff_t pos_src,
1302e72448bSAnna Schumaker 				struct nfs_lock_context *src_lock,
1312e72448bSAnna Schumaker 				struct file *dst, loff_t pos_dst,
1322e72448bSAnna Schumaker 				struct nfs_lock_context *dst_lock,
1332e72448bSAnna Schumaker 				size_t count)
1342e72448bSAnna Schumaker {
1352e72448bSAnna Schumaker 	struct nfs42_copy_args args = {
1362e72448bSAnna Schumaker 		.src_fh		= NFS_FH(file_inode(src)),
1372e72448bSAnna Schumaker 		.src_pos	= pos_src,
1382e72448bSAnna Schumaker 		.dst_fh		= NFS_FH(file_inode(dst)),
1392e72448bSAnna Schumaker 		.dst_pos	= pos_dst,
1402e72448bSAnna Schumaker 		.count		= count,
1412e72448bSAnna Schumaker 	};
1422e72448bSAnna Schumaker 	struct nfs42_copy_res res;
1432e72448bSAnna Schumaker 	struct rpc_message msg = {
1442e72448bSAnna Schumaker 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
1452e72448bSAnna Schumaker 		.rpc_argp = &args,
1462e72448bSAnna Schumaker 		.rpc_resp = &res,
1472e72448bSAnna Schumaker 	};
1482e72448bSAnna Schumaker 	struct inode *dst_inode = file_inode(dst);
1492e72448bSAnna Schumaker 	struct nfs_server *server = NFS_SERVER(dst_inode);
1502e72448bSAnna Schumaker 	int status;
1512e72448bSAnna Schumaker 
1522e72448bSAnna Schumaker 	status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
1532e72448bSAnna Schumaker 				     src_lock, FMODE_READ);
1542e72448bSAnna Schumaker 	if (status)
1552e72448bSAnna Schumaker 		return status;
1562e72448bSAnna Schumaker 
1572e72448bSAnna Schumaker 	status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1582e72448bSAnna Schumaker 				     dst_lock, FMODE_WRITE);
1592e72448bSAnna Schumaker 	if (status)
1602e72448bSAnna Schumaker 		return status;
1612e72448bSAnna Schumaker 
1622e72448bSAnna Schumaker 	status = nfs4_call_sync(server->client, server, &msg,
1632e72448bSAnna Schumaker 				&args.seq_args, &res.seq_res, 0);
1642e72448bSAnna Schumaker 	if (status == -ENOTSUPP)
1652e72448bSAnna Schumaker 		server->caps &= ~NFS_CAP_COPY;
1662e72448bSAnna Schumaker 	if (status)
1672e72448bSAnna Schumaker 		return status;
1682e72448bSAnna Schumaker 
1692e72448bSAnna Schumaker 	if (res.write_res.verifier.committed != NFS_FILE_SYNC) {
1702e72448bSAnna Schumaker 		status = nfs_commit_file(dst, &res.write_res.verifier.verifier);
1712e72448bSAnna Schumaker 		if (status)
1722e72448bSAnna Schumaker 			return status;
1732e72448bSAnna Schumaker 	}
1742e72448bSAnna Schumaker 
1752e72448bSAnna Schumaker 	truncate_pagecache_range(dst_inode, pos_dst,
1762e72448bSAnna Schumaker 				 pos_dst + res.write_res.count);
1772e72448bSAnna Schumaker 
1782e72448bSAnna Schumaker 	return res.write_res.count;
1792e72448bSAnna Schumaker }
1802e72448bSAnna Schumaker 
1812e72448bSAnna Schumaker ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
1822e72448bSAnna Schumaker 			struct file *dst, loff_t pos_dst,
1832e72448bSAnna Schumaker 			size_t count)
1842e72448bSAnna Schumaker {
1852e72448bSAnna Schumaker 	struct nfs_server *server = NFS_SERVER(file_inode(dst));
1862e72448bSAnna Schumaker 	struct nfs_lock_context *src_lock;
1872e72448bSAnna Schumaker 	struct nfs_lock_context *dst_lock;
1882e72448bSAnna Schumaker 	struct nfs4_exception src_exception = { };
1892e72448bSAnna Schumaker 	struct nfs4_exception dst_exception = { };
1902e72448bSAnna Schumaker 	ssize_t err, err2;
1912e72448bSAnna Schumaker 
1922e72448bSAnna Schumaker 	if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY))
1932e72448bSAnna Schumaker 		return -EOPNOTSUPP;
1942e72448bSAnna Schumaker 
1952e72448bSAnna Schumaker 	src_lock = nfs_get_lock_context(nfs_file_open_context(src));
1962e72448bSAnna Schumaker 	if (IS_ERR(src_lock))
1972e72448bSAnna Schumaker 		return PTR_ERR(src_lock);
1982e72448bSAnna Schumaker 
1992e72448bSAnna Schumaker 	src_exception.inode = file_inode(src);
2002e72448bSAnna Schumaker 	src_exception.state = src_lock->open_context->state;
2012e72448bSAnna Schumaker 
2022e72448bSAnna Schumaker 	dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
2032e72448bSAnna Schumaker 	if (IS_ERR(dst_lock)) {
2042e72448bSAnna Schumaker 		err = PTR_ERR(dst_lock);
2052e72448bSAnna Schumaker 		goto out_put_src_lock;
2062e72448bSAnna Schumaker 	}
2072e72448bSAnna Schumaker 
2082e72448bSAnna Schumaker 	dst_exception.inode = file_inode(dst);
2092e72448bSAnna Schumaker 	dst_exception.state = dst_lock->open_context->state;
2102e72448bSAnna Schumaker 
2112e72448bSAnna Schumaker 	do {
2122e72448bSAnna Schumaker 		mutex_lock(&file_inode(dst)->i_mutex);
2132e72448bSAnna Schumaker 		err = _nfs42_proc_copy(src, pos_src, src_lock,
2142e72448bSAnna Schumaker 				       dst, pos_dst, dst_lock, count);
2152e72448bSAnna Schumaker 		mutex_unlock(&file_inode(dst)->i_mutex);
2162e72448bSAnna Schumaker 
2172e72448bSAnna Schumaker 		if (err == -ENOTSUPP) {
2182e72448bSAnna Schumaker 			err = -EOPNOTSUPP;
2192e72448bSAnna Schumaker 			break;
2202e72448bSAnna Schumaker 		}
2212e72448bSAnna Schumaker 
2222e72448bSAnna Schumaker 		err2 = nfs4_handle_exception(server, err, &src_exception);
2232e72448bSAnna Schumaker 		err  = nfs4_handle_exception(server, err, &dst_exception);
2242e72448bSAnna Schumaker 		if (!err)
2252e72448bSAnna Schumaker 			err = err2;
2262e72448bSAnna Schumaker 	} while (src_exception.retry || dst_exception.retry);
2272e72448bSAnna Schumaker 
2282e72448bSAnna Schumaker 	nfs_put_lock_context(dst_lock);
2292e72448bSAnna Schumaker out_put_src_lock:
2302e72448bSAnna Schumaker 	nfs_put_lock_context(src_lock);
2312e72448bSAnna Schumaker 	return err;
2322e72448bSAnna Schumaker }
2332e72448bSAnna Schumaker 
2344bdf87ebSChristoph Hellwig static loff_t _nfs42_proc_llseek(struct file *filep,
2354bdf87ebSChristoph Hellwig 		struct nfs_lock_context *lock, loff_t offset, int whence)
2361c6dcbe5SAnna Schumaker {
2371c6dcbe5SAnna Schumaker 	struct inode *inode = file_inode(filep);
2381c6dcbe5SAnna Schumaker 	struct nfs42_seek_args args = {
2391c6dcbe5SAnna Schumaker 		.sa_fh		= NFS_FH(inode),
2401c6dcbe5SAnna Schumaker 		.sa_offset	= offset,
2411c6dcbe5SAnna Schumaker 		.sa_what	= (whence == SEEK_HOLE) ?
2421c6dcbe5SAnna Schumaker 					NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
2431c6dcbe5SAnna Schumaker 	};
2441c6dcbe5SAnna Schumaker 	struct nfs42_seek_res res;
2451c6dcbe5SAnna Schumaker 	struct rpc_message msg = {
2461c6dcbe5SAnna Schumaker 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
2471c6dcbe5SAnna Schumaker 		.rpc_argp = &args,
2481c6dcbe5SAnna Schumaker 		.rpc_resp = &res,
2491c6dcbe5SAnna Schumaker 	};
2501c6dcbe5SAnna Schumaker 	struct nfs_server *server = NFS_SERVER(inode);
2511c6dcbe5SAnna Schumaker 	int status;
2521c6dcbe5SAnna Schumaker 
253878ffa9fSAnna Schumaker 	if (!nfs_server_capable(inode, NFS_CAP_SEEK))
2541c6dcbe5SAnna Schumaker 		return -ENOTSUPP;
2551c6dcbe5SAnna Schumaker 
2564bdf87ebSChristoph Hellwig 	status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
2574bdf87ebSChristoph Hellwig 			lock, FMODE_READ);
2581c6dcbe5SAnna Schumaker 	if (status)
2591c6dcbe5SAnna Schumaker 		return status;
2601c6dcbe5SAnna Schumaker 
2611c6dcbe5SAnna Schumaker 	nfs_wb_all(inode);
2621c6dcbe5SAnna Schumaker 	status = nfs4_call_sync(server->client, server, &msg,
2631c6dcbe5SAnna Schumaker 				&args.seq_args, &res.seq_res, 0);
2641c6dcbe5SAnna Schumaker 	if (status == -ENOTSUPP)
2651c6dcbe5SAnna Schumaker 		server->caps &= ~NFS_CAP_SEEK;
2661c6dcbe5SAnna Schumaker 	if (status)
2671c6dcbe5SAnna Schumaker 		return status;
2681c6dcbe5SAnna Schumaker 
2691c6dcbe5SAnna Schumaker 	return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
2701c6dcbe5SAnna Schumaker }
271be3a5d23STrond Myklebust 
272bdcc2cd1SJ. Bruce Fields loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
273bdcc2cd1SJ. Bruce Fields {
274bdcc2cd1SJ. Bruce Fields 	struct nfs_server *server = NFS_SERVER(file_inode(filep));
275bdcc2cd1SJ. Bruce Fields 	struct nfs4_exception exception = { };
2764bdf87ebSChristoph Hellwig 	struct nfs_lock_context *lock;
277306a5549SJ. Bruce Fields 	loff_t err;
278bdcc2cd1SJ. Bruce Fields 
2794bdf87ebSChristoph Hellwig 	lock = nfs_get_lock_context(nfs_file_open_context(filep));
2804bdf87ebSChristoph Hellwig 	if (IS_ERR(lock))
2814bdf87ebSChristoph Hellwig 		return PTR_ERR(lock);
2824bdf87ebSChristoph Hellwig 
2834bdf87ebSChristoph Hellwig 	exception.inode = file_inode(filep);
2844bdf87ebSChristoph Hellwig 	exception.state = lock->open_context->state;
2854bdf87ebSChristoph Hellwig 
286bdcc2cd1SJ. Bruce Fields 	do {
2874bdf87ebSChristoph Hellwig 		err = _nfs42_proc_llseek(filep, lock, offset, whence);
288306a5549SJ. Bruce Fields 		if (err >= 0)
289306a5549SJ. Bruce Fields 			break;
2904bdf87ebSChristoph Hellwig 		if (err == -ENOTSUPP) {
2914bdf87ebSChristoph Hellwig 			err = -EOPNOTSUPP;
2924bdf87ebSChristoph Hellwig 			break;
2934bdf87ebSChristoph Hellwig 		}
294bdcc2cd1SJ. Bruce Fields 		err = nfs4_handle_exception(server, err, &exception);
295bdcc2cd1SJ. Bruce Fields 	} while (exception.retry);
296bdcc2cd1SJ. Bruce Fields 
2974bdf87ebSChristoph Hellwig 	nfs_put_lock_context(lock);
298bdcc2cd1SJ. Bruce Fields 	return err;
299bdcc2cd1SJ. Bruce Fields }
300bdcc2cd1SJ. Bruce Fields 
301bdcc2cd1SJ. Bruce Fields 
3021b4a4bd8SPeng Tao static void
3031b4a4bd8SPeng Tao nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
3041b4a4bd8SPeng Tao {
3051b4a4bd8SPeng Tao 	struct nfs42_layoutstat_data *data = calldata;
3061b4a4bd8SPeng Tao 	struct nfs_server *server = NFS_SERVER(data->args.inode);
3071b4a4bd8SPeng Tao 
3081b4a4bd8SPeng Tao 	nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args,
3091b4a4bd8SPeng Tao 			     &data->res.seq_res, task);
3101b4a4bd8SPeng Tao }
3111b4a4bd8SPeng Tao 
3121b4a4bd8SPeng Tao static void
3131b4a4bd8SPeng Tao nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
3141b4a4bd8SPeng Tao {
3151b4a4bd8SPeng Tao 	struct nfs42_layoutstat_data *data = calldata;
31668d264cfSPeng Tao 	struct inode *inode = data->inode;
31768d264cfSPeng Tao 	struct pnfs_layout_hdr *lo;
3181b4a4bd8SPeng Tao 
3191b4a4bd8SPeng Tao 	if (!nfs4_sequence_done(task, &data->res.seq_res))
3201b4a4bd8SPeng Tao 		return;
3211b4a4bd8SPeng Tao 
3226c5a0d89STrond Myklebust 	switch (task->tk_status) {
3236c5a0d89STrond Myklebust 	case 0:
3246c5a0d89STrond Myklebust 		break;
32568d264cfSPeng Tao 	case -NFS4ERR_EXPIRED:
32668d264cfSPeng Tao 	case -NFS4ERR_STALE_STATEID:
32768d264cfSPeng Tao 	case -NFS4ERR_OLD_STATEID:
32868d264cfSPeng Tao 	case -NFS4ERR_BAD_STATEID:
32968d264cfSPeng Tao 		spin_lock(&inode->i_lock);
33068d264cfSPeng Tao 		lo = NFS_I(inode)->layout;
33168d264cfSPeng Tao 		if (lo && nfs4_stateid_match(&data->args.stateid,
33268d264cfSPeng Tao 					     &lo->plh_stateid)) {
33368d264cfSPeng Tao 			LIST_HEAD(head);
33468d264cfSPeng Tao 
33568d264cfSPeng Tao 			/*
33668d264cfSPeng Tao 			 * Mark the bad layout state as invalid, then retry
33768d264cfSPeng Tao 			 * with the current stateid.
33868d264cfSPeng Tao 			 */
33968d264cfSPeng Tao 			set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
34068d264cfSPeng Tao 			pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
34168d264cfSPeng Tao 			spin_unlock(&inode->i_lock);
34268d264cfSPeng Tao 			pnfs_free_lseg_list(&head);
34368d264cfSPeng Tao 		} else
34468d264cfSPeng Tao 			spin_unlock(&inode->i_lock);
34568d264cfSPeng Tao 		break;
3466c5a0d89STrond Myklebust 	case -ENOTSUPP:
3476c5a0d89STrond Myklebust 	case -EOPNOTSUPP:
34868d264cfSPeng Tao 		NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
3496c5a0d89STrond Myklebust 	default:
35068d264cfSPeng Tao 		break;
3511b4a4bd8SPeng Tao 	}
35268d264cfSPeng Tao 
35368d264cfSPeng Tao 	dprintk("%s server returns %d\n", __func__, task->tk_status);
3546c5a0d89STrond Myklebust }
3551b4a4bd8SPeng Tao 
3561b4a4bd8SPeng Tao static void
3571b4a4bd8SPeng Tao nfs42_layoutstat_release(void *calldata)
3581b4a4bd8SPeng Tao {
3591b4a4bd8SPeng Tao 	struct nfs42_layoutstat_data *data = calldata;
3608733408dSPeng Tao 	struct nfs_server *nfss = NFS_SERVER(data->args.inode);
3618733408dSPeng Tao 
3628733408dSPeng Tao 	if (nfss->pnfs_curr_ld->cleanup_layoutstats)
3638733408dSPeng Tao 		nfss->pnfs_curr_ld->cleanup_layoutstats(data);
3641b4a4bd8SPeng Tao 
3651b4a4bd8SPeng Tao 	pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
3661bfe3b25SPeng Tao 	smp_mb__before_atomic();
3671bfe3b25SPeng Tao 	clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
3681bfe3b25SPeng Tao 	smp_mb__after_atomic();
3691b4a4bd8SPeng Tao 	nfs_iput_and_deactive(data->inode);
3701b4a4bd8SPeng Tao 	kfree(data->args.devinfo);
3711b4a4bd8SPeng Tao 	kfree(data);
3721b4a4bd8SPeng Tao }
3731b4a4bd8SPeng Tao 
374be3a5d23STrond Myklebust static const struct rpc_call_ops nfs42_layoutstat_ops = {
3751b4a4bd8SPeng Tao 	.rpc_call_prepare = nfs42_layoutstat_prepare,
3761b4a4bd8SPeng Tao 	.rpc_call_done = nfs42_layoutstat_done,
3771b4a4bd8SPeng Tao 	.rpc_release = nfs42_layoutstat_release,
378be3a5d23STrond Myklebust };
379be3a5d23STrond Myklebust 
380be3a5d23STrond Myklebust int nfs42_proc_layoutstats_generic(struct nfs_server *server,
381be3a5d23STrond Myklebust 				   struct nfs42_layoutstat_data *data)
382be3a5d23STrond Myklebust {
383be3a5d23STrond Myklebust 	struct rpc_message msg = {
384be3a5d23STrond Myklebust 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
385be3a5d23STrond Myklebust 		.rpc_argp = &data->args,
386be3a5d23STrond Myklebust 		.rpc_resp = &data->res,
387be3a5d23STrond Myklebust 	};
388be3a5d23STrond Myklebust 	struct rpc_task_setup task_setup = {
389be3a5d23STrond Myklebust 		.rpc_client = server->client,
390be3a5d23STrond Myklebust 		.rpc_message = &msg,
391be3a5d23STrond Myklebust 		.callback_ops = &nfs42_layoutstat_ops,
392be3a5d23STrond Myklebust 		.callback_data = data,
393be3a5d23STrond Myklebust 		.flags = RPC_TASK_ASYNC,
394be3a5d23STrond Myklebust 	};
395be3a5d23STrond Myklebust 	struct rpc_task *task;
396be3a5d23STrond Myklebust 
3971b4a4bd8SPeng Tao 	data->inode = nfs_igrab_and_active(data->args.inode);
3981b4a4bd8SPeng Tao 	if (!data->inode) {
3991b4a4bd8SPeng Tao 		nfs42_layoutstat_release(data);
4001b4a4bd8SPeng Tao 		return -EAGAIN;
4011b4a4bd8SPeng Tao 	}
402be3a5d23STrond Myklebust 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
403be3a5d23STrond Myklebust 	task = rpc_run_task(&task_setup);
404be3a5d23STrond Myklebust 	if (IS_ERR(task))
405be3a5d23STrond Myklebust 		return PTR_ERR(task);
406be3a5d23STrond Myklebust 	return 0;
407be3a5d23STrond Myklebust }
408e5341f3aSPeng Tao 
409e5341f3aSPeng Tao static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
4104bdf87ebSChristoph Hellwig 		struct file *dst_f, struct nfs_lock_context *src_lock,
4114bdf87ebSChristoph Hellwig 		struct nfs_lock_context *dst_lock, loff_t src_offset,
412e5341f3aSPeng Tao 		loff_t dst_offset, loff_t count)
413e5341f3aSPeng Tao {
414e5341f3aSPeng Tao 	struct inode *src_inode = file_inode(src_f);
415e5341f3aSPeng Tao 	struct inode *dst_inode = file_inode(dst_f);
416e5341f3aSPeng Tao 	struct nfs_server *server = NFS_SERVER(dst_inode);
417e5341f3aSPeng Tao 	struct nfs42_clone_args args = {
418e5341f3aSPeng Tao 		.src_fh = NFS_FH(src_inode),
419e5341f3aSPeng Tao 		.dst_fh = NFS_FH(dst_inode),
420e5341f3aSPeng Tao 		.src_offset = src_offset,
421e5341f3aSPeng Tao 		.dst_offset = dst_offset,
4229494b2ceSChristoph Hellwig 		.count = count,
423e5341f3aSPeng Tao 		.dst_bitmask = server->cache_consistency_bitmask,
424e5341f3aSPeng Tao 	};
425e5341f3aSPeng Tao 	struct nfs42_clone_res res = {
426e5341f3aSPeng Tao 		.server	= server,
427e5341f3aSPeng Tao 	};
428e5341f3aSPeng Tao 	int status;
429e5341f3aSPeng Tao 
430e5341f3aSPeng Tao 	msg->rpc_argp = &args;
431e5341f3aSPeng Tao 	msg->rpc_resp = &res;
432e5341f3aSPeng Tao 
4334bdf87ebSChristoph Hellwig 	status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
4344bdf87ebSChristoph Hellwig 			src_lock, FMODE_READ);
435e5341f3aSPeng Tao 	if (status)
436e5341f3aSPeng Tao 		return status;
437e5341f3aSPeng Tao 
4384bdf87ebSChristoph Hellwig 	status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
4394bdf87ebSChristoph Hellwig 			dst_lock, FMODE_WRITE);
440e5341f3aSPeng Tao 	if (status)
441e5341f3aSPeng Tao 		return status;
442e5341f3aSPeng Tao 
443e5341f3aSPeng Tao 	res.dst_fattr = nfs_alloc_fattr();
444e5341f3aSPeng Tao 	if (!res.dst_fattr)
445e5341f3aSPeng Tao 		return -ENOMEM;
446e5341f3aSPeng Tao 
447e5341f3aSPeng Tao 	status = nfs4_call_sync(server->client, server, msg,
448e5341f3aSPeng Tao 				&args.seq_args, &res.seq_res, 0);
449e5341f3aSPeng Tao 	if (status == 0)
450e5341f3aSPeng Tao 		status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
451e5341f3aSPeng Tao 
452e5341f3aSPeng Tao 	kfree(res.dst_fattr);
453e5341f3aSPeng Tao 	return status;
454e5341f3aSPeng Tao }
455e5341f3aSPeng Tao 
456e5341f3aSPeng Tao int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
457e5341f3aSPeng Tao 		     loff_t src_offset, loff_t dst_offset, loff_t count)
458e5341f3aSPeng Tao {
459e5341f3aSPeng Tao 	struct rpc_message msg = {
460e5341f3aSPeng Tao 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
461e5341f3aSPeng Tao 	};
462e5341f3aSPeng Tao 	struct inode *inode = file_inode(src_f);
463e5341f3aSPeng Tao 	struct nfs_server *server = NFS_SERVER(file_inode(src_f));
4644bdf87ebSChristoph Hellwig 	struct nfs_lock_context *src_lock;
4654bdf87ebSChristoph Hellwig 	struct nfs_lock_context *dst_lock;
4664bdf87ebSChristoph Hellwig 	struct nfs4_exception src_exception = { };
4674bdf87ebSChristoph Hellwig 	struct nfs4_exception dst_exception = { };
4684bdf87ebSChristoph Hellwig 	int err, err2;
469e5341f3aSPeng Tao 
470e5341f3aSPeng Tao 	if (!nfs_server_capable(inode, NFS_CAP_CLONE))
471e5341f3aSPeng Tao 		return -EOPNOTSUPP;
472e5341f3aSPeng Tao 
4734bdf87ebSChristoph Hellwig 	src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
4744bdf87ebSChristoph Hellwig 	if (IS_ERR(src_lock))
4754bdf87ebSChristoph Hellwig 		return PTR_ERR(src_lock);
4764bdf87ebSChristoph Hellwig 
4774bdf87ebSChristoph Hellwig 	src_exception.inode = file_inode(src_f);
4784bdf87ebSChristoph Hellwig 	src_exception.state = src_lock->open_context->state;
4794bdf87ebSChristoph Hellwig 
4804bdf87ebSChristoph Hellwig 	dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
4814bdf87ebSChristoph Hellwig 	if (IS_ERR(dst_lock)) {
4824bdf87ebSChristoph Hellwig 		err = PTR_ERR(dst_lock);
4834bdf87ebSChristoph Hellwig 		goto out_put_src_lock;
4844bdf87ebSChristoph Hellwig 	}
4854bdf87ebSChristoph Hellwig 
4864bdf87ebSChristoph Hellwig 	dst_exception.inode = file_inode(dst_f);
4874bdf87ebSChristoph Hellwig 	dst_exception.state = dst_lock->open_context->state;
4884bdf87ebSChristoph Hellwig 
489e5341f3aSPeng Tao 	do {
4904bdf87ebSChristoph Hellwig 		err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
4914bdf87ebSChristoph Hellwig 					src_offset, dst_offset, count);
492e5341f3aSPeng Tao 		if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
493e5341f3aSPeng Tao 			NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
4944bdf87ebSChristoph Hellwig 			err = -EOPNOTSUPP;
4954bdf87ebSChristoph Hellwig 			break;
496e5341f3aSPeng Tao 		}
497e5341f3aSPeng Tao 
4984bdf87ebSChristoph Hellwig 		err2 = nfs4_handle_exception(server, err, &src_exception);
4994bdf87ebSChristoph Hellwig 		err = nfs4_handle_exception(server, err, &dst_exception);
5004bdf87ebSChristoph Hellwig 		if (!err)
5014bdf87ebSChristoph Hellwig 			err = err2;
5024bdf87ebSChristoph Hellwig 	} while (src_exception.retry || dst_exception.retry);
5034bdf87ebSChristoph Hellwig 
5044bdf87ebSChristoph Hellwig 	nfs_put_lock_context(dst_lock);
5054bdf87ebSChristoph Hellwig out_put_src_lock:
5064bdf87ebSChristoph Hellwig 	nfs_put_lock_context(src_lock);
507e5341f3aSPeng Tao 	return err;
508e5341f3aSPeng Tao }
509