xfs_buf.c 47 KB

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