pnfs.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * pNFS functions to call and manage layout drivers.
  3. *
  4. * Copyright (c) 2002 [year of first publication]
  5. * The Regents of the University of Michigan
  6. * All Rights Reserved
  7. *
  8. * Dean Hildebrand <dhildebz@umich.edu>
  9. *
  10. * Permission is granted to use, copy, create derivative works, and
  11. * redistribute this software and such derivative works for any purpose,
  12. * so long as the name of the University of Michigan is not used in
  13. * any advertising or publicity pertaining to the use or distribution
  14. * of this software without specific, written prior authorization. If
  15. * the above copyright notice or any other identification of the
  16. * University of Michigan is included in any copy of any portion of
  17. * this software, then the disclaimer below must also be included.
  18. *
  19. * This software is provided as is, without representation or warranty
  20. * of any kind either express or implied, including without limitation
  21. * the implied warranties of merchantability, fitness for a particular
  22. * purpose, or noninfringement. The Regents of the University of
  23. * Michigan shall not be liable for any damages, including special,
  24. * indirect, incidental, or consequential damages, with respect to any
  25. * claim arising out of or in connection with the use of the software,
  26. * even if it has been or is hereafter advised of the possibility of
  27. * such damages.
  28. */
  29. #include <linux/nfs_fs.h>
  30. #include <linux/nfs_page.h>
  31. #include <linux/module.h>
  32. #include "internal.h"
  33. #include "pnfs.h"
  34. #include "iostat.h"
  35. #define NFSDBG_FACILITY NFSDBG_PNFS
  36. /* Locking:
  37. *
  38. * pnfs_spinlock:
  39. * protects pnfs_modules_tbl.
  40. */
  41. static DEFINE_SPINLOCK(pnfs_spinlock);
  42. /*
  43. * pnfs_modules_tbl holds all pnfs modules
  44. */
  45. static LIST_HEAD(pnfs_modules_tbl);
  46. /* Return the registered pnfs layout driver module matching given id */
  47. static struct pnfs_layoutdriver_type *
  48. find_pnfs_driver_locked(u32 id)
  49. {
  50. struct pnfs_layoutdriver_type *local;
  51. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  52. if (local->id == id)
  53. goto out;
  54. local = NULL;
  55. out:
  56. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  57. return local;
  58. }
  59. static struct pnfs_layoutdriver_type *
  60. find_pnfs_driver(u32 id)
  61. {
  62. struct pnfs_layoutdriver_type *local;
  63. spin_lock(&pnfs_spinlock);
  64. local = find_pnfs_driver_locked(id);
  65. spin_unlock(&pnfs_spinlock);
  66. return local;
  67. }
  68. void
  69. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  70. {
  71. if (nfss->pnfs_curr_ld) {
  72. if (nfss->pnfs_curr_ld->clear_layoutdriver)
  73. nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
  74. module_put(nfss->pnfs_curr_ld->owner);
  75. }
  76. nfss->pnfs_curr_ld = NULL;
  77. }
  78. /*
  79. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  80. * Currently only one pNFS layout driver per filesystem is supported.
  81. *
  82. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  83. */
  84. void
  85. set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
  86. u32 id)
  87. {
  88. struct pnfs_layoutdriver_type *ld_type = NULL;
  89. if (id == 0)
  90. goto out_no_driver;
  91. if (!(server->nfs_client->cl_exchange_flags &
  92. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  93. printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
  94. __func__, id, server->nfs_client->cl_exchange_flags);
  95. goto out_no_driver;
  96. }
  97. ld_type = find_pnfs_driver(id);
  98. if (!ld_type) {
  99. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  100. ld_type = find_pnfs_driver(id);
  101. if (!ld_type) {
  102. dprintk("%s: No pNFS module found for %u.\n",
  103. __func__, id);
  104. goto out_no_driver;
  105. }
  106. }
  107. if (!try_module_get(ld_type->owner)) {
  108. dprintk("%s: Could not grab reference on module\n", __func__);
  109. goto out_no_driver;
  110. }
  111. server->pnfs_curr_ld = ld_type;
  112. if (ld_type->set_layoutdriver
  113. && ld_type->set_layoutdriver(server, mntfh)) {
  114. printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
  115. "driver %u.\n", __func__, id);
  116. module_put(ld_type->owner);
  117. goto out_no_driver;
  118. }
  119. dprintk("%s: pNFS module for %u set\n", __func__, id);
  120. return;
  121. out_no_driver:
  122. dprintk("%s: Using NFSv4 I/O\n", __func__);
  123. server->pnfs_curr_ld = NULL;
  124. }
  125. int
  126. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  127. {
  128. int status = -EINVAL;
  129. struct pnfs_layoutdriver_type *tmp;
  130. if (ld_type->id == 0) {
  131. printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
  132. return status;
  133. }
  134. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  135. printk(KERN_ERR "NFS: %s Layout driver must provide "
  136. "alloc_lseg and free_lseg.\n", __func__);
  137. return status;
  138. }
  139. spin_lock(&pnfs_spinlock);
  140. tmp = find_pnfs_driver_locked(ld_type->id);
  141. if (!tmp) {
  142. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  143. status = 0;
  144. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  145. ld_type->name);
  146. } else {
  147. printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
  148. __func__, ld_type->id);
  149. }
  150. spin_unlock(&pnfs_spinlock);
  151. return status;
  152. }
  153. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  154. void
  155. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  156. {
  157. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  158. spin_lock(&pnfs_spinlock);
  159. list_del(&ld_type->pnfs_tblid);
  160. spin_unlock(&pnfs_spinlock);
  161. }
  162. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  163. /*
  164. * pNFS client layout cache
  165. */
  166. /* Need to hold i_lock if caller does not already hold reference */
  167. void
  168. get_layout_hdr(struct pnfs_layout_hdr *lo)
  169. {
  170. atomic_inc(&lo->plh_refcount);
  171. }
  172. static struct pnfs_layout_hdr *
  173. pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
  174. {
  175. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  176. return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
  177. kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
  178. }
  179. static void
  180. pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
  181. {
  182. struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
  183. put_rpccred(lo->plh_lc_cred);
  184. return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
  185. }
  186. static void
  187. destroy_layout_hdr(struct pnfs_layout_hdr *lo)
  188. {
  189. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  190. BUG_ON(!list_empty(&lo->plh_layouts));
  191. NFS_I(lo->plh_inode)->layout = NULL;
  192. pnfs_free_layout_hdr(lo);
  193. }
  194. static void
  195. put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  196. {
  197. if (atomic_dec_and_test(&lo->plh_refcount))
  198. destroy_layout_hdr(lo);
  199. }
  200. void
  201. put_layout_hdr(struct pnfs_layout_hdr *lo)
  202. {
  203. struct inode *inode = lo->plh_inode;
  204. if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
  205. destroy_layout_hdr(lo);
  206. spin_unlock(&inode->i_lock);
  207. }
  208. }
  209. static void
  210. init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
  211. {
  212. INIT_LIST_HEAD(&lseg->pls_list);
  213. INIT_LIST_HEAD(&lseg->pls_lc_list);
  214. atomic_set(&lseg->pls_refcount, 1);
  215. smp_mb();
  216. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  217. lseg->pls_layout = lo;
  218. }
  219. static void free_lseg(struct pnfs_layout_segment *lseg)
  220. {
  221. struct inode *ino = lseg->pls_layout->plh_inode;
  222. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  223. /* Matched by get_layout_hdr in pnfs_insert_layout */
  224. put_layout_hdr(NFS_I(ino)->layout);
  225. }
  226. static void
  227. put_lseg_common(struct pnfs_layout_segment *lseg)
  228. {
  229. struct inode *inode = lseg->pls_layout->plh_inode;
  230. WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  231. list_del_init(&lseg->pls_list);
  232. if (list_empty(&lseg->pls_layout->plh_segs)) {
  233. set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
  234. /* Matched by initial refcount set in alloc_init_layout_hdr */
  235. put_layout_hdr_locked(lseg->pls_layout);
  236. }
  237. rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
  238. }
  239. void
  240. put_lseg(struct pnfs_layout_segment *lseg)
  241. {
  242. struct inode *inode;
  243. if (!lseg)
  244. return;
  245. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  246. atomic_read(&lseg->pls_refcount),
  247. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  248. inode = lseg->pls_layout->plh_inode;
  249. if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
  250. LIST_HEAD(free_me);
  251. put_lseg_common(lseg);
  252. list_add(&lseg->pls_list, &free_me);
  253. spin_unlock(&inode->i_lock);
  254. pnfs_free_lseg_list(&free_me);
  255. }
  256. }
  257. EXPORT_SYMBOL_GPL(put_lseg);
  258. static inline u64
  259. end_offset(u64 start, u64 len)
  260. {
  261. u64 end;
  262. end = start + len;
  263. return end >= start ? end : NFS4_MAX_UINT64;
  264. }
  265. /* last octet in a range */
  266. static inline u64
  267. last_byte_offset(u64 start, u64 len)
  268. {
  269. u64 end;
  270. BUG_ON(!len);
  271. end = start + len;
  272. return end > start ? end - 1 : NFS4_MAX_UINT64;
  273. }
  274. /*
  275. * is l2 fully contained in l1?
  276. * start1 end1
  277. * [----------------------------------)
  278. * start2 end2
  279. * [----------------)
  280. */
  281. static inline int
  282. lo_seg_contained(struct pnfs_layout_range *l1,
  283. struct pnfs_layout_range *l2)
  284. {
  285. u64 start1 = l1->offset;
  286. u64 end1 = end_offset(start1, l1->length);
  287. u64 start2 = l2->offset;
  288. u64 end2 = end_offset(start2, l2->length);
  289. return (start1 <= start2) && (end1 >= end2);
  290. }
  291. /*
  292. * is l1 and l2 intersecting?
  293. * start1 end1
  294. * [----------------------------------)
  295. * start2 end2
  296. * [----------------)
  297. */
  298. static inline int
  299. lo_seg_intersecting(struct pnfs_layout_range *l1,
  300. struct pnfs_layout_range *l2)
  301. {
  302. u64 start1 = l1->offset;
  303. u64 end1 = end_offset(start1, l1->length);
  304. u64 start2 = l2->offset;
  305. u64 end2 = end_offset(start2, l2->length);
  306. return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
  307. (end2 == NFS4_MAX_UINT64 || end2 > start1);
  308. }
  309. static bool
  310. should_free_lseg(struct pnfs_layout_range *lseg_range,
  311. struct pnfs_layout_range *recall_range)
  312. {
  313. return (recall_range->iomode == IOMODE_ANY ||
  314. lseg_range->iomode == recall_range->iomode) &&
  315. lo_seg_intersecting(lseg_range, recall_range);
  316. }
  317. /* Returns 1 if lseg is removed from list, 0 otherwise */
  318. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  319. struct list_head *tmp_list)
  320. {
  321. int rv = 0;
  322. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  323. /* Remove the reference keeping the lseg in the
  324. * list. It will now be removed when all
  325. * outstanding io is finished.
  326. */
  327. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  328. atomic_read(&lseg->pls_refcount));
  329. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  330. put_lseg_common(lseg);
  331. list_add(&lseg->pls_list, tmp_list);
  332. rv = 1;
  333. }
  334. }
  335. return rv;
  336. }
  337. /* Returns count of number of matching invalid lsegs remaining in list
  338. * after call.
  339. */
  340. int
  341. mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  342. struct list_head *tmp_list,
  343. struct pnfs_layout_range *recall_range)
  344. {
  345. struct pnfs_layout_segment *lseg, *next;
  346. int invalid = 0, removed = 0;
  347. dprintk("%s:Begin lo %p\n", __func__, lo);
  348. if (list_empty(&lo->plh_segs)) {
  349. if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
  350. put_layout_hdr_locked(lo);
  351. return 0;
  352. }
  353. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  354. if (!recall_range ||
  355. should_free_lseg(&lseg->pls_range, recall_range)) {
  356. dprintk("%s: freeing lseg %p iomode %d "
  357. "offset %llu length %llu\n", __func__,
  358. lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
  359. lseg->pls_range.length);
  360. invalid++;
  361. removed += mark_lseg_invalid(lseg, tmp_list);
  362. }
  363. dprintk("%s:Return %i\n", __func__, invalid - removed);
  364. return invalid - removed;
  365. }
  366. /* note free_me must contain lsegs from a single layout_hdr */
  367. void
  368. pnfs_free_lseg_list(struct list_head *free_me)
  369. {
  370. struct pnfs_layout_segment *lseg, *tmp;
  371. struct pnfs_layout_hdr *lo;
  372. if (list_empty(free_me))
  373. return;
  374. lo = list_first_entry(free_me, struct pnfs_layout_segment,
  375. pls_list)->pls_layout;
  376. if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
  377. struct nfs_client *clp;
  378. clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  379. spin_lock(&clp->cl_lock);
  380. list_del_init(&lo->plh_layouts);
  381. spin_unlock(&clp->cl_lock);
  382. }
  383. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  384. list_del(&lseg->pls_list);
  385. free_lseg(lseg);
  386. }
  387. }
  388. void
  389. pnfs_destroy_layout(struct nfs_inode *nfsi)
  390. {
  391. struct pnfs_layout_hdr *lo;
  392. LIST_HEAD(tmp_list);
  393. spin_lock(&nfsi->vfs_inode.i_lock);
  394. lo = nfsi->layout;
  395. if (lo) {
  396. lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
  397. mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  398. }
  399. spin_unlock(&nfsi->vfs_inode.i_lock);
  400. pnfs_free_lseg_list(&tmp_list);
  401. }
  402. EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
  403. /*
  404. * Called by the state manger to remove all layouts established under an
  405. * expired lease.
  406. */
  407. void
  408. pnfs_destroy_all_layouts(struct nfs_client *clp)
  409. {
  410. struct nfs_server *server;
  411. struct pnfs_layout_hdr *lo;
  412. LIST_HEAD(tmp_list);
  413. nfs4_deviceid_mark_client_invalid(clp);
  414. nfs4_deviceid_purge_client(clp);
  415. spin_lock(&clp->cl_lock);
  416. rcu_read_lock();
  417. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  418. if (!list_empty(&server->layouts))
  419. list_splice_init(&server->layouts, &tmp_list);
  420. }
  421. rcu_read_unlock();
  422. spin_unlock(&clp->cl_lock);
  423. while (!list_empty(&tmp_list)) {
  424. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  425. plh_layouts);
  426. dprintk("%s freeing layout for inode %lu\n", __func__,
  427. lo->plh_inode->i_ino);
  428. list_del_init(&lo->plh_layouts);
  429. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  430. }
  431. }
  432. /* update lo->plh_stateid with new if is more recent */
  433. void
  434. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  435. bool update_barrier)
  436. {
  437. u32 oldseq, newseq;
  438. oldseq = be32_to_cpu(lo->plh_stateid.seqid);
  439. newseq = be32_to_cpu(new->seqid);
  440. if ((int)(newseq - oldseq) > 0) {
  441. nfs4_stateid_copy(&lo->plh_stateid, new);
  442. if (update_barrier) {
  443. u32 new_barrier = be32_to_cpu(new->seqid);
  444. if ((int)(new_barrier - lo->plh_barrier))
  445. lo->plh_barrier = new_barrier;
  446. } else {
  447. /* Because of wraparound, we want to keep the barrier
  448. * "close" to the current seqids. It needs to be
  449. * within 2**31 to count as "behind", so if it
  450. * gets too near that limit, give us a litle leeway
  451. * and bring it to within 2**30.
  452. * NOTE - and yes, this is all unsigned arithmetic.
  453. */
  454. if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
  455. lo->plh_barrier = newseq - (1 << 30);
  456. }
  457. }
  458. }
  459. /* lget is set to 1 if called from inside send_layoutget call chain */
  460. static bool
  461. pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
  462. int lget)
  463. {
  464. if ((stateid) &&
  465. (int)(lo->plh_barrier - be32_to_cpu(stateid->seqid)) >= 0)
  466. return true;
  467. return lo->plh_block_lgets ||
  468. test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
  469. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  470. (list_empty(&lo->plh_segs) &&
  471. (atomic_read(&lo->plh_outstanding) > lget));
  472. }
  473. int
  474. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  475. struct nfs4_state *open_state)
  476. {
  477. int status = 0;
  478. dprintk("--> %s\n", __func__);
  479. spin_lock(&lo->plh_inode->i_lock);
  480. if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
  481. status = -EAGAIN;
  482. } else if (list_empty(&lo->plh_segs)) {
  483. int seq;
  484. do {
  485. seq = read_seqbegin(&open_state->seqlock);
  486. nfs4_stateid_copy(dst, &open_state->stateid);
  487. } while (read_seqretry(&open_state->seqlock, seq));
  488. } else
  489. nfs4_stateid_copy(dst, &lo->plh_stateid);
  490. spin_unlock(&lo->plh_inode->i_lock);
  491. dprintk("<-- %s\n", __func__);
  492. return status;
  493. }
  494. /*
  495. * Get layout from server.
  496. * for now, assume that whole file layouts are requested.
  497. * arg->offset: 0
  498. * arg->length: all ones
  499. */
  500. static struct pnfs_layout_segment *
  501. send_layoutget(struct pnfs_layout_hdr *lo,
  502. struct nfs_open_context *ctx,
  503. struct pnfs_layout_range *range,
  504. gfp_t gfp_flags)
  505. {
  506. struct inode *ino = lo->plh_inode;
  507. struct nfs_server *server = NFS_SERVER(ino);
  508. struct nfs4_layoutget *lgp;
  509. struct pnfs_layout_segment *lseg = NULL;
  510. struct page **pages = NULL;
  511. int i;
  512. u32 max_resp_sz, max_pages;
  513. dprintk("--> %s\n", __func__);
  514. BUG_ON(ctx == NULL);
  515. lgp = kzalloc(sizeof(*lgp), gfp_flags);
  516. if (lgp == NULL)
  517. return NULL;
  518. /* allocate pages for xdr post processing */
  519. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  520. max_pages = nfs_page_array_len(0, max_resp_sz);
  521. pages = kcalloc(max_pages, sizeof(struct page *), gfp_flags);
  522. if (!pages)
  523. goto out_err_free;
  524. for (i = 0; i < max_pages; i++) {
  525. pages[i] = alloc_page(gfp_flags);
  526. if (!pages[i])
  527. goto out_err_free;
  528. }
  529. lgp->args.minlength = PAGE_CACHE_SIZE;
  530. if (lgp->args.minlength > range->length)
  531. lgp->args.minlength = range->length;
  532. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  533. lgp->args.range = *range;
  534. lgp->args.type = server->pnfs_curr_ld->id;
  535. lgp->args.inode = ino;
  536. lgp->args.ctx = get_nfs_open_context(ctx);
  537. lgp->args.layout.pages = pages;
  538. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  539. lgp->lsegpp = &lseg;
  540. lgp->gfp_flags = gfp_flags;
  541. /* Synchronously retrieve layout information from server and
  542. * store in lseg.
  543. */
  544. nfs4_proc_layoutget(lgp);
  545. if (!lseg) {
  546. /* remember that LAYOUTGET failed and suspend trying */
  547. set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
  548. }
  549. /* free xdr pages */
  550. for (i = 0; i < max_pages; i++)
  551. __free_page(pages[i]);
  552. kfree(pages);
  553. return lseg;
  554. out_err_free:
  555. /* free any allocated xdr pages, lgp as it's not used */
  556. if (pages) {
  557. for (i = 0; i < max_pages; i++) {
  558. if (!pages[i])
  559. break;
  560. __free_page(pages[i]);
  561. }
  562. kfree(pages);
  563. }
  564. kfree(lgp);
  565. return NULL;
  566. }
  567. /* Initiates a LAYOUTRETURN(FILE) */
  568. int
  569. _pnfs_return_layout(struct inode *ino)
  570. {
  571. struct pnfs_layout_hdr *lo = NULL;
  572. struct nfs_inode *nfsi = NFS_I(ino);
  573. LIST_HEAD(tmp_list);
  574. struct nfs4_layoutreturn *lrp;
  575. nfs4_stateid stateid;
  576. int status = 0;
  577. dprintk("--> %s\n", __func__);
  578. spin_lock(&ino->i_lock);
  579. lo = nfsi->layout;
  580. if (!lo) {
  581. spin_unlock(&ino->i_lock);
  582. dprintk("%s: no layout to return\n", __func__);
  583. return status;
  584. }
  585. stateid = nfsi->layout->plh_stateid;
  586. /* Reference matched in nfs4_layoutreturn_release */
  587. get_layout_hdr(lo);
  588. mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  589. lo->plh_block_lgets++;
  590. spin_unlock(&ino->i_lock);
  591. pnfs_free_lseg_list(&tmp_list);
  592. WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
  593. lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
  594. if (unlikely(lrp == NULL)) {
  595. status = -ENOMEM;
  596. set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
  597. set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
  598. put_layout_hdr(lo);
  599. goto out;
  600. }
  601. lrp->args.stateid = stateid;
  602. lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
  603. lrp->args.inode = ino;
  604. lrp->args.layout = lo;
  605. lrp->clp = NFS_SERVER(ino)->nfs_client;
  606. status = nfs4_proc_layoutreturn(lrp);
  607. out:
  608. dprintk("<-- %s status: %d\n", __func__, status);
  609. return status;
  610. }
  611. EXPORT_SYMBOL_GPL(_pnfs_return_layout);
  612. bool pnfs_roc(struct inode *ino)
  613. {
  614. struct pnfs_layout_hdr *lo;
  615. struct pnfs_layout_segment *lseg, *tmp;
  616. LIST_HEAD(tmp_list);
  617. bool found = false;
  618. spin_lock(&ino->i_lock);
  619. lo = NFS_I(ino)->layout;
  620. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  621. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  622. goto out_nolayout;
  623. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  624. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  625. mark_lseg_invalid(lseg, &tmp_list);
  626. found = true;
  627. }
  628. if (!found)
  629. goto out_nolayout;
  630. lo->plh_block_lgets++;
  631. get_layout_hdr(lo); /* matched in pnfs_roc_release */
  632. spin_unlock(&ino->i_lock);
  633. pnfs_free_lseg_list(&tmp_list);
  634. return true;
  635. out_nolayout:
  636. spin_unlock(&ino->i_lock);
  637. return false;
  638. }
  639. void pnfs_roc_release(struct inode *ino)
  640. {
  641. struct pnfs_layout_hdr *lo;
  642. spin_lock(&ino->i_lock);
  643. lo = NFS_I(ino)->layout;
  644. lo->plh_block_lgets--;
  645. put_layout_hdr_locked(lo);
  646. spin_unlock(&ino->i_lock);
  647. }
  648. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  649. {
  650. struct pnfs_layout_hdr *lo;
  651. spin_lock(&ino->i_lock);
  652. lo = NFS_I(ino)->layout;
  653. if ((int)(barrier - lo->plh_barrier) > 0)
  654. lo->plh_barrier = barrier;
  655. spin_unlock(&ino->i_lock);
  656. }
  657. bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
  658. {
  659. struct nfs_inode *nfsi = NFS_I(ino);
  660. struct pnfs_layout_segment *lseg;
  661. bool found = false;
  662. spin_lock(&ino->i_lock);
  663. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  664. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  665. found = true;
  666. break;
  667. }
  668. if (!found) {
  669. struct pnfs_layout_hdr *lo = nfsi->layout;
  670. u32 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
  671. /* Since close does not return a layout stateid for use as
  672. * a barrier, we choose the worst-case barrier.
  673. */
  674. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  675. }
  676. spin_unlock(&ino->i_lock);
  677. return found;
  678. }
  679. /*
  680. * Compare two layout segments for sorting into layout cache.
  681. * We want to preferentially return RW over RO layouts, so ensure those
  682. * are seen first.
  683. */
  684. static s64
  685. cmp_layout(struct pnfs_layout_range *l1,
  686. struct pnfs_layout_range *l2)
  687. {
  688. s64 d;
  689. /* high offset > low offset */
  690. d = l1->offset - l2->offset;
  691. if (d)
  692. return d;
  693. /* short length > long length */
  694. d = l2->length - l1->length;
  695. if (d)
  696. return d;
  697. /* read > read/write */
  698. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  699. }
  700. static void
  701. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  702. struct pnfs_layout_segment *lseg)
  703. {
  704. struct pnfs_layout_segment *lp;
  705. dprintk("%s:Begin\n", __func__);
  706. assert_spin_locked(&lo->plh_inode->i_lock);
  707. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  708. if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
  709. continue;
  710. list_add_tail(&lseg->pls_list, &lp->pls_list);
  711. dprintk("%s: inserted lseg %p "
  712. "iomode %d offset %llu length %llu before "
  713. "lp %p iomode %d offset %llu length %llu\n",
  714. __func__, lseg, lseg->pls_range.iomode,
  715. lseg->pls_range.offset, lseg->pls_range.length,
  716. lp, lp->pls_range.iomode, lp->pls_range.offset,
  717. lp->pls_range.length);
  718. goto out;
  719. }
  720. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  721. dprintk("%s: inserted lseg %p "
  722. "iomode %d offset %llu length %llu at tail\n",
  723. __func__, lseg, lseg->pls_range.iomode,
  724. lseg->pls_range.offset, lseg->pls_range.length);
  725. out:
  726. get_layout_hdr(lo);
  727. dprintk("%s:Return\n", __func__);
  728. }
  729. static struct pnfs_layout_hdr *
  730. alloc_init_layout_hdr(struct inode *ino,
  731. struct nfs_open_context *ctx,
  732. gfp_t gfp_flags)
  733. {
  734. struct pnfs_layout_hdr *lo;
  735. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  736. if (!lo)
  737. return NULL;
  738. atomic_set(&lo->plh_refcount, 1);
  739. INIT_LIST_HEAD(&lo->plh_layouts);
  740. INIT_LIST_HEAD(&lo->plh_segs);
  741. INIT_LIST_HEAD(&lo->plh_bulk_recall);
  742. lo->plh_inode = ino;
  743. lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
  744. return lo;
  745. }
  746. static struct pnfs_layout_hdr *
  747. pnfs_find_alloc_layout(struct inode *ino,
  748. struct nfs_open_context *ctx,
  749. gfp_t gfp_flags)
  750. {
  751. struct nfs_inode *nfsi = NFS_I(ino);
  752. struct pnfs_layout_hdr *new = NULL;
  753. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  754. assert_spin_locked(&ino->i_lock);
  755. if (nfsi->layout) {
  756. if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
  757. return NULL;
  758. else
  759. return nfsi->layout;
  760. }
  761. spin_unlock(&ino->i_lock);
  762. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  763. spin_lock(&ino->i_lock);
  764. if (likely(nfsi->layout == NULL)) /* Won the race? */
  765. nfsi->layout = new;
  766. else
  767. pnfs_free_layout_hdr(new);
  768. return nfsi->layout;
  769. }
  770. /*
  771. * iomode matching rules:
  772. * iomode lseg match
  773. * ----- ----- -----
  774. * ANY READ true
  775. * ANY RW true
  776. * RW READ false
  777. * RW RW true
  778. * READ READ true
  779. * READ RW true
  780. */
  781. static int
  782. is_matching_lseg(struct pnfs_layout_range *ls_range,
  783. struct pnfs_layout_range *range)
  784. {
  785. struct pnfs_layout_range range1;
  786. if ((range->iomode == IOMODE_RW &&
  787. ls_range->iomode != IOMODE_RW) ||
  788. !lo_seg_intersecting(ls_range, range))
  789. return 0;
  790. /* range1 covers only the first byte in the range */
  791. range1 = *range;
  792. range1.length = 1;
  793. return lo_seg_contained(ls_range, &range1);
  794. }
  795. /*
  796. * lookup range in layout
  797. */
  798. static struct pnfs_layout_segment *
  799. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  800. struct pnfs_layout_range *range)
  801. {
  802. struct pnfs_layout_segment *lseg, *ret = NULL;
  803. dprintk("%s:Begin\n", __func__);
  804. assert_spin_locked(&lo->plh_inode->i_lock);
  805. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  806. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  807. is_matching_lseg(&lseg->pls_range, range)) {
  808. ret = get_lseg(lseg);
  809. break;
  810. }
  811. if (lseg->pls_range.offset > range->offset)
  812. break;
  813. }
  814. dprintk("%s:Return lseg %p ref %d\n",
  815. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  816. return ret;
  817. }
  818. /*
  819. * Layout segment is retreived from the server if not cached.
  820. * The appropriate layout segment is referenced and returned to the caller.
  821. */
  822. struct pnfs_layout_segment *
  823. pnfs_update_layout(struct inode *ino,
  824. struct nfs_open_context *ctx,
  825. loff_t pos,
  826. u64 count,
  827. enum pnfs_iomode iomode,
  828. gfp_t gfp_flags)
  829. {
  830. struct pnfs_layout_range arg = {
  831. .iomode = iomode,
  832. .offset = pos,
  833. .length = count,
  834. };
  835. unsigned pg_offset;
  836. struct nfs_inode *nfsi = NFS_I(ino);
  837. struct nfs_server *server = NFS_SERVER(ino);
  838. struct nfs_client *clp = server->nfs_client;
  839. struct pnfs_layout_hdr *lo;
  840. struct pnfs_layout_segment *lseg = NULL;
  841. bool first = false;
  842. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  843. return NULL;
  844. spin_lock(&ino->i_lock);
  845. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  846. if (lo == NULL) {
  847. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  848. goto out_unlock;
  849. }
  850. /* Do we even need to bother with this? */
  851. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  852. dprintk("%s matches recall, use MDS\n", __func__);
  853. goto out_unlock;
  854. }
  855. /* if LAYOUTGET already failed once we don't try again */
  856. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  857. goto out_unlock;
  858. /* Check to see if the layout for the given range already exists */
  859. lseg = pnfs_find_lseg(lo, &arg);
  860. if (lseg)
  861. goto out_unlock;
  862. if (pnfs_layoutgets_blocked(lo, NULL, 0))
  863. goto out_unlock;
  864. atomic_inc(&lo->plh_outstanding);
  865. get_layout_hdr(lo);
  866. if (list_empty(&lo->plh_segs))
  867. first = true;
  868. spin_unlock(&ino->i_lock);
  869. if (first) {
  870. /* The lo must be on the clp list if there is any
  871. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  872. */
  873. spin_lock(&clp->cl_lock);
  874. BUG_ON(!list_empty(&lo->plh_layouts));
  875. list_add_tail(&lo->plh_layouts, &server->layouts);
  876. spin_unlock(&clp->cl_lock);
  877. }
  878. pg_offset = arg.offset & ~PAGE_CACHE_MASK;
  879. if (pg_offset) {
  880. arg.offset -= pg_offset;
  881. arg.length += pg_offset;
  882. }
  883. if (arg.length != NFS4_MAX_UINT64)
  884. arg.length = PAGE_CACHE_ALIGN(arg.length);
  885. lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
  886. if (!lseg && first) {
  887. spin_lock(&clp->cl_lock);
  888. list_del_init(&lo->plh_layouts);
  889. spin_unlock(&clp->cl_lock);
  890. }
  891. atomic_dec(&lo->plh_outstanding);
  892. put_layout_hdr(lo);
  893. out:
  894. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  895. nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
  896. return lseg;
  897. out_unlock:
  898. spin_unlock(&ino->i_lock);
  899. goto out;
  900. }
  901. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  902. int
  903. pnfs_layout_process(struct nfs4_layoutget *lgp)
  904. {
  905. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  906. struct nfs4_layoutget_res *res = &lgp->res;
  907. struct pnfs_layout_segment *lseg;
  908. struct inode *ino = lo->plh_inode;
  909. int status = 0;
  910. /* Inject layout blob into I/O device driver */
  911. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  912. if (!lseg || IS_ERR(lseg)) {
  913. if (!lseg)
  914. status = -ENOMEM;
  915. else
  916. status = PTR_ERR(lseg);
  917. dprintk("%s: Could not allocate layout: error %d\n",
  918. __func__, status);
  919. goto out;
  920. }
  921. spin_lock(&ino->i_lock);
  922. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  923. dprintk("%s forget reply due to recall\n", __func__);
  924. goto out_forget_reply;
  925. }
  926. if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
  927. dprintk("%s forget reply due to state\n", __func__);
  928. goto out_forget_reply;
  929. }
  930. init_lseg(lo, lseg);
  931. lseg->pls_range = res->range;
  932. *lgp->lsegpp = get_lseg(lseg);
  933. pnfs_insert_layout(lo, lseg);
  934. if (res->return_on_close) {
  935. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  936. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  937. }
  938. /* Done processing layoutget. Set the layout stateid */
  939. pnfs_set_layout_stateid(lo, &res->stateid, false);
  940. spin_unlock(&ino->i_lock);
  941. out:
  942. return status;
  943. out_forget_reply:
  944. spin_unlock(&ino->i_lock);
  945. lseg->pls_layout = lo;
  946. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  947. goto out;
  948. }
  949. void
  950. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  951. {
  952. BUG_ON(pgio->pg_lseg != NULL);
  953. if (req->wb_offset != req->wb_pgbase) {
  954. nfs_pageio_reset_read_mds(pgio);
  955. return;
  956. }
  957. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  958. req->wb_context,
  959. req_offset(req),
  960. req->wb_bytes,
  961. IOMODE_READ,
  962. GFP_KERNEL);
  963. /* If no lseg, fall back to read through mds */
  964. if (pgio->pg_lseg == NULL)
  965. nfs_pageio_reset_read_mds(pgio);
  966. }
  967. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  968. void
  969. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  970. {
  971. BUG_ON(pgio->pg_lseg != NULL);
  972. if (req->wb_offset != req->wb_pgbase) {
  973. nfs_pageio_reset_write_mds(pgio);
  974. return;
  975. }
  976. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  977. req->wb_context,
  978. req_offset(req),
  979. req->wb_bytes,
  980. IOMODE_RW,
  981. GFP_NOFS);
  982. /* If no lseg, fall back to write through mds */
  983. if (pgio->pg_lseg == NULL)
  984. nfs_pageio_reset_write_mds(pgio);
  985. }
  986. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  987. bool
  988. pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  989. const struct nfs_pgio_completion_ops *compl_ops)
  990. {
  991. struct nfs_server *server = NFS_SERVER(inode);
  992. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  993. if (ld == NULL)
  994. return false;
  995. nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops,
  996. server->rsize, 0);
  997. return true;
  998. }
  999. bool
  1000. pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  1001. int ioflags,
  1002. const struct nfs_pgio_completion_ops *compl_ops)
  1003. {
  1004. struct nfs_server *server = NFS_SERVER(inode);
  1005. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  1006. if (ld == NULL)
  1007. return false;
  1008. nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops,
  1009. server->wsize, ioflags);
  1010. return true;
  1011. }
  1012. bool
  1013. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  1014. struct nfs_page *req)
  1015. {
  1016. if (pgio->pg_lseg == NULL)
  1017. return nfs_generic_pg_test(pgio, prev, req);
  1018. /*
  1019. * Test if a nfs_page is fully contained in the pnfs_layout_range.
  1020. * Note that this test makes several assumptions:
  1021. * - that the previous nfs_page in the struct nfs_pageio_descriptor
  1022. * is known to lie within the range.
  1023. * - that the nfs_page being tested is known to be contiguous with the
  1024. * previous nfs_page.
  1025. * - Layout ranges are page aligned, so we only have to test the
  1026. * start offset of the request.
  1027. *
  1028. * Please also note that 'end_offset' is actually the offset of the
  1029. * first byte that lies outside the pnfs_layout_range. FIXME?
  1030. *
  1031. */
  1032. return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
  1033. pgio->pg_lseg->pls_range.length);
  1034. }
  1035. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  1036. int pnfs_write_done_resend_to_mds(struct inode *inode,
  1037. struct list_head *head,
  1038. const struct nfs_pgio_completion_ops *compl_ops)
  1039. {
  1040. struct nfs_pageio_descriptor pgio;
  1041. LIST_HEAD(failed);
  1042. /* Resend all requests through the MDS */
  1043. nfs_pageio_init_write_mds(&pgio, inode, FLUSH_STABLE, compl_ops);
  1044. while (!list_empty(head)) {
  1045. struct nfs_page *req = nfs_list_entry(head->next);
  1046. nfs_list_remove_request(req);
  1047. if (!nfs_pageio_add_request(&pgio, req))
  1048. nfs_list_add_request(req, &failed);
  1049. }
  1050. nfs_pageio_complete(&pgio);
  1051. if (!list_empty(&failed)) {
  1052. /* For some reason our attempt to resend pages. Mark the
  1053. * overall send request as having failed, and let
  1054. * nfs_writeback_release_full deal with the error.
  1055. */
  1056. list_move(&failed, head);
  1057. return -EIO;
  1058. }
  1059. return 0;
  1060. }
  1061. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  1062. static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
  1063. {
  1064. struct nfs_pgio_header *hdr = data->header;
  1065. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  1066. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1067. PNFS_LAYOUTRET_ON_ERROR) {
  1068. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1069. pnfs_return_layout(hdr->inode);
  1070. }
  1071. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1072. data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
  1073. &hdr->pages,
  1074. hdr->completion_ops);
  1075. }
  1076. /*
  1077. * Called by non rpc-based layout drivers
  1078. */
  1079. void pnfs_ld_write_done(struct nfs_write_data *data)
  1080. {
  1081. struct nfs_pgio_header *hdr = data->header;
  1082. if (!hdr->pnfs_error) {
  1083. pnfs_set_layoutcommit(data);
  1084. hdr->mds_ops->rpc_call_done(&data->task, data);
  1085. } else
  1086. pnfs_ld_handle_write_error(data);
  1087. hdr->mds_ops->rpc_release(data);
  1088. }
  1089. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  1090. static void
  1091. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  1092. struct nfs_write_data *data)
  1093. {
  1094. struct nfs_pgio_header *hdr = data->header;
  1095. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1096. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1097. nfs_pageio_reset_write_mds(desc);
  1098. desc->pg_recoalesce = 1;
  1099. }
  1100. nfs_writedata_release(data);
  1101. }
  1102. static enum pnfs_try_status
  1103. pnfs_try_to_write_data(struct nfs_write_data *wdata,
  1104. const struct rpc_call_ops *call_ops,
  1105. struct pnfs_layout_segment *lseg,
  1106. int how)
  1107. {
  1108. struct nfs_pgio_header *hdr = wdata->header;
  1109. struct inode *inode = hdr->inode;
  1110. enum pnfs_try_status trypnfs;
  1111. struct nfs_server *nfss = NFS_SERVER(inode);
  1112. hdr->mds_ops = call_ops;
  1113. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  1114. inode->i_ino, wdata->args.count, wdata->args.offset, how);
  1115. trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
  1116. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1117. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  1118. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1119. return trypnfs;
  1120. }
  1121. static void
  1122. pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
  1123. {
  1124. struct nfs_write_data *data;
  1125. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1126. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1127. desc->pg_lseg = NULL;
  1128. while (!list_empty(head)) {
  1129. enum pnfs_try_status trypnfs;
  1130. data = list_first_entry(head, struct nfs_write_data, list);
  1131. list_del_init(&data->list);
  1132. trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
  1133. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1134. pnfs_write_through_mds(desc, data);
  1135. }
  1136. put_lseg(lseg);
  1137. }
  1138. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  1139. {
  1140. put_lseg(hdr->lseg);
  1141. nfs_writehdr_free(hdr);
  1142. }
  1143. int
  1144. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1145. {
  1146. struct nfs_write_header *whdr;
  1147. struct nfs_pgio_header *hdr;
  1148. int ret;
  1149. whdr = nfs_writehdr_alloc();
  1150. if (!whdr) {
  1151. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1152. put_lseg(desc->pg_lseg);
  1153. desc->pg_lseg = NULL;
  1154. return -ENOMEM;
  1155. }
  1156. hdr = &whdr->header;
  1157. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  1158. hdr->lseg = get_lseg(desc->pg_lseg);
  1159. atomic_inc(&hdr->refcnt);
  1160. ret = nfs_generic_flush(desc, hdr);
  1161. if (ret != 0) {
  1162. put_lseg(desc->pg_lseg);
  1163. desc->pg_lseg = NULL;
  1164. } else
  1165. pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
  1166. if (atomic_dec_and_test(&hdr->refcnt))
  1167. hdr->completion_ops->completion(hdr);
  1168. return ret;
  1169. }
  1170. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  1171. int pnfs_read_done_resend_to_mds(struct inode *inode,
  1172. struct list_head *head,
  1173. const struct nfs_pgio_completion_ops *compl_ops)
  1174. {
  1175. struct nfs_pageio_descriptor pgio;
  1176. LIST_HEAD(failed);
  1177. /* Resend all requests through the MDS */
  1178. nfs_pageio_init_read_mds(&pgio, inode, compl_ops);
  1179. while (!list_empty(head)) {
  1180. struct nfs_page *req = nfs_list_entry(head->next);
  1181. nfs_list_remove_request(req);
  1182. if (!nfs_pageio_add_request(&pgio, req))
  1183. nfs_list_add_request(req, &failed);
  1184. }
  1185. nfs_pageio_complete(&pgio);
  1186. if (!list_empty(&failed)) {
  1187. list_move(&failed, head);
  1188. return -EIO;
  1189. }
  1190. return 0;
  1191. }
  1192. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  1193. static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
  1194. {
  1195. struct nfs_pgio_header *hdr = data->header;
  1196. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  1197. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1198. PNFS_LAYOUTRET_ON_ERROR) {
  1199. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1200. pnfs_return_layout(hdr->inode);
  1201. }
  1202. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1203. data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
  1204. &hdr->pages,
  1205. hdr->completion_ops);
  1206. }
  1207. /*
  1208. * Called by non rpc-based layout drivers
  1209. */
  1210. void pnfs_ld_read_done(struct nfs_read_data *data)
  1211. {
  1212. struct nfs_pgio_header *hdr = data->header;
  1213. if (likely(!hdr->pnfs_error)) {
  1214. __nfs4_read_done_cb(data);
  1215. hdr->mds_ops->rpc_call_done(&data->task, data);
  1216. } else
  1217. pnfs_ld_handle_read_error(data);
  1218. hdr->mds_ops->rpc_release(data);
  1219. }
  1220. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  1221. static void
  1222. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  1223. struct nfs_read_data *data)
  1224. {
  1225. struct nfs_pgio_header *hdr = data->header;
  1226. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1227. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1228. nfs_pageio_reset_read_mds(desc);
  1229. desc->pg_recoalesce = 1;
  1230. }
  1231. nfs_readdata_release(data);
  1232. }
  1233. /*
  1234. * Call the appropriate parallel I/O subsystem read function.
  1235. */
  1236. static enum pnfs_try_status
  1237. pnfs_try_to_read_data(struct nfs_read_data *rdata,
  1238. const struct rpc_call_ops *call_ops,
  1239. struct pnfs_layout_segment *lseg)
  1240. {
  1241. struct nfs_pgio_header *hdr = rdata->header;
  1242. struct inode *inode = hdr->inode;
  1243. struct nfs_server *nfss = NFS_SERVER(inode);
  1244. enum pnfs_try_status trypnfs;
  1245. hdr->mds_ops = call_ops;
  1246. dprintk("%s: Reading ino:%lu %u@%llu\n",
  1247. __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
  1248. trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
  1249. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1250. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  1251. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1252. return trypnfs;
  1253. }
  1254. static void
  1255. pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
  1256. {
  1257. struct nfs_read_data *data;
  1258. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1259. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1260. desc->pg_lseg = NULL;
  1261. while (!list_empty(head)) {
  1262. enum pnfs_try_status trypnfs;
  1263. data = list_first_entry(head, struct nfs_read_data, list);
  1264. list_del_init(&data->list);
  1265. trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
  1266. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1267. pnfs_read_through_mds(desc, data);
  1268. }
  1269. put_lseg(lseg);
  1270. }
  1271. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  1272. {
  1273. put_lseg(hdr->lseg);
  1274. nfs_readhdr_free(hdr);
  1275. }
  1276. int
  1277. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  1278. {
  1279. struct nfs_read_header *rhdr;
  1280. struct nfs_pgio_header *hdr;
  1281. int ret;
  1282. rhdr = nfs_readhdr_alloc();
  1283. if (!rhdr) {
  1284. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1285. ret = -ENOMEM;
  1286. put_lseg(desc->pg_lseg);
  1287. desc->pg_lseg = NULL;
  1288. return ret;
  1289. }
  1290. hdr = &rhdr->header;
  1291. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  1292. hdr->lseg = get_lseg(desc->pg_lseg);
  1293. atomic_inc(&hdr->refcnt);
  1294. ret = nfs_generic_pagein(desc, hdr);
  1295. if (ret != 0) {
  1296. put_lseg(desc->pg_lseg);
  1297. desc->pg_lseg = NULL;
  1298. } else
  1299. pnfs_do_multiple_reads(desc, &hdr->rpc_list);
  1300. if (atomic_dec_and_test(&hdr->refcnt))
  1301. hdr->completion_ops->completion(hdr);
  1302. return ret;
  1303. }
  1304. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  1305. /*
  1306. * There can be multiple RW segments.
  1307. */
  1308. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  1309. {
  1310. struct pnfs_layout_segment *lseg;
  1311. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  1312. if (lseg->pls_range.iomode == IOMODE_RW &&
  1313. test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1314. list_add(&lseg->pls_lc_list, listp);
  1315. }
  1316. }
  1317. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  1318. {
  1319. if (lseg->pls_range.iomode == IOMODE_RW) {
  1320. dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
  1321. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  1322. } else {
  1323. dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
  1324. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  1325. }
  1326. }
  1327. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  1328. void
  1329. pnfs_set_layoutcommit(struct nfs_write_data *wdata)
  1330. {
  1331. struct nfs_pgio_header *hdr = wdata->header;
  1332. struct inode *inode = hdr->inode;
  1333. struct nfs_inode *nfsi = NFS_I(inode);
  1334. loff_t end_pos = wdata->mds_offset + wdata->res.count;
  1335. bool mark_as_dirty = false;
  1336. spin_lock(&inode->i_lock);
  1337. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1338. mark_as_dirty = true;
  1339. dprintk("%s: Set layoutcommit for inode %lu ",
  1340. __func__, inode->i_ino);
  1341. }
  1342. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
  1343. /* references matched in nfs4_layoutcommit_release */
  1344. get_lseg(hdr->lseg);
  1345. }
  1346. if (end_pos > nfsi->layout->plh_lwb)
  1347. nfsi->layout->plh_lwb = end_pos;
  1348. spin_unlock(&inode->i_lock);
  1349. dprintk("%s: lseg %p end_pos %llu\n",
  1350. __func__, hdr->lseg, nfsi->layout->plh_lwb);
  1351. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  1352. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  1353. if (mark_as_dirty)
  1354. mark_inode_dirty_sync(inode);
  1355. }
  1356. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  1357. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  1358. {
  1359. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  1360. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  1361. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  1362. }
  1363. /*
  1364. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  1365. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  1366. * data to disk to allow the server to recover the data if it crashes.
  1367. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  1368. * is off, and a COMMIT is sent to a data server, or
  1369. * if WRITEs to a data server return NFS_DATA_SYNC.
  1370. */
  1371. int
  1372. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  1373. {
  1374. struct nfs4_layoutcommit_data *data;
  1375. struct nfs_inode *nfsi = NFS_I(inode);
  1376. loff_t end_pos;
  1377. int status = 0;
  1378. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  1379. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1380. return 0;
  1381. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  1382. data = kzalloc(sizeof(*data), GFP_NOFS);
  1383. if (!data) {
  1384. status = -ENOMEM;
  1385. goto out;
  1386. }
  1387. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1388. goto out_free;
  1389. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  1390. if (!sync) {
  1391. status = -EAGAIN;
  1392. goto out_free;
  1393. }
  1394. status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
  1395. nfs_wait_bit_killable, TASK_KILLABLE);
  1396. if (status)
  1397. goto out_free;
  1398. }
  1399. INIT_LIST_HEAD(&data->lseg_list);
  1400. spin_lock(&inode->i_lock);
  1401. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1402. clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
  1403. spin_unlock(&inode->i_lock);
  1404. wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
  1405. goto out_free;
  1406. }
  1407. pnfs_list_write_lseg(inode, &data->lseg_list);
  1408. end_pos = nfsi->layout->plh_lwb;
  1409. nfsi->layout->plh_lwb = 0;
  1410. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  1411. spin_unlock(&inode->i_lock);
  1412. data->args.inode = inode;
  1413. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  1414. nfs_fattr_init(&data->fattr);
  1415. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  1416. data->res.fattr = &data->fattr;
  1417. data->args.lastbytewritten = end_pos - 1;
  1418. data->res.server = NFS_SERVER(inode);
  1419. status = nfs4_proc_layoutcommit(data, sync);
  1420. out:
  1421. if (status)
  1422. mark_inode_dirty_sync(inode);
  1423. dprintk("<-- %s status %d\n", __func__, status);
  1424. return status;
  1425. out_free:
  1426. kfree(data);
  1427. goto out;
  1428. }