pnfs.c 47 KB

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