filelayout.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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), id,
  557. lo->plh_lc_cred, gfp_flags);
  558. if (d == NULL)
  559. goto out;
  560. dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  561. /* Found deviceid is unavailable */
  562. if (filelayout_test_devid_unavailable(&dsaddr->id_node))
  563. goto out_put;
  564. fl->dsaddr = dsaddr;
  565. if (fl->first_stripe_index >= dsaddr->stripe_count) {
  566. dprintk("%s Bad first_stripe_index %u\n",
  567. __func__, fl->first_stripe_index);
  568. goto out_put;
  569. }
  570. if ((fl->stripe_type == STRIPE_SPARSE &&
  571. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  572. (fl->stripe_type == STRIPE_DENSE &&
  573. fl->num_fh != dsaddr->stripe_count)) {
  574. dprintk("%s num_fh %u not valid for given packing\n",
  575. __func__, fl->num_fh);
  576. goto out_put;
  577. }
  578. status = 0;
  579. out:
  580. dprintk("--> %s returns %d\n", __func__, status);
  581. return status;
  582. out_put:
  583. nfs4_fl_put_deviceid(dsaddr);
  584. goto out;
  585. }
  586. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  587. {
  588. int i;
  589. for (i = 0; i < fl->num_fh; i++) {
  590. if (!fl->fh_array[i])
  591. break;
  592. kfree(fl->fh_array[i]);
  593. }
  594. kfree(fl->fh_array);
  595. fl->fh_array = NULL;
  596. }
  597. static void
  598. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  599. {
  600. filelayout_free_fh_array(fl);
  601. kfree(fl);
  602. }
  603. static int
  604. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  605. struct nfs4_filelayout_segment *fl,
  606. struct nfs4_layoutget_res *lgr,
  607. struct nfs4_deviceid *id,
  608. gfp_t gfp_flags)
  609. {
  610. struct xdr_stream stream;
  611. struct xdr_buf buf;
  612. struct page *scratch;
  613. __be32 *p;
  614. uint32_t nfl_util;
  615. int i;
  616. dprintk("%s: set_layout_map Begin\n", __func__);
  617. scratch = alloc_page(gfp_flags);
  618. if (!scratch)
  619. return -ENOMEM;
  620. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
  621. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  622. /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
  623. * num_fh (4) */
  624. p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
  625. if (unlikely(!p))
  626. goto out_err;
  627. memcpy(id, p, sizeof(*id));
  628. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  629. nfs4_print_deviceid(id);
  630. nfl_util = be32_to_cpup(p++);
  631. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  632. fl->commit_through_mds = 1;
  633. if (nfl_util & NFL4_UFLG_DENSE)
  634. fl->stripe_type = STRIPE_DENSE;
  635. else
  636. fl->stripe_type = STRIPE_SPARSE;
  637. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  638. fl->first_stripe_index = be32_to_cpup(p++);
  639. p = xdr_decode_hyper(p, &fl->pattern_offset);
  640. fl->num_fh = be32_to_cpup(p++);
  641. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  642. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  643. fl->pattern_offset);
  644. /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
  645. * Futher checking is done in filelayout_check_layout */
  646. if (fl->num_fh >
  647. max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
  648. goto out_err;
  649. if (fl->num_fh > 0) {
  650. fl->fh_array = kcalloc(fl->num_fh, sizeof(fl->fh_array[0]),
  651. gfp_flags);
  652. if (!fl->fh_array)
  653. goto out_err;
  654. }
  655. for (i = 0; i < fl->num_fh; i++) {
  656. /* Do we want to use a mempool here? */
  657. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
  658. if (!fl->fh_array[i])
  659. goto out_err_free;
  660. p = xdr_inline_decode(&stream, 4);
  661. if (unlikely(!p))
  662. goto out_err_free;
  663. fl->fh_array[i]->size = be32_to_cpup(p++);
  664. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  665. printk(KERN_ERR "NFS: Too big fh %d received %d\n",
  666. i, fl->fh_array[i]->size);
  667. goto out_err_free;
  668. }
  669. p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
  670. if (unlikely(!p))
  671. goto out_err_free;
  672. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  673. dprintk("DEBUG: %s: fh len %d\n", __func__,
  674. fl->fh_array[i]->size);
  675. }
  676. __free_page(scratch);
  677. return 0;
  678. out_err_free:
  679. filelayout_free_fh_array(fl);
  680. out_err:
  681. __free_page(scratch);
  682. return -EIO;
  683. }
  684. static void
  685. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  686. {
  687. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  688. dprintk("--> %s\n", __func__);
  689. nfs4_fl_put_deviceid(fl->dsaddr);
  690. /* This assumes a single RW lseg */
  691. if (lseg->pls_range.iomode == IOMODE_RW) {
  692. struct nfs4_filelayout *flo;
  693. flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
  694. flo->commit_info.nbuckets = 0;
  695. kfree(flo->commit_info.buckets);
  696. flo->commit_info.buckets = NULL;
  697. }
  698. _filelayout_free_lseg(fl);
  699. }
  700. static int
  701. filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
  702. struct nfs_commit_info *cinfo,
  703. gfp_t gfp_flags)
  704. {
  705. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  706. struct pnfs_commit_bucket *buckets;
  707. int size, i;
  708. if (fl->commit_through_mds)
  709. return 0;
  710. size = (fl->stripe_type == STRIPE_SPARSE) ?
  711. fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
  712. if (cinfo->ds->nbuckets >= size) {
  713. /* This assumes there is only one IOMODE_RW lseg. What
  714. * we really want to do is have a layout_hdr level
  715. * dictionary of <multipath_list4, fh> keys, each
  716. * associated with a struct list_head, populated by calls
  717. * to filelayout_write_pagelist().
  718. * */
  719. return 0;
  720. }
  721. buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
  722. gfp_flags);
  723. if (!buckets)
  724. return -ENOMEM;
  725. for (i = 0; i < size; i++) {
  726. INIT_LIST_HEAD(&buckets[i].written);
  727. INIT_LIST_HEAD(&buckets[i].committing);
  728. /* mark direct verifier as unset */
  729. buckets[i].direct_verf.committed = NFS_INVALID_STABLE_HOW;
  730. }
  731. spin_lock(cinfo->lock);
  732. if (cinfo->ds->nbuckets >= size)
  733. goto out;
  734. for (i = 0; i < cinfo->ds->nbuckets; i++) {
  735. list_splice(&cinfo->ds->buckets[i].written,
  736. &buckets[i].written);
  737. list_splice(&cinfo->ds->buckets[i].committing,
  738. &buckets[i].committing);
  739. buckets[i].direct_verf.committed =
  740. cinfo->ds->buckets[i].direct_verf.committed;
  741. buckets[i].wlseg = cinfo->ds->buckets[i].wlseg;
  742. buckets[i].clseg = cinfo->ds->buckets[i].clseg;
  743. }
  744. swap(cinfo->ds->buckets, buckets);
  745. cinfo->ds->nbuckets = size;
  746. out:
  747. spin_unlock(cinfo->lock);
  748. kfree(buckets);
  749. return 0;
  750. }
  751. static struct pnfs_layout_segment *
  752. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  753. struct nfs4_layoutget_res *lgr,
  754. gfp_t gfp_flags)
  755. {
  756. struct nfs4_filelayout_segment *fl;
  757. int rc;
  758. struct nfs4_deviceid id;
  759. dprintk("--> %s\n", __func__);
  760. fl = kzalloc(sizeof(*fl), gfp_flags);
  761. if (!fl)
  762. return NULL;
  763. rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
  764. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
  765. _filelayout_free_lseg(fl);
  766. return NULL;
  767. }
  768. return &fl->generic_hdr;
  769. }
  770. /*
  771. * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
  772. *
  773. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  774. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  775. */
  776. static size_t
  777. filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  778. struct nfs_page *req)
  779. {
  780. unsigned int size;
  781. u64 p_stripe, r_stripe;
  782. u32 stripe_offset;
  783. u64 segment_offset = pgio->pg_lseg->pls_range.offset;
  784. u32 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
  785. /* calls nfs_generic_pg_test */
  786. size = pnfs_generic_pg_test(pgio, prev, req);
  787. if (!size)
  788. return 0;
  789. /* see if req and prev are in the same stripe */
  790. if (prev) {
  791. p_stripe = (u64)req_offset(prev) - segment_offset;
  792. r_stripe = (u64)req_offset(req) - segment_offset;
  793. do_div(p_stripe, stripe_unit);
  794. do_div(r_stripe, stripe_unit);
  795. if (p_stripe != r_stripe)
  796. return 0;
  797. }
  798. /* calculate remaining bytes in the current stripe */
  799. div_u64_rem((u64)req_offset(req) - segment_offset,
  800. stripe_unit,
  801. &stripe_offset);
  802. WARN_ON_ONCE(stripe_offset > stripe_unit);
  803. if (stripe_offset >= stripe_unit)
  804. return 0;
  805. return min(stripe_unit - (unsigned int)stripe_offset, size);
  806. }
  807. static void
  808. filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  809. struct nfs_page *req)
  810. {
  811. if (!pgio->pg_lseg)
  812. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  813. req->wb_context,
  814. 0,
  815. NFS4_MAX_UINT64,
  816. IOMODE_READ,
  817. GFP_KERNEL);
  818. /* If no lseg, fall back to read through mds */
  819. if (pgio->pg_lseg == NULL)
  820. nfs_pageio_reset_read_mds(pgio);
  821. }
  822. static void
  823. filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  824. struct nfs_page *req)
  825. {
  826. struct nfs_commit_info cinfo;
  827. int status;
  828. if (!pgio->pg_lseg)
  829. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  830. req->wb_context,
  831. 0,
  832. NFS4_MAX_UINT64,
  833. IOMODE_RW,
  834. GFP_NOFS);
  835. /* If no lseg, fall back to write through mds */
  836. if (pgio->pg_lseg == NULL)
  837. goto out_mds;
  838. nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
  839. status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
  840. if (status < 0) {
  841. pnfs_put_lseg(pgio->pg_lseg);
  842. pgio->pg_lseg = NULL;
  843. goto out_mds;
  844. }
  845. return;
  846. out_mds:
  847. nfs_pageio_reset_write_mds(pgio);
  848. }
  849. static const struct nfs_pageio_ops filelayout_pg_read_ops = {
  850. .pg_init = filelayout_pg_init_read,
  851. .pg_test = filelayout_pg_test,
  852. .pg_doio = pnfs_generic_pg_readpages,
  853. };
  854. static const struct nfs_pageio_ops filelayout_pg_write_ops = {
  855. .pg_init = filelayout_pg_init_write,
  856. .pg_test = filelayout_pg_test,
  857. .pg_doio = pnfs_generic_pg_writepages,
  858. };
  859. static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
  860. {
  861. if (fl->stripe_type == STRIPE_SPARSE)
  862. return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
  863. else
  864. return j;
  865. }
  866. /* The generic layer is about to remove the req from the commit list.
  867. * If this will make the bucket empty, it will need to put the lseg reference.
  868. * Note this is must be called holding the inode (/cinfo) lock
  869. */
  870. static void
  871. filelayout_clear_request_commit(struct nfs_page *req,
  872. struct nfs_commit_info *cinfo)
  873. {
  874. struct pnfs_layout_segment *freeme = NULL;
  875. if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
  876. goto out;
  877. cinfo->ds->nwritten--;
  878. if (list_is_singular(&req->wb_list)) {
  879. struct pnfs_commit_bucket *bucket;
  880. bucket = list_first_entry(&req->wb_list,
  881. struct pnfs_commit_bucket,
  882. written);
  883. freeme = bucket->wlseg;
  884. bucket->wlseg = NULL;
  885. }
  886. out:
  887. nfs_request_remove_commit_list(req, cinfo);
  888. pnfs_put_lseg_locked(freeme);
  889. }
  890. static void
  891. filelayout_mark_request_commit(struct nfs_page *req,
  892. struct pnfs_layout_segment *lseg,
  893. struct nfs_commit_info *cinfo)
  894. {
  895. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  896. u32 i, j;
  897. struct list_head *list;
  898. struct pnfs_commit_bucket *buckets;
  899. if (fl->commit_through_mds) {
  900. list = &cinfo->mds->list;
  901. spin_lock(cinfo->lock);
  902. goto mds_commit;
  903. }
  904. /* Note that we are calling nfs4_fl_calc_j_index on each page
  905. * that ends up being committed to a data server. An attractive
  906. * alternative is to add a field to nfs_write_data and nfs_page
  907. * to store the value calculated in filelayout_write_pagelist
  908. * and just use that here.
  909. */
  910. j = nfs4_fl_calc_j_index(lseg, req_offset(req));
  911. i = select_bucket_index(fl, j);
  912. spin_lock(cinfo->lock);
  913. buckets = cinfo->ds->buckets;
  914. list = &buckets[i].written;
  915. if (list_empty(list)) {
  916. /* Non-empty buckets hold a reference on the lseg. That ref
  917. * is normally transferred to the COMMIT call and released
  918. * there. It could also be released if the last req is pulled
  919. * off due to a rewrite, in which case it will be done in
  920. * filelayout_clear_request_commit
  921. */
  922. buckets[i].wlseg = pnfs_get_lseg(lseg);
  923. }
  924. set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  925. cinfo->ds->nwritten++;
  926. mds_commit:
  927. /* nfs_request_add_commit_list(). We need to add req to list without
  928. * dropping cinfo lock.
  929. */
  930. set_bit(PG_CLEAN, &(req)->wb_flags);
  931. nfs_list_add_request(req, list);
  932. cinfo->mds->ncommit++;
  933. spin_unlock(cinfo->lock);
  934. if (!cinfo->dreq) {
  935. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  936. inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
  937. BDI_RECLAIMABLE);
  938. __mark_inode_dirty(req->wb_context->dentry->d_inode,
  939. I_DIRTY_DATASYNC);
  940. }
  941. }
  942. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  943. {
  944. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  945. if (flseg->stripe_type == STRIPE_SPARSE)
  946. return i;
  947. else
  948. return nfs4_fl_calc_ds_index(lseg, i);
  949. }
  950. static struct nfs_fh *
  951. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  952. {
  953. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  954. if (flseg->stripe_type == STRIPE_SPARSE) {
  955. if (flseg->num_fh == 1)
  956. i = 0;
  957. else if (flseg->num_fh == 0)
  958. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  959. return NULL;
  960. }
  961. return flseg->fh_array[i];
  962. }
  963. static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
  964. {
  965. struct pnfs_layout_segment *lseg = data->lseg;
  966. struct nfs4_pnfs_ds *ds;
  967. struct rpc_clnt *ds_clnt;
  968. u32 idx;
  969. struct nfs_fh *fh;
  970. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  971. ds = nfs4_fl_prepare_ds(lseg, idx);
  972. if (!ds)
  973. goto out_err;
  974. ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, data->inode);
  975. if (IS_ERR(ds_clnt))
  976. goto out_err;
  977. dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
  978. data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
  979. data->commit_done_cb = filelayout_commit_done_cb;
  980. atomic_inc(&ds->ds_clp->cl_count);
  981. data->ds_clp = ds->ds_clp;
  982. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  983. if (fh)
  984. data->args.fh = fh;
  985. return nfs_initiate_commit(ds_clnt, data,
  986. &filelayout_commit_call_ops, how,
  987. RPC_TASK_SOFTCONN);
  988. out_err:
  989. prepare_to_resend_writes(data);
  990. filelayout_commit_release(data);
  991. return -EAGAIN;
  992. }
  993. static int
  994. transfer_commit_list(struct list_head *src, struct list_head *dst,
  995. struct nfs_commit_info *cinfo, int max)
  996. {
  997. struct nfs_page *req, *tmp;
  998. int ret = 0;
  999. list_for_each_entry_safe(req, tmp, src, wb_list) {
  1000. if (!nfs_lock_request(req))
  1001. continue;
  1002. kref_get(&req->wb_kref);
  1003. if (cond_resched_lock(cinfo->lock))
  1004. list_safe_reset_next(req, tmp, wb_list);
  1005. nfs_request_remove_commit_list(req, cinfo);
  1006. clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  1007. nfs_list_add_request(req, dst);
  1008. ret++;
  1009. if ((ret == max) && !cinfo->dreq)
  1010. break;
  1011. }
  1012. return ret;
  1013. }
  1014. /* Note called with cinfo->lock held. */
  1015. static int
  1016. filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
  1017. struct nfs_commit_info *cinfo,
  1018. int max)
  1019. {
  1020. struct list_head *src = &bucket->written;
  1021. struct list_head *dst = &bucket->committing;
  1022. int ret;
  1023. ret = transfer_commit_list(src, dst, cinfo, max);
  1024. if (ret) {
  1025. cinfo->ds->nwritten -= ret;
  1026. cinfo->ds->ncommitting += ret;
  1027. bucket->clseg = bucket->wlseg;
  1028. if (list_empty(src))
  1029. bucket->wlseg = NULL;
  1030. else
  1031. pnfs_get_lseg(bucket->clseg);
  1032. }
  1033. return ret;
  1034. }
  1035. /* Move reqs from written to committing lists, returning count of number moved.
  1036. * Note called with cinfo->lock held.
  1037. */
  1038. static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
  1039. int max)
  1040. {
  1041. int i, rv = 0, cnt;
  1042. for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
  1043. cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
  1044. cinfo, max);
  1045. max -= cnt;
  1046. rv += cnt;
  1047. }
  1048. return rv;
  1049. }
  1050. /* Pull everything off the committing lists and dump into @dst */
  1051. static void filelayout_recover_commit_reqs(struct list_head *dst,
  1052. struct nfs_commit_info *cinfo)
  1053. {
  1054. struct pnfs_commit_bucket *b;
  1055. struct pnfs_layout_segment *freeme;
  1056. int i;
  1057. restart:
  1058. spin_lock(cinfo->lock);
  1059. for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
  1060. if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
  1061. freeme = b->wlseg;
  1062. b->wlseg = NULL;
  1063. spin_unlock(cinfo->lock);
  1064. pnfs_put_lseg(freeme);
  1065. goto restart;
  1066. }
  1067. }
  1068. cinfo->ds->nwritten = 0;
  1069. spin_unlock(cinfo->lock);
  1070. }
  1071. /* filelayout_search_commit_reqs - Search lists in @cinfo for the head reqest
  1072. * for @page
  1073. * @cinfo - commit info for current inode
  1074. * @page - page to search for matching head request
  1075. *
  1076. * Returns a the head request if one is found, otherwise returns NULL.
  1077. */
  1078. static struct nfs_page *
  1079. filelayout_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page)
  1080. {
  1081. struct nfs_page *freq, *t;
  1082. struct pnfs_commit_bucket *b;
  1083. int i;
  1084. /* Linearly search the commit lists for each bucket until a matching
  1085. * request is found */
  1086. for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
  1087. list_for_each_entry_safe(freq, t, &b->written, wb_list) {
  1088. if (freq->wb_page == page)
  1089. return freq->wb_head;
  1090. }
  1091. list_for_each_entry_safe(freq, t, &b->committing, wb_list) {
  1092. if (freq->wb_page == page)
  1093. return freq->wb_head;
  1094. }
  1095. }
  1096. return NULL;
  1097. }
  1098. static void filelayout_retry_commit(struct nfs_commit_info *cinfo, int idx)
  1099. {
  1100. struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
  1101. struct pnfs_commit_bucket *bucket;
  1102. struct pnfs_layout_segment *freeme;
  1103. int i;
  1104. for (i = idx; i < fl_cinfo->nbuckets; i++) {
  1105. bucket = &fl_cinfo->buckets[i];
  1106. if (list_empty(&bucket->committing))
  1107. continue;
  1108. nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
  1109. spin_lock(cinfo->lock);
  1110. freeme = bucket->clseg;
  1111. bucket->clseg = NULL;
  1112. spin_unlock(cinfo->lock);
  1113. pnfs_put_lseg(freeme);
  1114. }
  1115. }
  1116. static unsigned int
  1117. alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
  1118. {
  1119. struct pnfs_ds_commit_info *fl_cinfo;
  1120. struct pnfs_commit_bucket *bucket;
  1121. struct nfs_commit_data *data;
  1122. int i;
  1123. unsigned int nreq = 0;
  1124. fl_cinfo = cinfo->ds;
  1125. bucket = fl_cinfo->buckets;
  1126. for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
  1127. if (list_empty(&bucket->committing))
  1128. continue;
  1129. data = nfs_commitdata_alloc();
  1130. if (!data)
  1131. break;
  1132. data->ds_commit_index = i;
  1133. spin_lock(cinfo->lock);
  1134. data->lseg = bucket->clseg;
  1135. bucket->clseg = NULL;
  1136. spin_unlock(cinfo->lock);
  1137. list_add(&data->pages, list);
  1138. nreq++;
  1139. }
  1140. /* Clean up on error */
  1141. filelayout_retry_commit(cinfo, i);
  1142. /* Caller will clean up entries put on list */
  1143. return nreq;
  1144. }
  1145. /* This follows nfs_commit_list pretty closely */
  1146. static int
  1147. filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  1148. int how, struct nfs_commit_info *cinfo)
  1149. {
  1150. struct nfs_commit_data *data, *tmp;
  1151. LIST_HEAD(list);
  1152. unsigned int nreq = 0;
  1153. if (!list_empty(mds_pages)) {
  1154. data = nfs_commitdata_alloc();
  1155. if (data != NULL) {
  1156. data->lseg = NULL;
  1157. list_add(&data->pages, &list);
  1158. nreq++;
  1159. } else {
  1160. nfs_retry_commit(mds_pages, NULL, cinfo);
  1161. filelayout_retry_commit(cinfo, 0);
  1162. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  1163. return -ENOMEM;
  1164. }
  1165. }
  1166. nreq += alloc_ds_commits(cinfo, &list);
  1167. if (nreq == 0) {
  1168. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  1169. goto out;
  1170. }
  1171. atomic_add(nreq, &cinfo->mds->rpcs_out);
  1172. list_for_each_entry_safe(data, tmp, &list, pages) {
  1173. list_del_init(&data->pages);
  1174. if (!data->lseg) {
  1175. nfs_init_commit(data, mds_pages, NULL, cinfo);
  1176. nfs_initiate_commit(NFS_CLIENT(inode), data,
  1177. data->mds_ops, how, 0);
  1178. } else {
  1179. struct pnfs_commit_bucket *buckets;
  1180. buckets = cinfo->ds->buckets;
  1181. nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
  1182. filelayout_initiate_commit(data, how);
  1183. }
  1184. }
  1185. out:
  1186. cinfo->ds->ncommitting = 0;
  1187. return PNFS_ATTEMPTED;
  1188. }
  1189. static struct nfs4_deviceid_node *
  1190. filelayout_alloc_deviceid_node(struct nfs_server *server,
  1191. struct pnfs_device *pdev, gfp_t gfp_flags)
  1192. {
  1193. struct nfs4_file_layout_dsaddr *dsaddr;
  1194. dsaddr = nfs4_fl_alloc_deviceid_node(server, pdev, gfp_flags);
  1195. if (!dsaddr)
  1196. return NULL;
  1197. return &dsaddr->id_node;
  1198. }
  1199. static void
  1200. filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
  1201. {
  1202. nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
  1203. }
  1204. static struct pnfs_layout_hdr *
  1205. filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
  1206. {
  1207. struct nfs4_filelayout *flo;
  1208. flo = kzalloc(sizeof(*flo), gfp_flags);
  1209. return flo != NULL ? &flo->generic_hdr : NULL;
  1210. }
  1211. static void
  1212. filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
  1213. {
  1214. kfree(FILELAYOUT_FROM_HDR(lo));
  1215. }
  1216. static struct pnfs_ds_commit_info *
  1217. filelayout_get_ds_info(struct inode *inode)
  1218. {
  1219. struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
  1220. if (layout == NULL)
  1221. return NULL;
  1222. else
  1223. return &FILELAYOUT_FROM_HDR(layout)->commit_info;
  1224. }
  1225. static struct pnfs_layoutdriver_type filelayout_type = {
  1226. .id = LAYOUT_NFSV4_1_FILES,
  1227. .name = "LAYOUT_NFSV4_1_FILES",
  1228. .owner = THIS_MODULE,
  1229. .alloc_layout_hdr = filelayout_alloc_layout_hdr,
  1230. .free_layout_hdr = filelayout_free_layout_hdr,
  1231. .alloc_lseg = filelayout_alloc_lseg,
  1232. .free_lseg = filelayout_free_lseg,
  1233. .pg_read_ops = &filelayout_pg_read_ops,
  1234. .pg_write_ops = &filelayout_pg_write_ops,
  1235. .get_ds_info = &filelayout_get_ds_info,
  1236. .mark_request_commit = filelayout_mark_request_commit,
  1237. .clear_request_commit = filelayout_clear_request_commit,
  1238. .scan_commit_lists = filelayout_scan_commit_lists,
  1239. .recover_commit_reqs = filelayout_recover_commit_reqs,
  1240. .search_commit_reqs = filelayout_search_commit_reqs,
  1241. .commit_pagelist = filelayout_commit_pagelist,
  1242. .read_pagelist = filelayout_read_pagelist,
  1243. .write_pagelist = filelayout_write_pagelist,
  1244. .alloc_deviceid_node = filelayout_alloc_deviceid_node,
  1245. .free_deviceid_node = filelayout_free_deveiceid_node,
  1246. };
  1247. static int __init nfs4filelayout_init(void)
  1248. {
  1249. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  1250. __func__);
  1251. return pnfs_register_layoutdriver(&filelayout_type);
  1252. }
  1253. static void __exit nfs4filelayout_exit(void)
  1254. {
  1255. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  1256. __func__);
  1257. pnfs_unregister_layoutdriver(&filelayout_type);
  1258. }
  1259. MODULE_ALIAS("nfs-layouttype4-1");
  1260. module_init(nfs4filelayout_init);
  1261. module_exit(nfs4filelayout_exit);