nfs4filelayout.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 int
  40. filelayout_set_layoutdriver(struct nfs_server *nfss)
  41. {
  42. int status = pnfs_alloc_init_deviceid_cache(nfss->nfs_client,
  43. nfs4_fl_free_deviceid_callback);
  44. if (status) {
  45. printk(KERN_WARNING "%s: deviceid cache could not be "
  46. "initialized\n", __func__);
  47. return status;
  48. }
  49. dprintk("%s: deviceid cache has been initialized successfully\n",
  50. __func__);
  51. return 0;
  52. }
  53. /* Clear out the layout by destroying its device list */
  54. static int
  55. filelayout_clear_layoutdriver(struct nfs_server *nfss)
  56. {
  57. dprintk("--> %s\n", __func__);
  58. if (nfss->nfs_client->cl_devid_cache)
  59. pnfs_put_deviceid_cache(nfss->nfs_client);
  60. return 0;
  61. }
  62. static loff_t
  63. filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
  64. loff_t offset)
  65. {
  66. u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
  67. u64 tmp;
  68. offset -= flseg->pattern_offset;
  69. tmp = offset;
  70. do_div(tmp, stripe_width);
  71. return tmp * flseg->stripe_unit + do_div(offset, flseg->stripe_unit);
  72. }
  73. /* This function is used by the layout driver to calculate the
  74. * offset of the file on the dserver based on whether the
  75. * layout type is STRIPE_DENSE or STRIPE_SPARSE
  76. */
  77. static loff_t
  78. filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
  79. {
  80. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  81. switch (flseg->stripe_type) {
  82. case STRIPE_SPARSE:
  83. return offset;
  84. case STRIPE_DENSE:
  85. return filelayout_get_dense_offset(flseg, offset);
  86. }
  87. BUG();
  88. }
  89. /* For data server errors we don't recover from */
  90. static void
  91. filelayout_set_lo_fail(struct pnfs_layout_segment *lseg)
  92. {
  93. if (lseg->pls_range.iomode == IOMODE_RW) {
  94. dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
  95. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  96. } else {
  97. dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
  98. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  99. }
  100. }
  101. static int filelayout_async_handle_error(struct rpc_task *task,
  102. struct nfs4_state *state,
  103. struct nfs_client *clp,
  104. int *reset)
  105. {
  106. if (task->tk_status >= 0)
  107. return 0;
  108. *reset = 0;
  109. switch (task->tk_status) {
  110. case -NFS4ERR_BADSESSION:
  111. case -NFS4ERR_BADSLOT:
  112. case -NFS4ERR_BAD_HIGH_SLOT:
  113. case -NFS4ERR_DEADSESSION:
  114. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  115. case -NFS4ERR_SEQ_FALSE_RETRY:
  116. case -NFS4ERR_SEQ_MISORDERED:
  117. dprintk("%s ERROR %d, Reset session. Exchangeid "
  118. "flags 0x%x\n", __func__, task->tk_status,
  119. clp->cl_exchange_flags);
  120. nfs4_schedule_session_recovery(clp->cl_session);
  121. break;
  122. case -NFS4ERR_DELAY:
  123. case -NFS4ERR_GRACE:
  124. case -EKEYEXPIRED:
  125. rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
  126. break;
  127. default:
  128. dprintk("%s DS error. Retry through MDS %d\n", __func__,
  129. task->tk_status);
  130. *reset = 1;
  131. break;
  132. }
  133. task->tk_status = 0;
  134. return -EAGAIN;
  135. }
  136. /* NFS_PROTO call done callback routines */
  137. static int filelayout_read_done_cb(struct rpc_task *task,
  138. struct nfs_read_data *data)
  139. {
  140. struct nfs_client *clp = data->ds_clp;
  141. int reset = 0;
  142. dprintk("%s DS read\n", __func__);
  143. if (filelayout_async_handle_error(task, data->args.context->state,
  144. data->ds_clp, &reset) == -EAGAIN) {
  145. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  146. __func__, data->ds_clp, data->ds_clp->cl_session);
  147. if (reset) {
  148. filelayout_set_lo_fail(data->lseg);
  149. nfs4_reset_read(task, data);
  150. clp = NFS_SERVER(data->inode)->nfs_client;
  151. }
  152. nfs_restart_rpc(task, clp);
  153. return -EAGAIN;
  154. }
  155. return 0;
  156. }
  157. /*
  158. * Call ops for the async read/write cases
  159. * In the case of dense layouts, the offset needs to be reset to its
  160. * original value.
  161. */
  162. static void filelayout_read_prepare(struct rpc_task *task, void *data)
  163. {
  164. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  165. rdata->read_done_cb = filelayout_read_done_cb;
  166. if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
  167. &rdata->args.seq_args, &rdata->res.seq_res,
  168. 0, task))
  169. return;
  170. rpc_call_start(task);
  171. }
  172. static void filelayout_read_call_done(struct rpc_task *task, void *data)
  173. {
  174. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  175. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  176. /* Note this may cause RPC to be resent */
  177. rdata->mds_ops->rpc_call_done(task, data);
  178. }
  179. static void filelayout_read_release(void *data)
  180. {
  181. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  182. rdata->mds_ops->rpc_release(data);
  183. }
  184. struct rpc_call_ops filelayout_read_call_ops = {
  185. .rpc_call_prepare = filelayout_read_prepare,
  186. .rpc_call_done = filelayout_read_call_done,
  187. .rpc_release = filelayout_read_release,
  188. };
  189. static enum pnfs_try_status
  190. filelayout_read_pagelist(struct nfs_read_data *data)
  191. {
  192. struct pnfs_layout_segment *lseg = data->lseg;
  193. struct nfs4_pnfs_ds *ds;
  194. loff_t offset = data->args.offset;
  195. u32 j, idx;
  196. struct nfs_fh *fh;
  197. int status;
  198. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  199. __func__, data->inode->i_ino,
  200. data->args.pgbase, (size_t)data->args.count, offset);
  201. /* Retrieve the correct rpc_client for the byte range */
  202. j = nfs4_fl_calc_j_index(lseg, offset);
  203. idx = nfs4_fl_calc_ds_index(lseg, j);
  204. ds = nfs4_fl_prepare_ds(lseg, idx);
  205. if (!ds) {
  206. printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
  207. return PNFS_NOT_ATTEMPTED;
  208. }
  209. dprintk("%s USE DS:ip %x %hu\n", __func__,
  210. ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
  211. /* No multipath support. Use first DS */
  212. data->ds_clp = ds->ds_clp;
  213. fh = nfs4_fl_select_ds_fh(lseg, j);
  214. if (fh)
  215. data->args.fh = fh;
  216. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  217. data->mds_offset = offset;
  218. /* Perform an asynchronous read to ds */
  219. status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
  220. &filelayout_read_call_ops);
  221. BUG_ON(status != 0);
  222. return PNFS_ATTEMPTED;
  223. }
  224. /*
  225. * filelayout_check_layout()
  226. *
  227. * Make sure layout segment parameters are sane WRT the device.
  228. * At this point no generic layer initialization of the lseg has occurred,
  229. * and nothing has been added to the layout_hdr cache.
  230. *
  231. */
  232. static int
  233. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  234. struct nfs4_filelayout_segment *fl,
  235. struct nfs4_layoutget_res *lgr,
  236. struct nfs4_deviceid *id)
  237. {
  238. struct nfs4_file_layout_dsaddr *dsaddr;
  239. int status = -EINVAL;
  240. struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
  241. dprintk("--> %s\n", __func__);
  242. if (fl->pattern_offset > lgr->range.offset) {
  243. dprintk("%s pattern_offset %lld to large\n",
  244. __func__, fl->pattern_offset);
  245. goto out;
  246. }
  247. if (fl->stripe_unit % PAGE_SIZE) {
  248. dprintk("%s Stripe unit (%u) not page aligned\n",
  249. __func__, fl->stripe_unit);
  250. goto out;
  251. }
  252. /* find and reference the deviceid */
  253. dsaddr = nfs4_fl_find_get_deviceid(nfss->nfs_client, id);
  254. if (dsaddr == NULL) {
  255. dsaddr = get_device_info(lo->plh_inode, id);
  256. if (dsaddr == NULL)
  257. goto out;
  258. }
  259. fl->dsaddr = dsaddr;
  260. if (fl->first_stripe_index < 0 ||
  261. fl->first_stripe_index >= dsaddr->stripe_count) {
  262. dprintk("%s Bad first_stripe_index %d\n",
  263. __func__, fl->first_stripe_index);
  264. goto out_put;
  265. }
  266. if ((fl->stripe_type == STRIPE_SPARSE &&
  267. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  268. (fl->stripe_type == STRIPE_DENSE &&
  269. fl->num_fh != dsaddr->stripe_count)) {
  270. dprintk("%s num_fh %u not valid for given packing\n",
  271. __func__, fl->num_fh);
  272. goto out_put;
  273. }
  274. if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
  275. dprintk("%s Stripe unit (%u) not aligned with rsize %u "
  276. "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
  277. nfss->wsize);
  278. }
  279. status = 0;
  280. out:
  281. dprintk("--> %s returns %d\n", __func__, status);
  282. return status;
  283. out_put:
  284. pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, &dsaddr->deviceid);
  285. goto out;
  286. }
  287. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  288. {
  289. int i;
  290. for (i = 0; i < fl->num_fh; i++) {
  291. if (!fl->fh_array[i])
  292. break;
  293. kfree(fl->fh_array[i]);
  294. }
  295. kfree(fl->fh_array);
  296. fl->fh_array = NULL;
  297. }
  298. static void
  299. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  300. {
  301. filelayout_free_fh_array(fl);
  302. kfree(fl);
  303. }
  304. static int
  305. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  306. struct nfs4_filelayout_segment *fl,
  307. struct nfs4_layoutget_res *lgr,
  308. struct nfs4_deviceid *id)
  309. {
  310. uint32_t *p = (uint32_t *)lgr->layout.buf;
  311. uint32_t nfl_util;
  312. int i;
  313. dprintk("%s: set_layout_map Begin\n", __func__);
  314. memcpy(id, p, sizeof(*id));
  315. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  316. print_deviceid(id);
  317. nfl_util = be32_to_cpup(p++);
  318. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  319. fl->commit_through_mds = 1;
  320. if (nfl_util & NFL4_UFLG_DENSE)
  321. fl->stripe_type = STRIPE_DENSE;
  322. else
  323. fl->stripe_type = STRIPE_SPARSE;
  324. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  325. fl->first_stripe_index = be32_to_cpup(p++);
  326. p = xdr_decode_hyper(p, &fl->pattern_offset);
  327. fl->num_fh = be32_to_cpup(p++);
  328. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  329. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  330. fl->pattern_offset);
  331. fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
  332. GFP_KERNEL);
  333. if (!fl->fh_array)
  334. return -ENOMEM;
  335. for (i = 0; i < fl->num_fh; i++) {
  336. /* Do we want to use a mempool here? */
  337. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
  338. if (!fl->fh_array[i]) {
  339. filelayout_free_fh_array(fl);
  340. return -ENOMEM;
  341. }
  342. fl->fh_array[i]->size = be32_to_cpup(p++);
  343. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  344. printk(KERN_ERR "Too big fh %d received %d\n",
  345. i, fl->fh_array[i]->size);
  346. filelayout_free_fh_array(fl);
  347. return -EIO;
  348. }
  349. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  350. p += XDR_QUADLEN(fl->fh_array[i]->size);
  351. dprintk("DEBUG: %s: fh len %d\n", __func__,
  352. fl->fh_array[i]->size);
  353. }
  354. return 0;
  355. }
  356. static struct pnfs_layout_segment *
  357. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  358. struct nfs4_layoutget_res *lgr)
  359. {
  360. struct nfs4_filelayout_segment *fl;
  361. int rc;
  362. struct nfs4_deviceid id;
  363. dprintk("--> %s\n", __func__);
  364. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  365. if (!fl)
  366. return NULL;
  367. rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
  368. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
  369. _filelayout_free_lseg(fl);
  370. return NULL;
  371. }
  372. return &fl->generic_hdr;
  373. }
  374. static void
  375. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  376. {
  377. struct nfs_server *nfss = NFS_SERVER(lseg->pls_layout->plh_inode);
  378. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  379. dprintk("--> %s\n", __func__);
  380. pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache,
  381. &fl->dsaddr->deviceid);
  382. _filelayout_free_lseg(fl);
  383. }
  384. /*
  385. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  386. *
  387. * return 1 : coalesce page
  388. * return 0 : don't coalesce page
  389. */
  390. int
  391. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  392. struct nfs_page *req)
  393. {
  394. u64 p_stripe, r_stripe;
  395. u32 stripe_unit;
  396. if (!pgio->pg_lseg)
  397. return 1;
  398. p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
  399. r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
  400. stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  401. do_div(p_stripe, stripe_unit);
  402. do_div(r_stripe, stripe_unit);
  403. return (p_stripe == r_stripe);
  404. }
  405. static struct pnfs_layoutdriver_type filelayout_type = {
  406. .id = LAYOUT_NFSV4_1_FILES,
  407. .name = "LAYOUT_NFSV4_1_FILES",
  408. .owner = THIS_MODULE,
  409. .set_layoutdriver = filelayout_set_layoutdriver,
  410. .clear_layoutdriver = filelayout_clear_layoutdriver,
  411. .alloc_lseg = filelayout_alloc_lseg,
  412. .free_lseg = filelayout_free_lseg,
  413. .pg_test = filelayout_pg_test,
  414. .read_pagelist = filelayout_read_pagelist,
  415. };
  416. static int __init nfs4filelayout_init(void)
  417. {
  418. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  419. __func__);
  420. return pnfs_register_layoutdriver(&filelayout_type);
  421. }
  422. static void __exit nfs4filelayout_exit(void)
  423. {
  424. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  425. __func__);
  426. pnfs_unregister_layoutdriver(&filelayout_type);
  427. }
  428. module_init(nfs4filelayout_init);
  429. module_exit(nfs4filelayout_exit);