xfs_buf.c 40 KB

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