filelayout.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * Module for the pnfs nfs4 file layout driver.
  3. * Defines all I/O and Policy interface operations, plus code
  4. * to register itself with the pNFS client.
  5. *
  6. * Copyright (c) 2002
  7. * The Regents of the University of Michigan
  8. * All Rights Reserved
  9. *
  10. * Dean Hildebrand <dhildebz@umich.edu>
  11. *
  12. * Permission is granted to use, copy, create derivative works, and
  13. * redistribute this software and such derivative works for any purpose,
  14. * so long as the name of the University of Michigan is not used in
  15. * any advertising or publicity pertaining to the use or distribution
  16. * of this software without specific, written prior authorization. If
  17. * the above copyright notice or any other identification of the
  18. * University of Michigan is included in any copy of any portion of
  19. * this software, then the disclaimer below must also be included.
  20. *
  21. * This software is provided as is, without representation or warranty
  22. * of any kind either express or implied, including without limitation
  23. * the implied warranties of merchantability, fitness for a particular
  24. * purpose, or noninfringement. The Regents of the University of
  25. * Michigan shall not be liable for any damages, including special,
  26. * indirect, incidental, or consequential damages, with respect to any
  27. * claim arising out of or in connection with the use of the software,
  28. * even if it has been or is hereafter advised of the possibility of
  29. * such damages.
  30. */
  31. #include <linux/nfs_fs.h>
  32. #include <linux/nfs_page.h>
  33. #include <linux/module.h>
  34. #include <linux/backing-dev.h>
  35. #include <linux/sunrpc/metrics.h>
  36. #include "../nfs4session.h"
  37. #include "../internal.h"
  38. #include "../delegation.h"
  39. #include "filelayout.h"
  40. #include "../nfs4trace.h"
  41. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  42. MODULE_LICENSE("GPL");
  43. MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
  44. MODULE_DESCRIPTION("The NFSv4 file layout driver");
  45. #define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
  46. static const struct pnfs_commit_ops filelayout_commit_ops;
  47. static loff_t
  48. filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
  49. loff_t offset)
  50. {
  51. u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
  52. u64 stripe_no;
  53. u32 rem;
  54. offset -= flseg->pattern_offset;
  55. stripe_no = div_u64(offset, stripe_width);
  56. div_u64_rem(offset, flseg->stripe_unit, &rem);
  57. return stripe_no * flseg->stripe_unit + rem;
  58. }
  59. /* This function is used by the layout driver to calculate the
  60. * offset of the file on the dserver based on whether the
  61. * layout type is STRIPE_DENSE or STRIPE_SPARSE
  62. */
  63. static loff_t
  64. filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
  65. {
  66. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  67. switch (flseg->stripe_type) {
  68. case STRIPE_SPARSE:
  69. return offset;
  70. case STRIPE_DENSE:
  71. return filelayout_get_dense_offset(flseg, offset);
  72. }
  73. BUG();
  74. }
  75. static void filelayout_reset_write(struct nfs_pgio_header *hdr)
  76. {
  77. struct rpc_task *task = &hdr->task;
  78. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  79. dprintk("%s Reset task %5u for i/o through MDS "
  80. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  81. hdr->task.tk_pid,
  82. hdr->inode->i_sb->s_id,
  83. (unsigned long long)NFS_FILEID(hdr->inode),
  84. hdr->args.count,
  85. (unsigned long long)hdr->args.offset);
  86. task->tk_status = pnfs_write_done_resend_to_mds(hdr);
  87. }
  88. }
  89. static void filelayout_reset_read(struct nfs_pgio_header *hdr)
  90. {
  91. struct rpc_task *task = &hdr->task;
  92. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  93. dprintk("%s Reset task %5u for i/o through MDS "
  94. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  95. hdr->task.tk_pid,
  96. hdr->inode->i_sb->s_id,
  97. (unsigned long long)NFS_FILEID(hdr->inode),
  98. hdr->args.count,
  99. (unsigned long long)hdr->args.offset);
  100. task->tk_status = pnfs_read_done_resend_to_mds(hdr);
  101. }
  102. }
  103. static int filelayout_async_handle_error(struct rpc_task *task,
  104. struct nfs4_state *state,
  105. struct nfs_client *clp,
  106. struct pnfs_layout_segment *lseg)
  107. {
  108. struct pnfs_layout_hdr *lo = lseg->pls_layout;
  109. struct inode *inode = lo->plh_inode;
  110. struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
  111. struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
  112. if (task->tk_status >= 0)
  113. return 0;
  114. switch (task->tk_status) {
  115. /* DS session errors */
  116. case -NFS4ERR_BADSESSION:
  117. case -NFS4ERR_BADSLOT:
  118. case -NFS4ERR_BAD_HIGH_SLOT:
  119. case -NFS4ERR_DEADSESSION:
  120. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  121. case -NFS4ERR_SEQ_FALSE_RETRY:
  122. case -NFS4ERR_SEQ_MISORDERED:
  123. dprintk("%s ERROR %d, Reset session. Exchangeid "
  124. "flags 0x%x\n", __func__, task->tk_status,
  125. clp->cl_exchange_flags);
  126. nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
  127. break;
  128. case -NFS4ERR_DELAY:
  129. case -NFS4ERR_GRACE:
  130. rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
  131. break;
  132. case -NFS4ERR_RETRY_UNCACHED_REP:
  133. break;
  134. /* Invalidate Layout errors */
  135. case -NFS4ERR_ACCESS:
  136. case -NFS4ERR_PNFS_NO_LAYOUT:
  137. case -ESTALE: /* mapped NFS4ERR_STALE */
  138. case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
  139. case -EISDIR: /* mapped NFS4ERR_ISDIR */
  140. case -NFS4ERR_FHEXPIRED:
  141. case -NFS4ERR_WRONG_TYPE:
  142. dprintk("%s Invalid layout error %d\n", __func__,
  143. task->tk_status);
  144. /*
  145. * Destroy layout so new i/o will get a new layout.
  146. * Layout will not be destroyed until all current lseg
  147. * references are put. Mark layout as invalid to resend failed
  148. * i/o and all i/o waiting on the slot table to the MDS until
  149. * layout is destroyed and a new valid layout is obtained.
  150. */
  151. pnfs_destroy_layout(NFS_I(inode));
  152. rpc_wake_up(&tbl->slot_tbl_waitq);
  153. goto reset;
  154. /* RPC connection errors */
  155. case -ECONNREFUSED:
  156. case -EHOSTDOWN:
  157. case -EHOSTUNREACH:
  158. case -ENETUNREACH:
  159. case -EIO:
  160. case -ETIMEDOUT:
  161. case -EPIPE:
  162. dprintk("%s DS connection error %d\n", __func__,
  163. task->tk_status);
  164. nfs4_mark_deviceid_unavailable(devid);
  165. pnfs_error_mark_layout_for_return(inode, lseg);
  166. pnfs_set_lo_fail(lseg);
  167. rpc_wake_up(&tbl->slot_tbl_waitq);
  168. fallthrough;
  169. default:
  170. reset:
  171. dprintk("%s Retry through MDS. Error %d\n", __func__,
  172. task->tk_status);
  173. return -NFS4ERR_RESET_TO_MDS;
  174. }
  175. task->tk_status = 0;
  176. return -EAGAIN;
  177. }
  178. /* NFS_PROTO call done callback routines */
  179. static int filelayout_read_done_cb(struct rpc_task *task,
  180. struct nfs_pgio_header *hdr)
  181. {
  182. int err;
  183. trace_nfs4_pnfs_read(hdr, task->tk_status);
  184. err = filelayout_async_handle_error(task, hdr->args.context->state,
  185. hdr->ds_clp, hdr->lseg);
  186. switch (err) {
  187. case -NFS4ERR_RESET_TO_MDS:
  188. filelayout_reset_read(hdr);
  189. return task->tk_status;
  190. case -EAGAIN:
  191. rpc_restart_call_prepare(task);
  192. return -EAGAIN;
  193. }
  194. return 0;
  195. }
  196. /*
  197. * We reference the rpc_cred of the first WRITE that triggers the need for
  198. * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
  199. * rfc5661 is not clear about which credential should be used.
  200. */
  201. static void
  202. filelayout_set_layoutcommit(struct nfs_pgio_header *hdr)
  203. {
  204. loff_t end_offs = 0;
  205. if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
  206. hdr->res.verf->committed == NFS_FILE_SYNC)
  207. return;
  208. if (hdr->res.verf->committed == NFS_DATA_SYNC)
  209. end_offs = hdr->mds_offset + (loff_t)hdr->res.count;
  210. /* Note: if the write is unstable, don't set end_offs until commit */
  211. pnfs_set_layoutcommit(hdr->inode, hdr->lseg, end_offs);
  212. dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
  213. (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
  214. }
  215. bool
  216. filelayout_test_devid_unavailable(struct nfs4_deviceid_node *node)
  217. {
  218. return filelayout_test_devid_invalid(node) ||
  219. nfs4_test_deviceid_unavailable(node);
  220. }
  221. static bool
  222. filelayout_reset_to_mds(struct pnfs_layout_segment *lseg)
  223. {
  224. struct nfs4_deviceid_node *node = FILELAYOUT_DEVID_NODE(lseg);
  225. return filelayout_test_devid_unavailable(node);
  226. }
  227. /*
  228. * Call ops for the async read/write cases
  229. * In the case of dense layouts, the offset needs to be reset to its
  230. * original value.
  231. */
  232. static void filelayout_read_prepare(struct rpc_task *task, void *data)
  233. {
  234. struct nfs_pgio_header *hdr = data;
  235. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  236. rpc_exit(task, -EIO);
  237. return;
  238. }
  239. if (filelayout_reset_to_mds(hdr->lseg)) {
  240. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  241. filelayout_reset_read(hdr);
  242. rpc_exit(task, 0);
  243. return;
  244. }
  245. hdr->pgio_done_cb = filelayout_read_done_cb;
  246. if (nfs4_setup_sequence(hdr->ds_clp,
  247. &hdr->args.seq_args,
  248. &hdr->res.seq_res,
  249. task))
  250. return;
  251. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  252. hdr->args.lock_context, FMODE_READ) == -EIO)
  253. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  254. }
  255. static void filelayout_read_call_done(struct rpc_task *task, void *data)
  256. {
  257. struct nfs_pgio_header *hdr = data;
  258. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  259. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  260. task->tk_status == 0) {
  261. nfs41_sequence_done(task, &hdr->res.seq_res);
  262. return;
  263. }
  264. /* Note this may cause RPC to be resent */
  265. hdr->mds_ops->rpc_call_done(task, data);
  266. }
  267. static void filelayout_read_count_stats(struct rpc_task *task, void *data)
  268. {
  269. struct nfs_pgio_header *hdr = data;
  270. rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
  271. }
  272. static int filelayout_write_done_cb(struct rpc_task *task,
  273. struct nfs_pgio_header *hdr)
  274. {
  275. int err;
  276. trace_nfs4_pnfs_write(hdr, task->tk_status);
  277. err = filelayout_async_handle_error(task, hdr->args.context->state,
  278. hdr->ds_clp, hdr->lseg);
  279. switch (err) {
  280. case -NFS4ERR_RESET_TO_MDS:
  281. filelayout_reset_write(hdr);
  282. return task->tk_status;
  283. case -EAGAIN:
  284. rpc_restart_call_prepare(task);
  285. return -EAGAIN;
  286. }
  287. filelayout_set_layoutcommit(hdr);
  288. /* zero out the fattr */
  289. hdr->fattr.valid = 0;
  290. if (task->tk_status >= 0)
  291. nfs_writeback_update_inode(hdr);
  292. return 0;
  293. }
  294. static int filelayout_commit_done_cb(struct rpc_task *task,
  295. struct nfs_commit_data *data)
  296. {
  297. int err;
  298. trace_nfs4_pnfs_commit_ds(data, task->tk_status);
  299. err = filelayout_async_handle_error(task, NULL, data->ds_clp,
  300. data->lseg);
  301. switch (err) {
  302. case -NFS4ERR_RESET_TO_MDS:
  303. pnfs_generic_prepare_to_resend_writes(data);
  304. return -EAGAIN;
  305. case -EAGAIN:
  306. rpc_restart_call_prepare(task);
  307. return -EAGAIN;
  308. }
  309. pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
  310. return 0;
  311. }
  312. static void filelayout_write_prepare(struct rpc_task *task, void *data)
  313. {
  314. struct nfs_pgio_header *hdr = data;
  315. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  316. rpc_exit(task, -EIO);
  317. return;
  318. }
  319. if (filelayout_reset_to_mds(hdr->lseg)) {
  320. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  321. filelayout_reset_write(hdr);
  322. rpc_exit(task, 0);
  323. return;
  324. }
  325. if (nfs4_setup_sequence(hdr->ds_clp,
  326. &hdr->args.seq_args,
  327. &hdr->res.seq_res,
  328. task))
  329. return;
  330. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  331. hdr->args.lock_context, FMODE_WRITE) == -EIO)
  332. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  333. }
  334. static void filelayout_write_call_done(struct rpc_task *task, void *data)
  335. {
  336. struct nfs_pgio_header *hdr = data;
  337. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  338. task->tk_status == 0) {
  339. nfs41_sequence_done(task, &hdr->res.seq_res);
  340. return;
  341. }
  342. /* Note this may cause RPC to be resent */
  343. hdr->mds_ops->rpc_call_done(task, data);
  344. }
  345. static void filelayout_write_count_stats(struct rpc_task *task, void *data)
  346. {
  347. struct nfs_pgio_header *hdr = data;
  348. rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
  349. }
  350. static void filelayout_commit_prepare(struct rpc_task *task, void *data)
  351. {
  352. struct nfs_commit_data *wdata = data;
  353. nfs4_setup_sequence(wdata->ds_clp,
  354. &wdata->args.seq_args,
  355. &wdata->res.seq_res,
  356. task);
  357. }
  358. static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
  359. {
  360. struct nfs_commit_data *cdata = data;
  361. rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
  362. }
  363. static const struct rpc_call_ops filelayout_read_call_ops = {
  364. .rpc_call_prepare = filelayout_read_prepare,
  365. .rpc_call_done = filelayout_read_call_done,
  366. .rpc_count_stats = filelayout_read_count_stats,
  367. .rpc_release = pnfs_generic_rw_release,
  368. };
  369. static const struct rpc_call_ops filelayout_write_call_ops = {
  370. .rpc_call_prepare = filelayout_write_prepare,
  371. .rpc_call_done = filelayout_write_call_done,
  372. .rpc_count_stats = filelayout_write_count_stats,
  373. .rpc_release = pnfs_generic_rw_release,
  374. };
  375. static const struct rpc_call_ops filelayout_commit_call_ops = {
  376. .rpc_call_prepare = filelayout_commit_prepare,
  377. .rpc_call_done = pnfs_generic_write_commit_done,
  378. .rpc_count_stats = filelayout_commit_count_stats,
  379. .rpc_release = pnfs_generic_commit_release,
  380. };
  381. static enum pnfs_try_status
  382. filelayout_read_pagelist(struct nfs_pgio_header *hdr)
  383. {
  384. struct pnfs_layout_segment *lseg = hdr->lseg;
  385. struct nfs4_pnfs_ds *ds;
  386. struct rpc_clnt *ds_clnt;
  387. loff_t offset = hdr->args.offset;
  388. u32 j, idx;
  389. struct nfs_fh *fh;
  390. dprintk("--> %s ino %lu pgbase %u req %zu@%llu\n",
  391. __func__, hdr->inode->i_ino,
  392. hdr->args.pgbase, (size_t)hdr->args.count, offset);
  393. /* Retrieve the correct rpc_client for the byte range */
  394. j = nfs4_fl_calc_j_index(lseg, offset);
  395. idx = nfs4_fl_calc_ds_index(lseg, j);
  396. ds = nfs4_fl_prepare_ds(lseg, idx);
  397. if (!ds)
  398. return PNFS_NOT_ATTEMPTED;
  399. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
  400. if (IS_ERR(ds_clnt))
  401. return PNFS_NOT_ATTEMPTED;
  402. dprintk("%s USE DS: %s cl_count %d\n", __func__,
  403. ds->ds_remotestr, refcount_read(&ds->ds_clp->cl_count));
  404. /* No multipath support. Use first DS */
  405. refcount_inc(&ds->ds_clp->cl_count);
  406. hdr->ds_clp = ds->ds_clp;
  407. hdr->ds_commit_idx = idx;
  408. fh = nfs4_fl_select_ds_fh(lseg, j);
  409. if (fh)
  410. hdr->args.fh = fh;
  411. hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
  412. hdr->mds_offset = offset;
  413. /* Perform an asynchronous read to ds */
  414. nfs_initiate_pgio(ds_clnt, hdr, hdr->cred,
  415. NFS_PROTO(hdr->inode), &filelayout_read_call_ops,
  416. 0, RPC_TASK_SOFTCONN);
  417. return PNFS_ATTEMPTED;
  418. }
  419. /* Perform async writes. */
  420. static enum pnfs_try_status
  421. filelayout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
  422. {
  423. struct pnfs_layout_segment *lseg = hdr->lseg;
  424. struct nfs4_pnfs_ds *ds;
  425. struct rpc_clnt *ds_clnt;
  426. loff_t offset = hdr->args.offset;
  427. u32 j, idx;
  428. struct nfs_fh *fh;
  429. /* Retrieve the correct rpc_client for the byte range */
  430. j = nfs4_fl_calc_j_index(lseg, offset);
  431. idx = nfs4_fl_calc_ds_index(lseg, j);
  432. ds = nfs4_fl_prepare_ds(lseg, idx);
  433. if (!ds)
  434. return PNFS_NOT_ATTEMPTED;
  435. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
  436. if (IS_ERR(ds_clnt))
  437. return PNFS_NOT_ATTEMPTED;
  438. dprintk("%s ino %lu sync %d req %zu@%llu DS: %s cl_count %d\n",
  439. __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
  440. offset, ds->ds_remotestr, refcount_read(&ds->ds_clp->cl_count));
  441. hdr->pgio_done_cb = filelayout_write_done_cb;
  442. refcount_inc(&ds->ds_clp->cl_count);
  443. hdr->ds_clp = ds->ds_clp;
  444. hdr->ds_commit_idx = idx;
  445. fh = nfs4_fl_select_ds_fh(lseg, j);
  446. if (fh)
  447. hdr->args.fh = fh;
  448. hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
  449. /* Perform an asynchronous write */
  450. nfs_initiate_pgio(ds_clnt, hdr, hdr->cred,
  451. NFS_PROTO(hdr->inode), &filelayout_write_call_ops,
  452. sync, RPC_TASK_SOFTCONN);
  453. return PNFS_ATTEMPTED;
  454. }
  455. static int
  456. filelayout_check_deviceid(struct pnfs_layout_hdr *lo,
  457. struct nfs4_filelayout_segment *fl,
  458. gfp_t gfp_flags)
  459. {
  460. struct nfs4_deviceid_node *d;
  461. struct nfs4_file_layout_dsaddr *dsaddr;
  462. int status = -EINVAL;
  463. /* Is the deviceid already set? If so, we're good. */
  464. if (fl->dsaddr != NULL)
  465. return 0;
  466. /* find and reference the deviceid */
  467. d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode), &fl->deviceid,
  468. lo->plh_lc_cred, gfp_flags);
  469. if (d == NULL)
  470. goto out;
  471. dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  472. /* Found deviceid is unavailable */
  473. if (filelayout_test_devid_unavailable(&dsaddr->id_node))
  474. goto out_put;
  475. if (fl->first_stripe_index >= dsaddr->stripe_count) {
  476. dprintk("%s Bad first_stripe_index %u\n",
  477. __func__, fl->first_stripe_index);
  478. goto out_put;
  479. }
  480. if ((fl->stripe_type == STRIPE_SPARSE &&
  481. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  482. (fl->stripe_type == STRIPE_DENSE &&
  483. fl->num_fh != dsaddr->stripe_count)) {
  484. dprintk("%s num_fh %u not valid for given packing\n",
  485. __func__, fl->num_fh);
  486. goto out_put;
  487. }
  488. status = 0;
  489. /*
  490. * Atomic compare and xchange to ensure we don't scribble
  491. * over a non-NULL pointer.
  492. */
  493. if (cmpxchg(&fl->dsaddr, NULL, dsaddr) != NULL)
  494. goto out_put;
  495. out:
  496. return status;
  497. out_put:
  498. nfs4_fl_put_deviceid(dsaddr);
  499. goto out;
  500. }
  501. /*
  502. * filelayout_check_layout()
  503. *
  504. * Make sure layout segment parameters are sane WRT the device.
  505. * At this point no generic layer initialization of the lseg has occurred,
  506. * and nothing has been added to the layout_hdr cache.
  507. *
  508. */
  509. static int
  510. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  511. struct nfs4_filelayout_segment *fl,
  512. struct nfs4_layoutget_res *lgr,
  513. gfp_t gfp_flags)
  514. {
  515. int status = -EINVAL;
  516. dprintk("--> %s\n", __func__);
  517. /* FIXME: remove this check when layout segment support is added */
  518. if (lgr->range.offset != 0 ||
  519. lgr->range.length != NFS4_MAX_UINT64) {
  520. dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
  521. __func__);
  522. goto out;
  523. }
  524. if (fl->pattern_offset > lgr->range.offset) {
  525. dprintk("%s pattern_offset %lld too large\n",
  526. __func__, fl->pattern_offset);
  527. goto out;
  528. }
  529. if (!fl->stripe_unit) {
  530. dprintk("%s Invalid stripe unit (%u)\n",
  531. __func__, fl->stripe_unit);
  532. goto out;
  533. }
  534. status = 0;
  535. out:
  536. dprintk("--> %s returns %d\n", __func__, status);
  537. return status;
  538. }
  539. static void _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  540. {
  541. int i;
  542. if (fl->fh_array) {
  543. for (i = 0; i < fl->num_fh; i++) {
  544. if (!fl->fh_array[i])
  545. break;
  546. kfree(fl->fh_array[i]);
  547. }
  548. kfree(fl->fh_array);
  549. }
  550. kfree(fl);
  551. }
  552. static int
  553. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  554. struct nfs4_filelayout_segment *fl,
  555. struct nfs4_layoutget_res *lgr,
  556. gfp_t gfp_flags)
  557. {
  558. struct xdr_stream stream;
  559. struct xdr_buf buf;
  560. struct page *scratch;
  561. __be32 *p;
  562. uint32_t nfl_util;
  563. int i;
  564. dprintk("%s: set_layout_map Begin\n", __func__);
  565. scratch = alloc_page(gfp_flags);
  566. if (!scratch)
  567. return -ENOMEM;
  568. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
  569. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  570. /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
  571. * num_fh (4) */
  572. p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
  573. if (unlikely(!p))
  574. goto out_err;
  575. memcpy(&fl->deviceid, p, sizeof(fl->deviceid));
  576. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  577. nfs4_print_deviceid(&fl->deviceid);
  578. nfl_util = be32_to_cpup(p++);
  579. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  580. fl->commit_through_mds = 1;
  581. if (nfl_util & NFL4_UFLG_DENSE)
  582. fl->stripe_type = STRIPE_DENSE;
  583. else
  584. fl->stripe_type = STRIPE_SPARSE;
  585. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  586. fl->first_stripe_index = be32_to_cpup(p++);
  587. p = xdr_decode_hyper(p, &fl->pattern_offset);
  588. fl->num_fh = be32_to_cpup(p++);
  589. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  590. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  591. fl->pattern_offset);
  592. /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
  593. * Futher checking is done in filelayout_check_layout */
  594. if (fl->num_fh >
  595. max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
  596. goto out_err;
  597. if (fl->num_fh > 0) {
  598. fl->fh_array = kcalloc(fl->num_fh, sizeof(fl->fh_array[0]),
  599. gfp_flags);
  600. if (!fl->fh_array)
  601. goto out_err;
  602. }
  603. for (i = 0; i < fl->num_fh; i++) {
  604. /* Do we want to use a mempool here? */
  605. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
  606. if (!fl->fh_array[i])
  607. goto out_err;
  608. p = xdr_inline_decode(&stream, 4);
  609. if (unlikely(!p))
  610. goto out_err;
  611. fl->fh_array[i]->size = be32_to_cpup(p++);
  612. if (fl->fh_array[i]->size > NFS_MAXFHSIZE) {
  613. printk(KERN_ERR "NFS: Too big fh %d received %d\n",
  614. i, fl->fh_array[i]->size);
  615. goto out_err;
  616. }
  617. p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
  618. if (unlikely(!p))
  619. goto out_err;
  620. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  621. dprintk("DEBUG: %s: fh len %d\n", __func__,
  622. fl->fh_array[i]->size);
  623. }
  624. __free_page(scratch);
  625. return 0;
  626. out_err:
  627. __free_page(scratch);
  628. return -EIO;
  629. }
  630. static void
  631. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  632. {
  633. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  634. dprintk("--> %s\n", __func__);
  635. if (fl->dsaddr != NULL)
  636. nfs4_fl_put_deviceid(fl->dsaddr);
  637. /* This assumes a single RW lseg */
  638. if (lseg->pls_range.iomode == IOMODE_RW) {
  639. struct nfs4_filelayout *flo;
  640. struct inode *inode;
  641. flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
  642. inode = flo->generic_hdr.plh_inode;
  643. spin_lock(&inode->i_lock);
  644. pnfs_generic_ds_cinfo_release_lseg(&flo->commit_info, lseg);
  645. spin_unlock(&inode->i_lock);
  646. }
  647. _filelayout_free_lseg(fl);
  648. }
  649. static struct pnfs_layout_segment *
  650. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  651. struct nfs4_layoutget_res *lgr,
  652. gfp_t gfp_flags)
  653. {
  654. struct nfs4_filelayout_segment *fl;
  655. int rc;
  656. dprintk("--> %s\n", __func__);
  657. fl = kzalloc(sizeof(*fl), gfp_flags);
  658. if (!fl)
  659. return NULL;
  660. rc = filelayout_decode_layout(layoutid, fl, lgr, gfp_flags);
  661. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, gfp_flags)) {
  662. _filelayout_free_lseg(fl);
  663. return NULL;
  664. }
  665. return &fl->generic_hdr;
  666. }
  667. /*
  668. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  669. *
  670. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  671. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  672. */
  673. static size_t
  674. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  675. struct nfs_page *req)
  676. {
  677. unsigned int size;
  678. u64 p_stripe, r_stripe;
  679. u32 stripe_offset;
  680. u64 segment_offset = pgio->pg_lseg->pls_range.offset;
  681. u32 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  682. /* calls nfs_generic_pg_test */
  683. size = pnfs_generic_pg_test(pgio, prev, req);
  684. if (!size)
  685. return 0;
  686. /* see if req and prev are in the same stripe */
  687. if (prev) {
  688. p_stripe = (u64)req_offset(prev) - segment_offset;
  689. r_stripe = (u64)req_offset(req) - segment_offset;
  690. do_div(p_stripe, stripe_unit);
  691. do_div(r_stripe, stripe_unit);
  692. if (p_stripe != r_stripe)
  693. return 0;
  694. }
  695. /* calculate remaining bytes in the current stripe */
  696. div_u64_rem((u64)req_offset(req) - segment_offset,
  697. stripe_unit,
  698. &stripe_offset);
  699. WARN_ON_ONCE(stripe_offset > stripe_unit);
  700. if (stripe_offset >= stripe_unit)
  701. return 0;
  702. return min(stripe_unit - (unsigned int)stripe_offset, size);
  703. }
  704. static struct pnfs_layout_segment *
  705. fl_pnfs_update_layout(struct inode *ino,
  706. struct nfs_open_context *ctx,
  707. loff_t pos,
  708. u64 count,
  709. enum pnfs_iomode iomode,
  710. bool strict_iomode,
  711. gfp_t gfp_flags)
  712. {
  713. struct pnfs_layout_segment *lseg = NULL;
  714. struct pnfs_layout_hdr *lo;
  715. struct nfs4_filelayout_segment *fl;
  716. int status;
  717. lseg = pnfs_update_layout(ino, ctx, pos, count, iomode, strict_iomode,
  718. gfp_flags);
  719. if (IS_ERR_OR_NULL(lseg))
  720. goto out;
  721. lo = NFS_I(ino)->layout;
  722. fl = FILELAYOUT_LSEG(lseg);
  723. status = filelayout_check_deviceid(lo, fl, gfp_flags);
  724. if (status) {
  725. pnfs_put_lseg(lseg);
  726. lseg = NULL;
  727. }
  728. out:
  729. return lseg;
  730. }
  731. static void
  732. filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  733. struct nfs_page *req)
  734. {
  735. pnfs_generic_pg_check_layout(pgio);
  736. if (!pgio->pg_lseg) {
  737. pgio->pg_lseg = fl_pnfs_update_layout(pgio->pg_inode,
  738. nfs_req_openctx(req),
  739. 0,
  740. NFS4_MAX_UINT64,
  741. IOMODE_READ,
  742. false,
  743. GFP_KERNEL);
  744. if (IS_ERR(pgio->pg_lseg)) {
  745. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  746. pgio->pg_lseg = NULL;
  747. return;
  748. }
  749. }
  750. /* If no lseg, fall back to read through mds */
  751. if (pgio->pg_lseg == NULL)
  752. nfs_pageio_reset_read_mds(pgio);
  753. }
  754. static void
  755. filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  756. struct nfs_page *req)
  757. {
  758. pnfs_generic_pg_check_layout(pgio);
  759. if (!pgio->pg_lseg) {
  760. pgio->pg_lseg = fl_pnfs_update_layout(pgio->pg_inode,
  761. nfs_req_openctx(req),
  762. 0,
  763. NFS4_MAX_UINT64,
  764. IOMODE_RW,
  765. false,
  766. GFP_NOFS);
  767. if (IS_ERR(pgio->pg_lseg)) {
  768. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  769. pgio->pg_lseg = NULL;
  770. return;
  771. }
  772. }
  773. /* If no lseg, fall back to write through mds */
  774. if (pgio->pg_lseg == NULL)
  775. nfs_pageio_reset_write_mds(pgio);
  776. }
  777. static const struct nfs_pageio_ops filelayout_pg_read_ops = {
  778. .pg_init = filelayout_pg_init_read,
  779. .pg_test = filelayout_pg_test,
  780. .pg_doio = pnfs_generic_pg_readpages,
  781. .pg_cleanup = pnfs_generic_pg_cleanup,
  782. };
  783. static const struct nfs_pageio_ops filelayout_pg_write_ops = {
  784. .pg_init = filelayout_pg_init_write,
  785. .pg_test = filelayout_pg_test,
  786. .pg_doio = pnfs_generic_pg_writepages,
  787. .pg_cleanup = pnfs_generic_pg_cleanup,
  788. };
  789. static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
  790. {
  791. if (fl->stripe_type == STRIPE_SPARSE)
  792. return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
  793. else
  794. return j;
  795. }
  796. static void
  797. filelayout_mark_request_commit(struct nfs_page *req,
  798. struct pnfs_layout_segment *lseg,
  799. struct nfs_commit_info *cinfo,
  800. u32 ds_commit_idx)
  801. {
  802. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  803. u32 i, j;
  804. if (fl->commit_through_mds) {
  805. nfs_request_add_commit_list(req, cinfo);
  806. } else {
  807. /* Note that we are calling nfs4_fl_calc_j_index on each page
  808. * that ends up being committed to a data server. An attractive
  809. * alternative is to add a field to nfs_write_data and nfs_page
  810. * to store the value calculated in filelayout_write_pagelist
  811. * and just use that here.
  812. */
  813. j = nfs4_fl_calc_j_index(lseg, req_offset(req));
  814. i = select_bucket_index(fl, j);
  815. pnfs_layout_mark_request_commit(req, lseg, cinfo, i);
  816. }
  817. }
  818. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  819. {
  820. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  821. if (flseg->stripe_type == STRIPE_SPARSE)
  822. return i;
  823. else
  824. return nfs4_fl_calc_ds_index(lseg, i);
  825. }
  826. static struct nfs_fh *
  827. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  828. {
  829. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  830. if (flseg->stripe_type == STRIPE_SPARSE) {
  831. if (flseg->num_fh == 1)
  832. i = 0;
  833. else if (flseg->num_fh == 0)
  834. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  835. return NULL;
  836. }
  837. return flseg->fh_array[i];
  838. }
  839. static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
  840. {
  841. struct pnfs_layout_segment *lseg = data->lseg;
  842. struct nfs4_pnfs_ds *ds;
  843. struct rpc_clnt *ds_clnt;
  844. u32 idx;
  845. struct nfs_fh *fh;
  846. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  847. ds = nfs4_fl_prepare_ds(lseg, idx);
  848. if (!ds)
  849. goto out_err;
  850. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, data->inode);
  851. if (IS_ERR(ds_clnt))
  852. goto out_err;
  853. dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
  854. data->inode->i_ino, how, refcount_read(&ds->ds_clp->cl_count));
  855. data->commit_done_cb = filelayout_commit_done_cb;
  856. refcount_inc(&ds->ds_clp->cl_count);
  857. data->ds_clp = ds->ds_clp;
  858. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  859. if (fh)
  860. data->args.fh = fh;
  861. return nfs_initiate_commit(ds_clnt, data, NFS_PROTO(data->inode),
  862. &filelayout_commit_call_ops, how,
  863. RPC_TASK_SOFTCONN);
  864. out_err:
  865. pnfs_generic_prepare_to_resend_writes(data);
  866. pnfs_generic_commit_release(data);
  867. return -EAGAIN;
  868. }
  869. static int
  870. filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  871. int how, struct nfs_commit_info *cinfo)
  872. {
  873. return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
  874. filelayout_initiate_commit);
  875. }
  876. static struct nfs4_deviceid_node *
  877. filelayout_alloc_deviceid_node(struct nfs_server *server,
  878. struct pnfs_device *pdev, gfp_t gfp_flags)
  879. {
  880. struct nfs4_file_layout_dsaddr *dsaddr;
  881. dsaddr = nfs4_fl_alloc_deviceid_node(server, pdev, gfp_flags);
  882. if (!dsaddr)
  883. return NULL;
  884. return &dsaddr->id_node;
  885. }
  886. static void
  887. filelayout_free_deviceid_node(struct nfs4_deviceid_node *d)
  888. {
  889. nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
  890. }
  891. static struct pnfs_layout_hdr *
  892. filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
  893. {
  894. struct nfs4_filelayout *flo;
  895. flo = kzalloc(sizeof(*flo), gfp_flags);
  896. if (flo == NULL)
  897. return NULL;
  898. pnfs_init_ds_commit_info(&flo->commit_info);
  899. flo->commit_info.ops = &filelayout_commit_ops;
  900. return &flo->generic_hdr;
  901. }
  902. static void
  903. filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
  904. {
  905. kfree_rcu(FILELAYOUT_FROM_HDR(lo), generic_hdr.plh_rcu);
  906. }
  907. static struct pnfs_ds_commit_info *
  908. filelayout_get_ds_info(struct inode *inode)
  909. {
  910. struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
  911. if (layout == NULL)
  912. return NULL;
  913. else
  914. return &FILELAYOUT_FROM_HDR(layout)->commit_info;
  915. }
  916. static void
  917. filelayout_setup_ds_info(struct pnfs_ds_commit_info *fl_cinfo,
  918. struct pnfs_layout_segment *lseg)
  919. {
  920. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  921. struct inode *inode = lseg->pls_layout->plh_inode;
  922. struct pnfs_commit_array *array, *new;
  923. unsigned int size = (fl->stripe_type == STRIPE_SPARSE) ?
  924. fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
  925. new = pnfs_alloc_commit_array(size, GFP_NOIO);
  926. if (new) {
  927. spin_lock(&inode->i_lock);
  928. array = pnfs_add_commit_array(fl_cinfo, new, lseg);
  929. spin_unlock(&inode->i_lock);
  930. if (array != new)
  931. pnfs_free_commit_array(new);
  932. }
  933. }
  934. static void
  935. filelayout_release_ds_info(struct pnfs_ds_commit_info *fl_cinfo,
  936. struct inode *inode)
  937. {
  938. spin_lock(&inode->i_lock);
  939. pnfs_generic_ds_cinfo_destroy(fl_cinfo);
  940. spin_unlock(&inode->i_lock);
  941. }
  942. static const struct pnfs_commit_ops filelayout_commit_ops = {
  943. .setup_ds_info = filelayout_setup_ds_info,
  944. .release_ds_info = filelayout_release_ds_info,
  945. .mark_request_commit = filelayout_mark_request_commit,
  946. .clear_request_commit = pnfs_generic_clear_request_commit,
  947. .scan_commit_lists = pnfs_generic_scan_commit_lists,
  948. .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
  949. .search_commit_reqs = pnfs_generic_search_commit_reqs,
  950. .commit_pagelist = filelayout_commit_pagelist,
  951. };
  952. static struct pnfs_layoutdriver_type filelayout_type = {
  953. .id = LAYOUT_NFSV4_1_FILES,
  954. .name = "LAYOUT_NFSV4_1_FILES",
  955. .owner = THIS_MODULE,
  956. .flags = PNFS_LAYOUTGET_ON_OPEN,
  957. .max_layoutget_response = 4096, /* 1 page or so... */
  958. .alloc_layout_hdr = filelayout_alloc_layout_hdr,
  959. .free_layout_hdr = filelayout_free_layout_hdr,
  960. .alloc_lseg = filelayout_alloc_lseg,
  961. .free_lseg = filelayout_free_lseg,
  962. .pg_read_ops = &filelayout_pg_read_ops,
  963. .pg_write_ops = &filelayout_pg_write_ops,
  964. .get_ds_info = &filelayout_get_ds_info,
  965. .read_pagelist = filelayout_read_pagelist,
  966. .write_pagelist = filelayout_write_pagelist,
  967. .alloc_deviceid_node = filelayout_alloc_deviceid_node,
  968. .free_deviceid_node = filelayout_free_deviceid_node,
  969. .sync = pnfs_nfs_generic_sync,
  970. };
  971. static int __init nfs4filelayout_init(void)
  972. {
  973. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  974. __func__);
  975. return pnfs_register_layoutdriver(&filelayout_type);
  976. }
  977. static void __exit nfs4filelayout_exit(void)
  978. {
  979. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  980. __func__);
  981. pnfs_unregister_layoutdriver(&filelayout_type);
  982. }
  983. MODULE_ALIAS("nfs-layouttype4-1");
  984. module_init(nfs4filelayout_init);
  985. module_exit(nfs4filelayout_exit);