flexfilelayout.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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. p = xdr_inline_decode(&stream, 4);
  399. if (!p)
  400. goto out_err_free;
  401. fh_count = be32_to_cpup(p);
  402. fls->mirror_array[i]->fh_versions =
  403. kzalloc(fh_count * sizeof(struct nfs_fh),
  404. gfp_flags);
  405. if (fls->mirror_array[i]->fh_versions == NULL) {
  406. rc = -ENOMEM;
  407. goto out_err_free;
  408. }
  409. for (j = 0; j < fh_count; j++) {
  410. rc = decode_nfs_fh(&stream,
  411. &fls->mirror_array[i]->fh_versions[j]);
  412. if (rc)
  413. goto out_err_free;
  414. }
  415. fls->mirror_array[i]->fh_versions_cnt = fh_count;
  416. /* user */
  417. rc = decode_name(&stream, &fls->mirror_array[i]->uid);
  418. if (rc)
  419. goto out_err_free;
  420. /* group */
  421. rc = decode_name(&stream, &fls->mirror_array[i]->gid);
  422. if (rc)
  423. goto out_err_free;
  424. mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]);
  425. if (mirror != fls->mirror_array[i]) {
  426. ff_layout_free_mirror(fls->mirror_array[i]);
  427. fls->mirror_array[i] = mirror;
  428. }
  429. dprintk("%s: uid %d gid %d\n", __func__,
  430. fls->mirror_array[i]->uid,
  431. fls->mirror_array[i]->gid);
  432. }
  433. p = xdr_inline_decode(&stream, 4);
  434. if (!p)
  435. goto out_sort_mirrors;
  436. fls->flags = be32_to_cpup(p);
  437. p = xdr_inline_decode(&stream, 4);
  438. if (!p)
  439. goto out_sort_mirrors;
  440. for (i=0; i < fls->mirror_array_cnt; i++)
  441. fls->mirror_array[i]->report_interval = be32_to_cpup(p);
  442. out_sort_mirrors:
  443. ff_layout_sort_mirrors(fls);
  444. rc = ff_layout_check_layout(lgr);
  445. if (rc)
  446. goto out_err_free;
  447. ff_layout_mark_devices_valid(fls);
  448. ret = &fls->generic_hdr;
  449. dprintk("<-- %s (success)\n", __func__);
  450. out_free_page:
  451. __free_page(scratch);
  452. return ret;
  453. out_err_free:
  454. _ff_layout_free_lseg(fls);
  455. ret = ERR_PTR(rc);
  456. dprintk("<-- %s (%d)\n", __func__, rc);
  457. goto out_free_page;
  458. }
  459. static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout)
  460. {
  461. struct pnfs_layout_segment *lseg;
  462. list_for_each_entry(lseg, &layout->plh_segs, pls_list)
  463. if (lseg->pls_range.iomode == IOMODE_RW)
  464. return true;
  465. return false;
  466. }
  467. static void
  468. ff_layout_free_lseg(struct pnfs_layout_segment *lseg)
  469. {
  470. struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
  471. dprintk("--> %s\n", __func__);
  472. if (lseg->pls_range.iomode == IOMODE_RW) {
  473. struct nfs4_flexfile_layout *ffl;
  474. struct inode *inode;
  475. ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout);
  476. inode = ffl->generic_hdr.plh_inode;
  477. spin_lock(&inode->i_lock);
  478. if (!ff_layout_has_rw_segments(lseg->pls_layout)) {
  479. ffl->commit_info.nbuckets = 0;
  480. kfree(ffl->commit_info.buckets);
  481. ffl->commit_info.buckets = NULL;
  482. }
  483. spin_unlock(&inode->i_lock);
  484. }
  485. _ff_layout_free_lseg(fls);
  486. }
  487. /* Return 1 until we have multiple lsegs support */
  488. static int
  489. ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls)
  490. {
  491. return 1;
  492. }
  493. static void
  494. nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
  495. {
  496. /* first IO request? */
  497. if (atomic_inc_return(&timer->n_ops) == 1) {
  498. timer->start_time = now;
  499. }
  500. }
  501. static ktime_t
  502. nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
  503. {
  504. ktime_t start;
  505. if (atomic_dec_return(&timer->n_ops) < 0)
  506. WARN_ON_ONCE(1);
  507. start = timer->start_time;
  508. timer->start_time = now;
  509. return ktime_sub(now, start);
  510. }
  511. static bool
  512. nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror,
  513. struct nfs4_ff_layoutstat *layoutstat,
  514. ktime_t now)
  515. {
  516. static const ktime_t notime = {0};
  517. s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL;
  518. nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now);
  519. if (ktime_equal(mirror->start_time, notime))
  520. mirror->start_time = now;
  521. if (ktime_equal(mirror->last_report_time, notime))
  522. mirror->last_report_time = now;
  523. if (mirror->report_interval != 0)
  524. report_interval = (s64)mirror->report_interval * 1000LL;
  525. else if (layoutstats_timer != 0)
  526. report_interval = (s64)layoutstats_timer * 1000LL;
  527. if (ktime_to_ms(ktime_sub(now, mirror->last_report_time)) >=
  528. report_interval) {
  529. mirror->last_report_time = now;
  530. return true;
  531. }
  532. return false;
  533. }
  534. static void
  535. nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
  536. __u64 requested)
  537. {
  538. struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
  539. iostat->ops_requested++;
  540. iostat->bytes_requested += requested;
  541. }
  542. static void
  543. nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
  544. __u64 requested,
  545. __u64 completed,
  546. ktime_t time_completed,
  547. ktime_t time_started)
  548. {
  549. struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
  550. ktime_t completion_time = ktime_sub(time_completed, time_started);
  551. ktime_t timer;
  552. iostat->ops_completed++;
  553. iostat->bytes_completed += completed;
  554. iostat->bytes_not_delivered += requested - completed;
  555. timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed);
  556. iostat->total_busy_time =
  557. ktime_add(iostat->total_busy_time, timer);
  558. iostat->aggregate_completion_time =
  559. ktime_add(iostat->aggregate_completion_time,
  560. completion_time);
  561. }
  562. static void
  563. nfs4_ff_layout_stat_io_start_read(struct inode *inode,
  564. struct nfs4_ff_layout_mirror *mirror,
  565. __u64 requested, ktime_t now)
  566. {
  567. bool report;
  568. spin_lock(&mirror->lock);
  569. report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now);
  570. nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
  571. spin_unlock(&mirror->lock);
  572. if (report)
  573. pnfs_report_layoutstat(inode, GFP_KERNEL);
  574. }
  575. static void
  576. nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
  577. struct nfs4_ff_layout_mirror *mirror,
  578. __u64 requested,
  579. __u64 completed)
  580. {
  581. spin_lock(&mirror->lock);
  582. nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
  583. requested, completed,
  584. ktime_get(), task->tk_start);
  585. spin_unlock(&mirror->lock);
  586. }
  587. static void
  588. nfs4_ff_layout_stat_io_start_write(struct inode *inode,
  589. struct nfs4_ff_layout_mirror *mirror,
  590. __u64 requested, ktime_t now)
  591. {
  592. bool report;
  593. spin_lock(&mirror->lock);
  594. report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now);
  595. nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
  596. spin_unlock(&mirror->lock);
  597. if (report)
  598. pnfs_report_layoutstat(inode, GFP_NOIO);
  599. }
  600. static void
  601. nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
  602. struct nfs4_ff_layout_mirror *mirror,
  603. __u64 requested,
  604. __u64 completed,
  605. enum nfs3_stable_how committed)
  606. {
  607. if (committed == NFS_UNSTABLE)
  608. requested = completed = 0;
  609. spin_lock(&mirror->lock);
  610. nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
  611. requested, completed, ktime_get(), task->tk_start);
  612. spin_unlock(&mirror->lock);
  613. }
  614. static int
  615. ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
  616. struct nfs_commit_info *cinfo,
  617. gfp_t gfp_flags)
  618. {
  619. struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
  620. struct pnfs_commit_bucket *buckets;
  621. int size;
  622. if (cinfo->ds->nbuckets != 0) {
  623. /* This assumes there is only one RW lseg per file.
  624. * To support multiple lseg per file, we need to
  625. * change struct pnfs_commit_bucket to allow dynamic
  626. * increasing nbuckets.
  627. */
  628. return 0;
  629. }
  630. size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
  631. buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
  632. gfp_flags);
  633. if (!buckets)
  634. return -ENOMEM;
  635. else {
  636. int i;
  637. spin_lock(cinfo->lock);
  638. if (cinfo->ds->nbuckets != 0)
  639. kfree(buckets);
  640. else {
  641. cinfo->ds->buckets = buckets;
  642. cinfo->ds->nbuckets = size;
  643. for (i = 0; i < size; i++) {
  644. INIT_LIST_HEAD(&buckets[i].written);
  645. INIT_LIST_HEAD(&buckets[i].committing);
  646. /* mark direct verifier as unset */
  647. buckets[i].direct_verf.committed =
  648. NFS_INVALID_STABLE_HOW;
  649. }
  650. }
  651. spin_unlock(cinfo->lock);
  652. return 0;
  653. }
  654. }
  655. static struct nfs4_pnfs_ds *
  656. ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg,
  657. int start_idx,
  658. int *best_idx)
  659. {
  660. struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
  661. struct nfs4_pnfs_ds *ds;
  662. int idx;
  663. /* mirrors are sorted by efficiency */
  664. for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) {
  665. ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
  666. if (ds) {
  667. *best_idx = idx;
  668. return ds;
  669. }
  670. }
  671. return NULL;
  672. }
  673. static void
  674. ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
  675. struct nfs_page *req)
  676. {
  677. struct nfs_pgio_mirror *pgm;
  678. struct nfs4_ff_layout_mirror *mirror;
  679. struct nfs4_pnfs_ds *ds;
  680. int ds_idx;
  681. /* Use full layout for now */
  682. if (!pgio->pg_lseg) {
  683. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  684. req->wb_context,
  685. 0,
  686. NFS4_MAX_UINT64,
  687. IOMODE_READ,
  688. GFP_KERNEL);
  689. if (IS_ERR(pgio->pg_lseg)) {
  690. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  691. pgio->pg_lseg = NULL;
  692. return;
  693. }
  694. }
  695. /* If no lseg, fall back to read through mds */
  696. if (pgio->pg_lseg == NULL)
  697. goto out_mds;
  698. ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
  699. if (!ds)
  700. goto out_mds;
  701. mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
  702. pgio->pg_mirror_idx = ds_idx;
  703. /* read always uses only one mirror - idx 0 for pgio layer */
  704. pgm = &pgio->pg_mirrors[0];
  705. pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
  706. return;
  707. out_mds:
  708. pnfs_put_lseg(pgio->pg_lseg);
  709. pgio->pg_lseg = NULL;
  710. nfs_pageio_reset_read_mds(pgio);
  711. }
  712. static void
  713. ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
  714. struct nfs_page *req)
  715. {
  716. struct nfs4_ff_layout_mirror *mirror;
  717. struct nfs_pgio_mirror *pgm;
  718. struct nfs_commit_info cinfo;
  719. struct nfs4_pnfs_ds *ds;
  720. int i;
  721. int status;
  722. if (!pgio->pg_lseg) {
  723. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  724. req->wb_context,
  725. 0,
  726. NFS4_MAX_UINT64,
  727. IOMODE_RW,
  728. GFP_NOFS);
  729. if (IS_ERR(pgio->pg_lseg)) {
  730. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  731. pgio->pg_lseg = NULL;
  732. return;
  733. }
  734. }
  735. /* If no lseg, fall back to write through mds */
  736. if (pgio->pg_lseg == NULL)
  737. goto out_mds;
  738. nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
  739. status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
  740. if (status < 0)
  741. goto out_mds;
  742. /* Use a direct mapping of ds_idx to pgio mirror_idx */
  743. if (WARN_ON_ONCE(pgio->pg_mirror_count !=
  744. FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
  745. goto out_mds;
  746. for (i = 0; i < pgio->pg_mirror_count; i++) {
  747. ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
  748. if (!ds)
  749. goto out_mds;
  750. pgm = &pgio->pg_mirrors[i];
  751. mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
  752. pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
  753. }
  754. return;
  755. out_mds:
  756. pnfs_put_lseg(pgio->pg_lseg);
  757. pgio->pg_lseg = NULL;
  758. nfs_pageio_reset_write_mds(pgio);
  759. }
  760. static unsigned int
  761. ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
  762. struct nfs_page *req)
  763. {
  764. if (!pgio->pg_lseg) {
  765. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  766. req->wb_context,
  767. 0,
  768. NFS4_MAX_UINT64,
  769. IOMODE_RW,
  770. GFP_NOFS);
  771. if (IS_ERR(pgio->pg_lseg)) {
  772. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  773. pgio->pg_lseg = NULL;
  774. goto out;
  775. }
  776. }
  777. if (pgio->pg_lseg)
  778. return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
  779. /* no lseg means that pnfs is not in use, so no mirroring here */
  780. nfs_pageio_reset_write_mds(pgio);
  781. out:
  782. return 1;
  783. }
  784. static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
  785. .pg_init = ff_layout_pg_init_read,
  786. .pg_test = pnfs_generic_pg_test,
  787. .pg_doio = pnfs_generic_pg_readpages,
  788. .pg_cleanup = pnfs_generic_pg_cleanup,
  789. };
  790. static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
  791. .pg_init = ff_layout_pg_init_write,
  792. .pg_test = pnfs_generic_pg_test,
  793. .pg_doio = pnfs_generic_pg_writepages,
  794. .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
  795. .pg_cleanup = pnfs_generic_pg_cleanup,
  796. };
  797. static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
  798. {
  799. struct rpc_task *task = &hdr->task;
  800. pnfs_layoutcommit_inode(hdr->inode, false);
  801. if (retry_pnfs) {
  802. dprintk("%s Reset task %5u for i/o through pNFS "
  803. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  804. hdr->task.tk_pid,
  805. hdr->inode->i_sb->s_id,
  806. (unsigned long long)NFS_FILEID(hdr->inode),
  807. hdr->args.count,
  808. (unsigned long long)hdr->args.offset);
  809. hdr->completion_ops->reschedule_io(hdr);
  810. return;
  811. }
  812. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  813. dprintk("%s Reset task %5u for i/o through MDS "
  814. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  815. hdr->task.tk_pid,
  816. hdr->inode->i_sb->s_id,
  817. (unsigned long long)NFS_FILEID(hdr->inode),
  818. hdr->args.count,
  819. (unsigned long long)hdr->args.offset);
  820. task->tk_status = pnfs_write_done_resend_to_mds(hdr);
  821. }
  822. }
  823. static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
  824. {
  825. struct rpc_task *task = &hdr->task;
  826. pnfs_layoutcommit_inode(hdr->inode, false);
  827. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  828. dprintk("%s Reset task %5u for i/o through MDS "
  829. "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
  830. hdr->task.tk_pid,
  831. hdr->inode->i_sb->s_id,
  832. (unsigned long long)NFS_FILEID(hdr->inode),
  833. hdr->args.count,
  834. (unsigned long long)hdr->args.offset);
  835. task->tk_status = pnfs_read_done_resend_to_mds(hdr);
  836. }
  837. }
  838. static int ff_layout_async_handle_error_v4(struct rpc_task *task,
  839. struct nfs4_state *state,
  840. struct nfs_client *clp,
  841. struct pnfs_layout_segment *lseg,
  842. int idx)
  843. {
  844. struct pnfs_layout_hdr *lo = lseg->pls_layout;
  845. struct inode *inode = lo->plh_inode;
  846. struct nfs_server *mds_server = NFS_SERVER(inode);
  847. struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
  848. struct nfs_client *mds_client = mds_server->nfs_client;
  849. struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
  850. if (task->tk_status >= 0)
  851. return 0;
  852. switch (task->tk_status) {
  853. /* MDS state errors */
  854. case -NFS4ERR_DELEG_REVOKED:
  855. case -NFS4ERR_ADMIN_REVOKED:
  856. case -NFS4ERR_BAD_STATEID:
  857. if (state == NULL)
  858. break;
  859. nfs_remove_bad_delegation(state->inode);
  860. case -NFS4ERR_OPENMODE:
  861. if (state == NULL)
  862. break;
  863. if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
  864. goto out_bad_stateid;
  865. goto wait_on_recovery;
  866. case -NFS4ERR_EXPIRED:
  867. if (state != NULL) {
  868. if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
  869. goto out_bad_stateid;
  870. }
  871. nfs4_schedule_lease_recovery(mds_client);
  872. goto wait_on_recovery;
  873. /* DS session errors */
  874. case -NFS4ERR_BADSESSION:
  875. case -NFS4ERR_BADSLOT:
  876. case -NFS4ERR_BAD_HIGH_SLOT:
  877. case -NFS4ERR_DEADSESSION:
  878. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  879. case -NFS4ERR_SEQ_FALSE_RETRY:
  880. case -NFS4ERR_SEQ_MISORDERED:
  881. dprintk("%s ERROR %d, Reset session. Exchangeid "
  882. "flags 0x%x\n", __func__, task->tk_status,
  883. clp->cl_exchange_flags);
  884. nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
  885. break;
  886. case -NFS4ERR_DELAY:
  887. case -NFS4ERR_GRACE:
  888. rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
  889. break;
  890. case -NFS4ERR_RETRY_UNCACHED_REP:
  891. break;
  892. /* Invalidate Layout errors */
  893. case -NFS4ERR_PNFS_NO_LAYOUT:
  894. case -ESTALE: /* mapped NFS4ERR_STALE */
  895. case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
  896. case -EISDIR: /* mapped NFS4ERR_ISDIR */
  897. case -NFS4ERR_FHEXPIRED:
  898. case -NFS4ERR_WRONG_TYPE:
  899. dprintk("%s Invalid layout error %d\n", __func__,
  900. task->tk_status);
  901. /*
  902. * Destroy layout so new i/o will get a new layout.
  903. * Layout will not be destroyed until all current lseg
  904. * references are put. Mark layout as invalid to resend failed
  905. * i/o and all i/o waiting on the slot table to the MDS until
  906. * layout is destroyed and a new valid layout is obtained.
  907. */
  908. pnfs_destroy_layout(NFS_I(inode));
  909. rpc_wake_up(&tbl->slot_tbl_waitq);
  910. goto reset;
  911. /* RPC connection errors */
  912. case -ECONNREFUSED:
  913. case -EHOSTDOWN:
  914. case -EHOSTUNREACH:
  915. case -ENETUNREACH:
  916. case -EIO:
  917. case -ETIMEDOUT:
  918. case -EPIPE:
  919. dprintk("%s DS connection error %d\n", __func__,
  920. task->tk_status);
  921. nfs4_mark_deviceid_unavailable(devid);
  922. rpc_wake_up(&tbl->slot_tbl_waitq);
  923. /* fall through */
  924. default:
  925. if (ff_layout_no_fallback_to_mds(lseg) ||
  926. ff_layout_has_available_ds(lseg))
  927. return -NFS4ERR_RESET_TO_PNFS;
  928. reset:
  929. dprintk("%s Retry through MDS. Error %d\n", __func__,
  930. task->tk_status);
  931. return -NFS4ERR_RESET_TO_MDS;
  932. }
  933. out:
  934. task->tk_status = 0;
  935. return -EAGAIN;
  936. out_bad_stateid:
  937. task->tk_status = -EIO;
  938. return 0;
  939. wait_on_recovery:
  940. rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
  941. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
  942. rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
  943. goto out;
  944. }
  945. /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
  946. static int ff_layout_async_handle_error_v3(struct rpc_task *task,
  947. struct pnfs_layout_segment *lseg,
  948. int idx)
  949. {
  950. struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
  951. if (task->tk_status >= 0)
  952. return 0;
  953. switch (task->tk_status) {
  954. /* File access problems. Don't mark the device as unavailable */
  955. case -EACCES:
  956. case -ESTALE:
  957. case -EISDIR:
  958. case -EBADHANDLE:
  959. case -ELOOP:
  960. case -ENOSPC:
  961. break;
  962. case -EJUKEBOX:
  963. nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
  964. goto out_retry;
  965. default:
  966. dprintk("%s DS connection error %d\n", __func__,
  967. task->tk_status);
  968. nfs4_mark_deviceid_unavailable(devid);
  969. }
  970. /* FIXME: Need to prevent infinite looping here. */
  971. return -NFS4ERR_RESET_TO_PNFS;
  972. out_retry:
  973. task->tk_status = 0;
  974. rpc_restart_call_prepare(task);
  975. rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
  976. return -EAGAIN;
  977. }
  978. static int ff_layout_async_handle_error(struct rpc_task *task,
  979. struct nfs4_state *state,
  980. struct nfs_client *clp,
  981. struct pnfs_layout_segment *lseg,
  982. int idx)
  983. {
  984. int vers = clp->cl_nfs_mod->rpc_vers->number;
  985. switch (vers) {
  986. case 3:
  987. return ff_layout_async_handle_error_v3(task, lseg, idx);
  988. case 4:
  989. return ff_layout_async_handle_error_v4(task, state, clp,
  990. lseg, idx);
  991. default:
  992. /* should never happen */
  993. WARN_ON_ONCE(1);
  994. return 0;
  995. }
  996. }
  997. static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
  998. int idx, u64 offset, u64 length,
  999. u32 status, int opnum, int error)
  1000. {
  1001. struct nfs4_ff_layout_mirror *mirror;
  1002. int err;
  1003. if (status == 0) {
  1004. switch (error) {
  1005. case -ETIMEDOUT:
  1006. case -EPFNOSUPPORT:
  1007. case -EPROTONOSUPPORT:
  1008. case -EOPNOTSUPP:
  1009. case -ECONNREFUSED:
  1010. case -ECONNRESET:
  1011. case -EHOSTDOWN:
  1012. case -EHOSTUNREACH:
  1013. case -ENETUNREACH:
  1014. case -EADDRINUSE:
  1015. case -ENOBUFS:
  1016. case -EPIPE:
  1017. case -EPERM:
  1018. status = NFS4ERR_NXIO;
  1019. break;
  1020. case -EACCES:
  1021. status = NFS4ERR_ACCESS;
  1022. break;
  1023. default:
  1024. return;
  1025. }
  1026. }
  1027. switch (status) {
  1028. case NFS4ERR_DELAY:
  1029. case NFS4ERR_GRACE:
  1030. return;
  1031. default:
  1032. break;
  1033. }
  1034. mirror = FF_LAYOUT_COMP(lseg, idx);
  1035. err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
  1036. mirror, offset, length, status, opnum,
  1037. GFP_NOIO);
  1038. pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
  1039. dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
  1040. }
  1041. /* NFS_PROTO call done callback routines */
  1042. static int ff_layout_read_done_cb(struct rpc_task *task,
  1043. struct nfs_pgio_header *hdr)
  1044. {
  1045. int err;
  1046. trace_nfs4_pnfs_read(hdr, task->tk_status);
  1047. if (task->tk_status < 0)
  1048. ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
  1049. hdr->args.offset, hdr->args.count,
  1050. hdr->res.op_status, OP_READ,
  1051. task->tk_status);
  1052. err = ff_layout_async_handle_error(task, hdr->args.context->state,
  1053. hdr->ds_clp, hdr->lseg,
  1054. hdr->pgio_mirror_idx);
  1055. switch (err) {
  1056. case -NFS4ERR_RESET_TO_PNFS:
  1057. if (ff_layout_choose_best_ds_for_read(hdr->lseg,
  1058. hdr->pgio_mirror_idx + 1,
  1059. &hdr->pgio_mirror_idx))
  1060. goto out_eagain;
  1061. set_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
  1062. &hdr->lseg->pls_layout->plh_flags);
  1063. pnfs_read_resend_pnfs(hdr);
  1064. return task->tk_status;
  1065. case -NFS4ERR_RESET_TO_MDS:
  1066. ff_layout_reset_read(hdr);
  1067. return task->tk_status;
  1068. case -EAGAIN:
  1069. goto out_eagain;
  1070. }
  1071. return 0;
  1072. out_eagain:
  1073. rpc_restart_call_prepare(task);
  1074. return -EAGAIN;
  1075. }
  1076. static bool
  1077. ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
  1078. {
  1079. return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
  1080. }
  1081. /*
  1082. * We reference the rpc_cred of the first WRITE that triggers the need for
  1083. * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
  1084. * rfc5661 is not clear about which credential should be used.
  1085. *
  1086. * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
  1087. * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
  1088. * we always send layoutcommit after DS writes.
  1089. */
  1090. static void
  1091. ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr)
  1092. {
  1093. if (!ff_layout_need_layoutcommit(hdr->lseg))
  1094. return;
  1095. pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
  1096. hdr->mds_offset + hdr->res.count);
  1097. dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
  1098. (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
  1099. }
  1100. static bool
  1101. ff_layout_reset_to_mds(struct pnfs_layout_segment *lseg, int idx)
  1102. {
  1103. /* No mirroring for now */
  1104. struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
  1105. return ff_layout_test_devid_unavailable(node);
  1106. }
  1107. static void ff_layout_read_record_layoutstats_start(struct rpc_task *task,
  1108. struct nfs_pgio_header *hdr)
  1109. {
  1110. if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
  1111. return;
  1112. nfs4_ff_layout_stat_io_start_read(hdr->inode,
  1113. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1114. hdr->args.count,
  1115. task->tk_start);
  1116. }
  1117. static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
  1118. struct nfs_pgio_header *hdr)
  1119. {
  1120. if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
  1121. return;
  1122. nfs4_ff_layout_stat_io_end_read(task,
  1123. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1124. hdr->args.count,
  1125. hdr->res.count);
  1126. }
  1127. static int ff_layout_read_prepare_common(struct rpc_task *task,
  1128. struct nfs_pgio_header *hdr)
  1129. {
  1130. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  1131. rpc_exit(task, -EIO);
  1132. return -EIO;
  1133. }
  1134. if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
  1135. dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
  1136. if (ff_layout_has_available_ds(hdr->lseg))
  1137. pnfs_read_resend_pnfs(hdr);
  1138. else
  1139. ff_layout_reset_read(hdr);
  1140. rpc_exit(task, 0);
  1141. return -EAGAIN;
  1142. }
  1143. hdr->pgio_done_cb = ff_layout_read_done_cb;
  1144. ff_layout_read_record_layoutstats_start(task, hdr);
  1145. return 0;
  1146. }
  1147. /*
  1148. * Call ops for the async read/write cases
  1149. * In the case of dense layouts, the offset needs to be reset to its
  1150. * original value.
  1151. */
  1152. static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
  1153. {
  1154. struct nfs_pgio_header *hdr = data;
  1155. if (ff_layout_read_prepare_common(task, hdr))
  1156. return;
  1157. rpc_call_start(task);
  1158. }
  1159. static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
  1160. struct nfs4_sequence_args *args,
  1161. struct nfs4_sequence_res *res,
  1162. struct rpc_task *task)
  1163. {
  1164. if (ds_clp->cl_session)
  1165. return nfs41_setup_sequence(ds_clp->cl_session,
  1166. args,
  1167. res,
  1168. task);
  1169. return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
  1170. args,
  1171. res,
  1172. task);
  1173. }
  1174. static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
  1175. {
  1176. struct nfs_pgio_header *hdr = data;
  1177. if (ff_layout_setup_sequence(hdr->ds_clp,
  1178. &hdr->args.seq_args,
  1179. &hdr->res.seq_res,
  1180. task))
  1181. return;
  1182. if (ff_layout_read_prepare_common(task, hdr))
  1183. return;
  1184. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  1185. hdr->args.lock_context, FMODE_READ) == -EIO)
  1186. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  1187. }
  1188. static void ff_layout_read_call_done(struct rpc_task *task, void *data)
  1189. {
  1190. struct nfs_pgio_header *hdr = data;
  1191. dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
  1192. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  1193. task->tk_status == 0) {
  1194. nfs4_sequence_done(task, &hdr->res.seq_res);
  1195. return;
  1196. }
  1197. /* Note this may cause RPC to be resent */
  1198. hdr->mds_ops->rpc_call_done(task, hdr);
  1199. }
  1200. static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
  1201. {
  1202. struct nfs_pgio_header *hdr = data;
  1203. ff_layout_read_record_layoutstats_done(task, hdr);
  1204. rpc_count_iostats_metrics(task,
  1205. &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
  1206. }
  1207. static void ff_layout_read_release(void *data)
  1208. {
  1209. struct nfs_pgio_header *hdr = data;
  1210. ff_layout_read_record_layoutstats_done(&hdr->task, hdr);
  1211. pnfs_generic_rw_release(data);
  1212. }
  1213. static int ff_layout_write_done_cb(struct rpc_task *task,
  1214. struct nfs_pgio_header *hdr)
  1215. {
  1216. int err;
  1217. trace_nfs4_pnfs_write(hdr, task->tk_status);
  1218. if (task->tk_status < 0)
  1219. ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
  1220. hdr->args.offset, hdr->args.count,
  1221. hdr->res.op_status, OP_WRITE,
  1222. task->tk_status);
  1223. err = ff_layout_async_handle_error(task, hdr->args.context->state,
  1224. hdr->ds_clp, hdr->lseg,
  1225. hdr->pgio_mirror_idx);
  1226. switch (err) {
  1227. case -NFS4ERR_RESET_TO_PNFS:
  1228. ff_layout_reset_write(hdr, true);
  1229. return task->tk_status;
  1230. case -NFS4ERR_RESET_TO_MDS:
  1231. ff_layout_reset_write(hdr, false);
  1232. return task->tk_status;
  1233. case -EAGAIN:
  1234. return -EAGAIN;
  1235. }
  1236. if (hdr->res.verf->committed == NFS_FILE_SYNC ||
  1237. hdr->res.verf->committed == NFS_DATA_SYNC)
  1238. ff_layout_set_layoutcommit(hdr);
  1239. /* zero out fattr since we don't care DS attr at all */
  1240. hdr->fattr.valid = 0;
  1241. if (task->tk_status >= 0)
  1242. nfs_writeback_update_inode(hdr);
  1243. return 0;
  1244. }
  1245. static int ff_layout_commit_done_cb(struct rpc_task *task,
  1246. struct nfs_commit_data *data)
  1247. {
  1248. int err;
  1249. trace_nfs4_pnfs_commit_ds(data, task->tk_status);
  1250. if (task->tk_status < 0)
  1251. ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
  1252. data->args.offset, data->args.count,
  1253. data->res.op_status, OP_COMMIT,
  1254. task->tk_status);
  1255. err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
  1256. data->lseg, data->ds_commit_index);
  1257. switch (err) {
  1258. case -NFS4ERR_RESET_TO_PNFS:
  1259. pnfs_generic_prepare_to_resend_writes(data);
  1260. return -EAGAIN;
  1261. case -NFS4ERR_RESET_TO_MDS:
  1262. pnfs_generic_prepare_to_resend_writes(data);
  1263. return -EAGAIN;
  1264. case -EAGAIN:
  1265. rpc_restart_call_prepare(task);
  1266. return -EAGAIN;
  1267. }
  1268. if (data->verf.committed == NFS_UNSTABLE
  1269. && ff_layout_need_layoutcommit(data->lseg))
  1270. pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
  1271. return 0;
  1272. }
  1273. static void ff_layout_write_record_layoutstats_start(struct rpc_task *task,
  1274. struct nfs_pgio_header *hdr)
  1275. {
  1276. if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
  1277. return;
  1278. nfs4_ff_layout_stat_io_start_write(hdr->inode,
  1279. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1280. hdr->args.count,
  1281. task->tk_start);
  1282. }
  1283. static void ff_layout_write_record_layoutstats_done(struct rpc_task *task,
  1284. struct nfs_pgio_header *hdr)
  1285. {
  1286. if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
  1287. return;
  1288. nfs4_ff_layout_stat_io_end_write(task,
  1289. FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
  1290. hdr->args.count, hdr->res.count,
  1291. hdr->res.verf->committed);
  1292. }
  1293. static int ff_layout_write_prepare_common(struct rpc_task *task,
  1294. struct nfs_pgio_header *hdr)
  1295. {
  1296. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
  1297. rpc_exit(task, -EIO);
  1298. return -EIO;
  1299. }
  1300. if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
  1301. bool retry_pnfs;
  1302. retry_pnfs = ff_layout_has_available_ds(hdr->lseg);
  1303. dprintk("%s task %u reset io to %s\n", __func__,
  1304. task->tk_pid, retry_pnfs ? "pNFS" : "MDS");
  1305. ff_layout_reset_write(hdr, retry_pnfs);
  1306. rpc_exit(task, 0);
  1307. return -EAGAIN;
  1308. }
  1309. ff_layout_write_record_layoutstats_start(task, hdr);
  1310. return 0;
  1311. }
  1312. static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
  1313. {
  1314. struct nfs_pgio_header *hdr = data;
  1315. if (ff_layout_write_prepare_common(task, hdr))
  1316. return;
  1317. rpc_call_start(task);
  1318. }
  1319. static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
  1320. {
  1321. struct nfs_pgio_header *hdr = data;
  1322. if (ff_layout_setup_sequence(hdr->ds_clp,
  1323. &hdr->args.seq_args,
  1324. &hdr->res.seq_res,
  1325. task))
  1326. return;
  1327. if (ff_layout_write_prepare_common(task, hdr))
  1328. return;
  1329. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  1330. hdr->args.lock_context, FMODE_WRITE) == -EIO)
  1331. rpc_exit(task, -EIO); /* lost lock, terminate I/O */
  1332. }
  1333. static void ff_layout_write_call_done(struct rpc_task *task, void *data)
  1334. {
  1335. struct nfs_pgio_header *hdr = data;
  1336. if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
  1337. task->tk_status == 0) {
  1338. nfs4_sequence_done(task, &hdr->res.seq_res);
  1339. return;
  1340. }
  1341. /* Note this may cause RPC to be resent */
  1342. hdr->mds_ops->rpc_call_done(task, hdr);
  1343. }
  1344. static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
  1345. {
  1346. struct nfs_pgio_header *hdr = data;
  1347. ff_layout_write_record_layoutstats_done(task, hdr);
  1348. rpc_count_iostats_metrics(task,
  1349. &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
  1350. }
  1351. static void ff_layout_write_release(void *data)
  1352. {
  1353. struct nfs_pgio_header *hdr = data;
  1354. ff_layout_write_record_layoutstats_done(&hdr->task, hdr);
  1355. pnfs_generic_rw_release(data);
  1356. }
  1357. static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task,
  1358. struct nfs_commit_data *cdata)
  1359. {
  1360. if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags))
  1361. return;
  1362. nfs4_ff_layout_stat_io_start_write(cdata->inode,
  1363. FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
  1364. 0, task->tk_start);
  1365. }
  1366. static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task,
  1367. struct nfs_commit_data *cdata)
  1368. {
  1369. struct nfs_page *req;
  1370. __u64 count = 0;
  1371. if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags))
  1372. return;
  1373. if (task->tk_status == 0) {
  1374. list_for_each_entry(req, &cdata->pages, wb_list)
  1375. count += req->wb_bytes;
  1376. }
  1377. nfs4_ff_layout_stat_io_end_write(task,
  1378. FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
  1379. count, count, NFS_FILE_SYNC);
  1380. }
  1381. static void ff_layout_commit_prepare_common(struct rpc_task *task,
  1382. struct nfs_commit_data *cdata)
  1383. {
  1384. ff_layout_commit_record_layoutstats_start(task, cdata);
  1385. }
  1386. static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
  1387. {
  1388. ff_layout_commit_prepare_common(task, data);
  1389. rpc_call_start(task);
  1390. }
  1391. static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
  1392. {
  1393. struct nfs_commit_data *wdata = data;
  1394. if (ff_layout_setup_sequence(wdata->ds_clp,
  1395. &wdata->args.seq_args,
  1396. &wdata->res.seq_res,
  1397. task))
  1398. return;
  1399. ff_layout_commit_prepare_common(task, data);
  1400. }
  1401. static void ff_layout_commit_done(struct rpc_task *task, void *data)
  1402. {
  1403. pnfs_generic_write_commit_done(task, data);
  1404. }
  1405. static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
  1406. {
  1407. struct nfs_commit_data *cdata = data;
  1408. ff_layout_commit_record_layoutstats_done(task, cdata);
  1409. rpc_count_iostats_metrics(task,
  1410. &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
  1411. }
  1412. static void ff_layout_commit_release(void *data)
  1413. {
  1414. struct nfs_commit_data *cdata = data;
  1415. ff_layout_commit_record_layoutstats_done(&cdata->task, cdata);
  1416. pnfs_generic_commit_release(data);
  1417. }
  1418. static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
  1419. .rpc_call_prepare = ff_layout_read_prepare_v3,
  1420. .rpc_call_done = ff_layout_read_call_done,
  1421. .rpc_count_stats = ff_layout_read_count_stats,
  1422. .rpc_release = ff_layout_read_release,
  1423. };
  1424. static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
  1425. .rpc_call_prepare = ff_layout_read_prepare_v4,
  1426. .rpc_call_done = ff_layout_read_call_done,
  1427. .rpc_count_stats = ff_layout_read_count_stats,
  1428. .rpc_release = ff_layout_read_release,
  1429. };
  1430. static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
  1431. .rpc_call_prepare = ff_layout_write_prepare_v3,
  1432. .rpc_call_done = ff_layout_write_call_done,
  1433. .rpc_count_stats = ff_layout_write_count_stats,
  1434. .rpc_release = ff_layout_write_release,
  1435. };
  1436. static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
  1437. .rpc_call_prepare = ff_layout_write_prepare_v4,
  1438. .rpc_call_done = ff_layout_write_call_done,
  1439. .rpc_count_stats = ff_layout_write_count_stats,
  1440. .rpc_release = ff_layout_write_release,
  1441. };
  1442. static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
  1443. .rpc_call_prepare = ff_layout_commit_prepare_v3,
  1444. .rpc_call_done = ff_layout_commit_done,
  1445. .rpc_count_stats = ff_layout_commit_count_stats,
  1446. .rpc_release = ff_layout_commit_release,
  1447. };
  1448. static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
  1449. .rpc_call_prepare = ff_layout_commit_prepare_v4,
  1450. .rpc_call_done = ff_layout_commit_done,
  1451. .rpc_count_stats = ff_layout_commit_count_stats,
  1452. .rpc_release = ff_layout_commit_release,
  1453. };
  1454. static enum pnfs_try_status
  1455. ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
  1456. {
  1457. struct pnfs_layout_segment *lseg = hdr->lseg;
  1458. struct nfs4_pnfs_ds *ds;
  1459. struct rpc_clnt *ds_clnt;
  1460. struct rpc_cred *ds_cred;
  1461. loff_t offset = hdr->args.offset;
  1462. u32 idx = hdr->pgio_mirror_idx;
  1463. int vers;
  1464. struct nfs_fh *fh;
  1465. dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
  1466. __func__, hdr->inode->i_ino,
  1467. hdr->args.pgbase, (size_t)hdr->args.count, offset);
  1468. ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
  1469. if (!ds)
  1470. goto out_failed;
  1471. ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
  1472. hdr->inode);
  1473. if (IS_ERR(ds_clnt))
  1474. goto out_failed;
  1475. ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
  1476. if (IS_ERR(ds_cred))
  1477. goto out_failed;
  1478. vers = nfs4_ff_layout_ds_version(lseg, idx);
  1479. dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
  1480. ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
  1481. atomic_inc(&ds->ds_clp->cl_count);
  1482. hdr->ds_clp = ds->ds_clp;
  1483. fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
  1484. if (fh)
  1485. hdr->args.fh = fh;
  1486. /*
  1487. * Note that if we ever decide to split across DSes,
  1488. * then we may need to handle dense-like offsets.
  1489. */
  1490. hdr->args.offset = offset;
  1491. hdr->mds_offset = offset;
  1492. /* Perform an asynchronous read to ds */
  1493. nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
  1494. vers == 3 ? &ff_layout_read_call_ops_v3 :
  1495. &ff_layout_read_call_ops_v4,
  1496. 0, RPC_TASK_SOFTCONN);
  1497. return PNFS_ATTEMPTED;
  1498. out_failed:
  1499. if (ff_layout_has_available_ds(lseg))
  1500. return PNFS_TRY_AGAIN;
  1501. return PNFS_NOT_ATTEMPTED;
  1502. }
  1503. /* Perform async writes. */
  1504. static enum pnfs_try_status
  1505. ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
  1506. {
  1507. struct pnfs_layout_segment *lseg = hdr->lseg;
  1508. struct nfs4_pnfs_ds *ds;
  1509. struct rpc_clnt *ds_clnt;
  1510. struct rpc_cred *ds_cred;
  1511. loff_t offset = hdr->args.offset;
  1512. int vers;
  1513. struct nfs_fh *fh;
  1514. int idx = hdr->pgio_mirror_idx;
  1515. ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
  1516. if (!ds)
  1517. return PNFS_NOT_ATTEMPTED;
  1518. ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
  1519. hdr->inode);
  1520. if (IS_ERR(ds_clnt))
  1521. return PNFS_NOT_ATTEMPTED;
  1522. ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
  1523. if (IS_ERR(ds_cred))
  1524. return PNFS_NOT_ATTEMPTED;
  1525. vers = nfs4_ff_layout_ds_version(lseg, idx);
  1526. dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
  1527. __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
  1528. offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
  1529. vers);
  1530. hdr->pgio_done_cb = ff_layout_write_done_cb;
  1531. atomic_inc(&ds->ds_clp->cl_count);
  1532. hdr->ds_clp = ds->ds_clp;
  1533. hdr->ds_commit_idx = idx;
  1534. fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
  1535. if (fh)
  1536. hdr->args.fh = fh;
  1537. /*
  1538. * Note that if we ever decide to split across DSes,
  1539. * then we may need to handle dense-like offsets.
  1540. */
  1541. hdr->args.offset = offset;
  1542. /* Perform an asynchronous write */
  1543. nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
  1544. vers == 3 ? &ff_layout_write_call_ops_v3 :
  1545. &ff_layout_write_call_ops_v4,
  1546. sync, RPC_TASK_SOFTCONN);
  1547. return PNFS_ATTEMPTED;
  1548. }
  1549. static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  1550. {
  1551. return i;
  1552. }
  1553. static struct nfs_fh *
  1554. select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
  1555. {
  1556. struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
  1557. /* FIXME: Assume that there is only one NFS version available
  1558. * for the DS.
  1559. */
  1560. return &flseg->mirror_array[i]->fh_versions[0];
  1561. }
  1562. static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
  1563. {
  1564. struct pnfs_layout_segment *lseg = data->lseg;
  1565. struct nfs4_pnfs_ds *ds;
  1566. struct rpc_clnt *ds_clnt;
  1567. struct rpc_cred *ds_cred;
  1568. u32 idx;
  1569. int vers;
  1570. struct nfs_fh *fh;
  1571. idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
  1572. ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
  1573. if (!ds)
  1574. goto out_err;
  1575. ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
  1576. data->inode);
  1577. if (IS_ERR(ds_clnt))
  1578. goto out_err;
  1579. ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
  1580. if (IS_ERR(ds_cred))
  1581. goto out_err;
  1582. vers = nfs4_ff_layout_ds_version(lseg, idx);
  1583. dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
  1584. data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
  1585. vers);
  1586. data->commit_done_cb = ff_layout_commit_done_cb;
  1587. data->cred = ds_cred;
  1588. atomic_inc(&ds->ds_clp->cl_count);
  1589. data->ds_clp = ds->ds_clp;
  1590. fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
  1591. if (fh)
  1592. data->args.fh = fh;
  1593. return nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
  1594. vers == 3 ? &ff_layout_commit_call_ops_v3 :
  1595. &ff_layout_commit_call_ops_v4,
  1596. how, RPC_TASK_SOFTCONN);
  1597. out_err:
  1598. pnfs_generic_prepare_to_resend_writes(data);
  1599. pnfs_generic_commit_release(data);
  1600. return -EAGAIN;
  1601. }
  1602. static int
  1603. ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  1604. int how, struct nfs_commit_info *cinfo)
  1605. {
  1606. return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
  1607. ff_layout_initiate_commit);
  1608. }
  1609. static struct pnfs_ds_commit_info *
  1610. ff_layout_get_ds_info(struct inode *inode)
  1611. {
  1612. struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
  1613. if (layout == NULL)
  1614. return NULL;
  1615. return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
  1616. }
  1617. static void
  1618. ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
  1619. {
  1620. nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
  1621. id_node));
  1622. }
  1623. static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
  1624. struct xdr_stream *xdr,
  1625. const struct nfs4_layoutreturn_args *args)
  1626. {
  1627. struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
  1628. __be32 *start;
  1629. int count = 0, ret = 0;
  1630. start = xdr_reserve_space(xdr, 4);
  1631. if (unlikely(!start))
  1632. return -E2BIG;
  1633. /* This assume we always return _ALL_ layouts */
  1634. spin_lock(&hdr->plh_inode->i_lock);
  1635. ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
  1636. spin_unlock(&hdr->plh_inode->i_lock);
  1637. *start = cpu_to_be32(count);
  1638. return ret;
  1639. }
  1640. /* report nothing for now */
  1641. static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
  1642. struct xdr_stream *xdr,
  1643. const struct nfs4_layoutreturn_args *args)
  1644. {
  1645. __be32 *p;
  1646. p = xdr_reserve_space(xdr, 4);
  1647. if (likely(p))
  1648. *p = cpu_to_be32(0);
  1649. }
  1650. static struct nfs4_deviceid_node *
  1651. ff_layout_alloc_deviceid_node(struct nfs_server *server,
  1652. struct pnfs_device *pdev, gfp_t gfp_flags)
  1653. {
  1654. struct nfs4_ff_layout_ds *dsaddr;
  1655. dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
  1656. if (!dsaddr)
  1657. return NULL;
  1658. return &dsaddr->id_node;
  1659. }
  1660. static void
  1661. ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
  1662. struct xdr_stream *xdr,
  1663. const struct nfs4_layoutreturn_args *args)
  1664. {
  1665. struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
  1666. __be32 *start;
  1667. dprintk("%s: Begin\n", __func__);
  1668. start = xdr_reserve_space(xdr, 4);
  1669. BUG_ON(!start);
  1670. ff_layout_encode_ioerr(flo, xdr, args);
  1671. ff_layout_encode_iostats(flo, xdr, args);
  1672. *start = cpu_to_be32((xdr->p - start - 1) * 4);
  1673. dprintk("%s: Return\n", __func__);
  1674. }
  1675. static int
  1676. ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
  1677. {
  1678. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  1679. return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
  1680. }
  1681. static size_t
  1682. ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
  1683. const int buflen)
  1684. {
  1685. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  1686. const struct in6_addr *addr = &sin6->sin6_addr;
  1687. /*
  1688. * RFC 4291, Section 2.2.2
  1689. *
  1690. * Shorthanded ANY address
  1691. */
  1692. if (ipv6_addr_any(addr))
  1693. return snprintf(buf, buflen, "::");
  1694. /*
  1695. * RFC 4291, Section 2.2.2
  1696. *
  1697. * Shorthanded loopback address
  1698. */
  1699. if (ipv6_addr_loopback(addr))
  1700. return snprintf(buf, buflen, "::1");
  1701. /*
  1702. * RFC 4291, Section 2.2.3
  1703. *
  1704. * Special presentation address format for mapped v4
  1705. * addresses.
  1706. */
  1707. if (ipv6_addr_v4mapped(addr))
  1708. return snprintf(buf, buflen, "::ffff:%pI4",
  1709. &addr->s6_addr32[3]);
  1710. /*
  1711. * RFC 4291, Section 2.2.1
  1712. */
  1713. return snprintf(buf, buflen, "%pI6c", addr);
  1714. }
  1715. /* Derived from rpc_sockaddr2uaddr */
  1716. static void
  1717. ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
  1718. {
  1719. struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
  1720. char portbuf[RPCBIND_MAXUADDRPLEN];
  1721. char addrbuf[RPCBIND_MAXUADDRLEN];
  1722. char *netid;
  1723. unsigned short port;
  1724. int len, netid_len;
  1725. __be32 *p;
  1726. switch (sap->sa_family) {
  1727. case AF_INET:
  1728. if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
  1729. return;
  1730. port = ntohs(((struct sockaddr_in *)sap)->sin_port);
  1731. netid = "tcp";
  1732. netid_len = 3;
  1733. break;
  1734. case AF_INET6:
  1735. if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
  1736. return;
  1737. port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
  1738. netid = "tcp6";
  1739. netid_len = 4;
  1740. break;
  1741. default:
  1742. /* we only support tcp and tcp6 */
  1743. WARN_ON_ONCE(1);
  1744. return;
  1745. }
  1746. snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
  1747. len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
  1748. p = xdr_reserve_space(xdr, 4 + netid_len);
  1749. xdr_encode_opaque(p, netid, netid_len);
  1750. p = xdr_reserve_space(xdr, 4 + len);
  1751. xdr_encode_opaque(p, addrbuf, len);
  1752. }
  1753. static void
  1754. ff_layout_encode_nfstime(struct xdr_stream *xdr,
  1755. ktime_t t)
  1756. {
  1757. struct timespec64 ts;
  1758. __be32 *p;
  1759. p = xdr_reserve_space(xdr, 12);
  1760. ts = ktime_to_timespec64(t);
  1761. p = xdr_encode_hyper(p, ts.tv_sec);
  1762. *p++ = cpu_to_be32(ts.tv_nsec);
  1763. }
  1764. static void
  1765. ff_layout_encode_io_latency(struct xdr_stream *xdr,
  1766. struct nfs4_ff_io_stat *stat)
  1767. {
  1768. __be32 *p;
  1769. p = xdr_reserve_space(xdr, 5 * 8);
  1770. p = xdr_encode_hyper(p, stat->ops_requested);
  1771. p = xdr_encode_hyper(p, stat->bytes_requested);
  1772. p = xdr_encode_hyper(p, stat->ops_completed);
  1773. p = xdr_encode_hyper(p, stat->bytes_completed);
  1774. p = xdr_encode_hyper(p, stat->bytes_not_delivered);
  1775. ff_layout_encode_nfstime(xdr, stat->total_busy_time);
  1776. ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
  1777. }
  1778. static void
  1779. ff_layout_encode_layoutstats(struct xdr_stream *xdr,
  1780. struct nfs42_layoutstat_args *args,
  1781. struct nfs42_layoutstat_devinfo *devinfo)
  1782. {
  1783. struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
  1784. struct nfs4_pnfs_ds_addr *da;
  1785. struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
  1786. struct nfs_fh *fh = &mirror->fh_versions[0];
  1787. __be32 *p, *start;
  1788. da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
  1789. dprintk("%s: DS %s: encoding address %s\n",
  1790. __func__, ds->ds_remotestr, da->da_remotestr);
  1791. /* layoutupdate length */
  1792. start = xdr_reserve_space(xdr, 4);
  1793. /* netaddr4 */
  1794. ff_layout_encode_netaddr(xdr, da);
  1795. /* nfs_fh4 */
  1796. p = xdr_reserve_space(xdr, 4 + fh->size);
  1797. xdr_encode_opaque(p, fh->data, fh->size);
  1798. /* ff_io_latency4 read */
  1799. spin_lock(&mirror->lock);
  1800. ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
  1801. /* ff_io_latency4 write */
  1802. ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
  1803. spin_unlock(&mirror->lock);
  1804. /* nfstime4 */
  1805. ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
  1806. /* bool */
  1807. p = xdr_reserve_space(xdr, 4);
  1808. *p = cpu_to_be32(false);
  1809. *start = cpu_to_be32((xdr->p - start - 1) * 4);
  1810. }
  1811. static int
  1812. ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
  1813. struct pnfs_layout_hdr *lo,
  1814. int dev_limit)
  1815. {
  1816. struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
  1817. struct nfs4_ff_layout_mirror *mirror;
  1818. struct nfs4_deviceid_node *dev;
  1819. struct nfs42_layoutstat_devinfo *devinfo;
  1820. int i = 0;
  1821. list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
  1822. if (i >= dev_limit)
  1823. break;
  1824. if (!mirror->mirror_ds)
  1825. continue;
  1826. /* mirror refcount put in cleanup_layoutstats */
  1827. if (!atomic_inc_not_zero(&mirror->ref))
  1828. continue;
  1829. dev = &mirror->mirror_ds->id_node;
  1830. devinfo = &args->devinfo[i];
  1831. memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
  1832. devinfo->offset = 0;
  1833. devinfo->length = NFS4_MAX_UINT64;
  1834. devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
  1835. devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
  1836. devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
  1837. devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
  1838. devinfo->layout_type = LAYOUT_FLEX_FILES;
  1839. devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
  1840. devinfo->layout_private = mirror;
  1841. i++;
  1842. }
  1843. return i;
  1844. }
  1845. static int
  1846. ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
  1847. {
  1848. struct nfs4_flexfile_layout *ff_layout;
  1849. struct nfs4_ff_layout_mirror *mirror;
  1850. int dev_count = 0;
  1851. spin_lock(&args->inode->i_lock);
  1852. ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
  1853. list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
  1854. if (atomic_read(&mirror->ref) != 0)
  1855. dev_count ++;
  1856. }
  1857. spin_unlock(&args->inode->i_lock);
  1858. /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
  1859. if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
  1860. dprintk("%s: truncating devinfo to limit (%d:%d)\n",
  1861. __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
  1862. dev_count = PNFS_LAYOUTSTATS_MAXDEV;
  1863. }
  1864. args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
  1865. if (!args->devinfo)
  1866. return -ENOMEM;
  1867. spin_lock(&args->inode->i_lock);
  1868. args->num_dev = ff_layout_mirror_prepare_stats(args,
  1869. &ff_layout->generic_hdr, dev_count);
  1870. spin_unlock(&args->inode->i_lock);
  1871. return 0;
  1872. }
  1873. static void
  1874. ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
  1875. {
  1876. struct nfs4_ff_layout_mirror *mirror;
  1877. int i;
  1878. for (i = 0; i < data->args.num_dev; i++) {
  1879. mirror = data->args.devinfo[i].layout_private;
  1880. data->args.devinfo[i].layout_private = NULL;
  1881. ff_layout_put_mirror(mirror);
  1882. }
  1883. }
  1884. static struct pnfs_layoutdriver_type flexfilelayout_type = {
  1885. .id = LAYOUT_FLEX_FILES,
  1886. .name = "LAYOUT_FLEX_FILES",
  1887. .owner = THIS_MODULE,
  1888. .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
  1889. .free_layout_hdr = ff_layout_free_layout_hdr,
  1890. .alloc_lseg = ff_layout_alloc_lseg,
  1891. .free_lseg = ff_layout_free_lseg,
  1892. .add_lseg = ff_layout_add_lseg,
  1893. .pg_read_ops = &ff_layout_pg_read_ops,
  1894. .pg_write_ops = &ff_layout_pg_write_ops,
  1895. .get_ds_info = ff_layout_get_ds_info,
  1896. .free_deviceid_node = ff_layout_free_deviceid_node,
  1897. .mark_request_commit = pnfs_layout_mark_request_commit,
  1898. .clear_request_commit = pnfs_generic_clear_request_commit,
  1899. .scan_commit_lists = pnfs_generic_scan_commit_lists,
  1900. .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
  1901. .commit_pagelist = ff_layout_commit_pagelist,
  1902. .read_pagelist = ff_layout_read_pagelist,
  1903. .write_pagelist = ff_layout_write_pagelist,
  1904. .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
  1905. .encode_layoutreturn = ff_layout_encode_layoutreturn,
  1906. .sync = pnfs_nfs_generic_sync,
  1907. .prepare_layoutstats = ff_layout_prepare_layoutstats,
  1908. .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
  1909. };
  1910. static int __init nfs4flexfilelayout_init(void)
  1911. {
  1912. printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
  1913. __func__);
  1914. return pnfs_register_layoutdriver(&flexfilelayout_type);
  1915. }
  1916. static void __exit nfs4flexfilelayout_exit(void)
  1917. {
  1918. printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
  1919. __func__);
  1920. pnfs_unregister_layoutdriver(&flexfilelayout_type);
  1921. }
  1922. MODULE_ALIAS("nfs-layouttype4-4");
  1923. MODULE_LICENSE("GPL");
  1924. MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
  1925. module_init(nfs4flexfilelayout_init);
  1926. module_exit(nfs4flexfilelayout_exit);