xfs_buf.c 45 KB

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