pnfs.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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. /*
  403. * Called by the state manger to remove all layouts established under an
  404. * expired lease.
  405. */
  406. void
  407. pnfs_destroy_all_layouts(struct nfs_client *clp)
  408. {
  409. struct nfs_server *server;
  410. struct pnfs_layout_hdr *lo;
  411. LIST_HEAD(tmp_list);
  412. nfs4_deviceid_mark_client_invalid(clp);
  413. nfs4_deviceid_purge_client(clp);
  414. spin_lock(&clp->cl_lock);
  415. rcu_read_lock();
  416. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  417. if (!list_empty(&server->layouts))
  418. list_splice_init(&server->layouts, &tmp_list);
  419. }
  420. rcu_read_unlock();
  421. spin_unlock(&clp->cl_lock);
  422. while (!list_empty(&tmp_list)) {
  423. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  424. plh_layouts);
  425. dprintk("%s freeing layout for inode %lu\n", __func__,
  426. lo->plh_inode->i_ino);
  427. list_del_init(&lo->plh_layouts);
  428. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  429. }
  430. }
  431. /* update lo->plh_stateid with new if is more recent */
  432. void
  433. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  434. bool update_barrier)
  435. {
  436. u32 oldseq, newseq;
  437. oldseq = be32_to_cpu(lo->plh_stateid.seqid);
  438. newseq = be32_to_cpu(new->seqid);
  439. if ((int)(newseq - oldseq) > 0) {
  440. nfs4_stateid_copy(&lo->plh_stateid, new);
  441. if (update_barrier) {
  442. u32 new_barrier = be32_to_cpu(new->seqid);
  443. if ((int)(new_barrier - lo->plh_barrier))
  444. lo->plh_barrier = new_barrier;
  445. } else {
  446. /* Because of wraparound, we want to keep the barrier
  447. * "close" to the current seqids. It needs to be
  448. * within 2**31 to count as "behind", so if it
  449. * gets too near that limit, give us a litle leeway
  450. * and bring it to within 2**30.
  451. * NOTE - and yes, this is all unsigned arithmetic.
  452. */
  453. if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
  454. lo->plh_barrier = newseq - (1 << 30);
  455. }
  456. }
  457. }
  458. /* lget is set to 1 if called from inside send_layoutget call chain */
  459. static bool
  460. pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
  461. int lget)
  462. {
  463. if ((stateid) &&
  464. (int)(lo->plh_barrier - be32_to_cpu(stateid->seqid)) >= 0)
  465. return true;
  466. return lo->plh_block_lgets ||
  467. test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
  468. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  469. (list_empty(&lo->plh_segs) &&
  470. (atomic_read(&lo->plh_outstanding) > lget));
  471. }
  472. int
  473. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  474. struct nfs4_state *open_state)
  475. {
  476. int status = 0;
  477. dprintk("--> %s\n", __func__);
  478. spin_lock(&lo->plh_inode->i_lock);
  479. if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
  480. status = -EAGAIN;
  481. } else if (list_empty(&lo->plh_segs)) {
  482. int seq;
  483. do {
  484. seq = read_seqbegin(&open_state->seqlock);
  485. nfs4_stateid_copy(dst, &open_state->stateid);
  486. } while (read_seqretry(&open_state->seqlock, seq));
  487. } else
  488. nfs4_stateid_copy(dst, &lo->plh_stateid);
  489. spin_unlock(&lo->plh_inode->i_lock);
  490. dprintk("<-- %s\n", __func__);
  491. return status;
  492. }
  493. /*
  494. * Get layout from server.
  495. * for now, assume that whole file layouts are requested.
  496. * arg->offset: 0
  497. * arg->length: all ones
  498. */
  499. static struct pnfs_layout_segment *
  500. send_layoutget(struct pnfs_layout_hdr *lo,
  501. struct nfs_open_context *ctx,
  502. struct pnfs_layout_range *range,
  503. gfp_t gfp_flags)
  504. {
  505. struct inode *ino = lo->plh_inode;
  506. struct nfs_server *server = NFS_SERVER(ino);
  507. struct nfs4_layoutget *lgp;
  508. struct pnfs_layout_segment *lseg = NULL;
  509. struct page **pages = NULL;
  510. int i;
  511. u32 max_resp_sz, max_pages;
  512. dprintk("--> %s\n", __func__);
  513. BUG_ON(ctx == NULL);
  514. lgp = kzalloc(sizeof(*lgp), gfp_flags);
  515. if (lgp == NULL)
  516. return NULL;
  517. /* allocate pages for xdr post processing */
  518. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  519. max_pages = nfs_page_array_len(0, max_resp_sz);
  520. pages = kcalloc(max_pages, sizeof(struct page *), gfp_flags);
  521. if (!pages)
  522. goto out_err_free;
  523. for (i = 0; i < max_pages; i++) {
  524. pages[i] = alloc_page(gfp_flags);
  525. if (!pages[i])
  526. goto out_err_free;
  527. }
  528. lgp->args.minlength = PAGE_CACHE_SIZE;
  529. if (lgp->args.minlength > range->length)
  530. lgp->args.minlength = range->length;
  531. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  532. lgp->args.range = *range;
  533. lgp->args.type = server->pnfs_curr_ld->id;
  534. lgp->args.inode = ino;
  535. lgp->args.ctx = get_nfs_open_context(ctx);
  536. lgp->args.layout.pages = pages;
  537. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  538. lgp->lsegpp = &lseg;
  539. lgp->gfp_flags = gfp_flags;
  540. /* Synchronously retrieve layout information from server and
  541. * store in lseg.
  542. */
  543. nfs4_proc_layoutget(lgp);
  544. if (!lseg) {
  545. /* remember that LAYOUTGET failed and suspend trying */
  546. set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
  547. }
  548. /* free xdr pages */
  549. for (i = 0; i < max_pages; i++)
  550. __free_page(pages[i]);
  551. kfree(pages);
  552. return lseg;
  553. out_err_free:
  554. /* free any allocated xdr pages, lgp as it's not used */
  555. if (pages) {
  556. for (i = 0; i < max_pages; i++) {
  557. if (!pages[i])
  558. break;
  559. __free_page(pages[i]);
  560. }
  561. kfree(pages);
  562. }
  563. kfree(lgp);
  564. return NULL;
  565. }
  566. /* Initiates a LAYOUTRETURN(FILE) */
  567. int
  568. _pnfs_return_layout(struct inode *ino)
  569. {
  570. struct pnfs_layout_hdr *lo = NULL;
  571. struct nfs_inode *nfsi = NFS_I(ino);
  572. LIST_HEAD(tmp_list);
  573. struct nfs4_layoutreturn *lrp;
  574. nfs4_stateid stateid;
  575. int status = 0;
  576. dprintk("--> %s\n", __func__);
  577. spin_lock(&ino->i_lock);
  578. lo = nfsi->layout;
  579. if (!lo) {
  580. spin_unlock(&ino->i_lock);
  581. dprintk("%s: no layout to return\n", __func__);
  582. return status;
  583. }
  584. stateid = nfsi->layout->plh_stateid;
  585. /* Reference matched in nfs4_layoutreturn_release */
  586. get_layout_hdr(lo);
  587. mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  588. lo->plh_block_lgets++;
  589. spin_unlock(&ino->i_lock);
  590. pnfs_free_lseg_list(&tmp_list);
  591. WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
  592. lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
  593. if (unlikely(lrp == NULL)) {
  594. status = -ENOMEM;
  595. set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
  596. set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
  597. put_layout_hdr(lo);
  598. goto out;
  599. }
  600. lrp->args.stateid = stateid;
  601. lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
  602. lrp->args.inode = ino;
  603. lrp->args.layout = lo;
  604. lrp->clp = NFS_SERVER(ino)->nfs_client;
  605. status = nfs4_proc_layoutreturn(lrp);
  606. out:
  607. dprintk("<-- %s status: %d\n", __func__, status);
  608. return status;
  609. }
  610. bool pnfs_roc(struct inode *ino)
  611. {
  612. struct pnfs_layout_hdr *lo;
  613. struct pnfs_layout_segment *lseg, *tmp;
  614. LIST_HEAD(tmp_list);
  615. bool found = false;
  616. spin_lock(&ino->i_lock);
  617. lo = NFS_I(ino)->layout;
  618. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  619. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  620. goto out_nolayout;
  621. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  622. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  623. mark_lseg_invalid(lseg, &tmp_list);
  624. found = true;
  625. }
  626. if (!found)
  627. goto out_nolayout;
  628. lo->plh_block_lgets++;
  629. get_layout_hdr(lo); /* matched in pnfs_roc_release */
  630. spin_unlock(&ino->i_lock);
  631. pnfs_free_lseg_list(&tmp_list);
  632. return true;
  633. out_nolayout:
  634. spin_unlock(&ino->i_lock);
  635. return false;
  636. }
  637. void pnfs_roc_release(struct inode *ino)
  638. {
  639. struct pnfs_layout_hdr *lo;
  640. spin_lock(&ino->i_lock);
  641. lo = NFS_I(ino)->layout;
  642. lo->plh_block_lgets--;
  643. put_layout_hdr_locked(lo);
  644. spin_unlock(&ino->i_lock);
  645. }
  646. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  647. {
  648. struct pnfs_layout_hdr *lo;
  649. spin_lock(&ino->i_lock);
  650. lo = NFS_I(ino)->layout;
  651. if ((int)(barrier - lo->plh_barrier) > 0)
  652. lo->plh_barrier = barrier;
  653. spin_unlock(&ino->i_lock);
  654. }
  655. bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
  656. {
  657. struct nfs_inode *nfsi = NFS_I(ino);
  658. struct pnfs_layout_segment *lseg;
  659. bool found = false;
  660. spin_lock(&ino->i_lock);
  661. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  662. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  663. found = true;
  664. break;
  665. }
  666. if (!found) {
  667. struct pnfs_layout_hdr *lo = nfsi->layout;
  668. u32 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
  669. /* Since close does not return a layout stateid for use as
  670. * a barrier, we choose the worst-case barrier.
  671. */
  672. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  673. }
  674. spin_unlock(&ino->i_lock);
  675. return found;
  676. }
  677. /*
  678. * Compare two layout segments for sorting into layout cache.
  679. * We want to preferentially return RW over RO layouts, so ensure those
  680. * are seen first.
  681. */
  682. static s64
  683. cmp_layout(struct pnfs_layout_range *l1,
  684. struct pnfs_layout_range *l2)
  685. {
  686. s64 d;
  687. /* high offset > low offset */
  688. d = l1->offset - l2->offset;
  689. if (d)
  690. return d;
  691. /* short length > long length */
  692. d = l2->length - l1->length;
  693. if (d)
  694. return d;
  695. /* read > read/write */
  696. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  697. }
  698. static void
  699. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  700. struct pnfs_layout_segment *lseg)
  701. {
  702. struct pnfs_layout_segment *lp;
  703. dprintk("%s:Begin\n", __func__);
  704. assert_spin_locked(&lo->plh_inode->i_lock);
  705. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  706. if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
  707. continue;
  708. list_add_tail(&lseg->pls_list, &lp->pls_list);
  709. dprintk("%s: inserted lseg %p "
  710. "iomode %d offset %llu length %llu before "
  711. "lp %p iomode %d offset %llu length %llu\n",
  712. __func__, lseg, lseg->pls_range.iomode,
  713. lseg->pls_range.offset, lseg->pls_range.length,
  714. lp, lp->pls_range.iomode, lp->pls_range.offset,
  715. lp->pls_range.length);
  716. goto out;
  717. }
  718. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  719. dprintk("%s: inserted lseg %p "
  720. "iomode %d offset %llu length %llu at tail\n",
  721. __func__, lseg, lseg->pls_range.iomode,
  722. lseg->pls_range.offset, lseg->pls_range.length);
  723. out:
  724. get_layout_hdr(lo);
  725. dprintk("%s:Return\n", __func__);
  726. }
  727. static struct pnfs_layout_hdr *
  728. alloc_init_layout_hdr(struct inode *ino,
  729. struct nfs_open_context *ctx,
  730. gfp_t gfp_flags)
  731. {
  732. struct pnfs_layout_hdr *lo;
  733. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  734. if (!lo)
  735. return NULL;
  736. atomic_set(&lo->plh_refcount, 1);
  737. INIT_LIST_HEAD(&lo->plh_layouts);
  738. INIT_LIST_HEAD(&lo->plh_segs);
  739. INIT_LIST_HEAD(&lo->plh_bulk_recall);
  740. lo->plh_inode = ino;
  741. lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
  742. return lo;
  743. }
  744. static struct pnfs_layout_hdr *
  745. pnfs_find_alloc_layout(struct inode *ino,
  746. struct nfs_open_context *ctx,
  747. gfp_t gfp_flags)
  748. {
  749. struct nfs_inode *nfsi = NFS_I(ino);
  750. struct pnfs_layout_hdr *new = NULL;
  751. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  752. assert_spin_locked(&ino->i_lock);
  753. if (nfsi->layout) {
  754. if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
  755. return NULL;
  756. else
  757. return nfsi->layout;
  758. }
  759. spin_unlock(&ino->i_lock);
  760. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  761. spin_lock(&ino->i_lock);
  762. if (likely(nfsi->layout == NULL)) /* Won the race? */
  763. nfsi->layout = new;
  764. else
  765. pnfs_free_layout_hdr(new);
  766. return nfsi->layout;
  767. }
  768. /*
  769. * iomode matching rules:
  770. * iomode lseg match
  771. * ----- ----- -----
  772. * ANY READ true
  773. * ANY RW true
  774. * RW READ false
  775. * RW RW true
  776. * READ READ true
  777. * READ RW true
  778. */
  779. static int
  780. is_matching_lseg(struct pnfs_layout_range *ls_range,
  781. struct pnfs_layout_range *range)
  782. {
  783. struct pnfs_layout_range range1;
  784. if ((range->iomode == IOMODE_RW &&
  785. ls_range->iomode != IOMODE_RW) ||
  786. !lo_seg_intersecting(ls_range, range))
  787. return 0;
  788. /* range1 covers only the first byte in the range */
  789. range1 = *range;
  790. range1.length = 1;
  791. return lo_seg_contained(ls_range, &range1);
  792. }
  793. /*
  794. * lookup range in layout
  795. */
  796. static struct pnfs_layout_segment *
  797. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  798. struct pnfs_layout_range *range)
  799. {
  800. struct pnfs_layout_segment *lseg, *ret = NULL;
  801. dprintk("%s:Begin\n", __func__);
  802. assert_spin_locked(&lo->plh_inode->i_lock);
  803. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  804. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  805. is_matching_lseg(&lseg->pls_range, range)) {
  806. ret = get_lseg(lseg);
  807. break;
  808. }
  809. if (lseg->pls_range.offset > range->offset)
  810. break;
  811. }
  812. dprintk("%s:Return lseg %p ref %d\n",
  813. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  814. return ret;
  815. }
  816. /*
  817. * Layout segment is retreived from the server if not cached.
  818. * The appropriate layout segment is referenced and returned to the caller.
  819. */
  820. struct pnfs_layout_segment *
  821. pnfs_update_layout(struct inode *ino,
  822. struct nfs_open_context *ctx,
  823. loff_t pos,
  824. u64 count,
  825. enum pnfs_iomode iomode,
  826. gfp_t gfp_flags)
  827. {
  828. struct pnfs_layout_range arg = {
  829. .iomode = iomode,
  830. .offset = pos,
  831. .length = count,
  832. };
  833. unsigned pg_offset;
  834. struct nfs_inode *nfsi = NFS_I(ino);
  835. struct nfs_server *server = NFS_SERVER(ino);
  836. struct nfs_client *clp = server->nfs_client;
  837. struct pnfs_layout_hdr *lo;
  838. struct pnfs_layout_segment *lseg = NULL;
  839. bool first = false;
  840. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  841. return NULL;
  842. spin_lock(&ino->i_lock);
  843. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  844. if (lo == NULL) {
  845. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  846. goto out_unlock;
  847. }
  848. /* Do we even need to bother with this? */
  849. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  850. dprintk("%s matches recall, use MDS\n", __func__);
  851. goto out_unlock;
  852. }
  853. /* if LAYOUTGET already failed once we don't try again */
  854. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  855. goto out_unlock;
  856. /* Check to see if the layout for the given range already exists */
  857. lseg = pnfs_find_lseg(lo, &arg);
  858. if (lseg)
  859. goto out_unlock;
  860. if (pnfs_layoutgets_blocked(lo, NULL, 0))
  861. goto out_unlock;
  862. atomic_inc(&lo->plh_outstanding);
  863. get_layout_hdr(lo);
  864. if (list_empty(&lo->plh_segs))
  865. first = true;
  866. spin_unlock(&ino->i_lock);
  867. if (first) {
  868. /* The lo must be on the clp list if there is any
  869. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  870. */
  871. spin_lock(&clp->cl_lock);
  872. BUG_ON(!list_empty(&lo->plh_layouts));
  873. list_add_tail(&lo->plh_layouts, &server->layouts);
  874. spin_unlock(&clp->cl_lock);
  875. }
  876. pg_offset = arg.offset & ~PAGE_CACHE_MASK;
  877. if (pg_offset) {
  878. arg.offset -= pg_offset;
  879. arg.length += pg_offset;
  880. }
  881. if (arg.length != NFS4_MAX_UINT64)
  882. arg.length = PAGE_CACHE_ALIGN(arg.length);
  883. lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
  884. if (!lseg && first) {
  885. spin_lock(&clp->cl_lock);
  886. list_del_init(&lo->plh_layouts);
  887. spin_unlock(&clp->cl_lock);
  888. }
  889. atomic_dec(&lo->plh_outstanding);
  890. put_layout_hdr(lo);
  891. out:
  892. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  893. nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
  894. return lseg;
  895. out_unlock:
  896. spin_unlock(&ino->i_lock);
  897. goto out;
  898. }
  899. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  900. int
  901. pnfs_layout_process(struct nfs4_layoutget *lgp)
  902. {
  903. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  904. struct nfs4_layoutget_res *res = &lgp->res;
  905. struct pnfs_layout_segment *lseg;
  906. struct inode *ino = lo->plh_inode;
  907. int status = 0;
  908. /* Inject layout blob into I/O device driver */
  909. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  910. if (!lseg || IS_ERR(lseg)) {
  911. if (!lseg)
  912. status = -ENOMEM;
  913. else
  914. status = PTR_ERR(lseg);
  915. dprintk("%s: Could not allocate layout: error %d\n",
  916. __func__, status);
  917. goto out;
  918. }
  919. spin_lock(&ino->i_lock);
  920. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  921. dprintk("%s forget reply due to recall\n", __func__);
  922. goto out_forget_reply;
  923. }
  924. if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
  925. dprintk("%s forget reply due to state\n", __func__);
  926. goto out_forget_reply;
  927. }
  928. init_lseg(lo, lseg);
  929. lseg->pls_range = res->range;
  930. *lgp->lsegpp = get_lseg(lseg);
  931. pnfs_insert_layout(lo, lseg);
  932. if (res->return_on_close) {
  933. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  934. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  935. }
  936. /* Done processing layoutget. Set the layout stateid */
  937. pnfs_set_layout_stateid(lo, &res->stateid, false);
  938. spin_unlock(&ino->i_lock);
  939. out:
  940. return status;
  941. out_forget_reply:
  942. spin_unlock(&ino->i_lock);
  943. lseg->pls_layout = lo;
  944. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  945. goto out;
  946. }
  947. void
  948. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  949. {
  950. BUG_ON(pgio->pg_lseg != NULL);
  951. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  952. req->wb_context,
  953. req_offset(req),
  954. req->wb_bytes,
  955. IOMODE_READ,
  956. GFP_KERNEL);
  957. /* If no lseg, fall back to read through mds */
  958. if (pgio->pg_lseg == NULL)
  959. nfs_pageio_reset_read_mds(pgio);
  960. }
  961. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  962. void
  963. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  964. {
  965. BUG_ON(pgio->pg_lseg != NULL);
  966. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  967. req->wb_context,
  968. req_offset(req),
  969. req->wb_bytes,
  970. IOMODE_RW,
  971. GFP_NOFS);
  972. /* If no lseg, fall back to write through mds */
  973. if (pgio->pg_lseg == NULL)
  974. nfs_pageio_reset_write_mds(pgio);
  975. }
  976. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  977. bool
  978. pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  979. const struct nfs_pgio_completion_ops *compl_ops)
  980. {
  981. struct nfs_server *server = NFS_SERVER(inode);
  982. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  983. if (ld == NULL)
  984. return false;
  985. nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops,
  986. server->rsize, 0);
  987. return true;
  988. }
  989. bool
  990. pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  991. int ioflags,
  992. const struct nfs_pgio_completion_ops *compl_ops)
  993. {
  994. struct nfs_server *server = NFS_SERVER(inode);
  995. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  996. if (ld == NULL)
  997. return false;
  998. nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops,
  999. server->wsize, ioflags);
  1000. return true;
  1001. }
  1002. bool
  1003. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  1004. struct nfs_page *req)
  1005. {
  1006. if (pgio->pg_lseg == NULL)
  1007. return nfs_generic_pg_test(pgio, prev, req);
  1008. /*
  1009. * Test if a nfs_page is fully contained in the pnfs_layout_range.
  1010. * Note that this test makes several assumptions:
  1011. * - that the previous nfs_page in the struct nfs_pageio_descriptor
  1012. * is known to lie within the range.
  1013. * - that the nfs_page being tested is known to be contiguous with the
  1014. * previous nfs_page.
  1015. * - Layout ranges are page aligned, so we only have to test the
  1016. * start offset of the request.
  1017. *
  1018. * Please also note that 'end_offset' is actually the offset of the
  1019. * first byte that lies outside the pnfs_layout_range. FIXME?
  1020. *
  1021. */
  1022. return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
  1023. pgio->pg_lseg->pls_range.length);
  1024. }
  1025. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  1026. static int pnfs_write_done_resend_to_mds(struct inode *inode,
  1027. struct list_head *head,
  1028. const struct nfs_pgio_completion_ops *compl_ops)
  1029. {
  1030. struct nfs_pageio_descriptor pgio;
  1031. LIST_HEAD(failed);
  1032. /* Resend all requests through the MDS */
  1033. nfs_pageio_init_write_mds(&pgio, inode, FLUSH_STABLE, compl_ops);
  1034. while (!list_empty(head)) {
  1035. struct nfs_page *req = nfs_list_entry(head->next);
  1036. nfs_list_remove_request(req);
  1037. if (!nfs_pageio_add_request(&pgio, req))
  1038. nfs_list_add_request(req, &failed);
  1039. }
  1040. nfs_pageio_complete(&pgio);
  1041. if (!list_empty(&failed)) {
  1042. /* For some reason our attempt to resend pages. Mark the
  1043. * overall send request as having failed, and let
  1044. * nfs_writeback_release_full deal with the error.
  1045. */
  1046. list_move(&failed, head);
  1047. return -EIO;
  1048. }
  1049. return 0;
  1050. }
  1051. static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
  1052. {
  1053. struct nfs_pgio_header *hdr = data->header;
  1054. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  1055. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1056. PNFS_LAYOUTRET_ON_ERROR) {
  1057. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1058. pnfs_return_layout(hdr->inode);
  1059. }
  1060. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1061. data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
  1062. &hdr->pages,
  1063. hdr->completion_ops);
  1064. }
  1065. /*
  1066. * Called by non rpc-based layout drivers
  1067. */
  1068. void pnfs_ld_write_done(struct nfs_write_data *data)
  1069. {
  1070. struct nfs_pgio_header *hdr = data->header;
  1071. if (!hdr->pnfs_error) {
  1072. pnfs_set_layoutcommit(data);
  1073. hdr->mds_ops->rpc_call_done(&data->task, data);
  1074. } else
  1075. pnfs_ld_handle_write_error(data);
  1076. hdr->mds_ops->rpc_release(data);
  1077. }
  1078. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  1079. static void
  1080. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  1081. struct nfs_write_data *data)
  1082. {
  1083. struct nfs_pgio_header *hdr = data->header;
  1084. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1085. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1086. nfs_pageio_reset_write_mds(desc);
  1087. desc->pg_recoalesce = 1;
  1088. }
  1089. nfs_writedata_release(data);
  1090. }
  1091. static enum pnfs_try_status
  1092. pnfs_try_to_write_data(struct nfs_write_data *wdata,
  1093. const struct rpc_call_ops *call_ops,
  1094. struct pnfs_layout_segment *lseg,
  1095. int how)
  1096. {
  1097. struct nfs_pgio_header *hdr = wdata->header;
  1098. struct inode *inode = hdr->inode;
  1099. enum pnfs_try_status trypnfs;
  1100. struct nfs_server *nfss = NFS_SERVER(inode);
  1101. hdr->mds_ops = call_ops;
  1102. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  1103. inode->i_ino, wdata->args.count, wdata->args.offset, how);
  1104. trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
  1105. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1106. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  1107. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1108. return trypnfs;
  1109. }
  1110. static void
  1111. pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
  1112. {
  1113. struct nfs_write_data *data;
  1114. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1115. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1116. desc->pg_lseg = NULL;
  1117. while (!list_empty(head)) {
  1118. enum pnfs_try_status trypnfs;
  1119. data = list_first_entry(head, struct nfs_write_data, list);
  1120. list_del_init(&data->list);
  1121. trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
  1122. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1123. pnfs_write_through_mds(desc, data);
  1124. }
  1125. put_lseg(lseg);
  1126. }
  1127. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  1128. {
  1129. put_lseg(hdr->lseg);
  1130. nfs_writehdr_free(hdr);
  1131. }
  1132. int
  1133. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1134. {
  1135. struct nfs_write_header *whdr;
  1136. struct nfs_pgio_header *hdr;
  1137. int ret;
  1138. whdr = nfs_writehdr_alloc();
  1139. if (!whdr) {
  1140. desc->pg_completion_ops->error_cleanup(&hdr->pages);
  1141. put_lseg(desc->pg_lseg);
  1142. desc->pg_lseg = NULL;
  1143. return -ENOMEM;
  1144. }
  1145. hdr = &whdr->header;
  1146. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  1147. hdr->lseg = get_lseg(desc->pg_lseg);
  1148. atomic_inc(&hdr->refcnt);
  1149. ret = nfs_generic_flush(desc, hdr);
  1150. if (ret != 0) {
  1151. put_lseg(desc->pg_lseg);
  1152. desc->pg_lseg = NULL;
  1153. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  1154. } else
  1155. pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
  1156. if (atomic_dec_and_test(&hdr->refcnt))
  1157. hdr->completion_ops->completion(hdr);
  1158. return ret;
  1159. }
  1160. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  1161. static int pnfs_read_done_resend_to_mds(struct inode *inode,
  1162. struct list_head *head,
  1163. const struct nfs_pgio_completion_ops *compl_ops)
  1164. {
  1165. struct nfs_pageio_descriptor pgio;
  1166. LIST_HEAD(failed);
  1167. /* Resend all requests through the MDS */
  1168. nfs_pageio_init_read_mds(&pgio, inode, compl_ops);
  1169. while (!list_empty(head)) {
  1170. struct nfs_page *req = nfs_list_entry(head->next);
  1171. nfs_list_remove_request(req);
  1172. if (!nfs_pageio_add_request(&pgio, req))
  1173. nfs_list_add_request(req, &failed);
  1174. }
  1175. nfs_pageio_complete(&pgio);
  1176. if (!list_empty(&failed)) {
  1177. list_move(&failed, head);
  1178. return -EIO;
  1179. }
  1180. return 0;
  1181. }
  1182. static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
  1183. {
  1184. struct nfs_pgio_header *hdr = data->header;
  1185. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  1186. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1187. PNFS_LAYOUTRET_ON_ERROR) {
  1188. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1189. pnfs_return_layout(hdr->inode);
  1190. }
  1191. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1192. data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
  1193. &hdr->pages,
  1194. hdr->completion_ops);
  1195. }
  1196. /*
  1197. * Called by non rpc-based layout drivers
  1198. */
  1199. void pnfs_ld_read_done(struct nfs_read_data *data)
  1200. {
  1201. struct nfs_pgio_header *hdr = data->header;
  1202. if (likely(!hdr->pnfs_error)) {
  1203. __nfs4_read_done_cb(data);
  1204. hdr->mds_ops->rpc_call_done(&data->task, data);
  1205. } else
  1206. pnfs_ld_handle_read_error(data);
  1207. hdr->mds_ops->rpc_release(data);
  1208. }
  1209. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  1210. static void
  1211. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  1212. struct nfs_read_data *data)
  1213. {
  1214. struct nfs_pgio_header *hdr = data->header;
  1215. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1216. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1217. nfs_pageio_reset_read_mds(desc);
  1218. desc->pg_recoalesce = 1;
  1219. }
  1220. nfs_readdata_release(data);
  1221. }
  1222. /*
  1223. * Call the appropriate parallel I/O subsystem read function.
  1224. */
  1225. static enum pnfs_try_status
  1226. pnfs_try_to_read_data(struct nfs_read_data *rdata,
  1227. const struct rpc_call_ops *call_ops,
  1228. struct pnfs_layout_segment *lseg)
  1229. {
  1230. struct nfs_pgio_header *hdr = rdata->header;
  1231. struct inode *inode = hdr->inode;
  1232. struct nfs_server *nfss = NFS_SERVER(inode);
  1233. enum pnfs_try_status trypnfs;
  1234. hdr->mds_ops = call_ops;
  1235. dprintk("%s: Reading ino:%lu %u@%llu\n",
  1236. __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
  1237. trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
  1238. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1239. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  1240. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1241. return trypnfs;
  1242. }
  1243. static void
  1244. pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
  1245. {
  1246. struct nfs_read_data *data;
  1247. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1248. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1249. desc->pg_lseg = NULL;
  1250. while (!list_empty(head)) {
  1251. enum pnfs_try_status trypnfs;
  1252. data = list_first_entry(head, struct nfs_read_data, list);
  1253. list_del_init(&data->list);
  1254. trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
  1255. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1256. pnfs_read_through_mds(desc, data);
  1257. }
  1258. put_lseg(lseg);
  1259. }
  1260. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  1261. {
  1262. put_lseg(hdr->lseg);
  1263. nfs_readhdr_free(hdr);
  1264. }
  1265. int
  1266. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  1267. {
  1268. struct nfs_read_header *rhdr;
  1269. struct nfs_pgio_header *hdr;
  1270. int ret;
  1271. rhdr = nfs_readhdr_alloc();
  1272. if (!rhdr) {
  1273. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1274. ret = -ENOMEM;
  1275. put_lseg(desc->pg_lseg);
  1276. desc->pg_lseg = NULL;
  1277. return ret;
  1278. }
  1279. hdr = &rhdr->header;
  1280. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  1281. hdr->lseg = get_lseg(desc->pg_lseg);
  1282. atomic_inc(&hdr->refcnt);
  1283. ret = nfs_generic_pagein(desc, hdr);
  1284. if (ret != 0) {
  1285. put_lseg(desc->pg_lseg);
  1286. desc->pg_lseg = NULL;
  1287. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  1288. } else
  1289. pnfs_do_multiple_reads(desc, &hdr->rpc_list);
  1290. if (atomic_dec_and_test(&hdr->refcnt))
  1291. hdr->completion_ops->completion(hdr);
  1292. return ret;
  1293. }
  1294. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  1295. /*
  1296. * There can be multiple RW segments.
  1297. */
  1298. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  1299. {
  1300. struct pnfs_layout_segment *lseg;
  1301. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  1302. if (lseg->pls_range.iomode == IOMODE_RW &&
  1303. test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1304. list_add(&lseg->pls_lc_list, listp);
  1305. }
  1306. }
  1307. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  1308. {
  1309. if (lseg->pls_range.iomode == IOMODE_RW) {
  1310. dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
  1311. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  1312. } else {
  1313. dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
  1314. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  1315. }
  1316. }
  1317. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  1318. void
  1319. pnfs_set_layoutcommit(struct nfs_write_data *wdata)
  1320. {
  1321. struct nfs_pgio_header *hdr = wdata->header;
  1322. struct inode *inode = hdr->inode;
  1323. struct nfs_inode *nfsi = NFS_I(inode);
  1324. loff_t end_pos = wdata->mds_offset + wdata->res.count;
  1325. bool mark_as_dirty = false;
  1326. spin_lock(&inode->i_lock);
  1327. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1328. mark_as_dirty = true;
  1329. dprintk("%s: Set layoutcommit for inode %lu ",
  1330. __func__, inode->i_ino);
  1331. }
  1332. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
  1333. /* references matched in nfs4_layoutcommit_release */
  1334. get_lseg(hdr->lseg);
  1335. }
  1336. if (end_pos > nfsi->layout->plh_lwb)
  1337. nfsi->layout->plh_lwb = end_pos;
  1338. spin_unlock(&inode->i_lock);
  1339. dprintk("%s: lseg %p end_pos %llu\n",
  1340. __func__, hdr->lseg, nfsi->layout->plh_lwb);
  1341. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  1342. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  1343. if (mark_as_dirty)
  1344. mark_inode_dirty_sync(inode);
  1345. }
  1346. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  1347. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  1348. {
  1349. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  1350. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  1351. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  1352. }
  1353. /*
  1354. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  1355. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  1356. * data to disk to allow the server to recover the data if it crashes.
  1357. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  1358. * is off, and a COMMIT is sent to a data server, or
  1359. * if WRITEs to a data server return NFS_DATA_SYNC.
  1360. */
  1361. int
  1362. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  1363. {
  1364. struct nfs4_layoutcommit_data *data;
  1365. struct nfs_inode *nfsi = NFS_I(inode);
  1366. loff_t end_pos;
  1367. int status = 0;
  1368. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  1369. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1370. return 0;
  1371. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  1372. data = kzalloc(sizeof(*data), GFP_NOFS);
  1373. if (!data) {
  1374. status = -ENOMEM;
  1375. goto out;
  1376. }
  1377. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1378. goto out_free;
  1379. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  1380. if (!sync) {
  1381. status = -EAGAIN;
  1382. goto out_free;
  1383. }
  1384. status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
  1385. nfs_wait_bit_killable, TASK_KILLABLE);
  1386. if (status)
  1387. goto out_free;
  1388. }
  1389. INIT_LIST_HEAD(&data->lseg_list);
  1390. spin_lock(&inode->i_lock);
  1391. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1392. clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
  1393. spin_unlock(&inode->i_lock);
  1394. wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
  1395. goto out_free;
  1396. }
  1397. pnfs_list_write_lseg(inode, &data->lseg_list);
  1398. end_pos = nfsi->layout->plh_lwb;
  1399. nfsi->layout->plh_lwb = 0;
  1400. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  1401. spin_unlock(&inode->i_lock);
  1402. data->args.inode = inode;
  1403. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  1404. nfs_fattr_init(&data->fattr);
  1405. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  1406. data->res.fattr = &data->fattr;
  1407. data->args.lastbytewritten = end_pos - 1;
  1408. data->res.server = NFS_SERVER(inode);
  1409. status = nfs4_proc_layoutcommit(data, sync);
  1410. out:
  1411. if (status)
  1412. mark_inode_dirty_sync(inode);
  1413. dprintk("<-- %s status %d\n", __func__, status);
  1414. return status;
  1415. out_free:
  1416. kfree(data);
  1417. goto out;
  1418. }