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