xfs_buf.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  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. struct workqueue_struct *xfsdatad_workqueue;
  46. struct workqueue_struct *xfsconvertd_workqueue;
  47. #ifdef XFS_BUF_LOCK_TRACKING
  48. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  49. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  50. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  51. #else
  52. # define XB_SET_OWNER(bp) do { } while (0)
  53. # define XB_CLEAR_OWNER(bp) do { } while (0)
  54. # define XB_GET_OWNER(bp) do { } while (0)
  55. #endif
  56. #define xb_to_gfp(flags) \
  57. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
  58. ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
  59. #define xb_to_km(flags) \
  60. (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
  61. #define xfs_buf_allocate(flags) \
  62. kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
  63. #define xfs_buf_deallocate(bp) \
  64. kmem_zone_free(xfs_buf_zone, (bp));
  65. static inline int
  66. xfs_buf_is_vmapped(
  67. struct xfs_buf *bp)
  68. {
  69. /*
  70. * Return true if the buffer is vmapped.
  71. *
  72. * The XBF_MAPPED flag is set if the buffer should be mapped, but the
  73. * code is clever enough to know it doesn't have to map a single page,
  74. * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
  75. */
  76. return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
  77. }
  78. static inline int
  79. xfs_buf_vmap_len(
  80. struct xfs_buf *bp)
  81. {
  82. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  83. }
  84. /*
  85. * xfs_buf_lru_add - add a buffer to the LRU.
  86. *
  87. * The LRU takes a new reference to the buffer so that it will only be freed
  88. * once the shrinker takes the buffer off the LRU.
  89. */
  90. STATIC void
  91. xfs_buf_lru_add(
  92. struct xfs_buf *bp)
  93. {
  94. struct xfs_buftarg *btp = bp->b_target;
  95. spin_lock(&btp->bt_lru_lock);
  96. if (list_empty(&bp->b_lru)) {
  97. atomic_inc(&bp->b_hold);
  98. list_add_tail(&bp->b_lru, &btp->bt_lru);
  99. btp->bt_lru_nr++;
  100. }
  101. spin_unlock(&btp->bt_lru_lock);
  102. }
  103. /*
  104. * xfs_buf_lru_del - remove a buffer from the LRU
  105. *
  106. * The unlocked check is safe here because it only occurs when there are not
  107. * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
  108. * to optimise the shrinker removing the buffer from the LRU and calling
  109. * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
  110. * bt_lru_lock.
  111. */
  112. STATIC void
  113. xfs_buf_lru_del(
  114. struct xfs_buf *bp)
  115. {
  116. struct xfs_buftarg *btp = bp->b_target;
  117. if (list_empty(&bp->b_lru))
  118. return;
  119. spin_lock(&btp->bt_lru_lock);
  120. if (!list_empty(&bp->b_lru)) {
  121. list_del_init(&bp->b_lru);
  122. btp->bt_lru_nr--;
  123. }
  124. spin_unlock(&btp->bt_lru_lock);
  125. }
  126. /*
  127. * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  128. * b_lru_ref count so that the buffer is freed immediately when the buffer
  129. * reference count falls to zero. If the buffer is already on the LRU, we need
  130. * to remove the reference that LRU holds on the buffer.
  131. *
  132. * This prevents build-up of stale buffers on the LRU.
  133. */
  134. void
  135. xfs_buf_stale(
  136. struct xfs_buf *bp)
  137. {
  138. bp->b_flags |= XBF_STALE;
  139. atomic_set(&(bp)->b_lru_ref, 0);
  140. if (!list_empty(&bp->b_lru)) {
  141. struct xfs_buftarg *btp = bp->b_target;
  142. spin_lock(&btp->bt_lru_lock);
  143. if (!list_empty(&bp->b_lru)) {
  144. list_del_init(&bp->b_lru);
  145. btp->bt_lru_nr--;
  146. atomic_dec(&bp->b_hold);
  147. }
  148. spin_unlock(&btp->bt_lru_lock);
  149. }
  150. ASSERT(atomic_read(&bp->b_hold) >= 1);
  151. }
  152. STATIC void
  153. _xfs_buf_initialize(
  154. xfs_buf_t *bp,
  155. xfs_buftarg_t *target,
  156. xfs_off_t range_base,
  157. size_t range_length,
  158. xfs_buf_flags_t flags)
  159. {
  160. /*
  161. * We don't want certain flags to appear in b_flags.
  162. */
  163. flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
  164. memset(bp, 0, sizeof(xfs_buf_t));
  165. atomic_set(&bp->b_hold, 1);
  166. atomic_set(&bp->b_lru_ref, 1);
  167. init_completion(&bp->b_iowait);
  168. INIT_LIST_HEAD(&bp->b_lru);
  169. INIT_LIST_HEAD(&bp->b_list);
  170. RB_CLEAR_NODE(&bp->b_rbnode);
  171. sema_init(&bp->b_sema, 0); /* held, no waiters */
  172. XB_SET_OWNER(bp);
  173. bp->b_target = target;
  174. bp->b_file_offset = range_base;
  175. /*
  176. * Set buffer_length and count_desired to the same value initially.
  177. * I/O routines should use count_desired, which will be the same in
  178. * most cases but may be reset (e.g. XFS recovery).
  179. */
  180. bp->b_buffer_length = bp->b_count_desired = range_length;
  181. bp->b_flags = flags;
  182. bp->b_bn = XFS_BUF_DADDR_NULL;
  183. atomic_set(&bp->b_pin_count, 0);
  184. init_waitqueue_head(&bp->b_waiters);
  185. XFS_STATS_INC(xb_create);
  186. trace_xfs_buf_init(bp, _RET_IP_);
  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_offset = xfs_buf_poff(bp->b_file_offset);
  201. bp->b_page_count = page_count;
  202. if (page_count <= XB_PAGES) {
  203. bp->b_pages = bp->b_page_array;
  204. } else {
  205. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  206. page_count, xb_to_km(flags));
  207. if (bp->b_pages == NULL)
  208. return -ENOMEM;
  209. }
  210. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  211. }
  212. return 0;
  213. }
  214. /*
  215. * Frees b_pages if it was allocated.
  216. */
  217. STATIC void
  218. _xfs_buf_free_pages(
  219. xfs_buf_t *bp)
  220. {
  221. if (bp->b_pages != bp->b_page_array) {
  222. kmem_free(bp->b_pages);
  223. bp->b_pages = NULL;
  224. }
  225. }
  226. /*
  227. * Releases the specified buffer.
  228. *
  229. * The modification state of any associated pages is left unchanged.
  230. * The buffer most not be on any hash - use xfs_buf_rele instead for
  231. * hashed and refcounted buffers
  232. */
  233. void
  234. xfs_buf_free(
  235. xfs_buf_t *bp)
  236. {
  237. trace_xfs_buf_free(bp, _RET_IP_);
  238. ASSERT(list_empty(&bp->b_lru));
  239. if (bp->b_flags & _XBF_PAGES) {
  240. uint i;
  241. if (xfs_buf_is_vmapped(bp))
  242. vm_unmap_ram(bp->b_addr - bp->b_offset,
  243. bp->b_page_count);
  244. for (i = 0; i < bp->b_page_count; i++) {
  245. struct page *page = bp->b_pages[i];
  246. __free_page(page);
  247. }
  248. } else if (bp->b_flags & _XBF_KMEM)
  249. kmem_free(bp->b_addr);
  250. _xfs_buf_free_pages(bp);
  251. xfs_buf_deallocate(bp);
  252. }
  253. /*
  254. * Allocates all the pages for buffer in question and builds it's page list.
  255. */
  256. STATIC int
  257. xfs_buf_allocate_memory(
  258. xfs_buf_t *bp,
  259. uint flags)
  260. {
  261. size_t size = bp->b_count_desired;
  262. size_t nbytes, offset;
  263. gfp_t gfp_mask = xb_to_gfp(flags);
  264. unsigned short page_count, i;
  265. xfs_off_t end;
  266. int error;
  267. /*
  268. * for buffers that are contained within a single page, just allocate
  269. * the memory from the heap - there's no need for the complexity of
  270. * page arrays to keep allocation down to order 0.
  271. */
  272. if (bp->b_buffer_length < PAGE_SIZE) {
  273. bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
  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 + bp->b_buffer_length - 1) &
  279. PAGE_MASK) !=
  280. ((unsigned long)bp->b_addr & PAGE_MASK)) {
  281. /* b_addr spans two pages - use alloc_page instead */
  282. kmem_free(bp->b_addr);
  283. bp->b_addr = NULL;
  284. goto use_alloc_page;
  285. }
  286. bp->b_offset = offset_in_page(bp->b_addr);
  287. bp->b_pages = bp->b_page_array;
  288. bp->b_pages[0] = virt_to_page(bp->b_addr);
  289. bp->b_page_count = 1;
  290. bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
  291. return 0;
  292. }
  293. use_alloc_page:
  294. end = bp->b_file_offset + bp->b_buffer_length;
  295. page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
  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. bp->b_flags |= XBF_MAPPED;
  351. } else if (flags & XBF_MAPPED) {
  352. int retried = 0;
  353. do {
  354. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  355. -1, PAGE_KERNEL);
  356. if (bp->b_addr)
  357. break;
  358. vm_unmap_aliases();
  359. } while (retried++ <= 1);
  360. if (!bp->b_addr)
  361. return -ENOMEM;
  362. bp->b_addr += bp->b_offset;
  363. bp->b_flags |= XBF_MAPPED;
  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. xfs_buftarg_t *btp, /* block device target */
  378. xfs_off_t ioff, /* starting offset of range */
  379. size_t isize, /* length of range */
  380. xfs_buf_flags_t flags,
  381. xfs_buf_t *new_bp)
  382. {
  383. xfs_off_t range_base;
  384. size_t range_length;
  385. struct xfs_perag *pag;
  386. struct rb_node **rbp;
  387. struct rb_node *parent;
  388. xfs_buf_t *bp;
  389. range_base = (ioff << BBSHIFT);
  390. range_length = (isize << BBSHIFT);
  391. /* Check for IOs smaller than the sector size / not sector aligned */
  392. ASSERT(!(range_length < (1 << btp->bt_sshift)));
  393. ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
  394. /* get tree root */
  395. pag = xfs_perag_get(btp->bt_mount,
  396. xfs_daddr_to_agno(btp->bt_mount, ioff));
  397. /* walk tree */
  398. spin_lock(&pag->pag_buf_lock);
  399. rbp = &pag->pag_buf_tree.rb_node;
  400. parent = NULL;
  401. bp = NULL;
  402. while (*rbp) {
  403. parent = *rbp;
  404. bp = rb_entry(parent, struct xfs_buf, b_rbnode);
  405. if (range_base < bp->b_file_offset)
  406. rbp = &(*rbp)->rb_left;
  407. else if (range_base > bp->b_file_offset)
  408. rbp = &(*rbp)->rb_right;
  409. else {
  410. /*
  411. * found a block offset match. If the range doesn't
  412. * match, the only way this is allowed is if the buffer
  413. * in the cache is stale and the transaction that made
  414. * it stale has not yet committed. i.e. we are
  415. * reallocating a busy extent. Skip this buffer and
  416. * continue searching to the right for an exact match.
  417. */
  418. if (bp->b_buffer_length != range_length) {
  419. ASSERT(bp->b_flags & XBF_STALE);
  420. rbp = &(*rbp)->rb_right;
  421. continue;
  422. }
  423. atomic_inc(&bp->b_hold);
  424. goto found;
  425. }
  426. }
  427. /* No match found */
  428. if (new_bp) {
  429. _xfs_buf_initialize(new_bp, btp, range_base,
  430. range_length, flags);
  431. rb_link_node(&new_bp->b_rbnode, parent, rbp);
  432. rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
  433. /* the buffer keeps the perag reference until it is freed */
  434. new_bp->b_pag = pag;
  435. spin_unlock(&pag->pag_buf_lock);
  436. } else {
  437. XFS_STATS_INC(xb_miss_locked);
  438. spin_unlock(&pag->pag_buf_lock);
  439. xfs_perag_put(pag);
  440. }
  441. return new_bp;
  442. found:
  443. spin_unlock(&pag->pag_buf_lock);
  444. xfs_perag_put(pag);
  445. if (!xfs_buf_trylock(bp)) {
  446. if (flags & XBF_TRYLOCK) {
  447. xfs_buf_rele(bp);
  448. XFS_STATS_INC(xb_busy_locked);
  449. return NULL;
  450. }
  451. xfs_buf_lock(bp);
  452. XFS_STATS_INC(xb_get_locked_waited);
  453. }
  454. /*
  455. * if the buffer is stale, clear all the external state associated with
  456. * it. We need to keep flags such as how we allocated the buffer memory
  457. * intact here.
  458. */
  459. if (bp->b_flags & XBF_STALE) {
  460. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  461. bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
  462. }
  463. trace_xfs_buf_find(bp, flags, _RET_IP_);
  464. XFS_STATS_INC(xb_get_locked);
  465. return bp;
  466. }
  467. /*
  468. * Assembles a buffer covering the specified range.
  469. * Storage in memory for all portions of the buffer will be allocated,
  470. * although backing storage may not be.
  471. */
  472. xfs_buf_t *
  473. xfs_buf_get(
  474. xfs_buftarg_t *target,/* target for buffer */
  475. xfs_off_t ioff, /* starting offset of range */
  476. size_t isize, /* length of range */
  477. xfs_buf_flags_t flags)
  478. {
  479. xfs_buf_t *bp, *new_bp;
  480. int error = 0;
  481. new_bp = xfs_buf_allocate(flags);
  482. if (unlikely(!new_bp))
  483. return NULL;
  484. bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
  485. if (bp == new_bp) {
  486. error = xfs_buf_allocate_memory(bp, flags);
  487. if (error)
  488. goto no_buffer;
  489. } else {
  490. xfs_buf_deallocate(new_bp);
  491. if (unlikely(bp == NULL))
  492. return NULL;
  493. }
  494. if (!(bp->b_flags & XBF_MAPPED)) {
  495. error = _xfs_buf_map_pages(bp, flags);
  496. if (unlikely(error)) {
  497. xfs_warn(target->bt_mount,
  498. "%s: failed to map pages\n", __func__);
  499. goto no_buffer;
  500. }
  501. }
  502. XFS_STATS_INC(xb_get);
  503. /*
  504. * Always fill in the block number now, the mapped cases can do
  505. * their own overlay of this later.
  506. */
  507. bp->b_bn = ioff;
  508. bp->b_count_desired = bp->b_buffer_length;
  509. trace_xfs_buf_get(bp, flags, _RET_IP_);
  510. return bp;
  511. no_buffer:
  512. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  513. xfs_buf_unlock(bp);
  514. xfs_buf_rele(bp);
  515. return NULL;
  516. }
  517. STATIC int
  518. _xfs_buf_read(
  519. xfs_buf_t *bp,
  520. xfs_buf_flags_t flags)
  521. {
  522. int status;
  523. ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
  524. ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
  525. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | XBF_READ_AHEAD);
  526. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
  527. status = xfs_buf_iorequest(bp);
  528. if (status || bp->b_error || (flags & XBF_ASYNC))
  529. return status;
  530. return xfs_buf_iowait(bp);
  531. }
  532. xfs_buf_t *
  533. xfs_buf_read(
  534. xfs_buftarg_t *target,
  535. xfs_off_t ioff,
  536. size_t isize,
  537. xfs_buf_flags_t flags)
  538. {
  539. xfs_buf_t *bp;
  540. flags |= XBF_READ;
  541. bp = xfs_buf_get(target, ioff, isize, flags);
  542. if (bp) {
  543. trace_xfs_buf_read(bp, flags, _RET_IP_);
  544. if (!XFS_BUF_ISDONE(bp)) {
  545. XFS_STATS_INC(xb_get_read);
  546. _xfs_buf_read(bp, flags);
  547. } else if (flags & XBF_ASYNC) {
  548. /*
  549. * Read ahead call which is already satisfied,
  550. * drop the buffer
  551. */
  552. goto no_buffer;
  553. } else {
  554. /* We do not want read in the flags */
  555. bp->b_flags &= ~XBF_READ;
  556. }
  557. }
  558. return bp;
  559. no_buffer:
  560. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  561. xfs_buf_unlock(bp);
  562. xfs_buf_rele(bp);
  563. return NULL;
  564. }
  565. /*
  566. * If we are not low on memory then do the readahead in a deadlock
  567. * safe manner.
  568. */
  569. void
  570. xfs_buf_readahead(
  571. xfs_buftarg_t *target,
  572. xfs_off_t ioff,
  573. size_t isize)
  574. {
  575. if (bdi_read_congested(target->bt_bdi))
  576. return;
  577. xfs_buf_read(target, ioff, isize,
  578. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
  579. }
  580. /*
  581. * Read an uncached buffer from disk. Allocates and returns a locked
  582. * buffer containing the disk contents or nothing.
  583. */
  584. struct xfs_buf *
  585. xfs_buf_read_uncached(
  586. struct xfs_mount *mp,
  587. struct xfs_buftarg *target,
  588. xfs_daddr_t daddr,
  589. size_t length,
  590. int flags)
  591. {
  592. xfs_buf_t *bp;
  593. int error;
  594. bp = xfs_buf_get_uncached(target, length, flags);
  595. if (!bp)
  596. return NULL;
  597. /* set up the buffer for a read IO */
  598. XFS_BUF_SET_ADDR(bp, daddr);
  599. XFS_BUF_READ(bp);
  600. xfsbdstrat(mp, bp);
  601. error = xfs_buf_iowait(bp);
  602. if (error || bp->b_error) {
  603. xfs_buf_relse(bp);
  604. return NULL;
  605. }
  606. return bp;
  607. }
  608. xfs_buf_t *
  609. xfs_buf_get_empty(
  610. size_t len,
  611. xfs_buftarg_t *target)
  612. {
  613. xfs_buf_t *bp;
  614. bp = xfs_buf_allocate(0);
  615. if (bp)
  616. _xfs_buf_initialize(bp, target, 0, len, 0);
  617. return bp;
  618. }
  619. /*
  620. * Return a buffer allocated as an empty buffer and associated to external
  621. * memory via xfs_buf_associate_memory() back to it's empty state.
  622. */
  623. void
  624. xfs_buf_set_empty(
  625. struct xfs_buf *bp,
  626. size_t len)
  627. {
  628. if (bp->b_pages)
  629. _xfs_buf_free_pages(bp);
  630. bp->b_pages = NULL;
  631. bp->b_page_count = 0;
  632. bp->b_addr = NULL;
  633. bp->b_file_offset = 0;
  634. bp->b_buffer_length = bp->b_count_desired = len;
  635. bp->b_bn = XFS_BUF_DADDR_NULL;
  636. bp->b_flags &= ~XBF_MAPPED;
  637. }
  638. static inline struct page *
  639. mem_to_page(
  640. void *addr)
  641. {
  642. if ((!is_vmalloc_addr(addr))) {
  643. return virt_to_page(addr);
  644. } else {
  645. return vmalloc_to_page(addr);
  646. }
  647. }
  648. int
  649. xfs_buf_associate_memory(
  650. xfs_buf_t *bp,
  651. void *mem,
  652. size_t len)
  653. {
  654. int rval;
  655. int i = 0;
  656. unsigned long pageaddr;
  657. unsigned long offset;
  658. size_t buflen;
  659. int page_count;
  660. pageaddr = (unsigned long)mem & PAGE_MASK;
  661. offset = (unsigned long)mem - pageaddr;
  662. buflen = PAGE_ALIGN(len + offset);
  663. page_count = buflen >> PAGE_SHIFT;
  664. /* Free any previous set of page pointers */
  665. if (bp->b_pages)
  666. _xfs_buf_free_pages(bp);
  667. bp->b_pages = NULL;
  668. bp->b_addr = mem;
  669. rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
  670. if (rval)
  671. return rval;
  672. bp->b_offset = offset;
  673. for (i = 0; i < bp->b_page_count; i++) {
  674. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  675. pageaddr += PAGE_SIZE;
  676. }
  677. bp->b_count_desired = len;
  678. bp->b_buffer_length = buflen;
  679. bp->b_flags |= XBF_MAPPED;
  680. return 0;
  681. }
  682. xfs_buf_t *
  683. xfs_buf_get_uncached(
  684. struct xfs_buftarg *target,
  685. size_t len,
  686. int flags)
  687. {
  688. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  689. int error, i;
  690. xfs_buf_t *bp;
  691. bp = xfs_buf_allocate(0);
  692. if (unlikely(bp == NULL))
  693. goto fail;
  694. _xfs_buf_initialize(bp, target, 0, len, 0);
  695. error = _xfs_buf_get_pages(bp, page_count, 0);
  696. if (error)
  697. goto fail_free_buf;
  698. for (i = 0; i < page_count; i++) {
  699. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  700. if (!bp->b_pages[i])
  701. goto fail_free_mem;
  702. }
  703. bp->b_flags |= _XBF_PAGES;
  704. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  705. if (unlikely(error)) {
  706. xfs_warn(target->bt_mount,
  707. "%s: failed to map pages\n", __func__);
  708. goto fail_free_mem;
  709. }
  710. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  711. return bp;
  712. fail_free_mem:
  713. while (--i >= 0)
  714. __free_page(bp->b_pages[i]);
  715. _xfs_buf_free_pages(bp);
  716. fail_free_buf:
  717. xfs_buf_deallocate(bp);
  718. fail:
  719. return NULL;
  720. }
  721. /*
  722. * Increment reference count on buffer, to hold the buffer concurrently
  723. * with another thread which may release (free) the buffer asynchronously.
  724. * Must hold the buffer already to call this function.
  725. */
  726. void
  727. xfs_buf_hold(
  728. xfs_buf_t *bp)
  729. {
  730. trace_xfs_buf_hold(bp, _RET_IP_);
  731. atomic_inc(&bp->b_hold);
  732. }
  733. /*
  734. * Releases a hold on the specified buffer. If the
  735. * the hold count is 1, calls xfs_buf_free.
  736. */
  737. void
  738. xfs_buf_rele(
  739. xfs_buf_t *bp)
  740. {
  741. struct xfs_perag *pag = bp->b_pag;
  742. trace_xfs_buf_rele(bp, _RET_IP_);
  743. if (!pag) {
  744. ASSERT(list_empty(&bp->b_lru));
  745. ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
  746. if (atomic_dec_and_test(&bp->b_hold))
  747. xfs_buf_free(bp);
  748. return;
  749. }
  750. ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
  751. ASSERT(atomic_read(&bp->b_hold) > 0);
  752. if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
  753. if (!(bp->b_flags & XBF_STALE) &&
  754. atomic_read(&bp->b_lru_ref)) {
  755. xfs_buf_lru_add(bp);
  756. spin_unlock(&pag->pag_buf_lock);
  757. } else {
  758. xfs_buf_lru_del(bp);
  759. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  760. rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
  761. spin_unlock(&pag->pag_buf_lock);
  762. xfs_perag_put(pag);
  763. xfs_buf_free(bp);
  764. }
  765. }
  766. }
  767. /*
  768. * Lock a buffer object, if it is not already locked.
  769. *
  770. * If we come across a stale, pinned, locked buffer, we know that we are
  771. * being asked to lock a buffer that has been reallocated. Because it is
  772. * pinned, we know that the log has not been pushed to disk and hence it
  773. * will still be locked. Rather than continuing to have trylock attempts
  774. * fail until someone else pushes the log, push it ourselves before
  775. * returning. This means that the xfsaild will not get stuck trying
  776. * to push on stale inode buffers.
  777. */
  778. int
  779. xfs_buf_trylock(
  780. struct xfs_buf *bp)
  781. {
  782. int locked;
  783. locked = down_trylock(&bp->b_sema) == 0;
  784. if (locked)
  785. XB_SET_OWNER(bp);
  786. else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  787. xfs_log_force(bp->b_target->bt_mount, 0);
  788. trace_xfs_buf_trylock(bp, _RET_IP_);
  789. return locked;
  790. }
  791. /*
  792. * Lock a buffer object.
  793. *
  794. * If we come across a stale, pinned, locked buffer, we know that we
  795. * are being asked to lock a buffer that has been reallocated. Because
  796. * it is pinned, we know that the log has not been pushed to disk and
  797. * hence it will still be locked. Rather than sleeping until someone
  798. * else pushes the log, push it ourselves before trying to get the lock.
  799. */
  800. void
  801. xfs_buf_lock(
  802. struct xfs_buf *bp)
  803. {
  804. trace_xfs_buf_lock(bp, _RET_IP_);
  805. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  806. xfs_log_force(bp->b_target->bt_mount, 0);
  807. down(&bp->b_sema);
  808. XB_SET_OWNER(bp);
  809. trace_xfs_buf_lock_done(bp, _RET_IP_);
  810. }
  811. /*
  812. * Releases the lock on the buffer object.
  813. * If the buffer is marked delwri but is not queued, do so before we
  814. * unlock the buffer as we need to set flags correctly. We also need to
  815. * take a reference for the delwri queue because the unlocker is going to
  816. * drop their's and they don't know we just queued it.
  817. */
  818. void
  819. xfs_buf_unlock(
  820. struct xfs_buf *bp)
  821. {
  822. XB_CLEAR_OWNER(bp);
  823. up(&bp->b_sema);
  824. trace_xfs_buf_unlock(bp, _RET_IP_);
  825. }
  826. STATIC void
  827. xfs_buf_wait_unpin(
  828. xfs_buf_t *bp)
  829. {
  830. DECLARE_WAITQUEUE (wait, current);
  831. if (atomic_read(&bp->b_pin_count) == 0)
  832. return;
  833. add_wait_queue(&bp->b_waiters, &wait);
  834. for (;;) {
  835. set_current_state(TASK_UNINTERRUPTIBLE);
  836. if (atomic_read(&bp->b_pin_count) == 0)
  837. break;
  838. io_schedule();
  839. }
  840. remove_wait_queue(&bp->b_waiters, &wait);
  841. set_current_state(TASK_RUNNING);
  842. }
  843. /*
  844. * Buffer Utility Routines
  845. */
  846. STATIC void
  847. xfs_buf_iodone_work(
  848. struct work_struct *work)
  849. {
  850. xfs_buf_t *bp =
  851. container_of(work, xfs_buf_t, b_iodone_work);
  852. if (bp->b_iodone)
  853. (*(bp->b_iodone))(bp);
  854. else if (bp->b_flags & XBF_ASYNC)
  855. xfs_buf_relse(bp);
  856. }
  857. void
  858. xfs_buf_ioend(
  859. xfs_buf_t *bp,
  860. int schedule)
  861. {
  862. trace_xfs_buf_iodone(bp, _RET_IP_);
  863. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  864. if (bp->b_error == 0)
  865. bp->b_flags |= XBF_DONE;
  866. if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
  867. if (schedule) {
  868. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  869. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  870. } else {
  871. xfs_buf_iodone_work(&bp->b_iodone_work);
  872. }
  873. } else {
  874. complete(&bp->b_iowait);
  875. }
  876. }
  877. void
  878. xfs_buf_ioerror(
  879. xfs_buf_t *bp,
  880. int error)
  881. {
  882. ASSERT(error >= 0 && error <= 0xffff);
  883. bp->b_error = (unsigned short)error;
  884. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  885. }
  886. int
  887. xfs_bwrite(
  888. struct xfs_buf *bp)
  889. {
  890. int error;
  891. bp->b_flags |= XBF_WRITE;
  892. bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
  893. xfs_buf_delwri_dequeue(bp);
  894. xfs_bdstrat_cb(bp);
  895. error = xfs_buf_iowait(bp);
  896. if (error) {
  897. xfs_force_shutdown(bp->b_target->bt_mount,
  898. SHUTDOWN_META_IO_ERROR);
  899. }
  900. return error;
  901. }
  902. /*
  903. * Called when we want to stop a buffer from getting written or read.
  904. * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
  905. * so that the proper iodone callbacks get called.
  906. */
  907. STATIC int
  908. xfs_bioerror(
  909. xfs_buf_t *bp)
  910. {
  911. #ifdef XFSERRORDEBUG
  912. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  913. #endif
  914. /*
  915. * No need to wait until the buffer is unpinned, we aren't flushing it.
  916. */
  917. xfs_buf_ioerror(bp, EIO);
  918. /*
  919. * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
  920. */
  921. XFS_BUF_UNREAD(bp);
  922. xfs_buf_delwri_dequeue(bp);
  923. XFS_BUF_UNDONE(bp);
  924. XFS_BUF_STALE(bp);
  925. xfs_buf_ioend(bp, 0);
  926. return EIO;
  927. }
  928. /*
  929. * Same as xfs_bioerror, except that we are releasing the buffer
  930. * here ourselves, and avoiding the xfs_buf_ioend call.
  931. * This is meant for userdata errors; metadata bufs come with
  932. * iodone functions attached, so that we can track down errors.
  933. */
  934. STATIC int
  935. xfs_bioerror_relse(
  936. struct xfs_buf *bp)
  937. {
  938. int64_t fl = bp->b_flags;
  939. /*
  940. * No need to wait until the buffer is unpinned.
  941. * We aren't flushing it.
  942. *
  943. * chunkhold expects B_DONE to be set, whether
  944. * we actually finish the I/O or not. We don't want to
  945. * change that interface.
  946. */
  947. XFS_BUF_UNREAD(bp);
  948. xfs_buf_delwri_dequeue(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. XFS_BUF_FINISH_IOWAIT(bp);
  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 bufer 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. xfs_warn(btp->bt_mount,
  1267. "Cannot set_blocksize to %u on device %s\n",
  1268. sectorsize, xfs_buf_target_name(btp));
  1269. return EINVAL;
  1270. }
  1271. return 0;
  1272. }
  1273. /*
  1274. * When allocating the initial buffer target we have not yet
  1275. * read in the superblock, so don't know what sized sectors
  1276. * are being used is at this early stage. Play safe.
  1277. */
  1278. STATIC int
  1279. xfs_setsize_buftarg_early(
  1280. xfs_buftarg_t *btp,
  1281. struct block_device *bdev)
  1282. {
  1283. return xfs_setsize_buftarg_flags(btp,
  1284. PAGE_SIZE, bdev_logical_block_size(bdev), 0);
  1285. }
  1286. int
  1287. xfs_setsize_buftarg(
  1288. xfs_buftarg_t *btp,
  1289. unsigned int blocksize,
  1290. unsigned int sectorsize)
  1291. {
  1292. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1293. }
  1294. STATIC int
  1295. xfs_alloc_delwri_queue(
  1296. xfs_buftarg_t *btp,
  1297. const char *fsname)
  1298. {
  1299. INIT_LIST_HEAD(&btp->bt_delwri_queue);
  1300. spin_lock_init(&btp->bt_delwri_lock);
  1301. btp->bt_flags = 0;
  1302. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
  1303. if (IS_ERR(btp->bt_task))
  1304. return PTR_ERR(btp->bt_task);
  1305. return 0;
  1306. }
  1307. xfs_buftarg_t *
  1308. xfs_alloc_buftarg(
  1309. struct xfs_mount *mp,
  1310. struct block_device *bdev,
  1311. int external,
  1312. const char *fsname)
  1313. {
  1314. xfs_buftarg_t *btp;
  1315. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1316. btp->bt_mount = mp;
  1317. btp->bt_dev = bdev->bd_dev;
  1318. btp->bt_bdev = bdev;
  1319. btp->bt_bdi = blk_get_backing_dev_info(bdev);
  1320. if (!btp->bt_bdi)
  1321. goto error;
  1322. INIT_LIST_HEAD(&btp->bt_lru);
  1323. spin_lock_init(&btp->bt_lru_lock);
  1324. if (xfs_setsize_buftarg_early(btp, bdev))
  1325. goto error;
  1326. if (xfs_alloc_delwri_queue(btp, fsname))
  1327. goto error;
  1328. btp->bt_shrinker.shrink = xfs_buftarg_shrink;
  1329. btp->bt_shrinker.seeks = DEFAULT_SEEKS;
  1330. register_shrinker(&btp->bt_shrinker);
  1331. return btp;
  1332. error:
  1333. kmem_free(btp);
  1334. return NULL;
  1335. }
  1336. /*
  1337. * Delayed write buffer handling
  1338. */
  1339. void
  1340. xfs_buf_delwri_queue(
  1341. xfs_buf_t *bp)
  1342. {
  1343. struct xfs_buftarg *btp = bp->b_target;
  1344. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1345. ASSERT(!(bp->b_flags & XBF_READ));
  1346. spin_lock(&btp->bt_delwri_lock);
  1347. if (!list_empty(&bp->b_list)) {
  1348. /* if already in the queue, move it to the tail */
  1349. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1350. list_move_tail(&bp->b_list, &btp->bt_delwri_queue);
  1351. } else {
  1352. /* start xfsbufd as it is about to have something to do */
  1353. if (list_empty(&btp->bt_delwri_queue))
  1354. wake_up_process(bp->b_target->bt_task);
  1355. atomic_inc(&bp->b_hold);
  1356. bp->b_flags |= XBF_DELWRI | _XBF_DELWRI_Q | XBF_ASYNC;
  1357. list_add_tail(&bp->b_list, &btp->bt_delwri_queue);
  1358. }
  1359. bp->b_queuetime = jiffies;
  1360. spin_unlock(&btp->bt_delwri_lock);
  1361. }
  1362. void
  1363. xfs_buf_delwri_dequeue(
  1364. xfs_buf_t *bp)
  1365. {
  1366. int dequeued = 0;
  1367. spin_lock(&bp->b_target->bt_delwri_lock);
  1368. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1369. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1370. list_del_init(&bp->b_list);
  1371. dequeued = 1;
  1372. }
  1373. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1374. spin_unlock(&bp->b_target->bt_delwri_lock);
  1375. if (dequeued)
  1376. xfs_buf_rele(bp);
  1377. trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
  1378. }
  1379. /*
  1380. * If a delwri buffer needs to be pushed before it has aged out, then promote
  1381. * it to the head of the delwri queue so that it will be flushed on the next
  1382. * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
  1383. * than the age currently needed to flush the buffer. Hence the next time the
  1384. * xfsbufd sees it is guaranteed to be considered old enough to flush.
  1385. */
  1386. void
  1387. xfs_buf_delwri_promote(
  1388. struct xfs_buf *bp)
  1389. {
  1390. struct xfs_buftarg *btp = bp->b_target;
  1391. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
  1392. ASSERT(bp->b_flags & XBF_DELWRI);
  1393. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1394. /*
  1395. * Check the buffer age before locking the delayed write queue as we
  1396. * don't need to promote buffers that are already past the flush age.
  1397. */
  1398. if (bp->b_queuetime < jiffies - age)
  1399. return;
  1400. bp->b_queuetime = jiffies - age;
  1401. spin_lock(&btp->bt_delwri_lock);
  1402. list_move(&bp->b_list, &btp->bt_delwri_queue);
  1403. spin_unlock(&btp->bt_delwri_lock);
  1404. }
  1405. STATIC void
  1406. xfs_buf_runall_queues(
  1407. struct workqueue_struct *queue)
  1408. {
  1409. flush_workqueue(queue);
  1410. }
  1411. /*
  1412. * Move as many buffers as specified to the supplied list
  1413. * idicating if we skipped any buffers to prevent deadlocks.
  1414. */
  1415. STATIC int
  1416. xfs_buf_delwri_split(
  1417. xfs_buftarg_t *target,
  1418. struct list_head *list,
  1419. unsigned long age)
  1420. {
  1421. xfs_buf_t *bp, *n;
  1422. int skipped = 0;
  1423. int force;
  1424. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1425. INIT_LIST_HEAD(list);
  1426. spin_lock(&target->bt_delwri_lock);
  1427. list_for_each_entry_safe(bp, n, &target->bt_delwri_queue, b_list) {
  1428. ASSERT(bp->b_flags & XBF_DELWRI);
  1429. if (!xfs_buf_ispinned(bp) && xfs_buf_trylock(bp)) {
  1430. if (!force &&
  1431. time_before(jiffies, bp->b_queuetime + age)) {
  1432. xfs_buf_unlock(bp);
  1433. break;
  1434. }
  1435. bp->b_flags &= ~(XBF_DELWRI | _XBF_DELWRI_Q);
  1436. bp->b_flags |= XBF_WRITE;
  1437. list_move_tail(&bp->b_list, list);
  1438. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1439. } else
  1440. skipped++;
  1441. }
  1442. spin_unlock(&target->bt_delwri_lock);
  1443. return skipped;
  1444. }
  1445. /*
  1446. * Compare function is more complex than it needs to be because
  1447. * the return value is only 32 bits and we are doing comparisons
  1448. * on 64 bit values
  1449. */
  1450. static int
  1451. xfs_buf_cmp(
  1452. void *priv,
  1453. struct list_head *a,
  1454. struct list_head *b)
  1455. {
  1456. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1457. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1458. xfs_daddr_t diff;
  1459. diff = ap->b_bn - bp->b_bn;
  1460. if (diff < 0)
  1461. return -1;
  1462. if (diff > 0)
  1463. return 1;
  1464. return 0;
  1465. }
  1466. STATIC int
  1467. xfsbufd(
  1468. void *data)
  1469. {
  1470. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1471. current->flags |= PF_MEMALLOC;
  1472. set_freezable();
  1473. do {
  1474. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
  1475. long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
  1476. struct list_head tmp;
  1477. struct blk_plug plug;
  1478. if (unlikely(freezing(current))) {
  1479. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1480. refrigerator();
  1481. } else {
  1482. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1483. }
  1484. /* sleep for a long time if there is nothing to do. */
  1485. if (list_empty(&target->bt_delwri_queue))
  1486. tout = MAX_SCHEDULE_TIMEOUT;
  1487. schedule_timeout_interruptible(tout);
  1488. xfs_buf_delwri_split(target, &tmp, age);
  1489. list_sort(NULL, &tmp, xfs_buf_cmp);
  1490. blk_start_plug(&plug);
  1491. while (!list_empty(&tmp)) {
  1492. struct xfs_buf *bp;
  1493. bp = list_first_entry(&tmp, struct xfs_buf, b_list);
  1494. list_del_init(&bp->b_list);
  1495. xfs_bdstrat_cb(bp);
  1496. }
  1497. blk_finish_plug(&plug);
  1498. } while (!kthread_should_stop());
  1499. return 0;
  1500. }
  1501. /*
  1502. * Go through all incore buffers, and release buffers if they belong to
  1503. * the given device. This is used in filesystem error handling to
  1504. * preserve the consistency of its metadata.
  1505. */
  1506. int
  1507. xfs_flush_buftarg(
  1508. xfs_buftarg_t *target,
  1509. int wait)
  1510. {
  1511. xfs_buf_t *bp;
  1512. int pincount = 0;
  1513. LIST_HEAD(tmp_list);
  1514. LIST_HEAD(wait_list);
  1515. struct blk_plug plug;
  1516. xfs_buf_runall_queues(xfsconvertd_workqueue);
  1517. xfs_buf_runall_queues(xfsdatad_workqueue);
  1518. xfs_buf_runall_queues(xfslogd_workqueue);
  1519. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1520. pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
  1521. /*
  1522. * Dropped the delayed write list lock, now walk the temporary list.
  1523. * All I/O is issued async and then if we need to wait for completion
  1524. * we do that after issuing all the IO.
  1525. */
  1526. list_sort(NULL, &tmp_list, xfs_buf_cmp);
  1527. blk_start_plug(&plug);
  1528. while (!list_empty(&tmp_list)) {
  1529. bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
  1530. ASSERT(target == bp->b_target);
  1531. list_del_init(&bp->b_list);
  1532. if (wait) {
  1533. bp->b_flags &= ~XBF_ASYNC;
  1534. list_add(&bp->b_list, &wait_list);
  1535. }
  1536. xfs_bdstrat_cb(bp);
  1537. }
  1538. blk_finish_plug(&plug);
  1539. if (wait) {
  1540. /* Wait for IO to complete. */
  1541. while (!list_empty(&wait_list)) {
  1542. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1543. list_del_init(&bp->b_list);
  1544. xfs_buf_iowait(bp);
  1545. xfs_buf_relse(bp);
  1546. }
  1547. }
  1548. return pincount;
  1549. }
  1550. int __init
  1551. xfs_buf_init(void)
  1552. {
  1553. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1554. KM_ZONE_HWALIGN, NULL);
  1555. if (!xfs_buf_zone)
  1556. goto out;
  1557. xfslogd_workqueue = alloc_workqueue("xfslogd",
  1558. WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
  1559. if (!xfslogd_workqueue)
  1560. goto out_free_buf_zone;
  1561. xfsdatad_workqueue = alloc_workqueue("xfsdatad", WQ_MEM_RECLAIM, 1);
  1562. if (!xfsdatad_workqueue)
  1563. goto out_destroy_xfslogd_workqueue;
  1564. xfsconvertd_workqueue = alloc_workqueue("xfsconvertd",
  1565. WQ_MEM_RECLAIM, 1);
  1566. if (!xfsconvertd_workqueue)
  1567. goto out_destroy_xfsdatad_workqueue;
  1568. return 0;
  1569. out_destroy_xfsdatad_workqueue:
  1570. destroy_workqueue(xfsdatad_workqueue);
  1571. out_destroy_xfslogd_workqueue:
  1572. destroy_workqueue(xfslogd_workqueue);
  1573. out_free_buf_zone:
  1574. kmem_zone_destroy(xfs_buf_zone);
  1575. out:
  1576. return -ENOMEM;
  1577. }
  1578. void
  1579. xfs_buf_terminate(void)
  1580. {
  1581. destroy_workqueue(xfsconvertd_workqueue);
  1582. destroy_workqueue(xfsdatad_workqueue);
  1583. destroy_workqueue(xfslogd_workqueue);
  1584. kmem_zone_destroy(xfs_buf_zone);
  1585. }
  1586. #ifdef CONFIG_KDB_MODULES
  1587. struct list_head *
  1588. xfs_get_buftarg_list(void)
  1589. {
  1590. return &xfs_buftarg_list;
  1591. }
  1592. #endif