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