nfs4filelayout.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. struct rpc_call_ops filelayout_read_call_ops = {
  162. .rpc_call_prepare = filelayout_read_prepare,
  163. .rpc_call_done = filelayout_read_call_done,
  164. .rpc_release = filelayout_read_release,
  165. };
  166. static enum pnfs_try_status
  167. filelayout_read_pagelist(struct nfs_read_data *data)
  168. {
  169. struct pnfs_layout_segment *lseg = data->lseg;
  170. struct nfs4_pnfs_ds *ds;
  171. loff_t offset = data->args.offset;
  172. u32 j, idx;
  173. struct nfs_fh *fh;
  174. int status;
  175. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  176. __func__, data->inode->i_ino,
  177. data->args.pgbase, (size_t)data->args.count, offset);
  178. /* Retrieve the correct rpc_client for the byte range */
  179. j = nfs4_fl_calc_j_index(lseg, offset);
  180. idx = nfs4_fl_calc_ds_index(lseg, j);
  181. ds = nfs4_fl_prepare_ds(lseg, idx);
  182. if (!ds) {
  183. /* Either layout fh index faulty, or ds connect failed */
  184. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  185. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  186. return PNFS_NOT_ATTEMPTED;
  187. }
  188. dprintk("%s USE DS:ip %x %hu\n", __func__,
  189. ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
  190. /* No multipath support. Use first DS */
  191. data->ds_clp = ds->ds_clp;
  192. fh = nfs4_fl_select_ds_fh(lseg, j);
  193. if (fh)
  194. data->args.fh = fh;
  195. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  196. data->mds_offset = offset;
  197. /* Perform an asynchronous read to ds */
  198. status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
  199. &filelayout_read_call_ops);
  200. BUG_ON(status != 0);
  201. return PNFS_ATTEMPTED;
  202. }
  203. /*
  204. * filelayout_check_layout()
  205. *
  206. * Make sure layout segment parameters are sane WRT the device.
  207. * At this point no generic layer initialization of the lseg has occurred,
  208. * and nothing has been added to the layout_hdr cache.
  209. *
  210. */
  211. static int
  212. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  213. struct nfs4_filelayout_segment *fl,
  214. struct nfs4_layoutget_res *lgr,
  215. struct nfs4_deviceid *id)
  216. {
  217. struct nfs4_file_layout_dsaddr *dsaddr;
  218. int status = -EINVAL;
  219. struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
  220. dprintk("--> %s\n", __func__);
  221. if (fl->pattern_offset > lgr->range.offset) {
  222. dprintk("%s pattern_offset %lld to large\n",
  223. __func__, fl->pattern_offset);
  224. goto out;
  225. }
  226. if (fl->stripe_unit % PAGE_SIZE) {
  227. dprintk("%s Stripe unit (%u) not page aligned\n",
  228. __func__, fl->stripe_unit);
  229. goto out;
  230. }
  231. /* find and reference the deviceid */
  232. dsaddr = nfs4_fl_find_get_deviceid(id);
  233. if (dsaddr == NULL) {
  234. dsaddr = get_device_info(lo->plh_inode, id);
  235. if (dsaddr == NULL)
  236. goto out;
  237. }
  238. fl->dsaddr = dsaddr;
  239. if (fl->first_stripe_index < 0 ||
  240. fl->first_stripe_index >= dsaddr->stripe_count) {
  241. dprintk("%s Bad first_stripe_index %d\n",
  242. __func__, fl->first_stripe_index);
  243. goto out_put;
  244. }
  245. if ((fl->stripe_type == STRIPE_SPARSE &&
  246. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  247. (fl->stripe_type == STRIPE_DENSE &&
  248. fl->num_fh != dsaddr->stripe_count)) {
  249. dprintk("%s num_fh %u not valid for given packing\n",
  250. __func__, fl->num_fh);
  251. goto out_put;
  252. }
  253. if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
  254. dprintk("%s Stripe unit (%u) not aligned with rsize %u "
  255. "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
  256. nfss->wsize);
  257. }
  258. status = 0;
  259. out:
  260. dprintk("--> %s returns %d\n", __func__, status);
  261. return status;
  262. out_put:
  263. nfs4_fl_put_deviceid(dsaddr);
  264. goto out;
  265. }
  266. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  267. {
  268. int i;
  269. for (i = 0; i < fl->num_fh; i++) {
  270. if (!fl->fh_array[i])
  271. break;
  272. kfree(fl->fh_array[i]);
  273. }
  274. kfree(fl->fh_array);
  275. fl->fh_array = NULL;
  276. }
  277. static void
  278. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  279. {
  280. filelayout_free_fh_array(fl);
  281. kfree(fl);
  282. }
  283. static int
  284. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  285. struct nfs4_filelayout_segment *fl,
  286. struct nfs4_layoutget_res *lgr,
  287. struct nfs4_deviceid *id)
  288. {
  289. uint32_t *p = (uint32_t *)lgr->layout.buf;
  290. uint32_t nfl_util;
  291. int i;
  292. dprintk("%s: set_layout_map Begin\n", __func__);
  293. memcpy(id, p, sizeof(*id));
  294. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  295. print_deviceid(id);
  296. nfl_util = be32_to_cpup(p++);
  297. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  298. fl->commit_through_mds = 1;
  299. if (nfl_util & NFL4_UFLG_DENSE)
  300. fl->stripe_type = STRIPE_DENSE;
  301. else
  302. fl->stripe_type = STRIPE_SPARSE;
  303. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  304. fl->first_stripe_index = be32_to_cpup(p++);
  305. p = xdr_decode_hyper(p, &fl->pattern_offset);
  306. fl->num_fh = be32_to_cpup(p++);
  307. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  308. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  309. fl->pattern_offset);
  310. fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
  311. GFP_KERNEL);
  312. if (!fl->fh_array)
  313. return -ENOMEM;
  314. for (i = 0; i < fl->num_fh; i++) {
  315. /* Do we want to use a mempool here? */
  316. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
  317. if (!fl->fh_array[i]) {
  318. filelayout_free_fh_array(fl);
  319. return -ENOMEM;
  320. }
  321. fl->fh_array[i]->size = be32_to_cpup(p++);
  322. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  323. printk(KERN_ERR "Too big fh %d received %d\n",
  324. i, fl->fh_array[i]->size);
  325. filelayout_free_fh_array(fl);
  326. return -EIO;
  327. }
  328. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  329. p += XDR_QUADLEN(fl->fh_array[i]->size);
  330. dprintk("DEBUG: %s: fh len %d\n", __func__,
  331. fl->fh_array[i]->size);
  332. }
  333. return 0;
  334. }
  335. static struct pnfs_layout_segment *
  336. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  337. struct nfs4_layoutget_res *lgr)
  338. {
  339. struct nfs4_filelayout_segment *fl;
  340. int rc;
  341. struct nfs4_deviceid id;
  342. dprintk("--> %s\n", __func__);
  343. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  344. if (!fl)
  345. return NULL;
  346. rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
  347. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
  348. _filelayout_free_lseg(fl);
  349. return NULL;
  350. }
  351. return &fl->generic_hdr;
  352. }
  353. static void
  354. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  355. {
  356. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  357. dprintk("--> %s\n", __func__);
  358. nfs4_fl_put_deviceid(fl->dsaddr);
  359. _filelayout_free_lseg(fl);
  360. }
  361. /*
  362. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  363. *
  364. * return 1 : coalesce page
  365. * return 0 : don't coalesce page
  366. */
  367. int
  368. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  369. struct nfs_page *req)
  370. {
  371. u64 p_stripe, r_stripe;
  372. u32 stripe_unit;
  373. if (!pgio->pg_lseg)
  374. return 1;
  375. p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
  376. r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
  377. stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  378. do_div(p_stripe, stripe_unit);
  379. do_div(r_stripe, stripe_unit);
  380. return (p_stripe == r_stripe);
  381. }
  382. static struct pnfs_layoutdriver_type filelayout_type = {
  383. .id = LAYOUT_NFSV4_1_FILES,
  384. .name = "LAYOUT_NFSV4_1_FILES",
  385. .owner = THIS_MODULE,
  386. .alloc_lseg = filelayout_alloc_lseg,
  387. .free_lseg = filelayout_free_lseg,
  388. .pg_test = filelayout_pg_test,
  389. .read_pagelist = filelayout_read_pagelist,
  390. };
  391. static int __init nfs4filelayout_init(void)
  392. {
  393. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  394. __func__);
  395. return pnfs_register_layoutdriver(&filelayout_type);
  396. }
  397. static void __exit nfs4filelayout_exit(void)
  398. {
  399. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  400. __func__);
  401. pnfs_unregister_layoutdriver(&filelayout_type);
  402. }
  403. module_init(nfs4filelayout_init);
  404. module_exit(nfs4filelayout_exit);