| 1 | |
| 2 | |
| 3 | * NILFS inode operations.
|
| 4 | *
|
| 5 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
|
| 6 | *
|
| 7 | * Written by Ryusuke Konishi.
|
| 8 | *
|
| 9 |
|
| 10 |
|
| 11 | #include <linux/buffer_head.h>
|
| 12 | #include <linux/gfp.h>
|
| 13 | #include <linux/mpage.h>
|
| 14 | #include <linux/pagemap.h>
|
| 15 | #include <linux/writeback.h>
|
| 16 | #include <linux/uio.h>
|
| 17 | #include <linux/fiemap.h>
|
| 18 | #include <linux/random.h>
|
| 19 | #include "nilfs.h"
|
| 20 | #include "btnode.h"
|
| 21 | #include "segment.h"
|
| 22 | #include "page.h"
|
| 23 | #include "mdt.h"
|
| 24 | #include "cpfile.h"
|
| 25 | #include "ifile.h"
|
| 26 |
|
| 27 | |
| 28 | * struct nilfs_iget_args - arguments used during comparison between inodes
|
| 29 | * @ino: inode number
|
| 30 | * @cno: checkpoint number
|
| 31 | * @root: pointer on NILFS root object (mounted checkpoint)
|
| 32 | * @type: inode type
|
| 33 |
|
| 34 | struct nilfs_iget_args {
|
| 35 | u64 ino;
|
| 36 | __u64 cno;
|
| 37 | struct nilfs_root *root;
|
| 38 | unsigned int type;
|
| 39 | };
|
| 40 |
|
| 41 | static int nilfs_iget_test(struct inode *inode, void *opaque);
|
| 42 |
|
| 43 | void nilfs_inode_add_blocks(struct inode *inode, int n)
|
| 44 | {
|
| 45 | struct nilfs_root *root = NILFS_I(inode)->i_root;
|
| 46 |
|
| 47 | inode_add_bytes(inode, i_blocksize(inode) * n);
|
| 48 | if (root)
|
| 49 | atomic64_add(n, &root->blocks_count);
|
| 50 | }
|
| 51 |
|
| 52 | void nilfs_inode_sub_blocks(struct inode *inode, int n)
|
| 53 | {
|
| 54 | struct nilfs_root *root = NILFS_I(inode)->i_root;
|
| 55 |
|
| 56 | inode_sub_bytes(inode, i_blocksize(inode) * n);
|
| 57 | if (root)
|
| 58 | atomic64_sub(n, &root->blocks_count);
|
| 59 | }
|
| 60 |
|
| 61 | |
| 62 | * nilfs_get_block() - get a file block on the filesystem (callback function)
|
| 63 | * @inode: inode struct of the target file
|
| 64 | * @blkoff: file block number
|
| 65 | * @bh_result: buffer head to be mapped on
|
| 66 | * @create: indicate whether allocating the block or not when it has not
|
| 67 | * been allocated yet.
|
| 68 | *
|
| 69 | * This function does not issue actual read request of the specified data
|
| 70 | * block. It is done by VFS.
|
| 71 | *
|
| 72 | * Return: 0 on success, or a negative error code on failure.
|
| 73 |
|
| 74 | int nilfs_get_block(struct inode *inode, sector_t blkoff,
|
| 75 | struct buffer_head *bh_result, int create)
|
| 76 | {
|
| 77 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 78 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
|
| 79 | __u64 blknum = 0;
|
| 80 | int err = 0, ret;
|
| 81 | unsigned int maxblocks = bh_result->b_size >> inode->i_blkbits;
|
| 82 |
|
| 83 | down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
|
| 84 | ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
|
| 85 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
|
| 86 | if (ret >= 0) {
|
| 87 | map_bh(bh_result, inode->i_sb, blknum);
|
| 88 | if (ret > 0)
|
| 89 | bh_result->b_size = (ret << inode->i_blkbits);
|
| 90 | goto out;
|
| 91 | }
|
| 92 |
|
| 93 | if (ret == -ENOENT && create) {
|
| 94 | struct nilfs_transaction_info ti;
|
| 95 |
|
| 96 | bh_result->b_blocknr = 0;
|
| 97 | err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
|
| 98 | if (unlikely(err))
|
| 99 | goto out;
|
| 100 | err = nilfs_bmap_insert(ii->i_bmap, blkoff,
|
| 101 | (unsigned long)bh_result);
|
| 102 | if (unlikely(err != 0)) {
|
| 103 | if (err == -EEXIST) {
|
| 104 | |
| 105 | * The get_block() function could be called
|
| 106 | * from multiple callers for an inode.
|
| 107 | * However, the page having this block must
|
| 108 | * be locked in this case.
|
| 109 |
|
| 110 | nilfs_warn(inode->i_sb,
|
| 111 | "%s (ino=%llu): a race condition while inserting a data block at offset=%llu",
|
| 112 | __func__, inode->i_ino,
|
| 113 | (unsigned long long)blkoff);
|
| 114 | err = -EAGAIN;
|
| 115 | }
|
| 116 | nilfs_transaction_abort(inode->i_sb);
|
| 117 | goto out;
|
| 118 | }
|
| 119 | nilfs_mark_inode_dirty_sync(inode);
|
| 120 | nilfs_transaction_commit(inode->i_sb);
|
| 121 |
|
| 122 | set_buffer_new(bh_result);
|
| 123 | set_buffer_delay(bh_result);
|
| 124 | map_bh(bh_result, inode->i_sb, 0);
|
| 125 |
|
| 126 |
|
| 127 | } else if (ret == -ENOENT) {
|
| 128 | |
| 129 | * not found is not error (e.g. hole); must return without
|
| 130 | * the mapped state flag.
|
| 131 |
|
| 132 | ;
|
| 133 | } else {
|
| 134 | err = ret;
|
| 135 | }
|
| 136 |
|
| 137 | out:
|
| 138 | return err;
|
| 139 | }
|
| 140 |
|
| 141 | |
| 142 | * nilfs_read_folio() - implement read_folio() method of nilfs_aops {}
|
| 143 | * address_space_operations.
|
| 144 | * @file: file struct of the file to be read
|
| 145 | * @folio: the folio to be read
|
| 146 | *
|
| 147 | * Return: 0 on success, or a negative error code on failure.
|
| 148 |
|
| 149 | static int nilfs_read_folio(struct file *file, struct folio *folio)
|
| 150 | {
|
| 151 | return mpage_read_folio(folio, nilfs_get_block);
|
| 152 | }
|
| 153 |
|
| 154 | static void nilfs_readahead(struct readahead_control *rac)
|
| 155 | {
|
| 156 | mpage_readahead(rac, nilfs_get_block);
|
| 157 | }
|
| 158 |
|
| 159 | static int nilfs_writepages(struct address_space *mapping,
|
| 160 | struct writeback_control *wbc)
|
| 161 | {
|
| 162 | struct inode *inode = mapping->host;
|
| 163 | int err = 0;
|
| 164 |
|
| 165 | if (sb_rdonly(inode->i_sb)) {
|
| 166 | nilfs_clear_dirty_pages(mapping, false);
|
| 167 | return -EROFS;
|
| 168 | }
|
| 169 |
|
| 170 | if (wbc->sync_mode == WB_SYNC_ALL)
|
| 171 | err = nilfs_construct_dsync_segment(inode->i_sb, inode,
|
| 172 | wbc->range_start,
|
| 173 | wbc->range_end);
|
| 174 | return err;
|
| 175 | }
|
| 176 |
|
| 177 | static bool nilfs_dirty_folio(struct address_space *mapping,
|
| 178 | struct folio *folio)
|
| 179 | {
|
| 180 | struct inode *inode = mapping->host;
|
| 181 | struct buffer_head *head;
|
| 182 | unsigned int nr_dirty = 0;
|
| 183 | bool ret = filemap_dirty_folio(mapping, folio);
|
| 184 |
|
| 185 | |
| 186 | * The page may not be locked, eg if called from try_to_unmap_one()
|
| 187 |
|
| 188 | spin_lock(&mapping->i_private_lock);
|
| 189 | head = folio_buffers(folio);
|
| 190 | if (head) {
|
| 191 | struct buffer_head *bh = head;
|
| 192 |
|
| 193 | do {
|
| 194 |
|
| 195 | if (buffer_dirty(bh) || !buffer_mapped(bh))
|
| 196 | continue;
|
| 197 |
|
| 198 | set_buffer_dirty(bh);
|
| 199 | nr_dirty++;
|
| 200 | } while (bh = bh->b_this_page, bh != head);
|
| 201 | } else if (ret) {
|
| 202 | nr_dirty = 1 << (folio_shift(folio) - inode->i_blkbits);
|
| 203 | }
|
| 204 | spin_unlock(&mapping->i_private_lock);
|
| 205 |
|
| 206 | if (nr_dirty)
|
| 207 | nilfs_set_file_dirty(inode, nr_dirty);
|
| 208 | return ret;
|
| 209 | }
|
| 210 |
|
| 211 | void nilfs_write_failed(struct address_space *mapping, loff_t to)
|
| 212 | {
|
| 213 | struct inode *inode = mapping->host;
|
| 214 |
|
| 215 | if (to > inode->i_size) {
|
| 216 | truncate_pagecache(inode, inode->i_size);
|
| 217 | nilfs_truncate(inode);
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | static int nilfs_write_begin(const struct kiocb *iocb,
|
| 222 | struct address_space *mapping,
|
| 223 | loff_t pos, unsigned len,
|
| 224 | struct folio **foliop, void **fsdata)
|
| 225 |
|
| 226 | {
|
| 227 | struct inode *inode = mapping->host;
|
| 228 | int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
|
| 229 |
|
| 230 | if (unlikely(err))
|
| 231 | return err;
|
| 232 |
|
| 233 | err = block_write_begin(mapping, pos, len, foliop, nilfs_get_block);
|
| 234 | if (unlikely(err)) {
|
| 235 | nilfs_write_failed(mapping, pos + len);
|
| 236 | nilfs_transaction_abort(inode->i_sb);
|
| 237 | }
|
| 238 | return err;
|
| 239 | }
|
| 240 |
|
| 241 | static int nilfs_write_end(const struct kiocb *iocb,
|
| 242 | struct address_space *mapping,
|
| 243 | loff_t pos, unsigned len, unsigned copied,
|
| 244 | struct folio *folio, void *fsdata)
|
| 245 | {
|
| 246 | struct inode *inode = mapping->host;
|
| 247 | unsigned int start = pos & (PAGE_SIZE - 1);
|
| 248 | unsigned int nr_dirty;
|
| 249 | int err;
|
| 250 |
|
| 251 | nr_dirty = nilfs_page_count_clean_buffers(folio, start,
|
| 252 | start + copied);
|
| 253 | copied = generic_write_end(iocb, mapping, pos, len, copied, folio,
|
| 254 | fsdata);
|
| 255 | nilfs_set_file_dirty(inode, nr_dirty);
|
| 256 | err = nilfs_transaction_commit(inode->i_sb);
|
| 257 | return err ? : copied;
|
| 258 | }
|
| 259 |
|
| 260 | const struct address_space_operations nilfs_aops = {
|
| 261 | .read_folio = nilfs_read_folio,
|
| 262 | .writepages = nilfs_writepages,
|
| 263 | .dirty_folio = nilfs_dirty_folio,
|
| 264 | .readahead = nilfs_readahead,
|
| 265 | .write_begin = nilfs_write_begin,
|
| 266 | .write_end = nilfs_write_end,
|
| 267 | .invalidate_folio = block_invalidate_folio,
|
| 268 | .migrate_folio = buffer_migrate_folio_norefs,
|
| 269 | .is_partially_uptodate = block_is_partially_uptodate,
|
| 270 | };
|
| 271 |
|
| 272 | const struct address_space_operations nilfs_buffer_cache_aops = {
|
| 273 | .invalidate_folio = block_invalidate_folio,
|
| 274 | };
|
| 275 |
|
| 276 | static int nilfs_insert_inode_locked(struct inode *inode,
|
| 277 | struct nilfs_root *root, u64 ino)
|
| 278 | {
|
| 279 | struct nilfs_iget_args args = {
|
| 280 | .ino = ino, .root = root, .cno = 0, .type = NILFS_I_TYPE_NORMAL
|
| 281 | };
|
| 282 |
|
| 283 | return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
|
| 284 | }
|
| 285 |
|
| 286 | struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
|
| 287 | {
|
| 288 | struct super_block *sb = dir->i_sb;
|
| 289 | struct inode *inode;
|
| 290 | struct nilfs_inode_info *ii;
|
| 291 | struct nilfs_root *root;
|
| 292 | struct buffer_head *bh;
|
| 293 | int err = -ENOMEM;
|
| 294 | u64 ino;
|
| 295 |
|
| 296 | inode = new_inode(sb);
|
| 297 | if (unlikely(!inode))
|
| 298 | goto failed;
|
| 299 |
|
| 300 | mapping_set_gfp_mask(inode->i_mapping,
|
| 301 | mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
|
| 302 |
|
| 303 | root = NILFS_I(dir)->i_root;
|
| 304 | ii = NILFS_I(inode);
|
| 305 | ii->i_state = BIT(NILFS_I_NEW);
|
| 306 | ii->i_type = NILFS_I_TYPE_NORMAL;
|
| 307 | ii->i_root = root;
|
| 308 |
|
| 309 | err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
|
| 310 | if (unlikely(err))
|
| 311 | goto failed_ifile_create_inode;
|
| 312 |
|
| 313 | ii->i_bh = bh;
|
| 314 |
|
| 315 | atomic64_inc(&root->inodes_count);
|
| 316 | inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
|
| 317 | inode->i_ino = ino;
|
| 318 | simple_inode_init_ts(inode);
|
| 319 |
|
| 320 | if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
|
| 321 | err = nilfs_bmap_read(ii->i_bmap, NULL);
|
| 322 | if (err < 0)
|
| 323 | goto failed_after_creation;
|
| 324 |
|
| 325 | set_bit(NILFS_I_BMAP, &ii->i_state);
|
| 326 |
|
| 327 | }
|
| 328 |
|
| 329 | ii->i_flags = nilfs_mask_flags(
|
| 330 | mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
|
| 331 |
|
| 332 | ii->i_dir_start_lookup = 0;
|
| 333 | nilfs_set_inode_flags(inode);
|
| 334 | inode->i_generation = get_random_u32();
|
| 335 | if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
|
| 336 | err = -EIO;
|
| 337 | goto failed_after_creation;
|
| 338 | }
|
| 339 |
|
| 340 | err = nilfs_init_acl(inode, dir);
|
| 341 | if (unlikely(err))
|
| 342 | |
| 343 | * Never occur. When supporting nilfs_init_acl(),
|
| 344 | * proper cancellation of above jobs should be considered.
|
| 345 |
|
| 346 | goto failed_after_creation;
|
| 347 |
|
| 348 | return inode;
|
| 349 |
|
| 350 | failed_after_creation:
|
| 351 | clear_nlink(inode);
|
| 352 | if (inode_state_read_once(inode) & I_NEW)
|
| 353 | unlock_new_inode(inode);
|
| 354 | iput(inode); |
| 355 | * raw_inode will be deleted through
|
| 356 | * nilfs_evict_inode().
|
| 357 |
|
| 358 | goto failed;
|
| 359 |
|
| 360 | failed_ifile_create_inode:
|
| 361 | make_bad_inode(inode);
|
| 362 | iput(inode);
|
| 363 | failed:
|
| 364 | return ERR_PTR(err);
|
| 365 | }
|
| 366 |
|
| 367 | void nilfs_set_inode_flags(struct inode *inode)
|
| 368 | {
|
| 369 | unsigned int flags = NILFS_I(inode)->i_flags;
|
| 370 | unsigned int new_fl = 0;
|
| 371 |
|
| 372 | if (flags & FS_SYNC_FL)
|
| 373 | new_fl |= S_SYNC;
|
| 374 | if (flags & FS_APPEND_FL)
|
| 375 | new_fl |= S_APPEND;
|
| 376 | if (flags & FS_IMMUTABLE_FL)
|
| 377 | new_fl |= S_IMMUTABLE;
|
| 378 | if (flags & FS_NOATIME_FL)
|
| 379 | new_fl |= S_NOATIME;
|
| 380 | if (flags & FS_DIRSYNC_FL)
|
| 381 | new_fl |= S_DIRSYNC;
|
| 382 | inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
|
| 383 | S_NOATIME | S_DIRSYNC);
|
| 384 | }
|
| 385 |
|
| 386 | int nilfs_read_inode_common(struct inode *inode,
|
| 387 | struct nilfs_inode *raw_inode)
|
| 388 | {
|
| 389 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 390 | int err;
|
| 391 |
|
| 392 | inode->i_mode = le16_to_cpu(raw_inode->i_mode);
|
| 393 | i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
|
| 394 | i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
|
| 395 | set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
|
| 396 | inode->i_size = le64_to_cpu(raw_inode->i_size);
|
| 397 | inode_set_atime(inode, le64_to_cpu(raw_inode->i_mtime),
|
| 398 | le32_to_cpu(raw_inode->i_mtime_nsec));
|
| 399 | inode_set_ctime(inode, le64_to_cpu(raw_inode->i_ctime),
|
| 400 | le32_to_cpu(raw_inode->i_ctime_nsec));
|
| 401 | inode_set_mtime(inode, le64_to_cpu(raw_inode->i_mtime),
|
| 402 | le32_to_cpu(raw_inode->i_mtime_nsec));
|
| 403 | if (nilfs_is_metadata_file_inode(inode) && !S_ISREG(inode->i_mode))
|
| 404 | return -EIO;
|
| 405 | if (inode->i_nlink == 0)
|
| 406 | return -ESTALE;
|
| 407 |
|
| 408 | inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
|
| 409 | ii->i_flags = le32_to_cpu(raw_inode->i_flags);
|
| 410 | ii->i_dir_start_lookup = 0;
|
| 411 | inode->i_generation = le32_to_cpu(raw_inode->i_generation);
|
| 412 |
|
| 413 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
|
| 414 | S_ISLNK(inode->i_mode)) {
|
| 415 | err = nilfs_bmap_read(ii->i_bmap, raw_inode);
|
| 416 | if (err < 0)
|
| 417 | return err;
|
| 418 | set_bit(NILFS_I_BMAP, &ii->i_state);
|
| 419 |
|
| 420 | }
|
| 421 | return 0;
|
| 422 | }
|
| 423 |
|
| 424 | static int __nilfs_read_inode(struct super_block *sb,
|
| 425 | struct nilfs_root *root, u64 ino,
|
| 426 | struct inode *inode)
|
| 427 | {
|
| 428 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 429 | struct buffer_head *bh;
|
| 430 | struct nilfs_inode *raw_inode;
|
| 431 | int err;
|
| 432 |
|
| 433 | down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
|
| 434 | err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
|
| 435 | if (unlikely(err))
|
| 436 | goto bad_inode;
|
| 437 |
|
| 438 | raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
|
| 439 |
|
| 440 | err = nilfs_read_inode_common(inode, raw_inode);
|
| 441 | if (err)
|
| 442 | goto failed_unmap;
|
| 443 |
|
| 444 | if (S_ISREG(inode->i_mode)) {
|
| 445 | inode->i_op = &nilfs_file_inode_operations;
|
| 446 | inode->i_fop = &nilfs_file_operations;
|
| 447 | inode->i_mapping->a_ops = &nilfs_aops;
|
| 448 | } else if (S_ISDIR(inode->i_mode)) {
|
| 449 | inode->i_op = &nilfs_dir_inode_operations;
|
| 450 | inode->i_fop = &nilfs_dir_operations;
|
| 451 | inode->i_mapping->a_ops = &nilfs_aops;
|
| 452 | } else if (S_ISLNK(inode->i_mode)) {
|
| 453 | inode->i_op = &nilfs_symlink_inode_operations;
|
| 454 | inode_nohighmem(inode);
|
| 455 | inode->i_mapping->a_ops = &nilfs_aops;
|
| 456 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
|
| 457 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
|
| 458 | inode->i_op = &nilfs_special_inode_operations;
|
| 459 | init_special_inode(
|
| 460 | inode, inode->i_mode,
|
| 461 | huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
|
| 462 | } else {
|
| 463 | nilfs_error(sb,
|
| 464 | "invalid file type bits in mode 0%o for inode %llu",
|
| 465 | inode->i_mode, ino);
|
| 466 | err = -EIO;
|
| 467 | goto failed_unmap;
|
| 468 | }
|
| 469 | nilfs_ifile_unmap_inode(raw_inode);
|
| 470 | brelse(bh);
|
| 471 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
|
| 472 | nilfs_set_inode_flags(inode);
|
| 473 | mapping_set_gfp_mask(inode->i_mapping,
|
| 474 | mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
|
| 475 | return 0;
|
| 476 |
|
| 477 | failed_unmap:
|
| 478 | nilfs_ifile_unmap_inode(raw_inode);
|
| 479 | brelse(bh);
|
| 480 |
|
| 481 | bad_inode:
|
| 482 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
|
| 483 | return err;
|
| 484 | }
|
| 485 |
|
| 486 | static int nilfs_iget_test(struct inode *inode, void *opaque)
|
| 487 | {
|
| 488 | struct nilfs_iget_args *args = opaque;
|
| 489 | struct nilfs_inode_info *ii;
|
| 490 |
|
| 491 | if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
|
| 492 | return 0;
|
| 493 |
|
| 494 | ii = NILFS_I(inode);
|
| 495 | if (ii->i_type != args->type)
|
| 496 | return 0;
|
| 497 |
|
| 498 | return !(args->type & NILFS_I_TYPE_GC) || args->cno == ii->i_cno;
|
| 499 | }
|
| 500 |
|
| 501 | static int nilfs_iget_set(struct inode *inode, void *opaque)
|
| 502 | {
|
| 503 | struct nilfs_iget_args *args = opaque;
|
| 504 |
|
| 505 | inode->i_ino = args->ino;
|
| 506 | NILFS_I(inode)->i_cno = args->cno;
|
| 507 | NILFS_I(inode)->i_root = args->root;
|
| 508 | NILFS_I(inode)->i_type = args->type;
|
| 509 | if (args->root && args->ino == NILFS_ROOT_INO)
|
| 510 | nilfs_get_root(args->root);
|
| 511 | return 0;
|
| 512 | }
|
| 513 |
|
| 514 | struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
|
| 515 | u64 ino)
|
| 516 | {
|
| 517 | struct nilfs_iget_args args = {
|
| 518 | .ino = ino, .root = root, .cno = 0, .type = NILFS_I_TYPE_NORMAL
|
| 519 | };
|
| 520 |
|
| 521 | return ilookup5(sb, ino, nilfs_iget_test, &args);
|
| 522 | }
|
| 523 |
|
| 524 | struct inode *nilfs_iget_locked(struct super_block *sb,
|
| 525 | struct nilfs_root *root, u64 ino)
|
| 526 | {
|
| 527 | struct nilfs_iget_args args = {
|
| 528 | .ino = ino, .root = root, .cno = 0, .type = NILFS_I_TYPE_NORMAL
|
| 529 | };
|
| 530 |
|
| 531 | return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
|
| 532 | }
|
| 533 |
|
| 534 | struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
|
| 535 | u64 ino)
|
| 536 | {
|
| 537 | struct inode *inode;
|
| 538 | int err;
|
| 539 |
|
| 540 | inode = nilfs_iget_locked(sb, root, ino);
|
| 541 | if (unlikely(!inode))
|
| 542 | return ERR_PTR(-ENOMEM);
|
| 543 |
|
| 544 | if (!(inode_state_read_once(inode) & I_NEW)) {
|
| 545 | if (!inode->i_nlink) {
|
| 546 | iput(inode);
|
| 547 | return ERR_PTR(-ESTALE);
|
| 548 | }
|
| 549 | return inode;
|
| 550 | }
|
| 551 |
|
| 552 | err = __nilfs_read_inode(sb, root, ino, inode);
|
| 553 | if (unlikely(err)) {
|
| 554 | iget_failed(inode);
|
| 555 | return ERR_PTR(err);
|
| 556 | }
|
| 557 | unlock_new_inode(inode);
|
| 558 | return inode;
|
| 559 | }
|
| 560 |
|
| 561 | struct inode *nilfs_iget_for_gc(struct super_block *sb, u64 ino, __u64 cno)
|
| 562 | {
|
| 563 | struct nilfs_iget_args args = {
|
| 564 | .ino = ino, .root = NULL, .cno = cno, .type = NILFS_I_TYPE_GC
|
| 565 | };
|
| 566 | struct inode *inode;
|
| 567 | int err;
|
| 568 |
|
| 569 | inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
|
| 570 | if (unlikely(!inode))
|
| 571 | return ERR_PTR(-ENOMEM);
|
| 572 | if (!(inode_state_read_once(inode) & I_NEW))
|
| 573 | return inode;
|
| 574 |
|
| 575 | err = nilfs_init_gcinode(inode);
|
| 576 | if (unlikely(err)) {
|
| 577 | iget_failed(inode);
|
| 578 | return ERR_PTR(err);
|
| 579 | }
|
| 580 | unlock_new_inode(inode);
|
| 581 | return inode;
|
| 582 | }
|
| 583 |
|
| 584 | |
| 585 | * nilfs_attach_btree_node_cache - attach a B-tree node cache to the inode
|
| 586 | * @inode: inode object
|
| 587 | *
|
| 588 | * nilfs_attach_btree_node_cache() attaches a B-tree node cache to @inode,
|
| 589 | * or does nothing if the inode already has it. This function allocates
|
| 590 | * an additional inode to maintain page cache of B-tree nodes one-on-one.
|
| 591 | *
|
| 592 | * Return: 0 on success, or %-ENOMEM if memory is insufficient.
|
| 593 |
|
| 594 | int nilfs_attach_btree_node_cache(struct inode *inode)
|
| 595 | {
|
| 596 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 597 | struct inode *btnc_inode;
|
| 598 | struct nilfs_iget_args args;
|
| 599 |
|
| 600 | if (ii->i_assoc_inode)
|
| 601 | return 0;
|
| 602 |
|
| 603 | args.ino = inode->i_ino;
|
| 604 | args.root = ii->i_root;
|
| 605 | args.cno = ii->i_cno;
|
| 606 | args.type = ii->i_type | NILFS_I_TYPE_BTNC;
|
| 607 |
|
| 608 | btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
|
| 609 | nilfs_iget_set, &args);
|
| 610 | if (unlikely(!btnc_inode))
|
| 611 | return -ENOMEM;
|
| 612 | if (inode_state_read_once(btnc_inode) & I_NEW) {
|
| 613 | nilfs_init_btnc_inode(btnc_inode);
|
| 614 | unlock_new_inode(btnc_inode);
|
| 615 | }
|
| 616 | NILFS_I(btnc_inode)->i_assoc_inode = inode;
|
| 617 | NILFS_I(btnc_inode)->i_bmap = ii->i_bmap;
|
| 618 | ii->i_assoc_inode = btnc_inode;
|
| 619 |
|
| 620 | return 0;
|
| 621 | }
|
| 622 |
|
| 623 | |
| 624 | * nilfs_detach_btree_node_cache - detach the B-tree node cache from the inode
|
| 625 | * @inode: inode object
|
| 626 | *
|
| 627 | * nilfs_detach_btree_node_cache() detaches the B-tree node cache and its
|
| 628 | * holder inode bound to @inode, or does nothing if @inode doesn't have it.
|
| 629 |
|
| 630 | void nilfs_detach_btree_node_cache(struct inode *inode)
|
| 631 | {
|
| 632 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 633 | struct inode *btnc_inode = ii->i_assoc_inode;
|
| 634 |
|
| 635 | if (btnc_inode) {
|
| 636 | NILFS_I(btnc_inode)->i_assoc_inode = NULL;
|
| 637 | ii->i_assoc_inode = NULL;
|
| 638 | iput(btnc_inode);
|
| 639 | }
|
| 640 | }
|
| 641 |
|
| 642 | |
| 643 | * nilfs_iget_for_shadow - obtain inode for shadow mapping
|
| 644 | * @inode: inode object that uses shadow mapping
|
| 645 | *
|
| 646 | * nilfs_iget_for_shadow() allocates a pair of inodes that holds page
|
| 647 | * caches for shadow mapping. The page cache for data pages is set up
|
| 648 | * in one inode and the one for b-tree node pages is set up in the
|
| 649 | * other inode, which is attached to the former inode.
|
| 650 | *
|
| 651 | * Return: a pointer to the inode for data pages on success, or %-ENOMEM
|
| 652 | * if memory is insufficient.
|
| 653 |
|
| 654 | struct inode *nilfs_iget_for_shadow(struct inode *inode)
|
| 655 | {
|
| 656 | struct nilfs_iget_args args = {
|
| 657 | .ino = inode->i_ino, .root = NULL, .cno = 0,
|
| 658 | .type = NILFS_I_TYPE_SHADOW
|
| 659 | };
|
| 660 | struct inode *s_inode;
|
| 661 | int err;
|
| 662 |
|
| 663 | s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
|
| 664 | nilfs_iget_set, &args);
|
| 665 | if (unlikely(!s_inode))
|
| 666 | return ERR_PTR(-ENOMEM);
|
| 667 | if (!(inode_state_read_once(s_inode) & I_NEW))
|
| 668 | return inode;
|
| 669 |
|
| 670 | NILFS_I(s_inode)->i_flags = 0;
|
| 671 | memset(NILFS_I(s_inode)->i_bmap, 0, sizeof(struct nilfs_bmap));
|
| 672 | mapping_set_gfp_mask(s_inode->i_mapping, GFP_NOFS);
|
| 673 | s_inode->i_mapping->a_ops = &nilfs_buffer_cache_aops;
|
| 674 |
|
| 675 | err = nilfs_attach_btree_node_cache(s_inode);
|
| 676 | if (unlikely(err)) {
|
| 677 | iget_failed(s_inode);
|
| 678 | return ERR_PTR(err);
|
| 679 | }
|
| 680 | unlock_new_inode(s_inode);
|
| 681 | return s_inode;
|
| 682 | }
|
| 683 |
|
| 684 | |
| 685 | * nilfs_write_inode_common - export common inode information to on-disk inode
|
| 686 | * @inode: inode object
|
| 687 | * @raw_inode: on-disk inode
|
| 688 | *
|
| 689 | * This function writes standard information from the on-memory inode @inode
|
| 690 | * to @raw_inode on ifile, cpfile or a super root block. Since inode bmap
|
| 691 | * data is not exported, nilfs_bmap_write() must be called separately during
|
| 692 | * log writing.
|
| 693 |
|
| 694 | void nilfs_write_inode_common(struct inode *inode,
|
| 695 | struct nilfs_inode *raw_inode)
|
| 696 | {
|
| 697 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 698 |
|
| 699 | raw_inode->i_mode = cpu_to_le16(inode->i_mode);
|
| 700 | raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
|
| 701 | raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
|
| 702 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
|
| 703 | raw_inode->i_size = cpu_to_le64(inode->i_size);
|
| 704 | raw_inode->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
|
| 705 | raw_inode->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode));
|
| 706 | raw_inode->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
|
| 707 | raw_inode->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
|
| 708 | raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
|
| 709 |
|
| 710 | raw_inode->i_flags = cpu_to_le32(ii->i_flags);
|
| 711 | raw_inode->i_generation = cpu_to_le32(inode->i_generation);
|
| 712 |
|
| 713 | |
| 714 | * When extending inode, nilfs->ns_inode_size should be checked
|
| 715 | * for substitutions of appended fields.
|
| 716 |
|
| 717 | }
|
| 718 |
|
| 719 | void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
|
| 720 | {
|
| 721 | u64 ino = inode->i_ino;
|
| 722 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 723 | struct inode *ifile = ii->i_root->ifile;
|
| 724 | struct nilfs_inode *raw_inode;
|
| 725 |
|
| 726 | raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
|
| 727 |
|
| 728 | if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
|
| 729 | memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
|
| 730 | if (flags & I_DIRTY_DATASYNC)
|
| 731 | set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
|
| 732 |
|
| 733 | nilfs_write_inode_common(inode, raw_inode);
|
| 734 |
|
| 735 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
|
| 736 | raw_inode->i_device_code =
|
| 737 | cpu_to_le64(huge_encode_dev(inode->i_rdev));
|
| 738 |
|
| 739 | nilfs_ifile_unmap_inode(raw_inode);
|
| 740 | }
|
| 741 |
|
| 742 | #define NILFS_MAX_TRUNCATE_BLOCKS 16384
|
| 743 |
|
| 744 | static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
|
| 745 | unsigned long from)
|
| 746 | {
|
| 747 | __u64 b;
|
| 748 | int ret;
|
| 749 |
|
| 750 | if (!test_bit(NILFS_I_BMAP, &ii->i_state))
|
| 751 | return;
|
| 752 | repeat:
|
| 753 | ret = nilfs_bmap_last_key(ii->i_bmap, &b);
|
| 754 | if (ret == -ENOENT)
|
| 755 | return;
|
| 756 | else if (ret < 0)
|
| 757 | goto failed;
|
| 758 |
|
| 759 | if (b < from)
|
| 760 | return;
|
| 761 |
|
| 762 | b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
|
| 763 | ret = nilfs_bmap_truncate(ii->i_bmap, b);
|
| 764 | nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
|
| 765 | if (!ret || (ret == -ENOMEM &&
|
| 766 | nilfs_bmap_truncate(ii->i_bmap, b) == 0))
|
| 767 | goto repeat;
|
| 768 |
|
| 769 | failed:
|
| 770 | nilfs_warn(ii->vfs_inode.i_sb, "error %d truncating bmap (ino=%llu)",
|
| 771 | ret, ii->vfs_inode.i_ino);
|
| 772 | }
|
| 773 |
|
| 774 | void nilfs_truncate(struct inode *inode)
|
| 775 | {
|
| 776 | unsigned long blkoff;
|
| 777 | unsigned int blocksize;
|
| 778 | struct nilfs_transaction_info ti;
|
| 779 | struct super_block *sb = inode->i_sb;
|
| 780 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 781 |
|
| 782 | if (!test_bit(NILFS_I_BMAP, &ii->i_state))
|
| 783 | return;
|
| 784 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
|
| 785 | return;
|
| 786 |
|
| 787 | blocksize = sb->s_blocksize;
|
| 788 | blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
|
| 789 | nilfs_transaction_begin(sb, &ti, 0);
|
| 790 |
|
| 791 | block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
|
| 792 |
|
| 793 | nilfs_truncate_bmap(ii, blkoff);
|
| 794 |
|
| 795 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
|
| 796 | if (IS_SYNC(inode))
|
| 797 | nilfs_set_transaction_flag(NILFS_TI_SYNC);
|
| 798 |
|
| 799 | nilfs_mark_inode_dirty(inode);
|
| 800 | nilfs_set_file_dirty(inode, 0);
|
| 801 | nilfs_transaction_commit(sb);
|
| 802 | |
| 803 | * May construct a logical segment and may fail in sync mode.
|
| 804 | * But truncate has no return value.
|
| 805 |
|
| 806 | }
|
| 807 |
|
| 808 | static void nilfs_clear_inode(struct inode *inode)
|
| 809 | {
|
| 810 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 811 |
|
| 812 | |
| 813 | * Free resources allocated in nilfs_read_inode(), here.
|
| 814 |
|
| 815 | BUG_ON(!list_empty(&ii->i_dirty));
|
| 816 | brelse(ii->i_bh);
|
| 817 | ii->i_bh = NULL;
|
| 818 |
|
| 819 | if (nilfs_is_metadata_file_inode(inode))
|
| 820 | nilfs_mdt_clear(inode);
|
| 821 |
|
| 822 | if (test_bit(NILFS_I_BMAP, &ii->i_state))
|
| 823 | nilfs_bmap_clear(ii->i_bmap);
|
| 824 |
|
| 825 | if (!(ii->i_type & NILFS_I_TYPE_BTNC))
|
| 826 | nilfs_detach_btree_node_cache(inode);
|
| 827 |
|
| 828 | if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
|
| 829 | nilfs_put_root(ii->i_root);
|
| 830 | }
|
| 831 |
|
| 832 | void nilfs_evict_inode(struct inode *inode)
|
| 833 | {
|
| 834 | struct nilfs_transaction_info ti;
|
| 835 | struct super_block *sb = inode->i_sb;
|
| 836 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 837 | struct the_nilfs *nilfs;
|
| 838 | int ret;
|
| 839 |
|
| 840 | if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
|
| 841 | truncate_inode_pages_final(&inode->i_data);
|
| 842 | clear_inode(inode);
|
| 843 | nilfs_clear_inode(inode);
|
| 844 | return;
|
| 845 | }
|
| 846 | nilfs_transaction_begin(sb, &ti, 0);
|
| 847 |
|
| 848 | truncate_inode_pages_final(&inode->i_data);
|
| 849 |
|
| 850 | nilfs = sb->s_fs_info;
|
| 851 | if (unlikely(sb_rdonly(sb) || !nilfs->ns_writer)) {
|
| 852 | |
| 853 | * If this inode is about to be disposed after the file system
|
| 854 | * has been degraded to read-only due to file system corruption
|
| 855 | * or after the writer has been detached, do not make any
|
| 856 | * changes that cause writes, just clear it.
|
| 857 | * Do this check after read-locking ns_segctor_sem by
|
| 858 | * nilfs_transaction_begin() in order to avoid a race with
|
| 859 | * the writer detach operation.
|
| 860 |
|
| 861 | clear_inode(inode);
|
| 862 | nilfs_clear_inode(inode);
|
| 863 | nilfs_transaction_abort(sb);
|
| 864 | return;
|
| 865 | }
|
| 866 |
|
| 867 |
|
| 868 | nilfs_truncate_bmap(ii, 0);
|
| 869 | nilfs_mark_inode_dirty(inode);
|
| 870 | clear_inode(inode);
|
| 871 |
|
| 872 | ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
|
| 873 | if (!ret)
|
| 874 | atomic64_dec(&ii->i_root->inodes_count);
|
| 875 |
|
| 876 | nilfs_clear_inode(inode);
|
| 877 |
|
| 878 | nilfs_transaction_commit(sb);
|
| 879 | }
|
| 880 |
|
| 881 | int nilfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
|
| 882 | struct iattr *iattr)
|
| 883 | {
|
| 884 | struct nilfs_transaction_info ti;
|
| 885 | struct inode *inode = d_inode(dentry);
|
| 886 | struct super_block *sb = inode->i_sb;
|
| 887 | int err;
|
| 888 |
|
| 889 | err = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
|
| 890 | if (err)
|
| 891 | return err;
|
| 892 |
|
| 893 | err = nilfs_transaction_begin(sb, &ti, 0);
|
| 894 | if (unlikely(err))
|
| 895 | return err;
|
| 896 |
|
| 897 | if ((iattr->ia_valid & ATTR_SIZE) &&
|
| 898 | iattr->ia_size != i_size_read(inode)) {
|
| 899 | inode_dio_wait(inode);
|
| 900 | truncate_setsize(inode, iattr->ia_size);
|
| 901 | nilfs_truncate(inode);
|
| 902 | }
|
| 903 |
|
| 904 | setattr_copy(&nop_mnt_idmap, inode, iattr);
|
| 905 | mark_inode_dirty(inode);
|
| 906 |
|
| 907 | if (iattr->ia_valid & ATTR_MODE) {
|
| 908 | err = nilfs_acl_chmod(inode);
|
| 909 | if (unlikely(err))
|
| 910 | goto out_err;
|
| 911 | }
|
| 912 |
|
| 913 | return nilfs_transaction_commit(sb);
|
| 914 |
|
| 915 | out_err:
|
| 916 | nilfs_transaction_abort(sb);
|
| 917 | return err;
|
| 918 | }
|
| 919 |
|
| 920 | int nilfs_permission(const struct mnt_idmap *idmap, struct inode *inode,
|
| 921 | int mask)
|
| 922 | {
|
| 923 | struct nilfs_root *root = NILFS_I(inode)->i_root;
|
| 924 |
|
| 925 | if ((mask & MAY_WRITE) && root &&
|
| 926 | root->cno != NILFS_CPTREE_CURRENT_CNO)
|
| 927 | return -EROFS;
|
| 928 |
|
| 929 | return generic_permission(&nop_mnt_idmap, inode, mask);
|
| 930 | }
|
| 931 |
|
| 932 | int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
|
| 933 | {
|
| 934 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
|
| 935 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 936 | int err;
|
| 937 |
|
| 938 | spin_lock(&nilfs->ns_inode_lock);
|
| 939 | if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) {
|
| 940 | spin_unlock(&nilfs->ns_inode_lock);
|
| 941 | err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
|
| 942 | inode->i_ino, pbh);
|
| 943 | if (unlikely(err))
|
| 944 | return err;
|
| 945 | spin_lock(&nilfs->ns_inode_lock);
|
| 946 | if (ii->i_bh == NULL)
|
| 947 | ii->i_bh = *pbh;
|
| 948 | else if (unlikely(!buffer_uptodate(ii->i_bh))) {
|
| 949 | __brelse(ii->i_bh);
|
| 950 | ii->i_bh = *pbh;
|
| 951 | } else {
|
| 952 | brelse(*pbh);
|
| 953 | *pbh = ii->i_bh;
|
| 954 | }
|
| 955 | } else
|
| 956 | *pbh = ii->i_bh;
|
| 957 |
|
| 958 | get_bh(*pbh);
|
| 959 | spin_unlock(&nilfs->ns_inode_lock);
|
| 960 | return 0;
|
| 961 | }
|
| 962 |
|
| 963 | int nilfs_inode_dirty(struct inode *inode)
|
| 964 | {
|
| 965 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 966 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
|
| 967 | int ret = 0;
|
| 968 |
|
| 969 | if (!list_empty(&ii->i_dirty)) {
|
| 970 | spin_lock(&nilfs->ns_inode_lock);
|
| 971 | ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
|
| 972 | test_bit(NILFS_I_BUSY, &ii->i_state);
|
| 973 | spin_unlock(&nilfs->ns_inode_lock);
|
| 974 | }
|
| 975 | return ret;
|
| 976 | }
|
| 977 |
|
| 978 | int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty)
|
| 979 | {
|
| 980 | struct nilfs_inode_info *ii = NILFS_I(inode);
|
| 981 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
|
| 982 |
|
| 983 | atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
|
| 984 |
|
| 985 | if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
|
| 986 | return 0;
|
| 987 |
|
| 988 | spin_lock(&nilfs->ns_inode_lock);
|
| 989 | if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
|
| 990 | !test_bit(NILFS_I_BUSY, &ii->i_state)) {
|
| 991 | |
| 992 | * Because this routine may race with nilfs_dispose_list(),
|
| 993 | * we have to check NILFS_I_QUEUED here, too.
|
| 994 |
|
| 995 | if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
|
| 996 | |
| 997 | * This will happen when somebody is freeing
|
| 998 | * this inode.
|
| 999 |
|
| 1000 | nilfs_warn(inode->i_sb,
|
| 1001 | "cannot set file dirty (ino=%llu): the file is being freed",
|
| 1002 | inode->i_ino);
|
| 1003 | spin_unlock(&nilfs->ns_inode_lock);
|
| 1004 | return -EINVAL; |
| 1005 | * NILFS_I_DIRTY may remain for
|
| 1006 | * freeing inode.
|
| 1007 |
|
| 1008 | }
|
| 1009 | list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
|
| 1010 | set_bit(NILFS_I_QUEUED, &ii->i_state);
|
| 1011 | }
|
| 1012 | spin_unlock(&nilfs->ns_inode_lock);
|
| 1013 | return 0;
|
| 1014 | }
|
| 1015 |
|
| 1016 | int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
|
| 1017 | {
|
| 1018 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
|
| 1019 | struct buffer_head *ibh;
|
| 1020 | int err;
|
| 1021 |
|
| 1022 | |
| 1023 | * Do not dirty inodes after the log writer has been detached
|
| 1024 | * and its nilfs_root struct has been freed.
|
| 1025 |
|
| 1026 | if (unlikely(nilfs_purging(nilfs)))
|
| 1027 | return 0;
|
| 1028 |
|
| 1029 | err = nilfs_load_inode_block(inode, &ibh);
|
| 1030 | if (unlikely(err)) {
|
| 1031 | nilfs_warn(inode->i_sb,
|
| 1032 | "cannot mark inode dirty (ino=%llu): error %d loading inode block",
|
| 1033 | inode->i_ino, err);
|
| 1034 | return err;
|
| 1035 | }
|
| 1036 | nilfs_update_inode(inode, ibh, flags);
|
| 1037 | mark_buffer_dirty(ibh);
|
| 1038 | nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
|
| 1039 | brelse(ibh);
|
| 1040 | return 0;
|
| 1041 | }
|
| 1042 |
|
| 1043 | |
| 1044 | * nilfs_dirty_inode - reflect changes on given inode to an inode block.
|
| 1045 | * @inode: inode of the file to be registered.
|
| 1046 | * @flags: flags to determine the dirty state of the inode
|
| 1047 | *
|
| 1048 | * nilfs_dirty_inode() loads a inode block containing the specified
|
| 1049 | * @inode and copies data from a nilfs_inode to a corresponding inode
|
| 1050 | * entry in the inode block. This operation is excluded from the segment
|
| 1051 | * construction. This function can be called both as a single operation
|
| 1052 | * and as a part of indivisible file operations.
|
| 1053 |
|
| 1054 | void nilfs_dirty_inode(struct inode *inode, int flags)
|
| 1055 | {
|
| 1056 | struct nilfs_transaction_info ti;
|
| 1057 | struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
|
| 1058 |
|
| 1059 | if (is_bad_inode(inode)) {
|
| 1060 | nilfs_warn(inode->i_sb,
|
| 1061 | "tried to mark bad_inode dirty. ignored.");
|
| 1062 | dump_stack();
|
| 1063 | return;
|
| 1064 | }
|
| 1065 | if (mdi) {
|
| 1066 | nilfs_mdt_mark_dirty(inode);
|
| 1067 | return;
|
| 1068 | }
|
| 1069 | nilfs_transaction_begin(inode->i_sb, &ti, 0);
|
| 1070 | __nilfs_mark_inode_dirty(inode, flags);
|
| 1071 | nilfs_transaction_commit(inode->i_sb);
|
| 1072 | }
|
| 1073 |
|
| 1074 | int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
|
| 1075 | __u64 start, __u64 len)
|
| 1076 | {
|
| 1077 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
|
| 1078 | __u64 logical = 0, phys = 0, size = 0;
|
| 1079 | __u32 flags = 0;
|
| 1080 | loff_t isize;
|
| 1081 | sector_t blkoff, end_blkoff;
|
| 1082 | sector_t delalloc_blkoff;
|
| 1083 | unsigned long delalloc_blklen;
|
| 1084 | unsigned int blkbits = inode->i_blkbits;
|
| 1085 | int ret, n;
|
| 1086 |
|
| 1087 | ret = fiemap_prep(inode, fieinfo, start, &len, 0);
|
| 1088 | if (ret)
|
| 1089 | return ret;
|
| 1090 |
|
| 1091 | inode_lock(inode);
|
| 1092 |
|
| 1093 | isize = i_size_read(inode);
|
| 1094 |
|
| 1095 | blkoff = start >> blkbits;
|
| 1096 | end_blkoff = (start + len - 1) >> blkbits;
|
| 1097 |
|
| 1098 | delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
|
| 1099 | &delalloc_blkoff);
|
| 1100 |
|
| 1101 | do {
|
| 1102 | __u64 blkphy;
|
| 1103 | unsigned int maxblocks;
|
| 1104 |
|
| 1105 | if (delalloc_blklen && blkoff == delalloc_blkoff) {
|
| 1106 | if (size) {
|
| 1107 |
|
| 1108 | ret = fiemap_fill_next_extent(
|
| 1109 | fieinfo, logical, phys, size, flags);
|
| 1110 | if (ret)
|
| 1111 | break;
|
| 1112 | }
|
| 1113 | if (blkoff > end_blkoff)
|
| 1114 | break;
|
| 1115 |
|
| 1116 | flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
|
| 1117 | logical = blkoff << blkbits;
|
| 1118 | phys = 0;
|
| 1119 | size = delalloc_blklen << blkbits;
|
| 1120 |
|
| 1121 | blkoff = delalloc_blkoff + delalloc_blklen;
|
| 1122 | delalloc_blklen = nilfs_find_uncommitted_extent(
|
| 1123 | inode, blkoff, &delalloc_blkoff);
|
| 1124 | continue;
|
| 1125 | }
|
| 1126 |
|
| 1127 | |
| 1128 | * Limit the number of blocks that we look up so as
|
| 1129 | * not to get into the next delayed allocation extent.
|
| 1130 |
|
| 1131 | maxblocks = INT_MAX;
|
| 1132 | if (delalloc_blklen)
|
| 1133 | maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
|
| 1134 | maxblocks);
|
| 1135 | blkphy = 0;
|
| 1136 |
|
| 1137 | down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
|
| 1138 | n = nilfs_bmap_lookup_contig(
|
| 1139 | NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
|
| 1140 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
|
| 1141 |
|
| 1142 | if (n < 0) {
|
| 1143 | int past_eof;
|
| 1144 |
|
| 1145 | if (unlikely(n != -ENOENT))
|
| 1146 | break;
|
| 1147 |
|
| 1148 |
|
| 1149 | blkoff++;
|
| 1150 | past_eof = ((blkoff << blkbits) >= isize);
|
| 1151 |
|
| 1152 | if (size) {
|
| 1153 |
|
| 1154 |
|
| 1155 | if (past_eof)
|
| 1156 | flags |= FIEMAP_EXTENT_LAST;
|
| 1157 |
|
| 1158 | ret = fiemap_fill_next_extent(
|
| 1159 | fieinfo, logical, phys, size, flags);
|
| 1160 | if (ret)
|
| 1161 | break;
|
| 1162 | size = 0;
|
| 1163 | }
|
| 1164 | if (blkoff > end_blkoff || past_eof)
|
| 1165 | break;
|
| 1166 | } else {
|
| 1167 | if (size) {
|
| 1168 | if (phys && blkphy << blkbits == phys + size) {
|
| 1169 |
|
| 1170 | size += (u64)n << blkbits;
|
| 1171 | } else {
|
| 1172 |
|
| 1173 | ret = fiemap_fill_next_extent(
|
| 1174 | fieinfo, logical, phys, size,
|
| 1175 | flags);
|
| 1176 | if (ret || blkoff > end_blkoff)
|
| 1177 | break;
|
| 1178 |
|
| 1179 |
|
| 1180 | flags = FIEMAP_EXTENT_MERGED;
|
| 1181 | logical = blkoff << blkbits;
|
| 1182 | phys = blkphy << blkbits;
|
| 1183 | size = (u64)n << blkbits;
|
| 1184 | }
|
| 1185 | } else {
|
| 1186 |
|
| 1187 | flags = FIEMAP_EXTENT_MERGED;
|
| 1188 | logical = blkoff << blkbits;
|
| 1189 | phys = blkphy << blkbits;
|
| 1190 | size = (u64)n << blkbits;
|
| 1191 | }
|
| 1192 | blkoff += n;
|
| 1193 | }
|
| 1194 | cond_resched();
|
| 1195 | } while (true);
|
| 1196 |
|
| 1197 |
|
| 1198 | if (ret == 1)
|
| 1199 | ret = 0;
|
| 1200 |
|
| 1201 | inode_unlock(inode);
|
| 1202 | return ret;
|
| 1203 | }
|