nfs4filelayout.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. * Call ops for the async read/write cases
  136. * In the case of dense layouts, the offset needs to be reset to its
  137. * original value.
  138. */
  139. static void filelayout_read_prepare(struct rpc_task *task, void *data)
  140. {
  141. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  142. rdata->read_done_cb = filelayout_read_done_cb;
  143. if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
  144. &rdata->args.seq_args, &rdata->res.seq_res,
  145. 0, task))
  146. return;
  147. rpc_call_start(task);
  148. }
  149. static void filelayout_read_call_done(struct rpc_task *task, void *data)
  150. {
  151. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  152. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  153. /* Note this may cause RPC to be resent */
  154. rdata->mds_ops->rpc_call_done(task, data);
  155. }
  156. static void filelayout_read_release(void *data)
  157. {
  158. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  159. rdata->mds_ops->rpc_release(data);
  160. }
  161. static int filelayout_write_done_cb(struct rpc_task *task,
  162. struct nfs_write_data *data)
  163. {
  164. int reset = 0;
  165. if (filelayout_async_handle_error(task, data->args.context->state,
  166. data->ds_clp, &reset) == -EAGAIN) {
  167. struct nfs_client *clp;
  168. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  169. __func__, data->ds_clp, data->ds_clp->cl_session);
  170. if (reset) {
  171. filelayout_set_lo_fail(data->lseg);
  172. nfs4_reset_write(task, data);
  173. clp = NFS_SERVER(data->inode)->nfs_client;
  174. } else
  175. clp = data->ds_clp;
  176. nfs_restart_rpc(task, clp);
  177. return -EAGAIN;
  178. }
  179. return 0;
  180. }
  181. static void filelayout_write_prepare(struct rpc_task *task, void *data)
  182. {
  183. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  184. if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
  185. &wdata->args.seq_args, &wdata->res.seq_res,
  186. 0, task))
  187. return;
  188. rpc_call_start(task);
  189. }
  190. static void filelayout_write_call_done(struct rpc_task *task, void *data)
  191. {
  192. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  193. /* Note this may cause RPC to be resent */
  194. wdata->mds_ops->rpc_call_done(task, data);
  195. }
  196. static void filelayout_write_release(void *data)
  197. {
  198. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  199. wdata->mds_ops->rpc_release(data);
  200. }
  201. struct rpc_call_ops filelayout_read_call_ops = {
  202. .rpc_call_prepare = filelayout_read_prepare,
  203. .rpc_call_done = filelayout_read_call_done,
  204. .rpc_release = filelayout_read_release,
  205. };
  206. struct rpc_call_ops filelayout_write_call_ops = {
  207. .rpc_call_prepare = filelayout_write_prepare,
  208. .rpc_call_done = filelayout_write_call_done,
  209. .rpc_release = filelayout_write_release,
  210. };
  211. static enum pnfs_try_status
  212. filelayout_read_pagelist(struct nfs_read_data *data)
  213. {
  214. struct pnfs_layout_segment *lseg = data->lseg;
  215. struct nfs4_pnfs_ds *ds;
  216. loff_t offset = data->args.offset;
  217. u32 j, idx;
  218. struct nfs_fh *fh;
  219. int status;
  220. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  221. __func__, data->inode->i_ino,
  222. data->args.pgbase, (size_t)data->args.count, offset);
  223. /* Retrieve the correct rpc_client for the byte range */
  224. j = nfs4_fl_calc_j_index(lseg, offset);
  225. idx = nfs4_fl_calc_ds_index(lseg, j);
  226. ds = nfs4_fl_prepare_ds(lseg, idx);
  227. if (!ds) {
  228. /* Either layout fh index faulty, or ds connect failed */
  229. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  230. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  231. return PNFS_NOT_ATTEMPTED;
  232. }
  233. dprintk("%s USE DS:ip %x %hu\n", __func__,
  234. ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
  235. /* No multipath support. Use first DS */
  236. data->ds_clp = ds->ds_clp;
  237. fh = nfs4_fl_select_ds_fh(lseg, j);
  238. if (fh)
  239. data->args.fh = fh;
  240. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  241. data->mds_offset = offset;
  242. /* Perform an asynchronous read to ds */
  243. status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
  244. &filelayout_read_call_ops);
  245. BUG_ON(status != 0);
  246. return PNFS_ATTEMPTED;
  247. }
  248. /* Perform async writes. */
  249. static enum pnfs_try_status
  250. filelayout_write_pagelist(struct nfs_write_data *data, int sync)
  251. {
  252. struct pnfs_layout_segment *lseg = data->lseg;
  253. struct nfs4_pnfs_ds *ds;
  254. loff_t offset = data->args.offset;
  255. u32 j, idx;
  256. struct nfs_fh *fh;
  257. int status;
  258. /* Retrieve the correct rpc_client for the byte range */
  259. j = nfs4_fl_calc_j_index(lseg, offset);
  260. idx = nfs4_fl_calc_ds_index(lseg, j);
  261. ds = nfs4_fl_prepare_ds(lseg, idx);
  262. if (!ds) {
  263. printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
  264. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  265. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  266. return PNFS_NOT_ATTEMPTED;
  267. }
  268. dprintk("%s ino %lu sync %d req %Zu@%llu DS:%x:%hu\n", __func__,
  269. data->inode->i_ino, sync, (size_t) data->args.count, offset,
  270. ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
  271. /* We can't handle commit to ds yet */
  272. if (!FILELAYOUT_LSEG(lseg)->commit_through_mds)
  273. data->args.stable = NFS_FILE_SYNC;
  274. data->write_done_cb = filelayout_write_done_cb;
  275. data->ds_clp = ds->ds_clp;
  276. fh = nfs4_fl_select_ds_fh(lseg, j);
  277. if (fh)
  278. data->args.fh = fh;
  279. /*
  280. * Get the file offset on the dserver. Set the write offset to
  281. * this offset and save the original offset.
  282. */
  283. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  284. data->mds_offset = offset;
  285. /* Perform an asynchronous write */
  286. status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
  287. &filelayout_write_call_ops, sync);
  288. BUG_ON(status != 0);
  289. return PNFS_ATTEMPTED;
  290. }
  291. /*
  292. * filelayout_check_layout()
  293. *
  294. * Make sure layout segment parameters are sane WRT the device.
  295. * At this point no generic layer initialization of the lseg has occurred,
  296. * and nothing has been added to the layout_hdr cache.
  297. *
  298. */
  299. static int
  300. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  301. struct nfs4_filelayout_segment *fl,
  302. struct nfs4_layoutget_res *lgr,
  303. struct nfs4_deviceid *id)
  304. {
  305. struct nfs4_file_layout_dsaddr *dsaddr;
  306. int status = -EINVAL;
  307. struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
  308. dprintk("--> %s\n", __func__);
  309. if (fl->pattern_offset > lgr->range.offset) {
  310. dprintk("%s pattern_offset %lld to large\n",
  311. __func__, fl->pattern_offset);
  312. goto out;
  313. }
  314. if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
  315. dprintk("%s Invalid stripe unit (%u)\n",
  316. __func__, fl->stripe_unit);
  317. goto out;
  318. }
  319. /* find and reference the deviceid */
  320. dsaddr = nfs4_fl_find_get_deviceid(id);
  321. if (dsaddr == NULL) {
  322. dsaddr = get_device_info(lo->plh_inode, id);
  323. if (dsaddr == NULL)
  324. goto out;
  325. }
  326. fl->dsaddr = dsaddr;
  327. if (fl->first_stripe_index < 0 ||
  328. fl->first_stripe_index >= dsaddr->stripe_count) {
  329. dprintk("%s Bad first_stripe_index %d\n",
  330. __func__, fl->first_stripe_index);
  331. goto out_put;
  332. }
  333. if ((fl->stripe_type == STRIPE_SPARSE &&
  334. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  335. (fl->stripe_type == STRIPE_DENSE &&
  336. fl->num_fh != dsaddr->stripe_count)) {
  337. dprintk("%s num_fh %u not valid for given packing\n",
  338. __func__, fl->num_fh);
  339. goto out_put;
  340. }
  341. if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
  342. dprintk("%s Stripe unit (%u) not aligned with rsize %u "
  343. "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
  344. nfss->wsize);
  345. }
  346. status = 0;
  347. out:
  348. dprintk("--> %s returns %d\n", __func__, status);
  349. return status;
  350. out_put:
  351. nfs4_fl_put_deviceid(dsaddr);
  352. goto out;
  353. }
  354. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  355. {
  356. int i;
  357. for (i = 0; i < fl->num_fh; i++) {
  358. if (!fl->fh_array[i])
  359. break;
  360. kfree(fl->fh_array[i]);
  361. }
  362. kfree(fl->fh_array);
  363. fl->fh_array = NULL;
  364. }
  365. static void
  366. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  367. {
  368. filelayout_free_fh_array(fl);
  369. kfree(fl);
  370. }
  371. static int
  372. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  373. struct nfs4_filelayout_segment *fl,
  374. struct nfs4_layoutget_res *lgr,
  375. struct nfs4_deviceid *id)
  376. {
  377. uint32_t *p = (uint32_t *)lgr->layout.buf;
  378. uint32_t nfl_util;
  379. int i;
  380. dprintk("%s: set_layout_map Begin\n", __func__);
  381. memcpy(id, p, sizeof(*id));
  382. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  383. print_deviceid(id);
  384. nfl_util = be32_to_cpup(p++);
  385. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  386. fl->commit_through_mds = 1;
  387. if (nfl_util & NFL4_UFLG_DENSE)
  388. fl->stripe_type = STRIPE_DENSE;
  389. else
  390. fl->stripe_type = STRIPE_SPARSE;
  391. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  392. fl->first_stripe_index = be32_to_cpup(p++);
  393. p = xdr_decode_hyper(p, &fl->pattern_offset);
  394. fl->num_fh = be32_to_cpup(p++);
  395. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  396. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  397. fl->pattern_offset);
  398. fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
  399. GFP_KERNEL);
  400. if (!fl->fh_array)
  401. return -ENOMEM;
  402. for (i = 0; i < fl->num_fh; i++) {
  403. /* Do we want to use a mempool here? */
  404. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
  405. if (!fl->fh_array[i]) {
  406. filelayout_free_fh_array(fl);
  407. return -ENOMEM;
  408. }
  409. fl->fh_array[i]->size = be32_to_cpup(p++);
  410. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  411. printk(KERN_ERR "Too big fh %d received %d\n",
  412. i, fl->fh_array[i]->size);
  413. filelayout_free_fh_array(fl);
  414. return -EIO;
  415. }
  416. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  417. p += XDR_QUADLEN(fl->fh_array[i]->size);
  418. dprintk("DEBUG: %s: fh len %d\n", __func__,
  419. fl->fh_array[i]->size);
  420. }
  421. return 0;
  422. }
  423. static struct pnfs_layout_segment *
  424. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  425. struct nfs4_layoutget_res *lgr)
  426. {
  427. struct nfs4_filelayout_segment *fl;
  428. int rc;
  429. struct nfs4_deviceid id;
  430. dprintk("--> %s\n", __func__);
  431. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  432. if (!fl)
  433. return NULL;
  434. rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
  435. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
  436. _filelayout_free_lseg(fl);
  437. return NULL;
  438. }
  439. return &fl->generic_hdr;
  440. }
  441. static void
  442. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  443. {
  444. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  445. dprintk("--> %s\n", __func__);
  446. nfs4_fl_put_deviceid(fl->dsaddr);
  447. _filelayout_free_lseg(fl);
  448. }
  449. /*
  450. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  451. *
  452. * return 1 : coalesce page
  453. * return 0 : don't coalesce page
  454. */
  455. int
  456. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  457. struct nfs_page *req)
  458. {
  459. u64 p_stripe, r_stripe;
  460. u32 stripe_unit;
  461. if (!pgio->pg_lseg)
  462. return 1;
  463. p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
  464. r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
  465. stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  466. do_div(p_stripe, stripe_unit);
  467. do_div(r_stripe, stripe_unit);
  468. return (p_stripe == r_stripe);
  469. }
  470. static struct pnfs_layoutdriver_type filelayout_type = {
  471. .id = LAYOUT_NFSV4_1_FILES,
  472. .name = "LAYOUT_NFSV4_1_FILES",
  473. .owner = THIS_MODULE,
  474. .alloc_lseg = filelayout_alloc_lseg,
  475. .free_lseg = filelayout_free_lseg,
  476. .pg_test = filelayout_pg_test,
  477. .read_pagelist = filelayout_read_pagelist,
  478. .write_pagelist = filelayout_write_pagelist,
  479. };
  480. static int __init nfs4filelayout_init(void)
  481. {
  482. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  483. __func__);
  484. return pnfs_register_layoutdriver(&filelayout_type);
  485. }
  486. static void __exit nfs4filelayout_exit(void)
  487. {
  488. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  489. __func__);
  490. pnfs_unregister_layoutdriver(&filelayout_type);
  491. }
  492. module_init(nfs4filelayout_init);
  493. module_exit(nfs4filelayout_exit);