nfs42proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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_proc_fallocate(struct rpc_message *msg, struct file *filep,
  18. struct nfs_lock_context *lock, loff_t offset, loff_t len)
  19. {
  20. struct inode *inode = file_inode(filep);
  21. struct nfs_server *server = NFS_SERVER(inode);
  22. struct nfs42_falloc_args args = {
  23. .falloc_fh = NFS_FH(inode),
  24. .falloc_offset = offset,
  25. .falloc_length = len,
  26. .falloc_bitmask = server->cache_consistency_bitmask,
  27. };
  28. struct nfs42_falloc_res res = {
  29. .falloc_server = server,
  30. };
  31. int status;
  32. msg->rpc_argp = &args;
  33. msg->rpc_resp = &res;
  34. status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
  35. lock, FMODE_WRITE);
  36. if (status)
  37. return status;
  38. res.falloc_fattr = nfs_alloc_fattr();
  39. if (!res.falloc_fattr)
  40. return -ENOMEM;
  41. status = nfs4_call_sync(server->client, server, msg,
  42. &args.seq_args, &res.seq_res, 0);
  43. if (status == 0)
  44. status = nfs_post_op_update_inode(inode, res.falloc_fattr);
  45. kfree(res.falloc_fattr);
  46. return status;
  47. }
  48. static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
  49. loff_t offset, loff_t len)
  50. {
  51. struct nfs_server *server = NFS_SERVER(file_inode(filep));
  52. struct nfs4_exception exception = { };
  53. struct nfs_lock_context *lock;
  54. int err;
  55. lock = nfs_get_lock_context(nfs_file_open_context(filep));
  56. if (IS_ERR(lock))
  57. return PTR_ERR(lock);
  58. exception.inode = file_inode(filep);
  59. exception.state = lock->open_context->state;
  60. do {
  61. err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
  62. if (err == -ENOTSUPP) {
  63. err = -EOPNOTSUPP;
  64. break;
  65. }
  66. err = nfs4_handle_exception(server, err, &exception);
  67. } while (exception.retry);
  68. nfs_put_lock_context(lock);
  69. return err;
  70. }
  71. int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
  72. {
  73. struct rpc_message msg = {
  74. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
  75. };
  76. struct inode *inode = file_inode(filep);
  77. int err;
  78. if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
  79. return -EOPNOTSUPP;
  80. inode_lock(inode);
  81. err = nfs42_proc_fallocate(&msg, filep, offset, len);
  82. if (err == -EOPNOTSUPP)
  83. NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
  84. inode_unlock(inode);
  85. return err;
  86. }
  87. int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
  88. {
  89. struct rpc_message msg = {
  90. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
  91. };
  92. struct inode *inode = file_inode(filep);
  93. int err;
  94. if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
  95. return -EOPNOTSUPP;
  96. inode_lock(inode);
  97. err = nfs_sync_inode(inode);
  98. if (err)
  99. goto out_unlock;
  100. err = nfs42_proc_fallocate(&msg, filep, offset, len);
  101. if (err == 0)
  102. truncate_pagecache_range(inode, offset, (offset + len) -1);
  103. if (err == -EOPNOTSUPP)
  104. NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
  105. out_unlock:
  106. inode_unlock(inode);
  107. return err;
  108. }
  109. static ssize_t _nfs42_proc_copy(struct file *src,
  110. struct nfs_lock_context *src_lock,
  111. struct file *dst,
  112. struct nfs_lock_context *dst_lock,
  113. struct nfs42_copy_args *args,
  114. struct nfs42_copy_res *res)
  115. {
  116. struct rpc_message msg = {
  117. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
  118. .rpc_argp = args,
  119. .rpc_resp = res,
  120. };
  121. struct inode *dst_inode = file_inode(dst);
  122. struct nfs_server *server = NFS_SERVER(dst_inode);
  123. loff_t pos_src = args->src_pos;
  124. loff_t pos_dst = args->dst_pos;
  125. size_t count = args->count;
  126. int status;
  127. status = nfs4_set_rw_stateid(&args->src_stateid, src_lock->open_context,
  128. src_lock, FMODE_READ);
  129. if (status)
  130. return status;
  131. status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
  132. pos_src, pos_src + (loff_t)count - 1);
  133. if (status)
  134. return status;
  135. status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
  136. dst_lock, FMODE_WRITE);
  137. if (status)
  138. return status;
  139. status = nfs_sync_inode(dst_inode);
  140. if (status)
  141. return status;
  142. status = nfs4_call_sync(server->client, server, &msg,
  143. &args->seq_args, &res->seq_res, 0);
  144. if (status == -ENOTSUPP)
  145. server->caps &= ~NFS_CAP_COPY;
  146. if (status)
  147. return status;
  148. if (res->write_res.verifier.committed != NFS_FILE_SYNC) {
  149. status = nfs_commit_file(dst, &res->write_res.verifier.verifier);
  150. if (status)
  151. return status;
  152. }
  153. truncate_pagecache_range(dst_inode, pos_dst,
  154. pos_dst + res->write_res.count);
  155. return res->write_res.count;
  156. }
  157. ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
  158. struct file *dst, loff_t pos_dst,
  159. size_t count)
  160. {
  161. struct nfs_server *server = NFS_SERVER(file_inode(dst));
  162. struct nfs_lock_context *src_lock;
  163. struct nfs_lock_context *dst_lock;
  164. struct nfs42_copy_args args = {
  165. .src_fh = NFS_FH(file_inode(src)),
  166. .src_pos = pos_src,
  167. .dst_fh = NFS_FH(file_inode(dst)),
  168. .dst_pos = pos_dst,
  169. .count = count,
  170. };
  171. struct nfs42_copy_res res;
  172. struct nfs4_exception src_exception = {
  173. .inode = file_inode(src),
  174. .stateid = &args.src_stateid,
  175. };
  176. struct nfs4_exception dst_exception = {
  177. .inode = file_inode(dst),
  178. .stateid = &args.dst_stateid,
  179. };
  180. ssize_t err, err2;
  181. if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY))
  182. return -EOPNOTSUPP;
  183. src_lock = nfs_get_lock_context(nfs_file_open_context(src));
  184. if (IS_ERR(src_lock))
  185. return PTR_ERR(src_lock);
  186. src_exception.state = src_lock->open_context->state;
  187. dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
  188. if (IS_ERR(dst_lock)) {
  189. err = PTR_ERR(dst_lock);
  190. goto out_put_src_lock;
  191. }
  192. dst_exception.state = dst_lock->open_context->state;
  193. do {
  194. inode_lock(file_inode(dst));
  195. err = _nfs42_proc_copy(src, src_lock,
  196. dst, dst_lock,
  197. &args, &res);
  198. inode_unlock(file_inode(dst));
  199. if (err >= 0)
  200. break;
  201. if (err == -ENOTSUPP) {
  202. err = -EOPNOTSUPP;
  203. break;
  204. }
  205. err2 = nfs4_handle_exception(server, err, &src_exception);
  206. err = nfs4_handle_exception(server, err, &dst_exception);
  207. if (!err)
  208. err = err2;
  209. } while (src_exception.retry || dst_exception.retry);
  210. nfs_put_lock_context(dst_lock);
  211. out_put_src_lock:
  212. nfs_put_lock_context(src_lock);
  213. return err;
  214. }
  215. static loff_t _nfs42_proc_llseek(struct file *filep,
  216. struct nfs_lock_context *lock, loff_t offset, int whence)
  217. {
  218. struct inode *inode = file_inode(filep);
  219. struct nfs42_seek_args args = {
  220. .sa_fh = NFS_FH(inode),
  221. .sa_offset = offset,
  222. .sa_what = (whence == SEEK_HOLE) ?
  223. NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
  224. };
  225. struct nfs42_seek_res res;
  226. struct rpc_message msg = {
  227. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
  228. .rpc_argp = &args,
  229. .rpc_resp = &res,
  230. };
  231. struct nfs_server *server = NFS_SERVER(inode);
  232. int status;
  233. if (!nfs_server_capable(inode, NFS_CAP_SEEK))
  234. return -ENOTSUPP;
  235. status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
  236. lock, FMODE_READ);
  237. if (status)
  238. return status;
  239. status = nfs_filemap_write_and_wait_range(inode->i_mapping,
  240. offset, LLONG_MAX);
  241. if (status)
  242. return status;
  243. status = nfs4_call_sync(server->client, server, &msg,
  244. &args.seq_args, &res.seq_res, 0);
  245. if (status == -ENOTSUPP)
  246. server->caps &= ~NFS_CAP_SEEK;
  247. if (status)
  248. return status;
  249. return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
  250. }
  251. loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
  252. {
  253. struct nfs_server *server = NFS_SERVER(file_inode(filep));
  254. struct nfs4_exception exception = { };
  255. struct nfs_lock_context *lock;
  256. loff_t err;
  257. lock = nfs_get_lock_context(nfs_file_open_context(filep));
  258. if (IS_ERR(lock))
  259. return PTR_ERR(lock);
  260. exception.inode = file_inode(filep);
  261. exception.state = lock->open_context->state;
  262. do {
  263. err = _nfs42_proc_llseek(filep, lock, offset, whence);
  264. if (err >= 0)
  265. break;
  266. if (err == -ENOTSUPP) {
  267. err = -EOPNOTSUPP;
  268. break;
  269. }
  270. err = nfs4_handle_exception(server, err, &exception);
  271. } while (exception.retry);
  272. nfs_put_lock_context(lock);
  273. return err;
  274. }
  275. static void
  276. nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
  277. {
  278. struct nfs42_layoutstat_data *data = calldata;
  279. struct inode *inode = data->inode;
  280. struct nfs_server *server = NFS_SERVER(inode);
  281. struct pnfs_layout_hdr *lo;
  282. spin_lock(&inode->i_lock);
  283. lo = NFS_I(inode)->layout;
  284. if (!pnfs_layout_is_valid(lo)) {
  285. spin_unlock(&inode->i_lock);
  286. rpc_exit(task, 0);
  287. return;
  288. }
  289. nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
  290. spin_unlock(&inode->i_lock);
  291. nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args,
  292. &data->res.seq_res, task);
  293. }
  294. static void
  295. nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
  296. {
  297. struct nfs42_layoutstat_data *data = calldata;
  298. struct inode *inode = data->inode;
  299. struct pnfs_layout_hdr *lo;
  300. if (!nfs4_sequence_done(task, &data->res.seq_res))
  301. return;
  302. switch (task->tk_status) {
  303. case 0:
  304. break;
  305. case -NFS4ERR_EXPIRED:
  306. case -NFS4ERR_ADMIN_REVOKED:
  307. case -NFS4ERR_DELEG_REVOKED:
  308. case -NFS4ERR_STALE_STATEID:
  309. case -NFS4ERR_BAD_STATEID:
  310. spin_lock(&inode->i_lock);
  311. lo = NFS_I(inode)->layout;
  312. if (pnfs_layout_is_valid(lo) &&
  313. nfs4_stateid_match(&data->args.stateid,
  314. &lo->plh_stateid)) {
  315. LIST_HEAD(head);
  316. /*
  317. * Mark the bad layout state as invalid, then retry
  318. * with the current stateid.
  319. */
  320. pnfs_mark_layout_stateid_invalid(lo, &head);
  321. spin_unlock(&inode->i_lock);
  322. pnfs_free_lseg_list(&head);
  323. } else
  324. spin_unlock(&inode->i_lock);
  325. break;
  326. case -NFS4ERR_OLD_STATEID:
  327. spin_lock(&inode->i_lock);
  328. lo = NFS_I(inode)->layout;
  329. if (pnfs_layout_is_valid(lo) &&
  330. nfs4_stateid_match_other(&data->args.stateid,
  331. &lo->plh_stateid)) {
  332. /* Do we need to delay before resending? */
  333. if (!nfs4_stateid_is_newer(&lo->plh_stateid,
  334. &data->args.stateid))
  335. rpc_delay(task, HZ);
  336. rpc_restart_call_prepare(task);
  337. }
  338. spin_unlock(&inode->i_lock);
  339. break;
  340. case -ENOTSUPP:
  341. case -EOPNOTSUPP:
  342. NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
  343. }
  344. dprintk("%s server returns %d\n", __func__, task->tk_status);
  345. }
  346. static void
  347. nfs42_layoutstat_release(void *calldata)
  348. {
  349. struct nfs42_layoutstat_data *data = calldata;
  350. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  351. if (nfss->pnfs_curr_ld->cleanup_layoutstats)
  352. nfss->pnfs_curr_ld->cleanup_layoutstats(data);
  353. pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
  354. smp_mb__before_atomic();
  355. clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
  356. smp_mb__after_atomic();
  357. nfs_iput_and_deactive(data->inode);
  358. kfree(data->args.devinfo);
  359. kfree(data);
  360. }
  361. static const struct rpc_call_ops nfs42_layoutstat_ops = {
  362. .rpc_call_prepare = nfs42_layoutstat_prepare,
  363. .rpc_call_done = nfs42_layoutstat_done,
  364. .rpc_release = nfs42_layoutstat_release,
  365. };
  366. int nfs42_proc_layoutstats_generic(struct nfs_server *server,
  367. struct nfs42_layoutstat_data *data)
  368. {
  369. struct rpc_message msg = {
  370. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
  371. .rpc_argp = &data->args,
  372. .rpc_resp = &data->res,
  373. };
  374. struct rpc_task_setup task_setup = {
  375. .rpc_client = server->client,
  376. .rpc_message = &msg,
  377. .callback_ops = &nfs42_layoutstat_ops,
  378. .callback_data = data,
  379. .flags = RPC_TASK_ASYNC,
  380. };
  381. struct rpc_task *task;
  382. data->inode = nfs_igrab_and_active(data->args.inode);
  383. if (!data->inode) {
  384. nfs42_layoutstat_release(data);
  385. return -EAGAIN;
  386. }
  387. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  388. task = rpc_run_task(&task_setup);
  389. if (IS_ERR(task))
  390. return PTR_ERR(task);
  391. rpc_put_task(task);
  392. return 0;
  393. }
  394. static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
  395. struct file *dst_f, struct nfs_lock_context *src_lock,
  396. struct nfs_lock_context *dst_lock, loff_t src_offset,
  397. loff_t dst_offset, loff_t count)
  398. {
  399. struct inode *src_inode = file_inode(src_f);
  400. struct inode *dst_inode = file_inode(dst_f);
  401. struct nfs_server *server = NFS_SERVER(dst_inode);
  402. struct nfs42_clone_args args = {
  403. .src_fh = NFS_FH(src_inode),
  404. .dst_fh = NFS_FH(dst_inode),
  405. .src_offset = src_offset,
  406. .dst_offset = dst_offset,
  407. .count = count,
  408. .dst_bitmask = server->cache_consistency_bitmask,
  409. };
  410. struct nfs42_clone_res res = {
  411. .server = server,
  412. };
  413. int status;
  414. msg->rpc_argp = &args;
  415. msg->rpc_resp = &res;
  416. status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
  417. src_lock, FMODE_READ);
  418. if (status)
  419. return status;
  420. status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
  421. dst_lock, FMODE_WRITE);
  422. if (status)
  423. return status;
  424. res.dst_fattr = nfs_alloc_fattr();
  425. if (!res.dst_fattr)
  426. return -ENOMEM;
  427. status = nfs4_call_sync(server->client, server, msg,
  428. &args.seq_args, &res.seq_res, 0);
  429. if (status == 0)
  430. status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
  431. kfree(res.dst_fattr);
  432. return status;
  433. }
  434. int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
  435. loff_t src_offset, loff_t dst_offset, loff_t count)
  436. {
  437. struct rpc_message msg = {
  438. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
  439. };
  440. struct inode *inode = file_inode(src_f);
  441. struct nfs_server *server = NFS_SERVER(file_inode(src_f));
  442. struct nfs_lock_context *src_lock;
  443. struct nfs_lock_context *dst_lock;
  444. struct nfs4_exception src_exception = { };
  445. struct nfs4_exception dst_exception = { };
  446. int err, err2;
  447. if (!nfs_server_capable(inode, NFS_CAP_CLONE))
  448. return -EOPNOTSUPP;
  449. src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
  450. if (IS_ERR(src_lock))
  451. return PTR_ERR(src_lock);
  452. src_exception.inode = file_inode(src_f);
  453. src_exception.state = src_lock->open_context->state;
  454. dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
  455. if (IS_ERR(dst_lock)) {
  456. err = PTR_ERR(dst_lock);
  457. goto out_put_src_lock;
  458. }
  459. dst_exception.inode = file_inode(dst_f);
  460. dst_exception.state = dst_lock->open_context->state;
  461. do {
  462. err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
  463. src_offset, dst_offset, count);
  464. if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
  465. NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
  466. err = -EOPNOTSUPP;
  467. break;
  468. }
  469. err2 = nfs4_handle_exception(server, err, &src_exception);
  470. err = nfs4_handle_exception(server, err, &dst_exception);
  471. if (!err)
  472. err = err2;
  473. } while (src_exception.retry || dst_exception.retry);
  474. nfs_put_lock_context(dst_lock);
  475. out_put_src_lock:
  476. nfs_put_lock_context(src_lock);
  477. return err;
  478. }