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> 29a27bb332SKent Overstreet #include <linux/aio.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 52*705304a8SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque); 53*705304a8SRyusuke 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 308d8d3d94bSAl Viro nilfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, 309d8d3d94bSAl Viro loff_t offset) 31005fe58fdSRyusuke Konishi { 31105fe58fdSRyusuke Konishi struct file *file = iocb->ki_filp; 3122d1b399bSMarco Stornelli struct address_space *mapping = file->f_mapping; 31305fe58fdSRyusuke Konishi struct inode *inode = file->f_mapping->host; 314a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 31505fe58fdSRyusuke Konishi ssize_t size; 31605fe58fdSRyusuke Konishi 31705fe58fdSRyusuke Konishi if (rw == WRITE) 31805fe58fdSRyusuke Konishi return 0; 31905fe58fdSRyusuke Konishi 32005fe58fdSRyusuke Konishi /* Needs synchronization with the cleaner */ 32131b14039SAl Viro size = blockdev_direct_IO(rw, iocb, inode, iter, offset, 32231b14039SAl Viro nilfs_get_block); 323eafdc7d1SChristoph Hellwig 324eafdc7d1SChristoph Hellwig /* 325eafdc7d1SChristoph Hellwig * In case of error extending write may have instantiated a few 326eafdc7d1SChristoph Hellwig * blocks outside i_size. Trim these off again. 327eafdc7d1SChristoph Hellwig */ 328eafdc7d1SChristoph Hellwig if (unlikely((rw & WRITE) && size < 0)) { 329eafdc7d1SChristoph Hellwig loff_t isize = i_size_read(inode); 330a6cbcd4aSAl Viro loff_t end = offset + count; 331eafdc7d1SChristoph Hellwig 332eafdc7d1SChristoph Hellwig if (end > isize) 3332d1b399bSMarco Stornelli nilfs_write_failed(mapping, end); 334eafdc7d1SChristoph Hellwig } 335eafdc7d1SChristoph Hellwig 33605fe58fdSRyusuke Konishi return size; 33705fe58fdSRyusuke Konishi } 33805fe58fdSRyusuke Konishi 3397f09410bSAlexey Dobriyan const struct address_space_operations nilfs_aops = { 34005fe58fdSRyusuke Konishi .writepage = nilfs_writepage, 34105fe58fdSRyusuke Konishi .readpage = nilfs_readpage, 34205fe58fdSRyusuke Konishi .writepages = nilfs_writepages, 34305fe58fdSRyusuke Konishi .set_page_dirty = nilfs_set_page_dirty, 34405fe58fdSRyusuke Konishi .readpages = nilfs_readpages, 34505fe58fdSRyusuke Konishi .write_begin = nilfs_write_begin, 34605fe58fdSRyusuke Konishi .write_end = nilfs_write_end, 34705fe58fdSRyusuke Konishi /* .releasepage = nilfs_releasepage, */ 34805fe58fdSRyusuke Konishi .invalidatepage = block_invalidatepage, 34905fe58fdSRyusuke Konishi .direct_IO = nilfs_direct_IO, 350258ef67eSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 35105fe58fdSRyusuke Konishi }; 35205fe58fdSRyusuke Konishi 353*705304a8SRyusuke Konishi static int nilfs_insert_inode_locked(struct inode *inode, 354*705304a8SRyusuke Konishi struct nilfs_root *root, 355*705304a8SRyusuke Konishi unsigned long ino) 356*705304a8SRyusuke Konishi { 357*705304a8SRyusuke Konishi struct nilfs_iget_args args = { 358*705304a8SRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 359*705304a8SRyusuke Konishi }; 360*705304a8SRyusuke Konishi 361*705304a8SRyusuke Konishi return insert_inode_locked4(inode, ino, nilfs_iget_test, &args); 362*705304a8SRyusuke Konishi } 363*705304a8SRyusuke Konishi 364c6e49e3fSAl Viro struct inode *nilfs_new_inode(struct inode *dir, umode_t mode) 36505fe58fdSRyusuke Konishi { 36605fe58fdSRyusuke Konishi struct super_block *sb = dir->i_sb; 367e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info; 36805fe58fdSRyusuke Konishi struct inode *inode; 36905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii; 3704d8d9293SRyusuke Konishi struct nilfs_root *root; 37105fe58fdSRyusuke Konishi int err = -ENOMEM; 37205fe58fdSRyusuke Konishi ino_t ino; 37305fe58fdSRyusuke Konishi 37405fe58fdSRyusuke Konishi inode = new_inode(sb); 37505fe58fdSRyusuke Konishi if (unlikely(!inode)) 37605fe58fdSRyusuke Konishi goto failed; 37705fe58fdSRyusuke Konishi 37805fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 37905fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 38005fe58fdSRyusuke Konishi 3814d8d9293SRyusuke Konishi root = NILFS_I(dir)->i_root; 38205fe58fdSRyusuke Konishi ii = NILFS_I(inode); 38305fe58fdSRyusuke Konishi ii->i_state = 1 << NILFS_I_NEW; 3844d8d9293SRyusuke Konishi ii->i_root = root; 38505fe58fdSRyusuke Konishi 386e912a5b6SRyusuke Konishi err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); 38705fe58fdSRyusuke Konishi if (unlikely(err)) 38805fe58fdSRyusuke Konishi goto failed_ifile_create_inode; 38905fe58fdSRyusuke Konishi /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 39005fe58fdSRyusuke Konishi 391e5f7f848SVyacheslav Dubeyko atomic64_inc(&root->inodes_count); 39273459dccSDmitry Monakhov inode_init_owner(inode, dir, mode); 39305fe58fdSRyusuke Konishi inode->i_ino = ino; 39405fe58fdSRyusuke Konishi inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 39505fe58fdSRyusuke Konishi 39605fe58fdSRyusuke Konishi if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 39705fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, NULL); 39805fe58fdSRyusuke Konishi if (err < 0) 399*705304a8SRyusuke Konishi goto failed_after_creation; 40005fe58fdSRyusuke Konishi 40105fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 40205fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 40305fe58fdSRyusuke Konishi } 40405fe58fdSRyusuke Konishi 405b253a3e4SRyusuke Konishi ii->i_flags = nilfs_mask_flags( 406b253a3e4SRyusuke Konishi mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED); 40705fe58fdSRyusuke Konishi 40805fe58fdSRyusuke Konishi /* ii->i_file_acl = 0; */ 40905fe58fdSRyusuke Konishi /* ii->i_dir_acl = 0; */ 41005fe58fdSRyusuke Konishi ii->i_dir_start_lookup = 0; 41105fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 4129b1fc4e4SRyusuke Konishi spin_lock(&nilfs->ns_next_gen_lock); 4139b1fc4e4SRyusuke Konishi inode->i_generation = nilfs->ns_next_generation++; 4149b1fc4e4SRyusuke Konishi spin_unlock(&nilfs->ns_next_gen_lock); 415*705304a8SRyusuke Konishi if (nilfs_insert_inode_locked(inode, root, ino) < 0) { 416*705304a8SRyusuke Konishi err = -EIO; 417*705304a8SRyusuke Konishi goto failed_after_creation; 418*705304a8SRyusuke Konishi } 41905fe58fdSRyusuke Konishi 42005fe58fdSRyusuke Konishi err = nilfs_init_acl(inode, dir); 42105fe58fdSRyusuke Konishi if (unlikely(err)) 422*705304a8SRyusuke Konishi goto failed_after_creation; /* never occur. When supporting 42305fe58fdSRyusuke Konishi nilfs_init_acl(), proper cancellation of 42405fe58fdSRyusuke Konishi above jobs should be considered */ 42505fe58fdSRyusuke Konishi 42605fe58fdSRyusuke Konishi return inode; 42705fe58fdSRyusuke Konishi 428*705304a8SRyusuke Konishi failed_after_creation: 4296d6b77f1SMiklos Szeredi clear_nlink(inode); 430*705304a8SRyusuke Konishi unlock_new_inode(inode); 43105fe58fdSRyusuke Konishi iput(inode); /* raw_inode will be deleted through 432*705304a8SRyusuke Konishi nilfs_evict_inode() */ 43305fe58fdSRyusuke Konishi goto failed; 43405fe58fdSRyusuke Konishi 43505fe58fdSRyusuke Konishi failed_ifile_create_inode: 43605fe58fdSRyusuke Konishi make_bad_inode(inode); 43705fe58fdSRyusuke Konishi iput(inode); /* if i_nlink == 1, generic_forget_inode() will be 43805fe58fdSRyusuke Konishi called */ 43905fe58fdSRyusuke Konishi failed: 44005fe58fdSRyusuke Konishi return ERR_PTR(err); 44105fe58fdSRyusuke Konishi } 44205fe58fdSRyusuke Konishi 44305fe58fdSRyusuke Konishi void nilfs_set_inode_flags(struct inode *inode) 44405fe58fdSRyusuke Konishi { 44505fe58fdSRyusuke Konishi unsigned int flags = NILFS_I(inode)->i_flags; 44605fe58fdSRyusuke Konishi 44705fe58fdSRyusuke Konishi inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | 44805fe58fdSRyusuke Konishi S_DIRSYNC); 449f0c9f242SRyusuke Konishi if (flags & FS_SYNC_FL) 45005fe58fdSRyusuke Konishi inode->i_flags |= S_SYNC; 451f0c9f242SRyusuke Konishi if (flags & FS_APPEND_FL) 45205fe58fdSRyusuke Konishi inode->i_flags |= S_APPEND; 453f0c9f242SRyusuke Konishi if (flags & FS_IMMUTABLE_FL) 45405fe58fdSRyusuke Konishi inode->i_flags |= S_IMMUTABLE; 455f0c9f242SRyusuke Konishi if (flags & FS_NOATIME_FL) 45605fe58fdSRyusuke Konishi inode->i_flags |= S_NOATIME; 457f0c9f242SRyusuke Konishi if (flags & FS_DIRSYNC_FL) 45805fe58fdSRyusuke Konishi inode->i_flags |= S_DIRSYNC; 45905fe58fdSRyusuke Konishi mapping_set_gfp_mask(inode->i_mapping, 46005fe58fdSRyusuke Konishi mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 46105fe58fdSRyusuke Konishi } 46205fe58fdSRyusuke Konishi 46305fe58fdSRyusuke Konishi int nilfs_read_inode_common(struct inode *inode, 46405fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode) 46505fe58fdSRyusuke Konishi { 46605fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 46705fe58fdSRyusuke Konishi int err; 46805fe58fdSRyusuke Konishi 46905fe58fdSRyusuke Konishi inode->i_mode = le16_to_cpu(raw_inode->i_mode); 470305d3d0dSEric W. Biederman i_uid_write(inode, le32_to_cpu(raw_inode->i_uid)); 471305d3d0dSEric W. Biederman i_gid_write(inode, le32_to_cpu(raw_inode->i_gid)); 472bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 47305fe58fdSRyusuke Konishi inode->i_size = le64_to_cpu(raw_inode->i_size); 47405fe58fdSRyusuke Konishi inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 47505fe58fdSRyusuke Konishi inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); 47605fe58fdSRyusuke Konishi inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 47761239230SRyusuke Konishi inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 47861239230SRyusuke Konishi inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); 47961239230SRyusuke Konishi inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 480*705304a8SRyusuke Konishi if (inode->i_nlink == 0) 481*705304a8SRyusuke Konishi return -ESTALE; /* this inode is deleted */ 48205fe58fdSRyusuke Konishi 48305fe58fdSRyusuke Konishi inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 48405fe58fdSRyusuke Konishi ii->i_flags = le32_to_cpu(raw_inode->i_flags); 48505fe58fdSRyusuke Konishi #if 0 48605fe58fdSRyusuke Konishi ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 48705fe58fdSRyusuke Konishi ii->i_dir_acl = S_ISREG(inode->i_mode) ? 48805fe58fdSRyusuke Konishi 0 : le32_to_cpu(raw_inode->i_dir_acl); 48905fe58fdSRyusuke Konishi #endif 4903cc811bfSRyusuke Konishi ii->i_dir_start_lookup = 0; 49105fe58fdSRyusuke Konishi inode->i_generation = le32_to_cpu(raw_inode->i_generation); 49205fe58fdSRyusuke Konishi 49305fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 49405fe58fdSRyusuke Konishi S_ISLNK(inode->i_mode)) { 49505fe58fdSRyusuke Konishi err = nilfs_bmap_read(ii->i_bmap, raw_inode); 49605fe58fdSRyusuke Konishi if (err < 0) 49705fe58fdSRyusuke Konishi return err; 49805fe58fdSRyusuke Konishi set_bit(NILFS_I_BMAP, &ii->i_state); 49905fe58fdSRyusuke Konishi /* No lock is needed; iget() ensures it. */ 50005fe58fdSRyusuke Konishi } 50105fe58fdSRyusuke Konishi return 0; 50205fe58fdSRyusuke Konishi } 50305fe58fdSRyusuke Konishi 504e912a5b6SRyusuke Konishi static int __nilfs_read_inode(struct super_block *sb, 505e912a5b6SRyusuke Konishi struct nilfs_root *root, unsigned long ino, 50605fe58fdSRyusuke Konishi struct inode *inode) 50705fe58fdSRyusuke Konishi { 508e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info; 50905fe58fdSRyusuke Konishi struct buffer_head *bh; 51005fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 51105fe58fdSRyusuke Konishi int err; 51205fe58fdSRyusuke Konishi 513365e215cSRyusuke Konishi down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 514e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); 51505fe58fdSRyusuke Konishi if (unlikely(err)) 51605fe58fdSRyusuke Konishi goto bad_inode; 51705fe58fdSRyusuke Konishi 518e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); 51905fe58fdSRyusuke Konishi 5201b2f5a64SRyusuke Konishi err = nilfs_read_inode_common(inode, raw_inode); 5211b2f5a64SRyusuke Konishi if (err) 52205fe58fdSRyusuke Konishi goto failed_unmap; 52305fe58fdSRyusuke Konishi 52405fe58fdSRyusuke Konishi if (S_ISREG(inode->i_mode)) { 52505fe58fdSRyusuke Konishi inode->i_op = &nilfs_file_inode_operations; 52605fe58fdSRyusuke Konishi inode->i_fop = &nilfs_file_operations; 52705fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 52805fe58fdSRyusuke Konishi } else if (S_ISDIR(inode->i_mode)) { 52905fe58fdSRyusuke Konishi inode->i_op = &nilfs_dir_inode_operations; 53005fe58fdSRyusuke Konishi inode->i_fop = &nilfs_dir_operations; 53105fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 53205fe58fdSRyusuke Konishi } else if (S_ISLNK(inode->i_mode)) { 53305fe58fdSRyusuke Konishi inode->i_op = &nilfs_symlink_inode_operations; 53405fe58fdSRyusuke Konishi inode->i_mapping->a_ops = &nilfs_aops; 53505fe58fdSRyusuke Konishi } else { 53605fe58fdSRyusuke Konishi inode->i_op = &nilfs_special_inode_operations; 53705fe58fdSRyusuke Konishi init_special_inode( 53805fe58fdSRyusuke Konishi inode, inode->i_mode, 539cdce214eSRyusuke Konishi huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 54005fe58fdSRyusuke Konishi } 541e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 54205fe58fdSRyusuke Konishi brelse(bh); 543365e215cSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 54405fe58fdSRyusuke Konishi nilfs_set_inode_flags(inode); 54505fe58fdSRyusuke Konishi return 0; 54605fe58fdSRyusuke Konishi 54705fe58fdSRyusuke Konishi failed_unmap: 548e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(root->ifile, ino, bh); 54905fe58fdSRyusuke Konishi brelse(bh); 55005fe58fdSRyusuke Konishi 55105fe58fdSRyusuke Konishi bad_inode: 552365e215cSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 55305fe58fdSRyusuke Konishi return err; 55405fe58fdSRyusuke Konishi } 55505fe58fdSRyusuke Konishi 5560e14a359SRyusuke Konishi static int nilfs_iget_test(struct inode *inode, void *opaque) 5570e14a359SRyusuke Konishi { 5580e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 5590e14a359SRyusuke Konishi struct nilfs_inode_info *ii; 5600e14a359SRyusuke Konishi 5614d8d9293SRyusuke Konishi if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) 5620e14a359SRyusuke Konishi return 0; 5630e14a359SRyusuke Konishi 5640e14a359SRyusuke Konishi ii = NILFS_I(inode); 5650e14a359SRyusuke Konishi if (!test_bit(NILFS_I_GCINODE, &ii->i_state)) 5660e14a359SRyusuke Konishi return !args->for_gc; 5670e14a359SRyusuke Konishi 5680e14a359SRyusuke Konishi return args->for_gc && args->cno == ii->i_cno; 5690e14a359SRyusuke Konishi } 5700e14a359SRyusuke Konishi 5710e14a359SRyusuke Konishi static int nilfs_iget_set(struct inode *inode, void *opaque) 5720e14a359SRyusuke Konishi { 5730e14a359SRyusuke Konishi struct nilfs_iget_args *args = opaque; 5740e14a359SRyusuke Konishi 5750e14a359SRyusuke Konishi inode->i_ino = args->ino; 5760e14a359SRyusuke Konishi if (args->for_gc) { 5770e14a359SRyusuke Konishi NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE; 5780e14a359SRyusuke Konishi NILFS_I(inode)->i_cno = args->cno; 5794d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = NULL; 5804d8d9293SRyusuke Konishi } else { 5814d8d9293SRyusuke Konishi if (args->root && args->ino == NILFS_ROOT_INO) 5824d8d9293SRyusuke Konishi nilfs_get_root(args->root); 5834d8d9293SRyusuke Konishi NILFS_I(inode)->i_root = args->root; 5840e14a359SRyusuke Konishi } 5850e14a359SRyusuke Konishi return 0; 5860e14a359SRyusuke Konishi } 5870e14a359SRyusuke Konishi 588032dbb3bSRyusuke Konishi struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, 589032dbb3bSRyusuke Konishi unsigned long ino) 590032dbb3bSRyusuke Konishi { 591032dbb3bSRyusuke Konishi struct nilfs_iget_args args = { 592032dbb3bSRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 593032dbb3bSRyusuke Konishi }; 594032dbb3bSRyusuke Konishi 595032dbb3bSRyusuke Konishi return ilookup5(sb, ino, nilfs_iget_test, &args); 596032dbb3bSRyusuke Konishi } 597032dbb3bSRyusuke Konishi 598f1e89c86SRyusuke Konishi struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, 5994d8d9293SRyusuke Konishi unsigned long ino) 60005fe58fdSRyusuke Konishi { 6014d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 6024d8d9293SRyusuke Konishi .ino = ino, .root = root, .cno = 0, .for_gc = 0 6034d8d9293SRyusuke Konishi }; 604f1e89c86SRyusuke Konishi 605f1e89c86SRyusuke Konishi return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 606f1e89c86SRyusuke Konishi } 607f1e89c86SRyusuke Konishi 608f1e89c86SRyusuke Konishi struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, 609f1e89c86SRyusuke Konishi unsigned long ino) 610f1e89c86SRyusuke Konishi { 61105fe58fdSRyusuke Konishi struct inode *inode; 61205fe58fdSRyusuke Konishi int err; 61305fe58fdSRyusuke Konishi 614f1e89c86SRyusuke Konishi inode = nilfs_iget_locked(sb, root, ino); 61505fe58fdSRyusuke Konishi if (unlikely(!inode)) 61605fe58fdSRyusuke Konishi return ERR_PTR(-ENOMEM); 61705fe58fdSRyusuke Konishi if (!(inode->i_state & I_NEW)) 61805fe58fdSRyusuke Konishi return inode; 61905fe58fdSRyusuke Konishi 620e912a5b6SRyusuke Konishi err = __nilfs_read_inode(sb, root, ino, inode); 62105fe58fdSRyusuke Konishi if (unlikely(err)) { 62205fe58fdSRyusuke Konishi iget_failed(inode); 62305fe58fdSRyusuke Konishi return ERR_PTR(err); 62405fe58fdSRyusuke Konishi } 62505fe58fdSRyusuke Konishi unlock_new_inode(inode); 62605fe58fdSRyusuke Konishi return inode; 62705fe58fdSRyusuke Konishi } 62805fe58fdSRyusuke Konishi 629263d90ceSRyusuke Konishi struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino, 630263d90ceSRyusuke Konishi __u64 cno) 631263d90ceSRyusuke Konishi { 6324d8d9293SRyusuke Konishi struct nilfs_iget_args args = { 6334d8d9293SRyusuke Konishi .ino = ino, .root = NULL, .cno = cno, .for_gc = 1 6344d8d9293SRyusuke Konishi }; 635263d90ceSRyusuke Konishi struct inode *inode; 636263d90ceSRyusuke Konishi int err; 637263d90ceSRyusuke Konishi 638263d90ceSRyusuke Konishi inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 639263d90ceSRyusuke Konishi if (unlikely(!inode)) 640263d90ceSRyusuke Konishi return ERR_PTR(-ENOMEM); 641263d90ceSRyusuke Konishi if (!(inode->i_state & I_NEW)) 642263d90ceSRyusuke Konishi return inode; 643263d90ceSRyusuke Konishi 644263d90ceSRyusuke Konishi err = nilfs_init_gcinode(inode); 645263d90ceSRyusuke Konishi if (unlikely(err)) { 646263d90ceSRyusuke Konishi iget_failed(inode); 647263d90ceSRyusuke Konishi return ERR_PTR(err); 648263d90ceSRyusuke Konishi } 649263d90ceSRyusuke Konishi unlock_new_inode(inode); 650263d90ceSRyusuke Konishi return inode; 651263d90ceSRyusuke Konishi } 652263d90ceSRyusuke Konishi 65305fe58fdSRyusuke Konishi void nilfs_write_inode_common(struct inode *inode, 65405fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode, int has_bmap) 65505fe58fdSRyusuke Konishi { 65605fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 65705fe58fdSRyusuke Konishi 65805fe58fdSRyusuke Konishi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 659305d3d0dSEric W. Biederman raw_inode->i_uid = cpu_to_le32(i_uid_read(inode)); 660305d3d0dSEric W. Biederman raw_inode->i_gid = cpu_to_le32(i_gid_read(inode)); 66105fe58fdSRyusuke Konishi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 66205fe58fdSRyusuke Konishi raw_inode->i_size = cpu_to_le64(inode->i_size); 66305fe58fdSRyusuke Konishi raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 66405fe58fdSRyusuke Konishi raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); 66561239230SRyusuke Konishi raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 66661239230SRyusuke Konishi raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); 66705fe58fdSRyusuke Konishi raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 66805fe58fdSRyusuke Konishi 66905fe58fdSRyusuke Konishi raw_inode->i_flags = cpu_to_le32(ii->i_flags); 67005fe58fdSRyusuke Konishi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 67105fe58fdSRyusuke Konishi 67256eb5538SRyusuke Konishi if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) { 67356eb5538SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 67456eb5538SRyusuke Konishi 67556eb5538SRyusuke Konishi /* zero-fill unused portion in the case of super root block */ 67656eb5538SRyusuke Konishi raw_inode->i_xattr = 0; 67756eb5538SRyusuke Konishi raw_inode->i_pad = 0; 67856eb5538SRyusuke Konishi memset((void *)raw_inode + sizeof(*raw_inode), 0, 67956eb5538SRyusuke Konishi nilfs->ns_inode_size - sizeof(*raw_inode)); 68056eb5538SRyusuke Konishi } 68156eb5538SRyusuke Konishi 68205fe58fdSRyusuke Konishi if (has_bmap) 68305fe58fdSRyusuke Konishi nilfs_bmap_write(ii->i_bmap, raw_inode); 68405fe58fdSRyusuke Konishi else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 68505fe58fdSRyusuke Konishi raw_inode->i_device_code = 686cdce214eSRyusuke Konishi cpu_to_le64(huge_encode_dev(inode->i_rdev)); 68705fe58fdSRyusuke Konishi /* When extending inode, nilfs->ns_inode_size should be checked 68805fe58fdSRyusuke Konishi for substitutions of appended fields */ 68905fe58fdSRyusuke Konishi } 69005fe58fdSRyusuke Konishi 691b9f66140SAndreas Rohner void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags) 69205fe58fdSRyusuke Konishi { 69305fe58fdSRyusuke Konishi ino_t ino = inode->i_ino; 69405fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 695e912a5b6SRyusuke Konishi struct inode *ifile = ii->i_root->ifile; 69605fe58fdSRyusuke Konishi struct nilfs_inode *raw_inode; 69705fe58fdSRyusuke Konishi 698e912a5b6SRyusuke Konishi raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); 69905fe58fdSRyusuke Konishi 70005fe58fdSRyusuke Konishi if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 701e912a5b6SRyusuke Konishi memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); 702b9f66140SAndreas Rohner if (flags & I_DIRTY_DATASYNC) 703b9f66140SAndreas Rohner set_bit(NILFS_I_INODE_SYNC, &ii->i_state); 70405fe58fdSRyusuke Konishi 70505fe58fdSRyusuke Konishi nilfs_write_inode_common(inode, raw_inode, 0); 70605fe58fdSRyusuke Konishi /* XXX: call with has_bmap = 0 is a workaround to avoid 70705fe58fdSRyusuke Konishi deadlock of bmap. This delays update of i_bmap to just 70805fe58fdSRyusuke Konishi before writing */ 709e912a5b6SRyusuke Konishi nilfs_ifile_unmap_inode(ifile, ino, ibh); 71005fe58fdSRyusuke Konishi } 71105fe58fdSRyusuke Konishi 71205fe58fdSRyusuke Konishi #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 71305fe58fdSRyusuke Konishi 71405fe58fdSRyusuke Konishi static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 71505fe58fdSRyusuke Konishi unsigned long from) 71605fe58fdSRyusuke Konishi { 71705fe58fdSRyusuke Konishi unsigned long b; 71805fe58fdSRyusuke Konishi int ret; 71905fe58fdSRyusuke Konishi 72005fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 72105fe58fdSRyusuke Konishi return; 72205fe58fdSRyusuke Konishi repeat: 72305fe58fdSRyusuke Konishi ret = nilfs_bmap_last_key(ii->i_bmap, &b); 72405fe58fdSRyusuke Konishi if (ret == -ENOENT) 72505fe58fdSRyusuke Konishi return; 72605fe58fdSRyusuke Konishi else if (ret < 0) 72705fe58fdSRyusuke Konishi goto failed; 72805fe58fdSRyusuke Konishi 72905fe58fdSRyusuke Konishi if (b < from) 73005fe58fdSRyusuke Konishi return; 73105fe58fdSRyusuke Konishi 73205fe58fdSRyusuke Konishi b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 73305fe58fdSRyusuke Konishi ret = nilfs_bmap_truncate(ii->i_bmap, b); 73405fe58fdSRyusuke Konishi nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 73505fe58fdSRyusuke Konishi if (!ret || (ret == -ENOMEM && 73605fe58fdSRyusuke Konishi nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 73705fe58fdSRyusuke Konishi goto repeat; 73805fe58fdSRyusuke Konishi 73905fe58fdSRyusuke Konishi failed: 74005fe58fdSRyusuke Konishi nilfs_warning(ii->vfs_inode.i_sb, __func__, 74105fe58fdSRyusuke Konishi "failed to truncate bmap (ino=%lu, err=%d)", 74205fe58fdSRyusuke Konishi ii->vfs_inode.i_ino, ret); 74305fe58fdSRyusuke Konishi } 74405fe58fdSRyusuke Konishi 74505fe58fdSRyusuke Konishi void nilfs_truncate(struct inode *inode) 74605fe58fdSRyusuke Konishi { 74705fe58fdSRyusuke Konishi unsigned long blkoff; 74805fe58fdSRyusuke Konishi unsigned int blocksize; 74905fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 75005fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 75105fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 75205fe58fdSRyusuke Konishi 75305fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 75405fe58fdSRyusuke Konishi return; 75505fe58fdSRyusuke Konishi if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 75605fe58fdSRyusuke Konishi return; 75705fe58fdSRyusuke Konishi 75805fe58fdSRyusuke Konishi blocksize = sb->s_blocksize; 75905fe58fdSRyusuke Konishi blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 7601f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 76105fe58fdSRyusuke Konishi 76205fe58fdSRyusuke Konishi block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 76305fe58fdSRyusuke Konishi 76405fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, blkoff); 76505fe58fdSRyusuke Konishi 76605fe58fdSRyusuke Konishi inode->i_mtime = inode->i_ctime = CURRENT_TIME; 76705fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 76805fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 76905fe58fdSRyusuke Konishi 770abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 771bcbc8c64SRyusuke Konishi nilfs_set_file_dirty(inode, 0); 77247420c79SRyusuke Konishi nilfs_transaction_commit(sb); 77305fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 77405fe58fdSRyusuke Konishi But truncate has no return value. */ 77505fe58fdSRyusuke Konishi } 77605fe58fdSRyusuke Konishi 7776fd1e5c9SAl Viro static void nilfs_clear_inode(struct inode *inode) 7786fd1e5c9SAl Viro { 7796fd1e5c9SAl Viro struct nilfs_inode_info *ii = NILFS_I(inode); 780518d1a6aSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 7816fd1e5c9SAl Viro 7826fd1e5c9SAl Viro /* 7836fd1e5c9SAl Viro * Free resources allocated in nilfs_read_inode(), here. 7846fd1e5c9SAl Viro */ 7856fd1e5c9SAl Viro BUG_ON(!list_empty(&ii->i_dirty)); 7866fd1e5c9SAl Viro brelse(ii->i_bh); 7876fd1e5c9SAl Viro ii->i_bh = NULL; 7886fd1e5c9SAl Viro 789518d1a6aSRyusuke Konishi if (mdi && mdi->mi_palloc_cache) 790518d1a6aSRyusuke Konishi nilfs_palloc_destroy_cache(inode); 791518d1a6aSRyusuke Konishi 7926fd1e5c9SAl Viro if (test_bit(NILFS_I_BMAP, &ii->i_state)) 7936fd1e5c9SAl Viro nilfs_bmap_clear(ii->i_bmap); 7946fd1e5c9SAl Viro 7956fd1e5c9SAl Viro nilfs_btnode_cache_clear(&ii->i_btnode_cache); 7964d8d9293SRyusuke Konishi 7974d8d9293SRyusuke Konishi if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) 7984d8d9293SRyusuke Konishi nilfs_put_root(ii->i_root); 7996fd1e5c9SAl Viro } 8006fd1e5c9SAl Viro 8016fd1e5c9SAl Viro void nilfs_evict_inode(struct inode *inode) 80205fe58fdSRyusuke Konishi { 80305fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 80405fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 80505fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 80625b18d39SRyusuke Konishi int ret; 80705fe58fdSRyusuke Konishi 8084d8d9293SRyusuke Konishi if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { 80991b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 810dbd5768fSJan Kara clear_inode(inode); 8116fd1e5c9SAl Viro nilfs_clear_inode(inode); 81205fe58fdSRyusuke Konishi return; 81305fe58fdSRyusuke Konishi } 8141f5abe7eSRyusuke Konishi nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 8151f5abe7eSRyusuke Konishi 81691b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 81705fe58fdSRyusuke Konishi 818e912a5b6SRyusuke Konishi /* TODO: some of the following operations may fail. */ 81905fe58fdSRyusuke Konishi nilfs_truncate_bmap(ii, 0); 820abdb318bSJiro SEKIBA nilfs_mark_inode_dirty(inode); 821dbd5768fSJan Kara clear_inode(inode); 822e912a5b6SRyusuke Konishi 82325b18d39SRyusuke Konishi ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); 82425b18d39SRyusuke Konishi if (!ret) 825e5f7f848SVyacheslav Dubeyko atomic64_dec(&ii->i_root->inodes_count); 826e912a5b6SRyusuke Konishi 8276fd1e5c9SAl Viro nilfs_clear_inode(inode); 828e912a5b6SRyusuke Konishi 82905fe58fdSRyusuke Konishi if (IS_SYNC(inode)) 83005fe58fdSRyusuke Konishi nilfs_set_transaction_flag(NILFS_TI_SYNC); 83147420c79SRyusuke Konishi nilfs_transaction_commit(sb); 83205fe58fdSRyusuke Konishi /* May construct a logical segment and may fail in sync mode. 83305fe58fdSRyusuke Konishi But delete_inode has no return value. */ 83405fe58fdSRyusuke Konishi } 83505fe58fdSRyusuke Konishi 83605fe58fdSRyusuke Konishi int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) 83705fe58fdSRyusuke Konishi { 83805fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 83905fe58fdSRyusuke Konishi struct inode *inode = dentry->d_inode; 84005fe58fdSRyusuke Konishi struct super_block *sb = inode->i_sb; 84147420c79SRyusuke Konishi int err; 84205fe58fdSRyusuke Konishi 84305fe58fdSRyusuke Konishi err = inode_change_ok(inode, iattr); 84405fe58fdSRyusuke Konishi if (err) 84505fe58fdSRyusuke Konishi return err; 84605fe58fdSRyusuke Konishi 84705fe58fdSRyusuke Konishi err = nilfs_transaction_begin(sb, &ti, 0); 84805fe58fdSRyusuke Konishi if (unlikely(err)) 84905fe58fdSRyusuke Konishi return err; 85047420c79SRyusuke Konishi 8511025774cSChristoph Hellwig if ((iattr->ia_valid & ATTR_SIZE) && 8521025774cSChristoph Hellwig iattr->ia_size != i_size_read(inode)) { 853562c72aaSChristoph Hellwig inode_dio_wait(inode); 8542d1b399bSMarco Stornelli truncate_setsize(inode, iattr->ia_size); 8552d1b399bSMarco Stornelli nilfs_truncate(inode); 8561025774cSChristoph Hellwig } 8571025774cSChristoph Hellwig 8581025774cSChristoph Hellwig setattr_copy(inode, iattr); 8591025774cSChristoph Hellwig mark_inode_dirty(inode); 8601025774cSChristoph Hellwig 8611025774cSChristoph Hellwig if (iattr->ia_valid & ATTR_MODE) { 8621025774cSChristoph Hellwig err = nilfs_acl_chmod(inode); 8631025774cSChristoph Hellwig if (unlikely(err)) 8641025774cSChristoph Hellwig goto out_err; 8651025774cSChristoph Hellwig } 8661025774cSChristoph Hellwig 8671025774cSChristoph Hellwig return nilfs_transaction_commit(sb); 8681025774cSChristoph Hellwig 8691025774cSChristoph Hellwig out_err: 8701025774cSChristoph Hellwig nilfs_transaction_abort(sb); 87147420c79SRyusuke Konishi return err; 87205fe58fdSRyusuke Konishi } 87305fe58fdSRyusuke Konishi 87410556cb2SAl Viro int nilfs_permission(struct inode *inode, int mask) 875dc3d3b81SRyusuke Konishi { 876730e908fSAl Viro struct nilfs_root *root = NILFS_I(inode)->i_root; 877dc3d3b81SRyusuke Konishi if ((mask & MAY_WRITE) && root && 878dc3d3b81SRyusuke Konishi root->cno != NILFS_CPTREE_CURRENT_CNO) 879dc3d3b81SRyusuke Konishi return -EROFS; /* snapshot is not writable */ 880dc3d3b81SRyusuke Konishi 8812830ba7fSAl Viro return generic_permission(inode, mask); 882dc3d3b81SRyusuke Konishi } 883dc3d3b81SRyusuke Konishi 884bcbc8c64SRyusuke Konishi int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh) 88505fe58fdSRyusuke Konishi { 886e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 88705fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 88805fe58fdSRyusuke Konishi int err; 88905fe58fdSRyusuke Konishi 890693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 89105fe58fdSRyusuke Konishi if (ii->i_bh == NULL) { 892693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 893e912a5b6SRyusuke Konishi err = nilfs_ifile_get_inode_block(ii->i_root->ifile, 894e912a5b6SRyusuke Konishi inode->i_ino, pbh); 89505fe58fdSRyusuke Konishi if (unlikely(err)) 89605fe58fdSRyusuke Konishi return err; 897693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 89805fe58fdSRyusuke Konishi if (ii->i_bh == NULL) 89905fe58fdSRyusuke Konishi ii->i_bh = *pbh; 90005fe58fdSRyusuke Konishi else { 90105fe58fdSRyusuke Konishi brelse(*pbh); 90205fe58fdSRyusuke Konishi *pbh = ii->i_bh; 90305fe58fdSRyusuke Konishi } 90405fe58fdSRyusuke Konishi } else 90505fe58fdSRyusuke Konishi *pbh = ii->i_bh; 90605fe58fdSRyusuke Konishi 90705fe58fdSRyusuke Konishi get_bh(*pbh); 908693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 90905fe58fdSRyusuke Konishi return 0; 91005fe58fdSRyusuke Konishi } 91105fe58fdSRyusuke Konishi 91205fe58fdSRyusuke Konishi int nilfs_inode_dirty(struct inode *inode) 91305fe58fdSRyusuke Konishi { 91405fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 915e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 91605fe58fdSRyusuke Konishi int ret = 0; 91705fe58fdSRyusuke Konishi 91805fe58fdSRyusuke Konishi if (!list_empty(&ii->i_dirty)) { 919693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 92005fe58fdSRyusuke Konishi ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 92105fe58fdSRyusuke Konishi test_bit(NILFS_I_BUSY, &ii->i_state); 922693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 92305fe58fdSRyusuke Konishi } 92405fe58fdSRyusuke Konishi return ret; 92505fe58fdSRyusuke Konishi } 92605fe58fdSRyusuke Konishi 927bcbc8c64SRyusuke Konishi int nilfs_set_file_dirty(struct inode *inode, unsigned nr_dirty) 92805fe58fdSRyusuke Konishi { 92905fe58fdSRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 930e3154e97SRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 93105fe58fdSRyusuke Konishi 932693dd321SRyusuke Konishi atomic_add(nr_dirty, &nilfs->ns_ndirtyblks); 93305fe58fdSRyusuke Konishi 934458c5b08SRyusuke Konishi if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 93505fe58fdSRyusuke Konishi return 0; 93605fe58fdSRyusuke Konishi 937693dd321SRyusuke Konishi spin_lock(&nilfs->ns_inode_lock); 93805fe58fdSRyusuke Konishi if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 93905fe58fdSRyusuke Konishi !test_bit(NILFS_I_BUSY, &ii->i_state)) { 94005fe58fdSRyusuke Konishi /* Because this routine may race with nilfs_dispose_list(), 94105fe58fdSRyusuke Konishi we have to check NILFS_I_QUEUED here, too. */ 94205fe58fdSRyusuke Konishi if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 94305fe58fdSRyusuke Konishi /* This will happen when somebody is freeing 94405fe58fdSRyusuke Konishi this inode. */ 945693dd321SRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 94605fe58fdSRyusuke Konishi "cannot get inode (ino=%lu)\n", 94705fe58fdSRyusuke Konishi inode->i_ino); 948693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 94905fe58fdSRyusuke Konishi return -EINVAL; /* NILFS_I_DIRTY may remain for 95005fe58fdSRyusuke Konishi freeing inode */ 95105fe58fdSRyusuke Konishi } 952eaae0f37SNicolas Kaiser list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files); 95305fe58fdSRyusuke Konishi set_bit(NILFS_I_QUEUED, &ii->i_state); 95405fe58fdSRyusuke Konishi } 955693dd321SRyusuke Konishi spin_unlock(&nilfs->ns_inode_lock); 95605fe58fdSRyusuke Konishi return 0; 95705fe58fdSRyusuke Konishi } 95805fe58fdSRyusuke Konishi 959b9f66140SAndreas Rohner int __nilfs_mark_inode_dirty(struct inode *inode, int flags) 96005fe58fdSRyusuke Konishi { 96105fe58fdSRyusuke Konishi struct buffer_head *ibh; 96205fe58fdSRyusuke Konishi int err; 96305fe58fdSRyusuke Konishi 964bcbc8c64SRyusuke Konishi err = nilfs_load_inode_block(inode, &ibh); 96505fe58fdSRyusuke Konishi if (unlikely(err)) { 96605fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 96705fe58fdSRyusuke Konishi "failed to reget inode block.\n"); 96805fe58fdSRyusuke Konishi return err; 96905fe58fdSRyusuke Konishi } 970b9f66140SAndreas Rohner nilfs_update_inode(inode, ibh, flags); 9715fc7b141SRyusuke Konishi mark_buffer_dirty(ibh); 972e912a5b6SRyusuke Konishi nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); 97305fe58fdSRyusuke Konishi brelse(ibh); 97405fe58fdSRyusuke Konishi return 0; 97505fe58fdSRyusuke Konishi } 97605fe58fdSRyusuke Konishi 97705fe58fdSRyusuke Konishi /** 97805fe58fdSRyusuke Konishi * nilfs_dirty_inode - reflect changes on given inode to an inode block. 97905fe58fdSRyusuke Konishi * @inode: inode of the file to be registered. 98005fe58fdSRyusuke Konishi * 98105fe58fdSRyusuke Konishi * nilfs_dirty_inode() loads a inode block containing the specified 98205fe58fdSRyusuke Konishi * @inode and copies data from a nilfs_inode to a corresponding inode 98305fe58fdSRyusuke Konishi * entry in the inode block. This operation is excluded from the segment 98405fe58fdSRyusuke Konishi * construction. This function can be called both as a single operation 98505fe58fdSRyusuke Konishi * and as a part of indivisible file operations. 98605fe58fdSRyusuke Konishi */ 987aa385729SChristoph Hellwig void nilfs_dirty_inode(struct inode *inode, int flags) 98805fe58fdSRyusuke Konishi { 98905fe58fdSRyusuke Konishi struct nilfs_transaction_info ti; 9907d6cd92fSRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 99105fe58fdSRyusuke Konishi 99205fe58fdSRyusuke Konishi if (is_bad_inode(inode)) { 99305fe58fdSRyusuke Konishi nilfs_warning(inode->i_sb, __func__, 99405fe58fdSRyusuke Konishi "tried to mark bad_inode dirty. ignored.\n"); 99505fe58fdSRyusuke Konishi dump_stack(); 99605fe58fdSRyusuke Konishi return; 99705fe58fdSRyusuke Konishi } 9987d6cd92fSRyusuke Konishi if (mdi) { 9997d6cd92fSRyusuke Konishi nilfs_mdt_mark_dirty(inode); 10007d6cd92fSRyusuke Konishi return; 10017d6cd92fSRyusuke Konishi } 100205fe58fdSRyusuke Konishi nilfs_transaction_begin(inode->i_sb, &ti, 0); 1003b9f66140SAndreas Rohner __nilfs_mark_inode_dirty(inode, flags); 100447420c79SRyusuke Konishi nilfs_transaction_commit(inode->i_sb); /* never fails */ 100505fe58fdSRyusuke Konishi } 1006622daaffSRyusuke Konishi 1007622daaffSRyusuke Konishi int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1008622daaffSRyusuke Konishi __u64 start, __u64 len) 1009622daaffSRyusuke Konishi { 10100ef28f9aSRyusuke Konishi struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 1011622daaffSRyusuke Konishi __u64 logical = 0, phys = 0, size = 0; 1012622daaffSRyusuke Konishi __u32 flags = 0; 1013622daaffSRyusuke Konishi loff_t isize; 1014622daaffSRyusuke Konishi sector_t blkoff, end_blkoff; 1015622daaffSRyusuke Konishi sector_t delalloc_blkoff; 1016622daaffSRyusuke Konishi unsigned long delalloc_blklen; 1017622daaffSRyusuke Konishi unsigned int blkbits = inode->i_blkbits; 1018622daaffSRyusuke Konishi int ret, n; 1019622daaffSRyusuke Konishi 1020622daaffSRyusuke Konishi ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); 1021622daaffSRyusuke Konishi if (ret) 1022622daaffSRyusuke Konishi return ret; 1023622daaffSRyusuke Konishi 1024622daaffSRyusuke Konishi mutex_lock(&inode->i_mutex); 1025622daaffSRyusuke Konishi 1026622daaffSRyusuke Konishi isize = i_size_read(inode); 1027622daaffSRyusuke Konishi 1028622daaffSRyusuke Konishi blkoff = start >> blkbits; 1029622daaffSRyusuke Konishi end_blkoff = (start + len - 1) >> blkbits; 1030622daaffSRyusuke Konishi 1031622daaffSRyusuke Konishi delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff, 1032622daaffSRyusuke Konishi &delalloc_blkoff); 1033622daaffSRyusuke Konishi 1034622daaffSRyusuke Konishi do { 1035622daaffSRyusuke Konishi __u64 blkphy; 1036622daaffSRyusuke Konishi unsigned int maxblocks; 1037622daaffSRyusuke Konishi 1038622daaffSRyusuke Konishi if (delalloc_blklen && blkoff == delalloc_blkoff) { 1039622daaffSRyusuke Konishi if (size) { 1040622daaffSRyusuke Konishi /* End of the current extent */ 1041622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1042622daaffSRyusuke Konishi fieinfo, logical, phys, size, flags); 1043622daaffSRyusuke Konishi if (ret) 1044622daaffSRyusuke Konishi break; 1045622daaffSRyusuke Konishi } 1046622daaffSRyusuke Konishi if (blkoff > end_blkoff) 1047622daaffSRyusuke Konishi break; 1048622daaffSRyusuke Konishi 1049622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC; 1050622daaffSRyusuke Konishi logical = blkoff << blkbits; 1051622daaffSRyusuke Konishi phys = 0; 1052622daaffSRyusuke Konishi size = delalloc_blklen << blkbits; 1053622daaffSRyusuke Konishi 1054622daaffSRyusuke Konishi blkoff = delalloc_blkoff + delalloc_blklen; 1055622daaffSRyusuke Konishi delalloc_blklen = nilfs_find_uncommitted_extent( 1056622daaffSRyusuke Konishi inode, blkoff, &delalloc_blkoff); 1057622daaffSRyusuke Konishi continue; 1058622daaffSRyusuke Konishi } 1059622daaffSRyusuke Konishi 1060622daaffSRyusuke Konishi /* 1061622daaffSRyusuke Konishi * Limit the number of blocks that we look up so as 1062622daaffSRyusuke Konishi * not to get into the next delayed allocation extent. 1063622daaffSRyusuke Konishi */ 1064622daaffSRyusuke Konishi maxblocks = INT_MAX; 1065622daaffSRyusuke Konishi if (delalloc_blklen) 1066622daaffSRyusuke Konishi maxblocks = min_t(sector_t, delalloc_blkoff - blkoff, 1067622daaffSRyusuke Konishi maxblocks); 1068622daaffSRyusuke Konishi blkphy = 0; 1069622daaffSRyusuke Konishi 1070622daaffSRyusuke Konishi down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1071622daaffSRyusuke Konishi n = nilfs_bmap_lookup_contig( 1072622daaffSRyusuke Konishi NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks); 1073622daaffSRyusuke Konishi up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1074622daaffSRyusuke Konishi 1075622daaffSRyusuke Konishi if (n < 0) { 1076622daaffSRyusuke Konishi int past_eof; 1077622daaffSRyusuke Konishi 1078622daaffSRyusuke Konishi if (unlikely(n != -ENOENT)) 1079622daaffSRyusuke Konishi break; /* error */ 1080622daaffSRyusuke Konishi 1081622daaffSRyusuke Konishi /* HOLE */ 1082622daaffSRyusuke Konishi blkoff++; 1083622daaffSRyusuke Konishi past_eof = ((blkoff << blkbits) >= isize); 1084622daaffSRyusuke Konishi 1085622daaffSRyusuke Konishi if (size) { 1086622daaffSRyusuke Konishi /* End of the current extent */ 1087622daaffSRyusuke Konishi 1088622daaffSRyusuke Konishi if (past_eof) 1089622daaffSRyusuke Konishi flags |= FIEMAP_EXTENT_LAST; 1090622daaffSRyusuke Konishi 1091622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1092622daaffSRyusuke Konishi fieinfo, logical, phys, size, flags); 1093622daaffSRyusuke Konishi if (ret) 1094622daaffSRyusuke Konishi break; 1095622daaffSRyusuke Konishi size = 0; 1096622daaffSRyusuke Konishi } 1097622daaffSRyusuke Konishi if (blkoff > end_blkoff || past_eof) 1098622daaffSRyusuke Konishi break; 1099622daaffSRyusuke Konishi } else { 1100622daaffSRyusuke Konishi if (size) { 1101622daaffSRyusuke Konishi if (phys && blkphy << blkbits == phys + size) { 1102622daaffSRyusuke Konishi /* The current extent goes on */ 1103622daaffSRyusuke Konishi size += n << blkbits; 1104622daaffSRyusuke Konishi } else { 1105622daaffSRyusuke Konishi /* Terminate the current extent */ 1106622daaffSRyusuke Konishi ret = fiemap_fill_next_extent( 1107622daaffSRyusuke Konishi fieinfo, logical, phys, size, 1108622daaffSRyusuke Konishi flags); 1109622daaffSRyusuke Konishi if (ret || blkoff > end_blkoff) 1110622daaffSRyusuke Konishi break; 1111622daaffSRyusuke Konishi 1112622daaffSRyusuke Konishi /* Start another extent */ 1113622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED; 1114622daaffSRyusuke Konishi logical = blkoff << blkbits; 1115622daaffSRyusuke Konishi phys = blkphy << blkbits; 1116622daaffSRyusuke Konishi size = n << blkbits; 1117622daaffSRyusuke Konishi } 1118622daaffSRyusuke Konishi } else { 1119622daaffSRyusuke Konishi /* Start a new extent */ 1120622daaffSRyusuke Konishi flags = FIEMAP_EXTENT_MERGED; 1121622daaffSRyusuke Konishi logical = blkoff << blkbits; 1122622daaffSRyusuke Konishi phys = blkphy << blkbits; 1123622daaffSRyusuke Konishi size = n << blkbits; 1124622daaffSRyusuke Konishi } 1125622daaffSRyusuke Konishi blkoff += n; 1126622daaffSRyusuke Konishi } 1127622daaffSRyusuke Konishi cond_resched(); 1128622daaffSRyusuke Konishi } while (true); 1129622daaffSRyusuke Konishi 1130622daaffSRyusuke Konishi /* If ret is 1 then we just hit the end of the extent array */ 1131622daaffSRyusuke Konishi if (ret == 1) 1132622daaffSRyusuke Konishi ret = 0; 1133622daaffSRyusuke Konishi 1134622daaffSRyusuke Konishi mutex_unlock(&inode->i_mutex); 1135622daaffSRyusuke Konishi return ret; 1136622daaffSRyusuke Konishi } 1137