xfs_buf.c 38 KB

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