nfs4filelayout.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 "internal.h"
  34. #include "nfs4filelayout.h"
  35. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  36. MODULE_LICENSE("GPL");
  37. MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
  38. MODULE_DESCRIPTION("The NFSv4 file layout driver");
  39. #define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
  40. static loff_t
  41. filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
  42. loff_t offset)
  43. {
  44. u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
  45. u64 tmp;
  46. offset -= flseg->pattern_offset;
  47. tmp = offset;
  48. do_div(tmp, stripe_width);
  49. return tmp * flseg->stripe_unit + do_div(offset, flseg->stripe_unit);
  50. }
  51. /* This function is used by the layout driver to calculate the
  52. * offset of the file on the dserver based on whether the
  53. * layout type is STRIPE_DENSE or STRIPE_SPARSE
  54. */
  55. static loff_t
  56. filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
  57. {
  58. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  59. switch (flseg->stripe_type) {
  60. case STRIPE_SPARSE:
  61. return offset;
  62. case STRIPE_DENSE:
  63. return filelayout_get_dense_offset(flseg, offset);
  64. }
  65. BUG();
  66. }
  67. /* For data server errors we don't recover from */
  68. static void
  69. filelayout_set_lo_fail(struct pnfs_layout_segment *lseg)
  70. {
  71. if (lseg->pls_range.iomode == IOMODE_RW) {
  72. dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
  73. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  74. } else {
  75. dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
  76. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  77. }
  78. }
  79. static int filelayout_async_handle_error(struct rpc_task *task,
  80. struct nfs4_state *state,
  81. struct nfs_client *clp,
  82. int *reset)
  83. {
  84. if (task->tk_status >= 0)
  85. return 0;
  86. *reset = 0;
  87. switch (task->tk_status) {
  88. case -NFS4ERR_BADSESSION:
  89. case -NFS4ERR_BADSLOT:
  90. case -NFS4ERR_BAD_HIGH_SLOT:
  91. case -NFS4ERR_DEADSESSION:
  92. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  93. case -NFS4ERR_SEQ_FALSE_RETRY:
  94. case -NFS4ERR_SEQ_MISORDERED:
  95. dprintk("%s ERROR %d, Reset session. Exchangeid "
  96. "flags 0x%x\n", __func__, task->tk_status,
  97. clp->cl_exchange_flags);
  98. nfs4_schedule_session_recovery(clp->cl_session);
  99. break;
  100. case -NFS4ERR_DELAY:
  101. case -NFS4ERR_GRACE:
  102. case -EKEYEXPIRED:
  103. rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
  104. break;
  105. case -NFS4ERR_RETRY_UNCACHED_REP:
  106. break;
  107. default:
  108. dprintk("%s DS error. Retry through MDS %d\n", __func__,
  109. task->tk_status);
  110. *reset = 1;
  111. break;
  112. }
  113. task->tk_status = 0;
  114. return -EAGAIN;
  115. }
  116. /* NFS_PROTO call done callback routines */
  117. static int filelayout_read_done_cb(struct rpc_task *task,
  118. struct nfs_read_data *data)
  119. {
  120. struct nfs_client *clp = data->ds_clp;
  121. int reset = 0;
  122. dprintk("%s DS read\n", __func__);
  123. if (filelayout_async_handle_error(task, data->args.context->state,
  124. data->ds_clp, &reset) == -EAGAIN) {
  125. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  126. __func__, data->ds_clp, data->ds_clp->cl_session);
  127. if (reset) {
  128. filelayout_set_lo_fail(data->lseg);
  129. nfs4_reset_read(task, data);
  130. clp = NFS_SERVER(data->inode)->nfs_client;
  131. }
  132. nfs_restart_rpc(task, clp);
  133. return -EAGAIN;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * We reference the rpc_cred of the first WRITE that triggers the need for
  139. * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
  140. * rfc5661 is not clear about which credential should be used.
  141. */
  142. static void
  143. filelayout_set_layoutcommit(struct nfs_write_data *wdata)
  144. {
  145. if (FILELAYOUT_LSEG(wdata->lseg)->commit_through_mds ||
  146. wdata->res.verf->committed == NFS_FILE_SYNC)
  147. return;
  148. pnfs_set_layoutcommit(wdata);
  149. dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, wdata->inode->i_ino,
  150. (unsigned long) wdata->lseg->pls_end_pos);
  151. }
  152. /*
  153. * Call ops for the async read/write cases
  154. * In the case of dense layouts, the offset needs to be reset to its
  155. * original value.
  156. */
  157. static void filelayout_read_prepare(struct rpc_task *task, void *data)
  158. {
  159. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  160. rdata->read_done_cb = filelayout_read_done_cb;
  161. if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
  162. &rdata->args.seq_args, &rdata->res.seq_res,
  163. 0, task))
  164. return;
  165. rpc_call_start(task);
  166. }
  167. static void filelayout_read_call_done(struct rpc_task *task, void *data)
  168. {
  169. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  170. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  171. /* Note this may cause RPC to be resent */
  172. rdata->mds_ops->rpc_call_done(task, data);
  173. }
  174. static void filelayout_read_release(void *data)
  175. {
  176. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  177. rdata->mds_ops->rpc_release(data);
  178. }
  179. static int filelayout_write_done_cb(struct rpc_task *task,
  180. struct nfs_write_data *data)
  181. {
  182. int reset = 0;
  183. if (filelayout_async_handle_error(task, data->args.context->state,
  184. data->ds_clp, &reset) == -EAGAIN) {
  185. struct nfs_client *clp;
  186. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  187. __func__, data->ds_clp, data->ds_clp->cl_session);
  188. if (reset) {
  189. filelayout_set_lo_fail(data->lseg);
  190. nfs4_reset_write(task, data);
  191. clp = NFS_SERVER(data->inode)->nfs_client;
  192. } else
  193. clp = data->ds_clp;
  194. nfs_restart_rpc(task, clp);
  195. return -EAGAIN;
  196. }
  197. filelayout_set_layoutcommit(data);
  198. return 0;
  199. }
  200. /* Fake up some data that will cause nfs_commit_release to retry the writes. */
  201. static void prepare_to_resend_writes(struct nfs_write_data *data)
  202. {
  203. struct nfs_page *first = nfs_list_entry(data->pages.next);
  204. data->task.tk_status = 0;
  205. memcpy(data->verf.verifier, first->wb_verf.verifier,
  206. sizeof(first->wb_verf.verifier));
  207. data->verf.verifier[0]++; /* ensure verifier mismatch */
  208. }
  209. static int filelayout_commit_done_cb(struct rpc_task *task,
  210. struct nfs_write_data *data)
  211. {
  212. int reset = 0;
  213. if (filelayout_async_handle_error(task, data->args.context->state,
  214. data->ds_clp, &reset) == -EAGAIN) {
  215. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  216. __func__, data->ds_clp, data->ds_clp->cl_session);
  217. if (reset) {
  218. prepare_to_resend_writes(data);
  219. filelayout_set_lo_fail(data->lseg);
  220. } else
  221. nfs_restart_rpc(task, data->ds_clp);
  222. return -EAGAIN;
  223. }
  224. return 0;
  225. }
  226. static void filelayout_write_prepare(struct rpc_task *task, void *data)
  227. {
  228. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  229. if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
  230. &wdata->args.seq_args, &wdata->res.seq_res,
  231. 0, task))
  232. return;
  233. rpc_call_start(task);
  234. }
  235. static void filelayout_write_call_done(struct rpc_task *task, void *data)
  236. {
  237. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  238. /* Note this may cause RPC to be resent */
  239. wdata->mds_ops->rpc_call_done(task, data);
  240. }
  241. static void filelayout_write_release(void *data)
  242. {
  243. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  244. wdata->mds_ops->rpc_release(data);
  245. }
  246. static void filelayout_commit_release(void *data)
  247. {
  248. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  249. nfs_commit_release_pages(wdata);
  250. if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
  251. nfs_commit_clear_lock(NFS_I(wdata->inode));
  252. nfs_commitdata_release(wdata);
  253. }
  254. struct rpc_call_ops filelayout_read_call_ops = {
  255. .rpc_call_prepare = filelayout_read_prepare,
  256. .rpc_call_done = filelayout_read_call_done,
  257. .rpc_release = filelayout_read_release,
  258. };
  259. struct rpc_call_ops filelayout_write_call_ops = {
  260. .rpc_call_prepare = filelayout_write_prepare,
  261. .rpc_call_done = filelayout_write_call_done,
  262. .rpc_release = filelayout_write_release,
  263. };
  264. struct rpc_call_ops filelayout_commit_call_ops = {
  265. .rpc_call_prepare = filelayout_write_prepare,
  266. .rpc_call_done = filelayout_write_call_done,
  267. .rpc_release = filelayout_commit_release,
  268. };
  269. static enum pnfs_try_status
  270. filelayout_read_pagelist(struct nfs_read_data *data)
  271. {
  272. struct pnfs_layout_segment *lseg = data->lseg;
  273. struct nfs4_pnfs_ds *ds;
  274. loff_t offset = data->args.offset;
  275. u32 j, idx;
  276. struct nfs_fh *fh;
  277. int status;
  278. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  279. __func__, data->inode->i_ino,
  280. data->args.pgbase, (size_t)data->args.count, offset);
  281. /* Retrieve the correct rpc_client for the byte range */
  282. j = nfs4_fl_calc_j_index(lseg, offset);
  283. idx = nfs4_fl_calc_ds_index(lseg, j);
  284. ds = nfs4_fl_prepare_ds(lseg, idx);
  285. if (!ds) {
  286. /* Either layout fh index faulty, or ds connect failed */
  287. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  288. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  289. return PNFS_NOT_ATTEMPTED;
  290. }
  291. dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
  292. /* No multipath support. Use first DS */
  293. data->ds_clp = ds->ds_clp;
  294. fh = nfs4_fl_select_ds_fh(lseg, j);
  295. if (fh)
  296. data->args.fh = fh;
  297. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  298. data->mds_offset = offset;
  299. /* Perform an asynchronous read to ds */
  300. status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
  301. &filelayout_read_call_ops);
  302. BUG_ON(status != 0);
  303. return PNFS_ATTEMPTED;
  304. }
  305. /* Perform async writes. */
  306. static enum pnfs_try_status
  307. filelayout_write_pagelist(struct nfs_write_data *data, int sync)
  308. {
  309. struct pnfs_layout_segment *lseg = data->lseg;
  310. struct nfs4_pnfs_ds *ds;
  311. loff_t offset = data->args.offset;
  312. u32 j, idx;
  313. struct nfs_fh *fh;
  314. int status;
  315. /* Retrieve the correct rpc_client for the byte range */
  316. j = nfs4_fl_calc_j_index(lseg, offset);
  317. idx = nfs4_fl_calc_ds_index(lseg, j);
  318. ds = nfs4_fl_prepare_ds(lseg, idx);
  319. if (!ds) {
  320. printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
  321. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  322. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  323. return PNFS_NOT_ATTEMPTED;
  324. }
  325. dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
  326. data->inode->i_ino, sync, (size_t) data->args.count, offset,
  327. ds->ds_remotestr);
  328. data->write_done_cb = filelayout_write_done_cb;
  329. data->ds_clp = ds->ds_clp;
  330. fh = nfs4_fl_select_ds_fh(lseg, j);
  331. if (fh)
  332. data->args.fh = fh;
  333. /*
  334. * Get the file offset on the dserver. Set the write offset to
  335. * this offset and save the original offset.
  336. */
  337. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  338. /* Perform an asynchronous write */
  339. status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
  340. &filelayout_write_call_ops, sync);
  341. BUG_ON(status != 0);
  342. return PNFS_ATTEMPTED;
  343. }
  344. /*
  345. * filelayout_check_layout()
  346. *
  347. * Make sure layout segment parameters are sane WRT the device.
  348. * At this point no generic layer initialization of the lseg has occurred,
  349. * and nothing has been added to the layout_hdr cache.
  350. *
  351. */
  352. static int
  353. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  354. struct nfs4_filelayout_segment *fl,
  355. struct nfs4_layoutget_res *lgr,
  356. struct nfs4_deviceid *id,
  357. gfp_t gfp_flags)
  358. {
  359. struct nfs4_deviceid_node *d;
  360. struct nfs4_file_layout_dsaddr *dsaddr;
  361. int status = -EINVAL;
  362. struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
  363. dprintk("--> %s\n", __func__);
  364. /* FIXME: remove this check when layout segment support is added */
  365. if (lgr->range.offset != 0 ||
  366. lgr->range.length != NFS4_MAX_UINT64) {
  367. dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
  368. __func__);
  369. goto out;
  370. }
  371. if (fl->pattern_offset > lgr->range.offset) {
  372. dprintk("%s pattern_offset %lld too large\n",
  373. __func__, fl->pattern_offset);
  374. goto out;
  375. }
  376. if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
  377. dprintk("%s Invalid stripe unit (%u)\n",
  378. __func__, fl->stripe_unit);
  379. goto out;
  380. }
  381. /* find and reference the deviceid */
  382. d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
  383. NFS_SERVER(lo->plh_inode)->nfs_client, id);
  384. if (d == NULL) {
  385. dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
  386. if (dsaddr == NULL)
  387. goto out;
  388. } else
  389. dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  390. fl->dsaddr = dsaddr;
  391. if (fl->first_stripe_index < 0 ||
  392. fl->first_stripe_index >= dsaddr->stripe_count) {
  393. dprintk("%s Bad first_stripe_index %d\n",
  394. __func__, fl->first_stripe_index);
  395. goto out_put;
  396. }
  397. if ((fl->stripe_type == STRIPE_SPARSE &&
  398. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  399. (fl->stripe_type == STRIPE_DENSE &&
  400. fl->num_fh != dsaddr->stripe_count)) {
  401. dprintk("%s num_fh %u not valid for given packing\n",
  402. __func__, fl->num_fh);
  403. goto out_put;
  404. }
  405. if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
  406. dprintk("%s Stripe unit (%u) not aligned with rsize %u "
  407. "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
  408. nfss->wsize);
  409. }
  410. status = 0;
  411. out:
  412. dprintk("--> %s returns %d\n", __func__, status);
  413. return status;
  414. out_put:
  415. nfs4_fl_put_deviceid(dsaddr);
  416. goto out;
  417. }
  418. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  419. {
  420. int i;
  421. for (i = 0; i < fl->num_fh; i++) {
  422. if (!fl->fh_array[i])
  423. break;
  424. kfree(fl->fh_array[i]);
  425. }
  426. kfree(fl->fh_array);
  427. fl->fh_array = NULL;
  428. }
  429. static void
  430. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  431. {
  432. filelayout_free_fh_array(fl);
  433. kfree(fl);
  434. }
  435. static int
  436. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  437. struct nfs4_filelayout_segment *fl,
  438. struct nfs4_layoutget_res *lgr,
  439. struct nfs4_deviceid *id,
  440. gfp_t gfp_flags)
  441. {
  442. struct xdr_stream stream;
  443. struct xdr_buf buf;
  444. struct page *scratch;
  445. __be32 *p;
  446. uint32_t nfl_util;
  447. int i;
  448. dprintk("%s: set_layout_map Begin\n", __func__);
  449. scratch = alloc_page(gfp_flags);
  450. if (!scratch)
  451. return -ENOMEM;
  452. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
  453. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  454. /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
  455. * num_fh (4) */
  456. p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
  457. if (unlikely(!p))
  458. goto out_err;
  459. memcpy(id, p, sizeof(*id));
  460. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  461. nfs4_print_deviceid(id);
  462. nfl_util = be32_to_cpup(p++);
  463. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  464. fl->commit_through_mds = 1;
  465. if (nfl_util & NFL4_UFLG_DENSE)
  466. fl->stripe_type = STRIPE_DENSE;
  467. else
  468. fl->stripe_type = STRIPE_SPARSE;
  469. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  470. fl->first_stripe_index = be32_to_cpup(p++);
  471. p = xdr_decode_hyper(p, &fl->pattern_offset);
  472. fl->num_fh = be32_to_cpup(p++);
  473. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  474. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  475. fl->pattern_offset);
  476. /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
  477. * Futher checking is done in filelayout_check_layout */
  478. if (fl->num_fh < 0 || fl->num_fh >
  479. max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
  480. goto out_err;
  481. if (fl->num_fh > 0) {
  482. fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
  483. gfp_flags);
  484. if (!fl->fh_array)
  485. goto out_err;
  486. }
  487. for (i = 0; i < fl->num_fh; i++) {
  488. /* Do we want to use a mempool here? */
  489. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
  490. if (!fl->fh_array[i])
  491. goto out_err_free;
  492. p = xdr_inline_decode(&stream, 4);
  493. if (unlikely(!p))
  494. goto out_err_free;
  495. fl->fh_array[i]->size = be32_to_cpup(p++);
  496. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  497. printk(KERN_ERR "Too big fh %d received %d\n",
  498. i, fl->fh_array[i]->size);
  499. goto out_err_free;
  500. }
  501. p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
  502. if (unlikely(!p))
  503. goto out_err_free;
  504. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  505. dprintk("DEBUG: %s: fh len %d\n", __func__,
  506. fl->fh_array[i]->size);
  507. }
  508. __free_page(scratch);
  509. return 0;
  510. out_err_free:
  511. filelayout_free_fh_array(fl);
  512. out_err:
  513. __free_page(scratch);
  514. return -EIO;
  515. }
  516. static void
  517. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  518. {
  519. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  520. dprintk("--> %s\n", __func__);
  521. nfs4_fl_put_deviceid(fl->dsaddr);
  522. kfree(fl->commit_buckets);
  523. _filelayout_free_lseg(fl);
  524. }
  525. static struct pnfs_layout_segment *
  526. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  527. struct nfs4_layoutget_res *lgr,
  528. gfp_t gfp_flags)
  529. {
  530. struct nfs4_filelayout_segment *fl;
  531. int rc;
  532. struct nfs4_deviceid id;
  533. dprintk("--> %s\n", __func__);
  534. fl = kzalloc(sizeof(*fl), gfp_flags);
  535. if (!fl)
  536. return NULL;
  537. rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
  538. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
  539. _filelayout_free_lseg(fl);
  540. return NULL;
  541. }
  542. /* This assumes there is only one IOMODE_RW lseg. What
  543. * we really want to do is have a layout_hdr level
  544. * dictionary of <multipath_list4, fh> keys, each
  545. * associated with a struct list_head, populated by calls
  546. * to filelayout_write_pagelist().
  547. * */
  548. if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
  549. int i;
  550. int size = (fl->stripe_type == STRIPE_SPARSE) ?
  551. fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
  552. fl->commit_buckets = kcalloc(size, sizeof(struct list_head), gfp_flags);
  553. if (!fl->commit_buckets) {
  554. filelayout_free_lseg(&fl->generic_hdr);
  555. return NULL;
  556. }
  557. fl->number_of_buckets = size;
  558. for (i = 0; i < size; i++)
  559. INIT_LIST_HEAD(&fl->commit_buckets[i]);
  560. }
  561. return &fl->generic_hdr;
  562. }
  563. /*
  564. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  565. *
  566. * return true : coalesce page
  567. * return false : don't coalesce page
  568. */
  569. static bool
  570. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  571. struct nfs_page *req)
  572. {
  573. u64 p_stripe, r_stripe;
  574. u32 stripe_unit;
  575. if (!pnfs_generic_pg_test(pgio, prev, req) ||
  576. !nfs_generic_pg_test(pgio, prev, req))
  577. return false;
  578. p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
  579. r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
  580. stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  581. do_div(p_stripe, stripe_unit);
  582. do_div(r_stripe, stripe_unit);
  583. return (p_stripe == r_stripe);
  584. }
  585. void
  586. filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  587. struct nfs_page *req)
  588. {
  589. BUG_ON(pgio->pg_lseg != NULL);
  590. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  591. req->wb_context,
  592. 0,
  593. NFS4_MAX_UINT64,
  594. IOMODE_READ,
  595. GFP_KERNEL);
  596. /* If no lseg, fall back to read through mds */
  597. if (pgio->pg_lseg == NULL)
  598. nfs_pageio_init_read_mds(pgio, pgio->pg_inode);
  599. }
  600. void
  601. filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  602. struct nfs_page *req)
  603. {
  604. BUG_ON(pgio->pg_lseg != NULL);
  605. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  606. req->wb_context,
  607. 0,
  608. NFS4_MAX_UINT64,
  609. IOMODE_RW,
  610. GFP_NOFS);
  611. /* If no lseg, fall back to write through mds */
  612. if (pgio->pg_lseg == NULL)
  613. nfs_pageio_init_write_mds(pgio, pgio->pg_inode,
  614. pgio->pg_ioflags);
  615. }
  616. static const struct nfs_pageio_ops filelayout_pg_read_ops = {
  617. .pg_init = filelayout_pg_init_read,
  618. .pg_test = filelayout_pg_test,
  619. .pg_doio = nfs_generic_pg_readpages,
  620. };
  621. static const struct nfs_pageio_ops filelayout_pg_write_ops = {
  622. .pg_init = filelayout_pg_init_write,
  623. .pg_test = filelayout_pg_test,
  624. .pg_doio = nfs_generic_pg_writepages,
  625. };
  626. static bool filelayout_mark_pnfs_commit(struct pnfs_layout_segment *lseg)
  627. {
  628. return !FILELAYOUT_LSEG(lseg)->commit_through_mds;
  629. }
  630. static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
  631. {
  632. if (fl->stripe_type == STRIPE_SPARSE)
  633. return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
  634. else
  635. return j;
  636. }
  637. struct list_head *filelayout_choose_commit_list(struct nfs_page *req)
  638. {
  639. struct pnfs_layout_segment *lseg = req->wb_commit_lseg;
  640. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  641. u32 i, j;
  642. struct list_head *list;
  643. /* Note that we are calling nfs4_fl_calc_j_index on each page
  644. * that ends up being committed to a data server. An attractive
  645. * alternative is to add a field to nfs_write_data and nfs_page
  646. * to store the value calculated in filelayout_write_pagelist
  647. * and just use that here.
  648. */
  649. j = nfs4_fl_calc_j_index(lseg,
  650. (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
  651. i = select_bucket_index(fl, j);
  652. list = &fl->commit_buckets[i];
  653. if (list_empty(list)) {
  654. /* Non-empty buckets hold a reference on the lseg */
  655. get_lseg(lseg);
  656. }
  657. return list;
  658. }
  659. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  660. {
  661. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  662. if (flseg->stripe_type == STRIPE_SPARSE)
  663. return i;
  664. else
  665. return nfs4_fl_calc_ds_index(lseg, i);
  666. }
  667. static struct nfs_fh *
  668. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  669. {
  670. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  671. if (flseg->stripe_type == STRIPE_SPARSE) {
  672. if (flseg->num_fh == 1)
  673. i = 0;
  674. else if (flseg->num_fh == 0)
  675. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  676. return NULL;
  677. }
  678. return flseg->fh_array[i];
  679. }
  680. static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
  681. {
  682. struct pnfs_layout_segment *lseg = data->lseg;
  683. struct nfs4_pnfs_ds *ds;
  684. u32 idx;
  685. struct nfs_fh *fh;
  686. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  687. ds = nfs4_fl_prepare_ds(lseg, idx);
  688. if (!ds) {
  689. printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
  690. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  691. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  692. prepare_to_resend_writes(data);
  693. data->mds_ops->rpc_release(data);
  694. return -EAGAIN;
  695. }
  696. dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
  697. data->write_done_cb = filelayout_commit_done_cb;
  698. data->ds_clp = ds->ds_clp;
  699. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  700. if (fh)
  701. data->args.fh = fh;
  702. return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
  703. &filelayout_commit_call_ops, how);
  704. }
  705. /*
  706. * This is only useful while we are using whole file layouts.
  707. */
  708. static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
  709. {
  710. struct pnfs_layout_segment *lseg, *rv = NULL;
  711. spin_lock(&inode->i_lock);
  712. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
  713. if (lseg->pls_range.iomode == IOMODE_RW)
  714. rv = get_lseg(lseg);
  715. spin_unlock(&inode->i_lock);
  716. return rv;
  717. }
  718. static int alloc_ds_commits(struct inode *inode, struct list_head *list)
  719. {
  720. struct pnfs_layout_segment *lseg;
  721. struct nfs4_filelayout_segment *fl;
  722. struct nfs_write_data *data;
  723. int i, j;
  724. /* Won't need this when non-whole file layout segments are supported
  725. * instead we will use a pnfs_layout_hdr structure */
  726. lseg = find_only_write_lseg(inode);
  727. if (!lseg)
  728. return 0;
  729. fl = FILELAYOUT_LSEG(lseg);
  730. for (i = 0; i < fl->number_of_buckets; i++) {
  731. if (list_empty(&fl->commit_buckets[i]))
  732. continue;
  733. data = nfs_commitdata_alloc();
  734. if (!data)
  735. goto out_bad;
  736. data->ds_commit_index = i;
  737. data->lseg = lseg;
  738. list_add(&data->pages, list);
  739. }
  740. put_lseg(lseg);
  741. return 0;
  742. out_bad:
  743. for (j = i; j < fl->number_of_buckets; j++) {
  744. if (list_empty(&fl->commit_buckets[i]))
  745. continue;
  746. nfs_retry_commit(&fl->commit_buckets[i], lseg);
  747. put_lseg(lseg); /* associated with emptying bucket */
  748. }
  749. put_lseg(lseg);
  750. /* Caller will clean up entries put on list */
  751. return -ENOMEM;
  752. }
  753. /* This follows nfs_commit_list pretty closely */
  754. static int
  755. filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  756. int how)
  757. {
  758. struct nfs_write_data *data, *tmp;
  759. LIST_HEAD(list);
  760. if (!list_empty(mds_pages)) {
  761. data = nfs_commitdata_alloc();
  762. if (!data)
  763. goto out_bad;
  764. data->lseg = NULL;
  765. list_add(&data->pages, &list);
  766. }
  767. if (alloc_ds_commits(inode, &list))
  768. goto out_bad;
  769. list_for_each_entry_safe(data, tmp, &list, pages) {
  770. list_del_init(&data->pages);
  771. atomic_inc(&NFS_I(inode)->commits_outstanding);
  772. if (!data->lseg) {
  773. nfs_init_commit(data, mds_pages, NULL);
  774. nfs_initiate_commit(data, NFS_CLIENT(inode),
  775. data->mds_ops, how);
  776. } else {
  777. nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index], data->lseg);
  778. filelayout_initiate_commit(data, how);
  779. }
  780. }
  781. return 0;
  782. out_bad:
  783. list_for_each_entry_safe(data, tmp, &list, pages) {
  784. nfs_retry_commit(&data->pages, data->lseg);
  785. list_del_init(&data->pages);
  786. nfs_commit_free(data);
  787. }
  788. nfs_retry_commit(mds_pages, NULL);
  789. nfs_commit_clear_lock(NFS_I(inode));
  790. return -ENOMEM;
  791. }
  792. static void
  793. filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
  794. {
  795. nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
  796. }
  797. static struct pnfs_layoutdriver_type filelayout_type = {
  798. .id = LAYOUT_NFSV4_1_FILES,
  799. .name = "LAYOUT_NFSV4_1_FILES",
  800. .owner = THIS_MODULE,
  801. .alloc_lseg = filelayout_alloc_lseg,
  802. .free_lseg = filelayout_free_lseg,
  803. .pg_read_ops = &filelayout_pg_read_ops,
  804. .pg_write_ops = &filelayout_pg_write_ops,
  805. .mark_pnfs_commit = filelayout_mark_pnfs_commit,
  806. .choose_commit_list = filelayout_choose_commit_list,
  807. .commit_pagelist = filelayout_commit_pagelist,
  808. .read_pagelist = filelayout_read_pagelist,
  809. .write_pagelist = filelayout_write_pagelist,
  810. .free_deviceid_node = filelayout_free_deveiceid_node,
  811. };
  812. static int __init nfs4filelayout_init(void)
  813. {
  814. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  815. __func__);
  816. return pnfs_register_layoutdriver(&filelayout_type);
  817. }
  818. static void __exit nfs4filelayout_exit(void)
  819. {
  820. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  821. __func__);
  822. pnfs_unregister_layoutdriver(&filelayout_type);
  823. }
  824. module_init(nfs4filelayout_init);
  825. module_exit(nfs4filelayout_exit);