xfs_buf.c 38 KB

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