nfs4filelayout.c 25 KB

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