xfs_buf.c 45 KB

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