nfs4filelayout.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. uint32_t *p = (uint32_t *)lgr->layout.buf;
  430. uint32_t nfl_util;
  431. int i;
  432. dprintk("%s: set_layout_map Begin\n", __func__);
  433. memcpy(id, p, sizeof(*id));
  434. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  435. print_deviceid(id);
  436. nfl_util = be32_to_cpup(p++);
  437. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  438. fl->commit_through_mds = 1;
  439. if (nfl_util & NFL4_UFLG_DENSE)
  440. fl->stripe_type = STRIPE_DENSE;
  441. else
  442. fl->stripe_type = STRIPE_SPARSE;
  443. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  444. fl->first_stripe_index = be32_to_cpup(p++);
  445. p = xdr_decode_hyper(p, &fl->pattern_offset);
  446. fl->num_fh = be32_to_cpup(p++);
  447. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  448. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  449. fl->pattern_offset);
  450. fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
  451. GFP_KERNEL);
  452. if (!fl->fh_array)
  453. return -ENOMEM;
  454. for (i = 0; i < fl->num_fh; i++) {
  455. /* Do we want to use a mempool here? */
  456. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
  457. if (!fl->fh_array[i]) {
  458. filelayout_free_fh_array(fl);
  459. return -ENOMEM;
  460. }
  461. fl->fh_array[i]->size = be32_to_cpup(p++);
  462. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  463. printk(KERN_ERR "Too big fh %d received %d\n",
  464. i, fl->fh_array[i]->size);
  465. filelayout_free_fh_array(fl);
  466. return -EIO;
  467. }
  468. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  469. p += XDR_QUADLEN(fl->fh_array[i]->size);
  470. dprintk("DEBUG: %s: fh len %d\n", __func__,
  471. fl->fh_array[i]->size);
  472. }
  473. return 0;
  474. }
  475. static void
  476. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  477. {
  478. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  479. dprintk("--> %s\n", __func__);
  480. nfs4_fl_put_deviceid(fl->dsaddr);
  481. kfree(fl->commit_buckets);
  482. _filelayout_free_lseg(fl);
  483. }
  484. static struct pnfs_layout_segment *
  485. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  486. struct nfs4_layoutget_res *lgr)
  487. {
  488. struct nfs4_filelayout_segment *fl;
  489. int rc;
  490. struct nfs4_deviceid id;
  491. dprintk("--> %s\n", __func__);
  492. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  493. if (!fl)
  494. return NULL;
  495. rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
  496. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
  497. _filelayout_free_lseg(fl);
  498. return NULL;
  499. }
  500. /* This assumes there is only one IOMODE_RW lseg. What
  501. * we really want to do is have a layout_hdr level
  502. * dictionary of <multipath_list4, fh> keys, each
  503. * associated with a struct list_head, populated by calls
  504. * to filelayout_write_pagelist().
  505. * */
  506. if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
  507. int i;
  508. int size = (fl->stripe_type == STRIPE_SPARSE) ?
  509. fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
  510. fl->commit_buckets = kcalloc(size, sizeof(struct list_head), GFP_KERNEL);
  511. if (!fl->commit_buckets) {
  512. filelayout_free_lseg(&fl->generic_hdr);
  513. return NULL;
  514. }
  515. fl->number_of_buckets = size;
  516. for (i = 0; i < size; i++)
  517. INIT_LIST_HEAD(&fl->commit_buckets[i]);
  518. }
  519. return &fl->generic_hdr;
  520. }
  521. /*
  522. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  523. *
  524. * return 1 : coalesce page
  525. * return 0 : don't coalesce page
  526. */
  527. int
  528. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  529. struct nfs_page *req)
  530. {
  531. u64 p_stripe, r_stripe;
  532. u32 stripe_unit;
  533. if (!pgio->pg_lseg)
  534. return 1;
  535. p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
  536. r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
  537. stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  538. do_div(p_stripe, stripe_unit);
  539. do_div(r_stripe, stripe_unit);
  540. return (p_stripe == r_stripe);
  541. }
  542. static bool filelayout_mark_pnfs_commit(struct pnfs_layout_segment *lseg)
  543. {
  544. return !FILELAYOUT_LSEG(lseg)->commit_through_mds;
  545. }
  546. static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
  547. {
  548. if (fl->stripe_type == STRIPE_SPARSE)
  549. return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
  550. else
  551. return j;
  552. }
  553. struct list_head *filelayout_choose_commit_list(struct nfs_page *req)
  554. {
  555. struct pnfs_layout_segment *lseg = req->wb_commit_lseg;
  556. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  557. u32 i, j;
  558. struct list_head *list;
  559. /* Note that we are calling nfs4_fl_calc_j_index on each page
  560. * that ends up being committed to a data server. An attractive
  561. * alternative is to add a field to nfs_write_data and nfs_page
  562. * to store the value calculated in filelayout_write_pagelist
  563. * and just use that here.
  564. */
  565. j = nfs4_fl_calc_j_index(lseg,
  566. (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
  567. i = select_bucket_index(fl, j);
  568. list = &fl->commit_buckets[i];
  569. if (list_empty(list)) {
  570. /* Non-empty buckets hold a reference on the lseg */
  571. get_lseg(lseg);
  572. }
  573. return list;
  574. }
  575. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  576. {
  577. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  578. if (flseg->stripe_type == STRIPE_SPARSE)
  579. return i;
  580. else
  581. return nfs4_fl_calc_ds_index(lseg, i);
  582. }
  583. static struct nfs_fh *
  584. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  585. {
  586. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  587. if (flseg->stripe_type == STRIPE_SPARSE) {
  588. if (flseg->num_fh == 1)
  589. i = 0;
  590. else if (flseg->num_fh == 0)
  591. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  592. return NULL;
  593. }
  594. return flseg->fh_array[i];
  595. }
  596. static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
  597. {
  598. struct pnfs_layout_segment *lseg = data->lseg;
  599. struct nfs4_pnfs_ds *ds;
  600. u32 idx;
  601. struct nfs_fh *fh;
  602. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  603. ds = nfs4_fl_prepare_ds(lseg, idx);
  604. if (!ds) {
  605. printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
  606. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  607. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  608. prepare_to_resend_writes(data);
  609. data->mds_ops->rpc_release(data);
  610. return -EAGAIN;
  611. }
  612. dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
  613. data->write_done_cb = filelayout_commit_done_cb;
  614. data->ds_clp = ds->ds_clp;
  615. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  616. if (fh)
  617. data->args.fh = fh;
  618. return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
  619. &filelayout_commit_call_ops, how);
  620. }
  621. /*
  622. * This is only useful while we are using whole file layouts.
  623. */
  624. static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
  625. {
  626. struct pnfs_layout_segment *lseg, *rv = NULL;
  627. spin_lock(&inode->i_lock);
  628. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
  629. if (lseg->pls_range.iomode == IOMODE_RW)
  630. rv = get_lseg(lseg);
  631. spin_unlock(&inode->i_lock);
  632. return rv;
  633. }
  634. static int alloc_ds_commits(struct inode *inode, struct list_head *list)
  635. {
  636. struct pnfs_layout_segment *lseg;
  637. struct nfs4_filelayout_segment *fl;
  638. struct nfs_write_data *data;
  639. int i, j;
  640. /* Won't need this when non-whole file layout segments are supported
  641. * instead we will use a pnfs_layout_hdr structure */
  642. lseg = find_only_write_lseg(inode);
  643. if (!lseg)
  644. return 0;
  645. fl = FILELAYOUT_LSEG(lseg);
  646. for (i = 0; i < fl->number_of_buckets; i++) {
  647. if (list_empty(&fl->commit_buckets[i]))
  648. continue;
  649. data = nfs_commitdata_alloc();
  650. if (!data)
  651. goto out_bad;
  652. data->ds_commit_index = i;
  653. data->lseg = lseg;
  654. list_add(&data->pages, list);
  655. }
  656. put_lseg(lseg);
  657. return 0;
  658. out_bad:
  659. for (j = i; j < fl->number_of_buckets; j++) {
  660. if (list_empty(&fl->commit_buckets[i]))
  661. continue;
  662. nfs_retry_commit(&fl->commit_buckets[i], lseg);
  663. put_lseg(lseg); /* associated with emptying bucket */
  664. }
  665. put_lseg(lseg);
  666. /* Caller will clean up entries put on list */
  667. return -ENOMEM;
  668. }
  669. /* This follows nfs_commit_list pretty closely */
  670. static int
  671. filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  672. int how)
  673. {
  674. struct nfs_write_data *data, *tmp;
  675. LIST_HEAD(list);
  676. if (!list_empty(mds_pages)) {
  677. data = nfs_commitdata_alloc();
  678. if (!data)
  679. goto out_bad;
  680. data->lseg = NULL;
  681. list_add(&data->pages, &list);
  682. }
  683. if (alloc_ds_commits(inode, &list))
  684. goto out_bad;
  685. list_for_each_entry_safe(data, tmp, &list, pages) {
  686. list_del_init(&data->pages);
  687. atomic_inc(&NFS_I(inode)->commits_outstanding);
  688. if (!data->lseg) {
  689. nfs_init_commit(data, mds_pages, NULL);
  690. nfs_initiate_commit(data, NFS_CLIENT(inode),
  691. data->mds_ops, how);
  692. } else {
  693. nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index], data->lseg);
  694. filelayout_initiate_commit(data, how);
  695. }
  696. }
  697. return 0;
  698. out_bad:
  699. list_for_each_entry_safe(data, tmp, &list, pages) {
  700. nfs_retry_commit(&data->pages, data->lseg);
  701. list_del_init(&data->pages);
  702. nfs_commit_free(data);
  703. }
  704. nfs_retry_commit(mds_pages, NULL);
  705. nfs_commit_clear_lock(NFS_I(inode));
  706. return -ENOMEM;
  707. }
  708. static struct pnfs_layoutdriver_type filelayout_type = {
  709. .id = LAYOUT_NFSV4_1_FILES,
  710. .name = "LAYOUT_NFSV4_1_FILES",
  711. .owner = THIS_MODULE,
  712. .alloc_lseg = filelayout_alloc_lseg,
  713. .free_lseg = filelayout_free_lseg,
  714. .pg_test = filelayout_pg_test,
  715. .mark_pnfs_commit = filelayout_mark_pnfs_commit,
  716. .choose_commit_list = filelayout_choose_commit_list,
  717. .commit_pagelist = filelayout_commit_pagelist,
  718. .read_pagelist = filelayout_read_pagelist,
  719. .write_pagelist = filelayout_write_pagelist,
  720. };
  721. static int __init nfs4filelayout_init(void)
  722. {
  723. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  724. __func__);
  725. return pnfs_register_layoutdriver(&filelayout_type);
  726. }
  727. static void __exit nfs4filelayout_exit(void)
  728. {
  729. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  730. __func__);
  731. pnfs_unregister_layoutdriver(&filelayout_type);
  732. }
  733. module_init(nfs4filelayout_init);
  734. module_exit(nfs4filelayout_exit);