nfs4filelayout.c 24 KB

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