nfs42proc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. mutex_lock(&inode->i_mutex);
  88. err = nfs42_proc_fallocate(&msg, filep, offset, len);
  89. if (err == -EOPNOTSUPP)
  90. NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
  91. mutex_unlock(&inode->i_mutex);
  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. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. if (!nfs4_sequence_done(task, &data->res.seq_res))
  172. return;
  173. switch (task->tk_status) {
  174. case 0:
  175. break;
  176. case -ENOTSUPP:
  177. case -EOPNOTSUPP:
  178. NFS_SERVER(data->inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
  179. default:
  180. dprintk("%s server returns %d\n", __func__, task->tk_status);
  181. }
  182. }
  183. static void
  184. nfs42_layoutstat_release(void *calldata)
  185. {
  186. struct nfs42_layoutstat_data *data = calldata;
  187. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  188. if (nfss->pnfs_curr_ld->cleanup_layoutstats)
  189. nfss->pnfs_curr_ld->cleanup_layoutstats(data);
  190. pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
  191. smp_mb__before_atomic();
  192. clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
  193. smp_mb__after_atomic();
  194. nfs_iput_and_deactive(data->inode);
  195. kfree(data->args.devinfo);
  196. kfree(data);
  197. }
  198. static const struct rpc_call_ops nfs42_layoutstat_ops = {
  199. .rpc_call_prepare = nfs42_layoutstat_prepare,
  200. .rpc_call_done = nfs42_layoutstat_done,
  201. .rpc_release = nfs42_layoutstat_release,
  202. };
  203. int nfs42_proc_layoutstats_generic(struct nfs_server *server,
  204. struct nfs42_layoutstat_data *data)
  205. {
  206. struct rpc_message msg = {
  207. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
  208. .rpc_argp = &data->args,
  209. .rpc_resp = &data->res,
  210. };
  211. struct rpc_task_setup task_setup = {
  212. .rpc_client = server->client,
  213. .rpc_message = &msg,
  214. .callback_ops = &nfs42_layoutstat_ops,
  215. .callback_data = data,
  216. .flags = RPC_TASK_ASYNC,
  217. };
  218. struct rpc_task *task;
  219. data->inode = nfs_igrab_and_active(data->args.inode);
  220. if (!data->inode) {
  221. nfs42_layoutstat_release(data);
  222. return -EAGAIN;
  223. }
  224. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  225. task = rpc_run_task(&task_setup);
  226. if (IS_ERR(task))
  227. return PTR_ERR(task);
  228. rpc_put_task(task);
  229. return 0;
  230. }
  231. static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
  232. struct file *dst_f, loff_t src_offset,
  233. loff_t dst_offset, loff_t count)
  234. {
  235. struct inode *src_inode = file_inode(src_f);
  236. struct inode *dst_inode = file_inode(dst_f);
  237. struct nfs_server *server = NFS_SERVER(dst_inode);
  238. struct nfs42_clone_args args = {
  239. .src_fh = NFS_FH(src_inode),
  240. .dst_fh = NFS_FH(dst_inode),
  241. .src_offset = src_offset,
  242. .dst_offset = dst_offset,
  243. .count = count,
  244. .dst_bitmask = server->cache_consistency_bitmask,
  245. };
  246. struct nfs42_clone_res res = {
  247. .server = server,
  248. };
  249. int status;
  250. msg->rpc_argp = &args;
  251. msg->rpc_resp = &res;
  252. status = nfs42_set_rw_stateid(&args.src_stateid, src_f, FMODE_READ);
  253. if (status)
  254. return status;
  255. status = nfs42_set_rw_stateid(&args.dst_stateid, dst_f, FMODE_WRITE);
  256. if (status)
  257. return status;
  258. res.dst_fattr = nfs_alloc_fattr();
  259. if (!res.dst_fattr)
  260. return -ENOMEM;
  261. status = nfs4_call_sync(server->client, server, msg,
  262. &args.seq_args, &res.seq_res, 0);
  263. if (status == 0)
  264. status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
  265. kfree(res.dst_fattr);
  266. return status;
  267. }
  268. int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
  269. loff_t src_offset, loff_t dst_offset, loff_t count)
  270. {
  271. struct rpc_message msg = {
  272. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
  273. };
  274. struct inode *inode = file_inode(src_f);
  275. struct nfs_server *server = NFS_SERVER(file_inode(src_f));
  276. struct nfs4_exception exception = { };
  277. int err;
  278. if (!nfs_server_capable(inode, NFS_CAP_CLONE))
  279. return -EOPNOTSUPP;
  280. do {
  281. err = _nfs42_proc_clone(&msg, src_f, dst_f, src_offset,
  282. dst_offset, count);
  283. if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
  284. NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
  285. return -EOPNOTSUPP;
  286. }
  287. err = nfs4_handle_exception(server, err, &exception);
  288. } while (exception.retry);
  289. return err;
  290. }