2009-04-06 19:01:28 -07:00
|
|
|
/*
|
|
|
|
* mdt.c - meta data file for NILFS
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* Written by Ryusuke Konishi <ryusuke@osrg.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/buffer_head.h>
|
|
|
|
#include <linux/mpage.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/writeback.h>
|
|
|
|
#include <linux/backing-dev.h>
|
|
|
|
#include <linux/swap.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 01:04:11 -07:00
|
|
|
#include <linux/slab.h>
|
2009-04-06 19:01:28 -07:00
|
|
|
#include "nilfs.h"
|
2010-07-10 04:52:09 -07:00
|
|
|
#include "btnode.h"
|
2009-04-06 19:01:28 -07:00
|
|
|
#include "segment.h"
|
|
|
|
#include "page.h"
|
|
|
|
#include "mdt.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
|
|
|
|
|
|
|
|
#define INIT_UNUSED_INODE_FIELDS
|
|
|
|
|
|
|
|
static int
|
|
|
|
nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
|
|
|
|
struct buffer_head *bh,
|
|
|
|
void (*init_block)(struct inode *,
|
|
|
|
struct buffer_head *, void *))
|
|
|
|
{
|
|
|
|
struct nilfs_inode_info *ii = NILFS_I(inode);
|
|
|
|
void *kaddr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Caller exclude read accesses using page lock */
|
|
|
|
|
|
|
|
/* set_buffer_new(bh); */
|
|
|
|
bh->b_blocknr = 0;
|
|
|
|
|
|
|
|
ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
|
|
|
|
if (unlikely(ret))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
set_buffer_mapped(bh);
|
|
|
|
|
|
|
|
kaddr = kmap_atomic(bh->b_page, KM_USER0);
|
|
|
|
memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
|
|
|
|
if (init_block)
|
|
|
|
init_block(inode, bh, kaddr);
|
|
|
|
flush_dcache_page(bh->b_page);
|
|
|
|
kunmap_atomic(kaddr, KM_USER0);
|
|
|
|
|
|
|
|
set_buffer_uptodate(bh);
|
|
|
|
nilfs_mark_buffer_dirty(bh);
|
|
|
|
nilfs_mdt_mark_dirty(inode);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
|
|
|
|
struct buffer_head **out_bh,
|
|
|
|
void (*init_block)(struct inode *,
|
|
|
|
struct buffer_head *,
|
|
|
|
void *))
|
|
|
|
{
|
|
|
|
struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
|
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
struct nilfs_transaction_info ti;
|
|
|
|
struct buffer_head *bh;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!sb) {
|
2009-04-28 05:04:59 -07:00
|
|
|
/*
|
|
|
|
* Make sure this function is not called from any
|
|
|
|
* read-only context.
|
|
|
|
*/
|
|
|
|
if (!nilfs->ns_writer) {
|
|
|
|
WARN_ON(1);
|
2009-04-06 19:01:28 -07:00
|
|
|
err = -EROFS;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-04-28 05:04:59 -07:00
|
|
|
sb = nilfs->ns_writer->s_super;
|
2009-04-06 19:01:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nilfs_transaction_begin(sb, &ti, 0);
|
|
|
|
|
|
|
|
err = -ENOMEM;
|
|
|
|
bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
|
|
|
|
if (unlikely(!bh))
|
|
|
|
goto failed_unlock;
|
|
|
|
|
|
|
|
err = -EEXIST;
|
2009-09-06 01:49:49 -07:00
|
|
|
if (buffer_uptodate(bh))
|
2009-04-06 19:01:28 -07:00
|
|
|
goto failed_bh;
|
2009-09-06 01:49:49 -07:00
|
|
|
|
2009-04-06 19:01:28 -07:00
|
|
|
wait_on_buffer(bh);
|
|
|
|
if (buffer_uptodate(bh))
|
|
|
|
goto failed_bh;
|
|
|
|
|
|
|
|
bh->b_bdev = nilfs->ns_bdev;
|
|
|
|
err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
|
|
|
|
if (likely(!err)) {
|
|
|
|
get_bh(bh);
|
|
|
|
*out_bh = bh;
|
|
|
|
}
|
|
|
|
|
|
|
|
failed_bh:
|
|
|
|
unlock_page(bh->b_page);
|
|
|
|
page_cache_release(bh->b_page);
|
|
|
|
brelse(bh);
|
|
|
|
|
|
|
|
failed_unlock:
|
2009-04-06 19:01:45 -07:00
|
|
|
if (likely(!err))
|
|
|
|
err = nilfs_transaction_commit(sb);
|
|
|
|
else
|
|
|
|
nilfs_transaction_abort(sb);
|
2009-04-06 19:01:28 -07:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
|
|
|
|
int mode, struct buffer_head **out_bh)
|
|
|
|
{
|
|
|
|
struct buffer_head *bh;
|
2009-08-14 10:29:28 -07:00
|
|
|
__u64 blknum = 0;
|
2009-04-06 19:01:28 -07:00
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
|
|
|
|
if (unlikely(!bh))
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
ret = -EEXIST; /* internal code */
|
|
|
|
if (buffer_uptodate(bh))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (mode == READA) {
|
|
|
|
if (!trylock_buffer(bh)) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto failed_bh;
|
|
|
|
}
|
2009-04-06 19:01:55 -07:00
|
|
|
} else /* mode == READ */
|
2009-04-06 19:01:28 -07:00
|
|
|
lock_buffer(bh);
|
|
|
|
|
|
|
|
if (buffer_uptodate(bh)) {
|
|
|
|
unlock_buffer(bh);
|
|
|
|
goto out;
|
|
|
|
}
|
2009-09-06 01:49:49 -07:00
|
|
|
|
|
|
|
ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
|
|
|
|
if (unlikely(ret)) {
|
|
|
|
unlock_buffer(bh);
|
|
|
|
goto failed_bh;
|
2009-04-06 19:01:28 -07:00
|
|
|
}
|
2009-09-06 01:49:49 -07:00
|
|
|
bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev;
|
2009-08-14 10:29:28 -07:00
|
|
|
bh->b_blocknr = (sector_t)blknum;
|
2009-09-06 01:49:49 -07:00
|
|
|
set_buffer_mapped(bh);
|
2009-04-06 19:01:28 -07:00
|
|
|
|
|
|
|
bh->b_end_io = end_buffer_read_sync;
|
|
|
|
get_bh(bh);
|
|
|
|
submit_bh(mode, bh);
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
get_bh(bh);
|
|
|
|
*out_bh = bh;
|
|
|
|
|
|
|
|
failed_bh:
|
|
|
|
unlock_page(bh->b_page);
|
|
|
|
page_cache_release(bh->b_page);
|
|
|
|
brelse(bh);
|
|
|
|
failed:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
|
2009-11-13 08:09:47 -07:00
|
|
|
int readahead, struct buffer_head **out_bh)
|
2009-04-06 19:01:28 -07:00
|
|
|
{
|
|
|
|
struct buffer_head *first_bh, *bh;
|
|
|
|
unsigned long blkoff;
|
|
|
|
int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
|
|
|
|
if (err == -EEXIST) /* internal code */
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (unlikely(err))
|
|
|
|
goto failed;
|
|
|
|
|
2009-11-13 08:09:47 -07:00
|
|
|
if (readahead) {
|
|
|
|
blkoff = block + 1;
|
|
|
|
for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
|
|
|
|
err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
|
|
|
|
if (likely(!err || err == -EEXIST))
|
|
|
|
brelse(bh);
|
|
|
|
else if (err != -EBUSY)
|
|
|
|
break;
|
|
|
|
/* abort readahead if bmap lookup failed */
|
|
|
|
if (!buffer_locked(first_bh))
|
|
|
|
goto out_no_wait;
|
|
|
|
}
|
2009-04-06 19:01:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
wait_on_buffer(first_bh);
|
|
|
|
|
|
|
|
out_no_wait:
|
|
|
|
err = -EIO;
|
|
|
|
if (!buffer_uptodate(first_bh))
|
|
|
|
goto failed_bh;
|
|
|
|
out:
|
|
|
|
*out_bh = first_bh;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failed_bh:
|
|
|
|
brelse(first_bh);
|
|
|
|
failed:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nilfs_mdt_get_block - read or create a buffer on meta data file.
|
|
|
|
* @inode: inode of the meta data file
|
|
|
|
* @blkoff: block offset
|
|
|
|
* @create: create flag
|
|
|
|
* @init_block: initializer used for newly allocated block
|
|
|
|
* @out_bh: output of a pointer to the buffer_head
|
|
|
|
*
|
|
|
|
* nilfs_mdt_get_block() looks up the specified buffer and tries to create
|
|
|
|
* a new buffer if @create is not zero. On success, the returned buffer is
|
|
|
|
* assured to be either existing or formatted using a buffer lock on success.
|
|
|
|
* @out_bh is substituted only when zero is returned.
|
|
|
|
*
|
|
|
|
* Return Value: On success, it returns 0. On error, the following negative
|
|
|
|
* error code is returned.
|
|
|
|
*
|
|
|
|
* %-ENOMEM - Insufficient memory available.
|
|
|
|
*
|
|
|
|
* %-EIO - I/O error
|
|
|
|
*
|
|
|
|
* %-ENOENT - the specified block does not exist (hole block)
|
|
|
|
*
|
|
|
|
* %-EINVAL - bmap is broken. (the caller should call nilfs_error())
|
|
|
|
*
|
|
|
|
* %-EROFS - Read only filesystem (for create mode)
|
|
|
|
*/
|
|
|
|
int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
|
|
|
|
void (*init_block)(struct inode *,
|
|
|
|
struct buffer_head *, void *),
|
|
|
|
struct buffer_head **out_bh)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Should be rewritten with merging nilfs_mdt_read_block() */
|
|
|
|
retry:
|
2009-11-13 08:09:47 -07:00
|
|
|
ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
|
2009-04-06 19:01:28 -07:00
|
|
|
if (!create || ret != -ENOENT)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
|
|
|
|
if (unlikely(ret == -EEXIST)) {
|
|
|
|
/* create = 0; */ /* limit read-create loop retries */
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nilfs_mdt_delete_block - make a hole on the meta data file.
|
|
|
|
* @inode: inode of the meta data file
|
|
|
|
* @block: block offset
|
|
|
|
*
|
|
|
|
* Return Value: On success, zero is returned.
|
|
|
|
* On error, one of the following negative error code is returned.
|
|
|
|
*
|
|
|
|
* %-ENOMEM - Insufficient memory available.
|
|
|
|
*
|
|
|
|
* %-EIO - I/O error
|
|
|
|
*
|
|
|
|
* %-EINVAL - bmap is broken. (the caller should call nilfs_error())
|
|
|
|
*/
|
|
|
|
int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
|
|
|
|
{
|
|
|
|
struct nilfs_inode_info *ii = NILFS_I(inode);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = nilfs_bmap_delete(ii->i_bmap, block);
|
2009-05-05 05:52:06 -07:00
|
|
|
if (!err || err == -ENOENT) {
|
2009-04-06 19:01:28 -07:00
|
|
|
nilfs_mdt_mark_dirty(inode);
|
|
|
|
nilfs_mdt_forget_block(inode, block);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nilfs_mdt_forget_block - discard dirty state and try to remove the page
|
|
|
|
* @inode: inode of the meta data file
|
|
|
|
* @block: block offset
|
|
|
|
*
|
|
|
|
* nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
|
|
|
|
* tries to release the page including the buffer from a page cache.
|
|
|
|
*
|
|
|
|
* Return Value: On success, 0 is returned. On error, one of the following
|
|
|
|
* negative error code is returned.
|
|
|
|
*
|
|
|
|
* %-EBUSY - page has an active buffer.
|
|
|
|
*
|
|
|
|
* %-ENOENT - page cache has no page addressed by the offset.
|
|
|
|
*/
|
|
|
|
int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
|
|
|
|
{
|
|
|
|
pgoff_t index = (pgoff_t)block >>
|
|
|
|
(PAGE_CACHE_SHIFT - inode->i_blkbits);
|
|
|
|
struct page *page;
|
|
|
|
unsigned long first_block;
|
|
|
|
int ret = 0;
|
|
|
|
int still_dirty;
|
|
|
|
|
|
|
|
page = find_lock_page(inode->i_mapping, index);
|
|
|
|
if (!page)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
wait_on_page_writeback(page);
|
|
|
|
|
|
|
|
first_block = (unsigned long)index <<
|
|
|
|
(PAGE_CACHE_SHIFT - inode->i_blkbits);
|
|
|
|
if (page_has_buffers(page)) {
|
|
|
|
struct buffer_head *bh;
|
|
|
|
|
|
|
|
bh = nilfs_page_get_nth_block(page, block - first_block);
|
|
|
|
nilfs_forget_buffer(bh);
|
|
|
|
}
|
|
|
|
still_dirty = PageDirty(page);
|
|
|
|
unlock_page(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
|
|
|
|
if (still_dirty ||
|
|
|
|
invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
|
|
|
|
ret = -EBUSY;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
|
|
|
|
* @inode: inode of the meta data file
|
|
|
|
* @block: block offset
|
|
|
|
*
|
|
|
|
* Return Value: On success, it returns 0. On error, the following negative
|
|
|
|
* error code is returned.
|
|
|
|
*
|
|
|
|
* %-ENOMEM - Insufficient memory available.
|
|
|
|
*
|
|
|
|
* %-EIO - I/O error
|
|
|
|
*
|
|
|
|
* %-ENOENT - the specified block does not exist (hole block)
|
|
|
|
*
|
|
|
|
* %-EINVAL - bmap is broken. (the caller should call nilfs_error())
|
|
|
|
*/
|
|
|
|
int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
|
|
|
|
{
|
|
|
|
struct buffer_head *bh;
|
|
|
|
int err;
|
|
|
|
|
2009-11-13 08:09:47 -07:00
|
|
|
err = nilfs_mdt_read_block(inode, block, 0, &bh);
|
2009-04-06 19:01:28 -07:00
|
|
|
if (unlikely(err))
|
|
|
|
return err;
|
|
|
|
nilfs_mark_buffer_dirty(bh);
|
|
|
|
nilfs_mdt_mark_dirty(inode);
|
|
|
|
brelse(bh);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nilfs_mdt_fetch_dirty(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct nilfs_inode_info *ii = NILFS_I(inode);
|
|
|
|
|
|
|
|
if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
|
|
|
|
set_bit(NILFS_I_DIRTY, &ii->i_state);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return test_bit(NILFS_I_DIRTY, &ii->i_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
|
|
|
|
{
|
2010-09-05 20:05:43 -07:00
|
|
|
struct inode *inode;
|
|
|
|
struct super_block *sb;
|
|
|
|
struct the_nilfs *nilfs;
|
2009-04-06 19:01:28 -07:00
|
|
|
struct nilfs_sb_info *writer = NULL;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
redirty_page_for_writepage(wbc, page);
|
|
|
|
unlock_page(page);
|
|
|
|
|
2010-09-05 20:05:43 -07:00
|
|
|
inode = page->mapping->host;
|
|
|
|
if (!inode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sb = inode->i_sb;
|
|
|
|
nilfs = NILFS_MDT(inode)->mi_nilfs;
|
|
|
|
|
2009-04-06 19:01:28 -07:00
|
|
|
if (!sb) {
|
2009-08-02 06:45:33 -07:00
|
|
|
down_read(&nilfs->ns_writer_sem);
|
|
|
|
writer = nilfs->ns_writer;
|
2009-08-02 01:45:55 -07:00
|
|
|
if (!writer) {
|
2009-08-02 06:45:33 -07:00
|
|
|
up_read(&nilfs->ns_writer_sem);
|
2009-04-06 19:01:28 -07:00
|
|
|
return -EROFS;
|
2009-08-02 01:45:55 -07:00
|
|
|
}
|
2009-04-06 19:01:28 -07:00
|
|
|
sb = writer->s_super;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wbc->sync_mode == WB_SYNC_ALL)
|
|
|
|
err = nilfs_construct_segment(sb);
|
|
|
|
else if (wbc->for_reclaim)
|
|
|
|
nilfs_flush_segment(sb, inode->i_ino);
|
|
|
|
|
|
|
|
if (writer)
|
2009-08-02 06:45:33 -07:00
|
|
|
up_read(&nilfs->ns_writer_sem);
|
2009-04-06 19:01:28 -07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-21 17:01:10 -07:00
|
|
|
static const struct address_space_operations def_mdt_aops = {
|
2009-04-06 19:01:28 -07:00
|
|
|
.writepage = nilfs_mdt_write_page,
|
2009-05-27 06:44:34 -07:00
|
|
|
.sync_page = block_sync_page,
|
2009-04-06 19:01:28 -07:00
|
|
|
};
|
|
|
|
|
2009-09-21 17:01:11 -07:00
|
|
|
static const struct inode_operations def_mdt_iops;
|
2009-10-01 15:43:56 -07:00
|
|
|
static const struct file_operations def_mdt_fops;
|
2009-04-06 19:01:28 -07:00
|
|
|
|
2010-08-21 06:01:51 -07:00
|
|
|
|
2010-09-04 20:20:59 -07:00
|
|
|
int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
|
2010-08-21 06:01:51 -07:00
|
|
|
{
|
|
|
|
struct nilfs_mdt_info *mi;
|
|
|
|
|
|
|
|
mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
|
|
|
|
if (!mi)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-09-04 20:20:59 -07:00
|
|
|
mi->mi_nilfs = NILFS_I_NILFS(inode);
|
2010-08-21 06:01:51 -07:00
|
|
|
init_rwsem(&mi->mi_sem);
|
|
|
|
inode->i_private = mi;
|
|
|
|
|
|
|
|
inode->i_mode = S_IFREG;
|
|
|
|
mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
|
2010-09-04 20:20:59 -07:00
|
|
|
inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
|
|
|
|
|
|
|
|
inode->i_op = &def_mdt_iops;
|
|
|
|
inode->i_fop = &def_mdt_fops;
|
|
|
|
inode->i_mapping->a_ops = &def_mdt_aops;
|
2010-08-21 06:01:51 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-06 19:01:28 -07:00
|
|
|
/*
|
|
|
|
* NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
|
|
|
|
* ifile, or gcinodes. This allows the B-tree code and segment constructor
|
|
|
|
* to treat them like regular files, and this helps to simplify the
|
|
|
|
* implementation.
|
|
|
|
* On the other hand, some of the pseudo inodes have an irregular point:
|
|
|
|
* They don't have valid inode->i_sb pointer because their lifetimes are
|
|
|
|
* longer than those of the super block structs; they may continue for
|
|
|
|
* several consecutive mounts/umounts. This would need discussions.
|
|
|
|
*/
|
2009-11-12 06:42:04 -07:00
|
|
|
/**
|
|
|
|
* nilfs_mdt_new_common - allocate a pseudo inode for metadata file
|
|
|
|
* @nilfs: nilfs object
|
|
|
|
* @sb: super block instance the metadata file belongs to
|
|
|
|
* @ino: inode number
|
|
|
|
*/
|
2009-04-06 19:01:28 -07:00
|
|
|
struct inode *
|
|
|
|
nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
|
2010-08-21 06:01:51 -07:00
|
|
|
ino_t ino)
|
2009-04-06 19:01:28 -07:00
|
|
|
{
|
2009-05-27 06:11:46 -07:00
|
|
|
struct inode *inode = nilfs_alloc_inode_common(nilfs);
|
2009-04-06 19:01:28 -07:00
|
|
|
|
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
|
|
|
else {
|
|
|
|
struct address_space * const mapping = &inode->i_data;
|
|
|
|
|
|
|
|
inode->i_sb = sb; /* sb may be NULL for some meta data files */
|
|
|
|
inode->i_blkbits = nilfs->ns_blocksize_bits;
|
|
|
|
inode->i_flags = 0;
|
|
|
|
atomic_set(&inode->i_count, 1);
|
|
|
|
inode->i_nlink = 1;
|
|
|
|
inode->i_ino = ino;
|
|
|
|
|
|
|
|
#ifdef INIT_UNUSED_INODE_FIELDS
|
|
|
|
atomic_set(&inode->i_writecount, 0);
|
|
|
|
inode->i_size = 0;
|
|
|
|
inode->i_blocks = 0;
|
|
|
|
inode->i_bytes = 0;
|
|
|
|
inode->i_generation = 0;
|
|
|
|
#ifdef CONFIG_QUOTA
|
|
|
|
memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
|
|
|
|
#endif
|
|
|
|
inode->i_pipe = NULL;
|
|
|
|
inode->i_bdev = NULL;
|
|
|
|
inode->i_cdev = NULL;
|
|
|
|
inode->i_rdev = 0;
|
|
|
|
#ifdef CONFIG_SECURITY
|
|
|
|
inode->i_security = NULL;
|
|
|
|
#endif
|
|
|
|
inode->dirtied_when = 0;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&inode->i_list);
|
|
|
|
INIT_LIST_HEAD(&inode->i_sb_list);
|
|
|
|
inode->i_state = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
spin_lock_init(&inode->i_lock);
|
|
|
|
mutex_init(&inode->i_mutex);
|
|
|
|
init_rwsem(&inode->i_alloc_sem);
|
|
|
|
|
|
|
|
mapping->host = NULL; /* instead of inode */
|
|
|
|
mapping->flags = 0;
|
|
|
|
mapping->assoc_mapping = NULL;
|
|
|
|
|
|
|
|
inode->i_mapping = mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
|
2009-11-12 06:42:04 -07:00
|
|
|
ino_t ino, size_t objsz)
|
2009-04-06 19:01:28 -07:00
|
|
|
{
|
2009-11-12 06:42:04 -07:00
|
|
|
struct inode *inode;
|
2009-04-06 19:01:28 -07:00
|
|
|
|
2010-08-21 06:01:51 -07:00
|
|
|
inode = nilfs_mdt_new_common(nilfs, sb, ino);
|
2009-04-06 19:01:28 -07:00
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
|
|
|
|
2010-09-04 20:20:59 -07:00
|
|
|
if (nilfs_mdt_init(inode, NILFS_MDT_GFP, objsz) < 0) {
|
2010-08-21 06:01:51 -07:00
|
|
|
nilfs_destroy_inode(inode);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-06 19:01:28 -07:00
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
|
|
|
|
unsigned header_size)
|
|
|
|
{
|
|
|
|
struct nilfs_mdt_info *mi = NILFS_MDT(inode);
|
|
|
|
|
|
|
|
mi->mi_entry_size = entry_size;
|
|
|
|
mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
|
|
|
|
mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
|
|
|
|
}
|
|
|
|
|
2010-09-05 20:05:43 -07:00
|
|
|
static const struct address_space_operations shadow_map_aops = {
|
|
|
|
.sync_page = block_sync_page,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
|
|
|
|
* @inode: inode of the metadata file
|
|
|
|
* @shadow: shadow mapping
|
|
|
|
*/
|
|
|
|
int nilfs_mdt_setup_shadow_map(struct inode *inode,
|
|
|
|
struct nilfs_shadow_map *shadow)
|
|
|
|
{
|
|
|
|
struct nilfs_mdt_info *mi = NILFS_MDT(inode);
|
|
|
|
struct backing_dev_info *bdi = NILFS_I_NILFS(inode)->ns_bdi;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&shadow->frozen_buffers);
|
|
|
|
nilfs_mapping_init_once(&shadow->frozen_data);
|
|
|
|
nilfs_mapping_init(&shadow->frozen_data, bdi, &shadow_map_aops);
|
|
|
|
nilfs_mapping_init_once(&shadow->frozen_btnodes);
|
|
|
|
nilfs_mapping_init(&shadow->frozen_btnodes, bdi, &shadow_map_aops);
|
|
|
|
mi->mi_shadow = shadow;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
|
|
|
|
* @inode: inode of the metadata file
|
|
|
|
*/
|
|
|
|
int nilfs_mdt_save_to_shadow_map(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct nilfs_mdt_info *mi = NILFS_MDT(inode);
|
|
|
|
struct nilfs_inode_info *ii = NILFS_I(inode);
|
|
|
|
struct nilfs_shadow_map *shadow = mi->mi_shadow;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
|
|
|
|
&ii->i_btnode_cache);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-30 19:40:34 -07:00
|
|
|
int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
|
|
|
|
{
|
|
|
|
struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
|
|
|
|
struct buffer_head *bh_frozen;
|
|
|
|
struct page *page;
|
|
|
|
int blkbits = inode->i_blkbits;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
|
|
|
|
if (!page)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!page_has_buffers(page))
|
|
|
|
create_empty_buffers(page, 1 << blkbits, 0);
|
|
|
|
|
|
|
|
bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
|
|
|
|
if (bh_frozen) {
|
|
|
|
if (!buffer_uptodate(bh_frozen))
|
|
|
|
nilfs_copy_buffer(bh_frozen, bh);
|
|
|
|
if (list_empty(&bh_frozen->b_assoc_buffers)) {
|
|
|
|
list_add_tail(&bh_frozen->b_assoc_buffers,
|
|
|
|
&shadow->frozen_buffers);
|
|
|
|
set_buffer_nilfs_redirected(bh);
|
|
|
|
} else {
|
|
|
|
brelse(bh_frozen); /* already frozen */
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
unlock_page(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct buffer_head *
|
|
|
|
nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
|
|
|
|
{
|
|
|
|
struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
|
|
|
|
struct buffer_head *bh_frozen = NULL;
|
|
|
|
struct page *page;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
|
|
|
|
if (page) {
|
|
|
|
if (page_has_buffers(page)) {
|
|
|
|
n = bh_offset(bh) >> inode->i_blkbits;
|
|
|
|
bh_frozen = nilfs_page_get_nth_block(page, n);
|
|
|
|
}
|
|
|
|
unlock_page(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
}
|
|
|
|
return bh_frozen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
|
|
|
|
{
|
|
|
|
struct list_head *head = &shadow->frozen_buffers;
|
|
|
|
struct buffer_head *bh;
|
|
|
|
|
|
|
|
while (!list_empty(head)) {
|
|
|
|
bh = list_first_entry(head, struct buffer_head,
|
|
|
|
b_assoc_buffers);
|
|
|
|
list_del_init(&bh->b_assoc_buffers);
|
|
|
|
brelse(bh); /* drop ref-count to make it releasable */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-05 20:05:43 -07:00
|
|
|
/**
|
|
|
|
* nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
|
|
|
|
* @inode: inode of the metadata file
|
|
|
|
*/
|
|
|
|
void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct nilfs_mdt_info *mi = NILFS_MDT(inode);
|
|
|
|
struct nilfs_inode_info *ii = NILFS_I(inode);
|
|
|
|
struct nilfs_shadow_map *shadow = mi->mi_shadow;
|
|
|
|
|
|
|
|
down_write(&mi->mi_sem);
|
|
|
|
|
|
|
|
if (mi->mi_palloc_cache)
|
|
|
|
nilfs_palloc_clear_cache(inode);
|
|
|
|
|
|
|
|
nilfs_clear_dirty_pages(inode->i_mapping);
|
|
|
|
nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
|
|
|
|
|
|
|
|
nilfs_clear_dirty_pages(&ii->i_btnode_cache);
|
|
|
|
nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
|
|
|
|
|
|
|
|
nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
|
|
|
|
|
|
|
|
up_write(&mi->mi_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
|
|
|
|
* @inode: inode of the metadata file
|
|
|
|
*/
|
|
|
|
void nilfs_mdt_clear_shadow_map(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct nilfs_mdt_info *mi = NILFS_MDT(inode);
|
|
|
|
struct nilfs_shadow_map *shadow = mi->mi_shadow;
|
|
|
|
|
|
|
|
down_write(&mi->mi_sem);
|
2010-08-30 19:40:34 -07:00
|
|
|
nilfs_release_frozen_buffers(shadow);
|
2010-09-05 20:05:43 -07:00
|
|
|
truncate_inode_pages(&shadow->frozen_data, 0);
|
|
|
|
truncate_inode_pages(&shadow->frozen_btnodes, 0);
|
|
|
|
up_write(&mi->mi_sem);
|
|
|
|
}
|
|
|
|
|
2009-11-12 10:25:41 -07:00
|
|
|
static void nilfs_mdt_clear(struct inode *inode)
|
2009-04-06 19:01:28 -07:00
|
|
|
{
|
|
|
|
struct nilfs_inode_info *ii = NILFS_I(inode);
|
|
|
|
|
|
|
|
invalidate_mapping_pages(inode->i_mapping, 0, -1);
|
|
|
|
truncate_inode_pages(inode->i_mapping, 0);
|
|
|
|
|
2009-11-12 10:25:41 -07:00
|
|
|
if (test_bit(NILFS_I_BMAP, &ii->i_state))
|
|
|
|
nilfs_bmap_clear(ii->i_bmap);
|
2009-04-06 19:01:28 -07:00
|
|
|
nilfs_btnode_cache_clear(&ii->i_btnode_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nilfs_mdt_destroy(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
|
|
|
|
|
2009-11-13 23:54:27 -07:00
|
|
|
if (mdi->mi_palloc_cache)
|
|
|
|
nilfs_palloc_destroy_cache(inode);
|
2009-11-12 10:25:41 -07:00
|
|
|
nilfs_mdt_clear(inode);
|
|
|
|
|
2009-04-06 19:01:28 -07:00
|
|
|
nilfs_destroy_inode(inode);
|
|
|
|
}
|