pnfs.c 47 KB

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