meta_io.c 19 KB

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