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>
35 #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
37 #define INIT_UNUSED_INODE_FIELDS
40 nilfs_mdt_insert_new_block(struct inode
*inode
, unsigned long block
,
41 struct buffer_head
*bh
,
42 void (*init_block
)(struct inode
*,
43 struct buffer_head
*, void *))
45 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
49 /* Caller exclude read accesses using page lock */
51 /* set_buffer_new(bh); */
54 ret
= nilfs_bmap_insert(ii
->i_bmap
, block
, (unsigned long)bh
);
58 set_buffer_mapped(bh
);
60 kaddr
= kmap_atomic(bh
->b_page
, KM_USER0
);
61 memset(kaddr
+ bh_offset(bh
), 0, 1 << inode
->i_blkbits
);
63 init_block(inode
, bh
, kaddr
);
64 flush_dcache_page(bh
->b_page
);
65 kunmap_atomic(kaddr
, KM_USER0
);
67 set_buffer_uptodate(bh
);
68 nilfs_mark_buffer_dirty(bh
);
69 nilfs_mdt_mark_dirty(inode
);
73 static int nilfs_mdt_create_block(struct inode
*inode
, unsigned long block
,
74 struct buffer_head
**out_bh
,
75 void (*init_block
)(struct inode
*,
79 struct the_nilfs
*nilfs
= NILFS_MDT(inode
)->mi_nilfs
;
80 struct super_block
*sb
= inode
->i_sb
;
81 struct nilfs_transaction_info ti
;
82 struct buffer_head
*bh
;
87 * Make sure this function is not called from any
90 if (!nilfs
->ns_writer
) {
95 sb
= nilfs
->ns_writer
->s_super
;
98 nilfs_transaction_begin(sb
, &ti
, 0);
101 bh
= nilfs_grab_buffer(inode
, inode
->i_mapping
, block
, 0);
106 if (buffer_uptodate(bh
))
110 if (buffer_uptodate(bh
))
113 bh
->b_bdev
= nilfs
->ns_bdev
;
114 err
= nilfs_mdt_insert_new_block(inode
, block
, bh
, init_block
);
121 unlock_page(bh
->b_page
);
122 page_cache_release(bh
->b_page
);
127 err
= nilfs_transaction_commit(sb
);
129 nilfs_transaction_abort(sb
);
135 nilfs_mdt_submit_block(struct inode
*inode
, unsigned long blkoff
,
136 int mode
, struct buffer_head
**out_bh
)
138 struct buffer_head
*bh
;
142 bh
= nilfs_grab_buffer(inode
, inode
->i_mapping
, blkoff
, 0);
146 ret
= -EEXIST
; /* internal code */
147 if (buffer_uptodate(bh
))
151 if (!trylock_buffer(bh
)) {
155 } else /* mode == READ */
158 if (buffer_uptodate(bh
)) {
163 ret
= nilfs_bmap_lookup(NILFS_I(inode
)->i_bmap
, blkoff
, &blknum
);
168 bh
->b_bdev
= NILFS_MDT(inode
)->mi_nilfs
->ns_bdev
;
169 bh
->b_blocknr
= (sector_t
)blknum
;
170 set_buffer_mapped(bh
);
172 bh
->b_end_io
= end_buffer_read_sync
;
181 unlock_page(bh
->b_page
);
182 page_cache_release(bh
->b_page
);
188 static int nilfs_mdt_read_block(struct inode
*inode
, unsigned long block
,
189 int readahead
, struct buffer_head
**out_bh
)
191 struct buffer_head
*first_bh
, *bh
;
192 unsigned long blkoff
;
193 int i
, nr_ra_blocks
= NILFS_MDT_MAX_RA_BLOCKS
;
196 err
= nilfs_mdt_submit_block(inode
, block
, READ
, &first_bh
);
197 if (err
== -EEXIST
) /* internal code */
205 for (i
= 0; i
< nr_ra_blocks
; i
++, blkoff
++) {
206 err
= nilfs_mdt_submit_block(inode
, blkoff
, READA
, &bh
);
207 if (likely(!err
|| err
== -EEXIST
))
209 else if (err
!= -EBUSY
)
211 /* abort readahead if bmap lookup failed */
212 if (!buffer_locked(first_bh
))
217 wait_on_buffer(first_bh
);
221 if (!buffer_uptodate(first_bh
))
234 * nilfs_mdt_get_block - read or create a buffer on meta data file.
235 * @inode: inode of the meta data file
236 * @blkoff: block offset
237 * @create: create flag
238 * @init_block: initializer used for newly allocated block
239 * @out_bh: output of a pointer to the buffer_head
241 * nilfs_mdt_get_block() looks up the specified buffer and tries to create
242 * a new buffer if @create is not zero. On success, the returned buffer is
243 * assured to be either existing or formatted using a buffer lock on success.
244 * @out_bh is substituted only when zero is returned.
246 * Return Value: On success, it returns 0. On error, the following negative
247 * error code is returned.
249 * %-ENOMEM - Insufficient memory available.
253 * %-ENOENT - the specified block does not exist (hole block)
255 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
257 * %-EROFS - Read only filesystem (for create mode)
259 int nilfs_mdt_get_block(struct inode
*inode
, unsigned long blkoff
, int create
,
260 void (*init_block
)(struct inode
*,
261 struct buffer_head
*, void *),
262 struct buffer_head
**out_bh
)
266 /* Should be rewritten with merging nilfs_mdt_read_block() */
268 ret
= nilfs_mdt_read_block(inode
, blkoff
, !create
, out_bh
);
269 if (!create
|| ret
!= -ENOENT
)
272 ret
= nilfs_mdt_create_block(inode
, blkoff
, out_bh
, init_block
);
273 if (unlikely(ret
== -EEXIST
)) {
274 /* create = 0; */ /* limit read-create loop retries */
281 * nilfs_mdt_delete_block - make a hole on the meta data file.
282 * @inode: inode of the meta data file
283 * @block: block offset
285 * Return Value: On success, zero is returned.
286 * On error, one of the following negative error code is returned.
288 * %-ENOMEM - Insufficient memory available.
292 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
294 int nilfs_mdt_delete_block(struct inode
*inode
, unsigned long block
)
296 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
299 err
= nilfs_bmap_delete(ii
->i_bmap
, block
);
300 if (!err
|| err
== -ENOENT
) {
301 nilfs_mdt_mark_dirty(inode
);
302 nilfs_mdt_forget_block(inode
, block
);
308 * nilfs_mdt_forget_block - discard dirty state and try to remove the page
309 * @inode: inode of the meta data file
310 * @block: block offset
312 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
313 * tries to release the page including the buffer from a page cache.
315 * Return Value: On success, 0 is returned. On error, one of the following
316 * negative error code is returned.
318 * %-EBUSY - page has an active buffer.
320 * %-ENOENT - page cache has no page addressed by the offset.
322 int nilfs_mdt_forget_block(struct inode
*inode
, unsigned long block
)
324 pgoff_t index
= (pgoff_t
)block
>>
325 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
327 unsigned long first_block
;
331 page
= find_lock_page(inode
->i_mapping
, index
);
335 wait_on_page_writeback(page
);
337 first_block
= (unsigned long)index
<<
338 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
339 if (page_has_buffers(page
)) {
340 struct buffer_head
*bh
;
342 bh
= nilfs_page_get_nth_block(page
, block
- first_block
);
343 nilfs_forget_buffer(bh
);
345 still_dirty
= PageDirty(page
);
347 page_cache_release(page
);
350 invalidate_inode_pages2_range(inode
->i_mapping
, index
, index
) != 0)
356 * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
357 * @inode: inode of the meta data file
358 * @block: block offset
360 * Return Value: On success, it returns 0. On error, the following negative
361 * error code is returned.
363 * %-ENOMEM - Insufficient memory available.
367 * %-ENOENT - the specified block does not exist (hole block)
369 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
371 int nilfs_mdt_mark_block_dirty(struct inode
*inode
, unsigned long block
)
373 struct buffer_head
*bh
;
376 err
= nilfs_mdt_read_block(inode
, block
, 0, &bh
);
379 nilfs_mark_buffer_dirty(bh
);
380 nilfs_mdt_mark_dirty(inode
);
385 int nilfs_mdt_fetch_dirty(struct inode
*inode
)
387 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
389 if (nilfs_bmap_test_and_clear_dirty(ii
->i_bmap
)) {
390 set_bit(NILFS_I_DIRTY
, &ii
->i_state
);
393 return test_bit(NILFS_I_DIRTY
, &ii
->i_state
);
397 nilfs_mdt_write_page(struct page
*page
, struct writeback_control
*wbc
)
399 struct inode
*inode
= container_of(page
->mapping
,
400 struct inode
, i_data
);
401 struct super_block
*sb
= inode
->i_sb
;
402 struct the_nilfs
*nilfs
= NILFS_MDT(inode
)->mi_nilfs
;
403 struct nilfs_sb_info
*writer
= NULL
;
406 redirty_page_for_writepage(wbc
, page
);
409 if (page
->mapping
->assoc_mapping
)
410 return 0; /* Do not request flush for shadow page cache */
412 down_read(&nilfs
->ns_writer_sem
);
413 writer
= nilfs
->ns_writer
;
415 up_read(&nilfs
->ns_writer_sem
);
418 sb
= writer
->s_super
;
421 if (wbc
->sync_mode
== WB_SYNC_ALL
)
422 err
= nilfs_construct_segment(sb
);
423 else if (wbc
->for_reclaim
)
424 nilfs_flush_segment(sb
, inode
->i_ino
);
427 up_read(&nilfs
->ns_writer_sem
);
432 static const struct address_space_operations def_mdt_aops
= {
433 .writepage
= nilfs_mdt_write_page
,
434 .sync_page
= block_sync_page
,
437 static const struct inode_operations def_mdt_iops
;
438 static const struct file_operations def_mdt_fops
;
441 * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
442 * ifile, or gcinodes. This allows the B-tree code and segment constructor
443 * to treat them like regular files, and this helps to simplify the
445 * On the other hand, some of the pseudo inodes have an irregular point:
446 * They don't have valid inode->i_sb pointer because their lifetimes are
447 * longer than those of the super block structs; they may continue for
448 * several consecutive mounts/umounts. This would need discussions.
451 * nilfs_mdt_new_common - allocate a pseudo inode for metadata file
452 * @nilfs: nilfs object
453 * @sb: super block instance the metadata file belongs to
455 * @gfp_mask: gfp mask for data pages
456 * @objsz: size of the private object attached to inode->i_private
459 nilfs_mdt_new_common(struct the_nilfs
*nilfs
, struct super_block
*sb
,
460 ino_t ino
, gfp_t gfp_mask
, size_t objsz
)
462 struct inode
*inode
= nilfs_alloc_inode_common(nilfs
);
467 struct address_space
* const mapping
= &inode
->i_data
;
468 struct nilfs_mdt_info
*mi
;
470 mi
= kzalloc(max(sizeof(*mi
), objsz
), GFP_NOFS
);
472 nilfs_destroy_inode(inode
);
475 mi
->mi_nilfs
= nilfs
;
476 init_rwsem(&mi
->mi_sem
);
478 inode
->i_sb
= sb
; /* sb may be NULL for some meta data files */
479 inode
->i_blkbits
= nilfs
->ns_blocksize_bits
;
481 atomic_set(&inode
->i_count
, 1);
484 inode
->i_mode
= S_IFREG
;
485 inode
->i_private
= mi
;
487 #ifdef INIT_UNUSED_INODE_FIELDS
488 atomic_set(&inode
->i_writecount
, 0);
492 inode
->i_generation
= 0;
494 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
496 inode
->i_pipe
= NULL
;
497 inode
->i_bdev
= NULL
;
498 inode
->i_cdev
= NULL
;
500 #ifdef CONFIG_SECURITY
501 inode
->i_security
= NULL
;
503 inode
->dirtied_when
= 0;
505 INIT_LIST_HEAD(&inode
->i_list
);
506 INIT_LIST_HEAD(&inode
->i_sb_list
);
510 spin_lock_init(&inode
->i_lock
);
511 mutex_init(&inode
->i_mutex
);
512 init_rwsem(&inode
->i_alloc_sem
);
514 mapping
->host
= NULL
; /* instead of inode */
516 mapping_set_gfp_mask(mapping
, gfp_mask
);
517 mapping
->assoc_mapping
= NULL
;
518 mapping
->backing_dev_info
= nilfs
->ns_bdi
;
520 inode
->i_mapping
= mapping
;
526 struct inode
*nilfs_mdt_new(struct the_nilfs
*nilfs
, struct super_block
*sb
,
527 ino_t ino
, size_t objsz
)
531 inode
= nilfs_mdt_new_common(nilfs
, sb
, ino
, NILFS_MDT_GFP
, objsz
);
535 inode
->i_op
= &def_mdt_iops
;
536 inode
->i_fop
= &def_mdt_fops
;
537 inode
->i_mapping
->a_ops
= &def_mdt_aops
;
541 void nilfs_mdt_set_entry_size(struct inode
*inode
, unsigned entry_size
,
542 unsigned header_size
)
544 struct nilfs_mdt_info
*mi
= NILFS_MDT(inode
);
546 mi
->mi_entry_size
= entry_size
;
547 mi
->mi_entries_per_block
= (1 << inode
->i_blkbits
) / entry_size
;
548 mi
->mi_first_entry_offset
= DIV_ROUND_UP(header_size
, entry_size
);
551 void nilfs_mdt_set_shadow(struct inode
*orig
, struct inode
*shadow
)
553 shadow
->i_mapping
->assoc_mapping
= orig
->i_mapping
;
554 NILFS_I(shadow
)->i_btnode_cache
.assoc_mapping
=
555 &NILFS_I(orig
)->i_btnode_cache
;
558 static void nilfs_mdt_clear(struct inode
*inode
)
560 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
562 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
563 truncate_inode_pages(inode
->i_mapping
, 0);
565 if (test_bit(NILFS_I_BMAP
, &ii
->i_state
))
566 nilfs_bmap_clear(ii
->i_bmap
);
567 nilfs_btnode_cache_clear(&ii
->i_btnode_cache
);
570 void nilfs_mdt_destroy(struct inode
*inode
)
572 struct nilfs_mdt_info
*mdi
= NILFS_MDT(inode
);
574 if (mdi
->mi_palloc_cache
)
575 nilfs_palloc_destroy_cache(inode
);
576 nilfs_mdt_clear(inode
);
578 kfree(mdi
->mi_bgl
); /* kfree(NULL) is safe */
580 nilfs_destroy_inode(inode
);