xfs_buf.c 37 KB

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