xfs_buf.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  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_format.h"
  37. #include "xfs_log_format.h"
  38. #include "xfs_trans_resv.h"
  39. #include "xfs_sb.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_log.h"
  43. static kmem_zone_t *xfs_buf_zone;
  44. #ifdef XFS_BUF_LOCK_TRACKING
  45. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  46. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  47. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  48. #else
  49. # define XB_SET_OWNER(bp) do { } while (0)
  50. # define XB_CLEAR_OWNER(bp) do { } while (0)
  51. # define XB_GET_OWNER(bp) do { } while (0)
  52. #endif
  53. #define xb_to_gfp(flags) \
  54. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
  55. static inline int
  56. xfs_buf_is_vmapped(
  57. struct xfs_buf *bp)
  58. {
  59. /*
  60. * Return true if the buffer is vmapped.
  61. *
  62. * b_addr is null if the buffer is not mapped, but the code is clever
  63. * enough to know it doesn't have to map a single page, so the check has
  64. * to be both for b_addr and bp->b_page_count > 1.
  65. */
  66. return bp->b_addr && bp->b_page_count > 1;
  67. }
  68. static inline int
  69. xfs_buf_vmap_len(
  70. struct xfs_buf *bp)
  71. {
  72. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  73. }
  74. /*
  75. * Bump the I/O in flight count on the buftarg if we haven't yet done so for
  76. * this buffer. The count is incremented once per buffer (per hold cycle)
  77. * because the corresponding decrement is deferred to buffer release. Buffers
  78. * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
  79. * tracking adds unnecessary overhead. This is used for sychronization purposes
  80. * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
  81. * in-flight buffers.
  82. *
  83. * Buffers that are never released (e.g., superblock, iclog buffers) must set
  84. * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
  85. * never reaches zero and unmount hangs indefinitely.
  86. */
  87. static inline void
  88. xfs_buf_ioacct_inc(
  89. struct xfs_buf *bp)
  90. {
  91. if (bp->b_flags & (XBF_NO_IOACCT|_XBF_IN_FLIGHT))
  92. return;
  93. ASSERT(bp->b_flags & XBF_ASYNC);
  94. bp->b_flags |= _XBF_IN_FLIGHT;
  95. percpu_counter_inc(&bp->b_target->bt_io_count);
  96. }
  97. /*
  98. * Clear the in-flight state on a buffer about to be released to the LRU or
  99. * freed and unaccount from the buftarg.
  100. */
  101. static inline void
  102. xfs_buf_ioacct_dec(
  103. struct xfs_buf *bp)
  104. {
  105. if (!(bp->b_flags & _XBF_IN_FLIGHT))
  106. return;
  107. bp->b_flags &= ~_XBF_IN_FLIGHT;
  108. percpu_counter_dec(&bp->b_target->bt_io_count);
  109. }
  110. /*
  111. * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  112. * b_lru_ref count so that the buffer is freed immediately when the buffer
  113. * reference count falls to zero. If the buffer is already on the LRU, we need
  114. * to remove the reference that LRU holds on the buffer.
  115. *
  116. * This prevents build-up of stale buffers on the LRU.
  117. */
  118. void
  119. xfs_buf_stale(
  120. struct xfs_buf *bp)
  121. {
  122. ASSERT(xfs_buf_islocked(bp));
  123. bp->b_flags |= XBF_STALE;
  124. /*
  125. * Clear the delwri status so that a delwri queue walker will not
  126. * flush this buffer to disk now that it is stale. The delwri queue has
  127. * a reference to the buffer, so this is safe to do.
  128. */
  129. bp->b_flags &= ~_XBF_DELWRI_Q;
  130. /*
  131. * Once the buffer is marked stale and unlocked, a subsequent lookup
  132. * could reset b_flags. There is no guarantee that the buffer is
  133. * unaccounted (released to LRU) before that occurs. Drop in-flight
  134. * status now to preserve accounting consistency.
  135. */
  136. xfs_buf_ioacct_dec(bp);
  137. spin_lock(&bp->b_lock);
  138. atomic_set(&bp->b_lru_ref, 0);
  139. if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
  140. (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
  141. atomic_dec(&bp->b_hold);
  142. ASSERT(atomic_read(&bp->b_hold) >= 1);
  143. spin_unlock(&bp->b_lock);
  144. }
  145. static int
  146. xfs_buf_get_maps(
  147. struct xfs_buf *bp,
  148. int map_count)
  149. {
  150. ASSERT(bp->b_maps == NULL);
  151. bp->b_map_count = map_count;
  152. if (map_count == 1) {
  153. bp->b_maps = &bp->__b_map;
  154. return 0;
  155. }
  156. bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
  157. KM_NOFS);
  158. if (!bp->b_maps)
  159. return -ENOMEM;
  160. return 0;
  161. }
  162. /*
  163. * Frees b_pages if it was allocated.
  164. */
  165. static void
  166. xfs_buf_free_maps(
  167. struct xfs_buf *bp)
  168. {
  169. if (bp->b_maps != &bp->__b_map) {
  170. kmem_free(bp->b_maps);
  171. bp->b_maps = NULL;
  172. }
  173. }
  174. struct xfs_buf *
  175. _xfs_buf_alloc(
  176. struct xfs_buftarg *target,
  177. struct xfs_buf_map *map,
  178. int nmaps,
  179. xfs_buf_flags_t flags)
  180. {
  181. struct xfs_buf *bp;
  182. int error;
  183. int i;
  184. bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
  185. if (unlikely(!bp))
  186. return NULL;
  187. /*
  188. * We don't want certain flags to appear in b_flags unless they are
  189. * specifically set by later operations on the buffer.
  190. */
  191. flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
  192. atomic_set(&bp->b_hold, 1);
  193. atomic_set(&bp->b_lru_ref, 1);
  194. init_completion(&bp->b_iowait);
  195. INIT_LIST_HEAD(&bp->b_lru);
  196. INIT_LIST_HEAD(&bp->b_list);
  197. sema_init(&bp->b_sema, 0); /* held, no waiters */
  198. spin_lock_init(&bp->b_lock);
  199. XB_SET_OWNER(bp);
  200. bp->b_target = target;
  201. bp->b_flags = flags;
  202. /*
  203. * Set length and io_length to the same value initially.
  204. * I/O routines should use io_length, which will be the same in
  205. * most cases but may be reset (e.g. XFS recovery).
  206. */
  207. error = xfs_buf_get_maps(bp, nmaps);
  208. if (error) {
  209. kmem_zone_free(xfs_buf_zone, bp);
  210. return NULL;
  211. }
  212. bp->b_bn = map[0].bm_bn;
  213. bp->b_length = 0;
  214. for (i = 0; i < nmaps; i++) {
  215. bp->b_maps[i].bm_bn = map[i].bm_bn;
  216. bp->b_maps[i].bm_len = map[i].bm_len;
  217. bp->b_length += map[i].bm_len;
  218. }
  219. bp->b_io_length = bp->b_length;
  220. atomic_set(&bp->b_pin_count, 0);
  221. init_waitqueue_head(&bp->b_waiters);
  222. XFS_STATS_INC(target->bt_mount, xb_create);
  223. trace_xfs_buf_init(bp, _RET_IP_);
  224. return bp;
  225. }
  226. /*
  227. * Allocate a page array capable of holding a specified number
  228. * of pages, and point the page buf at it.
  229. */
  230. STATIC int
  231. _xfs_buf_get_pages(
  232. xfs_buf_t *bp,
  233. int page_count)
  234. {
  235. /* Make sure that we have a page list */
  236. if (bp->b_pages == NULL) {
  237. bp->b_page_count = page_count;
  238. if (page_count <= XB_PAGES) {
  239. bp->b_pages = bp->b_page_array;
  240. } else {
  241. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  242. page_count, KM_NOFS);
  243. if (bp->b_pages == NULL)
  244. return -ENOMEM;
  245. }
  246. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  247. }
  248. return 0;
  249. }
  250. /*
  251. * Frees b_pages if it was allocated.
  252. */
  253. STATIC void
  254. _xfs_buf_free_pages(
  255. xfs_buf_t *bp)
  256. {
  257. if (bp->b_pages != bp->b_page_array) {
  258. kmem_free(bp->b_pages);
  259. bp->b_pages = NULL;
  260. }
  261. }
  262. /*
  263. * Releases the specified buffer.
  264. *
  265. * The modification state of any associated pages is left unchanged.
  266. * The buffer must not be on any hash - use xfs_buf_rele instead for
  267. * hashed and refcounted buffers
  268. */
  269. void
  270. xfs_buf_free(
  271. xfs_buf_t *bp)
  272. {
  273. trace_xfs_buf_free(bp, _RET_IP_);
  274. ASSERT(list_empty(&bp->b_lru));
  275. if (bp->b_flags & _XBF_PAGES) {
  276. uint i;
  277. if (xfs_buf_is_vmapped(bp))
  278. vm_unmap_ram(bp->b_addr - bp->b_offset,
  279. bp->b_page_count);
  280. for (i = 0; i < bp->b_page_count; i++) {
  281. struct page *page = bp->b_pages[i];
  282. __free_page(page);
  283. }
  284. } else if (bp->b_flags & _XBF_KMEM)
  285. kmem_free(bp->b_addr);
  286. _xfs_buf_free_pages(bp);
  287. xfs_buf_free_maps(bp);
  288. kmem_zone_free(xfs_buf_zone, bp);
  289. }
  290. /*
  291. * Allocates all the pages for buffer in question and builds it's page list.
  292. */
  293. STATIC int
  294. xfs_buf_allocate_memory(
  295. xfs_buf_t *bp,
  296. uint flags)
  297. {
  298. size_t size;
  299. size_t nbytes, offset;
  300. gfp_t gfp_mask = xb_to_gfp(flags);
  301. unsigned short page_count, i;
  302. xfs_off_t start, end;
  303. int error;
  304. /*
  305. * for buffers that are contained within a single page, just allocate
  306. * the memory from the heap - there's no need for the complexity of
  307. * page arrays to keep allocation down to order 0.
  308. */
  309. size = BBTOB(bp->b_length);
  310. if (size < PAGE_SIZE) {
  311. bp->b_addr = kmem_alloc(size, KM_NOFS);
  312. if (!bp->b_addr) {
  313. /* low memory - use alloc_page loop instead */
  314. goto use_alloc_page;
  315. }
  316. if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
  317. ((unsigned long)bp->b_addr & PAGE_MASK)) {
  318. /* b_addr spans two pages - use alloc_page instead */
  319. kmem_free(bp->b_addr);
  320. bp->b_addr = NULL;
  321. goto use_alloc_page;
  322. }
  323. bp->b_offset = offset_in_page(bp->b_addr);
  324. bp->b_pages = bp->b_page_array;
  325. bp->b_pages[0] = virt_to_page(bp->b_addr);
  326. bp->b_page_count = 1;
  327. bp->b_flags |= _XBF_KMEM;
  328. return 0;
  329. }
  330. use_alloc_page:
  331. start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
  332. end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
  333. >> PAGE_SHIFT;
  334. page_count = end - start;
  335. error = _xfs_buf_get_pages(bp, page_count);
  336. if (unlikely(error))
  337. return error;
  338. offset = bp->b_offset;
  339. bp->b_flags |= _XBF_PAGES;
  340. for (i = 0; i < bp->b_page_count; i++) {
  341. struct page *page;
  342. uint retries = 0;
  343. retry:
  344. page = alloc_page(gfp_mask);
  345. if (unlikely(page == NULL)) {
  346. if (flags & XBF_READ_AHEAD) {
  347. bp->b_page_count = i;
  348. error = -ENOMEM;
  349. goto out_free_pages;
  350. }
  351. /*
  352. * This could deadlock.
  353. *
  354. * But until all the XFS lowlevel code is revamped to
  355. * handle buffer allocation failures we can't do much.
  356. */
  357. if (!(++retries % 100))
  358. xfs_err(NULL,
  359. "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
  360. current->comm, current->pid,
  361. __func__, gfp_mask);
  362. XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
  363. congestion_wait(BLK_RW_ASYNC, HZ/50);
  364. goto retry;
  365. }
  366. XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
  367. nbytes = min_t(size_t, size, PAGE_SIZE - offset);
  368. size -= nbytes;
  369. bp->b_pages[i] = page;
  370. offset = 0;
  371. }
  372. return 0;
  373. out_free_pages:
  374. for (i = 0; i < bp->b_page_count; i++)
  375. __free_page(bp->b_pages[i]);
  376. return error;
  377. }
  378. /*
  379. * Map buffer into kernel address-space if necessary.
  380. */
  381. STATIC int
  382. _xfs_buf_map_pages(
  383. xfs_buf_t *bp,
  384. uint flags)
  385. {
  386. ASSERT(bp->b_flags & _XBF_PAGES);
  387. if (bp->b_page_count == 1) {
  388. /* A single page buffer is always mappable */
  389. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  390. } else if (flags & XBF_UNMAPPED) {
  391. bp->b_addr = NULL;
  392. } else {
  393. int retried = 0;
  394. unsigned noio_flag;
  395. /*
  396. * vm_map_ram() will allocate auxillary structures (e.g.
  397. * pagetables) with GFP_KERNEL, yet we are likely to be under
  398. * GFP_NOFS context here. Hence we need to tell memory reclaim
  399. * that we are in such a context via PF_MEMALLOC_NOIO to prevent
  400. * memory reclaim re-entering the filesystem here and
  401. * potentially deadlocking.
  402. */
  403. noio_flag = memalloc_noio_save();
  404. do {
  405. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  406. -1, PAGE_KERNEL);
  407. if (bp->b_addr)
  408. break;
  409. vm_unmap_aliases();
  410. } while (retried++ <= 1);
  411. memalloc_noio_restore(noio_flag);
  412. if (!bp->b_addr)
  413. return -ENOMEM;
  414. bp->b_addr += bp->b_offset;
  415. }
  416. return 0;
  417. }
  418. /*
  419. * Finding and Reading Buffers
  420. */
  421. static int
  422. _xfs_buf_obj_cmp(
  423. struct rhashtable_compare_arg *arg,
  424. const void *obj)
  425. {
  426. const struct xfs_buf_map *map = arg->key;
  427. const struct xfs_buf *bp = obj;
  428. /*
  429. * The key hashing in the lookup path depends on the key being the
  430. * first element of the compare_arg, make sure to assert this.
  431. */
  432. BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
  433. if (bp->b_bn != map->bm_bn)
  434. return 1;
  435. if (unlikely(bp->b_length != map->bm_len)) {
  436. /*
  437. * found a block number match. If the range doesn't
  438. * match, the only way this is allowed is if the buffer
  439. * in the cache is stale and the transaction that made
  440. * it stale has not yet committed. i.e. we are
  441. * reallocating a busy extent. Skip this buffer and
  442. * continue searching for an exact match.
  443. */
  444. ASSERT(bp->b_flags & XBF_STALE);
  445. return 1;
  446. }
  447. return 0;
  448. }
  449. static const struct rhashtable_params xfs_buf_hash_params = {
  450. .min_size = 32, /* empty AGs have minimal footprint */
  451. .nelem_hint = 16,
  452. .key_len = sizeof(xfs_daddr_t),
  453. .key_offset = offsetof(struct xfs_buf, b_bn),
  454. .head_offset = offsetof(struct xfs_buf, b_rhash_head),
  455. .automatic_shrinking = true,
  456. .obj_cmpfn = _xfs_buf_obj_cmp,
  457. };
  458. int
  459. xfs_buf_hash_init(
  460. struct xfs_perag *pag)
  461. {
  462. spin_lock_init(&pag->pag_buf_lock);
  463. return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
  464. }
  465. void
  466. xfs_buf_hash_destroy(
  467. struct xfs_perag *pag)
  468. {
  469. rhashtable_destroy(&pag->pag_buf_hash);
  470. }
  471. /*
  472. * Look up, and creates if absent, a lockable buffer for
  473. * a given range of an inode. The buffer is returned
  474. * locked. No I/O is implied by this call.
  475. */
  476. xfs_buf_t *
  477. _xfs_buf_find(
  478. struct xfs_buftarg *btp,
  479. struct xfs_buf_map *map,
  480. int nmaps,
  481. xfs_buf_flags_t flags,
  482. xfs_buf_t *new_bp)
  483. {
  484. struct xfs_perag *pag;
  485. xfs_buf_t *bp;
  486. struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
  487. xfs_daddr_t eofs;
  488. int i;
  489. for (i = 0; i < nmaps; i++)
  490. cmap.bm_len += map[i].bm_len;
  491. /* Check for IOs smaller than the sector size / not sector aligned */
  492. ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
  493. ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
  494. /*
  495. * Corrupted block numbers can get through to here, unfortunately, so we
  496. * have to check that the buffer falls within the filesystem bounds.
  497. */
  498. eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
  499. if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
  500. /*
  501. * XXX (dgc): we should really be returning -EFSCORRUPTED here,
  502. * but none of the higher level infrastructure supports
  503. * returning a specific error on buffer lookup failures.
  504. */
  505. xfs_alert(btp->bt_mount,
  506. "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
  507. __func__, cmap.bm_bn, eofs);
  508. WARN_ON(1);
  509. return NULL;
  510. }
  511. pag = xfs_perag_get(btp->bt_mount,
  512. xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
  513. spin_lock(&pag->pag_buf_lock);
  514. bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
  515. xfs_buf_hash_params);
  516. if (bp) {
  517. atomic_inc(&bp->b_hold);
  518. goto found;
  519. }
  520. /* No match found */
  521. if (new_bp) {
  522. /* the buffer keeps the perag reference until it is freed */
  523. new_bp->b_pag = pag;
  524. rhashtable_insert_fast(&pag->pag_buf_hash,
  525. &new_bp->b_rhash_head,
  526. xfs_buf_hash_params);
  527. spin_unlock(&pag->pag_buf_lock);
  528. } else {
  529. XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
  530. spin_unlock(&pag->pag_buf_lock);
  531. xfs_perag_put(pag);
  532. }
  533. return new_bp;
  534. found:
  535. spin_unlock(&pag->pag_buf_lock);
  536. xfs_perag_put(pag);
  537. if (!xfs_buf_trylock(bp)) {
  538. if (flags & XBF_TRYLOCK) {
  539. xfs_buf_rele(bp);
  540. XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
  541. return NULL;
  542. }
  543. xfs_buf_lock(bp);
  544. XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
  545. }
  546. /*
  547. * if the buffer is stale, clear all the external state associated with
  548. * it. We need to keep flags such as how we allocated the buffer memory
  549. * intact here.
  550. */
  551. if (bp->b_flags & XBF_STALE) {
  552. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  553. ASSERT(bp->b_iodone == NULL);
  554. bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
  555. bp->b_ops = NULL;
  556. }
  557. trace_xfs_buf_find(bp, flags, _RET_IP_);
  558. XFS_STATS_INC(btp->bt_mount, xb_get_locked);
  559. return bp;
  560. }
  561. /*
  562. * Assembles a buffer covering the specified range. The code is optimised for
  563. * cache hits, as metadata intensive workloads will see 3 orders of magnitude
  564. * more hits than misses.
  565. */
  566. struct xfs_buf *
  567. xfs_buf_get_map(
  568. struct xfs_buftarg *target,
  569. struct xfs_buf_map *map,
  570. int nmaps,
  571. xfs_buf_flags_t flags)
  572. {
  573. struct xfs_buf *bp;
  574. struct xfs_buf *new_bp;
  575. int error = 0;
  576. bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
  577. if (likely(bp))
  578. goto found;
  579. new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
  580. if (unlikely(!new_bp))
  581. return NULL;
  582. error = xfs_buf_allocate_memory(new_bp, flags);
  583. if (error) {
  584. xfs_buf_free(new_bp);
  585. return NULL;
  586. }
  587. bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
  588. if (!bp) {
  589. xfs_buf_free(new_bp);
  590. return NULL;
  591. }
  592. if (bp != new_bp)
  593. xfs_buf_free(new_bp);
  594. found:
  595. if (!bp->b_addr) {
  596. error = _xfs_buf_map_pages(bp, flags);
  597. if (unlikely(error)) {
  598. xfs_warn(target->bt_mount,
  599. "%s: failed to map pagesn", __func__);
  600. xfs_buf_relse(bp);
  601. return NULL;
  602. }
  603. }
  604. /*
  605. * Clear b_error if this is a lookup from a caller that doesn't expect
  606. * valid data to be found in the buffer.
  607. */
  608. if (!(flags & XBF_READ))
  609. xfs_buf_ioerror(bp, 0);
  610. XFS_STATS_INC(target->bt_mount, xb_get);
  611. trace_xfs_buf_get(bp, flags, _RET_IP_);
  612. return bp;
  613. }
  614. STATIC int
  615. _xfs_buf_read(
  616. xfs_buf_t *bp,
  617. xfs_buf_flags_t flags)
  618. {
  619. ASSERT(!(flags & XBF_WRITE));
  620. ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
  621. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
  622. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
  623. if (flags & XBF_ASYNC) {
  624. xfs_buf_submit(bp);
  625. return 0;
  626. }
  627. return xfs_buf_submit_wait(bp);
  628. }
  629. xfs_buf_t *
  630. xfs_buf_read_map(
  631. struct xfs_buftarg *target,
  632. struct xfs_buf_map *map,
  633. int nmaps,
  634. xfs_buf_flags_t flags,
  635. const struct xfs_buf_ops *ops)
  636. {
  637. struct xfs_buf *bp;
  638. flags |= XBF_READ;
  639. bp = xfs_buf_get_map(target, map, nmaps, flags);
  640. if (bp) {
  641. trace_xfs_buf_read(bp, flags, _RET_IP_);
  642. if (!(bp->b_flags & XBF_DONE)) {
  643. XFS_STATS_INC(target->bt_mount, xb_get_read);
  644. bp->b_ops = ops;
  645. _xfs_buf_read(bp, flags);
  646. } else if (flags & XBF_ASYNC) {
  647. /*
  648. * Read ahead call which is already satisfied,
  649. * drop the buffer
  650. */
  651. xfs_buf_relse(bp);
  652. return NULL;
  653. } else {
  654. /* We do not want read in the flags */
  655. bp->b_flags &= ~XBF_READ;
  656. }
  657. }
  658. return bp;
  659. }
  660. /*
  661. * If we are not low on memory then do the readahead in a deadlock
  662. * safe manner.
  663. */
  664. void
  665. xfs_buf_readahead_map(
  666. struct xfs_buftarg *target,
  667. struct xfs_buf_map *map,
  668. int nmaps,
  669. const struct xfs_buf_ops *ops)
  670. {
  671. if (bdi_read_congested(target->bt_bdi))
  672. return;
  673. xfs_buf_read_map(target, map, nmaps,
  674. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
  675. }
  676. /*
  677. * Read an uncached buffer from disk. Allocates and returns a locked
  678. * buffer containing the disk contents or nothing.
  679. */
  680. int
  681. xfs_buf_read_uncached(
  682. struct xfs_buftarg *target,
  683. xfs_daddr_t daddr,
  684. size_t numblks,
  685. int flags,
  686. struct xfs_buf **bpp,
  687. const struct xfs_buf_ops *ops)
  688. {
  689. struct xfs_buf *bp;
  690. *bpp = NULL;
  691. bp = xfs_buf_get_uncached(target, numblks, flags);
  692. if (!bp)
  693. return -ENOMEM;
  694. /* set up the buffer for a read IO */
  695. ASSERT(bp->b_map_count == 1);
  696. bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
  697. bp->b_maps[0].bm_bn = daddr;
  698. bp->b_flags |= XBF_READ;
  699. bp->b_ops = ops;
  700. xfs_buf_submit_wait(bp);
  701. if (bp->b_error) {
  702. int error = bp->b_error;
  703. xfs_buf_relse(bp);
  704. return error;
  705. }
  706. *bpp = bp;
  707. return 0;
  708. }
  709. /*
  710. * Return a buffer allocated as an empty buffer and associated to external
  711. * memory via xfs_buf_associate_memory() back to it's empty state.
  712. */
  713. void
  714. xfs_buf_set_empty(
  715. struct xfs_buf *bp,
  716. size_t numblks)
  717. {
  718. if (bp->b_pages)
  719. _xfs_buf_free_pages(bp);
  720. bp->b_pages = NULL;
  721. bp->b_page_count = 0;
  722. bp->b_addr = NULL;
  723. bp->b_length = numblks;
  724. bp->b_io_length = numblks;
  725. ASSERT(bp->b_map_count == 1);
  726. bp->b_bn = XFS_BUF_DADDR_NULL;
  727. bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
  728. bp->b_maps[0].bm_len = bp->b_length;
  729. }
  730. static inline struct page *
  731. mem_to_page(
  732. void *addr)
  733. {
  734. if ((!is_vmalloc_addr(addr))) {
  735. return virt_to_page(addr);
  736. } else {
  737. return vmalloc_to_page(addr);
  738. }
  739. }
  740. int
  741. xfs_buf_associate_memory(
  742. xfs_buf_t *bp,
  743. void *mem,
  744. size_t len)
  745. {
  746. int rval;
  747. int i = 0;
  748. unsigned long pageaddr;
  749. unsigned long offset;
  750. size_t buflen;
  751. int page_count;
  752. pageaddr = (unsigned long)mem & PAGE_MASK;
  753. offset = (unsigned long)mem - pageaddr;
  754. buflen = PAGE_ALIGN(len + offset);
  755. page_count = buflen >> PAGE_SHIFT;
  756. /* Free any previous set of page pointers */
  757. if (bp->b_pages)
  758. _xfs_buf_free_pages(bp);
  759. bp->b_pages = NULL;
  760. bp->b_addr = mem;
  761. rval = _xfs_buf_get_pages(bp, page_count);
  762. if (rval)
  763. return rval;
  764. bp->b_offset = offset;
  765. for (i = 0; i < bp->b_page_count; i++) {
  766. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  767. pageaddr += PAGE_SIZE;
  768. }
  769. bp->b_io_length = BTOBB(len);
  770. bp->b_length = BTOBB(buflen);
  771. return 0;
  772. }
  773. xfs_buf_t *
  774. xfs_buf_get_uncached(
  775. struct xfs_buftarg *target,
  776. size_t numblks,
  777. int flags)
  778. {
  779. unsigned long page_count;
  780. int error, i;
  781. struct xfs_buf *bp;
  782. DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
  783. /* flags might contain irrelevant bits, pass only what we care about */
  784. bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
  785. if (unlikely(bp == NULL))
  786. goto fail;
  787. page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
  788. error = _xfs_buf_get_pages(bp, page_count);
  789. if (error)
  790. goto fail_free_buf;
  791. for (i = 0; i < page_count; i++) {
  792. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  793. if (!bp->b_pages[i])
  794. goto fail_free_mem;
  795. }
  796. bp->b_flags |= _XBF_PAGES;
  797. error = _xfs_buf_map_pages(bp, 0);
  798. if (unlikely(error)) {
  799. xfs_warn(target->bt_mount,
  800. "%s: failed to map pages", __func__);
  801. goto fail_free_mem;
  802. }
  803. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  804. return bp;
  805. fail_free_mem:
  806. while (--i >= 0)
  807. __free_page(bp->b_pages[i]);
  808. _xfs_buf_free_pages(bp);
  809. fail_free_buf:
  810. xfs_buf_free_maps(bp);
  811. kmem_zone_free(xfs_buf_zone, bp);
  812. fail:
  813. return NULL;
  814. }
  815. /*
  816. * Increment reference count on buffer, to hold the buffer concurrently
  817. * with another thread which may release (free) the buffer asynchronously.
  818. * Must hold the buffer already to call this function.
  819. */
  820. void
  821. xfs_buf_hold(
  822. xfs_buf_t *bp)
  823. {
  824. trace_xfs_buf_hold(bp, _RET_IP_);
  825. atomic_inc(&bp->b_hold);
  826. }
  827. /*
  828. * Release a hold on the specified buffer. If the hold count is 1, the buffer is
  829. * placed on LRU or freed (depending on b_lru_ref).
  830. */
  831. void
  832. xfs_buf_rele(
  833. xfs_buf_t *bp)
  834. {
  835. struct xfs_perag *pag = bp->b_pag;
  836. bool release;
  837. bool freebuf = false;
  838. trace_xfs_buf_rele(bp, _RET_IP_);
  839. if (!pag) {
  840. ASSERT(list_empty(&bp->b_lru));
  841. if (atomic_dec_and_test(&bp->b_hold)) {
  842. xfs_buf_ioacct_dec(bp);
  843. xfs_buf_free(bp);
  844. }
  845. return;
  846. }
  847. ASSERT(atomic_read(&bp->b_hold) > 0);
  848. release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
  849. spin_lock(&bp->b_lock);
  850. if (!release) {
  851. /*
  852. * Drop the in-flight state if the buffer is already on the LRU
  853. * and it holds the only reference. This is racy because we
  854. * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
  855. * ensures the decrement occurs only once per-buf.
  856. */
  857. if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
  858. xfs_buf_ioacct_dec(bp);
  859. goto out_unlock;
  860. }
  861. /* the last reference has been dropped ... */
  862. xfs_buf_ioacct_dec(bp);
  863. if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
  864. /*
  865. * If the buffer is added to the LRU take a new reference to the
  866. * buffer for the LRU and clear the (now stale) dispose list
  867. * state flag
  868. */
  869. if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
  870. bp->b_state &= ~XFS_BSTATE_DISPOSE;
  871. atomic_inc(&bp->b_hold);
  872. }
  873. spin_unlock(&pag->pag_buf_lock);
  874. } else {
  875. /*
  876. * most of the time buffers will already be removed from the
  877. * LRU, so optimise that case by checking for the
  878. * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
  879. * was on was the disposal list
  880. */
  881. if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
  882. list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
  883. } else {
  884. ASSERT(list_empty(&bp->b_lru));
  885. }
  886. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  887. rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
  888. xfs_buf_hash_params);
  889. spin_unlock(&pag->pag_buf_lock);
  890. xfs_perag_put(pag);
  891. freebuf = true;
  892. }
  893. out_unlock:
  894. spin_unlock(&bp->b_lock);
  895. if (freebuf)
  896. xfs_buf_free(bp);
  897. }
  898. /*
  899. * Lock a buffer object, if it is not already locked.
  900. *
  901. * If we come across a stale, pinned, locked buffer, we know that we are
  902. * being asked to lock a buffer that has been reallocated. Because it is
  903. * pinned, we know that the log has not been pushed to disk and hence it
  904. * will still be locked. Rather than continuing to have trylock attempts
  905. * fail until someone else pushes the log, push it ourselves before
  906. * returning. This means that the xfsaild will not get stuck trying
  907. * to push on stale inode buffers.
  908. */
  909. int
  910. xfs_buf_trylock(
  911. struct xfs_buf *bp)
  912. {
  913. int locked;
  914. locked = down_trylock(&bp->b_sema) == 0;
  915. if (locked) {
  916. XB_SET_OWNER(bp);
  917. trace_xfs_buf_trylock(bp, _RET_IP_);
  918. } else {
  919. trace_xfs_buf_trylock_fail(bp, _RET_IP_);
  920. }
  921. return locked;
  922. }
  923. /*
  924. * Lock a buffer object.
  925. *
  926. * If we come across a stale, pinned, locked buffer, we know that we
  927. * are being asked to lock a buffer that has been reallocated. Because
  928. * it is pinned, we know that the log has not been pushed to disk and
  929. * hence it will still be locked. Rather than sleeping until someone
  930. * else pushes the log, push it ourselves before trying to get the lock.
  931. */
  932. void
  933. xfs_buf_lock(
  934. struct xfs_buf *bp)
  935. {
  936. trace_xfs_buf_lock(bp, _RET_IP_);
  937. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  938. xfs_log_force(bp->b_target->bt_mount, 0);
  939. down(&bp->b_sema);
  940. XB_SET_OWNER(bp);
  941. trace_xfs_buf_lock_done(bp, _RET_IP_);
  942. }
  943. void
  944. xfs_buf_unlock(
  945. struct xfs_buf *bp)
  946. {
  947. XB_CLEAR_OWNER(bp);
  948. up(&bp->b_sema);
  949. trace_xfs_buf_unlock(bp, _RET_IP_);
  950. }
  951. STATIC void
  952. xfs_buf_wait_unpin(
  953. xfs_buf_t *bp)
  954. {
  955. DECLARE_WAITQUEUE (wait, current);
  956. if (atomic_read(&bp->b_pin_count) == 0)
  957. return;
  958. add_wait_queue(&bp->b_waiters, &wait);
  959. for (;;) {
  960. set_current_state(TASK_UNINTERRUPTIBLE);
  961. if (atomic_read(&bp->b_pin_count) == 0)
  962. break;
  963. io_schedule();
  964. }
  965. remove_wait_queue(&bp->b_waiters, &wait);
  966. set_current_state(TASK_RUNNING);
  967. }
  968. /*
  969. * Buffer Utility Routines
  970. */
  971. void
  972. xfs_buf_ioend(
  973. struct xfs_buf *bp)
  974. {
  975. bool read = bp->b_flags & XBF_READ;
  976. trace_xfs_buf_iodone(bp, _RET_IP_);
  977. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  978. /*
  979. * Pull in IO completion errors now. We are guaranteed to be running
  980. * single threaded, so we don't need the lock to read b_io_error.
  981. */
  982. if (!bp->b_error && bp->b_io_error)
  983. xfs_buf_ioerror(bp, bp->b_io_error);
  984. /* Only validate buffers that were read without errors */
  985. if (read && !bp->b_error && bp->b_ops) {
  986. ASSERT(!bp->b_iodone);
  987. bp->b_ops->verify_read(bp);
  988. }
  989. if (!bp->b_error)
  990. bp->b_flags |= XBF_DONE;
  991. if (bp->b_iodone)
  992. (*(bp->b_iodone))(bp);
  993. else if (bp->b_flags & XBF_ASYNC)
  994. xfs_buf_relse(bp);
  995. else
  996. complete(&bp->b_iowait);
  997. }
  998. static void
  999. xfs_buf_ioend_work(
  1000. struct work_struct *work)
  1001. {
  1002. struct xfs_buf *bp =
  1003. container_of(work, xfs_buf_t, b_ioend_work);
  1004. xfs_buf_ioend(bp);
  1005. }
  1006. static void
  1007. xfs_buf_ioend_async(
  1008. struct xfs_buf *bp)
  1009. {
  1010. INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
  1011. queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
  1012. }
  1013. void
  1014. xfs_buf_ioerror(
  1015. xfs_buf_t *bp,
  1016. int error)
  1017. {
  1018. ASSERT(error <= 0 && error >= -1000);
  1019. bp->b_error = error;
  1020. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  1021. }
  1022. void
  1023. xfs_buf_ioerror_alert(
  1024. struct xfs_buf *bp,
  1025. const char *func)
  1026. {
  1027. xfs_alert(bp->b_target->bt_mount,
  1028. "metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
  1029. (__uint64_t)XFS_BUF_ADDR(bp), func, -bp->b_error, bp->b_length);
  1030. }
  1031. int
  1032. xfs_bwrite(
  1033. struct xfs_buf *bp)
  1034. {
  1035. int error;
  1036. ASSERT(xfs_buf_islocked(bp));
  1037. bp->b_flags |= XBF_WRITE;
  1038. bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
  1039. XBF_WRITE_FAIL | XBF_DONE);
  1040. error = xfs_buf_submit_wait(bp);
  1041. if (error) {
  1042. xfs_force_shutdown(bp->b_target->bt_mount,
  1043. SHUTDOWN_META_IO_ERROR);
  1044. }
  1045. return error;
  1046. }
  1047. static void
  1048. xfs_buf_bio_end_io(
  1049. struct bio *bio)
  1050. {
  1051. struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
  1052. /*
  1053. * don't overwrite existing errors - otherwise we can lose errors on
  1054. * buffers that require multiple bios to complete.
  1055. */
  1056. if (bio->bi_error)
  1057. cmpxchg(&bp->b_io_error, 0, bio->bi_error);
  1058. if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1059. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1060. if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
  1061. xfs_buf_ioend_async(bp);
  1062. bio_put(bio);
  1063. }
  1064. static void
  1065. xfs_buf_ioapply_map(
  1066. struct xfs_buf *bp,
  1067. int map,
  1068. int *buf_offset,
  1069. int *count,
  1070. int op,
  1071. int op_flags)
  1072. {
  1073. int page_index;
  1074. int total_nr_pages = bp->b_page_count;
  1075. int nr_pages;
  1076. struct bio *bio;
  1077. sector_t sector = bp->b_maps[map].bm_bn;
  1078. int size;
  1079. int offset;
  1080. total_nr_pages = bp->b_page_count;
  1081. /* skip the pages in the buffer before the start offset */
  1082. page_index = 0;
  1083. offset = *buf_offset;
  1084. while (offset >= PAGE_SIZE) {
  1085. page_index++;
  1086. offset -= PAGE_SIZE;
  1087. }
  1088. /*
  1089. * Limit the IO size to the length of the current vector, and update the
  1090. * remaining IO count for the next time around.
  1091. */
  1092. size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
  1093. *count -= size;
  1094. *buf_offset += size;
  1095. next_chunk:
  1096. atomic_inc(&bp->b_io_remaining);
  1097. nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
  1098. bio = bio_alloc(GFP_NOIO, nr_pages);
  1099. bio->bi_bdev = bp->b_target->bt_bdev;
  1100. bio->bi_iter.bi_sector = sector;
  1101. bio->bi_end_io = xfs_buf_bio_end_io;
  1102. bio->bi_private = bp;
  1103. bio_set_op_attrs(bio, op, op_flags);
  1104. for (; size && nr_pages; nr_pages--, page_index++) {
  1105. int rbytes, nbytes = PAGE_SIZE - offset;
  1106. if (nbytes > size)
  1107. nbytes = size;
  1108. rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
  1109. offset);
  1110. if (rbytes < nbytes)
  1111. break;
  1112. offset = 0;
  1113. sector += BTOBB(nbytes);
  1114. size -= nbytes;
  1115. total_nr_pages--;
  1116. }
  1117. if (likely(bio->bi_iter.bi_size)) {
  1118. if (xfs_buf_is_vmapped(bp)) {
  1119. flush_kernel_vmap_range(bp->b_addr,
  1120. xfs_buf_vmap_len(bp));
  1121. }
  1122. submit_bio(bio);
  1123. if (size)
  1124. goto next_chunk;
  1125. } else {
  1126. /*
  1127. * This is guaranteed not to be the last io reference count
  1128. * because the caller (xfs_buf_submit) holds a count itself.
  1129. */
  1130. atomic_dec(&bp->b_io_remaining);
  1131. xfs_buf_ioerror(bp, -EIO);
  1132. bio_put(bio);
  1133. }
  1134. }
  1135. STATIC void
  1136. _xfs_buf_ioapply(
  1137. struct xfs_buf *bp)
  1138. {
  1139. struct blk_plug plug;
  1140. int op;
  1141. int op_flags = 0;
  1142. int offset;
  1143. int size;
  1144. int i;
  1145. /*
  1146. * Make sure we capture only current IO errors rather than stale errors
  1147. * left over from previous use of the buffer (e.g. failed readahead).
  1148. */
  1149. bp->b_error = 0;
  1150. /*
  1151. * Initialize the I/O completion workqueue if we haven't yet or the
  1152. * submitter has not opted to specify a custom one.
  1153. */
  1154. if (!bp->b_ioend_wq)
  1155. bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
  1156. if (bp->b_flags & XBF_WRITE) {
  1157. op = REQ_OP_WRITE;
  1158. if (bp->b_flags & XBF_SYNCIO)
  1159. op_flags = REQ_SYNC;
  1160. if (bp->b_flags & XBF_FUA)
  1161. op_flags |= REQ_FUA;
  1162. if (bp->b_flags & XBF_FLUSH)
  1163. op_flags |= REQ_PREFLUSH;
  1164. /*
  1165. * Run the write verifier callback function if it exists. If
  1166. * this function fails it will mark the buffer with an error and
  1167. * the IO should not be dispatched.
  1168. */
  1169. if (bp->b_ops) {
  1170. bp->b_ops->verify_write(bp);
  1171. if (bp->b_error) {
  1172. xfs_force_shutdown(bp->b_target->bt_mount,
  1173. SHUTDOWN_CORRUPT_INCORE);
  1174. return;
  1175. }
  1176. } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
  1177. struct xfs_mount *mp = bp->b_target->bt_mount;
  1178. /*
  1179. * non-crc filesystems don't attach verifiers during
  1180. * log recovery, so don't warn for such filesystems.
  1181. */
  1182. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  1183. xfs_warn(mp,
  1184. "%s: no ops on block 0x%llx/0x%x",
  1185. __func__, bp->b_bn, bp->b_length);
  1186. xfs_hex_dump(bp->b_addr, 64);
  1187. dump_stack();
  1188. }
  1189. }
  1190. } else if (bp->b_flags & XBF_READ_AHEAD) {
  1191. op = REQ_OP_READ;
  1192. op_flags = REQ_RAHEAD;
  1193. } else {
  1194. op = REQ_OP_READ;
  1195. }
  1196. /* we only use the buffer cache for meta-data */
  1197. op_flags |= REQ_META;
  1198. /*
  1199. * Walk all the vectors issuing IO on them. Set up the initial offset
  1200. * into the buffer and the desired IO size before we start -
  1201. * _xfs_buf_ioapply_vec() will modify them appropriately for each
  1202. * subsequent call.
  1203. */
  1204. offset = bp->b_offset;
  1205. size = BBTOB(bp->b_io_length);
  1206. blk_start_plug(&plug);
  1207. for (i = 0; i < bp->b_map_count; i++) {
  1208. xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
  1209. if (bp->b_error)
  1210. break;
  1211. if (size <= 0)
  1212. break; /* all done */
  1213. }
  1214. blk_finish_plug(&plug);
  1215. }
  1216. /*
  1217. * Asynchronous IO submission path. This transfers the buffer lock ownership and
  1218. * the current reference to the IO. It is not safe to reference the buffer after
  1219. * a call to this function unless the caller holds an additional reference
  1220. * itself.
  1221. */
  1222. void
  1223. xfs_buf_submit(
  1224. struct xfs_buf *bp)
  1225. {
  1226. trace_xfs_buf_submit(bp, _RET_IP_);
  1227. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  1228. ASSERT(bp->b_flags & XBF_ASYNC);
  1229. /* on shutdown we stale and complete the buffer immediately */
  1230. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  1231. xfs_buf_ioerror(bp, -EIO);
  1232. bp->b_flags &= ~XBF_DONE;
  1233. xfs_buf_stale(bp);
  1234. xfs_buf_ioend(bp);
  1235. return;
  1236. }
  1237. if (bp->b_flags & XBF_WRITE)
  1238. xfs_buf_wait_unpin(bp);
  1239. /* clear the internal error state to avoid spurious errors */
  1240. bp->b_io_error = 0;
  1241. /*
  1242. * The caller's reference is released during I/O completion.
  1243. * This occurs some time after the last b_io_remaining reference is
  1244. * released, so after we drop our Io reference we have to have some
  1245. * other reference to ensure the buffer doesn't go away from underneath
  1246. * us. Take a direct reference to ensure we have safe access to the
  1247. * buffer until we are finished with it.
  1248. */
  1249. xfs_buf_hold(bp);
  1250. /*
  1251. * Set the count to 1 initially, this will stop an I/O completion
  1252. * callout which happens before we have started all the I/O from calling
  1253. * xfs_buf_ioend too early.
  1254. */
  1255. atomic_set(&bp->b_io_remaining, 1);
  1256. xfs_buf_ioacct_inc(bp);
  1257. _xfs_buf_ioapply(bp);
  1258. /*
  1259. * If _xfs_buf_ioapply failed, we can get back here with only the IO
  1260. * reference we took above. If we drop it to zero, run completion so
  1261. * that we don't return to the caller with completion still pending.
  1262. */
  1263. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  1264. if (bp->b_error)
  1265. xfs_buf_ioend(bp);
  1266. else
  1267. xfs_buf_ioend_async(bp);
  1268. }
  1269. xfs_buf_rele(bp);
  1270. /* Note: it is not safe to reference bp now we've dropped our ref */
  1271. }
  1272. /*
  1273. * Synchronous buffer IO submission path, read or write.
  1274. */
  1275. int
  1276. xfs_buf_submit_wait(
  1277. struct xfs_buf *bp)
  1278. {
  1279. int error;
  1280. trace_xfs_buf_submit_wait(bp, _RET_IP_);
  1281. ASSERT(!(bp->b_flags & (_XBF_DELWRI_Q | XBF_ASYNC)));
  1282. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  1283. xfs_buf_ioerror(bp, -EIO);
  1284. xfs_buf_stale(bp);
  1285. bp->b_flags &= ~XBF_DONE;
  1286. return -EIO;
  1287. }
  1288. if (bp->b_flags & XBF_WRITE)
  1289. xfs_buf_wait_unpin(bp);
  1290. /* clear the internal error state to avoid spurious errors */
  1291. bp->b_io_error = 0;
  1292. /*
  1293. * For synchronous IO, the IO does not inherit the submitters reference
  1294. * count, nor the buffer lock. Hence we cannot release the reference we
  1295. * are about to take until we've waited for all IO completion to occur,
  1296. * including any xfs_buf_ioend_async() work that may be pending.
  1297. */
  1298. xfs_buf_hold(bp);
  1299. /*
  1300. * Set the count to 1 initially, this will stop an I/O completion
  1301. * callout which happens before we have started all the I/O from calling
  1302. * xfs_buf_ioend too early.
  1303. */
  1304. atomic_set(&bp->b_io_remaining, 1);
  1305. _xfs_buf_ioapply(bp);
  1306. /*
  1307. * make sure we run completion synchronously if it raced with us and is
  1308. * already complete.
  1309. */
  1310. if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
  1311. xfs_buf_ioend(bp);
  1312. /* wait for completion before gathering the error from the buffer */
  1313. trace_xfs_buf_iowait(bp, _RET_IP_);
  1314. wait_for_completion(&bp->b_iowait);
  1315. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1316. error = bp->b_error;
  1317. /*
  1318. * all done now, we can release the hold that keeps the buffer
  1319. * referenced for the entire IO.
  1320. */
  1321. xfs_buf_rele(bp);
  1322. return error;
  1323. }
  1324. void *
  1325. xfs_buf_offset(
  1326. struct xfs_buf *bp,
  1327. size_t offset)
  1328. {
  1329. struct page *page;
  1330. if (bp->b_addr)
  1331. return bp->b_addr + offset;
  1332. offset += bp->b_offset;
  1333. page = bp->b_pages[offset >> PAGE_SHIFT];
  1334. return page_address(page) + (offset & (PAGE_SIZE-1));
  1335. }
  1336. /*
  1337. * Move data into or out of a buffer.
  1338. */
  1339. void
  1340. xfs_buf_iomove(
  1341. xfs_buf_t *bp, /* buffer to process */
  1342. size_t boff, /* starting buffer offset */
  1343. size_t bsize, /* length to copy */
  1344. void *data, /* data address */
  1345. xfs_buf_rw_t mode) /* read/write/zero flag */
  1346. {
  1347. size_t bend;
  1348. bend = boff + bsize;
  1349. while (boff < bend) {
  1350. struct page *page;
  1351. int page_index, page_offset, csize;
  1352. page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
  1353. page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
  1354. page = bp->b_pages[page_index];
  1355. csize = min_t(size_t, PAGE_SIZE - page_offset,
  1356. BBTOB(bp->b_io_length) - boff);
  1357. ASSERT((csize + page_offset) <= PAGE_SIZE);
  1358. switch (mode) {
  1359. case XBRW_ZERO:
  1360. memset(page_address(page) + page_offset, 0, csize);
  1361. break;
  1362. case XBRW_READ:
  1363. memcpy(data, page_address(page) + page_offset, csize);
  1364. break;
  1365. case XBRW_WRITE:
  1366. memcpy(page_address(page) + page_offset, data, csize);
  1367. }
  1368. boff += csize;
  1369. data += csize;
  1370. }
  1371. }
  1372. /*
  1373. * Handling of buffer targets (buftargs).
  1374. */
  1375. /*
  1376. * Wait for any bufs with callbacks that have been submitted but have not yet
  1377. * returned. These buffers will have an elevated hold count, so wait on those
  1378. * while freeing all the buffers only held by the LRU.
  1379. */
  1380. static enum lru_status
  1381. xfs_buftarg_wait_rele(
  1382. struct list_head *item,
  1383. struct list_lru_one *lru,
  1384. spinlock_t *lru_lock,
  1385. void *arg)
  1386. {
  1387. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1388. struct list_head *dispose = arg;
  1389. if (atomic_read(&bp->b_hold) > 1) {
  1390. /* need to wait, so skip it this pass */
  1391. trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
  1392. return LRU_SKIP;
  1393. }
  1394. if (!spin_trylock(&bp->b_lock))
  1395. return LRU_SKIP;
  1396. /*
  1397. * clear the LRU reference count so the buffer doesn't get
  1398. * ignored in xfs_buf_rele().
  1399. */
  1400. atomic_set(&bp->b_lru_ref, 0);
  1401. bp->b_state |= XFS_BSTATE_DISPOSE;
  1402. list_lru_isolate_move(lru, item, dispose);
  1403. spin_unlock(&bp->b_lock);
  1404. return LRU_REMOVED;
  1405. }
  1406. void
  1407. xfs_wait_buftarg(
  1408. struct xfs_buftarg *btp)
  1409. {
  1410. LIST_HEAD(dispose);
  1411. int loop = 0;
  1412. /*
  1413. * First wait on the buftarg I/O count for all in-flight buffers to be
  1414. * released. This is critical as new buffers do not make the LRU until
  1415. * they are released.
  1416. *
  1417. * Next, flush the buffer workqueue to ensure all completion processing
  1418. * has finished. Just waiting on buffer locks is not sufficient for
  1419. * async IO as the reference count held over IO is not released until
  1420. * after the buffer lock is dropped. Hence we need to ensure here that
  1421. * all reference counts have been dropped before we start walking the
  1422. * LRU list.
  1423. */
  1424. while (percpu_counter_sum(&btp->bt_io_count))
  1425. delay(100);
  1426. flush_workqueue(btp->bt_mount->m_buf_workqueue);
  1427. /* loop until there is nothing left on the lru list. */
  1428. while (list_lru_count(&btp->bt_lru)) {
  1429. list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
  1430. &dispose, LONG_MAX);
  1431. while (!list_empty(&dispose)) {
  1432. struct xfs_buf *bp;
  1433. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1434. list_del_init(&bp->b_lru);
  1435. if (bp->b_flags & XBF_WRITE_FAIL) {
  1436. xfs_alert(btp->bt_mount,
  1437. "Corruption Alert: Buffer at block 0x%llx had permanent write failures!",
  1438. (long long)bp->b_bn);
  1439. xfs_alert(btp->bt_mount,
  1440. "Please run xfs_repair to determine the extent of the problem.");
  1441. }
  1442. xfs_buf_rele(bp);
  1443. }
  1444. if (loop++ != 0)
  1445. delay(100);
  1446. }
  1447. }
  1448. static enum lru_status
  1449. xfs_buftarg_isolate(
  1450. struct list_head *item,
  1451. struct list_lru_one *lru,
  1452. spinlock_t *lru_lock,
  1453. void *arg)
  1454. {
  1455. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1456. struct list_head *dispose = arg;
  1457. /*
  1458. * we are inverting the lru lock/bp->b_lock here, so use a trylock.
  1459. * If we fail to get the lock, just skip it.
  1460. */
  1461. if (!spin_trylock(&bp->b_lock))
  1462. return LRU_SKIP;
  1463. /*
  1464. * Decrement the b_lru_ref count unless the value is already
  1465. * zero. If the value is already zero, we need to reclaim the
  1466. * buffer, otherwise it gets another trip through the LRU.
  1467. */
  1468. if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
  1469. spin_unlock(&bp->b_lock);
  1470. return LRU_ROTATE;
  1471. }
  1472. bp->b_state |= XFS_BSTATE_DISPOSE;
  1473. list_lru_isolate_move(lru, item, dispose);
  1474. spin_unlock(&bp->b_lock);
  1475. return LRU_REMOVED;
  1476. }
  1477. static unsigned long
  1478. xfs_buftarg_shrink_scan(
  1479. struct shrinker *shrink,
  1480. struct shrink_control *sc)
  1481. {
  1482. struct xfs_buftarg *btp = container_of(shrink,
  1483. struct xfs_buftarg, bt_shrinker);
  1484. LIST_HEAD(dispose);
  1485. unsigned long freed;
  1486. freed = list_lru_shrink_walk(&btp->bt_lru, sc,
  1487. xfs_buftarg_isolate, &dispose);
  1488. while (!list_empty(&dispose)) {
  1489. struct xfs_buf *bp;
  1490. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1491. list_del_init(&bp->b_lru);
  1492. xfs_buf_rele(bp);
  1493. }
  1494. return freed;
  1495. }
  1496. static unsigned long
  1497. xfs_buftarg_shrink_count(
  1498. struct shrinker *shrink,
  1499. struct shrink_control *sc)
  1500. {
  1501. struct xfs_buftarg *btp = container_of(shrink,
  1502. struct xfs_buftarg, bt_shrinker);
  1503. return list_lru_shrink_count(&btp->bt_lru, sc);
  1504. }
  1505. void
  1506. xfs_free_buftarg(
  1507. struct xfs_mount *mp,
  1508. struct xfs_buftarg *btp)
  1509. {
  1510. unregister_shrinker(&btp->bt_shrinker);
  1511. ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
  1512. percpu_counter_destroy(&btp->bt_io_count);
  1513. list_lru_destroy(&btp->bt_lru);
  1514. xfs_blkdev_issue_flush(btp);
  1515. kmem_free(btp);
  1516. }
  1517. int
  1518. xfs_setsize_buftarg(
  1519. xfs_buftarg_t *btp,
  1520. unsigned int sectorsize)
  1521. {
  1522. /* Set up metadata sector size info */
  1523. btp->bt_meta_sectorsize = sectorsize;
  1524. btp->bt_meta_sectormask = sectorsize - 1;
  1525. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1526. xfs_warn(btp->bt_mount,
  1527. "Cannot set_blocksize to %u on device %pg",
  1528. sectorsize, btp->bt_bdev);
  1529. return -EINVAL;
  1530. }
  1531. /* Set up device logical sector size mask */
  1532. btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
  1533. btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
  1534. return 0;
  1535. }
  1536. /*
  1537. * When allocating the initial buffer target we have not yet
  1538. * read in the superblock, so don't know what sized sectors
  1539. * are being used at this early stage. Play safe.
  1540. */
  1541. STATIC int
  1542. xfs_setsize_buftarg_early(
  1543. xfs_buftarg_t *btp,
  1544. struct block_device *bdev)
  1545. {
  1546. return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
  1547. }
  1548. xfs_buftarg_t *
  1549. xfs_alloc_buftarg(
  1550. struct xfs_mount *mp,
  1551. struct block_device *bdev)
  1552. {
  1553. xfs_buftarg_t *btp;
  1554. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
  1555. btp->bt_mount = mp;
  1556. btp->bt_dev = bdev->bd_dev;
  1557. btp->bt_bdev = bdev;
  1558. btp->bt_bdi = blk_get_backing_dev_info(bdev);
  1559. if (xfs_setsize_buftarg_early(btp, bdev))
  1560. goto error;
  1561. if (list_lru_init(&btp->bt_lru))
  1562. goto error;
  1563. if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
  1564. goto error;
  1565. btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
  1566. btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
  1567. btp->bt_shrinker.seeks = DEFAULT_SEEKS;
  1568. btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
  1569. register_shrinker(&btp->bt_shrinker);
  1570. return btp;
  1571. error:
  1572. kmem_free(btp);
  1573. return NULL;
  1574. }
  1575. /*
  1576. * Add a buffer to the delayed write list.
  1577. *
  1578. * This queues a buffer for writeout if it hasn't already been. Note that
  1579. * neither this routine nor the buffer list submission functions perform
  1580. * any internal synchronization. It is expected that the lists are thread-local
  1581. * to the callers.
  1582. *
  1583. * Returns true if we queued up the buffer, or false if it already had
  1584. * been on the buffer list.
  1585. */
  1586. bool
  1587. xfs_buf_delwri_queue(
  1588. struct xfs_buf *bp,
  1589. struct list_head *list)
  1590. {
  1591. ASSERT(xfs_buf_islocked(bp));
  1592. ASSERT(!(bp->b_flags & XBF_READ));
  1593. /*
  1594. * If the buffer is already marked delwri it already is queued up
  1595. * by someone else for imediate writeout. Just ignore it in that
  1596. * case.
  1597. */
  1598. if (bp->b_flags & _XBF_DELWRI_Q) {
  1599. trace_xfs_buf_delwri_queued(bp, _RET_IP_);
  1600. return false;
  1601. }
  1602. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1603. /*
  1604. * If a buffer gets written out synchronously or marked stale while it
  1605. * is on a delwri list we lazily remove it. To do this, the other party
  1606. * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
  1607. * It remains referenced and on the list. In a rare corner case it
  1608. * might get readded to a delwri list after the synchronous writeout, in
  1609. * which case we need just need to re-add the flag here.
  1610. */
  1611. bp->b_flags |= _XBF_DELWRI_Q;
  1612. if (list_empty(&bp->b_list)) {
  1613. atomic_inc(&bp->b_hold);
  1614. list_add_tail(&bp->b_list, list);
  1615. }
  1616. return true;
  1617. }
  1618. /*
  1619. * Compare function is more complex than it needs to be because
  1620. * the return value is only 32 bits and we are doing comparisons
  1621. * on 64 bit values
  1622. */
  1623. static int
  1624. xfs_buf_cmp(
  1625. void *priv,
  1626. struct list_head *a,
  1627. struct list_head *b)
  1628. {
  1629. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1630. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1631. xfs_daddr_t diff;
  1632. diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
  1633. if (diff < 0)
  1634. return -1;
  1635. if (diff > 0)
  1636. return 1;
  1637. return 0;
  1638. }
  1639. /*
  1640. * submit buffers for write.
  1641. *
  1642. * When we have a large buffer list, we do not want to hold all the buffers
  1643. * locked while we block on the request queue waiting for IO dispatch. To avoid
  1644. * this problem, we lock and submit buffers in groups of 50, thereby minimising
  1645. * the lock hold times for lists which may contain thousands of objects.
  1646. *
  1647. * To do this, we sort the buffer list before we walk the list to lock and
  1648. * submit buffers, and we plug and unplug around each group of buffers we
  1649. * submit.
  1650. */
  1651. static int
  1652. xfs_buf_delwri_submit_buffers(
  1653. struct list_head *buffer_list,
  1654. struct list_head *wait_list)
  1655. {
  1656. struct xfs_buf *bp, *n;
  1657. LIST_HEAD (submit_list);
  1658. int pinned = 0;
  1659. struct blk_plug plug;
  1660. list_sort(NULL, buffer_list, xfs_buf_cmp);
  1661. blk_start_plug(&plug);
  1662. list_for_each_entry_safe(bp, n, buffer_list, b_list) {
  1663. if (!wait_list) {
  1664. if (xfs_buf_ispinned(bp)) {
  1665. pinned++;
  1666. continue;
  1667. }
  1668. if (!xfs_buf_trylock(bp))
  1669. continue;
  1670. } else {
  1671. xfs_buf_lock(bp);
  1672. }
  1673. /*
  1674. * Someone else might have written the buffer synchronously or
  1675. * marked it stale in the meantime. In that case only the
  1676. * _XBF_DELWRI_Q flag got cleared, and we have to drop the
  1677. * reference and remove it from the list here.
  1678. */
  1679. if (!(bp->b_flags & _XBF_DELWRI_Q)) {
  1680. list_del_init(&bp->b_list);
  1681. xfs_buf_relse(bp);
  1682. continue;
  1683. }
  1684. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1685. /*
  1686. * We do all IO submission async. This means if we need
  1687. * to wait for IO completion we need to take an extra
  1688. * reference so the buffer is still valid on the other
  1689. * side. We need to move the buffer onto the io_list
  1690. * at this point so the caller can still access it.
  1691. */
  1692. bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
  1693. bp->b_flags |= XBF_WRITE | XBF_ASYNC;
  1694. if (wait_list) {
  1695. xfs_buf_hold(bp);
  1696. list_move_tail(&bp->b_list, wait_list);
  1697. } else
  1698. list_del_init(&bp->b_list);
  1699. xfs_buf_submit(bp);
  1700. }
  1701. blk_finish_plug(&plug);
  1702. return pinned;
  1703. }
  1704. /*
  1705. * Write out a buffer list asynchronously.
  1706. *
  1707. * This will take the @buffer_list, write all non-locked and non-pinned buffers
  1708. * out and not wait for I/O completion on any of the buffers. This interface
  1709. * is only safely useable for callers that can track I/O completion by higher
  1710. * level means, e.g. AIL pushing as the @buffer_list is consumed in this
  1711. * function.
  1712. */
  1713. int
  1714. xfs_buf_delwri_submit_nowait(
  1715. struct list_head *buffer_list)
  1716. {
  1717. return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
  1718. }
  1719. /*
  1720. * Write out a buffer list synchronously.
  1721. *
  1722. * This will take the @buffer_list, write all buffers out and wait for I/O
  1723. * completion on all of the buffers. @buffer_list is consumed by the function,
  1724. * so callers must have some other way of tracking buffers if they require such
  1725. * functionality.
  1726. */
  1727. int
  1728. xfs_buf_delwri_submit(
  1729. struct list_head *buffer_list)
  1730. {
  1731. LIST_HEAD (wait_list);
  1732. int error = 0, error2;
  1733. struct xfs_buf *bp;
  1734. xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
  1735. /* Wait for IO to complete. */
  1736. while (!list_empty(&wait_list)) {
  1737. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1738. list_del_init(&bp->b_list);
  1739. /* locking the buffer will wait for async IO completion. */
  1740. xfs_buf_lock(bp);
  1741. error2 = bp->b_error;
  1742. xfs_buf_relse(bp);
  1743. if (!error)
  1744. error = error2;
  1745. }
  1746. return error;
  1747. }
  1748. int __init
  1749. xfs_buf_init(void)
  1750. {
  1751. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1752. KM_ZONE_HWALIGN, NULL);
  1753. if (!xfs_buf_zone)
  1754. goto out;
  1755. return 0;
  1756. out:
  1757. return -ENOMEM;
  1758. }
  1759. void
  1760. xfs_buf_terminate(void)
  1761. {
  1762. kmem_zone_destroy(xfs_buf_zone);
  1763. }