xfs_buf.c 40 KB

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