xfs_buf.c 42 KB

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