flexfilelayout.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172
  1. /*
  2. * Module for pnfs flexfile layout driver.
  3. *
  4. * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
  5. *
  6. * Tao Peng <bergwolf@primarydata.com>
  7. */
  8. #include <linux/nfs_fs.h>
  9. #include <linux/nfs_page.h>
  10. #include <linux/module.h>
  11. #include <linux/sunrpc/metrics.h>
  12. #include "flexfilelayout.h"
  13. #include "../nfs4session.h"
  14. #include "../nfs4idmap.h"
  15. #include "../internal.h"
  16. #include "../delegation.h"
  17. #include "../nfs4trace.h"
  18. #include "../iostat.h"
  19. #include "../nfs.h"
  20. #include "../nfs42.h"
  21. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  22. #define FF_LAYOUT_POLL_RETRY_MAX (15*HZ)
  23. static struct pnfs_layout_hdr *
  24. ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
  25. {
  26. struct nfs4_flexfile_layout *ffl;
  27. ffl = kzalloc(sizeof(*ffl), gfp_flags);
  28. if (ffl) {
  29. INIT_LIST_HEAD(&ffl->error_list);
  30. INIT_LIST_HEAD(&ffl->mirrors);
  31. return &ffl->generic_hdr;
  32. } else
  33. return NULL;
  34. }
  35. static void
  36. ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo)
  37. {
  38. struct nfs4_ff_layout_ds_err *err, *n;
  39. list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list,
  40. list) {
  41. list_del(&err->list);
  42. kfree(err);
  43. }
  44. kfree(FF_LAYOUT_FROM_HDR(lo));
  45. }
  46. static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
  47. {
  48. __be32 *p;
  49. p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
  50. if (unlikely(p == NULL))
  51. return -ENOBUFS;
  52. memcpy(stateid, p, NFS4_STATEID_SIZE);
  53. dprintk("%s: stateid id= [%x%x%x%x]\n", __func__,
  54. p[0], p[1], p[2], p[3]);
  55. return 0;
  56. }
  57. static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid)
  58. {
  59. __be32 *p;
  60. p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE);
  61. if (unlikely(!p))
  62. return -ENOBUFS;
  63. memcpy(devid, p, NFS4_DEVICEID4_SIZE);
  64. nfs4_print_deviceid(devid);
  65. return 0;
  66. }
  67. static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
  68. {
  69. __be32 *p;
  70. p = xdr_inline_decode(xdr, 4);
  71. if (unlikely(!p))
  72. return -ENOBUFS;
  73. fh->size = be32_to_cpup(p++);
  74. if (fh->size > sizeof(struct nfs_fh)) {
  75. printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n",
  76. fh->size);
  77. return -EOVERFLOW;
  78. }
  79. /* fh.data */
  80. p = xdr_inline_decode(xdr, fh->size);
  81. if (unlikely(!p))
  82. return -ENOBUFS;
  83. memcpy(&fh->data, p, fh->size);
  84. dprintk("%s: fh len %d\n", __func__, fh->size);
  85. return 0;
  86. }
  87. /*
  88. * Currently only stringified uids and gids are accepted.
  89. * I.e., kerberos is not supported to the DSes, so no pricipals.
  90. *
  91. * That means that one common function will suffice, but when
  92. * principals are added, this should be split to accomodate
  93. * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid().
  94. */
  95. static int
  96. decode_name(struct xdr_stream *xdr, u32 *id)
  97. {
  98. __be32 *p;
  99. int len;
  100. /* opaque_length(4)*/
  101. p = xdr_inline_decode(xdr, 4);
  102. if (unlikely(!p))
  103. return -ENOBUFS;
  104. len = be32_to_cpup(p++);
  105. if (len < 0)
  106. return -EINVAL;
  107. dprintk("%s: len %u\n", __func__, len);
  108. /* opaque body */
  109. p = xdr_inline_decode(xdr, len);
  110. if (unlikely(!p))
  111. return -ENOBUFS;
  112. if (!nfs_map_string_to_numeric((char *)p, len, id))
  113. return -EINVAL;
  114. return 0;
  115. }
  116. static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1,
  117. const struct nfs4_ff_layout_mirror *m2)
  118. {
  119. int i, j;
  120. if (m1->fh_versions_cnt != m2->fh_versions_cnt)
  121. return false;
  122. for (i = 0; i < m1->fh_versions_cnt; i++) {
  123. bool found_fh = false;
  124. for (j = 0; j < m2->fh_versions_cnt; j++) {
  125. if (nfs_compare_fh(&m1->fh_versions[i],
  126. &m2->fh_versions[j]) == 0) {
  127. found_fh = true;
  128. break;
  129. }
  130. }
  131. if (!found_fh)
  132. return false;
  133. }
  134. return true;
  135. }
  136. static struct nfs4_ff_layout_mirror *
  137. ff_layout_add_mirror(struct pnfs_layout_hdr *lo,
  138. struct nfs4_ff_layout_mirror *mirror)
  139. {
  140. struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
  141. struct nfs4_ff_layout_mirror *pos;
  142. struct inode *inode = lo->plh_inode;
  143. spin_lock(&inode->i_lock);
  144. list_for_each_entry(pos, &ff_layout->mirrors, mirrors) {
  145. if (mirror->mirror_ds != pos->mirror_ds)
  146. continue;
  147. if (!ff_mirror_match_fh(mirror, pos))
  148. continue;
  149. if (atomic_inc_not_zero(&pos->ref)) {
  150. spin_unlock(&inode->i_lock);
  151. return pos;
  152. }
  153. }
  154. list_add(&mirror->mirrors, &ff_layout->mirrors);
  155. mirror->layout = lo;
  156. spin_unlock(&inode->i_lock);
  157. return mirror;
  158. }
  159. static void
  160. ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror)
  161. {
  162. struct inode *inode;
  163. if (mirror->layout == NULL)
  164. return;
  165. inode = mirror->layout->plh_inode;
  166. spin_lock(&inode->i_lock);
  167. list_del(&mirror->mirrors);
  168. spin_unlock(&inode->i_lock);
  169. mirror->layout = NULL;
  170. }
  171. static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags)
  172. {
  173. struct nfs4_ff_layout_mirror *mirror;
  174. mirror = kzalloc(sizeof(*mirror), gfp_flags);
  175. if (mirror != NULL) {
  176. spin_lock_init(&mirror->lock);
  177. atomic_set(&mirror->ref, 1);
  178. INIT_LIST_HEAD(&mirror->mirrors);
  179. }
  180. return mirror;
  181. }
  182. static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror)
  183. {
  184. ff_layout_remove_mirror(mirror);
  185. kfree(mirror->fh_versions);
  186. if (mirror->cred)
  187. put_rpccred(mirror->cred);
  188. nfs4_ff_layout_put_deviceid(mirror->mirror_ds);
  189. kfree(mirror);
  190. }
  191. static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror)
  192. {
  193. if (mirror != NULL && atomic_dec_and_test(&mirror->ref))
  194. ff_layout_free_mirror(mirror);
  195. }
  196. static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls)
  197. {
  198. int i;
  199. if (fls->mirror_array) {
  200. for (i = 0; i < fls->mirror_array_cnt; i++) {
  201. /* normally mirror_ds is freed in
  202. * .free_deviceid_node but we still do it here
  203. * for .alloc_lseg error path */
  204. ff_layout_put_mirror(fls->mirror_array[i]);
  205. }
  206. kfree(fls->mirror_array);
  207. fls->mirror_array = NULL;
  208. }
  209. }
  210. static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr)
  211. {
  212. int ret = 0;
  213. dprintk("--> %s\n", __func__);
  214. /* FIXME: remove this check when layout segment support is added */
  215. if (lgr->range.offset != 0 ||
  216. lgr->range.length != NFS4_MAX_UINT64) {
  217. dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
  218. __func__);
  219. ret = -EINVAL;
  220. }
  221. dprintk("--> %s returns %d\n", __func__, ret);
  222. return ret;
  223. }
  224. static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls)
  225. {
  226. if (fls) {
  227. ff_layout_free_mirror_array(fls);
  228. kfree(fls);
  229. }
  230. }
  231. static bool
  232. ff_lseg_range_is_after(const struct pnfs_layout_range *l1,
  233. const struct pnfs_layout_range *l2)
  234. {
  235. u64 end1, end2;
  236. if (l1->iomode != l2->iomode)
  237. return l1->iomode != IOMODE_READ;
  238. end1 = pnfs_calc_offset_end(l1->offset, l1->length);
  239. end2 = pnfs_calc_offset_end(l2->offset, l2->length);
  240. if (end1 < l2->offset)
  241. return false;
  242. if (end2 < l1->offset)
  243. return true;
  244. return l2->offset <= l1->offset;
  245. }
  246. static bool
  247. ff_lseg_merge(struct pnfs_layout_segment *new,
  248. struct pnfs_layout_segment *old)
  249. {
  250. u64 new_end, old_end;
  251. if (new->pls_range.iomode != old->pls_range.iomode)
  252. return false;
  253. old_end = pnfs_calc_offset_end(old->pls_range.offset,
  254. old->pls_range.length);
  255. if (old_end < new->pls_range.offset)
  256. return false;
  257. new_end = pnfs_calc_offset_end(new->pls_range.offset,
  258. new->pls_range.length);
  259. if (new_end < old->pls_range.offset)
  260. return false;
  261. /* Mergeable: copy info from 'old' to 'new' */
  262. if (new_end < old_end)
  263. new_end = old_end;
  264. if (new->pls_range.offset < old->pls_range.offset)
  265. new->pls_range.offset = old->pls_range.offset;
  266. new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset,
  267. new_end);
  268. if (test_bit(NFS_LSEG_ROC, &old->pls_flags))
  269. set_bit(NFS_LSEG_ROC, &new->pls_flags);
  270. if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags))
  271. set_bit(NFS_LSEG_LAYOUTRETURN, &new->pls_flags);
  272. return true;
  273. }
  274. static void
  275. ff_layout_add_lseg(struct pnfs_layout_hdr *lo,
  276. struct pnfs_layout_segment *lseg,
  277. struct list_head *free_me)
  278. {
  279. pnfs_generic_layout_insert_lseg(lo, lseg,
  280. ff_lseg_range_is_after,
  281. ff_lseg_merge,
  282. free_me);
  283. }
  284. static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls)
  285. {
  286. int i, j;
  287. for (i = 0; i < fls->mirror_array_cnt - 1; i++) {
  288. for (j = i + 1; j < fls->mirror_array_cnt; j++)
  289. if (fls->mirror_array[i]->efficiency <
  290. fls->mirror_array[j]->efficiency)
  291. swap(fls->mirror_array[i],
  292. fls->mirror_array[j]);
  293. }
  294. }
  295. static void ff_layout_mark_devices_valid(struct nfs4_ff_layout_segment *fls)
  296. {
  297. struct nfs4_deviceid_node *node;
  298. int i;
  299. if (!(fls->flags & FF_FLAGS_NO_IO_THRU_MDS))
  300. return;
  301. for (i = 0; i < fls->mirror_array_cnt; i++) {
  302. node = &fls->mirror_array[i]->mirror_ds->id_node;
  303. clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
  304. }
  305. }
  306. static struct pnfs_layout_segment *
  307. ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh,
  308. struct nfs4_layoutget_res *lgr,
  309. gfp_t gfp_flags)
  310. {
  311. struct pnfs_layout_segment *ret;
  312. struct nfs4_ff_layout_segment *fls = NULL;
  313. struct xdr_stream stream;
  314. struct xdr_buf buf;
  315. struct page *scratch;
  316. u64 stripe_unit;
  317. u32 mirror_array_cnt;
  318. __be32 *p;
  319. int i, rc;
  320. dprintk("--> %s\n", __func__);
  321. scratch = alloc_page(gfp_flags);
  322. if (!scratch)
  323. return ERR_PTR(-ENOMEM);
  324. xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages,
  325. lgr->layoutp->len);
  326. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  327. /* stripe unit and mirror_array_cnt */
  328. rc = -EIO;
  329. p = xdr_inline_decode(&stream, 8 + 4);
  330. if (!p)
  331. goto out_err_free;
  332. p = xdr_decode_hyper(p, &stripe_unit);
  333. mirror_array_cnt = be32_to_cpup(p++);
  334. dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__,
  335. stripe_unit, mirror_array_cnt);
  336. if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT ||
  337. mirror_array_cnt == 0)
  338. goto out_err_free;
  339. rc = -ENOMEM;
  340. fls = kzalloc(sizeof(*fls), gfp_flags);
  341. if (!fls)
  342. goto out_err_free;
  343. fls->mirror_array_cnt = mirror_array_cnt;
  344. fls->stripe_unit = stripe_unit;
  345. fls->mirror_array = kcalloc(fls->mirror_array_cnt,
  346. sizeof(fls->mirror_array[0]), gfp_flags);
  347. if (fls->mirror_array == NULL)
  348. goto out_err_free;
  349. for (i = 0; i < fls->mirror_array_cnt; i++) {
  350. struct nfs4_ff_layout_mirror *mirror;
  351. struct nfs4_deviceid devid;
  352. struct nfs4_deviceid_node *idnode;
  353. u32 ds_count;
  354. u32 fh_count;
  355. int j;
  356. rc = -EIO;
  357. p = xdr_inline_decode(&stream, 4);
  358. if (!p)
  359. goto out_err_free;
  360. ds_count = be32_to_cpup(p);
  361. /* FIXME: allow for striping? */
  362. if (ds_count != 1)
  363. goto out_err_free;
  364. fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags);
  365. if (fls->mirror_array[i] == NULL) {
  366. rc = -ENOMEM;
  367. goto out_err_free;
  368. }
  369. fls->mirror_array[i]->ds_count = ds_count;
  370. /* deviceid */
  371. rc = decode_deviceid(&stream, &devid);
  372. if (rc)
  373. goto out_err_free;
  374. idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode),
  375. &devid, lh->plh_lc_cred,
  376. gfp_flags);
  377. /*
  378. * upon success, mirror_ds is allocated by previous
  379. * getdeviceinfo, or newly by .alloc_deviceid_node
  380. * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure
  381. */
  382. if (idnode)
  383. fls->mirror_array[i]->mirror_ds =
  384. FF_LAYOUT_MIRROR_DS(idnode);
  385. else
  386. goto out_err_free;
  387. /* efficiency */
  388. rc = -EIO;
  389. p = xdr_inline_decode(&stream, 4);
  390. if (!p)
  391. goto out_err_free;
  392. fls->mirror_array[i]->efficiency = be32_to_cpup(p);
  393. /* stateid */
  394. rc = decode_stateid(&stream, &fls->mirror_array[i]->stateid);
  395. if (rc)
  396. goto out_err_free;
  397. /* fh */
  398. rc = -EIO;
  399. p = xdr_inline_decode(&stream, 4);
  400. if (!p)
  401. goto out_err_free;
  402. fh_count = be32_to_cpup(p);
  403. fls->mirror_array[i]->fh_versions =
  404. kzalloc(fh_count * sizeof(struct nfs_fh),
  405. gfp_flags);
  406. if (fls->mirror_array[i]->fh_versions == NULL) {
  407. rc = -ENOMEM;
  408. goto out_err_free;
  409. }
  410. for (j = 0; j < fh_count; j++) {
  411. rc = decode_nfs_fh(&stream,
  412. &fls->mirror_array[i]->fh_versions[j]);
  413. if (rc)
  414. goto out_err_free;
  415. }
  416. fls->mirror_array[i]->fh_versions_cnt = fh_count;
  417. /* user */
  418. rc = decode_name(&stream, &fls->mirror_array[i]->uid);
  419. if (rc)
  420. goto out_err_free;
  421. /* group */
  422. rc = decode_name(&stream, &fls->mirror_array[i]->gid);
  423. if (rc)
  424. goto out_err_free;
  425. mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]);
  426. if (mirror != fls->mirror_array[i]) {
  427. ff_layout_free_mirror(fls->mirror_array[i]);
  428. fls->mirror_array[i] = mirror;
  429. }
  430. dprintk("%s: uid %d gid %d\n", __func__,
  431. fls->mirror_array[i]->uid,
  432. fls->mirror_array[i]->gid);
  433. }
  434. p = xdr_inline_decode(&stream, 4);
  435. if (p)
  436. fls->flags = be32_to_cpup(p);
  437. ff_layout_sort_mirrors(fls);
  438. rc = ff_layout_check_layout(lgr);
  439. if (rc)
  440. goto out_err_free;
  441. ff_layout_mark_devices_valid(fls);
  442. ret = &fls->generic_hdr;
  443. dprintk("<-- %s (success)\n", __func__);
  444. out_free_page:
  445. __free_page(scratch);
  446. return ret;
  447. out_err_free:
  448. _ff_layout_free_lseg(fls);
  449. ret = ERR_PTR(rc);
  450. dprintk("<-- %s (%d)\n", __func__, rc);
  451. goto out_free_page;
  452. }
  453. static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout)
  454. {
  455. struct pnfs_layout_segment *lseg;
  456. list_for_each_entry(lseg, &layout->plh_segs, pls_list)
  457. if (lseg->pls_range.iomode == IOMODE_RW)
  458. return true;
  459. return false;
  460. }
  461. static void
  462. ff_layout_free_lseg(struct pnfs_layout_segment *lseg)
  463. {
  464. struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
  465. dprintk("--> %s\n", __func__);
  466. if (lseg->pls_range.iomode == IOMODE_RW) {
  467. struct nfs4_flexfile_layout *ffl;
  468. struct inode *inode;
  469. ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout);
  470. inode = ffl->generic_hdr.plh_inode;
  471. spin_lock(&inode->i_lock);
  472. if (!ff_layout_has_rw_segments(lseg->pls_layout)) {
  473. ffl->commit_info.nbuckets = 0;
  474. kfree(ffl->commit_info.buckets);
  475. ffl->commit_info.buckets = NULL;
  476. }
  477. spin_unlock(&inode->i_lock);
  478. }
  479. _ff_layout_free_lseg(fls);
  480. }
  481. /* Return 1 until we have multiple lsegs support */
  482. static int
  483. ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls)
  484. {
  485. return 1;
  486. }
  487. static void
  488. nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
  489. {
  490. /* first IO request? */
  491. if (atomic_inc_return(&timer->n_ops) == 1) {
  492. timer->start_time = now;
  493. }
  494. }
  495. static ktime_t
  496. nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
  497. {
  498. ktime_t start;
  499. if (atomic_dec_return(&timer->n_ops) < 0)
  500. WARN_ON_ONCE(1);
  501. start = timer->start_time;
  502. timer->start_time = now;
  503. return ktime_sub(now, start);
  504. }
  505. static bool
  506. nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror,
  507. struct nfs4_ff_layoutstat *layoutstat,
  508. ktime_t now)
  509. {
  510. static const ktime_t notime = {0};
  511. s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL;
  512. nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now);
  513. if (ktime_equal(mirror->start_time, notime))
  514. mirror->start_time = now;
  515. if (ktime_equal(mirror->last_report_time, notime))
  516. mirror->last_report_time = now;
  517. if (layoutstats_timer != 0)
  518. report_interval = (s64)layoutstats_timer * 1000LL;
  519. if (ktime_to_ms(ktime_sub(now, mirror->last_report_time)) >=
  520. report_interval) {
  521. mirror->last_report_time = now;
  522. return true;
  523. }
  524. return false;
  525. }
  526. static void
  527. nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
  528. __u64 requested)
  529. {
  530. struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
  531. iostat->ops_requested++;
  532. iostat->bytes_requested += requested;
  533. }
  534. static void
  535. nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
  536. __u64 requested,
  537. __u64 completed,
  538. ktime_t time_completed,
  539. ktime_t time_started)
  540. {
  541. struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
  542. ktime_t completion_time = ktime_sub(time_completed, time_started);
  543. ktime_t timer;
  544. iostat->ops_completed++;
  545. iostat->bytes_completed += completed;
  546. iostat->bytes_not_delivered += requested - completed;
  547. timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed);
  548. iostat->total_busy_time =
  549. ktime_add(iostat->total_busy_time, timer);
  550. iostat->aggregate_completion_time =
  551. ktime_add(iostat->aggregate_completion_time,
  552. completion_time);
  553. }
  554. static void
  555. nfs4_ff_layout_stat_io_start_read(struct inode *inode,
  556. struct nfs4_ff_layout_mirror *mirror,
  557. __u64 requested, ktime_t now)
  558. {
  559. bool report;
  560. spin_lock(&mirror->lock);
  561. report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now);
  562. nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
  563. spin_unlock(&mirror->lock);
  564. if (report)
  565. pnfs_report_layoutstat(inode, GFP_KERNEL);
  566. }
  567. static void
  568. nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
  569. struct nfs4_ff_layout_mirror *mirror,
  570. __u64 requested,
  571. __u64 completed)
  572. {
  573. spin_lock(&mirror->lock);
  574. nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
  575. requested, completed,
  576. ktime_get(), task->tk_start);
  577. spin_unlock(&mirror->lock);
  578. }
  579. static void
  580. nfs4_ff_layout_stat_io_start_write(struct inode *inode,
  581. struct nfs4_ff_layout_mirror *mirror,
  582. __u64 requested, ktime_t now)
  583. {
  584. bool report;
  585. spin_lock(&mirror->lock);
  586. report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now);
  587. nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
  588. spin_unlock(&mirror->lock);
  589. if (report)
  590. pnfs_report_layoutstat(inode, GFP_NOIO);
  591. }
  592. static void
  593. nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
  594. struct nfs4_ff_layout_mirror *mirror,
  595. __u64 requested,
  596. __u64 completed,
  597. enum nfs3_stable_how committed)
  598. {
  599. if (committed == NFS_UNSTABLE)
  600. requested = completed = 0;
  601. spin_lock(&mirror->lock);
  602. nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
  603. requested, completed, ktime_get(), task->tk_start);
  604. spin_unlock(&mirror->lock);
  605. }
  606. static int
  607. ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
  608. struct nfs_commit_info *cinfo,
  609. gfp_t gfp_flags)
  610. {
  611. struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
  612. struct pnfs_commit_bucket *buckets;
  613. int size;
  614. if (cinfo->ds->nbuckets != 0) {
  615. /* This assumes there is only one RW lseg per file.
  616. * To support multiple lseg per file, we need to
  617. * change struct pnfs_commit_bucket to allow dynamic
  618. * increasing nbuckets.
  619. */
  620. return 0;
  621. }
  622. size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
  623. buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
  624. gfp_flags);
  625. if (!buckets)
  626. return -ENOMEM;
  627. else {
  628. int i;
  629. spin_lock(cinfo->lock);
  630. if (cinfo->ds->nbuckets != 0)
  631. kfree(buckets);
  632. else {
  633. cinfo->ds->buckets = buckets;
  634. cinfo->ds->nbuckets = size;
  635. for (i = 0; i < size; i++) {
  636. INIT_LIST_HEAD(&buckets[i].written);
  637. INIT_LIST_HEAD(&buckets[i].committing);
  638. /* mark direct verifier as unset */
  639. buckets[i].direct_verf.committed =
  640. NFS_INVALID_STABLE_HOW;
  641. }
  642. }
  643. spin_unlock(cinfo->lock);
  644. return 0;
  645. }
  646. }
  647. static struct nfs4_pnfs_ds *
  648. ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg,
  649. int start_idx,
  650. int *best_idx)
  651. {
  652. struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
  653. struct nfs4_pnfs_ds *ds;
  654. int idx;
  655. /* mirrors are sorted by efficiency */
  656. for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) {
  657. ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
  658. if (ds) {
  659. *best_idx = idx;
  660. return ds;
  661. }
  662. }
  663. return NULL;
  664. }
  665. static void
  666. ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  667. struct nfs_page *req)
  668. {
  669. struct nfs_pgio_mirror *pgm;
  670. struct nfs4_ff_layout_mirror *mirror;
  671. struct nfs4_pnfs_ds *ds;
  672. int ds_idx;
  673. /* Use full layout for now */
  674. if (!pgio->pg_lseg) {
  675. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  676. req->wb_context,
  677. 0,
  678. NFS4_MAX_UINT64,
  679. IOMODE_READ,
  680. GFP_KERNEL);
  681. if (IS_ERR(pgio->pg_lseg)) {
  682. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  683. pgio->pg_lseg = NULL;
  684. return;
  685. }
  686. }
  687. /* If no lseg, fall back to read through mds */
  688. if (pgio->pg_lseg == NULL)
  689. goto out_mds;
  690. ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
  691. if (!ds)
  692. goto out_mds;
  693. mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
  694. pgio->pg_mirror_idx = ds_idx;
  695. /* read always uses only one mirror - idx 0 for pgio layer */
  696. pgm = &pgio->pg_mirrors[0];
  697. pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
  698. return;
  699. out_mds:
  700. pnfs_put_lseg(pgio->pg_lseg);
  701. pgio->pg_lseg = NULL;
  702. nfs_pageio_reset_read_mds(pgio);
  703. }
  704. static void
  705. ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  706. struct nfs_page *req)
  707. {
  708. struct nfs4_ff_layout_mirror *mirror;
  709. struct nfs_pgio_mirror *pgm;
  710. struct nfs_commit_info cinfo;
  711. struct nfs4_pnfs_ds *ds;
  712. int i;
  713. int status;
  714. if (!pgio->pg_lseg) {
  715. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  716. req->wb_context,
  717. 0,
  718. NFS4_MAX_UINT64,
  719. IOMODE_RW,
  720. GFP_NOFS);
  721. if (IS_ERR(pgio->pg_lseg)) {
  722. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  723. pgio->pg_lseg = NULL;
  724. return;
  725. }
  726. }
  727. /* If no lseg, fall back to write through mds */
  728. if (pgio->pg_lseg == NULL)
  729. goto out_mds;
  730. nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
  731. status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
  732. if (status < 0)
  733. goto out_mds;
  734. /* Use a direct mapping of ds_idx to pgio mirror_idx */
  735. if (WARN_ON_ONCE(pgio->pg_mirror_count !=
  736. FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
  737. goto out_mds;
  738. for (i = 0; i < pgio->pg_mirror_count; i++) {
  739. ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
  740. if (!ds)
  741. goto out_mds;
  742. pgm = &pgio->pg_mirrors[i];
  743. mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
  744. pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
  745. }
  746. return;
  747. out_mds:
  748. pnfs_put_lseg(pgio->pg_lseg);
  749. pgio->pg_lseg = NULL;
  750. nfs_pageio_reset_write_mds(pgio);
  751. }
  752. static unsigned int
  753. ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
  754. struct nfs_page *req)
  755. {
  756. if (!pgio->pg_lseg) {
  757. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  758. req->wb_context,
  759. 0,
  760. NFS4_MAX_UINT64,
  761. IOMODE_RW,
  762. GFP_NOFS);
  763. if (IS_ERR(pgio->pg_lseg)) {
  764. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  765. pgio->pg_lseg = NULL;
  766. goto out;
  767. }
  768. }
  769. if (pgio->pg_lseg)
  770. return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
  771. /* no lseg means that pnfs is not in use, so no mirroring here */
  772. nfs_pageio_reset_write_mds(pgio);
  773. out:
  774. return 1;
  775. }
  776. static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
  777. .pg_init = ff_layout_pg_init_read,
  778. .pg_test = pnfs_generic_pg_test,
  779. .pg_doio = pnfs_generic_pg_readpages,
  780. .pg_cleanup = pnfs_generic_pg_cleanup,
  781. };
  782. static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
  783. .pg_init = ff_layout_pg_init_write,
  784. .pg_test = pnfs_generic_pg_test,
  785. .pg_doio = pnfs_generic_pg_writepages,
  786. .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
  787. .pg_cleanup = pnfs_generic_pg_cleanup,
  788. };
  789. static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
  790. {
  791. struct rpc_task *task = &hdr->task;
  792. pnfs_layoutcommit_inode(hdr->inode, false);
  793. if (retry_pnfs) {
  794. dprintk("%s Reset task %5u for i/o through pNFS "
  795. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  796. hdr->task.tk_pid,
  797. hdr->inode->i_sb->s_id,
  798. (unsigned long long)NFS_FILEID(hdr->inode),
  799. hdr->args.count,
  800. (unsigned long long)hdr->args.offset);
  801. if (!hdr->dreq) {
  802. struct nfs_open_context *ctx;
  803. ctx = nfs_list_entry(hdr->pages.next)->wb_context;
  804. set_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  805. hdr->completion_ops->error_cleanup(&hdr->pages);
  806. } else {
  807. nfs_direct_set_resched_writes(hdr->dreq);
  808. /* fake unstable write to let common nfs resend pages */
  809. hdr->verf.committed = NFS_UNSTABLE;
  810. hdr->good_bytes = hdr->args.count;
  811. }
  812. return;
  813. }
  814. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  815. dprintk("%s Reset task %5u for i/o through MDS "
  816. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  817. hdr->task.tk_pid,
  818. hdr->inode->i_sb->s_id,
  819. (unsigned long long)NFS_FILEID(hdr->inode),
  820. hdr->args.count,
  821. (unsigned long long)hdr->args.offset);
  822. task->tk_status = pnfs_write_done_resend_to_mds(hdr);
  823. }
  824. }
  825. static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
  826. {
  827. struct rpc_task *task = &hdr->task;
  828. pnfs_layoutcommit_inode(hdr->inode, false);
  829. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  830. dprintk("%s Reset task %5u for i/o through MDS "
  831. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  832. hdr->task.tk_pid,
  833. hdr->inode->i_sb->s_id,
  834. (unsigned long long)NFS_FILEID(hdr->inode),
  835. hdr->args.count,
  836. (unsigned long long)hdr->args.offset);
  837. task->tk_status = pnfs_read_done_resend_to_mds(hdr);
  838. }
  839. }
  840. static int ff_layout_async_handle_error_v4(struct rpc_task *task,
  841. struct nfs4_state *state,
  842. struct nfs_client *clp,
  843. struct pnfs_layout_segment *lseg,
  844. int idx)
  845. {
  846. struct pnfs_layout_hdr *lo = lseg->pls_layout;
  847. struct inode *inode = lo->plh_inode;
  848. struct nfs_server *mds_server = NFS_SERVER(inode);
  849. struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
  850. struct nfs_client *mds_client = mds_server->nfs_client;
  851. struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
  852. if (task->tk_status >= 0)
  853. return 0;
  854. switch (task->tk_status) {
  855. /* MDS state errors */
  856. case -NFS4ERR_DELEG_REVOKED:
  857. case -NFS4ERR_ADMIN_REVOKED:
  858. case -NFS4ERR_BAD_STATEID:
  859. if (state == NULL)
  860. break;
  861. nfs_remove_bad_delegation(state->inode);
  862. case -NFS4ERR_OPENMODE:
  863. if (state == NULL)
  864. break;
  865. if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
  866. goto out_bad_stateid;
  867. goto wait_on_recovery;
  868. case -NFS4ERR_EXPIRED:
  869. if (state != NULL) {
  870. if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
  871. goto out_bad_stateid;
  872. }
  873. nfs4_schedule_lease_recovery(mds_client);
  874. goto wait_on_recovery;
  875. /* DS session errors */
  876. case -NFS4ERR_BADSESSION:
  877. case -NFS4ERR_BADSLOT:
  878. case -NFS4ERR_BAD_HIGH_SLOT:
  879. case -NFS4ERR_DEADSESSION:
  880. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  881. case -NFS4ERR_SEQ_FALSE_RETRY:
  882. case -NFS4ERR_SEQ_MISORDERED:
  883. dprintk("%s ERROR %d, Reset session. Exchangeid "
  884. "flags 0x%x\n", __func__, task->tk_status,
  885. clp->cl_exchange_flags);
  886. nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
  887. break;
  888. case -NFS4ERR_DELAY:
  889. case -NFS4ERR_GRACE:
  890. rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
  891. break;
  892. case -NFS4ERR_RETRY_UNCACHED_REP:
  893. break;
  894. /* Invalidate Layout errors */
  895. case -NFS4ERR_PNFS_NO_LAYOUT:
  896. case -ESTALE: /* mapped NFS4ERR_STALE */
  897. case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
  898. case -EISDIR: /* mapped NFS4ERR_ISDIR */
  899. case -NFS4ERR_FHEXPIRED:
  900. case -NFS4ERR_WRONG_TYPE:
  901. dprintk("%s Invalid layout error %d\n", __func__,
  902. task->tk_status);
  903. /*
  904. * Destroy layout so new i/o will get a new layout.
  905. * Layout will not be destroyed until all current lseg
  906. * references are put. Mark layout as invalid to resend failed
  907. * i/o and all i/o waiting on the slot table to the MDS until
  908. * layout is destroyed and a new valid layout is obtained.
  909. */
  910. pnfs_destroy_layout(NFS_I(inode));
  911. rpc_wake_up(&tbl->slot_tbl_waitq);
  912. goto reset;
  913. /* RPC connection errors */
  914. case -ECONNREFUSED:
  915. case -EHOSTDOWN:
  916. case -EHOSTUNREACH:
  917. case -ENETUNREACH:
  918. case -EIO:
  919. case -ETIMEDOUT:
  920. case -EPIPE:
  921. dprintk("%s DS connection error %d\n", __func__,
  922. task->tk_status);
  923. nfs4_mark_deviceid_unavailable(devid);
  924. rpc_wake_up(&tbl->slot_tbl_waitq);
  925. /* fall through */
  926. default:
  927. if (ff_layout_no_fallback_to_mds(lseg) ||
  928. ff_layout_has_available_ds(lseg))
  929. return -NFS4ERR_RESET_TO_PNFS;
  930. reset:
  931. dprintk("%s Retry through MDS. Error %d\n", __func__,
  932. task->tk_status);
  933. return -NFS4ERR_RESET_TO_MDS;
  934. }
  935. out:
  936. task->tk_status = 0;
  937. return -EAGAIN;
  938. out_bad_stateid:
  939. task->tk_status = -EIO;
  940. return 0;
  941. wait_on_recovery:
  942. rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
  943. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
  944. rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
  945. goto out;
  946. }
  947. /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
  948. static int ff_layout_async_handle_error_v3(struct rpc_task *task,
  949. struct pnfs_layout_segment *lseg,
  950. int idx)
  951. {
  952. struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
  953. if (task->tk_status >= 0)
  954. return 0;
  955. switch (task->tk_status) {
  956. /* File access problems. Don't mark the device as unavailable */
  957. case -EACCES:
  958. case -ESTALE:
  959. case -EISDIR:
  960. case -EBADHANDLE:
  961. case -ELOOP:
  962. case -ENOSPC:
  963. break;
  964. case -EJUKEBOX:
  965. nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
  966. goto out_retry;
  967. default:
  968. dprintk("%s DS connection error %d\n", __func__,
  969. task->tk_status);
  970. nfs4_mark_deviceid_unavailable(devid);
  971. }
  972. /* FIXME: Need to prevent infinite looping here. */
  973. return -NFS4ERR_RESET_TO_PNFS;
  974. out_retry:
  975. task->tk_status = 0;
  976. rpc_restart_call(task);
  977. rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
  978. return -EAGAIN;
  979. }
  980. static int ff_layout_async_handle_error(struct rpc_task *task,
  981. struct nfs4_state *state,
  982. struct nfs_client *clp,
  983. struct pnfs_layout_segment *lseg,
  984. int idx)
  985. {
  986. int vers = clp->cl_nfs_mod->rpc_vers->number;
  987. switch (vers) {
  988. case 3:
  989. return ff_layout_async_handle_error_v3(task, lseg, idx);
  990. case 4:
  991. return ff_layout_async_handle_error_v4(task, state, clp,
  992. lseg, idx);
  993. default:
  994. /* should never happen */
  995. WARN_ON_ONCE(1);
  996. return 0;
  997. }
  998. }
  999. static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
  1000. int idx, u64 offset, u64 length,
  1001. u32 status, int opnum, int error)
  1002. {
  1003. struct nfs4_ff_layout_mirror *mirror;
  1004. int err;
  1005. if (status == 0) {
  1006. switch (error) {
  1007. case -ETIMEDOUT:
  1008. case -EPFNOSUPPORT:
  1009. case -EPROTONOSUPPORT:
  1010. case -EOPNOTSUPP:
  1011. case -ECONNREFUSED:
  1012. case -ECONNRESET:
  1013. case -EHOSTDOWN:
  1014. case -EHOSTUNREACH:
  1015. case -ENETUNREACH:
  1016. case -EADDRINUSE:
  1017. case -ENOBUFS:
  1018. case -EPIPE:
  1019. case -EPERM:
  1020. status = NFS4ERR_NXIO;
  1021. break;
  1022. case -EACCES:
  1023. status = NFS4ERR_ACCESS;
  1024. break;
  1025. default:
  1026. return;
  1027. }
  1028. }
  1029. mirror = FF_LAYOUT_COMP(lseg, idx);
  1030. err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
  1031. mirror, offset, length, status, opnum,
  1032. GFP_NOIO);
  1033. pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
  1034. dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
  1035. }
  1036. /* NFS_PROTO call done callback routines */
  1037. static int ff_layout_read_done_cb(struct rpc_task *task,
  1038. struct nfs_pgio_header *hdr)
  1039. {
  1040. int err;
  1041. trace_nfs4_pnfs_read(hdr, task->tk_status);
  1042. if (task->tk_status < 0)
  1043. ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
  1044. hdr->args.offset, hdr->args.count,
  1045. hdr->res.op_status, OP_READ,
  1046. task->tk_status);
  1047. err = ff_layout_async_handle_error(task, hdr->args.context->state,
  1048. hdr->ds_clp, hdr->lseg,
  1049. hdr->pgio_mirror_idx);
  1050. switch (err) {
  1051. case -NFS4ERR_RESET_TO_PNFS:
  1052. if (ff_layout_choose_best_ds_for_read(hdr->lseg,
  1053. hdr->pgio_mirror_idx + 1,
  1054. &hdr->pgio_mirror_idx))
  1055. goto out_eagain;
  1056. set_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
  1057. &hdr->lseg->pls_layout->plh_flags);
  1058. pnfs_read_resend_pnfs(hdr);
  1059. return task->tk_status;
  1060. case -NFS4ERR_RESET_TO_MDS:
  1061. ff_layout_reset_read(hdr);
  1062. return task->tk_status;
  1063. case -EAGAIN:
  1064. goto out_eagain;
  1065. }
  1066. return 0;
  1067. out_eagain:
  1068. rpc_restart_call_prepare(task);
  1069. return -EAGAIN;
  1070. }
  1071. static bool
  1072. ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
  1073. {
  1074. return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
  1075. }
  1076. /*
  1077. * We reference the rpc_cred of the first WRITE that triggers the need for
  1078. * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
  1079. * rfc5661 is not clear about which credential should be used.
  1080. *
  1081. * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
  1082. * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
  1083. * we always send layoutcommit after DS writes.
  1084. */
  1085. static void
  1086. ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr)
  1087. {
  1088. if (!ff_layout_need_layoutcommit(hdr->lseg))
  1089. return;
  1090. pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
  1091. hdr->mds_offset + hdr->res.count);
  1092. dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
  1093. (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
  1094. }
  1095. static bool
  1096. ff_layout_reset_to_mds(struct pnfs_layout_segment *lseg, int idx)
  1097. {
  1098. /* No mirroring for now */
  1099. struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
  1100. return ff_layout_test_devid_unavailable(node);
  1101. }
  1102. static int ff_layout_read_prepare_common(struct rpc_task *task,
  1103. struct nfs_pgio_header *hdr)
  1104. {
  1105. nfs4_ff_layout_stat_io_start_read(hdr->inode,
  1106. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1107. hdr->args.count,
  1108. task->tk_start);
  1109. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  1110. rpc_exit(task, -EIO);
  1111. return -EIO;
  1112. }
  1113. if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
  1114. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  1115. if (ff_layout_has_available_ds(hdr->lseg))
  1116. pnfs_read_resend_pnfs(hdr);
  1117. else
  1118. ff_layout_reset_read(hdr);
  1119. rpc_exit(task, 0);
  1120. return -EAGAIN;
  1121. }
  1122. hdr->pgio_done_cb = ff_layout_read_done_cb;
  1123. return 0;
  1124. }
  1125. /*
  1126. * Call ops for the async read/write cases
  1127. * In the case of dense layouts, the offset needs to be reset to its
  1128. * original value.
  1129. */
  1130. static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
  1131. {
  1132. struct nfs_pgio_header *hdr = data;
  1133. if (ff_layout_read_prepare_common(task, hdr))
  1134. return;
  1135. rpc_call_start(task);
  1136. }
  1137. static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
  1138. struct nfs4_sequence_args *args,
  1139. struct nfs4_sequence_res *res,
  1140. struct rpc_task *task)
  1141. {
  1142. if (ds_clp->cl_session)
  1143. return nfs41_setup_sequence(ds_clp->cl_session,
  1144. args,
  1145. res,
  1146. task);
  1147. return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
  1148. args,
  1149. res,
  1150. task);
  1151. }
  1152. static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
  1153. {
  1154. struct nfs_pgio_header *hdr = data;
  1155. if (ff_layout_setup_sequence(hdr->ds_clp,
  1156. &hdr->args.seq_args,
  1157. &hdr->res.seq_res,
  1158. task))
  1159. return;
  1160. if (ff_layout_read_prepare_common(task, hdr))
  1161. return;
  1162. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  1163. hdr->args.lock_context, FMODE_READ) == -EIO)
  1164. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  1165. }
  1166. static void ff_layout_read_call_done(struct rpc_task *task, void *data)
  1167. {
  1168. struct nfs_pgio_header *hdr = data;
  1169. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  1170. nfs4_ff_layout_stat_io_end_read(task,
  1171. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1172. hdr->args.count, hdr->res.count);
  1173. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  1174. task->tk_status == 0) {
  1175. nfs4_sequence_done(task, &hdr->res.seq_res);
  1176. return;
  1177. }
  1178. /* Note this may cause RPC to be resent */
  1179. hdr->mds_ops->rpc_call_done(task, hdr);
  1180. }
  1181. static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
  1182. {
  1183. struct nfs_pgio_header *hdr = data;
  1184. rpc_count_iostats_metrics(task,
  1185. &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
  1186. }
  1187. static int ff_layout_write_done_cb(struct rpc_task *task,
  1188. struct nfs_pgio_header *hdr)
  1189. {
  1190. int err;
  1191. trace_nfs4_pnfs_write(hdr, task->tk_status);
  1192. if (task->tk_status < 0)
  1193. ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
  1194. hdr->args.offset, hdr->args.count,
  1195. hdr->res.op_status, OP_WRITE,
  1196. task->tk_status);
  1197. err = ff_layout_async_handle_error(task, hdr->args.context->state,
  1198. hdr->ds_clp, hdr->lseg,
  1199. hdr->pgio_mirror_idx);
  1200. switch (err) {
  1201. case -NFS4ERR_RESET_TO_PNFS:
  1202. pnfs_set_retry_layoutget(hdr->lseg->pls_layout);
  1203. ff_layout_reset_write(hdr, true);
  1204. return task->tk_status;
  1205. case -NFS4ERR_RESET_TO_MDS:
  1206. pnfs_clear_retry_layoutget(hdr->lseg->pls_layout);
  1207. ff_layout_reset_write(hdr, false);
  1208. return task->tk_status;
  1209. case -EAGAIN:
  1210. rpc_restart_call_prepare(task);
  1211. return -EAGAIN;
  1212. }
  1213. if (hdr->res.verf->committed == NFS_FILE_SYNC ||
  1214. hdr->res.verf->committed == NFS_DATA_SYNC)
  1215. ff_layout_set_layoutcommit(hdr);
  1216. /* zero out fattr since we don't care DS attr at all */
  1217. hdr->fattr.valid = 0;
  1218. if (task->tk_status >= 0)
  1219. nfs_writeback_update_inode(hdr);
  1220. return 0;
  1221. }
  1222. static int ff_layout_commit_done_cb(struct rpc_task *task,
  1223. struct nfs_commit_data *data)
  1224. {
  1225. int err;
  1226. trace_nfs4_pnfs_commit_ds(data, task->tk_status);
  1227. if (task->tk_status < 0)
  1228. ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
  1229. data->args.offset, data->args.count,
  1230. data->res.op_status, OP_COMMIT,
  1231. task->tk_status);
  1232. err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
  1233. data->lseg, data->ds_commit_index);
  1234. switch (err) {
  1235. case -NFS4ERR_RESET_TO_PNFS:
  1236. pnfs_set_retry_layoutget(data->lseg->pls_layout);
  1237. pnfs_generic_prepare_to_resend_writes(data);
  1238. return -EAGAIN;
  1239. case -NFS4ERR_RESET_TO_MDS:
  1240. pnfs_clear_retry_layoutget(data->lseg->pls_layout);
  1241. pnfs_generic_prepare_to_resend_writes(data);
  1242. return -EAGAIN;
  1243. case -EAGAIN:
  1244. rpc_restart_call_prepare(task);
  1245. return -EAGAIN;
  1246. }
  1247. if (ff_layout_need_layoutcommit(data->lseg))
  1248. pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
  1249. return 0;
  1250. }
  1251. static int ff_layout_write_prepare_common(struct rpc_task *task,
  1252. struct nfs_pgio_header *hdr)
  1253. {
  1254. nfs4_ff_layout_stat_io_start_write(hdr->inode,
  1255. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1256. hdr->args.count,
  1257. task->tk_start);
  1258. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  1259. rpc_exit(task, -EIO);
  1260. return -EIO;
  1261. }
  1262. if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
  1263. bool retry_pnfs;
  1264. retry_pnfs = ff_layout_has_available_ds(hdr->lseg);
  1265. dprintk("%s task %u reset io to %s\n", __func__,
  1266. task->tk_pid, retry_pnfs ? "pNFS" : "MDS");
  1267. ff_layout_reset_write(hdr, retry_pnfs);
  1268. rpc_exit(task, 0);
  1269. return -EAGAIN;
  1270. }
  1271. return 0;
  1272. }
  1273. static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
  1274. {
  1275. struct nfs_pgio_header *hdr = data;
  1276. if (ff_layout_write_prepare_common(task, hdr))
  1277. return;
  1278. rpc_call_start(task);
  1279. }
  1280. static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
  1281. {
  1282. struct nfs_pgio_header *hdr = data;
  1283. if (ff_layout_setup_sequence(hdr->ds_clp,
  1284. &hdr->args.seq_args,
  1285. &hdr->res.seq_res,
  1286. task))
  1287. return;
  1288. if (ff_layout_write_prepare_common(task, hdr))
  1289. return;
  1290. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  1291. hdr->args.lock_context, FMODE_WRITE) == -EIO)
  1292. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  1293. }
  1294. static void ff_layout_write_call_done(struct rpc_task *task, void *data)
  1295. {
  1296. struct nfs_pgio_header *hdr = data;
  1297. nfs4_ff_layout_stat_io_end_write(task,
  1298. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1299. hdr->args.count, hdr->res.count,
  1300. hdr->res.verf->committed);
  1301. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  1302. task->tk_status == 0) {
  1303. nfs4_sequence_done(task, &hdr->res.seq_res);
  1304. return;
  1305. }
  1306. /* Note this may cause RPC to be resent */
  1307. hdr->mds_ops->rpc_call_done(task, hdr);
  1308. }
  1309. static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
  1310. {
  1311. struct nfs_pgio_header *hdr = data;
  1312. rpc_count_iostats_metrics(task,
  1313. &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
  1314. }
  1315. static void ff_layout_commit_prepare_common(struct rpc_task *task,
  1316. struct nfs_commit_data *cdata)
  1317. {
  1318. nfs4_ff_layout_stat_io_start_write(cdata->inode,
  1319. FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
  1320. 0, task->tk_start);
  1321. }
  1322. static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
  1323. {
  1324. ff_layout_commit_prepare_common(task, data);
  1325. rpc_call_start(task);
  1326. }
  1327. static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
  1328. {
  1329. struct nfs_commit_data *wdata = data;
  1330. if (ff_layout_setup_sequence(wdata->ds_clp,
  1331. &wdata->args.seq_args,
  1332. &wdata->res.seq_res,
  1333. task))
  1334. return;
  1335. ff_layout_commit_prepare_common(task, data);
  1336. }
  1337. static void ff_layout_commit_done(struct rpc_task *task, void *data)
  1338. {
  1339. struct nfs_commit_data *cdata = data;
  1340. struct nfs_page *req;
  1341. __u64 count = 0;
  1342. if (task->tk_status == 0) {
  1343. list_for_each_entry(req, &cdata->pages, wb_list)
  1344. count += req->wb_bytes;
  1345. }
  1346. nfs4_ff_layout_stat_io_end_write(task,
  1347. FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
  1348. count, count, NFS_FILE_SYNC);
  1349. pnfs_generic_write_commit_done(task, data);
  1350. }
  1351. static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
  1352. {
  1353. struct nfs_commit_data *cdata = data;
  1354. rpc_count_iostats_metrics(task,
  1355. &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
  1356. }
  1357. static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
  1358. .rpc_call_prepare = ff_layout_read_prepare_v3,
  1359. .rpc_call_done = ff_layout_read_call_done,
  1360. .rpc_count_stats = ff_layout_read_count_stats,
  1361. .rpc_release = pnfs_generic_rw_release,
  1362. };
  1363. static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
  1364. .rpc_call_prepare = ff_layout_read_prepare_v4,
  1365. .rpc_call_done = ff_layout_read_call_done,
  1366. .rpc_count_stats = ff_layout_read_count_stats,
  1367. .rpc_release = pnfs_generic_rw_release,
  1368. };
  1369. static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
  1370. .rpc_call_prepare = ff_layout_write_prepare_v3,
  1371. .rpc_call_done = ff_layout_write_call_done,
  1372. .rpc_count_stats = ff_layout_write_count_stats,
  1373. .rpc_release = pnfs_generic_rw_release,
  1374. };
  1375. static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
  1376. .rpc_call_prepare = ff_layout_write_prepare_v4,
  1377. .rpc_call_done = ff_layout_write_call_done,
  1378. .rpc_count_stats = ff_layout_write_count_stats,
  1379. .rpc_release = pnfs_generic_rw_release,
  1380. };
  1381. static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
  1382. .rpc_call_prepare = ff_layout_commit_prepare_v3,
  1383. .rpc_call_done = ff_layout_commit_done,
  1384. .rpc_count_stats = ff_layout_commit_count_stats,
  1385. .rpc_release = pnfs_generic_commit_release,
  1386. };
  1387. static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
  1388. .rpc_call_prepare = ff_layout_commit_prepare_v4,
  1389. .rpc_call_done = ff_layout_commit_done,
  1390. .rpc_count_stats = ff_layout_commit_count_stats,
  1391. .rpc_release = pnfs_generic_commit_release,
  1392. };
  1393. static enum pnfs_try_status
  1394. ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
  1395. {
  1396. struct pnfs_layout_segment *lseg = hdr->lseg;
  1397. struct nfs4_pnfs_ds *ds;
  1398. struct rpc_clnt *ds_clnt;
  1399. struct rpc_cred *ds_cred;
  1400. loff_t offset = hdr->args.offset;
  1401. u32 idx = hdr->pgio_mirror_idx;
  1402. int vers;
  1403. struct nfs_fh *fh;
  1404. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  1405. __func__, hdr->inode->i_ino,
  1406. hdr->args.pgbase, (size_t)hdr->args.count, offset);
  1407. ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
  1408. if (!ds)
  1409. goto out_failed;
  1410. ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
  1411. hdr->inode);
  1412. if (IS_ERR(ds_clnt))
  1413. goto out_failed;
  1414. ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
  1415. if (IS_ERR(ds_cred))
  1416. goto out_failed;
  1417. vers = nfs4_ff_layout_ds_version(lseg, idx);
  1418. dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
  1419. ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
  1420. atomic_inc(&ds->ds_clp->cl_count);
  1421. hdr->ds_clp = ds->ds_clp;
  1422. fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
  1423. if (fh)
  1424. hdr->args.fh = fh;
  1425. /*
  1426. * Note that if we ever decide to split across DSes,
  1427. * then we may need to handle dense-like offsets.
  1428. */
  1429. hdr->args.offset = offset;
  1430. hdr->mds_offset = offset;
  1431. /* Perform an asynchronous read to ds */
  1432. nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
  1433. vers == 3 ? &ff_layout_read_call_ops_v3 :
  1434. &ff_layout_read_call_ops_v4,
  1435. 0, RPC_TASK_SOFTCONN);
  1436. return PNFS_ATTEMPTED;
  1437. out_failed:
  1438. if (ff_layout_has_available_ds(lseg))
  1439. return PNFS_TRY_AGAIN;
  1440. return PNFS_NOT_ATTEMPTED;
  1441. }
  1442. /* Perform async writes. */
  1443. static enum pnfs_try_status
  1444. ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
  1445. {
  1446. struct pnfs_layout_segment *lseg = hdr->lseg;
  1447. struct nfs4_pnfs_ds *ds;
  1448. struct rpc_clnt *ds_clnt;
  1449. struct rpc_cred *ds_cred;
  1450. loff_t offset = hdr->args.offset;
  1451. int vers;
  1452. struct nfs_fh *fh;
  1453. int idx = hdr->pgio_mirror_idx;
  1454. ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
  1455. if (!ds)
  1456. return PNFS_NOT_ATTEMPTED;
  1457. ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
  1458. hdr->inode);
  1459. if (IS_ERR(ds_clnt))
  1460. return PNFS_NOT_ATTEMPTED;
  1461. ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
  1462. if (IS_ERR(ds_cred))
  1463. return PNFS_NOT_ATTEMPTED;
  1464. vers = nfs4_ff_layout_ds_version(lseg, idx);
  1465. dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
  1466. __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
  1467. offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
  1468. vers);
  1469. hdr->pgio_done_cb = ff_layout_write_done_cb;
  1470. atomic_inc(&ds->ds_clp->cl_count);
  1471. hdr->ds_clp = ds->ds_clp;
  1472. hdr->ds_commit_idx = idx;
  1473. fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
  1474. if (fh)
  1475. hdr->args.fh = fh;
  1476. /*
  1477. * Note that if we ever decide to split across DSes,
  1478. * then we may need to handle dense-like offsets.
  1479. */
  1480. hdr->args.offset = offset;
  1481. /* Perform an asynchronous write */
  1482. nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
  1483. vers == 3 ? &ff_layout_write_call_ops_v3 :
  1484. &ff_layout_write_call_ops_v4,
  1485. sync, RPC_TASK_SOFTCONN);
  1486. return PNFS_ATTEMPTED;
  1487. }
  1488. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  1489. {
  1490. return i;
  1491. }
  1492. static struct nfs_fh *
  1493. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  1494. {
  1495. struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
  1496. /* FIXME: Assume that there is only one NFS version available
  1497. * for the DS.
  1498. */
  1499. return &flseg->mirror_array[i]->fh_versions[0];
  1500. }
  1501. static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
  1502. {
  1503. struct pnfs_layout_segment *lseg = data->lseg;
  1504. struct nfs4_pnfs_ds *ds;
  1505. struct rpc_clnt *ds_clnt;
  1506. struct rpc_cred *ds_cred;
  1507. u32 idx;
  1508. int vers;
  1509. struct nfs_fh *fh;
  1510. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  1511. ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
  1512. if (!ds)
  1513. goto out_err;
  1514. ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
  1515. data->inode);
  1516. if (IS_ERR(ds_clnt))
  1517. goto out_err;
  1518. ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
  1519. if (IS_ERR(ds_cred))
  1520. goto out_err;
  1521. vers = nfs4_ff_layout_ds_version(lseg, idx);
  1522. dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
  1523. data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
  1524. vers);
  1525. data->commit_done_cb = ff_layout_commit_done_cb;
  1526. data->cred = ds_cred;
  1527. atomic_inc(&ds->ds_clp->cl_count);
  1528. data->ds_clp = ds->ds_clp;
  1529. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  1530. if (fh)
  1531. data->args.fh = fh;
  1532. return nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
  1533. vers == 3 ? &ff_layout_commit_call_ops_v3 :
  1534. &ff_layout_commit_call_ops_v4,
  1535. how, RPC_TASK_SOFTCONN);
  1536. out_err:
  1537. pnfs_generic_prepare_to_resend_writes(data);
  1538. pnfs_generic_commit_release(data);
  1539. return -EAGAIN;
  1540. }
  1541. static int
  1542. ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  1543. int how, struct nfs_commit_info *cinfo)
  1544. {
  1545. return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
  1546. ff_layout_initiate_commit);
  1547. }
  1548. static struct pnfs_ds_commit_info *
  1549. ff_layout_get_ds_info(struct inode *inode)
  1550. {
  1551. struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
  1552. if (layout == NULL)
  1553. return NULL;
  1554. return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
  1555. }
  1556. static void
  1557. ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
  1558. {
  1559. nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
  1560. id_node));
  1561. }
  1562. static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
  1563. struct xdr_stream *xdr,
  1564. const struct nfs4_layoutreturn_args *args)
  1565. {
  1566. struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
  1567. __be32 *start;
  1568. int count = 0, ret = 0;
  1569. start = xdr_reserve_space(xdr, 4);
  1570. if (unlikely(!start))
  1571. return -E2BIG;
  1572. /* This assume we always return _ALL_ layouts */
  1573. spin_lock(&hdr->plh_inode->i_lock);
  1574. ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
  1575. spin_unlock(&hdr->plh_inode->i_lock);
  1576. *start = cpu_to_be32(count);
  1577. return ret;
  1578. }
  1579. /* report nothing for now */
  1580. static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
  1581. struct xdr_stream *xdr,
  1582. const struct nfs4_layoutreturn_args *args)
  1583. {
  1584. __be32 *p;
  1585. p = xdr_reserve_space(xdr, 4);
  1586. if (likely(p))
  1587. *p = cpu_to_be32(0);
  1588. }
  1589. static struct nfs4_deviceid_node *
  1590. ff_layout_alloc_deviceid_node(struct nfs_server *server,
  1591. struct pnfs_device *pdev, gfp_t gfp_flags)
  1592. {
  1593. struct nfs4_ff_layout_ds *dsaddr;
  1594. dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
  1595. if (!dsaddr)
  1596. return NULL;
  1597. return &dsaddr->id_node;
  1598. }
  1599. static void
  1600. ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
  1601. struct xdr_stream *xdr,
  1602. const struct nfs4_layoutreturn_args *args)
  1603. {
  1604. struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
  1605. __be32 *start;
  1606. dprintk("%s: Begin\n", __func__);
  1607. start = xdr_reserve_space(xdr, 4);
  1608. BUG_ON(!start);
  1609. ff_layout_encode_ioerr(flo, xdr, args);
  1610. ff_layout_encode_iostats(flo, xdr, args);
  1611. *start = cpu_to_be32((xdr->p - start - 1) * 4);
  1612. dprintk("%s: Return\n", __func__);
  1613. }
  1614. static int
  1615. ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
  1616. {
  1617. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  1618. return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
  1619. }
  1620. static size_t
  1621. ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
  1622. const int buflen)
  1623. {
  1624. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  1625. const struct in6_addr *addr = &sin6->sin6_addr;
  1626. /*
  1627. * RFC 4291, Section 2.2.2
  1628. *
  1629. * Shorthanded ANY address
  1630. */
  1631. if (ipv6_addr_any(addr))
  1632. return snprintf(buf, buflen, "::");
  1633. /*
  1634. * RFC 4291, Section 2.2.2
  1635. *
  1636. * Shorthanded loopback address
  1637. */
  1638. if (ipv6_addr_loopback(addr))
  1639. return snprintf(buf, buflen, "::1");
  1640. /*
  1641. * RFC 4291, Section 2.2.3
  1642. *
  1643. * Special presentation address format for mapped v4
  1644. * addresses.
  1645. */
  1646. if (ipv6_addr_v4mapped(addr))
  1647. return snprintf(buf, buflen, "::ffff:%pI4",
  1648. &addr->s6_addr32[3]);
  1649. /*
  1650. * RFC 4291, Section 2.2.1
  1651. */
  1652. return snprintf(buf, buflen, "%pI6c", addr);
  1653. }
  1654. /* Derived from rpc_sockaddr2uaddr */
  1655. static void
  1656. ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
  1657. {
  1658. struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
  1659. char portbuf[RPCBIND_MAXUADDRPLEN];
  1660. char addrbuf[RPCBIND_MAXUADDRLEN];
  1661. char *netid;
  1662. unsigned short port;
  1663. int len, netid_len;
  1664. __be32 *p;
  1665. switch (sap->sa_family) {
  1666. case AF_INET:
  1667. if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
  1668. return;
  1669. port = ntohs(((struct sockaddr_in *)sap)->sin_port);
  1670. netid = "tcp";
  1671. netid_len = 3;
  1672. break;
  1673. case AF_INET6:
  1674. if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
  1675. return;
  1676. port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
  1677. netid = "tcp6";
  1678. netid_len = 4;
  1679. break;
  1680. default:
  1681. /* we only support tcp and tcp6 */
  1682. WARN_ON_ONCE(1);
  1683. return;
  1684. }
  1685. snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
  1686. len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
  1687. p = xdr_reserve_space(xdr, 4 + netid_len);
  1688. xdr_encode_opaque(p, netid, netid_len);
  1689. p = xdr_reserve_space(xdr, 4 + len);
  1690. xdr_encode_opaque(p, addrbuf, len);
  1691. }
  1692. static void
  1693. ff_layout_encode_nfstime(struct xdr_stream *xdr,
  1694. ktime_t t)
  1695. {
  1696. struct timespec64 ts;
  1697. __be32 *p;
  1698. p = xdr_reserve_space(xdr, 12);
  1699. ts = ktime_to_timespec64(t);
  1700. p = xdr_encode_hyper(p, ts.tv_sec);
  1701. *p++ = cpu_to_be32(ts.tv_nsec);
  1702. }
  1703. static void
  1704. ff_layout_encode_io_latency(struct xdr_stream *xdr,
  1705. struct nfs4_ff_io_stat *stat)
  1706. {
  1707. __be32 *p;
  1708. p = xdr_reserve_space(xdr, 5 * 8);
  1709. p = xdr_encode_hyper(p, stat->ops_requested);
  1710. p = xdr_encode_hyper(p, stat->bytes_requested);
  1711. p = xdr_encode_hyper(p, stat->ops_completed);
  1712. p = xdr_encode_hyper(p, stat->bytes_completed);
  1713. p = xdr_encode_hyper(p, stat->bytes_not_delivered);
  1714. ff_layout_encode_nfstime(xdr, stat->total_busy_time);
  1715. ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
  1716. }
  1717. static void
  1718. ff_layout_encode_layoutstats(struct xdr_stream *xdr,
  1719. struct nfs42_layoutstat_args *args,
  1720. struct nfs42_layoutstat_devinfo *devinfo)
  1721. {
  1722. struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
  1723. struct nfs4_pnfs_ds_addr *da;
  1724. struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
  1725. struct nfs_fh *fh = &mirror->fh_versions[0];
  1726. __be32 *p, *start;
  1727. da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
  1728. dprintk("%s: DS %s: encoding address %s\n",
  1729. __func__, ds->ds_remotestr, da->da_remotestr);
  1730. /* layoutupdate length */
  1731. start = xdr_reserve_space(xdr, 4);
  1732. /* netaddr4 */
  1733. ff_layout_encode_netaddr(xdr, da);
  1734. /* nfs_fh4 */
  1735. p = xdr_reserve_space(xdr, 4 + fh->size);
  1736. xdr_encode_opaque(p, fh->data, fh->size);
  1737. /* ff_io_latency4 read */
  1738. spin_lock(&mirror->lock);
  1739. ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
  1740. /* ff_io_latency4 write */
  1741. ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
  1742. spin_unlock(&mirror->lock);
  1743. /* nfstime4 */
  1744. ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
  1745. /* bool */
  1746. p = xdr_reserve_space(xdr, 4);
  1747. *p = cpu_to_be32(false);
  1748. *start = cpu_to_be32((xdr->p - start - 1) * 4);
  1749. }
  1750. static int
  1751. ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
  1752. struct pnfs_layout_hdr *lo,
  1753. int dev_limit)
  1754. {
  1755. struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
  1756. struct nfs4_ff_layout_mirror *mirror;
  1757. struct nfs4_deviceid_node *dev;
  1758. struct nfs42_layoutstat_devinfo *devinfo;
  1759. int i = 0;
  1760. list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
  1761. if (i >= dev_limit)
  1762. break;
  1763. if (!mirror->mirror_ds)
  1764. continue;
  1765. /* mirror refcount put in cleanup_layoutstats */
  1766. if (!atomic_inc_not_zero(&mirror->ref))
  1767. continue;
  1768. dev = &mirror->mirror_ds->id_node;
  1769. devinfo = &args->devinfo[i];
  1770. memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
  1771. devinfo->offset = 0;
  1772. devinfo->length = NFS4_MAX_UINT64;
  1773. devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
  1774. devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
  1775. devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
  1776. devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
  1777. devinfo->layout_type = LAYOUT_FLEX_FILES;
  1778. devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
  1779. devinfo->layout_private = mirror;
  1780. i++;
  1781. }
  1782. return i;
  1783. }
  1784. static int
  1785. ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
  1786. {
  1787. struct nfs4_flexfile_layout *ff_layout;
  1788. struct nfs4_ff_layout_mirror *mirror;
  1789. int dev_count = 0;
  1790. spin_lock(&args->inode->i_lock);
  1791. ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
  1792. list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
  1793. if (atomic_read(&mirror->ref) != 0)
  1794. dev_count ++;
  1795. }
  1796. spin_unlock(&args->inode->i_lock);
  1797. /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
  1798. if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
  1799. dprintk("%s: truncating devinfo to limit (%d:%d)\n",
  1800. __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
  1801. dev_count = PNFS_LAYOUTSTATS_MAXDEV;
  1802. }
  1803. args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
  1804. if (!args->devinfo)
  1805. return -ENOMEM;
  1806. spin_lock(&args->inode->i_lock);
  1807. args->num_dev = ff_layout_mirror_prepare_stats(args,
  1808. &ff_layout->generic_hdr, dev_count);
  1809. spin_unlock(&args->inode->i_lock);
  1810. return 0;
  1811. }
  1812. static void
  1813. ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
  1814. {
  1815. struct nfs4_ff_layout_mirror *mirror;
  1816. int i;
  1817. for (i = 0; i < data->args.num_dev; i++) {
  1818. mirror = data->args.devinfo[i].layout_private;
  1819. data->args.devinfo[i].layout_private = NULL;
  1820. ff_layout_put_mirror(mirror);
  1821. }
  1822. }
  1823. static struct pnfs_layoutdriver_type flexfilelayout_type = {
  1824. .id = LAYOUT_FLEX_FILES,
  1825. .name = "LAYOUT_FLEX_FILES",
  1826. .owner = THIS_MODULE,
  1827. .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
  1828. .free_layout_hdr = ff_layout_free_layout_hdr,
  1829. .alloc_lseg = ff_layout_alloc_lseg,
  1830. .free_lseg = ff_layout_free_lseg,
  1831. .add_lseg = ff_layout_add_lseg,
  1832. .pg_read_ops = &ff_layout_pg_read_ops,
  1833. .pg_write_ops = &ff_layout_pg_write_ops,
  1834. .get_ds_info = ff_layout_get_ds_info,
  1835. .free_deviceid_node = ff_layout_free_deviceid_node,
  1836. .mark_request_commit = pnfs_layout_mark_request_commit,
  1837. .clear_request_commit = pnfs_generic_clear_request_commit,
  1838. .scan_commit_lists = pnfs_generic_scan_commit_lists,
  1839. .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
  1840. .commit_pagelist = ff_layout_commit_pagelist,
  1841. .read_pagelist = ff_layout_read_pagelist,
  1842. .write_pagelist = ff_layout_write_pagelist,
  1843. .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
  1844. .encode_layoutreturn = ff_layout_encode_layoutreturn,
  1845. .sync = pnfs_nfs_generic_sync,
  1846. .prepare_layoutstats = ff_layout_prepare_layoutstats,
  1847. .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
  1848. };
  1849. static int __init nfs4flexfilelayout_init(void)
  1850. {
  1851. printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
  1852. __func__);
  1853. return pnfs_register_layoutdriver(&flexfilelayout_type);
  1854. }
  1855. static void __exit nfs4flexfilelayout_exit(void)
  1856. {
  1857. printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
  1858. __func__);
  1859. pnfs_unregister_layoutdriver(&flexfilelayout_type);
  1860. }
  1861. MODULE_ALIAS("nfs-layouttype4-4");
  1862. MODULE_LICENSE("GPL");
  1863. MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
  1864. module_init(nfs4flexfilelayout_init);
  1865. module_exit(nfs4flexfilelayout_exit);