xfs_buf.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  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. XFS_STATS_INC(xb_page_found);
  366. nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
  367. size -= nbytes;
  368. ASSERT(!PagePrivate(page));
  369. if (!PageUptodate(page)) {
  370. page_count--;
  371. if (blocksize >= PAGE_CACHE_SIZE) {
  372. if (flags & XBF_READ)
  373. bp->b_locked = 1;
  374. } else if (!PagePrivate(page)) {
  375. if (test_page_region(page, offset, nbytes))
  376. page_count++;
  377. }
  378. }
  379. bp->b_pages[i] = page;
  380. offset = 0;
  381. }
  382. if (!bp->b_locked) {
  383. for (i = 0; i < bp->b_page_count; i++)
  384. unlock_page(bp->b_pages[i]);
  385. }
  386. if (page_count == bp->b_page_count)
  387. bp->b_flags |= XBF_DONE;
  388. XB_TRACE(bp, "lookup_pages", (long)page_count);
  389. return error;
  390. }
  391. /*
  392. * Map buffer into kernel address-space if nessecary.
  393. */
  394. STATIC int
  395. _xfs_buf_map_pages(
  396. xfs_buf_t *bp,
  397. uint flags)
  398. {
  399. /* A single page buffer is always mappable */
  400. if (bp->b_page_count == 1) {
  401. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  402. bp->b_flags |= XBF_MAPPED;
  403. } else if (flags & XBF_MAPPED) {
  404. if (as_list_len > 64)
  405. purge_addresses();
  406. bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
  407. VM_MAP, PAGE_KERNEL);
  408. if (unlikely(bp->b_addr == NULL))
  409. return -ENOMEM;
  410. bp->b_addr += bp->b_offset;
  411. bp->b_flags |= XBF_MAPPED;
  412. }
  413. return 0;
  414. }
  415. /*
  416. * Finding and Reading Buffers
  417. */
  418. /*
  419. * Look up, and creates if absent, a lockable buffer for
  420. * a given range of an inode. The buffer is returned
  421. * locked. If other overlapping buffers exist, they are
  422. * released before the new buffer is created and locked,
  423. * which may imply that this call will block until those buffers
  424. * are unlocked. No I/O is implied by this call.
  425. */
  426. xfs_buf_t *
  427. _xfs_buf_find(
  428. xfs_buftarg_t *btp, /* block device target */
  429. xfs_off_t ioff, /* starting offset of range */
  430. size_t isize, /* length of range */
  431. xfs_buf_flags_t flags,
  432. xfs_buf_t *new_bp)
  433. {
  434. xfs_off_t range_base;
  435. size_t range_length;
  436. xfs_bufhash_t *hash;
  437. xfs_buf_t *bp, *n;
  438. range_base = (ioff << BBSHIFT);
  439. range_length = (isize << BBSHIFT);
  440. /* Check for IOs smaller than the sector size / not sector aligned */
  441. ASSERT(!(range_length < (1 << btp->bt_sshift)));
  442. ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
  443. hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
  444. spin_lock(&hash->bh_lock);
  445. list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
  446. ASSERT(btp == bp->b_target);
  447. if (bp->b_file_offset == range_base &&
  448. bp->b_buffer_length == range_length) {
  449. /*
  450. * If we look at something, bring it to the
  451. * front of the list for next time.
  452. */
  453. atomic_inc(&bp->b_hold);
  454. list_move(&bp->b_hash_list, &hash->bh_list);
  455. goto found;
  456. }
  457. }
  458. /* No match found */
  459. if (new_bp) {
  460. _xfs_buf_initialize(new_bp, btp, range_base,
  461. range_length, flags);
  462. new_bp->b_hash = hash;
  463. list_add(&new_bp->b_hash_list, &hash->bh_list);
  464. } else {
  465. XFS_STATS_INC(xb_miss_locked);
  466. }
  467. spin_unlock(&hash->bh_lock);
  468. return new_bp;
  469. found:
  470. spin_unlock(&hash->bh_lock);
  471. /* Attempt to get the semaphore without sleeping,
  472. * if this does not work then we need to drop the
  473. * spinlock and do a hard attempt on the semaphore.
  474. */
  475. if (down_trylock(&bp->b_sema)) {
  476. if (!(flags & XBF_TRYLOCK)) {
  477. /* wait for buffer ownership */
  478. XB_TRACE(bp, "get_lock", 0);
  479. xfs_buf_lock(bp);
  480. XFS_STATS_INC(xb_get_locked_waited);
  481. } else {
  482. /* We asked for a trylock and failed, no need
  483. * to look at file offset and length here, we
  484. * know that this buffer at least overlaps our
  485. * buffer and is locked, therefore our buffer
  486. * either does not exist, or is this buffer.
  487. */
  488. xfs_buf_rele(bp);
  489. XFS_STATS_INC(xb_busy_locked);
  490. return NULL;
  491. }
  492. } else {
  493. /* trylock worked */
  494. XB_SET_OWNER(bp);
  495. }
  496. if (bp->b_flags & XBF_STALE) {
  497. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  498. bp->b_flags &= XBF_MAPPED;
  499. }
  500. XB_TRACE(bp, "got_lock", 0);
  501. XFS_STATS_INC(xb_get_locked);
  502. return bp;
  503. }
  504. /*
  505. * Assembles a buffer covering the specified range.
  506. * Storage in memory for all portions of the buffer will be allocated,
  507. * although backing storage may not be.
  508. */
  509. xfs_buf_t *
  510. xfs_buf_get_flags(
  511. xfs_buftarg_t *target,/* target for buffer */
  512. xfs_off_t ioff, /* starting offset of range */
  513. size_t isize, /* length of range */
  514. xfs_buf_flags_t flags)
  515. {
  516. xfs_buf_t *bp, *new_bp;
  517. int error = 0, i;
  518. new_bp = xfs_buf_allocate(flags);
  519. if (unlikely(!new_bp))
  520. return NULL;
  521. bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
  522. if (bp == new_bp) {
  523. error = _xfs_buf_lookup_pages(bp, flags);
  524. if (error)
  525. goto no_buffer;
  526. } else {
  527. xfs_buf_deallocate(new_bp);
  528. if (unlikely(bp == NULL))
  529. return NULL;
  530. }
  531. for (i = 0; i < bp->b_page_count; i++)
  532. mark_page_accessed(bp->b_pages[i]);
  533. if (!(bp->b_flags & XBF_MAPPED)) {
  534. error = _xfs_buf_map_pages(bp, flags);
  535. if (unlikely(error)) {
  536. printk(KERN_WARNING "%s: failed to map pages\n",
  537. __FUNCTION__);
  538. goto no_buffer;
  539. }
  540. }
  541. XFS_STATS_INC(xb_get);
  542. /*
  543. * Always fill in the block number now, the mapped cases can do
  544. * their own overlay of this later.
  545. */
  546. bp->b_bn = ioff;
  547. bp->b_count_desired = bp->b_buffer_length;
  548. XB_TRACE(bp, "get", (unsigned long)flags);
  549. return bp;
  550. no_buffer:
  551. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  552. xfs_buf_unlock(bp);
  553. xfs_buf_rele(bp);
  554. return NULL;
  555. }
  556. xfs_buf_t *
  557. xfs_buf_read_flags(
  558. xfs_buftarg_t *target,
  559. xfs_off_t ioff,
  560. size_t isize,
  561. xfs_buf_flags_t flags)
  562. {
  563. xfs_buf_t *bp;
  564. flags |= XBF_READ;
  565. bp = xfs_buf_get_flags(target, ioff, isize, flags);
  566. if (bp) {
  567. if (!XFS_BUF_ISDONE(bp)) {
  568. XB_TRACE(bp, "read", (unsigned long)flags);
  569. XFS_STATS_INC(xb_get_read);
  570. xfs_buf_iostart(bp, flags);
  571. } else if (flags & XBF_ASYNC) {
  572. XB_TRACE(bp, "read_async", (unsigned long)flags);
  573. /*
  574. * Read ahead call which is already satisfied,
  575. * drop the buffer
  576. */
  577. goto no_buffer;
  578. } else {
  579. XB_TRACE(bp, "read_done", (unsigned long)flags);
  580. /* We do not want read in the flags */
  581. bp->b_flags &= ~XBF_READ;
  582. }
  583. }
  584. return bp;
  585. no_buffer:
  586. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  587. xfs_buf_unlock(bp);
  588. xfs_buf_rele(bp);
  589. return NULL;
  590. }
  591. /*
  592. * If we are not low on memory then do the readahead in a deadlock
  593. * safe manner.
  594. */
  595. void
  596. xfs_buf_readahead(
  597. xfs_buftarg_t *target,
  598. xfs_off_t ioff,
  599. size_t isize,
  600. xfs_buf_flags_t flags)
  601. {
  602. struct backing_dev_info *bdi;
  603. bdi = target->bt_mapping->backing_dev_info;
  604. if (bdi_read_congested(bdi))
  605. return;
  606. flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
  607. xfs_buf_read_flags(target, ioff, isize, flags);
  608. }
  609. xfs_buf_t *
  610. xfs_buf_get_empty(
  611. size_t len,
  612. xfs_buftarg_t *target)
  613. {
  614. xfs_buf_t *bp;
  615. bp = xfs_buf_allocate(0);
  616. if (bp)
  617. _xfs_buf_initialize(bp, target, 0, len, 0);
  618. return bp;
  619. }
  620. static inline struct page *
  621. mem_to_page(
  622. void *addr)
  623. {
  624. if (((unsigned long)addr < VMALLOC_START) ||
  625. ((unsigned long)addr >= VMALLOC_END)) {
  626. return virt_to_page(addr);
  627. } else {
  628. return vmalloc_to_page(addr);
  629. }
  630. }
  631. int
  632. xfs_buf_associate_memory(
  633. xfs_buf_t *bp,
  634. void *mem,
  635. size_t len)
  636. {
  637. int rval;
  638. int i = 0;
  639. size_t ptr;
  640. size_t end, end_cur;
  641. off_t offset;
  642. int page_count;
  643. page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
  644. offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
  645. if (offset && (len > PAGE_CACHE_SIZE))
  646. page_count++;
  647. /* Free any previous set of page pointers */
  648. if (bp->b_pages)
  649. _xfs_buf_free_pages(bp);
  650. bp->b_pages = NULL;
  651. bp->b_addr = mem;
  652. rval = _xfs_buf_get_pages(bp, page_count, 0);
  653. if (rval)
  654. return rval;
  655. bp->b_offset = offset;
  656. ptr = (size_t) mem & PAGE_CACHE_MASK;
  657. end = PAGE_CACHE_ALIGN((size_t) mem + len);
  658. end_cur = end;
  659. /* set up first page */
  660. bp->b_pages[0] = mem_to_page(mem);
  661. ptr += PAGE_CACHE_SIZE;
  662. bp->b_page_count = ++i;
  663. while (ptr < end) {
  664. bp->b_pages[i] = mem_to_page((void *)ptr);
  665. bp->b_page_count = ++i;
  666. ptr += PAGE_CACHE_SIZE;
  667. }
  668. bp->b_locked = 0;
  669. bp->b_count_desired = bp->b_buffer_length = len;
  670. bp->b_flags |= XBF_MAPPED;
  671. return 0;
  672. }
  673. xfs_buf_t *
  674. xfs_buf_get_noaddr(
  675. size_t len,
  676. xfs_buftarg_t *target)
  677. {
  678. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  679. int error, i;
  680. xfs_buf_t *bp;
  681. bp = xfs_buf_allocate(0);
  682. if (unlikely(bp == NULL))
  683. goto fail;
  684. _xfs_buf_initialize(bp, target, 0, len, 0);
  685. error = _xfs_buf_get_pages(bp, page_count, 0);
  686. if (error)
  687. goto fail_free_buf;
  688. for (i = 0; i < page_count; i++) {
  689. bp->b_pages[i] = alloc_page(GFP_KERNEL);
  690. if (!bp->b_pages[i])
  691. goto fail_free_mem;
  692. }
  693. bp->b_flags |= _XBF_PAGES;
  694. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  695. if (unlikely(error)) {
  696. printk(KERN_WARNING "%s: failed to map pages\n",
  697. __FUNCTION__);
  698. goto fail_free_mem;
  699. }
  700. xfs_buf_unlock(bp);
  701. XB_TRACE(bp, "no_daddr", len);
  702. return bp;
  703. fail_free_mem:
  704. while (--i >= 0)
  705. __free_page(bp->b_pages[i]);
  706. _xfs_buf_free_pages(bp);
  707. fail_free_buf:
  708. xfs_buf_deallocate(bp);
  709. fail:
  710. return NULL;
  711. }
  712. /*
  713. * Increment reference count on buffer, to hold the buffer concurrently
  714. * with another thread which may release (free) the buffer asynchronously.
  715. * Must hold the buffer already to call this function.
  716. */
  717. void
  718. xfs_buf_hold(
  719. xfs_buf_t *bp)
  720. {
  721. atomic_inc(&bp->b_hold);
  722. XB_TRACE(bp, "hold", 0);
  723. }
  724. /*
  725. * Releases a hold on the specified buffer. If the
  726. * the hold count is 1, calls xfs_buf_free.
  727. */
  728. void
  729. xfs_buf_rele(
  730. xfs_buf_t *bp)
  731. {
  732. xfs_bufhash_t *hash = bp->b_hash;
  733. XB_TRACE(bp, "rele", bp->b_relse);
  734. if (unlikely(!hash)) {
  735. ASSERT(!bp->b_relse);
  736. if (atomic_dec_and_test(&bp->b_hold))
  737. xfs_buf_free(bp);
  738. return;
  739. }
  740. if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
  741. if (bp->b_relse) {
  742. atomic_inc(&bp->b_hold);
  743. spin_unlock(&hash->bh_lock);
  744. (*(bp->b_relse)) (bp);
  745. } else if (bp->b_flags & XBF_FS_MANAGED) {
  746. spin_unlock(&hash->bh_lock);
  747. } else {
  748. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  749. list_del_init(&bp->b_hash_list);
  750. spin_unlock(&hash->bh_lock);
  751. xfs_buf_free(bp);
  752. }
  753. } else {
  754. /*
  755. * Catch reference count leaks
  756. */
  757. ASSERT(atomic_read(&bp->b_hold) >= 0);
  758. }
  759. }
  760. /*
  761. * Mutual exclusion on buffers. Locking model:
  762. *
  763. * Buffers associated with inodes for which buffer locking
  764. * is not enabled are not protected by semaphores, and are
  765. * assumed to be exclusively owned by the caller. There is a
  766. * spinlock in the buffer, used by the caller when concurrent
  767. * access is possible.
  768. */
  769. /*
  770. * Locks a buffer object, if it is not already locked.
  771. * Note that this in no way locks the underlying pages, so it is only
  772. * useful for synchronizing concurrent use of buffer objects, not for
  773. * synchronizing independent access to the underlying pages.
  774. */
  775. int
  776. xfs_buf_cond_lock(
  777. xfs_buf_t *bp)
  778. {
  779. int locked;
  780. locked = down_trylock(&bp->b_sema) == 0;
  781. if (locked) {
  782. XB_SET_OWNER(bp);
  783. }
  784. XB_TRACE(bp, "cond_lock", (long)locked);
  785. return locked ? 0 : -EBUSY;
  786. }
  787. #if defined(DEBUG) || defined(XFS_BLI_TRACE)
  788. int
  789. xfs_buf_lock_value(
  790. xfs_buf_t *bp)
  791. {
  792. return atomic_read(&bp->b_sema.count);
  793. }
  794. #endif
  795. /*
  796. * Locks a buffer object.
  797. * Note that this in no way locks the underlying pages, so it is only
  798. * useful for synchronizing concurrent use of buffer objects, not for
  799. * synchronizing independent access to the underlying pages.
  800. */
  801. void
  802. xfs_buf_lock(
  803. xfs_buf_t *bp)
  804. {
  805. XB_TRACE(bp, "lock", 0);
  806. if (atomic_read(&bp->b_io_remaining))
  807. blk_run_address_space(bp->b_target->bt_mapping);
  808. down(&bp->b_sema);
  809. XB_SET_OWNER(bp);
  810. XB_TRACE(bp, "locked", 0);
  811. }
  812. /*
  813. * Releases the lock on the buffer object.
  814. * If the buffer is marked delwri but is not queued, do so before we
  815. * unlock the buffer as we need to set flags correctly. We also need to
  816. * take a reference for the delwri queue because the unlocker is going to
  817. * drop their's and they don't know we just queued it.
  818. */
  819. void
  820. xfs_buf_unlock(
  821. xfs_buf_t *bp)
  822. {
  823. if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
  824. atomic_inc(&bp->b_hold);
  825. bp->b_flags |= XBF_ASYNC;
  826. xfs_buf_delwri_queue(bp, 0);
  827. }
  828. XB_CLEAR_OWNER(bp);
  829. up(&bp->b_sema);
  830. XB_TRACE(bp, "unlock", 0);
  831. }
  832. /*
  833. * Pinning Buffer Storage in Memory
  834. * Ensure that no attempt to force a buffer to disk will succeed.
  835. */
  836. void
  837. xfs_buf_pin(
  838. xfs_buf_t *bp)
  839. {
  840. atomic_inc(&bp->b_pin_count);
  841. XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
  842. }
  843. void
  844. xfs_buf_unpin(
  845. xfs_buf_t *bp)
  846. {
  847. if (atomic_dec_and_test(&bp->b_pin_count))
  848. wake_up_all(&bp->b_waiters);
  849. XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
  850. }
  851. int
  852. xfs_buf_ispin(
  853. xfs_buf_t *bp)
  854. {
  855. return atomic_read(&bp->b_pin_count);
  856. }
  857. STATIC void
  858. xfs_buf_wait_unpin(
  859. xfs_buf_t *bp)
  860. {
  861. DECLARE_WAITQUEUE (wait, current);
  862. if (atomic_read(&bp->b_pin_count) == 0)
  863. return;
  864. add_wait_queue(&bp->b_waiters, &wait);
  865. for (;;) {
  866. set_current_state(TASK_UNINTERRUPTIBLE);
  867. if (atomic_read(&bp->b_pin_count) == 0)
  868. break;
  869. if (atomic_read(&bp->b_io_remaining))
  870. blk_run_address_space(bp->b_target->bt_mapping);
  871. schedule();
  872. }
  873. remove_wait_queue(&bp->b_waiters, &wait);
  874. set_current_state(TASK_RUNNING);
  875. }
  876. /*
  877. * Buffer Utility Routines
  878. */
  879. STATIC void
  880. xfs_buf_iodone_work(
  881. struct work_struct *work)
  882. {
  883. xfs_buf_t *bp =
  884. container_of(work, xfs_buf_t, b_iodone_work);
  885. /*
  886. * We can get an EOPNOTSUPP to ordered writes. Here we clear the
  887. * ordered flag and reissue them. Because we can't tell the higher
  888. * layers directly that they should not issue ordered I/O anymore, they
  889. * need to check if the ordered flag was cleared during I/O completion.
  890. */
  891. if ((bp->b_error == EOPNOTSUPP) &&
  892. (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
  893. XB_TRACE(bp, "ordered_retry", bp->b_iodone);
  894. bp->b_flags &= ~XBF_ORDERED;
  895. xfs_buf_iorequest(bp);
  896. } else if (bp->b_iodone)
  897. (*(bp->b_iodone))(bp);
  898. else if (bp->b_flags & XBF_ASYNC)
  899. xfs_buf_relse(bp);
  900. }
  901. void
  902. xfs_buf_ioend(
  903. xfs_buf_t *bp,
  904. int schedule)
  905. {
  906. bp->b_flags &= ~(XBF_READ | XBF_WRITE);
  907. if (bp->b_error == 0)
  908. bp->b_flags |= XBF_DONE;
  909. XB_TRACE(bp, "iodone", bp->b_iodone);
  910. if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
  911. if (schedule) {
  912. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  913. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  914. } else {
  915. xfs_buf_iodone_work(&bp->b_iodone_work);
  916. }
  917. } else {
  918. up(&bp->b_iodonesema);
  919. }
  920. }
  921. void
  922. xfs_buf_ioerror(
  923. xfs_buf_t *bp,
  924. int error)
  925. {
  926. ASSERT(error >= 0 && error <= 0xffff);
  927. bp->b_error = (unsigned short)error;
  928. XB_TRACE(bp, "ioerror", (unsigned long)error);
  929. }
  930. /*
  931. * Initiate I/O on a buffer, based on the flags supplied.
  932. * The b_iodone routine in the buffer supplied will only be called
  933. * when all of the subsidiary I/O requests, if any, have been completed.
  934. */
  935. int
  936. xfs_buf_iostart(
  937. xfs_buf_t *bp,
  938. xfs_buf_flags_t flags)
  939. {
  940. int status = 0;
  941. XB_TRACE(bp, "iostart", (unsigned long)flags);
  942. if (flags & XBF_DELWRI) {
  943. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC);
  944. bp->b_flags |= flags & (XBF_DELWRI | XBF_ASYNC);
  945. xfs_buf_delwri_queue(bp, 1);
  946. return status;
  947. }
  948. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
  949. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  950. bp->b_flags |= flags & (XBF_READ | XBF_WRITE | XBF_ASYNC | \
  951. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  952. BUG_ON(bp->b_bn == XFS_BUF_DADDR_NULL);
  953. /* For writes allow an alternate strategy routine to precede
  954. * the actual I/O request (which may not be issued at all in
  955. * a shutdown situation, for example).
  956. */
  957. status = (flags & XBF_WRITE) ?
  958. xfs_buf_iostrategy(bp) : xfs_buf_iorequest(bp);
  959. /* Wait for I/O if we are not an async request.
  960. * Note: async I/O request completion will release the buffer,
  961. * and that can already be done by this point. So using the
  962. * buffer pointer from here on, after async I/O, is invalid.
  963. */
  964. if (!status && !(flags & XBF_ASYNC))
  965. status = xfs_buf_iowait(bp);
  966. return status;
  967. }
  968. STATIC_INLINE int
  969. _xfs_buf_iolocked(
  970. xfs_buf_t *bp)
  971. {
  972. ASSERT(bp->b_flags & (XBF_READ | XBF_WRITE));
  973. if (bp->b_flags & XBF_READ)
  974. return bp->b_locked;
  975. return 0;
  976. }
  977. STATIC_INLINE void
  978. _xfs_buf_ioend(
  979. xfs_buf_t *bp,
  980. int schedule)
  981. {
  982. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  983. bp->b_locked = 0;
  984. xfs_buf_ioend(bp, schedule);
  985. }
  986. }
  987. STATIC void
  988. xfs_buf_bio_end_io(
  989. struct bio *bio,
  990. int error)
  991. {
  992. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  993. unsigned int blocksize = bp->b_target->bt_bsize;
  994. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  995. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  996. bp->b_error = EIO;
  997. do {
  998. struct page *page = bvec->bv_page;
  999. ASSERT(!PagePrivate(page));
  1000. if (unlikely(bp->b_error)) {
  1001. if (bp->b_flags & XBF_READ)
  1002. ClearPageUptodate(page);
  1003. } else if (blocksize >= PAGE_CACHE_SIZE) {
  1004. SetPageUptodate(page);
  1005. } else if (!PagePrivate(page) &&
  1006. (bp->b_flags & _XBF_PAGE_CACHE)) {
  1007. set_page_region(page, bvec->bv_offset, bvec->bv_len);
  1008. }
  1009. if (--bvec >= bio->bi_io_vec)
  1010. prefetchw(&bvec->bv_page->flags);
  1011. if (_xfs_buf_iolocked(bp)) {
  1012. unlock_page(page);
  1013. }
  1014. } while (bvec >= bio->bi_io_vec);
  1015. _xfs_buf_ioend(bp, 1);
  1016. bio_put(bio);
  1017. }
  1018. STATIC void
  1019. _xfs_buf_ioapply(
  1020. xfs_buf_t *bp)
  1021. {
  1022. int i, rw, map_i, total_nr_pages, nr_pages;
  1023. struct bio *bio;
  1024. int offset = bp->b_offset;
  1025. int size = bp->b_count_desired;
  1026. sector_t sector = bp->b_bn;
  1027. unsigned int blocksize = bp->b_target->bt_bsize;
  1028. int locking = _xfs_buf_iolocked(bp);
  1029. total_nr_pages = bp->b_page_count;
  1030. map_i = 0;
  1031. if (bp->b_flags & XBF_ORDERED) {
  1032. ASSERT(!(bp->b_flags & XBF_READ));
  1033. rw = WRITE_BARRIER;
  1034. } else if (bp->b_flags & _XBF_RUN_QUEUES) {
  1035. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1036. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1037. rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
  1038. } else {
  1039. rw = (bp->b_flags & XBF_WRITE) ? WRITE :
  1040. (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
  1041. }
  1042. /* Special code path for reading a sub page size buffer in --
  1043. * we populate up the whole page, and hence the other metadata
  1044. * in the same page. This optimization is only valid when the
  1045. * filesystem block size is not smaller than the page size.
  1046. */
  1047. if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
  1048. (bp->b_flags & XBF_READ) && locking &&
  1049. (blocksize >= PAGE_CACHE_SIZE)) {
  1050. bio = bio_alloc(GFP_NOIO, 1);
  1051. bio->bi_bdev = bp->b_target->bt_bdev;
  1052. bio->bi_sector = sector - (offset >> BBSHIFT);
  1053. bio->bi_end_io = xfs_buf_bio_end_io;
  1054. bio->bi_private = bp;
  1055. bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
  1056. size = 0;
  1057. atomic_inc(&bp->b_io_remaining);
  1058. goto submit_io;
  1059. }
  1060. /* Lock down the pages which we need to for the request */
  1061. if (locking && (bp->b_flags & XBF_WRITE) && (bp->b_locked == 0)) {
  1062. for (i = 0; size; i++) {
  1063. int nbytes = PAGE_CACHE_SIZE - offset;
  1064. struct page *page = bp->b_pages[i];
  1065. if (nbytes > size)
  1066. nbytes = size;
  1067. lock_page(page);
  1068. size -= nbytes;
  1069. offset = 0;
  1070. }
  1071. offset = bp->b_offset;
  1072. size = bp->b_count_desired;
  1073. }
  1074. next_chunk:
  1075. atomic_inc(&bp->b_io_remaining);
  1076. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1077. if (nr_pages > total_nr_pages)
  1078. nr_pages = total_nr_pages;
  1079. bio = bio_alloc(GFP_NOIO, nr_pages);
  1080. bio->bi_bdev = bp->b_target->bt_bdev;
  1081. bio->bi_sector = sector;
  1082. bio->bi_end_io = xfs_buf_bio_end_io;
  1083. bio->bi_private = bp;
  1084. for (; size && nr_pages; nr_pages--, map_i++) {
  1085. int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
  1086. if (nbytes > size)
  1087. nbytes = size;
  1088. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1089. if (rbytes < nbytes)
  1090. break;
  1091. offset = 0;
  1092. sector += nbytes >> BBSHIFT;
  1093. size -= nbytes;
  1094. total_nr_pages--;
  1095. }
  1096. submit_io:
  1097. if (likely(bio->bi_size)) {
  1098. submit_bio(rw, bio);
  1099. if (size)
  1100. goto next_chunk;
  1101. } else {
  1102. bio_put(bio);
  1103. xfs_buf_ioerror(bp, EIO);
  1104. }
  1105. }
  1106. int
  1107. xfs_buf_iorequest(
  1108. xfs_buf_t *bp)
  1109. {
  1110. XB_TRACE(bp, "iorequest", 0);
  1111. if (bp->b_flags & XBF_DELWRI) {
  1112. xfs_buf_delwri_queue(bp, 1);
  1113. return 0;
  1114. }
  1115. if (bp->b_flags & XBF_WRITE) {
  1116. xfs_buf_wait_unpin(bp);
  1117. }
  1118. xfs_buf_hold(bp);
  1119. /* Set the count to 1 initially, this will stop an I/O
  1120. * completion callout which happens before we have started
  1121. * all the I/O from calling xfs_buf_ioend too early.
  1122. */
  1123. atomic_set(&bp->b_io_remaining, 1);
  1124. _xfs_buf_ioapply(bp);
  1125. _xfs_buf_ioend(bp, 0);
  1126. xfs_buf_rele(bp);
  1127. return 0;
  1128. }
  1129. /*
  1130. * Waits for I/O to complete on the buffer supplied.
  1131. * It returns immediately if no I/O is pending.
  1132. * It returns the I/O error code, if any, or 0 if there was no error.
  1133. */
  1134. int
  1135. xfs_buf_iowait(
  1136. xfs_buf_t *bp)
  1137. {
  1138. XB_TRACE(bp, "iowait", 0);
  1139. if (atomic_read(&bp->b_io_remaining))
  1140. blk_run_address_space(bp->b_target->bt_mapping);
  1141. down(&bp->b_iodonesema);
  1142. XB_TRACE(bp, "iowaited", (long)bp->b_error);
  1143. return bp->b_error;
  1144. }
  1145. xfs_caddr_t
  1146. xfs_buf_offset(
  1147. xfs_buf_t *bp,
  1148. size_t offset)
  1149. {
  1150. struct page *page;
  1151. if (bp->b_flags & XBF_MAPPED)
  1152. return XFS_BUF_PTR(bp) + offset;
  1153. offset += bp->b_offset;
  1154. page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
  1155. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
  1156. }
  1157. /*
  1158. * Move data into or out of a buffer.
  1159. */
  1160. void
  1161. xfs_buf_iomove(
  1162. xfs_buf_t *bp, /* buffer to process */
  1163. size_t boff, /* starting buffer offset */
  1164. size_t bsize, /* length to copy */
  1165. caddr_t data, /* data address */
  1166. xfs_buf_rw_t mode) /* read/write/zero flag */
  1167. {
  1168. size_t bend, cpoff, csize;
  1169. struct page *page;
  1170. bend = boff + bsize;
  1171. while (boff < bend) {
  1172. page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
  1173. cpoff = xfs_buf_poff(boff + bp->b_offset);
  1174. csize = min_t(size_t,
  1175. PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
  1176. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1177. switch (mode) {
  1178. case XBRW_ZERO:
  1179. memset(page_address(page) + cpoff, 0, csize);
  1180. break;
  1181. case XBRW_READ:
  1182. memcpy(data, page_address(page) + cpoff, csize);
  1183. break;
  1184. case XBRW_WRITE:
  1185. memcpy(page_address(page) + cpoff, data, csize);
  1186. }
  1187. boff += csize;
  1188. data += csize;
  1189. }
  1190. }
  1191. /*
  1192. * Handling of buffer targets (buftargs).
  1193. */
  1194. /*
  1195. * Wait for any bufs with callbacks that have been submitted but
  1196. * have not yet returned... walk the hash list for the target.
  1197. */
  1198. void
  1199. xfs_wait_buftarg(
  1200. xfs_buftarg_t *btp)
  1201. {
  1202. xfs_buf_t *bp, *n;
  1203. xfs_bufhash_t *hash;
  1204. uint i;
  1205. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1206. hash = &btp->bt_hash[i];
  1207. again:
  1208. spin_lock(&hash->bh_lock);
  1209. list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
  1210. ASSERT(btp == bp->b_target);
  1211. if (!(bp->b_flags & XBF_FS_MANAGED)) {
  1212. spin_unlock(&hash->bh_lock);
  1213. /*
  1214. * Catch superblock reference count leaks
  1215. * immediately
  1216. */
  1217. BUG_ON(bp->b_bn == 0);
  1218. delay(100);
  1219. goto again;
  1220. }
  1221. }
  1222. spin_unlock(&hash->bh_lock);
  1223. }
  1224. }
  1225. /*
  1226. * Allocate buffer hash table for a given target.
  1227. * For devices containing metadata (i.e. not the log/realtime devices)
  1228. * we need to allocate a much larger hash table.
  1229. */
  1230. STATIC void
  1231. xfs_alloc_bufhash(
  1232. xfs_buftarg_t *btp,
  1233. int external)
  1234. {
  1235. unsigned int i;
  1236. btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
  1237. btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
  1238. btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
  1239. sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
  1240. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1241. spin_lock_init(&btp->bt_hash[i].bh_lock);
  1242. INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
  1243. }
  1244. }
  1245. STATIC void
  1246. xfs_free_bufhash(
  1247. xfs_buftarg_t *btp)
  1248. {
  1249. kmem_free(btp->bt_hash, (1<<btp->bt_hashshift) * sizeof(xfs_bufhash_t));
  1250. btp->bt_hash = NULL;
  1251. }
  1252. /*
  1253. * buftarg list for delwrite queue processing
  1254. */
  1255. static LIST_HEAD(xfs_buftarg_list);
  1256. static DEFINE_SPINLOCK(xfs_buftarg_lock);
  1257. STATIC void
  1258. xfs_register_buftarg(
  1259. xfs_buftarg_t *btp)
  1260. {
  1261. spin_lock(&xfs_buftarg_lock);
  1262. list_add(&btp->bt_list, &xfs_buftarg_list);
  1263. spin_unlock(&xfs_buftarg_lock);
  1264. }
  1265. STATIC void
  1266. xfs_unregister_buftarg(
  1267. xfs_buftarg_t *btp)
  1268. {
  1269. spin_lock(&xfs_buftarg_lock);
  1270. list_del(&btp->bt_list);
  1271. spin_unlock(&xfs_buftarg_lock);
  1272. }
  1273. void
  1274. xfs_free_buftarg(
  1275. xfs_buftarg_t *btp,
  1276. int external)
  1277. {
  1278. xfs_flush_buftarg(btp, 1);
  1279. xfs_blkdev_issue_flush(btp);
  1280. if (external)
  1281. xfs_blkdev_put(btp->bt_bdev);
  1282. xfs_free_bufhash(btp);
  1283. iput(btp->bt_mapping->host);
  1284. /* Unregister the buftarg first so that we don't get a
  1285. * wakeup finding a non-existent task
  1286. */
  1287. xfs_unregister_buftarg(btp);
  1288. kthread_stop(btp->bt_task);
  1289. kmem_free(btp, sizeof(*btp));
  1290. }
  1291. STATIC int
  1292. xfs_setsize_buftarg_flags(
  1293. xfs_buftarg_t *btp,
  1294. unsigned int blocksize,
  1295. unsigned int sectorsize,
  1296. int verbose)
  1297. {
  1298. btp->bt_bsize = blocksize;
  1299. btp->bt_sshift = ffs(sectorsize) - 1;
  1300. btp->bt_smask = sectorsize - 1;
  1301. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1302. printk(KERN_WARNING
  1303. "XFS: Cannot set_blocksize to %u on device %s\n",
  1304. sectorsize, XFS_BUFTARG_NAME(btp));
  1305. return EINVAL;
  1306. }
  1307. if (verbose &&
  1308. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1309. printk(KERN_WARNING
  1310. "XFS: %u byte sectors in use on device %s. "
  1311. "This is suboptimal; %u or greater is ideal.\n",
  1312. sectorsize, XFS_BUFTARG_NAME(btp),
  1313. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1314. }
  1315. return 0;
  1316. }
  1317. /*
  1318. * When allocating the initial buffer target we have not yet
  1319. * read in the superblock, so don't know what sized sectors
  1320. * are being used is at this early stage. Play safe.
  1321. */
  1322. STATIC int
  1323. xfs_setsize_buftarg_early(
  1324. xfs_buftarg_t *btp,
  1325. struct block_device *bdev)
  1326. {
  1327. return xfs_setsize_buftarg_flags(btp,
  1328. PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
  1329. }
  1330. int
  1331. xfs_setsize_buftarg(
  1332. xfs_buftarg_t *btp,
  1333. unsigned int blocksize,
  1334. unsigned int sectorsize)
  1335. {
  1336. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1337. }
  1338. STATIC int
  1339. xfs_mapping_buftarg(
  1340. xfs_buftarg_t *btp,
  1341. struct block_device *bdev)
  1342. {
  1343. struct backing_dev_info *bdi;
  1344. struct inode *inode;
  1345. struct address_space *mapping;
  1346. static const struct address_space_operations mapping_aops = {
  1347. .sync_page = block_sync_page,
  1348. .migratepage = fail_migrate_page,
  1349. };
  1350. inode = new_inode(bdev->bd_inode->i_sb);
  1351. if (!inode) {
  1352. printk(KERN_WARNING
  1353. "XFS: Cannot allocate mapping inode for device %s\n",
  1354. XFS_BUFTARG_NAME(btp));
  1355. return ENOMEM;
  1356. }
  1357. inode->i_mode = S_IFBLK;
  1358. inode->i_bdev = bdev;
  1359. inode->i_rdev = bdev->bd_dev;
  1360. bdi = blk_get_backing_dev_info(bdev);
  1361. if (!bdi)
  1362. bdi = &default_backing_dev_info;
  1363. mapping = &inode->i_data;
  1364. mapping->a_ops = &mapping_aops;
  1365. mapping->backing_dev_info = bdi;
  1366. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1367. btp->bt_mapping = mapping;
  1368. return 0;
  1369. }
  1370. STATIC int
  1371. xfs_alloc_delwrite_queue(
  1372. xfs_buftarg_t *btp)
  1373. {
  1374. int error = 0;
  1375. INIT_LIST_HEAD(&btp->bt_list);
  1376. INIT_LIST_HEAD(&btp->bt_delwrite_queue);
  1377. spinlock_init(&btp->bt_delwrite_lock, "delwri_lock");
  1378. btp->bt_flags = 0;
  1379. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
  1380. if (IS_ERR(btp->bt_task)) {
  1381. error = PTR_ERR(btp->bt_task);
  1382. goto out_error;
  1383. }
  1384. xfs_register_buftarg(btp);
  1385. out_error:
  1386. return error;
  1387. }
  1388. xfs_buftarg_t *
  1389. xfs_alloc_buftarg(
  1390. struct block_device *bdev,
  1391. int external)
  1392. {
  1393. xfs_buftarg_t *btp;
  1394. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1395. btp->bt_dev = bdev->bd_dev;
  1396. btp->bt_bdev = bdev;
  1397. if (xfs_setsize_buftarg_early(btp, bdev))
  1398. goto error;
  1399. if (xfs_mapping_buftarg(btp, bdev))
  1400. goto error;
  1401. if (xfs_alloc_delwrite_queue(btp))
  1402. goto error;
  1403. xfs_alloc_bufhash(btp, external);
  1404. return btp;
  1405. error:
  1406. kmem_free(btp, sizeof(*btp));
  1407. return NULL;
  1408. }
  1409. /*
  1410. * Delayed write buffer handling
  1411. */
  1412. STATIC void
  1413. xfs_buf_delwri_queue(
  1414. xfs_buf_t *bp,
  1415. int unlock)
  1416. {
  1417. struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
  1418. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1419. XB_TRACE(bp, "delwri_q", (long)unlock);
  1420. ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
  1421. spin_lock(dwlk);
  1422. /* If already in the queue, dequeue and place at tail */
  1423. if (!list_empty(&bp->b_list)) {
  1424. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1425. if (unlock)
  1426. atomic_dec(&bp->b_hold);
  1427. list_del(&bp->b_list);
  1428. }
  1429. bp->b_flags |= _XBF_DELWRI_Q;
  1430. list_add_tail(&bp->b_list, dwq);
  1431. bp->b_queuetime = jiffies;
  1432. spin_unlock(dwlk);
  1433. if (unlock)
  1434. xfs_buf_unlock(bp);
  1435. }
  1436. void
  1437. xfs_buf_delwri_dequeue(
  1438. xfs_buf_t *bp)
  1439. {
  1440. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1441. int dequeued = 0;
  1442. spin_lock(dwlk);
  1443. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1444. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1445. list_del_init(&bp->b_list);
  1446. dequeued = 1;
  1447. }
  1448. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1449. spin_unlock(dwlk);
  1450. if (dequeued)
  1451. xfs_buf_rele(bp);
  1452. XB_TRACE(bp, "delwri_dq", (long)dequeued);
  1453. }
  1454. STATIC void
  1455. xfs_buf_runall_queues(
  1456. struct workqueue_struct *queue)
  1457. {
  1458. flush_workqueue(queue);
  1459. }
  1460. STATIC int
  1461. xfsbufd_wakeup(
  1462. int priority,
  1463. gfp_t mask)
  1464. {
  1465. xfs_buftarg_t *btp;
  1466. spin_lock(&xfs_buftarg_lock);
  1467. list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
  1468. if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
  1469. continue;
  1470. set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
  1471. wake_up_process(btp->bt_task);
  1472. }
  1473. spin_unlock(&xfs_buftarg_lock);
  1474. return 0;
  1475. }
  1476. /*
  1477. * Move as many buffers as specified to the supplied list
  1478. * idicating if we skipped any buffers to prevent deadlocks.
  1479. */
  1480. STATIC int
  1481. xfs_buf_delwri_split(
  1482. xfs_buftarg_t *target,
  1483. struct list_head *list,
  1484. unsigned long age)
  1485. {
  1486. xfs_buf_t *bp, *n;
  1487. struct list_head *dwq = &target->bt_delwrite_queue;
  1488. spinlock_t *dwlk = &target->bt_delwrite_lock;
  1489. int skipped = 0;
  1490. int force;
  1491. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1492. INIT_LIST_HEAD(list);
  1493. spin_lock(dwlk);
  1494. list_for_each_entry_safe(bp, n, dwq, b_list) {
  1495. XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
  1496. ASSERT(bp->b_flags & XBF_DELWRI);
  1497. if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
  1498. if (!force &&
  1499. time_before(jiffies, bp->b_queuetime + age)) {
  1500. xfs_buf_unlock(bp);
  1501. break;
  1502. }
  1503. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
  1504. _XBF_RUN_QUEUES);
  1505. bp->b_flags |= XBF_WRITE;
  1506. list_move_tail(&bp->b_list, list);
  1507. } else
  1508. skipped++;
  1509. }
  1510. spin_unlock(dwlk);
  1511. return skipped;
  1512. }
  1513. STATIC int
  1514. xfsbufd(
  1515. void *data)
  1516. {
  1517. struct list_head tmp;
  1518. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1519. int count;
  1520. xfs_buf_t *bp;
  1521. current->flags |= PF_MEMALLOC;
  1522. do {
  1523. if (unlikely(freezing(current))) {
  1524. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1525. refrigerator();
  1526. } else {
  1527. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1528. }
  1529. schedule_timeout_interruptible(
  1530. xfs_buf_timer_centisecs * msecs_to_jiffies(10));
  1531. xfs_buf_delwri_split(target, &tmp,
  1532. xfs_buf_age_centisecs * msecs_to_jiffies(10));
  1533. count = 0;
  1534. while (!list_empty(&tmp)) {
  1535. bp = list_entry(tmp.next, xfs_buf_t, b_list);
  1536. ASSERT(target == bp->b_target);
  1537. list_del_init(&bp->b_list);
  1538. xfs_buf_iostrategy(bp);
  1539. count++;
  1540. }
  1541. if (as_list_len > 0)
  1542. purge_addresses();
  1543. if (count)
  1544. blk_run_address_space(target->bt_mapping);
  1545. } while (!kthread_should_stop());
  1546. return 0;
  1547. }
  1548. /*
  1549. * Go through all incore buffers, and release buffers if they belong to
  1550. * the given device. This is used in filesystem error handling to
  1551. * preserve the consistency of its metadata.
  1552. */
  1553. int
  1554. xfs_flush_buftarg(
  1555. xfs_buftarg_t *target,
  1556. int wait)
  1557. {
  1558. struct list_head tmp;
  1559. xfs_buf_t *bp, *n;
  1560. int pincount = 0;
  1561. xfs_buf_runall_queues(xfsdatad_workqueue);
  1562. xfs_buf_runall_queues(xfslogd_workqueue);
  1563. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1564. pincount = xfs_buf_delwri_split(target, &tmp, 0);
  1565. /*
  1566. * Dropped the delayed write list lock, now walk the temporary list
  1567. */
  1568. list_for_each_entry_safe(bp, n, &tmp, b_list) {
  1569. ASSERT(target == bp->b_target);
  1570. if (wait)
  1571. bp->b_flags &= ~XBF_ASYNC;
  1572. else
  1573. list_del_init(&bp->b_list);
  1574. xfs_buf_iostrategy(bp);
  1575. }
  1576. if (wait)
  1577. blk_run_address_space(target->bt_mapping);
  1578. /*
  1579. * Remaining list items must be flushed before returning
  1580. */
  1581. while (!list_empty(&tmp)) {
  1582. bp = list_entry(tmp.next, xfs_buf_t, b_list);
  1583. list_del_init(&bp->b_list);
  1584. xfs_iowait(bp);
  1585. xfs_buf_relse(bp);
  1586. }
  1587. return pincount;
  1588. }
  1589. int __init
  1590. xfs_buf_init(void)
  1591. {
  1592. #ifdef XFS_BUF_TRACE
  1593. xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_SLEEP);
  1594. #endif
  1595. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1596. KM_ZONE_HWALIGN, NULL);
  1597. if (!xfs_buf_zone)
  1598. goto out_free_trace_buf;
  1599. xfslogd_workqueue = create_workqueue("xfslogd");
  1600. if (!xfslogd_workqueue)
  1601. goto out_free_buf_zone;
  1602. xfsdatad_workqueue = create_workqueue("xfsdatad");
  1603. if (!xfsdatad_workqueue)
  1604. goto out_destroy_xfslogd_workqueue;
  1605. register_shrinker(&xfs_buf_shake);
  1606. return 0;
  1607. out_destroy_xfslogd_workqueue:
  1608. destroy_workqueue(xfslogd_workqueue);
  1609. out_free_buf_zone:
  1610. kmem_zone_destroy(xfs_buf_zone);
  1611. out_free_trace_buf:
  1612. #ifdef XFS_BUF_TRACE
  1613. ktrace_free(xfs_buf_trace_buf);
  1614. #endif
  1615. return -ENOMEM;
  1616. }
  1617. void
  1618. xfs_buf_terminate(void)
  1619. {
  1620. unregister_shrinker(&xfs_buf_shake);
  1621. destroy_workqueue(xfsdatad_workqueue);
  1622. destroy_workqueue(xfslogd_workqueue);
  1623. kmem_zone_destroy(xfs_buf_zone);
  1624. #ifdef XFS_BUF_TRACE
  1625. ktrace_free(xfs_buf_trace_buf);
  1626. #endif
  1627. }
  1628. #ifdef CONFIG_KDB_MODULES
  1629. struct list_head *
  1630. xfs_get_buftarg_list(void)
  1631. {
  1632. return &xfs_buftarg_list;
  1633. }
  1634. #endif