xfs_buf.c 42 KB

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