2 * mdt.c - meta data file for NILFS
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
23 #include <linux/buffer_head.h>
24 #include <linux/mpage.h>
26 #include <linux/writeback.h>
27 #include <linux/backing-dev.h>
28 #include <linux/swap.h>
29 #include <linux/slab.h>
37 #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
39 #define INIT_UNUSED_INODE_FIELDS
42 nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
43 struct buffer_head *bh,
44 void (*init_block)(struct inode *,
45 struct buffer_head *, void *))
47 struct nilfs_inode_info *ii = NILFS_I(inode);
51 /* Caller exclude read accesses using page lock */
53 /* set_buffer_new(bh); */
56 ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
60 set_buffer_mapped(bh);
62 kaddr = kmap_atomic(bh->b_page, KM_USER0);
63 memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
65 init_block(inode, bh, kaddr);
66 flush_dcache_page(bh->b_page);
67 kunmap_atomic(kaddr, KM_USER0);
69 set_buffer_uptodate(bh);
70 nilfs_mark_buffer_dirty(bh);
71 nilfs_mdt_mark_dirty(inode);
75 static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
76 struct buffer_head **out_bh,
77 void (*init_block)(struct inode *,
81 struct super_block *sb = inode->i_sb;
82 struct nilfs_transaction_info ti;
83 struct buffer_head *bh;
86 nilfs_transaction_begin(sb, &ti, 0);
89 bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
94 if (buffer_uptodate(bh))
98 if (buffer_uptodate(bh))
101 bh->b_bdev = sb->s_bdev;
102 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
109 unlock_page(bh->b_page);
110 page_cache_release(bh->b_page);
115 err = nilfs_transaction_commit(sb);
117 nilfs_transaction_abort(sb);
123 nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
124 int mode, struct buffer_head **out_bh)
126 struct buffer_head *bh;
130 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
134 ret = -EEXIST; /* internal code */
135 if (buffer_uptodate(bh))
139 if (!trylock_buffer(bh)) {
143 } else /* mode == READ */
146 if (buffer_uptodate(bh)) {
151 ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
156 map_bh(bh, inode->i_sb, (sector_t)blknum);
158 bh->b_end_io = end_buffer_read_sync;
167 unlock_page(bh->b_page);
168 page_cache_release(bh->b_page);
174 static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
175 int readahead, struct buffer_head **out_bh)
177 struct buffer_head *first_bh, *bh;
178 unsigned long blkoff;
179 int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
182 err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
183 if (err == -EEXIST) /* internal code */
191 for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
192 err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
193 if (likely(!err || err == -EEXIST))
195 else if (err != -EBUSY)
197 /* abort readahead if bmap lookup failed */
198 if (!buffer_locked(first_bh))
203 wait_on_buffer(first_bh);
207 if (!buffer_uptodate(first_bh))
220 * nilfs_mdt_get_block - read or create a buffer on meta data file.
221 * @inode: inode of the meta data file
222 * @blkoff: block offset
223 * @create: create flag
224 * @init_block: initializer used for newly allocated block
225 * @out_bh: output of a pointer to the buffer_head
227 * nilfs_mdt_get_block() looks up the specified buffer and tries to create
228 * a new buffer if @create is not zero. On success, the returned buffer is
229 * assured to be either existing or formatted using a buffer lock on success.
230 * @out_bh is substituted only when zero is returned.
232 * Return Value: On success, it returns 0. On error, the following negative
233 * error code is returned.
235 * %-ENOMEM - Insufficient memory available.
239 * %-ENOENT - the specified block does not exist (hole block)
241 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
243 * %-EROFS - Read only filesystem (for create mode)
245 int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
246 void (*init_block)(struct inode *,
247 struct buffer_head *, void *),
248 struct buffer_head **out_bh)
252 /* Should be rewritten with merging nilfs_mdt_read_block() */
254 ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
255 if (!create || ret != -ENOENT)
258 ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
259 if (unlikely(ret == -EEXIST)) {
260 /* create = 0; */ /* limit read-create loop retries */
267 * nilfs_mdt_delete_block - make a hole on the meta data file.
268 * @inode: inode of the meta data file
269 * @block: block offset
271 * Return Value: On success, zero is returned.
272 * On error, one of the following negative error code is returned.
274 * %-ENOMEM - Insufficient memory available.
278 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
280 int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
282 struct nilfs_inode_info *ii = NILFS_I(inode);
285 err = nilfs_bmap_delete(ii->i_bmap, block);
286 if (!err || err == -ENOENT) {
287 nilfs_mdt_mark_dirty(inode);
288 nilfs_mdt_forget_block(inode, block);
294 * nilfs_mdt_forget_block - discard dirty state and try to remove the page
295 * @inode: inode of the meta data file
296 * @block: block offset
298 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
299 * tries to release the page including the buffer from a page cache.
301 * Return Value: On success, 0 is returned. On error, one of the following
302 * negative error code is returned.
304 * %-EBUSY - page has an active buffer.
306 * %-ENOENT - page cache has no page addressed by the offset.
308 int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
310 pgoff_t index = (pgoff_t)block >>
311 (PAGE_CACHE_SHIFT - inode->i_blkbits);
313 unsigned long first_block;
317 page = find_lock_page(inode->i_mapping, index);
321 wait_on_page_writeback(page);
323 first_block = (unsigned long)index <<
324 (PAGE_CACHE_SHIFT - inode->i_blkbits);
325 if (page_has_buffers(page)) {
326 struct buffer_head *bh;
328 bh = nilfs_page_get_nth_block(page, block - first_block);
329 nilfs_forget_buffer(bh);
331 still_dirty = PageDirty(page);
333 page_cache_release(page);
336 invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
342 * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
343 * @inode: inode of the meta data file
344 * @block: block offset
346 * Return Value: On success, it returns 0. On error, the following negative
347 * error code is returned.
349 * %-ENOMEM - Insufficient memory available.
353 * %-ENOENT - the specified block does not exist (hole block)
355 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
357 int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
359 struct buffer_head *bh;
362 err = nilfs_mdt_read_block(inode, block, 0, &bh);
365 nilfs_mark_buffer_dirty(bh);
366 nilfs_mdt_mark_dirty(inode);
371 int nilfs_mdt_fetch_dirty(struct inode *inode)
373 struct nilfs_inode_info *ii = NILFS_I(inode);
375 if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
376 set_bit(NILFS_I_DIRTY, &ii->i_state);
379 return test_bit(NILFS_I_DIRTY, &ii->i_state);
383 nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
386 struct super_block *sb;
389 redirty_page_for_writepage(wbc, page);
392 inode = page->mapping->host;
398 if (wbc->sync_mode == WB_SYNC_ALL)
399 err = nilfs_construct_segment(sb);
400 else if (wbc->for_reclaim)
401 nilfs_flush_segment(sb, inode->i_ino);
407 static const struct address_space_operations def_mdt_aops = {
408 .writepage = nilfs_mdt_write_page,
409 .sync_page = block_sync_page,
412 static const struct inode_operations def_mdt_iops;
413 static const struct file_operations def_mdt_fops;
416 int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
418 struct nilfs_mdt_info *mi;
420 mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
424 init_rwsem(&mi->mi_sem);
425 inode->i_private = mi;
427 inode->i_mode = S_IFREG;
428 mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
429 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
431 inode->i_op = &def_mdt_iops;
432 inode->i_fop = &def_mdt_fops;
433 inode->i_mapping->a_ops = &def_mdt_aops;
439 * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
440 * ifile, or gcinodes. This allows the B-tree code and segment constructor
441 * to treat them like regular files, and this helps to simplify the
443 * On the other hand, some of the pseudo inodes have an irregular point:
444 * They don't have valid inode->i_sb pointer because their lifetimes are
445 * longer than those of the super block structs; they may continue for
446 * several consecutive mounts/umounts. This would need discussions.
449 * nilfs_mdt_new_common - allocate a pseudo inode for metadata file
450 * @nilfs: nilfs object
451 * @sb: super block instance the metadata file belongs to
455 nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
458 struct inode *inode = nilfs_alloc_inode_common(nilfs);
463 struct address_space * const mapping = &inode->i_data;
465 inode->i_sb = sb; /* sb may be NULL for some meta data files */
466 inode->i_blkbits = nilfs->ns_blocksize_bits;
468 atomic_set(&inode->i_count, 1);
472 #ifdef INIT_UNUSED_INODE_FIELDS
473 atomic_set(&inode->i_writecount, 0);
477 inode->i_generation = 0;
479 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
481 inode->i_pipe = NULL;
482 inode->i_bdev = NULL;
483 inode->i_cdev = NULL;
485 #ifdef CONFIG_SECURITY
486 inode->i_security = NULL;
488 inode->dirtied_when = 0;
490 INIT_LIST_HEAD(&inode->i_list);
491 INIT_LIST_HEAD(&inode->i_sb_list);
495 spin_lock_init(&inode->i_lock);
496 mutex_init(&inode->i_mutex);
497 init_rwsem(&inode->i_alloc_sem);
499 mapping->host = NULL; /* instead of inode */
501 mapping->assoc_mapping = NULL;
503 inode->i_mapping = mapping;
509 struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
510 ino_t ino, size_t objsz)
514 inode = nilfs_mdt_new_common(nilfs, sb, ino);
518 if (nilfs_mdt_init(inode, NILFS_MDT_GFP, objsz) < 0) {
519 nilfs_destroy_inode(inode);
525 void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
526 unsigned header_size)
528 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
530 mi->mi_entry_size = entry_size;
531 mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
532 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
535 static const struct address_space_operations shadow_map_aops = {
536 .sync_page = block_sync_page,
540 * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
541 * @inode: inode of the metadata file
542 * @shadow: shadow mapping
544 int nilfs_mdt_setup_shadow_map(struct inode *inode,
545 struct nilfs_shadow_map *shadow)
547 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
548 struct backing_dev_info *bdi = NILFS_I_NILFS(inode)->ns_bdi;
550 INIT_LIST_HEAD(&shadow->frozen_buffers);
551 nilfs_mapping_init_once(&shadow->frozen_data);
552 nilfs_mapping_init(&shadow->frozen_data, bdi, &shadow_map_aops);
553 nilfs_mapping_init_once(&shadow->frozen_btnodes);
554 nilfs_mapping_init(&shadow->frozen_btnodes, bdi, &shadow_map_aops);
555 mi->mi_shadow = shadow;
560 * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
561 * @inode: inode of the metadata file
563 int nilfs_mdt_save_to_shadow_map(struct inode *inode)
565 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
566 struct nilfs_inode_info *ii = NILFS_I(inode);
567 struct nilfs_shadow_map *shadow = mi->mi_shadow;
570 ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
574 ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
575 &ii->i_btnode_cache);
579 nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
584 int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
586 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
587 struct buffer_head *bh_frozen;
589 int blkbits = inode->i_blkbits;
592 page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
596 if (!page_has_buffers(page))
597 create_empty_buffers(page, 1 << blkbits, 0);
599 bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
601 if (!buffer_uptodate(bh_frozen))
602 nilfs_copy_buffer(bh_frozen, bh);
603 if (list_empty(&bh_frozen->b_assoc_buffers)) {
604 list_add_tail(&bh_frozen->b_assoc_buffers,
605 &shadow->frozen_buffers);
606 set_buffer_nilfs_redirected(bh);
608 brelse(bh_frozen); /* already frozen */
613 page_cache_release(page);
618 nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
620 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
621 struct buffer_head *bh_frozen = NULL;
625 page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
627 if (page_has_buffers(page)) {
628 n = bh_offset(bh) >> inode->i_blkbits;
629 bh_frozen = nilfs_page_get_nth_block(page, n);
632 page_cache_release(page);
637 static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
639 struct list_head *head = &shadow->frozen_buffers;
640 struct buffer_head *bh;
642 while (!list_empty(head)) {
643 bh = list_first_entry(head, struct buffer_head,
645 list_del_init(&bh->b_assoc_buffers);
646 brelse(bh); /* drop ref-count to make it releasable */
651 * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
652 * @inode: inode of the metadata file
654 void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
656 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
657 struct nilfs_inode_info *ii = NILFS_I(inode);
658 struct nilfs_shadow_map *shadow = mi->mi_shadow;
660 down_write(&mi->mi_sem);
662 if (mi->mi_palloc_cache)
663 nilfs_palloc_clear_cache(inode);
665 nilfs_clear_dirty_pages(inode->i_mapping);
666 nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
668 nilfs_clear_dirty_pages(&ii->i_btnode_cache);
669 nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
671 nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
673 up_write(&mi->mi_sem);
677 * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
678 * @inode: inode of the metadata file
680 void nilfs_mdt_clear_shadow_map(struct inode *inode)
682 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
683 struct nilfs_shadow_map *shadow = mi->mi_shadow;
685 down_write(&mi->mi_sem);
686 nilfs_release_frozen_buffers(shadow);
687 truncate_inode_pages(&shadow->frozen_data, 0);
688 truncate_inode_pages(&shadow->frozen_btnodes, 0);
689 up_write(&mi->mi_sem);
692 static void nilfs_mdt_clear(struct inode *inode)
694 struct nilfs_inode_info *ii = NILFS_I(inode);
696 invalidate_mapping_pages(inode->i_mapping, 0, -1);
697 truncate_inode_pages(inode->i_mapping, 0);
699 if (test_bit(NILFS_I_BMAP, &ii->i_state))
700 nilfs_bmap_clear(ii->i_bmap);
701 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
704 void nilfs_mdt_destroy(struct inode *inode)
706 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
708 if (mdi->mi_palloc_cache)
709 nilfs_palloc_destroy_cache(inode);
710 nilfs_mdt_clear(inode);
712 nilfs_destroy_inode(inode);