nfs4filelayout.c 35 KB

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