mdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 "nilfs.h"
  29. #include "segment.h"
  30. #include "page.h"
  31. #include "mdt.h"
  32. #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
  33. #define INIT_UNUSED_INODE_FIELDS
  34. static int
  35. nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
  36. struct buffer_head *bh,
  37. void (*init_block)(struct inode *,
  38. struct buffer_head *, void *))
  39. {
  40. struct nilfs_inode_info *ii = NILFS_I(inode);
  41. void *kaddr;
  42. int ret;
  43. /* Caller exclude read accesses using page lock */
  44. /* set_buffer_new(bh); */
  45. bh->b_blocknr = 0;
  46. ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
  47. if (unlikely(ret))
  48. return ret;
  49. set_buffer_mapped(bh);
  50. kaddr = kmap_atomic(bh->b_page, KM_USER0);
  51. memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
  52. if (init_block)
  53. init_block(inode, bh, kaddr);
  54. flush_dcache_page(bh->b_page);
  55. kunmap_atomic(kaddr, KM_USER0);
  56. set_buffer_uptodate(bh);
  57. nilfs_mark_buffer_dirty(bh);
  58. nilfs_mdt_mark_dirty(inode);
  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 the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
  68. struct super_block *sb = inode->i_sb;
  69. struct nilfs_transaction_info ti;
  70. struct buffer_head *bh;
  71. int err;
  72. if (!sb) {
  73. /*
  74. * Make sure this function is not called from any
  75. * read-only context.
  76. */
  77. if (!nilfs->ns_writer) {
  78. WARN_ON(1);
  79. err = -EROFS;
  80. goto out;
  81. }
  82. sb = nilfs->ns_writer->s_super;
  83. }
  84. nilfs_transaction_begin(sb, &ti, 0);
  85. err = -ENOMEM;
  86. bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
  87. if (unlikely(!bh))
  88. goto failed_unlock;
  89. err = -EEXIST;
  90. if (buffer_uptodate(bh) || buffer_mapped(bh))
  91. goto failed_bh;
  92. #if 0
  93. /* The uptodate flag is not protected by the page lock, but
  94. the mapped flag is. Thus, we don't have to wait the buffer. */
  95. wait_on_buffer(bh);
  96. if (buffer_uptodate(bh))
  97. goto failed_bh;
  98. #endif
  99. bh->b_bdev = nilfs->ns_bdev;
  100. err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
  101. if (likely(!err)) {
  102. get_bh(bh);
  103. *out_bh = bh;
  104. }
  105. failed_bh:
  106. unlock_page(bh->b_page);
  107. page_cache_release(bh->b_page);
  108. brelse(bh);
  109. failed_unlock:
  110. if (likely(!err))
  111. err = nilfs_transaction_commit(sb);
  112. else
  113. nilfs_transaction_abort(sb);
  114. out:
  115. return err;
  116. }
  117. static int
  118. nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
  119. int mode, struct buffer_head **out_bh)
  120. {
  121. struct buffer_head *bh;
  122. unsigned long blknum = 0;
  123. int ret = -ENOMEM;
  124. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  125. if (unlikely(!bh))
  126. goto failed;
  127. ret = -EEXIST; /* internal code */
  128. if (buffer_uptodate(bh))
  129. goto out;
  130. if (mode == READA) {
  131. if (!trylock_buffer(bh)) {
  132. ret = -EBUSY;
  133. goto failed_bh;
  134. }
  135. } else /* mode == READ */
  136. lock_buffer(bh);
  137. if (buffer_uptodate(bh)) {
  138. unlock_buffer(bh);
  139. goto out;
  140. }
  141. if (!buffer_mapped(bh)) { /* unused buffer */
  142. ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff,
  143. &blknum);
  144. if (unlikely(ret)) {
  145. unlock_buffer(bh);
  146. goto failed_bh;
  147. }
  148. bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev;
  149. bh->b_blocknr = blknum;
  150. set_buffer_mapped(bh);
  151. }
  152. bh->b_end_io = end_buffer_read_sync;
  153. get_bh(bh);
  154. submit_bh(mode, bh);
  155. ret = 0;
  156. out:
  157. get_bh(bh);
  158. *out_bh = bh;
  159. failed_bh:
  160. unlock_page(bh->b_page);
  161. page_cache_release(bh->b_page);
  162. brelse(bh);
  163. failed:
  164. return ret;
  165. }
  166. static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
  167. struct buffer_head **out_bh)
  168. {
  169. struct buffer_head *first_bh, *bh;
  170. unsigned long blkoff;
  171. int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
  172. int err;
  173. err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
  174. if (err == -EEXIST) /* internal code */
  175. goto out;
  176. if (unlikely(err))
  177. goto failed;
  178. blkoff = block + 1;
  179. for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
  180. err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
  181. if (likely(!err || err == -EEXIST))
  182. brelse(bh);
  183. else if (err != -EBUSY)
  184. break; /* abort readahead if bmap lookup failed */
  185. if (!buffer_locked(first_bh))
  186. goto out_no_wait;
  187. }
  188. wait_on_buffer(first_bh);
  189. out_no_wait:
  190. err = -EIO;
  191. if (!buffer_uptodate(first_bh))
  192. goto failed_bh;
  193. out:
  194. *out_bh = first_bh;
  195. return 0;
  196. failed_bh:
  197. brelse(first_bh);
  198. failed:
  199. return err;
  200. }
  201. /**
  202. * nilfs_mdt_get_block - read or create a buffer on meta data file.
  203. * @inode: inode of the meta data file
  204. * @blkoff: block offset
  205. * @create: create flag
  206. * @init_block: initializer used for newly allocated block
  207. * @out_bh: output of a pointer to the buffer_head
  208. *
  209. * nilfs_mdt_get_block() looks up the specified buffer and tries to create
  210. * a new buffer if @create is not zero. On success, the returned buffer is
  211. * assured to be either existing or formatted using a buffer lock on success.
  212. * @out_bh is substituted only when zero is returned.
  213. *
  214. * Return Value: On success, it returns 0. On error, the following negative
  215. * error code is returned.
  216. *
  217. * %-ENOMEM - Insufficient memory available.
  218. *
  219. * %-EIO - I/O error
  220. *
  221. * %-ENOENT - the specified block does not exist (hole block)
  222. *
  223. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  224. *
  225. * %-EROFS - Read only filesystem (for create mode)
  226. */
  227. int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
  228. void (*init_block)(struct inode *,
  229. struct buffer_head *, void *),
  230. struct buffer_head **out_bh)
  231. {
  232. int ret;
  233. /* Should be rewritten with merging nilfs_mdt_read_block() */
  234. retry:
  235. ret = nilfs_mdt_read_block(inode, blkoff, out_bh);
  236. if (!create || ret != -ENOENT)
  237. return ret;
  238. ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
  239. if (unlikely(ret == -EEXIST)) {
  240. /* create = 0; */ /* limit read-create loop retries */
  241. goto retry;
  242. }
  243. return ret;
  244. }
  245. /**
  246. * nilfs_mdt_delete_block - make a hole on the meta data file.
  247. * @inode: inode of the meta data file
  248. * @block: block offset
  249. *
  250. * Return Value: On success, zero is returned.
  251. * On error, one of the following negative error code is returned.
  252. *
  253. * %-ENOMEM - Insufficient memory available.
  254. *
  255. * %-EIO - I/O error
  256. *
  257. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  258. */
  259. int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
  260. {
  261. struct nilfs_inode_info *ii = NILFS_I(inode);
  262. int err;
  263. err = nilfs_bmap_delete(ii->i_bmap, block);
  264. if (!err || err == -ENOENT) {
  265. nilfs_mdt_mark_dirty(inode);
  266. nilfs_mdt_forget_block(inode, block);
  267. }
  268. return err;
  269. }
  270. /**
  271. * nilfs_mdt_forget_block - discard dirty state and try to remove the page
  272. * @inode: inode of the meta data file
  273. * @block: block offset
  274. *
  275. * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
  276. * tries to release the page including the buffer from a page cache.
  277. *
  278. * Return Value: On success, 0 is returned. On error, one of the following
  279. * negative error code is returned.
  280. *
  281. * %-EBUSY - page has an active buffer.
  282. *
  283. * %-ENOENT - page cache has no page addressed by the offset.
  284. */
  285. int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
  286. {
  287. pgoff_t index = (pgoff_t)block >>
  288. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  289. struct page *page;
  290. unsigned long first_block;
  291. int ret = 0;
  292. int still_dirty;
  293. page = find_lock_page(inode->i_mapping, index);
  294. if (!page)
  295. return -ENOENT;
  296. wait_on_page_writeback(page);
  297. first_block = (unsigned long)index <<
  298. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  299. if (page_has_buffers(page)) {
  300. struct buffer_head *bh;
  301. bh = nilfs_page_get_nth_block(page, block - first_block);
  302. nilfs_forget_buffer(bh);
  303. }
  304. still_dirty = PageDirty(page);
  305. unlock_page(page);
  306. page_cache_release(page);
  307. if (still_dirty ||
  308. invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
  309. ret = -EBUSY;
  310. return ret;
  311. }
  312. /**
  313. * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
  314. * @inode: inode of the meta data file
  315. * @block: block offset
  316. *
  317. * Return Value: On success, it returns 0. On error, the following negative
  318. * error code is returned.
  319. *
  320. * %-ENOMEM - Insufficient memory available.
  321. *
  322. * %-EIO - I/O error
  323. *
  324. * %-ENOENT - the specified block does not exist (hole block)
  325. *
  326. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  327. */
  328. int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
  329. {
  330. struct buffer_head *bh;
  331. int err;
  332. err = nilfs_mdt_read_block(inode, block, &bh);
  333. if (unlikely(err))
  334. return err;
  335. nilfs_mark_buffer_dirty(bh);
  336. nilfs_mdt_mark_dirty(inode);
  337. brelse(bh);
  338. return 0;
  339. }
  340. int nilfs_mdt_fetch_dirty(struct inode *inode)
  341. {
  342. struct nilfs_inode_info *ii = NILFS_I(inode);
  343. if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
  344. set_bit(NILFS_I_DIRTY, &ii->i_state);
  345. return 1;
  346. }
  347. return test_bit(NILFS_I_DIRTY, &ii->i_state);
  348. }
  349. static int
  350. nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
  351. {
  352. struct inode *inode = container_of(page->mapping,
  353. struct inode, i_data);
  354. struct super_block *sb = inode->i_sb;
  355. struct nilfs_sb_info *writer = NULL;
  356. int err = 0;
  357. redirty_page_for_writepage(wbc, page);
  358. unlock_page(page);
  359. if (page->mapping->assoc_mapping)
  360. return 0; /* Do not request flush for shadow page cache */
  361. if (!sb) {
  362. writer = nilfs_get_writer(NILFS_MDT(inode)->mi_nilfs);
  363. if (!writer)
  364. return -EROFS;
  365. sb = writer->s_super;
  366. }
  367. if (wbc->sync_mode == WB_SYNC_ALL)
  368. err = nilfs_construct_segment(sb);
  369. else if (wbc->for_reclaim)
  370. nilfs_flush_segment(sb, inode->i_ino);
  371. if (writer)
  372. nilfs_put_writer(NILFS_MDT(inode)->mi_nilfs);
  373. return err;
  374. }
  375. static struct address_space_operations def_mdt_aops = {
  376. .writepage = nilfs_mdt_write_page,
  377. };
  378. static struct inode_operations def_mdt_iops;
  379. static struct file_operations def_mdt_fops;
  380. /*
  381. * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
  382. * ifile, or gcinodes. This allows the B-tree code and segment constructor
  383. * to treat them like regular files, and this helps to simplify the
  384. * implementation.
  385. * On the other hand, some of the pseudo inodes have an irregular point:
  386. * They don't have valid inode->i_sb pointer because their lifetimes are
  387. * longer than those of the super block structs; they may continue for
  388. * several consecutive mounts/umounts. This would need discussions.
  389. */
  390. struct inode *
  391. nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
  392. ino_t ino, gfp_t gfp_mask)
  393. {
  394. struct inode *inode = nilfs_alloc_inode(sb);
  395. if (!inode)
  396. return NULL;
  397. else {
  398. struct address_space * const mapping = &inode->i_data;
  399. struct nilfs_mdt_info *mi = kzalloc(sizeof(*mi), GFP_NOFS);
  400. if (!mi) {
  401. nilfs_destroy_inode(inode);
  402. return NULL;
  403. }
  404. mi->mi_nilfs = nilfs;
  405. init_rwsem(&mi->mi_sem);
  406. inode->i_sb = sb; /* sb may be NULL for some meta data files */
  407. inode->i_blkbits = nilfs->ns_blocksize_bits;
  408. inode->i_flags = 0;
  409. atomic_set(&inode->i_count, 1);
  410. inode->i_nlink = 1;
  411. inode->i_ino = ino;
  412. inode->i_mode = S_IFREG;
  413. inode->i_private = mi;
  414. #ifdef INIT_UNUSED_INODE_FIELDS
  415. atomic_set(&inode->i_writecount, 0);
  416. inode->i_size = 0;
  417. inode->i_blocks = 0;
  418. inode->i_bytes = 0;
  419. inode->i_generation = 0;
  420. #ifdef CONFIG_QUOTA
  421. memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
  422. #endif
  423. inode->i_pipe = NULL;
  424. inode->i_bdev = NULL;
  425. inode->i_cdev = NULL;
  426. inode->i_rdev = 0;
  427. #ifdef CONFIG_SECURITY
  428. inode->i_security = NULL;
  429. #endif
  430. inode->dirtied_when = 0;
  431. INIT_LIST_HEAD(&inode->i_list);
  432. INIT_LIST_HEAD(&inode->i_sb_list);
  433. inode->i_state = 0;
  434. #endif
  435. spin_lock_init(&inode->i_lock);
  436. mutex_init(&inode->i_mutex);
  437. init_rwsem(&inode->i_alloc_sem);
  438. mapping->host = NULL; /* instead of inode */
  439. mapping->flags = 0;
  440. mapping_set_gfp_mask(mapping, gfp_mask);
  441. mapping->assoc_mapping = NULL;
  442. mapping->backing_dev_info = nilfs->ns_bdi;
  443. inode->i_mapping = mapping;
  444. }
  445. return inode;
  446. }
  447. struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
  448. ino_t ino, gfp_t gfp_mask)
  449. {
  450. struct inode *inode = nilfs_mdt_new_common(nilfs, sb, ino, gfp_mask);
  451. if (!inode)
  452. return NULL;
  453. inode->i_op = &def_mdt_iops;
  454. inode->i_fop = &def_mdt_fops;
  455. inode->i_mapping->a_ops = &def_mdt_aops;
  456. return inode;
  457. }
  458. void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
  459. unsigned header_size)
  460. {
  461. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  462. mi->mi_entry_size = entry_size;
  463. mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
  464. mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
  465. }
  466. void nilfs_mdt_set_shadow(struct inode *orig, struct inode *shadow)
  467. {
  468. shadow->i_mapping->assoc_mapping = orig->i_mapping;
  469. NILFS_I(shadow)->i_btnode_cache.assoc_mapping =
  470. &NILFS_I(orig)->i_btnode_cache;
  471. }
  472. void nilfs_mdt_clear(struct inode *inode)
  473. {
  474. struct nilfs_inode_info *ii = NILFS_I(inode);
  475. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  476. truncate_inode_pages(inode->i_mapping, 0);
  477. nilfs_bmap_clear(ii->i_bmap);
  478. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  479. }
  480. void nilfs_mdt_destroy(struct inode *inode)
  481. {
  482. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  483. kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
  484. kfree(mdi);
  485. nilfs_destroy_inode(inode);
  486. }