1 // SPDX-License-Identifier: GPL-2.0+
3 * inode.c - NILFS inode operations.
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Ryusuke Konishi.
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>
26 * struct nilfs_iget_args - arguments used during comparison between inodes
28 * @cno: checkpoint number
29 * @root: pointer on NILFS root object (mounted checkpoint)
30 * @for_gc: inode for GC flag
31 * @for_btnc: inode for B-tree node cache flag
32 * @for_shadow: inode for shadowed page cache flag
34 struct nilfs_iget_args {
37 struct nilfs_root *root;
43 static int nilfs_iget_test(struct inode *inode, void *opaque);
45 void nilfs_inode_add_blocks(struct inode *inode, int n)
47 struct nilfs_root *root = NILFS_I(inode)->i_root;
49 inode_add_bytes(inode, i_blocksize(inode) * n);
51 atomic64_add(n, &root->blocks_count);
54 void nilfs_inode_sub_blocks(struct inode *inode, int n)
56 struct nilfs_root *root = NILFS_I(inode)->i_root;
58 inode_sub_bytes(inode, i_blocksize(inode) * n);
60 atomic64_sub(n, &root->blocks_count);
64 * nilfs_get_block() - get a file block on the filesystem (callback function)
65 * @inode - inode struct of the target file
66 * @blkoff - file block number
67 * @bh_result - buffer head to be mapped on
68 * @create - indicate whether allocating the block or not when it has not
71 * This function does not issue actual read request of the specified data
72 * block. It is done by VFS.
74 int nilfs_get_block(struct inode *inode, sector_t blkoff,
75 struct buffer_head *bh_result, int create)
77 struct nilfs_inode_info *ii = NILFS_I(inode);
78 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
81 unsigned int maxblocks = bh_result->b_size >> inode->i_blkbits;
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) { /* found */
87 map_bh(bh_result, inode->i_sb, blknum);
89 bh_result->b_size = (ret << inode->i_blkbits);
92 /* data block was not found */
93 if (ret == -ENOENT && create) {
94 struct nilfs_transaction_info ti;
96 bh_result->b_blocknr = 0;
97 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
100 err = nilfs_bmap_insert(ii->i_bmap, blkoff,
101 (unsigned long)bh_result);
102 if (unlikely(err != 0)) {
103 if (err == -EEXIST) {
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.
110 nilfs_warn(inode->i_sb,
111 "%s (ino=%lu): a race condition while inserting a data block at offset=%llu",
112 __func__, inode->i_ino,
113 (unsigned long long)blkoff);
116 nilfs_transaction_abort(inode->i_sb);
119 nilfs_mark_inode_dirty_sync(inode);
120 nilfs_transaction_commit(inode->i_sb); /* never fails */
121 /* Error handling should be detailed */
122 set_buffer_new(bh_result);
123 set_buffer_delay(bh_result);
124 map_bh(bh_result, inode->i_sb, 0);
125 /* Disk block number must be changed to proper value */
127 } else if (ret == -ENOENT) {
129 * not found is not error (e.g. hole); must return without
130 * the mapped state flag.
142 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
143 * address_space_operations.
144 * @file - file struct of the file to be read
145 * @page - the page to be read
147 static int nilfs_readpage(struct file *file, struct page *page)
149 return mpage_readpage(page, nilfs_get_block);
153 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
154 * address_space_operations.
155 * @file - file struct of the file to be read
156 * @mapping - address_space struct used for reading multiple pages
157 * @pages - the pages to be read
158 * @nr_pages - number of pages to be read
160 static int nilfs_readpages(struct file *file, struct address_space *mapping,
161 struct list_head *pages, unsigned int nr_pages)
163 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
166 static int nilfs_writepages(struct address_space *mapping,
167 struct writeback_control *wbc)
169 struct inode *inode = mapping->host;
172 if (sb_rdonly(inode->i_sb)) {
173 nilfs_clear_dirty_pages(mapping, false);
177 if (wbc->sync_mode == WB_SYNC_ALL)
178 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
184 static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
186 struct inode *inode = page->mapping->host;
189 if (sb_rdonly(inode->i_sb)) {
191 * It means that filesystem was remounted in read-only
192 * mode because of error or metadata corruption. But we
193 * have dirty pages that try to be flushed in background.
194 * So, here we simply discard this dirty page.
196 nilfs_clear_dirty_page(page, false);
201 redirty_page_for_writepage(wbc, page);
204 if (wbc->sync_mode == WB_SYNC_ALL) {
205 err = nilfs_construct_segment(inode->i_sb);
208 } else if (wbc->for_reclaim)
209 nilfs_flush_segment(inode->i_sb, inode->i_ino);
214 static int nilfs_set_page_dirty(struct page *page)
216 struct inode *inode = page->mapping->host;
217 int ret = __set_page_dirty_nobuffers(page);
219 if (page_has_buffers(page)) {
220 unsigned int nr_dirty = 0;
221 struct buffer_head *bh, *head;
224 * This page is locked by callers, and no other thread
225 * concurrently marks its buffers dirty since they are
226 * only dirtied through routines in fs/buffer.c in
227 * which call sites of mark_buffer_dirty are protected
230 bh = head = page_buffers(page);
232 /* Do not mark hole blocks dirty */
233 if (buffer_dirty(bh) || !buffer_mapped(bh))
236 set_buffer_dirty(bh);
238 } while (bh = bh->b_this_page, bh != head);
241 nilfs_set_file_dirty(inode, nr_dirty);
243 unsigned int nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
245 nilfs_set_file_dirty(inode, nr_dirty);
250 void nilfs_write_failed(struct address_space *mapping, loff_t to)
252 struct inode *inode = mapping->host;
254 if (to > inode->i_size) {
255 truncate_pagecache(inode, inode->i_size);
256 nilfs_truncate(inode);
260 static int nilfs_write_begin(struct file *file, struct address_space *mapping,
261 loff_t pos, unsigned len, unsigned flags,
262 struct page **pagep, void **fsdata)
265 struct inode *inode = mapping->host;
266 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
271 err = block_write_begin(mapping, pos, len, flags, pagep,
274 nilfs_write_failed(mapping, pos + len);
275 nilfs_transaction_abort(inode->i_sb);
280 static int nilfs_write_end(struct file *file, struct address_space *mapping,
281 loff_t pos, unsigned len, unsigned copied,
282 struct page *page, void *fsdata)
284 struct inode *inode = mapping->host;
285 unsigned int start = pos & (PAGE_SIZE - 1);
286 unsigned int nr_dirty;
289 nr_dirty = nilfs_page_count_clean_buffers(page, start,
291 copied = generic_write_end(file, mapping, pos, len, copied, page,
293 nilfs_set_file_dirty(inode, nr_dirty);
294 err = nilfs_transaction_commit(inode->i_sb);
295 return err ? : copied;
299 nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
301 struct inode *inode = file_inode(iocb->ki_filp);
303 if (iov_iter_rw(iter) == WRITE)
306 /* Needs synchronization with the cleaner */
307 return blockdev_direct_IO(iocb, inode, iter, nilfs_get_block);
310 const struct address_space_operations nilfs_aops = {
311 .writepage = nilfs_writepage,
312 .readpage = nilfs_readpage,
313 .writepages = nilfs_writepages,
314 .set_page_dirty = nilfs_set_page_dirty,
315 .readpages = nilfs_readpages,
316 .write_begin = nilfs_write_begin,
317 .write_end = nilfs_write_end,
318 /* .releasepage = nilfs_releasepage, */
319 .invalidatepage = block_invalidatepage,
320 .direct_IO = nilfs_direct_IO,
321 .is_partially_uptodate = block_is_partially_uptodate,
324 static int nilfs_insert_inode_locked(struct inode *inode,
325 struct nilfs_root *root,
328 struct nilfs_iget_args args = {
329 .ino = ino, .root = root, .cno = 0, .for_gc = false,
330 .for_btnc = false, .for_shadow = false
333 return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
336 struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
338 struct super_block *sb = dir->i_sb;
339 struct the_nilfs *nilfs = sb->s_fs_info;
341 struct nilfs_inode_info *ii;
342 struct nilfs_root *root;
343 struct buffer_head *bh;
347 inode = new_inode(sb);
348 if (unlikely(!inode))
351 mapping_set_gfp_mask(inode->i_mapping,
352 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
354 root = NILFS_I(dir)->i_root;
356 ii->i_state = BIT(NILFS_I_NEW);
359 err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
361 goto failed_ifile_create_inode;
362 /* reference count of i_bh inherits from nilfs_mdt_read_block() */
364 if (unlikely(ino < NILFS_USER_INO)) {
365 nilfs_msg(sb, KERN_WARNING,
366 "inode bitmap is inconsistent for reserved inodes");
369 err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
371 goto failed_ifile_create_inode;
372 } while (ino < NILFS_USER_INO);
374 nilfs_msg(sb, KERN_INFO,
375 "repaired inode bitmap for reserved inodes");
379 atomic64_inc(&root->inodes_count);
380 inode_init_owner(inode, dir, mode);
382 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
384 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
385 err = nilfs_bmap_read(ii->i_bmap, NULL);
387 goto failed_after_creation;
389 set_bit(NILFS_I_BMAP, &ii->i_state);
390 /* No lock is needed; iget() ensures it. */
393 ii->i_flags = nilfs_mask_flags(
394 mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
396 /* ii->i_file_acl = 0; */
397 /* ii->i_dir_acl = 0; */
398 ii->i_dir_start_lookup = 0;
399 nilfs_set_inode_flags(inode);
400 spin_lock(&nilfs->ns_next_gen_lock);
401 inode->i_generation = nilfs->ns_next_generation++;
402 spin_unlock(&nilfs->ns_next_gen_lock);
403 if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
405 goto failed_after_creation;
408 err = nilfs_init_acl(inode, dir);
411 * Never occur. When supporting nilfs_init_acl(),
412 * proper cancellation of above jobs should be considered.
414 goto failed_after_creation;
418 failed_after_creation:
420 unlock_new_inode(inode);
422 * raw_inode will be deleted through
423 * nilfs_evict_inode().
427 failed_ifile_create_inode:
428 make_bad_inode(inode);
434 void nilfs_set_inode_flags(struct inode *inode)
436 unsigned int flags = NILFS_I(inode)->i_flags;
437 unsigned int new_fl = 0;
439 if (flags & FS_SYNC_FL)
441 if (flags & FS_APPEND_FL)
443 if (flags & FS_IMMUTABLE_FL)
444 new_fl |= S_IMMUTABLE;
445 if (flags & FS_NOATIME_FL)
447 if (flags & FS_DIRSYNC_FL)
449 inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
450 S_NOATIME | S_DIRSYNC);
453 int nilfs_read_inode_common(struct inode *inode,
454 struct nilfs_inode *raw_inode)
456 struct nilfs_inode_info *ii = NILFS_I(inode);
459 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
460 i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
461 i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
462 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
463 inode->i_size = le64_to_cpu(raw_inode->i_size);
464 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
465 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
466 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
467 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
468 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
469 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
470 if (nilfs_is_metadata_file_inode(inode) && !S_ISREG(inode->i_mode))
471 return -EIO; /* this inode is for metadata and corrupted */
472 if (inode->i_nlink == 0)
473 return -ESTALE; /* this inode is deleted */
475 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
476 ii->i_flags = le32_to_cpu(raw_inode->i_flags);
478 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
479 ii->i_dir_acl = S_ISREG(inode->i_mode) ?
480 0 : le32_to_cpu(raw_inode->i_dir_acl);
482 ii->i_dir_start_lookup = 0;
483 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
485 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
486 S_ISLNK(inode->i_mode)) {
487 err = nilfs_bmap_read(ii->i_bmap, raw_inode);
490 set_bit(NILFS_I_BMAP, &ii->i_state);
491 /* No lock is needed; iget() ensures it. */
496 static int __nilfs_read_inode(struct super_block *sb,
497 struct nilfs_root *root, unsigned long ino,
500 struct the_nilfs *nilfs = sb->s_fs_info;
501 struct buffer_head *bh;
502 struct nilfs_inode *raw_inode;
505 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
506 err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
510 raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
512 err = nilfs_read_inode_common(inode, raw_inode);
516 if (S_ISREG(inode->i_mode)) {
517 inode->i_op = &nilfs_file_inode_operations;
518 inode->i_fop = &nilfs_file_operations;
519 inode->i_mapping->a_ops = &nilfs_aops;
520 } else if (S_ISDIR(inode->i_mode)) {
521 inode->i_op = &nilfs_dir_inode_operations;
522 inode->i_fop = &nilfs_dir_operations;
523 inode->i_mapping->a_ops = &nilfs_aops;
524 } else if (S_ISLNK(inode->i_mode)) {
525 inode->i_op = &nilfs_symlink_inode_operations;
526 inode_nohighmem(inode);
527 inode->i_mapping->a_ops = &nilfs_aops;
529 inode->i_op = &nilfs_special_inode_operations;
531 inode, inode->i_mode,
532 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
534 nilfs_ifile_unmap_inode(root->ifile, ino, bh);
536 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
537 nilfs_set_inode_flags(inode);
538 mapping_set_gfp_mask(inode->i_mapping,
539 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
543 nilfs_ifile_unmap_inode(root->ifile, ino, bh);
547 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
551 static int nilfs_iget_test(struct inode *inode, void *opaque)
553 struct nilfs_iget_args *args = opaque;
554 struct nilfs_inode_info *ii;
556 if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
560 if (test_bit(NILFS_I_BTNC, &ii->i_state)) {
563 } else if (args->for_btnc) {
566 if (test_bit(NILFS_I_SHADOW, &ii->i_state)) {
567 if (!args->for_shadow)
569 } else if (args->for_shadow) {
573 if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
574 return !args->for_gc;
576 return args->for_gc && args->cno == ii->i_cno;
579 static int nilfs_iget_set(struct inode *inode, void *opaque)
581 struct nilfs_iget_args *args = opaque;
583 inode->i_ino = args->ino;
584 NILFS_I(inode)->i_cno = args->cno;
585 NILFS_I(inode)->i_root = args->root;
586 if (args->root && args->ino == NILFS_ROOT_INO)
587 nilfs_get_root(args->root);
590 NILFS_I(inode)->i_state = BIT(NILFS_I_GCINODE);
592 NILFS_I(inode)->i_state |= BIT(NILFS_I_BTNC);
593 if (args->for_shadow)
594 NILFS_I(inode)->i_state |= BIT(NILFS_I_SHADOW);
598 struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
601 struct nilfs_iget_args args = {
602 .ino = ino, .root = root, .cno = 0, .for_gc = false,
603 .for_btnc = false, .for_shadow = false
606 return ilookup5(sb, ino, nilfs_iget_test, &args);
609 struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
612 struct nilfs_iget_args args = {
613 .ino = ino, .root = root, .cno = 0, .for_gc = false,
614 .for_btnc = false, .for_shadow = false
617 return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
620 struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
626 inode = nilfs_iget_locked(sb, root, ino);
627 if (unlikely(!inode))
628 return ERR_PTR(-ENOMEM);
629 if (!(inode->i_state & I_NEW))
632 err = __nilfs_read_inode(sb, root, ino, inode);
637 unlock_new_inode(inode);
641 struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
644 struct nilfs_iget_args args = {
645 .ino = ino, .root = NULL, .cno = cno, .for_gc = true,
646 .for_btnc = false, .for_shadow = false
651 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
652 if (unlikely(!inode))
653 return ERR_PTR(-ENOMEM);
654 if (!(inode->i_state & I_NEW))
657 err = nilfs_init_gcinode(inode);
662 unlock_new_inode(inode);
667 * nilfs_attach_btree_node_cache - attach a B-tree node cache to the inode
668 * @inode: inode object
670 * nilfs_attach_btree_node_cache() attaches a B-tree node cache to @inode,
671 * or does nothing if the inode already has it. This function allocates
672 * an additional inode to maintain page cache of B-tree nodes one-on-one.
674 * Return Value: On success, 0 is returned. On errors, one of the following
675 * negative error code is returned.
677 * %-ENOMEM - Insufficient memory available.
679 int nilfs_attach_btree_node_cache(struct inode *inode)
681 struct nilfs_inode_info *ii = NILFS_I(inode);
682 struct inode *btnc_inode;
683 struct nilfs_iget_args args;
685 if (ii->i_assoc_inode)
688 args.ino = inode->i_ino;
689 args.root = ii->i_root;
690 args.cno = ii->i_cno;
691 args.for_gc = test_bit(NILFS_I_GCINODE, &ii->i_state) != 0;
692 args.for_btnc = true;
693 args.for_shadow = test_bit(NILFS_I_SHADOW, &ii->i_state) != 0;
695 btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
696 nilfs_iget_set, &args);
697 if (unlikely(!btnc_inode))
699 if (btnc_inode->i_state & I_NEW) {
700 nilfs_init_btnc_inode(btnc_inode);
701 unlock_new_inode(btnc_inode);
703 NILFS_I(btnc_inode)->i_assoc_inode = inode;
704 NILFS_I(btnc_inode)->i_bmap = ii->i_bmap;
705 ii->i_assoc_inode = btnc_inode;
711 * nilfs_detach_btree_node_cache - detach the B-tree node cache from the inode
712 * @inode: inode object
714 * nilfs_detach_btree_node_cache() detaches the B-tree node cache and its
715 * holder inode bound to @inode, or does nothing if @inode doesn't have it.
717 void nilfs_detach_btree_node_cache(struct inode *inode)
719 struct nilfs_inode_info *ii = NILFS_I(inode);
720 struct inode *btnc_inode = ii->i_assoc_inode;
723 NILFS_I(btnc_inode)->i_assoc_inode = NULL;
724 ii->i_assoc_inode = NULL;
730 * nilfs_iget_for_shadow - obtain inode for shadow mapping
731 * @inode: inode object that uses shadow mapping
733 * nilfs_iget_for_shadow() allocates a pair of inodes that holds page
734 * caches for shadow mapping. The page cache for data pages is set up
735 * in one inode and the one for b-tree node pages is set up in the
736 * other inode, which is attached to the former inode.
738 * Return Value: On success, a pointer to the inode for data pages is
739 * returned. On errors, one of the following negative error code is returned
742 * %-ENOMEM - Insufficient memory available.
744 struct inode *nilfs_iget_for_shadow(struct inode *inode)
746 struct nilfs_iget_args args = {
747 .ino = inode->i_ino, .root = NULL, .cno = 0, .for_gc = false,
748 .for_btnc = false, .for_shadow = true
750 struct inode *s_inode;
753 s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
754 nilfs_iget_set, &args);
755 if (unlikely(!s_inode))
756 return ERR_PTR(-ENOMEM);
757 if (!(s_inode->i_state & I_NEW))
760 NILFS_I(s_inode)->i_flags = 0;
761 memset(NILFS_I(s_inode)->i_bmap, 0, sizeof(struct nilfs_bmap));
762 mapping_set_gfp_mask(s_inode->i_mapping, GFP_NOFS);
764 err = nilfs_attach_btree_node_cache(s_inode);
766 iget_failed(s_inode);
769 unlock_new_inode(s_inode);
773 void nilfs_write_inode_common(struct inode *inode,
774 struct nilfs_inode *raw_inode, int has_bmap)
776 struct nilfs_inode_info *ii = NILFS_I(inode);
778 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
779 raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
780 raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
781 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
782 raw_inode->i_size = cpu_to_le64(inode->i_size);
783 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
784 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
785 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
786 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
787 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
789 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
790 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
792 if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
793 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
795 /* zero-fill unused portion in the case of super root block */
796 raw_inode->i_xattr = 0;
797 raw_inode->i_pad = 0;
798 memset((void *)raw_inode + sizeof(*raw_inode), 0,
799 nilfs->ns_inode_size - sizeof(*raw_inode));
803 nilfs_bmap_write(ii->i_bmap, raw_inode);
804 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
805 raw_inode->i_device_code =
806 cpu_to_le64(huge_encode_dev(inode->i_rdev));
808 * When extending inode, nilfs->ns_inode_size should be checked
809 * for substitutions of appended fields.
813 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
815 ino_t ino = inode->i_ino;
816 struct nilfs_inode_info *ii = NILFS_I(inode);
817 struct inode *ifile = ii->i_root->ifile;
818 struct nilfs_inode *raw_inode;
820 raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
822 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
823 memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
824 if (flags & I_DIRTY_DATASYNC)
825 set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
827 nilfs_write_inode_common(inode, raw_inode, 0);
829 * XXX: call with has_bmap = 0 is a workaround to avoid
830 * deadlock of bmap. This delays update of i_bmap to just
834 nilfs_ifile_unmap_inode(ifile, ino, ibh);
837 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
839 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
845 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
848 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
857 b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
858 ret = nilfs_bmap_truncate(ii->i_bmap, b);
859 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
860 if (!ret || (ret == -ENOMEM &&
861 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
865 nilfs_warn(ii->vfs_inode.i_sb, "error %d truncating bmap (ino=%lu)",
866 ret, ii->vfs_inode.i_ino);
869 void nilfs_truncate(struct inode *inode)
871 unsigned long blkoff;
872 unsigned int blocksize;
873 struct nilfs_transaction_info ti;
874 struct super_block *sb = inode->i_sb;
875 struct nilfs_inode_info *ii = NILFS_I(inode);
877 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
879 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
882 blocksize = sb->s_blocksize;
883 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
884 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
886 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
888 nilfs_truncate_bmap(ii, blkoff);
890 inode->i_mtime = inode->i_ctime = current_time(inode);
892 nilfs_set_transaction_flag(NILFS_TI_SYNC);
894 nilfs_mark_inode_dirty(inode);
895 nilfs_set_file_dirty(inode, 0);
896 nilfs_transaction_commit(sb);
898 * May construct a logical segment and may fail in sync mode.
899 * But truncate has no return value.
903 static void nilfs_clear_inode(struct inode *inode)
905 struct nilfs_inode_info *ii = NILFS_I(inode);
908 * Free resources allocated in nilfs_read_inode(), here.
910 BUG_ON(!list_empty(&ii->i_dirty));
914 if (nilfs_is_metadata_file_inode(inode))
915 nilfs_mdt_clear(inode);
917 if (test_bit(NILFS_I_BMAP, &ii->i_state))
918 nilfs_bmap_clear(ii->i_bmap);
920 if (!test_bit(NILFS_I_BTNC, &ii->i_state))
921 nilfs_detach_btree_node_cache(inode);
923 if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
924 nilfs_put_root(ii->i_root);
927 void nilfs_evict_inode(struct inode *inode)
929 struct nilfs_transaction_info ti;
930 struct super_block *sb = inode->i_sb;
931 struct nilfs_inode_info *ii = NILFS_I(inode);
932 struct the_nilfs *nilfs;
935 if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
936 truncate_inode_pages_final(&inode->i_data);
938 nilfs_clear_inode(inode);
941 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
943 truncate_inode_pages_final(&inode->i_data);
945 nilfs = sb->s_fs_info;
946 if (unlikely(sb_rdonly(sb) || !nilfs->ns_writer)) {
948 * If this inode is about to be disposed after the file system
949 * has been degraded to read-only due to file system corruption
950 * or after the writer has been detached, do not make any
951 * changes that cause writes, just clear it.
952 * Do this check after read-locking ns_segctor_sem by
953 * nilfs_transaction_begin() in order to avoid a race with
954 * the writer detach operation.
957 nilfs_clear_inode(inode);
958 nilfs_transaction_abort(sb);
962 /* TODO: some of the following operations may fail. */
963 nilfs_truncate_bmap(ii, 0);
964 nilfs_mark_inode_dirty(inode);
967 ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
969 atomic64_dec(&ii->i_root->inodes_count);
971 nilfs_clear_inode(inode);
974 nilfs_set_transaction_flag(NILFS_TI_SYNC);
975 nilfs_transaction_commit(sb);
977 * May construct a logical segment and may fail in sync mode.
978 * But delete_inode has no return value.
982 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
984 struct nilfs_transaction_info ti;
985 struct inode *inode = d_inode(dentry);
986 struct super_block *sb = inode->i_sb;
989 err = setattr_prepare(dentry, iattr);
993 err = nilfs_transaction_begin(sb, &ti, 0);
997 if ((iattr->ia_valid & ATTR_SIZE) &&
998 iattr->ia_size != i_size_read(inode)) {
999 inode_dio_wait(inode);
1000 truncate_setsize(inode, iattr->ia_size);
1001 nilfs_truncate(inode);
1004 setattr_copy(inode, iattr);
1005 mark_inode_dirty(inode);
1007 if (iattr->ia_valid & ATTR_MODE) {
1008 err = nilfs_acl_chmod(inode);
1013 return nilfs_transaction_commit(sb);
1016 nilfs_transaction_abort(sb);
1020 int nilfs_permission(struct inode *inode, int mask)
1022 struct nilfs_root *root = NILFS_I(inode)->i_root;
1024 if ((mask & MAY_WRITE) && root &&
1025 root->cno != NILFS_CPTREE_CURRENT_CNO)
1026 return -EROFS; /* snapshot is not writable */
1028 return generic_permission(inode, mask);
1031 int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
1033 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1034 struct nilfs_inode_info *ii = NILFS_I(inode);
1037 spin_lock(&nilfs->ns_inode_lock);
1038 if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) {
1039 spin_unlock(&nilfs->ns_inode_lock);
1040 err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
1044 spin_lock(&nilfs->ns_inode_lock);
1045 if (ii->i_bh == NULL)
1047 else if (unlikely(!buffer_uptodate(ii->i_bh))) {
1058 spin_unlock(&nilfs->ns_inode_lock);
1062 int nilfs_inode_dirty(struct inode *inode)
1064 struct nilfs_inode_info *ii = NILFS_I(inode);
1065 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1068 if (!list_empty(&ii->i_dirty)) {
1069 spin_lock(&nilfs->ns_inode_lock);
1070 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
1071 test_bit(NILFS_I_BUSY, &ii->i_state);
1072 spin_unlock(&nilfs->ns_inode_lock);
1077 int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty)
1079 struct nilfs_inode_info *ii = NILFS_I(inode);
1080 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1082 atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
1084 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
1087 spin_lock(&nilfs->ns_inode_lock);
1088 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
1089 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
1091 * Because this routine may race with nilfs_dispose_list(),
1092 * we have to check NILFS_I_QUEUED here, too.
1094 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
1096 * This will happen when somebody is freeing
1099 nilfs_warn(inode->i_sb,
1100 "cannot set file dirty (ino=%lu): the file is being freed",
1102 spin_unlock(&nilfs->ns_inode_lock);
1104 * NILFS_I_DIRTY may remain for
1108 list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
1109 set_bit(NILFS_I_QUEUED, &ii->i_state);
1111 spin_unlock(&nilfs->ns_inode_lock);
1115 int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
1117 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1118 struct buffer_head *ibh;
1122 * Do not dirty inodes after the log writer has been detached
1123 * and its nilfs_root struct has been freed.
1125 if (unlikely(nilfs_purging(nilfs)))
1128 err = nilfs_load_inode_block(inode, &ibh);
1129 if (unlikely(err)) {
1130 nilfs_warn(inode->i_sb,
1131 "cannot mark inode dirty (ino=%lu): error %d loading inode block",
1135 nilfs_update_inode(inode, ibh, flags);
1136 mark_buffer_dirty(ibh);
1137 nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
1143 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
1144 * @inode: inode of the file to be registered.
1146 * nilfs_dirty_inode() loads a inode block containing the specified
1147 * @inode and copies data from a nilfs_inode to a corresponding inode
1148 * entry in the inode block. This operation is excluded from the segment
1149 * construction. This function can be called both as a single operation
1150 * and as a part of indivisible file operations.
1152 void nilfs_dirty_inode(struct inode *inode, int flags)
1154 struct nilfs_transaction_info ti;
1155 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
1157 if (is_bad_inode(inode)) {
1158 nilfs_warn(inode->i_sb,
1159 "tried to mark bad_inode dirty. ignored.");
1164 nilfs_mdt_mark_dirty(inode);
1167 nilfs_transaction_begin(inode->i_sb, &ti, 0);
1168 __nilfs_mark_inode_dirty(inode, flags);
1169 nilfs_transaction_commit(inode->i_sb); /* never fails */
1172 int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1173 __u64 start, __u64 len)
1175 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1176 __u64 logical = 0, phys = 0, size = 0;
1179 sector_t blkoff, end_blkoff;
1180 sector_t delalloc_blkoff;
1181 unsigned long delalloc_blklen;
1182 unsigned int blkbits = inode->i_blkbits;
1185 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1191 isize = i_size_read(inode);
1193 blkoff = start >> blkbits;
1194 end_blkoff = (start + len - 1) >> blkbits;
1196 delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
1201 unsigned int maxblocks;
1203 if (delalloc_blklen && blkoff == delalloc_blkoff) {
1205 /* End of the current extent */
1206 ret = fiemap_fill_next_extent(
1207 fieinfo, logical, phys, size, flags);
1211 if (blkoff > end_blkoff)
1214 flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
1215 logical = blkoff << blkbits;
1217 size = delalloc_blklen << blkbits;
1219 blkoff = delalloc_blkoff + delalloc_blklen;
1220 delalloc_blklen = nilfs_find_uncommitted_extent(
1221 inode, blkoff, &delalloc_blkoff);
1226 * Limit the number of blocks that we look up so as
1227 * not to get into the next delayed allocation extent.
1229 maxblocks = INT_MAX;
1230 if (delalloc_blklen)
1231 maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
1235 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1236 n = nilfs_bmap_lookup_contig(
1237 NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
1238 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1243 if (unlikely(n != -ENOENT))
1248 past_eof = ((blkoff << blkbits) >= isize);
1251 /* End of the current extent */
1254 flags |= FIEMAP_EXTENT_LAST;
1256 ret = fiemap_fill_next_extent(
1257 fieinfo, logical, phys, size, flags);
1262 if (blkoff > end_blkoff || past_eof)
1266 if (phys && blkphy << blkbits == phys + size) {
1267 /* The current extent goes on */
1268 size += n << blkbits;
1270 /* Terminate the current extent */
1271 ret = fiemap_fill_next_extent(
1272 fieinfo, logical, phys, size,
1274 if (ret || blkoff > end_blkoff)
1277 /* Start another extent */
1278 flags = FIEMAP_EXTENT_MERGED;
1279 logical = blkoff << blkbits;
1280 phys = blkphy << blkbits;
1281 size = n << blkbits;
1284 /* Start a new extent */
1285 flags = FIEMAP_EXTENT_MERGED;
1286 logical = blkoff << blkbits;
1287 phys = blkphy << blkbits;
1288 size = n << blkbits;
1295 /* If ret is 1 then we just hit the end of the extent array */
1299 inode_unlock(inode);