xfs_buf.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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 <linux/list_sort.h>
  37. #include "xfs_sb.h"
  38. #include "xfs_inum.h"
  39. #include "xfs_log.h"
  40. #include "xfs_ag.h"
  41. #include "xfs_mount.h"
  42. #include "xfs_trace.h"
  43. static kmem_zone_t *xfs_buf_zone;
  44. STATIC int xfsbufd(void *);
  45. STATIC int xfsbufd_wakeup(struct shrinker *, int, gfp_t);
  46. STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
  47. static struct shrinker xfs_buf_shake = {
  48. .shrink = xfsbufd_wakeup,
  49. .seeks = DEFAULT_SEEKS,
  50. };
  51. static struct workqueue_struct *xfslogd_workqueue;
  52. struct workqueue_struct *xfsdatad_workqueue;
  53. struct workqueue_struct *xfsconvertd_workqueue;
  54. #ifdef XFS_BUF_LOCK_TRACKING
  55. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  56. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  57. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  58. #else
  59. # define XB_SET_OWNER(bp) do { } while (0)
  60. # define XB_CLEAR_OWNER(bp) do { } while (0)
  61. # define XB_GET_OWNER(bp) do { } while (0)
  62. #endif
  63. #define xb_to_gfp(flags) \
  64. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
  65. ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
  66. #define xb_to_km(flags) \
  67. (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
  68. #define xfs_buf_allocate(flags) \
  69. kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
  70. #define xfs_buf_deallocate(bp) \
  71. kmem_zone_free(xfs_buf_zone, (bp));
  72. static inline int
  73. xfs_buf_is_vmapped(
  74. struct xfs_buf *bp)
  75. {
  76. /*
  77. * Return true if the buffer is vmapped.
  78. *
  79. * The XBF_MAPPED flag is set if the buffer should be mapped, but the
  80. * code is clever enough to know it doesn't have to map a single page,
  81. * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
  82. */
  83. return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
  84. }
  85. static inline int
  86. xfs_buf_vmap_len(
  87. struct xfs_buf *bp)
  88. {
  89. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  90. }
  91. /*
  92. * Page Region interfaces.
  93. *
  94. * For pages in filesystems where the blocksize is smaller than the
  95. * pagesize, we use the page->private field (long) to hold a bitmap
  96. * of uptodate regions within the page.
  97. *
  98. * Each such region is "bytes per page / bits per long" bytes long.
  99. *
  100. * NBPPR == number-of-bytes-per-page-region
  101. * BTOPR == bytes-to-page-region (rounded up)
  102. * BTOPRT == bytes-to-page-region-truncated (rounded down)
  103. */
  104. #if (BITS_PER_LONG == 32)
  105. #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
  106. #elif (BITS_PER_LONG == 64)
  107. #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
  108. #else
  109. #error BITS_PER_LONG must be 32 or 64
  110. #endif
  111. #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
  112. #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
  113. #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
  114. STATIC unsigned long
  115. page_region_mask(
  116. size_t offset,
  117. size_t length)
  118. {
  119. unsigned long mask;
  120. int first, final;
  121. first = BTOPR(offset);
  122. final = BTOPRT(offset + length - 1);
  123. first = min(first, final);
  124. mask = ~0UL;
  125. mask <<= BITS_PER_LONG - (final - first);
  126. mask >>= BITS_PER_LONG - (final);
  127. ASSERT(offset + length <= PAGE_CACHE_SIZE);
  128. ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
  129. return mask;
  130. }
  131. STATIC void
  132. set_page_region(
  133. struct page *page,
  134. size_t offset,
  135. size_t length)
  136. {
  137. set_page_private(page,
  138. page_private(page) | page_region_mask(offset, length));
  139. if (page_private(page) == ~0UL)
  140. SetPageUptodate(page);
  141. }
  142. STATIC int
  143. test_page_region(
  144. struct page *page,
  145. size_t offset,
  146. size_t length)
  147. {
  148. unsigned long mask = page_region_mask(offset, length);
  149. return (mask && (page_private(page) & mask) == mask);
  150. }
  151. /*
  152. * Internal xfs_buf_t object manipulation
  153. */
  154. STATIC void
  155. _xfs_buf_initialize(
  156. xfs_buf_t *bp,
  157. xfs_buftarg_t *target,
  158. xfs_off_t range_base,
  159. size_t range_length,
  160. xfs_buf_flags_t flags)
  161. {
  162. /*
  163. * We don't want certain flags to appear in b_flags.
  164. */
  165. flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
  166. memset(bp, 0, sizeof(xfs_buf_t));
  167. atomic_set(&bp->b_hold, 1);
  168. init_completion(&bp->b_iowait);
  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. if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
  239. uint i;
  240. if (xfs_buf_is_vmapped(bp))
  241. vm_unmap_ram(bp->b_addr - bp->b_offset,
  242. bp->b_page_count);
  243. for (i = 0; i < bp->b_page_count; i++) {
  244. struct page *page = bp->b_pages[i];
  245. if (bp->b_flags & _XBF_PAGE_CACHE)
  246. ASSERT(!PagePrivate(page));
  247. page_cache_release(page);
  248. }
  249. }
  250. _xfs_buf_free_pages(bp);
  251. xfs_buf_deallocate(bp);
  252. }
  253. /*
  254. * Finds all pages for buffer in question and builds it's page list.
  255. */
  256. STATIC int
  257. _xfs_buf_lookup_pages(
  258. xfs_buf_t *bp,
  259. uint flags)
  260. {
  261. struct address_space *mapping = bp->b_target->bt_mapping;
  262. size_t blocksize = bp->b_target->bt_bsize;
  263. size_t size = bp->b_count_desired;
  264. size_t nbytes, offset;
  265. gfp_t gfp_mask = xb_to_gfp(flags);
  266. unsigned short page_count, i;
  267. pgoff_t first;
  268. xfs_off_t end;
  269. int error;
  270. end = bp->b_file_offset + bp->b_buffer_length;
  271. page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
  272. error = _xfs_buf_get_pages(bp, page_count, flags);
  273. if (unlikely(error))
  274. return error;
  275. bp->b_flags |= _XBF_PAGE_CACHE;
  276. offset = bp->b_offset;
  277. first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
  278. for (i = 0; i < bp->b_page_count; i++) {
  279. struct page *page;
  280. uint retries = 0;
  281. retry:
  282. page = find_or_create_page(mapping, first + i, gfp_mask);
  283. if (unlikely(page == NULL)) {
  284. if (flags & XBF_READ_AHEAD) {
  285. bp->b_page_count = i;
  286. for (i = 0; i < bp->b_page_count; i++)
  287. unlock_page(bp->b_pages[i]);
  288. return -ENOMEM;
  289. }
  290. /*
  291. * This could deadlock.
  292. *
  293. * But until all the XFS lowlevel code is revamped to
  294. * handle buffer allocation failures we can't do much.
  295. */
  296. if (!(++retries % 100))
  297. printk(KERN_ERR
  298. "XFS: possible memory allocation "
  299. "deadlock in %s (mode:0x%x)\n",
  300. __func__, gfp_mask);
  301. XFS_STATS_INC(xb_page_retries);
  302. xfsbufd_wakeup(NULL, 0, gfp_mask);
  303. congestion_wait(BLK_RW_ASYNC, HZ/50);
  304. goto retry;
  305. }
  306. XFS_STATS_INC(xb_page_found);
  307. nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
  308. size -= nbytes;
  309. ASSERT(!PagePrivate(page));
  310. if (!PageUptodate(page)) {
  311. page_count--;
  312. if (blocksize >= PAGE_CACHE_SIZE) {
  313. if (flags & XBF_READ)
  314. bp->b_flags |= _XBF_PAGE_LOCKED;
  315. } else if (!PagePrivate(page)) {
  316. if (test_page_region(page, offset, nbytes))
  317. page_count++;
  318. }
  319. }
  320. bp->b_pages[i] = page;
  321. offset = 0;
  322. }
  323. if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
  324. for (i = 0; i < bp->b_page_count; i++)
  325. unlock_page(bp->b_pages[i]);
  326. }
  327. if (page_count == bp->b_page_count)
  328. bp->b_flags |= XBF_DONE;
  329. return error;
  330. }
  331. /*
  332. * Map buffer into kernel address-space if nessecary.
  333. */
  334. STATIC int
  335. _xfs_buf_map_pages(
  336. xfs_buf_t *bp,
  337. uint flags)
  338. {
  339. /* A single page buffer is always mappable */
  340. if (bp->b_page_count == 1) {
  341. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  342. bp->b_flags |= XBF_MAPPED;
  343. } else if (flags & XBF_MAPPED) {
  344. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  345. -1, PAGE_KERNEL);
  346. if (unlikely(bp->b_addr == NULL))
  347. return -ENOMEM;
  348. bp->b_addr += bp->b_offset;
  349. bp->b_flags |= XBF_MAPPED;
  350. }
  351. return 0;
  352. }
  353. /*
  354. * Finding and Reading Buffers
  355. */
  356. /*
  357. * Look up, and creates if absent, a lockable buffer for
  358. * a given range of an inode. The buffer is returned
  359. * locked. If other overlapping buffers exist, they are
  360. * released before the new buffer is created and locked,
  361. * which may imply that this call will block until those buffers
  362. * are unlocked. No I/O is implied by this call.
  363. */
  364. xfs_buf_t *
  365. _xfs_buf_find(
  366. xfs_buftarg_t *btp, /* block device target */
  367. xfs_off_t ioff, /* starting offset of range */
  368. size_t isize, /* length of range */
  369. xfs_buf_flags_t flags,
  370. xfs_buf_t *new_bp)
  371. {
  372. xfs_off_t range_base;
  373. size_t range_length;
  374. struct xfs_perag *pag;
  375. struct rb_node **rbp;
  376. struct rb_node *parent;
  377. xfs_buf_t *bp;
  378. range_base = (ioff << BBSHIFT);
  379. range_length = (isize << BBSHIFT);
  380. /* Check for IOs smaller than the sector size / not sector aligned */
  381. ASSERT(!(range_length < (1 << btp->bt_sshift)));
  382. ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
  383. /* get tree root */
  384. pag = xfs_perag_get(btp->bt_mount,
  385. xfs_daddr_to_agno(btp->bt_mount, ioff));
  386. /* walk tree */
  387. spin_lock(&pag->pag_buf_lock);
  388. rbp = &pag->pag_buf_tree.rb_node;
  389. parent = NULL;
  390. bp = NULL;
  391. while (*rbp) {
  392. parent = *rbp;
  393. bp = rb_entry(parent, struct xfs_buf, b_rbnode);
  394. if (range_base < bp->b_file_offset)
  395. rbp = &(*rbp)->rb_left;
  396. else if (range_base > bp->b_file_offset)
  397. rbp = &(*rbp)->rb_right;
  398. else {
  399. /*
  400. * found a block offset match. If the range doesn't
  401. * match, the only way this is allowed is if the buffer
  402. * in the cache is stale and the transaction that made
  403. * it stale has not yet committed. i.e. we are
  404. * reallocating a busy extent. Skip this buffer and
  405. * continue searching to the right for an exact match.
  406. */
  407. if (bp->b_buffer_length != range_length) {
  408. ASSERT(bp->b_flags & XBF_STALE);
  409. rbp = &(*rbp)->rb_right;
  410. continue;
  411. }
  412. atomic_inc(&bp->b_hold);
  413. goto found;
  414. }
  415. }
  416. /* No match found */
  417. if (new_bp) {
  418. _xfs_buf_initialize(new_bp, btp, range_base,
  419. range_length, flags);
  420. rb_link_node(&new_bp->b_rbnode, parent, rbp);
  421. rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
  422. /* the buffer keeps the perag reference until it is freed */
  423. new_bp->b_pag = pag;
  424. spin_unlock(&pag->pag_buf_lock);
  425. } else {
  426. XFS_STATS_INC(xb_miss_locked);
  427. spin_unlock(&pag->pag_buf_lock);
  428. xfs_perag_put(pag);
  429. }
  430. return new_bp;
  431. found:
  432. spin_unlock(&pag->pag_buf_lock);
  433. xfs_perag_put(pag);
  434. if (xfs_buf_cond_lock(bp)) {
  435. /* failed, so wait for the lock if requested. */
  436. if (!(flags & XBF_TRYLOCK)) {
  437. xfs_buf_lock(bp);
  438. XFS_STATS_INC(xb_get_locked_waited);
  439. } else {
  440. xfs_buf_rele(bp);
  441. XFS_STATS_INC(xb_busy_locked);
  442. return NULL;
  443. }
  444. }
  445. if (bp->b_flags & XBF_STALE) {
  446. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  447. bp->b_flags &= XBF_MAPPED;
  448. }
  449. trace_xfs_buf_find(bp, flags, _RET_IP_);
  450. XFS_STATS_INC(xb_get_locked);
  451. return bp;
  452. }
  453. /*
  454. * Assembles a buffer covering the specified range.
  455. * Storage in memory for all portions of the buffer will be allocated,
  456. * although backing storage may not be.
  457. */
  458. xfs_buf_t *
  459. xfs_buf_get(
  460. xfs_buftarg_t *target,/* target for buffer */
  461. xfs_off_t ioff, /* starting offset of range */
  462. size_t isize, /* length of range */
  463. xfs_buf_flags_t flags)
  464. {
  465. xfs_buf_t *bp, *new_bp;
  466. int error = 0, i;
  467. new_bp = xfs_buf_allocate(flags);
  468. if (unlikely(!new_bp))
  469. return NULL;
  470. bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
  471. if (bp == new_bp) {
  472. error = _xfs_buf_lookup_pages(bp, flags);
  473. if (error)
  474. goto no_buffer;
  475. } else {
  476. xfs_buf_deallocate(new_bp);
  477. if (unlikely(bp == NULL))
  478. return NULL;
  479. }
  480. for (i = 0; i < bp->b_page_count; i++)
  481. mark_page_accessed(bp->b_pages[i]);
  482. if (!(bp->b_flags & XBF_MAPPED)) {
  483. error = _xfs_buf_map_pages(bp, flags);
  484. if (unlikely(error)) {
  485. printk(KERN_WARNING "%s: failed to map pages\n",
  486. __func__);
  487. goto no_buffer;
  488. }
  489. }
  490. XFS_STATS_INC(xb_get);
  491. /*
  492. * Always fill in the block number now, the mapped cases can do
  493. * their own overlay of this later.
  494. */
  495. bp->b_bn = ioff;
  496. bp->b_count_desired = bp->b_buffer_length;
  497. trace_xfs_buf_get(bp, flags, _RET_IP_);
  498. return bp;
  499. no_buffer:
  500. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  501. xfs_buf_unlock(bp);
  502. xfs_buf_rele(bp);
  503. return NULL;
  504. }
  505. STATIC int
  506. _xfs_buf_read(
  507. xfs_buf_t *bp,
  508. xfs_buf_flags_t flags)
  509. {
  510. int status;
  511. ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
  512. ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
  513. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
  514. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  515. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
  516. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  517. status = xfs_buf_iorequest(bp);
  518. if (status || XFS_BUF_ISERROR(bp) || (flags & XBF_ASYNC))
  519. return status;
  520. return xfs_buf_iowait(bp);
  521. }
  522. xfs_buf_t *
  523. xfs_buf_read(
  524. xfs_buftarg_t *target,
  525. xfs_off_t ioff,
  526. size_t isize,
  527. xfs_buf_flags_t flags)
  528. {
  529. xfs_buf_t *bp;
  530. flags |= XBF_READ;
  531. bp = xfs_buf_get(target, ioff, isize, flags);
  532. if (bp) {
  533. trace_xfs_buf_read(bp, flags, _RET_IP_);
  534. if (!XFS_BUF_ISDONE(bp)) {
  535. XFS_STATS_INC(xb_get_read);
  536. _xfs_buf_read(bp, flags);
  537. } else if (flags & XBF_ASYNC) {
  538. /*
  539. * Read ahead call which is already satisfied,
  540. * drop the buffer
  541. */
  542. goto no_buffer;
  543. } else {
  544. /* We do not want read in the flags */
  545. bp->b_flags &= ~XBF_READ;
  546. }
  547. }
  548. return bp;
  549. no_buffer:
  550. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  551. xfs_buf_unlock(bp);
  552. xfs_buf_rele(bp);
  553. return NULL;
  554. }
  555. /*
  556. * If we are not low on memory then do the readahead in a deadlock
  557. * safe manner.
  558. */
  559. void
  560. xfs_buf_readahead(
  561. xfs_buftarg_t *target,
  562. xfs_off_t ioff,
  563. size_t isize)
  564. {
  565. struct backing_dev_info *bdi;
  566. bdi = target->bt_mapping->backing_dev_info;
  567. if (bdi_read_congested(bdi))
  568. return;
  569. xfs_buf_read(target, ioff, isize,
  570. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
  571. }
  572. /*
  573. * Read an uncached buffer from disk. Allocates and returns a locked
  574. * buffer containing the disk contents or nothing.
  575. */
  576. struct xfs_buf *
  577. xfs_buf_read_uncached(
  578. struct xfs_mount *mp,
  579. struct xfs_buftarg *target,
  580. xfs_daddr_t daddr,
  581. size_t length,
  582. int flags)
  583. {
  584. xfs_buf_t *bp;
  585. int error;
  586. bp = xfs_buf_get_uncached(target, length, flags);
  587. if (!bp)
  588. return NULL;
  589. /* set up the buffer for a read IO */
  590. xfs_buf_lock(bp);
  591. XFS_BUF_SET_ADDR(bp, daddr);
  592. XFS_BUF_READ(bp);
  593. XFS_BUF_BUSY(bp);
  594. xfsbdstrat(mp, bp);
  595. error = xfs_buf_iowait(bp);
  596. if (error || bp->b_error) {
  597. xfs_buf_relse(bp);
  598. return NULL;
  599. }
  600. return bp;
  601. }
  602. xfs_buf_t *
  603. xfs_buf_get_empty(
  604. size_t len,
  605. xfs_buftarg_t *target)
  606. {
  607. xfs_buf_t *bp;
  608. bp = xfs_buf_allocate(0);
  609. if (bp)
  610. _xfs_buf_initialize(bp, target, 0, len, 0);
  611. return bp;
  612. }
  613. static inline struct page *
  614. mem_to_page(
  615. void *addr)
  616. {
  617. if ((!is_vmalloc_addr(addr))) {
  618. return virt_to_page(addr);
  619. } else {
  620. return vmalloc_to_page(addr);
  621. }
  622. }
  623. int
  624. xfs_buf_associate_memory(
  625. xfs_buf_t *bp,
  626. void *mem,
  627. size_t len)
  628. {
  629. int rval;
  630. int i = 0;
  631. unsigned long pageaddr;
  632. unsigned long offset;
  633. size_t buflen;
  634. int page_count;
  635. pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
  636. offset = (unsigned long)mem - pageaddr;
  637. buflen = PAGE_CACHE_ALIGN(len + offset);
  638. page_count = buflen >> PAGE_CACHE_SHIFT;
  639. /* Free any previous set of page pointers */
  640. if (bp->b_pages)
  641. _xfs_buf_free_pages(bp);
  642. bp->b_pages = NULL;
  643. bp->b_addr = mem;
  644. rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
  645. if (rval)
  646. return rval;
  647. bp->b_offset = offset;
  648. for (i = 0; i < bp->b_page_count; i++) {
  649. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  650. pageaddr += PAGE_CACHE_SIZE;
  651. }
  652. bp->b_count_desired = len;
  653. bp->b_buffer_length = buflen;
  654. bp->b_flags |= XBF_MAPPED;
  655. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  656. return 0;
  657. }
  658. xfs_buf_t *
  659. xfs_buf_get_uncached(
  660. struct xfs_buftarg *target,
  661. size_t len,
  662. int flags)
  663. {
  664. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  665. int error, i;
  666. xfs_buf_t *bp;
  667. bp = xfs_buf_allocate(0);
  668. if (unlikely(bp == NULL))
  669. goto fail;
  670. _xfs_buf_initialize(bp, target, 0, len, 0);
  671. error = _xfs_buf_get_pages(bp, page_count, 0);
  672. if (error)
  673. goto fail_free_buf;
  674. for (i = 0; i < page_count; i++) {
  675. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  676. if (!bp->b_pages[i])
  677. goto fail_free_mem;
  678. }
  679. bp->b_flags |= _XBF_PAGES;
  680. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  681. if (unlikely(error)) {
  682. printk(KERN_WARNING "%s: failed to map pages\n",
  683. __func__);
  684. goto fail_free_mem;
  685. }
  686. xfs_buf_unlock(bp);
  687. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  688. return bp;
  689. fail_free_mem:
  690. while (--i >= 0)
  691. __free_page(bp->b_pages[i]);
  692. _xfs_buf_free_pages(bp);
  693. fail_free_buf:
  694. xfs_buf_deallocate(bp);
  695. fail:
  696. return NULL;
  697. }
  698. /*
  699. * Increment reference count on buffer, to hold the buffer concurrently
  700. * with another thread which may release (free) the buffer asynchronously.
  701. * Must hold the buffer already to call this function.
  702. */
  703. void
  704. xfs_buf_hold(
  705. xfs_buf_t *bp)
  706. {
  707. trace_xfs_buf_hold(bp, _RET_IP_);
  708. atomic_inc(&bp->b_hold);
  709. }
  710. /*
  711. * Releases a hold on the specified buffer. If the
  712. * the hold count is 1, calls xfs_buf_free.
  713. */
  714. void
  715. xfs_buf_rele(
  716. xfs_buf_t *bp)
  717. {
  718. struct xfs_perag *pag = bp->b_pag;
  719. trace_xfs_buf_rele(bp, _RET_IP_);
  720. if (!pag) {
  721. ASSERT(!bp->b_relse);
  722. ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
  723. if (atomic_dec_and_test(&bp->b_hold))
  724. xfs_buf_free(bp);
  725. return;
  726. }
  727. ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
  728. ASSERT(atomic_read(&bp->b_hold) > 0);
  729. if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
  730. if (bp->b_relse) {
  731. atomic_inc(&bp->b_hold);
  732. spin_unlock(&pag->pag_buf_lock);
  733. bp->b_relse(bp);
  734. } else {
  735. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  736. rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
  737. spin_unlock(&pag->pag_buf_lock);
  738. xfs_perag_put(pag);
  739. xfs_buf_free(bp);
  740. }
  741. }
  742. }
  743. /*
  744. * Mutual exclusion on buffers. Locking model:
  745. *
  746. * Buffers associated with inodes for which buffer locking
  747. * is not enabled are not protected by semaphores, and are
  748. * assumed to be exclusively owned by the caller. There is a
  749. * spinlock in the buffer, used by the caller when concurrent
  750. * access is possible.
  751. */
  752. /*
  753. * Locks a buffer object, if it is not already locked. Note that this in
  754. * no way locks the underlying pages, so it is only useful for
  755. * synchronizing concurrent use of buffer objects, not for synchronizing
  756. * independent access to the underlying pages.
  757. *
  758. * If we come across a stale, pinned, locked buffer, we know that we are
  759. * being asked to lock a buffer that has been reallocated. Because it is
  760. * pinned, we know that the log has not been pushed to disk and hence it
  761. * will still be locked. Rather than continuing to have trylock attempts
  762. * fail until someone else pushes the log, push it ourselves before
  763. * returning. This means that the xfsaild will not get stuck trying
  764. * to push on stale inode buffers.
  765. */
  766. int
  767. xfs_buf_cond_lock(
  768. xfs_buf_t *bp)
  769. {
  770. int locked;
  771. locked = down_trylock(&bp->b_sema) == 0;
  772. if (locked)
  773. XB_SET_OWNER(bp);
  774. else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  775. xfs_log_force(bp->b_target->bt_mount, 0);
  776. trace_xfs_buf_cond_lock(bp, _RET_IP_);
  777. return locked ? 0 : -EBUSY;
  778. }
  779. int
  780. xfs_buf_lock_value(
  781. xfs_buf_t *bp)
  782. {
  783. return bp->b_sema.count;
  784. }
  785. /*
  786. * Locks a buffer object.
  787. * Note that this in no way locks the underlying pages, so it is only
  788. * useful for synchronizing concurrent use of buffer objects, not for
  789. * synchronizing independent access to the underlying pages.
  790. *
  791. * If we come across a stale, pinned, locked buffer, we know that we
  792. * are being asked to lock a buffer that has been reallocated. Because
  793. * it is pinned, we know that the log has not been pushed to disk and
  794. * hence it will still be locked. Rather than sleeping until someone
  795. * else pushes the log, push it ourselves before trying to get the lock.
  796. */
  797. void
  798. xfs_buf_lock(
  799. xfs_buf_t *bp)
  800. {
  801. trace_xfs_buf_lock(bp, _RET_IP_);
  802. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  803. xfs_log_force(bp->b_target->bt_mount, 0);
  804. if (atomic_read(&bp->b_io_remaining))
  805. blk_run_address_space(bp->b_target->bt_mapping);
  806. down(&bp->b_sema);
  807. XB_SET_OWNER(bp);
  808. trace_xfs_buf_lock_done(bp, _RET_IP_);
  809. }
  810. /*
  811. * Releases the lock on the buffer object.
  812. * If the buffer is marked delwri but is not queued, do so before we
  813. * unlock the buffer as we need to set flags correctly. We also need to
  814. * take a reference for the delwri queue because the unlocker is going to
  815. * drop their's and they don't know we just queued it.
  816. */
  817. void
  818. xfs_buf_unlock(
  819. xfs_buf_t *bp)
  820. {
  821. if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
  822. atomic_inc(&bp->b_hold);
  823. bp->b_flags |= XBF_ASYNC;
  824. xfs_buf_delwri_queue(bp, 0);
  825. }
  826. XB_CLEAR_OWNER(bp);
  827. up(&bp->b_sema);
  828. trace_xfs_buf_unlock(bp, _RET_IP_);
  829. }
  830. STATIC void
  831. xfs_buf_wait_unpin(
  832. xfs_buf_t *bp)
  833. {
  834. DECLARE_WAITQUEUE (wait, current);
  835. if (atomic_read(&bp->b_pin_count) == 0)
  836. return;
  837. add_wait_queue(&bp->b_waiters, &wait);
  838. for (;;) {
  839. set_current_state(TASK_UNINTERRUPTIBLE);
  840. if (atomic_read(&bp->b_pin_count) == 0)
  841. break;
  842. if (atomic_read(&bp->b_io_remaining))
  843. blk_run_address_space(bp->b_target->bt_mapping);
  844. schedule();
  845. }
  846. remove_wait_queue(&bp->b_waiters, &wait);
  847. set_current_state(TASK_RUNNING);
  848. }
  849. /*
  850. * Buffer Utility Routines
  851. */
  852. STATIC void
  853. xfs_buf_iodone_work(
  854. struct work_struct *work)
  855. {
  856. xfs_buf_t *bp =
  857. container_of(work, xfs_buf_t, b_iodone_work);
  858. if (bp->b_iodone)
  859. (*(bp->b_iodone))(bp);
  860. else if (bp->b_flags & XBF_ASYNC)
  861. xfs_buf_relse(bp);
  862. }
  863. void
  864. xfs_buf_ioend(
  865. xfs_buf_t *bp,
  866. int schedule)
  867. {
  868. trace_xfs_buf_iodone(bp, _RET_IP_);
  869. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  870. if (bp->b_error == 0)
  871. bp->b_flags |= XBF_DONE;
  872. if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
  873. if (schedule) {
  874. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  875. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  876. } else {
  877. xfs_buf_iodone_work(&bp->b_iodone_work);
  878. }
  879. } else {
  880. complete(&bp->b_iowait);
  881. }
  882. }
  883. void
  884. xfs_buf_ioerror(
  885. xfs_buf_t *bp,
  886. int error)
  887. {
  888. ASSERT(error >= 0 && error <= 0xffff);
  889. bp->b_error = (unsigned short)error;
  890. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  891. }
  892. int
  893. xfs_bwrite(
  894. struct xfs_mount *mp,
  895. struct xfs_buf *bp)
  896. {
  897. int error;
  898. bp->b_flags |= XBF_WRITE;
  899. bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
  900. xfs_buf_delwri_dequeue(bp);
  901. xfs_bdstrat_cb(bp);
  902. error = xfs_buf_iowait(bp);
  903. if (error)
  904. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  905. xfs_buf_relse(bp);
  906. return error;
  907. }
  908. void
  909. xfs_bdwrite(
  910. void *mp,
  911. struct xfs_buf *bp)
  912. {
  913. trace_xfs_buf_bdwrite(bp, _RET_IP_);
  914. bp->b_flags &= ~XBF_READ;
  915. bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
  916. xfs_buf_delwri_queue(bp, 1);
  917. }
  918. /*
  919. * Called when we want to stop a buffer from getting written or read.
  920. * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
  921. * so that the proper iodone callbacks get called.
  922. */
  923. STATIC int
  924. xfs_bioerror(
  925. xfs_buf_t *bp)
  926. {
  927. #ifdef XFSERRORDEBUG
  928. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  929. #endif
  930. /*
  931. * No need to wait until the buffer is unpinned, we aren't flushing it.
  932. */
  933. XFS_BUF_ERROR(bp, EIO);
  934. /*
  935. * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
  936. */
  937. XFS_BUF_UNREAD(bp);
  938. XFS_BUF_UNDELAYWRITE(bp);
  939. XFS_BUF_UNDONE(bp);
  940. XFS_BUF_STALE(bp);
  941. xfs_buf_ioend(bp, 0);
  942. return EIO;
  943. }
  944. /*
  945. * Same as xfs_bioerror, except that we are releasing the buffer
  946. * here ourselves, and avoiding the xfs_buf_ioend call.
  947. * This is meant for userdata errors; metadata bufs come with
  948. * iodone functions attached, so that we can track down errors.
  949. */
  950. STATIC int
  951. xfs_bioerror_relse(
  952. struct xfs_buf *bp)
  953. {
  954. int64_t fl = XFS_BUF_BFLAGS(bp);
  955. /*
  956. * No need to wait until the buffer is unpinned.
  957. * We aren't flushing it.
  958. *
  959. * chunkhold expects B_DONE to be set, whether
  960. * we actually finish the I/O or not. We don't want to
  961. * change that interface.
  962. */
  963. XFS_BUF_UNREAD(bp);
  964. XFS_BUF_UNDELAYWRITE(bp);
  965. XFS_BUF_DONE(bp);
  966. XFS_BUF_STALE(bp);
  967. XFS_BUF_CLR_IODONE_FUNC(bp);
  968. if (!(fl & XBF_ASYNC)) {
  969. /*
  970. * Mark b_error and B_ERROR _both_.
  971. * Lot's of chunkcache code assumes that.
  972. * There's no reason to mark error for
  973. * ASYNC buffers.
  974. */
  975. XFS_BUF_ERROR(bp, EIO);
  976. XFS_BUF_FINISH_IOWAIT(bp);
  977. } else {
  978. xfs_buf_relse(bp);
  979. }
  980. return EIO;
  981. }
  982. /*
  983. * All xfs metadata buffers except log state machine buffers
  984. * get this attached as their b_bdstrat callback function.
  985. * This is so that we can catch a buffer
  986. * after prematurely unpinning it to forcibly shutdown the filesystem.
  987. */
  988. int
  989. xfs_bdstrat_cb(
  990. struct xfs_buf *bp)
  991. {
  992. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  993. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  994. /*
  995. * Metadata write that didn't get logged but
  996. * written delayed anyway. These aren't associated
  997. * with a transaction, and can be ignored.
  998. */
  999. if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
  1000. return xfs_bioerror_relse(bp);
  1001. else
  1002. return xfs_bioerror(bp);
  1003. }
  1004. xfs_buf_iorequest(bp);
  1005. return 0;
  1006. }
  1007. /*
  1008. * Wrapper around bdstrat so that we can stop data from going to disk in case
  1009. * we are shutting down the filesystem. Typically user data goes thru this
  1010. * path; one of the exceptions is the superblock.
  1011. */
  1012. void
  1013. xfsbdstrat(
  1014. struct xfs_mount *mp,
  1015. struct xfs_buf *bp)
  1016. {
  1017. if (XFS_FORCED_SHUTDOWN(mp)) {
  1018. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  1019. xfs_bioerror_relse(bp);
  1020. return;
  1021. }
  1022. xfs_buf_iorequest(bp);
  1023. }
  1024. STATIC void
  1025. _xfs_buf_ioend(
  1026. xfs_buf_t *bp,
  1027. int schedule)
  1028. {
  1029. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  1030. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  1031. xfs_buf_ioend(bp, schedule);
  1032. }
  1033. }
  1034. STATIC void
  1035. xfs_buf_bio_end_io(
  1036. struct bio *bio,
  1037. int error)
  1038. {
  1039. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  1040. unsigned int blocksize = bp->b_target->bt_bsize;
  1041. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1042. xfs_buf_ioerror(bp, -error);
  1043. if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1044. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1045. do {
  1046. struct page *page = bvec->bv_page;
  1047. ASSERT(!PagePrivate(page));
  1048. if (unlikely(bp->b_error)) {
  1049. if (bp->b_flags & XBF_READ)
  1050. ClearPageUptodate(page);
  1051. } else if (blocksize >= PAGE_CACHE_SIZE) {
  1052. SetPageUptodate(page);
  1053. } else if (!PagePrivate(page) &&
  1054. (bp->b_flags & _XBF_PAGE_CACHE)) {
  1055. set_page_region(page, bvec->bv_offset, bvec->bv_len);
  1056. }
  1057. if (--bvec >= bio->bi_io_vec)
  1058. prefetchw(&bvec->bv_page->flags);
  1059. if (bp->b_flags & _XBF_PAGE_LOCKED)
  1060. unlock_page(page);
  1061. } while (bvec >= bio->bi_io_vec);
  1062. _xfs_buf_ioend(bp, 1);
  1063. bio_put(bio);
  1064. }
  1065. STATIC void
  1066. _xfs_buf_ioapply(
  1067. xfs_buf_t *bp)
  1068. {
  1069. int rw, map_i, total_nr_pages, nr_pages;
  1070. struct bio *bio;
  1071. int offset = bp->b_offset;
  1072. int size = bp->b_count_desired;
  1073. sector_t sector = bp->b_bn;
  1074. unsigned int blocksize = bp->b_target->bt_bsize;
  1075. total_nr_pages = bp->b_page_count;
  1076. map_i = 0;
  1077. if (bp->b_flags & XBF_ORDERED) {
  1078. ASSERT(!(bp->b_flags & XBF_READ));
  1079. rw = WRITE_FLUSH_FUA;
  1080. } else if (bp->b_flags & XBF_LOG_BUFFER) {
  1081. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1082. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1083. rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
  1084. } else if (bp->b_flags & _XBF_RUN_QUEUES) {
  1085. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1086. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1087. rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
  1088. } else {
  1089. rw = (bp->b_flags & XBF_WRITE) ? WRITE :
  1090. (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
  1091. }
  1092. /* Special code path for reading a sub page size buffer in --
  1093. * we populate up the whole page, and hence the other metadata
  1094. * in the same page. This optimization is only valid when the
  1095. * filesystem block size is not smaller than the page size.
  1096. */
  1097. if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
  1098. ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
  1099. (XBF_READ|_XBF_PAGE_LOCKED)) &&
  1100. (blocksize >= PAGE_CACHE_SIZE)) {
  1101. bio = bio_alloc(GFP_NOIO, 1);
  1102. bio->bi_bdev = bp->b_target->bt_bdev;
  1103. bio->bi_sector = sector - (offset >> BBSHIFT);
  1104. bio->bi_end_io = xfs_buf_bio_end_io;
  1105. bio->bi_private = bp;
  1106. bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
  1107. size = 0;
  1108. atomic_inc(&bp->b_io_remaining);
  1109. goto submit_io;
  1110. }
  1111. next_chunk:
  1112. atomic_inc(&bp->b_io_remaining);
  1113. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1114. if (nr_pages > total_nr_pages)
  1115. nr_pages = total_nr_pages;
  1116. bio = bio_alloc(GFP_NOIO, nr_pages);
  1117. bio->bi_bdev = bp->b_target->bt_bdev;
  1118. bio->bi_sector = sector;
  1119. bio->bi_end_io = xfs_buf_bio_end_io;
  1120. bio->bi_private = bp;
  1121. for (; size && nr_pages; nr_pages--, map_i++) {
  1122. int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
  1123. if (nbytes > size)
  1124. nbytes = size;
  1125. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1126. if (rbytes < nbytes)
  1127. break;
  1128. offset = 0;
  1129. sector += nbytes >> BBSHIFT;
  1130. size -= nbytes;
  1131. total_nr_pages--;
  1132. }
  1133. submit_io:
  1134. if (likely(bio->bi_size)) {
  1135. if (xfs_buf_is_vmapped(bp)) {
  1136. flush_kernel_vmap_range(bp->b_addr,
  1137. xfs_buf_vmap_len(bp));
  1138. }
  1139. submit_bio(rw, bio);
  1140. if (size)
  1141. goto next_chunk;
  1142. } else {
  1143. /*
  1144. * if we get here, no pages were added to the bio. However,
  1145. * we can't just error out here - if the pages are locked then
  1146. * we have to unlock them otherwise we can hang on a later
  1147. * access to the page.
  1148. */
  1149. xfs_buf_ioerror(bp, EIO);
  1150. if (bp->b_flags & _XBF_PAGE_LOCKED) {
  1151. int i;
  1152. for (i = 0; i < bp->b_page_count; i++)
  1153. unlock_page(bp->b_pages[i]);
  1154. }
  1155. bio_put(bio);
  1156. }
  1157. }
  1158. int
  1159. xfs_buf_iorequest(
  1160. xfs_buf_t *bp)
  1161. {
  1162. trace_xfs_buf_iorequest(bp, _RET_IP_);
  1163. if (bp->b_flags & XBF_DELWRI) {
  1164. xfs_buf_delwri_queue(bp, 1);
  1165. return 0;
  1166. }
  1167. if (bp->b_flags & XBF_WRITE) {
  1168. xfs_buf_wait_unpin(bp);
  1169. }
  1170. xfs_buf_hold(bp);
  1171. /* Set the count to 1 initially, this will stop an I/O
  1172. * completion callout which happens before we have started
  1173. * all the I/O from calling xfs_buf_ioend too early.
  1174. */
  1175. atomic_set(&bp->b_io_remaining, 1);
  1176. _xfs_buf_ioapply(bp);
  1177. _xfs_buf_ioend(bp, 0);
  1178. xfs_buf_rele(bp);
  1179. return 0;
  1180. }
  1181. /*
  1182. * Waits for I/O to complete on the buffer supplied.
  1183. * It returns immediately if no I/O is pending.
  1184. * It returns the I/O error code, if any, or 0 if there was no error.
  1185. */
  1186. int
  1187. xfs_buf_iowait(
  1188. xfs_buf_t *bp)
  1189. {
  1190. trace_xfs_buf_iowait(bp, _RET_IP_);
  1191. if (atomic_read(&bp->b_io_remaining))
  1192. blk_run_address_space(bp->b_target->bt_mapping);
  1193. wait_for_completion(&bp->b_iowait);
  1194. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1195. return bp->b_error;
  1196. }
  1197. xfs_caddr_t
  1198. xfs_buf_offset(
  1199. xfs_buf_t *bp,
  1200. size_t offset)
  1201. {
  1202. struct page *page;
  1203. if (bp->b_flags & XBF_MAPPED)
  1204. return XFS_BUF_PTR(bp) + offset;
  1205. offset += bp->b_offset;
  1206. page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
  1207. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
  1208. }
  1209. /*
  1210. * Move data into or out of a buffer.
  1211. */
  1212. void
  1213. xfs_buf_iomove(
  1214. xfs_buf_t *bp, /* buffer to process */
  1215. size_t boff, /* starting buffer offset */
  1216. size_t bsize, /* length to copy */
  1217. void *data, /* data address */
  1218. xfs_buf_rw_t mode) /* read/write/zero flag */
  1219. {
  1220. size_t bend, cpoff, csize;
  1221. struct page *page;
  1222. bend = boff + bsize;
  1223. while (boff < bend) {
  1224. page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
  1225. cpoff = xfs_buf_poff(boff + bp->b_offset);
  1226. csize = min_t(size_t,
  1227. PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
  1228. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1229. switch (mode) {
  1230. case XBRW_ZERO:
  1231. memset(page_address(page) + cpoff, 0, csize);
  1232. break;
  1233. case XBRW_READ:
  1234. memcpy(data, page_address(page) + cpoff, csize);
  1235. break;
  1236. case XBRW_WRITE:
  1237. memcpy(page_address(page) + cpoff, data, csize);
  1238. }
  1239. boff += csize;
  1240. data += csize;
  1241. }
  1242. }
  1243. /*
  1244. * Handling of buffer targets (buftargs).
  1245. */
  1246. /*
  1247. * Wait for any bufs with callbacks that have been submitted but
  1248. * have not yet returned... walk the hash list for the target.
  1249. */
  1250. void
  1251. xfs_wait_buftarg(
  1252. struct xfs_buftarg *btp)
  1253. {
  1254. struct xfs_perag *pag;
  1255. uint i;
  1256. for (i = 0; i < btp->bt_mount->m_sb.sb_agcount; i++) {
  1257. pag = xfs_perag_get(btp->bt_mount, i);
  1258. spin_lock(&pag->pag_buf_lock);
  1259. while (rb_first(&pag->pag_buf_tree)) {
  1260. spin_unlock(&pag->pag_buf_lock);
  1261. delay(100);
  1262. spin_lock(&pag->pag_buf_lock);
  1263. }
  1264. spin_unlock(&pag->pag_buf_lock);
  1265. xfs_perag_put(pag);
  1266. }
  1267. }
  1268. /*
  1269. * buftarg list for delwrite queue processing
  1270. */
  1271. static LIST_HEAD(xfs_buftarg_list);
  1272. static DEFINE_SPINLOCK(xfs_buftarg_lock);
  1273. STATIC void
  1274. xfs_register_buftarg(
  1275. xfs_buftarg_t *btp)
  1276. {
  1277. spin_lock(&xfs_buftarg_lock);
  1278. list_add(&btp->bt_list, &xfs_buftarg_list);
  1279. spin_unlock(&xfs_buftarg_lock);
  1280. }
  1281. STATIC void
  1282. xfs_unregister_buftarg(
  1283. xfs_buftarg_t *btp)
  1284. {
  1285. spin_lock(&xfs_buftarg_lock);
  1286. list_del(&btp->bt_list);
  1287. spin_unlock(&xfs_buftarg_lock);
  1288. }
  1289. void
  1290. xfs_free_buftarg(
  1291. struct xfs_mount *mp,
  1292. struct xfs_buftarg *btp)
  1293. {
  1294. xfs_flush_buftarg(btp, 1);
  1295. if (mp->m_flags & XFS_MOUNT_BARRIER)
  1296. xfs_blkdev_issue_flush(btp);
  1297. iput(btp->bt_mapping->host);
  1298. /* Unregister the buftarg first so that we don't get a
  1299. * wakeup finding a non-existent task
  1300. */
  1301. xfs_unregister_buftarg(btp);
  1302. kthread_stop(btp->bt_task);
  1303. kmem_free(btp);
  1304. }
  1305. STATIC int
  1306. xfs_setsize_buftarg_flags(
  1307. xfs_buftarg_t *btp,
  1308. unsigned int blocksize,
  1309. unsigned int sectorsize,
  1310. int verbose)
  1311. {
  1312. btp->bt_bsize = blocksize;
  1313. btp->bt_sshift = ffs(sectorsize) - 1;
  1314. btp->bt_smask = sectorsize - 1;
  1315. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1316. printk(KERN_WARNING
  1317. "XFS: Cannot set_blocksize to %u on device %s\n",
  1318. sectorsize, XFS_BUFTARG_NAME(btp));
  1319. return EINVAL;
  1320. }
  1321. if (verbose &&
  1322. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1323. printk(KERN_WARNING
  1324. "XFS: %u byte sectors in use on device %s. "
  1325. "This is suboptimal; %u or greater is ideal.\n",
  1326. sectorsize, XFS_BUFTARG_NAME(btp),
  1327. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1328. }
  1329. return 0;
  1330. }
  1331. /*
  1332. * When allocating the initial buffer target we have not yet
  1333. * read in the superblock, so don't know what sized sectors
  1334. * are being used is at this early stage. Play safe.
  1335. */
  1336. STATIC int
  1337. xfs_setsize_buftarg_early(
  1338. xfs_buftarg_t *btp,
  1339. struct block_device *bdev)
  1340. {
  1341. return xfs_setsize_buftarg_flags(btp,
  1342. PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
  1343. }
  1344. int
  1345. xfs_setsize_buftarg(
  1346. xfs_buftarg_t *btp,
  1347. unsigned int blocksize,
  1348. unsigned int sectorsize)
  1349. {
  1350. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1351. }
  1352. STATIC int
  1353. xfs_mapping_buftarg(
  1354. xfs_buftarg_t *btp,
  1355. struct block_device *bdev)
  1356. {
  1357. struct backing_dev_info *bdi;
  1358. struct inode *inode;
  1359. struct address_space *mapping;
  1360. static const struct address_space_operations mapping_aops = {
  1361. .sync_page = block_sync_page,
  1362. .migratepage = fail_migrate_page,
  1363. };
  1364. inode = new_inode(bdev->bd_inode->i_sb);
  1365. if (!inode) {
  1366. printk(KERN_WARNING
  1367. "XFS: Cannot allocate mapping inode for device %s\n",
  1368. XFS_BUFTARG_NAME(btp));
  1369. return ENOMEM;
  1370. }
  1371. inode->i_ino = get_next_ino();
  1372. inode->i_mode = S_IFBLK;
  1373. inode->i_bdev = bdev;
  1374. inode->i_rdev = bdev->bd_dev;
  1375. bdi = blk_get_backing_dev_info(bdev);
  1376. if (!bdi)
  1377. bdi = &default_backing_dev_info;
  1378. mapping = &inode->i_data;
  1379. mapping->a_ops = &mapping_aops;
  1380. mapping->backing_dev_info = bdi;
  1381. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1382. btp->bt_mapping = mapping;
  1383. return 0;
  1384. }
  1385. STATIC int
  1386. xfs_alloc_delwrite_queue(
  1387. xfs_buftarg_t *btp,
  1388. const char *fsname)
  1389. {
  1390. int error = 0;
  1391. INIT_LIST_HEAD(&btp->bt_list);
  1392. INIT_LIST_HEAD(&btp->bt_delwrite_queue);
  1393. spin_lock_init(&btp->bt_delwrite_lock);
  1394. btp->bt_flags = 0;
  1395. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
  1396. if (IS_ERR(btp->bt_task)) {
  1397. error = PTR_ERR(btp->bt_task);
  1398. goto out_error;
  1399. }
  1400. xfs_register_buftarg(btp);
  1401. out_error:
  1402. return error;
  1403. }
  1404. xfs_buftarg_t *
  1405. xfs_alloc_buftarg(
  1406. struct xfs_mount *mp,
  1407. struct block_device *bdev,
  1408. int external,
  1409. const char *fsname)
  1410. {
  1411. xfs_buftarg_t *btp;
  1412. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1413. btp->bt_mount = mp;
  1414. btp->bt_dev = bdev->bd_dev;
  1415. btp->bt_bdev = bdev;
  1416. if (xfs_setsize_buftarg_early(btp, bdev))
  1417. goto error;
  1418. if (xfs_mapping_buftarg(btp, bdev))
  1419. goto error;
  1420. if (xfs_alloc_delwrite_queue(btp, fsname))
  1421. goto error;
  1422. return btp;
  1423. error:
  1424. kmem_free(btp);
  1425. return NULL;
  1426. }
  1427. /*
  1428. * Delayed write buffer handling
  1429. */
  1430. STATIC void
  1431. xfs_buf_delwri_queue(
  1432. xfs_buf_t *bp,
  1433. int unlock)
  1434. {
  1435. struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
  1436. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1437. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1438. ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
  1439. spin_lock(dwlk);
  1440. /* If already in the queue, dequeue and place at tail */
  1441. if (!list_empty(&bp->b_list)) {
  1442. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1443. if (unlock)
  1444. atomic_dec(&bp->b_hold);
  1445. list_del(&bp->b_list);
  1446. }
  1447. if (list_empty(dwq)) {
  1448. /* start xfsbufd as it is about to have something to do */
  1449. wake_up_process(bp->b_target->bt_task);
  1450. }
  1451. bp->b_flags |= _XBF_DELWRI_Q;
  1452. list_add_tail(&bp->b_list, dwq);
  1453. bp->b_queuetime = jiffies;
  1454. spin_unlock(dwlk);
  1455. if (unlock)
  1456. xfs_buf_unlock(bp);
  1457. }
  1458. void
  1459. xfs_buf_delwri_dequeue(
  1460. xfs_buf_t *bp)
  1461. {
  1462. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1463. int dequeued = 0;
  1464. spin_lock(dwlk);
  1465. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1466. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1467. list_del_init(&bp->b_list);
  1468. dequeued = 1;
  1469. }
  1470. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1471. spin_unlock(dwlk);
  1472. if (dequeued)
  1473. xfs_buf_rele(bp);
  1474. trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
  1475. }
  1476. /*
  1477. * If a delwri buffer needs to be pushed before it has aged out, then promote
  1478. * it to the head of the delwri queue so that it will be flushed on the next
  1479. * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
  1480. * than the age currently needed to flush the buffer. Hence the next time the
  1481. * xfsbufd sees it is guaranteed to be considered old enough to flush.
  1482. */
  1483. void
  1484. xfs_buf_delwri_promote(
  1485. struct xfs_buf *bp)
  1486. {
  1487. struct xfs_buftarg *btp = bp->b_target;
  1488. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
  1489. ASSERT(bp->b_flags & XBF_DELWRI);
  1490. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1491. /*
  1492. * Check the buffer age before locking the delayed write queue as we
  1493. * don't need to promote buffers that are already past the flush age.
  1494. */
  1495. if (bp->b_queuetime < jiffies - age)
  1496. return;
  1497. bp->b_queuetime = jiffies - age;
  1498. spin_lock(&btp->bt_delwrite_lock);
  1499. list_move(&bp->b_list, &btp->bt_delwrite_queue);
  1500. spin_unlock(&btp->bt_delwrite_lock);
  1501. }
  1502. STATIC void
  1503. xfs_buf_runall_queues(
  1504. struct workqueue_struct *queue)
  1505. {
  1506. flush_workqueue(queue);
  1507. }
  1508. STATIC int
  1509. xfsbufd_wakeup(
  1510. struct shrinker *shrink,
  1511. int priority,
  1512. gfp_t mask)
  1513. {
  1514. xfs_buftarg_t *btp;
  1515. spin_lock(&xfs_buftarg_lock);
  1516. list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
  1517. if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
  1518. continue;
  1519. if (list_empty(&btp->bt_delwrite_queue))
  1520. continue;
  1521. set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
  1522. wake_up_process(btp->bt_task);
  1523. }
  1524. spin_unlock(&xfs_buftarg_lock);
  1525. return 0;
  1526. }
  1527. /*
  1528. * Move as many buffers as specified to the supplied list
  1529. * idicating if we skipped any buffers to prevent deadlocks.
  1530. */
  1531. STATIC int
  1532. xfs_buf_delwri_split(
  1533. xfs_buftarg_t *target,
  1534. struct list_head *list,
  1535. unsigned long age)
  1536. {
  1537. xfs_buf_t *bp, *n;
  1538. struct list_head *dwq = &target->bt_delwrite_queue;
  1539. spinlock_t *dwlk = &target->bt_delwrite_lock;
  1540. int skipped = 0;
  1541. int force;
  1542. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1543. INIT_LIST_HEAD(list);
  1544. spin_lock(dwlk);
  1545. list_for_each_entry_safe(bp, n, dwq, b_list) {
  1546. ASSERT(bp->b_flags & XBF_DELWRI);
  1547. if (!XFS_BUF_ISPINNED(bp) && !xfs_buf_cond_lock(bp)) {
  1548. if (!force &&
  1549. time_before(jiffies, bp->b_queuetime + age)) {
  1550. xfs_buf_unlock(bp);
  1551. break;
  1552. }
  1553. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
  1554. _XBF_RUN_QUEUES);
  1555. bp->b_flags |= XBF_WRITE;
  1556. list_move_tail(&bp->b_list, list);
  1557. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1558. } else
  1559. skipped++;
  1560. }
  1561. spin_unlock(dwlk);
  1562. return skipped;
  1563. }
  1564. /*
  1565. * Compare function is more complex than it needs to be because
  1566. * the return value is only 32 bits and we are doing comparisons
  1567. * on 64 bit values
  1568. */
  1569. static int
  1570. xfs_buf_cmp(
  1571. void *priv,
  1572. struct list_head *a,
  1573. struct list_head *b)
  1574. {
  1575. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1576. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1577. xfs_daddr_t diff;
  1578. diff = ap->b_bn - bp->b_bn;
  1579. if (diff < 0)
  1580. return -1;
  1581. if (diff > 0)
  1582. return 1;
  1583. return 0;
  1584. }
  1585. void
  1586. xfs_buf_delwri_sort(
  1587. xfs_buftarg_t *target,
  1588. struct list_head *list)
  1589. {
  1590. list_sort(NULL, list, xfs_buf_cmp);
  1591. }
  1592. STATIC int
  1593. xfsbufd(
  1594. void *data)
  1595. {
  1596. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1597. current->flags |= PF_MEMALLOC;
  1598. set_freezable();
  1599. do {
  1600. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
  1601. long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
  1602. int count = 0;
  1603. struct list_head tmp;
  1604. if (unlikely(freezing(current))) {
  1605. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1606. refrigerator();
  1607. } else {
  1608. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1609. }
  1610. /* sleep for a long time if there is nothing to do. */
  1611. if (list_empty(&target->bt_delwrite_queue))
  1612. tout = MAX_SCHEDULE_TIMEOUT;
  1613. schedule_timeout_interruptible(tout);
  1614. xfs_buf_delwri_split(target, &tmp, age);
  1615. list_sort(NULL, &tmp, xfs_buf_cmp);
  1616. while (!list_empty(&tmp)) {
  1617. struct xfs_buf *bp;
  1618. bp = list_first_entry(&tmp, struct xfs_buf, b_list);
  1619. list_del_init(&bp->b_list);
  1620. xfs_bdstrat_cb(bp);
  1621. count++;
  1622. }
  1623. if (count)
  1624. blk_run_address_space(target->bt_mapping);
  1625. } while (!kthread_should_stop());
  1626. return 0;
  1627. }
  1628. /*
  1629. * Go through all incore buffers, and release buffers if they belong to
  1630. * the given device. This is used in filesystem error handling to
  1631. * preserve the consistency of its metadata.
  1632. */
  1633. int
  1634. xfs_flush_buftarg(
  1635. xfs_buftarg_t *target,
  1636. int wait)
  1637. {
  1638. xfs_buf_t *bp;
  1639. int pincount = 0;
  1640. LIST_HEAD(tmp_list);
  1641. LIST_HEAD(wait_list);
  1642. xfs_buf_runall_queues(xfsconvertd_workqueue);
  1643. xfs_buf_runall_queues(xfsdatad_workqueue);
  1644. xfs_buf_runall_queues(xfslogd_workqueue);
  1645. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1646. pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
  1647. /*
  1648. * Dropped the delayed write list lock, now walk the temporary list.
  1649. * All I/O is issued async and then if we need to wait for completion
  1650. * we do that after issuing all the IO.
  1651. */
  1652. list_sort(NULL, &tmp_list, xfs_buf_cmp);
  1653. while (!list_empty(&tmp_list)) {
  1654. bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
  1655. ASSERT(target == bp->b_target);
  1656. list_del_init(&bp->b_list);
  1657. if (wait) {
  1658. bp->b_flags &= ~XBF_ASYNC;
  1659. list_add(&bp->b_list, &wait_list);
  1660. }
  1661. xfs_bdstrat_cb(bp);
  1662. }
  1663. if (wait) {
  1664. /* Expedite and wait for IO to complete. */
  1665. blk_run_address_space(target->bt_mapping);
  1666. while (!list_empty(&wait_list)) {
  1667. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1668. list_del_init(&bp->b_list);
  1669. xfs_buf_iowait(bp);
  1670. xfs_buf_relse(bp);
  1671. }
  1672. }
  1673. return pincount;
  1674. }
  1675. int __init
  1676. xfs_buf_init(void)
  1677. {
  1678. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1679. KM_ZONE_HWALIGN, NULL);
  1680. if (!xfs_buf_zone)
  1681. goto out;
  1682. xfslogd_workqueue = alloc_workqueue("xfslogd",
  1683. WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
  1684. if (!xfslogd_workqueue)
  1685. goto out_free_buf_zone;
  1686. xfsdatad_workqueue = create_workqueue("xfsdatad");
  1687. if (!xfsdatad_workqueue)
  1688. goto out_destroy_xfslogd_workqueue;
  1689. xfsconvertd_workqueue = create_workqueue("xfsconvertd");
  1690. if (!xfsconvertd_workqueue)
  1691. goto out_destroy_xfsdatad_workqueue;
  1692. register_shrinker(&xfs_buf_shake);
  1693. return 0;
  1694. out_destroy_xfsdatad_workqueue:
  1695. destroy_workqueue(xfsdatad_workqueue);
  1696. out_destroy_xfslogd_workqueue:
  1697. destroy_workqueue(xfslogd_workqueue);
  1698. out_free_buf_zone:
  1699. kmem_zone_destroy(xfs_buf_zone);
  1700. out:
  1701. return -ENOMEM;
  1702. }
  1703. void
  1704. xfs_buf_terminate(void)
  1705. {
  1706. unregister_shrinker(&xfs_buf_shake);
  1707. destroy_workqueue(xfsconvertd_workqueue);
  1708. destroy_workqueue(xfsdatad_workqueue);
  1709. destroy_workqueue(xfslogd_workqueue);
  1710. kmem_zone_destroy(xfs_buf_zone);
  1711. }
  1712. #ifdef CONFIG_KDB_MODULES
  1713. struct list_head *
  1714. xfs_get_buftarg_list(void)
  1715. {
  1716. return &xfs_buftarg_list;
  1717. }
  1718. #endif