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 370e14a359SRyusuke Konishi struct nilfs_iget_args { 380e14a359SRyusuke Konishi u64 ino; 390e14a359SRyusuke Konishi __u64 cno; 404d8d9293SRyusuke Konishi struct nilfs_root *root; 410e14a359SRyusuke Konishi int for_gc; 420e14a359SRyusuke Konishi }; 4305fe58fdSRyusuke Konishi 4405fe58fdSRyusuke Konishi /** 4505fe58fdSRyusuke Konishi * nilfs_get_block() - get a file block on the filesystem (callback function) 4605fe58fdSRyusuke Konishi * @inode - inode struct of the target file 4705fe58fdSRyusuke Konishi * @blkoff - file block number 4805fe58fdSRyusuke Konishi * @bh_result - buffer head to be mapped on 4905fe58fdSRyusuke Konishi * @create - indicate whether allocating the block or not when it has not 5005fe58fdSRyusuke Konishi * been allocated yet. 5105fe58fdSRyusuke Konishi * 5205fe58fdSRyusuke Konishi * This function does not issue actual read request of the specified data 5305fe58fdSRyusuke Konishi * block. It is done by VFS. 5405fe58fdSRyusuke Konishi */ 5505fe58fdSRyusuke Konishi int nilfs_get_block(struct inode *inode, sector_t blkoff, 5605fe58fdSRyusuke Konishi struct buffer_head *bh_result, int create) 5705fe58fdSRyusuke Konishi { 5805fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 59c3a7abf0SRyusuke Konishi __u64 blknum = 0; 6005fe58fdSRyusuke Konishi int err = 0, ret; 6105fe58fdSRyusuke Konishi struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode)); 62c3a7abf0SRyusuke Konishi unsigned maxblocks = bh_result->b_size >> inode->i_blkbits; 6305fe58fdSRyusuke Konishi 64c3a7abf0SRyusuke Konishi down_read(&NILFS_MDT(dat)->mi_sem); 65c3a7abf0SRyusuke Konishi ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); 66c3a7abf0SRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); 67c3a7abf0SRyusuke Konishi if (ret >= 0) { /* found */ 6805fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, blknum); 69c3a7abf0SRyusuke Konishi if (ret > 0) 70c3a7abf0SRyusuke Konishi bh_result->b_size = (ret << inode->i_blkbits); 7105fe58fdSRyusuke Konishi goto out; 7205fe58fdSRyusuke Konishi } 7305fe58fdSRyusuke Konishi /* data block was not found */ 7405fe58fdSRyusuke Konishi if (ret == -ENOENT && create) { 7505fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 7605fe58fdSRyusuke Konishi 7705fe58fdSRyusuke Konishi bh_result->b_blocknr = 0; 7805fe58fdSRyusuke Konishi err = nilfs_transaction_begin(inode->i_sb, &ti, 1); 7905fe58fdSRyusuke Konishi if (unlikely(err)) 8005fe58fdSRyusuke Konishi goto out; 8105fe58fdSRyusuke Konishi err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, 8205fe58fdSRyusuke Konishi (unsigned long)bh_result); 8305fe58fdSRyusuke Konishi if (unlikely(err != 0)) { 8405fe58fdSRyusuke Konishi if (err == -EEXIST) { 8505fe58fdSRyusuke Konishi /* 8605fe58fdSRyusuke Konishi * The get_block() function could be called 8705fe58fdSRyusuke Konishi * from multiple callers for an inode. 8805fe58fdSRyusuke Konishi * However, the page having this block must 8905fe58fdSRyusuke Konishi * be locked in this case. 9005fe58fdSRyusuke Konishi */ 911f5abe7eSRyusuke Konishi printk(KERN_WARNING 9205fe58fdSRyusuke Konishi "nilfs_get_block: a race condition " 9305fe58fdSRyusuke Konishi "while inserting a data block. " 9405fe58fdSRyusuke Konishi "(inode number=%lu, file block " 9505fe58fdSRyusuke Konishi "offset=%llu)\n", 9605fe58fdSRyusuke Konishi inode->i_ino, 9705fe58fdSRyusuke Konishi (unsigned long long)blkoff); 981f5abe7eSRyusuke Konishi err = 0; 9905fe58fdSRyusuke Konishi } else if (err == -EINVAL) { 10005fe58fdSRyusuke Konishi nilfs_error(inode->i_sb, __func__, 10105fe58fdSRyusuke Konishi "broken bmap (inode=%lu)\n", 10205fe58fdSRyusuke Konishi inode->i_ino); 10305fe58fdSRyusuke Konishi err = -EIO; 10405fe58fdSRyusuke Konishi } 10547420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 10605fe58fdSRyusuke Konishi goto out; 10705fe58fdSRyusuke Konishi } 108abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 10947420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 11005fe58fdSRyusuke Konishi /* Error handling should be detailed */ 11105fe58fdSRyusuke Konishi set_buffer_new(bh_result); 11205fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed 11305fe58fdSRyusuke Konishi to proper value */ 11405fe58fdSRyusuke Konishi } else if (ret == -ENOENT) { 11505fe58fdSRyusuke Konishi /* not found is not error (e.g. hole); must return without 11605fe58fdSRyusuke Konishi the mapped state flag. */ 11705fe58fdSRyusuke Konishi ; 11805fe58fdSRyusuke Konishi } else { 11905fe58fdSRyusuke Konishi err = ret; 12005fe58fdSRyusuke Konishi } 12105fe58fdSRyusuke Konishi 12205fe58fdSRyusuke Konishi out: 12305fe58fdSRyusuke Konishi return err; 12405fe58fdSRyusuke Konishi } 12505fe58fdSRyusuke Konishi 12605fe58fdSRyusuke Konishi /** 12705fe58fdSRyusuke Konishi * nilfs_readpage() - implement readpage() method of nilfs_aops {} 12805fe58fdSRyusuke Konishi * address_space_operations. 12905fe58fdSRyusuke Konishi * @file - file struct of the file to be read 13005fe58fdSRyusuke Konishi * @page - the page to be read 13105fe58fdSRyusuke Konishi */ 13205fe58fdSRyusuke Konishi static int nilfs_readpage(struct file *file, struct page *page) 13305fe58fdSRyusuke Konishi { 13405fe58fdSRyusuke Konishi return mpage_readpage(page, nilfs_get_block); 13505fe58fdSRyusuke Konishi } 13605fe58fdSRyusuke Konishi 13705fe58fdSRyusuke Konishi /** 13805fe58fdSRyusuke Konishi * nilfs_readpages() - implement readpages() method of nilfs_aops {} 13905fe58fdSRyusuke Konishi * address_space_operations. 14005fe58fdSRyusuke Konishi * @file - file struct of the file to be read 14105fe58fdSRyusuke Konishi * @mapping - address_space struct used for reading multiple pages 14205fe58fdSRyusuke Konishi * @pages - the pages to be read 14305fe58fdSRyusuke Konishi * @nr_pages - number of pages to be read 14405fe58fdSRyusuke Konishi */ 14505fe58fdSRyusuke Konishi static int nilfs_readpages(struct file *file, struct address_space *mapping, 14605fe58fdSRyusuke Konishi struct list_head *pages, unsigned nr_pages) 14705fe58fdSRyusuke Konishi { 14805fe58fdSRyusuke Konishi return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); 14905fe58fdSRyusuke Konishi } 15005fe58fdSRyusuke Konishi 15105fe58fdSRyusuke Konishi static int nilfs_writepages(struct address_space *mapping, 15205fe58fdSRyusuke Konishi struct writeback_control *wbc) 15305fe58fdSRyusuke Konishi { 154f30bf3e4SRyusuke Konishi struct inode *inode = mapping->host; 155f30bf3e4SRyusuke Konishi int err = 0; 156f30bf3e4SRyusuke Konishi 157f30bf3e4SRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) 158f30bf3e4SRyusuke Konishi err = nilfs_construct_dsync_segment(inode->i_sb, inode, 159f30bf3e4SRyusuke Konishi wbc->range_start, 160f30bf3e4SRyusuke Konishi wbc->range_end); 161f30bf3e4SRyusuke Konishi return err; 16205fe58fdSRyusuke Konishi } 16305fe58fdSRyusuke Konishi 16405fe58fdSRyusuke Konishi static int nilfs_writepage(struct page *page, struct writeback_control *wbc) 16505fe58fdSRyusuke Konishi { 16605fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 16705fe58fdSRyusuke Konishi int err; 16805fe58fdSRyusuke Konishi 16905fe58fdSRyusuke Konishi redirty_page_for_writepage(wbc, page); 17005fe58fdSRyusuke Konishi unlock_page(page); 17105fe58fdSRyusuke Konishi 17205fe58fdSRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) { 17305fe58fdSRyusuke Konishi err = nilfs_construct_segment(inode->i_sb); 17405fe58fdSRyusuke Konishi if (unlikely(err)) 17505fe58fdSRyusuke Konishi return err; 17605fe58fdSRyusuke Konishi } else if (wbc->for_reclaim) 17705fe58fdSRyusuke Konishi nilfs_flush_segment(inode->i_sb, inode->i_ino); 17805fe58fdSRyusuke Konishi 17905fe58fdSRyusuke Konishi return 0; 18005fe58fdSRyusuke Konishi } 18105fe58fdSRyusuke Konishi 18205fe58fdSRyusuke Konishi static int nilfs_set_page_dirty(struct page *page) 18305fe58fdSRyusuke Konishi { 18405fe58fdSRyusuke Konishi int ret = __set_page_dirty_buffers(page); 18505fe58fdSRyusuke Konishi 18605fe58fdSRyusuke Konishi if (ret) { 18705fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 18805fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 18905fe58fdSRyusuke Konishi unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); 19005fe58fdSRyusuke Konishi 19105fe58fdSRyusuke Konishi nilfs_set_file_dirty(sbi, inode, nr_dirty); 19205fe58fdSRyusuke Konishi } 19305fe58fdSRyusuke Konishi return ret; 19405fe58fdSRyusuke Konishi } 19505fe58fdSRyusuke Konishi 19605fe58fdSRyusuke Konishi static int nilfs_write_begin(struct file *file, struct address_space *mapping, 19705fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned flags, 19805fe58fdSRyusuke Konishi struct page **pagep, void **fsdata) 19905fe58fdSRyusuke Konishi 20005fe58fdSRyusuke Konishi { 20105fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 20205fe58fdSRyusuke Konishi int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); 20305fe58fdSRyusuke Konishi 20405fe58fdSRyusuke Konishi if (unlikely(err)) 20505fe58fdSRyusuke Konishi return err; 20605fe58fdSRyusuke Konishi 207155130a4SChristoph Hellwig err = block_write_begin(mapping, pos, len, flags, pagep, 208155130a4SChristoph Hellwig nilfs_get_block); 209155130a4SChristoph Hellwig if (unlikely(err)) { 210155130a4SChristoph Hellwig loff_t isize = mapping->host->i_size; 211155130a4SChristoph Hellwig if (pos + len > isize) 212155130a4SChristoph Hellwig vmtruncate(mapping->host, isize); 213155130a4SChristoph Hellwig 21447420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 215155130a4SChristoph Hellwig } 21605fe58fdSRyusuke Konishi return err; 21705fe58fdSRyusuke Konishi } 21805fe58fdSRyusuke Konishi 21905fe58fdSRyusuke Konishi static int nilfs_write_end(struct file *file, struct address_space *mapping, 22005fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned copied, 22105fe58fdSRyusuke Konishi struct page *page, void *fsdata) 22205fe58fdSRyusuke Konishi { 22305fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 22405fe58fdSRyusuke Konishi unsigned start = pos & (PAGE_CACHE_SIZE - 1); 22505fe58fdSRyusuke Konishi unsigned nr_dirty; 22605fe58fdSRyusuke Konishi int err; 22705fe58fdSRyusuke Konishi 22805fe58fdSRyusuke Konishi nr_dirty = nilfs_page_count_clean_buffers(page, start, 22905fe58fdSRyusuke Konishi start + copied); 23005fe58fdSRyusuke Konishi copied = generic_write_end(file, mapping, pos, len, copied, page, 23105fe58fdSRyusuke Konishi fsdata); 23205fe58fdSRyusuke Konishi nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty); 23347420c79SRyusuke Konishi err = nilfs_transaction_commit(inode->i_sb); 23405fe58fdSRyusuke Konishi return err ? : copied; 23505fe58fdSRyusuke Konishi } 23605fe58fdSRyusuke Konishi 23705fe58fdSRyusuke Konishi static ssize_t 23805fe58fdSRyusuke Konishi nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, 23905fe58fdSRyusuke Konishi loff_t offset, unsigned long nr_segs) 24005fe58fdSRyusuke Konishi { 24105fe58fdSRyusuke Konishi struct file *file = iocb->ki_filp; 24205fe58fdSRyusuke Konishi struct inode *inode = file->f_mapping->host; 24305fe58fdSRyusuke Konishi ssize_t size; 24405fe58fdSRyusuke Konishi 24505fe58fdSRyusuke Konishi if (rw == WRITE) 24605fe58fdSRyusuke Konishi return 0; 24705fe58fdSRyusuke Konishi 24805fe58fdSRyusuke Konishi /* Needs synchronization with the cleaner */ 24905fe58fdSRyusuke Konishi size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, 25005fe58fdSRyusuke Konishi offset, nr_segs, nilfs_get_block, NULL); 251eafdc7d1SChristoph Hellwig 252eafdc7d1SChristoph Hellwig /* 253eafdc7d1SChristoph Hellwig * In case of error extending write may have instantiated a few 254eafdc7d1SChristoph Hellwig * blocks outside i_size. Trim these off again. 255eafdc7d1SChristoph Hellwig */ 256eafdc7d1SChristoph Hellwig if (unlikely((rw & WRITE) && size < 0)) { 257eafdc7d1SChristoph Hellwig loff_t isize = i_size_read(inode); 258eafdc7d1SChristoph Hellwig loff_t end = offset + iov_length(iov, nr_segs); 259eafdc7d1SChristoph Hellwig 260eafdc7d1SChristoph Hellwig if (end > isize) 261eafdc7d1SChristoph Hellwig vmtruncate(inode, isize); 262eafdc7d1SChristoph Hellwig } 263eafdc7d1SChristoph Hellwig 26405fe58fdSRyusuke Konishi return size; 26505fe58fdSRyusuke Konishi } 26605fe58fdSRyusuke Konishi 2677f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = { 26805fe58fdSRyusuke Konishi .writepage = nilfs_writepage, 26905fe58fdSRyusuke Konishi .readpage = nilfs_readpage, 270e85dc1d5SRyusuke Konishi .sync_page = block_sync_page, 27105fe58fdSRyusuke Konishi .writepages = nilfs_writepages, 27205fe58fdSRyusuke Konishi .set_page_dirty = nilfs_set_page_dirty, 27305fe58fdSRyusuke Konishi .readpages = nilfs_readpages, 27405fe58fdSRyusuke Konishi .write_begin = nilfs_write_begin, 27505fe58fdSRyusuke Konishi .write_end = nilfs_write_end, 27605fe58fdSRyusuke Konishi /* .releasepage = nilfs_releasepage, */ 27705fe58fdSRyusuke Konishi .invalidatepage = block_invalidatepage, 27805fe58fdSRyusuke Konishi .direct_IO = nilfs_direct_IO, 279258ef67eSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 28005fe58fdSRyusuke Konishi }; 28105fe58fdSRyusuke Konishi 28205fe58fdSRyusuke Konishi struct inode *nilfs_new_inode(struct inode *dir, int mode) 28305fe58fdSRyusuke Konishi { 28405fe58fdSRyusuke Konishi struct super_block *sb = dir->i_sb; 28505fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 28605fe58fdSRyusuke Konishi struct inode *inode; 28705fe58fdSRyusuke Konishi struct nilfs_inode_info *ii; 2884d8d9293SRyusuke Konishi struct nilfs_root *root; 28905fe58fdSRyusuke Konishi int err = -ENOMEM; 29005fe58fdSRyusuke Konishi ino_t ino; 29105fe58fdSRyusuke Konishi 29205fe58fdSRyusuke Konishi inode = new_inode(sb); 29305fe58fdSRyusuke Konishi if (unlikely(!inode)) 29405fe58fdSRyusuke Konishi goto failed; 29505fe58fdSRyusuke Konishi 29605fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 29705fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 29805fe58fdSRyusuke Konishi 2994d8d9293SRyusuke Konishi root = NILFS_I(dir)->i_root; 30005fe58fdSRyusuke Konishi ii = NILFS_I(inode); 30105fe58fdSRyusuke Konishi ii->i_state = 1 << NILFS_I_NEW; 3024d8d9293SRyusuke Konishi ii->i_root = root; 30305fe58fdSRyusuke Konishi 304e912a5b6SRyusuke Konishi err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); 30505fe58fdSRyusuke Konishi if (unlikely(err)) 30605fe58fdSRyusuke Konishi goto failed_ifile_create_inode; 30705fe58fdSRyusuke Konishi /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 30805fe58fdSRyusuke Konishi 309b7c06342SRyusuke Konishi atomic_inc(&root->inodes_count); 31073459dccSDmitry Monakhov inode_init_owner(inode, dir, mode); 31105fe58fdSRyusuke Konishi inode->i_ino = ino; 31205fe58fdSRyusuke Konishi inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 31305fe58fdSRyusuke Konishi 31405fe58fdSRyusuke Konishi if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 31505fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, NULL); 31605fe58fdSRyusuke Konishi if (err < 0) 31705fe58fdSRyusuke Konishi goto failed_bmap; 31805fe58fdSRyusuke Konishi 31905fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 32005fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 32105fe58fdSRyusuke Konishi } 32205fe58fdSRyusuke Konishi 32305fe58fdSRyusuke Konishi ii->i_flags = NILFS_I(dir)->i_flags; 32405fe58fdSRyusuke Konishi if (S_ISLNK(mode)) 32505fe58fdSRyusuke Konishi ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL); 32605fe58fdSRyusuke Konishi if (!S_ISDIR(mode)) 32705fe58fdSRyusuke Konishi ii->i_flags &= ~NILFS_DIRSYNC_FL; 32805fe58fdSRyusuke Konishi 32905fe58fdSRyusuke Konishi /* ii->i_file_acl = 0; */ 33005fe58fdSRyusuke Konishi /* ii->i_dir_acl = 0; */ 33105fe58fdSRyusuke Konishi ii->i_dir_start_lookup = 0; 33205fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 33305fe58fdSRyusuke Konishi spin_lock(&sbi->s_next_gen_lock); 33405fe58fdSRyusuke Konishi inode->i_generation = sbi->s_next_generation++; 33505fe58fdSRyusuke Konishi spin_unlock(&sbi->s_next_gen_lock); 33605fe58fdSRyusuke Konishi insert_inode_hash(inode); 33705fe58fdSRyusuke Konishi 33805fe58fdSRyusuke Konishi err = nilfs_init_acl(inode, dir); 33905fe58fdSRyusuke Konishi if (unlikely(err)) 34005fe58fdSRyusuke Konishi goto failed_acl; /* never occur. When supporting 34105fe58fdSRyusuke Konishi nilfs_init_acl(), proper cancellation of 34205fe58fdSRyusuke Konishi above jobs should be considered */ 34305fe58fdSRyusuke Konishi 34405fe58fdSRyusuke Konishi return inode; 34505fe58fdSRyusuke Konishi 34605fe58fdSRyusuke Konishi failed_acl: 34705fe58fdSRyusuke Konishi failed_bmap: 34805fe58fdSRyusuke Konishi inode->i_nlink = 0; 34905fe58fdSRyusuke Konishi iput(inode); /* raw_inode will be deleted through 35005fe58fdSRyusuke Konishi generic_delete_inode() */ 35105fe58fdSRyusuke Konishi goto failed; 35205fe58fdSRyusuke Konishi 35305fe58fdSRyusuke Konishi failed_ifile_create_inode: 35405fe58fdSRyusuke Konishi make_bad_inode(inode); 35505fe58fdSRyusuke Konishi iput(inode); /* if i_nlink == 1, generic_forget_inode() will be 35605fe58fdSRyusuke Konishi called */ 35705fe58fdSRyusuke Konishi failed: 35805fe58fdSRyusuke Konishi return ERR_PTR(err); 35905fe58fdSRyusuke Konishi } 36005fe58fdSRyusuke Konishi 36105fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode) 36205fe58fdSRyusuke Konishi { 36305fe58fdSRyusuke Konishi unsigned int flags = NILFS_I(inode)->i_flags; 36405fe58fdSRyusuke Konishi 36505fe58fdSRyusuke Konishi inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | 36605fe58fdSRyusuke Konishi S_DIRSYNC); 36705fe58fdSRyusuke Konishi if (flags & NILFS_SYNC_FL) 36805fe58fdSRyusuke Konishi inode->i_flags |= S_SYNC; 36905fe58fdSRyusuke Konishi if (flags & NILFS_APPEND_FL) 37005fe58fdSRyusuke Konishi inode->i_flags |= S_APPEND; 37105fe58fdSRyusuke Konishi if (flags & NILFS_IMMUTABLE_FL) 37205fe58fdSRyusuke Konishi inode->i_flags |= S_IMMUTABLE; 37305fe58fdSRyusuke Konishi #ifndef NILFS_ATIME_DISABLE 37405fe58fdSRyusuke Konishi if (flags & NILFS_NOATIME_FL) 37505fe58fdSRyusuke Konishi #endif 37605fe58fdSRyusuke Konishi inode->i_flags |= S_NOATIME; 37705fe58fdSRyusuke Konishi if (flags & NILFS_DIRSYNC_FL) 37805fe58fdSRyusuke Konishi inode->i_flags |= S_DIRSYNC; 37905fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 38005fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 38105fe58fdSRyusuke Konishi } 38205fe58fdSRyusuke Konishi 38305fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode, 38405fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode) 38505fe58fdSRyusuke Konishi { 38605fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 38705fe58fdSRyusuke Konishi int err; 38805fe58fdSRyusuke Konishi 38905fe58fdSRyusuke Konishi inode->i_mode = le16_to_cpu(raw_inode->i_mode); 39005fe58fdSRyusuke Konishi inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid); 39105fe58fdSRyusuke Konishi inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid); 39205fe58fdSRyusuke Konishi inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 39305fe58fdSRyusuke Konishi inode->i_size = le64_to_cpu(raw_inode->i_size); 39405fe58fdSRyusuke Konishi inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 39505fe58fdSRyusuke Konishi inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); 39605fe58fdSRyusuke Konishi inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 39761239230SRyusuke Konishi inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 39861239230SRyusuke Konishi inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); 39961239230SRyusuke Konishi inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 40061239230SRyusuke Konishi if (inode->i_nlink == 0 && inode->i_mode == 0) 40105fe58fdSRyusuke Konishi return -EINVAL; /* this inode is deleted */ 40205fe58fdSRyusuke Konishi 40305fe58fdSRyusuke Konishi inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 40405fe58fdSRyusuke Konishi ii->i_flags = le32_to_cpu(raw_inode->i_flags); 40505fe58fdSRyusuke Konishi #if 0 40605fe58fdSRyusuke Konishi ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 40705fe58fdSRyusuke Konishi ii->i_dir_acl = S_ISREG(inode->i_mode) ? 40805fe58fdSRyusuke Konishi 0 : le32_to_cpu(raw_inode->i_dir_acl); 40905fe58fdSRyusuke Konishi #endif 4103cc811bfSRyusuke Konishi ii->i_dir_start_lookup = 0; 41105fe58fdSRyusuke Konishi inode->i_generation = le32_to_cpu(raw_inode->i_generation); 41205fe58fdSRyusuke Konishi 41305fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 41405fe58fdSRyusuke Konishi S_ISLNK(inode->i_mode)) { 41505fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, raw_inode); 41605fe58fdSRyusuke Konishi if (err < 0) 41705fe58fdSRyusuke Konishi return err; 41805fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 41905fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 42005fe58fdSRyusuke Konishi } 42105fe58fdSRyusuke Konishi return 0; 42205fe58fdSRyusuke Konishi } 42305fe58fdSRyusuke Konishi 424e912a5b6SRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb, 425e912a5b6SRyusuke Konishi struct nilfs_root *root, unsigned long ino, 42605fe58fdSRyusuke Konishi struct inode *inode) 42705fe58fdSRyusuke Konishi { 42805fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 42905fe58fdSRyusuke Konishi struct inode *dat = nilfs_dat_inode(sbi->s_nilfs); 43005fe58fdSRyusuke Konishi struct buffer_head *bh; 43105fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 43205fe58fdSRyusuke Konishi int err; 43305fe58fdSRyusuke Konishi 43405fe58fdSRyusuke Konishi down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 435e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); 43605fe58fdSRyusuke Konishi if (unlikely(err)) 43705fe58fdSRyusuke Konishi goto bad_inode; 43805fe58fdSRyusuke Konishi 439e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); 44005fe58fdSRyusuke Konishi 4411b2f5a64SRyusuke Konishi err = nilfs_read_inode_common(inode, raw_inode); 4421b2f5a64SRyusuke Konishi if (err) 44305fe58fdSRyusuke Konishi goto failed_unmap; 44405fe58fdSRyusuke Konishi 44505fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode)) { 44605fe58fdSRyusuke Konishi inode->i_op = &nilfs_file_inode_operations; 44705fe58fdSRyusuke Konishi inode->i_fop = &nilfs_file_operations; 44805fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 44905fe58fdSRyusuke Konishi } else if (S_ISDIR(inode->i_mode)) { 45005fe58fdSRyusuke Konishi inode->i_op = &nilfs_dir_inode_operations; 45105fe58fdSRyusuke Konishi inode->i_fop = &nilfs_dir_operations; 45205fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 45305fe58fdSRyusuke Konishi } else if (S_ISLNK(inode->i_mode)) { 45405fe58fdSRyusuke Konishi inode->i_op = &nilfs_symlink_inode_operations; 45505fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 45605fe58fdSRyusuke Konishi } else { 45705fe58fdSRyusuke Konishi inode->i_op = &nilfs_special_inode_operations; 45805fe58fdSRyusuke Konishi init_special_inode( 45905fe58fdSRyusuke Konishi inode, inode->i_mode, 460cdce214eSRyusuke Konishi huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 46105fe58fdSRyusuke Konishi } 462e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 46305fe58fdSRyusuke Konishi brelse(bh); 46405fe58fdSRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 46505fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 46605fe58fdSRyusuke Konishi return 0; 46705fe58fdSRyusuke Konishi 46805fe58fdSRyusuke Konishi failed_unmap: 469e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 47005fe58fdSRyusuke Konishi brelse(bh); 47105fe58fdSRyusuke Konishi 47205fe58fdSRyusuke Konishi bad_inode: 47305fe58fdSRyusuke Konishi up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 47405fe58fdSRyusuke Konishi return err; 47505fe58fdSRyusuke Konishi } 47605fe58fdSRyusuke Konishi 4770e14a359SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque) 4780e14a359SRyusuke Konishi { 4790e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 4800e14a359SRyusuke Konishi struct nilfs_inode_info *ii; 4810e14a359SRyusuke Konishi 4824d8d9293SRyusuke Konishi if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) 4830e14a359SRyusuke Konishi return 0; 4840e14a359SRyusuke Konishi 4850e14a359SRyusuke Konishi ii = NILFS_I(inode); 4860e14a359SRyusuke Konishi if (!test_bit(NILFS_I_GCINODE, &ii->i_state)) 4870e14a359SRyusuke Konishi return !args->for_gc; 4880e14a359SRyusuke Konishi 4890e14a359SRyusuke Konishi return args->for_gc && args->cno == ii->i_cno; 4900e14a359SRyusuke Konishi } 4910e14a359SRyusuke Konishi 4920e14a359SRyusuke Konishi static int nilfs_iget_set(struct inode *inode, void *opaque) 4930e14a359SRyusuke Konishi { 4940e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 4950e14a359SRyusuke Konishi 4960e14a359SRyusuke Konishi inode->i_ino = args->ino; 4970e14a359SRyusuke Konishi if (args->for_gc) { 4980e14a359SRyusuke Konishi NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE; 4990e14a359SRyusuke Konishi NILFS_I(inode)->i_cno = args->cno; 5004d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = NULL; 5014d8d9293SRyusuke Konishi } else { 5024d8d9293SRyusuke Konishi if (args->root && args->ino == NILFS_ROOT_INO) 5034d8d9293SRyusuke Konishi nilfs_get_root(args->root); 5044d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = args->root; 5050e14a359SRyusuke Konishi } 5060e14a359SRyusuke Konishi return 0; 5070e14a359SRyusuke Konishi } 5080e14a359SRyusuke Konishi 509*032dbb3bSRyusuke Konishi struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, 510*032dbb3bSRyusuke Konishi unsigned long ino) 511*032dbb3bSRyusuke Konishi { 512*032dbb3bSRyusuke Konishi struct nilfs_iget_args args = { 513*032dbb3bSRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 514*032dbb3bSRyusuke Konishi }; 515*032dbb3bSRyusuke Konishi 516*032dbb3bSRyusuke Konishi return ilookup5(sb, ino, nilfs_iget_test, &args); 517*032dbb3bSRyusuke Konishi } 518*032dbb3bSRyusuke Konishi 519f1e89c86SRyusuke Konishi struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, 5204d8d9293SRyusuke Konishi unsigned long ino) 52105fe58fdSRyusuke Konishi { 5224d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 5234d8d9293SRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 5244d8d9293SRyusuke Konishi }; 525f1e89c86SRyusuke Konishi 526f1e89c86SRyusuke Konishi return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 527f1e89c86SRyusuke Konishi } 528f1e89c86SRyusuke Konishi 529f1e89c86SRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, 530f1e89c86SRyusuke Konishi unsigned long ino) 531f1e89c86SRyusuke Konishi { 53205fe58fdSRyusuke Konishi struct inode *inode; 53305fe58fdSRyusuke Konishi int err; 53405fe58fdSRyusuke Konishi 535f1e89c86SRyusuke Konishi inode = nilfs_iget_locked(sb, root, ino); 53605fe58fdSRyusuke Konishi if (unlikely(!inode)) 53705fe58fdSRyusuke Konishi return ERR_PTR(-ENOMEM); 53805fe58fdSRyusuke Konishi if (!(inode->i_state & I_NEW)) 53905fe58fdSRyusuke Konishi return inode; 54005fe58fdSRyusuke Konishi 541e912a5b6SRyusuke Konishi err = __nilfs_read_inode(sb, root, ino, inode); 54205fe58fdSRyusuke Konishi if (unlikely(err)) { 54305fe58fdSRyusuke Konishi iget_failed(inode); 54405fe58fdSRyusuke Konishi return ERR_PTR(err); 54505fe58fdSRyusuke Konishi } 54605fe58fdSRyusuke Konishi unlock_new_inode(inode); 54705fe58fdSRyusuke Konishi return inode; 54805fe58fdSRyusuke Konishi } 54905fe58fdSRyusuke Konishi 550263d90ceSRyusuke Konishi struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino, 551263d90ceSRyusuke Konishi __u64 cno) 552263d90ceSRyusuke Konishi { 5534d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 5544d8d9293SRyusuke Konishi .ino = ino, .root = NULL, .cno = cno, .for_gc = 1 5554d8d9293SRyusuke Konishi }; 556263d90ceSRyusuke Konishi struct inode *inode; 557263d90ceSRyusuke Konishi int err; 558263d90ceSRyusuke Konishi 559263d90ceSRyusuke Konishi inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 560263d90ceSRyusuke Konishi if (unlikely(!inode)) 561263d90ceSRyusuke Konishi return ERR_PTR(-ENOMEM); 562263d90ceSRyusuke Konishi if (!(inode->i_state & I_NEW)) 563263d90ceSRyusuke Konishi return inode; 564263d90ceSRyusuke Konishi 565263d90ceSRyusuke Konishi err = nilfs_init_gcinode(inode); 566263d90ceSRyusuke Konishi if (unlikely(err)) { 567263d90ceSRyusuke Konishi iget_failed(inode); 568263d90ceSRyusuke Konishi return ERR_PTR(err); 569263d90ceSRyusuke Konishi } 570263d90ceSRyusuke Konishi unlock_new_inode(inode); 571263d90ceSRyusuke Konishi return inode; 572263d90ceSRyusuke Konishi } 573263d90ceSRyusuke Konishi 57405fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode, 57505fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode, int has_bmap) 57605fe58fdSRyusuke Konishi { 57705fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 57805fe58fdSRyusuke Konishi 57905fe58fdSRyusuke Konishi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 58005fe58fdSRyusuke Konishi raw_inode->i_uid = cpu_to_le32(inode->i_uid); 58105fe58fdSRyusuke Konishi raw_inode->i_gid = cpu_to_le32(inode->i_gid); 58205fe58fdSRyusuke Konishi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 58305fe58fdSRyusuke Konishi raw_inode->i_size = cpu_to_le64(inode->i_size); 58405fe58fdSRyusuke Konishi raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 58505fe58fdSRyusuke Konishi raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); 58661239230SRyusuke Konishi raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 58761239230SRyusuke Konishi raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); 58805fe58fdSRyusuke Konishi raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 58905fe58fdSRyusuke Konishi 59005fe58fdSRyusuke Konishi raw_inode->i_flags = cpu_to_le32(ii->i_flags); 59105fe58fdSRyusuke Konishi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 59205fe58fdSRyusuke Konishi 59305fe58fdSRyusuke Konishi if (has_bmap) 59405fe58fdSRyusuke Konishi nilfs_bmap_write(ii->i_bmap, raw_inode); 59505fe58fdSRyusuke Konishi else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 59605fe58fdSRyusuke Konishi raw_inode->i_device_code = 597cdce214eSRyusuke Konishi cpu_to_le64(huge_encode_dev(inode->i_rdev)); 59805fe58fdSRyusuke Konishi /* When extending inode, nilfs->ns_inode_size should be checked 59905fe58fdSRyusuke Konishi for substitutions of appended fields */ 60005fe58fdSRyusuke Konishi } 60105fe58fdSRyusuke Konishi 60205fe58fdSRyusuke Konishi void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh) 60305fe58fdSRyusuke Konishi { 60405fe58fdSRyusuke Konishi ino_t ino = inode->i_ino; 60505fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 606e912a5b6SRyusuke Konishi struct inode *ifile = ii->i_root->ifile; 60705fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 60805fe58fdSRyusuke Konishi 609e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); 61005fe58fdSRyusuke Konishi 61105fe58fdSRyusuke Konishi if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 612e912a5b6SRyusuke Konishi memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); 61305fe58fdSRyusuke Konishi set_bit(NILFS_I_INODE_DIRTY, &ii->i_state); 61405fe58fdSRyusuke Konishi 61505fe58fdSRyusuke Konishi nilfs_write_inode_common(inode, raw_inode, 0); 61605fe58fdSRyusuke Konishi /* XXX: call with has_bmap = 0 is a workaround to avoid 61705fe58fdSRyusuke Konishi deadlock of bmap. This delays update of i_bmap to just 61805fe58fdSRyusuke Konishi before writing */ 619e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(ifile, ino, ibh); 62005fe58fdSRyusuke Konishi } 62105fe58fdSRyusuke Konishi 62205fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 62305fe58fdSRyusuke Konishi 62405fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 62505fe58fdSRyusuke Konishi unsigned long from) 62605fe58fdSRyusuke Konishi { 62705fe58fdSRyusuke Konishi unsigned long b; 62805fe58fdSRyusuke Konishi int ret; 62905fe58fdSRyusuke Konishi 63005fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 63105fe58fdSRyusuke Konishi return; 63205fe58fdSRyusuke Konishi repeat: 63305fe58fdSRyusuke Konishi ret = nilfs_bmap_last_key(ii->i_bmap, &b); 63405fe58fdSRyusuke Konishi if (ret == -ENOENT) 63505fe58fdSRyusuke Konishi return; 63605fe58fdSRyusuke Konishi else if (ret < 0) 63705fe58fdSRyusuke Konishi goto failed; 63805fe58fdSRyusuke Konishi 63905fe58fdSRyusuke Konishi if (b < from) 64005fe58fdSRyusuke Konishi return; 64105fe58fdSRyusuke Konishi 64205fe58fdSRyusuke Konishi b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 64305fe58fdSRyusuke Konishi ret = nilfs_bmap_truncate(ii->i_bmap, b); 64405fe58fdSRyusuke Konishi nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 64505fe58fdSRyusuke Konishi if (!ret || (ret == -ENOMEM && 64605fe58fdSRyusuke Konishi nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 64705fe58fdSRyusuke Konishi goto repeat; 64805fe58fdSRyusuke Konishi 64905fe58fdSRyusuke Konishi failed: 65005fe58fdSRyusuke Konishi if (ret == -EINVAL) 65105fe58fdSRyusuke Konishi nilfs_error(ii->vfs_inode.i_sb, __func__, 65205fe58fdSRyusuke Konishi "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino); 65305fe58fdSRyusuke Konishi else 65405fe58fdSRyusuke Konishi nilfs_warning(ii->vfs_inode.i_sb, __func__, 65505fe58fdSRyusuke Konishi "failed to truncate bmap (ino=%lu, err=%d)", 65605fe58fdSRyusuke Konishi ii->vfs_inode.i_ino, ret); 65705fe58fdSRyusuke Konishi } 65805fe58fdSRyusuke Konishi 65905fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode) 66005fe58fdSRyusuke Konishi { 66105fe58fdSRyusuke Konishi unsigned long blkoff; 66205fe58fdSRyusuke Konishi unsigned int blocksize; 66305fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 66405fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 66505fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 66605fe58fdSRyusuke Konishi 66705fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 66805fe58fdSRyusuke Konishi return; 66905fe58fdSRyusuke Konishi if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 67005fe58fdSRyusuke Konishi return; 67105fe58fdSRyusuke Konishi 67205fe58fdSRyusuke Konishi blocksize = sb->s_blocksize; 67305fe58fdSRyusuke Konishi blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 6741f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 67505fe58fdSRyusuke Konishi 67605fe58fdSRyusuke Konishi block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 67705fe58fdSRyusuke Konishi 67805fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, blkoff); 67905fe58fdSRyusuke Konishi 68005fe58fdSRyusuke Konishi inode->i_mtime = inode->i_ctime = CURRENT_TIME; 68105fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 68205fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 68305fe58fdSRyusuke Konishi 684abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 68505fe58fdSRyusuke Konishi nilfs_set_file_dirty(NILFS_SB(sb), inode, 0); 68647420c79SRyusuke Konishi nilfs_transaction_commit(sb); 68705fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 68805fe58fdSRyusuke Konishi But truncate has no return value. */ 68905fe58fdSRyusuke Konishi } 69005fe58fdSRyusuke Konishi 6916fd1e5c9SAl Viro static void nilfs_clear_inode(struct inode *inode) 6926fd1e5c9SAl Viro { 6936fd1e5c9SAl Viro struct nilfs_inode_info *ii = NILFS_I(inode); 694518d1a6aSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 6956fd1e5c9SAl Viro 6966fd1e5c9SAl Viro /* 6976fd1e5c9SAl Viro * Free resources allocated in nilfs_read_inode(), here. 6986fd1e5c9SAl Viro */ 6996fd1e5c9SAl Viro BUG_ON(!list_empty(&ii->i_dirty)); 7006fd1e5c9SAl Viro brelse(ii->i_bh); 7016fd1e5c9SAl Viro ii->i_bh = NULL; 7026fd1e5c9SAl Viro 703518d1a6aSRyusuke Konishi if (mdi && mdi->mi_palloc_cache) 704518d1a6aSRyusuke Konishi nilfs_palloc_destroy_cache(inode); 705518d1a6aSRyusuke Konishi 7066fd1e5c9SAl Viro if (test_bit(NILFS_I_BMAP, &ii->i_state)) 7076fd1e5c9SAl Viro nilfs_bmap_clear(ii->i_bmap); 7086fd1e5c9SAl Viro 7096fd1e5c9SAl Viro nilfs_btnode_cache_clear(&ii->i_btnode_cache); 7104d8d9293SRyusuke Konishi 7114d8d9293SRyusuke Konishi if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) 7124d8d9293SRyusuke Konishi nilfs_put_root(ii->i_root); 7136fd1e5c9SAl Viro } 7146fd1e5c9SAl Viro 7156fd1e5c9SAl Viro void nilfs_evict_inode(struct inode *inode) 71605fe58fdSRyusuke Konishi { 71705fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 71805fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 71905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 72005fe58fdSRyusuke Konishi 7214d8d9293SRyusuke Konishi if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { 72205fe58fdSRyusuke Konishi if (inode->i_data.nrpages) 72305fe58fdSRyusuke Konishi truncate_inode_pages(&inode->i_data, 0); 7246fd1e5c9SAl Viro end_writeback(inode); 7256fd1e5c9SAl Viro nilfs_clear_inode(inode); 72605fe58fdSRyusuke Konishi return; 72705fe58fdSRyusuke Konishi } 7281f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 7291f5abe7eSRyusuke Konishi 73005fe58fdSRyusuke Konishi if (inode->i_data.nrpages) 73105fe58fdSRyusuke Konishi truncate_inode_pages(&inode->i_data, 0); 73205fe58fdSRyusuke Konishi 733e912a5b6SRyusuke Konishi /* TODO: some of the following operations may fail. */ 73405fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, 0); 735abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 7366fd1e5c9SAl Viro end_writeback(inode); 737e912a5b6SRyusuke Konishi 738e912a5b6SRyusuke Konishi nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); 739b7c06342SRyusuke Konishi atomic_dec(&ii->i_root->inodes_count); 740e912a5b6SRyusuke Konishi 7416fd1e5c9SAl Viro nilfs_clear_inode(inode); 742e912a5b6SRyusuke Konishi 74305fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 74405fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 74547420c79SRyusuke Konishi nilfs_transaction_commit(sb); 74605fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 74705fe58fdSRyusuke Konishi But delete_inode has no return value. */ 74805fe58fdSRyusuke Konishi } 74905fe58fdSRyusuke Konishi 75005fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) 75105fe58fdSRyusuke Konishi { 75205fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 75305fe58fdSRyusuke Konishi struct inode *inode = dentry->d_inode; 75405fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 75547420c79SRyusuke Konishi int err; 75605fe58fdSRyusuke Konishi 75705fe58fdSRyusuke Konishi err = inode_change_ok(inode, iattr); 75805fe58fdSRyusuke Konishi if (err) 75905fe58fdSRyusuke Konishi return err; 76005fe58fdSRyusuke Konishi 76105fe58fdSRyusuke Konishi err = nilfs_transaction_begin(sb, &ti, 0); 76205fe58fdSRyusuke Konishi if (unlikely(err)) 76305fe58fdSRyusuke Konishi return err; 76447420c79SRyusuke Konishi 7651025774cSChristoph Hellwig if ((iattr->ia_valid & ATTR_SIZE) && 7661025774cSChristoph Hellwig iattr->ia_size != i_size_read(inode)) { 7671025774cSChristoph Hellwig err = vmtruncate(inode, iattr->ia_size); 7681025774cSChristoph Hellwig if (unlikely(err)) 7691025774cSChristoph Hellwig goto out_err; 7701025774cSChristoph Hellwig } 7711025774cSChristoph Hellwig 7721025774cSChristoph Hellwig setattr_copy(inode, iattr); 7731025774cSChristoph Hellwig mark_inode_dirty(inode); 7741025774cSChristoph Hellwig 7751025774cSChristoph Hellwig if (iattr->ia_valid & ATTR_MODE) { 7761025774cSChristoph Hellwig err = nilfs_acl_chmod(inode); 7771025774cSChristoph Hellwig if (unlikely(err)) 7781025774cSChristoph Hellwig goto out_err; 7791025774cSChristoph Hellwig } 7801025774cSChristoph Hellwig 7811025774cSChristoph Hellwig return nilfs_transaction_commit(sb); 7821025774cSChristoph Hellwig 7831025774cSChristoph Hellwig out_err: 7841025774cSChristoph Hellwig nilfs_transaction_abort(sb); 78547420c79SRyusuke Konishi return err; 78605fe58fdSRyusuke Konishi } 78705fe58fdSRyusuke Konishi 788dc3d3b81SRyusuke Konishi int nilfs_permission(struct inode *inode, int mask) 789dc3d3b81SRyusuke Konishi { 790dc3d3b81SRyusuke Konishi struct nilfs_root *root = NILFS_I(inode)->i_root; 791dc3d3b81SRyusuke Konishi 792dc3d3b81SRyusuke Konishi if ((mask & MAY_WRITE) && root && 793dc3d3b81SRyusuke Konishi root->cno != NILFS_CPTREE_CURRENT_CNO) 794dc3d3b81SRyusuke Konishi return -EROFS; /* snapshot is not writable */ 795dc3d3b81SRyusuke Konishi 796dc3d3b81SRyusuke Konishi return generic_permission(inode, mask, NULL); 797dc3d3b81SRyusuke Konishi } 798dc3d3b81SRyusuke Konishi 79905fe58fdSRyusuke Konishi int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode, 80005fe58fdSRyusuke Konishi struct buffer_head **pbh) 80105fe58fdSRyusuke Konishi { 80205fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 80305fe58fdSRyusuke Konishi int err; 80405fe58fdSRyusuke Konishi 80505fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 80605fe58fdSRyusuke Konishi if (ii->i_bh == NULL) { 80705fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 808e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(ii->i_root->ifile, 809e912a5b6SRyusuke Konishi inode->i_ino, pbh); 81005fe58fdSRyusuke Konishi if (unlikely(err)) 81105fe58fdSRyusuke Konishi return err; 81205fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 81305fe58fdSRyusuke Konishi if (ii->i_bh == NULL) 81405fe58fdSRyusuke Konishi ii->i_bh = *pbh; 81505fe58fdSRyusuke Konishi else { 81605fe58fdSRyusuke Konishi brelse(*pbh); 81705fe58fdSRyusuke Konishi *pbh = ii->i_bh; 81805fe58fdSRyusuke Konishi } 81905fe58fdSRyusuke Konishi } else 82005fe58fdSRyusuke Konishi *pbh = ii->i_bh; 82105fe58fdSRyusuke Konishi 82205fe58fdSRyusuke Konishi get_bh(*pbh); 82305fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 82405fe58fdSRyusuke Konishi return 0; 82505fe58fdSRyusuke Konishi } 82605fe58fdSRyusuke Konishi 82705fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode) 82805fe58fdSRyusuke Konishi { 82905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 83005fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 83105fe58fdSRyusuke Konishi int ret = 0; 83205fe58fdSRyusuke Konishi 83305fe58fdSRyusuke Konishi if (!list_empty(&ii->i_dirty)) { 83405fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 83505fe58fdSRyusuke Konishi ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 83605fe58fdSRyusuke Konishi test_bit(NILFS_I_BUSY, &ii->i_state); 83705fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 83805fe58fdSRyusuke Konishi } 83905fe58fdSRyusuke Konishi return ret; 84005fe58fdSRyusuke Konishi } 84105fe58fdSRyusuke Konishi 84205fe58fdSRyusuke Konishi int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode, 84305fe58fdSRyusuke Konishi unsigned nr_dirty) 84405fe58fdSRyusuke Konishi { 84505fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 84605fe58fdSRyusuke Konishi 84705fe58fdSRyusuke Konishi atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks); 84805fe58fdSRyusuke Konishi 849458c5b08SRyusuke Konishi if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 85005fe58fdSRyusuke Konishi return 0; 85105fe58fdSRyusuke Konishi 85205fe58fdSRyusuke Konishi spin_lock(&sbi->s_inode_lock); 85305fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 85405fe58fdSRyusuke Konishi !test_bit(NILFS_I_BUSY, &ii->i_state)) { 85505fe58fdSRyusuke Konishi /* Because this routine may race with nilfs_dispose_list(), 85605fe58fdSRyusuke Konishi we have to check NILFS_I_QUEUED here, too. */ 85705fe58fdSRyusuke Konishi if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 85805fe58fdSRyusuke Konishi /* This will happen when somebody is freeing 85905fe58fdSRyusuke Konishi this inode. */ 86005fe58fdSRyusuke Konishi nilfs_warning(sbi->s_super, __func__, 86105fe58fdSRyusuke Konishi "cannot get inode (ino=%lu)\n", 86205fe58fdSRyusuke Konishi inode->i_ino); 86305fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 86405fe58fdSRyusuke Konishi return -EINVAL; /* NILFS_I_DIRTY may remain for 86505fe58fdSRyusuke Konishi freeing inode */ 86605fe58fdSRyusuke Konishi } 86705fe58fdSRyusuke Konishi list_del(&ii->i_dirty); 86805fe58fdSRyusuke Konishi list_add_tail(&ii->i_dirty, &sbi->s_dirty_files); 86905fe58fdSRyusuke Konishi set_bit(NILFS_I_QUEUED, &ii->i_state); 87005fe58fdSRyusuke Konishi } 87105fe58fdSRyusuke Konishi spin_unlock(&sbi->s_inode_lock); 87205fe58fdSRyusuke Konishi return 0; 87305fe58fdSRyusuke Konishi } 87405fe58fdSRyusuke Konishi 87505fe58fdSRyusuke Konishi int nilfs_mark_inode_dirty(struct inode *inode) 87605fe58fdSRyusuke Konishi { 87705fe58fdSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); 87805fe58fdSRyusuke Konishi struct buffer_head *ibh; 87905fe58fdSRyusuke Konishi int err; 88005fe58fdSRyusuke Konishi 88105fe58fdSRyusuke Konishi err = nilfs_load_inode_block(sbi, inode, &ibh); 88205fe58fdSRyusuke Konishi if (unlikely(err)) { 88305fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 88405fe58fdSRyusuke Konishi "failed to reget inode block.\n"); 88505fe58fdSRyusuke Konishi return err; 88605fe58fdSRyusuke Konishi } 88705fe58fdSRyusuke Konishi nilfs_update_inode(inode, ibh); 88805fe58fdSRyusuke Konishi nilfs_mdt_mark_buffer_dirty(ibh); 889e912a5b6SRyusuke Konishi nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); 89005fe58fdSRyusuke Konishi brelse(ibh); 89105fe58fdSRyusuke Konishi return 0; 89205fe58fdSRyusuke Konishi } 89305fe58fdSRyusuke Konishi 89405fe58fdSRyusuke Konishi /** 89505fe58fdSRyusuke Konishi * nilfs_dirty_inode - reflect changes on given inode to an inode block. 89605fe58fdSRyusuke Konishi * @inode: inode of the file to be registered. 89705fe58fdSRyusuke Konishi * 89805fe58fdSRyusuke Konishi * nilfs_dirty_inode() loads a inode block containing the specified 89905fe58fdSRyusuke Konishi * @inode and copies data from a nilfs_inode to a corresponding inode 90005fe58fdSRyusuke Konishi * entry in the inode block. This operation is excluded from the segment 90105fe58fdSRyusuke Konishi * construction. This function can be called both as a single operation 90205fe58fdSRyusuke Konishi * and as a part of indivisible file operations. 90305fe58fdSRyusuke Konishi */ 90405fe58fdSRyusuke Konishi void nilfs_dirty_inode(struct inode *inode) 90505fe58fdSRyusuke Konishi { 90605fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 9077d6cd92fSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 90805fe58fdSRyusuke Konishi 90905fe58fdSRyusuke Konishi if (is_bad_inode(inode)) { 91005fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 91105fe58fdSRyusuke Konishi "tried to mark bad_inode dirty. ignored.\n"); 91205fe58fdSRyusuke Konishi dump_stack(); 91305fe58fdSRyusuke Konishi return; 91405fe58fdSRyusuke Konishi } 9157d6cd92fSRyusuke Konishi if (mdi) { 9167d6cd92fSRyusuke Konishi nilfs_mdt_mark_dirty(inode); 9177d6cd92fSRyusuke Konishi return; 9187d6cd92fSRyusuke Konishi } 91905fe58fdSRyusuke Konishi nilfs_transaction_begin(inode->i_sb, &ti, 0); 92005fe58fdSRyusuke Konishi nilfs_mark_inode_dirty(inode); 92147420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 92205fe58fdSRyusuke Konishi } 923