nfs4filelayout.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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 <linux/nfs_page.h>
  33. #include <linux/module.h>
  34. #include <linux/sunrpc/metrics.h>
  35. #include "internal.h"
  36. #include "delegation.h"
  37. #include "nfs4filelayout.h"
  38. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  39. MODULE_LICENSE("GPL");
  40. MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
  41. MODULE_DESCRIPTION("The NFSv4 file layout driver");
  42. #define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
  43. static loff_t
  44. filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
  45. loff_t offset)
  46. {
  47. u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
  48. u64 stripe_no;
  49. u32 rem;
  50. offset -= flseg->pattern_offset;
  51. stripe_no = div_u64(offset, stripe_width);
  52. div_u64_rem(offset, flseg->stripe_unit, &rem);
  53. return stripe_no * flseg->stripe_unit + rem;
  54. }
  55. /* This function is used by the layout driver to calculate the
  56. * offset of the file on the dserver based on whether the
  57. * layout type is STRIPE_DENSE or STRIPE_SPARSE
  58. */
  59. static loff_t
  60. filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
  61. {
  62. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  63. switch (flseg->stripe_type) {
  64. case STRIPE_SPARSE:
  65. return offset;
  66. case STRIPE_DENSE:
  67. return filelayout_get_dense_offset(flseg, offset);
  68. }
  69. BUG();
  70. }
  71. static int filelayout_async_handle_error(struct rpc_task *task,
  72. struct nfs4_state *state,
  73. struct nfs_client *clp,
  74. int *reset)
  75. {
  76. struct nfs_server *mds_server = NFS_SERVER(state->inode);
  77. struct nfs_client *mds_client = mds_server->nfs_client;
  78. if (task->tk_status >= 0)
  79. return 0;
  80. *reset = 0;
  81. switch (task->tk_status) {
  82. /* MDS state errors */
  83. case -NFS4ERR_DELEG_REVOKED:
  84. case -NFS4ERR_ADMIN_REVOKED:
  85. case -NFS4ERR_BAD_STATEID:
  86. nfs_remove_bad_delegation(state->inode);
  87. case -NFS4ERR_OPENMODE:
  88. nfs4_schedule_stateid_recovery(mds_server, state);
  89. goto wait_on_recovery;
  90. case -NFS4ERR_EXPIRED:
  91. nfs4_schedule_stateid_recovery(mds_server, state);
  92. nfs4_schedule_lease_recovery(mds_client);
  93. goto wait_on_recovery;
  94. /* DS session errors */
  95. case -NFS4ERR_BADSESSION:
  96. case -NFS4ERR_BADSLOT:
  97. case -NFS4ERR_BAD_HIGH_SLOT:
  98. case -NFS4ERR_DEADSESSION:
  99. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  100. case -NFS4ERR_SEQ_FALSE_RETRY:
  101. case -NFS4ERR_SEQ_MISORDERED:
  102. dprintk("%s ERROR %d, Reset session. Exchangeid "
  103. "flags 0x%x\n", __func__, task->tk_status,
  104. clp->cl_exchange_flags);
  105. nfs4_schedule_session_recovery(clp->cl_session);
  106. break;
  107. case -NFS4ERR_DELAY:
  108. case -NFS4ERR_GRACE:
  109. rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
  110. break;
  111. case -NFS4ERR_RETRY_UNCACHED_REP:
  112. break;
  113. default:
  114. dprintk("%s DS error. Retry through MDS %d\n", __func__,
  115. task->tk_status);
  116. *reset = 1;
  117. break;
  118. }
  119. out:
  120. task->tk_status = 0;
  121. return -EAGAIN;
  122. wait_on_recovery:
  123. rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
  124. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
  125. rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
  126. goto out;
  127. }
  128. /* NFS_PROTO call done callback routines */
  129. static int filelayout_read_done_cb(struct rpc_task *task,
  130. struct nfs_read_data *data)
  131. {
  132. int reset = 0;
  133. dprintk("%s DS read\n", __func__);
  134. if (filelayout_async_handle_error(task, data->args.context->state,
  135. data->ds_clp, &reset) == -EAGAIN) {
  136. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  137. __func__, data->ds_clp, data->ds_clp->cl_session);
  138. if (reset) {
  139. pnfs_set_lo_fail(data->lseg);
  140. nfs4_reset_read(task, data);
  141. }
  142. rpc_restart_call_prepare(task);
  143. return -EAGAIN;
  144. }
  145. return 0;
  146. }
  147. /*
  148. * We reference the rpc_cred of the first WRITE that triggers the need for
  149. * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
  150. * rfc5661 is not clear about which credential should be used.
  151. */
  152. static void
  153. filelayout_set_layoutcommit(struct nfs_write_data *wdata)
  154. {
  155. if (FILELAYOUT_LSEG(wdata->lseg)->commit_through_mds ||
  156. wdata->res.verf->committed == NFS_FILE_SYNC)
  157. return;
  158. pnfs_set_layoutcommit(wdata);
  159. dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, wdata->inode->i_ino,
  160. (unsigned long) NFS_I(wdata->inode)->layout->plh_lwb);
  161. }
  162. /*
  163. * Call ops for the async read/write cases
  164. * In the case of dense layouts, the offset needs to be reset to its
  165. * original value.
  166. */
  167. static void filelayout_read_prepare(struct rpc_task *task, void *data)
  168. {
  169. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  170. rdata->read_done_cb = filelayout_read_done_cb;
  171. if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
  172. &rdata->args.seq_args, &rdata->res.seq_res,
  173. task))
  174. return;
  175. rpc_call_start(task);
  176. }
  177. static void filelayout_read_call_done(struct rpc_task *task, void *data)
  178. {
  179. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  180. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  181. /* Note this may cause RPC to be resent */
  182. rdata->mds_ops->rpc_call_done(task, data);
  183. }
  184. static void filelayout_read_count_stats(struct rpc_task *task, void *data)
  185. {
  186. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  187. rpc_count_iostats(task, NFS_SERVER(rdata->inode)->client->cl_metrics);
  188. }
  189. static void filelayout_read_release(void *data)
  190. {
  191. struct nfs_read_data *rdata = (struct nfs_read_data *)data;
  192. put_lseg(rdata->lseg);
  193. rdata->mds_ops->rpc_release(data);
  194. }
  195. static int filelayout_write_done_cb(struct rpc_task *task,
  196. struct nfs_write_data *data)
  197. {
  198. int reset = 0;
  199. if (filelayout_async_handle_error(task, data->args.context->state,
  200. data->ds_clp, &reset) == -EAGAIN) {
  201. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  202. __func__, data->ds_clp, data->ds_clp->cl_session);
  203. if (reset) {
  204. pnfs_set_lo_fail(data->lseg);
  205. nfs4_reset_write(task, data);
  206. }
  207. rpc_restart_call_prepare(task);
  208. return -EAGAIN;
  209. }
  210. filelayout_set_layoutcommit(data);
  211. return 0;
  212. }
  213. /* Fake up some data that will cause nfs_commit_release to retry the writes. */
  214. static void prepare_to_resend_writes(struct nfs_write_data *data)
  215. {
  216. struct nfs_page *first = nfs_list_entry(data->pages.next);
  217. data->task.tk_status = 0;
  218. memcpy(data->verf.verifier, first->wb_verf.verifier,
  219. sizeof(first->wb_verf.verifier));
  220. data->verf.verifier[0]++; /* ensure verifier mismatch */
  221. }
  222. static int filelayout_commit_done_cb(struct rpc_task *task,
  223. struct nfs_write_data *data)
  224. {
  225. int reset = 0;
  226. if (filelayout_async_handle_error(task, data->args.context->state,
  227. data->ds_clp, &reset) == -EAGAIN) {
  228. dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
  229. __func__, data->ds_clp, data->ds_clp->cl_session);
  230. if (reset) {
  231. prepare_to_resend_writes(data);
  232. pnfs_set_lo_fail(data->lseg);
  233. } else
  234. rpc_restart_call_prepare(task);
  235. return -EAGAIN;
  236. }
  237. return 0;
  238. }
  239. static void filelayout_write_prepare(struct rpc_task *task, void *data)
  240. {
  241. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  242. if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
  243. &wdata->args.seq_args, &wdata->res.seq_res,
  244. task))
  245. return;
  246. rpc_call_start(task);
  247. }
  248. static void filelayout_write_call_done(struct rpc_task *task, void *data)
  249. {
  250. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  251. /* Note this may cause RPC to be resent */
  252. wdata->mds_ops->rpc_call_done(task, data);
  253. }
  254. static void filelayout_write_count_stats(struct rpc_task *task, void *data)
  255. {
  256. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  257. rpc_count_iostats(task, NFS_SERVER(wdata->inode)->client->cl_metrics);
  258. }
  259. static void filelayout_write_release(void *data)
  260. {
  261. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  262. put_lseg(wdata->lseg);
  263. wdata->mds_ops->rpc_release(data);
  264. }
  265. static void filelayout_commit_release(void *data)
  266. {
  267. struct nfs_write_data *wdata = (struct nfs_write_data *)data;
  268. nfs_commit_release_pages(wdata);
  269. if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
  270. nfs_commit_clear_lock(NFS_I(wdata->inode));
  271. put_lseg(wdata->lseg);
  272. nfs_commitdata_release(wdata);
  273. }
  274. static const struct rpc_call_ops filelayout_read_call_ops = {
  275. .rpc_call_prepare = filelayout_read_prepare,
  276. .rpc_call_done = filelayout_read_call_done,
  277. .rpc_count_stats = filelayout_read_count_stats,
  278. .rpc_release = filelayout_read_release,
  279. };
  280. static const struct rpc_call_ops filelayout_write_call_ops = {
  281. .rpc_call_prepare = filelayout_write_prepare,
  282. .rpc_call_done = filelayout_write_call_done,
  283. .rpc_count_stats = filelayout_write_count_stats,
  284. .rpc_release = filelayout_write_release,
  285. };
  286. static const struct rpc_call_ops filelayout_commit_call_ops = {
  287. .rpc_call_prepare = filelayout_write_prepare,
  288. .rpc_call_done = filelayout_write_call_done,
  289. .rpc_count_stats = filelayout_write_count_stats,
  290. .rpc_release = filelayout_commit_release,
  291. };
  292. static enum pnfs_try_status
  293. filelayout_read_pagelist(struct nfs_read_data *data)
  294. {
  295. struct pnfs_layout_segment *lseg = data->lseg;
  296. struct nfs4_pnfs_ds *ds;
  297. loff_t offset = data->args.offset;
  298. u32 j, idx;
  299. struct nfs_fh *fh;
  300. int status;
  301. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  302. __func__, data->inode->i_ino,
  303. data->args.pgbase, (size_t)data->args.count, offset);
  304. if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
  305. return PNFS_NOT_ATTEMPTED;
  306. /* Retrieve the correct rpc_client for the byte range */
  307. j = nfs4_fl_calc_j_index(lseg, offset);
  308. idx = nfs4_fl_calc_ds_index(lseg, j);
  309. ds = nfs4_fl_prepare_ds(lseg, idx);
  310. if (!ds) {
  311. /* Either layout fh index faulty, or ds connect failed */
  312. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  313. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  314. return PNFS_NOT_ATTEMPTED;
  315. }
  316. dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
  317. /* No multipath support. Use first DS */
  318. data->ds_clp = ds->ds_clp;
  319. fh = nfs4_fl_select_ds_fh(lseg, j);
  320. if (fh)
  321. data->args.fh = fh;
  322. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  323. data->mds_offset = offset;
  324. /* Perform an asynchronous read to ds */
  325. status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
  326. &filelayout_read_call_ops);
  327. BUG_ON(status != 0);
  328. return PNFS_ATTEMPTED;
  329. }
  330. /* Perform async writes. */
  331. static enum pnfs_try_status
  332. filelayout_write_pagelist(struct nfs_write_data *data, int sync)
  333. {
  334. struct pnfs_layout_segment *lseg = data->lseg;
  335. struct nfs4_pnfs_ds *ds;
  336. loff_t offset = data->args.offset;
  337. u32 j, idx;
  338. struct nfs_fh *fh;
  339. int status;
  340. if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
  341. return PNFS_NOT_ATTEMPTED;
  342. /* Retrieve the correct rpc_client for the byte range */
  343. j = nfs4_fl_calc_j_index(lseg, offset);
  344. idx = nfs4_fl_calc_ds_index(lseg, j);
  345. ds = nfs4_fl_prepare_ds(lseg, idx);
  346. if (!ds) {
  347. printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
  348. __func__);
  349. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  350. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  351. return PNFS_NOT_ATTEMPTED;
  352. }
  353. dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
  354. data->inode->i_ino, sync, (size_t) data->args.count, offset,
  355. ds->ds_remotestr);
  356. data->write_done_cb = filelayout_write_done_cb;
  357. data->ds_clp = ds->ds_clp;
  358. fh = nfs4_fl_select_ds_fh(lseg, j);
  359. if (fh)
  360. data->args.fh = fh;
  361. /*
  362. * Get the file offset on the dserver. Set the write offset to
  363. * this offset and save the original offset.
  364. */
  365. data->args.offset = filelayout_get_dserver_offset(lseg, offset);
  366. /* Perform an asynchronous write */
  367. status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
  368. &filelayout_write_call_ops, sync);
  369. BUG_ON(status != 0);
  370. return PNFS_ATTEMPTED;
  371. }
  372. /*
  373. * filelayout_check_layout()
  374. *
  375. * Make sure layout segment parameters are sane WRT the device.
  376. * At this point no generic layer initialization of the lseg has occurred,
  377. * and nothing has been added to the layout_hdr cache.
  378. *
  379. */
  380. static int
  381. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  382. struct nfs4_filelayout_segment *fl,
  383. struct nfs4_layoutget_res *lgr,
  384. struct nfs4_deviceid *id,
  385. gfp_t gfp_flags)
  386. {
  387. struct nfs4_deviceid_node *d;
  388. struct nfs4_file_layout_dsaddr *dsaddr;
  389. int status = -EINVAL;
  390. struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
  391. dprintk("--> %s\n", __func__);
  392. /* FIXME: remove this check when layout segment support is added */
  393. if (lgr->range.offset != 0 ||
  394. lgr->range.length != NFS4_MAX_UINT64) {
  395. dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
  396. __func__);
  397. goto out;
  398. }
  399. if (fl->pattern_offset > lgr->range.offset) {
  400. dprintk("%s pattern_offset %lld too large\n",
  401. __func__, fl->pattern_offset);
  402. goto out;
  403. }
  404. if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
  405. dprintk("%s Invalid stripe unit (%u)\n",
  406. __func__, fl->stripe_unit);
  407. goto out;
  408. }
  409. /* find and reference the deviceid */
  410. d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
  411. NFS_SERVER(lo->plh_inode)->nfs_client, id);
  412. if (d == NULL) {
  413. dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
  414. if (dsaddr == NULL)
  415. goto out;
  416. } else
  417. dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  418. /* Found deviceid is being reaped */
  419. if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
  420. goto out_put;
  421. fl->dsaddr = dsaddr;
  422. if (fl->first_stripe_index >= dsaddr->stripe_count) {
  423. dprintk("%s Bad first_stripe_index %u\n",
  424. __func__, fl->first_stripe_index);
  425. goto out_put;
  426. }
  427. if ((fl->stripe_type == STRIPE_SPARSE &&
  428. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  429. (fl->stripe_type == STRIPE_DENSE &&
  430. fl->num_fh != dsaddr->stripe_count)) {
  431. dprintk("%s num_fh %u not valid for given packing\n",
  432. __func__, fl->num_fh);
  433. goto out_put;
  434. }
  435. if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
  436. dprintk("%s Stripe unit (%u) not aligned with rsize %u "
  437. "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
  438. nfss->wsize);
  439. }
  440. status = 0;
  441. out:
  442. dprintk("--> %s returns %d\n", __func__, status);
  443. return status;
  444. out_put:
  445. nfs4_fl_put_deviceid(dsaddr);
  446. goto out;
  447. }
  448. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  449. {
  450. int i;
  451. for (i = 0; i < fl->num_fh; i++) {
  452. if (!fl->fh_array[i])
  453. break;
  454. kfree(fl->fh_array[i]);
  455. }
  456. kfree(fl->fh_array);
  457. fl->fh_array = NULL;
  458. }
  459. static void
  460. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  461. {
  462. filelayout_free_fh_array(fl);
  463. kfree(fl);
  464. }
  465. static int
  466. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  467. struct nfs4_filelayout_segment *fl,
  468. struct nfs4_layoutget_res *lgr,
  469. struct nfs4_deviceid *id,
  470. gfp_t gfp_flags)
  471. {
  472. struct xdr_stream stream;
  473. struct xdr_buf buf;
  474. struct page *scratch;
  475. __be32 *p;
  476. uint32_t nfl_util;
  477. int i;
  478. dprintk("%s: set_layout_map Begin\n", __func__);
  479. scratch = alloc_page(gfp_flags);
  480. if (!scratch)
  481. return -ENOMEM;
  482. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
  483. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  484. /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
  485. * num_fh (4) */
  486. p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
  487. if (unlikely(!p))
  488. goto out_err;
  489. memcpy(id, p, sizeof(*id));
  490. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  491. nfs4_print_deviceid(id);
  492. nfl_util = be32_to_cpup(p++);
  493. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  494. fl->commit_through_mds = 1;
  495. if (nfl_util & NFL4_UFLG_DENSE)
  496. fl->stripe_type = STRIPE_DENSE;
  497. else
  498. fl->stripe_type = STRIPE_SPARSE;
  499. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  500. fl->first_stripe_index = be32_to_cpup(p++);
  501. p = xdr_decode_hyper(p, &fl->pattern_offset);
  502. fl->num_fh = be32_to_cpup(p++);
  503. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  504. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  505. fl->pattern_offset);
  506. /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
  507. * Futher checking is done in filelayout_check_layout */
  508. if (fl->num_fh >
  509. max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
  510. goto out_err;
  511. if (fl->num_fh > 0) {
  512. fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
  513. gfp_flags);
  514. if (!fl->fh_array)
  515. goto out_err;
  516. }
  517. for (i = 0; i < fl->num_fh; i++) {
  518. /* Do we want to use a mempool here? */
  519. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
  520. if (!fl->fh_array[i])
  521. goto out_err_free;
  522. p = xdr_inline_decode(&stream, 4);
  523. if (unlikely(!p))
  524. goto out_err_free;
  525. fl->fh_array[i]->size = be32_to_cpup(p++);
  526. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  527. printk(KERN_ERR "NFS: Too big fh %d received %d\n",
  528. i, fl->fh_array[i]->size);
  529. goto out_err_free;
  530. }
  531. p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
  532. if (unlikely(!p))
  533. goto out_err_free;
  534. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  535. dprintk("DEBUG: %s: fh len %d\n", __func__,
  536. fl->fh_array[i]->size);
  537. }
  538. __free_page(scratch);
  539. return 0;
  540. out_err_free:
  541. filelayout_free_fh_array(fl);
  542. out_err:
  543. __free_page(scratch);
  544. return -EIO;
  545. }
  546. static void
  547. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  548. {
  549. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  550. dprintk("--> %s\n", __func__);
  551. nfs4_fl_put_deviceid(fl->dsaddr);
  552. kfree(fl->commit_buckets);
  553. _filelayout_free_lseg(fl);
  554. }
  555. static struct pnfs_layout_segment *
  556. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  557. struct nfs4_layoutget_res *lgr,
  558. gfp_t gfp_flags)
  559. {
  560. struct nfs4_filelayout_segment *fl;
  561. int rc;
  562. struct nfs4_deviceid id;
  563. dprintk("--> %s\n", __func__);
  564. fl = kzalloc(sizeof(*fl), gfp_flags);
  565. if (!fl)
  566. return NULL;
  567. rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
  568. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
  569. _filelayout_free_lseg(fl);
  570. return NULL;
  571. }
  572. /* This assumes there is only one IOMODE_RW lseg. What
  573. * we really want to do is have a layout_hdr level
  574. * dictionary of <multipath_list4, fh> keys, each
  575. * associated with a struct list_head, populated by calls
  576. * to filelayout_write_pagelist().
  577. * */
  578. if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
  579. int i;
  580. int size = (fl->stripe_type == STRIPE_SPARSE) ?
  581. fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
  582. fl->commit_buckets = kcalloc(size, sizeof(struct nfs4_fl_commit_bucket), gfp_flags);
  583. if (!fl->commit_buckets) {
  584. filelayout_free_lseg(&fl->generic_hdr);
  585. return NULL;
  586. }
  587. fl->number_of_buckets = size;
  588. for (i = 0; i < size; i++) {
  589. INIT_LIST_HEAD(&fl->commit_buckets[i].written);
  590. INIT_LIST_HEAD(&fl->commit_buckets[i].committing);
  591. }
  592. }
  593. return &fl->generic_hdr;
  594. }
  595. /*
  596. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  597. *
  598. * return true : coalesce page
  599. * return false : don't coalesce page
  600. */
  601. static bool
  602. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  603. struct nfs_page *req)
  604. {
  605. u64 p_stripe, r_stripe;
  606. u32 stripe_unit;
  607. if (!pnfs_generic_pg_test(pgio, prev, req) ||
  608. !nfs_generic_pg_test(pgio, prev, req))
  609. return false;
  610. p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
  611. r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
  612. stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  613. do_div(p_stripe, stripe_unit);
  614. do_div(r_stripe, stripe_unit);
  615. return (p_stripe == r_stripe);
  616. }
  617. static void
  618. filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  619. struct nfs_page *req)
  620. {
  621. BUG_ON(pgio->pg_lseg != NULL);
  622. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  623. req->wb_context,
  624. 0,
  625. NFS4_MAX_UINT64,
  626. IOMODE_READ,
  627. GFP_KERNEL);
  628. /* If no lseg, fall back to read through mds */
  629. if (pgio->pg_lseg == NULL)
  630. nfs_pageio_reset_read_mds(pgio);
  631. }
  632. static void
  633. filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  634. struct nfs_page *req)
  635. {
  636. BUG_ON(pgio->pg_lseg != NULL);
  637. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  638. req->wb_context,
  639. 0,
  640. NFS4_MAX_UINT64,
  641. IOMODE_RW,
  642. GFP_NOFS);
  643. /* If no lseg, fall back to write through mds */
  644. if (pgio->pg_lseg == NULL)
  645. nfs_pageio_reset_write_mds(pgio);
  646. }
  647. static const struct nfs_pageio_ops filelayout_pg_read_ops = {
  648. .pg_init = filelayout_pg_init_read,
  649. .pg_test = filelayout_pg_test,
  650. .pg_doio = pnfs_generic_pg_readpages,
  651. };
  652. static const struct nfs_pageio_ops filelayout_pg_write_ops = {
  653. .pg_init = filelayout_pg_init_write,
  654. .pg_test = filelayout_pg_test,
  655. .pg_doio = pnfs_generic_pg_writepages,
  656. };
  657. static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
  658. {
  659. if (fl->stripe_type == STRIPE_SPARSE)
  660. return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
  661. else
  662. return j;
  663. }
  664. /* The generic layer is about to remove the req from the commit list.
  665. * If this will make the bucket empty, it will need to put the lseg reference.
  666. */
  667. static void
  668. filelayout_clear_request_commit(struct nfs_page *req)
  669. {
  670. struct pnfs_layout_segment *freeme = NULL;
  671. struct inode *inode = req->wb_context->dentry->d_inode;
  672. spin_lock(&inode->i_lock);
  673. if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
  674. goto out;
  675. if (list_is_singular(&req->wb_list)) {
  676. struct pnfs_layout_segment *lseg;
  677. /* From here we can find the bucket, but for the moment,
  678. * since there is only one relevant lseg...
  679. */
  680. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  681. if (lseg->pls_range.iomode == IOMODE_RW) {
  682. freeme = lseg;
  683. break;
  684. }
  685. }
  686. }
  687. out:
  688. nfs_request_remove_commit_list(req);
  689. spin_unlock(&inode->i_lock);
  690. put_lseg(freeme);
  691. }
  692. static struct list_head *
  693. filelayout_choose_commit_list(struct nfs_page *req,
  694. struct pnfs_layout_segment *lseg)
  695. {
  696. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  697. u32 i, j;
  698. struct list_head *list;
  699. if (fl->commit_through_mds)
  700. return &NFS_I(req->wb_context->dentry->d_inode)->commit_list;
  701. /* Note that we are calling nfs4_fl_calc_j_index on each page
  702. * that ends up being committed to a data server. An attractive
  703. * alternative is to add a field to nfs_write_data and nfs_page
  704. * to store the value calculated in filelayout_write_pagelist
  705. * and just use that here.
  706. */
  707. j = nfs4_fl_calc_j_index(lseg,
  708. (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
  709. i = select_bucket_index(fl, j);
  710. list = &fl->commit_buckets[i].written;
  711. if (list_empty(list)) {
  712. /* Non-empty buckets hold a reference on the lseg. That ref
  713. * is normally transferred to the COMMIT call and released
  714. * there. It could also be released if the last req is pulled
  715. * off due to a rewrite, in which case it will be done in
  716. * filelayout_remove_commit_req
  717. */
  718. get_lseg(lseg);
  719. }
  720. set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  721. return list;
  722. }
  723. static void
  724. filelayout_mark_request_commit(struct nfs_page *req,
  725. struct pnfs_layout_segment *lseg)
  726. {
  727. struct list_head *list;
  728. list = filelayout_choose_commit_list(req, lseg);
  729. nfs_request_add_commit_list(req, list);
  730. }
  731. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  732. {
  733. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  734. if (flseg->stripe_type == STRIPE_SPARSE)
  735. return i;
  736. else
  737. return nfs4_fl_calc_ds_index(lseg, i);
  738. }
  739. static struct nfs_fh *
  740. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  741. {
  742. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  743. if (flseg->stripe_type == STRIPE_SPARSE) {
  744. if (flseg->num_fh == 1)
  745. i = 0;
  746. else if (flseg->num_fh == 0)
  747. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  748. return NULL;
  749. }
  750. return flseg->fh_array[i];
  751. }
  752. static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
  753. {
  754. struct pnfs_layout_segment *lseg = data->lseg;
  755. struct nfs4_pnfs_ds *ds;
  756. u32 idx;
  757. struct nfs_fh *fh;
  758. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  759. ds = nfs4_fl_prepare_ds(lseg, idx);
  760. if (!ds) {
  761. printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
  762. __func__);
  763. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  764. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  765. prepare_to_resend_writes(data);
  766. filelayout_commit_release(data);
  767. return -EAGAIN;
  768. }
  769. dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
  770. data->write_done_cb = filelayout_commit_done_cb;
  771. data->ds_clp = ds->ds_clp;
  772. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  773. if (fh)
  774. data->args.fh = fh;
  775. return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
  776. &filelayout_commit_call_ops, how);
  777. }
  778. /*
  779. * This is only useful while we are using whole file layouts.
  780. */
  781. static struct pnfs_layout_segment *
  782. find_only_write_lseg_locked(struct inode *inode)
  783. {
  784. struct pnfs_layout_segment *lseg;
  785. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
  786. if (lseg->pls_range.iomode == IOMODE_RW)
  787. return lseg;
  788. return NULL;
  789. }
  790. static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
  791. {
  792. struct pnfs_layout_segment *rv;
  793. spin_lock(&inode->i_lock);
  794. rv = find_only_write_lseg_locked(inode);
  795. if (rv)
  796. get_lseg(rv);
  797. spin_unlock(&inode->i_lock);
  798. return rv;
  799. }
  800. static int
  801. filelayout_scan_ds_commit_list(struct nfs4_fl_commit_bucket *bucket, int max,
  802. spinlock_t *lock)
  803. {
  804. struct list_head *src = &bucket->written;
  805. struct list_head *dst = &bucket->committing;
  806. struct nfs_page *req, *tmp;
  807. int ret = 0;
  808. list_for_each_entry_safe(req, tmp, src, wb_list) {
  809. if (!nfs_lock_request(req))
  810. continue;
  811. if (cond_resched_lock(lock))
  812. list_safe_reset_next(req, tmp, wb_list);
  813. nfs_request_remove_commit_list(req);
  814. clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  815. nfs_list_add_request(req, dst);
  816. ret++;
  817. if (ret == max)
  818. break;
  819. }
  820. return ret;
  821. }
  822. /* Move reqs from written to committing lists, returning count of number moved.
  823. * Note called with i_lock held.
  824. */
  825. static int filelayout_scan_commit_lists(struct inode *inode, int max,
  826. spinlock_t *lock)
  827. {
  828. struct pnfs_layout_segment *lseg;
  829. struct nfs4_filelayout_segment *fl;
  830. int i, rv = 0, cnt;
  831. lseg = find_only_write_lseg_locked(inode);
  832. if (!lseg)
  833. goto out_done;
  834. fl = FILELAYOUT_LSEG(lseg);
  835. if (fl->commit_through_mds)
  836. goto out_done;
  837. for (i = 0; i < fl->number_of_buckets && max != 0; i++) {
  838. cnt = filelayout_scan_ds_commit_list(&fl->commit_buckets[i],
  839. max, lock);
  840. max -= cnt;
  841. rv += cnt;
  842. }
  843. out_done:
  844. return rv;
  845. }
  846. static unsigned int
  847. alloc_ds_commits(struct inode *inode, struct list_head *list)
  848. {
  849. struct pnfs_layout_segment *lseg;
  850. struct nfs4_filelayout_segment *fl;
  851. struct nfs_write_data *data;
  852. int i, j;
  853. unsigned int nreq = 0;
  854. /* Won't need this when non-whole file layout segments are supported
  855. * instead we will use a pnfs_layout_hdr structure */
  856. lseg = find_only_write_lseg(inode);
  857. if (!lseg)
  858. return 0;
  859. fl = FILELAYOUT_LSEG(lseg);
  860. for (i = 0; i < fl->number_of_buckets; i++) {
  861. if (list_empty(&fl->commit_buckets[i].committing))
  862. continue;
  863. data = nfs_commitdata_alloc();
  864. if (!data)
  865. break;
  866. data->ds_commit_index = i;
  867. data->lseg = lseg;
  868. list_add(&data->pages, list);
  869. nreq++;
  870. }
  871. /* Clean up on error */
  872. for (j = i; j < fl->number_of_buckets; j++) {
  873. if (list_empty(&fl->commit_buckets[i].committing))
  874. continue;
  875. nfs_retry_commit(&fl->commit_buckets[i].committing, lseg);
  876. put_lseg(lseg); /* associated with emptying bucket */
  877. }
  878. put_lseg(lseg);
  879. /* Caller will clean up entries put on list */
  880. return nreq;
  881. }
  882. /* This follows nfs_commit_list pretty closely */
  883. static int
  884. filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  885. int how)
  886. {
  887. struct nfs_write_data *data, *tmp;
  888. LIST_HEAD(list);
  889. unsigned int nreq = 0;
  890. if (!list_empty(mds_pages)) {
  891. data = nfs_commitdata_alloc();
  892. if (data != NULL) {
  893. data->lseg = NULL;
  894. list_add(&data->pages, &list);
  895. nreq++;
  896. } else
  897. nfs_retry_commit(mds_pages, NULL);
  898. }
  899. nreq += alloc_ds_commits(inode, &list);
  900. if (nreq == 0) {
  901. nfs_commit_clear_lock(NFS_I(inode));
  902. goto out;
  903. }
  904. atomic_add(nreq, &NFS_I(inode)->commits_outstanding);
  905. list_for_each_entry_safe(data, tmp, &list, pages) {
  906. list_del_init(&data->pages);
  907. if (!data->lseg) {
  908. nfs_init_commit(data, mds_pages, NULL);
  909. nfs_initiate_commit(data, NFS_CLIENT(inode),
  910. data->mds_ops, how);
  911. } else {
  912. nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index].committing, data->lseg);
  913. filelayout_initiate_commit(data, how);
  914. }
  915. }
  916. out:
  917. return PNFS_ATTEMPTED;
  918. }
  919. static void
  920. filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
  921. {
  922. nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
  923. }
  924. static struct pnfs_layoutdriver_type filelayout_type = {
  925. .id = LAYOUT_NFSV4_1_FILES,
  926. .name = "LAYOUT_NFSV4_1_FILES",
  927. .owner = THIS_MODULE,
  928. .alloc_lseg = filelayout_alloc_lseg,
  929. .free_lseg = filelayout_free_lseg,
  930. .pg_read_ops = &filelayout_pg_read_ops,
  931. .pg_write_ops = &filelayout_pg_write_ops,
  932. .mark_request_commit = filelayout_mark_request_commit,
  933. .clear_request_commit = filelayout_clear_request_commit,
  934. .scan_commit_lists = filelayout_scan_commit_lists,
  935. .commit_pagelist = filelayout_commit_pagelist,
  936. .read_pagelist = filelayout_read_pagelist,
  937. .write_pagelist = filelayout_write_pagelist,
  938. .free_deviceid_node = filelayout_free_deveiceid_node,
  939. };
  940. static int __init nfs4filelayout_init(void)
  941. {
  942. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  943. __func__);
  944. return pnfs_register_layoutdriver(&filelayout_type);
  945. }
  946. static void __exit nfs4filelayout_exit(void)
  947. {
  948. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  949. __func__);
  950. pnfs_unregister_layoutdriver(&filelayout_type);
  951. }
  952. MODULE_ALIAS("nfs-layouttype4-1");
  953. module_init(nfs4filelayout_init);
  954. module_exit(nfs4filelayout_exit);