xref: /openbmc/linux/fs/nilfs2/inode.c (revision 263d90cefc7d82a01c296c59532ff59d67c63509)
105fe58fdSRyusuke Konishi /*
205fe58fdSRyusuke Konishi  * inode.c - NILFS inode operations.
305fe58fdSRyusuke Konishi  *
405fe58fdSRyusuke Konishi  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
505fe58fdSRyusuke Konishi  *
605fe58fdSRyusuke Konishi  * This program is free software; you can redistribute it and/or modify
705fe58fdSRyusuke Konishi  * it under the terms of the GNU General Public License as published by
805fe58fdSRyusuke Konishi  * the Free Software Foundation; either version 2 of the License, or
905fe58fdSRyusuke Konishi  * (at your option) any later version.
1005fe58fdSRyusuke Konishi  *
1105fe58fdSRyusuke Konishi  * This program is distributed in the hope that it will be useful,
1205fe58fdSRyusuke Konishi  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1305fe58fdSRyusuke Konishi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1405fe58fdSRyusuke Konishi  * GNU General Public License for more details.
1505fe58fdSRyusuke Konishi  *
1605fe58fdSRyusuke Konishi  * You should have received a copy of the GNU General Public License
1705fe58fdSRyusuke Konishi  * along with this program; if not, write to the Free Software
1805fe58fdSRyusuke Konishi  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1905fe58fdSRyusuke Konishi  *
2005fe58fdSRyusuke Konishi  * Written by Ryusuke Konishi <ryusuke@osrg.net>
2105fe58fdSRyusuke Konishi  *
2205fe58fdSRyusuke Konishi  */
2305fe58fdSRyusuke Konishi 
2405fe58fdSRyusuke Konishi #include <linux/buffer_head.h>
255a0e3ad6STejun Heo #include <linux/gfp.h>
2605fe58fdSRyusuke Konishi #include <linux/mpage.h>
2705fe58fdSRyusuke Konishi #include <linux/writeback.h>
28f30bf3e4SRyusuke Konishi #include <linux/uio.h>
2905fe58fdSRyusuke Konishi #include "nilfs.h"
306fd1e5c9SAl Viro #include "btnode.h"
3105fe58fdSRyusuke Konishi #include "segment.h"
3205fe58fdSRyusuke Konishi #include "page.h"
3305fe58fdSRyusuke Konishi #include "mdt.h"
3405fe58fdSRyusuke Konishi #include "cpfile.h"
3505fe58fdSRyusuke Konishi #include "ifile.h"
3605fe58fdSRyusuke Konishi 
370e14a359SRyusuke Konishi struct nilfs_iget_args {
380e14a359SRyusuke Konishi 	u64 ino;
390e14a359SRyusuke Konishi 	__u64 cno;
400e14a359SRyusuke Konishi 	int for_gc;
410e14a359SRyusuke Konishi };
4205fe58fdSRyusuke Konishi 
4305fe58fdSRyusuke Konishi /**
4405fe58fdSRyusuke Konishi  * nilfs_get_block() - get a file block on the filesystem (callback function)
4505fe58fdSRyusuke Konishi  * @inode - inode struct of the target file
4605fe58fdSRyusuke Konishi  * @blkoff - file block number
4705fe58fdSRyusuke Konishi  * @bh_result - buffer head to be mapped on
4805fe58fdSRyusuke Konishi  * @create - indicate whether allocating the block or not when it has not
4905fe58fdSRyusuke Konishi  *      been allocated yet.
5005fe58fdSRyusuke Konishi  *
5105fe58fdSRyusuke Konishi  * This function does not issue actual read request of the specified data
5205fe58fdSRyusuke Konishi  * block. It is done by VFS.
5305fe58fdSRyusuke Konishi  */
5405fe58fdSRyusuke Konishi int nilfs_get_block(struct inode *inode, sector_t blkoff,
5505fe58fdSRyusuke Konishi 		    struct buffer_head *bh_result, int create)
5605fe58fdSRyusuke Konishi {
5705fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
58c3a7abf0SRyusuke Konishi 	__u64 blknum = 0;
5905fe58fdSRyusuke Konishi 	int err = 0, ret;
6005fe58fdSRyusuke Konishi 	struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
61c3a7abf0SRyusuke Konishi 	unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
6205fe58fdSRyusuke Konishi 
63c3a7abf0SRyusuke Konishi 	down_read(&NILFS_MDT(dat)->mi_sem);
64c3a7abf0SRyusuke Konishi 	ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
65c3a7abf0SRyusuke Konishi 	up_read(&NILFS_MDT(dat)->mi_sem);
66c3a7abf0SRyusuke Konishi 	if (ret >= 0) {	/* found */
6705fe58fdSRyusuke Konishi 		map_bh(bh_result, inode->i_sb, blknum);
68c3a7abf0SRyusuke Konishi 		if (ret > 0)
69c3a7abf0SRyusuke Konishi 			bh_result->b_size = (ret << inode->i_blkbits);
7005fe58fdSRyusuke Konishi 		goto out;
7105fe58fdSRyusuke Konishi 	}
7205fe58fdSRyusuke Konishi 	/* data block was not found */
7305fe58fdSRyusuke Konishi 	if (ret == -ENOENT && create) {
7405fe58fdSRyusuke Konishi 		struct nilfs_transaction_info ti;
7505fe58fdSRyusuke Konishi 
7605fe58fdSRyusuke Konishi 		bh_result->b_blocknr = 0;
7705fe58fdSRyusuke Konishi 		err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
7805fe58fdSRyusuke Konishi 		if (unlikely(err))
7905fe58fdSRyusuke Konishi 			goto out;
8005fe58fdSRyusuke Konishi 		err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
8105fe58fdSRyusuke Konishi 					(unsigned long)bh_result);
8205fe58fdSRyusuke Konishi 		if (unlikely(err != 0)) {
8305fe58fdSRyusuke Konishi 			if (err == -EEXIST) {
8405fe58fdSRyusuke Konishi 				/*
8505fe58fdSRyusuke Konishi 				 * The get_block() function could be called
8605fe58fdSRyusuke Konishi 				 * from multiple callers for an inode.
8705fe58fdSRyusuke Konishi 				 * However, the page having this block must
8805fe58fdSRyusuke Konishi 				 * be locked in this case.
8905fe58fdSRyusuke Konishi 				 */
901f5abe7eSRyusuke Konishi 				printk(KERN_WARNING
9105fe58fdSRyusuke Konishi 				       "nilfs_get_block: a race condition "
9205fe58fdSRyusuke Konishi 				       "while inserting a data block. "
9305fe58fdSRyusuke Konishi 				       "(inode number=%lu, file block "
9405fe58fdSRyusuke Konishi 				       "offset=%llu)\n",
9505fe58fdSRyusuke Konishi 				       inode->i_ino,
9605fe58fdSRyusuke Konishi 				       (unsigned long long)blkoff);
971f5abe7eSRyusuke Konishi 				err = 0;
9805fe58fdSRyusuke Konishi 			} else if (err == -EINVAL) {
9905fe58fdSRyusuke Konishi 				nilfs_error(inode->i_sb, __func__,
10005fe58fdSRyusuke Konishi 					    "broken bmap (inode=%lu)\n",
10105fe58fdSRyusuke Konishi 					    inode->i_ino);
10205fe58fdSRyusuke Konishi 				err = -EIO;
10305fe58fdSRyusuke Konishi 			}
10447420c79SRyusuke Konishi 			nilfs_transaction_abort(inode->i_sb);
10505fe58fdSRyusuke Konishi 			goto out;
10605fe58fdSRyusuke Konishi 		}
107abdb318bSJiro SEKIBA 		nilfs_mark_inode_dirty(inode);
10847420c79SRyusuke Konishi 		nilfs_transaction_commit(inode->i_sb); /* never fails */
10905fe58fdSRyusuke Konishi 		/* Error handling should be detailed */
11005fe58fdSRyusuke Konishi 		set_buffer_new(bh_result);
11105fe58fdSRyusuke Konishi 		map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
11205fe58fdSRyusuke Konishi 						      to proper value */
11305fe58fdSRyusuke Konishi 	} else if (ret == -ENOENT) {
11405fe58fdSRyusuke Konishi 		/* not found is not error (e.g. hole); must return without
11505fe58fdSRyusuke Konishi 		   the mapped state flag. */
11605fe58fdSRyusuke Konishi 		;
11705fe58fdSRyusuke Konishi 	} else {
11805fe58fdSRyusuke Konishi 		err = ret;
11905fe58fdSRyusuke Konishi 	}
12005fe58fdSRyusuke Konishi 
12105fe58fdSRyusuke Konishi  out:
12205fe58fdSRyusuke Konishi 	return err;
12305fe58fdSRyusuke Konishi }
12405fe58fdSRyusuke Konishi 
12505fe58fdSRyusuke Konishi /**
12605fe58fdSRyusuke Konishi  * nilfs_readpage() - implement readpage() method of nilfs_aops {}
12705fe58fdSRyusuke Konishi  * address_space_operations.
12805fe58fdSRyusuke Konishi  * @file - file struct of the file to be read
12905fe58fdSRyusuke Konishi  * @page - the page to be read
13005fe58fdSRyusuke Konishi  */
13105fe58fdSRyusuke Konishi static int nilfs_readpage(struct file *file, struct page *page)
13205fe58fdSRyusuke Konishi {
13305fe58fdSRyusuke Konishi 	return mpage_readpage(page, nilfs_get_block);
13405fe58fdSRyusuke Konishi }
13505fe58fdSRyusuke Konishi 
13605fe58fdSRyusuke Konishi /**
13705fe58fdSRyusuke Konishi  * nilfs_readpages() - implement readpages() method of nilfs_aops {}
13805fe58fdSRyusuke Konishi  * address_space_operations.
13905fe58fdSRyusuke Konishi  * @file - file struct of the file to be read
14005fe58fdSRyusuke Konishi  * @mapping - address_space struct used for reading multiple pages
14105fe58fdSRyusuke Konishi  * @pages - the pages to be read
14205fe58fdSRyusuke Konishi  * @nr_pages - number of pages to be read
14305fe58fdSRyusuke Konishi  */
14405fe58fdSRyusuke Konishi static int nilfs_readpages(struct file *file, struct address_space *mapping,
14505fe58fdSRyusuke Konishi 			   struct list_head *pages, unsigned nr_pages)
14605fe58fdSRyusuke Konishi {
14705fe58fdSRyusuke Konishi 	return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
14805fe58fdSRyusuke Konishi }
14905fe58fdSRyusuke Konishi 
15005fe58fdSRyusuke Konishi static int nilfs_writepages(struct address_space *mapping,
15105fe58fdSRyusuke Konishi 			    struct writeback_control *wbc)
15205fe58fdSRyusuke Konishi {
153f30bf3e4SRyusuke Konishi 	struct inode *inode = mapping->host;
154f30bf3e4SRyusuke Konishi 	int err = 0;
155f30bf3e4SRyusuke Konishi 
156f30bf3e4SRyusuke Konishi 	if (wbc->sync_mode == WB_SYNC_ALL)
157f30bf3e4SRyusuke Konishi 		err = nilfs_construct_dsync_segment(inode->i_sb, inode,
158f30bf3e4SRyusuke Konishi 						    wbc->range_start,
159f30bf3e4SRyusuke Konishi 						    wbc->range_end);
160f30bf3e4SRyusuke Konishi 	return err;
16105fe58fdSRyusuke Konishi }
16205fe58fdSRyusuke Konishi 
16305fe58fdSRyusuke Konishi static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
16405fe58fdSRyusuke Konishi {
16505fe58fdSRyusuke Konishi 	struct inode *inode = page->mapping->host;
16605fe58fdSRyusuke Konishi 	int err;
16705fe58fdSRyusuke Konishi 
16805fe58fdSRyusuke Konishi 	redirty_page_for_writepage(wbc, page);
16905fe58fdSRyusuke Konishi 	unlock_page(page);
17005fe58fdSRyusuke Konishi 
17105fe58fdSRyusuke Konishi 	if (wbc->sync_mode == WB_SYNC_ALL) {
17205fe58fdSRyusuke Konishi 		err = nilfs_construct_segment(inode->i_sb);
17305fe58fdSRyusuke Konishi 		if (unlikely(err))
17405fe58fdSRyusuke Konishi 			return err;
17505fe58fdSRyusuke Konishi 	} else if (wbc->for_reclaim)
17605fe58fdSRyusuke Konishi 		nilfs_flush_segment(inode->i_sb, inode->i_ino);
17705fe58fdSRyusuke Konishi 
17805fe58fdSRyusuke Konishi 	return 0;
17905fe58fdSRyusuke Konishi }
18005fe58fdSRyusuke Konishi 
18105fe58fdSRyusuke Konishi static int nilfs_set_page_dirty(struct page *page)
18205fe58fdSRyusuke Konishi {
18305fe58fdSRyusuke Konishi 	int ret = __set_page_dirty_buffers(page);
18405fe58fdSRyusuke Konishi 
18505fe58fdSRyusuke Konishi 	if (ret) {
18605fe58fdSRyusuke Konishi 		struct inode *inode = page->mapping->host;
18705fe58fdSRyusuke Konishi 		struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
18805fe58fdSRyusuke Konishi 		unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
18905fe58fdSRyusuke Konishi 
19005fe58fdSRyusuke Konishi 		nilfs_set_file_dirty(sbi, inode, nr_dirty);
19105fe58fdSRyusuke Konishi 	}
19205fe58fdSRyusuke Konishi 	return ret;
19305fe58fdSRyusuke Konishi }
19405fe58fdSRyusuke Konishi 
19505fe58fdSRyusuke Konishi static int nilfs_write_begin(struct file *file, struct address_space *mapping,
19605fe58fdSRyusuke Konishi 			     loff_t pos, unsigned len, unsigned flags,
19705fe58fdSRyusuke Konishi 			     struct page **pagep, void **fsdata)
19805fe58fdSRyusuke Konishi 
19905fe58fdSRyusuke Konishi {
20005fe58fdSRyusuke Konishi 	struct inode *inode = mapping->host;
20105fe58fdSRyusuke Konishi 	int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
20205fe58fdSRyusuke Konishi 
20305fe58fdSRyusuke Konishi 	if (unlikely(err))
20405fe58fdSRyusuke Konishi 		return err;
20505fe58fdSRyusuke Konishi 
206155130a4SChristoph Hellwig 	err = block_write_begin(mapping, pos, len, flags, pagep,
207155130a4SChristoph Hellwig 				nilfs_get_block);
208155130a4SChristoph Hellwig 	if (unlikely(err)) {
209155130a4SChristoph Hellwig 		loff_t isize = mapping->host->i_size;
210155130a4SChristoph Hellwig 		if (pos + len > isize)
211155130a4SChristoph Hellwig 			vmtruncate(mapping->host, isize);
212155130a4SChristoph Hellwig 
21347420c79SRyusuke Konishi 		nilfs_transaction_abort(inode->i_sb);
214155130a4SChristoph Hellwig 	}
21505fe58fdSRyusuke Konishi 	return err;
21605fe58fdSRyusuke Konishi }
21705fe58fdSRyusuke Konishi 
21805fe58fdSRyusuke Konishi static int nilfs_write_end(struct file *file, struct address_space *mapping,
21905fe58fdSRyusuke Konishi 			   loff_t pos, unsigned len, unsigned copied,
22005fe58fdSRyusuke Konishi 			   struct page *page, void *fsdata)
22105fe58fdSRyusuke Konishi {
22205fe58fdSRyusuke Konishi 	struct inode *inode = mapping->host;
22305fe58fdSRyusuke Konishi 	unsigned start = pos & (PAGE_CACHE_SIZE - 1);
22405fe58fdSRyusuke Konishi 	unsigned nr_dirty;
22505fe58fdSRyusuke Konishi 	int err;
22605fe58fdSRyusuke Konishi 
22705fe58fdSRyusuke Konishi 	nr_dirty = nilfs_page_count_clean_buffers(page, start,
22805fe58fdSRyusuke Konishi 						  start + copied);
22905fe58fdSRyusuke Konishi 	copied = generic_write_end(file, mapping, pos, len, copied, page,
23005fe58fdSRyusuke Konishi 				   fsdata);
23105fe58fdSRyusuke Konishi 	nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
23247420c79SRyusuke Konishi 	err = nilfs_transaction_commit(inode->i_sb);
23305fe58fdSRyusuke Konishi 	return err ? : copied;
23405fe58fdSRyusuke Konishi }
23505fe58fdSRyusuke Konishi 
23605fe58fdSRyusuke Konishi static ssize_t
23705fe58fdSRyusuke Konishi nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
23805fe58fdSRyusuke Konishi 		loff_t offset, unsigned long nr_segs)
23905fe58fdSRyusuke Konishi {
24005fe58fdSRyusuke Konishi 	struct file *file = iocb->ki_filp;
24105fe58fdSRyusuke Konishi 	struct inode *inode = file->f_mapping->host;
24205fe58fdSRyusuke Konishi 	ssize_t size;
24305fe58fdSRyusuke Konishi 
24405fe58fdSRyusuke Konishi 	if (rw == WRITE)
24505fe58fdSRyusuke Konishi 		return 0;
24605fe58fdSRyusuke Konishi 
24705fe58fdSRyusuke Konishi 	/* Needs synchronization with the cleaner */
24805fe58fdSRyusuke Konishi 	size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
24905fe58fdSRyusuke Konishi 				  offset, nr_segs, nilfs_get_block, NULL);
250eafdc7d1SChristoph Hellwig 
251eafdc7d1SChristoph Hellwig 	/*
252eafdc7d1SChristoph Hellwig 	 * In case of error extending write may have instantiated a few
253eafdc7d1SChristoph Hellwig 	 * blocks outside i_size. Trim these off again.
254eafdc7d1SChristoph Hellwig 	 */
255eafdc7d1SChristoph Hellwig 	if (unlikely((rw & WRITE) && size < 0)) {
256eafdc7d1SChristoph Hellwig 		loff_t isize = i_size_read(inode);
257eafdc7d1SChristoph Hellwig 		loff_t end = offset + iov_length(iov, nr_segs);
258eafdc7d1SChristoph Hellwig 
259eafdc7d1SChristoph Hellwig 		if (end > isize)
260eafdc7d1SChristoph Hellwig 			vmtruncate(inode, isize);
261eafdc7d1SChristoph Hellwig 	}
262eafdc7d1SChristoph Hellwig 
26305fe58fdSRyusuke Konishi 	return size;
26405fe58fdSRyusuke Konishi }
26505fe58fdSRyusuke Konishi 
2667f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = {
26705fe58fdSRyusuke Konishi 	.writepage		= nilfs_writepage,
26805fe58fdSRyusuke Konishi 	.readpage		= nilfs_readpage,
269e85dc1d5SRyusuke Konishi 	.sync_page		= block_sync_page,
27005fe58fdSRyusuke Konishi 	.writepages		= nilfs_writepages,
27105fe58fdSRyusuke Konishi 	.set_page_dirty		= nilfs_set_page_dirty,
27205fe58fdSRyusuke Konishi 	.readpages		= nilfs_readpages,
27305fe58fdSRyusuke Konishi 	.write_begin		= nilfs_write_begin,
27405fe58fdSRyusuke Konishi 	.write_end		= nilfs_write_end,
27505fe58fdSRyusuke Konishi 	/* .releasepage		= nilfs_releasepage, */
27605fe58fdSRyusuke Konishi 	.invalidatepage		= block_invalidatepage,
27705fe58fdSRyusuke Konishi 	.direct_IO		= nilfs_direct_IO,
278258ef67eSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
27905fe58fdSRyusuke Konishi };
28005fe58fdSRyusuke Konishi 
28105fe58fdSRyusuke Konishi struct inode *nilfs_new_inode(struct inode *dir, int mode)
28205fe58fdSRyusuke Konishi {
28305fe58fdSRyusuke Konishi 	struct super_block *sb = dir->i_sb;
28405fe58fdSRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
28505fe58fdSRyusuke Konishi 	struct inode *inode;
28605fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii;
28705fe58fdSRyusuke Konishi 	int err = -ENOMEM;
28805fe58fdSRyusuke Konishi 	ino_t ino;
28905fe58fdSRyusuke Konishi 
29005fe58fdSRyusuke Konishi 	inode = new_inode(sb);
29105fe58fdSRyusuke Konishi 	if (unlikely(!inode))
29205fe58fdSRyusuke Konishi 		goto failed;
29305fe58fdSRyusuke Konishi 
29405fe58fdSRyusuke Konishi 	mapping_set_gfp_mask(inode->i_mapping,
29505fe58fdSRyusuke Konishi 			     mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
29605fe58fdSRyusuke Konishi 
29705fe58fdSRyusuke Konishi 	ii = NILFS_I(inode);
29805fe58fdSRyusuke Konishi 	ii->i_state = 1 << NILFS_I_NEW;
29905fe58fdSRyusuke Konishi 
30005fe58fdSRyusuke Konishi 	err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
30105fe58fdSRyusuke Konishi 	if (unlikely(err))
30205fe58fdSRyusuke Konishi 		goto failed_ifile_create_inode;
30305fe58fdSRyusuke Konishi 	/* reference count of i_bh inherits from nilfs_mdt_read_block() */
30405fe58fdSRyusuke Konishi 
30505fe58fdSRyusuke Konishi 	atomic_inc(&sbi->s_inodes_count);
30673459dccSDmitry Monakhov 	inode_init_owner(inode, dir, mode);
30705fe58fdSRyusuke Konishi 	inode->i_ino = ino;
30805fe58fdSRyusuke Konishi 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
30905fe58fdSRyusuke Konishi 
31005fe58fdSRyusuke Konishi 	if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
31105fe58fdSRyusuke Konishi 		err = nilfs_bmap_read(ii->i_bmap, NULL);
31205fe58fdSRyusuke Konishi 		if (err < 0)
31305fe58fdSRyusuke Konishi 			goto failed_bmap;
31405fe58fdSRyusuke Konishi 
31505fe58fdSRyusuke Konishi 		set_bit(NILFS_I_BMAP, &ii->i_state);
31605fe58fdSRyusuke Konishi 		/* No lock is needed; iget() ensures it. */
31705fe58fdSRyusuke Konishi 	}
31805fe58fdSRyusuke Konishi 
31905fe58fdSRyusuke Konishi 	ii->i_flags = NILFS_I(dir)->i_flags;
32005fe58fdSRyusuke Konishi 	if (S_ISLNK(mode))
32105fe58fdSRyusuke Konishi 		ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
32205fe58fdSRyusuke Konishi 	if (!S_ISDIR(mode))
32305fe58fdSRyusuke Konishi 		ii->i_flags &= ~NILFS_DIRSYNC_FL;
32405fe58fdSRyusuke Konishi 
32505fe58fdSRyusuke Konishi 	/* ii->i_file_acl = 0; */
32605fe58fdSRyusuke Konishi 	/* ii->i_dir_acl = 0; */
32705fe58fdSRyusuke Konishi 	ii->i_dir_start_lookup = 0;
32805fe58fdSRyusuke Konishi 	nilfs_set_inode_flags(inode);
32905fe58fdSRyusuke Konishi 	spin_lock(&sbi->s_next_gen_lock);
33005fe58fdSRyusuke Konishi 	inode->i_generation = sbi->s_next_generation++;
33105fe58fdSRyusuke Konishi 	spin_unlock(&sbi->s_next_gen_lock);
33205fe58fdSRyusuke Konishi 	insert_inode_hash(inode);
33305fe58fdSRyusuke Konishi 
33405fe58fdSRyusuke Konishi 	err = nilfs_init_acl(inode, dir);
33505fe58fdSRyusuke Konishi 	if (unlikely(err))
33605fe58fdSRyusuke Konishi 		goto failed_acl; /* never occur. When supporting
33705fe58fdSRyusuke Konishi 				    nilfs_init_acl(), proper cancellation of
33805fe58fdSRyusuke Konishi 				    above jobs should be considered */
33905fe58fdSRyusuke Konishi 
34005fe58fdSRyusuke Konishi 	return inode;
34105fe58fdSRyusuke Konishi 
34205fe58fdSRyusuke Konishi  failed_acl:
34305fe58fdSRyusuke Konishi  failed_bmap:
34405fe58fdSRyusuke Konishi 	inode->i_nlink = 0;
34505fe58fdSRyusuke Konishi 	iput(inode);  /* raw_inode will be deleted through
34605fe58fdSRyusuke Konishi 			 generic_delete_inode() */
34705fe58fdSRyusuke Konishi 	goto failed;
34805fe58fdSRyusuke Konishi 
34905fe58fdSRyusuke Konishi  failed_ifile_create_inode:
35005fe58fdSRyusuke Konishi 	make_bad_inode(inode);
35105fe58fdSRyusuke Konishi 	iput(inode);  /* if i_nlink == 1, generic_forget_inode() will be
35205fe58fdSRyusuke Konishi 			 called */
35305fe58fdSRyusuke Konishi  failed:
35405fe58fdSRyusuke Konishi 	return ERR_PTR(err);
35505fe58fdSRyusuke Konishi }
35605fe58fdSRyusuke Konishi 
35705fe58fdSRyusuke Konishi void nilfs_free_inode(struct inode *inode)
35805fe58fdSRyusuke Konishi {
35905fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
36005fe58fdSRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
36105fe58fdSRyusuke Konishi 
36205fe58fdSRyusuke Konishi 	/* XXX: check error code? Is there any thing I can do? */
36305fe58fdSRyusuke Konishi 	(void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
36405fe58fdSRyusuke Konishi 	atomic_dec(&sbi->s_inodes_count);
36505fe58fdSRyusuke Konishi }
36605fe58fdSRyusuke Konishi 
36705fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode)
36805fe58fdSRyusuke Konishi {
36905fe58fdSRyusuke Konishi 	unsigned int flags = NILFS_I(inode)->i_flags;
37005fe58fdSRyusuke Konishi 
37105fe58fdSRyusuke Konishi 	inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
37205fe58fdSRyusuke Konishi 			    S_DIRSYNC);
37305fe58fdSRyusuke Konishi 	if (flags & NILFS_SYNC_FL)
37405fe58fdSRyusuke Konishi 		inode->i_flags |= S_SYNC;
37505fe58fdSRyusuke Konishi 	if (flags & NILFS_APPEND_FL)
37605fe58fdSRyusuke Konishi 		inode->i_flags |= S_APPEND;
37705fe58fdSRyusuke Konishi 	if (flags & NILFS_IMMUTABLE_FL)
37805fe58fdSRyusuke Konishi 		inode->i_flags |= S_IMMUTABLE;
37905fe58fdSRyusuke Konishi #ifndef NILFS_ATIME_DISABLE
38005fe58fdSRyusuke Konishi 	if (flags & NILFS_NOATIME_FL)
38105fe58fdSRyusuke Konishi #endif
38205fe58fdSRyusuke Konishi 		inode->i_flags |= S_NOATIME;
38305fe58fdSRyusuke Konishi 	if (flags & NILFS_DIRSYNC_FL)
38405fe58fdSRyusuke Konishi 		inode->i_flags |= S_DIRSYNC;
38505fe58fdSRyusuke Konishi 	mapping_set_gfp_mask(inode->i_mapping,
38605fe58fdSRyusuke Konishi 			     mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
38705fe58fdSRyusuke Konishi }
38805fe58fdSRyusuke Konishi 
38905fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode,
39005fe58fdSRyusuke Konishi 			    struct nilfs_inode *raw_inode)
39105fe58fdSRyusuke Konishi {
39205fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
39305fe58fdSRyusuke Konishi 	int err;
39405fe58fdSRyusuke Konishi 
39505fe58fdSRyusuke Konishi 	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
39605fe58fdSRyusuke Konishi 	inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
39705fe58fdSRyusuke Konishi 	inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
39805fe58fdSRyusuke Konishi 	inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
39905fe58fdSRyusuke Konishi 	inode->i_size = le64_to_cpu(raw_inode->i_size);
40005fe58fdSRyusuke Konishi 	inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
40105fe58fdSRyusuke Konishi 	inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
40205fe58fdSRyusuke Konishi 	inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
40361239230SRyusuke Konishi 	inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
40461239230SRyusuke Konishi 	inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
40561239230SRyusuke Konishi 	inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
40661239230SRyusuke Konishi 	if (inode->i_nlink == 0 && inode->i_mode == 0)
40705fe58fdSRyusuke Konishi 		return -EINVAL; /* this inode is deleted */
40805fe58fdSRyusuke Konishi 
40905fe58fdSRyusuke Konishi 	inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
41005fe58fdSRyusuke Konishi 	ii->i_flags = le32_to_cpu(raw_inode->i_flags);
41105fe58fdSRyusuke Konishi #if 0
41205fe58fdSRyusuke Konishi 	ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
41305fe58fdSRyusuke Konishi 	ii->i_dir_acl = S_ISREG(inode->i_mode) ?
41405fe58fdSRyusuke Konishi 		0 : le32_to_cpu(raw_inode->i_dir_acl);
41505fe58fdSRyusuke Konishi #endif
4163cc811bfSRyusuke Konishi 	ii->i_dir_start_lookup = 0;
41705fe58fdSRyusuke Konishi 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
41805fe58fdSRyusuke Konishi 
41905fe58fdSRyusuke Konishi 	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
42005fe58fdSRyusuke Konishi 	    S_ISLNK(inode->i_mode)) {
42105fe58fdSRyusuke Konishi 		err = nilfs_bmap_read(ii->i_bmap, raw_inode);
42205fe58fdSRyusuke Konishi 		if (err < 0)
42305fe58fdSRyusuke Konishi 			return err;
42405fe58fdSRyusuke Konishi 		set_bit(NILFS_I_BMAP, &ii->i_state);
42505fe58fdSRyusuke Konishi 		/* No lock is needed; iget() ensures it. */
42605fe58fdSRyusuke Konishi 	}
42705fe58fdSRyusuke Konishi 	return 0;
42805fe58fdSRyusuke Konishi }
42905fe58fdSRyusuke Konishi 
43005fe58fdSRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
43105fe58fdSRyusuke Konishi 			      struct inode *inode)
43205fe58fdSRyusuke Konishi {
43305fe58fdSRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
43405fe58fdSRyusuke Konishi 	struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
43505fe58fdSRyusuke Konishi 	struct buffer_head *bh;
43605fe58fdSRyusuke Konishi 	struct nilfs_inode *raw_inode;
43705fe58fdSRyusuke Konishi 	int err;
43805fe58fdSRyusuke Konishi 
43905fe58fdSRyusuke Konishi 	down_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
44005fe58fdSRyusuke Konishi 	err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
44105fe58fdSRyusuke Konishi 	if (unlikely(err))
44205fe58fdSRyusuke Konishi 		goto bad_inode;
44305fe58fdSRyusuke Konishi 
44405fe58fdSRyusuke Konishi 	raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
44505fe58fdSRyusuke Konishi 
4461b2f5a64SRyusuke Konishi 	err = nilfs_read_inode_common(inode, raw_inode);
4471b2f5a64SRyusuke Konishi 	if (err)
44805fe58fdSRyusuke Konishi 		goto failed_unmap;
44905fe58fdSRyusuke Konishi 
45005fe58fdSRyusuke Konishi 	if (S_ISREG(inode->i_mode)) {
45105fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_file_inode_operations;
45205fe58fdSRyusuke Konishi 		inode->i_fop = &nilfs_file_operations;
45305fe58fdSRyusuke Konishi 		inode->i_mapping->a_ops = &nilfs_aops;
45405fe58fdSRyusuke Konishi 	} else if (S_ISDIR(inode->i_mode)) {
45505fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_dir_inode_operations;
45605fe58fdSRyusuke Konishi 		inode->i_fop = &nilfs_dir_operations;
45705fe58fdSRyusuke Konishi 		inode->i_mapping->a_ops = &nilfs_aops;
45805fe58fdSRyusuke Konishi 	} else if (S_ISLNK(inode->i_mode)) {
45905fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_symlink_inode_operations;
46005fe58fdSRyusuke Konishi 		inode->i_mapping->a_ops = &nilfs_aops;
46105fe58fdSRyusuke Konishi 	} else {
46205fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_special_inode_operations;
46305fe58fdSRyusuke Konishi 		init_special_inode(
46405fe58fdSRyusuke Konishi 			inode, inode->i_mode,
465cdce214eSRyusuke Konishi 			huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
46605fe58fdSRyusuke Konishi 	}
46705fe58fdSRyusuke Konishi 	nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
46805fe58fdSRyusuke Konishi 	brelse(bh);
46905fe58fdSRyusuke Konishi 	up_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
47005fe58fdSRyusuke Konishi 	nilfs_set_inode_flags(inode);
47105fe58fdSRyusuke Konishi 	return 0;
47205fe58fdSRyusuke Konishi 
47305fe58fdSRyusuke Konishi  failed_unmap:
47405fe58fdSRyusuke Konishi 	nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
47505fe58fdSRyusuke Konishi 	brelse(bh);
47605fe58fdSRyusuke Konishi 
47705fe58fdSRyusuke Konishi  bad_inode:
47805fe58fdSRyusuke Konishi 	up_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
47905fe58fdSRyusuke Konishi 	return err;
48005fe58fdSRyusuke Konishi }
48105fe58fdSRyusuke Konishi 
4820e14a359SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque)
4830e14a359SRyusuke Konishi {
4840e14a359SRyusuke Konishi 	struct nilfs_iget_args *args = opaque;
4850e14a359SRyusuke Konishi 	struct nilfs_inode_info *ii;
4860e14a359SRyusuke Konishi 
4870e14a359SRyusuke Konishi 	if (args->ino != inode->i_ino)
4880e14a359SRyusuke Konishi 		return 0;
4890e14a359SRyusuke Konishi 
4900e14a359SRyusuke Konishi 	ii = NILFS_I(inode);
4910e14a359SRyusuke Konishi 	if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
4920e14a359SRyusuke Konishi 		return !args->for_gc;
4930e14a359SRyusuke Konishi 
4940e14a359SRyusuke Konishi 	return args->for_gc && args->cno == ii->i_cno;
4950e14a359SRyusuke Konishi }
4960e14a359SRyusuke Konishi 
4970e14a359SRyusuke Konishi static int nilfs_iget_set(struct inode *inode, void *opaque)
4980e14a359SRyusuke Konishi {
4990e14a359SRyusuke Konishi 	struct nilfs_iget_args *args = opaque;
5000e14a359SRyusuke Konishi 
5010e14a359SRyusuke Konishi 	inode->i_ino = args->ino;
5020e14a359SRyusuke Konishi 	if (args->for_gc) {
5030e14a359SRyusuke Konishi 		NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE;
5040e14a359SRyusuke Konishi 		NILFS_I(inode)->i_cno = args->cno;
5050e14a359SRyusuke Konishi 	}
5060e14a359SRyusuke Konishi 	return 0;
5070e14a359SRyusuke Konishi }
5080e14a359SRyusuke Konishi 
50905fe58fdSRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
51005fe58fdSRyusuke Konishi {
5110e14a359SRyusuke Konishi 	struct nilfs_iget_args args = { .ino = ino, .cno = 0, .for_gc = 0 };
51205fe58fdSRyusuke Konishi 	struct inode *inode;
51305fe58fdSRyusuke Konishi 	int err;
51405fe58fdSRyusuke Konishi 
5150e14a359SRyusuke Konishi 	inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
51605fe58fdSRyusuke Konishi 	if (unlikely(!inode))
51705fe58fdSRyusuke Konishi 		return ERR_PTR(-ENOMEM);
51805fe58fdSRyusuke Konishi 	if (!(inode->i_state & I_NEW))
51905fe58fdSRyusuke Konishi 		return inode;
52005fe58fdSRyusuke Konishi 
52105fe58fdSRyusuke Konishi 	err = __nilfs_read_inode(sb, ino, inode);
52205fe58fdSRyusuke Konishi 	if (unlikely(err)) {
52305fe58fdSRyusuke Konishi 		iget_failed(inode);
52405fe58fdSRyusuke Konishi 		return ERR_PTR(err);
52505fe58fdSRyusuke Konishi 	}
52605fe58fdSRyusuke Konishi 	unlock_new_inode(inode);
52705fe58fdSRyusuke Konishi 	return inode;
52805fe58fdSRyusuke Konishi }
52905fe58fdSRyusuke Konishi 
530*263d90ceSRyusuke Konishi struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
531*263d90ceSRyusuke Konishi 				__u64 cno)
532*263d90ceSRyusuke Konishi {
533*263d90ceSRyusuke Konishi 	struct nilfs_iget_args args = { .ino = ino, .cno = cno, .for_gc = 1 };
534*263d90ceSRyusuke Konishi 	struct inode *inode;
535*263d90ceSRyusuke Konishi 	int err;
536*263d90ceSRyusuke Konishi 
537*263d90ceSRyusuke Konishi 	inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
538*263d90ceSRyusuke Konishi 	if (unlikely(!inode))
539*263d90ceSRyusuke Konishi 		return ERR_PTR(-ENOMEM);
540*263d90ceSRyusuke Konishi 	if (!(inode->i_state & I_NEW))
541*263d90ceSRyusuke Konishi 		return inode;
542*263d90ceSRyusuke Konishi 
543*263d90ceSRyusuke Konishi 	err = nilfs_init_gcinode(inode);
544*263d90ceSRyusuke Konishi 	if (unlikely(err)) {
545*263d90ceSRyusuke Konishi 		iget_failed(inode);
546*263d90ceSRyusuke Konishi 		return ERR_PTR(err);
547*263d90ceSRyusuke Konishi 	}
548*263d90ceSRyusuke Konishi 	unlock_new_inode(inode);
549*263d90ceSRyusuke Konishi 	return inode;
550*263d90ceSRyusuke Konishi }
551*263d90ceSRyusuke Konishi 
55205fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode,
55305fe58fdSRyusuke Konishi 			      struct nilfs_inode *raw_inode, int has_bmap)
55405fe58fdSRyusuke Konishi {
55505fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
55605fe58fdSRyusuke Konishi 
55705fe58fdSRyusuke Konishi 	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
55805fe58fdSRyusuke Konishi 	raw_inode->i_uid = cpu_to_le32(inode->i_uid);
55905fe58fdSRyusuke Konishi 	raw_inode->i_gid = cpu_to_le32(inode->i_gid);
56005fe58fdSRyusuke Konishi 	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
56105fe58fdSRyusuke Konishi 	raw_inode->i_size = cpu_to_le64(inode->i_size);
56205fe58fdSRyusuke Konishi 	raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
56305fe58fdSRyusuke Konishi 	raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
56461239230SRyusuke Konishi 	raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
56561239230SRyusuke Konishi 	raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
56605fe58fdSRyusuke Konishi 	raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
56705fe58fdSRyusuke Konishi 
56805fe58fdSRyusuke Konishi 	raw_inode->i_flags = cpu_to_le32(ii->i_flags);
56905fe58fdSRyusuke Konishi 	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
57005fe58fdSRyusuke Konishi 
57105fe58fdSRyusuke Konishi 	if (has_bmap)
57205fe58fdSRyusuke Konishi 		nilfs_bmap_write(ii->i_bmap, raw_inode);
57305fe58fdSRyusuke Konishi 	else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
57405fe58fdSRyusuke Konishi 		raw_inode->i_device_code =
575cdce214eSRyusuke Konishi 			cpu_to_le64(huge_encode_dev(inode->i_rdev));
57605fe58fdSRyusuke Konishi 	/* When extending inode, nilfs->ns_inode_size should be checked
57705fe58fdSRyusuke Konishi 	   for substitutions of appended fields */
57805fe58fdSRyusuke Konishi }
57905fe58fdSRyusuke Konishi 
58005fe58fdSRyusuke Konishi void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
58105fe58fdSRyusuke Konishi {
58205fe58fdSRyusuke Konishi 	ino_t ino = inode->i_ino;
58305fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
58405fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
58505fe58fdSRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
58605fe58fdSRyusuke Konishi 	struct nilfs_inode *raw_inode;
58705fe58fdSRyusuke Konishi 
58805fe58fdSRyusuke Konishi 	raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
58905fe58fdSRyusuke Konishi 
59005fe58fdSRyusuke Konishi 	if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
59105fe58fdSRyusuke Konishi 		memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
59205fe58fdSRyusuke Konishi 	set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
59305fe58fdSRyusuke Konishi 
59405fe58fdSRyusuke Konishi 	nilfs_write_inode_common(inode, raw_inode, 0);
59505fe58fdSRyusuke Konishi 		/* XXX: call with has_bmap = 0 is a workaround to avoid
59605fe58fdSRyusuke Konishi 		   deadlock of bmap. This delays update of i_bmap to just
59705fe58fdSRyusuke Konishi 		   before writing */
59805fe58fdSRyusuke Konishi 	nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
59905fe58fdSRyusuke Konishi }
60005fe58fdSRyusuke Konishi 
60105fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS	16384  /* 64MB for 4KB block */
60205fe58fdSRyusuke Konishi 
60305fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
60405fe58fdSRyusuke Konishi 				unsigned long from)
60505fe58fdSRyusuke Konishi {
60605fe58fdSRyusuke Konishi 	unsigned long b;
60705fe58fdSRyusuke Konishi 	int ret;
60805fe58fdSRyusuke Konishi 
60905fe58fdSRyusuke Konishi 	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
61005fe58fdSRyusuke Konishi 		return;
61105fe58fdSRyusuke Konishi  repeat:
61205fe58fdSRyusuke Konishi 	ret = nilfs_bmap_last_key(ii->i_bmap, &b);
61305fe58fdSRyusuke Konishi 	if (ret == -ENOENT)
61405fe58fdSRyusuke Konishi 		return;
61505fe58fdSRyusuke Konishi 	else if (ret < 0)
61605fe58fdSRyusuke Konishi 		goto failed;
61705fe58fdSRyusuke Konishi 
61805fe58fdSRyusuke Konishi 	if (b < from)
61905fe58fdSRyusuke Konishi 		return;
62005fe58fdSRyusuke Konishi 
62105fe58fdSRyusuke Konishi 	b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
62205fe58fdSRyusuke Konishi 	ret = nilfs_bmap_truncate(ii->i_bmap, b);
62305fe58fdSRyusuke Konishi 	nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
62405fe58fdSRyusuke Konishi 	if (!ret || (ret == -ENOMEM &&
62505fe58fdSRyusuke Konishi 		     nilfs_bmap_truncate(ii->i_bmap, b) == 0))
62605fe58fdSRyusuke Konishi 		goto repeat;
62705fe58fdSRyusuke Konishi 
62805fe58fdSRyusuke Konishi  failed:
62905fe58fdSRyusuke Konishi 	if (ret == -EINVAL)
63005fe58fdSRyusuke Konishi 		nilfs_error(ii->vfs_inode.i_sb, __func__,
63105fe58fdSRyusuke Konishi 			    "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
63205fe58fdSRyusuke Konishi 	else
63305fe58fdSRyusuke Konishi 		nilfs_warning(ii->vfs_inode.i_sb, __func__,
63405fe58fdSRyusuke Konishi 			      "failed to truncate bmap (ino=%lu, err=%d)",
63505fe58fdSRyusuke Konishi 			      ii->vfs_inode.i_ino, ret);
63605fe58fdSRyusuke Konishi }
63705fe58fdSRyusuke Konishi 
63805fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode)
63905fe58fdSRyusuke Konishi {
64005fe58fdSRyusuke Konishi 	unsigned long blkoff;
64105fe58fdSRyusuke Konishi 	unsigned int blocksize;
64205fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
64305fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
64405fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
64505fe58fdSRyusuke Konishi 
64605fe58fdSRyusuke Konishi 	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
64705fe58fdSRyusuke Konishi 		return;
64805fe58fdSRyusuke Konishi 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
64905fe58fdSRyusuke Konishi 		return;
65005fe58fdSRyusuke Konishi 
65105fe58fdSRyusuke Konishi 	blocksize = sb->s_blocksize;
65205fe58fdSRyusuke Konishi 	blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
6531f5abe7eSRyusuke Konishi 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
65405fe58fdSRyusuke Konishi 
65505fe58fdSRyusuke Konishi 	block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
65605fe58fdSRyusuke Konishi 
65705fe58fdSRyusuke Konishi 	nilfs_truncate_bmap(ii, blkoff);
65805fe58fdSRyusuke Konishi 
65905fe58fdSRyusuke Konishi 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
66005fe58fdSRyusuke Konishi 	if (IS_SYNC(inode))
66105fe58fdSRyusuke Konishi 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
66205fe58fdSRyusuke Konishi 
663abdb318bSJiro SEKIBA 	nilfs_mark_inode_dirty(inode);
66405fe58fdSRyusuke Konishi 	nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
66547420c79SRyusuke Konishi 	nilfs_transaction_commit(sb);
66605fe58fdSRyusuke Konishi 	/* May construct a logical segment and may fail in sync mode.
66705fe58fdSRyusuke Konishi 	   But truncate has no return value. */
66805fe58fdSRyusuke Konishi }
66905fe58fdSRyusuke Konishi 
6706fd1e5c9SAl Viro static void nilfs_clear_inode(struct inode *inode)
6716fd1e5c9SAl Viro {
6726fd1e5c9SAl Viro 	struct nilfs_inode_info *ii = NILFS_I(inode);
6736fd1e5c9SAl Viro 
6746fd1e5c9SAl Viro 	/*
6756fd1e5c9SAl Viro 	 * Free resources allocated in nilfs_read_inode(), here.
6766fd1e5c9SAl Viro 	 */
6776fd1e5c9SAl Viro 	BUG_ON(!list_empty(&ii->i_dirty));
6786fd1e5c9SAl Viro 	brelse(ii->i_bh);
6796fd1e5c9SAl Viro 	ii->i_bh = NULL;
6806fd1e5c9SAl Viro 
6816fd1e5c9SAl Viro 	if (test_bit(NILFS_I_BMAP, &ii->i_state))
6826fd1e5c9SAl Viro 		nilfs_bmap_clear(ii->i_bmap);
6836fd1e5c9SAl Viro 
6846fd1e5c9SAl Viro 	nilfs_btnode_cache_clear(&ii->i_btnode_cache);
6856fd1e5c9SAl Viro }
6866fd1e5c9SAl Viro 
6876fd1e5c9SAl Viro void nilfs_evict_inode(struct inode *inode)
68805fe58fdSRyusuke Konishi {
68905fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
69005fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
69105fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
69205fe58fdSRyusuke Konishi 
6936fd1e5c9SAl Viro 	if (inode->i_nlink || unlikely(is_bad_inode(inode))) {
69405fe58fdSRyusuke Konishi 		if (inode->i_data.nrpages)
69505fe58fdSRyusuke Konishi 			truncate_inode_pages(&inode->i_data, 0);
6966fd1e5c9SAl Viro 		end_writeback(inode);
6976fd1e5c9SAl Viro 		nilfs_clear_inode(inode);
69805fe58fdSRyusuke Konishi 		return;
69905fe58fdSRyusuke Konishi 	}
7001f5abe7eSRyusuke Konishi 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
7011f5abe7eSRyusuke Konishi 
70205fe58fdSRyusuke Konishi 	if (inode->i_data.nrpages)
70305fe58fdSRyusuke Konishi 		truncate_inode_pages(&inode->i_data, 0);
70405fe58fdSRyusuke Konishi 
70505fe58fdSRyusuke Konishi 	nilfs_truncate_bmap(ii, 0);
706abdb318bSJiro SEKIBA 	nilfs_mark_inode_dirty(inode);
7076fd1e5c9SAl Viro 	end_writeback(inode);
7086fd1e5c9SAl Viro 	nilfs_clear_inode(inode);
70905fe58fdSRyusuke Konishi 	nilfs_free_inode(inode);
71005fe58fdSRyusuke Konishi 	/* nilfs_free_inode() marks inode buffer dirty */
71105fe58fdSRyusuke Konishi 	if (IS_SYNC(inode))
71205fe58fdSRyusuke Konishi 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
71347420c79SRyusuke Konishi 	nilfs_transaction_commit(sb);
71405fe58fdSRyusuke Konishi 	/* May construct a logical segment and may fail in sync mode.
71505fe58fdSRyusuke Konishi 	   But delete_inode has no return value. */
71605fe58fdSRyusuke Konishi }
71705fe58fdSRyusuke Konishi 
71805fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
71905fe58fdSRyusuke Konishi {
72005fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
72105fe58fdSRyusuke Konishi 	struct inode *inode = dentry->d_inode;
72205fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
72347420c79SRyusuke Konishi 	int err;
72405fe58fdSRyusuke Konishi 
72505fe58fdSRyusuke Konishi 	err = inode_change_ok(inode, iattr);
72605fe58fdSRyusuke Konishi 	if (err)
72705fe58fdSRyusuke Konishi 		return err;
72805fe58fdSRyusuke Konishi 
72905fe58fdSRyusuke Konishi 	err = nilfs_transaction_begin(sb, &ti, 0);
73005fe58fdSRyusuke Konishi 	if (unlikely(err))
73105fe58fdSRyusuke Konishi 		return err;
73247420c79SRyusuke Konishi 
7331025774cSChristoph Hellwig 	if ((iattr->ia_valid & ATTR_SIZE) &&
7341025774cSChristoph Hellwig 	    iattr->ia_size != i_size_read(inode)) {
7351025774cSChristoph Hellwig 		err = vmtruncate(inode, iattr->ia_size);
7361025774cSChristoph Hellwig 		if (unlikely(err))
7371025774cSChristoph Hellwig 			goto out_err;
7381025774cSChristoph Hellwig 	}
7391025774cSChristoph Hellwig 
7401025774cSChristoph Hellwig 	setattr_copy(inode, iattr);
7411025774cSChristoph Hellwig 	mark_inode_dirty(inode);
7421025774cSChristoph Hellwig 
7431025774cSChristoph Hellwig 	if (iattr->ia_valid & ATTR_MODE) {
7441025774cSChristoph Hellwig 		err = nilfs_acl_chmod(inode);
7451025774cSChristoph Hellwig 		if (unlikely(err))
7461025774cSChristoph Hellwig 			goto out_err;
7471025774cSChristoph Hellwig 	}
7481025774cSChristoph Hellwig 
7491025774cSChristoph Hellwig 	return nilfs_transaction_commit(sb);
7501025774cSChristoph Hellwig 
7511025774cSChristoph Hellwig out_err:
7521025774cSChristoph Hellwig 	nilfs_transaction_abort(sb);
75347420c79SRyusuke Konishi 	return err;
75405fe58fdSRyusuke Konishi }
75505fe58fdSRyusuke Konishi 
75605fe58fdSRyusuke Konishi int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
75705fe58fdSRyusuke Konishi 			   struct buffer_head **pbh)
75805fe58fdSRyusuke Konishi {
75905fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
76005fe58fdSRyusuke Konishi 	int err;
76105fe58fdSRyusuke Konishi 
76205fe58fdSRyusuke Konishi 	spin_lock(&sbi->s_inode_lock);
76305fe58fdSRyusuke Konishi 	if (ii->i_bh == NULL) {
76405fe58fdSRyusuke Konishi 		spin_unlock(&sbi->s_inode_lock);
76505fe58fdSRyusuke Konishi 		err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
76605fe58fdSRyusuke Konishi 						  pbh);
76705fe58fdSRyusuke Konishi 		if (unlikely(err))
76805fe58fdSRyusuke Konishi 			return err;
76905fe58fdSRyusuke Konishi 		spin_lock(&sbi->s_inode_lock);
77005fe58fdSRyusuke Konishi 		if (ii->i_bh == NULL)
77105fe58fdSRyusuke Konishi 			ii->i_bh = *pbh;
77205fe58fdSRyusuke Konishi 		else {
77305fe58fdSRyusuke Konishi 			brelse(*pbh);
77405fe58fdSRyusuke Konishi 			*pbh = ii->i_bh;
77505fe58fdSRyusuke Konishi 		}
77605fe58fdSRyusuke Konishi 	} else
77705fe58fdSRyusuke Konishi 		*pbh = ii->i_bh;
77805fe58fdSRyusuke Konishi 
77905fe58fdSRyusuke Konishi 	get_bh(*pbh);
78005fe58fdSRyusuke Konishi 	spin_unlock(&sbi->s_inode_lock);
78105fe58fdSRyusuke Konishi 	return 0;
78205fe58fdSRyusuke Konishi }
78305fe58fdSRyusuke Konishi 
78405fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode)
78505fe58fdSRyusuke Konishi {
78605fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
78705fe58fdSRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
78805fe58fdSRyusuke Konishi 	int ret = 0;
78905fe58fdSRyusuke Konishi 
79005fe58fdSRyusuke Konishi 	if (!list_empty(&ii->i_dirty)) {
79105fe58fdSRyusuke Konishi 		spin_lock(&sbi->s_inode_lock);
79205fe58fdSRyusuke Konishi 		ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
79305fe58fdSRyusuke Konishi 			test_bit(NILFS_I_BUSY, &ii->i_state);
79405fe58fdSRyusuke Konishi 		spin_unlock(&sbi->s_inode_lock);
79505fe58fdSRyusuke Konishi 	}
79605fe58fdSRyusuke Konishi 	return ret;
79705fe58fdSRyusuke Konishi }
79805fe58fdSRyusuke Konishi 
79905fe58fdSRyusuke Konishi int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
80005fe58fdSRyusuke Konishi 			 unsigned nr_dirty)
80105fe58fdSRyusuke Konishi {
80205fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
80305fe58fdSRyusuke Konishi 
80405fe58fdSRyusuke Konishi 	atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
80505fe58fdSRyusuke Konishi 
806458c5b08SRyusuke Konishi 	if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
80705fe58fdSRyusuke Konishi 		return 0;
80805fe58fdSRyusuke Konishi 
80905fe58fdSRyusuke Konishi 	spin_lock(&sbi->s_inode_lock);
81005fe58fdSRyusuke Konishi 	if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
81105fe58fdSRyusuke Konishi 	    !test_bit(NILFS_I_BUSY, &ii->i_state)) {
81205fe58fdSRyusuke Konishi 		/* Because this routine may race with nilfs_dispose_list(),
81305fe58fdSRyusuke Konishi 		   we have to check NILFS_I_QUEUED here, too. */
81405fe58fdSRyusuke Konishi 		if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
81505fe58fdSRyusuke Konishi 			/* This will happen when somebody is freeing
81605fe58fdSRyusuke Konishi 			   this inode. */
81705fe58fdSRyusuke Konishi 			nilfs_warning(sbi->s_super, __func__,
81805fe58fdSRyusuke Konishi 				      "cannot get inode (ino=%lu)\n",
81905fe58fdSRyusuke Konishi 				      inode->i_ino);
82005fe58fdSRyusuke Konishi 			spin_unlock(&sbi->s_inode_lock);
82105fe58fdSRyusuke Konishi 			return -EINVAL; /* NILFS_I_DIRTY may remain for
82205fe58fdSRyusuke Konishi 					   freeing inode */
82305fe58fdSRyusuke Konishi 		}
82405fe58fdSRyusuke Konishi 		list_del(&ii->i_dirty);
82505fe58fdSRyusuke Konishi 		list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
82605fe58fdSRyusuke Konishi 		set_bit(NILFS_I_QUEUED, &ii->i_state);
82705fe58fdSRyusuke Konishi 	}
82805fe58fdSRyusuke Konishi 	spin_unlock(&sbi->s_inode_lock);
82905fe58fdSRyusuke Konishi 	return 0;
83005fe58fdSRyusuke Konishi }
83105fe58fdSRyusuke Konishi 
83205fe58fdSRyusuke Konishi int nilfs_mark_inode_dirty(struct inode *inode)
83305fe58fdSRyusuke Konishi {
83405fe58fdSRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
83505fe58fdSRyusuke Konishi 	struct buffer_head *ibh;
83605fe58fdSRyusuke Konishi 	int err;
83705fe58fdSRyusuke Konishi 
83805fe58fdSRyusuke Konishi 	err = nilfs_load_inode_block(sbi, inode, &ibh);
83905fe58fdSRyusuke Konishi 	if (unlikely(err)) {
84005fe58fdSRyusuke Konishi 		nilfs_warning(inode->i_sb, __func__,
84105fe58fdSRyusuke Konishi 			      "failed to reget inode block.\n");
84205fe58fdSRyusuke Konishi 		return err;
84305fe58fdSRyusuke Konishi 	}
84405fe58fdSRyusuke Konishi 	nilfs_update_inode(inode, ibh);
84505fe58fdSRyusuke Konishi 	nilfs_mdt_mark_buffer_dirty(ibh);
84605fe58fdSRyusuke Konishi 	nilfs_mdt_mark_dirty(sbi->s_ifile);
84705fe58fdSRyusuke Konishi 	brelse(ibh);
84805fe58fdSRyusuke Konishi 	return 0;
84905fe58fdSRyusuke Konishi }
85005fe58fdSRyusuke Konishi 
85105fe58fdSRyusuke Konishi /**
85205fe58fdSRyusuke Konishi  * nilfs_dirty_inode - reflect changes on given inode to an inode block.
85305fe58fdSRyusuke Konishi  * @inode: inode of the file to be registered.
85405fe58fdSRyusuke Konishi  *
85505fe58fdSRyusuke Konishi  * nilfs_dirty_inode() loads a inode block containing the specified
85605fe58fdSRyusuke Konishi  * @inode and copies data from a nilfs_inode to a corresponding inode
85705fe58fdSRyusuke Konishi  * entry in the inode block. This operation is excluded from the segment
85805fe58fdSRyusuke Konishi  * construction. This function can be called both as a single operation
85905fe58fdSRyusuke Konishi  * and as a part of indivisible file operations.
86005fe58fdSRyusuke Konishi  */
86105fe58fdSRyusuke Konishi void nilfs_dirty_inode(struct inode *inode)
86205fe58fdSRyusuke Konishi {
86305fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
8647d6cd92fSRyusuke Konishi 	struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
86505fe58fdSRyusuke Konishi 
86605fe58fdSRyusuke Konishi 	if (is_bad_inode(inode)) {
86705fe58fdSRyusuke Konishi 		nilfs_warning(inode->i_sb, __func__,
86805fe58fdSRyusuke Konishi 			      "tried to mark bad_inode dirty. ignored.\n");
86905fe58fdSRyusuke Konishi 		dump_stack();
87005fe58fdSRyusuke Konishi 		return;
87105fe58fdSRyusuke Konishi 	}
8727d6cd92fSRyusuke Konishi 	if (mdi) {
8737d6cd92fSRyusuke Konishi 		nilfs_mdt_mark_dirty(inode);
8747d6cd92fSRyusuke Konishi 		return;
8757d6cd92fSRyusuke Konishi 	}
87605fe58fdSRyusuke Konishi 	nilfs_transaction_begin(inode->i_sb, &ti, 0);
87705fe58fdSRyusuke Konishi 	nilfs_mark_inode_dirty(inode);
87847420c79SRyusuke Konishi 	nilfs_transaction_commit(inode->i_sb); /* never fails */
87905fe58fdSRyusuke Konishi }
880