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" 3005fe58fdSRyusuke Konishi #include "segment.h" 3105fe58fdSRyusuke Konishi #include "page.h" 3205fe58fdSRyusuke Konishi #include "mdt.h" 3305fe58fdSRyusuke Konishi #include "cpfile.h" 3405fe58fdSRyusuke Konishi #include "ifile.h" 3505fe58fdSRyusuke Konishi 3605fe58fdSRyusuke Konishi 3705fe58fdSRyusuke Konishi /** 3805fe58fdSRyusuke Konishi * nilfs_get_block() - get a file block on the filesystem (callback function) 3905fe58fdSRyusuke Konishi * @inode - inode struct of the target file 4005fe58fdSRyusuke Konishi * @blkoff - file block number 4105fe58fdSRyusuke Konishi * @bh_result - buffer head to be mapped on 4205fe58fdSRyusuke Konishi * @create - indicate whether allocating the block or not when it has not 4305fe58fdSRyusuke Konishi * been allocated yet. 4405fe58fdSRyusuke Konishi * 4505fe58fdSRyusuke Konishi * This function does not issue actual read request of the specified data 4605fe58fdSRyusuke Konishi * block. It is done by VFS. 4705fe58fdSRyusuke Konishi */ 4805fe58fdSRyusuke Konishi int nilfs_get_block(struct inode *inode, sector_t blkoff, 4905fe58fdSRyusuke Konishi struct buffer_head *bh_result, int create) 5005fe58fdSRyusuke Konishi { 5105fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 52c3a7abf0SRyusuke Konishi __u64 blknum = 0; 5305fe58fdSRyusuke Konishi int err = 0, ret; 5405fe58fdSRyusuke Konishi struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode)); 55c3a7abf0SRyusuke Konishi unsigned maxblocks = bh_result->b_size >> inode->i_blkbits; 5605fe58fdSRyusuke Konishi 57c3a7abf0SRyusuke Konishi down_read(&NILFS_MDT(dat)->mi_sem); 58c3a7abf0SRyusuke Konishi ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); 59c3a7abf0SRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); 60c3a7abf0SRyusuke Konishi if (ret >= 0) { /* found */ 6105fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, blknum); 62c3a7abf0SRyusuke Konishi if (ret > 0) 63c3a7abf0SRyusuke Konishi bh_result->b_size = (ret << inode->i_blkbits); 6405fe58fdSRyusuke Konishi goto out; 6505fe58fdSRyusuke Konishi } 6605fe58fdSRyusuke Konishi /* data block was not found */ 6705fe58fdSRyusuke Konishi if (ret == -ENOENT && create) { 6805fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 6905fe58fdSRyusuke Konishi 7005fe58fdSRyusuke Konishi bh_result->b_blocknr = 0; 7105fe58fdSRyusuke Konishi err = nilfs_transaction_begin(inode->i_sb, &ti, 1); 7205fe58fdSRyusuke Konishi if (unlikely(err)) 7305fe58fdSRyusuke Konishi goto out; 7405fe58fdSRyusuke Konishi err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, 7505fe58fdSRyusuke Konishi (unsigned long)bh_result); 7605fe58fdSRyusuke Konishi if (unlikely(err != 0)) { 7705fe58fdSRyusuke Konishi if (err == -EEXIST) { 7805fe58fdSRyusuke Konishi /* 7905fe58fdSRyusuke Konishi * The get_block() function could be called 8005fe58fdSRyusuke Konishi * from multiple callers for an inode. 8105fe58fdSRyusuke Konishi * However, the page having this block must 8205fe58fdSRyusuke Konishi * be locked in this case. 8305fe58fdSRyusuke Konishi */ 841f5abe7eSRyusuke Konishi printk(KERN_WARNING 8505fe58fdSRyusuke Konishi "nilfs_get_block: a race condition " 8605fe58fdSRyusuke Konishi "while inserting a data block. " 8705fe58fdSRyusuke Konishi "(inode number=%lu, file block " 8805fe58fdSRyusuke Konishi "offset=%llu)\n", 8905fe58fdSRyusuke Konishi inode->i_ino, 9005fe58fdSRyusuke Konishi (unsigned long long)blkoff); 911f5abe7eSRyusuke Konishi err = 0; 9205fe58fdSRyusuke Konishi } else if (err == -EINVAL) { 9305fe58fdSRyusuke Konishi nilfs_error(inode->i_sb, __func__, 9405fe58fdSRyusuke Konishi "broken bmap (inode=%lu)\n", 9505fe58fdSRyusuke Konishi inode->i_ino); 9605fe58fdSRyusuke Konishi err = -EIO; 9705fe58fdSRyusuke Konishi } 9847420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 9905fe58fdSRyusuke Konishi goto out; 10005fe58fdSRyusuke Konishi } 101abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 10247420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 10305fe58fdSRyusuke Konishi /* Error handling should be detailed */ 10405fe58fdSRyusuke Konishi set_buffer_new(bh_result); 10505fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed 10605fe58fdSRyusuke Konishi to proper value */ 10705fe58fdSRyusuke Konishi } else if (ret == -ENOENT) { 10805fe58fdSRyusuke Konishi /* not found is not error (e.g. hole); must return without 10905fe58fdSRyusuke Konishi the mapped state flag. */ 11005fe58fdSRyusuke Konishi ; 11105fe58fdSRyusuke Konishi } else { 11205fe58fdSRyusuke Konishi err = ret; 11305fe58fdSRyusuke Konishi } 11405fe58fdSRyusuke Konishi 11505fe58fdSRyusuke Konishi out: 11605fe58fdSRyusuke Konishi return err; 11705fe58fdSRyusuke Konishi } 11805fe58fdSRyusuke Konishi 11905fe58fdSRyusuke Konishi /** 12005fe58fdSRyusuke Konishi * nilfs_readpage() - implement readpage() method of nilfs_aops {} 12105fe58fdSRyusuke Konishi * address_space_operations. 12205fe58fdSRyusuke Konishi * @file - file struct of the file to be read 12305fe58fdSRyusuke Konishi * @page - the page to be read 12405fe58fdSRyusuke Konishi */ 12505fe58fdSRyusuke Konishi static int nilfs_readpage(struct file *file, struct page *page) 12605fe58fdSRyusuke Konishi { 12705fe58fdSRyusuke Konishi return mpage_readpage(page, nilfs_get_block); 12805fe58fdSRyusuke Konishi } 12905fe58fdSRyusuke Konishi 13005fe58fdSRyusuke Konishi /** 13105fe58fdSRyusuke Konishi * nilfs_readpages() - implement readpages() method of nilfs_aops {} 13205fe58fdSRyusuke Konishi * address_space_operations. 13305fe58fdSRyusuke Konishi * @file - file struct of the file to be read 13405fe58fdSRyusuke Konishi * @mapping - address_space struct used for reading multiple pages 13505fe58fdSRyusuke Konishi * @pages - the pages to be read 13605fe58fdSRyusuke Konishi * @nr_pages - number of pages to be read 13705fe58fdSRyusuke Konishi */ 13805fe58fdSRyusuke Konishi static int nilfs_readpages(struct file *file, struct address_space *mapping, 13905fe58fdSRyusuke Konishi struct list_head *pages, unsigned nr_pages) 14005fe58fdSRyusuke Konishi { 14105fe58fdSRyusuke Konishi return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); 14205fe58fdSRyusuke Konishi } 14305fe58fdSRyusuke Konishi 14405fe58fdSRyusuke Konishi static int nilfs_writepages(struct address_space *mapping, 14505fe58fdSRyusuke Konishi struct writeback_control *wbc) 14605fe58fdSRyusuke Konishi { 147f30bf3e4SRyusuke Konishi struct inode *inode = mapping->host; 148f30bf3e4SRyusuke Konishi int err = 0; 149f30bf3e4SRyusuke Konishi 150f30bf3e4SRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) 151f30bf3e4SRyusuke Konishi err = nilfs_construct_dsync_segment(inode->i_sb, inode, 152f30bf3e4SRyusuke Konishi wbc->range_start, 153f30bf3e4SRyusuke Konishi wbc->range_end); 154f30bf3e4SRyusuke Konishi return err; 15505fe58fdSRyusuke Konishi } 15605fe58fdSRyusuke Konishi 15705fe58fdSRyusuke Konishi static int nilfs_writepage(struct page *page, struct writeback_control *wbc) 15805fe58fdSRyusuke Konishi { 15905fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 16005fe58fdSRyusuke Konishi int err; 16105fe58fdSRyusuke Konishi 16205fe58fdSRyusuke Konishi redirty_page_for_writepage(wbc, page); 16305fe58fdSRyusuke Konishi unlock_page(page); 16405fe58fdSRyusuke Konishi 16505fe58fdSRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) { 16605fe58fdSRyusuke Konishi err = nilfs_construct_segment(inode->i_sb); 16705fe58fdSRyusuke Konishi if (unlikely(err)) 16805fe58fdSRyusuke Konishi return err; 16905fe58fdSRyusuke Konishi } else if (wbc->for_reclaim) 17005fe58fdSRyusuke Konishi nilfs_flush_segment(inode->i_sb, inode->i_ino); 17105fe58fdSRyusuke Konishi 17205fe58fdSRyusuke Konishi return 0; 17305fe58fdSRyusuke Konishi } 17405fe58fdSRyusuke Konishi 17505fe58fdSRyusuke Konishi static int nilfs_set_page_dirty(struct page *page) 17605fe58fdSRyusuke Konishi { 17705fe58fdSRyusuke Konishi int ret = __set_page_dirty_buffers(page); 17805fe58fdSRyusuke Konishi 17905fe58fdSRyusuke Konishi if (ret) { 18005fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 18105fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 18205fe58fdSRyusuke Konishi unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); 18305fe58fdSRyusuke Konishi 18405fe58fdSRyusuke Konishi nilfs_set_file_dirty(sbi, inode, nr_dirty); 18505fe58fdSRyusuke Konishi } 18605fe58fdSRyusuke Konishi return ret; 18705fe58fdSRyusuke Konishi } 18805fe58fdSRyusuke Konishi 18905fe58fdSRyusuke Konishi static int nilfs_write_begin(struct file *file, struct address_space *mapping, 19005fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned flags, 19105fe58fdSRyusuke Konishi struct page **pagep, void **fsdata) 19205fe58fdSRyusuke Konishi 19305fe58fdSRyusuke Konishi { 19405fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 19505fe58fdSRyusuke Konishi int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); 19605fe58fdSRyusuke Konishi 19705fe58fdSRyusuke Konishi if (unlikely(err)) 19805fe58fdSRyusuke Konishi return err; 19905fe58fdSRyusuke Konishi 20005fe58fdSRyusuke Konishi *pagep = NULL; 20105fe58fdSRyusuke Konishi err = block_write_begin(file, mapping, pos, len, flags, pagep, 20205fe58fdSRyusuke Konishi fsdata, nilfs_get_block); 20305fe58fdSRyusuke Konishi if (unlikely(err)) 20447420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 20505fe58fdSRyusuke Konishi return err; 20605fe58fdSRyusuke Konishi } 20705fe58fdSRyusuke Konishi 20805fe58fdSRyusuke Konishi static int nilfs_write_end(struct file *file, struct address_space *mapping, 20905fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned copied, 21005fe58fdSRyusuke Konishi struct page *page, void *fsdata) 21105fe58fdSRyusuke Konishi { 21205fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 21305fe58fdSRyusuke Konishi unsigned start = pos & (PAGE_CACHE_SIZE - 1); 21405fe58fdSRyusuke Konishi unsigned nr_dirty; 21505fe58fdSRyusuke Konishi int err; 21605fe58fdSRyusuke Konishi 21705fe58fdSRyusuke Konishi nr_dirty = nilfs_page_count_clean_buffers(page, start, 21805fe58fdSRyusuke Konishi start + copied); 21905fe58fdSRyusuke Konishi copied = generic_write_end(file, mapping, pos, len, copied, page, 22005fe58fdSRyusuke Konishi fsdata); 22105fe58fdSRyusuke Konishi nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty); 22247420c79SRyusuke Konishi err = nilfs_transaction_commit(inode->i_sb); 22305fe58fdSRyusuke Konishi return err ? : copied; 22405fe58fdSRyusuke Konishi } 22505fe58fdSRyusuke Konishi 22605fe58fdSRyusuke Konishi static ssize_t 22705fe58fdSRyusuke Konishi nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, 22805fe58fdSRyusuke Konishi loff_t offset, unsigned long nr_segs) 22905fe58fdSRyusuke Konishi { 23005fe58fdSRyusuke Konishi struct file *file = iocb->ki_filp; 23105fe58fdSRyusuke Konishi struct inode *inode = file->f_mapping->host; 23205fe58fdSRyusuke Konishi ssize_t size; 23305fe58fdSRyusuke Konishi 23405fe58fdSRyusuke Konishi if (rw == WRITE) 23505fe58fdSRyusuke Konishi return 0; 23605fe58fdSRyusuke Konishi 23705fe58fdSRyusuke Konishi /* Needs synchronization with the cleaner */ 23805fe58fdSRyusuke Konishi size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, 23905fe58fdSRyusuke Konishi offset, nr_segs, nilfs_get_block, NULL); 24005fe58fdSRyusuke Konishi return size; 24105fe58fdSRyusuke Konishi } 24205fe58fdSRyusuke Konishi 2437f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = { 24405fe58fdSRyusuke Konishi .writepage = nilfs_writepage, 24505fe58fdSRyusuke Konishi .readpage = nilfs_readpage, 246e85dc1d5SRyusuke Konishi .sync_page = block_sync_page, 24705fe58fdSRyusuke Konishi .writepages = nilfs_writepages, 24805fe58fdSRyusuke Konishi .set_page_dirty = nilfs_set_page_dirty, 24905fe58fdSRyusuke Konishi .readpages = nilfs_readpages, 25005fe58fdSRyusuke Konishi .write_begin = nilfs_write_begin, 25105fe58fdSRyusuke Konishi .write_end = nilfs_write_end, 25205fe58fdSRyusuke Konishi /* .releasepage = nilfs_releasepage, */ 25305fe58fdSRyusuke Konishi .invalidatepage = block_invalidatepage, 25405fe58fdSRyusuke Konishi .direct_IO = nilfs_direct_IO, 255258ef67eSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 25605fe58fdSRyusuke Konishi }; 25705fe58fdSRyusuke Konishi 25805fe58fdSRyusuke Konishi struct inode *nilfs_new_inode(struct inode *dir, int mode) 25905fe58fdSRyusuke Konishi { 26005fe58fdSRyusuke Konishi struct super_block *sb = dir->i_sb; 26105fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 26205fe58fdSRyusuke Konishi struct inode *inode; 26305fe58fdSRyusuke Konishi struct nilfs_inode_info *ii; 26405fe58fdSRyusuke Konishi int err = -ENOMEM; 26505fe58fdSRyusuke Konishi ino_t ino; 26605fe58fdSRyusuke Konishi 26705fe58fdSRyusuke Konishi inode = new_inode(sb); 26805fe58fdSRyusuke Konishi if (unlikely(!inode)) 26905fe58fdSRyusuke Konishi goto failed; 27005fe58fdSRyusuke Konishi 27105fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 27205fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 27305fe58fdSRyusuke Konishi 27405fe58fdSRyusuke Konishi ii = NILFS_I(inode); 27505fe58fdSRyusuke Konishi ii->i_state = 1 << NILFS_I_NEW; 27605fe58fdSRyusuke Konishi 27705fe58fdSRyusuke Konishi err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh); 27805fe58fdSRyusuke Konishi if (unlikely(err)) 27905fe58fdSRyusuke Konishi goto failed_ifile_create_inode; 28005fe58fdSRyusuke Konishi /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 28105fe58fdSRyusuke Konishi 28205fe58fdSRyusuke Konishi atomic_inc(&sbi->s_inodes_count); 28305fe58fdSRyusuke Konishi 28405fe58fdSRyusuke Konishi inode->i_uid = current_fsuid(); 28505fe58fdSRyusuke Konishi if (dir->i_mode & S_ISGID) { 28605fe58fdSRyusuke Konishi inode->i_gid = dir->i_gid; 28705fe58fdSRyusuke Konishi if (S_ISDIR(mode)) 28805fe58fdSRyusuke Konishi mode |= S_ISGID; 28905fe58fdSRyusuke Konishi } else 29005fe58fdSRyusuke Konishi inode->i_gid = current_fsgid(); 29105fe58fdSRyusuke Konishi 29205fe58fdSRyusuke Konishi inode->i_mode = mode; 29305fe58fdSRyusuke Konishi inode->i_ino = ino; 29405fe58fdSRyusuke Konishi inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 29505fe58fdSRyusuke Konishi 29605fe58fdSRyusuke Konishi if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 29705fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, NULL); 29805fe58fdSRyusuke Konishi if (err < 0) 29905fe58fdSRyusuke Konishi goto failed_bmap; 30005fe58fdSRyusuke Konishi 30105fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 30205fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 30305fe58fdSRyusuke Konishi } 30405fe58fdSRyusuke Konishi 30505fe58fdSRyusuke Konishi ii->i_flags = NILFS_I(dir)->i_flags; 30605fe58fdSRyusuke Konishi if (S_ISLNK(mode)) 30705fe58fdSRyusuke Konishi ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL); 30805fe58fdSRyusuke Konishi if (!S_ISDIR(mode)) 30905fe58fdSRyusuke Konishi ii->i_flags &= ~NILFS_DIRSYNC_FL; 31005fe58fdSRyusuke Konishi 31105fe58fdSRyusuke Konishi /* ii->i_file_acl = 0; */ 31205fe58fdSRyusuke Konishi /* ii->i_dir_acl = 0; */ 31305fe58fdSRyusuke Konishi ii->i_dir_start_lookup = 0; 31405fe58fdSRyusuke Konishi ii->i_cno = 0; 31505fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 31605fe58fdSRyusuke Konishi spin_lock(&sbi->s_next_gen_lock); 31705fe58fdSRyusuke Konishi inode->i_generation = sbi->s_next_generation++; 31805fe58fdSRyusuke Konishi spin_unlock(&sbi->s_next_gen_lock); 31905fe58fdSRyusuke Konishi insert_inode_hash(inode); 32005fe58fdSRyusuke Konishi 32105fe58fdSRyusuke Konishi err = nilfs_init_acl(inode, dir); 32205fe58fdSRyusuke Konishi if (unlikely(err)) 32305fe58fdSRyusuke Konishi goto failed_acl; /* never occur. When supporting 32405fe58fdSRyusuke Konishi nilfs_init_acl(), proper cancellation of 32505fe58fdSRyusuke Konishi above jobs should be considered */ 32605fe58fdSRyusuke Konishi 32705fe58fdSRyusuke Konishi return inode; 32805fe58fdSRyusuke Konishi 32905fe58fdSRyusuke Konishi failed_acl: 33005fe58fdSRyusuke Konishi failed_bmap: 33105fe58fdSRyusuke Konishi inode->i_nlink = 0; 33205fe58fdSRyusuke Konishi iput(inode); /* raw_inode will be deleted through 33305fe58fdSRyusuke Konishi generic_delete_inode() */ 33405fe58fdSRyusuke Konishi goto failed; 33505fe58fdSRyusuke Konishi 33605fe58fdSRyusuke Konishi failed_ifile_create_inode: 33705fe58fdSRyusuke Konishi make_bad_inode(inode); 33805fe58fdSRyusuke Konishi iput(inode); /* if i_nlink == 1, generic_forget_inode() will be 33905fe58fdSRyusuke Konishi called */ 34005fe58fdSRyusuke Konishi failed: 34105fe58fdSRyusuke Konishi return ERR_PTR(err); 34205fe58fdSRyusuke Konishi } 34305fe58fdSRyusuke Konishi 34405fe58fdSRyusuke Konishi void nilfs_free_inode(struct inode *inode) 34505fe58fdSRyusuke Konishi { 34605fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 34705fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 34805fe58fdSRyusuke Konishi 34905fe58fdSRyusuke Konishi clear_inode(inode); 35005fe58fdSRyusuke Konishi /* XXX: check error code? Is there any thing I can do? */ 35105fe58fdSRyusuke Konishi (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino); 35205fe58fdSRyusuke Konishi atomic_dec(&sbi->s_inodes_count); 35305fe58fdSRyusuke Konishi } 35405fe58fdSRyusuke Konishi 35505fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode) 35605fe58fdSRyusuke Konishi { 35705fe58fdSRyusuke Konishi unsigned int flags = NILFS_I(inode)->i_flags; 35805fe58fdSRyusuke Konishi 35905fe58fdSRyusuke Konishi inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | 36005fe58fdSRyusuke Konishi S_DIRSYNC); 36105fe58fdSRyusuke Konishi if (flags & NILFS_SYNC_FL) 36205fe58fdSRyusuke Konishi inode->i_flags |= S_SYNC; 36305fe58fdSRyusuke Konishi if (flags & NILFS_APPEND_FL) 36405fe58fdSRyusuke Konishi inode->i_flags |= S_APPEND; 36505fe58fdSRyusuke Konishi if (flags & NILFS_IMMUTABLE_FL) 36605fe58fdSRyusuke Konishi inode->i_flags |= S_IMMUTABLE; 36705fe58fdSRyusuke Konishi #ifndef NILFS_ATIME_DISABLE 36805fe58fdSRyusuke Konishi if (flags & NILFS_NOATIME_FL) 36905fe58fdSRyusuke Konishi #endif 37005fe58fdSRyusuke Konishi inode->i_flags |= S_NOATIME; 37105fe58fdSRyusuke Konishi if (flags & NILFS_DIRSYNC_FL) 37205fe58fdSRyusuke Konishi inode->i_flags |= S_DIRSYNC; 37305fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 37405fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 37505fe58fdSRyusuke Konishi } 37605fe58fdSRyusuke Konishi 37705fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode, 37805fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode) 37905fe58fdSRyusuke Konishi { 38005fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 38105fe58fdSRyusuke Konishi int err; 38205fe58fdSRyusuke Konishi 38305fe58fdSRyusuke Konishi inode->i_mode = le16_to_cpu(raw_inode->i_mode); 38405fe58fdSRyusuke Konishi inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid); 38505fe58fdSRyusuke Konishi inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid); 38605fe58fdSRyusuke Konishi inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 38705fe58fdSRyusuke Konishi inode->i_size = le64_to_cpu(raw_inode->i_size); 38805fe58fdSRyusuke Konishi inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 38905fe58fdSRyusuke Konishi inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); 39005fe58fdSRyusuke Konishi inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 39161239230SRyusuke Konishi inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 39261239230SRyusuke Konishi inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); 39361239230SRyusuke Konishi inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 39461239230SRyusuke Konishi if (inode->i_nlink == 0 && inode->i_mode == 0) 39505fe58fdSRyusuke Konishi return -EINVAL; /* this inode is deleted */ 39605fe58fdSRyusuke Konishi 39705fe58fdSRyusuke Konishi inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 39805fe58fdSRyusuke Konishi ii->i_flags = le32_to_cpu(raw_inode->i_flags); 39905fe58fdSRyusuke Konishi #if 0 40005fe58fdSRyusuke Konishi ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 40105fe58fdSRyusuke Konishi ii->i_dir_acl = S_ISREG(inode->i_mode) ? 40205fe58fdSRyusuke Konishi 0 : le32_to_cpu(raw_inode->i_dir_acl); 40305fe58fdSRyusuke Konishi #endif 4043cc811bfSRyusuke Konishi ii->i_dir_start_lookup = 0; 40505fe58fdSRyusuke Konishi ii->i_cno = 0; 40605fe58fdSRyusuke Konishi inode->i_generation = le32_to_cpu(raw_inode->i_generation); 40705fe58fdSRyusuke Konishi 40805fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 40905fe58fdSRyusuke Konishi S_ISLNK(inode->i_mode)) { 41005fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, raw_inode); 41105fe58fdSRyusuke Konishi if (err < 0) 41205fe58fdSRyusuke Konishi return err; 41305fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 41405fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 41505fe58fdSRyusuke Konishi } 41605fe58fdSRyusuke Konishi return 0; 41705fe58fdSRyusuke Konishi } 41805fe58fdSRyusuke Konishi 41905fe58fdSRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb, unsigned long ino, 42005fe58fdSRyusuke Konishi struct inode *inode) 42105fe58fdSRyusuke Konishi { 42205fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 42305fe58fdSRyusuke Konishi struct inode *dat = nilfs_dat_inode(sbi->s_nilfs); 42405fe58fdSRyusuke Konishi struct buffer_head *bh; 42505fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 42605fe58fdSRyusuke Konishi int err; 42705fe58fdSRyusuke Konishi 42805fe58fdSRyusuke Konishi down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 42905fe58fdSRyusuke Konishi err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh); 43005fe58fdSRyusuke Konishi if (unlikely(err)) 43105fe58fdSRyusuke Konishi goto bad_inode; 43205fe58fdSRyusuke Konishi 43305fe58fdSRyusuke Konishi raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh); 43405fe58fdSRyusuke Konishi 4351b2f5a64SRyusuke Konishi err = nilfs_read_inode_common(inode, raw_inode); 4361b2f5a64SRyusuke Konishi if (err) 43705fe58fdSRyusuke Konishi goto failed_unmap; 43805fe58fdSRyusuke Konishi 43905fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode)) { 44005fe58fdSRyusuke Konishi inode->i_op = &nilfs_file_inode_operations; 44105fe58fdSRyusuke Konishi inode->i_fop = &nilfs_file_operations; 44205fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 44305fe58fdSRyusuke Konishi } else if (S_ISDIR(inode->i_mode)) { 44405fe58fdSRyusuke Konishi inode->i_op = &nilfs_dir_inode_operations; 44505fe58fdSRyusuke Konishi inode->i_fop = &nilfs_dir_operations; 44605fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 44705fe58fdSRyusuke Konishi } else if (S_ISLNK(inode->i_mode)) { 44805fe58fdSRyusuke Konishi inode->i_op = &nilfs_symlink_inode_operations; 44905fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 45005fe58fdSRyusuke Konishi } else { 45105fe58fdSRyusuke Konishi inode->i_op = &nilfs_special_inode_operations; 45205fe58fdSRyusuke Konishi init_special_inode( 45305fe58fdSRyusuke Konishi inode, inode->i_mode, 454*cdce214eSRyusuke Konishi huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 45505fe58fdSRyusuke Konishi } 45605fe58fdSRyusuke Konishi nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh); 45705fe58fdSRyusuke Konishi brelse(bh); 45805fe58fdSRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 45905fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 46005fe58fdSRyusuke Konishi return 0; 46105fe58fdSRyusuke Konishi 46205fe58fdSRyusuke Konishi failed_unmap: 46305fe58fdSRyusuke Konishi nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh); 46405fe58fdSRyusuke Konishi brelse(bh); 46505fe58fdSRyusuke Konishi 46605fe58fdSRyusuke Konishi bad_inode: 46705fe58fdSRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 46805fe58fdSRyusuke Konishi return err; 46905fe58fdSRyusuke Konishi } 47005fe58fdSRyusuke Konishi 47105fe58fdSRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, unsigned long ino) 47205fe58fdSRyusuke Konishi { 47305fe58fdSRyusuke Konishi struct inode *inode; 47405fe58fdSRyusuke Konishi int err; 47505fe58fdSRyusuke Konishi 47605fe58fdSRyusuke Konishi inode = iget_locked(sb, ino); 47705fe58fdSRyusuke Konishi if (unlikely(!inode)) 47805fe58fdSRyusuke Konishi return ERR_PTR(-ENOMEM); 47905fe58fdSRyusuke Konishi if (!(inode->i_state & I_NEW)) 48005fe58fdSRyusuke Konishi return inode; 48105fe58fdSRyusuke Konishi 48205fe58fdSRyusuke Konishi err = __nilfs_read_inode(sb, ino, inode); 48305fe58fdSRyusuke Konishi if (unlikely(err)) { 48405fe58fdSRyusuke Konishi iget_failed(inode); 48505fe58fdSRyusuke Konishi return ERR_PTR(err); 48605fe58fdSRyusuke Konishi } 48705fe58fdSRyusuke Konishi unlock_new_inode(inode); 48805fe58fdSRyusuke Konishi return inode; 48905fe58fdSRyusuke Konishi } 49005fe58fdSRyusuke Konishi 49105fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode, 49205fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode, int has_bmap) 49305fe58fdSRyusuke Konishi { 49405fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 49505fe58fdSRyusuke Konishi 49605fe58fdSRyusuke Konishi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 49705fe58fdSRyusuke Konishi raw_inode->i_uid = cpu_to_le32(inode->i_uid); 49805fe58fdSRyusuke Konishi raw_inode->i_gid = cpu_to_le32(inode->i_gid); 49905fe58fdSRyusuke Konishi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 50005fe58fdSRyusuke Konishi raw_inode->i_size = cpu_to_le64(inode->i_size); 50105fe58fdSRyusuke Konishi raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 50205fe58fdSRyusuke Konishi raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); 50361239230SRyusuke Konishi raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 50461239230SRyusuke Konishi raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); 50505fe58fdSRyusuke Konishi raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 50605fe58fdSRyusuke Konishi 50705fe58fdSRyusuke Konishi raw_inode->i_flags = cpu_to_le32(ii->i_flags); 50805fe58fdSRyusuke Konishi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 50905fe58fdSRyusuke Konishi 51005fe58fdSRyusuke Konishi if (has_bmap) 51105fe58fdSRyusuke Konishi nilfs_bmap_write(ii->i_bmap, raw_inode); 51205fe58fdSRyusuke Konishi else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 51305fe58fdSRyusuke Konishi raw_inode->i_device_code = 514*cdce214eSRyusuke Konishi cpu_to_le64(huge_encode_dev(inode->i_rdev)); 51505fe58fdSRyusuke Konishi /* When extending inode, nilfs->ns_inode_size should be checked 51605fe58fdSRyusuke Konishi for substitutions of appended fields */ 51705fe58fdSRyusuke Konishi } 51805fe58fdSRyusuke Konishi 51905fe58fdSRyusuke Konishi void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh) 52005fe58fdSRyusuke Konishi { 52105fe58fdSRyusuke Konishi ino_t ino = inode->i_ino; 52205fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 52305fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 52405fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 52505fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 52605fe58fdSRyusuke Konishi 52705fe58fdSRyusuke Konishi raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh); 52805fe58fdSRyusuke Konishi 52905fe58fdSRyusuke Konishi if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 53005fe58fdSRyusuke Konishi memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size); 53105fe58fdSRyusuke Konishi set_bit(NILFS_I_INODE_DIRTY, &ii->i_state); 53205fe58fdSRyusuke Konishi 53305fe58fdSRyusuke Konishi nilfs_write_inode_common(inode, raw_inode, 0); 53405fe58fdSRyusuke Konishi /* XXX: call with has_bmap = 0 is a workaround to avoid 53505fe58fdSRyusuke Konishi deadlock of bmap. This delays update of i_bmap to just 53605fe58fdSRyusuke Konishi before writing */ 53705fe58fdSRyusuke Konishi nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh); 53805fe58fdSRyusuke Konishi } 53905fe58fdSRyusuke Konishi 54005fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 54105fe58fdSRyusuke Konishi 54205fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 54305fe58fdSRyusuke Konishi unsigned long from) 54405fe58fdSRyusuke Konishi { 54505fe58fdSRyusuke Konishi unsigned long b; 54605fe58fdSRyusuke Konishi int ret; 54705fe58fdSRyusuke Konishi 54805fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 54905fe58fdSRyusuke Konishi return; 55005fe58fdSRyusuke Konishi repeat: 55105fe58fdSRyusuke Konishi ret = nilfs_bmap_last_key(ii->i_bmap, &b); 55205fe58fdSRyusuke Konishi if (ret == -ENOENT) 55305fe58fdSRyusuke Konishi return; 55405fe58fdSRyusuke Konishi else if (ret < 0) 55505fe58fdSRyusuke Konishi goto failed; 55605fe58fdSRyusuke Konishi 55705fe58fdSRyusuke Konishi if (b < from) 55805fe58fdSRyusuke Konishi return; 55905fe58fdSRyusuke Konishi 56005fe58fdSRyusuke Konishi b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 56105fe58fdSRyusuke Konishi ret = nilfs_bmap_truncate(ii->i_bmap, b); 56205fe58fdSRyusuke Konishi nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 56305fe58fdSRyusuke Konishi if (!ret || (ret == -ENOMEM && 56405fe58fdSRyusuke Konishi nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 56505fe58fdSRyusuke Konishi goto repeat; 56605fe58fdSRyusuke Konishi 56705fe58fdSRyusuke Konishi failed: 56805fe58fdSRyusuke Konishi if (ret == -EINVAL) 56905fe58fdSRyusuke Konishi nilfs_error(ii->vfs_inode.i_sb, __func__, 57005fe58fdSRyusuke Konishi "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino); 57105fe58fdSRyusuke Konishi else 57205fe58fdSRyusuke Konishi nilfs_warning(ii->vfs_inode.i_sb, __func__, 57305fe58fdSRyusuke Konishi "failed to truncate bmap (ino=%lu, err=%d)", 57405fe58fdSRyusuke Konishi ii->vfs_inode.i_ino, ret); 57505fe58fdSRyusuke Konishi } 57605fe58fdSRyusuke Konishi 57705fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode) 57805fe58fdSRyusuke Konishi { 57905fe58fdSRyusuke Konishi unsigned long blkoff; 58005fe58fdSRyusuke Konishi unsigned int blocksize; 58105fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 58205fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 58305fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 58405fe58fdSRyusuke Konishi 58505fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 58605fe58fdSRyusuke Konishi return; 58705fe58fdSRyusuke Konishi if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 58805fe58fdSRyusuke Konishi return; 58905fe58fdSRyusuke Konishi 59005fe58fdSRyusuke Konishi blocksize = sb->s_blocksize; 59105fe58fdSRyusuke Konishi blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 5921f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 59305fe58fdSRyusuke Konishi 59405fe58fdSRyusuke Konishi block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 59505fe58fdSRyusuke Konishi 59605fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, blkoff); 59705fe58fdSRyusuke Konishi 59805fe58fdSRyusuke Konishi inode->i_mtime = inode->i_ctime = CURRENT_TIME; 59905fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 60005fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 60105fe58fdSRyusuke Konishi 602abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 60305fe58fdSRyusuke Konishi nilfs_set_file_dirty(NILFS_SB(sb), inode, 0); 60447420c79SRyusuke Konishi nilfs_transaction_commit(sb); 60505fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 60605fe58fdSRyusuke Konishi But truncate has no return value. */ 60705fe58fdSRyusuke Konishi } 60805fe58fdSRyusuke Konishi 60905fe58fdSRyusuke Konishi void nilfs_delete_inode(struct inode *inode) 61005fe58fdSRyusuke Konishi { 61105fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 61205fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 61305fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 61405fe58fdSRyusuke Konishi 61505fe58fdSRyusuke Konishi if (unlikely(is_bad_inode(inode))) { 61605fe58fdSRyusuke Konishi if (inode->i_data.nrpages) 61705fe58fdSRyusuke Konishi truncate_inode_pages(&inode->i_data, 0); 61805fe58fdSRyusuke Konishi clear_inode(inode); 61905fe58fdSRyusuke Konishi return; 62005fe58fdSRyusuke Konishi } 6211f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 6221f5abe7eSRyusuke Konishi 62305fe58fdSRyusuke Konishi if (inode->i_data.nrpages) 62405fe58fdSRyusuke Konishi truncate_inode_pages(&inode->i_data, 0); 62505fe58fdSRyusuke Konishi 62605fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, 0); 627abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 62805fe58fdSRyusuke Konishi nilfs_free_inode(inode); 62905fe58fdSRyusuke Konishi /* nilfs_free_inode() marks inode buffer dirty */ 63005fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 63105fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 63247420c79SRyusuke Konishi nilfs_transaction_commit(sb); 63305fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 63405fe58fdSRyusuke Konishi But delete_inode has no return value. */ 63505fe58fdSRyusuke Konishi } 63605fe58fdSRyusuke Konishi 63705fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) 63805fe58fdSRyusuke Konishi { 63905fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 64005fe58fdSRyusuke Konishi struct inode *inode = dentry->d_inode; 64105fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 64247420c79SRyusuke Konishi int err; 64305fe58fdSRyusuke Konishi 64405fe58fdSRyusuke Konishi err = inode_change_ok(inode, iattr); 64505fe58fdSRyusuke Konishi if (err) 64605fe58fdSRyusuke Konishi return err; 64705fe58fdSRyusuke Konishi 64805fe58fdSRyusuke Konishi err = nilfs_transaction_begin(sb, &ti, 0); 64905fe58fdSRyusuke Konishi if (unlikely(err)) 65005fe58fdSRyusuke Konishi return err; 65105fe58fdSRyusuke Konishi err = inode_setattr(inode, iattr); 65205fe58fdSRyusuke Konishi if (!err && (iattr->ia_valid & ATTR_MODE)) 65305fe58fdSRyusuke Konishi err = nilfs_acl_chmod(inode); 65447420c79SRyusuke Konishi if (likely(!err)) 65547420c79SRyusuke Konishi err = nilfs_transaction_commit(sb); 65647420c79SRyusuke Konishi else 65747420c79SRyusuke Konishi nilfs_transaction_abort(sb); 65847420c79SRyusuke Konishi 65947420c79SRyusuke Konishi return err; 66005fe58fdSRyusuke Konishi } 66105fe58fdSRyusuke Konishi 66205fe58fdSRyusuke Konishi int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode, 66305fe58fdSRyusuke Konishi struct buffer_head **pbh) 66405fe58fdSRyusuke Konishi { 66505fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 66605fe58fdSRyusuke Konishi int err; 66705fe58fdSRyusuke Konishi 66805fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 66905fe58fdSRyusuke Konishi if (ii->i_bh == NULL) { 67005fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 67105fe58fdSRyusuke Konishi err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino, 67205fe58fdSRyusuke Konishi pbh); 67305fe58fdSRyusuke Konishi if (unlikely(err)) 67405fe58fdSRyusuke Konishi return err; 67505fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 67605fe58fdSRyusuke Konishi if (ii->i_bh == NULL) 67705fe58fdSRyusuke Konishi ii->i_bh = *pbh; 67805fe58fdSRyusuke Konishi else { 67905fe58fdSRyusuke Konishi brelse(*pbh); 68005fe58fdSRyusuke Konishi *pbh = ii->i_bh; 68105fe58fdSRyusuke Konishi } 68205fe58fdSRyusuke Konishi } else 68305fe58fdSRyusuke Konishi *pbh = ii->i_bh; 68405fe58fdSRyusuke Konishi 68505fe58fdSRyusuke Konishi get_bh(*pbh); 68605fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 68705fe58fdSRyusuke Konishi return 0; 68805fe58fdSRyusuke Konishi } 68905fe58fdSRyusuke Konishi 69005fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode) 69105fe58fdSRyusuke Konishi { 69205fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 69305fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 69405fe58fdSRyusuke Konishi int ret = 0; 69505fe58fdSRyusuke Konishi 69605fe58fdSRyusuke Konishi if (!list_empty(&ii->i_dirty)) { 69705fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 69805fe58fdSRyusuke Konishi ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 69905fe58fdSRyusuke Konishi test_bit(NILFS_I_BUSY, &ii->i_state); 70005fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 70105fe58fdSRyusuke Konishi } 70205fe58fdSRyusuke Konishi return ret; 70305fe58fdSRyusuke Konishi } 70405fe58fdSRyusuke Konishi 70505fe58fdSRyusuke Konishi int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode, 70605fe58fdSRyusuke Konishi unsigned nr_dirty) 70705fe58fdSRyusuke Konishi { 70805fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 70905fe58fdSRyusuke Konishi 71005fe58fdSRyusuke Konishi atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks); 71105fe58fdSRyusuke Konishi 712458c5b08SRyusuke Konishi if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 71305fe58fdSRyusuke Konishi return 0; 71405fe58fdSRyusuke Konishi 71505fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 71605fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 71705fe58fdSRyusuke Konishi !test_bit(NILFS_I_BUSY, &ii->i_state)) { 71805fe58fdSRyusuke Konishi /* Because this routine may race with nilfs_dispose_list(), 71905fe58fdSRyusuke Konishi we have to check NILFS_I_QUEUED here, too. */ 72005fe58fdSRyusuke Konishi if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 72105fe58fdSRyusuke Konishi /* This will happen when somebody is freeing 72205fe58fdSRyusuke Konishi this inode. */ 72305fe58fdSRyusuke Konishi nilfs_warning(sbi->s_super, __func__, 72405fe58fdSRyusuke Konishi "cannot get inode (ino=%lu)\n", 72505fe58fdSRyusuke Konishi inode->i_ino); 72605fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 72705fe58fdSRyusuke Konishi return -EINVAL; /* NILFS_I_DIRTY may remain for 72805fe58fdSRyusuke Konishi freeing inode */ 72905fe58fdSRyusuke Konishi } 73005fe58fdSRyusuke Konishi list_del(&ii->i_dirty); 73105fe58fdSRyusuke Konishi list_add_tail(&ii->i_dirty, &sbi->s_dirty_files); 73205fe58fdSRyusuke Konishi set_bit(NILFS_I_QUEUED, &ii->i_state); 73305fe58fdSRyusuke Konishi } 73405fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 73505fe58fdSRyusuke Konishi return 0; 73605fe58fdSRyusuke Konishi } 73705fe58fdSRyusuke Konishi 73805fe58fdSRyusuke Konishi int nilfs_mark_inode_dirty(struct inode *inode) 73905fe58fdSRyusuke Konishi { 74005fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 74105fe58fdSRyusuke Konishi struct buffer_head *ibh; 74205fe58fdSRyusuke Konishi int err; 74305fe58fdSRyusuke Konishi 74405fe58fdSRyusuke Konishi err = nilfs_load_inode_block(sbi, inode, &ibh); 74505fe58fdSRyusuke Konishi if (unlikely(err)) { 74605fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 74705fe58fdSRyusuke Konishi "failed to reget inode block.\n"); 74805fe58fdSRyusuke Konishi return err; 74905fe58fdSRyusuke Konishi } 75005fe58fdSRyusuke Konishi nilfs_update_inode(inode, ibh); 75105fe58fdSRyusuke Konishi nilfs_mdt_mark_buffer_dirty(ibh); 75205fe58fdSRyusuke Konishi nilfs_mdt_mark_dirty(sbi->s_ifile); 75305fe58fdSRyusuke Konishi brelse(ibh); 75405fe58fdSRyusuke Konishi return 0; 75505fe58fdSRyusuke Konishi } 75605fe58fdSRyusuke Konishi 75705fe58fdSRyusuke Konishi /** 75805fe58fdSRyusuke Konishi * nilfs_dirty_inode - reflect changes on given inode to an inode block. 75905fe58fdSRyusuke Konishi * @inode: inode of the file to be registered. 76005fe58fdSRyusuke Konishi * 76105fe58fdSRyusuke Konishi * nilfs_dirty_inode() loads a inode block containing the specified 76205fe58fdSRyusuke Konishi * @inode and copies data from a nilfs_inode to a corresponding inode 76305fe58fdSRyusuke Konishi * entry in the inode block. This operation is excluded from the segment 76405fe58fdSRyusuke Konishi * construction. This function can be called both as a single operation 76505fe58fdSRyusuke Konishi * and as a part of indivisible file operations. 76605fe58fdSRyusuke Konishi */ 76705fe58fdSRyusuke Konishi void nilfs_dirty_inode(struct inode *inode) 76805fe58fdSRyusuke Konishi { 76905fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 77005fe58fdSRyusuke Konishi 77105fe58fdSRyusuke Konishi if (is_bad_inode(inode)) { 77205fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 77305fe58fdSRyusuke Konishi "tried to mark bad_inode dirty. ignored.\n"); 77405fe58fdSRyusuke Konishi dump_stack(); 77505fe58fdSRyusuke Konishi return; 77605fe58fdSRyusuke Konishi } 77705fe58fdSRyusuke Konishi nilfs_transaction_begin(inode->i_sb, &ti, 0); 77805fe58fdSRyusuke Konishi nilfs_mark_inode_dirty(inode); 77947420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 78005fe58fdSRyusuke Konishi } 781