nfs4filelayout.c 36 KB

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