xfs_buf.c 41 KB

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