xref: /openbmc/linux/fs/nilfs2/inode.c (revision ae98043f5f7fa45b65084f70e3ada3209873ebb4)
1*ae98043fSRyusuke Konishi // SPDX-License-Identifier: GPL-2.0+
205fe58fdSRyusuke Konishi /*
305fe58fdSRyusuke Konishi  * inode.c - NILFS inode operations.
405fe58fdSRyusuke Konishi  *
505fe58fdSRyusuke Konishi  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
605fe58fdSRyusuke Konishi  *
74b420ab4SRyusuke Konishi  * Written by Ryusuke Konishi.
805fe58fdSRyusuke Konishi  *
905fe58fdSRyusuke Konishi  */
1005fe58fdSRyusuke Konishi 
1105fe58fdSRyusuke Konishi #include <linux/buffer_head.h>
125a0e3ad6STejun Heo #include <linux/gfp.h>
1305fe58fdSRyusuke Konishi #include <linux/mpage.h>
1456d7acc7SAndreas Rohner #include <linux/pagemap.h>
1505fe58fdSRyusuke Konishi #include <linux/writeback.h>
16e2e40f2cSChristoph Hellwig #include <linux/uio.h>
1705fe58fdSRyusuke Konishi #include "nilfs.h"
186fd1e5c9SAl Viro #include "btnode.h"
1905fe58fdSRyusuke Konishi #include "segment.h"
2005fe58fdSRyusuke Konishi #include "page.h"
2105fe58fdSRyusuke Konishi #include "mdt.h"
2205fe58fdSRyusuke Konishi #include "cpfile.h"
2305fe58fdSRyusuke Konishi #include "ifile.h"
2405fe58fdSRyusuke Konishi 
25f5974c8fSVyacheslav Dubeyko /**
26f5974c8fSVyacheslav Dubeyko  * struct nilfs_iget_args - arguments used during comparison between inodes
27f5974c8fSVyacheslav Dubeyko  * @ino: inode number
28f5974c8fSVyacheslav Dubeyko  * @cno: checkpoint number
29f5974c8fSVyacheslav Dubeyko  * @root: pointer on NILFS root object (mounted checkpoint)
30f5974c8fSVyacheslav Dubeyko  * @for_gc: inode for GC flag
31f5974c8fSVyacheslav Dubeyko  */
320e14a359SRyusuke Konishi struct nilfs_iget_args {
330e14a359SRyusuke Konishi 	u64 ino;
340e14a359SRyusuke Konishi 	__u64 cno;
354d8d9293SRyusuke Konishi 	struct nilfs_root *root;
360e14a359SRyusuke Konishi 	int for_gc;
370e14a359SRyusuke Konishi };
3805fe58fdSRyusuke Konishi 
39705304a8SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque);
40705304a8SRyusuke Konishi 
41be667377SRyusuke Konishi void nilfs_inode_add_blocks(struct inode *inode, int n)
42be667377SRyusuke Konishi {
43be667377SRyusuke Konishi 	struct nilfs_root *root = NILFS_I(inode)->i_root;
44be667377SRyusuke Konishi 
4593407472SFabian Frederick 	inode_add_bytes(inode, i_blocksize(inode) * n);
46be667377SRyusuke Konishi 	if (root)
47e5f7f848SVyacheslav Dubeyko 		atomic64_add(n, &root->blocks_count);
48be667377SRyusuke Konishi }
49be667377SRyusuke Konishi 
50be667377SRyusuke Konishi void nilfs_inode_sub_blocks(struct inode *inode, int n)
51be667377SRyusuke Konishi {
52be667377SRyusuke Konishi 	struct nilfs_root *root = NILFS_I(inode)->i_root;
53be667377SRyusuke Konishi 
5493407472SFabian Frederick 	inode_sub_bytes(inode, i_blocksize(inode) * n);
55be667377SRyusuke Konishi 	if (root)
56e5f7f848SVyacheslav Dubeyko 		atomic64_sub(n, &root->blocks_count);
57be667377SRyusuke Konishi }
58be667377SRyusuke Konishi 
5905fe58fdSRyusuke Konishi /**
6005fe58fdSRyusuke Konishi  * nilfs_get_block() - get a file block on the filesystem (callback function)
6105fe58fdSRyusuke Konishi  * @inode - inode struct of the target file
6205fe58fdSRyusuke Konishi  * @blkoff - file block number
6305fe58fdSRyusuke Konishi  * @bh_result - buffer head to be mapped on
6405fe58fdSRyusuke Konishi  * @create - indicate whether allocating the block or not when it has not
6505fe58fdSRyusuke Konishi  *      been allocated yet.
6605fe58fdSRyusuke Konishi  *
6705fe58fdSRyusuke Konishi  * This function does not issue actual read request of the specified data
6805fe58fdSRyusuke Konishi  * block. It is done by VFS.
6905fe58fdSRyusuke Konishi  */
7005fe58fdSRyusuke Konishi int nilfs_get_block(struct inode *inode, sector_t blkoff,
7105fe58fdSRyusuke Konishi 		    struct buffer_head *bh_result, int create)
7205fe58fdSRyusuke Konishi {
7305fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
740ef28f9aSRyusuke Konishi 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
75c3a7abf0SRyusuke Konishi 	__u64 blknum = 0;
7605fe58fdSRyusuke Konishi 	int err = 0, ret;
770c6c44cbSRyusuke Konishi 	unsigned int maxblocks = bh_result->b_size >> inode->i_blkbits;
7805fe58fdSRyusuke Konishi 
790ef28f9aSRyusuke Konishi 	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
80c3a7abf0SRyusuke Konishi 	ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
810ef28f9aSRyusuke Konishi 	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
82c3a7abf0SRyusuke Konishi 	if (ret >= 0) {	/* found */
8305fe58fdSRyusuke Konishi 		map_bh(bh_result, inode->i_sb, blknum);
84c3a7abf0SRyusuke Konishi 		if (ret > 0)
85c3a7abf0SRyusuke Konishi 			bh_result->b_size = (ret << inode->i_blkbits);
8605fe58fdSRyusuke Konishi 		goto out;
8705fe58fdSRyusuke Konishi 	}
8805fe58fdSRyusuke Konishi 	/* data block was not found */
8905fe58fdSRyusuke Konishi 	if (ret == -ENOENT && create) {
9005fe58fdSRyusuke Konishi 		struct nilfs_transaction_info ti;
9105fe58fdSRyusuke Konishi 
9205fe58fdSRyusuke Konishi 		bh_result->b_blocknr = 0;
9305fe58fdSRyusuke Konishi 		err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
9405fe58fdSRyusuke Konishi 		if (unlikely(err))
9505fe58fdSRyusuke Konishi 			goto out;
963568a13fSRyusuke Konishi 		err = nilfs_bmap_insert(ii->i_bmap, blkoff,
9705fe58fdSRyusuke Konishi 					(unsigned long)bh_result);
9805fe58fdSRyusuke Konishi 		if (unlikely(err != 0)) {
9905fe58fdSRyusuke Konishi 			if (err == -EEXIST) {
10005fe58fdSRyusuke Konishi 				/*
10105fe58fdSRyusuke Konishi 				 * The get_block() function could be called
10205fe58fdSRyusuke Konishi 				 * from multiple callers for an inode.
10305fe58fdSRyusuke Konishi 				 * However, the page having this block must
10405fe58fdSRyusuke Konishi 				 * be locked in this case.
10505fe58fdSRyusuke Konishi 				 */
106feee880fSRyusuke Konishi 				nilfs_msg(inode->i_sb, KERN_WARNING,
107feee880fSRyusuke Konishi 					  "%s (ino=%lu): a race condition while inserting a data block at offset=%llu",
108feee880fSRyusuke Konishi 					  __func__, inode->i_ino,
10905fe58fdSRyusuke Konishi 					  (unsigned long long)blkoff);
1101f5abe7eSRyusuke Konishi 				err = 0;
11105fe58fdSRyusuke Konishi 			}
11247420c79SRyusuke Konishi 			nilfs_transaction_abort(inode->i_sb);
11305fe58fdSRyusuke Konishi 			goto out;
11405fe58fdSRyusuke Konishi 		}
115b9f66140SAndreas Rohner 		nilfs_mark_inode_dirty_sync(inode);
11647420c79SRyusuke Konishi 		nilfs_transaction_commit(inode->i_sb); /* never fails */
11705fe58fdSRyusuke Konishi 		/* Error handling should be detailed */
11805fe58fdSRyusuke Konishi 		set_buffer_new(bh_result);
11927e6c7a3SRyusuke Konishi 		set_buffer_delay(bh_result);
120076a378bSRyusuke Konishi 		map_bh(bh_result, inode->i_sb, 0);
121076a378bSRyusuke Konishi 		/* Disk block number must be changed to proper value */
122076a378bSRyusuke Konishi 
12305fe58fdSRyusuke Konishi 	} else if (ret == -ENOENT) {
124076a378bSRyusuke Konishi 		/*
125076a378bSRyusuke Konishi 		 * not found is not error (e.g. hole); must return without
126076a378bSRyusuke Konishi 		 * the mapped state flag.
127076a378bSRyusuke Konishi 		 */
12805fe58fdSRyusuke Konishi 		;
12905fe58fdSRyusuke Konishi 	} else {
13005fe58fdSRyusuke Konishi 		err = ret;
13105fe58fdSRyusuke Konishi 	}
13205fe58fdSRyusuke Konishi 
13305fe58fdSRyusuke Konishi  out:
13405fe58fdSRyusuke Konishi 	return err;
13505fe58fdSRyusuke Konishi }
13605fe58fdSRyusuke Konishi 
13705fe58fdSRyusuke Konishi /**
13805fe58fdSRyusuke Konishi  * nilfs_readpage() - implement readpage() method of nilfs_aops {}
13905fe58fdSRyusuke Konishi  * address_space_operations.
14005fe58fdSRyusuke Konishi  * @file - file struct of the file to be read
14105fe58fdSRyusuke Konishi  * @page - the page to be read
14205fe58fdSRyusuke Konishi  */
14305fe58fdSRyusuke Konishi static int nilfs_readpage(struct file *file, struct page *page)
14405fe58fdSRyusuke Konishi {
14505fe58fdSRyusuke Konishi 	return mpage_readpage(page, nilfs_get_block);
14605fe58fdSRyusuke Konishi }
14705fe58fdSRyusuke Konishi 
14805fe58fdSRyusuke Konishi /**
14905fe58fdSRyusuke Konishi  * nilfs_readpages() - implement readpages() method of nilfs_aops {}
15005fe58fdSRyusuke Konishi  * address_space_operations.
15105fe58fdSRyusuke Konishi  * @file - file struct of the file to be read
15205fe58fdSRyusuke Konishi  * @mapping - address_space struct used for reading multiple pages
15305fe58fdSRyusuke Konishi  * @pages - the pages to be read
15405fe58fdSRyusuke Konishi  * @nr_pages - number of pages to be read
15505fe58fdSRyusuke Konishi  */
15605fe58fdSRyusuke Konishi static int nilfs_readpages(struct file *file, struct address_space *mapping,
1570c6c44cbSRyusuke Konishi 			   struct list_head *pages, unsigned int nr_pages)
15805fe58fdSRyusuke Konishi {
15905fe58fdSRyusuke Konishi 	return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
16005fe58fdSRyusuke Konishi }
16105fe58fdSRyusuke Konishi 
16205fe58fdSRyusuke Konishi static int nilfs_writepages(struct address_space *mapping,
16305fe58fdSRyusuke Konishi 			    struct writeback_control *wbc)
16405fe58fdSRyusuke Konishi {
165f30bf3e4SRyusuke Konishi 	struct inode *inode = mapping->host;
166f30bf3e4SRyusuke Konishi 	int err = 0;
167f30bf3e4SRyusuke Konishi 
168bc98a42cSDavid Howells 	if (sb_rdonly(inode->i_sb)) {
1698c26c4e2SVyacheslav Dubeyko 		nilfs_clear_dirty_pages(mapping, false);
1708c26c4e2SVyacheslav Dubeyko 		return -EROFS;
1718c26c4e2SVyacheslav Dubeyko 	}
1728c26c4e2SVyacheslav Dubeyko 
173f30bf3e4SRyusuke Konishi 	if (wbc->sync_mode == WB_SYNC_ALL)
174f30bf3e4SRyusuke Konishi 		err = nilfs_construct_dsync_segment(inode->i_sb, inode,
175f30bf3e4SRyusuke Konishi 						    wbc->range_start,
176f30bf3e4SRyusuke Konishi 						    wbc->range_end);
177f30bf3e4SRyusuke Konishi 	return err;
17805fe58fdSRyusuke Konishi }
17905fe58fdSRyusuke Konishi 
18005fe58fdSRyusuke Konishi static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
18105fe58fdSRyusuke Konishi {
18205fe58fdSRyusuke Konishi 	struct inode *inode = page->mapping->host;
18305fe58fdSRyusuke Konishi 	int err;
18405fe58fdSRyusuke Konishi 
185bc98a42cSDavid Howells 	if (sb_rdonly(inode->i_sb)) {
1868c26c4e2SVyacheslav Dubeyko 		/*
1878c26c4e2SVyacheslav Dubeyko 		 * It means that filesystem was remounted in read-only
1888c26c4e2SVyacheslav Dubeyko 		 * mode because of error or metadata corruption. But we
1898c26c4e2SVyacheslav Dubeyko 		 * have dirty pages that try to be flushed in background.
1908c26c4e2SVyacheslav Dubeyko 		 * So, here we simply discard this dirty page.
1918c26c4e2SVyacheslav Dubeyko 		 */
1928c26c4e2SVyacheslav Dubeyko 		nilfs_clear_dirty_page(page, false);
1938c26c4e2SVyacheslav Dubeyko 		unlock_page(page);
1948c26c4e2SVyacheslav Dubeyko 		return -EROFS;
1958c26c4e2SVyacheslav Dubeyko 	}
1968c26c4e2SVyacheslav Dubeyko 
19705fe58fdSRyusuke Konishi 	redirty_page_for_writepage(wbc, page);
19805fe58fdSRyusuke Konishi 	unlock_page(page);
19905fe58fdSRyusuke Konishi 
20005fe58fdSRyusuke Konishi 	if (wbc->sync_mode == WB_SYNC_ALL) {
20105fe58fdSRyusuke Konishi 		err = nilfs_construct_segment(inode->i_sb);
20205fe58fdSRyusuke Konishi 		if (unlikely(err))
20305fe58fdSRyusuke Konishi 			return err;
20405fe58fdSRyusuke Konishi 	} else if (wbc->for_reclaim)
20505fe58fdSRyusuke Konishi 		nilfs_flush_segment(inode->i_sb, inode->i_ino);
20605fe58fdSRyusuke Konishi 
20705fe58fdSRyusuke Konishi 	return 0;
20805fe58fdSRyusuke Konishi }
20905fe58fdSRyusuke Konishi 
21005fe58fdSRyusuke Konishi static int nilfs_set_page_dirty(struct page *page)
21105fe58fdSRyusuke Konishi {
21256d7acc7SAndreas Rohner 	struct inode *inode = page->mapping->host;
213136e8770SRyusuke Konishi 	int ret = __set_page_dirty_nobuffers(page);
21405fe58fdSRyusuke Konishi 
215136e8770SRyusuke Konishi 	if (page_has_buffers(page)) {
2160c6c44cbSRyusuke Konishi 		unsigned int nr_dirty = 0;
217136e8770SRyusuke Konishi 		struct buffer_head *bh, *head;
21805fe58fdSRyusuke Konishi 
219136e8770SRyusuke Konishi 		/*
220136e8770SRyusuke Konishi 		 * This page is locked by callers, and no other thread
221136e8770SRyusuke Konishi 		 * concurrently marks its buffers dirty since they are
222136e8770SRyusuke Konishi 		 * only dirtied through routines in fs/buffer.c in
223136e8770SRyusuke Konishi 		 * which call sites of mark_buffer_dirty are protected
224136e8770SRyusuke Konishi 		 * by page lock.
225136e8770SRyusuke Konishi 		 */
226136e8770SRyusuke Konishi 		bh = head = page_buffers(page);
227136e8770SRyusuke Konishi 		do {
228136e8770SRyusuke Konishi 			/* Do not mark hole blocks dirty */
229136e8770SRyusuke Konishi 			if (buffer_dirty(bh) || !buffer_mapped(bh))
230136e8770SRyusuke Konishi 				continue;
231136e8770SRyusuke Konishi 
232136e8770SRyusuke Konishi 			set_buffer_dirty(bh);
233136e8770SRyusuke Konishi 			nr_dirty++;
234136e8770SRyusuke Konishi 		} while (bh = bh->b_this_page, bh != head);
235136e8770SRyusuke Konishi 
236136e8770SRyusuke Konishi 		if (nr_dirty)
237bcbc8c64SRyusuke Konishi 			nilfs_set_file_dirty(inode, nr_dirty);
23856d7acc7SAndreas Rohner 	} else if (ret) {
2390c6c44cbSRyusuke Konishi 		unsigned int nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
24056d7acc7SAndreas Rohner 
24156d7acc7SAndreas Rohner 		nilfs_set_file_dirty(inode, nr_dirty);
24205fe58fdSRyusuke Konishi 	}
24305fe58fdSRyusuke Konishi 	return ret;
24405fe58fdSRyusuke Konishi }
24505fe58fdSRyusuke Konishi 
2462d1b399bSMarco Stornelli void nilfs_write_failed(struct address_space *mapping, loff_t to)
2472d1b399bSMarco Stornelli {
2482d1b399bSMarco Stornelli 	struct inode *inode = mapping->host;
2492d1b399bSMarco Stornelli 
2502d1b399bSMarco Stornelli 	if (to > inode->i_size) {
2517caef267SKirill A. Shutemov 		truncate_pagecache(inode, inode->i_size);
2522d1b399bSMarco Stornelli 		nilfs_truncate(inode);
2532d1b399bSMarco Stornelli 	}
2542d1b399bSMarco Stornelli }
2552d1b399bSMarco Stornelli 
25605fe58fdSRyusuke Konishi static int nilfs_write_begin(struct file *file, struct address_space *mapping,
25705fe58fdSRyusuke Konishi 			     loff_t pos, unsigned len, unsigned flags,
25805fe58fdSRyusuke Konishi 			     struct page **pagep, void **fsdata)
25905fe58fdSRyusuke Konishi 
26005fe58fdSRyusuke Konishi {
26105fe58fdSRyusuke Konishi 	struct inode *inode = mapping->host;
26205fe58fdSRyusuke Konishi 	int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
26305fe58fdSRyusuke Konishi 
26405fe58fdSRyusuke Konishi 	if (unlikely(err))
26505fe58fdSRyusuke Konishi 		return err;
26605fe58fdSRyusuke Konishi 
267155130a4SChristoph Hellwig 	err = block_write_begin(mapping, pos, len, flags, pagep,
268155130a4SChristoph Hellwig 				nilfs_get_block);
269155130a4SChristoph Hellwig 	if (unlikely(err)) {
2702d1b399bSMarco Stornelli 		nilfs_write_failed(mapping, pos + len);
27147420c79SRyusuke Konishi 		nilfs_transaction_abort(inode->i_sb);
272155130a4SChristoph Hellwig 	}
27305fe58fdSRyusuke Konishi 	return err;
27405fe58fdSRyusuke Konishi }
27505fe58fdSRyusuke Konishi 
27605fe58fdSRyusuke Konishi static int nilfs_write_end(struct file *file, struct address_space *mapping,
27705fe58fdSRyusuke Konishi 			   loff_t pos, unsigned len, unsigned copied,
27805fe58fdSRyusuke Konishi 			   struct page *page, void *fsdata)
27905fe58fdSRyusuke Konishi {
28005fe58fdSRyusuke Konishi 	struct inode *inode = mapping->host;
2810c6c44cbSRyusuke Konishi 	unsigned int start = pos & (PAGE_SIZE - 1);
2820c6c44cbSRyusuke Konishi 	unsigned int nr_dirty;
28305fe58fdSRyusuke Konishi 	int err;
28405fe58fdSRyusuke Konishi 
28505fe58fdSRyusuke Konishi 	nr_dirty = nilfs_page_count_clean_buffers(page, start,
28605fe58fdSRyusuke Konishi 						  start + copied);
28705fe58fdSRyusuke Konishi 	copied = generic_write_end(file, mapping, pos, len, copied, page,
28805fe58fdSRyusuke Konishi 				   fsdata);
289bcbc8c64SRyusuke Konishi 	nilfs_set_file_dirty(inode, nr_dirty);
29047420c79SRyusuke Konishi 	err = nilfs_transaction_commit(inode->i_sb);
29105fe58fdSRyusuke Konishi 	return err ? : copied;
29205fe58fdSRyusuke Konishi }
29305fe58fdSRyusuke Konishi 
29405fe58fdSRyusuke Konishi static ssize_t
295c8b8e32dSChristoph Hellwig nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
29605fe58fdSRyusuke Konishi {
2976b6dabc8SAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
29805fe58fdSRyusuke Konishi 
2996f673763SOmar Sandoval 	if (iov_iter_rw(iter) == WRITE)
30005fe58fdSRyusuke Konishi 		return 0;
30105fe58fdSRyusuke Konishi 
30205fe58fdSRyusuke Konishi 	/* Needs synchronization with the cleaner */
303c8b8e32dSChristoph Hellwig 	return blockdev_direct_IO(iocb, inode, iter, nilfs_get_block);
30405fe58fdSRyusuke Konishi }
30505fe58fdSRyusuke Konishi 
3067f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = {
30705fe58fdSRyusuke Konishi 	.writepage		= nilfs_writepage,
30805fe58fdSRyusuke Konishi 	.readpage		= nilfs_readpage,
30905fe58fdSRyusuke Konishi 	.writepages		= nilfs_writepages,
31005fe58fdSRyusuke Konishi 	.set_page_dirty		= nilfs_set_page_dirty,
31105fe58fdSRyusuke Konishi 	.readpages		= nilfs_readpages,
31205fe58fdSRyusuke Konishi 	.write_begin		= nilfs_write_begin,
31305fe58fdSRyusuke Konishi 	.write_end		= nilfs_write_end,
31405fe58fdSRyusuke Konishi 	/* .releasepage		= nilfs_releasepage, */
31505fe58fdSRyusuke Konishi 	.invalidatepage		= block_invalidatepage,
31605fe58fdSRyusuke Konishi 	.direct_IO		= nilfs_direct_IO,
317258ef67eSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
31805fe58fdSRyusuke Konishi };
31905fe58fdSRyusuke Konishi 
320705304a8SRyusuke Konishi static int nilfs_insert_inode_locked(struct inode *inode,
321705304a8SRyusuke Konishi 				     struct nilfs_root *root,
322705304a8SRyusuke Konishi 				     unsigned long ino)
323705304a8SRyusuke Konishi {
324705304a8SRyusuke Konishi 	struct nilfs_iget_args args = {
325705304a8SRyusuke Konishi 		.ino = ino, .root = root, .cno = 0, .for_gc = 0
326705304a8SRyusuke Konishi 	};
327705304a8SRyusuke Konishi 
328705304a8SRyusuke Konishi 	return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
329705304a8SRyusuke Konishi }
330705304a8SRyusuke Konishi 
331c6e49e3fSAl Viro struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
33205fe58fdSRyusuke Konishi {
33305fe58fdSRyusuke Konishi 	struct super_block *sb = dir->i_sb;
334e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
33505fe58fdSRyusuke Konishi 	struct inode *inode;
33605fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii;
3374d8d9293SRyusuke Konishi 	struct nilfs_root *root;
33805fe58fdSRyusuke Konishi 	int err = -ENOMEM;
33905fe58fdSRyusuke Konishi 	ino_t ino;
34005fe58fdSRyusuke Konishi 
34105fe58fdSRyusuke Konishi 	inode = new_inode(sb);
34205fe58fdSRyusuke Konishi 	if (unlikely(!inode))
34305fe58fdSRyusuke Konishi 		goto failed;
34405fe58fdSRyusuke Konishi 
34505fe58fdSRyusuke Konishi 	mapping_set_gfp_mask(inode->i_mapping,
346c62d2555SMichal Hocko 			   mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
34705fe58fdSRyusuke Konishi 
3484d8d9293SRyusuke Konishi 	root = NILFS_I(dir)->i_root;
34905fe58fdSRyusuke Konishi 	ii = NILFS_I(inode);
3504ce5c342SRyusuke Konishi 	ii->i_state = BIT(NILFS_I_NEW);
3514d8d9293SRyusuke Konishi 	ii->i_root = root;
35205fe58fdSRyusuke Konishi 
353e912a5b6SRyusuke Konishi 	err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh);
35405fe58fdSRyusuke Konishi 	if (unlikely(err))
35505fe58fdSRyusuke Konishi 		goto failed_ifile_create_inode;
35605fe58fdSRyusuke Konishi 	/* reference count of i_bh inherits from nilfs_mdt_read_block() */
35705fe58fdSRyusuke Konishi 
358e5f7f848SVyacheslav Dubeyko 	atomic64_inc(&root->inodes_count);
35973459dccSDmitry Monakhov 	inode_init_owner(inode, dir, mode);
36005fe58fdSRyusuke Konishi 	inode->i_ino = ino;
361078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
36205fe58fdSRyusuke Konishi 
36305fe58fdSRyusuke Konishi 	if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
36405fe58fdSRyusuke Konishi 		err = nilfs_bmap_read(ii->i_bmap, NULL);
36505fe58fdSRyusuke Konishi 		if (err < 0)
366705304a8SRyusuke Konishi 			goto failed_after_creation;
36705fe58fdSRyusuke Konishi 
36805fe58fdSRyusuke Konishi 		set_bit(NILFS_I_BMAP, &ii->i_state);
36905fe58fdSRyusuke Konishi 		/* No lock is needed; iget() ensures it. */
37005fe58fdSRyusuke Konishi 	}
37105fe58fdSRyusuke Konishi 
372b253a3e4SRyusuke Konishi 	ii->i_flags = nilfs_mask_flags(
373b253a3e4SRyusuke Konishi 		mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
37405fe58fdSRyusuke Konishi 
37505fe58fdSRyusuke Konishi 	/* ii->i_file_acl = 0; */
37605fe58fdSRyusuke Konishi 	/* ii->i_dir_acl = 0; */
37705fe58fdSRyusuke Konishi 	ii->i_dir_start_lookup = 0;
37805fe58fdSRyusuke Konishi 	nilfs_set_inode_flags(inode);
3799b1fc4e4SRyusuke Konishi 	spin_lock(&nilfs->ns_next_gen_lock);
3809b1fc4e4SRyusuke Konishi 	inode->i_generation = nilfs->ns_next_generation++;
3819b1fc4e4SRyusuke Konishi 	spin_unlock(&nilfs->ns_next_gen_lock);
382705304a8SRyusuke Konishi 	if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
383705304a8SRyusuke Konishi 		err = -EIO;
384705304a8SRyusuke Konishi 		goto failed_after_creation;
385705304a8SRyusuke Konishi 	}
38605fe58fdSRyusuke Konishi 
38705fe58fdSRyusuke Konishi 	err = nilfs_init_acl(inode, dir);
38805fe58fdSRyusuke Konishi 	if (unlikely(err))
389076a378bSRyusuke Konishi 		/*
390076a378bSRyusuke Konishi 		 * Never occur.  When supporting nilfs_init_acl(),
391076a378bSRyusuke Konishi 		 * proper cancellation of above jobs should be considered.
392076a378bSRyusuke Konishi 		 */
393076a378bSRyusuke Konishi 		goto failed_after_creation;
39405fe58fdSRyusuke Konishi 
39505fe58fdSRyusuke Konishi 	return inode;
39605fe58fdSRyusuke Konishi 
397705304a8SRyusuke Konishi  failed_after_creation:
3986d6b77f1SMiklos Szeredi 	clear_nlink(inode);
399705304a8SRyusuke Konishi 	unlock_new_inode(inode);
400076a378bSRyusuke Konishi 	iput(inode);  /*
401076a378bSRyusuke Konishi 		       * raw_inode will be deleted through
402076a378bSRyusuke Konishi 		       * nilfs_evict_inode().
403076a378bSRyusuke Konishi 		       */
40405fe58fdSRyusuke Konishi 	goto failed;
40505fe58fdSRyusuke Konishi 
40605fe58fdSRyusuke Konishi  failed_ifile_create_inode:
40705fe58fdSRyusuke Konishi 	make_bad_inode(inode);
408076a378bSRyusuke Konishi 	iput(inode);
40905fe58fdSRyusuke Konishi  failed:
41005fe58fdSRyusuke Konishi 	return ERR_PTR(err);
41105fe58fdSRyusuke Konishi }
41205fe58fdSRyusuke Konishi 
41305fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode)
41405fe58fdSRyusuke Konishi {
41505fe58fdSRyusuke Konishi 	unsigned int flags = NILFS_I(inode)->i_flags;
416faea2c53SRyusuke Konishi 	unsigned int new_fl = 0;
41705fe58fdSRyusuke Konishi 
418f0c9f242SRyusuke Konishi 	if (flags & FS_SYNC_FL)
419faea2c53SRyusuke Konishi 		new_fl |= S_SYNC;
420f0c9f242SRyusuke Konishi 	if (flags & FS_APPEND_FL)
421faea2c53SRyusuke Konishi 		new_fl |= S_APPEND;
422f0c9f242SRyusuke Konishi 	if (flags & FS_IMMUTABLE_FL)
423faea2c53SRyusuke Konishi 		new_fl |= S_IMMUTABLE;
424f0c9f242SRyusuke Konishi 	if (flags & FS_NOATIME_FL)
425faea2c53SRyusuke Konishi 		new_fl |= S_NOATIME;
426f0c9f242SRyusuke Konishi 	if (flags & FS_DIRSYNC_FL)
427faea2c53SRyusuke Konishi 		new_fl |= S_DIRSYNC;
428faea2c53SRyusuke Konishi 	inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
429faea2c53SRyusuke Konishi 			S_NOATIME | S_DIRSYNC);
43005fe58fdSRyusuke Konishi }
43105fe58fdSRyusuke Konishi 
43205fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode,
43305fe58fdSRyusuke Konishi 			    struct nilfs_inode *raw_inode)
43405fe58fdSRyusuke Konishi {
43505fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
43605fe58fdSRyusuke Konishi 	int err;
43705fe58fdSRyusuke Konishi 
43805fe58fdSRyusuke Konishi 	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
439305d3d0dSEric W. Biederman 	i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
440305d3d0dSEric W. Biederman 	i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
441bfe86848SMiklos Szeredi 	set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
44205fe58fdSRyusuke Konishi 	inode->i_size = le64_to_cpu(raw_inode->i_size);
44305fe58fdSRyusuke Konishi 	inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
44405fe58fdSRyusuke Konishi 	inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
44505fe58fdSRyusuke Konishi 	inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
44661239230SRyusuke Konishi 	inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
44761239230SRyusuke Konishi 	inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
44861239230SRyusuke Konishi 	inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
449705304a8SRyusuke Konishi 	if (inode->i_nlink == 0)
450705304a8SRyusuke Konishi 		return -ESTALE; /* this inode is deleted */
45105fe58fdSRyusuke Konishi 
45205fe58fdSRyusuke Konishi 	inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
45305fe58fdSRyusuke Konishi 	ii->i_flags = le32_to_cpu(raw_inode->i_flags);
45405fe58fdSRyusuke Konishi #if 0
45505fe58fdSRyusuke Konishi 	ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
45605fe58fdSRyusuke Konishi 	ii->i_dir_acl = S_ISREG(inode->i_mode) ?
45705fe58fdSRyusuke Konishi 		0 : le32_to_cpu(raw_inode->i_dir_acl);
45805fe58fdSRyusuke Konishi #endif
4593cc811bfSRyusuke Konishi 	ii->i_dir_start_lookup = 0;
46005fe58fdSRyusuke Konishi 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
46105fe58fdSRyusuke Konishi 
46205fe58fdSRyusuke Konishi 	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
46305fe58fdSRyusuke Konishi 	    S_ISLNK(inode->i_mode)) {
46405fe58fdSRyusuke Konishi 		err = nilfs_bmap_read(ii->i_bmap, raw_inode);
46505fe58fdSRyusuke Konishi 		if (err < 0)
46605fe58fdSRyusuke Konishi 			return err;
46705fe58fdSRyusuke Konishi 		set_bit(NILFS_I_BMAP, &ii->i_state);
46805fe58fdSRyusuke Konishi 		/* No lock is needed; iget() ensures it. */
46905fe58fdSRyusuke Konishi 	}
47005fe58fdSRyusuke Konishi 	return 0;
47105fe58fdSRyusuke Konishi }
47205fe58fdSRyusuke Konishi 
473e912a5b6SRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb,
474e912a5b6SRyusuke Konishi 			      struct nilfs_root *root, unsigned long ino,
47505fe58fdSRyusuke Konishi 			      struct inode *inode)
47605fe58fdSRyusuke Konishi {
477e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
47805fe58fdSRyusuke Konishi 	struct buffer_head *bh;
47905fe58fdSRyusuke Konishi 	struct nilfs_inode *raw_inode;
48005fe58fdSRyusuke Konishi 	int err;
48105fe58fdSRyusuke Konishi 
482365e215cSRyusuke Konishi 	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
483e912a5b6SRyusuke Konishi 	err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
48405fe58fdSRyusuke Konishi 	if (unlikely(err))
48505fe58fdSRyusuke Konishi 		goto bad_inode;
48605fe58fdSRyusuke Konishi 
487e912a5b6SRyusuke Konishi 	raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
48805fe58fdSRyusuke Konishi 
4891b2f5a64SRyusuke Konishi 	err = nilfs_read_inode_common(inode, raw_inode);
4901b2f5a64SRyusuke Konishi 	if (err)
49105fe58fdSRyusuke Konishi 		goto failed_unmap;
49205fe58fdSRyusuke Konishi 
49305fe58fdSRyusuke Konishi 	if (S_ISREG(inode->i_mode)) {
49405fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_file_inode_operations;
49505fe58fdSRyusuke Konishi 		inode->i_fop = &nilfs_file_operations;
49605fe58fdSRyusuke Konishi 		inode->i_mapping->a_ops = &nilfs_aops;
49705fe58fdSRyusuke Konishi 	} else if (S_ISDIR(inode->i_mode)) {
49805fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_dir_inode_operations;
49905fe58fdSRyusuke Konishi 		inode->i_fop = &nilfs_dir_operations;
50005fe58fdSRyusuke Konishi 		inode->i_mapping->a_ops = &nilfs_aops;
50105fe58fdSRyusuke Konishi 	} else if (S_ISLNK(inode->i_mode)) {
50205fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_symlink_inode_operations;
50321fc61c7SAl Viro 		inode_nohighmem(inode);
50405fe58fdSRyusuke Konishi 		inode->i_mapping->a_ops = &nilfs_aops;
50505fe58fdSRyusuke Konishi 	} else {
50605fe58fdSRyusuke Konishi 		inode->i_op = &nilfs_special_inode_operations;
50705fe58fdSRyusuke Konishi 		init_special_inode(
50805fe58fdSRyusuke Konishi 			inode, inode->i_mode,
509cdce214eSRyusuke Konishi 			huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
51005fe58fdSRyusuke Konishi 	}
511e912a5b6SRyusuke Konishi 	nilfs_ifile_unmap_inode(root->ifile, ino, bh);
51205fe58fdSRyusuke Konishi 	brelse(bh);
513365e215cSRyusuke Konishi 	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
51405fe58fdSRyusuke Konishi 	nilfs_set_inode_flags(inode);
5150ce187c4SRyusuke Konishi 	mapping_set_gfp_mask(inode->i_mapping,
516c62d2555SMichal Hocko 			   mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
51705fe58fdSRyusuke Konishi 	return 0;
51805fe58fdSRyusuke Konishi 
51905fe58fdSRyusuke Konishi  failed_unmap:
520e912a5b6SRyusuke Konishi 	nilfs_ifile_unmap_inode(root->ifile, ino, bh);
52105fe58fdSRyusuke Konishi 	brelse(bh);
52205fe58fdSRyusuke Konishi 
52305fe58fdSRyusuke Konishi  bad_inode:
524365e215cSRyusuke Konishi 	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
52505fe58fdSRyusuke Konishi 	return err;
52605fe58fdSRyusuke Konishi }
52705fe58fdSRyusuke Konishi 
5280e14a359SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque)
5290e14a359SRyusuke Konishi {
5300e14a359SRyusuke Konishi 	struct nilfs_iget_args *args = opaque;
5310e14a359SRyusuke Konishi 	struct nilfs_inode_info *ii;
5320e14a359SRyusuke Konishi 
5334d8d9293SRyusuke Konishi 	if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
5340e14a359SRyusuke Konishi 		return 0;
5350e14a359SRyusuke Konishi 
5360e14a359SRyusuke Konishi 	ii = NILFS_I(inode);
5370e14a359SRyusuke Konishi 	if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
5380e14a359SRyusuke Konishi 		return !args->for_gc;
5390e14a359SRyusuke Konishi 
5400e14a359SRyusuke Konishi 	return args->for_gc && args->cno == ii->i_cno;
5410e14a359SRyusuke Konishi }
5420e14a359SRyusuke Konishi 
5430e14a359SRyusuke Konishi static int nilfs_iget_set(struct inode *inode, void *opaque)
5440e14a359SRyusuke Konishi {
5450e14a359SRyusuke Konishi 	struct nilfs_iget_args *args = opaque;
5460e14a359SRyusuke Konishi 
5470e14a359SRyusuke Konishi 	inode->i_ino = args->ino;
5480e14a359SRyusuke Konishi 	if (args->for_gc) {
5494ce5c342SRyusuke Konishi 		NILFS_I(inode)->i_state = BIT(NILFS_I_GCINODE);
5500e14a359SRyusuke Konishi 		NILFS_I(inode)->i_cno = args->cno;
5514d8d9293SRyusuke Konishi 		NILFS_I(inode)->i_root = NULL;
5524d8d9293SRyusuke Konishi 	} else {
5534d8d9293SRyusuke Konishi 		if (args->root && args->ino == NILFS_ROOT_INO)
5544d8d9293SRyusuke Konishi 			nilfs_get_root(args->root);
5554d8d9293SRyusuke Konishi 		NILFS_I(inode)->i_root = args->root;
5560e14a359SRyusuke Konishi 	}
5570e14a359SRyusuke Konishi 	return 0;
5580e14a359SRyusuke Konishi }
5590e14a359SRyusuke Konishi 
560032dbb3bSRyusuke Konishi struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
561032dbb3bSRyusuke Konishi 			    unsigned long ino)
562032dbb3bSRyusuke Konishi {
563032dbb3bSRyusuke Konishi 	struct nilfs_iget_args args = {
564032dbb3bSRyusuke Konishi 		.ino = ino, .root = root, .cno = 0, .for_gc = 0
565032dbb3bSRyusuke Konishi 	};
566032dbb3bSRyusuke Konishi 
567032dbb3bSRyusuke Konishi 	return ilookup5(sb, ino, nilfs_iget_test, &args);
568032dbb3bSRyusuke Konishi }
569032dbb3bSRyusuke Konishi 
570f1e89c86SRyusuke Konishi struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
5714d8d9293SRyusuke Konishi 				unsigned long ino)
57205fe58fdSRyusuke Konishi {
5734d8d9293SRyusuke Konishi 	struct nilfs_iget_args args = {
5744d8d9293SRyusuke Konishi 		.ino = ino, .root = root, .cno = 0, .for_gc = 0
5754d8d9293SRyusuke Konishi 	};
576f1e89c86SRyusuke Konishi 
577f1e89c86SRyusuke Konishi 	return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
578f1e89c86SRyusuke Konishi }
579f1e89c86SRyusuke Konishi 
580f1e89c86SRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
581f1e89c86SRyusuke Konishi 			 unsigned long ino)
582f1e89c86SRyusuke Konishi {
58305fe58fdSRyusuke Konishi 	struct inode *inode;
58405fe58fdSRyusuke Konishi 	int err;
58505fe58fdSRyusuke Konishi 
586f1e89c86SRyusuke Konishi 	inode = nilfs_iget_locked(sb, root, ino);
58705fe58fdSRyusuke Konishi 	if (unlikely(!inode))
58805fe58fdSRyusuke Konishi 		return ERR_PTR(-ENOMEM);
58905fe58fdSRyusuke Konishi 	if (!(inode->i_state & I_NEW))
59005fe58fdSRyusuke Konishi 		return inode;
59105fe58fdSRyusuke Konishi 
592e912a5b6SRyusuke Konishi 	err = __nilfs_read_inode(sb, root, ino, inode);
59305fe58fdSRyusuke Konishi 	if (unlikely(err)) {
59405fe58fdSRyusuke Konishi 		iget_failed(inode);
59505fe58fdSRyusuke Konishi 		return ERR_PTR(err);
59605fe58fdSRyusuke Konishi 	}
59705fe58fdSRyusuke Konishi 	unlock_new_inode(inode);
59805fe58fdSRyusuke Konishi 	return inode;
59905fe58fdSRyusuke Konishi }
60005fe58fdSRyusuke Konishi 
601263d90ceSRyusuke Konishi struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
602263d90ceSRyusuke Konishi 				__u64 cno)
603263d90ceSRyusuke Konishi {
6044d8d9293SRyusuke Konishi 	struct nilfs_iget_args args = {
6054d8d9293SRyusuke Konishi 		.ino = ino, .root = NULL, .cno = cno, .for_gc = 1
6064d8d9293SRyusuke Konishi 	};
607263d90ceSRyusuke Konishi 	struct inode *inode;
608263d90ceSRyusuke Konishi 	int err;
609263d90ceSRyusuke Konishi 
610263d90ceSRyusuke Konishi 	inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
611263d90ceSRyusuke Konishi 	if (unlikely(!inode))
612263d90ceSRyusuke Konishi 		return ERR_PTR(-ENOMEM);
613263d90ceSRyusuke Konishi 	if (!(inode->i_state & I_NEW))
614263d90ceSRyusuke Konishi 		return inode;
615263d90ceSRyusuke Konishi 
616263d90ceSRyusuke Konishi 	err = nilfs_init_gcinode(inode);
617263d90ceSRyusuke Konishi 	if (unlikely(err)) {
618263d90ceSRyusuke Konishi 		iget_failed(inode);
619263d90ceSRyusuke Konishi 		return ERR_PTR(err);
620263d90ceSRyusuke Konishi 	}
621263d90ceSRyusuke Konishi 	unlock_new_inode(inode);
622263d90ceSRyusuke Konishi 	return inode;
623263d90ceSRyusuke Konishi }
624263d90ceSRyusuke Konishi 
62505fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode,
62605fe58fdSRyusuke Konishi 			      struct nilfs_inode *raw_inode, int has_bmap)
62705fe58fdSRyusuke Konishi {
62805fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
62905fe58fdSRyusuke Konishi 
63005fe58fdSRyusuke Konishi 	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
631305d3d0dSEric W. Biederman 	raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
632305d3d0dSEric W. Biederman 	raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
63305fe58fdSRyusuke Konishi 	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
63405fe58fdSRyusuke Konishi 	raw_inode->i_size = cpu_to_le64(inode->i_size);
63505fe58fdSRyusuke Konishi 	raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
63605fe58fdSRyusuke Konishi 	raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
63761239230SRyusuke Konishi 	raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
63861239230SRyusuke Konishi 	raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
63905fe58fdSRyusuke Konishi 	raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
64005fe58fdSRyusuke Konishi 
64105fe58fdSRyusuke Konishi 	raw_inode->i_flags = cpu_to_le32(ii->i_flags);
64205fe58fdSRyusuke Konishi 	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
64305fe58fdSRyusuke Konishi 
64456eb5538SRyusuke Konishi 	if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
64556eb5538SRyusuke Konishi 		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
64656eb5538SRyusuke Konishi 
64756eb5538SRyusuke Konishi 		/* zero-fill unused portion in the case of super root block */
64856eb5538SRyusuke Konishi 		raw_inode->i_xattr = 0;
64956eb5538SRyusuke Konishi 		raw_inode->i_pad = 0;
65056eb5538SRyusuke Konishi 		memset((void *)raw_inode + sizeof(*raw_inode), 0,
65156eb5538SRyusuke Konishi 		       nilfs->ns_inode_size - sizeof(*raw_inode));
65256eb5538SRyusuke Konishi 	}
65356eb5538SRyusuke Konishi 
65405fe58fdSRyusuke Konishi 	if (has_bmap)
65505fe58fdSRyusuke Konishi 		nilfs_bmap_write(ii->i_bmap, raw_inode);
65605fe58fdSRyusuke Konishi 	else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
65705fe58fdSRyusuke Konishi 		raw_inode->i_device_code =
658cdce214eSRyusuke Konishi 			cpu_to_le64(huge_encode_dev(inode->i_rdev));
659076a378bSRyusuke Konishi 	/*
660076a378bSRyusuke Konishi 	 * When extending inode, nilfs->ns_inode_size should be checked
661076a378bSRyusuke Konishi 	 * for substitutions of appended fields.
662076a378bSRyusuke Konishi 	 */
66305fe58fdSRyusuke Konishi }
66405fe58fdSRyusuke Konishi 
665b9f66140SAndreas Rohner void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
66605fe58fdSRyusuke Konishi {
66705fe58fdSRyusuke Konishi 	ino_t ino = inode->i_ino;
66805fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
669e912a5b6SRyusuke Konishi 	struct inode *ifile = ii->i_root->ifile;
67005fe58fdSRyusuke Konishi 	struct nilfs_inode *raw_inode;
67105fe58fdSRyusuke Konishi 
672e912a5b6SRyusuke Konishi 	raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
67305fe58fdSRyusuke Konishi 
67405fe58fdSRyusuke Konishi 	if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
675e912a5b6SRyusuke Konishi 		memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
676b9f66140SAndreas Rohner 	if (flags & I_DIRTY_DATASYNC)
677b9f66140SAndreas Rohner 		set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
67805fe58fdSRyusuke Konishi 
67905fe58fdSRyusuke Konishi 	nilfs_write_inode_common(inode, raw_inode, 0);
680076a378bSRyusuke Konishi 		/*
681076a378bSRyusuke Konishi 		 * XXX: call with has_bmap = 0 is a workaround to avoid
682076a378bSRyusuke Konishi 		 * deadlock of bmap.  This delays update of i_bmap to just
683076a378bSRyusuke Konishi 		 * before writing.
684076a378bSRyusuke Konishi 		 */
685076a378bSRyusuke Konishi 
686e912a5b6SRyusuke Konishi 	nilfs_ifile_unmap_inode(ifile, ino, ibh);
68705fe58fdSRyusuke Konishi }
68805fe58fdSRyusuke Konishi 
68905fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS	16384  /* 64MB for 4KB block */
69005fe58fdSRyusuke Konishi 
69105fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
69205fe58fdSRyusuke Konishi 				unsigned long from)
69305fe58fdSRyusuke Konishi {
6943568a13fSRyusuke Konishi 	__u64 b;
69505fe58fdSRyusuke Konishi 	int ret;
69605fe58fdSRyusuke Konishi 
69705fe58fdSRyusuke Konishi 	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
69805fe58fdSRyusuke Konishi 		return;
69905fe58fdSRyusuke Konishi repeat:
70005fe58fdSRyusuke Konishi 	ret = nilfs_bmap_last_key(ii->i_bmap, &b);
70105fe58fdSRyusuke Konishi 	if (ret == -ENOENT)
70205fe58fdSRyusuke Konishi 		return;
70305fe58fdSRyusuke Konishi 	else if (ret < 0)
70405fe58fdSRyusuke Konishi 		goto failed;
70505fe58fdSRyusuke Konishi 
70605fe58fdSRyusuke Konishi 	if (b < from)
70705fe58fdSRyusuke Konishi 		return;
70805fe58fdSRyusuke Konishi 
7093568a13fSRyusuke Konishi 	b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
71005fe58fdSRyusuke Konishi 	ret = nilfs_bmap_truncate(ii->i_bmap, b);
71105fe58fdSRyusuke Konishi 	nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
71205fe58fdSRyusuke Konishi 	if (!ret || (ret == -ENOMEM &&
71305fe58fdSRyusuke Konishi 		     nilfs_bmap_truncate(ii->i_bmap, b) == 0))
71405fe58fdSRyusuke Konishi 		goto repeat;
71505fe58fdSRyusuke Konishi 
71605fe58fdSRyusuke Konishi failed:
717d6517debSRyusuke Konishi 	nilfs_msg(ii->vfs_inode.i_sb, KERN_WARNING,
718d6517debSRyusuke Konishi 		  "error %d truncating bmap (ino=%lu)", ret,
719d6517debSRyusuke Konishi 		  ii->vfs_inode.i_ino);
72005fe58fdSRyusuke Konishi }
72105fe58fdSRyusuke Konishi 
72205fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode)
72305fe58fdSRyusuke Konishi {
72405fe58fdSRyusuke Konishi 	unsigned long blkoff;
72505fe58fdSRyusuke Konishi 	unsigned int blocksize;
72605fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
72705fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
72805fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
72905fe58fdSRyusuke Konishi 
73005fe58fdSRyusuke Konishi 	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
73105fe58fdSRyusuke Konishi 		return;
73205fe58fdSRyusuke Konishi 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
73305fe58fdSRyusuke Konishi 		return;
73405fe58fdSRyusuke Konishi 
73505fe58fdSRyusuke Konishi 	blocksize = sb->s_blocksize;
73605fe58fdSRyusuke Konishi 	blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
7371f5abe7eSRyusuke Konishi 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
73805fe58fdSRyusuke Konishi 
73905fe58fdSRyusuke Konishi 	block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
74005fe58fdSRyusuke Konishi 
74105fe58fdSRyusuke Konishi 	nilfs_truncate_bmap(ii, blkoff);
74205fe58fdSRyusuke Konishi 
743078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
74405fe58fdSRyusuke Konishi 	if (IS_SYNC(inode))
74505fe58fdSRyusuke Konishi 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
74605fe58fdSRyusuke Konishi 
747abdb318bSJiro SEKIBA 	nilfs_mark_inode_dirty(inode);
748bcbc8c64SRyusuke Konishi 	nilfs_set_file_dirty(inode, 0);
74947420c79SRyusuke Konishi 	nilfs_transaction_commit(sb);
750076a378bSRyusuke Konishi 	/*
751076a378bSRyusuke Konishi 	 * May construct a logical segment and may fail in sync mode.
752076a378bSRyusuke Konishi 	 * But truncate has no return value.
753076a378bSRyusuke Konishi 	 */
75405fe58fdSRyusuke Konishi }
75505fe58fdSRyusuke Konishi 
7566fd1e5c9SAl Viro static void nilfs_clear_inode(struct inode *inode)
7576fd1e5c9SAl Viro {
7586fd1e5c9SAl Viro 	struct nilfs_inode_info *ii = NILFS_I(inode);
7596fd1e5c9SAl Viro 
7606fd1e5c9SAl Viro 	/*
7616fd1e5c9SAl Viro 	 * Free resources allocated in nilfs_read_inode(), here.
7626fd1e5c9SAl Viro 	 */
7636fd1e5c9SAl Viro 	BUG_ON(!list_empty(&ii->i_dirty));
7646fd1e5c9SAl Viro 	brelse(ii->i_bh);
7656fd1e5c9SAl Viro 	ii->i_bh = NULL;
7666fd1e5c9SAl Viro 
7672d19961dSRyusuke Konishi 	if (nilfs_is_metadata_file_inode(inode))
7682d19961dSRyusuke Konishi 		nilfs_mdt_clear(inode);
769518d1a6aSRyusuke Konishi 
7706fd1e5c9SAl Viro 	if (test_bit(NILFS_I_BMAP, &ii->i_state))
7716fd1e5c9SAl Viro 		nilfs_bmap_clear(ii->i_bmap);
7726fd1e5c9SAl Viro 
7736fd1e5c9SAl Viro 	nilfs_btnode_cache_clear(&ii->i_btnode_cache);
7744d8d9293SRyusuke Konishi 
7754d8d9293SRyusuke Konishi 	if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
7764d8d9293SRyusuke Konishi 		nilfs_put_root(ii->i_root);
7776fd1e5c9SAl Viro }
7786fd1e5c9SAl Viro 
7796fd1e5c9SAl Viro void nilfs_evict_inode(struct inode *inode)
78005fe58fdSRyusuke Konishi {
78105fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
78205fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
78305fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
78425b18d39SRyusuke Konishi 	int ret;
78505fe58fdSRyusuke Konishi 
7864d8d9293SRyusuke Konishi 	if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
78791b0abe3SJohannes Weiner 		truncate_inode_pages_final(&inode->i_data);
788dbd5768fSJan Kara 		clear_inode(inode);
7896fd1e5c9SAl Viro 		nilfs_clear_inode(inode);
79005fe58fdSRyusuke Konishi 		return;
79105fe58fdSRyusuke Konishi 	}
7921f5abe7eSRyusuke Konishi 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
7931f5abe7eSRyusuke Konishi 
79491b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
79505fe58fdSRyusuke Konishi 
796e912a5b6SRyusuke Konishi 	/* TODO: some of the following operations may fail.  */
79705fe58fdSRyusuke Konishi 	nilfs_truncate_bmap(ii, 0);
798abdb318bSJiro SEKIBA 	nilfs_mark_inode_dirty(inode);
799dbd5768fSJan Kara 	clear_inode(inode);
800e912a5b6SRyusuke Konishi 
80125b18d39SRyusuke Konishi 	ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
80225b18d39SRyusuke Konishi 	if (!ret)
803e5f7f848SVyacheslav Dubeyko 		atomic64_dec(&ii->i_root->inodes_count);
804e912a5b6SRyusuke Konishi 
8056fd1e5c9SAl Viro 	nilfs_clear_inode(inode);
806e912a5b6SRyusuke Konishi 
80705fe58fdSRyusuke Konishi 	if (IS_SYNC(inode))
80805fe58fdSRyusuke Konishi 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
80947420c79SRyusuke Konishi 	nilfs_transaction_commit(sb);
810076a378bSRyusuke Konishi 	/*
811076a378bSRyusuke Konishi 	 * May construct a logical segment and may fail in sync mode.
812076a378bSRyusuke Konishi 	 * But delete_inode has no return value.
813076a378bSRyusuke Konishi 	 */
81405fe58fdSRyusuke Konishi }
81505fe58fdSRyusuke Konishi 
81605fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
81705fe58fdSRyusuke Konishi {
81805fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
8192b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
82005fe58fdSRyusuke Konishi 	struct super_block *sb = inode->i_sb;
82147420c79SRyusuke Konishi 	int err;
82205fe58fdSRyusuke Konishi 
82331051c85SJan Kara 	err = setattr_prepare(dentry, iattr);
82405fe58fdSRyusuke Konishi 	if (err)
82505fe58fdSRyusuke Konishi 		return err;
82605fe58fdSRyusuke Konishi 
82705fe58fdSRyusuke Konishi 	err = nilfs_transaction_begin(sb, &ti, 0);
82805fe58fdSRyusuke Konishi 	if (unlikely(err))
82905fe58fdSRyusuke Konishi 		return err;
83047420c79SRyusuke Konishi 
8311025774cSChristoph Hellwig 	if ((iattr->ia_valid & ATTR_SIZE) &&
8321025774cSChristoph Hellwig 	    iattr->ia_size != i_size_read(inode)) {
833562c72aaSChristoph Hellwig 		inode_dio_wait(inode);
8342d1b399bSMarco Stornelli 		truncate_setsize(inode, iattr->ia_size);
8352d1b399bSMarco Stornelli 		nilfs_truncate(inode);
8361025774cSChristoph Hellwig 	}
8371025774cSChristoph Hellwig 
8381025774cSChristoph Hellwig 	setattr_copy(inode, iattr);
8391025774cSChristoph Hellwig 	mark_inode_dirty(inode);
8401025774cSChristoph Hellwig 
8411025774cSChristoph Hellwig 	if (iattr->ia_valid & ATTR_MODE) {
8421025774cSChristoph Hellwig 		err = nilfs_acl_chmod(inode);
8431025774cSChristoph Hellwig 		if (unlikely(err))
8441025774cSChristoph Hellwig 			goto out_err;
8451025774cSChristoph Hellwig 	}
8461025774cSChristoph Hellwig 
8471025774cSChristoph Hellwig 	return nilfs_transaction_commit(sb);
8481025774cSChristoph Hellwig 
8491025774cSChristoph Hellwig out_err:
8501025774cSChristoph Hellwig 	nilfs_transaction_abort(sb);
85147420c79SRyusuke Konishi 	return err;
85205fe58fdSRyusuke Konishi }
85305fe58fdSRyusuke Konishi 
85410556cb2SAl Viro int nilfs_permission(struct inode *inode, int mask)
855dc3d3b81SRyusuke Konishi {
856730e908fSAl Viro 	struct nilfs_root *root = NILFS_I(inode)->i_root;
8574ad364caSRyusuke Konishi 
858dc3d3b81SRyusuke Konishi 	if ((mask & MAY_WRITE) && root &&
859dc3d3b81SRyusuke Konishi 	    root->cno != NILFS_CPTREE_CURRENT_CNO)
860dc3d3b81SRyusuke Konishi 		return -EROFS; /* snapshot is not writable */
861dc3d3b81SRyusuke Konishi 
8622830ba7fSAl Viro 	return generic_permission(inode, mask);
863dc3d3b81SRyusuke Konishi }
864dc3d3b81SRyusuke Konishi 
865bcbc8c64SRyusuke Konishi int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
86605fe58fdSRyusuke Konishi {
867e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
86805fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
86905fe58fdSRyusuke Konishi 	int err;
87005fe58fdSRyusuke Konishi 
871693dd321SRyusuke Konishi 	spin_lock(&nilfs->ns_inode_lock);
87205fe58fdSRyusuke Konishi 	if (ii->i_bh == NULL) {
873693dd321SRyusuke Konishi 		spin_unlock(&nilfs->ns_inode_lock);
874e912a5b6SRyusuke Konishi 		err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
875e912a5b6SRyusuke Konishi 						  inode->i_ino, pbh);
87605fe58fdSRyusuke Konishi 		if (unlikely(err))
87705fe58fdSRyusuke Konishi 			return err;
878693dd321SRyusuke Konishi 		spin_lock(&nilfs->ns_inode_lock);
87905fe58fdSRyusuke Konishi 		if (ii->i_bh == NULL)
88005fe58fdSRyusuke Konishi 			ii->i_bh = *pbh;
88105fe58fdSRyusuke Konishi 		else {
88205fe58fdSRyusuke Konishi 			brelse(*pbh);
88305fe58fdSRyusuke Konishi 			*pbh = ii->i_bh;
88405fe58fdSRyusuke Konishi 		}
88505fe58fdSRyusuke Konishi 	} else
88605fe58fdSRyusuke Konishi 		*pbh = ii->i_bh;
88705fe58fdSRyusuke Konishi 
88805fe58fdSRyusuke Konishi 	get_bh(*pbh);
889693dd321SRyusuke Konishi 	spin_unlock(&nilfs->ns_inode_lock);
89005fe58fdSRyusuke Konishi 	return 0;
89105fe58fdSRyusuke Konishi }
89205fe58fdSRyusuke Konishi 
89305fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode)
89405fe58fdSRyusuke Konishi {
89505fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
896e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
89705fe58fdSRyusuke Konishi 	int ret = 0;
89805fe58fdSRyusuke Konishi 
89905fe58fdSRyusuke Konishi 	if (!list_empty(&ii->i_dirty)) {
900693dd321SRyusuke Konishi 		spin_lock(&nilfs->ns_inode_lock);
90105fe58fdSRyusuke Konishi 		ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
90205fe58fdSRyusuke Konishi 			test_bit(NILFS_I_BUSY, &ii->i_state);
903693dd321SRyusuke Konishi 		spin_unlock(&nilfs->ns_inode_lock);
90405fe58fdSRyusuke Konishi 	}
90505fe58fdSRyusuke Konishi 	return ret;
90605fe58fdSRyusuke Konishi }
90705fe58fdSRyusuke Konishi 
9080c6c44cbSRyusuke Konishi int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty)
90905fe58fdSRyusuke Konishi {
91005fe58fdSRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
911e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
91205fe58fdSRyusuke Konishi 
913693dd321SRyusuke Konishi 	atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
91405fe58fdSRyusuke Konishi 
915458c5b08SRyusuke Konishi 	if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
91605fe58fdSRyusuke Konishi 		return 0;
91705fe58fdSRyusuke Konishi 
918693dd321SRyusuke Konishi 	spin_lock(&nilfs->ns_inode_lock);
91905fe58fdSRyusuke Konishi 	if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
92005fe58fdSRyusuke Konishi 	    !test_bit(NILFS_I_BUSY, &ii->i_state)) {
921076a378bSRyusuke Konishi 		/*
922076a378bSRyusuke Konishi 		 * Because this routine may race with nilfs_dispose_list(),
923076a378bSRyusuke Konishi 		 * we have to check NILFS_I_QUEUED here, too.
924076a378bSRyusuke Konishi 		 */
92505fe58fdSRyusuke Konishi 		if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
926076a378bSRyusuke Konishi 			/*
927076a378bSRyusuke Konishi 			 * This will happen when somebody is freeing
928076a378bSRyusuke Konishi 			 * this inode.
929076a378bSRyusuke Konishi 			 */
930d6517debSRyusuke Konishi 			nilfs_msg(inode->i_sb, KERN_WARNING,
931d6517debSRyusuke Konishi 				  "cannot set file dirty (ino=%lu): the file is being freed",
93205fe58fdSRyusuke Konishi 				  inode->i_ino);
933693dd321SRyusuke Konishi 			spin_unlock(&nilfs->ns_inode_lock);
934076a378bSRyusuke Konishi 			return -EINVAL; /*
935076a378bSRyusuke Konishi 					 * NILFS_I_DIRTY may remain for
936076a378bSRyusuke Konishi 					 * freeing inode.
937076a378bSRyusuke Konishi 					 */
93805fe58fdSRyusuke Konishi 		}
939eaae0f37SNicolas Kaiser 		list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
94005fe58fdSRyusuke Konishi 		set_bit(NILFS_I_QUEUED, &ii->i_state);
94105fe58fdSRyusuke Konishi 	}
942693dd321SRyusuke Konishi 	spin_unlock(&nilfs->ns_inode_lock);
94305fe58fdSRyusuke Konishi 	return 0;
94405fe58fdSRyusuke Konishi }
94505fe58fdSRyusuke Konishi 
946b9f66140SAndreas Rohner int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
94705fe58fdSRyusuke Konishi {
94805fe58fdSRyusuke Konishi 	struct buffer_head *ibh;
94905fe58fdSRyusuke Konishi 	int err;
95005fe58fdSRyusuke Konishi 
951bcbc8c64SRyusuke Konishi 	err = nilfs_load_inode_block(inode, &ibh);
95205fe58fdSRyusuke Konishi 	if (unlikely(err)) {
953d6517debSRyusuke Konishi 		nilfs_msg(inode->i_sb, KERN_WARNING,
954d6517debSRyusuke Konishi 			  "cannot mark inode dirty (ino=%lu): error %d loading inode block",
955d6517debSRyusuke Konishi 			  inode->i_ino, err);
95605fe58fdSRyusuke Konishi 		return err;
95705fe58fdSRyusuke Konishi 	}
958b9f66140SAndreas Rohner 	nilfs_update_inode(inode, ibh, flags);
9595fc7b141SRyusuke Konishi 	mark_buffer_dirty(ibh);
960e912a5b6SRyusuke Konishi 	nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
96105fe58fdSRyusuke Konishi 	brelse(ibh);
96205fe58fdSRyusuke Konishi 	return 0;
96305fe58fdSRyusuke Konishi }
96405fe58fdSRyusuke Konishi 
96505fe58fdSRyusuke Konishi /**
96605fe58fdSRyusuke Konishi  * nilfs_dirty_inode - reflect changes on given inode to an inode block.
96705fe58fdSRyusuke Konishi  * @inode: inode of the file to be registered.
96805fe58fdSRyusuke Konishi  *
96905fe58fdSRyusuke Konishi  * nilfs_dirty_inode() loads a inode block containing the specified
97005fe58fdSRyusuke Konishi  * @inode and copies data from a nilfs_inode to a corresponding inode
97105fe58fdSRyusuke Konishi  * entry in the inode block. This operation is excluded from the segment
97205fe58fdSRyusuke Konishi  * construction. This function can be called both as a single operation
97305fe58fdSRyusuke Konishi  * and as a part of indivisible file operations.
97405fe58fdSRyusuke Konishi  */
975aa385729SChristoph Hellwig void nilfs_dirty_inode(struct inode *inode, int flags)
97605fe58fdSRyusuke Konishi {
97705fe58fdSRyusuke Konishi 	struct nilfs_transaction_info ti;
9787d6cd92fSRyusuke Konishi 	struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
97905fe58fdSRyusuke Konishi 
98005fe58fdSRyusuke Konishi 	if (is_bad_inode(inode)) {
981d6517debSRyusuke Konishi 		nilfs_msg(inode->i_sb, KERN_WARNING,
98206f4abf6SRyusuke Konishi 			  "tried to mark bad_inode dirty. ignored.");
98305fe58fdSRyusuke Konishi 		dump_stack();
98405fe58fdSRyusuke Konishi 		return;
98505fe58fdSRyusuke Konishi 	}
9867d6cd92fSRyusuke Konishi 	if (mdi) {
9877d6cd92fSRyusuke Konishi 		nilfs_mdt_mark_dirty(inode);
9887d6cd92fSRyusuke Konishi 		return;
9897d6cd92fSRyusuke Konishi 	}
99005fe58fdSRyusuke Konishi 	nilfs_transaction_begin(inode->i_sb, &ti, 0);
991b9f66140SAndreas Rohner 	__nilfs_mark_inode_dirty(inode, flags);
99247420c79SRyusuke Konishi 	nilfs_transaction_commit(inode->i_sb); /* never fails */
99305fe58fdSRyusuke Konishi }
994622daaffSRyusuke Konishi 
995622daaffSRyusuke Konishi int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
996622daaffSRyusuke Konishi 		 __u64 start, __u64 len)
997622daaffSRyusuke Konishi {
9980ef28f9aSRyusuke Konishi 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
999622daaffSRyusuke Konishi 	__u64 logical = 0, phys = 0, size = 0;
1000622daaffSRyusuke Konishi 	__u32 flags = 0;
1001622daaffSRyusuke Konishi 	loff_t isize;
1002622daaffSRyusuke Konishi 	sector_t blkoff, end_blkoff;
1003622daaffSRyusuke Konishi 	sector_t delalloc_blkoff;
1004622daaffSRyusuke Konishi 	unsigned long delalloc_blklen;
1005622daaffSRyusuke Konishi 	unsigned int blkbits = inode->i_blkbits;
1006622daaffSRyusuke Konishi 	int ret, n;
1007622daaffSRyusuke Konishi 
1008622daaffSRyusuke Konishi 	ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1009622daaffSRyusuke Konishi 	if (ret)
1010622daaffSRyusuke Konishi 		return ret;
1011622daaffSRyusuke Konishi 
10125955102cSAl Viro 	inode_lock(inode);
1013622daaffSRyusuke Konishi 
1014622daaffSRyusuke Konishi 	isize = i_size_read(inode);
1015622daaffSRyusuke Konishi 
1016622daaffSRyusuke Konishi 	blkoff = start >> blkbits;
1017622daaffSRyusuke Konishi 	end_blkoff = (start + len - 1) >> blkbits;
1018622daaffSRyusuke Konishi 
1019622daaffSRyusuke Konishi 	delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
1020622daaffSRyusuke Konishi 							&delalloc_blkoff);
1021622daaffSRyusuke Konishi 
1022622daaffSRyusuke Konishi 	do {
1023622daaffSRyusuke Konishi 		__u64 blkphy;
1024622daaffSRyusuke Konishi 		unsigned int maxblocks;
1025622daaffSRyusuke Konishi 
1026622daaffSRyusuke Konishi 		if (delalloc_blklen && blkoff == delalloc_blkoff) {
1027622daaffSRyusuke Konishi 			if (size) {
1028622daaffSRyusuke Konishi 				/* End of the current extent */
1029622daaffSRyusuke Konishi 				ret = fiemap_fill_next_extent(
1030622daaffSRyusuke Konishi 					fieinfo, logical, phys, size, flags);
1031622daaffSRyusuke Konishi 				if (ret)
1032622daaffSRyusuke Konishi 					break;
1033622daaffSRyusuke Konishi 			}
1034622daaffSRyusuke Konishi 			if (blkoff > end_blkoff)
1035622daaffSRyusuke Konishi 				break;
1036622daaffSRyusuke Konishi 
1037622daaffSRyusuke Konishi 			flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
1038622daaffSRyusuke Konishi 			logical = blkoff << blkbits;
1039622daaffSRyusuke Konishi 			phys = 0;
1040622daaffSRyusuke Konishi 			size = delalloc_blklen << blkbits;
1041622daaffSRyusuke Konishi 
1042622daaffSRyusuke Konishi 			blkoff = delalloc_blkoff + delalloc_blklen;
1043622daaffSRyusuke Konishi 			delalloc_blklen = nilfs_find_uncommitted_extent(
1044622daaffSRyusuke Konishi 				inode, blkoff, &delalloc_blkoff);
1045622daaffSRyusuke Konishi 			continue;
1046622daaffSRyusuke Konishi 		}
1047622daaffSRyusuke Konishi 
1048622daaffSRyusuke Konishi 		/*
1049622daaffSRyusuke Konishi 		 * Limit the number of blocks that we look up so as
1050622daaffSRyusuke Konishi 		 * not to get into the next delayed allocation extent.
1051622daaffSRyusuke Konishi 		 */
1052622daaffSRyusuke Konishi 		maxblocks = INT_MAX;
1053622daaffSRyusuke Konishi 		if (delalloc_blklen)
1054622daaffSRyusuke Konishi 			maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
1055622daaffSRyusuke Konishi 					  maxblocks);
1056622daaffSRyusuke Konishi 		blkphy = 0;
1057622daaffSRyusuke Konishi 
1058622daaffSRyusuke Konishi 		down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1059622daaffSRyusuke Konishi 		n = nilfs_bmap_lookup_contig(
1060622daaffSRyusuke Konishi 			NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
1061622daaffSRyusuke Konishi 		up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1062622daaffSRyusuke Konishi 
1063622daaffSRyusuke Konishi 		if (n < 0) {
1064622daaffSRyusuke Konishi 			int past_eof;
1065622daaffSRyusuke Konishi 
1066622daaffSRyusuke Konishi 			if (unlikely(n != -ENOENT))
1067622daaffSRyusuke Konishi 				break; /* error */
1068622daaffSRyusuke Konishi 
1069622daaffSRyusuke Konishi 			/* HOLE */
1070622daaffSRyusuke Konishi 			blkoff++;
1071622daaffSRyusuke Konishi 			past_eof = ((blkoff << blkbits) >= isize);
1072622daaffSRyusuke Konishi 
1073622daaffSRyusuke Konishi 			if (size) {
1074622daaffSRyusuke Konishi 				/* End of the current extent */
1075622daaffSRyusuke Konishi 
1076622daaffSRyusuke Konishi 				if (past_eof)
1077622daaffSRyusuke Konishi 					flags |= FIEMAP_EXTENT_LAST;
1078622daaffSRyusuke Konishi 
1079622daaffSRyusuke Konishi 				ret = fiemap_fill_next_extent(
1080622daaffSRyusuke Konishi 					fieinfo, logical, phys, size, flags);
1081622daaffSRyusuke Konishi 				if (ret)
1082622daaffSRyusuke Konishi 					break;
1083622daaffSRyusuke Konishi 				size = 0;
1084622daaffSRyusuke Konishi 			}
1085622daaffSRyusuke Konishi 			if (blkoff > end_blkoff || past_eof)
1086622daaffSRyusuke Konishi 				break;
1087622daaffSRyusuke Konishi 		} else {
1088622daaffSRyusuke Konishi 			if (size) {
1089622daaffSRyusuke Konishi 				if (phys && blkphy << blkbits == phys + size) {
1090622daaffSRyusuke Konishi 					/* The current extent goes on */
1091622daaffSRyusuke Konishi 					size += n << blkbits;
1092622daaffSRyusuke Konishi 				} else {
1093622daaffSRyusuke Konishi 					/* Terminate the current extent */
1094622daaffSRyusuke Konishi 					ret = fiemap_fill_next_extent(
1095622daaffSRyusuke Konishi 						fieinfo, logical, phys, size,
1096622daaffSRyusuke Konishi 						flags);
1097622daaffSRyusuke Konishi 					if (ret || blkoff > end_blkoff)
1098622daaffSRyusuke Konishi 						break;
1099622daaffSRyusuke Konishi 
1100622daaffSRyusuke Konishi 					/* Start another extent */
1101622daaffSRyusuke Konishi 					flags = FIEMAP_EXTENT_MERGED;
1102622daaffSRyusuke Konishi 					logical = blkoff << blkbits;
1103622daaffSRyusuke Konishi 					phys = blkphy << blkbits;
1104622daaffSRyusuke Konishi 					size = n << blkbits;
1105622daaffSRyusuke Konishi 				}
1106622daaffSRyusuke Konishi 			} else {
1107622daaffSRyusuke Konishi 				/* Start a new extent */
1108622daaffSRyusuke Konishi 				flags = FIEMAP_EXTENT_MERGED;
1109622daaffSRyusuke Konishi 				logical = blkoff << blkbits;
1110622daaffSRyusuke Konishi 				phys = blkphy << blkbits;
1111622daaffSRyusuke Konishi 				size = n << blkbits;
1112622daaffSRyusuke Konishi 			}
1113622daaffSRyusuke Konishi 			blkoff += n;
1114622daaffSRyusuke Konishi 		}
1115622daaffSRyusuke Konishi 		cond_resched();
1116622daaffSRyusuke Konishi 	} while (true);
1117622daaffSRyusuke Konishi 
1118622daaffSRyusuke Konishi 	/* If ret is 1 then we just hit the end of the extent array */
1119622daaffSRyusuke Konishi 	if (ret == 1)
1120622daaffSRyusuke Konishi 		ret = 0;
1121622daaffSRyusuke Konishi 
11225955102cSAl Viro 	inode_unlock(inode);
1123622daaffSRyusuke Konishi 	return ret;
1124622daaffSRyusuke Konishi }
1125