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> 2756d7acc7SAndreas Rohner #include <linux/pagemap.h> 2805fe58fdSRyusuke Konishi #include <linux/writeback.h> 29e2e40f2cSChristoph Hellwig #include <linux/uio.h> 3005fe58fdSRyusuke Konishi #include "nilfs.h" 316fd1e5c9SAl Viro #include "btnode.h" 3205fe58fdSRyusuke Konishi #include "segment.h" 3305fe58fdSRyusuke Konishi #include "page.h" 3405fe58fdSRyusuke Konishi #include "mdt.h" 3505fe58fdSRyusuke Konishi #include "cpfile.h" 3605fe58fdSRyusuke Konishi #include "ifile.h" 3705fe58fdSRyusuke Konishi 38f5974c8fSVyacheslav Dubeyko /** 39f5974c8fSVyacheslav Dubeyko * struct nilfs_iget_args - arguments used during comparison between inodes 40f5974c8fSVyacheslav Dubeyko * @ino: inode number 41f5974c8fSVyacheslav Dubeyko * @cno: checkpoint number 42f5974c8fSVyacheslav Dubeyko * @root: pointer on NILFS root object (mounted checkpoint) 43f5974c8fSVyacheslav Dubeyko * @for_gc: inode for GC flag 44f5974c8fSVyacheslav Dubeyko */ 450e14a359SRyusuke Konishi struct nilfs_iget_args { 460e14a359SRyusuke Konishi u64 ino; 470e14a359SRyusuke Konishi __u64 cno; 484d8d9293SRyusuke Konishi struct nilfs_root *root; 490e14a359SRyusuke Konishi int for_gc; 500e14a359SRyusuke Konishi }; 5105fe58fdSRyusuke Konishi 52705304a8SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque); 53705304a8SRyusuke Konishi 54be667377SRyusuke Konishi void nilfs_inode_add_blocks(struct inode *inode, int n) 55be667377SRyusuke Konishi { 56be667377SRyusuke Konishi struct nilfs_root *root = NILFS_I(inode)->i_root; 57be667377SRyusuke Konishi 58be667377SRyusuke Konishi inode_add_bytes(inode, (1 << inode->i_blkbits) * n); 59be667377SRyusuke Konishi if (root) 60e5f7f848SVyacheslav Dubeyko atomic64_add(n, &root->blocks_count); 61be667377SRyusuke Konishi } 62be667377SRyusuke Konishi 63be667377SRyusuke Konishi void nilfs_inode_sub_blocks(struct inode *inode, int n) 64be667377SRyusuke Konishi { 65be667377SRyusuke Konishi struct nilfs_root *root = NILFS_I(inode)->i_root; 66be667377SRyusuke Konishi 67be667377SRyusuke Konishi inode_sub_bytes(inode, (1 << inode->i_blkbits) * n); 68be667377SRyusuke Konishi if (root) 69e5f7f848SVyacheslav Dubeyko atomic64_sub(n, &root->blocks_count); 70be667377SRyusuke Konishi } 71be667377SRyusuke Konishi 7205fe58fdSRyusuke Konishi /** 7305fe58fdSRyusuke Konishi * nilfs_get_block() - get a file block on the filesystem (callback function) 7405fe58fdSRyusuke Konishi * @inode - inode struct of the target file 7505fe58fdSRyusuke Konishi * @blkoff - file block number 7605fe58fdSRyusuke Konishi * @bh_result - buffer head to be mapped on 7705fe58fdSRyusuke Konishi * @create - indicate whether allocating the block or not when it has not 7805fe58fdSRyusuke Konishi * been allocated yet. 7905fe58fdSRyusuke Konishi * 8005fe58fdSRyusuke Konishi * This function does not issue actual read request of the specified data 8105fe58fdSRyusuke Konishi * block. It is done by VFS. 8205fe58fdSRyusuke Konishi */ 8305fe58fdSRyusuke Konishi int nilfs_get_block(struct inode *inode, sector_t blkoff, 8405fe58fdSRyusuke Konishi struct buffer_head *bh_result, int create) 8505fe58fdSRyusuke Konishi { 8605fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 870ef28f9aSRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 88c3a7abf0SRyusuke Konishi __u64 blknum = 0; 8905fe58fdSRyusuke Konishi int err = 0, ret; 90c3a7abf0SRyusuke Konishi unsigned maxblocks = bh_result->b_size >> inode->i_blkbits; 9105fe58fdSRyusuke Konishi 920ef28f9aSRyusuke Konishi down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 93c3a7abf0SRyusuke Konishi ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); 940ef28f9aSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 95c3a7abf0SRyusuke Konishi if (ret >= 0) { /* found */ 9605fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, blknum); 97c3a7abf0SRyusuke Konishi if (ret > 0) 98c3a7abf0SRyusuke Konishi bh_result->b_size = (ret << inode->i_blkbits); 9905fe58fdSRyusuke Konishi goto out; 10005fe58fdSRyusuke Konishi } 10105fe58fdSRyusuke Konishi /* data block was not found */ 10205fe58fdSRyusuke Konishi if (ret == -ENOENT && create) { 10305fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 10405fe58fdSRyusuke Konishi 10505fe58fdSRyusuke Konishi bh_result->b_blocknr = 0; 10605fe58fdSRyusuke Konishi err = nilfs_transaction_begin(inode->i_sb, &ti, 1); 10705fe58fdSRyusuke Konishi if (unlikely(err)) 10805fe58fdSRyusuke Konishi goto out; 10905fe58fdSRyusuke Konishi err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, 11005fe58fdSRyusuke Konishi (unsigned long)bh_result); 11105fe58fdSRyusuke Konishi if (unlikely(err != 0)) { 11205fe58fdSRyusuke Konishi if (err == -EEXIST) { 11305fe58fdSRyusuke Konishi /* 11405fe58fdSRyusuke Konishi * The get_block() function could be called 11505fe58fdSRyusuke Konishi * from multiple callers for an inode. 11605fe58fdSRyusuke Konishi * However, the page having this block must 11705fe58fdSRyusuke Konishi * be locked in this case. 11805fe58fdSRyusuke Konishi */ 1191f5abe7eSRyusuke Konishi printk(KERN_WARNING 12005fe58fdSRyusuke Konishi "nilfs_get_block: a race condition " 12105fe58fdSRyusuke Konishi "while inserting a data block. " 12205fe58fdSRyusuke Konishi "(inode number=%lu, file block " 12305fe58fdSRyusuke Konishi "offset=%llu)\n", 12405fe58fdSRyusuke Konishi inode->i_ino, 12505fe58fdSRyusuke Konishi (unsigned long long)blkoff); 1261f5abe7eSRyusuke Konishi err = 0; 12705fe58fdSRyusuke Konishi } 12847420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 12905fe58fdSRyusuke Konishi goto out; 13005fe58fdSRyusuke Konishi } 131b9f66140SAndreas Rohner nilfs_mark_inode_dirty_sync(inode); 13247420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 13305fe58fdSRyusuke Konishi /* Error handling should be detailed */ 13405fe58fdSRyusuke Konishi set_buffer_new(bh_result); 13527e6c7a3SRyusuke Konishi set_buffer_delay(bh_result); 13605fe58fdSRyusuke Konishi map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed 13705fe58fdSRyusuke Konishi to proper value */ 13805fe58fdSRyusuke Konishi } else if (ret == -ENOENT) { 13905fe58fdSRyusuke Konishi /* not found is not error (e.g. hole); must return without 14005fe58fdSRyusuke Konishi the mapped state flag. */ 14105fe58fdSRyusuke Konishi ; 14205fe58fdSRyusuke Konishi } else { 14305fe58fdSRyusuke Konishi err = ret; 14405fe58fdSRyusuke Konishi } 14505fe58fdSRyusuke Konishi 14605fe58fdSRyusuke Konishi out: 14705fe58fdSRyusuke Konishi return err; 14805fe58fdSRyusuke Konishi } 14905fe58fdSRyusuke Konishi 15005fe58fdSRyusuke Konishi /** 15105fe58fdSRyusuke Konishi * nilfs_readpage() - implement readpage() method of nilfs_aops {} 15205fe58fdSRyusuke Konishi * address_space_operations. 15305fe58fdSRyusuke Konishi * @file - file struct of the file to be read 15405fe58fdSRyusuke Konishi * @page - the page to be read 15505fe58fdSRyusuke Konishi */ 15605fe58fdSRyusuke Konishi static int nilfs_readpage(struct file *file, struct page *page) 15705fe58fdSRyusuke Konishi { 15805fe58fdSRyusuke Konishi return mpage_readpage(page, nilfs_get_block); 15905fe58fdSRyusuke Konishi } 16005fe58fdSRyusuke Konishi 16105fe58fdSRyusuke Konishi /** 16205fe58fdSRyusuke Konishi * nilfs_readpages() - implement readpages() method of nilfs_aops {} 16305fe58fdSRyusuke Konishi * address_space_operations. 16405fe58fdSRyusuke Konishi * @file - file struct of the file to be read 16505fe58fdSRyusuke Konishi * @mapping - address_space struct used for reading multiple pages 16605fe58fdSRyusuke Konishi * @pages - the pages to be read 16705fe58fdSRyusuke Konishi * @nr_pages - number of pages to be read 16805fe58fdSRyusuke Konishi */ 16905fe58fdSRyusuke Konishi static int nilfs_readpages(struct file *file, struct address_space *mapping, 17005fe58fdSRyusuke Konishi struct list_head *pages, unsigned nr_pages) 17105fe58fdSRyusuke Konishi { 17205fe58fdSRyusuke Konishi return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); 17305fe58fdSRyusuke Konishi } 17405fe58fdSRyusuke Konishi 17505fe58fdSRyusuke Konishi static int nilfs_writepages(struct address_space *mapping, 17605fe58fdSRyusuke Konishi struct writeback_control *wbc) 17705fe58fdSRyusuke Konishi { 178f30bf3e4SRyusuke Konishi struct inode *inode = mapping->host; 179f30bf3e4SRyusuke Konishi int err = 0; 180f30bf3e4SRyusuke Konishi 1818c26c4e2SVyacheslav Dubeyko if (inode->i_sb->s_flags & MS_RDONLY) { 1828c26c4e2SVyacheslav Dubeyko nilfs_clear_dirty_pages(mapping, false); 1838c26c4e2SVyacheslav Dubeyko return -EROFS; 1848c26c4e2SVyacheslav Dubeyko } 1858c26c4e2SVyacheslav Dubeyko 186f30bf3e4SRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) 187f30bf3e4SRyusuke Konishi err = nilfs_construct_dsync_segment(inode->i_sb, inode, 188f30bf3e4SRyusuke Konishi wbc->range_start, 189f30bf3e4SRyusuke Konishi wbc->range_end); 190f30bf3e4SRyusuke Konishi return err; 19105fe58fdSRyusuke Konishi } 19205fe58fdSRyusuke Konishi 19305fe58fdSRyusuke Konishi static int nilfs_writepage(struct page *page, struct writeback_control *wbc) 19405fe58fdSRyusuke Konishi { 19505fe58fdSRyusuke Konishi struct inode *inode = page->mapping->host; 19605fe58fdSRyusuke Konishi int err; 19705fe58fdSRyusuke Konishi 198eb53b6dbSVyacheslav Dubeyko if (inode->i_sb->s_flags & MS_RDONLY) { 1998c26c4e2SVyacheslav Dubeyko /* 2008c26c4e2SVyacheslav Dubeyko * It means that filesystem was remounted in read-only 2018c26c4e2SVyacheslav Dubeyko * mode because of error or metadata corruption. But we 2028c26c4e2SVyacheslav Dubeyko * have dirty pages that try to be flushed in background. 2038c26c4e2SVyacheslav Dubeyko * So, here we simply discard this dirty page. 2048c26c4e2SVyacheslav Dubeyko */ 2058c26c4e2SVyacheslav Dubeyko nilfs_clear_dirty_page(page, false); 2068c26c4e2SVyacheslav Dubeyko unlock_page(page); 2078c26c4e2SVyacheslav Dubeyko return -EROFS; 2088c26c4e2SVyacheslav Dubeyko } 2098c26c4e2SVyacheslav Dubeyko 21005fe58fdSRyusuke Konishi redirty_page_for_writepage(wbc, page); 21105fe58fdSRyusuke Konishi unlock_page(page); 21205fe58fdSRyusuke Konishi 21305fe58fdSRyusuke Konishi if (wbc->sync_mode == WB_SYNC_ALL) { 21405fe58fdSRyusuke Konishi err = nilfs_construct_segment(inode->i_sb); 21505fe58fdSRyusuke Konishi if (unlikely(err)) 21605fe58fdSRyusuke Konishi return err; 21705fe58fdSRyusuke Konishi } else if (wbc->for_reclaim) 21805fe58fdSRyusuke Konishi nilfs_flush_segment(inode->i_sb, inode->i_ino); 21905fe58fdSRyusuke Konishi 22005fe58fdSRyusuke Konishi return 0; 22105fe58fdSRyusuke Konishi } 22205fe58fdSRyusuke Konishi 22305fe58fdSRyusuke Konishi static int nilfs_set_page_dirty(struct page *page) 22405fe58fdSRyusuke Konishi { 22556d7acc7SAndreas Rohner struct inode *inode = page->mapping->host; 226136e8770SRyusuke Konishi int ret = __set_page_dirty_nobuffers(page); 22705fe58fdSRyusuke Konishi 228136e8770SRyusuke Konishi if (page_has_buffers(page)) { 229136e8770SRyusuke Konishi unsigned nr_dirty = 0; 230136e8770SRyusuke Konishi struct buffer_head *bh, *head; 23105fe58fdSRyusuke Konishi 232136e8770SRyusuke Konishi /* 233136e8770SRyusuke Konishi * This page is locked by callers, and no other thread 234136e8770SRyusuke Konishi * concurrently marks its buffers dirty since they are 235136e8770SRyusuke Konishi * only dirtied through routines in fs/buffer.c in 236136e8770SRyusuke Konishi * which call sites of mark_buffer_dirty are protected 237136e8770SRyusuke Konishi * by page lock. 238136e8770SRyusuke Konishi */ 239136e8770SRyusuke Konishi bh = head = page_buffers(page); 240136e8770SRyusuke Konishi do { 241136e8770SRyusuke Konishi /* Do not mark hole blocks dirty */ 242136e8770SRyusuke Konishi if (buffer_dirty(bh) || !buffer_mapped(bh)) 243136e8770SRyusuke Konishi continue; 244136e8770SRyusuke Konishi 245136e8770SRyusuke Konishi set_buffer_dirty(bh); 246136e8770SRyusuke Konishi nr_dirty++; 247136e8770SRyusuke Konishi } while (bh = bh->b_this_page, bh != head); 248136e8770SRyusuke Konishi 249136e8770SRyusuke Konishi if (nr_dirty) 250bcbc8c64SRyusuke Konishi nilfs_set_file_dirty(inode, nr_dirty); 25156d7acc7SAndreas Rohner } else if (ret) { 25256d7acc7SAndreas Rohner unsigned nr_dirty = 1 << (PAGE_CACHE_SHIFT - inode->i_blkbits); 25356d7acc7SAndreas Rohner 25456d7acc7SAndreas Rohner nilfs_set_file_dirty(inode, nr_dirty); 25505fe58fdSRyusuke Konishi } 25605fe58fdSRyusuke Konishi return ret; 25705fe58fdSRyusuke Konishi } 25805fe58fdSRyusuke Konishi 2592d1b399bSMarco Stornelli void nilfs_write_failed(struct address_space *mapping, loff_t to) 2602d1b399bSMarco Stornelli { 2612d1b399bSMarco Stornelli struct inode *inode = mapping->host; 2622d1b399bSMarco Stornelli 2632d1b399bSMarco Stornelli if (to > inode->i_size) { 2647caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 2652d1b399bSMarco Stornelli nilfs_truncate(inode); 2662d1b399bSMarco Stornelli } 2672d1b399bSMarco Stornelli } 2682d1b399bSMarco Stornelli 26905fe58fdSRyusuke Konishi static int nilfs_write_begin(struct file *file, struct address_space *mapping, 27005fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned flags, 27105fe58fdSRyusuke Konishi struct page **pagep, void **fsdata) 27205fe58fdSRyusuke Konishi 27305fe58fdSRyusuke Konishi { 27405fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 27505fe58fdSRyusuke Konishi int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); 27605fe58fdSRyusuke Konishi 27705fe58fdSRyusuke Konishi if (unlikely(err)) 27805fe58fdSRyusuke Konishi return err; 27905fe58fdSRyusuke Konishi 280155130a4SChristoph Hellwig err = block_write_begin(mapping, pos, len, flags, pagep, 281155130a4SChristoph Hellwig nilfs_get_block); 282155130a4SChristoph Hellwig if (unlikely(err)) { 2832d1b399bSMarco Stornelli nilfs_write_failed(mapping, pos + len); 28447420c79SRyusuke Konishi nilfs_transaction_abort(inode->i_sb); 285155130a4SChristoph Hellwig } 28605fe58fdSRyusuke Konishi return err; 28705fe58fdSRyusuke Konishi } 28805fe58fdSRyusuke Konishi 28905fe58fdSRyusuke Konishi static int nilfs_write_end(struct file *file, struct address_space *mapping, 29005fe58fdSRyusuke Konishi loff_t pos, unsigned len, unsigned copied, 29105fe58fdSRyusuke Konishi struct page *page, void *fsdata) 29205fe58fdSRyusuke Konishi { 29305fe58fdSRyusuke Konishi struct inode *inode = mapping->host; 29405fe58fdSRyusuke Konishi unsigned start = pos & (PAGE_CACHE_SIZE - 1); 29505fe58fdSRyusuke Konishi unsigned nr_dirty; 29605fe58fdSRyusuke Konishi int err; 29705fe58fdSRyusuke Konishi 29805fe58fdSRyusuke Konishi nr_dirty = nilfs_page_count_clean_buffers(page, start, 29905fe58fdSRyusuke Konishi start + copied); 30005fe58fdSRyusuke Konishi copied = generic_write_end(file, mapping, pos, len, copied, page, 30105fe58fdSRyusuke Konishi fsdata); 302bcbc8c64SRyusuke Konishi nilfs_set_file_dirty(inode, nr_dirty); 30347420c79SRyusuke Konishi err = nilfs_transaction_commit(inode->i_sb); 30405fe58fdSRyusuke Konishi return err ? : copied; 30505fe58fdSRyusuke Konishi } 30605fe58fdSRyusuke Konishi 30705fe58fdSRyusuke Konishi static ssize_t 30822c6186eSOmar Sandoval nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset) 30905fe58fdSRyusuke Konishi { 31005fe58fdSRyusuke Konishi struct file *file = iocb->ki_filp; 3112d1b399bSMarco Stornelli struct address_space *mapping = file->f_mapping; 31205fe58fdSRyusuke Konishi struct inode *inode = file->f_mapping->host; 313a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 31405fe58fdSRyusuke Konishi ssize_t size; 31505fe58fdSRyusuke Konishi 3166f673763SOmar Sandoval if (iov_iter_rw(iter) == WRITE) 31705fe58fdSRyusuke Konishi return 0; 31805fe58fdSRyusuke Konishi 31905fe58fdSRyusuke Konishi /* Needs synchronization with the cleaner */ 32017f8c842SOmar Sandoval size = blockdev_direct_IO(iocb, inode, iter, offset, nilfs_get_block); 321eafdc7d1SChristoph Hellwig 322eafdc7d1SChristoph Hellwig /* 323eafdc7d1SChristoph Hellwig * In case of error extending write may have instantiated a few 324eafdc7d1SChristoph Hellwig * blocks outside i_size. Trim these off again. 325eafdc7d1SChristoph Hellwig */ 3266f673763SOmar Sandoval if (unlikely(iov_iter_rw(iter) == WRITE && size < 0)) { 327eafdc7d1SChristoph Hellwig loff_t isize = i_size_read(inode); 328a6cbcd4aSAl Viro loff_t end = offset + count; 329eafdc7d1SChristoph Hellwig 330eafdc7d1SChristoph Hellwig if (end > isize) 3312d1b399bSMarco Stornelli nilfs_write_failed(mapping, end); 332eafdc7d1SChristoph Hellwig } 333eafdc7d1SChristoph Hellwig 33405fe58fdSRyusuke Konishi return size; 33505fe58fdSRyusuke Konishi } 33605fe58fdSRyusuke Konishi 3377f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = { 33805fe58fdSRyusuke Konishi .writepage = nilfs_writepage, 33905fe58fdSRyusuke Konishi .readpage = nilfs_readpage, 34005fe58fdSRyusuke Konishi .writepages = nilfs_writepages, 34105fe58fdSRyusuke Konishi .set_page_dirty = nilfs_set_page_dirty, 34205fe58fdSRyusuke Konishi .readpages = nilfs_readpages, 34305fe58fdSRyusuke Konishi .write_begin = nilfs_write_begin, 34405fe58fdSRyusuke Konishi .write_end = nilfs_write_end, 34505fe58fdSRyusuke Konishi /* .releasepage = nilfs_releasepage, */ 34605fe58fdSRyusuke Konishi .invalidatepage = block_invalidatepage, 34705fe58fdSRyusuke Konishi .direct_IO = nilfs_direct_IO, 348258ef67eSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 34905fe58fdSRyusuke Konishi }; 35005fe58fdSRyusuke Konishi 351705304a8SRyusuke Konishi static int nilfs_insert_inode_locked(struct inode *inode, 352705304a8SRyusuke Konishi struct nilfs_root *root, 353705304a8SRyusuke Konishi unsigned long ino) 354705304a8SRyusuke Konishi { 355705304a8SRyusuke Konishi struct nilfs_iget_args args = { 356705304a8SRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 357705304a8SRyusuke Konishi }; 358705304a8SRyusuke Konishi 359705304a8SRyusuke Konishi return insert_inode_locked4(inode, ino, nilfs_iget_test, &args); 360705304a8SRyusuke Konishi } 361705304a8SRyusuke Konishi 362c6e49e3fSAl Viro struct inode *nilfs_new_inode(struct inode *dir, umode_t mode) 36305fe58fdSRyusuke Konishi { 36405fe58fdSRyusuke Konishi struct super_block *sb = dir->i_sb; 365e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info; 36605fe58fdSRyusuke Konishi struct inode *inode; 36705fe58fdSRyusuke Konishi struct nilfs_inode_info *ii; 3684d8d9293SRyusuke Konishi struct nilfs_root *root; 36905fe58fdSRyusuke Konishi int err = -ENOMEM; 37005fe58fdSRyusuke Konishi ino_t ino; 37105fe58fdSRyusuke Konishi 37205fe58fdSRyusuke Konishi inode = new_inode(sb); 37305fe58fdSRyusuke Konishi if (unlikely(!inode)) 37405fe58fdSRyusuke Konishi goto failed; 37505fe58fdSRyusuke Konishi 37605fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 37705fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 37805fe58fdSRyusuke Konishi 3794d8d9293SRyusuke Konishi root = NILFS_I(dir)->i_root; 38005fe58fdSRyusuke Konishi ii = NILFS_I(inode); 38105fe58fdSRyusuke Konishi ii->i_state = 1 << NILFS_I_NEW; 3824d8d9293SRyusuke Konishi ii->i_root = root; 38305fe58fdSRyusuke Konishi 384e912a5b6SRyusuke Konishi err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); 38505fe58fdSRyusuke Konishi if (unlikely(err)) 38605fe58fdSRyusuke Konishi goto failed_ifile_create_inode; 38705fe58fdSRyusuke Konishi /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 38805fe58fdSRyusuke Konishi 389e5f7f848SVyacheslav Dubeyko atomic64_inc(&root->inodes_count); 39073459dccSDmitry Monakhov inode_init_owner(inode, dir, mode); 39105fe58fdSRyusuke Konishi inode->i_ino = ino; 39205fe58fdSRyusuke Konishi inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 39305fe58fdSRyusuke Konishi 39405fe58fdSRyusuke Konishi if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 39505fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, NULL); 39605fe58fdSRyusuke Konishi if (err < 0) 397705304a8SRyusuke Konishi goto failed_after_creation; 39805fe58fdSRyusuke Konishi 39905fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 40005fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 40105fe58fdSRyusuke Konishi } 40205fe58fdSRyusuke Konishi 403b253a3e4SRyusuke Konishi ii->i_flags = nilfs_mask_flags( 404b253a3e4SRyusuke Konishi mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED); 40505fe58fdSRyusuke Konishi 40605fe58fdSRyusuke Konishi /* ii->i_file_acl = 0; */ 40705fe58fdSRyusuke Konishi /* ii->i_dir_acl = 0; */ 40805fe58fdSRyusuke Konishi ii->i_dir_start_lookup = 0; 40905fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 4109b1fc4e4SRyusuke Konishi spin_lock(&nilfs->ns_next_gen_lock); 4119b1fc4e4SRyusuke Konishi inode->i_generation = nilfs->ns_next_generation++; 4129b1fc4e4SRyusuke Konishi spin_unlock(&nilfs->ns_next_gen_lock); 413705304a8SRyusuke Konishi if (nilfs_insert_inode_locked(inode, root, ino) < 0) { 414705304a8SRyusuke Konishi err = -EIO; 415705304a8SRyusuke Konishi goto failed_after_creation; 416705304a8SRyusuke Konishi } 41705fe58fdSRyusuke Konishi 41805fe58fdSRyusuke Konishi err = nilfs_init_acl(inode, dir); 41905fe58fdSRyusuke Konishi if (unlikely(err)) 420705304a8SRyusuke Konishi goto failed_after_creation; /* never occur. When supporting 42105fe58fdSRyusuke Konishi nilfs_init_acl(), proper cancellation of 42205fe58fdSRyusuke Konishi above jobs should be considered */ 42305fe58fdSRyusuke Konishi 42405fe58fdSRyusuke Konishi return inode; 42505fe58fdSRyusuke Konishi 426705304a8SRyusuke Konishi failed_after_creation: 4276d6b77f1SMiklos Szeredi clear_nlink(inode); 428705304a8SRyusuke Konishi unlock_new_inode(inode); 42905fe58fdSRyusuke Konishi iput(inode); /* raw_inode will be deleted through 430705304a8SRyusuke Konishi nilfs_evict_inode() */ 43105fe58fdSRyusuke Konishi goto failed; 43205fe58fdSRyusuke Konishi 43305fe58fdSRyusuke Konishi failed_ifile_create_inode: 43405fe58fdSRyusuke Konishi make_bad_inode(inode); 43505fe58fdSRyusuke Konishi iput(inode); /* if i_nlink == 1, generic_forget_inode() will be 43605fe58fdSRyusuke Konishi called */ 43705fe58fdSRyusuke Konishi failed: 43805fe58fdSRyusuke Konishi return ERR_PTR(err); 43905fe58fdSRyusuke Konishi } 44005fe58fdSRyusuke Konishi 44105fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode) 44205fe58fdSRyusuke Konishi { 44305fe58fdSRyusuke Konishi unsigned int flags = NILFS_I(inode)->i_flags; 44405fe58fdSRyusuke Konishi 44505fe58fdSRyusuke Konishi inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | 44605fe58fdSRyusuke Konishi S_DIRSYNC); 447f0c9f242SRyusuke Konishi if (flags & FS_SYNC_FL) 44805fe58fdSRyusuke Konishi inode->i_flags |= S_SYNC; 449f0c9f242SRyusuke Konishi if (flags & FS_APPEND_FL) 45005fe58fdSRyusuke Konishi inode->i_flags |= S_APPEND; 451f0c9f242SRyusuke Konishi if (flags & FS_IMMUTABLE_FL) 45205fe58fdSRyusuke Konishi inode->i_flags |= S_IMMUTABLE; 453f0c9f242SRyusuke Konishi if (flags & FS_NOATIME_FL) 45405fe58fdSRyusuke Konishi inode->i_flags |= S_NOATIME; 455f0c9f242SRyusuke Konishi if (flags & FS_DIRSYNC_FL) 45605fe58fdSRyusuke Konishi inode->i_flags |= S_DIRSYNC; 45705fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 45805fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 45905fe58fdSRyusuke Konishi } 46005fe58fdSRyusuke Konishi 46105fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode, 46205fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode) 46305fe58fdSRyusuke Konishi { 46405fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 46505fe58fdSRyusuke Konishi int err; 46605fe58fdSRyusuke Konishi 46705fe58fdSRyusuke Konishi inode->i_mode = le16_to_cpu(raw_inode->i_mode); 468305d3d0dSEric W. Biederman i_uid_write(inode, le32_to_cpu(raw_inode->i_uid)); 469305d3d0dSEric W. Biederman i_gid_write(inode, le32_to_cpu(raw_inode->i_gid)); 470bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 47105fe58fdSRyusuke Konishi inode->i_size = le64_to_cpu(raw_inode->i_size); 47205fe58fdSRyusuke Konishi inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 47305fe58fdSRyusuke Konishi inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); 47405fe58fdSRyusuke Konishi inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 47561239230SRyusuke Konishi inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 47661239230SRyusuke Konishi inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); 47761239230SRyusuke Konishi inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 478705304a8SRyusuke Konishi if (inode->i_nlink == 0) 479705304a8SRyusuke Konishi return -ESTALE; /* this inode is deleted */ 48005fe58fdSRyusuke Konishi 48105fe58fdSRyusuke Konishi inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 48205fe58fdSRyusuke Konishi ii->i_flags = le32_to_cpu(raw_inode->i_flags); 48305fe58fdSRyusuke Konishi #if 0 48405fe58fdSRyusuke Konishi ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 48505fe58fdSRyusuke Konishi ii->i_dir_acl = S_ISREG(inode->i_mode) ? 48605fe58fdSRyusuke Konishi 0 : le32_to_cpu(raw_inode->i_dir_acl); 48705fe58fdSRyusuke Konishi #endif 4883cc811bfSRyusuke Konishi ii->i_dir_start_lookup = 0; 48905fe58fdSRyusuke Konishi inode->i_generation = le32_to_cpu(raw_inode->i_generation); 49005fe58fdSRyusuke Konishi 49105fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 49205fe58fdSRyusuke Konishi S_ISLNK(inode->i_mode)) { 49305fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, raw_inode); 49405fe58fdSRyusuke Konishi if (err < 0) 49505fe58fdSRyusuke Konishi return err; 49605fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 49705fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 49805fe58fdSRyusuke Konishi } 49905fe58fdSRyusuke Konishi return 0; 50005fe58fdSRyusuke Konishi } 50105fe58fdSRyusuke Konishi 502e912a5b6SRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb, 503e912a5b6SRyusuke Konishi struct nilfs_root *root, unsigned long ino, 50405fe58fdSRyusuke Konishi struct inode *inode) 50505fe58fdSRyusuke Konishi { 506e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info; 50705fe58fdSRyusuke Konishi struct buffer_head *bh; 50805fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 50905fe58fdSRyusuke Konishi int err; 51005fe58fdSRyusuke Konishi 511365e215cSRyusuke Konishi down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 512e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); 51305fe58fdSRyusuke Konishi if (unlikely(err)) 51405fe58fdSRyusuke Konishi goto bad_inode; 51505fe58fdSRyusuke Konishi 516e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); 51705fe58fdSRyusuke Konishi 5181b2f5a64SRyusuke Konishi err = nilfs_read_inode_common(inode, raw_inode); 5191b2f5a64SRyusuke Konishi if (err) 52005fe58fdSRyusuke Konishi goto failed_unmap; 52105fe58fdSRyusuke Konishi 52205fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode)) { 52305fe58fdSRyusuke Konishi inode->i_op = &nilfs_file_inode_operations; 52405fe58fdSRyusuke Konishi inode->i_fop = &nilfs_file_operations; 52505fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 52605fe58fdSRyusuke Konishi } else if (S_ISDIR(inode->i_mode)) { 52705fe58fdSRyusuke Konishi inode->i_op = &nilfs_dir_inode_operations; 52805fe58fdSRyusuke Konishi inode->i_fop = &nilfs_dir_operations; 52905fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 53005fe58fdSRyusuke Konishi } else if (S_ISLNK(inode->i_mode)) { 53105fe58fdSRyusuke Konishi inode->i_op = &nilfs_symlink_inode_operations; 53205fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 53305fe58fdSRyusuke Konishi } else { 53405fe58fdSRyusuke Konishi inode->i_op = &nilfs_special_inode_operations; 53505fe58fdSRyusuke Konishi init_special_inode( 53605fe58fdSRyusuke Konishi inode, inode->i_mode, 537cdce214eSRyusuke Konishi huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 53805fe58fdSRyusuke Konishi } 539e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 54005fe58fdSRyusuke Konishi brelse(bh); 541365e215cSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 54205fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 54305fe58fdSRyusuke Konishi return 0; 54405fe58fdSRyusuke Konishi 54505fe58fdSRyusuke Konishi failed_unmap: 546e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 54705fe58fdSRyusuke Konishi brelse(bh); 54805fe58fdSRyusuke Konishi 54905fe58fdSRyusuke Konishi bad_inode: 550365e215cSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 55105fe58fdSRyusuke Konishi return err; 55205fe58fdSRyusuke Konishi } 55305fe58fdSRyusuke Konishi 5540e14a359SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque) 5550e14a359SRyusuke Konishi { 5560e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 5570e14a359SRyusuke Konishi struct nilfs_inode_info *ii; 5580e14a359SRyusuke Konishi 5594d8d9293SRyusuke Konishi if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) 5600e14a359SRyusuke Konishi return 0; 5610e14a359SRyusuke Konishi 5620e14a359SRyusuke Konishi ii = NILFS_I(inode); 5630e14a359SRyusuke Konishi if (!test_bit(NILFS_I_GCINODE, &ii->i_state)) 5640e14a359SRyusuke Konishi return !args->for_gc; 5650e14a359SRyusuke Konishi 5660e14a359SRyusuke Konishi return args->for_gc && args->cno == ii->i_cno; 5670e14a359SRyusuke Konishi } 5680e14a359SRyusuke Konishi 5690e14a359SRyusuke Konishi static int nilfs_iget_set(struct inode *inode, void *opaque) 5700e14a359SRyusuke Konishi { 5710e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 5720e14a359SRyusuke Konishi 5730e14a359SRyusuke Konishi inode->i_ino = args->ino; 5740e14a359SRyusuke Konishi if (args->for_gc) { 5750e14a359SRyusuke Konishi NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE; 5760e14a359SRyusuke Konishi NILFS_I(inode)->i_cno = args->cno; 5774d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = NULL; 5784d8d9293SRyusuke Konishi } else { 5794d8d9293SRyusuke Konishi if (args->root && args->ino == NILFS_ROOT_INO) 5804d8d9293SRyusuke Konishi nilfs_get_root(args->root); 5814d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = args->root; 5820e14a359SRyusuke Konishi } 5830e14a359SRyusuke Konishi return 0; 5840e14a359SRyusuke Konishi } 5850e14a359SRyusuke Konishi 586032dbb3bSRyusuke Konishi struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, 587032dbb3bSRyusuke Konishi unsigned long ino) 588032dbb3bSRyusuke Konishi { 589032dbb3bSRyusuke Konishi struct nilfs_iget_args args = { 590032dbb3bSRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 591032dbb3bSRyusuke Konishi }; 592032dbb3bSRyusuke Konishi 593032dbb3bSRyusuke Konishi return ilookup5(sb, ino, nilfs_iget_test, &args); 594032dbb3bSRyusuke Konishi } 595032dbb3bSRyusuke Konishi 596f1e89c86SRyusuke Konishi struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, 5974d8d9293SRyusuke Konishi unsigned long ino) 59805fe58fdSRyusuke Konishi { 5994d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 6004d8d9293SRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 6014d8d9293SRyusuke Konishi }; 602f1e89c86SRyusuke Konishi 603f1e89c86SRyusuke Konishi return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 604f1e89c86SRyusuke Konishi } 605f1e89c86SRyusuke Konishi 606f1e89c86SRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, 607f1e89c86SRyusuke Konishi unsigned long ino) 608f1e89c86SRyusuke Konishi { 60905fe58fdSRyusuke Konishi struct inode *inode; 61005fe58fdSRyusuke Konishi int err; 61105fe58fdSRyusuke Konishi 612f1e89c86SRyusuke Konishi inode = nilfs_iget_locked(sb, root, ino); 61305fe58fdSRyusuke Konishi if (unlikely(!inode)) 61405fe58fdSRyusuke Konishi return ERR_PTR(-ENOMEM); 61505fe58fdSRyusuke Konishi if (!(inode->i_state & I_NEW)) 61605fe58fdSRyusuke Konishi return inode; 61705fe58fdSRyusuke Konishi 618e912a5b6SRyusuke Konishi err = __nilfs_read_inode(sb, root, ino, inode); 61905fe58fdSRyusuke Konishi if (unlikely(err)) { 62005fe58fdSRyusuke Konishi iget_failed(inode); 62105fe58fdSRyusuke Konishi return ERR_PTR(err); 62205fe58fdSRyusuke Konishi } 62305fe58fdSRyusuke Konishi unlock_new_inode(inode); 62405fe58fdSRyusuke Konishi return inode; 62505fe58fdSRyusuke Konishi } 62605fe58fdSRyusuke Konishi 627263d90ceSRyusuke Konishi struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino, 628263d90ceSRyusuke Konishi __u64 cno) 629263d90ceSRyusuke Konishi { 6304d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 6314d8d9293SRyusuke Konishi .ino = ino, .root = NULL, .cno = cno, .for_gc = 1 6324d8d9293SRyusuke Konishi }; 633263d90ceSRyusuke Konishi struct inode *inode; 634263d90ceSRyusuke Konishi int err; 635263d90ceSRyusuke Konishi 636263d90ceSRyusuke Konishi inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 637263d90ceSRyusuke Konishi if (unlikely(!inode)) 638263d90ceSRyusuke Konishi return ERR_PTR(-ENOMEM); 639263d90ceSRyusuke Konishi if (!(inode->i_state & I_NEW)) 640263d90ceSRyusuke Konishi return inode; 641263d90ceSRyusuke Konishi 642263d90ceSRyusuke Konishi err = nilfs_init_gcinode(inode); 643263d90ceSRyusuke Konishi if (unlikely(err)) { 644263d90ceSRyusuke Konishi iget_failed(inode); 645263d90ceSRyusuke Konishi return ERR_PTR(err); 646263d90ceSRyusuke Konishi } 647263d90ceSRyusuke Konishi unlock_new_inode(inode); 648263d90ceSRyusuke Konishi return inode; 649263d90ceSRyusuke Konishi } 650263d90ceSRyusuke Konishi 65105fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode, 65205fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode, int has_bmap) 65305fe58fdSRyusuke Konishi { 65405fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 65505fe58fdSRyusuke Konishi 65605fe58fdSRyusuke Konishi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 657305d3d0dSEric W. Biederman raw_inode->i_uid = cpu_to_le32(i_uid_read(inode)); 658305d3d0dSEric W. Biederman raw_inode->i_gid = cpu_to_le32(i_gid_read(inode)); 65905fe58fdSRyusuke Konishi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 66005fe58fdSRyusuke Konishi raw_inode->i_size = cpu_to_le64(inode->i_size); 66105fe58fdSRyusuke Konishi raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 66205fe58fdSRyusuke Konishi raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); 66361239230SRyusuke Konishi raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 66461239230SRyusuke Konishi raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); 66505fe58fdSRyusuke Konishi raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 66605fe58fdSRyusuke Konishi 66705fe58fdSRyusuke Konishi raw_inode->i_flags = cpu_to_le32(ii->i_flags); 66805fe58fdSRyusuke Konishi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 66905fe58fdSRyusuke Konishi 67056eb5538SRyusuke Konishi if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) { 67156eb5538SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 67256eb5538SRyusuke Konishi 67356eb5538SRyusuke Konishi /* zero-fill unused portion in the case of super root block */ 67456eb5538SRyusuke Konishi raw_inode->i_xattr = 0; 67556eb5538SRyusuke Konishi raw_inode->i_pad = 0; 67656eb5538SRyusuke Konishi memset((void *)raw_inode + sizeof(*raw_inode), 0, 67756eb5538SRyusuke Konishi nilfs->ns_inode_size - sizeof(*raw_inode)); 67856eb5538SRyusuke Konishi } 67956eb5538SRyusuke Konishi 68005fe58fdSRyusuke Konishi if (has_bmap) 68105fe58fdSRyusuke Konishi nilfs_bmap_write(ii->i_bmap, raw_inode); 68205fe58fdSRyusuke Konishi else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 68305fe58fdSRyusuke Konishi raw_inode->i_device_code = 684cdce214eSRyusuke Konishi cpu_to_le64(huge_encode_dev(inode->i_rdev)); 68505fe58fdSRyusuke Konishi /* When extending inode, nilfs->ns_inode_size should be checked 68605fe58fdSRyusuke Konishi for substitutions of appended fields */ 68705fe58fdSRyusuke Konishi } 68805fe58fdSRyusuke Konishi 689b9f66140SAndreas Rohner void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags) 69005fe58fdSRyusuke Konishi { 69105fe58fdSRyusuke Konishi ino_t ino = inode->i_ino; 69205fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 693e912a5b6SRyusuke Konishi struct inode *ifile = ii->i_root->ifile; 69405fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 69505fe58fdSRyusuke Konishi 696e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); 69705fe58fdSRyusuke Konishi 69805fe58fdSRyusuke Konishi if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 699e912a5b6SRyusuke Konishi memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); 700b9f66140SAndreas Rohner if (flags & I_DIRTY_DATASYNC) 701b9f66140SAndreas Rohner set_bit(NILFS_I_INODE_SYNC, &ii->i_state); 70205fe58fdSRyusuke Konishi 70305fe58fdSRyusuke Konishi nilfs_write_inode_common(inode, raw_inode, 0); 70405fe58fdSRyusuke Konishi /* XXX: call with has_bmap = 0 is a workaround to avoid 70505fe58fdSRyusuke Konishi deadlock of bmap. This delays update of i_bmap to just 70605fe58fdSRyusuke Konishi before writing */ 707e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(ifile, ino, ibh); 70805fe58fdSRyusuke Konishi } 70905fe58fdSRyusuke Konishi 71005fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 71105fe58fdSRyusuke Konishi 71205fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 71305fe58fdSRyusuke Konishi unsigned long from) 71405fe58fdSRyusuke Konishi { 71505fe58fdSRyusuke Konishi unsigned long b; 71605fe58fdSRyusuke Konishi int ret; 71705fe58fdSRyusuke Konishi 71805fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 71905fe58fdSRyusuke Konishi return; 72005fe58fdSRyusuke Konishi repeat: 72105fe58fdSRyusuke Konishi ret = nilfs_bmap_last_key(ii->i_bmap, &b); 72205fe58fdSRyusuke Konishi if (ret == -ENOENT) 72305fe58fdSRyusuke Konishi return; 72405fe58fdSRyusuke Konishi else if (ret < 0) 72505fe58fdSRyusuke Konishi goto failed; 72605fe58fdSRyusuke Konishi 72705fe58fdSRyusuke Konishi if (b < from) 72805fe58fdSRyusuke Konishi return; 72905fe58fdSRyusuke Konishi 73005fe58fdSRyusuke Konishi b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 73105fe58fdSRyusuke Konishi ret = nilfs_bmap_truncate(ii->i_bmap, b); 73205fe58fdSRyusuke Konishi nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 73305fe58fdSRyusuke Konishi if (!ret || (ret == -ENOMEM && 73405fe58fdSRyusuke Konishi nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 73505fe58fdSRyusuke Konishi goto repeat; 73605fe58fdSRyusuke Konishi 73705fe58fdSRyusuke Konishi failed: 73805fe58fdSRyusuke Konishi nilfs_warning(ii->vfs_inode.i_sb, __func__, 73905fe58fdSRyusuke Konishi "failed to truncate bmap (ino=%lu, err=%d)", 74005fe58fdSRyusuke Konishi ii->vfs_inode.i_ino, ret); 74105fe58fdSRyusuke Konishi } 74205fe58fdSRyusuke Konishi 74305fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode) 74405fe58fdSRyusuke Konishi { 74505fe58fdSRyusuke Konishi unsigned long blkoff; 74605fe58fdSRyusuke Konishi unsigned int blocksize; 74705fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 74805fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 74905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 75005fe58fdSRyusuke Konishi 75105fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 75205fe58fdSRyusuke Konishi return; 75305fe58fdSRyusuke Konishi if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 75405fe58fdSRyusuke Konishi return; 75505fe58fdSRyusuke Konishi 75605fe58fdSRyusuke Konishi blocksize = sb->s_blocksize; 75705fe58fdSRyusuke Konishi blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 7581f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 75905fe58fdSRyusuke Konishi 76005fe58fdSRyusuke Konishi block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 76105fe58fdSRyusuke Konishi 76205fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, blkoff); 76305fe58fdSRyusuke Konishi 76405fe58fdSRyusuke Konishi inode->i_mtime = inode->i_ctime = CURRENT_TIME; 76505fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 76605fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 76705fe58fdSRyusuke Konishi 768abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 769bcbc8c64SRyusuke Konishi nilfs_set_file_dirty(inode, 0); 77047420c79SRyusuke Konishi nilfs_transaction_commit(sb); 77105fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 77205fe58fdSRyusuke Konishi But truncate has no return value. */ 77305fe58fdSRyusuke Konishi } 77405fe58fdSRyusuke Konishi 7756fd1e5c9SAl Viro static void nilfs_clear_inode(struct inode *inode) 7766fd1e5c9SAl Viro { 7776fd1e5c9SAl Viro struct nilfs_inode_info *ii = NILFS_I(inode); 778518d1a6aSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 7796fd1e5c9SAl Viro 7806fd1e5c9SAl Viro /* 7816fd1e5c9SAl Viro * Free resources allocated in nilfs_read_inode(), here. 7826fd1e5c9SAl Viro */ 7836fd1e5c9SAl Viro BUG_ON(!list_empty(&ii->i_dirty)); 7846fd1e5c9SAl Viro brelse(ii->i_bh); 7856fd1e5c9SAl Viro ii->i_bh = NULL; 7866fd1e5c9SAl Viro 787518d1a6aSRyusuke Konishi if (mdi && mdi->mi_palloc_cache) 788518d1a6aSRyusuke Konishi nilfs_palloc_destroy_cache(inode); 789518d1a6aSRyusuke Konishi 7906fd1e5c9SAl Viro if (test_bit(NILFS_I_BMAP, &ii->i_state)) 7916fd1e5c9SAl Viro nilfs_bmap_clear(ii->i_bmap); 7926fd1e5c9SAl Viro 7936fd1e5c9SAl Viro nilfs_btnode_cache_clear(&ii->i_btnode_cache); 7944d8d9293SRyusuke Konishi 7954d8d9293SRyusuke Konishi if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) 7964d8d9293SRyusuke Konishi nilfs_put_root(ii->i_root); 7976fd1e5c9SAl Viro } 7986fd1e5c9SAl Viro 7996fd1e5c9SAl Viro void nilfs_evict_inode(struct inode *inode) 80005fe58fdSRyusuke Konishi { 80105fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 80205fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 80305fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 80425b18d39SRyusuke Konishi int ret; 80505fe58fdSRyusuke Konishi 8064d8d9293SRyusuke Konishi if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { 80791b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 808dbd5768fSJan Kara clear_inode(inode); 8096fd1e5c9SAl Viro nilfs_clear_inode(inode); 81005fe58fdSRyusuke Konishi return; 81105fe58fdSRyusuke Konishi } 8121f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 8131f5abe7eSRyusuke Konishi 81491b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 81505fe58fdSRyusuke Konishi 816e912a5b6SRyusuke Konishi /* TODO: some of the following operations may fail. */ 81705fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, 0); 818abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 819dbd5768fSJan Kara clear_inode(inode); 820e912a5b6SRyusuke Konishi 82125b18d39SRyusuke Konishi ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); 82225b18d39SRyusuke Konishi if (!ret) 823e5f7f848SVyacheslav Dubeyko atomic64_dec(&ii->i_root->inodes_count); 824e912a5b6SRyusuke Konishi 8256fd1e5c9SAl Viro nilfs_clear_inode(inode); 826e912a5b6SRyusuke Konishi 82705fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 82805fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 82947420c79SRyusuke Konishi nilfs_transaction_commit(sb); 83005fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 83105fe58fdSRyusuke Konishi But delete_inode has no return value. */ 83205fe58fdSRyusuke Konishi } 83305fe58fdSRyusuke Konishi 83405fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) 83505fe58fdSRyusuke Konishi { 83605fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 837*2b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 83805fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 83947420c79SRyusuke Konishi int err; 84005fe58fdSRyusuke Konishi 84105fe58fdSRyusuke Konishi err = inode_change_ok(inode, iattr); 84205fe58fdSRyusuke Konishi if (err) 84305fe58fdSRyusuke Konishi return err; 84405fe58fdSRyusuke Konishi 84505fe58fdSRyusuke Konishi err = nilfs_transaction_begin(sb, &ti, 0); 84605fe58fdSRyusuke Konishi if (unlikely(err)) 84705fe58fdSRyusuke Konishi return err; 84847420c79SRyusuke Konishi 8491025774cSChristoph Hellwig if ((iattr->ia_valid & ATTR_SIZE) && 8501025774cSChristoph Hellwig iattr->ia_size != i_size_read(inode)) { 851562c72aaSChristoph Hellwig inode_dio_wait(inode); 8522d1b399bSMarco Stornelli truncate_setsize(inode, iattr->ia_size); 8532d1b399bSMarco Stornelli nilfs_truncate(inode); 8541025774cSChristoph Hellwig } 8551025774cSChristoph Hellwig 8561025774cSChristoph Hellwig setattr_copy(inode, iattr); 8571025774cSChristoph Hellwig mark_inode_dirty(inode); 8581025774cSChristoph Hellwig 8591025774cSChristoph Hellwig if (iattr->ia_valid & ATTR_MODE) { 8601025774cSChristoph Hellwig err = nilfs_acl_chmod(inode); 8611025774cSChristoph Hellwig if (unlikely(err)) 8621025774cSChristoph Hellwig goto out_err; 8631025774cSChristoph Hellwig } 8641025774cSChristoph Hellwig 8651025774cSChristoph Hellwig return nilfs_transaction_commit(sb); 8661025774cSChristoph Hellwig 8671025774cSChristoph Hellwig out_err: 8681025774cSChristoph Hellwig nilfs_transaction_abort(sb); 86947420c79SRyusuke Konishi return err; 87005fe58fdSRyusuke Konishi } 87105fe58fdSRyusuke Konishi 87210556cb2SAl Viro int nilfs_permission(struct inode *inode, int mask) 873dc3d3b81SRyusuke Konishi { 874730e908fSAl Viro struct nilfs_root *root = NILFS_I(inode)->i_root; 875dc3d3b81SRyusuke Konishi if ((mask & MAY_WRITE) && root && 876dc3d3b81SRyusuke Konishi root->cno != NILFS_CPTREE_CURRENT_CNO) 877dc3d3b81SRyusuke Konishi return -EROFS; /* snapshot is not writable */ 878dc3d3b81SRyusuke Konishi 8792830ba7fSAl Viro return generic_permission(inode, mask); 880dc3d3b81SRyusuke Konishi } 881dc3d3b81SRyusuke Konishi 882bcbc8c64SRyusuke Konishi int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh) 88305fe58fdSRyusuke Konishi { 884e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 88505fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 88605fe58fdSRyusuke Konishi int err; 88705fe58fdSRyusuke Konishi 888693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 88905fe58fdSRyusuke Konishi if (ii->i_bh == NULL) { 890693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 891e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(ii->i_root->ifile, 892e912a5b6SRyusuke Konishi inode->i_ino, pbh); 89305fe58fdSRyusuke Konishi if (unlikely(err)) 89405fe58fdSRyusuke Konishi return err; 895693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 89605fe58fdSRyusuke Konishi if (ii->i_bh == NULL) 89705fe58fdSRyusuke Konishi ii->i_bh = *pbh; 89805fe58fdSRyusuke Konishi else { 89905fe58fdSRyusuke Konishi brelse(*pbh); 90005fe58fdSRyusuke Konishi *pbh = ii->i_bh; 90105fe58fdSRyusuke Konishi } 90205fe58fdSRyusuke Konishi } else 90305fe58fdSRyusuke Konishi *pbh = ii->i_bh; 90405fe58fdSRyusuke Konishi 90505fe58fdSRyusuke Konishi get_bh(*pbh); 906693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 90705fe58fdSRyusuke Konishi return 0; 90805fe58fdSRyusuke Konishi } 90905fe58fdSRyusuke Konishi 91005fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode) 91105fe58fdSRyusuke Konishi { 91205fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 913e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 91405fe58fdSRyusuke Konishi int ret = 0; 91505fe58fdSRyusuke Konishi 91605fe58fdSRyusuke Konishi if (!list_empty(&ii->i_dirty)) { 917693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 91805fe58fdSRyusuke Konishi ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 91905fe58fdSRyusuke Konishi test_bit(NILFS_I_BUSY, &ii->i_state); 920693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 92105fe58fdSRyusuke Konishi } 92205fe58fdSRyusuke Konishi return ret; 92305fe58fdSRyusuke Konishi } 92405fe58fdSRyusuke Konishi 925bcbc8c64SRyusuke Konishi int nilfs_set_file_dirty(struct inode *inode, unsigned nr_dirty) 92605fe58fdSRyusuke Konishi { 92705fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 928e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 92905fe58fdSRyusuke Konishi 930693dd321SRyusuke Konishi atomic_add(nr_dirty, &nilfs->ns_ndirtyblks); 93105fe58fdSRyusuke Konishi 932458c5b08SRyusuke Konishi if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 93305fe58fdSRyusuke Konishi return 0; 93405fe58fdSRyusuke Konishi 935693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 93605fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 93705fe58fdSRyusuke Konishi !test_bit(NILFS_I_BUSY, &ii->i_state)) { 93805fe58fdSRyusuke Konishi /* Because this routine may race with nilfs_dispose_list(), 93905fe58fdSRyusuke Konishi we have to check NILFS_I_QUEUED here, too. */ 94005fe58fdSRyusuke Konishi if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 94105fe58fdSRyusuke Konishi /* This will happen when somebody is freeing 94205fe58fdSRyusuke Konishi this inode. */ 943693dd321SRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 94405fe58fdSRyusuke Konishi "cannot get inode (ino=%lu)\n", 94505fe58fdSRyusuke Konishi inode->i_ino); 946693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 94705fe58fdSRyusuke Konishi return -EINVAL; /* NILFS_I_DIRTY may remain for 94805fe58fdSRyusuke Konishi freeing inode */ 94905fe58fdSRyusuke Konishi } 950eaae0f37SNicolas Kaiser list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files); 95105fe58fdSRyusuke Konishi set_bit(NILFS_I_QUEUED, &ii->i_state); 95205fe58fdSRyusuke Konishi } 953693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 95405fe58fdSRyusuke Konishi return 0; 95505fe58fdSRyusuke Konishi } 95605fe58fdSRyusuke Konishi 957b9f66140SAndreas Rohner int __nilfs_mark_inode_dirty(struct inode *inode, int flags) 95805fe58fdSRyusuke Konishi { 95905fe58fdSRyusuke Konishi struct buffer_head *ibh; 96005fe58fdSRyusuke Konishi int err; 96105fe58fdSRyusuke Konishi 962bcbc8c64SRyusuke Konishi err = nilfs_load_inode_block(inode, &ibh); 96305fe58fdSRyusuke Konishi if (unlikely(err)) { 96405fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 96505fe58fdSRyusuke Konishi "failed to reget inode block.\n"); 96605fe58fdSRyusuke Konishi return err; 96705fe58fdSRyusuke Konishi } 968b9f66140SAndreas Rohner nilfs_update_inode(inode, ibh, flags); 9695fc7b141SRyusuke Konishi mark_buffer_dirty(ibh); 970e912a5b6SRyusuke Konishi nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); 97105fe58fdSRyusuke Konishi brelse(ibh); 97205fe58fdSRyusuke Konishi return 0; 97305fe58fdSRyusuke Konishi } 97405fe58fdSRyusuke Konishi 97505fe58fdSRyusuke Konishi /** 97605fe58fdSRyusuke Konishi * nilfs_dirty_inode - reflect changes on given inode to an inode block. 97705fe58fdSRyusuke Konishi * @inode: inode of the file to be registered. 97805fe58fdSRyusuke Konishi * 97905fe58fdSRyusuke Konishi * nilfs_dirty_inode() loads a inode block containing the specified 98005fe58fdSRyusuke Konishi * @inode and copies data from a nilfs_inode to a corresponding inode 98105fe58fdSRyusuke Konishi * entry in the inode block. This operation is excluded from the segment 98205fe58fdSRyusuke Konishi * construction. This function can be called both as a single operation 98305fe58fdSRyusuke Konishi * and as a part of indivisible file operations. 98405fe58fdSRyusuke Konishi */ 985aa385729SChristoph Hellwig void nilfs_dirty_inode(struct inode *inode, int flags) 98605fe58fdSRyusuke Konishi { 98705fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 9887d6cd92fSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 98905fe58fdSRyusuke Konishi 99005fe58fdSRyusuke Konishi if (is_bad_inode(inode)) { 99105fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 99205fe58fdSRyusuke Konishi "tried to mark bad_inode dirty. ignored.\n"); 99305fe58fdSRyusuke Konishi dump_stack(); 99405fe58fdSRyusuke Konishi return; 99505fe58fdSRyusuke Konishi } 9967d6cd92fSRyusuke Konishi if (mdi) { 9977d6cd92fSRyusuke Konishi nilfs_mdt_mark_dirty(inode); 9987d6cd92fSRyusuke Konishi return; 9997d6cd92fSRyusuke Konishi } 100005fe58fdSRyusuke Konishi nilfs_transaction_begin(inode->i_sb, &ti, 0); 1001b9f66140SAndreas Rohner __nilfs_mark_inode_dirty(inode, flags); 100247420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 100305fe58fdSRyusuke Konishi } 1004622daaffSRyusuke Konishi 1005622daaffSRyusuke Konishi int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1006622daaffSRyusuke Konishi __u64 start, __u64 len) 1007622daaffSRyusuke Konishi { 10080ef28f9aSRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 1009622daaffSRyusuke Konishi __u64 logical = 0, phys = 0, size = 0; 1010622daaffSRyusuke Konishi __u32 flags = 0; 1011622daaffSRyusuke Konishi loff_t isize; 1012622daaffSRyusuke Konishi sector_t blkoff, end_blkoff; 1013622daaffSRyusuke Konishi sector_t delalloc_blkoff; 1014622daaffSRyusuke Konishi unsigned long delalloc_blklen; 1015622daaffSRyusuke Konishi unsigned int blkbits = inode->i_blkbits; 1016622daaffSRyusuke Konishi int ret, n; 1017622daaffSRyusuke Konishi 1018622daaffSRyusuke Konishi ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); 1019622daaffSRyusuke Konishi if (ret) 1020622daaffSRyusuke Konishi return ret; 1021622daaffSRyusuke Konishi 1022622daaffSRyusuke Konishi mutex_lock(&inode->i_mutex); 1023622daaffSRyusuke Konishi 1024622daaffSRyusuke Konishi isize = i_size_read(inode); 1025622daaffSRyusuke Konishi 1026622daaffSRyusuke Konishi blkoff = start >> blkbits; 1027622daaffSRyusuke Konishi end_blkoff = (start + len - 1) >> blkbits; 1028622daaffSRyusuke Konishi 1029622daaffSRyusuke Konishi delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff, 1030622daaffSRyusuke Konishi &delalloc_blkoff); 1031622daaffSRyusuke Konishi 1032622daaffSRyusuke Konishi do { 1033622daaffSRyusuke Konishi __u64 blkphy; 1034622daaffSRyusuke Konishi unsigned int maxblocks; 1035622daaffSRyusuke Konishi 1036622daaffSRyusuke Konishi if (delalloc_blklen && blkoff == delalloc_blkoff) { 1037622daaffSRyusuke Konishi if (size) { 1038622daaffSRyusuke Konishi /* End of the current extent */ 1039622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1040622daaffSRyusuke Konishi fieinfo, logical, phys, size, flags); 1041622daaffSRyusuke Konishi if (ret) 1042622daaffSRyusuke Konishi break; 1043622daaffSRyusuke Konishi } 1044622daaffSRyusuke Konishi if (blkoff > end_blkoff) 1045622daaffSRyusuke Konishi break; 1046622daaffSRyusuke Konishi 1047622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC; 1048622daaffSRyusuke Konishi logical = blkoff << blkbits; 1049622daaffSRyusuke Konishi phys = 0; 1050622daaffSRyusuke Konishi size = delalloc_blklen << blkbits; 1051622daaffSRyusuke Konishi 1052622daaffSRyusuke Konishi blkoff = delalloc_blkoff + delalloc_blklen; 1053622daaffSRyusuke Konishi delalloc_blklen = nilfs_find_uncommitted_extent( 1054622daaffSRyusuke Konishi inode, blkoff, &delalloc_blkoff); 1055622daaffSRyusuke Konishi continue; 1056622daaffSRyusuke Konishi } 1057622daaffSRyusuke Konishi 1058622daaffSRyusuke Konishi /* 1059622daaffSRyusuke Konishi * Limit the number of blocks that we look up so as 1060622daaffSRyusuke Konishi * not to get into the next delayed allocation extent. 1061622daaffSRyusuke Konishi */ 1062622daaffSRyusuke Konishi maxblocks = INT_MAX; 1063622daaffSRyusuke Konishi if (delalloc_blklen) 1064622daaffSRyusuke Konishi maxblocks = min_t(sector_t, delalloc_blkoff - blkoff, 1065622daaffSRyusuke Konishi maxblocks); 1066622daaffSRyusuke Konishi blkphy = 0; 1067622daaffSRyusuke Konishi 1068622daaffSRyusuke Konishi down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1069622daaffSRyusuke Konishi n = nilfs_bmap_lookup_contig( 1070622daaffSRyusuke Konishi NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks); 1071622daaffSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1072622daaffSRyusuke Konishi 1073622daaffSRyusuke Konishi if (n < 0) { 1074622daaffSRyusuke Konishi int past_eof; 1075622daaffSRyusuke Konishi 1076622daaffSRyusuke Konishi if (unlikely(n != -ENOENT)) 1077622daaffSRyusuke Konishi break; /* error */ 1078622daaffSRyusuke Konishi 1079622daaffSRyusuke Konishi /* HOLE */ 1080622daaffSRyusuke Konishi blkoff++; 1081622daaffSRyusuke Konishi past_eof = ((blkoff << blkbits) >= isize); 1082622daaffSRyusuke Konishi 1083622daaffSRyusuke Konishi if (size) { 1084622daaffSRyusuke Konishi /* End of the current extent */ 1085622daaffSRyusuke Konishi 1086622daaffSRyusuke Konishi if (past_eof) 1087622daaffSRyusuke Konishi flags |= FIEMAP_EXTENT_LAST; 1088622daaffSRyusuke Konishi 1089622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1090622daaffSRyusuke Konishi fieinfo, logical, phys, size, flags); 1091622daaffSRyusuke Konishi if (ret) 1092622daaffSRyusuke Konishi break; 1093622daaffSRyusuke Konishi size = 0; 1094622daaffSRyusuke Konishi } 1095622daaffSRyusuke Konishi if (blkoff > end_blkoff || past_eof) 1096622daaffSRyusuke Konishi break; 1097622daaffSRyusuke Konishi } else { 1098622daaffSRyusuke Konishi if (size) { 1099622daaffSRyusuke Konishi if (phys && blkphy << blkbits == phys + size) { 1100622daaffSRyusuke Konishi /* The current extent goes on */ 1101622daaffSRyusuke Konishi size += n << blkbits; 1102622daaffSRyusuke Konishi } else { 1103622daaffSRyusuke Konishi /* Terminate the current extent */ 1104622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1105622daaffSRyusuke Konishi fieinfo, logical, phys, size, 1106622daaffSRyusuke Konishi flags); 1107622daaffSRyusuke Konishi if (ret || blkoff > end_blkoff) 1108622daaffSRyusuke Konishi break; 1109622daaffSRyusuke Konishi 1110622daaffSRyusuke Konishi /* Start another extent */ 1111622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED; 1112622daaffSRyusuke Konishi logical = blkoff << blkbits; 1113622daaffSRyusuke Konishi phys = blkphy << blkbits; 1114622daaffSRyusuke Konishi size = n << blkbits; 1115622daaffSRyusuke Konishi } 1116622daaffSRyusuke Konishi } else { 1117622daaffSRyusuke Konishi /* Start a new extent */ 1118622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED; 1119622daaffSRyusuke Konishi logical = blkoff << blkbits; 1120622daaffSRyusuke Konishi phys = blkphy << blkbits; 1121622daaffSRyusuke Konishi size = n << blkbits; 1122622daaffSRyusuke Konishi } 1123622daaffSRyusuke Konishi blkoff += n; 1124622daaffSRyusuke Konishi } 1125622daaffSRyusuke Konishi cond_resched(); 1126622daaffSRyusuke Konishi } while (true); 1127622daaffSRyusuke Konishi 1128622daaffSRyusuke Konishi /* If ret is 1 then we just hit the end of the extent array */ 1129622daaffSRyusuke Konishi if (ret == 1) 1130622daaffSRyusuke Konishi ret = 0; 1131622daaffSRyusuke Konishi 1132622daaffSRyusuke Konishi mutex_unlock(&inode->i_mutex); 1133622daaffSRyusuke Konishi return ret; 1134622daaffSRyusuke Konishi } 1135