filelayout.c 39 KB

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