mdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Ryusuke Konishi <ryusuke@osrg.net>
  21. */
  22. #include <linux/buffer_head.h>
  23. #include <linux/mpage.h>
  24. #include <linux/mm.h>
  25. #include <linux/writeback.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/swap.h>
  28. #include <linux/slab.h>
  29. #include "nilfs.h"
  30. #include "btnode.h"
  31. #include "segment.h"
  32. #include "page.h"
  33. #include "mdt.h"
  34. #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
  35. static int
  36. nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
  37. struct buffer_head *bh,
  38. void (*init_block)(struct inode *,
  39. struct buffer_head *, void *))
  40. {
  41. struct nilfs_inode_info *ii = NILFS_I(inode);
  42. void *kaddr;
  43. int ret;
  44. /* Caller exclude read accesses using page lock */
  45. /* set_buffer_new(bh); */
  46. bh->b_blocknr = 0;
  47. ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
  48. if (unlikely(ret))
  49. return ret;
  50. set_buffer_mapped(bh);
  51. kaddr = kmap_atomic(bh->b_page, KM_USER0);
  52. memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
  53. if (init_block)
  54. init_block(inode, bh, kaddr);
  55. flush_dcache_page(bh->b_page);
  56. kunmap_atomic(kaddr, KM_USER0);
  57. set_buffer_uptodate(bh);
  58. mark_buffer_dirty(bh);
  59. nilfs_mdt_mark_dirty(inode);
  60. return 0;
  61. }
  62. static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
  63. struct buffer_head **out_bh,
  64. void (*init_block)(struct inode *,
  65. struct buffer_head *,
  66. void *))
  67. {
  68. struct super_block *sb = inode->i_sb;
  69. struct nilfs_transaction_info ti;
  70. struct buffer_head *bh;
  71. int err;
  72. nilfs_transaction_begin(sb, &ti, 0);
  73. err = -ENOMEM;
  74. bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
  75. if (unlikely(!bh))
  76. goto failed_unlock;
  77. err = -EEXIST;
  78. if (buffer_uptodate(bh))
  79. goto failed_bh;
  80. wait_on_buffer(bh);
  81. if (buffer_uptodate(bh))
  82. goto failed_bh;
  83. bh->b_bdev = sb->s_bdev;
  84. err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
  85. if (likely(!err)) {
  86. get_bh(bh);
  87. *out_bh = bh;
  88. }
  89. failed_bh:
  90. unlock_page(bh->b_page);
  91. page_cache_release(bh->b_page);
  92. brelse(bh);
  93. failed_unlock:
  94. if (likely(!err))
  95. err = nilfs_transaction_commit(sb);
  96. else
  97. nilfs_transaction_abort(sb);
  98. return err;
  99. }
  100. static int
  101. nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
  102. int mode, struct buffer_head **out_bh)
  103. {
  104. struct buffer_head *bh;
  105. __u64 blknum = 0;
  106. int ret = -ENOMEM;
  107. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  108. if (unlikely(!bh))
  109. goto failed;
  110. ret = -EEXIST; /* internal code */
  111. if (buffer_uptodate(bh))
  112. goto out;
  113. if (mode == READA) {
  114. if (!trylock_buffer(bh)) {
  115. ret = -EBUSY;
  116. goto failed_bh;
  117. }
  118. } else /* mode == READ */
  119. lock_buffer(bh);
  120. if (buffer_uptodate(bh)) {
  121. unlock_buffer(bh);
  122. goto out;
  123. }
  124. ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
  125. if (unlikely(ret)) {
  126. unlock_buffer(bh);
  127. goto failed_bh;
  128. }
  129. map_bh(bh, inode->i_sb, (sector_t)blknum);
  130. bh->b_end_io = end_buffer_read_sync;
  131. get_bh(bh);
  132. submit_bh(mode, bh);
  133. ret = 0;
  134. out:
  135. get_bh(bh);
  136. *out_bh = bh;
  137. failed_bh:
  138. unlock_page(bh->b_page);
  139. page_cache_release(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, READ, &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, READA, &bh);
  160. if (likely(!err || err == -EEXIST))
  161. brelse(bh);
  162. else if (err != -EBUSY)
  163. break;
  164. /* abort readahead if bmap lookup failed */
  165. if (!buffer_locked(first_bh))
  166. goto out_no_wait;
  167. }
  168. }
  169. wait_on_buffer(first_bh);
  170. out_no_wait:
  171. err = -EIO;
  172. if (!buffer_uptodate(first_bh))
  173. goto failed_bh;
  174. out:
  175. *out_bh = first_bh;
  176. return 0;
  177. failed_bh:
  178. brelse(first_bh);
  179. failed:
  180. return err;
  181. }
  182. /**
  183. * nilfs_mdt_get_block - read or create a buffer on meta data file.
  184. * @inode: inode of the meta data file
  185. * @blkoff: block offset
  186. * @create: create flag
  187. * @init_block: initializer used for newly allocated block
  188. * @out_bh: output of a pointer to the buffer_head
  189. *
  190. * nilfs_mdt_get_block() looks up the specified buffer and tries to create
  191. * a new buffer if @create is not zero. On success, the returned buffer is
  192. * assured to be either existing or formatted using a buffer lock on success.
  193. * @out_bh is substituted only when zero is returned.
  194. *
  195. * Return Value: On success, it returns 0. On error, the following negative
  196. * error code is returned.
  197. *
  198. * %-ENOMEM - Insufficient memory available.
  199. *
  200. * %-EIO - I/O error
  201. *
  202. * %-ENOENT - the specified block does not exist (hole block)
  203. *
  204. * %-EROFS - Read only filesystem (for create mode)
  205. */
  206. int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
  207. void (*init_block)(struct inode *,
  208. struct buffer_head *, void *),
  209. struct buffer_head **out_bh)
  210. {
  211. int ret;
  212. /* Should be rewritten with merging nilfs_mdt_read_block() */
  213. retry:
  214. ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
  215. if (!create || ret != -ENOENT)
  216. return ret;
  217. ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
  218. if (unlikely(ret == -EEXIST)) {
  219. /* create = 0; */ /* limit read-create loop retries */
  220. goto retry;
  221. }
  222. return ret;
  223. }
  224. /**
  225. * nilfs_mdt_delete_block - make a hole on the meta data file.
  226. * @inode: inode of the meta data file
  227. * @block: block offset
  228. *
  229. * Return Value: On success, zero is returned.
  230. * On error, one of the following negative error code is returned.
  231. *
  232. * %-ENOMEM - Insufficient memory available.
  233. *
  234. * %-EIO - I/O error
  235. */
  236. int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
  237. {
  238. struct nilfs_inode_info *ii = NILFS_I(inode);
  239. int err;
  240. err = nilfs_bmap_delete(ii->i_bmap, block);
  241. if (!err || err == -ENOENT) {
  242. nilfs_mdt_mark_dirty(inode);
  243. nilfs_mdt_forget_block(inode, block);
  244. }
  245. return err;
  246. }
  247. /**
  248. * nilfs_mdt_forget_block - discard dirty state and try to remove the page
  249. * @inode: inode of the meta data file
  250. * @block: block offset
  251. *
  252. * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
  253. * tries to release the page including the buffer from a page cache.
  254. *
  255. * Return Value: On success, 0 is returned. On error, one of the following
  256. * negative error code is returned.
  257. *
  258. * %-EBUSY - page has an active buffer.
  259. *
  260. * %-ENOENT - page cache has no page addressed by the offset.
  261. */
  262. int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
  263. {
  264. pgoff_t index = (pgoff_t)block >>
  265. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  266. struct page *page;
  267. unsigned long first_block;
  268. int ret = 0;
  269. int still_dirty;
  270. page = find_lock_page(inode->i_mapping, index);
  271. if (!page)
  272. return -ENOENT;
  273. wait_on_page_writeback(page);
  274. first_block = (unsigned long)index <<
  275. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  276. if (page_has_buffers(page)) {
  277. struct buffer_head *bh;
  278. bh = nilfs_page_get_nth_block(page, block - first_block);
  279. nilfs_forget_buffer(bh);
  280. }
  281. still_dirty = PageDirty(page);
  282. unlock_page(page);
  283. page_cache_release(page);
  284. if (still_dirty ||
  285. invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
  286. ret = -EBUSY;
  287. return ret;
  288. }
  289. /**
  290. * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
  291. * @inode: inode of the meta data file
  292. * @block: block offset
  293. *
  294. * Return Value: On success, it returns 0. On error, the following negative
  295. * error code is returned.
  296. *
  297. * %-ENOMEM - Insufficient memory available.
  298. *
  299. * %-EIO - I/O error
  300. *
  301. * %-ENOENT - the specified block does not exist (hole block)
  302. */
  303. int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
  304. {
  305. struct buffer_head *bh;
  306. int err;
  307. err = nilfs_mdt_read_block(inode, block, 0, &bh);
  308. if (unlikely(err))
  309. return err;
  310. mark_buffer_dirty(bh);
  311. nilfs_mdt_mark_dirty(inode);
  312. brelse(bh);
  313. return 0;
  314. }
  315. int nilfs_mdt_fetch_dirty(struct inode *inode)
  316. {
  317. struct nilfs_inode_info *ii = NILFS_I(inode);
  318. if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
  319. set_bit(NILFS_I_DIRTY, &ii->i_state);
  320. return 1;
  321. }
  322. return test_bit(NILFS_I_DIRTY, &ii->i_state);
  323. }
  324. static int
  325. nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
  326. {
  327. struct inode *inode;
  328. struct super_block *sb;
  329. int err = 0;
  330. redirty_page_for_writepage(wbc, page);
  331. unlock_page(page);
  332. inode = page->mapping->host;
  333. if (!inode)
  334. return 0;
  335. sb = inode->i_sb;
  336. if (wbc->sync_mode == WB_SYNC_ALL)
  337. err = nilfs_construct_segment(sb);
  338. else if (wbc->for_reclaim)
  339. nilfs_flush_segment(sb, inode->i_ino);
  340. return err;
  341. }
  342. static const struct address_space_operations def_mdt_aops = {
  343. .writepage = nilfs_mdt_write_page,
  344. };
  345. static const struct inode_operations def_mdt_iops;
  346. static const struct file_operations def_mdt_fops;
  347. int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
  348. {
  349. struct nilfs_mdt_info *mi;
  350. mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
  351. if (!mi)
  352. return -ENOMEM;
  353. init_rwsem(&mi->mi_sem);
  354. inode->i_private = mi;
  355. inode->i_mode = S_IFREG;
  356. mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
  357. inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
  358. inode->i_op = &def_mdt_iops;
  359. inode->i_fop = &def_mdt_fops;
  360. inode->i_mapping->a_ops = &def_mdt_aops;
  361. return 0;
  362. }
  363. void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
  364. unsigned header_size)
  365. {
  366. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  367. mi->mi_entry_size = entry_size;
  368. mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
  369. mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
  370. }
  371. /**
  372. * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
  373. * @inode: inode of the metadata file
  374. * @shadow: shadow mapping
  375. */
  376. int nilfs_mdt_setup_shadow_map(struct inode *inode,
  377. struct nilfs_shadow_map *shadow)
  378. {
  379. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  380. struct backing_dev_info *bdi = inode->i_sb->s_bdi;
  381. INIT_LIST_HEAD(&shadow->frozen_buffers);
  382. address_space_init_once(&shadow->frozen_data);
  383. nilfs_mapping_init(&shadow->frozen_data, inode, bdi);
  384. address_space_init_once(&shadow->frozen_btnodes);
  385. nilfs_mapping_init(&shadow->frozen_btnodes, inode, bdi);
  386. mi->mi_shadow = shadow;
  387. return 0;
  388. }
  389. /**
  390. * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
  391. * @inode: inode of the metadata file
  392. */
  393. int nilfs_mdt_save_to_shadow_map(struct inode *inode)
  394. {
  395. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  396. struct nilfs_inode_info *ii = NILFS_I(inode);
  397. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  398. int ret;
  399. ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
  400. if (ret)
  401. goto out;
  402. ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
  403. &ii->i_btnode_cache);
  404. if (ret)
  405. goto out;
  406. nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
  407. out:
  408. return ret;
  409. }
  410. int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
  411. {
  412. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  413. struct buffer_head *bh_frozen;
  414. struct page *page;
  415. int blkbits = inode->i_blkbits;
  416. page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
  417. if (!page)
  418. return -ENOMEM;
  419. if (!page_has_buffers(page))
  420. create_empty_buffers(page, 1 << blkbits, 0);
  421. bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
  422. if (!buffer_uptodate(bh_frozen))
  423. nilfs_copy_buffer(bh_frozen, bh);
  424. if (list_empty(&bh_frozen->b_assoc_buffers)) {
  425. list_add_tail(&bh_frozen->b_assoc_buffers,
  426. &shadow->frozen_buffers);
  427. set_buffer_nilfs_redirected(bh);
  428. } else {
  429. brelse(bh_frozen); /* already frozen */
  430. }
  431. unlock_page(page);
  432. page_cache_release(page);
  433. return 0;
  434. }
  435. struct buffer_head *
  436. nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
  437. {
  438. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  439. struct buffer_head *bh_frozen = NULL;
  440. struct page *page;
  441. int n;
  442. page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
  443. if (page) {
  444. if (page_has_buffers(page)) {
  445. n = bh_offset(bh) >> inode->i_blkbits;
  446. bh_frozen = nilfs_page_get_nth_block(page, n);
  447. }
  448. unlock_page(page);
  449. page_cache_release(page);
  450. }
  451. return bh_frozen;
  452. }
  453. static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
  454. {
  455. struct list_head *head = &shadow->frozen_buffers;
  456. struct buffer_head *bh;
  457. while (!list_empty(head)) {
  458. bh = list_first_entry(head, struct buffer_head,
  459. b_assoc_buffers);
  460. list_del_init(&bh->b_assoc_buffers);
  461. brelse(bh); /* drop ref-count to make it releasable */
  462. }
  463. }
  464. /**
  465. * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
  466. * @inode: inode of the metadata file
  467. */
  468. void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
  469. {
  470. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  471. struct nilfs_inode_info *ii = NILFS_I(inode);
  472. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  473. down_write(&mi->mi_sem);
  474. if (mi->mi_palloc_cache)
  475. nilfs_palloc_clear_cache(inode);
  476. nilfs_clear_dirty_pages(inode->i_mapping);
  477. nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
  478. nilfs_clear_dirty_pages(&ii->i_btnode_cache);
  479. nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
  480. nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
  481. up_write(&mi->mi_sem);
  482. }
  483. /**
  484. * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
  485. * @inode: inode of the metadata file
  486. */
  487. void nilfs_mdt_clear_shadow_map(struct inode *inode)
  488. {
  489. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  490. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  491. down_write(&mi->mi_sem);
  492. nilfs_release_frozen_buffers(shadow);
  493. truncate_inode_pages(&shadow->frozen_data, 0);
  494. truncate_inode_pages(&shadow->frozen_btnodes, 0);
  495. up_write(&mi->mi_sem);
  496. }