xfs_buf.c 45 KB

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