filelayout.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. case -EPROTO:
  163. case -ENODEV:
  164. dprintk("%s DS connection error %d\n", __func__,
  165. task->tk_status);
  166. nfs4_mark_deviceid_unavailable(devid);
  167. pnfs_error_mark_layout_for_return(inode, lseg);
  168. pnfs_set_lo_fail(lseg);
  169. rpc_wake_up(&tbl->slot_tbl_waitq);
  170. fallthrough;
  171. default:
  172. reset:
  173. dprintk("%s Retry through MDS. Error %d\n", __func__,
  174. task->tk_status);
  175. return -NFS4ERR_RESET_TO_MDS;
  176. }
  177. task->tk_status = 0;
  178. return -EAGAIN;
  179. }
  180. /* NFS_PROTO call done callback routines */
  181. static int filelayout_read_done_cb(struct rpc_task *task,
  182. struct nfs_pgio_header *hdr)
  183. {
  184. int err;
  185. trace_nfs4_pnfs_read(hdr, task->tk_status);
  186. err = filelayout_async_handle_error(task, hdr->args.context->state,
  187. hdr->ds_clp, hdr->lseg);
  188. switch (err) {
  189. case -NFS4ERR_RESET_TO_MDS:
  190. filelayout_reset_read(hdr);
  191. return task->tk_status;
  192. case -EAGAIN:
  193. rpc_restart_call_prepare(task);
  194. return -EAGAIN;
  195. }
  196. return 0;
  197. }
  198. /*
  199. * We reference the rpc_cred of the first WRITE that triggers the need for
  200. * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
  201. * rfc5661 is not clear about which credential should be used.
  202. */
  203. static void
  204. filelayout_set_layoutcommit(struct nfs_pgio_header *hdr)
  205. {
  206. loff_t end_offs = 0;
  207. if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
  208. hdr->res.verf->committed == NFS_FILE_SYNC)
  209. return;
  210. if (hdr->res.verf->committed == NFS_DATA_SYNC)
  211. end_offs = hdr->mds_offset + (loff_t)hdr->res.count;
  212. /* Note: if the write is unstable, don't set end_offs until commit */
  213. pnfs_set_layoutcommit(hdr->inode, hdr->lseg, end_offs);
  214. dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
  215. (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
  216. }
  217. bool
  218. filelayout_test_devid_unavailable(struct nfs4_deviceid_node *node)
  219. {
  220. return filelayout_test_devid_invalid(node) ||
  221. nfs4_test_deviceid_unavailable(node);
  222. }
  223. static bool
  224. filelayout_reset_to_mds(struct pnfs_layout_segment *lseg)
  225. {
  226. struct nfs4_deviceid_node *node = FILELAYOUT_DEVID_NODE(lseg);
  227. return filelayout_test_devid_unavailable(node);
  228. }
  229. /*
  230. * Call ops for the async read/write cases
  231. * In the case of dense layouts, the offset needs to be reset to its
  232. * original value.
  233. */
  234. static void filelayout_read_prepare(struct rpc_task *task, void *data)
  235. {
  236. struct nfs_pgio_header *hdr = data;
  237. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  238. rpc_exit(task, -EIO);
  239. return;
  240. }
  241. if (filelayout_reset_to_mds(hdr->lseg)) {
  242. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  243. filelayout_reset_read(hdr);
  244. rpc_exit(task, 0);
  245. return;
  246. }
  247. hdr->pgio_done_cb = filelayout_read_done_cb;
  248. if (nfs4_setup_sequence(hdr->ds_clp,
  249. &hdr->args.seq_args,
  250. &hdr->res.seq_res,
  251. task))
  252. return;
  253. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  254. hdr->args.lock_context, FMODE_READ) == -EIO)
  255. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  256. }
  257. static void filelayout_read_call_done(struct rpc_task *task, void *data)
  258. {
  259. struct nfs_pgio_header *hdr = data;
  260. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  261. task->tk_status == 0) {
  262. nfs41_sequence_done(task, &hdr->res.seq_res);
  263. return;
  264. }
  265. /* Note this may cause RPC to be resent */
  266. hdr->mds_ops->rpc_call_done(task, data);
  267. }
  268. static void filelayout_read_count_stats(struct rpc_task *task, void *data)
  269. {
  270. struct nfs_pgio_header *hdr = data;
  271. rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
  272. }
  273. static int filelayout_write_done_cb(struct rpc_task *task,
  274. struct nfs_pgio_header *hdr)
  275. {
  276. int err;
  277. trace_nfs4_pnfs_write(hdr, task->tk_status);
  278. err = filelayout_async_handle_error(task, hdr->args.context->state,
  279. hdr->ds_clp, hdr->lseg);
  280. switch (err) {
  281. case -NFS4ERR_RESET_TO_MDS:
  282. filelayout_reset_write(hdr);
  283. return task->tk_status;
  284. case -EAGAIN:
  285. rpc_restart_call_prepare(task);
  286. return -EAGAIN;
  287. }
  288. filelayout_set_layoutcommit(hdr);
  289. /* zero out the fattr */
  290. hdr->fattr.valid = 0;
  291. if (task->tk_status >= 0)
  292. nfs_writeback_update_inode(hdr);
  293. return 0;
  294. }
  295. static int filelayout_commit_done_cb(struct rpc_task *task,
  296. struct nfs_commit_data *data)
  297. {
  298. int err;
  299. trace_nfs4_pnfs_commit_ds(data, task->tk_status);
  300. err = filelayout_async_handle_error(task, NULL, data->ds_clp,
  301. data->lseg);
  302. switch (err) {
  303. case -NFS4ERR_RESET_TO_MDS:
  304. pnfs_generic_prepare_to_resend_writes(data);
  305. return -EAGAIN;
  306. case -EAGAIN:
  307. rpc_restart_call_prepare(task);
  308. return -EAGAIN;
  309. }
  310. pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
  311. return 0;
  312. }
  313. static void filelayout_write_prepare(struct rpc_task *task, void *data)
  314. {
  315. struct nfs_pgio_header *hdr = data;
  316. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  317. rpc_exit(task, -EIO);
  318. return;
  319. }
  320. if (filelayout_reset_to_mds(hdr->lseg)) {
  321. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  322. filelayout_reset_write(hdr);
  323. rpc_exit(task, 0);
  324. return;
  325. }
  326. if (nfs4_setup_sequence(hdr->ds_clp,
  327. &hdr->args.seq_args,
  328. &hdr->res.seq_res,
  329. task))
  330. return;
  331. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  332. hdr->args.lock_context, FMODE_WRITE) == -EIO)
  333. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  334. }
  335. static void filelayout_write_call_done(struct rpc_task *task, void *data)
  336. {
  337. struct nfs_pgio_header *hdr = data;
  338. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  339. task->tk_status == 0) {
  340. nfs41_sequence_done(task, &hdr->res.seq_res);
  341. return;
  342. }
  343. /* Note this may cause RPC to be resent */
  344. hdr->mds_ops->rpc_call_done(task, data);
  345. }
  346. static void filelayout_write_count_stats(struct rpc_task *task, void *data)
  347. {
  348. struct nfs_pgio_header *hdr = data;
  349. rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
  350. }
  351. static void filelayout_commit_prepare(struct rpc_task *task, void *data)
  352. {
  353. struct nfs_commit_data *wdata = data;
  354. nfs4_setup_sequence(wdata->ds_clp,
  355. &wdata->args.seq_args,
  356. &wdata->res.seq_res,
  357. task);
  358. }
  359. static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
  360. {
  361. struct nfs_commit_data *cdata = data;
  362. rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
  363. }
  364. static const struct rpc_call_ops filelayout_read_call_ops = {
  365. .rpc_call_prepare = filelayout_read_prepare,
  366. .rpc_call_done = filelayout_read_call_done,
  367. .rpc_count_stats = filelayout_read_count_stats,
  368. .rpc_release = pnfs_generic_rw_release,
  369. };
  370. static const struct rpc_call_ops filelayout_write_call_ops = {
  371. .rpc_call_prepare = filelayout_write_prepare,
  372. .rpc_call_done = filelayout_write_call_done,
  373. .rpc_count_stats = filelayout_write_count_stats,
  374. .rpc_release = pnfs_generic_rw_release,
  375. };
  376. static const struct rpc_call_ops filelayout_commit_call_ops = {
  377. .rpc_call_prepare = filelayout_commit_prepare,
  378. .rpc_call_done = pnfs_generic_write_commit_done,
  379. .rpc_count_stats = filelayout_commit_count_stats,
  380. .rpc_release = pnfs_generic_commit_release,
  381. };
  382. static enum pnfs_try_status
  383. filelayout_read_pagelist(struct nfs_pgio_header *hdr)
  384. {
  385. struct pnfs_layout_segment *lseg = hdr->lseg;
  386. struct nfs4_pnfs_ds *ds;
  387. struct rpc_clnt *ds_clnt;
  388. loff_t offset = hdr->args.offset;
  389. u32 j, idx;
  390. struct nfs_fh *fh;
  391. dprintk("--> %s ino %lu pgbase %u req %zu@%llu\n",
  392. __func__, hdr->inode->i_ino,
  393. hdr->args.pgbase, (size_t)hdr->args.count, offset);
  394. /* Retrieve the correct rpc_client for the byte range */
  395. j = nfs4_fl_calc_j_index(lseg, offset);
  396. idx = nfs4_fl_calc_ds_index(lseg, j);
  397. ds = nfs4_fl_prepare_ds(lseg, idx);
  398. if (!ds)
  399. return PNFS_NOT_ATTEMPTED;
  400. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
  401. if (IS_ERR(ds_clnt))
  402. return PNFS_NOT_ATTEMPTED;
  403. dprintk("%s USE DS: %s cl_count %d\n", __func__,
  404. ds->ds_remotestr, refcount_read(&ds->ds_clp->cl_count));
  405. /* No multipath support. Use first DS */
  406. refcount_inc(&ds->ds_clp->cl_count);
  407. hdr->ds_clp = ds->ds_clp;
  408. hdr->ds_commit_idx = idx;
  409. fh = nfs4_fl_select_ds_fh(lseg, j);
  410. if (fh)
  411. hdr->args.fh = fh;
  412. hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
  413. hdr->mds_offset = offset;
  414. /* Perform an asynchronous read to ds */
  415. nfs_initiate_pgio(ds_clnt, hdr, hdr->cred,
  416. NFS_PROTO(hdr->inode), &filelayout_read_call_ops,
  417. 0, RPC_TASK_SOFTCONN, NULL);
  418. return PNFS_ATTEMPTED;
  419. }
  420. /* Perform async writes. */
  421. static enum pnfs_try_status
  422. filelayout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
  423. {
  424. struct pnfs_layout_segment *lseg = hdr->lseg;
  425. struct nfs4_pnfs_ds *ds;
  426. struct rpc_clnt *ds_clnt;
  427. loff_t offset = hdr->args.offset;
  428. u32 j, idx;
  429. struct nfs_fh *fh;
  430. /* Retrieve the correct rpc_client for the byte range */
  431. j = nfs4_fl_calc_j_index(lseg, offset);
  432. idx = nfs4_fl_calc_ds_index(lseg, j);
  433. ds = nfs4_fl_prepare_ds(lseg, idx);
  434. if (!ds)
  435. return PNFS_NOT_ATTEMPTED;
  436. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
  437. if (IS_ERR(ds_clnt))
  438. return PNFS_NOT_ATTEMPTED;
  439. dprintk("%s ino %lu sync %d req %zu@%llu DS: %s cl_count %d\n",
  440. __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
  441. offset, ds->ds_remotestr, refcount_read(&ds->ds_clp->cl_count));
  442. hdr->pgio_done_cb = filelayout_write_done_cb;
  443. refcount_inc(&ds->ds_clp->cl_count);
  444. hdr->ds_clp = ds->ds_clp;
  445. hdr->ds_commit_idx = idx;
  446. fh = nfs4_fl_select_ds_fh(lseg, j);
  447. if (fh)
  448. hdr->args.fh = fh;
  449. hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
  450. /* Perform an asynchronous write */
  451. nfs_initiate_pgio(ds_clnt, hdr, hdr->cred,
  452. NFS_PROTO(hdr->inode), &filelayout_write_call_ops,
  453. sync, RPC_TASK_SOFTCONN, NULL);
  454. return PNFS_ATTEMPTED;
  455. }
  456. static int
  457. filelayout_check_deviceid(struct pnfs_layout_hdr *lo,
  458. struct nfs4_filelayout_segment *fl,
  459. gfp_t gfp_flags)
  460. {
  461. struct nfs4_deviceid_node *d;
  462. struct nfs4_file_layout_dsaddr *dsaddr;
  463. int status = -EINVAL;
  464. /* Is the deviceid already set? If so, we're good. */
  465. if (fl->dsaddr != NULL)
  466. return 0;
  467. /* find and reference the deviceid */
  468. d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode), &fl->deviceid,
  469. lo->plh_lc_cred, gfp_flags);
  470. if (d == NULL)
  471. goto out;
  472. dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  473. /* Found deviceid is unavailable */
  474. if (filelayout_test_devid_unavailable(&dsaddr->id_node))
  475. goto out_put;
  476. if (fl->first_stripe_index >= dsaddr->stripe_count) {
  477. dprintk("%s Bad first_stripe_index %u\n",
  478. __func__, fl->first_stripe_index);
  479. goto out_put;
  480. }
  481. if ((fl->stripe_type == STRIPE_SPARSE &&
  482. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  483. (fl->stripe_type == STRIPE_DENSE &&
  484. fl->num_fh != dsaddr->stripe_count)) {
  485. dprintk("%s num_fh %u not valid for given packing\n",
  486. __func__, fl->num_fh);
  487. goto out_put;
  488. }
  489. status = 0;
  490. /*
  491. * Atomic compare and xchange to ensure we don't scribble
  492. * over a non-NULL pointer.
  493. */
  494. if (cmpxchg(&fl->dsaddr, NULL, dsaddr) != NULL)
  495. goto out_put;
  496. out:
  497. return status;
  498. out_put:
  499. nfs4_fl_put_deviceid(dsaddr);
  500. goto out;
  501. }
  502. /*
  503. * filelayout_check_layout()
  504. *
  505. * Make sure layout segment parameters are sane WRT the device.
  506. * At this point no generic layer initialization of the lseg has occurred,
  507. * and nothing has been added to the layout_hdr cache.
  508. *
  509. */
  510. static int
  511. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  512. struct nfs4_filelayout_segment *fl,
  513. struct nfs4_layoutget_res *lgr,
  514. gfp_t gfp_flags)
  515. {
  516. int status = -EINVAL;
  517. dprintk("--> %s\n", __func__);
  518. if (fl->pattern_offset > lgr->range.offset) {
  519. dprintk("%s pattern_offset %lld too large\n",
  520. __func__, fl->pattern_offset);
  521. goto out;
  522. }
  523. if (!fl->stripe_unit) {
  524. dprintk("%s Invalid stripe unit (%u)\n",
  525. __func__, fl->stripe_unit);
  526. goto out;
  527. }
  528. status = 0;
  529. out:
  530. dprintk("--> %s returns %d\n", __func__, status);
  531. return status;
  532. }
  533. static void _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  534. {
  535. int i;
  536. if (fl->fh_array) {
  537. for (i = 0; i < fl->num_fh; i++) {
  538. if (!fl->fh_array[i])
  539. break;
  540. kfree(fl->fh_array[i]);
  541. }
  542. kfree(fl->fh_array);
  543. }
  544. kfree(fl);
  545. }
  546. static int
  547. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  548. struct nfs4_filelayout_segment *fl,
  549. struct nfs4_layoutget_res *lgr,
  550. gfp_t gfp_flags)
  551. {
  552. struct xdr_stream stream;
  553. struct xdr_buf buf;
  554. struct page *scratch;
  555. __be32 *p;
  556. uint32_t nfl_util;
  557. int i;
  558. dprintk("%s: set_layout_map Begin\n", __func__);
  559. scratch = alloc_page(gfp_flags);
  560. if (!scratch)
  561. return -ENOMEM;
  562. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
  563. xdr_set_scratch_page(&stream, scratch);
  564. /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
  565. * num_fh (4) */
  566. p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
  567. if (unlikely(!p))
  568. goto out_err;
  569. memcpy(&fl->deviceid, p, sizeof(fl->deviceid));
  570. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  571. nfs4_print_deviceid(&fl->deviceid);
  572. nfl_util = be32_to_cpup(p++);
  573. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  574. fl->commit_through_mds = 1;
  575. if (nfl_util & NFL4_UFLG_DENSE)
  576. fl->stripe_type = STRIPE_DENSE;
  577. else
  578. fl->stripe_type = STRIPE_SPARSE;
  579. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  580. fl->first_stripe_index = be32_to_cpup(p++);
  581. p = xdr_decode_hyper(p, &fl->pattern_offset);
  582. fl->num_fh = be32_to_cpup(p++);
  583. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  584. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  585. fl->pattern_offset);
  586. /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
  587. * Futher checking is done in filelayout_check_layout */
  588. if (fl->num_fh >
  589. max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
  590. goto out_err;
  591. if (fl->num_fh > 0) {
  592. fl->fh_array = kcalloc(fl->num_fh, sizeof(fl->fh_array[0]),
  593. gfp_flags);
  594. if (!fl->fh_array)
  595. goto out_err;
  596. }
  597. for (i = 0; i < fl->num_fh; i++) {
  598. /* Do we want to use a mempool here? */
  599. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
  600. if (!fl->fh_array[i])
  601. goto out_err;
  602. p = xdr_inline_decode(&stream, 4);
  603. if (unlikely(!p))
  604. goto out_err;
  605. fl->fh_array[i]->size = be32_to_cpup(p++);
  606. if (fl->fh_array[i]->size > NFS_MAXFHSIZE) {
  607. printk(KERN_ERR "NFS: Too big fh %d received %d\n",
  608. i, fl->fh_array[i]->size);
  609. goto out_err;
  610. }
  611. p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
  612. if (unlikely(!p))
  613. goto out_err;
  614. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  615. dprintk("DEBUG: %s: fh len %d\n", __func__,
  616. fl->fh_array[i]->size);
  617. }
  618. __free_page(scratch);
  619. return 0;
  620. out_err:
  621. __free_page(scratch);
  622. return -EIO;
  623. }
  624. static void
  625. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  626. {
  627. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  628. dprintk("--> %s\n", __func__);
  629. if (fl->dsaddr != NULL)
  630. nfs4_fl_put_deviceid(fl->dsaddr);
  631. /* This assumes a single RW lseg */
  632. if (lseg->pls_range.iomode == IOMODE_RW) {
  633. struct nfs4_filelayout *flo;
  634. struct inode *inode;
  635. flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
  636. inode = flo->generic_hdr.plh_inode;
  637. spin_lock(&inode->i_lock);
  638. pnfs_generic_ds_cinfo_release_lseg(&flo->commit_info, lseg);
  639. spin_unlock(&inode->i_lock);
  640. }
  641. _filelayout_free_lseg(fl);
  642. }
  643. static struct pnfs_layout_segment *
  644. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  645. struct nfs4_layoutget_res *lgr,
  646. gfp_t gfp_flags)
  647. {
  648. struct nfs4_filelayout_segment *fl;
  649. int rc;
  650. dprintk("--> %s\n", __func__);
  651. fl = kzalloc(sizeof(*fl), gfp_flags);
  652. if (!fl)
  653. return NULL;
  654. rc = filelayout_decode_layout(layoutid, fl, lgr, gfp_flags);
  655. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, gfp_flags)) {
  656. _filelayout_free_lseg(fl);
  657. return NULL;
  658. }
  659. return &fl->generic_hdr;
  660. }
  661. static bool
  662. filelayout_lseg_is_striped(const struct nfs4_filelayout_segment *flseg)
  663. {
  664. return flseg->num_fh > 1;
  665. }
  666. /*
  667. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  668. *
  669. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  670. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  671. */
  672. static size_t
  673. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  674. struct nfs_page *req)
  675. {
  676. unsigned int size;
  677. u64 p_stripe, r_stripe;
  678. u32 stripe_offset;
  679. u64 segment_offset = pgio->pg_lseg->pls_range.offset;
  680. u32 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  681. /* calls nfs_generic_pg_test */
  682. size = pnfs_generic_pg_test(pgio, prev, req);
  683. if (!size)
  684. return 0;
  685. else if (!filelayout_lseg_is_striped(FILELAYOUT_LSEG(pgio->pg_lseg)))
  686. return size;
  687. /* see if req and prev are in the same stripe */
  688. if (prev) {
  689. p_stripe = (u64)req_offset(prev) - segment_offset;
  690. r_stripe = (u64)req_offset(req) - segment_offset;
  691. do_div(p_stripe, stripe_unit);
  692. do_div(r_stripe, stripe_unit);
  693. if (p_stripe != r_stripe)
  694. return 0;
  695. }
  696. /* calculate remaining bytes in the current stripe */
  697. div_u64_rem((u64)req_offset(req) - segment_offset,
  698. stripe_unit,
  699. &stripe_offset);
  700. WARN_ON_ONCE(stripe_offset > stripe_unit);
  701. if (stripe_offset >= stripe_unit)
  702. return 0;
  703. return min(stripe_unit - (unsigned int)stripe_offset, size);
  704. }
  705. static struct pnfs_layout_segment *
  706. fl_pnfs_update_layout(struct inode *ino,
  707. struct nfs_open_context *ctx,
  708. loff_t pos,
  709. u64 count,
  710. enum pnfs_iomode iomode,
  711. bool strict_iomode,
  712. gfp_t gfp_flags)
  713. {
  714. struct pnfs_layout_segment *lseg = NULL;
  715. struct pnfs_layout_hdr *lo;
  716. struct nfs4_filelayout_segment *fl;
  717. int status;
  718. lseg = pnfs_update_layout(ino, ctx, pos, count, iomode, strict_iomode,
  719. gfp_flags);
  720. if (IS_ERR(lseg)) {
  721. /* Fall back to MDS on recoverable errors */
  722. if (!nfs_error_is_fatal_on_server(PTR_ERR(lseg)))
  723. lseg = NULL;
  724. goto out;
  725. } else if (!lseg)
  726. goto out;
  727. lo = NFS_I(ino)->layout;
  728. fl = FILELAYOUT_LSEG(lseg);
  729. status = filelayout_check_deviceid(lo, fl, gfp_flags);
  730. if (status) {
  731. pnfs_error_mark_layout_for_return(ino, lseg);
  732. pnfs_set_lo_fail(lseg);
  733. pnfs_put_lseg(lseg);
  734. lseg = NULL;
  735. }
  736. out:
  737. return lseg;
  738. }
  739. static void
  740. filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  741. struct nfs_page *req)
  742. {
  743. pnfs_generic_pg_check_layout(pgio, req);
  744. if (!pgio->pg_lseg) {
  745. pgio->pg_lseg = fl_pnfs_update_layout(pgio->pg_inode,
  746. nfs_req_openctx(req),
  747. req_offset(req),
  748. req->wb_bytes,
  749. IOMODE_READ,
  750. false,
  751. nfs_io_gfp_mask());
  752. if (IS_ERR(pgio->pg_lseg)) {
  753. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  754. pgio->pg_lseg = NULL;
  755. return;
  756. }
  757. }
  758. /* If no lseg, fall back to read through mds */
  759. if (pgio->pg_lseg == NULL)
  760. nfs_pageio_reset_read_mds(pgio);
  761. }
  762. static void
  763. filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  764. struct nfs_page *req)
  765. {
  766. pnfs_generic_pg_check_layout(pgio, req);
  767. if (!pgio->pg_lseg) {
  768. pgio->pg_lseg = fl_pnfs_update_layout(pgio->pg_inode,
  769. nfs_req_openctx(req),
  770. req_offset(req),
  771. req->wb_bytes,
  772. IOMODE_RW,
  773. false,
  774. nfs_io_gfp_mask());
  775. if (IS_ERR(pgio->pg_lseg)) {
  776. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  777. pgio->pg_lseg = NULL;
  778. return;
  779. }
  780. }
  781. /* If no lseg, fall back to write through mds */
  782. if (pgio->pg_lseg == NULL)
  783. nfs_pageio_reset_write_mds(pgio);
  784. }
  785. static const struct nfs_pageio_ops filelayout_pg_read_ops = {
  786. .pg_init = filelayout_pg_init_read,
  787. .pg_test = filelayout_pg_test,
  788. .pg_doio = pnfs_generic_pg_readpages,
  789. .pg_cleanup = pnfs_generic_pg_cleanup,
  790. };
  791. static const struct nfs_pageio_ops filelayout_pg_write_ops = {
  792. .pg_init = filelayout_pg_init_write,
  793. .pg_test = filelayout_pg_test,
  794. .pg_doio = pnfs_generic_pg_writepages,
  795. .pg_cleanup = pnfs_generic_pg_cleanup,
  796. };
  797. static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
  798. {
  799. if (fl->stripe_type == STRIPE_SPARSE)
  800. return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
  801. else
  802. return j;
  803. }
  804. static void
  805. filelayout_mark_request_commit(struct nfs_page *req,
  806. struct pnfs_layout_segment *lseg,
  807. struct nfs_commit_info *cinfo,
  808. u32 ds_commit_idx)
  809. {
  810. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  811. u32 i, j;
  812. if (fl->commit_through_mds) {
  813. nfs_request_add_commit_list(req, cinfo);
  814. } else {
  815. /* Note that we are calling nfs4_fl_calc_j_index on each page
  816. * that ends up being committed to a data server. An attractive
  817. * alternative is to add a field to nfs_write_data and nfs_page
  818. * to store the value calculated in filelayout_write_pagelist
  819. * and just use that here.
  820. */
  821. j = nfs4_fl_calc_j_index(lseg, req_offset(req));
  822. i = select_bucket_index(fl, j);
  823. pnfs_layout_mark_request_commit(req, lseg, cinfo, i);
  824. }
  825. }
  826. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  827. {
  828. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  829. if (flseg->stripe_type == STRIPE_SPARSE)
  830. return i;
  831. else
  832. return nfs4_fl_calc_ds_index(lseg, i);
  833. }
  834. static struct nfs_fh *
  835. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  836. {
  837. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  838. if (flseg->stripe_type == STRIPE_SPARSE) {
  839. if (flseg->num_fh == 1)
  840. i = 0;
  841. else if (flseg->num_fh == 0)
  842. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  843. return NULL;
  844. }
  845. return flseg->fh_array[i];
  846. }
  847. static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
  848. {
  849. struct pnfs_layout_segment *lseg = data->lseg;
  850. struct nfs4_pnfs_ds *ds;
  851. struct rpc_clnt *ds_clnt;
  852. u32 idx;
  853. struct nfs_fh *fh;
  854. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  855. ds = nfs4_fl_prepare_ds(lseg, idx);
  856. if (!ds)
  857. goto out_err;
  858. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, data->inode);
  859. if (IS_ERR(ds_clnt))
  860. goto out_err;
  861. dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
  862. data->inode->i_ino, how, refcount_read(&ds->ds_clp->cl_count));
  863. data->commit_done_cb = filelayout_commit_done_cb;
  864. refcount_inc(&ds->ds_clp->cl_count);
  865. data->ds_clp = ds->ds_clp;
  866. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  867. if (fh)
  868. data->args.fh = fh;
  869. return nfs_initiate_commit(ds_clnt, data, NFS_PROTO(data->inode),
  870. &filelayout_commit_call_ops, how,
  871. RPC_TASK_SOFTCONN, NULL);
  872. out_err:
  873. pnfs_generic_prepare_to_resend_writes(data);
  874. pnfs_generic_commit_release(data);
  875. return -EAGAIN;
  876. }
  877. static int
  878. filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  879. int how, struct nfs_commit_info *cinfo)
  880. {
  881. return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
  882. filelayout_initiate_commit);
  883. }
  884. static struct nfs4_deviceid_node *
  885. filelayout_alloc_deviceid_node(struct nfs_server *server,
  886. struct pnfs_device *pdev, gfp_t gfp_flags)
  887. {
  888. struct nfs4_file_layout_dsaddr *dsaddr;
  889. dsaddr = nfs4_fl_alloc_deviceid_node(server, pdev, gfp_flags);
  890. if (!dsaddr)
  891. return NULL;
  892. return &dsaddr->id_node;
  893. }
  894. static void
  895. filelayout_free_deviceid_node(struct nfs4_deviceid_node *d)
  896. {
  897. nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
  898. }
  899. static struct pnfs_layout_hdr *
  900. filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
  901. {
  902. struct nfs4_filelayout *flo;
  903. flo = kzalloc(sizeof(*flo), gfp_flags);
  904. if (flo == NULL)
  905. return NULL;
  906. pnfs_init_ds_commit_info(&flo->commit_info);
  907. flo->commit_info.ops = &filelayout_commit_ops;
  908. return &flo->generic_hdr;
  909. }
  910. static void
  911. filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
  912. {
  913. kfree_rcu(FILELAYOUT_FROM_HDR(lo), generic_hdr.plh_rcu);
  914. }
  915. static struct pnfs_ds_commit_info *
  916. filelayout_get_ds_info(struct inode *inode)
  917. {
  918. struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
  919. if (layout == NULL)
  920. return NULL;
  921. else
  922. return &FILELAYOUT_FROM_HDR(layout)->commit_info;
  923. }
  924. static void
  925. filelayout_setup_ds_info(struct pnfs_ds_commit_info *fl_cinfo,
  926. struct pnfs_layout_segment *lseg)
  927. {
  928. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  929. struct inode *inode = lseg->pls_layout->plh_inode;
  930. struct pnfs_commit_array *array, *new;
  931. unsigned int size = (fl->stripe_type == STRIPE_SPARSE) ?
  932. fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
  933. new = pnfs_alloc_commit_array(size, nfs_io_gfp_mask());
  934. if (new) {
  935. spin_lock(&inode->i_lock);
  936. array = pnfs_add_commit_array(fl_cinfo, new, lseg);
  937. spin_unlock(&inode->i_lock);
  938. if (array != new)
  939. pnfs_free_commit_array(new);
  940. }
  941. }
  942. static void
  943. filelayout_release_ds_info(struct pnfs_ds_commit_info *fl_cinfo,
  944. struct inode *inode)
  945. {
  946. spin_lock(&inode->i_lock);
  947. pnfs_generic_ds_cinfo_destroy(fl_cinfo);
  948. spin_unlock(&inode->i_lock);
  949. }
  950. static const struct pnfs_commit_ops filelayout_commit_ops = {
  951. .setup_ds_info = filelayout_setup_ds_info,
  952. .release_ds_info = filelayout_release_ds_info,
  953. .mark_request_commit = filelayout_mark_request_commit,
  954. .clear_request_commit = pnfs_generic_clear_request_commit,
  955. .scan_commit_lists = pnfs_generic_scan_commit_lists,
  956. .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
  957. .commit_pagelist = filelayout_commit_pagelist,
  958. };
  959. static struct pnfs_layoutdriver_type filelayout_type = {
  960. .id = LAYOUT_NFSV4_1_FILES,
  961. .name = "LAYOUT_NFSV4_1_FILES",
  962. .owner = THIS_MODULE,
  963. .flags = PNFS_LAYOUTGET_ON_OPEN,
  964. .max_layoutget_response = 4096, /* 1 page or so... */
  965. .alloc_layout_hdr = filelayout_alloc_layout_hdr,
  966. .free_layout_hdr = filelayout_free_layout_hdr,
  967. .alloc_lseg = filelayout_alloc_lseg,
  968. .free_lseg = filelayout_free_lseg,
  969. .pg_read_ops = &filelayout_pg_read_ops,
  970. .pg_write_ops = &filelayout_pg_write_ops,
  971. .get_ds_info = &filelayout_get_ds_info,
  972. .read_pagelist = filelayout_read_pagelist,
  973. .write_pagelist = filelayout_write_pagelist,
  974. .alloc_deviceid_node = filelayout_alloc_deviceid_node,
  975. .free_deviceid_node = filelayout_free_deviceid_node,
  976. .sync = pnfs_nfs_generic_sync,
  977. };
  978. static int __init nfs4filelayout_init(void)
  979. {
  980. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  981. __func__);
  982. return pnfs_register_layoutdriver(&filelayout_type);
  983. }
  984. static void __exit nfs4filelayout_exit(void)
  985. {
  986. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  987. __func__);
  988. pnfs_unregister_layoutdriver(&filelayout_type);
  989. }
  990. MODULE_ALIAS("nfs-layouttype4-1");
  991. module_init(nfs4filelayout_init);
  992. module_exit(nfs4filelayout_exit);