meta_io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. set_v2bd(bh, NULL);
  125. }
  126. bh = bh->b_this_page;
  127. }
  128. while (bh != head);
  129. out:
  130. return try_to_free_buffers(page);
  131. }
  132. static struct address_space_operations aspace_aops = {
  133. .writepage = gfs2_aspace_writepage,
  134. .releasepage = gfs2_aspace_releasepage,
  135. };
  136. /**
  137. * gfs2_aspace_get - Create and initialize a struct inode structure
  138. * @sdp: the filesystem the aspace is in
  139. *
  140. * Right now a struct inode is just a struct inode. Maybe Linux
  141. * will supply a more lightweight address space construct (that works)
  142. * in the future.
  143. *
  144. * Make sure pages/buffers in this aspace aren't in high memory.
  145. *
  146. * Returns: the aspace
  147. */
  148. struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
  149. {
  150. struct inode *aspace;
  151. aspace = new_inode(sdp->sd_vfs);
  152. if (aspace) {
  153. mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
  154. aspace->i_mapping->a_ops = &aspace_aops;
  155. aspace->i_size = ~0ULL;
  156. set_v2ip(aspace, NULL);
  157. insert_inode_hash(aspace);
  158. }
  159. return aspace;
  160. }
  161. void gfs2_aspace_put(struct inode *aspace)
  162. {
  163. remove_inode_hash(aspace);
  164. iput(aspace);
  165. }
  166. /**
  167. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  168. * @sdp: the filesystem
  169. * @tr: the part of the AIL
  170. *
  171. */
  172. void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  173. {
  174. struct gfs2_bufdata *bd, *s;
  175. struct buffer_head *bh;
  176. int retry;
  177. do {
  178. retry = 0;
  179. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  180. bd_ail_st_list) {
  181. bh = bd->bd_bh;
  182. gfs2_assert(sdp, bd->bd_ail == ai);
  183. if (!buffer_busy(bh)) {
  184. if (!buffer_uptodate(bh))
  185. gfs2_io_error_bh(sdp, bh);
  186. list_move(&bd->bd_ail_st_list,
  187. &ai->ai_ail2_list);
  188. continue;
  189. }
  190. if (!buffer_dirty(bh))
  191. continue;
  192. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  193. gfs2_log_unlock(sdp);
  194. wait_on_buffer(bh);
  195. ll_rw_block(WRITE, 1, &bh);
  196. gfs2_log_lock(sdp);
  197. retry = 1;
  198. break;
  199. }
  200. } while (retry);
  201. }
  202. /**
  203. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  204. * @sdp: the filesystem
  205. * @ai: the AIL entry
  206. *
  207. */
  208. int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
  209. {
  210. struct gfs2_bufdata *bd, *s;
  211. struct buffer_head *bh;
  212. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  213. bd_ail_st_list) {
  214. bh = bd->bd_bh;
  215. gfs2_assert(sdp, bd->bd_ail == ai);
  216. if (buffer_busy(bh)) {
  217. if (flags & DIO_ALL)
  218. continue;
  219. else
  220. break;
  221. }
  222. if (!buffer_uptodate(bh))
  223. gfs2_io_error_bh(sdp, bh);
  224. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  225. }
  226. return list_empty(&ai->ai_ail1_list);
  227. }
  228. /**
  229. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  230. * @sdp: the filesystem
  231. * @ai: the AIL entry
  232. *
  233. */
  234. void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  235. {
  236. struct list_head *head = &ai->ai_ail2_list;
  237. struct gfs2_bufdata *bd;
  238. while (!list_empty(head)) {
  239. bd = list_entry(head->prev, struct gfs2_bufdata,
  240. bd_ail_st_list);
  241. gfs2_assert(sdp, bd->bd_ail == ai);
  242. bd->bd_ail = NULL;
  243. list_del(&bd->bd_ail_st_list);
  244. list_del(&bd->bd_ail_gl_list);
  245. atomic_dec(&bd->bd_gl->gl_ail_count);
  246. brelse(bd->bd_bh);
  247. }
  248. }
  249. /**
  250. * ail_empty_gl - remove all buffers for a given lock from the AIL
  251. * @gl: the glock
  252. *
  253. * None of the buffers should be dirty, locked, or pinned.
  254. */
  255. void gfs2_ail_empty_gl(struct gfs2_glock *gl)
  256. {
  257. struct gfs2_sbd *sdp = gl->gl_sbd;
  258. unsigned int blocks;
  259. struct list_head *head = &gl->gl_ail_list;
  260. struct gfs2_bufdata *bd;
  261. struct buffer_head *bh;
  262. uint64_t blkno;
  263. int error;
  264. blocks = atomic_read(&gl->gl_ail_count);
  265. if (!blocks)
  266. return;
  267. error = gfs2_trans_begin(sdp, 0, blocks);
  268. if (gfs2_assert_withdraw(sdp, !error))
  269. return;
  270. gfs2_log_lock(sdp);
  271. while (!list_empty(head)) {
  272. bd = list_entry(head->next, struct gfs2_bufdata,
  273. bd_ail_gl_list);
  274. bh = bd->bd_bh;
  275. blkno = bh->b_blocknr;
  276. gfs2_assert_withdraw(sdp, !buffer_busy(bh));
  277. bd->bd_ail = NULL;
  278. list_del(&bd->bd_ail_st_list);
  279. list_del(&bd->bd_ail_gl_list);
  280. atomic_dec(&gl->gl_ail_count);
  281. brelse(bh);
  282. gfs2_log_unlock(sdp);
  283. gfs2_trans_add_revoke(sdp, blkno);
  284. gfs2_log_lock(sdp);
  285. }
  286. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  287. gfs2_log_unlock(sdp);
  288. gfs2_trans_end(sdp);
  289. gfs2_log_flush(sdp);
  290. }
  291. /**
  292. * gfs2_meta_inval - Invalidate all buffers associated with a glock
  293. * @gl: the glock
  294. *
  295. */
  296. void gfs2_meta_inval(struct gfs2_glock *gl)
  297. {
  298. struct gfs2_sbd *sdp = gl->gl_sbd;
  299. struct inode *aspace = gl->gl_aspace;
  300. struct address_space *mapping = gl->gl_aspace->i_mapping;
  301. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  302. atomic_inc(&aspace->i_writecount);
  303. truncate_inode_pages(mapping, 0);
  304. atomic_dec(&aspace->i_writecount);
  305. gfs2_assert_withdraw(sdp, !mapping->nrpages);
  306. }
  307. /**
  308. * gfs2_meta_sync - Sync all buffers associated with a glock
  309. * @gl: The glock
  310. * @flags: DIO_START | DIO_WAIT
  311. *
  312. */
  313. void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
  314. {
  315. struct address_space *mapping = gl->gl_aspace->i_mapping;
  316. int error = 0;
  317. if (flags & DIO_START)
  318. filemap_fdatawrite(mapping);
  319. if (!error && (flags & DIO_WAIT))
  320. error = filemap_fdatawait(mapping);
  321. if (error)
  322. gfs2_io_error(gl->gl_sbd);
  323. }
  324. /**
  325. * getbuf - Get a buffer with a given address space
  326. * @sdp: the filesystem
  327. * @aspace: the address space
  328. * @blkno: the block number (filesystem scope)
  329. * @create: 1 if the buffer should be created
  330. *
  331. * Returns: the buffer
  332. */
  333. static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
  334. uint64_t blkno, int create)
  335. {
  336. struct page *page;
  337. struct buffer_head *bh;
  338. unsigned int shift;
  339. unsigned long index;
  340. unsigned int bufnum;
  341. shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  342. index = blkno >> shift; /* convert block to page */
  343. bufnum = blkno - (index << shift); /* block buf index within page */
  344. if (create) {
  345. for (;;) {
  346. page = grab_cache_page(aspace->i_mapping, index);
  347. if (page)
  348. break;
  349. yield();
  350. }
  351. } else {
  352. page = find_lock_page(aspace->i_mapping, index);
  353. if (!page)
  354. return NULL;
  355. }
  356. if (!page_has_buffers(page))
  357. create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  358. /* Locate header for our buffer within our page */
  359. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  360. /* Do nothing */;
  361. get_bh(bh);
  362. if (!buffer_mapped(bh))
  363. map_bh(bh, sdp->sd_vfs, blkno);
  364. unlock_page(page);
  365. mark_page_accessed(page);
  366. page_cache_release(page);
  367. return bh;
  368. }
  369. static void meta_prep_new(struct buffer_head *bh)
  370. {
  371. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  372. lock_buffer(bh);
  373. clear_buffer_dirty(bh);
  374. set_buffer_uptodate(bh);
  375. unlock_buffer(bh);
  376. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  377. }
  378. /**
  379. * gfs2_meta_new - Get a block
  380. * @gl: The glock associated with this block
  381. * @blkno: The block number
  382. *
  383. * Returns: The buffer
  384. */
  385. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno)
  386. {
  387. struct buffer_head *bh;
  388. bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  389. meta_prep_new(bh);
  390. return bh;
  391. }
  392. /**
  393. * gfs2_meta_read - Read a block from disk
  394. * @gl: The glock covering the block
  395. * @blkno: The block number
  396. * @flags: flags to gfs2_dreread()
  397. * @bhp: the place where the buffer is returned (NULL on failure)
  398. *
  399. * Returns: errno
  400. */
  401. int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags,
  402. struct buffer_head **bhp)
  403. {
  404. int error;
  405. *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  406. error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
  407. if (error)
  408. brelse(*bhp);
  409. return error;
  410. }
  411. /**
  412. * gfs2_meta_reread - Reread a block from disk
  413. * @sdp: the filesystem
  414. * @bh: The block to read
  415. * @flags: Flags that control the read
  416. *
  417. * Returns: errno
  418. */
  419. int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
  420. {
  421. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  422. return -EIO;
  423. if (flags & DIO_FORCE)
  424. clear_buffer_uptodate(bh);
  425. if ((flags & DIO_START) && !buffer_uptodate(bh))
  426. ll_rw_block(READ, 1, &bh);
  427. if (flags & DIO_WAIT) {
  428. wait_on_buffer(bh);
  429. if (!buffer_uptodate(bh)) {
  430. struct gfs2_trans *tr = get_transaction;
  431. if (tr && tr->tr_touched)
  432. gfs2_io_error_bh(sdp, bh);
  433. return -EIO;
  434. }
  435. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  436. return -EIO;
  437. }
  438. return 0;
  439. }
  440. /**
  441. * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
  442. * @gl: the glock the buffer belongs to
  443. * @bh: The buffer to be attached to
  444. * @meta: Flag to indicate whether its metadata or not
  445. */
  446. void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
  447. {
  448. struct gfs2_bufdata *bd;
  449. if (meta)
  450. lock_page(bh->b_page);
  451. if (get_v2bd(bh)) {
  452. if (meta)
  453. unlock_page(bh->b_page);
  454. return;
  455. }
  456. bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
  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. if (meta) {
  462. lops_init_le(&bd->bd_le, &gfs2_buf_lops);
  463. } else {
  464. lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
  465. get_bh(bh);
  466. }
  467. set_v2bd(bh, bd);
  468. if (meta)
  469. unlock_page(bh->b_page);
  470. }
  471. /**
  472. * gfs2_pin - Pin a buffer in memory
  473. * @sdp: the filesystem the buffer belongs to
  474. * @bh: The buffer to be pinned
  475. *
  476. */
  477. void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
  478. {
  479. struct gfs2_bufdata *bd = get_v2bd(bh);
  480. gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
  481. if (test_set_buffer_pinned(bh))
  482. gfs2_assert_withdraw(sdp, 0);
  483. wait_on_buffer(bh);
  484. /* If this buffer is in the AIL and it has already been written
  485. to in-place disk block, remove it from the AIL. */
  486. gfs2_log_lock(sdp);
  487. if (bd->bd_ail && !buffer_in_io(bh))
  488. list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
  489. gfs2_log_unlock(sdp);
  490. clear_buffer_dirty(bh);
  491. wait_on_buffer(bh);
  492. if (!buffer_uptodate(bh))
  493. gfs2_io_error_bh(sdp, bh);
  494. get_bh(bh);
  495. }
  496. /**
  497. * gfs2_unpin - Unpin a buffer
  498. * @sdp: the filesystem the buffer belongs to
  499. * @bh: The buffer to unpin
  500. * @ai:
  501. *
  502. */
  503. void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  504. struct gfs2_ail *ai)
  505. {
  506. struct gfs2_bufdata *bd = get_v2bd(bh);
  507. gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
  508. if (!buffer_pinned(bh))
  509. gfs2_assert_withdraw(sdp, 0);
  510. mark_buffer_dirty(bh);
  511. clear_buffer_pinned(bh);
  512. gfs2_log_lock(sdp);
  513. if (bd->bd_ail) {
  514. list_del(&bd->bd_ail_st_list);
  515. brelse(bh);
  516. } else {
  517. struct gfs2_glock *gl = bd->bd_gl;
  518. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  519. atomic_inc(&gl->gl_ail_count);
  520. }
  521. bd->bd_ail = ai;
  522. list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  523. gfs2_log_unlock(sdp);
  524. }
  525. /**
  526. * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
  527. * @ip: the inode who owns the buffers
  528. * @bstart: the first buffer in the run
  529. * @blen: the number of buffers in the run
  530. *
  531. */
  532. void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  533. {
  534. struct gfs2_sbd *sdp = ip->i_sbd;
  535. struct inode *aspace = ip->i_gl->gl_aspace;
  536. struct buffer_head *bh;
  537. while (blen) {
  538. bh = getbuf(sdp, aspace, bstart, NO_CREATE);
  539. if (bh) {
  540. struct gfs2_bufdata *bd = get_v2bd(bh);
  541. if (test_clear_buffer_pinned(bh)) {
  542. gfs2_log_lock(sdp);
  543. list_del_init(&bd->bd_le.le_list);
  544. gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
  545. sdp->sd_log_num_buf--;
  546. gfs2_log_unlock(sdp);
  547. get_transaction->tr_num_buf_rm++;
  548. brelse(bh);
  549. }
  550. if (bd) {
  551. gfs2_log_lock(sdp);
  552. if (bd->bd_ail) {
  553. uint64_t blkno = bh->b_blocknr;
  554. bd->bd_ail = NULL;
  555. list_del(&bd->bd_ail_st_list);
  556. list_del(&bd->bd_ail_gl_list);
  557. atomic_dec(&bd->bd_gl->gl_ail_count);
  558. brelse(bh);
  559. gfs2_log_unlock(sdp);
  560. gfs2_trans_add_revoke(sdp, blkno);
  561. } else
  562. gfs2_log_unlock(sdp);
  563. }
  564. lock_buffer(bh);
  565. clear_buffer_dirty(bh);
  566. clear_buffer_uptodate(bh);
  567. unlock_buffer(bh);
  568. brelse(bh);
  569. }
  570. bstart++;
  571. blen--;
  572. }
  573. }
  574. /**
  575. * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
  576. * @ip: The GFS2 inode
  577. *
  578. * This releases buffers that are in the most-recently-used array of
  579. * blocks used for indirect block addressing for this inode.
  580. */
  581. void gfs2_meta_cache_flush(struct gfs2_inode *ip)
  582. {
  583. struct buffer_head **bh_slot;
  584. unsigned int x;
  585. spin_lock(&ip->i_spin);
  586. for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
  587. bh_slot = &ip->i_cache[x];
  588. if (!*bh_slot)
  589. break;
  590. brelse(*bh_slot);
  591. *bh_slot = NULL;
  592. }
  593. spin_unlock(&ip->i_spin);
  594. }
  595. /**
  596. * gfs2_meta_indirect_buffer - Get a metadata buffer
  597. * @ip: The GFS2 inode
  598. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  599. * @num: The block number (device relative) of the buffer
  600. * @new: Non-zero if we may create a new buffer
  601. * @bhp: the buffer is returned here
  602. *
  603. * Try to use the gfs2_inode's MRU metadata tree cache.
  604. *
  605. * Returns: errno
  606. */
  607. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
  608. int new, struct buffer_head **bhp)
  609. {
  610. struct buffer_head *bh, **bh_slot = ip->i_cache + height;
  611. int error;
  612. spin_lock(&ip->i_spin);
  613. bh = *bh_slot;
  614. if (bh) {
  615. if (bh->b_blocknr == num)
  616. get_bh(bh);
  617. else
  618. bh = NULL;
  619. }
  620. spin_unlock(&ip->i_spin);
  621. if (bh) {
  622. if (new)
  623. meta_prep_new(bh);
  624. else {
  625. error = gfs2_meta_reread(ip->i_sbd, bh,
  626. DIO_START | DIO_WAIT);
  627. if (error) {
  628. brelse(bh);
  629. return error;
  630. }
  631. }
  632. } else {
  633. if (new)
  634. bh = gfs2_meta_new(ip->i_gl, num);
  635. else {
  636. error = gfs2_meta_read(ip->i_gl, num,
  637. DIO_START | DIO_WAIT, &bh);
  638. if (error)
  639. return error;
  640. }
  641. spin_lock(&ip->i_spin);
  642. if (*bh_slot != bh) {
  643. brelse(*bh_slot);
  644. *bh_slot = bh;
  645. get_bh(bh);
  646. }
  647. spin_unlock(&ip->i_spin);
  648. }
  649. if (new) {
  650. if (gfs2_assert_warn(ip->i_sbd, height)) {
  651. brelse(bh);
  652. return -EIO;
  653. }
  654. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  655. gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  656. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  657. } else if (gfs2_metatype_check(ip->i_sbd, bh,
  658. (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
  659. brelse(bh);
  660. return -EIO;
  661. }
  662. *bhp = bh;
  663. return 0;
  664. }
  665. /**
  666. * gfs2_meta_ra - start readahead on an extent of a file
  667. * @gl: the glock the blocks belong to
  668. * @dblock: the starting disk block
  669. * @extlen: the number of blocks in the extent
  670. *
  671. */
  672. void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
  673. {
  674. struct gfs2_sbd *sdp = gl->gl_sbd;
  675. struct inode *aspace = gl->gl_aspace;
  676. struct buffer_head *first_bh, *bh;
  677. uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift;
  678. int error;
  679. if (!extlen || !max_ra)
  680. return;
  681. if (extlen > max_ra)
  682. extlen = max_ra;
  683. first_bh = getbuf(sdp, aspace, dblock, CREATE);
  684. if (buffer_uptodate(first_bh))
  685. goto out;
  686. if (!buffer_locked(first_bh)) {
  687. error = gfs2_meta_reread(sdp, first_bh, DIO_START);
  688. if (error)
  689. goto out;
  690. }
  691. dblock++;
  692. extlen--;
  693. while (extlen) {
  694. bh = getbuf(sdp, aspace, dblock, CREATE);
  695. if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
  696. error = gfs2_meta_reread(sdp, bh, DIO_START);
  697. brelse(bh);
  698. if (error)
  699. goto out;
  700. } else
  701. brelse(bh);
  702. dblock++;
  703. extlen--;
  704. if (buffer_uptodate(first_bh))
  705. break;
  706. }
  707. out:
  708. brelse(first_bh);
  709. }
  710. /**
  711. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  712. * @sdp: the filesystem
  713. *
  714. */
  715. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  716. {
  717. gfs2_log_flush(sdp);
  718. for (;;) {
  719. gfs2_ail1_start(sdp, DIO_ALL);
  720. if (gfs2_ail1_empty(sdp, DIO_ALL))
  721. break;
  722. msleep(100);
  723. }
  724. }