nfs42proc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #include "iostat.h"
  14. #include "pnfs.h"
  15. #include "internal.h"
  16. #define NFSDBG_FACILITY NFSDBG_PROC
  17. static int nfs42_set_rw_stateid(nfs4_stateid *dst, struct file *file,
  18. fmode_t fmode)
  19. {
  20. struct nfs_open_context *open;
  21. struct nfs_lock_context *lock;
  22. int ret;
  23. open = get_nfs_open_context(nfs_file_open_context(file));
  24. lock = nfs_get_lock_context(open);
  25. if (IS_ERR(lock)) {
  26. put_nfs_open_context(open);
  27. return PTR_ERR(lock);
  28. }
  29. ret = nfs4_set_rw_stateid(dst, open, lock, fmode);
  30. nfs_put_lock_context(lock);
  31. put_nfs_open_context(open);
  32. return ret;
  33. }
  34. static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
  35. loff_t offset, loff_t len)
  36. {
  37. struct inode *inode = file_inode(filep);
  38. struct nfs_server *server = NFS_SERVER(inode);
  39. struct nfs42_falloc_args args = {
  40. .falloc_fh = NFS_FH(inode),
  41. .falloc_offset = offset,
  42. .falloc_length = len,
  43. .falloc_bitmask = server->cache_consistency_bitmask,
  44. };
  45. struct nfs42_falloc_res res = {
  46. .falloc_server = server,
  47. };
  48. int status;
  49. msg->rpc_argp = &args;
  50. msg->rpc_resp = &res;
  51. status = nfs42_set_rw_stateid(&args.falloc_stateid, filep, FMODE_WRITE);
  52. if (status)
  53. return status;
  54. res.falloc_fattr = nfs_alloc_fattr();
  55. if (!res.falloc_fattr)
  56. return -ENOMEM;
  57. status = nfs4_call_sync(server->client, server, msg,
  58. &args.seq_args, &res.seq_res, 0);
  59. if (status == 0)
  60. status = nfs_post_op_update_inode(inode, res.falloc_fattr);
  61. kfree(res.falloc_fattr);
  62. return status;
  63. }
  64. static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
  65. loff_t offset, loff_t len)
  66. {
  67. struct nfs_server *server = NFS_SERVER(file_inode(filep));
  68. struct nfs4_exception exception = { };
  69. int err;
  70. do {
  71. err = _nfs42_proc_fallocate(msg, filep, offset, len);
  72. if (err == -ENOTSUPP)
  73. return -EOPNOTSUPP;
  74. err = nfs4_handle_exception(server, err, &exception);
  75. } while (exception.retry);
  76. return err;
  77. }
  78. int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
  79. {
  80. struct rpc_message msg = {
  81. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
  82. };
  83. struct inode *inode = file_inode(filep);
  84. int err;
  85. if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
  86. return -EOPNOTSUPP;
  87. inode_lock(inode);
  88. err = nfs42_proc_fallocate(&msg, filep, offset, len);
  89. if (err == -EOPNOTSUPP)
  90. NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
  91. inode_unlock(inode);
  92. return err;
  93. }
  94. int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
  95. {
  96. struct rpc_message msg = {
  97. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
  98. };
  99. struct inode *inode = file_inode(filep);
  100. int err;
  101. if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
  102. return -EOPNOTSUPP;
  103. nfs_wb_all(inode);
  104. inode_lock(inode);
  105. err = nfs42_proc_fallocate(&msg, filep, offset, len);
  106. if (err == 0)
  107. truncate_pagecache_range(inode, offset, (offset + len) -1);
  108. if (err == -EOPNOTSUPP)
  109. NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
  110. inode_unlock(inode);
  111. return err;
  112. }
  113. static loff_t _nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
  114. {
  115. struct inode *inode = file_inode(filep);
  116. struct nfs42_seek_args args = {
  117. .sa_fh = NFS_FH(inode),
  118. .sa_offset = offset,
  119. .sa_what = (whence == SEEK_HOLE) ?
  120. NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
  121. };
  122. struct nfs42_seek_res res;
  123. struct rpc_message msg = {
  124. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
  125. .rpc_argp = &args,
  126. .rpc_resp = &res,
  127. };
  128. struct nfs_server *server = NFS_SERVER(inode);
  129. int status;
  130. if (!nfs_server_capable(inode, NFS_CAP_SEEK))
  131. return -ENOTSUPP;
  132. status = nfs42_set_rw_stateid(&args.sa_stateid, filep, FMODE_READ);
  133. if (status)
  134. return status;
  135. nfs_wb_all(inode);
  136. status = nfs4_call_sync(server->client, server, &msg,
  137. &args.seq_args, &res.seq_res, 0);
  138. if (status == -ENOTSUPP)
  139. server->caps &= ~NFS_CAP_SEEK;
  140. if (status)
  141. return status;
  142. return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
  143. }
  144. loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
  145. {
  146. struct nfs_server *server = NFS_SERVER(file_inode(filep));
  147. struct nfs4_exception exception = { };
  148. loff_t err;
  149. do {
  150. err = _nfs42_proc_llseek(filep, offset, whence);
  151. if (err >= 0)
  152. break;
  153. if (err == -ENOTSUPP)
  154. return -EOPNOTSUPP;
  155. err = nfs4_handle_exception(server, err, &exception);
  156. } while (exception.retry);
  157. return err;
  158. }
  159. static void
  160. nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
  161. {
  162. struct nfs42_layoutstat_data *data = calldata;
  163. struct nfs_server *server = NFS_SERVER(data->args.inode);
  164. nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args,
  165. &data->res.seq_res, task);
  166. }
  167. static void
  168. nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
  169. {
  170. struct nfs42_layoutstat_data *data = calldata;
  171. struct inode *inode = data->inode;
  172. struct pnfs_layout_hdr *lo;
  173. if (!nfs4_sequence_done(task, &data->res.seq_res))
  174. return;
  175. switch (task->tk_status) {
  176. case 0:
  177. break;
  178. case -NFS4ERR_EXPIRED:
  179. case -NFS4ERR_STALE_STATEID:
  180. case -NFS4ERR_OLD_STATEID:
  181. case -NFS4ERR_BAD_STATEID:
  182. spin_lock(&inode->i_lock);
  183. lo = NFS_I(inode)->layout;
  184. if (lo && nfs4_stateid_match(&data->args.stateid,
  185. &lo->plh_stateid)) {
  186. LIST_HEAD(head);
  187. /*
  188. * Mark the bad layout state as invalid, then retry
  189. * with the current stateid.
  190. */
  191. set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  192. pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
  193. spin_unlock(&inode->i_lock);
  194. pnfs_free_lseg_list(&head);
  195. } else
  196. spin_unlock(&inode->i_lock);
  197. break;
  198. case -ENOTSUPP:
  199. case -EOPNOTSUPP:
  200. NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
  201. default:
  202. break;
  203. }
  204. dprintk("%s server returns %d\n", __func__, task->tk_status);
  205. }
  206. static void
  207. nfs42_layoutstat_release(void *calldata)
  208. {
  209. struct nfs42_layoutstat_data *data = calldata;
  210. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  211. if (nfss->pnfs_curr_ld->cleanup_layoutstats)
  212. nfss->pnfs_curr_ld->cleanup_layoutstats(data);
  213. pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
  214. smp_mb__before_atomic();
  215. clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
  216. smp_mb__after_atomic();
  217. nfs_iput_and_deactive(data->inode);
  218. kfree(data->args.devinfo);
  219. kfree(data);
  220. }
  221. static const struct rpc_call_ops nfs42_layoutstat_ops = {
  222. .rpc_call_prepare = nfs42_layoutstat_prepare,
  223. .rpc_call_done = nfs42_layoutstat_done,
  224. .rpc_release = nfs42_layoutstat_release,
  225. };
  226. int nfs42_proc_layoutstats_generic(struct nfs_server *server,
  227. struct nfs42_layoutstat_data *data)
  228. {
  229. struct rpc_message msg = {
  230. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
  231. .rpc_argp = &data->args,
  232. .rpc_resp = &data->res,
  233. };
  234. struct rpc_task_setup task_setup = {
  235. .rpc_client = server->client,
  236. .rpc_message = &msg,
  237. .callback_ops = &nfs42_layoutstat_ops,
  238. .callback_data = data,
  239. .flags = RPC_TASK_ASYNC,
  240. };
  241. struct rpc_task *task;
  242. data->inode = nfs_igrab_and_active(data->args.inode);
  243. if (!data->inode) {
  244. nfs42_layoutstat_release(data);
  245. return -EAGAIN;
  246. }
  247. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  248. task = rpc_run_task(&task_setup);
  249. if (IS_ERR(task))
  250. return PTR_ERR(task);
  251. return 0;
  252. }
  253. static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
  254. struct file *dst_f, loff_t src_offset,
  255. loff_t dst_offset, loff_t count)
  256. {
  257. struct inode *src_inode = file_inode(src_f);
  258. struct inode *dst_inode = file_inode(dst_f);
  259. struct nfs_server *server = NFS_SERVER(dst_inode);
  260. struct nfs42_clone_args args = {
  261. .src_fh = NFS_FH(src_inode),
  262. .dst_fh = NFS_FH(dst_inode),
  263. .src_offset = src_offset,
  264. .dst_offset = dst_offset,
  265. .count = count,
  266. .dst_bitmask = server->cache_consistency_bitmask,
  267. };
  268. struct nfs42_clone_res res = {
  269. .server = server,
  270. };
  271. int status;
  272. msg->rpc_argp = &args;
  273. msg->rpc_resp = &res;
  274. status = nfs42_set_rw_stateid(&args.src_stateid, src_f, FMODE_READ);
  275. if (status)
  276. return status;
  277. status = nfs42_set_rw_stateid(&args.dst_stateid, dst_f, FMODE_WRITE);
  278. if (status)
  279. return status;
  280. res.dst_fattr = nfs_alloc_fattr();
  281. if (!res.dst_fattr)
  282. return -ENOMEM;
  283. status = nfs4_call_sync(server->client, server, msg,
  284. &args.seq_args, &res.seq_res, 0);
  285. if (status == 0)
  286. status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
  287. kfree(res.dst_fattr);
  288. return status;
  289. }
  290. int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
  291. loff_t src_offset, loff_t dst_offset, loff_t count)
  292. {
  293. struct rpc_message msg = {
  294. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
  295. };
  296. struct inode *inode = file_inode(src_f);
  297. struct nfs_server *server = NFS_SERVER(file_inode(src_f));
  298. struct nfs4_exception exception = { };
  299. int err;
  300. if (!nfs_server_capable(inode, NFS_CAP_CLONE))
  301. return -EOPNOTSUPP;
  302. do {
  303. err = _nfs42_proc_clone(&msg, src_f, dst_f, src_offset,
  304. dst_offset, count);
  305. if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
  306. NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
  307. return -EOPNOTSUPP;
  308. }
  309. err = nfs4_handle_exception(server, err, &exception);
  310. } while (exception.retry);
  311. return err;
  312. }