nfs4filelayout.c 36 KB

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