filelayout.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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 "../nfs4session.h"
  36. #include "../internal.h"
  37. #include "../delegation.h"
  38. #include "filelayout.h"
  39. #include "../nfs4trace.h"
  40. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  41. MODULE_LICENSE("GPL");
  42. MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
  43. MODULE_DESCRIPTION("The NFSv4 file layout driver");
  44. #define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
  45. static loff_t
  46. filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
  47. loff_t offset)
  48. {
  49. u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
  50. u64 stripe_no;
  51. u32 rem;
  52. offset -= flseg->pattern_offset;
  53. stripe_no = div_u64(offset, stripe_width);
  54. div_u64_rem(offset, flseg->stripe_unit, &rem);
  55. return stripe_no * flseg->stripe_unit + rem;
  56. }
  57. /* This function is used by the layout driver to calculate the
  58. * offset of the file on the dserver based on whether the
  59. * layout type is STRIPE_DENSE or STRIPE_SPARSE
  60. */
  61. static loff_t
  62. filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
  63. {
  64. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  65. switch (flseg->stripe_type) {
  66. case STRIPE_SPARSE:
  67. return offset;
  68. case STRIPE_DENSE:
  69. return filelayout_get_dense_offset(flseg, offset);
  70. }
  71. BUG();
  72. }
  73. static void filelayout_reset_write(struct nfs_pgio_header *hdr)
  74. {
  75. struct rpc_task *task = &hdr->task;
  76. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  77. dprintk("%s Reset task %5u for i/o through MDS "
  78. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  79. hdr->task.tk_pid,
  80. hdr->inode->i_sb->s_id,
  81. (unsigned long long)NFS_FILEID(hdr->inode),
  82. hdr->args.count,
  83. (unsigned long long)hdr->args.offset);
  84. task->tk_status = pnfs_write_done_resend_to_mds(hdr);
  85. }
  86. }
  87. static void filelayout_reset_read(struct nfs_pgio_header *hdr)
  88. {
  89. struct rpc_task *task = &hdr->task;
  90. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  91. dprintk("%s Reset task %5u for i/o through MDS "
  92. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  93. hdr->task.tk_pid,
  94. hdr->inode->i_sb->s_id,
  95. (unsigned long long)NFS_FILEID(hdr->inode),
  96. hdr->args.count,
  97. (unsigned long long)hdr->args.offset);
  98. task->tk_status = pnfs_read_done_resend_to_mds(hdr);
  99. }
  100. }
  101. static void filelayout_fenceme(struct inode *inode, struct pnfs_layout_hdr *lo)
  102. {
  103. if (!test_and_clear_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
  104. return;
  105. pnfs_return_layout(inode);
  106. }
  107. static int filelayout_async_handle_error(struct rpc_task *task,
  108. struct nfs4_state *state,
  109. struct nfs_client *clp,
  110. struct pnfs_layout_segment *lseg)
  111. {
  112. struct pnfs_layout_hdr *lo = lseg->pls_layout;
  113. struct inode *inode = lo->plh_inode;
  114. struct nfs_server *mds_server = NFS_SERVER(inode);
  115. struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
  116. struct nfs_client *mds_client = mds_server->nfs_client;
  117. struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
  118. if (task->tk_status >= 0)
  119. return 0;
  120. switch (task->tk_status) {
  121. /* MDS state errors */
  122. case -NFS4ERR_DELEG_REVOKED:
  123. case -NFS4ERR_ADMIN_REVOKED:
  124. case -NFS4ERR_BAD_STATEID:
  125. if (state == NULL)
  126. break;
  127. nfs_remove_bad_delegation(state->inode);
  128. case -NFS4ERR_OPENMODE:
  129. if (state == NULL)
  130. break;
  131. if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
  132. goto out_bad_stateid;
  133. goto wait_on_recovery;
  134. case -NFS4ERR_EXPIRED:
  135. if (state != NULL) {
  136. if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
  137. goto out_bad_stateid;
  138. }
  139. nfs4_schedule_lease_recovery(mds_client);
  140. goto wait_on_recovery;
  141. /* DS session errors */
  142. case -NFS4ERR_BADSESSION:
  143. case -NFS4ERR_BADSLOT:
  144. case -NFS4ERR_BAD_HIGH_SLOT:
  145. case -NFS4ERR_DEADSESSION:
  146. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  147. case -NFS4ERR_SEQ_FALSE_RETRY:
  148. case -NFS4ERR_SEQ_MISORDERED:
  149. dprintk("%s ERROR %d, Reset session. Exchangeid "
  150. "flags 0x%x\n", __func__, task->tk_status,
  151. clp->cl_exchange_flags);
  152. nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
  153. break;
  154. case -NFS4ERR_DELAY:
  155. case -NFS4ERR_GRACE:
  156. rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
  157. break;
  158. case -NFS4ERR_RETRY_UNCACHED_REP:
  159. break;
  160. /* Invalidate Layout errors */
  161. case -NFS4ERR_PNFS_NO_LAYOUT:
  162. case -ESTALE: /* mapped NFS4ERR_STALE */
  163. case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
  164. case -EISDIR: /* mapped NFS4ERR_ISDIR */
  165. case -NFS4ERR_FHEXPIRED:
  166. case -NFS4ERR_WRONG_TYPE:
  167. dprintk("%s Invalid layout error %d\n", __func__,
  168. task->tk_status);
  169. /*
  170. * Destroy layout so new i/o will get a new layout.
  171. * Layout will not be destroyed until all current lseg
  172. * references are put. Mark layout as invalid to resend failed
  173. * i/o and all i/o waiting on the slot table to the MDS until
  174. * layout is destroyed and a new valid layout is obtained.
  175. */
  176. pnfs_destroy_layout(NFS_I(inode));
  177. rpc_wake_up(&tbl->slot_tbl_waitq);
  178. goto reset;
  179. /* RPC connection errors */
  180. case -ECONNREFUSED:
  181. case -EHOSTDOWN:
  182. case -EHOSTUNREACH:
  183. case -ENETUNREACH:
  184. case -EIO:
  185. case -ETIMEDOUT:
  186. case -EPIPE:
  187. dprintk("%s DS connection error %d\n", __func__,
  188. task->tk_status);
  189. nfs4_mark_deviceid_unavailable(devid);
  190. set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
  191. rpc_wake_up(&tbl->slot_tbl_waitq);
  192. /* fall through */
  193. default:
  194. reset:
  195. dprintk("%s Retry through MDS. Error %d\n", __func__,
  196. task->tk_status);
  197. return -NFS4ERR_RESET_TO_MDS;
  198. }
  199. out:
  200. task->tk_status = 0;
  201. return -EAGAIN;
  202. out_bad_stateid:
  203. task->tk_status = -EIO;
  204. return 0;
  205. wait_on_recovery:
  206. rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
  207. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
  208. rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
  209. goto out;
  210. }
  211. /* NFS_PROTO call done callback routines */
  212. static int filelayout_read_done_cb(struct rpc_task *task,
  213. struct nfs_pgio_header *hdr)
  214. {
  215. int err;
  216. trace_nfs4_pnfs_read(hdr, task->tk_status);
  217. err = filelayout_async_handle_error(task, hdr->args.context->state,
  218. hdr->ds_clp, hdr->lseg);
  219. switch (err) {
  220. case -NFS4ERR_RESET_TO_MDS:
  221. filelayout_reset_read(hdr);
  222. return task->tk_status;
  223. case -EAGAIN:
  224. rpc_restart_call_prepare(task);
  225. return -EAGAIN;
  226. }
  227. return 0;
  228. }
  229. /*
  230. * We reference the rpc_cred of the first WRITE that triggers the need for
  231. * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
  232. * rfc5661 is not clear about which credential should be used.
  233. */
  234. static void
  235. filelayout_set_layoutcommit(struct nfs_pgio_header *hdr)
  236. {
  237. if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
  238. hdr->res.verf->committed != NFS_DATA_SYNC)
  239. return;
  240. pnfs_set_layoutcommit(hdr);
  241. dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
  242. (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
  243. }
  244. bool
  245. filelayout_test_devid_unavailable(struct nfs4_deviceid_node *node)
  246. {
  247. return filelayout_test_devid_invalid(node) ||
  248. nfs4_test_deviceid_unavailable(node);
  249. }
  250. static bool
  251. filelayout_reset_to_mds(struct pnfs_layout_segment *lseg)
  252. {
  253. struct nfs4_deviceid_node *node = FILELAYOUT_DEVID_NODE(lseg);
  254. return filelayout_test_devid_unavailable(node);
  255. }
  256. /*
  257. * Call ops for the async read/write cases
  258. * In the case of dense layouts, the offset needs to be reset to its
  259. * original value.
  260. */
  261. static void filelayout_read_prepare(struct rpc_task *task, void *data)
  262. {
  263. struct nfs_pgio_header *hdr = data;
  264. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  265. rpc_exit(task, -EIO);
  266. return;
  267. }
  268. if (filelayout_reset_to_mds(hdr->lseg)) {
  269. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  270. filelayout_reset_read(hdr);
  271. rpc_exit(task, 0);
  272. return;
  273. }
  274. hdr->pgio_done_cb = filelayout_read_done_cb;
  275. if (nfs41_setup_sequence(hdr->ds_clp->cl_session,
  276. &hdr->args.seq_args,
  277. &hdr->res.seq_res,
  278. task))
  279. return;
  280. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  281. hdr->args.lock_context, FMODE_READ) == -EIO)
  282. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  283. }
  284. static void filelayout_read_call_done(struct rpc_task *task, void *data)
  285. {
  286. struct nfs_pgio_header *hdr = data;
  287. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  288. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  289. task->tk_status == 0) {
  290. nfs41_sequence_done(task, &hdr->res.seq_res);
  291. return;
  292. }
  293. /* Note this may cause RPC to be resent */
  294. hdr->mds_ops->rpc_call_done(task, data);
  295. }
  296. static void filelayout_read_count_stats(struct rpc_task *task, void *data)
  297. {
  298. struct nfs_pgio_header *hdr = data;
  299. rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
  300. }
  301. static void filelayout_read_release(void *data)
  302. {
  303. struct nfs_pgio_header *hdr = data;
  304. struct pnfs_layout_hdr *lo = hdr->lseg->pls_layout;
  305. filelayout_fenceme(lo->plh_inode, lo);
  306. nfs_put_client(hdr->ds_clp);
  307. hdr->mds_ops->rpc_release(data);
  308. }
  309. static int filelayout_write_done_cb(struct rpc_task *task,
  310. struct nfs_pgio_header *hdr)
  311. {
  312. int err;
  313. trace_nfs4_pnfs_write(hdr, task->tk_status);
  314. err = filelayout_async_handle_error(task, hdr->args.context->state,
  315. hdr->ds_clp, hdr->lseg);
  316. switch (err) {
  317. case -NFS4ERR_RESET_TO_MDS:
  318. filelayout_reset_write(hdr);
  319. return task->tk_status;
  320. case -EAGAIN:
  321. rpc_restart_call_prepare(task);
  322. return -EAGAIN;
  323. }
  324. filelayout_set_layoutcommit(hdr);
  325. return 0;
  326. }
  327. /* Fake up some data that will cause nfs_commit_release to retry the writes. */
  328. static void prepare_to_resend_writes(struct nfs_commit_data *data)
  329. {
  330. struct nfs_page *first = nfs_list_entry(data->pages.next);
  331. data->task.tk_status = 0;
  332. memcpy(&data->verf.verifier, &first->wb_verf,
  333. sizeof(data->verf.verifier));
  334. data->verf.verifier.data[0]++; /* ensure verifier mismatch */
  335. }
  336. static int filelayout_commit_done_cb(struct rpc_task *task,
  337. struct nfs_commit_data *data)
  338. {
  339. int err;
  340. trace_nfs4_pnfs_commit_ds(data, task->tk_status);
  341. err = filelayout_async_handle_error(task, NULL, data->ds_clp,
  342. data->lseg);
  343. switch (err) {
  344. case -NFS4ERR_RESET_TO_MDS:
  345. prepare_to_resend_writes(data);
  346. return -EAGAIN;
  347. case -EAGAIN:
  348. rpc_restart_call_prepare(task);
  349. return -EAGAIN;
  350. }
  351. if (data->verf.committed == NFS_UNSTABLE)
  352. pnfs_commit_set_layoutcommit(data);
  353. return 0;
  354. }
  355. static void filelayout_write_prepare(struct rpc_task *task, void *data)
  356. {
  357. struct nfs_pgio_header *hdr = data;
  358. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  359. rpc_exit(task, -EIO);
  360. return;
  361. }
  362. if (filelayout_reset_to_mds(hdr->lseg)) {
  363. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  364. filelayout_reset_write(hdr);
  365. rpc_exit(task, 0);
  366. return;
  367. }
  368. if (nfs41_setup_sequence(hdr->ds_clp->cl_session,
  369. &hdr->args.seq_args,
  370. &hdr->res.seq_res,
  371. task))
  372. return;
  373. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  374. hdr->args.lock_context, FMODE_WRITE) == -EIO)
  375. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  376. }
  377. static void filelayout_write_call_done(struct rpc_task *task, void *data)
  378. {
  379. struct nfs_pgio_header *hdr = data;
  380. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  381. task->tk_status == 0) {
  382. nfs41_sequence_done(task, &hdr->res.seq_res);
  383. return;
  384. }
  385. /* Note this may cause RPC to be resent */
  386. hdr->mds_ops->rpc_call_done(task, data);
  387. }
  388. static void filelayout_write_count_stats(struct rpc_task *task, void *data)
  389. {
  390. struct nfs_pgio_header *hdr = data;
  391. rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
  392. }
  393. static void filelayout_write_release(void *data)
  394. {
  395. struct nfs_pgio_header *hdr = data;
  396. struct pnfs_layout_hdr *lo = hdr->lseg->pls_layout;
  397. filelayout_fenceme(lo->plh_inode, lo);
  398. nfs_put_client(hdr->ds_clp);
  399. hdr->mds_ops->rpc_release(data);
  400. }
  401. static void filelayout_commit_prepare(struct rpc_task *task, void *data)
  402. {
  403. struct nfs_commit_data *wdata = data;
  404. nfs41_setup_sequence(wdata->ds_clp->cl_session,
  405. &wdata->args.seq_args,
  406. &wdata->res.seq_res,
  407. task);
  408. }
  409. static void filelayout_write_commit_done(struct rpc_task *task, void *data)
  410. {
  411. struct nfs_commit_data *wdata = data;
  412. /* Note this may cause RPC to be resent */
  413. wdata->mds_ops->rpc_call_done(task, data);
  414. }
  415. static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
  416. {
  417. struct nfs_commit_data *cdata = data;
  418. rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
  419. }
  420. static void filelayout_commit_release(void *calldata)
  421. {
  422. struct nfs_commit_data *data = calldata;
  423. data->completion_ops->completion(data);
  424. pnfs_put_lseg(data->lseg);
  425. nfs_put_client(data->ds_clp);
  426. nfs_commitdata_release(data);
  427. }
  428. static const struct rpc_call_ops filelayout_read_call_ops = {
  429. .rpc_call_prepare = filelayout_read_prepare,
  430. .rpc_call_done = filelayout_read_call_done,
  431. .rpc_count_stats = filelayout_read_count_stats,
  432. .rpc_release = filelayout_read_release,
  433. };
  434. static const struct rpc_call_ops filelayout_write_call_ops = {
  435. .rpc_call_prepare = filelayout_write_prepare,
  436. .rpc_call_done = filelayout_write_call_done,
  437. .rpc_count_stats = filelayout_write_count_stats,
  438. .rpc_release = filelayout_write_release,
  439. };
  440. static const struct rpc_call_ops filelayout_commit_call_ops = {
  441. .rpc_call_prepare = filelayout_commit_prepare,
  442. .rpc_call_done = filelayout_write_commit_done,
  443. .rpc_count_stats = filelayout_commit_count_stats,
  444. .rpc_release = filelayout_commit_release,
  445. };
  446. static enum pnfs_try_status
  447. filelayout_read_pagelist(struct nfs_pgio_header *hdr)
  448. {
  449. struct pnfs_layout_segment *lseg = hdr->lseg;
  450. struct nfs4_pnfs_ds *ds;
  451. struct rpc_clnt *ds_clnt;
  452. loff_t offset = hdr->args.offset;
  453. u32 j, idx;
  454. struct nfs_fh *fh;
  455. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  456. __func__, hdr->inode->i_ino,
  457. hdr->args.pgbase, (size_t)hdr->args.count, offset);
  458. /* Retrieve the correct rpc_client for the byte range */
  459. j = nfs4_fl_calc_j_index(lseg, offset);
  460. idx = nfs4_fl_calc_ds_index(lseg, j);
  461. ds = nfs4_fl_prepare_ds(lseg, idx);
  462. if (!ds)
  463. return PNFS_NOT_ATTEMPTED;
  464. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
  465. if (IS_ERR(ds_clnt))
  466. return PNFS_NOT_ATTEMPTED;
  467. dprintk("%s USE DS: %s cl_count %d\n", __func__,
  468. ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
  469. /* No multipath support. Use first DS */
  470. atomic_inc(&ds->ds_clp->cl_count);
  471. hdr->ds_clp = ds->ds_clp;
  472. hdr->ds_idx = idx;
  473. fh = nfs4_fl_select_ds_fh(lseg, j);
  474. if (fh)
  475. hdr->args.fh = fh;
  476. hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
  477. hdr->mds_offset = offset;
  478. /* Perform an asynchronous read to ds */
  479. nfs_initiate_pgio(ds_clnt, hdr,
  480. &filelayout_read_call_ops, 0, RPC_TASK_SOFTCONN);
  481. return PNFS_ATTEMPTED;
  482. }
  483. /* Perform async writes. */
  484. static enum pnfs_try_status
  485. filelayout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
  486. {
  487. struct pnfs_layout_segment *lseg = hdr->lseg;
  488. struct nfs4_pnfs_ds *ds;
  489. struct rpc_clnt *ds_clnt;
  490. loff_t offset = hdr->args.offset;
  491. u32 j, idx;
  492. struct nfs_fh *fh;
  493. /* Retrieve the correct rpc_client for the byte range */
  494. j = nfs4_fl_calc_j_index(lseg, offset);
  495. idx = nfs4_fl_calc_ds_index(lseg, j);
  496. ds = nfs4_fl_prepare_ds(lseg, idx);
  497. if (!ds)
  498. return PNFS_NOT_ATTEMPTED;
  499. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
  500. if (IS_ERR(ds_clnt))
  501. return PNFS_NOT_ATTEMPTED;
  502. dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
  503. __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
  504. offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
  505. hdr->pgio_done_cb = filelayout_write_done_cb;
  506. atomic_inc(&ds->ds_clp->cl_count);
  507. hdr->ds_clp = ds->ds_clp;
  508. hdr->ds_idx = idx;
  509. fh = nfs4_fl_select_ds_fh(lseg, j);
  510. if (fh)
  511. hdr->args.fh = fh;
  512. hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
  513. /* Perform an asynchronous write */
  514. nfs_initiate_pgio(ds_clnt, hdr,
  515. &filelayout_write_call_ops, sync,
  516. RPC_TASK_SOFTCONN);
  517. return PNFS_ATTEMPTED;
  518. }
  519. /*
  520. * filelayout_check_layout()
  521. *
  522. * Make sure layout segment parameters are sane WRT the device.
  523. * At this point no generic layer initialization of the lseg has occurred,
  524. * and nothing has been added to the layout_hdr cache.
  525. *
  526. */
  527. static int
  528. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  529. struct nfs4_filelayout_segment *fl,
  530. struct nfs4_layoutget_res *lgr,
  531. struct nfs4_deviceid *id,
  532. gfp_t gfp_flags)
  533. {
  534. struct nfs4_deviceid_node *d;
  535. struct nfs4_file_layout_dsaddr *dsaddr;
  536. int status = -EINVAL;
  537. dprintk("--> %s\n", __func__);
  538. /* FIXME: remove this check when layout segment support is added */
  539. if (lgr->range.offset != 0 ||
  540. lgr->range.length != NFS4_MAX_UINT64) {
  541. dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
  542. __func__);
  543. goto out;
  544. }
  545. if (fl->pattern_offset > lgr->range.offset) {
  546. dprintk("%s pattern_offset %lld too large\n",
  547. __func__, fl->pattern_offset);
  548. goto out;
  549. }
  550. if (!fl->stripe_unit) {
  551. dprintk("%s Invalid stripe unit (%u)\n",
  552. __func__, fl->stripe_unit);
  553. goto out;
  554. }
  555. /* find and reference the deviceid */
  556. d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
  557. NFS_SERVER(lo->plh_inode)->nfs_client, id);
  558. if (d == NULL) {
  559. dsaddr = filelayout_get_device_info(lo->plh_inode, id,
  560. lo->plh_lc_cred, gfp_flags);
  561. if (dsaddr == NULL)
  562. goto out;
  563. } else
  564. dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  565. /* Found deviceid is unavailable */
  566. if (filelayout_test_devid_unavailable(&dsaddr->id_node))
  567. goto out_put;
  568. fl->dsaddr = dsaddr;
  569. if (fl->first_stripe_index >= dsaddr->stripe_count) {
  570. dprintk("%s Bad first_stripe_index %u\n",
  571. __func__, fl->first_stripe_index);
  572. goto out_put;
  573. }
  574. if ((fl->stripe_type == STRIPE_SPARSE &&
  575. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  576. (fl->stripe_type == STRIPE_DENSE &&
  577. fl->num_fh != dsaddr->stripe_count)) {
  578. dprintk("%s num_fh %u not valid for given packing\n",
  579. __func__, fl->num_fh);
  580. goto out_put;
  581. }
  582. status = 0;
  583. out:
  584. dprintk("--> %s returns %d\n", __func__, status);
  585. return status;
  586. out_put:
  587. nfs4_fl_put_deviceid(dsaddr);
  588. goto out;
  589. }
  590. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  591. {
  592. int i;
  593. for (i = 0; i < fl->num_fh; i++) {
  594. if (!fl->fh_array[i])
  595. break;
  596. kfree(fl->fh_array[i]);
  597. }
  598. kfree(fl->fh_array);
  599. fl->fh_array = NULL;
  600. }
  601. static void
  602. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  603. {
  604. filelayout_free_fh_array(fl);
  605. kfree(fl);
  606. }
  607. static int
  608. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  609. struct nfs4_filelayout_segment *fl,
  610. struct nfs4_layoutget_res *lgr,
  611. struct nfs4_deviceid *id,
  612. gfp_t gfp_flags)
  613. {
  614. struct xdr_stream stream;
  615. struct xdr_buf buf;
  616. struct page *scratch;
  617. __be32 *p;
  618. uint32_t nfl_util;
  619. int i;
  620. dprintk("%s: set_layout_map Begin\n", __func__);
  621. scratch = alloc_page(gfp_flags);
  622. if (!scratch)
  623. return -ENOMEM;
  624. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
  625. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  626. /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
  627. * num_fh (4) */
  628. p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
  629. if (unlikely(!p))
  630. goto out_err;
  631. memcpy(id, p, sizeof(*id));
  632. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  633. nfs4_print_deviceid(id);
  634. nfl_util = be32_to_cpup(p++);
  635. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  636. fl->commit_through_mds = 1;
  637. if (nfl_util & NFL4_UFLG_DENSE)
  638. fl->stripe_type = STRIPE_DENSE;
  639. else
  640. fl->stripe_type = STRIPE_SPARSE;
  641. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  642. fl->first_stripe_index = be32_to_cpup(p++);
  643. p = xdr_decode_hyper(p, &fl->pattern_offset);
  644. fl->num_fh = be32_to_cpup(p++);
  645. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  646. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  647. fl->pattern_offset);
  648. /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
  649. * Futher checking is done in filelayout_check_layout */
  650. if (fl->num_fh >
  651. max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
  652. goto out_err;
  653. if (fl->num_fh > 0) {
  654. fl->fh_array = kcalloc(fl->num_fh, sizeof(fl->fh_array[0]),
  655. gfp_flags);
  656. if (!fl->fh_array)
  657. goto out_err;
  658. }
  659. for (i = 0; i < fl->num_fh; i++) {
  660. /* Do we want to use a mempool here? */
  661. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
  662. if (!fl->fh_array[i])
  663. goto out_err_free;
  664. p = xdr_inline_decode(&stream, 4);
  665. if (unlikely(!p))
  666. goto out_err_free;
  667. fl->fh_array[i]->size = be32_to_cpup(p++);
  668. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  669. printk(KERN_ERR "NFS: Too big fh %d received %d\n",
  670. i, fl->fh_array[i]->size);
  671. goto out_err_free;
  672. }
  673. p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
  674. if (unlikely(!p))
  675. goto out_err_free;
  676. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  677. dprintk("DEBUG: %s: fh len %d\n", __func__,
  678. fl->fh_array[i]->size);
  679. }
  680. __free_page(scratch);
  681. return 0;
  682. out_err_free:
  683. filelayout_free_fh_array(fl);
  684. out_err:
  685. __free_page(scratch);
  686. return -EIO;
  687. }
  688. static void
  689. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  690. {
  691. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  692. dprintk("--> %s\n", __func__);
  693. nfs4_fl_put_deviceid(fl->dsaddr);
  694. /* This assumes a single RW lseg */
  695. if (lseg->pls_range.iomode == IOMODE_RW) {
  696. struct nfs4_filelayout *flo;
  697. flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
  698. flo->commit_info.nbuckets = 0;
  699. kfree(flo->commit_info.buckets);
  700. flo->commit_info.buckets = NULL;
  701. }
  702. _filelayout_free_lseg(fl);
  703. }
  704. static int
  705. filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
  706. struct nfs_commit_info *cinfo,
  707. gfp_t gfp_flags)
  708. {
  709. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  710. struct pnfs_commit_bucket *buckets;
  711. int size, i;
  712. if (fl->commit_through_mds)
  713. return 0;
  714. size = (fl->stripe_type == STRIPE_SPARSE) ?
  715. fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
  716. if (cinfo->ds->nbuckets >= size) {
  717. /* This assumes there is only one IOMODE_RW lseg. What
  718. * we really want to do is have a layout_hdr level
  719. * dictionary of <multipath_list4, fh> keys, each
  720. * associated with a struct list_head, populated by calls
  721. * to filelayout_write_pagelist().
  722. * */
  723. return 0;
  724. }
  725. buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
  726. gfp_flags);
  727. if (!buckets)
  728. return -ENOMEM;
  729. for (i = 0; i < size; i++) {
  730. INIT_LIST_HEAD(&buckets[i].written);
  731. INIT_LIST_HEAD(&buckets[i].committing);
  732. /* mark direct verifier as unset */
  733. buckets[i].direct_verf.committed = NFS_INVALID_STABLE_HOW;
  734. }
  735. spin_lock(cinfo->lock);
  736. if (cinfo->ds->nbuckets >= size)
  737. goto out;
  738. for (i = 0; i < cinfo->ds->nbuckets; i++) {
  739. list_splice(&cinfo->ds->buckets[i].written,
  740. &buckets[i].written);
  741. list_splice(&cinfo->ds->buckets[i].committing,
  742. &buckets[i].committing);
  743. buckets[i].direct_verf.committed =
  744. cinfo->ds->buckets[i].direct_verf.committed;
  745. buckets[i].wlseg = cinfo->ds->buckets[i].wlseg;
  746. buckets[i].clseg = cinfo->ds->buckets[i].clseg;
  747. }
  748. swap(cinfo->ds->buckets, buckets);
  749. cinfo->ds->nbuckets = size;
  750. out:
  751. spin_unlock(cinfo->lock);
  752. kfree(buckets);
  753. return 0;
  754. }
  755. static struct pnfs_layout_segment *
  756. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  757. struct nfs4_layoutget_res *lgr,
  758. gfp_t gfp_flags)
  759. {
  760. struct nfs4_filelayout_segment *fl;
  761. int rc;
  762. struct nfs4_deviceid id;
  763. dprintk("--> %s\n", __func__);
  764. fl = kzalloc(sizeof(*fl), gfp_flags);
  765. if (!fl)
  766. return NULL;
  767. rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
  768. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
  769. _filelayout_free_lseg(fl);
  770. return NULL;
  771. }
  772. return &fl->generic_hdr;
  773. }
  774. /*
  775. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  776. *
  777. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  778. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  779. */
  780. static size_t
  781. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  782. struct nfs_page *req)
  783. {
  784. unsigned int size;
  785. u64 p_stripe, r_stripe;
  786. u32 stripe_offset;
  787. u64 segment_offset = pgio->pg_lseg->pls_range.offset;
  788. u32 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  789. /* calls nfs_generic_pg_test */
  790. size = pnfs_generic_pg_test(pgio, prev, req);
  791. if (!size)
  792. return 0;
  793. /* see if req and prev are in the same stripe */
  794. if (prev) {
  795. p_stripe = (u64)req_offset(prev) - segment_offset;
  796. r_stripe = (u64)req_offset(req) - segment_offset;
  797. do_div(p_stripe, stripe_unit);
  798. do_div(r_stripe, stripe_unit);
  799. if (p_stripe != r_stripe)
  800. return 0;
  801. }
  802. /* calculate remaining bytes in the current stripe */
  803. div_u64_rem((u64)req_offset(req) - segment_offset,
  804. stripe_unit,
  805. &stripe_offset);
  806. WARN_ON_ONCE(stripe_offset > stripe_unit);
  807. if (stripe_offset >= stripe_unit)
  808. return 0;
  809. return min(stripe_unit - (unsigned int)stripe_offset, size);
  810. }
  811. static void
  812. filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  813. struct nfs_page *req)
  814. {
  815. if (!pgio->pg_lseg)
  816. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  817. req->wb_context,
  818. 0,
  819. NFS4_MAX_UINT64,
  820. IOMODE_READ,
  821. GFP_KERNEL);
  822. /* If no lseg, fall back to read through mds */
  823. if (pgio->pg_lseg == NULL)
  824. nfs_pageio_reset_read_mds(pgio);
  825. }
  826. static void
  827. filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  828. struct nfs_page *req)
  829. {
  830. struct nfs_commit_info cinfo;
  831. int status;
  832. if (!pgio->pg_lseg)
  833. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  834. req->wb_context,
  835. 0,
  836. NFS4_MAX_UINT64,
  837. IOMODE_RW,
  838. GFP_NOFS);
  839. /* If no lseg, fall back to write through mds */
  840. if (pgio->pg_lseg == NULL)
  841. goto out_mds;
  842. nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
  843. status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
  844. if (status < 0) {
  845. pnfs_put_lseg(pgio->pg_lseg);
  846. pgio->pg_lseg = NULL;
  847. goto out_mds;
  848. }
  849. return;
  850. out_mds:
  851. nfs_pageio_reset_write_mds(pgio);
  852. }
  853. static const struct nfs_pageio_ops filelayout_pg_read_ops = {
  854. .pg_init = filelayout_pg_init_read,
  855. .pg_test = filelayout_pg_test,
  856. .pg_doio = pnfs_generic_pg_readpages,
  857. };
  858. static const struct nfs_pageio_ops filelayout_pg_write_ops = {
  859. .pg_init = filelayout_pg_init_write,
  860. .pg_test = filelayout_pg_test,
  861. .pg_doio = pnfs_generic_pg_writepages,
  862. };
  863. static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
  864. {
  865. if (fl->stripe_type == STRIPE_SPARSE)
  866. return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
  867. else
  868. return j;
  869. }
  870. /* The generic layer is about to remove the req from the commit list.
  871. * If this will make the bucket empty, it will need to put the lseg reference.
  872. * Note this is must be called holding the inode (/cinfo) lock
  873. */
  874. static void
  875. filelayout_clear_request_commit(struct nfs_page *req,
  876. struct nfs_commit_info *cinfo)
  877. {
  878. struct pnfs_layout_segment *freeme = NULL;
  879. if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
  880. goto out;
  881. cinfo->ds->nwritten--;
  882. if (list_is_singular(&req->wb_list)) {
  883. struct pnfs_commit_bucket *bucket;
  884. bucket = list_first_entry(&req->wb_list,
  885. struct pnfs_commit_bucket,
  886. written);
  887. freeme = bucket->wlseg;
  888. bucket->wlseg = NULL;
  889. }
  890. out:
  891. nfs_request_remove_commit_list(req, cinfo);
  892. pnfs_put_lseg_async(freeme);
  893. }
  894. static void
  895. filelayout_mark_request_commit(struct nfs_page *req,
  896. struct pnfs_layout_segment *lseg,
  897. struct nfs_commit_info *cinfo)
  898. {
  899. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  900. u32 i, j;
  901. struct list_head *list;
  902. struct pnfs_commit_bucket *buckets;
  903. if (fl->commit_through_mds) {
  904. list = &cinfo->mds->list;
  905. spin_lock(cinfo->lock);
  906. goto mds_commit;
  907. }
  908. /* Note that we are calling nfs4_fl_calc_j_index on each page
  909. * that ends up being committed to a data server. An attractive
  910. * alternative is to add a field to nfs_write_data and nfs_page
  911. * to store the value calculated in filelayout_write_pagelist
  912. * and just use that here.
  913. */
  914. j = nfs4_fl_calc_j_index(lseg, req_offset(req));
  915. i = select_bucket_index(fl, j);
  916. spin_lock(cinfo->lock);
  917. buckets = cinfo->ds->buckets;
  918. list = &buckets[i].written;
  919. if (list_empty(list)) {
  920. /* Non-empty buckets hold a reference on the lseg. That ref
  921. * is normally transferred to the COMMIT call and released
  922. * there. It could also be released if the last req is pulled
  923. * off due to a rewrite, in which case it will be done in
  924. * filelayout_clear_request_commit
  925. */
  926. buckets[i].wlseg = pnfs_get_lseg(lseg);
  927. }
  928. set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  929. cinfo->ds->nwritten++;
  930. mds_commit:
  931. /* nfs_request_add_commit_list(). We need to add req to list without
  932. * dropping cinfo lock.
  933. */
  934. set_bit(PG_CLEAN, &(req)->wb_flags);
  935. nfs_list_add_request(req, list);
  936. cinfo->mds->ncommit++;
  937. spin_unlock(cinfo->lock);
  938. if (!cinfo->dreq) {
  939. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  940. inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
  941. BDI_RECLAIMABLE);
  942. __mark_inode_dirty(req->wb_context->dentry->d_inode,
  943. I_DIRTY_DATASYNC);
  944. }
  945. }
  946. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  947. {
  948. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  949. if (flseg->stripe_type == STRIPE_SPARSE)
  950. return i;
  951. else
  952. return nfs4_fl_calc_ds_index(lseg, i);
  953. }
  954. static struct nfs_fh *
  955. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  956. {
  957. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  958. if (flseg->stripe_type == STRIPE_SPARSE) {
  959. if (flseg->num_fh == 1)
  960. i = 0;
  961. else if (flseg->num_fh == 0)
  962. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  963. return NULL;
  964. }
  965. return flseg->fh_array[i];
  966. }
  967. static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
  968. {
  969. struct pnfs_layout_segment *lseg = data->lseg;
  970. struct nfs4_pnfs_ds *ds;
  971. struct rpc_clnt *ds_clnt;
  972. u32 idx;
  973. struct nfs_fh *fh;
  974. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  975. ds = nfs4_fl_prepare_ds(lseg, idx);
  976. if (!ds)
  977. goto out_err;
  978. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, data->inode);
  979. if (IS_ERR(ds_clnt))
  980. goto out_err;
  981. dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
  982. data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
  983. data->commit_done_cb = filelayout_commit_done_cb;
  984. atomic_inc(&ds->ds_clp->cl_count);
  985. data->ds_clp = ds->ds_clp;
  986. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  987. if (fh)
  988. data->args.fh = fh;
  989. return nfs_initiate_commit(ds_clnt, data,
  990. &filelayout_commit_call_ops, how,
  991. RPC_TASK_SOFTCONN);
  992. out_err:
  993. prepare_to_resend_writes(data);
  994. filelayout_commit_release(data);
  995. return -EAGAIN;
  996. }
  997. static int
  998. transfer_commit_list(struct list_head *src, struct list_head *dst,
  999. struct nfs_commit_info *cinfo, int max)
  1000. {
  1001. struct nfs_page *req, *tmp;
  1002. int ret = 0;
  1003. list_for_each_entry_safe(req, tmp, src, wb_list) {
  1004. if (!nfs_lock_request(req))
  1005. continue;
  1006. kref_get(&req->wb_kref);
  1007. if (cond_resched_lock(cinfo->lock))
  1008. list_safe_reset_next(req, tmp, wb_list);
  1009. nfs_request_remove_commit_list(req, cinfo);
  1010. clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  1011. nfs_list_add_request(req, dst);
  1012. ret++;
  1013. if ((ret == max) && !cinfo->dreq)
  1014. break;
  1015. }
  1016. return ret;
  1017. }
  1018. /* Note called with cinfo->lock held. */
  1019. static int
  1020. filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
  1021. struct nfs_commit_info *cinfo,
  1022. int max)
  1023. {
  1024. struct list_head *src = &bucket->written;
  1025. struct list_head *dst = &bucket->committing;
  1026. int ret;
  1027. ret = transfer_commit_list(src, dst, cinfo, max);
  1028. if (ret) {
  1029. cinfo->ds->nwritten -= ret;
  1030. cinfo->ds->ncommitting += ret;
  1031. bucket->clseg = bucket->wlseg;
  1032. if (list_empty(src))
  1033. bucket->wlseg = NULL;
  1034. else
  1035. pnfs_get_lseg(bucket->clseg);
  1036. }
  1037. return ret;
  1038. }
  1039. /* Move reqs from written to committing lists, returning count of number moved.
  1040. * Note called with cinfo->lock held.
  1041. */
  1042. static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
  1043. int max)
  1044. {
  1045. int i, rv = 0, cnt;
  1046. for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
  1047. cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
  1048. cinfo, max);
  1049. max -= cnt;
  1050. rv += cnt;
  1051. }
  1052. return rv;
  1053. }
  1054. /* Pull everything off the committing lists and dump into @dst */
  1055. static void filelayout_recover_commit_reqs(struct list_head *dst,
  1056. struct nfs_commit_info *cinfo)
  1057. {
  1058. struct pnfs_commit_bucket *b;
  1059. struct pnfs_layout_segment *freeme;
  1060. int i;
  1061. restart:
  1062. spin_lock(cinfo->lock);
  1063. for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
  1064. if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
  1065. freeme = b->wlseg;
  1066. b->wlseg = NULL;
  1067. spin_unlock(cinfo->lock);
  1068. pnfs_put_lseg(freeme);
  1069. goto restart;
  1070. }
  1071. }
  1072. cinfo->ds->nwritten = 0;
  1073. spin_unlock(cinfo->lock);
  1074. }
  1075. /* filelayout_search_commit_reqs - Search lists in @cinfo for the head reqest
  1076. * for @page
  1077. * @cinfo - commit info for current inode
  1078. * @page - page to search for matching head request
  1079. *
  1080. * Returns a the head request if one is found, otherwise returns NULL.
  1081. */
  1082. static struct nfs_page *
  1083. filelayout_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page)
  1084. {
  1085. struct nfs_page *freq, *t;
  1086. struct pnfs_commit_bucket *b;
  1087. int i;
  1088. /* Linearly search the commit lists for each bucket until a matching
  1089. * request is found */
  1090. for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
  1091. list_for_each_entry_safe(freq, t, &b->written, wb_list) {
  1092. if (freq->wb_page == page)
  1093. return freq->wb_head;
  1094. }
  1095. list_for_each_entry_safe(freq, t, &b->committing, wb_list) {
  1096. if (freq->wb_page == page)
  1097. return freq->wb_head;
  1098. }
  1099. }
  1100. return NULL;
  1101. }
  1102. static void filelayout_retry_commit(struct nfs_commit_info *cinfo, int idx)
  1103. {
  1104. struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
  1105. struct pnfs_commit_bucket *bucket;
  1106. struct pnfs_layout_segment *freeme;
  1107. int i;
  1108. for (i = idx; i < fl_cinfo->nbuckets; i++) {
  1109. bucket = &fl_cinfo->buckets[i];
  1110. if (list_empty(&bucket->committing))
  1111. continue;
  1112. nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
  1113. spin_lock(cinfo->lock);
  1114. freeme = bucket->clseg;
  1115. bucket->clseg = NULL;
  1116. spin_unlock(cinfo->lock);
  1117. pnfs_put_lseg(freeme);
  1118. }
  1119. }
  1120. static unsigned int
  1121. alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
  1122. {
  1123. struct pnfs_ds_commit_info *fl_cinfo;
  1124. struct pnfs_commit_bucket *bucket;
  1125. struct nfs_commit_data *data;
  1126. int i;
  1127. unsigned int nreq = 0;
  1128. fl_cinfo = cinfo->ds;
  1129. bucket = fl_cinfo->buckets;
  1130. for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
  1131. if (list_empty(&bucket->committing))
  1132. continue;
  1133. data = nfs_commitdata_alloc();
  1134. if (!data)
  1135. break;
  1136. data->ds_commit_index = i;
  1137. spin_lock(cinfo->lock);
  1138. data->lseg = bucket->clseg;
  1139. bucket->clseg = NULL;
  1140. spin_unlock(cinfo->lock);
  1141. list_add(&data->pages, list);
  1142. nreq++;
  1143. }
  1144. /* Clean up on error */
  1145. filelayout_retry_commit(cinfo, i);
  1146. /* Caller will clean up entries put on list */
  1147. return nreq;
  1148. }
  1149. /* This follows nfs_commit_list pretty closely */
  1150. static int
  1151. filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  1152. int how, struct nfs_commit_info *cinfo)
  1153. {
  1154. struct nfs_commit_data *data, *tmp;
  1155. LIST_HEAD(list);
  1156. unsigned int nreq = 0;
  1157. if (!list_empty(mds_pages)) {
  1158. data = nfs_commitdata_alloc();
  1159. if (data != NULL) {
  1160. data->lseg = NULL;
  1161. list_add(&data->pages, &list);
  1162. nreq++;
  1163. } else {
  1164. nfs_retry_commit(mds_pages, NULL, cinfo);
  1165. filelayout_retry_commit(cinfo, 0);
  1166. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  1167. return -ENOMEM;
  1168. }
  1169. }
  1170. nreq += alloc_ds_commits(cinfo, &list);
  1171. if (nreq == 0) {
  1172. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  1173. goto out;
  1174. }
  1175. atomic_add(nreq, &cinfo->mds->rpcs_out);
  1176. list_for_each_entry_safe(data, tmp, &list, pages) {
  1177. list_del_init(&data->pages);
  1178. if (!data->lseg) {
  1179. nfs_init_commit(data, mds_pages, NULL, cinfo);
  1180. nfs_initiate_commit(NFS_CLIENT(inode), data,
  1181. data->mds_ops, how, 0);
  1182. } else {
  1183. struct pnfs_commit_bucket *buckets;
  1184. buckets = cinfo->ds->buckets;
  1185. nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
  1186. filelayout_initiate_commit(data, how);
  1187. }
  1188. }
  1189. out:
  1190. cinfo->ds->ncommitting = 0;
  1191. return PNFS_ATTEMPTED;
  1192. }
  1193. static void
  1194. filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
  1195. {
  1196. nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
  1197. }
  1198. static struct pnfs_layout_hdr *
  1199. filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
  1200. {
  1201. struct nfs4_filelayout *flo;
  1202. flo = kzalloc(sizeof(*flo), gfp_flags);
  1203. return flo != NULL ? &flo->generic_hdr : NULL;
  1204. }
  1205. static void
  1206. filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
  1207. {
  1208. kfree(FILELAYOUT_FROM_HDR(lo));
  1209. }
  1210. static struct pnfs_ds_commit_info *
  1211. filelayout_get_ds_info(struct inode *inode)
  1212. {
  1213. struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
  1214. if (layout == NULL)
  1215. return NULL;
  1216. else
  1217. return &FILELAYOUT_FROM_HDR(layout)->commit_info;
  1218. }
  1219. static struct pnfs_layoutdriver_type filelayout_type = {
  1220. .id = LAYOUT_NFSV4_1_FILES,
  1221. .name = "LAYOUT_NFSV4_1_FILES",
  1222. .owner = THIS_MODULE,
  1223. .alloc_layout_hdr = filelayout_alloc_layout_hdr,
  1224. .free_layout_hdr = filelayout_free_layout_hdr,
  1225. .alloc_lseg = filelayout_alloc_lseg,
  1226. .free_lseg = filelayout_free_lseg,
  1227. .pg_read_ops = &filelayout_pg_read_ops,
  1228. .pg_write_ops = &filelayout_pg_write_ops,
  1229. .get_ds_info = &filelayout_get_ds_info,
  1230. .mark_request_commit = filelayout_mark_request_commit,
  1231. .clear_request_commit = filelayout_clear_request_commit,
  1232. .scan_commit_lists = filelayout_scan_commit_lists,
  1233. .recover_commit_reqs = filelayout_recover_commit_reqs,
  1234. .search_commit_reqs = filelayout_search_commit_reqs,
  1235. .commit_pagelist = filelayout_commit_pagelist,
  1236. .read_pagelist = filelayout_read_pagelist,
  1237. .write_pagelist = filelayout_write_pagelist,
  1238. .free_deviceid_node = filelayout_free_deveiceid_node,
  1239. };
  1240. static int __init nfs4filelayout_init(void)
  1241. {
  1242. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  1243. __func__);
  1244. return pnfs_register_layoutdriver(&filelayout_type);
  1245. }
  1246. static void __exit nfs4filelayout_exit(void)
  1247. {
  1248. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  1249. __func__);
  1250. pnfs_unregister_layoutdriver(&filelayout_type);
  1251. }
  1252. MODULE_ALIAS("nfs-layouttype4-1");
  1253. module_init(nfs4filelayout_init);
  1254. module_exit(nfs4filelayout_exit);