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>
36 #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
38 #define INIT_UNUSED_INODE_FIELDS
41 nilfs_mdt_insert_new_block(struct inode
*inode
, unsigned long block
,
42 struct buffer_head
*bh
,
43 void (*init_block
)(struct inode
*,
44 struct buffer_head
*, void *))
46 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
50 /* Caller exclude read accesses using page lock */
52 /* set_buffer_new(bh); */
55 ret
= nilfs_bmap_insert(ii
->i_bmap
, block
, (unsigned long)bh
);
59 set_buffer_mapped(bh
);
61 kaddr
= kmap_atomic(bh
->b_page
, KM_USER0
);
62 memset(kaddr
+ bh_offset(bh
), 0, 1 << inode
->i_blkbits
);
64 init_block(inode
, bh
, kaddr
);
65 flush_dcache_page(bh
->b_page
);
66 kunmap_atomic(kaddr
, KM_USER0
);
68 set_buffer_uptodate(bh
);
69 nilfs_mark_buffer_dirty(bh
);
70 nilfs_mdt_mark_dirty(inode
);
74 static int nilfs_mdt_create_block(struct inode
*inode
, unsigned long block
,
75 struct buffer_head
**out_bh
,
76 void (*init_block
)(struct inode
*,
80 struct the_nilfs
*nilfs
= NILFS_MDT(inode
)->mi_nilfs
;
81 struct super_block
*sb
= inode
->i_sb
;
82 struct nilfs_transaction_info ti
;
83 struct buffer_head
*bh
;
88 * Make sure this function is not called from any
91 if (!nilfs
->ns_writer
) {
96 sb
= nilfs
->ns_writer
->s_super
;
99 nilfs_transaction_begin(sb
, &ti
, 0);
102 bh
= nilfs_grab_buffer(inode
, inode
->i_mapping
, block
, 0);
107 if (buffer_uptodate(bh
))
111 if (buffer_uptodate(bh
))
114 bh
->b_bdev
= nilfs
->ns_bdev
;
115 err
= nilfs_mdt_insert_new_block(inode
, block
, bh
, init_block
);
122 unlock_page(bh
->b_page
);
123 page_cache_release(bh
->b_page
);
128 err
= nilfs_transaction_commit(sb
);
130 nilfs_transaction_abort(sb
);
136 nilfs_mdt_submit_block(struct inode
*inode
, unsigned long blkoff
,
137 int mode
, struct buffer_head
**out_bh
)
139 struct buffer_head
*bh
;
143 bh
= nilfs_grab_buffer(inode
, inode
->i_mapping
, blkoff
, 0);
147 ret
= -EEXIST
; /* internal code */
148 if (buffer_uptodate(bh
))
152 if (!trylock_buffer(bh
)) {
156 } else /* mode == READ */
159 if (buffer_uptodate(bh
)) {
164 ret
= nilfs_bmap_lookup(NILFS_I(inode
)->i_bmap
, blkoff
, &blknum
);
169 bh
->b_bdev
= NILFS_MDT(inode
)->mi_nilfs
->ns_bdev
;
170 bh
->b_blocknr
= (sector_t
)blknum
;
171 set_buffer_mapped(bh
);
173 bh
->b_end_io
= end_buffer_read_sync
;
182 unlock_page(bh
->b_page
);
183 page_cache_release(bh
->b_page
);
189 static int nilfs_mdt_read_block(struct inode
*inode
, unsigned long block
,
190 int readahead
, struct buffer_head
**out_bh
)
192 struct buffer_head
*first_bh
, *bh
;
193 unsigned long blkoff
;
194 int i
, nr_ra_blocks
= NILFS_MDT_MAX_RA_BLOCKS
;
197 err
= nilfs_mdt_submit_block(inode
, block
, READ
, &first_bh
);
198 if (err
== -EEXIST
) /* internal code */
206 for (i
= 0; i
< nr_ra_blocks
; i
++, blkoff
++) {
207 err
= nilfs_mdt_submit_block(inode
, blkoff
, READA
, &bh
);
208 if (likely(!err
|| err
== -EEXIST
))
210 else if (err
!= -EBUSY
)
212 /* abort readahead if bmap lookup failed */
213 if (!buffer_locked(first_bh
))
218 wait_on_buffer(first_bh
);
222 if (!buffer_uptodate(first_bh
))
235 * nilfs_mdt_get_block - read or create a buffer on meta data file.
236 * @inode: inode of the meta data file
237 * @blkoff: block offset
238 * @create: create flag
239 * @init_block: initializer used for newly allocated block
240 * @out_bh: output of a pointer to the buffer_head
242 * nilfs_mdt_get_block() looks up the specified buffer and tries to create
243 * a new buffer if @create is not zero. On success, the returned buffer is
244 * assured to be either existing or formatted using a buffer lock on success.
245 * @out_bh is substituted only when zero is returned.
247 * Return Value: On success, it returns 0. On error, the following negative
248 * error code is returned.
250 * %-ENOMEM - Insufficient memory available.
254 * %-ENOENT - the specified block does not exist (hole block)
256 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
258 * %-EROFS - Read only filesystem (for create mode)
260 int nilfs_mdt_get_block(struct inode
*inode
, unsigned long blkoff
, int create
,
261 void (*init_block
)(struct inode
*,
262 struct buffer_head
*, void *),
263 struct buffer_head
**out_bh
)
267 /* Should be rewritten with merging nilfs_mdt_read_block() */
269 ret
= nilfs_mdt_read_block(inode
, blkoff
, !create
, out_bh
);
270 if (!create
|| ret
!= -ENOENT
)
273 ret
= nilfs_mdt_create_block(inode
, blkoff
, out_bh
, init_block
);
274 if (unlikely(ret
== -EEXIST
)) {
275 /* create = 0; */ /* limit read-create loop retries */
282 * nilfs_mdt_delete_block - make a hole on the meta data file.
283 * @inode: inode of the meta data file
284 * @block: block offset
286 * Return Value: On success, zero is returned.
287 * On error, one of the following negative error code is returned.
289 * %-ENOMEM - Insufficient memory available.
293 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
295 int nilfs_mdt_delete_block(struct inode
*inode
, unsigned long block
)
297 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
300 err
= nilfs_bmap_delete(ii
->i_bmap
, block
);
301 if (!err
|| err
== -ENOENT
) {
302 nilfs_mdt_mark_dirty(inode
);
303 nilfs_mdt_forget_block(inode
, block
);
309 * nilfs_mdt_forget_block - discard dirty state and try to remove the page
310 * @inode: inode of the meta data file
311 * @block: block offset
313 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
314 * tries to release the page including the buffer from a page cache.
316 * Return Value: On success, 0 is returned. On error, one of the following
317 * negative error code is returned.
319 * %-EBUSY - page has an active buffer.
321 * %-ENOENT - page cache has no page addressed by the offset.
323 int nilfs_mdt_forget_block(struct inode
*inode
, unsigned long block
)
325 pgoff_t index
= (pgoff_t
)block
>>
326 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
328 unsigned long first_block
;
332 page
= find_lock_page(inode
->i_mapping
, index
);
336 wait_on_page_writeback(page
);
338 first_block
= (unsigned long)index
<<
339 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
340 if (page_has_buffers(page
)) {
341 struct buffer_head
*bh
;
343 bh
= nilfs_page_get_nth_block(page
, block
- first_block
);
344 nilfs_forget_buffer(bh
);
346 still_dirty
= PageDirty(page
);
348 page_cache_release(page
);
351 invalidate_inode_pages2_range(inode
->i_mapping
, index
, index
) != 0)
357 * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
358 * @inode: inode of the meta data file
359 * @block: block offset
361 * Return Value: On success, it returns 0. On error, the following negative
362 * error code is returned.
364 * %-ENOMEM - Insufficient memory available.
368 * %-ENOENT - the specified block does not exist (hole block)
370 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
372 int nilfs_mdt_mark_block_dirty(struct inode
*inode
, unsigned long block
)
374 struct buffer_head
*bh
;
377 err
= nilfs_mdt_read_block(inode
, block
, 0, &bh
);
380 nilfs_mark_buffer_dirty(bh
);
381 nilfs_mdt_mark_dirty(inode
);
386 int nilfs_mdt_fetch_dirty(struct inode
*inode
)
388 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
390 if (nilfs_bmap_test_and_clear_dirty(ii
->i_bmap
)) {
391 set_bit(NILFS_I_DIRTY
, &ii
->i_state
);
394 return test_bit(NILFS_I_DIRTY
, &ii
->i_state
);
398 nilfs_mdt_write_page(struct page
*page
, struct writeback_control
*wbc
)
400 struct inode
*inode
= container_of(page
->mapping
,
401 struct inode
, i_data
);
402 struct super_block
*sb
= inode
->i_sb
;
403 struct the_nilfs
*nilfs
= NILFS_MDT(inode
)->mi_nilfs
;
404 struct nilfs_sb_info
*writer
= NULL
;
407 redirty_page_for_writepage(wbc
, page
);
410 if (page
->mapping
->assoc_mapping
)
411 return 0; /* Do not request flush for shadow page cache */
413 down_read(&nilfs
->ns_writer_sem
);
414 writer
= nilfs
->ns_writer
;
416 up_read(&nilfs
->ns_writer_sem
);
419 sb
= writer
->s_super
;
422 if (wbc
->sync_mode
== WB_SYNC_ALL
)
423 err
= nilfs_construct_segment(sb
);
424 else if (wbc
->for_reclaim
)
425 nilfs_flush_segment(sb
, inode
->i_ino
);
428 up_read(&nilfs
->ns_writer_sem
);
433 static const struct address_space_operations def_mdt_aops
= {
434 .writepage
= nilfs_mdt_write_page
,
435 .sync_page
= block_sync_page
,
438 static const struct inode_operations def_mdt_iops
;
439 static const struct file_operations def_mdt_fops
;
442 * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
443 * ifile, or gcinodes. This allows the B-tree code and segment constructor
444 * to treat them like regular files, and this helps to simplify the
446 * On the other hand, some of the pseudo inodes have an irregular point:
447 * They don't have valid inode->i_sb pointer because their lifetimes are
448 * longer than those of the super block structs; they may continue for
449 * several consecutive mounts/umounts. This would need discussions.
452 * nilfs_mdt_new_common - allocate a pseudo inode for metadata file
453 * @nilfs: nilfs object
454 * @sb: super block instance the metadata file belongs to
456 * @gfp_mask: gfp mask for data pages
457 * @objsz: size of the private object attached to inode->i_private
460 nilfs_mdt_new_common(struct the_nilfs
*nilfs
, struct super_block
*sb
,
461 ino_t ino
, gfp_t gfp_mask
, size_t objsz
)
463 struct inode
*inode
= nilfs_alloc_inode_common(nilfs
);
468 struct address_space
* const mapping
= &inode
->i_data
;
469 struct nilfs_mdt_info
*mi
;
471 mi
= kzalloc(max(sizeof(*mi
), objsz
), GFP_NOFS
);
473 nilfs_destroy_inode(inode
);
476 mi
->mi_nilfs
= nilfs
;
477 init_rwsem(&mi
->mi_sem
);
479 inode
->i_sb
= sb
; /* sb may be NULL for some meta data files */
480 inode
->i_blkbits
= nilfs
->ns_blocksize_bits
;
482 atomic_set(&inode
->i_count
, 1);
485 inode
->i_mode
= S_IFREG
;
486 inode
->i_private
= mi
;
488 #ifdef INIT_UNUSED_INODE_FIELDS
489 atomic_set(&inode
->i_writecount
, 0);
493 inode
->i_generation
= 0;
495 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
497 inode
->i_pipe
= NULL
;
498 inode
->i_bdev
= NULL
;
499 inode
->i_cdev
= NULL
;
501 #ifdef CONFIG_SECURITY
502 inode
->i_security
= NULL
;
504 inode
->dirtied_when
= 0;
506 INIT_LIST_HEAD(&inode
->i_list
);
507 INIT_LIST_HEAD(&inode
->i_sb_list
);
511 spin_lock_init(&inode
->i_lock
);
512 mutex_init(&inode
->i_mutex
);
513 init_rwsem(&inode
->i_alloc_sem
);
515 mapping
->host
= NULL
; /* instead of inode */
517 mapping_set_gfp_mask(mapping
, gfp_mask
);
518 mapping
->assoc_mapping
= NULL
;
519 mapping
->backing_dev_info
= nilfs
->ns_bdi
;
521 inode
->i_mapping
= mapping
;
527 struct inode
*nilfs_mdt_new(struct the_nilfs
*nilfs
, struct super_block
*sb
,
528 ino_t ino
, size_t objsz
)
532 inode
= nilfs_mdt_new_common(nilfs
, sb
, ino
, NILFS_MDT_GFP
, objsz
);
536 inode
->i_op
= &def_mdt_iops
;
537 inode
->i_fop
= &def_mdt_fops
;
538 inode
->i_mapping
->a_ops
= &def_mdt_aops
;
542 void nilfs_mdt_set_entry_size(struct inode
*inode
, unsigned entry_size
,
543 unsigned header_size
)
545 struct nilfs_mdt_info
*mi
= NILFS_MDT(inode
);
547 mi
->mi_entry_size
= entry_size
;
548 mi
->mi_entries_per_block
= (1 << inode
->i_blkbits
) / entry_size
;
549 mi
->mi_first_entry_offset
= DIV_ROUND_UP(header_size
, entry_size
);
552 void nilfs_mdt_set_shadow(struct inode
*orig
, struct inode
*shadow
)
554 shadow
->i_mapping
->assoc_mapping
= orig
->i_mapping
;
555 NILFS_I(shadow
)->i_btnode_cache
.assoc_mapping
=
556 &NILFS_I(orig
)->i_btnode_cache
;
559 static void nilfs_mdt_clear(struct inode
*inode
)
561 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
563 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
564 truncate_inode_pages(inode
->i_mapping
, 0);
566 if (test_bit(NILFS_I_BMAP
, &ii
->i_state
))
567 nilfs_bmap_clear(ii
->i_bmap
);
568 nilfs_btnode_cache_clear(&ii
->i_btnode_cache
);
571 void nilfs_mdt_destroy(struct inode
*inode
)
573 struct nilfs_mdt_info
*mdi
= NILFS_MDT(inode
);
575 if (mdi
->mi_palloc_cache
)
576 nilfs_palloc_destroy_cache(inode
);
577 nilfs_mdt_clear(inode
);
579 kfree(mdi
->mi_bgl
); /* kfree(NULL) is safe */
581 nilfs_destroy_inode(inode
);