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 1165955102cSAl Viro inode_lock(inode); 1171e564d3dSTrond Myklebust err = nfs_sync_inode(inode); 1181e564d3dSTrond Myklebust if (err) 1191e564d3dSTrond Myklebust goto out_unlock; 120f830f7ddSAnna Schumaker 121624bd5b7SAnna Schumaker err = nfs42_proc_fallocate(&msg, filep, offset, len); 1229a51940bSAnna Schumaker if (err == 0) 1239a51940bSAnna Schumaker truncate_pagecache_range(inode, offset, (offset + len) -1); 124624bd5b7SAnna Schumaker if (err == -EOPNOTSUPP) 125624bd5b7SAnna Schumaker NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE; 1261e564d3dSTrond Myklebust out_unlock: 1275955102cSAl Viro inode_unlock(inode); 128624bd5b7SAnna Schumaker return err; 129624bd5b7SAnna Schumaker } 130624bd5b7SAnna Schumaker 1312e72448bSAnna Schumaker static ssize_t _nfs42_proc_copy(struct file *src, loff_t pos_src, 1322e72448bSAnna Schumaker struct nfs_lock_context *src_lock, 1332e72448bSAnna Schumaker struct file *dst, loff_t pos_dst, 1342e72448bSAnna Schumaker struct nfs_lock_context *dst_lock, 1352e72448bSAnna Schumaker size_t count) 1362e72448bSAnna Schumaker { 1372e72448bSAnna Schumaker struct nfs42_copy_args args = { 1382e72448bSAnna Schumaker .src_fh = NFS_FH(file_inode(src)), 1392e72448bSAnna Schumaker .src_pos = pos_src, 1402e72448bSAnna Schumaker .dst_fh = NFS_FH(file_inode(dst)), 1412e72448bSAnna Schumaker .dst_pos = pos_dst, 1422e72448bSAnna Schumaker .count = count, 1432e72448bSAnna Schumaker }; 1442e72448bSAnna Schumaker struct nfs42_copy_res res; 1452e72448bSAnna Schumaker struct rpc_message msg = { 1462e72448bSAnna Schumaker .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY], 1472e72448bSAnna Schumaker .rpc_argp = &args, 1482e72448bSAnna Schumaker .rpc_resp = &res, 1492e72448bSAnna Schumaker }; 1502e72448bSAnna Schumaker struct inode *dst_inode = file_inode(dst); 1512e72448bSAnna Schumaker struct nfs_server *server = NFS_SERVER(dst_inode); 1522e72448bSAnna Schumaker int status; 1532e72448bSAnna Schumaker 1542e72448bSAnna Schumaker status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context, 1552e72448bSAnna Schumaker src_lock, FMODE_READ); 1562e72448bSAnna Schumaker if (status) 1572e72448bSAnna Schumaker return status; 1582e72448bSAnna Schumaker 159837bb1d7STrond Myklebust status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping, 160837bb1d7STrond Myklebust pos_src, pos_src + (loff_t)count - 1); 161837bb1d7STrond Myklebust if (status) 162837bb1d7STrond Myklebust return status; 163837bb1d7STrond Myklebust 1642e72448bSAnna Schumaker status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context, 1652e72448bSAnna Schumaker dst_lock, FMODE_WRITE); 1662e72448bSAnna Schumaker if (status) 1672e72448bSAnna Schumaker return status; 1682e72448bSAnna Schumaker 169837bb1d7STrond Myklebust status = nfs_sync_inode(dst_inode); 170837bb1d7STrond Myklebust if (status) 171837bb1d7STrond Myklebust return status; 172837bb1d7STrond Myklebust 1732e72448bSAnna Schumaker status = nfs4_call_sync(server->client, server, &msg, 1742e72448bSAnna Schumaker &args.seq_args, &res.seq_res, 0); 1752e72448bSAnna Schumaker if (status == -ENOTSUPP) 1762e72448bSAnna Schumaker server->caps &= ~NFS_CAP_COPY; 1772e72448bSAnna Schumaker if (status) 1782e72448bSAnna Schumaker return status; 1792e72448bSAnna Schumaker 1802e72448bSAnna Schumaker if (res.write_res.verifier.committed != NFS_FILE_SYNC) { 1812e72448bSAnna Schumaker status = nfs_commit_file(dst, &res.write_res.verifier.verifier); 1822e72448bSAnna Schumaker if (status) 1832e72448bSAnna Schumaker return status; 1842e72448bSAnna Schumaker } 1852e72448bSAnna Schumaker 1862e72448bSAnna Schumaker truncate_pagecache_range(dst_inode, pos_dst, 1872e72448bSAnna Schumaker pos_dst + res.write_res.count); 1882e72448bSAnna Schumaker 1892e72448bSAnna Schumaker return res.write_res.count; 1902e72448bSAnna Schumaker } 1912e72448bSAnna Schumaker 1922e72448bSAnna Schumaker ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src, 1932e72448bSAnna Schumaker struct file *dst, loff_t pos_dst, 1942e72448bSAnna Schumaker size_t count) 1952e72448bSAnna Schumaker { 1962e72448bSAnna Schumaker struct nfs_server *server = NFS_SERVER(file_inode(dst)); 1972e72448bSAnna Schumaker struct nfs_lock_context *src_lock; 1982e72448bSAnna Schumaker struct nfs_lock_context *dst_lock; 1992e72448bSAnna Schumaker struct nfs4_exception src_exception = { }; 2002e72448bSAnna Schumaker struct nfs4_exception dst_exception = { }; 2012e72448bSAnna Schumaker ssize_t err, err2; 2022e72448bSAnna Schumaker 2032e72448bSAnna Schumaker if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY)) 2042e72448bSAnna Schumaker return -EOPNOTSUPP; 2052e72448bSAnna Schumaker 2062e72448bSAnna Schumaker src_lock = nfs_get_lock_context(nfs_file_open_context(src)); 2072e72448bSAnna Schumaker if (IS_ERR(src_lock)) 2082e72448bSAnna Schumaker return PTR_ERR(src_lock); 2092e72448bSAnna Schumaker 2102e72448bSAnna Schumaker src_exception.inode = file_inode(src); 2112e72448bSAnna Schumaker src_exception.state = src_lock->open_context->state; 2122e72448bSAnna Schumaker 2132e72448bSAnna Schumaker dst_lock = nfs_get_lock_context(nfs_file_open_context(dst)); 2142e72448bSAnna Schumaker if (IS_ERR(dst_lock)) { 2152e72448bSAnna Schumaker err = PTR_ERR(dst_lock); 2162e72448bSAnna Schumaker goto out_put_src_lock; 2172e72448bSAnna Schumaker } 2182e72448bSAnna Schumaker 2192e72448bSAnna Schumaker dst_exception.inode = file_inode(dst); 2202e72448bSAnna Schumaker dst_exception.state = dst_lock->open_context->state; 2212e72448bSAnna Schumaker 2222e72448bSAnna Schumaker do { 223ea8ea737SLinus Torvalds inode_lock(file_inode(dst)); 2242e72448bSAnna Schumaker err = _nfs42_proc_copy(src, pos_src, src_lock, 2252e72448bSAnna Schumaker dst, pos_dst, dst_lock, count); 226ea8ea737SLinus Torvalds inode_unlock(file_inode(dst)); 2272e72448bSAnna Schumaker 2282e72448bSAnna Schumaker if (err == -ENOTSUPP) { 2292e72448bSAnna Schumaker err = -EOPNOTSUPP; 2302e72448bSAnna Schumaker break; 2312e72448bSAnna Schumaker } 2322e72448bSAnna Schumaker 2332e72448bSAnna Schumaker err2 = nfs4_handle_exception(server, err, &src_exception); 2342e72448bSAnna Schumaker err = nfs4_handle_exception(server, err, &dst_exception); 2352e72448bSAnna Schumaker if (!err) 2362e72448bSAnna Schumaker err = err2; 2372e72448bSAnna Schumaker } while (src_exception.retry || dst_exception.retry); 2382e72448bSAnna Schumaker 2392e72448bSAnna Schumaker nfs_put_lock_context(dst_lock); 2402e72448bSAnna Schumaker out_put_src_lock: 2412e72448bSAnna Schumaker nfs_put_lock_context(src_lock); 2422e72448bSAnna Schumaker return err; 2432e72448bSAnna Schumaker } 2442e72448bSAnna Schumaker 2454bdf87ebSChristoph Hellwig static loff_t _nfs42_proc_llseek(struct file *filep, 2464bdf87ebSChristoph Hellwig struct nfs_lock_context *lock, loff_t offset, int whence) 2471c6dcbe5SAnna Schumaker { 2481c6dcbe5SAnna Schumaker struct inode *inode = file_inode(filep); 2491c6dcbe5SAnna Schumaker struct nfs42_seek_args args = { 2501c6dcbe5SAnna Schumaker .sa_fh = NFS_FH(inode), 2511c6dcbe5SAnna Schumaker .sa_offset = offset, 2521c6dcbe5SAnna Schumaker .sa_what = (whence == SEEK_HOLE) ? 2531c6dcbe5SAnna Schumaker NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA, 2541c6dcbe5SAnna Schumaker }; 2551c6dcbe5SAnna Schumaker struct nfs42_seek_res res; 2561c6dcbe5SAnna Schumaker struct rpc_message msg = { 2571c6dcbe5SAnna Schumaker .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK], 2581c6dcbe5SAnna Schumaker .rpc_argp = &args, 2591c6dcbe5SAnna Schumaker .rpc_resp = &res, 2601c6dcbe5SAnna Schumaker }; 2611c6dcbe5SAnna Schumaker struct nfs_server *server = NFS_SERVER(inode); 2621c6dcbe5SAnna Schumaker int status; 2631c6dcbe5SAnna Schumaker 264878ffa9fSAnna Schumaker if (!nfs_server_capable(inode, NFS_CAP_SEEK)) 2651c6dcbe5SAnna Schumaker return -ENOTSUPP; 2661c6dcbe5SAnna Schumaker 2674bdf87ebSChristoph Hellwig status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context, 2684bdf87ebSChristoph Hellwig lock, FMODE_READ); 2691c6dcbe5SAnna Schumaker if (status) 2701c6dcbe5SAnna Schumaker return status; 2711c6dcbe5SAnna Schumaker 272e95fc4a0STrond Myklebust status = nfs_filemap_write_and_wait_range(inode->i_mapping, 273e95fc4a0STrond Myklebust offset, LLONG_MAX); 274e95fc4a0STrond Myklebust if (status) 275e95fc4a0STrond Myklebust return status; 276e95fc4a0STrond Myklebust 2771c6dcbe5SAnna Schumaker status = nfs4_call_sync(server->client, server, &msg, 2781c6dcbe5SAnna Schumaker &args.seq_args, &res.seq_res, 0); 2791c6dcbe5SAnna Schumaker if (status == -ENOTSUPP) 2801c6dcbe5SAnna Schumaker server->caps &= ~NFS_CAP_SEEK; 2811c6dcbe5SAnna Schumaker if (status) 2821c6dcbe5SAnna Schumaker return status; 2831c6dcbe5SAnna Schumaker 2841c6dcbe5SAnna Schumaker return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes); 2851c6dcbe5SAnna Schumaker } 286be3a5d23STrond Myklebust 287bdcc2cd1SJ. Bruce Fields loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence) 288bdcc2cd1SJ. Bruce Fields { 289bdcc2cd1SJ. Bruce Fields struct nfs_server *server = NFS_SERVER(file_inode(filep)); 290bdcc2cd1SJ. Bruce Fields struct nfs4_exception exception = { }; 2914bdf87ebSChristoph Hellwig struct nfs_lock_context *lock; 292306a5549SJ. Bruce Fields loff_t err; 293bdcc2cd1SJ. Bruce Fields 2944bdf87ebSChristoph Hellwig lock = nfs_get_lock_context(nfs_file_open_context(filep)); 2954bdf87ebSChristoph Hellwig if (IS_ERR(lock)) 2964bdf87ebSChristoph Hellwig return PTR_ERR(lock); 2974bdf87ebSChristoph Hellwig 2984bdf87ebSChristoph Hellwig exception.inode = file_inode(filep); 2994bdf87ebSChristoph Hellwig exception.state = lock->open_context->state; 3004bdf87ebSChristoph Hellwig 301bdcc2cd1SJ. Bruce Fields do { 3024bdf87ebSChristoph Hellwig err = _nfs42_proc_llseek(filep, lock, offset, whence); 303306a5549SJ. Bruce Fields if (err >= 0) 304306a5549SJ. Bruce Fields break; 3054bdf87ebSChristoph Hellwig if (err == -ENOTSUPP) { 3064bdf87ebSChristoph Hellwig err = -EOPNOTSUPP; 3074bdf87ebSChristoph Hellwig break; 3084bdf87ebSChristoph Hellwig } 309bdcc2cd1SJ. Bruce Fields err = nfs4_handle_exception(server, err, &exception); 310bdcc2cd1SJ. Bruce Fields } while (exception.retry); 311bdcc2cd1SJ. Bruce Fields 3124bdf87ebSChristoph Hellwig nfs_put_lock_context(lock); 313bdcc2cd1SJ. Bruce Fields return err; 314bdcc2cd1SJ. Bruce Fields } 315bdcc2cd1SJ. Bruce Fields 316bdcc2cd1SJ. Bruce Fields 3171b4a4bd8SPeng Tao static void 3181b4a4bd8SPeng Tao nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata) 3191b4a4bd8SPeng Tao { 3201b4a4bd8SPeng Tao struct nfs42_layoutstat_data *data = calldata; 3219a0fe867STrond Myklebust struct inode *inode = data->inode; 3229a0fe867STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 3239a0fe867STrond Myklebust struct pnfs_layout_hdr *lo; 3241b4a4bd8SPeng Tao 3259a0fe867STrond Myklebust spin_lock(&inode->i_lock); 3269a0fe867STrond Myklebust lo = NFS_I(inode)->layout; 3279a0fe867STrond Myklebust if (!pnfs_layout_is_valid(lo)) { 3289a0fe867STrond Myklebust spin_unlock(&inode->i_lock); 3299a0fe867STrond Myklebust rpc_exit(task, 0); 3309a0fe867STrond Myklebust return; 3319a0fe867STrond Myklebust } 3329a0fe867STrond Myklebust nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid); 3339a0fe867STrond Myklebust spin_unlock(&inode->i_lock); 3341b4a4bd8SPeng Tao nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args, 3351b4a4bd8SPeng Tao &data->res.seq_res, task); 3369a0fe867STrond Myklebust 3371b4a4bd8SPeng Tao } 3381b4a4bd8SPeng Tao 3391b4a4bd8SPeng Tao static void 3401b4a4bd8SPeng Tao nfs42_layoutstat_done(struct rpc_task *task, void *calldata) 3411b4a4bd8SPeng Tao { 3421b4a4bd8SPeng Tao struct nfs42_layoutstat_data *data = calldata; 34368d264cfSPeng Tao struct inode *inode = data->inode; 34468d264cfSPeng Tao struct pnfs_layout_hdr *lo; 3451b4a4bd8SPeng Tao 3461b4a4bd8SPeng Tao if (!nfs4_sequence_done(task, &data->res.seq_res)) 3471b4a4bd8SPeng Tao return; 3481b4a4bd8SPeng Tao 3496c5a0d89STrond Myklebust switch (task->tk_status) { 3506c5a0d89STrond Myklebust case 0: 3516c5a0d89STrond Myklebust break; 35268d264cfSPeng Tao case -NFS4ERR_EXPIRED: 353206b3bb5STrond Myklebust case -NFS4ERR_ADMIN_REVOKED: 354206b3bb5STrond Myklebust case -NFS4ERR_DELEG_REVOKED: 35568d264cfSPeng Tao case -NFS4ERR_STALE_STATEID: 35668d264cfSPeng Tao case -NFS4ERR_BAD_STATEID: 35768d264cfSPeng Tao spin_lock(&inode->i_lock); 35868d264cfSPeng Tao lo = NFS_I(inode)->layout; 3599a0fe867STrond Myklebust if (pnfs_layout_is_valid(lo) && 3609a0fe867STrond Myklebust nfs4_stateid_match(&data->args.stateid, 36168d264cfSPeng Tao &lo->plh_stateid)) { 36268d264cfSPeng Tao LIST_HEAD(head); 36368d264cfSPeng Tao 36468d264cfSPeng Tao /* 36568d264cfSPeng Tao * Mark the bad layout state as invalid, then retry 36668d264cfSPeng Tao * with the current stateid. 36768d264cfSPeng Tao */ 3685f46be04STrond Myklebust pnfs_mark_layout_stateid_invalid(lo, &head); 36968d264cfSPeng Tao spin_unlock(&inode->i_lock); 37068d264cfSPeng Tao pnfs_free_lseg_list(&head); 37168d264cfSPeng Tao } else 37268d264cfSPeng Tao spin_unlock(&inode->i_lock); 37368d264cfSPeng Tao break; 3749a0fe867STrond Myklebust case -NFS4ERR_OLD_STATEID: 3759a0fe867STrond Myklebust spin_lock(&inode->i_lock); 3769a0fe867STrond Myklebust lo = NFS_I(inode)->layout; 3779a0fe867STrond Myklebust if (pnfs_layout_is_valid(lo) && 3789a0fe867STrond Myklebust nfs4_stateid_match_other(&data->args.stateid, 3799a0fe867STrond Myklebust &lo->plh_stateid)) { 3809a0fe867STrond Myklebust /* Do we need to delay before resending? */ 3819a0fe867STrond Myklebust if (!nfs4_stateid_is_newer(&lo->plh_stateid, 3829a0fe867STrond Myklebust &data->args.stateid)) 3839a0fe867STrond Myklebust rpc_delay(task, HZ); 3849a0fe867STrond Myklebust rpc_restart_call_prepare(task); 3859a0fe867STrond Myklebust } 3869a0fe867STrond Myklebust spin_unlock(&inode->i_lock); 3879a0fe867STrond Myklebust break; 3886c5a0d89STrond Myklebust case -ENOTSUPP: 3896c5a0d89STrond Myklebust case -EOPNOTSUPP: 39068d264cfSPeng Tao NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS; 3911b4a4bd8SPeng Tao } 39268d264cfSPeng Tao 39368d264cfSPeng Tao dprintk("%s server returns %d\n", __func__, task->tk_status); 3946c5a0d89STrond Myklebust } 3951b4a4bd8SPeng Tao 3961b4a4bd8SPeng Tao static void 3971b4a4bd8SPeng Tao nfs42_layoutstat_release(void *calldata) 3981b4a4bd8SPeng Tao { 3991b4a4bd8SPeng Tao struct nfs42_layoutstat_data *data = calldata; 400422c93c8STrond Myklebust struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo; 401422c93c8STrond Myklebust int i; 4028733408dSPeng Tao 403422c93c8STrond Myklebust for (i = 0; i < data->args.num_dev; i++) { 404422c93c8STrond Myklebust if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free) 405422c93c8STrond Myklebust devinfo[i].ld_private.ops->free(&devinfo[i].ld_private); 406422c93c8STrond Myklebust } 4071b4a4bd8SPeng Tao 4081b4a4bd8SPeng Tao pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout); 4091bfe3b25SPeng Tao smp_mb__before_atomic(); 4101bfe3b25SPeng Tao clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags); 4111bfe3b25SPeng Tao smp_mb__after_atomic(); 4121b4a4bd8SPeng Tao nfs_iput_and_deactive(data->inode); 4131b4a4bd8SPeng Tao kfree(data->args.devinfo); 4141b4a4bd8SPeng Tao kfree(data); 4151b4a4bd8SPeng Tao } 4161b4a4bd8SPeng Tao 417be3a5d23STrond Myklebust static const struct rpc_call_ops nfs42_layoutstat_ops = { 4181b4a4bd8SPeng Tao .rpc_call_prepare = nfs42_layoutstat_prepare, 4191b4a4bd8SPeng Tao .rpc_call_done = nfs42_layoutstat_done, 4201b4a4bd8SPeng Tao .rpc_release = nfs42_layoutstat_release, 421be3a5d23STrond Myklebust }; 422be3a5d23STrond Myklebust 423be3a5d23STrond Myklebust int nfs42_proc_layoutstats_generic(struct nfs_server *server, 424be3a5d23STrond Myklebust struct nfs42_layoutstat_data *data) 425be3a5d23STrond Myklebust { 426be3a5d23STrond Myklebust struct rpc_message msg = { 427be3a5d23STrond Myklebust .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS], 428be3a5d23STrond Myklebust .rpc_argp = &data->args, 429be3a5d23STrond Myklebust .rpc_resp = &data->res, 430be3a5d23STrond Myklebust }; 431be3a5d23STrond Myklebust struct rpc_task_setup task_setup = { 432be3a5d23STrond Myklebust .rpc_client = server->client, 433be3a5d23STrond Myklebust .rpc_message = &msg, 434be3a5d23STrond Myklebust .callback_ops = &nfs42_layoutstat_ops, 435be3a5d23STrond Myklebust .callback_data = data, 436be3a5d23STrond Myklebust .flags = RPC_TASK_ASYNC, 437be3a5d23STrond Myklebust }; 438be3a5d23STrond Myklebust struct rpc_task *task; 439be3a5d23STrond Myklebust 4401b4a4bd8SPeng Tao data->inode = nfs_igrab_and_active(data->args.inode); 4411b4a4bd8SPeng Tao if (!data->inode) { 4421b4a4bd8SPeng Tao nfs42_layoutstat_release(data); 4431b4a4bd8SPeng Tao return -EAGAIN; 4441b4a4bd8SPeng Tao } 445be3a5d23STrond Myklebust nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); 446be3a5d23STrond Myklebust task = rpc_run_task(&task_setup); 447be3a5d23STrond Myklebust if (IS_ERR(task)) 448be3a5d23STrond Myklebust return PTR_ERR(task); 4493f807e5aSJeff Layton rpc_put_task(task); 450be3a5d23STrond Myklebust return 0; 451be3a5d23STrond Myklebust } 452e5341f3aSPeng Tao 453e5341f3aSPeng Tao static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f, 4544bdf87ebSChristoph Hellwig struct file *dst_f, struct nfs_lock_context *src_lock, 4554bdf87ebSChristoph Hellwig struct nfs_lock_context *dst_lock, loff_t src_offset, 456e5341f3aSPeng Tao loff_t dst_offset, loff_t count) 457e5341f3aSPeng Tao { 458e5341f3aSPeng Tao struct inode *src_inode = file_inode(src_f); 459e5341f3aSPeng Tao struct inode *dst_inode = file_inode(dst_f); 460e5341f3aSPeng Tao struct nfs_server *server = NFS_SERVER(dst_inode); 461e5341f3aSPeng Tao struct nfs42_clone_args args = { 462e5341f3aSPeng Tao .src_fh = NFS_FH(src_inode), 463e5341f3aSPeng Tao .dst_fh = NFS_FH(dst_inode), 464e5341f3aSPeng Tao .src_offset = src_offset, 465e5341f3aSPeng Tao .dst_offset = dst_offset, 4669494b2ceSChristoph Hellwig .count = count, 467e5341f3aSPeng Tao .dst_bitmask = server->cache_consistency_bitmask, 468e5341f3aSPeng Tao }; 469e5341f3aSPeng Tao struct nfs42_clone_res res = { 470e5341f3aSPeng Tao .server = server, 471e5341f3aSPeng Tao }; 472e5341f3aSPeng Tao int status; 473e5341f3aSPeng Tao 474e5341f3aSPeng Tao msg->rpc_argp = &args; 475e5341f3aSPeng Tao msg->rpc_resp = &res; 476e5341f3aSPeng Tao 4774bdf87ebSChristoph Hellwig status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context, 4784bdf87ebSChristoph Hellwig src_lock, FMODE_READ); 479e5341f3aSPeng Tao if (status) 480e5341f3aSPeng Tao return status; 481e5341f3aSPeng Tao 4824bdf87ebSChristoph Hellwig status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context, 4834bdf87ebSChristoph Hellwig dst_lock, FMODE_WRITE); 484e5341f3aSPeng Tao if (status) 485e5341f3aSPeng Tao return status; 486e5341f3aSPeng Tao 487e5341f3aSPeng Tao res.dst_fattr = nfs_alloc_fattr(); 488e5341f3aSPeng Tao if (!res.dst_fattr) 489e5341f3aSPeng Tao return -ENOMEM; 490e5341f3aSPeng Tao 491e5341f3aSPeng Tao status = nfs4_call_sync(server->client, server, msg, 492e5341f3aSPeng Tao &args.seq_args, &res.seq_res, 0); 493e5341f3aSPeng Tao if (status == 0) 494e5341f3aSPeng Tao status = nfs_post_op_update_inode(dst_inode, res.dst_fattr); 495e5341f3aSPeng Tao 496e5341f3aSPeng Tao kfree(res.dst_fattr); 497e5341f3aSPeng Tao return status; 498e5341f3aSPeng Tao } 499e5341f3aSPeng Tao 500e5341f3aSPeng Tao int nfs42_proc_clone(struct file *src_f, struct file *dst_f, 501e5341f3aSPeng Tao loff_t src_offset, loff_t dst_offset, loff_t count) 502e5341f3aSPeng Tao { 503e5341f3aSPeng Tao struct rpc_message msg = { 504e5341f3aSPeng Tao .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE], 505e5341f3aSPeng Tao }; 506e5341f3aSPeng Tao struct inode *inode = file_inode(src_f); 507e5341f3aSPeng Tao struct nfs_server *server = NFS_SERVER(file_inode(src_f)); 5084bdf87ebSChristoph Hellwig struct nfs_lock_context *src_lock; 5094bdf87ebSChristoph Hellwig struct nfs_lock_context *dst_lock; 5104bdf87ebSChristoph Hellwig struct nfs4_exception src_exception = { }; 5114bdf87ebSChristoph Hellwig struct nfs4_exception dst_exception = { }; 5124bdf87ebSChristoph Hellwig int err, err2; 513e5341f3aSPeng Tao 514e5341f3aSPeng Tao if (!nfs_server_capable(inode, NFS_CAP_CLONE)) 515e5341f3aSPeng Tao return -EOPNOTSUPP; 516e5341f3aSPeng Tao 5174bdf87ebSChristoph Hellwig src_lock = nfs_get_lock_context(nfs_file_open_context(src_f)); 5184bdf87ebSChristoph Hellwig if (IS_ERR(src_lock)) 5194bdf87ebSChristoph Hellwig return PTR_ERR(src_lock); 5204bdf87ebSChristoph Hellwig 5214bdf87ebSChristoph Hellwig src_exception.inode = file_inode(src_f); 5224bdf87ebSChristoph Hellwig src_exception.state = src_lock->open_context->state; 5234bdf87ebSChristoph Hellwig 5244bdf87ebSChristoph Hellwig dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f)); 5254bdf87ebSChristoph Hellwig if (IS_ERR(dst_lock)) { 5264bdf87ebSChristoph Hellwig err = PTR_ERR(dst_lock); 5274bdf87ebSChristoph Hellwig goto out_put_src_lock; 5284bdf87ebSChristoph Hellwig } 5294bdf87ebSChristoph Hellwig 5304bdf87ebSChristoph Hellwig dst_exception.inode = file_inode(dst_f); 5314bdf87ebSChristoph Hellwig dst_exception.state = dst_lock->open_context->state; 5324bdf87ebSChristoph Hellwig 533e5341f3aSPeng Tao do { 5344bdf87ebSChristoph Hellwig err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock, 5354bdf87ebSChristoph Hellwig src_offset, dst_offset, count); 536e5341f3aSPeng Tao if (err == -ENOTSUPP || err == -EOPNOTSUPP) { 537e5341f3aSPeng Tao NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE; 5384bdf87ebSChristoph Hellwig err = -EOPNOTSUPP; 5394bdf87ebSChristoph Hellwig break; 540e5341f3aSPeng Tao } 541e5341f3aSPeng Tao 5424bdf87ebSChristoph Hellwig err2 = nfs4_handle_exception(server, err, &src_exception); 5434bdf87ebSChristoph Hellwig err = nfs4_handle_exception(server, err, &dst_exception); 5444bdf87ebSChristoph Hellwig if (!err) 5454bdf87ebSChristoph Hellwig err = err2; 5464bdf87ebSChristoph Hellwig } while (src_exception.retry || dst_exception.retry); 5474bdf87ebSChristoph Hellwig 5484bdf87ebSChristoph Hellwig nfs_put_lock_context(dst_lock); 5494bdf87ebSChristoph Hellwig out_put_src_lock: 5504bdf87ebSChristoph Hellwig nfs_put_lock_context(src_lock); 551e5341f3aSPeng Tao return err; 552e5341f3aSPeng Tao } 553