meta_io.c 19 KB

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