xfs_buf.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include <linux/stddef.h>
  20. #include <linux/errno.h>
  21. #include <linux/gfp.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/init.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/bio.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/percpu.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/hash.h>
  32. #include <linux/kthread.h>
  33. #include <linux/migrate.h>
  34. #include <linux/backing-dev.h>
  35. #include <linux/freezer.h>
  36. #include "xfs_sb.h"
  37. #include "xfs_inum.h"
  38. #include "xfs_log.h"
  39. #include "xfs_ag.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_trace.h"
  42. static kmem_zone_t *xfs_buf_zone;
  43. static struct workqueue_struct *xfslogd_workqueue;
  44. #ifdef XFS_BUF_LOCK_TRACKING
  45. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  46. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  47. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  48. #else
  49. # define XB_SET_OWNER(bp) do { } while (0)
  50. # define XB_CLEAR_OWNER(bp) do { } while (0)
  51. # define XB_GET_OWNER(bp) do { } while (0)
  52. #endif
  53. #define xb_to_gfp(flags) \
  54. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
  55. ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
  56. #define xb_to_km(flags) \
  57. (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
  58. static inline int
  59. xfs_buf_is_vmapped(
  60. struct xfs_buf *bp)
  61. {
  62. /*
  63. * Return true if the buffer is vmapped.
  64. *
  65. * The XBF_MAPPED flag is set if the buffer should be mapped, but the
  66. * code is clever enough to know it doesn't have to map a single page,
  67. * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
  68. */
  69. return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
  70. }
  71. static inline int
  72. xfs_buf_vmap_len(
  73. struct xfs_buf *bp)
  74. {
  75. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  76. }
  77. /*
  78. * xfs_buf_lru_add - add a buffer to the LRU.
  79. *
  80. * The LRU takes a new reference to the buffer so that it will only be freed
  81. * once the shrinker takes the buffer off the LRU.
  82. */
  83. STATIC void
  84. xfs_buf_lru_add(
  85. struct xfs_buf *bp)
  86. {
  87. struct xfs_buftarg *btp = bp->b_target;
  88. spin_lock(&btp->bt_lru_lock);
  89. if (list_empty(&bp->b_lru)) {
  90. atomic_inc(&bp->b_hold);
  91. list_add_tail(&bp->b_lru, &btp->bt_lru);
  92. btp->bt_lru_nr++;
  93. }
  94. spin_unlock(&btp->bt_lru_lock);
  95. }
  96. /*
  97. * xfs_buf_lru_del - remove a buffer from the LRU
  98. *
  99. * The unlocked check is safe here because it only occurs when there are not
  100. * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
  101. * to optimise the shrinker removing the buffer from the LRU and calling
  102. * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
  103. * bt_lru_lock.
  104. */
  105. STATIC void
  106. xfs_buf_lru_del(
  107. struct xfs_buf *bp)
  108. {
  109. struct xfs_buftarg *btp = bp->b_target;
  110. if (list_empty(&bp->b_lru))
  111. return;
  112. spin_lock(&btp->bt_lru_lock);
  113. if (!list_empty(&bp->b_lru)) {
  114. list_del_init(&bp->b_lru);
  115. btp->bt_lru_nr--;
  116. }
  117. spin_unlock(&btp->bt_lru_lock);
  118. }
  119. /*
  120. * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  121. * b_lru_ref count so that the buffer is freed immediately when the buffer
  122. * reference count falls to zero. If the buffer is already on the LRU, we need
  123. * to remove the reference that LRU holds on the buffer.
  124. *
  125. * This prevents build-up of stale buffers on the LRU.
  126. */
  127. void
  128. xfs_buf_stale(
  129. struct xfs_buf *bp)
  130. {
  131. ASSERT(xfs_buf_islocked(bp));
  132. bp->b_flags |= XBF_STALE;
  133. /*
  134. * Clear the delwri status so that a delwri queue walker will not
  135. * flush this buffer to disk now that it is stale. The delwri queue has
  136. * a reference to the buffer, so this is safe to do.
  137. */
  138. bp->b_flags &= ~_XBF_DELWRI_Q;
  139. atomic_set(&(bp)->b_lru_ref, 0);
  140. if (!list_empty(&bp->b_lru)) {
  141. struct xfs_buftarg *btp = bp->b_target;
  142. spin_lock(&btp->bt_lru_lock);
  143. if (!list_empty(&bp->b_lru)) {
  144. list_del_init(&bp->b_lru);
  145. btp->bt_lru_nr--;
  146. atomic_dec(&bp->b_hold);
  147. }
  148. spin_unlock(&btp->bt_lru_lock);
  149. }
  150. ASSERT(atomic_read(&bp->b_hold) >= 1);
  151. }
  152. struct xfs_buf *
  153. xfs_buf_alloc(
  154. struct xfs_buftarg *target,
  155. xfs_daddr_t blkno,
  156. size_t numblks,
  157. xfs_buf_flags_t flags)
  158. {
  159. struct xfs_buf *bp;
  160. bp = kmem_zone_zalloc(xfs_buf_zone, xb_to_km(flags));
  161. if (unlikely(!bp))
  162. return NULL;
  163. /*
  164. * We don't want certain flags to appear in b_flags.
  165. */
  166. flags &= ~(XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
  167. atomic_set(&bp->b_hold, 1);
  168. atomic_set(&bp->b_lru_ref, 1);
  169. init_completion(&bp->b_iowait);
  170. INIT_LIST_HEAD(&bp->b_lru);
  171. INIT_LIST_HEAD(&bp->b_list);
  172. RB_CLEAR_NODE(&bp->b_rbnode);
  173. sema_init(&bp->b_sema, 0); /* held, no waiters */
  174. XB_SET_OWNER(bp);
  175. bp->b_target = target;
  176. /*
  177. * Set length and io_length to the same value initially.
  178. * I/O routines should use io_length, which will be the same in
  179. * most cases but may be reset (e.g. XFS recovery).
  180. */
  181. bp->b_length = numblks;
  182. bp->b_io_length = numblks;
  183. bp->b_flags = flags;
  184. /*
  185. * We do not set the block number here in the buffer because we have not
  186. * finished initialising the buffer. We insert the buffer into the cache
  187. * in this state, so this ensures that we are unable to do IO on a
  188. * buffer that hasn't been fully initialised.
  189. */
  190. bp->b_bn = XFS_BUF_DADDR_NULL;
  191. atomic_set(&bp->b_pin_count, 0);
  192. init_waitqueue_head(&bp->b_waiters);
  193. XFS_STATS_INC(xb_create);
  194. trace_xfs_buf_init(bp, _RET_IP_);
  195. return bp;
  196. }
  197. /*
  198. * Allocate a page array capable of holding a specified number
  199. * of pages, and point the page buf at it.
  200. */
  201. STATIC int
  202. _xfs_buf_get_pages(
  203. xfs_buf_t *bp,
  204. int page_count,
  205. xfs_buf_flags_t flags)
  206. {
  207. /* Make sure that we have a page list */
  208. if (bp->b_pages == NULL) {
  209. bp->b_page_count = page_count;
  210. if (page_count <= XB_PAGES) {
  211. bp->b_pages = bp->b_page_array;
  212. } else {
  213. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  214. page_count, xb_to_km(flags));
  215. if (bp->b_pages == NULL)
  216. return -ENOMEM;
  217. }
  218. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  219. }
  220. return 0;
  221. }
  222. /*
  223. * Frees b_pages if it was allocated.
  224. */
  225. STATIC void
  226. _xfs_buf_free_pages(
  227. xfs_buf_t *bp)
  228. {
  229. if (bp->b_pages != bp->b_page_array) {
  230. kmem_free(bp->b_pages);
  231. bp->b_pages = NULL;
  232. }
  233. }
  234. /*
  235. * Releases the specified buffer.
  236. *
  237. * The modification state of any associated pages is left unchanged.
  238. * The buffer most not be on any hash - use xfs_buf_rele instead for
  239. * hashed and refcounted buffers
  240. */
  241. void
  242. xfs_buf_free(
  243. xfs_buf_t *bp)
  244. {
  245. trace_xfs_buf_free(bp, _RET_IP_);
  246. ASSERT(list_empty(&bp->b_lru));
  247. if (bp->b_flags & _XBF_PAGES) {
  248. uint i;
  249. if (xfs_buf_is_vmapped(bp))
  250. vm_unmap_ram(bp->b_addr - bp->b_offset,
  251. bp->b_page_count);
  252. for (i = 0; i < bp->b_page_count; i++) {
  253. struct page *page = bp->b_pages[i];
  254. __free_page(page);
  255. }
  256. } else if (bp->b_flags & _XBF_KMEM)
  257. kmem_free(bp->b_addr);
  258. _xfs_buf_free_pages(bp);
  259. kmem_zone_free(xfs_buf_zone, bp);
  260. }
  261. /*
  262. * Allocates all the pages for buffer in question and builds it's page list.
  263. */
  264. STATIC int
  265. xfs_buf_allocate_memory(
  266. xfs_buf_t *bp,
  267. uint flags)
  268. {
  269. size_t size;
  270. size_t nbytes, offset;
  271. gfp_t gfp_mask = xb_to_gfp(flags);
  272. unsigned short page_count, i;
  273. xfs_off_t start, end;
  274. int error;
  275. /*
  276. * for buffers that are contained within a single page, just allocate
  277. * the memory from the heap - there's no need for the complexity of
  278. * page arrays to keep allocation down to order 0.
  279. */
  280. size = BBTOB(bp->b_length);
  281. if (size < PAGE_SIZE) {
  282. bp->b_addr = kmem_alloc(size, xb_to_km(flags));
  283. if (!bp->b_addr) {
  284. /* low memory - use alloc_page loop instead */
  285. goto use_alloc_page;
  286. }
  287. if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
  288. ((unsigned long)bp->b_addr & PAGE_MASK)) {
  289. /* b_addr spans two pages - use alloc_page instead */
  290. kmem_free(bp->b_addr);
  291. bp->b_addr = NULL;
  292. goto use_alloc_page;
  293. }
  294. bp->b_offset = offset_in_page(bp->b_addr);
  295. bp->b_pages = bp->b_page_array;
  296. bp->b_pages[0] = virt_to_page(bp->b_addr);
  297. bp->b_page_count = 1;
  298. bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
  299. return 0;
  300. }
  301. use_alloc_page:
  302. start = BBTOB(bp->b_bn) >> PAGE_SHIFT;
  303. end = (BBTOB(bp->b_bn + bp->b_length) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  304. page_count = end - start;
  305. error = _xfs_buf_get_pages(bp, page_count, flags);
  306. if (unlikely(error))
  307. return error;
  308. offset = bp->b_offset;
  309. bp->b_flags |= _XBF_PAGES;
  310. for (i = 0; i < bp->b_page_count; i++) {
  311. struct page *page;
  312. uint retries = 0;
  313. retry:
  314. page = alloc_page(gfp_mask);
  315. if (unlikely(page == NULL)) {
  316. if (flags & XBF_READ_AHEAD) {
  317. bp->b_page_count = i;
  318. error = ENOMEM;
  319. goto out_free_pages;
  320. }
  321. /*
  322. * This could deadlock.
  323. *
  324. * But until all the XFS lowlevel code is revamped to
  325. * handle buffer allocation failures we can't do much.
  326. */
  327. if (!(++retries % 100))
  328. xfs_err(NULL,
  329. "possible memory allocation deadlock in %s (mode:0x%x)",
  330. __func__, gfp_mask);
  331. XFS_STATS_INC(xb_page_retries);
  332. congestion_wait(BLK_RW_ASYNC, HZ/50);
  333. goto retry;
  334. }
  335. XFS_STATS_INC(xb_page_found);
  336. nbytes = min_t(size_t, size, PAGE_SIZE - offset);
  337. size -= nbytes;
  338. bp->b_pages[i] = page;
  339. offset = 0;
  340. }
  341. return 0;
  342. out_free_pages:
  343. for (i = 0; i < bp->b_page_count; i++)
  344. __free_page(bp->b_pages[i]);
  345. return error;
  346. }
  347. /*
  348. * Map buffer into kernel address-space if necessary.
  349. */
  350. STATIC int
  351. _xfs_buf_map_pages(
  352. xfs_buf_t *bp,
  353. uint flags)
  354. {
  355. ASSERT(bp->b_flags & _XBF_PAGES);
  356. if (bp->b_page_count == 1) {
  357. /* A single page buffer is always mappable */
  358. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  359. bp->b_flags |= XBF_MAPPED;
  360. } else if (flags & XBF_MAPPED) {
  361. int retried = 0;
  362. do {
  363. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  364. -1, PAGE_KERNEL);
  365. if (bp->b_addr)
  366. break;
  367. vm_unmap_aliases();
  368. } while (retried++ <= 1);
  369. if (!bp->b_addr)
  370. return -ENOMEM;
  371. bp->b_addr += bp->b_offset;
  372. bp->b_flags |= XBF_MAPPED;
  373. }
  374. return 0;
  375. }
  376. /*
  377. * Finding and Reading Buffers
  378. */
  379. /*
  380. * Look up, and creates if absent, a lockable buffer for
  381. * a given range of an inode. The buffer is returned
  382. * locked. No I/O is implied by this call.
  383. */
  384. xfs_buf_t *
  385. _xfs_buf_find(
  386. struct xfs_buftarg *btp,
  387. xfs_daddr_t blkno,
  388. size_t numblks,
  389. xfs_buf_flags_t flags,
  390. xfs_buf_t *new_bp)
  391. {
  392. size_t numbytes;
  393. struct xfs_perag *pag;
  394. struct rb_node **rbp;
  395. struct rb_node *parent;
  396. xfs_buf_t *bp;
  397. numbytes = BBTOB(numblks);
  398. /* Check for IOs smaller than the sector size / not sector aligned */
  399. ASSERT(!(numbytes < (1 << btp->bt_sshift)));
  400. ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
  401. /* get tree root */
  402. pag = xfs_perag_get(btp->bt_mount,
  403. xfs_daddr_to_agno(btp->bt_mount, blkno));
  404. /* walk tree */
  405. spin_lock(&pag->pag_buf_lock);
  406. rbp = &pag->pag_buf_tree.rb_node;
  407. parent = NULL;
  408. bp = NULL;
  409. while (*rbp) {
  410. parent = *rbp;
  411. bp = rb_entry(parent, struct xfs_buf, b_rbnode);
  412. if (blkno < bp->b_bn)
  413. rbp = &(*rbp)->rb_left;
  414. else if (blkno > bp->b_bn)
  415. rbp = &(*rbp)->rb_right;
  416. else {
  417. /*
  418. * found a block number match. If the range doesn't
  419. * match, the only way this is allowed is if the buffer
  420. * in the cache is stale and the transaction that made
  421. * it stale has not yet committed. i.e. we are
  422. * reallocating a busy extent. Skip this buffer and
  423. * continue searching to the right for an exact match.
  424. */
  425. if (bp->b_length != numblks) {
  426. ASSERT(bp->b_flags & XBF_STALE);
  427. rbp = &(*rbp)->rb_right;
  428. continue;
  429. }
  430. atomic_inc(&bp->b_hold);
  431. goto found;
  432. }
  433. }
  434. /* No match found */
  435. if (new_bp) {
  436. rb_link_node(&new_bp->b_rbnode, parent, rbp);
  437. rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
  438. /* the buffer keeps the perag reference until it is freed */
  439. new_bp->b_pag = pag;
  440. spin_unlock(&pag->pag_buf_lock);
  441. } else {
  442. XFS_STATS_INC(xb_miss_locked);
  443. spin_unlock(&pag->pag_buf_lock);
  444. xfs_perag_put(pag);
  445. }
  446. return new_bp;
  447. found:
  448. spin_unlock(&pag->pag_buf_lock);
  449. xfs_perag_put(pag);
  450. if (!xfs_buf_trylock(bp)) {
  451. if (flags & XBF_TRYLOCK) {
  452. xfs_buf_rele(bp);
  453. XFS_STATS_INC(xb_busy_locked);
  454. return NULL;
  455. }
  456. xfs_buf_lock(bp);
  457. XFS_STATS_INC(xb_get_locked_waited);
  458. }
  459. /*
  460. * if the buffer is stale, clear all the external state associated with
  461. * it. We need to keep flags such as how we allocated the buffer memory
  462. * intact here.
  463. */
  464. if (bp->b_flags & XBF_STALE) {
  465. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  466. bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
  467. }
  468. trace_xfs_buf_find(bp, flags, _RET_IP_);
  469. XFS_STATS_INC(xb_get_locked);
  470. return bp;
  471. }
  472. /*
  473. * Assembles a buffer covering the specified range. The code is optimised for
  474. * cache hits, as metadata intensive workloads will see 3 orders of magnitude
  475. * more hits than misses.
  476. */
  477. struct xfs_buf *
  478. xfs_buf_get(
  479. xfs_buftarg_t *target,
  480. xfs_daddr_t blkno,
  481. size_t numblks,
  482. xfs_buf_flags_t flags)
  483. {
  484. struct xfs_buf *bp;
  485. struct xfs_buf *new_bp;
  486. int error = 0;
  487. bp = _xfs_buf_find(target, blkno, numblks, flags, NULL);
  488. if (likely(bp))
  489. goto found;
  490. new_bp = xfs_buf_alloc(target, blkno, numblks, flags);
  491. if (unlikely(!new_bp))
  492. return NULL;
  493. error = xfs_buf_allocate_memory(new_bp, flags);
  494. if (error) {
  495. kmem_zone_free(xfs_buf_zone, new_bp);
  496. return NULL;
  497. }
  498. bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp);
  499. if (!bp) {
  500. xfs_buf_free(new_bp);
  501. return NULL;
  502. }
  503. if (bp != new_bp)
  504. xfs_buf_free(new_bp);
  505. /*
  506. * Now we have a workable buffer, fill in the block number so
  507. * that we can do IO on it.
  508. */
  509. bp->b_bn = blkno;
  510. bp->b_io_length = bp->b_length;
  511. found:
  512. if (!(bp->b_flags & XBF_MAPPED)) {
  513. error = _xfs_buf_map_pages(bp, flags);
  514. if (unlikely(error)) {
  515. xfs_warn(target->bt_mount,
  516. "%s: failed to map pages\n", __func__);
  517. xfs_buf_relse(bp);
  518. return NULL;
  519. }
  520. }
  521. XFS_STATS_INC(xb_get);
  522. trace_xfs_buf_get(bp, flags, _RET_IP_);
  523. return bp;
  524. }
  525. STATIC int
  526. _xfs_buf_read(
  527. xfs_buf_t *bp,
  528. xfs_buf_flags_t flags)
  529. {
  530. ASSERT(!(flags & XBF_WRITE));
  531. ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
  532. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
  533. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
  534. xfs_buf_iorequest(bp);
  535. if (flags & XBF_ASYNC)
  536. return 0;
  537. return xfs_buf_iowait(bp);
  538. }
  539. xfs_buf_t *
  540. xfs_buf_read(
  541. xfs_buftarg_t *target,
  542. xfs_daddr_t blkno,
  543. size_t numblks,
  544. xfs_buf_flags_t flags)
  545. {
  546. xfs_buf_t *bp;
  547. flags |= XBF_READ;
  548. bp = xfs_buf_get(target, blkno, numblks, flags);
  549. if (bp) {
  550. trace_xfs_buf_read(bp, flags, _RET_IP_);
  551. if (!XFS_BUF_ISDONE(bp)) {
  552. XFS_STATS_INC(xb_get_read);
  553. _xfs_buf_read(bp, flags);
  554. } else if (flags & XBF_ASYNC) {
  555. /*
  556. * Read ahead call which is already satisfied,
  557. * drop the buffer
  558. */
  559. xfs_buf_relse(bp);
  560. return NULL;
  561. } else {
  562. /* We do not want read in the flags */
  563. bp->b_flags &= ~XBF_READ;
  564. }
  565. }
  566. return bp;
  567. }
  568. /*
  569. * If we are not low on memory then do the readahead in a deadlock
  570. * safe manner.
  571. */
  572. void
  573. xfs_buf_readahead(
  574. xfs_buftarg_t *target,
  575. xfs_daddr_t blkno,
  576. size_t numblks)
  577. {
  578. if (bdi_read_congested(target->bt_bdi))
  579. return;
  580. xfs_buf_read(target, blkno, numblks,
  581. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
  582. }
  583. /*
  584. * Read an uncached buffer from disk. Allocates and returns a locked
  585. * buffer containing the disk contents or nothing.
  586. */
  587. struct xfs_buf *
  588. xfs_buf_read_uncached(
  589. struct xfs_buftarg *target,
  590. xfs_daddr_t daddr,
  591. size_t numblks,
  592. int flags)
  593. {
  594. xfs_buf_t *bp;
  595. int error;
  596. bp = xfs_buf_get_uncached(target, numblks, flags);
  597. if (!bp)
  598. return NULL;
  599. /* set up the buffer for a read IO */
  600. XFS_BUF_SET_ADDR(bp, daddr);
  601. XFS_BUF_READ(bp);
  602. xfsbdstrat(target->bt_mount, bp);
  603. error = xfs_buf_iowait(bp);
  604. if (error) {
  605. xfs_buf_relse(bp);
  606. return NULL;
  607. }
  608. return bp;
  609. }
  610. /*
  611. * Return a buffer allocated as an empty buffer and associated to external
  612. * memory via xfs_buf_associate_memory() back to it's empty state.
  613. */
  614. void
  615. xfs_buf_set_empty(
  616. struct xfs_buf *bp,
  617. size_t numblks)
  618. {
  619. if (bp->b_pages)
  620. _xfs_buf_free_pages(bp);
  621. bp->b_pages = NULL;
  622. bp->b_page_count = 0;
  623. bp->b_addr = NULL;
  624. bp->b_length = numblks;
  625. bp->b_io_length = numblks;
  626. bp->b_bn = XFS_BUF_DADDR_NULL;
  627. bp->b_flags &= ~XBF_MAPPED;
  628. }
  629. static inline struct page *
  630. mem_to_page(
  631. void *addr)
  632. {
  633. if ((!is_vmalloc_addr(addr))) {
  634. return virt_to_page(addr);
  635. } else {
  636. return vmalloc_to_page(addr);
  637. }
  638. }
  639. int
  640. xfs_buf_associate_memory(
  641. xfs_buf_t *bp,
  642. void *mem,
  643. size_t len)
  644. {
  645. int rval;
  646. int i = 0;
  647. unsigned long pageaddr;
  648. unsigned long offset;
  649. size_t buflen;
  650. int page_count;
  651. pageaddr = (unsigned long)mem & PAGE_MASK;
  652. offset = (unsigned long)mem - pageaddr;
  653. buflen = PAGE_ALIGN(len + offset);
  654. page_count = buflen >> PAGE_SHIFT;
  655. /* Free any previous set of page pointers */
  656. if (bp->b_pages)
  657. _xfs_buf_free_pages(bp);
  658. bp->b_pages = NULL;
  659. bp->b_addr = mem;
  660. rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
  661. if (rval)
  662. return rval;
  663. bp->b_offset = offset;
  664. for (i = 0; i < bp->b_page_count; i++) {
  665. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  666. pageaddr += PAGE_SIZE;
  667. }
  668. bp->b_io_length = BTOBB(len);
  669. bp->b_length = BTOBB(buflen);
  670. bp->b_flags |= XBF_MAPPED;
  671. return 0;
  672. }
  673. xfs_buf_t *
  674. xfs_buf_get_uncached(
  675. struct xfs_buftarg *target,
  676. size_t numblks,
  677. int flags)
  678. {
  679. unsigned long page_count;
  680. int error, i;
  681. xfs_buf_t *bp;
  682. bp = xfs_buf_alloc(target, 0, numblks, 0);
  683. if (unlikely(bp == NULL))
  684. goto fail;
  685. page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
  686. error = _xfs_buf_get_pages(bp, page_count, 0);
  687. if (error)
  688. goto fail_free_buf;
  689. for (i = 0; i < page_count; i++) {
  690. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  691. if (!bp->b_pages[i])
  692. goto fail_free_mem;
  693. }
  694. bp->b_flags |= _XBF_PAGES;
  695. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  696. if (unlikely(error)) {
  697. xfs_warn(target->bt_mount,
  698. "%s: failed to map pages\n", __func__);
  699. goto fail_free_mem;
  700. }
  701. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  702. return bp;
  703. fail_free_mem:
  704. while (--i >= 0)
  705. __free_page(bp->b_pages[i]);
  706. _xfs_buf_free_pages(bp);
  707. fail_free_buf:
  708. kmem_zone_free(xfs_buf_zone, bp);
  709. fail:
  710. return NULL;
  711. }
  712. /*
  713. * Increment reference count on buffer, to hold the buffer concurrently
  714. * with another thread which may release (free) the buffer asynchronously.
  715. * Must hold the buffer already to call this function.
  716. */
  717. void
  718. xfs_buf_hold(
  719. xfs_buf_t *bp)
  720. {
  721. trace_xfs_buf_hold(bp, _RET_IP_);
  722. atomic_inc(&bp->b_hold);
  723. }
  724. /*
  725. * Releases a hold on the specified buffer. If the
  726. * the hold count is 1, calls xfs_buf_free.
  727. */
  728. void
  729. xfs_buf_rele(
  730. xfs_buf_t *bp)
  731. {
  732. struct xfs_perag *pag = bp->b_pag;
  733. trace_xfs_buf_rele(bp, _RET_IP_);
  734. if (!pag) {
  735. ASSERT(list_empty(&bp->b_lru));
  736. ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
  737. if (atomic_dec_and_test(&bp->b_hold))
  738. xfs_buf_free(bp);
  739. return;
  740. }
  741. ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
  742. ASSERT(atomic_read(&bp->b_hold) > 0);
  743. if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
  744. if (!(bp->b_flags & XBF_STALE) &&
  745. atomic_read(&bp->b_lru_ref)) {
  746. xfs_buf_lru_add(bp);
  747. spin_unlock(&pag->pag_buf_lock);
  748. } else {
  749. xfs_buf_lru_del(bp);
  750. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  751. rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
  752. spin_unlock(&pag->pag_buf_lock);
  753. xfs_perag_put(pag);
  754. xfs_buf_free(bp);
  755. }
  756. }
  757. }
  758. /*
  759. * Lock a buffer object, if it is not already locked.
  760. *
  761. * If we come across a stale, pinned, locked buffer, we know that we are
  762. * being asked to lock a buffer that has been reallocated. Because it is
  763. * pinned, we know that the log has not been pushed to disk and hence it
  764. * will still be locked. Rather than continuing to have trylock attempts
  765. * fail until someone else pushes the log, push it ourselves before
  766. * returning. This means that the xfsaild will not get stuck trying
  767. * to push on stale inode buffers.
  768. */
  769. int
  770. xfs_buf_trylock(
  771. struct xfs_buf *bp)
  772. {
  773. int locked;
  774. locked = down_trylock(&bp->b_sema) == 0;
  775. if (locked)
  776. XB_SET_OWNER(bp);
  777. else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  778. xfs_log_force(bp->b_target->bt_mount, 0);
  779. trace_xfs_buf_trylock(bp, _RET_IP_);
  780. return locked;
  781. }
  782. /*
  783. * Lock a buffer object.
  784. *
  785. * If we come across a stale, pinned, locked buffer, we know that we
  786. * are being asked to lock a buffer that has been reallocated. Because
  787. * it is pinned, we know that the log has not been pushed to disk and
  788. * hence it will still be locked. Rather than sleeping until someone
  789. * else pushes the log, push it ourselves before trying to get the lock.
  790. */
  791. void
  792. xfs_buf_lock(
  793. struct xfs_buf *bp)
  794. {
  795. trace_xfs_buf_lock(bp, _RET_IP_);
  796. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  797. xfs_log_force(bp->b_target->bt_mount, 0);
  798. down(&bp->b_sema);
  799. XB_SET_OWNER(bp);
  800. trace_xfs_buf_lock_done(bp, _RET_IP_);
  801. }
  802. void
  803. xfs_buf_unlock(
  804. struct xfs_buf *bp)
  805. {
  806. XB_CLEAR_OWNER(bp);
  807. up(&bp->b_sema);
  808. trace_xfs_buf_unlock(bp, _RET_IP_);
  809. }
  810. STATIC void
  811. xfs_buf_wait_unpin(
  812. xfs_buf_t *bp)
  813. {
  814. DECLARE_WAITQUEUE (wait, current);
  815. if (atomic_read(&bp->b_pin_count) == 0)
  816. return;
  817. add_wait_queue(&bp->b_waiters, &wait);
  818. for (;;) {
  819. set_current_state(TASK_UNINTERRUPTIBLE);
  820. if (atomic_read(&bp->b_pin_count) == 0)
  821. break;
  822. io_schedule();
  823. }
  824. remove_wait_queue(&bp->b_waiters, &wait);
  825. set_current_state(TASK_RUNNING);
  826. }
  827. /*
  828. * Buffer Utility Routines
  829. */
  830. STATIC void
  831. xfs_buf_iodone_work(
  832. struct work_struct *work)
  833. {
  834. xfs_buf_t *bp =
  835. container_of(work, xfs_buf_t, b_iodone_work);
  836. if (bp->b_iodone)
  837. (*(bp->b_iodone))(bp);
  838. else if (bp->b_flags & XBF_ASYNC)
  839. xfs_buf_relse(bp);
  840. }
  841. void
  842. xfs_buf_ioend(
  843. xfs_buf_t *bp,
  844. int schedule)
  845. {
  846. trace_xfs_buf_iodone(bp, _RET_IP_);
  847. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  848. if (bp->b_error == 0)
  849. bp->b_flags |= XBF_DONE;
  850. if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
  851. if (schedule) {
  852. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  853. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  854. } else {
  855. xfs_buf_iodone_work(&bp->b_iodone_work);
  856. }
  857. } else {
  858. complete(&bp->b_iowait);
  859. }
  860. }
  861. void
  862. xfs_buf_ioerror(
  863. xfs_buf_t *bp,
  864. int error)
  865. {
  866. ASSERT(error >= 0 && error <= 0xffff);
  867. bp->b_error = (unsigned short)error;
  868. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  869. }
  870. void
  871. xfs_buf_ioerror_alert(
  872. struct xfs_buf *bp,
  873. const char *func)
  874. {
  875. xfs_alert(bp->b_target->bt_mount,
  876. "metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
  877. (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
  878. }
  879. int
  880. xfs_bwrite(
  881. struct xfs_buf *bp)
  882. {
  883. int error;
  884. ASSERT(xfs_buf_islocked(bp));
  885. bp->b_flags |= XBF_WRITE;
  886. bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
  887. xfs_bdstrat_cb(bp);
  888. error = xfs_buf_iowait(bp);
  889. if (error) {
  890. xfs_force_shutdown(bp->b_target->bt_mount,
  891. SHUTDOWN_META_IO_ERROR);
  892. }
  893. return error;
  894. }
  895. /*
  896. * Called when we want to stop a buffer from getting written or read.
  897. * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
  898. * so that the proper iodone callbacks get called.
  899. */
  900. STATIC int
  901. xfs_bioerror(
  902. xfs_buf_t *bp)
  903. {
  904. #ifdef XFSERRORDEBUG
  905. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  906. #endif
  907. /*
  908. * No need to wait until the buffer is unpinned, we aren't flushing it.
  909. */
  910. xfs_buf_ioerror(bp, EIO);
  911. /*
  912. * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
  913. */
  914. XFS_BUF_UNREAD(bp);
  915. XFS_BUF_UNDONE(bp);
  916. xfs_buf_stale(bp);
  917. xfs_buf_ioend(bp, 0);
  918. return EIO;
  919. }
  920. /*
  921. * Same as xfs_bioerror, except that we are releasing the buffer
  922. * here ourselves, and avoiding the xfs_buf_ioend call.
  923. * This is meant for userdata errors; metadata bufs come with
  924. * iodone functions attached, so that we can track down errors.
  925. */
  926. STATIC int
  927. xfs_bioerror_relse(
  928. struct xfs_buf *bp)
  929. {
  930. int64_t fl = bp->b_flags;
  931. /*
  932. * No need to wait until the buffer is unpinned.
  933. * We aren't flushing it.
  934. *
  935. * chunkhold expects B_DONE to be set, whether
  936. * we actually finish the I/O or not. We don't want to
  937. * change that interface.
  938. */
  939. XFS_BUF_UNREAD(bp);
  940. XFS_BUF_DONE(bp);
  941. xfs_buf_stale(bp);
  942. bp->b_iodone = NULL;
  943. if (!(fl & XBF_ASYNC)) {
  944. /*
  945. * Mark b_error and B_ERROR _both_.
  946. * Lot's of chunkcache code assumes that.
  947. * There's no reason to mark error for
  948. * ASYNC buffers.
  949. */
  950. xfs_buf_ioerror(bp, EIO);
  951. complete(&bp->b_iowait);
  952. } else {
  953. xfs_buf_relse(bp);
  954. }
  955. return EIO;
  956. }
  957. /*
  958. * All xfs metadata buffers except log state machine buffers
  959. * get this attached as their b_bdstrat callback function.
  960. * This is so that we can catch a buffer
  961. * after prematurely unpinning it to forcibly shutdown the filesystem.
  962. */
  963. int
  964. xfs_bdstrat_cb(
  965. struct xfs_buf *bp)
  966. {
  967. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  968. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  969. /*
  970. * Metadata write that didn't get logged but
  971. * written delayed anyway. These aren't associated
  972. * with a transaction, and can be ignored.
  973. */
  974. if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
  975. return xfs_bioerror_relse(bp);
  976. else
  977. return xfs_bioerror(bp);
  978. }
  979. xfs_buf_iorequest(bp);
  980. return 0;
  981. }
  982. /*
  983. * Wrapper around bdstrat so that we can stop data from going to disk in case
  984. * we are shutting down the filesystem. Typically user data goes thru this
  985. * path; one of the exceptions is the superblock.
  986. */
  987. void
  988. xfsbdstrat(
  989. struct xfs_mount *mp,
  990. struct xfs_buf *bp)
  991. {
  992. if (XFS_FORCED_SHUTDOWN(mp)) {
  993. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  994. xfs_bioerror_relse(bp);
  995. return;
  996. }
  997. xfs_buf_iorequest(bp);
  998. }
  999. STATIC void
  1000. _xfs_buf_ioend(
  1001. xfs_buf_t *bp,
  1002. int schedule)
  1003. {
  1004. if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
  1005. xfs_buf_ioend(bp, schedule);
  1006. }
  1007. STATIC void
  1008. xfs_buf_bio_end_io(
  1009. struct bio *bio,
  1010. int error)
  1011. {
  1012. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  1013. xfs_buf_ioerror(bp, -error);
  1014. if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1015. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1016. _xfs_buf_ioend(bp, 1);
  1017. bio_put(bio);
  1018. }
  1019. STATIC void
  1020. _xfs_buf_ioapply(
  1021. xfs_buf_t *bp)
  1022. {
  1023. int rw, map_i, total_nr_pages, nr_pages;
  1024. struct bio *bio;
  1025. int offset = bp->b_offset;
  1026. int size = BBTOB(bp->b_io_length);
  1027. sector_t sector = bp->b_bn;
  1028. total_nr_pages = bp->b_page_count;
  1029. map_i = 0;
  1030. if (bp->b_flags & XBF_WRITE) {
  1031. if (bp->b_flags & XBF_SYNCIO)
  1032. rw = WRITE_SYNC;
  1033. else
  1034. rw = WRITE;
  1035. if (bp->b_flags & XBF_FUA)
  1036. rw |= REQ_FUA;
  1037. if (bp->b_flags & XBF_FLUSH)
  1038. rw |= REQ_FLUSH;
  1039. } else if (bp->b_flags & XBF_READ_AHEAD) {
  1040. rw = READA;
  1041. } else {
  1042. rw = READ;
  1043. }
  1044. /* we only use the buffer cache for meta-data */
  1045. rw |= REQ_META;
  1046. next_chunk:
  1047. atomic_inc(&bp->b_io_remaining);
  1048. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1049. if (nr_pages > total_nr_pages)
  1050. nr_pages = total_nr_pages;
  1051. bio = bio_alloc(GFP_NOIO, nr_pages);
  1052. bio->bi_bdev = bp->b_target->bt_bdev;
  1053. bio->bi_sector = sector;
  1054. bio->bi_end_io = xfs_buf_bio_end_io;
  1055. bio->bi_private = bp;
  1056. for (; size && nr_pages; nr_pages--, map_i++) {
  1057. int rbytes, nbytes = PAGE_SIZE - offset;
  1058. if (nbytes > size)
  1059. nbytes = size;
  1060. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1061. if (rbytes < nbytes)
  1062. break;
  1063. offset = 0;
  1064. sector += BTOBB(nbytes);
  1065. size -= nbytes;
  1066. total_nr_pages--;
  1067. }
  1068. if (likely(bio->bi_size)) {
  1069. if (xfs_buf_is_vmapped(bp)) {
  1070. flush_kernel_vmap_range(bp->b_addr,
  1071. xfs_buf_vmap_len(bp));
  1072. }
  1073. submit_bio(rw, bio);
  1074. if (size)
  1075. goto next_chunk;
  1076. } else {
  1077. xfs_buf_ioerror(bp, EIO);
  1078. bio_put(bio);
  1079. }
  1080. }
  1081. void
  1082. xfs_buf_iorequest(
  1083. xfs_buf_t *bp)
  1084. {
  1085. trace_xfs_buf_iorequest(bp, _RET_IP_);
  1086. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  1087. if (bp->b_flags & XBF_WRITE)
  1088. xfs_buf_wait_unpin(bp);
  1089. xfs_buf_hold(bp);
  1090. /* Set the count to 1 initially, this will stop an I/O
  1091. * completion callout which happens before we have started
  1092. * all the I/O from calling xfs_buf_ioend too early.
  1093. */
  1094. atomic_set(&bp->b_io_remaining, 1);
  1095. _xfs_buf_ioapply(bp);
  1096. _xfs_buf_ioend(bp, 0);
  1097. xfs_buf_rele(bp);
  1098. }
  1099. /*
  1100. * Waits for I/O to complete on the buffer supplied. It returns immediately if
  1101. * no I/O is pending or there is already a pending error on the buffer. It
  1102. * returns the I/O error code, if any, or 0 if there was no error.
  1103. */
  1104. int
  1105. xfs_buf_iowait(
  1106. xfs_buf_t *bp)
  1107. {
  1108. trace_xfs_buf_iowait(bp, _RET_IP_);
  1109. if (!bp->b_error)
  1110. wait_for_completion(&bp->b_iowait);
  1111. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1112. return bp->b_error;
  1113. }
  1114. xfs_caddr_t
  1115. xfs_buf_offset(
  1116. xfs_buf_t *bp,
  1117. size_t offset)
  1118. {
  1119. struct page *page;
  1120. if (bp->b_flags & XBF_MAPPED)
  1121. return bp->b_addr + offset;
  1122. offset += bp->b_offset;
  1123. page = bp->b_pages[offset >> PAGE_SHIFT];
  1124. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
  1125. }
  1126. /*
  1127. * Move data into or out of a buffer.
  1128. */
  1129. void
  1130. xfs_buf_iomove(
  1131. xfs_buf_t *bp, /* buffer to process */
  1132. size_t boff, /* starting buffer offset */
  1133. size_t bsize, /* length to copy */
  1134. void *data, /* data address */
  1135. xfs_buf_rw_t mode) /* read/write/zero flag */
  1136. {
  1137. size_t bend;
  1138. bend = boff + bsize;
  1139. while (boff < bend) {
  1140. struct page *page;
  1141. int page_index, page_offset, csize;
  1142. page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
  1143. page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
  1144. page = bp->b_pages[page_index];
  1145. csize = min_t(size_t, PAGE_SIZE - page_offset,
  1146. BBTOB(bp->b_io_length) - boff);
  1147. ASSERT((csize + page_offset) <= PAGE_SIZE);
  1148. switch (mode) {
  1149. case XBRW_ZERO:
  1150. memset(page_address(page) + page_offset, 0, csize);
  1151. break;
  1152. case XBRW_READ:
  1153. memcpy(data, page_address(page) + page_offset, csize);
  1154. break;
  1155. case XBRW_WRITE:
  1156. memcpy(page_address(page) + page_offset, data, csize);
  1157. }
  1158. boff += csize;
  1159. data += csize;
  1160. }
  1161. }
  1162. /*
  1163. * Handling of buffer targets (buftargs).
  1164. */
  1165. /*
  1166. * Wait for any bufs with callbacks that have been submitted but have not yet
  1167. * returned. These buffers will have an elevated hold count, so wait on those
  1168. * while freeing all the buffers only held by the LRU.
  1169. */
  1170. void
  1171. xfs_wait_buftarg(
  1172. struct xfs_buftarg *btp)
  1173. {
  1174. struct xfs_buf *bp;
  1175. restart:
  1176. spin_lock(&btp->bt_lru_lock);
  1177. while (!list_empty(&btp->bt_lru)) {
  1178. bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
  1179. if (atomic_read(&bp->b_hold) > 1) {
  1180. spin_unlock(&btp->bt_lru_lock);
  1181. delay(100);
  1182. goto restart;
  1183. }
  1184. /*
  1185. * clear the LRU reference count so the buffer doesn't get
  1186. * ignored in xfs_buf_rele().
  1187. */
  1188. atomic_set(&bp->b_lru_ref, 0);
  1189. spin_unlock(&btp->bt_lru_lock);
  1190. xfs_buf_rele(bp);
  1191. spin_lock(&btp->bt_lru_lock);
  1192. }
  1193. spin_unlock(&btp->bt_lru_lock);
  1194. }
  1195. int
  1196. xfs_buftarg_shrink(
  1197. struct shrinker *shrink,
  1198. struct shrink_control *sc)
  1199. {
  1200. struct xfs_buftarg *btp = container_of(shrink,
  1201. struct xfs_buftarg, bt_shrinker);
  1202. struct xfs_buf *bp;
  1203. int nr_to_scan = sc->nr_to_scan;
  1204. LIST_HEAD(dispose);
  1205. if (!nr_to_scan)
  1206. return btp->bt_lru_nr;
  1207. spin_lock(&btp->bt_lru_lock);
  1208. while (!list_empty(&btp->bt_lru)) {
  1209. if (nr_to_scan-- <= 0)
  1210. break;
  1211. bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
  1212. /*
  1213. * Decrement the b_lru_ref count unless the value is already
  1214. * zero. If the value is already zero, we need to reclaim the
  1215. * buffer, otherwise it gets another trip through the LRU.
  1216. */
  1217. if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
  1218. list_move_tail(&bp->b_lru, &btp->bt_lru);
  1219. continue;
  1220. }
  1221. /*
  1222. * remove the buffer from the LRU now to avoid needing another
  1223. * lock round trip inside xfs_buf_rele().
  1224. */
  1225. list_move(&bp->b_lru, &dispose);
  1226. btp->bt_lru_nr--;
  1227. }
  1228. spin_unlock(&btp->bt_lru_lock);
  1229. while (!list_empty(&dispose)) {
  1230. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1231. list_del_init(&bp->b_lru);
  1232. xfs_buf_rele(bp);
  1233. }
  1234. return btp->bt_lru_nr;
  1235. }
  1236. void
  1237. xfs_free_buftarg(
  1238. struct xfs_mount *mp,
  1239. struct xfs_buftarg *btp)
  1240. {
  1241. unregister_shrinker(&btp->bt_shrinker);
  1242. if (mp->m_flags & XFS_MOUNT_BARRIER)
  1243. xfs_blkdev_issue_flush(btp);
  1244. kmem_free(btp);
  1245. }
  1246. STATIC int
  1247. xfs_setsize_buftarg_flags(
  1248. xfs_buftarg_t *btp,
  1249. unsigned int blocksize,
  1250. unsigned int sectorsize,
  1251. int verbose)
  1252. {
  1253. btp->bt_bsize = blocksize;
  1254. btp->bt_sshift = ffs(sectorsize) - 1;
  1255. btp->bt_smask = sectorsize - 1;
  1256. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1257. char name[BDEVNAME_SIZE];
  1258. bdevname(btp->bt_bdev, name);
  1259. xfs_warn(btp->bt_mount,
  1260. "Cannot set_blocksize to %u on device %s\n",
  1261. sectorsize, name);
  1262. return EINVAL;
  1263. }
  1264. return 0;
  1265. }
  1266. /*
  1267. * When allocating the initial buffer target we have not yet
  1268. * read in the superblock, so don't know what sized sectors
  1269. * are being used is at this early stage. Play safe.
  1270. */
  1271. STATIC int
  1272. xfs_setsize_buftarg_early(
  1273. xfs_buftarg_t *btp,
  1274. struct block_device *bdev)
  1275. {
  1276. return xfs_setsize_buftarg_flags(btp,
  1277. PAGE_SIZE, bdev_logical_block_size(bdev), 0);
  1278. }
  1279. int
  1280. xfs_setsize_buftarg(
  1281. xfs_buftarg_t *btp,
  1282. unsigned int blocksize,
  1283. unsigned int sectorsize)
  1284. {
  1285. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1286. }
  1287. xfs_buftarg_t *
  1288. xfs_alloc_buftarg(
  1289. struct xfs_mount *mp,
  1290. struct block_device *bdev,
  1291. int external,
  1292. const char *fsname)
  1293. {
  1294. xfs_buftarg_t *btp;
  1295. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1296. btp->bt_mount = mp;
  1297. btp->bt_dev = bdev->bd_dev;
  1298. btp->bt_bdev = bdev;
  1299. btp->bt_bdi = blk_get_backing_dev_info(bdev);
  1300. if (!btp->bt_bdi)
  1301. goto error;
  1302. INIT_LIST_HEAD(&btp->bt_lru);
  1303. spin_lock_init(&btp->bt_lru_lock);
  1304. if (xfs_setsize_buftarg_early(btp, bdev))
  1305. goto error;
  1306. btp->bt_shrinker.shrink = xfs_buftarg_shrink;
  1307. btp->bt_shrinker.seeks = DEFAULT_SEEKS;
  1308. register_shrinker(&btp->bt_shrinker);
  1309. return btp;
  1310. error:
  1311. kmem_free(btp);
  1312. return NULL;
  1313. }
  1314. /*
  1315. * Add a buffer to the delayed write list.
  1316. *
  1317. * This queues a buffer for writeout if it hasn't already been. Note that
  1318. * neither this routine nor the buffer list submission functions perform
  1319. * any internal synchronization. It is expected that the lists are thread-local
  1320. * to the callers.
  1321. *
  1322. * Returns true if we queued up the buffer, or false if it already had
  1323. * been on the buffer list.
  1324. */
  1325. bool
  1326. xfs_buf_delwri_queue(
  1327. struct xfs_buf *bp,
  1328. struct list_head *list)
  1329. {
  1330. ASSERT(xfs_buf_islocked(bp));
  1331. ASSERT(!(bp->b_flags & XBF_READ));
  1332. /*
  1333. * If the buffer is already marked delwri it already is queued up
  1334. * by someone else for imediate writeout. Just ignore it in that
  1335. * case.
  1336. */
  1337. if (bp->b_flags & _XBF_DELWRI_Q) {
  1338. trace_xfs_buf_delwri_queued(bp, _RET_IP_);
  1339. return false;
  1340. }
  1341. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1342. /*
  1343. * If a buffer gets written out synchronously or marked stale while it
  1344. * is on a delwri list we lazily remove it. To do this, the other party
  1345. * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
  1346. * It remains referenced and on the list. In a rare corner case it
  1347. * might get readded to a delwri list after the synchronous writeout, in
  1348. * which case we need just need to re-add the flag here.
  1349. */
  1350. bp->b_flags |= _XBF_DELWRI_Q;
  1351. if (list_empty(&bp->b_list)) {
  1352. atomic_inc(&bp->b_hold);
  1353. list_add_tail(&bp->b_list, list);
  1354. }
  1355. return true;
  1356. }
  1357. /*
  1358. * Compare function is more complex than it needs to be because
  1359. * the return value is only 32 bits and we are doing comparisons
  1360. * on 64 bit values
  1361. */
  1362. static int
  1363. xfs_buf_cmp(
  1364. void *priv,
  1365. struct list_head *a,
  1366. struct list_head *b)
  1367. {
  1368. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1369. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1370. xfs_daddr_t diff;
  1371. diff = ap->b_bn - bp->b_bn;
  1372. if (diff < 0)
  1373. return -1;
  1374. if (diff > 0)
  1375. return 1;
  1376. return 0;
  1377. }
  1378. static int
  1379. __xfs_buf_delwri_submit(
  1380. struct list_head *buffer_list,
  1381. struct list_head *io_list,
  1382. bool wait)
  1383. {
  1384. struct blk_plug plug;
  1385. struct xfs_buf *bp, *n;
  1386. int pinned = 0;
  1387. list_for_each_entry_safe(bp, n, buffer_list, b_list) {
  1388. if (!wait) {
  1389. if (xfs_buf_ispinned(bp)) {
  1390. pinned++;
  1391. continue;
  1392. }
  1393. if (!xfs_buf_trylock(bp))
  1394. continue;
  1395. } else {
  1396. xfs_buf_lock(bp);
  1397. }
  1398. /*
  1399. * Someone else might have written the buffer synchronously or
  1400. * marked it stale in the meantime. In that case only the
  1401. * _XBF_DELWRI_Q flag got cleared, and we have to drop the
  1402. * reference and remove it from the list here.
  1403. */
  1404. if (!(bp->b_flags & _XBF_DELWRI_Q)) {
  1405. list_del_init(&bp->b_list);
  1406. xfs_buf_relse(bp);
  1407. continue;
  1408. }
  1409. list_move_tail(&bp->b_list, io_list);
  1410. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1411. }
  1412. list_sort(NULL, io_list, xfs_buf_cmp);
  1413. blk_start_plug(&plug);
  1414. list_for_each_entry_safe(bp, n, io_list, b_list) {
  1415. bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
  1416. bp->b_flags |= XBF_WRITE;
  1417. if (!wait) {
  1418. bp->b_flags |= XBF_ASYNC;
  1419. list_del_init(&bp->b_list);
  1420. }
  1421. xfs_bdstrat_cb(bp);
  1422. }
  1423. blk_finish_plug(&plug);
  1424. return pinned;
  1425. }
  1426. /*
  1427. * Write out a buffer list asynchronously.
  1428. *
  1429. * This will take the @buffer_list, write all non-locked and non-pinned buffers
  1430. * out and not wait for I/O completion on any of the buffers. This interface
  1431. * is only safely useable for callers that can track I/O completion by higher
  1432. * level means, e.g. AIL pushing as the @buffer_list is consumed in this
  1433. * function.
  1434. */
  1435. int
  1436. xfs_buf_delwri_submit_nowait(
  1437. struct list_head *buffer_list)
  1438. {
  1439. LIST_HEAD (io_list);
  1440. return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
  1441. }
  1442. /*
  1443. * Write out a buffer list synchronously.
  1444. *
  1445. * This will take the @buffer_list, write all buffers out and wait for I/O
  1446. * completion on all of the buffers. @buffer_list is consumed by the function,
  1447. * so callers must have some other way of tracking buffers if they require such
  1448. * functionality.
  1449. */
  1450. int
  1451. xfs_buf_delwri_submit(
  1452. struct list_head *buffer_list)
  1453. {
  1454. LIST_HEAD (io_list);
  1455. int error = 0, error2;
  1456. struct xfs_buf *bp;
  1457. __xfs_buf_delwri_submit(buffer_list, &io_list, true);
  1458. /* Wait for IO to complete. */
  1459. while (!list_empty(&io_list)) {
  1460. bp = list_first_entry(&io_list, struct xfs_buf, b_list);
  1461. list_del_init(&bp->b_list);
  1462. error2 = xfs_buf_iowait(bp);
  1463. xfs_buf_relse(bp);
  1464. if (!error)
  1465. error = error2;
  1466. }
  1467. return error;
  1468. }
  1469. int __init
  1470. xfs_buf_init(void)
  1471. {
  1472. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1473. KM_ZONE_HWALIGN, NULL);
  1474. if (!xfs_buf_zone)
  1475. goto out;
  1476. xfslogd_workqueue = alloc_workqueue("xfslogd",
  1477. WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
  1478. if (!xfslogd_workqueue)
  1479. goto out_free_buf_zone;
  1480. return 0;
  1481. out_free_buf_zone:
  1482. kmem_zone_destroy(xfs_buf_zone);
  1483. out:
  1484. return -ENOMEM;
  1485. }
  1486. void
  1487. xfs_buf_terminate(void)
  1488. {
  1489. destroy_workqueue(xfslogd_workqueue);
  1490. kmem_zone_destroy(xfs_buf_zone);
  1491. }