Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * inode.c - NILFS inode operations. |
| 3 | * |
| 4 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Written by Ryusuke Konishi <ryusuke@osrg.net> |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/buffer_head.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/gfp.h> |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 26 | #include <linux/mpage.h> |
| 27 | #include <linux/writeback.h> |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 28 | #include <linux/uio.h> |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 29 | #include "nilfs.h" |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 30 | #include "btnode.h" |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 31 | #include "segment.h" |
| 32 | #include "page.h" |
| 33 | #include "mdt.h" |
| 34 | #include "cpfile.h" |
| 35 | #include "ifile.h" |
| 36 | |
Vyacheslav Dubeyko | f5974c8 | 2012-07-30 14:42:10 -0700 | [diff] [blame] | 37 | /** |
| 38 | * struct nilfs_iget_args - arguments used during comparison between inodes |
| 39 | * @ino: inode number |
| 40 | * @cno: checkpoint number |
| 41 | * @root: pointer on NILFS root object (mounted checkpoint) |
| 42 | * @for_gc: inode for GC flag |
| 43 | */ |
Ryusuke Konishi | 0e14a35 | 2010-08-20 21:20:29 +0900 | [diff] [blame] | 44 | struct nilfs_iget_args { |
| 45 | u64 ino; |
| 46 | __u64 cno; |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 47 | struct nilfs_root *root; |
Ryusuke Konishi | 0e14a35 | 2010-08-20 21:20:29 +0900 | [diff] [blame] | 48 | int for_gc; |
| 49 | }; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 50 | |
Ryusuke Konishi | be66737 | 2011-03-05 00:19:32 +0900 | [diff] [blame] | 51 | void nilfs_inode_add_blocks(struct inode *inode, int n) |
| 52 | { |
| 53 | struct nilfs_root *root = NILFS_I(inode)->i_root; |
| 54 | |
| 55 | inode_add_bytes(inode, (1 << inode->i_blkbits) * n); |
| 56 | if (root) |
| 57 | atomic_add(n, &root->blocks_count); |
| 58 | } |
| 59 | |
| 60 | void nilfs_inode_sub_blocks(struct inode *inode, int n) |
| 61 | { |
| 62 | struct nilfs_root *root = NILFS_I(inode)->i_root; |
| 63 | |
| 64 | inode_sub_bytes(inode, (1 << inode->i_blkbits) * n); |
| 65 | if (root) |
| 66 | atomic_sub(n, &root->blocks_count); |
| 67 | } |
| 68 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 69 | /** |
| 70 | * nilfs_get_block() - get a file block on the filesystem (callback function) |
| 71 | * @inode - inode struct of the target file |
| 72 | * @blkoff - file block number |
| 73 | * @bh_result - buffer head to be mapped on |
| 74 | * @create - indicate whether allocating the block or not when it has not |
| 75 | * been allocated yet. |
| 76 | * |
| 77 | * This function does not issue actual read request of the specified data |
| 78 | * block. It is done by VFS. |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 79 | */ |
| 80 | int nilfs_get_block(struct inode *inode, sector_t blkoff, |
| 81 | struct buffer_head *bh_result, int create) |
| 82 | { |
| 83 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 84 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 85 | __u64 blknum = 0; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 86 | int err = 0, ret; |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 87 | unsigned maxblocks = bh_result->b_size >> inode->i_blkbits; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 88 | |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 89 | down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 90 | ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 91 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 92 | if (ret >= 0) { /* found */ |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 93 | map_bh(bh_result, inode->i_sb, blknum); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 94 | if (ret > 0) |
| 95 | bh_result->b_size = (ret << inode->i_blkbits); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 96 | goto out; |
| 97 | } |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 98 | /* data block was not found */ |
| 99 | if (ret == -ENOENT && create) { |
| 100 | struct nilfs_transaction_info ti; |
| 101 | |
| 102 | bh_result->b_blocknr = 0; |
| 103 | err = nilfs_transaction_begin(inode->i_sb, &ti, 1); |
| 104 | if (unlikely(err)) |
| 105 | goto out; |
| 106 | err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, |
| 107 | (unsigned long)bh_result); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 108 | if (unlikely(err != 0)) { |
| 109 | if (err == -EEXIST) { |
| 110 | /* |
| 111 | * The get_block() function could be called |
| 112 | * from multiple callers for an inode. |
| 113 | * However, the page having this block must |
| 114 | * be locked in this case. |
| 115 | */ |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 116 | printk(KERN_WARNING |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 117 | "nilfs_get_block: a race condition " |
| 118 | "while inserting a data block. " |
| 119 | "(inode number=%lu, file block " |
| 120 | "offset=%llu)\n", |
| 121 | inode->i_ino, |
| 122 | (unsigned long long)blkoff); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 123 | err = 0; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 124 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 125 | nilfs_transaction_abort(inode->i_sb); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 126 | goto out; |
| 127 | } |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 128 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 129 | nilfs_transaction_commit(inode->i_sb); /* never fails */ |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 130 | /* Error handling should be detailed */ |
| 131 | set_buffer_new(bh_result); |
Ryusuke Konishi | 27e6c7a | 2010-12-26 16:28:28 +0900 | [diff] [blame] | 132 | set_buffer_delay(bh_result); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 133 | map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed |
| 134 | to proper value */ |
| 135 | } else if (ret == -ENOENT) { |
| 136 | /* not found is not error (e.g. hole); must return without |
| 137 | the mapped state flag. */ |
| 138 | ; |
| 139 | } else { |
| 140 | err = ret; |
| 141 | } |
| 142 | |
| 143 | out: |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * nilfs_readpage() - implement readpage() method of nilfs_aops {} |
| 149 | * address_space_operations. |
| 150 | * @file - file struct of the file to be read |
| 151 | * @page - the page to be read |
| 152 | */ |
| 153 | static int nilfs_readpage(struct file *file, struct page *page) |
| 154 | { |
| 155 | return mpage_readpage(page, nilfs_get_block); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * nilfs_readpages() - implement readpages() method of nilfs_aops {} |
| 160 | * address_space_operations. |
| 161 | * @file - file struct of the file to be read |
| 162 | * @mapping - address_space struct used for reading multiple pages |
| 163 | * @pages - the pages to be read |
| 164 | * @nr_pages - number of pages to be read |
| 165 | */ |
| 166 | static int nilfs_readpages(struct file *file, struct address_space *mapping, |
| 167 | struct list_head *pages, unsigned nr_pages) |
| 168 | { |
| 169 | return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); |
| 170 | } |
| 171 | |
| 172 | static int nilfs_writepages(struct address_space *mapping, |
| 173 | struct writeback_control *wbc) |
| 174 | { |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 175 | struct inode *inode = mapping->host; |
| 176 | int err = 0; |
| 177 | |
Vyacheslav Dubeyko | 8c26c4e | 2013-04-30 15:27:48 -0700 | [diff] [blame^] | 178 | if (inode->i_sb->s_flags & MS_RDONLY) { |
| 179 | nilfs_clear_dirty_pages(mapping, false); |
| 180 | return -EROFS; |
| 181 | } |
| 182 | |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 183 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 184 | err = nilfs_construct_dsync_segment(inode->i_sb, inode, |
| 185 | wbc->range_start, |
| 186 | wbc->range_end); |
| 187 | return err; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static int nilfs_writepage(struct page *page, struct writeback_control *wbc) |
| 191 | { |
| 192 | struct inode *inode = page->mapping->host; |
| 193 | int err; |
| 194 | |
Vyacheslav Dubeyko | 8c26c4e | 2013-04-30 15:27:48 -0700 | [diff] [blame^] | 195 | if (inode && (inode->i_sb->s_flags & MS_RDONLY)) { |
| 196 | /* |
| 197 | * It means that filesystem was remounted in read-only |
| 198 | * mode because of error or metadata corruption. But we |
| 199 | * have dirty pages that try to be flushed in background. |
| 200 | * So, here we simply discard this dirty page. |
| 201 | */ |
| 202 | nilfs_clear_dirty_page(page, false); |
| 203 | unlock_page(page); |
| 204 | return -EROFS; |
| 205 | } |
| 206 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 207 | redirty_page_for_writepage(wbc, page); |
| 208 | unlock_page(page); |
| 209 | |
| 210 | if (wbc->sync_mode == WB_SYNC_ALL) { |
| 211 | err = nilfs_construct_segment(inode->i_sb); |
| 212 | if (unlikely(err)) |
| 213 | return err; |
| 214 | } else if (wbc->for_reclaim) |
| 215 | nilfs_flush_segment(inode->i_sb, inode->i_ino); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int nilfs_set_page_dirty(struct page *page) |
| 221 | { |
| 222 | int ret = __set_page_dirty_buffers(page); |
| 223 | |
| 224 | if (ret) { |
| 225 | struct inode *inode = page->mapping->host; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 226 | unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); |
| 227 | |
Ryusuke Konishi | bcbc8c6 | 2010-12-27 00:05:49 +0900 | [diff] [blame] | 228 | nilfs_set_file_dirty(inode, nr_dirty); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 229 | } |
| 230 | return ret; |
| 231 | } |
| 232 | |
Marco Stornelli | 2d1b399 | 2012-12-15 11:57:37 +0100 | [diff] [blame] | 233 | void nilfs_write_failed(struct address_space *mapping, loff_t to) |
| 234 | { |
| 235 | struct inode *inode = mapping->host; |
| 236 | |
| 237 | if (to > inode->i_size) { |
| 238 | truncate_pagecache(inode, to, inode->i_size); |
| 239 | nilfs_truncate(inode); |
| 240 | } |
| 241 | } |
| 242 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 243 | static int nilfs_write_begin(struct file *file, struct address_space *mapping, |
| 244 | loff_t pos, unsigned len, unsigned flags, |
| 245 | struct page **pagep, void **fsdata) |
| 246 | |
| 247 | { |
| 248 | struct inode *inode = mapping->host; |
| 249 | int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); |
| 250 | |
| 251 | if (unlikely(err)) |
| 252 | return err; |
| 253 | |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 254 | err = block_write_begin(mapping, pos, len, flags, pagep, |
| 255 | nilfs_get_block); |
| 256 | if (unlikely(err)) { |
Marco Stornelli | 2d1b399 | 2012-12-15 11:57:37 +0100 | [diff] [blame] | 257 | nilfs_write_failed(mapping, pos + len); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 258 | nilfs_transaction_abort(inode->i_sb); |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 259 | } |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 260 | return err; |
| 261 | } |
| 262 | |
| 263 | static int nilfs_write_end(struct file *file, struct address_space *mapping, |
| 264 | loff_t pos, unsigned len, unsigned copied, |
| 265 | struct page *page, void *fsdata) |
| 266 | { |
| 267 | struct inode *inode = mapping->host; |
| 268 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 269 | unsigned nr_dirty; |
| 270 | int err; |
| 271 | |
| 272 | nr_dirty = nilfs_page_count_clean_buffers(page, start, |
| 273 | start + copied); |
| 274 | copied = generic_write_end(file, mapping, pos, len, copied, page, |
| 275 | fsdata); |
Ryusuke Konishi | bcbc8c6 | 2010-12-27 00:05:49 +0900 | [diff] [blame] | 276 | nilfs_set_file_dirty(inode, nr_dirty); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 277 | err = nilfs_transaction_commit(inode->i_sb); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 278 | return err ? : copied; |
| 279 | } |
| 280 | |
| 281 | static ssize_t |
| 282 | nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 283 | loff_t offset, unsigned long nr_segs) |
| 284 | { |
| 285 | struct file *file = iocb->ki_filp; |
Marco Stornelli | 2d1b399 | 2012-12-15 11:57:37 +0100 | [diff] [blame] | 286 | struct address_space *mapping = file->f_mapping; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 287 | struct inode *inode = file->f_mapping->host; |
| 288 | ssize_t size; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 289 | |
| 290 | if (rw == WRITE) |
| 291 | return 0; |
| 292 | |
| 293 | /* Needs synchronization with the cleaner */ |
Christoph Hellwig | aacfc19c | 2011-06-24 14:29:47 -0400 | [diff] [blame] | 294 | size = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs, |
| 295 | nilfs_get_block); |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * In case of error extending write may have instantiated a few |
| 299 | * blocks outside i_size. Trim these off again. |
| 300 | */ |
| 301 | if (unlikely((rw & WRITE) && size < 0)) { |
| 302 | loff_t isize = i_size_read(inode); |
| 303 | loff_t end = offset + iov_length(iov, nr_segs); |
| 304 | |
| 305 | if (end > isize) |
Marco Stornelli | 2d1b399 | 2012-12-15 11:57:37 +0100 | [diff] [blame] | 306 | nilfs_write_failed(mapping, end); |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 309 | return size; |
| 310 | } |
| 311 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 312 | const struct address_space_operations nilfs_aops = { |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 313 | .writepage = nilfs_writepage, |
| 314 | .readpage = nilfs_readpage, |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 315 | .writepages = nilfs_writepages, |
| 316 | .set_page_dirty = nilfs_set_page_dirty, |
| 317 | .readpages = nilfs_readpages, |
| 318 | .write_begin = nilfs_write_begin, |
| 319 | .write_end = nilfs_write_end, |
| 320 | /* .releasepage = nilfs_releasepage, */ |
| 321 | .invalidatepage = block_invalidatepage, |
| 322 | .direct_IO = nilfs_direct_IO, |
Hisashi Hifumi | 258ef67 | 2009-05-13 11:19:40 +0900 | [diff] [blame] | 323 | .is_partially_uptodate = block_is_partially_uptodate, |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
Al Viro | c6e49e3 | 2011-07-26 03:07:14 -0400 | [diff] [blame] | 326 | struct inode *nilfs_new_inode(struct inode *dir, umode_t mode) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 327 | { |
| 328 | struct super_block *sb = dir->i_sb; |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 329 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 330 | struct inode *inode; |
| 331 | struct nilfs_inode_info *ii; |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 332 | struct nilfs_root *root; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 333 | int err = -ENOMEM; |
| 334 | ino_t ino; |
| 335 | |
| 336 | inode = new_inode(sb); |
| 337 | if (unlikely(!inode)) |
| 338 | goto failed; |
| 339 | |
| 340 | mapping_set_gfp_mask(inode->i_mapping, |
| 341 | mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); |
| 342 | |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 343 | root = NILFS_I(dir)->i_root; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 344 | ii = NILFS_I(inode); |
| 345 | ii->i_state = 1 << NILFS_I_NEW; |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 346 | ii->i_root = root; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 347 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 348 | err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 349 | if (unlikely(err)) |
| 350 | goto failed_ifile_create_inode; |
| 351 | /* reference count of i_bh inherits from nilfs_mdt_read_block() */ |
| 352 | |
Ryusuke Konishi | b7c0634 | 2010-08-14 14:48:32 +0900 | [diff] [blame] | 353 | atomic_inc(&root->inodes_count); |
Dmitry Monakhov | 73459dc | 2010-03-04 17:32:15 +0300 | [diff] [blame] | 354 | inode_init_owner(inode, dir, mode); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 355 | inode->i_ino = ino; |
| 356 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 357 | |
| 358 | if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { |
| 359 | err = nilfs_bmap_read(ii->i_bmap, NULL); |
| 360 | if (err < 0) |
| 361 | goto failed_bmap; |
| 362 | |
| 363 | set_bit(NILFS_I_BMAP, &ii->i_state); |
| 364 | /* No lock is needed; iget() ensures it. */ |
| 365 | } |
| 366 | |
Ryusuke Konishi | b253a3e | 2011-01-20 02:09:53 +0900 | [diff] [blame] | 367 | ii->i_flags = nilfs_mask_flags( |
| 368 | mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 369 | |
| 370 | /* ii->i_file_acl = 0; */ |
| 371 | /* ii->i_dir_acl = 0; */ |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 372 | ii->i_dir_start_lookup = 0; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 373 | nilfs_set_inode_flags(inode); |
Ryusuke Konishi | 9b1fc4e4 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 374 | spin_lock(&nilfs->ns_next_gen_lock); |
| 375 | inode->i_generation = nilfs->ns_next_generation++; |
| 376 | spin_unlock(&nilfs->ns_next_gen_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 377 | insert_inode_hash(inode); |
| 378 | |
| 379 | err = nilfs_init_acl(inode, dir); |
| 380 | if (unlikely(err)) |
| 381 | goto failed_acl; /* never occur. When supporting |
| 382 | nilfs_init_acl(), proper cancellation of |
| 383 | above jobs should be considered */ |
| 384 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 385 | return inode; |
| 386 | |
| 387 | failed_acl: |
| 388 | failed_bmap: |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 389 | clear_nlink(inode); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 390 | iput(inode); /* raw_inode will be deleted through |
| 391 | generic_delete_inode() */ |
| 392 | goto failed; |
| 393 | |
| 394 | failed_ifile_create_inode: |
| 395 | make_bad_inode(inode); |
| 396 | iput(inode); /* if i_nlink == 1, generic_forget_inode() will be |
| 397 | called */ |
| 398 | failed: |
| 399 | return ERR_PTR(err); |
| 400 | } |
| 401 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 402 | void nilfs_set_inode_flags(struct inode *inode) |
| 403 | { |
| 404 | unsigned int flags = NILFS_I(inode)->i_flags; |
| 405 | |
| 406 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | |
| 407 | S_DIRSYNC); |
Ryusuke Konishi | f0c9f24 | 2011-01-20 02:09:52 +0900 | [diff] [blame] | 408 | if (flags & FS_SYNC_FL) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 409 | inode->i_flags |= S_SYNC; |
Ryusuke Konishi | f0c9f24 | 2011-01-20 02:09:52 +0900 | [diff] [blame] | 410 | if (flags & FS_APPEND_FL) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 411 | inode->i_flags |= S_APPEND; |
Ryusuke Konishi | f0c9f24 | 2011-01-20 02:09:52 +0900 | [diff] [blame] | 412 | if (flags & FS_IMMUTABLE_FL) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 413 | inode->i_flags |= S_IMMUTABLE; |
Ryusuke Konishi | f0c9f24 | 2011-01-20 02:09:52 +0900 | [diff] [blame] | 414 | if (flags & FS_NOATIME_FL) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 415 | inode->i_flags |= S_NOATIME; |
Ryusuke Konishi | f0c9f24 | 2011-01-20 02:09:52 +0900 | [diff] [blame] | 416 | if (flags & FS_DIRSYNC_FL) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 417 | inode->i_flags |= S_DIRSYNC; |
| 418 | mapping_set_gfp_mask(inode->i_mapping, |
| 419 | mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); |
| 420 | } |
| 421 | |
| 422 | int nilfs_read_inode_common(struct inode *inode, |
| 423 | struct nilfs_inode *raw_inode) |
| 424 | { |
| 425 | struct nilfs_inode_info *ii = NILFS_I(inode); |
| 426 | int err; |
| 427 | |
| 428 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
Eric W. Biederman | 305d3d0 | 2012-02-10 12:31:23 -0800 | [diff] [blame] | 429 | i_uid_write(inode, le32_to_cpu(raw_inode->i_uid)); |
| 430 | i_gid_write(inode, le32_to_cpu(raw_inode->i_gid)); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 431 | set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 432 | inode->i_size = le64_to_cpu(raw_inode->i_size); |
| 433 | inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); |
| 434 | inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); |
| 435 | inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); |
Ryusuke Konishi | 6123923 | 2009-04-06 19:02:00 -0700 | [diff] [blame] | 436 | inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); |
| 437 | inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); |
| 438 | inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); |
| 439 | if (inode->i_nlink == 0 && inode->i_mode == 0) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 440 | return -EINVAL; /* this inode is deleted */ |
| 441 | |
| 442 | inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); |
| 443 | ii->i_flags = le32_to_cpu(raw_inode->i_flags); |
| 444 | #if 0 |
| 445 | ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); |
| 446 | ii->i_dir_acl = S_ISREG(inode->i_mode) ? |
| 447 | 0 : le32_to_cpu(raw_inode->i_dir_acl); |
| 448 | #endif |
Ryusuke Konishi | 3cc811b | 2009-09-28 13:02:46 +0900 | [diff] [blame] | 449 | ii->i_dir_start_lookup = 0; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 450 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); |
| 451 | |
| 452 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 453 | S_ISLNK(inode->i_mode)) { |
| 454 | err = nilfs_bmap_read(ii->i_bmap, raw_inode); |
| 455 | if (err < 0) |
| 456 | return err; |
| 457 | set_bit(NILFS_I_BMAP, &ii->i_state); |
| 458 | /* No lock is needed; iget() ensures it. */ |
| 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 463 | static int __nilfs_read_inode(struct super_block *sb, |
| 464 | struct nilfs_root *root, unsigned long ino, |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 465 | struct inode *inode) |
| 466 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 467 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 468 | struct buffer_head *bh; |
| 469 | struct nilfs_inode *raw_inode; |
| 470 | int err; |
| 471 | |
Ryusuke Konishi | 365e215 | 2010-12-27 00:07:30 +0900 | [diff] [blame] | 472 | down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 473 | err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 474 | if (unlikely(err)) |
| 475 | goto bad_inode; |
| 476 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 477 | raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 478 | |
Ryusuke Konishi | 1b2f5a64 | 2009-08-22 19:10:07 +0900 | [diff] [blame] | 479 | err = nilfs_read_inode_common(inode, raw_inode); |
| 480 | if (err) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 481 | goto failed_unmap; |
| 482 | |
| 483 | if (S_ISREG(inode->i_mode)) { |
| 484 | inode->i_op = &nilfs_file_inode_operations; |
| 485 | inode->i_fop = &nilfs_file_operations; |
| 486 | inode->i_mapping->a_ops = &nilfs_aops; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 487 | } else if (S_ISDIR(inode->i_mode)) { |
| 488 | inode->i_op = &nilfs_dir_inode_operations; |
| 489 | inode->i_fop = &nilfs_dir_operations; |
| 490 | inode->i_mapping->a_ops = &nilfs_aops; |
| 491 | } else if (S_ISLNK(inode->i_mode)) { |
| 492 | inode->i_op = &nilfs_symlink_inode_operations; |
| 493 | inode->i_mapping->a_ops = &nilfs_aops; |
| 494 | } else { |
| 495 | inode->i_op = &nilfs_special_inode_operations; |
| 496 | init_special_inode( |
| 497 | inode, inode->i_mode, |
Ryusuke Konishi | cdce214 | 2010-05-09 15:31:22 +0900 | [diff] [blame] | 498 | huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 499 | } |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 500 | nilfs_ifile_unmap_inode(root->ifile, ino, bh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 501 | brelse(bh); |
Ryusuke Konishi | 365e215 | 2010-12-27 00:07:30 +0900 | [diff] [blame] | 502 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 503 | nilfs_set_inode_flags(inode); |
| 504 | return 0; |
| 505 | |
| 506 | failed_unmap: |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 507 | nilfs_ifile_unmap_inode(root->ifile, ino, bh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 508 | brelse(bh); |
| 509 | |
| 510 | bad_inode: |
Ryusuke Konishi | 365e215 | 2010-12-27 00:07:30 +0900 | [diff] [blame] | 511 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 512 | return err; |
| 513 | } |
| 514 | |
Ryusuke Konishi | 0e14a35 | 2010-08-20 21:20:29 +0900 | [diff] [blame] | 515 | static int nilfs_iget_test(struct inode *inode, void *opaque) |
| 516 | { |
| 517 | struct nilfs_iget_args *args = opaque; |
| 518 | struct nilfs_inode_info *ii; |
| 519 | |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 520 | if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) |
Ryusuke Konishi | 0e14a35 | 2010-08-20 21:20:29 +0900 | [diff] [blame] | 521 | return 0; |
| 522 | |
| 523 | ii = NILFS_I(inode); |
| 524 | if (!test_bit(NILFS_I_GCINODE, &ii->i_state)) |
| 525 | return !args->for_gc; |
| 526 | |
| 527 | return args->for_gc && args->cno == ii->i_cno; |
| 528 | } |
| 529 | |
| 530 | static int nilfs_iget_set(struct inode *inode, void *opaque) |
| 531 | { |
| 532 | struct nilfs_iget_args *args = opaque; |
| 533 | |
| 534 | inode->i_ino = args->ino; |
| 535 | if (args->for_gc) { |
| 536 | NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE; |
| 537 | NILFS_I(inode)->i_cno = args->cno; |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 538 | NILFS_I(inode)->i_root = NULL; |
| 539 | } else { |
| 540 | if (args->root && args->ino == NILFS_ROOT_INO) |
| 541 | nilfs_get_root(args->root); |
| 542 | NILFS_I(inode)->i_root = args->root; |
Ryusuke Konishi | 0e14a35 | 2010-08-20 21:20:29 +0900 | [diff] [blame] | 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 547 | struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, |
| 548 | unsigned long ino) |
| 549 | { |
| 550 | struct nilfs_iget_args args = { |
| 551 | .ino = ino, .root = root, .cno = 0, .for_gc = 0 |
| 552 | }; |
| 553 | |
| 554 | return ilookup5(sb, ino, nilfs_iget_test, &args); |
| 555 | } |
| 556 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 557 | struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, |
| 558 | unsigned long ino) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 559 | { |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 560 | struct nilfs_iget_args args = { |
| 561 | .ino = ino, .root = root, .cno = 0, .for_gc = 0 |
| 562 | }; |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 563 | |
| 564 | return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); |
| 565 | } |
| 566 | |
| 567 | struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, |
| 568 | unsigned long ino) |
| 569 | { |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 570 | struct inode *inode; |
| 571 | int err; |
| 572 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 573 | inode = nilfs_iget_locked(sb, root, ino); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 574 | if (unlikely(!inode)) |
| 575 | return ERR_PTR(-ENOMEM); |
| 576 | if (!(inode->i_state & I_NEW)) |
| 577 | return inode; |
| 578 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 579 | err = __nilfs_read_inode(sb, root, ino, inode); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 580 | if (unlikely(err)) { |
| 581 | iget_failed(inode); |
| 582 | return ERR_PTR(err); |
| 583 | } |
| 584 | unlock_new_inode(inode); |
| 585 | return inode; |
| 586 | } |
| 587 | |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 588 | struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino, |
| 589 | __u64 cno) |
| 590 | { |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 591 | struct nilfs_iget_args args = { |
| 592 | .ino = ino, .root = NULL, .cno = cno, .for_gc = 1 |
| 593 | }; |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 594 | struct inode *inode; |
| 595 | int err; |
| 596 | |
| 597 | inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); |
| 598 | if (unlikely(!inode)) |
| 599 | return ERR_PTR(-ENOMEM); |
| 600 | if (!(inode->i_state & I_NEW)) |
| 601 | return inode; |
| 602 | |
| 603 | err = nilfs_init_gcinode(inode); |
| 604 | if (unlikely(err)) { |
| 605 | iget_failed(inode); |
| 606 | return ERR_PTR(err); |
| 607 | } |
| 608 | unlock_new_inode(inode); |
| 609 | return inode; |
| 610 | } |
| 611 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 612 | void nilfs_write_inode_common(struct inode *inode, |
| 613 | struct nilfs_inode *raw_inode, int has_bmap) |
| 614 | { |
| 615 | struct nilfs_inode_info *ii = NILFS_I(inode); |
| 616 | |
| 617 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
Eric W. Biederman | 305d3d0 | 2012-02-10 12:31:23 -0800 | [diff] [blame] | 618 | raw_inode->i_uid = cpu_to_le32(i_uid_read(inode)); |
| 619 | raw_inode->i_gid = cpu_to_le32(i_gid_read(inode)); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 620 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); |
| 621 | raw_inode->i_size = cpu_to_le64(inode->i_size); |
| 622 | raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 623 | raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); |
Ryusuke Konishi | 6123923 | 2009-04-06 19:02:00 -0700 | [diff] [blame] | 624 | raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 625 | raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 626 | raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); |
| 627 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 628 | raw_inode->i_flags = cpu_to_le32(ii->i_flags); |
| 629 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); |
| 630 | |
Ryusuke Konishi | 56eb553 | 2011-04-30 18:56:12 +0900 | [diff] [blame] | 631 | if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) { |
| 632 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
| 633 | |
| 634 | /* zero-fill unused portion in the case of super root block */ |
| 635 | raw_inode->i_xattr = 0; |
| 636 | raw_inode->i_pad = 0; |
| 637 | memset((void *)raw_inode + sizeof(*raw_inode), 0, |
| 638 | nilfs->ns_inode_size - sizeof(*raw_inode)); |
| 639 | } |
| 640 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 641 | if (has_bmap) |
| 642 | nilfs_bmap_write(ii->i_bmap, raw_inode); |
| 643 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) |
| 644 | raw_inode->i_device_code = |
Ryusuke Konishi | cdce214 | 2010-05-09 15:31:22 +0900 | [diff] [blame] | 645 | cpu_to_le64(huge_encode_dev(inode->i_rdev)); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 646 | /* When extending inode, nilfs->ns_inode_size should be checked |
| 647 | for substitutions of appended fields */ |
| 648 | } |
| 649 | |
| 650 | void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh) |
| 651 | { |
| 652 | ino_t ino = inode->i_ino; |
| 653 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 654 | struct inode *ifile = ii->i_root->ifile; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 655 | struct nilfs_inode *raw_inode; |
| 656 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 657 | raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 658 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 659 | if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 660 | memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 661 | set_bit(NILFS_I_INODE_DIRTY, &ii->i_state); |
| 662 | |
| 663 | nilfs_write_inode_common(inode, raw_inode, 0); |
| 664 | /* XXX: call with has_bmap = 0 is a workaround to avoid |
| 665 | deadlock of bmap. This delays update of i_bmap to just |
| 666 | before writing */ |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 667 | nilfs_ifile_unmap_inode(ifile, ino, ibh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ |
| 671 | |
| 672 | static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, |
| 673 | unsigned long from) |
| 674 | { |
| 675 | unsigned long b; |
| 676 | int ret; |
| 677 | |
| 678 | if (!test_bit(NILFS_I_BMAP, &ii->i_state)) |
| 679 | return; |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 680 | repeat: |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 681 | ret = nilfs_bmap_last_key(ii->i_bmap, &b); |
| 682 | if (ret == -ENOENT) |
| 683 | return; |
| 684 | else if (ret < 0) |
| 685 | goto failed; |
| 686 | |
| 687 | if (b < from) |
| 688 | return; |
| 689 | |
| 690 | b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); |
| 691 | ret = nilfs_bmap_truncate(ii->i_bmap, b); |
| 692 | nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); |
| 693 | if (!ret || (ret == -ENOMEM && |
| 694 | nilfs_bmap_truncate(ii->i_bmap, b) == 0)) |
| 695 | goto repeat; |
| 696 | |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 697 | failed: |
| 698 | nilfs_warning(ii->vfs_inode.i_sb, __func__, |
| 699 | "failed to truncate bmap (ino=%lu, err=%d)", |
| 700 | ii->vfs_inode.i_ino, ret); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void nilfs_truncate(struct inode *inode) |
| 704 | { |
| 705 | unsigned long blkoff; |
| 706 | unsigned int blocksize; |
| 707 | struct nilfs_transaction_info ti; |
| 708 | struct super_block *sb = inode->i_sb; |
| 709 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 710 | |
| 711 | if (!test_bit(NILFS_I_BMAP, &ii->i_state)) |
| 712 | return; |
| 713 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 714 | return; |
| 715 | |
| 716 | blocksize = sb->s_blocksize; |
| 717 | blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 718 | nilfs_transaction_begin(sb, &ti, 0); /* never fails */ |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 719 | |
| 720 | block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); |
| 721 | |
| 722 | nilfs_truncate_bmap(ii, blkoff); |
| 723 | |
| 724 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 725 | if (IS_SYNC(inode)) |
| 726 | nilfs_set_transaction_flag(NILFS_TI_SYNC); |
| 727 | |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 728 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | bcbc8c6 | 2010-12-27 00:05:49 +0900 | [diff] [blame] | 729 | nilfs_set_file_dirty(inode, 0); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 730 | nilfs_transaction_commit(sb); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 731 | /* May construct a logical segment and may fail in sync mode. |
| 732 | But truncate has no return value. */ |
| 733 | } |
| 734 | |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 735 | static void nilfs_clear_inode(struct inode *inode) |
| 736 | { |
| 737 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | 518d1a6 | 2010-08-20 23:40:54 +0900 | [diff] [blame] | 738 | struct nilfs_mdt_info *mdi = NILFS_MDT(inode); |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 739 | |
| 740 | /* |
| 741 | * Free resources allocated in nilfs_read_inode(), here. |
| 742 | */ |
| 743 | BUG_ON(!list_empty(&ii->i_dirty)); |
| 744 | brelse(ii->i_bh); |
| 745 | ii->i_bh = NULL; |
| 746 | |
Ryusuke Konishi | 518d1a6 | 2010-08-20 23:40:54 +0900 | [diff] [blame] | 747 | if (mdi && mdi->mi_palloc_cache) |
| 748 | nilfs_palloc_destroy_cache(inode); |
| 749 | |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 750 | if (test_bit(NILFS_I_BMAP, &ii->i_state)) |
| 751 | nilfs_bmap_clear(ii->i_bmap); |
| 752 | |
| 753 | nilfs_btnode_cache_clear(&ii->i_btnode_cache); |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 754 | |
| 755 | if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) |
| 756 | nilfs_put_root(ii->i_root); |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void nilfs_evict_inode(struct inode *inode) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 760 | { |
| 761 | struct nilfs_transaction_info ti; |
| 762 | struct super_block *sb = inode->i_sb; |
| 763 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | 25b18d3 | 2011-02-11 15:23:27 +0900 | [diff] [blame] | 764 | int ret; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 765 | |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 766 | if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 767 | if (inode->i_data.nrpages) |
| 768 | truncate_inode_pages(&inode->i_data, 0); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 769 | clear_inode(inode); |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 770 | nilfs_clear_inode(inode); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 771 | return; |
| 772 | } |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 773 | nilfs_transaction_begin(sb, &ti, 0); /* never fails */ |
| 774 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 775 | if (inode->i_data.nrpages) |
| 776 | truncate_inode_pages(&inode->i_data, 0); |
| 777 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 778 | /* TODO: some of the following operations may fail. */ |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 779 | nilfs_truncate_bmap(ii, 0); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 780 | nilfs_mark_inode_dirty(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 781 | clear_inode(inode); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 782 | |
Ryusuke Konishi | 25b18d3 | 2011-02-11 15:23:27 +0900 | [diff] [blame] | 783 | ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); |
| 784 | if (!ret) |
| 785 | atomic_dec(&ii->i_root->inodes_count); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 786 | |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 787 | nilfs_clear_inode(inode); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 788 | |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 789 | if (IS_SYNC(inode)) |
| 790 | nilfs_set_transaction_flag(NILFS_TI_SYNC); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 791 | nilfs_transaction_commit(sb); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 792 | /* May construct a logical segment and may fail in sync mode. |
| 793 | But delete_inode has no return value. */ |
| 794 | } |
| 795 | |
| 796 | int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) |
| 797 | { |
| 798 | struct nilfs_transaction_info ti; |
| 799 | struct inode *inode = dentry->d_inode; |
| 800 | struct super_block *sb = inode->i_sb; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 801 | int err; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 802 | |
| 803 | err = inode_change_ok(inode, iattr); |
| 804 | if (err) |
| 805 | return err; |
| 806 | |
| 807 | err = nilfs_transaction_begin(sb, &ti, 0); |
| 808 | if (unlikely(err)) |
| 809 | return err; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 810 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 811 | if ((iattr->ia_valid & ATTR_SIZE) && |
| 812 | iattr->ia_size != i_size_read(inode)) { |
Christoph Hellwig | 562c72aa5 | 2011-06-24 14:29:45 -0400 | [diff] [blame] | 813 | inode_dio_wait(inode); |
Marco Stornelli | 2d1b399 | 2012-12-15 11:57:37 +0100 | [diff] [blame] | 814 | truncate_setsize(inode, iattr->ia_size); |
| 815 | nilfs_truncate(inode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | setattr_copy(inode, iattr); |
| 819 | mark_inode_dirty(inode); |
| 820 | |
| 821 | if (iattr->ia_valid & ATTR_MODE) { |
| 822 | err = nilfs_acl_chmod(inode); |
| 823 | if (unlikely(err)) |
| 824 | goto out_err; |
| 825 | } |
| 826 | |
| 827 | return nilfs_transaction_commit(sb); |
| 828 | |
| 829 | out_err: |
| 830 | nilfs_transaction_abort(sb); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 831 | return err; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 834 | int nilfs_permission(struct inode *inode, int mask) |
Ryusuke Konishi | dc3d3b8 | 2010-08-15 23:33:57 +0900 | [diff] [blame] | 835 | { |
Al Viro | 730e908 | 2011-06-18 20:21:44 -0400 | [diff] [blame] | 836 | struct nilfs_root *root = NILFS_I(inode)->i_root; |
Ryusuke Konishi | dc3d3b8 | 2010-08-15 23:33:57 +0900 | [diff] [blame] | 837 | if ((mask & MAY_WRITE) && root && |
| 838 | root->cno != NILFS_CPTREE_CURRENT_CNO) |
| 839 | return -EROFS; /* snapshot is not writable */ |
| 840 | |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 841 | return generic_permission(inode, mask); |
Ryusuke Konishi | dc3d3b8 | 2010-08-15 23:33:57 +0900 | [diff] [blame] | 842 | } |
| 843 | |
Ryusuke Konishi | bcbc8c6 | 2010-12-27 00:05:49 +0900 | [diff] [blame] | 844 | int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 845 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 846 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 847 | struct nilfs_inode_info *ii = NILFS_I(inode); |
| 848 | int err; |
| 849 | |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 850 | spin_lock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 851 | if (ii->i_bh == NULL) { |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 852 | spin_unlock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 853 | err = nilfs_ifile_get_inode_block(ii->i_root->ifile, |
| 854 | inode->i_ino, pbh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 855 | if (unlikely(err)) |
| 856 | return err; |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 857 | spin_lock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 858 | if (ii->i_bh == NULL) |
| 859 | ii->i_bh = *pbh; |
| 860 | else { |
| 861 | brelse(*pbh); |
| 862 | *pbh = ii->i_bh; |
| 863 | } |
| 864 | } else |
| 865 | *pbh = ii->i_bh; |
| 866 | |
| 867 | get_bh(*pbh); |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 868 | spin_unlock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | int nilfs_inode_dirty(struct inode *inode) |
| 873 | { |
| 874 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 875 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 876 | int ret = 0; |
| 877 | |
| 878 | if (!list_empty(&ii->i_dirty)) { |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 879 | spin_lock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 880 | ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || |
| 881 | test_bit(NILFS_I_BUSY, &ii->i_state); |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 882 | spin_unlock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 883 | } |
| 884 | return ret; |
| 885 | } |
| 886 | |
Ryusuke Konishi | bcbc8c6 | 2010-12-27 00:05:49 +0900 | [diff] [blame] | 887 | int nilfs_set_file_dirty(struct inode *inode, unsigned nr_dirty) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 888 | { |
| 889 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 890 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 891 | |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 892 | atomic_add(nr_dirty, &nilfs->ns_ndirtyblks); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 893 | |
Ryusuke Konishi | 458c5b0 | 2009-04-06 19:01:56 -0700 | [diff] [blame] | 894 | if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 895 | return 0; |
| 896 | |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 897 | spin_lock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 898 | if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && |
| 899 | !test_bit(NILFS_I_BUSY, &ii->i_state)) { |
| 900 | /* Because this routine may race with nilfs_dispose_list(), |
| 901 | we have to check NILFS_I_QUEUED here, too. */ |
| 902 | if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { |
| 903 | /* This will happen when somebody is freeing |
| 904 | this inode. */ |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 905 | nilfs_warning(inode->i_sb, __func__, |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 906 | "cannot get inode (ino=%lu)\n", |
| 907 | inode->i_ino); |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 908 | spin_unlock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 909 | return -EINVAL; /* NILFS_I_DIRTY may remain for |
| 910 | freeing inode */ |
| 911 | } |
Nicolas Kaiser | eaae0f3 | 2011-03-19 16:45:30 +0100 | [diff] [blame] | 912 | list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 913 | set_bit(NILFS_I_QUEUED, &ii->i_state); |
| 914 | } |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 915 | spin_unlock(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | int nilfs_mark_inode_dirty(struct inode *inode) |
| 920 | { |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 921 | struct buffer_head *ibh; |
| 922 | int err; |
| 923 | |
Ryusuke Konishi | bcbc8c6 | 2010-12-27 00:05:49 +0900 | [diff] [blame] | 924 | err = nilfs_load_inode_block(inode, &ibh); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 925 | if (unlikely(err)) { |
| 926 | nilfs_warning(inode->i_sb, __func__, |
| 927 | "failed to reget inode block.\n"); |
| 928 | return err; |
| 929 | } |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 930 | nilfs_update_inode(inode, ibh); |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 931 | mark_buffer_dirty(ibh); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 932 | nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 933 | brelse(ibh); |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * nilfs_dirty_inode - reflect changes on given inode to an inode block. |
| 939 | * @inode: inode of the file to be registered. |
| 940 | * |
| 941 | * nilfs_dirty_inode() loads a inode block containing the specified |
| 942 | * @inode and copies data from a nilfs_inode to a corresponding inode |
| 943 | * entry in the inode block. This operation is excluded from the segment |
| 944 | * construction. This function can be called both as a single operation |
| 945 | * and as a part of indivisible file operations. |
| 946 | */ |
Christoph Hellwig | aa38572 | 2011-05-27 06:53:02 -0400 | [diff] [blame] | 947 | void nilfs_dirty_inode(struct inode *inode, int flags) |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 948 | { |
| 949 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 7d6cd92 | 2010-08-21 00:30:39 +0900 | [diff] [blame] | 950 | struct nilfs_mdt_info *mdi = NILFS_MDT(inode); |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 951 | |
| 952 | if (is_bad_inode(inode)) { |
| 953 | nilfs_warning(inode->i_sb, __func__, |
| 954 | "tried to mark bad_inode dirty. ignored.\n"); |
| 955 | dump_stack(); |
| 956 | return; |
| 957 | } |
Ryusuke Konishi | 7d6cd92 | 2010-08-21 00:30:39 +0900 | [diff] [blame] | 958 | if (mdi) { |
| 959 | nilfs_mdt_mark_dirty(inode); |
| 960 | return; |
| 961 | } |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 962 | nilfs_transaction_begin(inode->i_sb, &ti, 0); |
Ryusuke Konishi | 458c5b0 | 2009-04-06 19:01:56 -0700 | [diff] [blame] | 963 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 964 | nilfs_transaction_commit(inode->i_sb); /* never fails */ |
Ryusuke Konishi | 05fe58f | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 965 | } |
Ryusuke Konishi | 622daaff | 2010-12-26 16:38:43 +0900 | [diff] [blame] | 966 | |
| 967 | int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 968 | __u64 start, __u64 len) |
| 969 | { |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 970 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
Ryusuke Konishi | 622daaff | 2010-12-26 16:38:43 +0900 | [diff] [blame] | 971 | __u64 logical = 0, phys = 0, size = 0; |
| 972 | __u32 flags = 0; |
| 973 | loff_t isize; |
| 974 | sector_t blkoff, end_blkoff; |
| 975 | sector_t delalloc_blkoff; |
| 976 | unsigned long delalloc_blklen; |
| 977 | unsigned int blkbits = inode->i_blkbits; |
| 978 | int ret, n; |
| 979 | |
| 980 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); |
| 981 | if (ret) |
| 982 | return ret; |
| 983 | |
| 984 | mutex_lock(&inode->i_mutex); |
| 985 | |
| 986 | isize = i_size_read(inode); |
| 987 | |
| 988 | blkoff = start >> blkbits; |
| 989 | end_blkoff = (start + len - 1) >> blkbits; |
| 990 | |
| 991 | delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff, |
| 992 | &delalloc_blkoff); |
| 993 | |
| 994 | do { |
| 995 | __u64 blkphy; |
| 996 | unsigned int maxblocks; |
| 997 | |
| 998 | if (delalloc_blklen && blkoff == delalloc_blkoff) { |
| 999 | if (size) { |
| 1000 | /* End of the current extent */ |
| 1001 | ret = fiemap_fill_next_extent( |
| 1002 | fieinfo, logical, phys, size, flags); |
| 1003 | if (ret) |
| 1004 | break; |
| 1005 | } |
| 1006 | if (blkoff > end_blkoff) |
| 1007 | break; |
| 1008 | |
| 1009 | flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC; |
| 1010 | logical = blkoff << blkbits; |
| 1011 | phys = 0; |
| 1012 | size = delalloc_blklen << blkbits; |
| 1013 | |
| 1014 | blkoff = delalloc_blkoff + delalloc_blklen; |
| 1015 | delalloc_blklen = nilfs_find_uncommitted_extent( |
| 1016 | inode, blkoff, &delalloc_blkoff); |
| 1017 | continue; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * Limit the number of blocks that we look up so as |
| 1022 | * not to get into the next delayed allocation extent. |
| 1023 | */ |
| 1024 | maxblocks = INT_MAX; |
| 1025 | if (delalloc_blklen) |
| 1026 | maxblocks = min_t(sector_t, delalloc_blkoff - blkoff, |
| 1027 | maxblocks); |
| 1028 | blkphy = 0; |
| 1029 | |
| 1030 | down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
| 1031 | n = nilfs_bmap_lookup_contig( |
| 1032 | NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks); |
| 1033 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
| 1034 | |
| 1035 | if (n < 0) { |
| 1036 | int past_eof; |
| 1037 | |
| 1038 | if (unlikely(n != -ENOENT)) |
| 1039 | break; /* error */ |
| 1040 | |
| 1041 | /* HOLE */ |
| 1042 | blkoff++; |
| 1043 | past_eof = ((blkoff << blkbits) >= isize); |
| 1044 | |
| 1045 | if (size) { |
| 1046 | /* End of the current extent */ |
| 1047 | |
| 1048 | if (past_eof) |
| 1049 | flags |= FIEMAP_EXTENT_LAST; |
| 1050 | |
| 1051 | ret = fiemap_fill_next_extent( |
| 1052 | fieinfo, logical, phys, size, flags); |
| 1053 | if (ret) |
| 1054 | break; |
| 1055 | size = 0; |
| 1056 | } |
| 1057 | if (blkoff > end_blkoff || past_eof) |
| 1058 | break; |
| 1059 | } else { |
| 1060 | if (size) { |
| 1061 | if (phys && blkphy << blkbits == phys + size) { |
| 1062 | /* The current extent goes on */ |
| 1063 | size += n << blkbits; |
| 1064 | } else { |
| 1065 | /* Terminate the current extent */ |
| 1066 | ret = fiemap_fill_next_extent( |
| 1067 | fieinfo, logical, phys, size, |
| 1068 | flags); |
| 1069 | if (ret || blkoff > end_blkoff) |
| 1070 | break; |
| 1071 | |
| 1072 | /* Start another extent */ |
| 1073 | flags = FIEMAP_EXTENT_MERGED; |
| 1074 | logical = blkoff << blkbits; |
| 1075 | phys = blkphy << blkbits; |
| 1076 | size = n << blkbits; |
| 1077 | } |
| 1078 | } else { |
| 1079 | /* Start a new extent */ |
| 1080 | flags = FIEMAP_EXTENT_MERGED; |
| 1081 | logical = blkoff << blkbits; |
| 1082 | phys = blkphy << blkbits; |
| 1083 | size = n << blkbits; |
| 1084 | } |
| 1085 | blkoff += n; |
| 1086 | } |
| 1087 | cond_resched(); |
| 1088 | } while (true); |
| 1089 | |
| 1090 | /* If ret is 1 then we just hit the end of the extent array */ |
| 1091 | if (ret == 1) |
| 1092 | ret = 0; |
| 1093 | |
| 1094 | mutex_unlock(&inode->i_mutex); |
| 1095 | return ret; |
| 1096 | } |