1ae98043fSRyusuke 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 148*d4388340SMatthew Wilcox (Oracle) static void nilfs_readahead(struct readahead_control *rac) 14905fe58fdSRyusuke Konishi { 150*d4388340SMatthew Wilcox (Oracle) mpage_readahead(rac, nilfs_get_block); 15105fe58fdSRyusuke Konishi } 15205fe58fdSRyusuke Konishi 15305fe58fdSRyusuke Konishi static int nilfs_writepages(struct address_space *mapping, 15405fe58fdSRyusuke Konishi struct writeback_control *wbc) 15505fe58fdSRyusuke Konishi { 156f30bf3e4SRyusuke Konishi struct inode *inode = mapping->host; 157f30bf3e4SRyusuke Konishi int err = 0; 158f30bf3e4SRyusuke Konishi 159bc98a42cSDavid Howells if (sb_rdonly(inode->i_sb)) { 1608c26c4e2SVyacheslav Dubeyko nilfs_clear_dirty_pages(mapping, false); 1618c26c4e2SVyacheslav Dubeyko return -EROFS; 1628c26c4e2SVyacheslav Dubeyko } 1638c26c4e2SVyacheslav Dubeyko 164f30bf3e4SRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) 165f30bf3e4SRyusuke Konishi err = nilfs_construct_dsync_segment(inode->i_sb, inode, 166f30bf3e4SRyusuke Konishi wbc->range_start, 167f30bf3e4SRyusuke Konishi wbc->range_end); 168f30bf3e4SRyusuke Konishi return err; 16905fe58fdSRyusuke Konishi } 17005fe58fdSRyusuke Konishi 17105fe58fdSRyusuke Konishi static int nilfs_writepage(struct page *page, struct writeback_control *wbc) 17205fe58fdSRyusuke Konishi { 17305fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 17405fe58fdSRyusuke Konishi int err; 17505fe58fdSRyusuke Konishi 176bc98a42cSDavid Howells if (sb_rdonly(inode->i_sb)) { 1778c26c4e2SVyacheslav Dubeyko /* 1788c26c4e2SVyacheslav Dubeyko * It means that filesystem was remounted in read-only 1798c26c4e2SVyacheslav Dubeyko * mode because of error or metadata corruption. But we 1808c26c4e2SVyacheslav Dubeyko * have dirty pages that try to be flushed in background. 1818c26c4e2SVyacheslav Dubeyko * So, here we simply discard this dirty page. 1828c26c4e2SVyacheslav Dubeyko */ 1838c26c4e2SVyacheslav Dubeyko nilfs_clear_dirty_page(page, false); 1848c26c4e2SVyacheslav Dubeyko unlock_page(page); 1858c26c4e2SVyacheslav Dubeyko return -EROFS; 1868c26c4e2SVyacheslav Dubeyko } 1878c26c4e2SVyacheslav Dubeyko 18805fe58fdSRyusuke Konishi redirty_page_for_writepage(wbc, page); 18905fe58fdSRyusuke Konishi unlock_page(page); 19005fe58fdSRyusuke Konishi 19105fe58fdSRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) { 19205fe58fdSRyusuke Konishi err = nilfs_construct_segment(inode->i_sb); 19305fe58fdSRyusuke Konishi if (unlikely(err)) 19405fe58fdSRyusuke Konishi return err; 19505fe58fdSRyusuke Konishi } else if (wbc->for_reclaim) 19605fe58fdSRyusuke Konishi nilfs_flush_segment(inode->i_sb, inode->i_ino); 19705fe58fdSRyusuke Konishi 19805fe58fdSRyusuke Konishi return 0; 19905fe58fdSRyusuke Konishi } 20005fe58fdSRyusuke Konishi 20105fe58fdSRyusuke Konishi static int nilfs_set_page_dirty(struct page *page) 20205fe58fdSRyusuke Konishi { 20356d7acc7SAndreas Rohner struct inode *inode = page->mapping->host; 204136e8770SRyusuke Konishi int ret = __set_page_dirty_nobuffers(page); 20505fe58fdSRyusuke Konishi 206136e8770SRyusuke Konishi if (page_has_buffers(page)) { 2070c6c44cbSRyusuke Konishi unsigned int nr_dirty = 0; 208136e8770SRyusuke Konishi struct buffer_head *bh, *head; 20905fe58fdSRyusuke Konishi 210136e8770SRyusuke Konishi /* 211136e8770SRyusuke Konishi * This page is locked by callers, and no other thread 212136e8770SRyusuke Konishi * concurrently marks its buffers dirty since they are 213136e8770SRyusuke Konishi * only dirtied through routines in fs/buffer.c in 214136e8770SRyusuke Konishi * which call sites of mark_buffer_dirty are protected 215136e8770SRyusuke Konishi * by page lock. 216136e8770SRyusuke Konishi */ 217136e8770SRyusuke Konishi bh = head = page_buffers(page); 218136e8770SRyusuke Konishi do { 219136e8770SRyusuke Konishi /* Do not mark hole blocks dirty */ 220136e8770SRyusuke Konishi if (buffer_dirty(bh) || !buffer_mapped(bh)) 221136e8770SRyusuke Konishi continue; 222136e8770SRyusuke Konishi 223136e8770SRyusuke Konishi set_buffer_dirty(bh); 224136e8770SRyusuke Konishi nr_dirty++; 225136e8770SRyusuke Konishi } while (bh = bh->b_this_page, bh != head); 226136e8770SRyusuke Konishi 227136e8770SRyusuke Konishi if (nr_dirty) 228bcbc8c64SRyusuke Konishi nilfs_set_file_dirty(inode, nr_dirty); 22956d7acc7SAndreas Rohner } else if (ret) { 2300c6c44cbSRyusuke Konishi unsigned int nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); 23156d7acc7SAndreas Rohner 23256d7acc7SAndreas Rohner nilfs_set_file_dirty(inode, nr_dirty); 23305fe58fdSRyusuke Konishi } 23405fe58fdSRyusuke Konishi return ret; 23505fe58fdSRyusuke Konishi } 23605fe58fdSRyusuke Konishi 2372d1b399bSMarco Stornelli void nilfs_write_failed(struct address_space *mapping, loff_t to) 2382d1b399bSMarco Stornelli { 2392d1b399bSMarco Stornelli struct inode *inode = mapping->host; 2402d1b399bSMarco Stornelli 2412d1b399bSMarco Stornelli if (to > inode->i_size) { 2427caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 2432d1b399bSMarco Stornelli nilfs_truncate(inode); 2442d1b399bSMarco Stornelli } 2452d1b399bSMarco Stornelli } 2462d1b399bSMarco Stornelli 24705fe58fdSRyusuke Konishi static int nilfs_write_begin(struct file *file, struct address_space *mapping, 24805fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned flags, 24905fe58fdSRyusuke Konishi struct page **pagep, void **fsdata) 25005fe58fdSRyusuke Konishi 25105fe58fdSRyusuke Konishi { 25205fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 25305fe58fdSRyusuke Konishi int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); 25405fe58fdSRyusuke Konishi 25505fe58fdSRyusuke Konishi if (unlikely(err)) 25605fe58fdSRyusuke Konishi return err; 25705fe58fdSRyusuke Konishi 258155130a4SChristoph Hellwig err = block_write_begin(mapping, pos, len, flags, pagep, 259155130a4SChristoph Hellwig nilfs_get_block); 260155130a4SChristoph Hellwig if (unlikely(err)) { 2612d1b399bSMarco Stornelli nilfs_write_failed(mapping, pos + len); 26247420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 263155130a4SChristoph Hellwig } 26405fe58fdSRyusuke Konishi return err; 26505fe58fdSRyusuke Konishi } 26605fe58fdSRyusuke Konishi 26705fe58fdSRyusuke Konishi static int nilfs_write_end(struct file *file, struct address_space *mapping, 26805fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned copied, 26905fe58fdSRyusuke Konishi struct page *page, void *fsdata) 27005fe58fdSRyusuke Konishi { 27105fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 2720c6c44cbSRyusuke Konishi unsigned int start = pos & (PAGE_SIZE - 1); 2730c6c44cbSRyusuke Konishi unsigned int nr_dirty; 27405fe58fdSRyusuke Konishi int err; 27505fe58fdSRyusuke Konishi 27605fe58fdSRyusuke Konishi nr_dirty = nilfs_page_count_clean_buffers(page, start, 27705fe58fdSRyusuke Konishi start + copied); 27805fe58fdSRyusuke Konishi copied = generic_write_end(file, mapping, pos, len, copied, page, 27905fe58fdSRyusuke Konishi fsdata); 280bcbc8c64SRyusuke Konishi nilfs_set_file_dirty(inode, nr_dirty); 28147420c79SRyusuke Konishi err = nilfs_transaction_commit(inode->i_sb); 28205fe58fdSRyusuke Konishi return err ? : copied; 28305fe58fdSRyusuke Konishi } 28405fe58fdSRyusuke Konishi 28505fe58fdSRyusuke Konishi static ssize_t 286c8b8e32dSChristoph Hellwig nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 28705fe58fdSRyusuke Konishi { 2886b6dabc8SAl Viro struct inode *inode = file_inode(iocb->ki_filp); 28905fe58fdSRyusuke Konishi 2906f673763SOmar Sandoval if (iov_iter_rw(iter) == WRITE) 29105fe58fdSRyusuke Konishi return 0; 29205fe58fdSRyusuke Konishi 29305fe58fdSRyusuke Konishi /* Needs synchronization with the cleaner */ 294c8b8e32dSChristoph Hellwig return blockdev_direct_IO(iocb, inode, iter, nilfs_get_block); 29505fe58fdSRyusuke Konishi } 29605fe58fdSRyusuke Konishi 2977f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = { 29805fe58fdSRyusuke Konishi .writepage = nilfs_writepage, 29905fe58fdSRyusuke Konishi .readpage = nilfs_readpage, 30005fe58fdSRyusuke Konishi .writepages = nilfs_writepages, 30105fe58fdSRyusuke Konishi .set_page_dirty = nilfs_set_page_dirty, 302*d4388340SMatthew Wilcox (Oracle) .readahead = nilfs_readahead, 30305fe58fdSRyusuke Konishi .write_begin = nilfs_write_begin, 30405fe58fdSRyusuke Konishi .write_end = nilfs_write_end, 30505fe58fdSRyusuke Konishi /* .releasepage = nilfs_releasepage, */ 30605fe58fdSRyusuke Konishi .invalidatepage = block_invalidatepage, 30705fe58fdSRyusuke Konishi .direct_IO = nilfs_direct_IO, 308258ef67eSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 30905fe58fdSRyusuke Konishi }; 31005fe58fdSRyusuke Konishi 311705304a8SRyusuke Konishi static int nilfs_insert_inode_locked(struct inode *inode, 312705304a8SRyusuke Konishi struct nilfs_root *root, 313705304a8SRyusuke Konishi unsigned long ino) 314705304a8SRyusuke Konishi { 315705304a8SRyusuke Konishi struct nilfs_iget_args args = { 316705304a8SRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 317705304a8SRyusuke Konishi }; 318705304a8SRyusuke Konishi 319705304a8SRyusuke Konishi return insert_inode_locked4(inode, ino, nilfs_iget_test, &args); 320705304a8SRyusuke Konishi } 321705304a8SRyusuke Konishi 322c6e49e3fSAl Viro struct inode *nilfs_new_inode(struct inode *dir, umode_t mode) 32305fe58fdSRyusuke Konishi { 32405fe58fdSRyusuke Konishi struct super_block *sb = dir->i_sb; 325e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info; 32605fe58fdSRyusuke Konishi struct inode *inode; 32705fe58fdSRyusuke Konishi struct nilfs_inode_info *ii; 3284d8d9293SRyusuke Konishi struct nilfs_root *root; 32905fe58fdSRyusuke Konishi int err = -ENOMEM; 33005fe58fdSRyusuke Konishi ino_t ino; 33105fe58fdSRyusuke Konishi 33205fe58fdSRyusuke Konishi inode = new_inode(sb); 33305fe58fdSRyusuke Konishi if (unlikely(!inode)) 33405fe58fdSRyusuke Konishi goto failed; 33505fe58fdSRyusuke Konishi 33605fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 337c62d2555SMichal Hocko mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS)); 33805fe58fdSRyusuke Konishi 3394d8d9293SRyusuke Konishi root = NILFS_I(dir)->i_root; 34005fe58fdSRyusuke Konishi ii = NILFS_I(inode); 3414ce5c342SRyusuke Konishi ii->i_state = BIT(NILFS_I_NEW); 3424d8d9293SRyusuke Konishi ii->i_root = root; 34305fe58fdSRyusuke Konishi 344e912a5b6SRyusuke Konishi err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); 34505fe58fdSRyusuke Konishi if (unlikely(err)) 34605fe58fdSRyusuke Konishi goto failed_ifile_create_inode; 34705fe58fdSRyusuke Konishi /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 34805fe58fdSRyusuke Konishi 349e5f7f848SVyacheslav Dubeyko atomic64_inc(&root->inodes_count); 35073459dccSDmitry Monakhov inode_init_owner(inode, dir, mode); 35105fe58fdSRyusuke Konishi inode->i_ino = ino; 352078cd827SDeepa Dinamani inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 35305fe58fdSRyusuke Konishi 35405fe58fdSRyusuke Konishi if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 35505fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, NULL); 35605fe58fdSRyusuke Konishi if (err < 0) 357705304a8SRyusuke Konishi goto failed_after_creation; 35805fe58fdSRyusuke Konishi 35905fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 36005fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 36105fe58fdSRyusuke Konishi } 36205fe58fdSRyusuke Konishi 363b253a3e4SRyusuke Konishi ii->i_flags = nilfs_mask_flags( 364b253a3e4SRyusuke Konishi mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED); 36505fe58fdSRyusuke Konishi 36605fe58fdSRyusuke Konishi /* ii->i_file_acl = 0; */ 36705fe58fdSRyusuke Konishi /* ii->i_dir_acl = 0; */ 36805fe58fdSRyusuke Konishi ii->i_dir_start_lookup = 0; 36905fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 3709b1fc4e4SRyusuke Konishi spin_lock(&nilfs->ns_next_gen_lock); 3719b1fc4e4SRyusuke Konishi inode->i_generation = nilfs->ns_next_generation++; 3729b1fc4e4SRyusuke Konishi spin_unlock(&nilfs->ns_next_gen_lock); 373705304a8SRyusuke Konishi if (nilfs_insert_inode_locked(inode, root, ino) < 0) { 374705304a8SRyusuke Konishi err = -EIO; 375705304a8SRyusuke Konishi goto failed_after_creation; 376705304a8SRyusuke Konishi } 37705fe58fdSRyusuke Konishi 37805fe58fdSRyusuke Konishi err = nilfs_init_acl(inode, dir); 37905fe58fdSRyusuke Konishi if (unlikely(err)) 380076a378bSRyusuke Konishi /* 381076a378bSRyusuke Konishi * Never occur. When supporting nilfs_init_acl(), 382076a378bSRyusuke Konishi * proper cancellation of above jobs should be considered. 383076a378bSRyusuke Konishi */ 384076a378bSRyusuke Konishi goto failed_after_creation; 38505fe58fdSRyusuke Konishi 38605fe58fdSRyusuke Konishi return inode; 38705fe58fdSRyusuke Konishi 388705304a8SRyusuke Konishi failed_after_creation: 3896d6b77f1SMiklos Szeredi clear_nlink(inode); 390705304a8SRyusuke Konishi unlock_new_inode(inode); 391076a378bSRyusuke Konishi iput(inode); /* 392076a378bSRyusuke Konishi * raw_inode will be deleted through 393076a378bSRyusuke Konishi * nilfs_evict_inode(). 394076a378bSRyusuke Konishi */ 39505fe58fdSRyusuke Konishi goto failed; 39605fe58fdSRyusuke Konishi 39705fe58fdSRyusuke Konishi failed_ifile_create_inode: 39805fe58fdSRyusuke Konishi make_bad_inode(inode); 399076a378bSRyusuke Konishi iput(inode); 40005fe58fdSRyusuke Konishi failed: 40105fe58fdSRyusuke Konishi return ERR_PTR(err); 40205fe58fdSRyusuke Konishi } 40305fe58fdSRyusuke Konishi 40405fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode) 40505fe58fdSRyusuke Konishi { 40605fe58fdSRyusuke Konishi unsigned int flags = NILFS_I(inode)->i_flags; 407faea2c53SRyusuke Konishi unsigned int new_fl = 0; 40805fe58fdSRyusuke Konishi 409f0c9f242SRyusuke Konishi if (flags & FS_SYNC_FL) 410faea2c53SRyusuke Konishi new_fl |= S_SYNC; 411f0c9f242SRyusuke Konishi if (flags & FS_APPEND_FL) 412faea2c53SRyusuke Konishi new_fl |= S_APPEND; 413f0c9f242SRyusuke Konishi if (flags & FS_IMMUTABLE_FL) 414faea2c53SRyusuke Konishi new_fl |= S_IMMUTABLE; 415f0c9f242SRyusuke Konishi if (flags & FS_NOATIME_FL) 416faea2c53SRyusuke Konishi new_fl |= S_NOATIME; 417f0c9f242SRyusuke Konishi if (flags & FS_DIRSYNC_FL) 418faea2c53SRyusuke Konishi new_fl |= S_DIRSYNC; 419faea2c53SRyusuke Konishi inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE | 420faea2c53SRyusuke Konishi S_NOATIME | S_DIRSYNC); 42105fe58fdSRyusuke Konishi } 42205fe58fdSRyusuke Konishi 42305fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode, 42405fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode) 42505fe58fdSRyusuke Konishi { 42605fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 42705fe58fdSRyusuke Konishi int err; 42805fe58fdSRyusuke Konishi 42905fe58fdSRyusuke Konishi inode->i_mode = le16_to_cpu(raw_inode->i_mode); 430305d3d0dSEric W. Biederman i_uid_write(inode, le32_to_cpu(raw_inode->i_uid)); 431305d3d0dSEric W. Biederman i_gid_write(inode, le32_to_cpu(raw_inode->i_gid)); 432bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 43305fe58fdSRyusuke Konishi inode->i_size = le64_to_cpu(raw_inode->i_size); 43405fe58fdSRyusuke Konishi inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 43505fe58fdSRyusuke Konishi inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); 43605fe58fdSRyusuke Konishi inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 43761239230SRyusuke Konishi inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 43861239230SRyusuke Konishi inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); 43961239230SRyusuke Konishi inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 440705304a8SRyusuke Konishi if (inode->i_nlink == 0) 441705304a8SRyusuke Konishi return -ESTALE; /* this inode is deleted */ 44205fe58fdSRyusuke Konishi 44305fe58fdSRyusuke Konishi inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 44405fe58fdSRyusuke Konishi ii->i_flags = le32_to_cpu(raw_inode->i_flags); 44505fe58fdSRyusuke Konishi #if 0 44605fe58fdSRyusuke Konishi ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 44705fe58fdSRyusuke Konishi ii->i_dir_acl = S_ISREG(inode->i_mode) ? 44805fe58fdSRyusuke Konishi 0 : le32_to_cpu(raw_inode->i_dir_acl); 44905fe58fdSRyusuke Konishi #endif 4503cc811bfSRyusuke Konishi ii->i_dir_start_lookup = 0; 45105fe58fdSRyusuke Konishi inode->i_generation = le32_to_cpu(raw_inode->i_generation); 45205fe58fdSRyusuke Konishi 45305fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 45405fe58fdSRyusuke Konishi S_ISLNK(inode->i_mode)) { 45505fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, raw_inode); 45605fe58fdSRyusuke Konishi if (err < 0) 45705fe58fdSRyusuke Konishi return err; 45805fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 45905fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 46005fe58fdSRyusuke Konishi } 46105fe58fdSRyusuke Konishi return 0; 46205fe58fdSRyusuke Konishi } 46305fe58fdSRyusuke Konishi 464e912a5b6SRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb, 465e912a5b6SRyusuke Konishi struct nilfs_root *root, unsigned long ino, 46605fe58fdSRyusuke Konishi struct inode *inode) 46705fe58fdSRyusuke Konishi { 468e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info; 46905fe58fdSRyusuke Konishi struct buffer_head *bh; 47005fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 47105fe58fdSRyusuke Konishi int err; 47205fe58fdSRyusuke Konishi 473365e215cSRyusuke Konishi down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 474e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); 47505fe58fdSRyusuke Konishi if (unlikely(err)) 47605fe58fdSRyusuke Konishi goto bad_inode; 47705fe58fdSRyusuke Konishi 478e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); 47905fe58fdSRyusuke Konishi 4801b2f5a64SRyusuke Konishi err = nilfs_read_inode_common(inode, raw_inode); 4811b2f5a64SRyusuke Konishi if (err) 48205fe58fdSRyusuke Konishi goto failed_unmap; 48305fe58fdSRyusuke Konishi 48405fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode)) { 48505fe58fdSRyusuke Konishi inode->i_op = &nilfs_file_inode_operations; 48605fe58fdSRyusuke Konishi inode->i_fop = &nilfs_file_operations; 48705fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 48805fe58fdSRyusuke Konishi } else if (S_ISDIR(inode->i_mode)) { 48905fe58fdSRyusuke Konishi inode->i_op = &nilfs_dir_inode_operations; 49005fe58fdSRyusuke Konishi inode->i_fop = &nilfs_dir_operations; 49105fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 49205fe58fdSRyusuke Konishi } else if (S_ISLNK(inode->i_mode)) { 49305fe58fdSRyusuke Konishi inode->i_op = &nilfs_symlink_inode_operations; 49421fc61c7SAl Viro inode_nohighmem(inode); 49505fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 49605fe58fdSRyusuke Konishi } else { 49705fe58fdSRyusuke Konishi inode->i_op = &nilfs_special_inode_operations; 49805fe58fdSRyusuke Konishi init_special_inode( 49905fe58fdSRyusuke Konishi inode, inode->i_mode, 500cdce214eSRyusuke Konishi huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 50105fe58fdSRyusuke Konishi } 502e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 50305fe58fdSRyusuke Konishi brelse(bh); 504365e215cSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 50505fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 5060ce187c4SRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 507c62d2555SMichal Hocko mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS)); 50805fe58fdSRyusuke Konishi return 0; 50905fe58fdSRyusuke Konishi 51005fe58fdSRyusuke Konishi failed_unmap: 511e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 51205fe58fdSRyusuke Konishi brelse(bh); 51305fe58fdSRyusuke Konishi 51405fe58fdSRyusuke Konishi bad_inode: 515365e215cSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 51605fe58fdSRyusuke Konishi return err; 51705fe58fdSRyusuke Konishi } 51805fe58fdSRyusuke Konishi 5190e14a359SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque) 5200e14a359SRyusuke Konishi { 5210e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 5220e14a359SRyusuke Konishi struct nilfs_inode_info *ii; 5230e14a359SRyusuke Konishi 5244d8d9293SRyusuke Konishi if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) 5250e14a359SRyusuke Konishi return 0; 5260e14a359SRyusuke Konishi 5270e14a359SRyusuke Konishi ii = NILFS_I(inode); 5280e14a359SRyusuke Konishi if (!test_bit(NILFS_I_GCINODE, &ii->i_state)) 5290e14a359SRyusuke Konishi return !args->for_gc; 5300e14a359SRyusuke Konishi 5310e14a359SRyusuke Konishi return args->for_gc && args->cno == ii->i_cno; 5320e14a359SRyusuke Konishi } 5330e14a359SRyusuke Konishi 5340e14a359SRyusuke Konishi static int nilfs_iget_set(struct inode *inode, void *opaque) 5350e14a359SRyusuke Konishi { 5360e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 5370e14a359SRyusuke Konishi 5380e14a359SRyusuke Konishi inode->i_ino = args->ino; 5390e14a359SRyusuke Konishi if (args->for_gc) { 5404ce5c342SRyusuke Konishi NILFS_I(inode)->i_state = BIT(NILFS_I_GCINODE); 5410e14a359SRyusuke Konishi NILFS_I(inode)->i_cno = args->cno; 5424d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = NULL; 5434d8d9293SRyusuke Konishi } else { 5444d8d9293SRyusuke Konishi if (args->root && args->ino == NILFS_ROOT_INO) 5454d8d9293SRyusuke Konishi nilfs_get_root(args->root); 5464d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = args->root; 5470e14a359SRyusuke Konishi } 5480e14a359SRyusuke Konishi return 0; 5490e14a359SRyusuke Konishi } 5500e14a359SRyusuke Konishi 551032dbb3bSRyusuke Konishi struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, 552032dbb3bSRyusuke Konishi unsigned long ino) 553032dbb3bSRyusuke Konishi { 554032dbb3bSRyusuke Konishi struct nilfs_iget_args args = { 555032dbb3bSRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 556032dbb3bSRyusuke Konishi }; 557032dbb3bSRyusuke Konishi 558032dbb3bSRyusuke Konishi return ilookup5(sb, ino, nilfs_iget_test, &args); 559032dbb3bSRyusuke Konishi } 560032dbb3bSRyusuke Konishi 561f1e89c86SRyusuke Konishi struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, 5624d8d9293SRyusuke Konishi unsigned long ino) 56305fe58fdSRyusuke Konishi { 5644d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 5654d8d9293SRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 5664d8d9293SRyusuke Konishi }; 567f1e89c86SRyusuke Konishi 568f1e89c86SRyusuke Konishi return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 569f1e89c86SRyusuke Konishi } 570f1e89c86SRyusuke Konishi 571f1e89c86SRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, 572f1e89c86SRyusuke Konishi unsigned long ino) 573f1e89c86SRyusuke Konishi { 57405fe58fdSRyusuke Konishi struct inode *inode; 57505fe58fdSRyusuke Konishi int err; 57605fe58fdSRyusuke Konishi 577f1e89c86SRyusuke Konishi inode = nilfs_iget_locked(sb, root, ino); 57805fe58fdSRyusuke Konishi if (unlikely(!inode)) 57905fe58fdSRyusuke Konishi return ERR_PTR(-ENOMEM); 58005fe58fdSRyusuke Konishi if (!(inode->i_state & I_NEW)) 58105fe58fdSRyusuke Konishi return inode; 58205fe58fdSRyusuke Konishi 583e912a5b6SRyusuke Konishi err = __nilfs_read_inode(sb, root, ino, inode); 58405fe58fdSRyusuke Konishi if (unlikely(err)) { 58505fe58fdSRyusuke Konishi iget_failed(inode); 58605fe58fdSRyusuke Konishi return ERR_PTR(err); 58705fe58fdSRyusuke Konishi } 58805fe58fdSRyusuke Konishi unlock_new_inode(inode); 58905fe58fdSRyusuke Konishi return inode; 59005fe58fdSRyusuke Konishi } 59105fe58fdSRyusuke Konishi 592263d90ceSRyusuke Konishi struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino, 593263d90ceSRyusuke Konishi __u64 cno) 594263d90ceSRyusuke Konishi { 5954d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 5964d8d9293SRyusuke Konishi .ino = ino, .root = NULL, .cno = cno, .for_gc = 1 5974d8d9293SRyusuke Konishi }; 598263d90ceSRyusuke Konishi struct inode *inode; 599263d90ceSRyusuke Konishi int err; 600263d90ceSRyusuke Konishi 601263d90ceSRyusuke Konishi inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 602263d90ceSRyusuke Konishi if (unlikely(!inode)) 603263d90ceSRyusuke Konishi return ERR_PTR(-ENOMEM); 604263d90ceSRyusuke Konishi if (!(inode->i_state & I_NEW)) 605263d90ceSRyusuke Konishi return inode; 606263d90ceSRyusuke Konishi 607263d90ceSRyusuke Konishi err = nilfs_init_gcinode(inode); 608263d90ceSRyusuke Konishi if (unlikely(err)) { 609263d90ceSRyusuke Konishi iget_failed(inode); 610263d90ceSRyusuke Konishi return ERR_PTR(err); 611263d90ceSRyusuke Konishi } 612263d90ceSRyusuke Konishi unlock_new_inode(inode); 613263d90ceSRyusuke Konishi return inode; 614263d90ceSRyusuke Konishi } 615263d90ceSRyusuke Konishi 61605fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode, 61705fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode, int has_bmap) 61805fe58fdSRyusuke Konishi { 61905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 62005fe58fdSRyusuke Konishi 62105fe58fdSRyusuke Konishi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 622305d3d0dSEric W. Biederman raw_inode->i_uid = cpu_to_le32(i_uid_read(inode)); 623305d3d0dSEric W. Biederman raw_inode->i_gid = cpu_to_le32(i_gid_read(inode)); 62405fe58fdSRyusuke Konishi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 62505fe58fdSRyusuke Konishi raw_inode->i_size = cpu_to_le64(inode->i_size); 62605fe58fdSRyusuke Konishi raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 62705fe58fdSRyusuke Konishi raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); 62861239230SRyusuke Konishi raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 62961239230SRyusuke Konishi raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); 63005fe58fdSRyusuke Konishi raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 63105fe58fdSRyusuke Konishi 63205fe58fdSRyusuke Konishi raw_inode->i_flags = cpu_to_le32(ii->i_flags); 63305fe58fdSRyusuke Konishi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 63405fe58fdSRyusuke Konishi 63556eb5538SRyusuke Konishi if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) { 63656eb5538SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 63756eb5538SRyusuke Konishi 63856eb5538SRyusuke Konishi /* zero-fill unused portion in the case of super root block */ 63956eb5538SRyusuke Konishi raw_inode->i_xattr = 0; 64056eb5538SRyusuke Konishi raw_inode->i_pad = 0; 64156eb5538SRyusuke Konishi memset((void *)raw_inode + sizeof(*raw_inode), 0, 64256eb5538SRyusuke Konishi nilfs->ns_inode_size - sizeof(*raw_inode)); 64356eb5538SRyusuke Konishi } 64456eb5538SRyusuke Konishi 64505fe58fdSRyusuke Konishi if (has_bmap) 64605fe58fdSRyusuke Konishi nilfs_bmap_write(ii->i_bmap, raw_inode); 64705fe58fdSRyusuke Konishi else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 64805fe58fdSRyusuke Konishi raw_inode->i_device_code = 649cdce214eSRyusuke Konishi cpu_to_le64(huge_encode_dev(inode->i_rdev)); 650076a378bSRyusuke Konishi /* 651076a378bSRyusuke Konishi * When extending inode, nilfs->ns_inode_size should be checked 652076a378bSRyusuke Konishi * for substitutions of appended fields. 653076a378bSRyusuke Konishi */ 65405fe58fdSRyusuke Konishi } 65505fe58fdSRyusuke Konishi 656b9f66140SAndreas Rohner void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags) 65705fe58fdSRyusuke Konishi { 65805fe58fdSRyusuke Konishi ino_t ino = inode->i_ino; 65905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 660e912a5b6SRyusuke Konishi struct inode *ifile = ii->i_root->ifile; 66105fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 66205fe58fdSRyusuke Konishi 663e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); 66405fe58fdSRyusuke Konishi 66505fe58fdSRyusuke Konishi if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 666e912a5b6SRyusuke Konishi memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); 667b9f66140SAndreas Rohner if (flags & I_DIRTY_DATASYNC) 668b9f66140SAndreas Rohner set_bit(NILFS_I_INODE_SYNC, &ii->i_state); 66905fe58fdSRyusuke Konishi 67005fe58fdSRyusuke Konishi nilfs_write_inode_common(inode, raw_inode, 0); 671076a378bSRyusuke Konishi /* 672076a378bSRyusuke Konishi * XXX: call with has_bmap = 0 is a workaround to avoid 673076a378bSRyusuke Konishi * deadlock of bmap. This delays update of i_bmap to just 674076a378bSRyusuke Konishi * before writing. 675076a378bSRyusuke Konishi */ 676076a378bSRyusuke Konishi 677e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(ifile, ino, ibh); 67805fe58fdSRyusuke Konishi } 67905fe58fdSRyusuke Konishi 68005fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 68105fe58fdSRyusuke Konishi 68205fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 68305fe58fdSRyusuke Konishi unsigned long from) 68405fe58fdSRyusuke Konishi { 6853568a13fSRyusuke Konishi __u64 b; 68605fe58fdSRyusuke Konishi int ret; 68705fe58fdSRyusuke Konishi 68805fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 68905fe58fdSRyusuke Konishi return; 69005fe58fdSRyusuke Konishi repeat: 69105fe58fdSRyusuke Konishi ret = nilfs_bmap_last_key(ii->i_bmap, &b); 69205fe58fdSRyusuke Konishi if (ret == -ENOENT) 69305fe58fdSRyusuke Konishi return; 69405fe58fdSRyusuke Konishi else if (ret < 0) 69505fe58fdSRyusuke Konishi goto failed; 69605fe58fdSRyusuke Konishi 69705fe58fdSRyusuke Konishi if (b < from) 69805fe58fdSRyusuke Konishi return; 69905fe58fdSRyusuke Konishi 7003568a13fSRyusuke Konishi b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 70105fe58fdSRyusuke Konishi ret = nilfs_bmap_truncate(ii->i_bmap, b); 70205fe58fdSRyusuke Konishi nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 70305fe58fdSRyusuke Konishi if (!ret || (ret == -ENOMEM && 70405fe58fdSRyusuke Konishi nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 70505fe58fdSRyusuke Konishi goto repeat; 70605fe58fdSRyusuke Konishi 70705fe58fdSRyusuke Konishi failed: 708d6517debSRyusuke Konishi nilfs_msg(ii->vfs_inode.i_sb, KERN_WARNING, 709d6517debSRyusuke Konishi "error %d truncating bmap (ino=%lu)", ret, 710d6517debSRyusuke Konishi ii->vfs_inode.i_ino); 71105fe58fdSRyusuke Konishi } 71205fe58fdSRyusuke Konishi 71305fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode) 71405fe58fdSRyusuke Konishi { 71505fe58fdSRyusuke Konishi unsigned long blkoff; 71605fe58fdSRyusuke Konishi unsigned int blocksize; 71705fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 71805fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 71905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 72005fe58fdSRyusuke Konishi 72105fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 72205fe58fdSRyusuke Konishi return; 72305fe58fdSRyusuke Konishi if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 72405fe58fdSRyusuke Konishi return; 72505fe58fdSRyusuke Konishi 72605fe58fdSRyusuke Konishi blocksize = sb->s_blocksize; 72705fe58fdSRyusuke Konishi blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 7281f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 72905fe58fdSRyusuke Konishi 73005fe58fdSRyusuke Konishi block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 73105fe58fdSRyusuke Konishi 73205fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, blkoff); 73305fe58fdSRyusuke Konishi 734078cd827SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 73505fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 73605fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 73705fe58fdSRyusuke Konishi 738abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 739bcbc8c64SRyusuke Konishi nilfs_set_file_dirty(inode, 0); 74047420c79SRyusuke Konishi nilfs_transaction_commit(sb); 741076a378bSRyusuke Konishi /* 742076a378bSRyusuke Konishi * May construct a logical segment and may fail in sync mode. 743076a378bSRyusuke Konishi * But truncate has no return value. 744076a378bSRyusuke Konishi */ 74505fe58fdSRyusuke Konishi } 74605fe58fdSRyusuke Konishi 7476fd1e5c9SAl Viro static void nilfs_clear_inode(struct inode *inode) 7486fd1e5c9SAl Viro { 7496fd1e5c9SAl Viro struct nilfs_inode_info *ii = NILFS_I(inode); 7506fd1e5c9SAl Viro 7516fd1e5c9SAl Viro /* 7526fd1e5c9SAl Viro * Free resources allocated in nilfs_read_inode(), here. 7536fd1e5c9SAl Viro */ 7546fd1e5c9SAl Viro BUG_ON(!list_empty(&ii->i_dirty)); 7556fd1e5c9SAl Viro brelse(ii->i_bh); 7566fd1e5c9SAl Viro ii->i_bh = NULL; 7576fd1e5c9SAl Viro 7582d19961dSRyusuke Konishi if (nilfs_is_metadata_file_inode(inode)) 7592d19961dSRyusuke Konishi nilfs_mdt_clear(inode); 760518d1a6aSRyusuke Konishi 7616fd1e5c9SAl Viro if (test_bit(NILFS_I_BMAP, &ii->i_state)) 7626fd1e5c9SAl Viro nilfs_bmap_clear(ii->i_bmap); 7636fd1e5c9SAl Viro 7646fd1e5c9SAl Viro nilfs_btnode_cache_clear(&ii->i_btnode_cache); 7654d8d9293SRyusuke Konishi 7664d8d9293SRyusuke Konishi if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) 7674d8d9293SRyusuke Konishi nilfs_put_root(ii->i_root); 7686fd1e5c9SAl Viro } 7696fd1e5c9SAl Viro 7706fd1e5c9SAl Viro void nilfs_evict_inode(struct inode *inode) 77105fe58fdSRyusuke Konishi { 77205fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 77305fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 77405fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 77525b18d39SRyusuke Konishi int ret; 77605fe58fdSRyusuke Konishi 7774d8d9293SRyusuke Konishi if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { 77891b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 779dbd5768fSJan Kara clear_inode(inode); 7806fd1e5c9SAl Viro nilfs_clear_inode(inode); 78105fe58fdSRyusuke Konishi return; 78205fe58fdSRyusuke Konishi } 7831f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 7841f5abe7eSRyusuke Konishi 78591b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 78605fe58fdSRyusuke Konishi 787e912a5b6SRyusuke Konishi /* TODO: some of the following operations may fail. */ 78805fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, 0); 789abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 790dbd5768fSJan Kara clear_inode(inode); 791e912a5b6SRyusuke Konishi 79225b18d39SRyusuke Konishi ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); 79325b18d39SRyusuke Konishi if (!ret) 794e5f7f848SVyacheslav Dubeyko atomic64_dec(&ii->i_root->inodes_count); 795e912a5b6SRyusuke Konishi 7966fd1e5c9SAl Viro nilfs_clear_inode(inode); 797e912a5b6SRyusuke Konishi 79805fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 79905fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 80047420c79SRyusuke Konishi nilfs_transaction_commit(sb); 801076a378bSRyusuke Konishi /* 802076a378bSRyusuke Konishi * May construct a logical segment and may fail in sync mode. 803076a378bSRyusuke Konishi * But delete_inode has no return value. 804076a378bSRyusuke Konishi */ 80505fe58fdSRyusuke Konishi } 80605fe58fdSRyusuke Konishi 80705fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) 80805fe58fdSRyusuke Konishi { 80905fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 8102b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 81105fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 81247420c79SRyusuke Konishi int err; 81305fe58fdSRyusuke Konishi 81431051c85SJan Kara err = setattr_prepare(dentry, iattr); 81505fe58fdSRyusuke Konishi if (err) 81605fe58fdSRyusuke Konishi return err; 81705fe58fdSRyusuke Konishi 81805fe58fdSRyusuke Konishi err = nilfs_transaction_begin(sb, &ti, 0); 81905fe58fdSRyusuke Konishi if (unlikely(err)) 82005fe58fdSRyusuke Konishi return err; 82147420c79SRyusuke Konishi 8221025774cSChristoph Hellwig if ((iattr->ia_valid & ATTR_SIZE) && 8231025774cSChristoph Hellwig iattr->ia_size != i_size_read(inode)) { 824562c72aaSChristoph Hellwig inode_dio_wait(inode); 8252d1b399bSMarco Stornelli truncate_setsize(inode, iattr->ia_size); 8262d1b399bSMarco Stornelli nilfs_truncate(inode); 8271025774cSChristoph Hellwig } 8281025774cSChristoph Hellwig 8291025774cSChristoph Hellwig setattr_copy(inode, iattr); 8301025774cSChristoph Hellwig mark_inode_dirty(inode); 8311025774cSChristoph Hellwig 8321025774cSChristoph Hellwig if (iattr->ia_valid & ATTR_MODE) { 8331025774cSChristoph Hellwig err = nilfs_acl_chmod(inode); 8341025774cSChristoph Hellwig if (unlikely(err)) 8351025774cSChristoph Hellwig goto out_err; 8361025774cSChristoph Hellwig } 8371025774cSChristoph Hellwig 8381025774cSChristoph Hellwig return nilfs_transaction_commit(sb); 8391025774cSChristoph Hellwig 8401025774cSChristoph Hellwig out_err: 8411025774cSChristoph Hellwig nilfs_transaction_abort(sb); 84247420c79SRyusuke Konishi return err; 84305fe58fdSRyusuke Konishi } 84405fe58fdSRyusuke Konishi 84510556cb2SAl Viro int nilfs_permission(struct inode *inode, int mask) 846dc3d3b81SRyusuke Konishi { 847730e908fSAl Viro struct nilfs_root *root = NILFS_I(inode)->i_root; 8484ad364caSRyusuke Konishi 849dc3d3b81SRyusuke Konishi if ((mask & MAY_WRITE) && root && 850dc3d3b81SRyusuke Konishi root->cno != NILFS_CPTREE_CURRENT_CNO) 851dc3d3b81SRyusuke Konishi return -EROFS; /* snapshot is not writable */ 852dc3d3b81SRyusuke Konishi 8532830ba7fSAl Viro return generic_permission(inode, mask); 854dc3d3b81SRyusuke Konishi } 855dc3d3b81SRyusuke Konishi 856bcbc8c64SRyusuke Konishi int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh) 85705fe58fdSRyusuke Konishi { 858e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 85905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 86005fe58fdSRyusuke Konishi int err; 86105fe58fdSRyusuke Konishi 862693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 86305fe58fdSRyusuke Konishi if (ii->i_bh == NULL) { 864693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 865e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(ii->i_root->ifile, 866e912a5b6SRyusuke Konishi inode->i_ino, pbh); 86705fe58fdSRyusuke Konishi if (unlikely(err)) 86805fe58fdSRyusuke Konishi return err; 869693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 87005fe58fdSRyusuke Konishi if (ii->i_bh == NULL) 87105fe58fdSRyusuke Konishi ii->i_bh = *pbh; 87205fe58fdSRyusuke Konishi else { 87305fe58fdSRyusuke Konishi brelse(*pbh); 87405fe58fdSRyusuke Konishi *pbh = ii->i_bh; 87505fe58fdSRyusuke Konishi } 87605fe58fdSRyusuke Konishi } else 87705fe58fdSRyusuke Konishi *pbh = ii->i_bh; 87805fe58fdSRyusuke Konishi 87905fe58fdSRyusuke Konishi get_bh(*pbh); 880693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 88105fe58fdSRyusuke Konishi return 0; 88205fe58fdSRyusuke Konishi } 88305fe58fdSRyusuke Konishi 88405fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode) 88505fe58fdSRyusuke Konishi { 88605fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 887e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 88805fe58fdSRyusuke Konishi int ret = 0; 88905fe58fdSRyusuke Konishi 89005fe58fdSRyusuke Konishi if (!list_empty(&ii->i_dirty)) { 891693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 89205fe58fdSRyusuke Konishi ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 89305fe58fdSRyusuke Konishi test_bit(NILFS_I_BUSY, &ii->i_state); 894693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 89505fe58fdSRyusuke Konishi } 89605fe58fdSRyusuke Konishi return ret; 89705fe58fdSRyusuke Konishi } 89805fe58fdSRyusuke Konishi 8990c6c44cbSRyusuke Konishi int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty) 90005fe58fdSRyusuke Konishi { 90105fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 902e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 90305fe58fdSRyusuke Konishi 904693dd321SRyusuke Konishi atomic_add(nr_dirty, &nilfs->ns_ndirtyblks); 90505fe58fdSRyusuke Konishi 906458c5b08SRyusuke Konishi if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 90705fe58fdSRyusuke Konishi return 0; 90805fe58fdSRyusuke Konishi 909693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 91005fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 91105fe58fdSRyusuke Konishi !test_bit(NILFS_I_BUSY, &ii->i_state)) { 912076a378bSRyusuke Konishi /* 913076a378bSRyusuke Konishi * Because this routine may race with nilfs_dispose_list(), 914076a378bSRyusuke Konishi * we have to check NILFS_I_QUEUED here, too. 915076a378bSRyusuke Konishi */ 91605fe58fdSRyusuke Konishi if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 917076a378bSRyusuke Konishi /* 918076a378bSRyusuke Konishi * This will happen when somebody is freeing 919076a378bSRyusuke Konishi * this inode. 920076a378bSRyusuke Konishi */ 921d6517debSRyusuke Konishi nilfs_msg(inode->i_sb, KERN_WARNING, 922d6517debSRyusuke Konishi "cannot set file dirty (ino=%lu): the file is being freed", 92305fe58fdSRyusuke Konishi inode->i_ino); 924693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 925076a378bSRyusuke Konishi return -EINVAL; /* 926076a378bSRyusuke Konishi * NILFS_I_DIRTY may remain for 927076a378bSRyusuke Konishi * freeing inode. 928076a378bSRyusuke Konishi */ 92905fe58fdSRyusuke Konishi } 930eaae0f37SNicolas Kaiser list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files); 93105fe58fdSRyusuke Konishi set_bit(NILFS_I_QUEUED, &ii->i_state); 93205fe58fdSRyusuke Konishi } 933693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 93405fe58fdSRyusuke Konishi return 0; 93505fe58fdSRyusuke Konishi } 93605fe58fdSRyusuke Konishi 937b9f66140SAndreas Rohner int __nilfs_mark_inode_dirty(struct inode *inode, int flags) 93805fe58fdSRyusuke Konishi { 93905fe58fdSRyusuke Konishi struct buffer_head *ibh; 94005fe58fdSRyusuke Konishi int err; 94105fe58fdSRyusuke Konishi 942bcbc8c64SRyusuke Konishi err = nilfs_load_inode_block(inode, &ibh); 94305fe58fdSRyusuke Konishi if (unlikely(err)) { 944d6517debSRyusuke Konishi nilfs_msg(inode->i_sb, KERN_WARNING, 945d6517debSRyusuke Konishi "cannot mark inode dirty (ino=%lu): error %d loading inode block", 946d6517debSRyusuke Konishi inode->i_ino, err); 94705fe58fdSRyusuke Konishi return err; 94805fe58fdSRyusuke Konishi } 949b9f66140SAndreas Rohner nilfs_update_inode(inode, ibh, flags); 9505fc7b141SRyusuke Konishi mark_buffer_dirty(ibh); 951e912a5b6SRyusuke Konishi nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); 95205fe58fdSRyusuke Konishi brelse(ibh); 95305fe58fdSRyusuke Konishi return 0; 95405fe58fdSRyusuke Konishi } 95505fe58fdSRyusuke Konishi 95605fe58fdSRyusuke Konishi /** 95705fe58fdSRyusuke Konishi * nilfs_dirty_inode - reflect changes on given inode to an inode block. 95805fe58fdSRyusuke Konishi * @inode: inode of the file to be registered. 95905fe58fdSRyusuke Konishi * 96005fe58fdSRyusuke Konishi * nilfs_dirty_inode() loads a inode block containing the specified 96105fe58fdSRyusuke Konishi * @inode and copies data from a nilfs_inode to a corresponding inode 96205fe58fdSRyusuke Konishi * entry in the inode block. This operation is excluded from the segment 96305fe58fdSRyusuke Konishi * construction. This function can be called both as a single operation 96405fe58fdSRyusuke Konishi * and as a part of indivisible file operations. 96505fe58fdSRyusuke Konishi */ 966aa385729SChristoph Hellwig void nilfs_dirty_inode(struct inode *inode, int flags) 96705fe58fdSRyusuke Konishi { 96805fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 9697d6cd92fSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 97005fe58fdSRyusuke Konishi 97105fe58fdSRyusuke Konishi if (is_bad_inode(inode)) { 972d6517debSRyusuke Konishi nilfs_msg(inode->i_sb, KERN_WARNING, 97306f4abf6SRyusuke Konishi "tried to mark bad_inode dirty. ignored."); 97405fe58fdSRyusuke Konishi dump_stack(); 97505fe58fdSRyusuke Konishi return; 97605fe58fdSRyusuke Konishi } 9777d6cd92fSRyusuke Konishi if (mdi) { 9787d6cd92fSRyusuke Konishi nilfs_mdt_mark_dirty(inode); 9797d6cd92fSRyusuke Konishi return; 9807d6cd92fSRyusuke Konishi } 98105fe58fdSRyusuke Konishi nilfs_transaction_begin(inode->i_sb, &ti, 0); 982b9f66140SAndreas Rohner __nilfs_mark_inode_dirty(inode, flags); 98347420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 98405fe58fdSRyusuke Konishi } 985622daaffSRyusuke Konishi 986622daaffSRyusuke Konishi int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 987622daaffSRyusuke Konishi __u64 start, __u64 len) 988622daaffSRyusuke Konishi { 9890ef28f9aSRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 990622daaffSRyusuke Konishi __u64 logical = 0, phys = 0, size = 0; 991622daaffSRyusuke Konishi __u32 flags = 0; 992622daaffSRyusuke Konishi loff_t isize; 993622daaffSRyusuke Konishi sector_t blkoff, end_blkoff; 994622daaffSRyusuke Konishi sector_t delalloc_blkoff; 995622daaffSRyusuke Konishi unsigned long delalloc_blklen; 996622daaffSRyusuke Konishi unsigned int blkbits = inode->i_blkbits; 997622daaffSRyusuke Konishi int ret, n; 998622daaffSRyusuke Konishi 999622daaffSRyusuke Konishi ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); 1000622daaffSRyusuke Konishi if (ret) 1001622daaffSRyusuke Konishi return ret; 1002622daaffSRyusuke Konishi 10035955102cSAl Viro inode_lock(inode); 1004622daaffSRyusuke Konishi 1005622daaffSRyusuke Konishi isize = i_size_read(inode); 1006622daaffSRyusuke Konishi 1007622daaffSRyusuke Konishi blkoff = start >> blkbits; 1008622daaffSRyusuke Konishi end_blkoff = (start + len - 1) >> blkbits; 1009622daaffSRyusuke Konishi 1010622daaffSRyusuke Konishi delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff, 1011622daaffSRyusuke Konishi &delalloc_blkoff); 1012622daaffSRyusuke Konishi 1013622daaffSRyusuke Konishi do { 1014622daaffSRyusuke Konishi __u64 blkphy; 1015622daaffSRyusuke Konishi unsigned int maxblocks; 1016622daaffSRyusuke Konishi 1017622daaffSRyusuke Konishi if (delalloc_blklen && blkoff == delalloc_blkoff) { 1018622daaffSRyusuke Konishi if (size) { 1019622daaffSRyusuke Konishi /* End of the current extent */ 1020622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1021622daaffSRyusuke Konishi fieinfo, logical, phys, size, flags); 1022622daaffSRyusuke Konishi if (ret) 1023622daaffSRyusuke Konishi break; 1024622daaffSRyusuke Konishi } 1025622daaffSRyusuke Konishi if (blkoff > end_blkoff) 1026622daaffSRyusuke Konishi break; 1027622daaffSRyusuke Konishi 1028622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC; 1029622daaffSRyusuke Konishi logical = blkoff << blkbits; 1030622daaffSRyusuke Konishi phys = 0; 1031622daaffSRyusuke Konishi size = delalloc_blklen << blkbits; 1032622daaffSRyusuke Konishi 1033622daaffSRyusuke Konishi blkoff = delalloc_blkoff + delalloc_blklen; 1034622daaffSRyusuke Konishi delalloc_blklen = nilfs_find_uncommitted_extent( 1035622daaffSRyusuke Konishi inode, blkoff, &delalloc_blkoff); 1036622daaffSRyusuke Konishi continue; 1037622daaffSRyusuke Konishi } 1038622daaffSRyusuke Konishi 1039622daaffSRyusuke Konishi /* 1040622daaffSRyusuke Konishi * Limit the number of blocks that we look up so as 1041622daaffSRyusuke Konishi * not to get into the next delayed allocation extent. 1042622daaffSRyusuke Konishi */ 1043622daaffSRyusuke Konishi maxblocks = INT_MAX; 1044622daaffSRyusuke Konishi if (delalloc_blklen) 1045622daaffSRyusuke Konishi maxblocks = min_t(sector_t, delalloc_blkoff - blkoff, 1046622daaffSRyusuke Konishi maxblocks); 1047622daaffSRyusuke Konishi blkphy = 0; 1048622daaffSRyusuke Konishi 1049622daaffSRyusuke Konishi down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1050622daaffSRyusuke Konishi n = nilfs_bmap_lookup_contig( 1051622daaffSRyusuke Konishi NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks); 1052622daaffSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1053622daaffSRyusuke Konishi 1054622daaffSRyusuke Konishi if (n < 0) { 1055622daaffSRyusuke Konishi int past_eof; 1056622daaffSRyusuke Konishi 1057622daaffSRyusuke Konishi if (unlikely(n != -ENOENT)) 1058622daaffSRyusuke Konishi break; /* error */ 1059622daaffSRyusuke Konishi 1060622daaffSRyusuke Konishi /* HOLE */ 1061622daaffSRyusuke Konishi blkoff++; 1062622daaffSRyusuke Konishi past_eof = ((blkoff << blkbits) >= isize); 1063622daaffSRyusuke Konishi 1064622daaffSRyusuke Konishi if (size) { 1065622daaffSRyusuke Konishi /* End of the current extent */ 1066622daaffSRyusuke Konishi 1067622daaffSRyusuke Konishi if (past_eof) 1068622daaffSRyusuke Konishi flags |= FIEMAP_EXTENT_LAST; 1069622daaffSRyusuke Konishi 1070622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1071622daaffSRyusuke Konishi fieinfo, logical, phys, size, flags); 1072622daaffSRyusuke Konishi if (ret) 1073622daaffSRyusuke Konishi break; 1074622daaffSRyusuke Konishi size = 0; 1075622daaffSRyusuke Konishi } 1076622daaffSRyusuke Konishi if (blkoff > end_blkoff || past_eof) 1077622daaffSRyusuke Konishi break; 1078622daaffSRyusuke Konishi } else { 1079622daaffSRyusuke Konishi if (size) { 1080622daaffSRyusuke Konishi if (phys && blkphy << blkbits == phys + size) { 1081622daaffSRyusuke Konishi /* The current extent goes on */ 1082622daaffSRyusuke Konishi size += n << blkbits; 1083622daaffSRyusuke Konishi } else { 1084622daaffSRyusuke Konishi /* Terminate the current extent */ 1085622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1086622daaffSRyusuke Konishi fieinfo, logical, phys, size, 1087622daaffSRyusuke Konishi flags); 1088622daaffSRyusuke Konishi if (ret || blkoff > end_blkoff) 1089622daaffSRyusuke Konishi break; 1090622daaffSRyusuke Konishi 1091622daaffSRyusuke Konishi /* Start another extent */ 1092622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED; 1093622daaffSRyusuke Konishi logical = blkoff << blkbits; 1094622daaffSRyusuke Konishi phys = blkphy << blkbits; 1095622daaffSRyusuke Konishi size = n << blkbits; 1096622daaffSRyusuke Konishi } 1097622daaffSRyusuke Konishi } else { 1098622daaffSRyusuke Konishi /* Start a new extent */ 1099622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED; 1100622daaffSRyusuke Konishi logical = blkoff << blkbits; 1101622daaffSRyusuke Konishi phys = blkphy << blkbits; 1102622daaffSRyusuke Konishi size = n << blkbits; 1103622daaffSRyusuke Konishi } 1104622daaffSRyusuke Konishi blkoff += n; 1105622daaffSRyusuke Konishi } 1106622daaffSRyusuke Konishi cond_resched(); 1107622daaffSRyusuke Konishi } while (true); 1108622daaffSRyusuke Konishi 1109622daaffSRyusuke Konishi /* If ret is 1 then we just hit the end of the extent array */ 1110622daaffSRyusuke Konishi if (ret == 1) 1111622daaffSRyusuke Konishi ret = 0; 1112622daaffSRyusuke Konishi 11135955102cSAl Viro inode_unlock(inode); 1114622daaffSRyusuke Konishi return ret; 1115622daaffSRyusuke Konishi } 1116