105fe58fdSRyusuke Konishi /* 205fe58fdSRyusuke Konishi * inode.c - NILFS inode operations. 305fe58fdSRyusuke Konishi * 405fe58fdSRyusuke Konishi * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. 505fe58fdSRyusuke Konishi * 605fe58fdSRyusuke Konishi * This program is free software; you can redistribute it and/or modify 705fe58fdSRyusuke Konishi * it under the terms of the GNU General Public License as published by 805fe58fdSRyusuke Konishi * the Free Software Foundation; either version 2 of the License, or 905fe58fdSRyusuke Konishi * (at your option) any later version. 1005fe58fdSRyusuke Konishi * 1105fe58fdSRyusuke Konishi * This program is distributed in the hope that it will be useful, 1205fe58fdSRyusuke Konishi * but WITHOUT ANY WARRANTY; without even the implied warranty of 1305fe58fdSRyusuke Konishi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1405fe58fdSRyusuke Konishi * GNU General Public License for more details. 1505fe58fdSRyusuke Konishi * 1605fe58fdSRyusuke Konishi * You should have received a copy of the GNU General Public License 1705fe58fdSRyusuke Konishi * along with this program; if not, write to the Free Software 1805fe58fdSRyusuke Konishi * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 1905fe58fdSRyusuke Konishi * 2005fe58fdSRyusuke Konishi * Written by Ryusuke Konishi <ryusuke@osrg.net> 2105fe58fdSRyusuke Konishi * 2205fe58fdSRyusuke Konishi */ 2305fe58fdSRyusuke Konishi 2405fe58fdSRyusuke Konishi #include <linux/buffer_head.h> 255a0e3ad6STejun Heo #include <linux/gfp.h> 2605fe58fdSRyusuke Konishi #include <linux/mpage.h> 2705fe58fdSRyusuke Konishi #include <linux/writeback.h> 28f30bf3e4SRyusuke Konishi #include <linux/uio.h> 2905fe58fdSRyusuke Konishi #include "nilfs.h" 306fd1e5c9SAl Viro #include "btnode.h" 3105fe58fdSRyusuke Konishi #include "segment.h" 3205fe58fdSRyusuke Konishi #include "page.h" 3305fe58fdSRyusuke Konishi #include "mdt.h" 3405fe58fdSRyusuke Konishi #include "cpfile.h" 3505fe58fdSRyusuke Konishi #include "ifile.h" 3605fe58fdSRyusuke Konishi 3705fe58fdSRyusuke Konishi 3805fe58fdSRyusuke Konishi /** 3905fe58fdSRyusuke Konishi * nilfs_get_block() - get a file block on the filesystem (callback function) 4005fe58fdSRyusuke Konishi * @inode - inode struct of the target file 4105fe58fdSRyusuke Konishi * @blkoff - file block number 4205fe58fdSRyusuke Konishi * @bh_result - buffer head to be mapped on 4305fe58fdSRyusuke Konishi * @create - indicate whether allocating the block or not when it has not 4405fe58fdSRyusuke Konishi * been allocated yet. 4505fe58fdSRyusuke Konishi * 4605fe58fdSRyusuke Konishi * This function does not issue actual read request of the specified data 4705fe58fdSRyusuke Konishi * block. It is done by VFS. 4805fe58fdSRyusuke Konishi */ 4905fe58fdSRyusuke Konishi int nilfs_get_block(struct inode *inode, sector_t blkoff, 5005fe58fdSRyusuke Konishi struct buffer_head *bh_result, int create) 5105fe58fdSRyusuke Konishi { 5205fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 53c3a7abf0SRyusuke Konishi __u64 blknum = 0; 5405fe58fdSRyusuke Konishi int err = 0, ret; 5505fe58fdSRyusuke Konishi struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode)); 56c3a7abf0SRyusuke Konishi unsigned maxblocks = bh_result->b_size >> inode->i_blkbits; 5705fe58fdSRyusuke Konishi 58c3a7abf0SRyusuke Konishi down_read(&NILFS_MDT(dat)->mi_sem); 59c3a7abf0SRyusuke Konishi ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); 60c3a7abf0SRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); 61c3a7abf0SRyusuke Konishi if (ret >= 0) { /* found */ 6205fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, blknum); 63c3a7abf0SRyusuke Konishi if (ret > 0) 64c3a7abf0SRyusuke Konishi bh_result->b_size = (ret << inode->i_blkbits); 6505fe58fdSRyusuke Konishi goto out; 6605fe58fdSRyusuke Konishi } 6705fe58fdSRyusuke Konishi /* data block was not found */ 6805fe58fdSRyusuke Konishi if (ret == -ENOENT && create) { 6905fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 7005fe58fdSRyusuke Konishi 7105fe58fdSRyusuke Konishi bh_result->b_blocknr = 0; 7205fe58fdSRyusuke Konishi err = nilfs_transaction_begin(inode->i_sb, &ti, 1); 7305fe58fdSRyusuke Konishi if (unlikely(err)) 7405fe58fdSRyusuke Konishi goto out; 7505fe58fdSRyusuke Konishi err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, 7605fe58fdSRyusuke Konishi (unsigned long)bh_result); 7705fe58fdSRyusuke Konishi if (unlikely(err != 0)) { 7805fe58fdSRyusuke Konishi if (err == -EEXIST) { 7905fe58fdSRyusuke Konishi /* 8005fe58fdSRyusuke Konishi * The get_block() function could be called 8105fe58fdSRyusuke Konishi * from multiple callers for an inode. 8205fe58fdSRyusuke Konishi * However, the page having this block must 8305fe58fdSRyusuke Konishi * be locked in this case. 8405fe58fdSRyusuke Konishi */ 851f5abe7eSRyusuke Konishi printk(KERN_WARNING 8605fe58fdSRyusuke Konishi "nilfs_get_block: a race condition " 8705fe58fdSRyusuke Konishi "while inserting a data block. " 8805fe58fdSRyusuke Konishi "(inode number=%lu, file block " 8905fe58fdSRyusuke Konishi "offset=%llu)\n", 9005fe58fdSRyusuke Konishi inode->i_ino, 9105fe58fdSRyusuke Konishi (unsigned long long)blkoff); 921f5abe7eSRyusuke Konishi err = 0; 9305fe58fdSRyusuke Konishi } else if (err == -EINVAL) { 9405fe58fdSRyusuke Konishi nilfs_error(inode->i_sb, __func__, 9505fe58fdSRyusuke Konishi "broken bmap (inode=%lu)\n", 9605fe58fdSRyusuke Konishi inode->i_ino); 9705fe58fdSRyusuke Konishi err = -EIO; 9805fe58fdSRyusuke Konishi } 9947420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 10005fe58fdSRyusuke Konishi goto out; 10105fe58fdSRyusuke Konishi } 102abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 10347420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 10405fe58fdSRyusuke Konishi /* Error handling should be detailed */ 10505fe58fdSRyusuke Konishi set_buffer_new(bh_result); 10605fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed 10705fe58fdSRyusuke Konishi to proper value */ 10805fe58fdSRyusuke Konishi } else if (ret == -ENOENT) { 10905fe58fdSRyusuke Konishi /* not found is not error (e.g. hole); must return without 11005fe58fdSRyusuke Konishi the mapped state flag. */ 11105fe58fdSRyusuke Konishi ; 11205fe58fdSRyusuke Konishi } else { 11305fe58fdSRyusuke Konishi err = ret; 11405fe58fdSRyusuke Konishi } 11505fe58fdSRyusuke Konishi 11605fe58fdSRyusuke Konishi out: 11705fe58fdSRyusuke Konishi return err; 11805fe58fdSRyusuke Konishi } 11905fe58fdSRyusuke Konishi 12005fe58fdSRyusuke Konishi /** 12105fe58fdSRyusuke Konishi * nilfs_readpage() - implement readpage() method of nilfs_aops {} 12205fe58fdSRyusuke Konishi * address_space_operations. 12305fe58fdSRyusuke Konishi * @file - file struct of the file to be read 12405fe58fdSRyusuke Konishi * @page - the page to be read 12505fe58fdSRyusuke Konishi */ 12605fe58fdSRyusuke Konishi static int nilfs_readpage(struct file *file, struct page *page) 12705fe58fdSRyusuke Konishi { 12805fe58fdSRyusuke Konishi return mpage_readpage(page, nilfs_get_block); 12905fe58fdSRyusuke Konishi } 13005fe58fdSRyusuke Konishi 13105fe58fdSRyusuke Konishi /** 13205fe58fdSRyusuke Konishi * nilfs_readpages() - implement readpages() method of nilfs_aops {} 13305fe58fdSRyusuke Konishi * address_space_operations. 13405fe58fdSRyusuke Konishi * @file - file struct of the file to be read 13505fe58fdSRyusuke Konishi * @mapping - address_space struct used for reading multiple pages 13605fe58fdSRyusuke Konishi * @pages - the pages to be read 13705fe58fdSRyusuke Konishi * @nr_pages - number of pages to be read 13805fe58fdSRyusuke Konishi */ 13905fe58fdSRyusuke Konishi static int nilfs_readpages(struct file *file, struct address_space *mapping, 14005fe58fdSRyusuke Konishi struct list_head *pages, unsigned nr_pages) 14105fe58fdSRyusuke Konishi { 14205fe58fdSRyusuke Konishi return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); 14305fe58fdSRyusuke Konishi } 14405fe58fdSRyusuke Konishi 14505fe58fdSRyusuke Konishi static int nilfs_writepages(struct address_space *mapping, 14605fe58fdSRyusuke Konishi struct writeback_control *wbc) 14705fe58fdSRyusuke Konishi { 148f30bf3e4SRyusuke Konishi struct inode *inode = mapping->host; 149f30bf3e4SRyusuke Konishi int err = 0; 150f30bf3e4SRyusuke Konishi 151f30bf3e4SRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) 152f30bf3e4SRyusuke Konishi err = nilfs_construct_dsync_segment(inode->i_sb, inode, 153f30bf3e4SRyusuke Konishi wbc->range_start, 154f30bf3e4SRyusuke Konishi wbc->range_end); 155f30bf3e4SRyusuke Konishi return err; 15605fe58fdSRyusuke Konishi } 15705fe58fdSRyusuke Konishi 15805fe58fdSRyusuke Konishi static int nilfs_writepage(struct page *page, struct writeback_control *wbc) 15905fe58fdSRyusuke Konishi { 16005fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 16105fe58fdSRyusuke Konishi int err; 16205fe58fdSRyusuke Konishi 16305fe58fdSRyusuke Konishi redirty_page_for_writepage(wbc, page); 16405fe58fdSRyusuke Konishi unlock_page(page); 16505fe58fdSRyusuke Konishi 16605fe58fdSRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) { 16705fe58fdSRyusuke Konishi err = nilfs_construct_segment(inode->i_sb); 16805fe58fdSRyusuke Konishi if (unlikely(err)) 16905fe58fdSRyusuke Konishi return err; 17005fe58fdSRyusuke Konishi } else if (wbc->for_reclaim) 17105fe58fdSRyusuke Konishi nilfs_flush_segment(inode->i_sb, inode->i_ino); 17205fe58fdSRyusuke Konishi 17305fe58fdSRyusuke Konishi return 0; 17405fe58fdSRyusuke Konishi } 17505fe58fdSRyusuke Konishi 17605fe58fdSRyusuke Konishi static int nilfs_set_page_dirty(struct page *page) 17705fe58fdSRyusuke Konishi { 17805fe58fdSRyusuke Konishi int ret = __set_page_dirty_buffers(page); 17905fe58fdSRyusuke Konishi 18005fe58fdSRyusuke Konishi if (ret) { 18105fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 18205fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 18305fe58fdSRyusuke Konishi unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); 18405fe58fdSRyusuke Konishi 18505fe58fdSRyusuke Konishi nilfs_set_file_dirty(sbi, inode, nr_dirty); 18605fe58fdSRyusuke Konishi } 18705fe58fdSRyusuke Konishi return ret; 18805fe58fdSRyusuke Konishi } 18905fe58fdSRyusuke Konishi 19005fe58fdSRyusuke Konishi static int nilfs_write_begin(struct file *file, struct address_space *mapping, 19105fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned flags, 19205fe58fdSRyusuke Konishi struct page **pagep, void **fsdata) 19305fe58fdSRyusuke Konishi 19405fe58fdSRyusuke Konishi { 19505fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 19605fe58fdSRyusuke Konishi int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); 19705fe58fdSRyusuke Konishi 19805fe58fdSRyusuke Konishi if (unlikely(err)) 19905fe58fdSRyusuke Konishi return err; 20005fe58fdSRyusuke Konishi 201155130a4SChristoph Hellwig err = block_write_begin(mapping, pos, len, flags, pagep, 202155130a4SChristoph Hellwig nilfs_get_block); 203155130a4SChristoph Hellwig if (unlikely(err)) { 204155130a4SChristoph Hellwig loff_t isize = mapping->host->i_size; 205155130a4SChristoph Hellwig if (pos + len > isize) 206155130a4SChristoph Hellwig vmtruncate(mapping->host, isize); 207155130a4SChristoph Hellwig 20847420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 209155130a4SChristoph Hellwig } 21005fe58fdSRyusuke Konishi return err; 21105fe58fdSRyusuke Konishi } 21205fe58fdSRyusuke Konishi 21305fe58fdSRyusuke Konishi static int nilfs_write_end(struct file *file, struct address_space *mapping, 21405fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned copied, 21505fe58fdSRyusuke Konishi struct page *page, void *fsdata) 21605fe58fdSRyusuke Konishi { 21705fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 21805fe58fdSRyusuke Konishi unsigned start = pos & (PAGE_CACHE_SIZE - 1); 21905fe58fdSRyusuke Konishi unsigned nr_dirty; 22005fe58fdSRyusuke Konishi int err; 22105fe58fdSRyusuke Konishi 22205fe58fdSRyusuke Konishi nr_dirty = nilfs_page_count_clean_buffers(page, start, 22305fe58fdSRyusuke Konishi start + copied); 22405fe58fdSRyusuke Konishi copied = generic_write_end(file, mapping, pos, len, copied, page, 22505fe58fdSRyusuke Konishi fsdata); 22605fe58fdSRyusuke Konishi nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty); 22747420c79SRyusuke Konishi err = nilfs_transaction_commit(inode->i_sb); 22805fe58fdSRyusuke Konishi return err ? : copied; 22905fe58fdSRyusuke Konishi } 23005fe58fdSRyusuke Konishi 23105fe58fdSRyusuke Konishi static ssize_t 23205fe58fdSRyusuke Konishi nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, 23305fe58fdSRyusuke Konishi loff_t offset, unsigned long nr_segs) 23405fe58fdSRyusuke Konishi { 23505fe58fdSRyusuke Konishi struct file *file = iocb->ki_filp; 23605fe58fdSRyusuke Konishi struct inode *inode = file->f_mapping->host; 23705fe58fdSRyusuke Konishi ssize_t size; 23805fe58fdSRyusuke Konishi 23905fe58fdSRyusuke Konishi if (rw == WRITE) 24005fe58fdSRyusuke Konishi return 0; 24105fe58fdSRyusuke Konishi 24205fe58fdSRyusuke Konishi /* Needs synchronization with the cleaner */ 24305fe58fdSRyusuke Konishi size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, 24405fe58fdSRyusuke Konishi offset, nr_segs, nilfs_get_block, NULL); 245eafdc7d1SChristoph Hellwig 246eafdc7d1SChristoph Hellwig /* 247eafdc7d1SChristoph Hellwig * In case of error extending write may have instantiated a few 248eafdc7d1SChristoph Hellwig * blocks outside i_size. Trim these off again. 249eafdc7d1SChristoph Hellwig */ 250eafdc7d1SChristoph Hellwig if (unlikely((rw & WRITE) && size < 0)) { 251eafdc7d1SChristoph Hellwig loff_t isize = i_size_read(inode); 252eafdc7d1SChristoph Hellwig loff_t end = offset + iov_length(iov, nr_segs); 253eafdc7d1SChristoph Hellwig 254eafdc7d1SChristoph Hellwig if (end > isize) 255eafdc7d1SChristoph Hellwig vmtruncate(inode, isize); 256eafdc7d1SChristoph Hellwig } 257eafdc7d1SChristoph Hellwig 25805fe58fdSRyusuke Konishi return size; 25905fe58fdSRyusuke Konishi } 26005fe58fdSRyusuke Konishi 2617f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = { 26205fe58fdSRyusuke Konishi .writepage = nilfs_writepage, 26305fe58fdSRyusuke Konishi .readpage = nilfs_readpage, 264e85dc1d5SRyusuke Konishi .sync_page = block_sync_page, 26505fe58fdSRyusuke Konishi .writepages = nilfs_writepages, 26605fe58fdSRyusuke Konishi .set_page_dirty = nilfs_set_page_dirty, 26705fe58fdSRyusuke Konishi .readpages = nilfs_readpages, 26805fe58fdSRyusuke Konishi .write_begin = nilfs_write_begin, 26905fe58fdSRyusuke Konishi .write_end = nilfs_write_end, 27005fe58fdSRyusuke Konishi /* .releasepage = nilfs_releasepage, */ 27105fe58fdSRyusuke Konishi .invalidatepage = block_invalidatepage, 27205fe58fdSRyusuke Konishi .direct_IO = nilfs_direct_IO, 273258ef67eSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 27405fe58fdSRyusuke Konishi }; 27505fe58fdSRyusuke Konishi 27605fe58fdSRyusuke Konishi struct inode *nilfs_new_inode(struct inode *dir, int mode) 27705fe58fdSRyusuke Konishi { 27805fe58fdSRyusuke Konishi struct super_block *sb = dir->i_sb; 27905fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 28005fe58fdSRyusuke Konishi struct inode *inode; 28105fe58fdSRyusuke Konishi struct nilfs_inode_info *ii; 28205fe58fdSRyusuke Konishi int err = -ENOMEM; 28305fe58fdSRyusuke Konishi ino_t ino; 28405fe58fdSRyusuke Konishi 28505fe58fdSRyusuke Konishi inode = new_inode(sb); 28605fe58fdSRyusuke Konishi if (unlikely(!inode)) 28705fe58fdSRyusuke Konishi goto failed; 28805fe58fdSRyusuke Konishi 28905fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 29005fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 29105fe58fdSRyusuke Konishi 29205fe58fdSRyusuke Konishi ii = NILFS_I(inode); 29305fe58fdSRyusuke Konishi ii->i_state = 1 << NILFS_I_NEW; 29405fe58fdSRyusuke Konishi 29505fe58fdSRyusuke Konishi err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh); 29605fe58fdSRyusuke Konishi if (unlikely(err)) 29705fe58fdSRyusuke Konishi goto failed_ifile_create_inode; 29805fe58fdSRyusuke Konishi /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 29905fe58fdSRyusuke Konishi 30005fe58fdSRyusuke Konishi atomic_inc(&sbi->s_inodes_count); 30173459dccSDmitry Monakhov inode_init_owner(inode, dir, mode); 30205fe58fdSRyusuke Konishi inode->i_ino = ino; 30305fe58fdSRyusuke Konishi inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 30405fe58fdSRyusuke Konishi 30505fe58fdSRyusuke Konishi if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 30605fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, NULL); 30705fe58fdSRyusuke Konishi if (err < 0) 30805fe58fdSRyusuke Konishi goto failed_bmap; 30905fe58fdSRyusuke Konishi 31005fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 31105fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 31205fe58fdSRyusuke Konishi } 31305fe58fdSRyusuke Konishi 31405fe58fdSRyusuke Konishi ii->i_flags = NILFS_I(dir)->i_flags; 31505fe58fdSRyusuke Konishi if (S_ISLNK(mode)) 31605fe58fdSRyusuke Konishi ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL); 31705fe58fdSRyusuke Konishi if (!S_ISDIR(mode)) 31805fe58fdSRyusuke Konishi ii->i_flags &= ~NILFS_DIRSYNC_FL; 31905fe58fdSRyusuke Konishi 32005fe58fdSRyusuke Konishi /* ii->i_file_acl = 0; */ 32105fe58fdSRyusuke Konishi /* ii->i_dir_acl = 0; */ 32205fe58fdSRyusuke Konishi ii->i_dir_start_lookup = 0; 32305fe58fdSRyusuke Konishi ii->i_cno = 0; 32405fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 32505fe58fdSRyusuke Konishi spin_lock(&sbi->s_next_gen_lock); 32605fe58fdSRyusuke Konishi inode->i_generation = sbi->s_next_generation++; 32705fe58fdSRyusuke Konishi spin_unlock(&sbi->s_next_gen_lock); 32805fe58fdSRyusuke Konishi insert_inode_hash(inode); 32905fe58fdSRyusuke Konishi 33005fe58fdSRyusuke Konishi err = nilfs_init_acl(inode, dir); 33105fe58fdSRyusuke Konishi if (unlikely(err)) 33205fe58fdSRyusuke Konishi goto failed_acl; /* never occur. When supporting 33305fe58fdSRyusuke Konishi nilfs_init_acl(), proper cancellation of 33405fe58fdSRyusuke Konishi above jobs should be considered */ 33505fe58fdSRyusuke Konishi 33605fe58fdSRyusuke Konishi return inode; 33705fe58fdSRyusuke Konishi 33805fe58fdSRyusuke Konishi failed_acl: 33905fe58fdSRyusuke Konishi failed_bmap: 34005fe58fdSRyusuke Konishi inode->i_nlink = 0; 34105fe58fdSRyusuke Konishi iput(inode); /* raw_inode will be deleted through 34205fe58fdSRyusuke Konishi generic_delete_inode() */ 34305fe58fdSRyusuke Konishi goto failed; 34405fe58fdSRyusuke Konishi 34505fe58fdSRyusuke Konishi failed_ifile_create_inode: 34605fe58fdSRyusuke Konishi make_bad_inode(inode); 34705fe58fdSRyusuke Konishi iput(inode); /* if i_nlink == 1, generic_forget_inode() will be 34805fe58fdSRyusuke Konishi called */ 34905fe58fdSRyusuke Konishi failed: 35005fe58fdSRyusuke Konishi return ERR_PTR(err); 35105fe58fdSRyusuke Konishi } 35205fe58fdSRyusuke Konishi 35305fe58fdSRyusuke Konishi void nilfs_free_inode(struct inode *inode) 35405fe58fdSRyusuke Konishi { 35505fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 35605fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 35705fe58fdSRyusuke Konishi 35805fe58fdSRyusuke Konishi /* XXX: check error code? Is there any thing I can do? */ 35905fe58fdSRyusuke Konishi (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino); 36005fe58fdSRyusuke Konishi atomic_dec(&sbi->s_inodes_count); 36105fe58fdSRyusuke Konishi } 36205fe58fdSRyusuke Konishi 36305fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode) 36405fe58fdSRyusuke Konishi { 36505fe58fdSRyusuke Konishi unsigned int flags = NILFS_I(inode)->i_flags; 36605fe58fdSRyusuke Konishi 36705fe58fdSRyusuke Konishi inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | 36805fe58fdSRyusuke Konishi S_DIRSYNC); 36905fe58fdSRyusuke Konishi if (flags & NILFS_SYNC_FL) 37005fe58fdSRyusuke Konishi inode->i_flags |= S_SYNC; 37105fe58fdSRyusuke Konishi if (flags & NILFS_APPEND_FL) 37205fe58fdSRyusuke Konishi inode->i_flags |= S_APPEND; 37305fe58fdSRyusuke Konishi if (flags & NILFS_IMMUTABLE_FL) 37405fe58fdSRyusuke Konishi inode->i_flags |= S_IMMUTABLE; 37505fe58fdSRyusuke Konishi #ifndef NILFS_ATIME_DISABLE 37605fe58fdSRyusuke Konishi if (flags & NILFS_NOATIME_FL) 37705fe58fdSRyusuke Konishi #endif 37805fe58fdSRyusuke Konishi inode->i_flags |= S_NOATIME; 37905fe58fdSRyusuke Konishi if (flags & NILFS_DIRSYNC_FL) 38005fe58fdSRyusuke Konishi inode->i_flags |= S_DIRSYNC; 38105fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 38205fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 38305fe58fdSRyusuke Konishi } 38405fe58fdSRyusuke Konishi 38505fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode, 38605fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode) 38705fe58fdSRyusuke Konishi { 38805fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 38905fe58fdSRyusuke Konishi int err; 39005fe58fdSRyusuke Konishi 39105fe58fdSRyusuke Konishi inode->i_mode = le16_to_cpu(raw_inode->i_mode); 39205fe58fdSRyusuke Konishi inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid); 39305fe58fdSRyusuke Konishi inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid); 39405fe58fdSRyusuke Konishi inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 39505fe58fdSRyusuke Konishi inode->i_size = le64_to_cpu(raw_inode->i_size); 39605fe58fdSRyusuke Konishi inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 39705fe58fdSRyusuke Konishi inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); 39805fe58fdSRyusuke Konishi inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 39961239230SRyusuke Konishi inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 40061239230SRyusuke Konishi inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); 40161239230SRyusuke Konishi inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 40261239230SRyusuke Konishi if (inode->i_nlink == 0 && inode->i_mode == 0) 40305fe58fdSRyusuke Konishi return -EINVAL; /* this inode is deleted */ 40405fe58fdSRyusuke Konishi 40505fe58fdSRyusuke Konishi inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 40605fe58fdSRyusuke Konishi ii->i_flags = le32_to_cpu(raw_inode->i_flags); 40705fe58fdSRyusuke Konishi #if 0 40805fe58fdSRyusuke Konishi ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 40905fe58fdSRyusuke Konishi ii->i_dir_acl = S_ISREG(inode->i_mode) ? 41005fe58fdSRyusuke Konishi 0 : le32_to_cpu(raw_inode->i_dir_acl); 41105fe58fdSRyusuke Konishi #endif 4123cc811bfSRyusuke Konishi ii->i_dir_start_lookup = 0; 41305fe58fdSRyusuke Konishi ii->i_cno = 0; 41405fe58fdSRyusuke Konishi inode->i_generation = le32_to_cpu(raw_inode->i_generation); 41505fe58fdSRyusuke Konishi 41605fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 41705fe58fdSRyusuke Konishi S_ISLNK(inode->i_mode)) { 41805fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, raw_inode); 41905fe58fdSRyusuke Konishi if (err < 0) 42005fe58fdSRyusuke Konishi return err; 42105fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 42205fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 42305fe58fdSRyusuke Konishi } 42405fe58fdSRyusuke Konishi return 0; 42505fe58fdSRyusuke Konishi } 42605fe58fdSRyusuke Konishi 42705fe58fdSRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb, unsigned long ino, 42805fe58fdSRyusuke Konishi struct inode *inode) 42905fe58fdSRyusuke Konishi { 43005fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 43105fe58fdSRyusuke Konishi struct inode *dat = nilfs_dat_inode(sbi->s_nilfs); 43205fe58fdSRyusuke Konishi struct buffer_head *bh; 43305fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 43405fe58fdSRyusuke Konishi int err; 43505fe58fdSRyusuke Konishi 43605fe58fdSRyusuke Konishi down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 43705fe58fdSRyusuke Konishi err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh); 43805fe58fdSRyusuke Konishi if (unlikely(err)) 43905fe58fdSRyusuke Konishi goto bad_inode; 44005fe58fdSRyusuke Konishi 44105fe58fdSRyusuke Konishi raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh); 44205fe58fdSRyusuke Konishi 4431b2f5a64SRyusuke Konishi err = nilfs_read_inode_common(inode, raw_inode); 4441b2f5a64SRyusuke Konishi if (err) 44505fe58fdSRyusuke Konishi goto failed_unmap; 44605fe58fdSRyusuke Konishi 44705fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode)) { 44805fe58fdSRyusuke Konishi inode->i_op = &nilfs_file_inode_operations; 44905fe58fdSRyusuke Konishi inode->i_fop = &nilfs_file_operations; 45005fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 45105fe58fdSRyusuke Konishi } else if (S_ISDIR(inode->i_mode)) { 45205fe58fdSRyusuke Konishi inode->i_op = &nilfs_dir_inode_operations; 45305fe58fdSRyusuke Konishi inode->i_fop = &nilfs_dir_operations; 45405fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 45505fe58fdSRyusuke Konishi } else if (S_ISLNK(inode->i_mode)) { 45605fe58fdSRyusuke Konishi inode->i_op = &nilfs_symlink_inode_operations; 45705fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 45805fe58fdSRyusuke Konishi } else { 45905fe58fdSRyusuke Konishi inode->i_op = &nilfs_special_inode_operations; 46005fe58fdSRyusuke Konishi init_special_inode( 46105fe58fdSRyusuke Konishi inode, inode->i_mode, 462cdce214eSRyusuke Konishi huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 46305fe58fdSRyusuke Konishi } 46405fe58fdSRyusuke Konishi nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh); 46505fe58fdSRyusuke Konishi brelse(bh); 46605fe58fdSRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 46705fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 46805fe58fdSRyusuke Konishi return 0; 46905fe58fdSRyusuke Konishi 47005fe58fdSRyusuke Konishi failed_unmap: 47105fe58fdSRyusuke Konishi nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh); 47205fe58fdSRyusuke Konishi brelse(bh); 47305fe58fdSRyusuke Konishi 47405fe58fdSRyusuke Konishi bad_inode: 47505fe58fdSRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 47605fe58fdSRyusuke Konishi return err; 47705fe58fdSRyusuke Konishi } 47805fe58fdSRyusuke Konishi 47905fe58fdSRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, unsigned long ino) 48005fe58fdSRyusuke Konishi { 48105fe58fdSRyusuke Konishi struct inode *inode; 48205fe58fdSRyusuke Konishi int err; 48305fe58fdSRyusuke Konishi 48405fe58fdSRyusuke Konishi inode = iget_locked(sb, ino); 48505fe58fdSRyusuke Konishi if (unlikely(!inode)) 48605fe58fdSRyusuke Konishi return ERR_PTR(-ENOMEM); 48705fe58fdSRyusuke Konishi if (!(inode->i_state & I_NEW)) 48805fe58fdSRyusuke Konishi return inode; 48905fe58fdSRyusuke Konishi 49005fe58fdSRyusuke Konishi err = __nilfs_read_inode(sb, ino, inode); 49105fe58fdSRyusuke Konishi if (unlikely(err)) { 49205fe58fdSRyusuke Konishi iget_failed(inode); 49305fe58fdSRyusuke Konishi return ERR_PTR(err); 49405fe58fdSRyusuke Konishi } 49505fe58fdSRyusuke Konishi unlock_new_inode(inode); 49605fe58fdSRyusuke Konishi return inode; 49705fe58fdSRyusuke Konishi } 49805fe58fdSRyusuke Konishi 49905fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode, 50005fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode, int has_bmap) 50105fe58fdSRyusuke Konishi { 50205fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 50305fe58fdSRyusuke Konishi 50405fe58fdSRyusuke Konishi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 50505fe58fdSRyusuke Konishi raw_inode->i_uid = cpu_to_le32(inode->i_uid); 50605fe58fdSRyusuke Konishi raw_inode->i_gid = cpu_to_le32(inode->i_gid); 50705fe58fdSRyusuke Konishi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 50805fe58fdSRyusuke Konishi raw_inode->i_size = cpu_to_le64(inode->i_size); 50905fe58fdSRyusuke Konishi raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 51005fe58fdSRyusuke Konishi raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); 51161239230SRyusuke Konishi raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 51261239230SRyusuke Konishi raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); 51305fe58fdSRyusuke Konishi raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 51405fe58fdSRyusuke Konishi 51505fe58fdSRyusuke Konishi raw_inode->i_flags = cpu_to_le32(ii->i_flags); 51605fe58fdSRyusuke Konishi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 51705fe58fdSRyusuke Konishi 51805fe58fdSRyusuke Konishi if (has_bmap) 51905fe58fdSRyusuke Konishi nilfs_bmap_write(ii->i_bmap, raw_inode); 52005fe58fdSRyusuke Konishi else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 52105fe58fdSRyusuke Konishi raw_inode->i_device_code = 522cdce214eSRyusuke Konishi cpu_to_le64(huge_encode_dev(inode->i_rdev)); 52305fe58fdSRyusuke Konishi /* When extending inode, nilfs->ns_inode_size should be checked 52405fe58fdSRyusuke Konishi for substitutions of appended fields */ 52505fe58fdSRyusuke Konishi } 52605fe58fdSRyusuke Konishi 52705fe58fdSRyusuke Konishi void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh) 52805fe58fdSRyusuke Konishi { 52905fe58fdSRyusuke Konishi ino_t ino = inode->i_ino; 53005fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 53105fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 53205fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 53305fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 53405fe58fdSRyusuke Konishi 53505fe58fdSRyusuke Konishi raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh); 53605fe58fdSRyusuke Konishi 53705fe58fdSRyusuke Konishi if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 53805fe58fdSRyusuke Konishi memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size); 53905fe58fdSRyusuke Konishi set_bit(NILFS_I_INODE_DIRTY, &ii->i_state); 54005fe58fdSRyusuke Konishi 54105fe58fdSRyusuke Konishi nilfs_write_inode_common(inode, raw_inode, 0); 54205fe58fdSRyusuke Konishi /* XXX: call with has_bmap = 0 is a workaround to avoid 54305fe58fdSRyusuke Konishi deadlock of bmap. This delays update of i_bmap to just 54405fe58fdSRyusuke Konishi before writing */ 54505fe58fdSRyusuke Konishi nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh); 54605fe58fdSRyusuke Konishi } 54705fe58fdSRyusuke Konishi 54805fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 54905fe58fdSRyusuke Konishi 55005fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 55105fe58fdSRyusuke Konishi unsigned long from) 55205fe58fdSRyusuke Konishi { 55305fe58fdSRyusuke Konishi unsigned long b; 55405fe58fdSRyusuke Konishi int ret; 55505fe58fdSRyusuke Konishi 55605fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 55705fe58fdSRyusuke Konishi return; 55805fe58fdSRyusuke Konishi repeat: 55905fe58fdSRyusuke Konishi ret = nilfs_bmap_last_key(ii->i_bmap, &b); 56005fe58fdSRyusuke Konishi if (ret == -ENOENT) 56105fe58fdSRyusuke Konishi return; 56205fe58fdSRyusuke Konishi else if (ret < 0) 56305fe58fdSRyusuke Konishi goto failed; 56405fe58fdSRyusuke Konishi 56505fe58fdSRyusuke Konishi if (b < from) 56605fe58fdSRyusuke Konishi return; 56705fe58fdSRyusuke Konishi 56805fe58fdSRyusuke Konishi b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 56905fe58fdSRyusuke Konishi ret = nilfs_bmap_truncate(ii->i_bmap, b); 57005fe58fdSRyusuke Konishi nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 57105fe58fdSRyusuke Konishi if (!ret || (ret == -ENOMEM && 57205fe58fdSRyusuke Konishi nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 57305fe58fdSRyusuke Konishi goto repeat; 57405fe58fdSRyusuke Konishi 57505fe58fdSRyusuke Konishi failed: 57605fe58fdSRyusuke Konishi if (ret == -EINVAL) 57705fe58fdSRyusuke Konishi nilfs_error(ii->vfs_inode.i_sb, __func__, 57805fe58fdSRyusuke Konishi "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino); 57905fe58fdSRyusuke Konishi else 58005fe58fdSRyusuke Konishi nilfs_warning(ii->vfs_inode.i_sb, __func__, 58105fe58fdSRyusuke Konishi "failed to truncate bmap (ino=%lu, err=%d)", 58205fe58fdSRyusuke Konishi ii->vfs_inode.i_ino, ret); 58305fe58fdSRyusuke Konishi } 58405fe58fdSRyusuke Konishi 58505fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode) 58605fe58fdSRyusuke Konishi { 58705fe58fdSRyusuke Konishi unsigned long blkoff; 58805fe58fdSRyusuke Konishi unsigned int blocksize; 58905fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 59005fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 59105fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 59205fe58fdSRyusuke Konishi 59305fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 59405fe58fdSRyusuke Konishi return; 59505fe58fdSRyusuke Konishi if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 59605fe58fdSRyusuke Konishi return; 59705fe58fdSRyusuke Konishi 59805fe58fdSRyusuke Konishi blocksize = sb->s_blocksize; 59905fe58fdSRyusuke Konishi blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 6001f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 60105fe58fdSRyusuke Konishi 60205fe58fdSRyusuke Konishi block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 60305fe58fdSRyusuke Konishi 60405fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, blkoff); 60505fe58fdSRyusuke Konishi 60605fe58fdSRyusuke Konishi inode->i_mtime = inode->i_ctime = CURRENT_TIME; 60705fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 60805fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 60905fe58fdSRyusuke Konishi 610abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 61105fe58fdSRyusuke Konishi nilfs_set_file_dirty(NILFS_SB(sb), inode, 0); 61247420c79SRyusuke Konishi nilfs_transaction_commit(sb); 61305fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 61405fe58fdSRyusuke Konishi But truncate has no return value. */ 61505fe58fdSRyusuke Konishi } 61605fe58fdSRyusuke Konishi 6176fd1e5c9SAl Viro static void nilfs_clear_inode(struct inode *inode) 6186fd1e5c9SAl Viro { 6196fd1e5c9SAl Viro struct nilfs_inode_info *ii = NILFS_I(inode); 6206fd1e5c9SAl Viro 6216fd1e5c9SAl Viro /* 6226fd1e5c9SAl Viro * Free resources allocated in nilfs_read_inode(), here. 6236fd1e5c9SAl Viro */ 6246fd1e5c9SAl Viro BUG_ON(!list_empty(&ii->i_dirty)); 6256fd1e5c9SAl Viro brelse(ii->i_bh); 6266fd1e5c9SAl Viro ii->i_bh = NULL; 6276fd1e5c9SAl Viro 6286fd1e5c9SAl Viro if (test_bit(NILFS_I_BMAP, &ii->i_state)) 6296fd1e5c9SAl Viro nilfs_bmap_clear(ii->i_bmap); 6306fd1e5c9SAl Viro 6316fd1e5c9SAl Viro nilfs_btnode_cache_clear(&ii->i_btnode_cache); 6326fd1e5c9SAl Viro } 6336fd1e5c9SAl Viro 6346fd1e5c9SAl Viro void nilfs_evict_inode(struct inode *inode) 63505fe58fdSRyusuke Konishi { 63605fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 63705fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 63805fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 63905fe58fdSRyusuke Konishi 6406fd1e5c9SAl Viro if (inode->i_nlink || unlikely(is_bad_inode(inode))) { 64105fe58fdSRyusuke Konishi if (inode->i_data.nrpages) 64205fe58fdSRyusuke Konishi truncate_inode_pages(&inode->i_data, 0); 6436fd1e5c9SAl Viro end_writeback(inode); 6446fd1e5c9SAl Viro nilfs_clear_inode(inode); 64505fe58fdSRyusuke Konishi return; 64605fe58fdSRyusuke Konishi } 6471f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 6481f5abe7eSRyusuke Konishi 64905fe58fdSRyusuke Konishi if (inode->i_data.nrpages) 65005fe58fdSRyusuke Konishi truncate_inode_pages(&inode->i_data, 0); 65105fe58fdSRyusuke Konishi 65205fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, 0); 653abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 6546fd1e5c9SAl Viro end_writeback(inode); 6556fd1e5c9SAl Viro nilfs_clear_inode(inode); 65605fe58fdSRyusuke Konishi nilfs_free_inode(inode); 65705fe58fdSRyusuke Konishi /* nilfs_free_inode() marks inode buffer dirty */ 65805fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 65905fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 66047420c79SRyusuke Konishi nilfs_transaction_commit(sb); 66105fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 66205fe58fdSRyusuke Konishi But delete_inode has no return value. */ 66305fe58fdSRyusuke Konishi } 66405fe58fdSRyusuke Konishi 66505fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) 66605fe58fdSRyusuke Konishi { 66705fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 66805fe58fdSRyusuke Konishi struct inode *inode = dentry->d_inode; 66905fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 67047420c79SRyusuke Konishi int err; 67105fe58fdSRyusuke Konishi 67205fe58fdSRyusuke Konishi err = inode_change_ok(inode, iattr); 67305fe58fdSRyusuke Konishi if (err) 67405fe58fdSRyusuke Konishi return err; 67505fe58fdSRyusuke Konishi 67605fe58fdSRyusuke Konishi err = nilfs_transaction_begin(sb, &ti, 0); 67705fe58fdSRyusuke Konishi if (unlikely(err)) 67805fe58fdSRyusuke Konishi return err; 67947420c79SRyusuke Konishi 6801025774cSChristoph Hellwig if ((iattr->ia_valid & ATTR_SIZE) && 6811025774cSChristoph Hellwig iattr->ia_size != i_size_read(inode)) { 6821025774cSChristoph Hellwig err = vmtruncate(inode, iattr->ia_size); 6831025774cSChristoph Hellwig if (unlikely(err)) 6841025774cSChristoph Hellwig goto out_err; 6851025774cSChristoph Hellwig } 6861025774cSChristoph Hellwig 6871025774cSChristoph Hellwig setattr_copy(inode, iattr); 6881025774cSChristoph Hellwig mark_inode_dirty(inode); 6891025774cSChristoph Hellwig 6901025774cSChristoph Hellwig if (iattr->ia_valid & ATTR_MODE) { 6911025774cSChristoph Hellwig err = nilfs_acl_chmod(inode); 6921025774cSChristoph Hellwig if (unlikely(err)) 6931025774cSChristoph Hellwig goto out_err; 6941025774cSChristoph Hellwig } 6951025774cSChristoph Hellwig 6961025774cSChristoph Hellwig return nilfs_transaction_commit(sb); 6971025774cSChristoph Hellwig 6981025774cSChristoph Hellwig out_err: 6991025774cSChristoph Hellwig nilfs_transaction_abort(sb); 70047420c79SRyusuke Konishi return err; 70105fe58fdSRyusuke Konishi } 70205fe58fdSRyusuke Konishi 70305fe58fdSRyusuke Konishi int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode, 70405fe58fdSRyusuke Konishi struct buffer_head **pbh) 70505fe58fdSRyusuke Konishi { 70605fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 70705fe58fdSRyusuke Konishi int err; 70805fe58fdSRyusuke Konishi 70905fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 71005fe58fdSRyusuke Konishi if (ii->i_bh == NULL) { 71105fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 71205fe58fdSRyusuke Konishi err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino, 71305fe58fdSRyusuke Konishi pbh); 71405fe58fdSRyusuke Konishi if (unlikely(err)) 71505fe58fdSRyusuke Konishi return err; 71605fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 71705fe58fdSRyusuke Konishi if (ii->i_bh == NULL) 71805fe58fdSRyusuke Konishi ii->i_bh = *pbh; 71905fe58fdSRyusuke Konishi else { 72005fe58fdSRyusuke Konishi brelse(*pbh); 72105fe58fdSRyusuke Konishi *pbh = ii->i_bh; 72205fe58fdSRyusuke Konishi } 72305fe58fdSRyusuke Konishi } else 72405fe58fdSRyusuke Konishi *pbh = ii->i_bh; 72505fe58fdSRyusuke Konishi 72605fe58fdSRyusuke Konishi get_bh(*pbh); 72705fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 72805fe58fdSRyusuke Konishi return 0; 72905fe58fdSRyusuke Konishi } 73005fe58fdSRyusuke Konishi 73105fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode) 73205fe58fdSRyusuke Konishi { 73305fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 73405fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 73505fe58fdSRyusuke Konishi int ret = 0; 73605fe58fdSRyusuke Konishi 73705fe58fdSRyusuke Konishi if (!list_empty(&ii->i_dirty)) { 73805fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 73905fe58fdSRyusuke Konishi ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 74005fe58fdSRyusuke Konishi test_bit(NILFS_I_BUSY, &ii->i_state); 74105fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 74205fe58fdSRyusuke Konishi } 74305fe58fdSRyusuke Konishi return ret; 74405fe58fdSRyusuke Konishi } 74505fe58fdSRyusuke Konishi 74605fe58fdSRyusuke Konishi int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode, 74705fe58fdSRyusuke Konishi unsigned nr_dirty) 74805fe58fdSRyusuke Konishi { 74905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 75005fe58fdSRyusuke Konishi 75105fe58fdSRyusuke Konishi atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks); 75205fe58fdSRyusuke Konishi 753458c5b08SRyusuke Konishi if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 75405fe58fdSRyusuke Konishi return 0; 75505fe58fdSRyusuke Konishi 75605fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 75705fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 75805fe58fdSRyusuke Konishi !test_bit(NILFS_I_BUSY, &ii->i_state)) { 75905fe58fdSRyusuke Konishi /* Because this routine may race with nilfs_dispose_list(), 76005fe58fdSRyusuke Konishi we have to check NILFS_I_QUEUED here, too. */ 76105fe58fdSRyusuke Konishi if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 76205fe58fdSRyusuke Konishi /* This will happen when somebody is freeing 76305fe58fdSRyusuke Konishi this inode. */ 76405fe58fdSRyusuke Konishi nilfs_warning(sbi->s_super, __func__, 76505fe58fdSRyusuke Konishi "cannot get inode (ino=%lu)\n", 76605fe58fdSRyusuke Konishi inode->i_ino); 76705fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 76805fe58fdSRyusuke Konishi return -EINVAL; /* NILFS_I_DIRTY may remain for 76905fe58fdSRyusuke Konishi freeing inode */ 77005fe58fdSRyusuke Konishi } 77105fe58fdSRyusuke Konishi list_del(&ii->i_dirty); 77205fe58fdSRyusuke Konishi list_add_tail(&ii->i_dirty, &sbi->s_dirty_files); 77305fe58fdSRyusuke Konishi set_bit(NILFS_I_QUEUED, &ii->i_state); 77405fe58fdSRyusuke Konishi } 77505fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 77605fe58fdSRyusuke Konishi return 0; 77705fe58fdSRyusuke Konishi } 77805fe58fdSRyusuke Konishi 77905fe58fdSRyusuke Konishi int nilfs_mark_inode_dirty(struct inode *inode) 78005fe58fdSRyusuke Konishi { 78105fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 78205fe58fdSRyusuke Konishi struct buffer_head *ibh; 78305fe58fdSRyusuke Konishi int err; 78405fe58fdSRyusuke Konishi 78505fe58fdSRyusuke Konishi err = nilfs_load_inode_block(sbi, inode, &ibh); 78605fe58fdSRyusuke Konishi if (unlikely(err)) { 78705fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 78805fe58fdSRyusuke Konishi "failed to reget inode block.\n"); 78905fe58fdSRyusuke Konishi return err; 79005fe58fdSRyusuke Konishi } 79105fe58fdSRyusuke Konishi nilfs_update_inode(inode, ibh); 79205fe58fdSRyusuke Konishi nilfs_mdt_mark_buffer_dirty(ibh); 79305fe58fdSRyusuke Konishi nilfs_mdt_mark_dirty(sbi->s_ifile); 79405fe58fdSRyusuke Konishi brelse(ibh); 79505fe58fdSRyusuke Konishi return 0; 79605fe58fdSRyusuke Konishi } 79705fe58fdSRyusuke Konishi 79805fe58fdSRyusuke Konishi /** 79905fe58fdSRyusuke Konishi * nilfs_dirty_inode - reflect changes on given inode to an inode block. 80005fe58fdSRyusuke Konishi * @inode: inode of the file to be registered. 80105fe58fdSRyusuke Konishi * 80205fe58fdSRyusuke Konishi * nilfs_dirty_inode() loads a inode block containing the specified 80305fe58fdSRyusuke Konishi * @inode and copies data from a nilfs_inode to a corresponding inode 80405fe58fdSRyusuke Konishi * entry in the inode block. This operation is excluded from the segment 80505fe58fdSRyusuke Konishi * construction. This function can be called both as a single operation 80605fe58fdSRyusuke Konishi * and as a part of indivisible file operations. 80705fe58fdSRyusuke Konishi */ 80805fe58fdSRyusuke Konishi void nilfs_dirty_inode(struct inode *inode) 80905fe58fdSRyusuke Konishi { 81005fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 811*7d6cd92fSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 81205fe58fdSRyusuke Konishi 81305fe58fdSRyusuke Konishi if (is_bad_inode(inode)) { 81405fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 81505fe58fdSRyusuke Konishi "tried to mark bad_inode dirty. ignored.\n"); 81605fe58fdSRyusuke Konishi dump_stack(); 81705fe58fdSRyusuke Konishi return; 81805fe58fdSRyusuke Konishi } 819*7d6cd92fSRyusuke Konishi if (mdi) { 820*7d6cd92fSRyusuke Konishi nilfs_mdt_mark_dirty(inode); 821*7d6cd92fSRyusuke Konishi return; 822*7d6cd92fSRyusuke Konishi } 82305fe58fdSRyusuke Konishi nilfs_transaction_begin(inode->i_sb, &ti, 0); 82405fe58fdSRyusuke Konishi nilfs_mark_inode_dirty(inode); 82547420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 82605fe58fdSRyusuke Konishi } 827