xfs_buf.c 42 KB

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