meta_io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/mm.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/writeback.h>
  17. #include <linux/swap.h>
  18. #include <linux/delay.h>
  19. #include <asm/semaphore.h>
  20. #include "gfs2.h"
  21. #include "glock.h"
  22. #include "glops.h"
  23. #include "inode.h"
  24. #include "log.h"
  25. #include "lops.h"
  26. #include "meta_io.h"
  27. #include "rgrp.h"
  28. #include "trans.h"
  29. #define buffer_busy(bh) \
  30. ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
  31. #define buffer_in_io(bh) \
  32. ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
  33. static int aspace_get_block(struct inode *inode, sector_t lblock,
  34. struct buffer_head *bh_result, int create)
  35. {
  36. gfs2_assert_warn(get_v2sdp(inode->i_sb), 0);
  37. return -EOPNOTSUPP;
  38. }
  39. static int gfs2_aspace_writepage(struct page *page,
  40. struct writeback_control *wbc)
  41. {
  42. return block_write_full_page(page, aspace_get_block, wbc);
  43. }
  44. /**
  45. * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
  46. * @bh: the buffer we're stuck on
  47. *
  48. */
  49. static void stuck_releasepage(struct buffer_head *bh)
  50. {
  51. struct gfs2_sbd *sdp = get_v2sdp(bh->b_page->mapping->host->i_sb);
  52. struct gfs2_bufdata *bd = get_v2bd(bh);
  53. struct gfs2_glock *gl;
  54. fs_warn(sdp, "stuck in gfs2_releasepage()\n");
  55. fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
  56. (uint64_t)bh->b_blocknr, atomic_read(&bh->b_count));
  57. fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
  58. fs_warn(sdp, "get_v2bd(bh) = %s\n", (bd) ? "!NULL" : "NULL");
  59. if (!bd)
  60. return;
  61. gl = bd->bd_gl;
  62. fs_warn(sdp, "gl = (%u, %llu)\n",
  63. gl->gl_name.ln_type, gl->gl_name.ln_number);
  64. fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
  65. (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
  66. (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
  67. if (gl->gl_ops == &gfs2_inode_glops) {
  68. struct gfs2_inode *ip = get_gl2ip(gl);
  69. unsigned int x;
  70. if (!ip)
  71. return;
  72. fs_warn(sdp, "ip = %llu %llu\n",
  73. ip->i_num.no_formal_ino, ip->i_num.no_addr);
  74. fs_warn(sdp, "ip->i_count = %d, ip->i_vnode = %s\n",
  75. atomic_read(&ip->i_count),
  76. (ip->i_vnode) ? "!NULL" : "NULL");
  77. for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
  78. fs_warn(sdp, "ip->i_cache[%u] = %s\n",
  79. x, (ip->i_cache[x]) ? "!NULL" : "NULL");
  80. }
  81. }
  82. /**
  83. * gfs2_aspace_releasepage - free the metadata associated with a page
  84. * @page: the page that's being released
  85. * @gfp_mask: passed from Linux VFS, ignored by us
  86. *
  87. * Call try_to_free_buffers() if the buffers in this page can be
  88. * released.
  89. *
  90. * Returns: 0
  91. */
  92. static int gfs2_aspace_releasepage(struct page *page, gfp_t gfp_mask)
  93. {
  94. struct inode *aspace = page->mapping->host;
  95. struct gfs2_sbd *sdp = get_v2sdp(aspace->i_sb);
  96. struct buffer_head *bh, *head;
  97. struct gfs2_bufdata *bd;
  98. unsigned long t;
  99. if (!page_has_buffers(page))
  100. goto out;
  101. head = bh = page_buffers(page);
  102. do {
  103. t = jiffies;
  104. while (atomic_read(&bh->b_count)) {
  105. if (atomic_read(&aspace->i_writecount)) {
  106. if (time_after_eq(jiffies, t +
  107. gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
  108. stuck_releasepage(bh);
  109. t = jiffies;
  110. }
  111. yield();
  112. continue;
  113. }
  114. return 0;
  115. }
  116. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  117. bd = get_v2bd(bh);
  118. if (bd) {
  119. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  120. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  121. gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
  122. gfs2_assert_warn(sdp, !bd->bd_ail);
  123. kmem_cache_free(gfs2_bufdata_cachep, bd);
  124. atomic_dec(&sdp->sd_bufdata_count);
  125. set_v2bd(bh, NULL);
  126. }
  127. bh = bh->b_this_page;
  128. }
  129. while (bh != head);
  130. out:
  131. return try_to_free_buffers(page);
  132. }
  133. static struct address_space_operations aspace_aops = {
  134. .writepage = gfs2_aspace_writepage,
  135. .releasepage = gfs2_aspace_releasepage,
  136. };
  137. /**
  138. * gfs2_aspace_get - Create and initialize a struct inode structure
  139. * @sdp: the filesystem the aspace is in
  140. *
  141. * Right now a struct inode is just a struct inode. Maybe Linux
  142. * will supply a more lightweight address space construct (that works)
  143. * in the future.
  144. *
  145. * Make sure pages/buffers in this aspace aren't in high memory.
  146. *
  147. * Returns: the aspace
  148. */
  149. struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
  150. {
  151. struct inode *aspace;
  152. aspace = new_inode(sdp->sd_vfs);
  153. if (aspace) {
  154. mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
  155. aspace->i_mapping->a_ops = &aspace_aops;
  156. aspace->i_size = ~0ULL;
  157. set_v2ip(aspace, NULL);
  158. insert_inode_hash(aspace);
  159. }
  160. return aspace;
  161. }
  162. void gfs2_aspace_put(struct inode *aspace)
  163. {
  164. remove_inode_hash(aspace);
  165. iput(aspace);
  166. }
  167. /**
  168. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  169. * @sdp: the filesystem
  170. * @tr: the part of the AIL
  171. *
  172. */
  173. void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  174. {
  175. struct gfs2_bufdata *bd, *s;
  176. struct buffer_head *bh;
  177. int retry;
  178. do {
  179. retry = 0;
  180. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  181. bd_ail_st_list) {
  182. bh = bd->bd_bh;
  183. gfs2_assert(sdp, bd->bd_ail == ai);
  184. if (!buffer_busy(bh)) {
  185. if (!buffer_uptodate(bh))
  186. gfs2_io_error_bh(sdp, bh);
  187. list_move(&bd->bd_ail_st_list,
  188. &ai->ai_ail2_list);
  189. continue;
  190. }
  191. if (!buffer_dirty(bh))
  192. continue;
  193. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  194. gfs2_log_unlock(sdp);
  195. wait_on_buffer(bh);
  196. ll_rw_block(WRITE, 1, &bh);
  197. gfs2_log_lock(sdp);
  198. retry = 1;
  199. break;
  200. }
  201. } while (retry);
  202. }
  203. /**
  204. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  205. * @sdp: the filesystem
  206. * @ai: the AIL entry
  207. *
  208. */
  209. int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
  210. {
  211. struct gfs2_bufdata *bd, *s;
  212. struct buffer_head *bh;
  213. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  214. bd_ail_st_list) {
  215. bh = bd->bd_bh;
  216. gfs2_assert(sdp, bd->bd_ail == ai);
  217. if (buffer_busy(bh)) {
  218. if (flags & DIO_ALL)
  219. continue;
  220. else
  221. break;
  222. }
  223. if (!buffer_uptodate(bh))
  224. gfs2_io_error_bh(sdp, bh);
  225. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  226. }
  227. return list_empty(&ai->ai_ail1_list);
  228. }
  229. /**
  230. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  231. * @sdp: the filesystem
  232. * @ai: the AIL entry
  233. *
  234. */
  235. void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  236. {
  237. struct list_head *head = &ai->ai_ail2_list;
  238. struct gfs2_bufdata *bd;
  239. while (!list_empty(head)) {
  240. bd = list_entry(head->prev, struct gfs2_bufdata,
  241. bd_ail_st_list);
  242. gfs2_assert(sdp, bd->bd_ail == ai);
  243. bd->bd_ail = NULL;
  244. list_del(&bd->bd_ail_st_list);
  245. list_del(&bd->bd_ail_gl_list);
  246. atomic_dec(&bd->bd_gl->gl_ail_count);
  247. brelse(bd->bd_bh);
  248. }
  249. }
  250. /**
  251. * ail_empty_gl - remove all buffers for a given lock from the AIL
  252. * @gl: the glock
  253. *
  254. * None of the buffers should be dirty, locked, or pinned.
  255. */
  256. void gfs2_ail_empty_gl(struct gfs2_glock *gl)
  257. {
  258. struct gfs2_sbd *sdp = gl->gl_sbd;
  259. unsigned int blocks;
  260. struct list_head *head = &gl->gl_ail_list;
  261. struct gfs2_bufdata *bd;
  262. struct buffer_head *bh;
  263. uint64_t blkno;
  264. int error;
  265. blocks = atomic_read(&gl->gl_ail_count);
  266. if (!blocks)
  267. return;
  268. error = gfs2_trans_begin(sdp, 0, blocks);
  269. if (gfs2_assert_withdraw(sdp, !error))
  270. return;
  271. gfs2_log_lock(sdp);
  272. while (!list_empty(head)) {
  273. bd = list_entry(head->next, struct gfs2_bufdata,
  274. bd_ail_gl_list);
  275. bh = bd->bd_bh;
  276. blkno = bh->b_blocknr;
  277. gfs2_assert_withdraw(sdp, !buffer_busy(bh));
  278. bd->bd_ail = NULL;
  279. list_del(&bd->bd_ail_st_list);
  280. list_del(&bd->bd_ail_gl_list);
  281. atomic_dec(&gl->gl_ail_count);
  282. brelse(bh);
  283. gfs2_log_unlock(sdp);
  284. gfs2_trans_add_revoke(sdp, blkno);
  285. gfs2_log_lock(sdp);
  286. }
  287. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  288. gfs2_log_unlock(sdp);
  289. gfs2_trans_end(sdp);
  290. gfs2_log_flush(sdp);
  291. }
  292. /**
  293. * gfs2_meta_inval - Invalidate all buffers associated with a glock
  294. * @gl: the glock
  295. *
  296. */
  297. void gfs2_meta_inval(struct gfs2_glock *gl)
  298. {
  299. struct gfs2_sbd *sdp = gl->gl_sbd;
  300. struct inode *aspace = gl->gl_aspace;
  301. struct address_space *mapping = gl->gl_aspace->i_mapping;
  302. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  303. atomic_inc(&aspace->i_writecount);
  304. truncate_inode_pages(mapping, 0);
  305. atomic_dec(&aspace->i_writecount);
  306. gfs2_assert_withdraw(sdp, !mapping->nrpages);
  307. }
  308. /**
  309. * gfs2_meta_sync - Sync all buffers associated with a glock
  310. * @gl: The glock
  311. * @flags: DIO_START | DIO_WAIT
  312. *
  313. */
  314. void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
  315. {
  316. struct address_space *mapping = gl->gl_aspace->i_mapping;
  317. int error = 0;
  318. if (flags & DIO_START)
  319. filemap_fdatawrite(mapping);
  320. if (!error && (flags & DIO_WAIT))
  321. error = filemap_fdatawait(mapping);
  322. if (error)
  323. gfs2_io_error(gl->gl_sbd);
  324. }
  325. /**
  326. * getbuf - Get a buffer with a given address space
  327. * @sdp: the filesystem
  328. * @aspace: the address space
  329. * @blkno: the block number (filesystem scope)
  330. * @create: 1 if the buffer should be created
  331. *
  332. * Returns: the buffer
  333. */
  334. static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
  335. uint64_t blkno, int create)
  336. {
  337. struct page *page;
  338. struct buffer_head *bh;
  339. unsigned int shift;
  340. unsigned long index;
  341. unsigned int bufnum;
  342. shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  343. index = blkno >> shift; /* convert block to page */
  344. bufnum = blkno - (index << shift); /* block buf index within page */
  345. if (create) {
  346. for (;;) {
  347. page = grab_cache_page(aspace->i_mapping, index);
  348. if (page)
  349. break;
  350. yield();
  351. }
  352. } else {
  353. page = find_lock_page(aspace->i_mapping, index);
  354. if (!page)
  355. return NULL;
  356. }
  357. if (!page_has_buffers(page))
  358. create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  359. /* Locate header for our buffer within our page */
  360. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  361. /* Do nothing */;
  362. get_bh(bh);
  363. if (!buffer_mapped(bh))
  364. map_bh(bh, sdp->sd_vfs, blkno);
  365. unlock_page(page);
  366. mark_page_accessed(page);
  367. page_cache_release(page);
  368. return bh;
  369. }
  370. static void meta_prep_new(struct buffer_head *bh)
  371. {
  372. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  373. lock_buffer(bh);
  374. clear_buffer_dirty(bh);
  375. set_buffer_uptodate(bh);
  376. unlock_buffer(bh);
  377. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  378. }
  379. /**
  380. * gfs2_meta_new - Get a block
  381. * @gl: The glock associated with this block
  382. * @blkno: The block number
  383. *
  384. * Returns: The buffer
  385. */
  386. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno)
  387. {
  388. struct buffer_head *bh;
  389. bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  390. meta_prep_new(bh);
  391. return bh;
  392. }
  393. /**
  394. * gfs2_meta_read - Read a block from disk
  395. * @gl: The glock covering the block
  396. * @blkno: The block number
  397. * @flags: flags to gfs2_dreread()
  398. * @bhp: the place where the buffer is returned (NULL on failure)
  399. *
  400. * Returns: errno
  401. */
  402. int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags,
  403. struct buffer_head **bhp)
  404. {
  405. int error;
  406. *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  407. error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
  408. if (error)
  409. brelse(*bhp);
  410. return error;
  411. }
  412. /**
  413. * gfs2_meta_reread - Reread a block from disk
  414. * @sdp: the filesystem
  415. * @bh: The block to read
  416. * @flags: Flags that control the read
  417. *
  418. * Returns: errno
  419. */
  420. int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
  421. {
  422. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  423. return -EIO;
  424. if (flags & DIO_FORCE)
  425. clear_buffer_uptodate(bh);
  426. if ((flags & DIO_START) && !buffer_uptodate(bh))
  427. ll_rw_block(READ, 1, &bh);
  428. if (flags & DIO_WAIT) {
  429. wait_on_buffer(bh);
  430. if (!buffer_uptodate(bh)) {
  431. struct gfs2_trans *tr = get_transaction;
  432. if (tr && tr->tr_touched)
  433. gfs2_io_error_bh(sdp, bh);
  434. return -EIO;
  435. }
  436. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  437. return -EIO;
  438. }
  439. return 0;
  440. }
  441. /**
  442. * gfs2_meta_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
  443. * @gl: the glock the buffer belongs to
  444. * @bh: The buffer to be attached to
  445. *
  446. */
  447. void gfs2_meta_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh)
  448. {
  449. struct gfs2_bufdata *bd;
  450. lock_page(bh->b_page);
  451. if (get_v2bd(bh)) {
  452. unlock_page(bh->b_page);
  453. return;
  454. }
  455. bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_KERNEL | __GFP_NOFAIL),
  456. atomic_inc(&gl->gl_sbd->sd_bufdata_count);
  457. memset(bd, 0, sizeof(struct gfs2_bufdata));
  458. bd->bd_bh = bh;
  459. bd->bd_gl = gl;
  460. INIT_LIST_HEAD(&bd->bd_list_tr);
  461. lops_init_le(&bd->bd_le, &gfs2_buf_lops);
  462. set_v2bd(bh, bd);
  463. unlock_page(bh->b_page);
  464. }
  465. /**
  466. * gfs2_meta_pin - Pin a metadata buffer in memory
  467. * @sdp: the filesystem the buffer belongs to
  468. * @bh: The buffer to be pinned
  469. *
  470. */
  471. void gfs2_meta_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
  472. {
  473. struct gfs2_bufdata *bd = get_v2bd(bh);
  474. gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
  475. if (test_set_buffer_pinned(bh))
  476. gfs2_assert_withdraw(sdp, 0);
  477. wait_on_buffer(bh);
  478. /* If this buffer is in the AIL and it has already been written
  479. to in-place disk block, remove it from the AIL. */
  480. gfs2_log_lock(sdp);
  481. if (bd->bd_ail && !buffer_in_io(bh))
  482. list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
  483. gfs2_log_unlock(sdp);
  484. clear_buffer_dirty(bh);
  485. wait_on_buffer(bh);
  486. if (!buffer_uptodate(bh))
  487. gfs2_io_error_bh(sdp, bh);
  488. get_bh(bh);
  489. }
  490. /**
  491. * gfs2_meta_unpin - Unpin a buffer
  492. * @sdp: the filesystem the buffer belongs to
  493. * @bh: The buffer to unpin
  494. * @ai:
  495. *
  496. */
  497. void gfs2_meta_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  498. struct gfs2_ail *ai)
  499. {
  500. struct gfs2_bufdata *bd = get_v2bd(bh);
  501. gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
  502. if (!buffer_pinned(bh))
  503. gfs2_assert_withdraw(sdp, 0);
  504. mark_buffer_dirty(bh);
  505. clear_buffer_pinned(bh);
  506. gfs2_log_lock(sdp);
  507. if (bd->bd_ail) {
  508. list_del(&bd->bd_ail_st_list);
  509. brelse(bh);
  510. } else {
  511. struct gfs2_glock *gl = bd->bd_gl;
  512. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  513. atomic_inc(&gl->gl_ail_count);
  514. }
  515. bd->bd_ail = ai;
  516. list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  517. gfs2_log_unlock(sdp);
  518. }
  519. /**
  520. * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
  521. * @ip: the inode who owns the buffers
  522. * @bstart: the first buffer in the run
  523. * @blen: the number of buffers in the run
  524. *
  525. */
  526. void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  527. {
  528. struct gfs2_sbd *sdp = ip->i_sbd;
  529. struct inode *aspace = ip->i_gl->gl_aspace;
  530. struct buffer_head *bh;
  531. while (blen) {
  532. bh = getbuf(sdp, aspace, bstart, NO_CREATE);
  533. if (bh) {
  534. struct gfs2_bufdata *bd = get_v2bd(bh);
  535. if (test_clear_buffer_pinned(bh)) {
  536. gfs2_log_lock(sdp);
  537. list_del_init(&bd->bd_le.le_list);
  538. gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
  539. sdp->sd_log_num_buf--;
  540. gfs2_log_unlock(sdp);
  541. get_transaction->tr_num_buf_rm++;
  542. brelse(bh);
  543. }
  544. if (bd) {
  545. gfs2_log_lock(sdp);
  546. if (bd->bd_ail) {
  547. uint64_t blkno = bh->b_blocknr;
  548. bd->bd_ail = NULL;
  549. list_del(&bd->bd_ail_st_list);
  550. list_del(&bd->bd_ail_gl_list);
  551. atomic_dec(&bd->bd_gl->gl_ail_count);
  552. brelse(bh);
  553. gfs2_log_unlock(sdp);
  554. gfs2_trans_add_revoke(sdp, blkno);
  555. } else
  556. gfs2_log_unlock(sdp);
  557. }
  558. lock_buffer(bh);
  559. clear_buffer_dirty(bh);
  560. clear_buffer_uptodate(bh);
  561. unlock_buffer(bh);
  562. brelse(bh);
  563. }
  564. bstart++;
  565. blen--;
  566. }
  567. }
  568. /**
  569. * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
  570. * @ip: The GFS2 inode
  571. *
  572. * This releases buffers that are in the most-recently-used array of
  573. * blocks used for indirect block addressing for this inode.
  574. */
  575. void gfs2_meta_cache_flush(struct gfs2_inode *ip)
  576. {
  577. struct buffer_head **bh_slot;
  578. unsigned int x;
  579. spin_lock(&ip->i_spin);
  580. for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
  581. bh_slot = &ip->i_cache[x];
  582. if (!*bh_slot)
  583. break;
  584. brelse(*bh_slot);
  585. *bh_slot = NULL;
  586. }
  587. spin_unlock(&ip->i_spin);
  588. }
  589. /**
  590. * gfs2_meta_indirect_buffer - Get a metadata buffer
  591. * @ip: The GFS2 inode
  592. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  593. * @num: The block number (device relative) of the buffer
  594. * @new: Non-zero if we may create a new buffer
  595. * @bhp: the buffer is returned here
  596. *
  597. * Try to use the gfs2_inode's MRU metadata tree cache.
  598. *
  599. * Returns: errno
  600. */
  601. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
  602. int new, struct buffer_head **bhp)
  603. {
  604. struct buffer_head *bh, **bh_slot = ip->i_cache + height;
  605. int error;
  606. spin_lock(&ip->i_spin);
  607. bh = *bh_slot;
  608. if (bh) {
  609. if (bh->b_blocknr == num)
  610. get_bh(bh);
  611. else
  612. bh = NULL;
  613. }
  614. spin_unlock(&ip->i_spin);
  615. if (bh) {
  616. if (new)
  617. meta_prep_new(bh);
  618. else {
  619. error = gfs2_meta_reread(ip->i_sbd, bh,
  620. DIO_START | DIO_WAIT);
  621. if (error) {
  622. brelse(bh);
  623. return error;
  624. }
  625. }
  626. } else {
  627. if (new)
  628. bh = gfs2_meta_new(ip->i_gl, num);
  629. else {
  630. error = gfs2_meta_read(ip->i_gl, num,
  631. DIO_START | DIO_WAIT, &bh);
  632. if (error)
  633. return error;
  634. }
  635. spin_lock(&ip->i_spin);
  636. if (*bh_slot != bh) {
  637. brelse(*bh_slot);
  638. *bh_slot = bh;
  639. get_bh(bh);
  640. }
  641. spin_unlock(&ip->i_spin);
  642. }
  643. if (new) {
  644. if (gfs2_assert_warn(ip->i_sbd, height)) {
  645. brelse(bh);
  646. return -EIO;
  647. }
  648. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  649. gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  650. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  651. } else if (gfs2_metatype_check(ip->i_sbd, bh,
  652. (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
  653. brelse(bh);
  654. return -EIO;
  655. }
  656. *bhp = bh;
  657. return 0;
  658. }
  659. /**
  660. * gfs2_meta_ra - start readahead on an extent of a file
  661. * @gl: the glock the blocks belong to
  662. * @dblock: the starting disk block
  663. * @extlen: the number of blocks in the extent
  664. *
  665. */
  666. void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
  667. {
  668. struct gfs2_sbd *sdp = gl->gl_sbd;
  669. struct inode *aspace = gl->gl_aspace;
  670. struct buffer_head *first_bh, *bh;
  671. uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift;
  672. int error;
  673. if (!extlen || !max_ra)
  674. return;
  675. if (extlen > max_ra)
  676. extlen = max_ra;
  677. first_bh = getbuf(sdp, aspace, dblock, CREATE);
  678. if (buffer_uptodate(first_bh))
  679. goto out;
  680. if (!buffer_locked(first_bh)) {
  681. error = gfs2_meta_reread(sdp, first_bh, DIO_START);
  682. if (error)
  683. goto out;
  684. }
  685. dblock++;
  686. extlen--;
  687. while (extlen) {
  688. bh = getbuf(sdp, aspace, dblock, CREATE);
  689. if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
  690. error = gfs2_meta_reread(sdp, bh, DIO_START);
  691. brelse(bh);
  692. if (error)
  693. goto out;
  694. } else
  695. brelse(bh);
  696. dblock++;
  697. extlen--;
  698. if (buffer_uptodate(first_bh))
  699. break;
  700. }
  701. out:
  702. brelse(first_bh);
  703. }
  704. /**
  705. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  706. * @sdp: the filesystem
  707. *
  708. */
  709. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  710. {
  711. gfs2_log_flush(sdp);
  712. for (;;) {
  713. gfs2_ail1_start(sdp, DIO_ALL);
  714. if (gfs2_ail1_empty(sdp, DIO_ALL))
  715. break;
  716. msleep(100);
  717. }
  718. }