mdt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * mdt.c - meta data file for NILFS
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Written by Ryusuke Konishi.
  17. */
  18. #include <linux/buffer_head.h>
  19. #include <linux/mpage.h>
  20. #include <linux/mm.h>
  21. #include <linux/writeback.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/swap.h>
  24. #include <linux/slab.h>
  25. #include "nilfs.h"
  26. #include "btnode.h"
  27. #include "segment.h"
  28. #include "page.h"
  29. #include "mdt.h"
  30. #include "alloc.h" /* nilfs_palloc_destroy_cache() */
  31. #include <trace/events/nilfs2.h>
  32. #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
  33. static int
  34. nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
  35. struct buffer_head *bh,
  36. void (*init_block)(struct inode *,
  37. struct buffer_head *, void *))
  38. {
  39. struct nilfs_inode_info *ii = NILFS_I(inode);
  40. void *kaddr;
  41. int ret;
  42. /* Caller exclude read accesses using page lock */
  43. /* set_buffer_new(bh); */
  44. bh->b_blocknr = 0;
  45. ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
  46. if (unlikely(ret))
  47. return ret;
  48. set_buffer_mapped(bh);
  49. kaddr = kmap_atomic(bh->b_page);
  50. memset(kaddr + bh_offset(bh), 0, i_blocksize(inode));
  51. if (init_block)
  52. init_block(inode, bh, kaddr);
  53. flush_dcache_page(bh->b_page);
  54. kunmap_atomic(kaddr);
  55. set_buffer_uptodate(bh);
  56. mark_buffer_dirty(bh);
  57. nilfs_mdt_mark_dirty(inode);
  58. trace_nilfs2_mdt_insert_new_block(inode, inode->i_ino, block);
  59. return 0;
  60. }
  61. static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
  62. struct buffer_head **out_bh,
  63. void (*init_block)(struct inode *,
  64. struct buffer_head *,
  65. void *))
  66. {
  67. struct super_block *sb = inode->i_sb;
  68. struct nilfs_transaction_info ti;
  69. struct buffer_head *bh;
  70. int err;
  71. nilfs_transaction_begin(sb, &ti, 0);
  72. err = -ENOMEM;
  73. bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
  74. if (unlikely(!bh))
  75. goto failed_unlock;
  76. err = -EEXIST;
  77. if (buffer_uptodate(bh))
  78. goto failed_bh;
  79. wait_on_buffer(bh);
  80. if (buffer_uptodate(bh))
  81. goto failed_bh;
  82. bh->b_bdev = sb->s_bdev;
  83. err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
  84. if (likely(!err)) {
  85. get_bh(bh);
  86. *out_bh = bh;
  87. }
  88. failed_bh:
  89. unlock_page(bh->b_page);
  90. put_page(bh->b_page);
  91. brelse(bh);
  92. failed_unlock:
  93. if (likely(!err))
  94. err = nilfs_transaction_commit(sb);
  95. else
  96. nilfs_transaction_abort(sb);
  97. return err;
  98. }
  99. static int
  100. nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
  101. int mode, int mode_flags, struct buffer_head **out_bh)
  102. {
  103. struct buffer_head *bh;
  104. __u64 blknum = 0;
  105. int ret = -ENOMEM;
  106. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  107. if (unlikely(!bh))
  108. goto failed;
  109. ret = -EEXIST; /* internal code */
  110. if (buffer_uptodate(bh))
  111. goto out;
  112. if (mode_flags & REQ_RAHEAD) {
  113. if (!trylock_buffer(bh)) {
  114. ret = -EBUSY;
  115. goto failed_bh;
  116. }
  117. } else /* mode == READ */
  118. lock_buffer(bh);
  119. if (buffer_uptodate(bh)) {
  120. unlock_buffer(bh);
  121. goto out;
  122. }
  123. ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
  124. if (unlikely(ret)) {
  125. unlock_buffer(bh);
  126. goto failed_bh;
  127. }
  128. map_bh(bh, inode->i_sb, (sector_t)blknum);
  129. bh->b_end_io = end_buffer_read_sync;
  130. get_bh(bh);
  131. submit_bh(mode, mode_flags, bh);
  132. ret = 0;
  133. trace_nilfs2_mdt_submit_block(inode, inode->i_ino, blkoff, mode);
  134. out:
  135. get_bh(bh);
  136. *out_bh = bh;
  137. failed_bh:
  138. unlock_page(bh->b_page);
  139. put_page(bh->b_page);
  140. brelse(bh);
  141. failed:
  142. return ret;
  143. }
  144. static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
  145. int readahead, struct buffer_head **out_bh)
  146. {
  147. struct buffer_head *first_bh, *bh;
  148. unsigned long blkoff;
  149. int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
  150. int err;
  151. err = nilfs_mdt_submit_block(inode, block, REQ_OP_READ, 0, &first_bh);
  152. if (err == -EEXIST) /* internal code */
  153. goto out;
  154. if (unlikely(err))
  155. goto failed;
  156. if (readahead) {
  157. blkoff = block + 1;
  158. for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
  159. err = nilfs_mdt_submit_block(inode, blkoff, REQ_OP_READ,
  160. REQ_RAHEAD, &bh);
  161. if (likely(!err || err == -EEXIST))
  162. brelse(bh);
  163. else if (err != -EBUSY)
  164. break;
  165. /* abort readahead if bmap lookup failed */
  166. if (!buffer_locked(first_bh))
  167. goto out_no_wait;
  168. }
  169. }
  170. wait_on_buffer(first_bh);
  171. out_no_wait:
  172. err = -EIO;
  173. if (!buffer_uptodate(first_bh)) {
  174. nilfs_msg(inode->i_sb, KERN_ERR,
  175. "I/O error reading meta-data file (ino=%lu, block-offset=%lu)",
  176. inode->i_ino, block);
  177. goto failed_bh;
  178. }
  179. out:
  180. *out_bh = first_bh;
  181. return 0;
  182. failed_bh:
  183. brelse(first_bh);
  184. failed:
  185. return err;
  186. }
  187. /**
  188. * nilfs_mdt_get_block - read or create a buffer on meta data file.
  189. * @inode: inode of the meta data file
  190. * @blkoff: block offset
  191. * @create: create flag
  192. * @init_block: initializer used for newly allocated block
  193. * @out_bh: output of a pointer to the buffer_head
  194. *
  195. * nilfs_mdt_get_block() looks up the specified buffer and tries to create
  196. * a new buffer if @create is not zero. On success, the returned buffer is
  197. * assured to be either existing or formatted using a buffer lock on success.
  198. * @out_bh is substituted only when zero is returned.
  199. *
  200. * Return Value: On success, it returns 0. On error, the following negative
  201. * error code is returned.
  202. *
  203. * %-ENOMEM - Insufficient memory available.
  204. *
  205. * %-EIO - I/O error
  206. *
  207. * %-ENOENT - the specified block does not exist (hole block)
  208. *
  209. * %-EROFS - Read only filesystem (for create mode)
  210. */
  211. int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
  212. void (*init_block)(struct inode *,
  213. struct buffer_head *, void *),
  214. struct buffer_head **out_bh)
  215. {
  216. int ret;
  217. /* Should be rewritten with merging nilfs_mdt_read_block() */
  218. retry:
  219. ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
  220. if (!create || ret != -ENOENT)
  221. return ret;
  222. ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
  223. if (unlikely(ret == -EEXIST)) {
  224. /* create = 0; */ /* limit read-create loop retries */
  225. goto retry;
  226. }
  227. return ret;
  228. }
  229. /**
  230. * nilfs_mdt_find_block - find and get a buffer on meta data file.
  231. * @inode: inode of the meta data file
  232. * @start: start block offset (inclusive)
  233. * @end: end block offset (inclusive)
  234. * @blkoff: block offset
  235. * @out_bh: place to store a pointer to buffer_head struct
  236. *
  237. * nilfs_mdt_find_block() looks up an existing block in range of
  238. * [@start, @end] and stores pointer to a buffer head of the block to
  239. * @out_bh, and block offset to @blkoff, respectively. @out_bh and
  240. * @blkoff are substituted only when zero is returned.
  241. *
  242. * Return Value: On success, it returns 0. On error, the following negative
  243. * error code is returned.
  244. *
  245. * %-ENOMEM - Insufficient memory available.
  246. *
  247. * %-EIO - I/O error
  248. *
  249. * %-ENOENT - no block was found in the range
  250. */
  251. int nilfs_mdt_find_block(struct inode *inode, unsigned long start,
  252. unsigned long end, unsigned long *blkoff,
  253. struct buffer_head **out_bh)
  254. {
  255. __u64 next;
  256. int ret;
  257. if (unlikely(start > end))
  258. return -ENOENT;
  259. ret = nilfs_mdt_read_block(inode, start, true, out_bh);
  260. if (!ret) {
  261. *blkoff = start;
  262. goto out;
  263. }
  264. if (unlikely(ret != -ENOENT || start == ULONG_MAX))
  265. goto out;
  266. ret = nilfs_bmap_seek_key(NILFS_I(inode)->i_bmap, start + 1, &next);
  267. if (!ret) {
  268. if (next <= end) {
  269. ret = nilfs_mdt_read_block(inode, next, true, out_bh);
  270. if (!ret)
  271. *blkoff = next;
  272. } else {
  273. ret = -ENOENT;
  274. }
  275. }
  276. out:
  277. return ret;
  278. }
  279. /**
  280. * nilfs_mdt_delete_block - make a hole on the meta data file.
  281. * @inode: inode of the meta data file
  282. * @block: block offset
  283. *
  284. * Return Value: On success, zero is returned.
  285. * On error, one of the following negative error code is returned.
  286. *
  287. * %-ENOMEM - Insufficient memory available.
  288. *
  289. * %-EIO - I/O error
  290. */
  291. int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
  292. {
  293. struct nilfs_inode_info *ii = NILFS_I(inode);
  294. int err;
  295. err = nilfs_bmap_delete(ii->i_bmap, block);
  296. if (!err || err == -ENOENT) {
  297. nilfs_mdt_mark_dirty(inode);
  298. nilfs_mdt_forget_block(inode, block);
  299. }
  300. return err;
  301. }
  302. /**
  303. * nilfs_mdt_forget_block - discard dirty state and try to remove the page
  304. * @inode: inode of the meta data file
  305. * @block: block offset
  306. *
  307. * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
  308. * tries to release the page including the buffer from a page cache.
  309. *
  310. * Return Value: On success, 0 is returned. On error, one of the following
  311. * negative error code is returned.
  312. *
  313. * %-EBUSY - page has an active buffer.
  314. *
  315. * %-ENOENT - page cache has no page addressed by the offset.
  316. */
  317. int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
  318. {
  319. pgoff_t index = (pgoff_t)block >>
  320. (PAGE_SHIFT - inode->i_blkbits);
  321. struct page *page;
  322. unsigned long first_block;
  323. int ret = 0;
  324. int still_dirty;
  325. page = find_lock_page(inode->i_mapping, index);
  326. if (!page)
  327. return -ENOENT;
  328. wait_on_page_writeback(page);
  329. first_block = (unsigned long)index <<
  330. (PAGE_SHIFT - inode->i_blkbits);
  331. if (page_has_buffers(page)) {
  332. struct buffer_head *bh;
  333. bh = nilfs_page_get_nth_block(page, block - first_block);
  334. nilfs_forget_buffer(bh);
  335. }
  336. still_dirty = PageDirty(page);
  337. unlock_page(page);
  338. put_page(page);
  339. if (still_dirty ||
  340. invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
  341. ret = -EBUSY;
  342. return ret;
  343. }
  344. int nilfs_mdt_fetch_dirty(struct inode *inode)
  345. {
  346. struct nilfs_inode_info *ii = NILFS_I(inode);
  347. if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
  348. set_bit(NILFS_I_DIRTY, &ii->i_state);
  349. return 1;
  350. }
  351. return test_bit(NILFS_I_DIRTY, &ii->i_state);
  352. }
  353. static int
  354. nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
  355. {
  356. struct inode *inode = page->mapping->host;
  357. struct super_block *sb;
  358. int err = 0;
  359. if (inode && (inode->i_sb->s_flags & MS_RDONLY)) {
  360. /*
  361. * It means that filesystem was remounted in read-only
  362. * mode because of error or metadata corruption. But we
  363. * have dirty pages that try to be flushed in background.
  364. * So, here we simply discard this dirty page.
  365. */
  366. nilfs_clear_dirty_page(page, false);
  367. unlock_page(page);
  368. return -EROFS;
  369. }
  370. redirty_page_for_writepage(wbc, page);
  371. unlock_page(page);
  372. if (!inode)
  373. return 0;
  374. sb = inode->i_sb;
  375. if (wbc->sync_mode == WB_SYNC_ALL)
  376. err = nilfs_construct_segment(sb);
  377. else if (wbc->for_reclaim)
  378. nilfs_flush_segment(sb, inode->i_ino);
  379. return err;
  380. }
  381. static const struct address_space_operations def_mdt_aops = {
  382. .writepage = nilfs_mdt_write_page,
  383. };
  384. static const struct inode_operations def_mdt_iops;
  385. static const struct file_operations def_mdt_fops;
  386. int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
  387. {
  388. struct nilfs_mdt_info *mi;
  389. mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
  390. if (!mi)
  391. return -ENOMEM;
  392. init_rwsem(&mi->mi_sem);
  393. inode->i_private = mi;
  394. inode->i_mode = S_IFREG;
  395. mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
  396. inode->i_op = &def_mdt_iops;
  397. inode->i_fop = &def_mdt_fops;
  398. inode->i_mapping->a_ops = &def_mdt_aops;
  399. return 0;
  400. }
  401. /**
  402. * nilfs_mdt_clear - do cleanup for the metadata file
  403. * @inode: inode of the metadata file
  404. */
  405. void nilfs_mdt_clear(struct inode *inode)
  406. {
  407. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  408. if (mdi->mi_palloc_cache)
  409. nilfs_palloc_destroy_cache(inode);
  410. }
  411. /**
  412. * nilfs_mdt_destroy - release resources used by the metadata file
  413. * @inode: inode of the metadata file
  414. */
  415. void nilfs_mdt_destroy(struct inode *inode)
  416. {
  417. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  418. kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
  419. kfree(mdi);
  420. }
  421. void nilfs_mdt_set_entry_size(struct inode *inode, unsigned int entry_size,
  422. unsigned int header_size)
  423. {
  424. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  425. mi->mi_entry_size = entry_size;
  426. mi->mi_entries_per_block = i_blocksize(inode) / entry_size;
  427. mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
  428. }
  429. /**
  430. * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
  431. * @inode: inode of the metadata file
  432. * @shadow: shadow mapping
  433. */
  434. int nilfs_mdt_setup_shadow_map(struct inode *inode,
  435. struct nilfs_shadow_map *shadow)
  436. {
  437. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  438. INIT_LIST_HEAD(&shadow->frozen_buffers);
  439. address_space_init_once(&shadow->frozen_data);
  440. nilfs_mapping_init(&shadow->frozen_data, inode);
  441. address_space_init_once(&shadow->frozen_btnodes);
  442. nilfs_mapping_init(&shadow->frozen_btnodes, inode);
  443. mi->mi_shadow = shadow;
  444. return 0;
  445. }
  446. /**
  447. * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
  448. * @inode: inode of the metadata file
  449. */
  450. int nilfs_mdt_save_to_shadow_map(struct inode *inode)
  451. {
  452. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  453. struct nilfs_inode_info *ii = NILFS_I(inode);
  454. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  455. int ret;
  456. ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
  457. if (ret)
  458. goto out;
  459. ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
  460. &ii->i_btnode_cache);
  461. if (ret)
  462. goto out;
  463. nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
  464. out:
  465. return ret;
  466. }
  467. int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
  468. {
  469. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  470. struct buffer_head *bh_frozen;
  471. struct page *page;
  472. int blkbits = inode->i_blkbits;
  473. page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
  474. if (!page)
  475. return -ENOMEM;
  476. if (!page_has_buffers(page))
  477. create_empty_buffers(page, 1 << blkbits, 0);
  478. bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
  479. if (!buffer_uptodate(bh_frozen))
  480. nilfs_copy_buffer(bh_frozen, bh);
  481. if (list_empty(&bh_frozen->b_assoc_buffers)) {
  482. list_add_tail(&bh_frozen->b_assoc_buffers,
  483. &shadow->frozen_buffers);
  484. set_buffer_nilfs_redirected(bh);
  485. } else {
  486. brelse(bh_frozen); /* already frozen */
  487. }
  488. unlock_page(page);
  489. put_page(page);
  490. return 0;
  491. }
  492. struct buffer_head *
  493. nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
  494. {
  495. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  496. struct buffer_head *bh_frozen = NULL;
  497. struct page *page;
  498. int n;
  499. page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
  500. if (page) {
  501. if (page_has_buffers(page)) {
  502. n = bh_offset(bh) >> inode->i_blkbits;
  503. bh_frozen = nilfs_page_get_nth_block(page, n);
  504. }
  505. unlock_page(page);
  506. put_page(page);
  507. }
  508. return bh_frozen;
  509. }
  510. static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
  511. {
  512. struct list_head *head = &shadow->frozen_buffers;
  513. struct buffer_head *bh;
  514. while (!list_empty(head)) {
  515. bh = list_first_entry(head, struct buffer_head,
  516. b_assoc_buffers);
  517. list_del_init(&bh->b_assoc_buffers);
  518. brelse(bh); /* drop ref-count to make it releasable */
  519. }
  520. }
  521. /**
  522. * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
  523. * @inode: inode of the metadata file
  524. */
  525. void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
  526. {
  527. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  528. struct nilfs_inode_info *ii = NILFS_I(inode);
  529. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  530. down_write(&mi->mi_sem);
  531. if (mi->mi_palloc_cache)
  532. nilfs_palloc_clear_cache(inode);
  533. nilfs_clear_dirty_pages(inode->i_mapping, true);
  534. nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
  535. nilfs_clear_dirty_pages(&ii->i_btnode_cache, true);
  536. nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
  537. nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
  538. up_write(&mi->mi_sem);
  539. }
  540. /**
  541. * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
  542. * @inode: inode of the metadata file
  543. */
  544. void nilfs_mdt_clear_shadow_map(struct inode *inode)
  545. {
  546. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  547. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  548. down_write(&mi->mi_sem);
  549. nilfs_release_frozen_buffers(shadow);
  550. truncate_inode_pages(&shadow->frozen_data, 0);
  551. truncate_inode_pages(&shadow->frozen_btnodes, 0);
  552. up_write(&mi->mi_sem);
  553. }