2 * inode.c - NILFS inode operations.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
16 * Written by Ryusuke Konishi.
20 #include <linux/buffer_head.h>
21 #include <linux/gfp.h>
22 #include <linux/mpage.h>
23 #include <linux/pagemap.h>
24 #include <linux/writeback.h>
25 #include <linux/uio.h>
35 * struct nilfs_iget_args - arguments used during comparison between inodes
37 * @cno: checkpoint number
38 * @root: pointer on NILFS root object (mounted checkpoint)
39 * @for_gc: inode for GC flag
40 * @for_btnc: inode for B-tree node cache flag
41 * @for_shadow: inode for shadowed page cache flag
43 struct nilfs_iget_args {
46 struct nilfs_root *root;
52 static int nilfs_iget_test(struct inode *inode, void *opaque);
54 void nilfs_inode_add_blocks(struct inode *inode, int n)
56 struct nilfs_root *root = NILFS_I(inode)->i_root;
58 inode_add_bytes(inode, i_blocksize(inode) * n);
60 atomic64_add(n, &root->blocks_count);
63 void nilfs_inode_sub_blocks(struct inode *inode, int n)
65 struct nilfs_root *root = NILFS_I(inode)->i_root;
67 inode_sub_bytes(inode, i_blocksize(inode) * n);
69 atomic64_sub(n, &root->blocks_count);
73 * nilfs_get_block() - get a file block on the filesystem (callback function)
74 * @inode - inode struct of the target file
75 * @blkoff - file block number
76 * @bh_result - buffer head to be mapped on
77 * @create - indicate whether allocating the block or not when it has not
80 * This function does not issue actual read request of the specified data
81 * block. It is done by VFS.
83 int nilfs_get_block(struct inode *inode, sector_t blkoff,
84 struct buffer_head *bh_result, int create)
86 struct nilfs_inode_info *ii = NILFS_I(inode);
87 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
90 unsigned int maxblocks = bh_result->b_size >> inode->i_blkbits;
92 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
93 ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
94 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
95 if (ret >= 0) { /* found */
96 map_bh(bh_result, inode->i_sb, blknum);
98 bh_result->b_size = (ret << inode->i_blkbits);
101 /* data block was not found */
102 if (ret == -ENOENT && create) {
103 struct nilfs_transaction_info ti;
105 bh_result->b_blocknr = 0;
106 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
109 err = nilfs_bmap_insert(ii->i_bmap, blkoff,
110 (unsigned long)bh_result);
111 if (unlikely(err != 0)) {
112 if (err == -EEXIST) {
114 * The get_block() function could be called
115 * from multiple callers for an inode.
116 * However, the page having this block must
117 * be locked in this case.
119 nilfs_msg(inode->i_sb, KERN_WARNING,
120 "%s (ino=%lu): a race condition while inserting a data block at offset=%llu",
121 __func__, inode->i_ino,
122 (unsigned long long)blkoff);
125 nilfs_transaction_abort(inode->i_sb);
128 nilfs_mark_inode_dirty_sync(inode);
129 nilfs_transaction_commit(inode->i_sb); /* never fails */
130 /* Error handling should be detailed */
131 set_buffer_new(bh_result);
132 set_buffer_delay(bh_result);
133 map_bh(bh_result, inode->i_sb, 0);
134 /* Disk block number must be changed to proper value */
136 } else if (ret == -ENOENT) {
138 * not found is not error (e.g. hole); must return without
139 * the mapped state flag.
151 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
152 * address_space_operations.
153 * @file - file struct of the file to be read
154 * @page - the page to be read
156 static int nilfs_readpage(struct file *file, struct page *page)
158 return mpage_readpage(page, nilfs_get_block);
162 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
163 * address_space_operations.
164 * @file - file struct of the file to be read
165 * @mapping - address_space struct used for reading multiple pages
166 * @pages - the pages to be read
167 * @nr_pages - number of pages to be read
169 static int nilfs_readpages(struct file *file, struct address_space *mapping,
170 struct list_head *pages, unsigned int nr_pages)
172 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
175 static int nilfs_writepages(struct address_space *mapping,
176 struct writeback_control *wbc)
178 struct inode *inode = mapping->host;
181 if (sb_rdonly(inode->i_sb)) {
182 nilfs_clear_dirty_pages(mapping, false);
186 if (wbc->sync_mode == WB_SYNC_ALL)
187 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
193 static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
195 struct inode *inode = page->mapping->host;
198 if (sb_rdonly(inode->i_sb)) {
200 * It means that filesystem was remounted in read-only
201 * mode because of error or metadata corruption. But we
202 * have dirty pages that try to be flushed in background.
203 * So, here we simply discard this dirty page.
205 nilfs_clear_dirty_page(page, false);
210 redirty_page_for_writepage(wbc, page);
213 if (wbc->sync_mode == WB_SYNC_ALL) {
214 err = nilfs_construct_segment(inode->i_sb);
217 } else if (wbc->for_reclaim)
218 nilfs_flush_segment(inode->i_sb, inode->i_ino);
223 static int nilfs_set_page_dirty(struct page *page)
225 struct inode *inode = page->mapping->host;
226 int ret = __set_page_dirty_nobuffers(page);
228 if (page_has_buffers(page)) {
229 unsigned int nr_dirty = 0;
230 struct buffer_head *bh, *head;
233 * This page is locked by callers, and no other thread
234 * concurrently marks its buffers dirty since they are
235 * only dirtied through routines in fs/buffer.c in
236 * which call sites of mark_buffer_dirty are protected
239 bh = head = page_buffers(page);
241 /* Do not mark hole blocks dirty */
242 if (buffer_dirty(bh) || !buffer_mapped(bh))
245 set_buffer_dirty(bh);
247 } while (bh = bh->b_this_page, bh != head);
250 nilfs_set_file_dirty(inode, nr_dirty);
252 unsigned int nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
254 nilfs_set_file_dirty(inode, nr_dirty);
259 void nilfs_write_failed(struct address_space *mapping, loff_t to)
261 struct inode *inode = mapping->host;
263 if (to > inode->i_size) {
264 truncate_pagecache(inode, inode->i_size);
265 nilfs_truncate(inode);
269 static int nilfs_write_begin(struct file *file, struct address_space *mapping,
270 loff_t pos, unsigned len, unsigned flags,
271 struct page **pagep, void **fsdata)
274 struct inode *inode = mapping->host;
275 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
280 err = block_write_begin(mapping, pos, len, flags, pagep,
283 nilfs_write_failed(mapping, pos + len);
284 nilfs_transaction_abort(inode->i_sb);
289 static int nilfs_write_end(struct file *file, struct address_space *mapping,
290 loff_t pos, unsigned len, unsigned copied,
291 struct page *page, void *fsdata)
293 struct inode *inode = mapping->host;
294 unsigned int start = pos & (PAGE_SIZE - 1);
295 unsigned int nr_dirty;
298 nr_dirty = nilfs_page_count_clean_buffers(page, start,
300 copied = generic_write_end(file, mapping, pos, len, copied, page,
302 nilfs_set_file_dirty(inode, nr_dirty);
303 err = nilfs_transaction_commit(inode->i_sb);
304 return err ? : copied;
308 nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
310 struct inode *inode = file_inode(iocb->ki_filp);
312 if (iov_iter_rw(iter) == WRITE)
315 /* Needs synchronization with the cleaner */
316 return blockdev_direct_IO(iocb, inode, iter, nilfs_get_block);
319 const struct address_space_operations nilfs_aops = {
320 .writepage = nilfs_writepage,
321 .readpage = nilfs_readpage,
322 .writepages = nilfs_writepages,
323 .set_page_dirty = nilfs_set_page_dirty,
324 .readpages = nilfs_readpages,
325 .write_begin = nilfs_write_begin,
326 .write_end = nilfs_write_end,
327 /* .releasepage = nilfs_releasepage, */
328 .invalidatepage = block_invalidatepage,
329 .direct_IO = nilfs_direct_IO,
330 .is_partially_uptodate = block_is_partially_uptodate,
333 static int nilfs_insert_inode_locked(struct inode *inode,
334 struct nilfs_root *root,
337 struct nilfs_iget_args args = {
338 .ino = ino, .root = root, .cno = 0, .for_gc = false,
339 .for_btnc = false, .for_shadow = false
342 return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
345 struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
347 struct super_block *sb = dir->i_sb;
348 struct the_nilfs *nilfs = sb->s_fs_info;
350 struct nilfs_inode_info *ii;
351 struct nilfs_root *root;
352 struct buffer_head *bh;
356 inode = new_inode(sb);
357 if (unlikely(!inode))
360 mapping_set_gfp_mask(inode->i_mapping,
361 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
363 root = NILFS_I(dir)->i_root;
365 ii->i_state = BIT(NILFS_I_NEW);
368 err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
370 goto failed_ifile_create_inode;
371 /* reference count of i_bh inherits from nilfs_mdt_read_block() */
373 if (unlikely(ino < NILFS_USER_INO)) {
374 nilfs_msg(sb, KERN_WARNING,
375 "inode bitmap is inconsistent for reserved inodes");
378 err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
380 goto failed_ifile_create_inode;
381 } while (ino < NILFS_USER_INO);
383 nilfs_msg(sb, KERN_INFO,
384 "repaired inode bitmap for reserved inodes");
388 atomic64_inc(&root->inodes_count);
389 inode_init_owner(inode, dir, mode);
391 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
393 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
394 err = nilfs_bmap_read(ii->i_bmap, NULL);
396 goto failed_after_creation;
398 set_bit(NILFS_I_BMAP, &ii->i_state);
399 /* No lock is needed; iget() ensures it. */
402 ii->i_flags = nilfs_mask_flags(
403 mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
405 /* ii->i_file_acl = 0; */
406 /* ii->i_dir_acl = 0; */
407 ii->i_dir_start_lookup = 0;
408 nilfs_set_inode_flags(inode);
409 spin_lock(&nilfs->ns_next_gen_lock);
410 inode->i_generation = nilfs->ns_next_generation++;
411 spin_unlock(&nilfs->ns_next_gen_lock);
412 if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
414 goto failed_after_creation;
417 err = nilfs_init_acl(inode, dir);
420 * Never occur. When supporting nilfs_init_acl(),
421 * proper cancellation of above jobs should be considered.
423 goto failed_after_creation;
427 failed_after_creation:
429 unlock_new_inode(inode);
431 * raw_inode will be deleted through
432 * nilfs_evict_inode().
436 failed_ifile_create_inode:
437 make_bad_inode(inode);
443 void nilfs_set_inode_flags(struct inode *inode)
445 unsigned int flags = NILFS_I(inode)->i_flags;
446 unsigned int new_fl = 0;
448 if (flags & FS_SYNC_FL)
450 if (flags & FS_APPEND_FL)
452 if (flags & FS_IMMUTABLE_FL)
453 new_fl |= S_IMMUTABLE;
454 if (flags & FS_NOATIME_FL)
456 if (flags & FS_DIRSYNC_FL)
458 inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
459 S_NOATIME | S_DIRSYNC);
462 int nilfs_read_inode_common(struct inode *inode,
463 struct nilfs_inode *raw_inode)
465 struct nilfs_inode_info *ii = NILFS_I(inode);
468 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
469 i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
470 i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
471 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
472 inode->i_size = le64_to_cpu(raw_inode->i_size);
473 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
474 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
475 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
476 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
477 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
478 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
479 if (nilfs_is_metadata_file_inode(inode) && !S_ISREG(inode->i_mode))
480 return -EIO; /* this inode is for metadata and corrupted */
481 if (inode->i_nlink == 0)
482 return -ESTALE; /* this inode is deleted */
484 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
485 ii->i_flags = le32_to_cpu(raw_inode->i_flags);
487 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
488 ii->i_dir_acl = S_ISREG(inode->i_mode) ?
489 0 : le32_to_cpu(raw_inode->i_dir_acl);
491 ii->i_dir_start_lookup = 0;
492 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
494 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
495 S_ISLNK(inode->i_mode)) {
496 err = nilfs_bmap_read(ii->i_bmap, raw_inode);
499 set_bit(NILFS_I_BMAP, &ii->i_state);
500 /* No lock is needed; iget() ensures it. */
505 static int __nilfs_read_inode(struct super_block *sb,
506 struct nilfs_root *root, unsigned long ino,
509 struct the_nilfs *nilfs = sb->s_fs_info;
510 struct buffer_head *bh;
511 struct nilfs_inode *raw_inode;
514 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
515 err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
519 raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
521 err = nilfs_read_inode_common(inode, raw_inode);
525 if (S_ISREG(inode->i_mode)) {
526 inode->i_op = &nilfs_file_inode_operations;
527 inode->i_fop = &nilfs_file_operations;
528 inode->i_mapping->a_ops = &nilfs_aops;
529 } else if (S_ISDIR(inode->i_mode)) {
530 inode->i_op = &nilfs_dir_inode_operations;
531 inode->i_fop = &nilfs_dir_operations;
532 inode->i_mapping->a_ops = &nilfs_aops;
533 } else if (S_ISLNK(inode->i_mode)) {
534 inode->i_op = &nilfs_symlink_inode_operations;
535 inode_nohighmem(inode);
536 inode->i_mapping->a_ops = &nilfs_aops;
538 inode->i_op = &nilfs_special_inode_operations;
540 inode, inode->i_mode,
541 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
543 nilfs_ifile_unmap_inode(root->ifile, ino, bh);
545 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
546 nilfs_set_inode_flags(inode);
547 mapping_set_gfp_mask(inode->i_mapping,
548 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
552 nilfs_ifile_unmap_inode(root->ifile, ino, bh);
556 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
560 static int nilfs_iget_test(struct inode *inode, void *opaque)
562 struct nilfs_iget_args *args = opaque;
563 struct nilfs_inode_info *ii;
565 if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
569 if (test_bit(NILFS_I_BTNC, &ii->i_state)) {
572 } else if (args->for_btnc) {
575 if (test_bit(NILFS_I_SHADOW, &ii->i_state)) {
576 if (!args->for_shadow)
578 } else if (args->for_shadow) {
582 if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
583 return !args->for_gc;
585 return args->for_gc && args->cno == ii->i_cno;
588 static int nilfs_iget_set(struct inode *inode, void *opaque)
590 struct nilfs_iget_args *args = opaque;
592 inode->i_ino = args->ino;
593 NILFS_I(inode)->i_cno = args->cno;
594 NILFS_I(inode)->i_root = args->root;
595 if (args->root && args->ino == NILFS_ROOT_INO)
596 nilfs_get_root(args->root);
599 NILFS_I(inode)->i_state = BIT(NILFS_I_GCINODE);
601 NILFS_I(inode)->i_state |= BIT(NILFS_I_BTNC);
602 if (args->for_shadow)
603 NILFS_I(inode)->i_state |= BIT(NILFS_I_SHADOW);
607 struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
610 struct nilfs_iget_args args = {
611 .ino = ino, .root = root, .cno = 0, .for_gc = false,
612 .for_btnc = false, .for_shadow = false
615 return ilookup5(sb, ino, nilfs_iget_test, &args);
618 struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
621 struct nilfs_iget_args args = {
622 .ino = ino, .root = root, .cno = 0, .for_gc = false,
623 .for_btnc = false, .for_shadow = false
626 return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
629 struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
635 inode = nilfs_iget_locked(sb, root, ino);
636 if (unlikely(!inode))
637 return ERR_PTR(-ENOMEM);
638 if (!(inode->i_state & I_NEW))
641 err = __nilfs_read_inode(sb, root, ino, inode);
646 unlock_new_inode(inode);
650 struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
653 struct nilfs_iget_args args = {
654 .ino = ino, .root = NULL, .cno = cno, .for_gc = true,
655 .for_btnc = false, .for_shadow = false
660 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
661 if (unlikely(!inode))
662 return ERR_PTR(-ENOMEM);
663 if (!(inode->i_state & I_NEW))
666 err = nilfs_init_gcinode(inode);
671 unlock_new_inode(inode);
676 * nilfs_attach_btree_node_cache - attach a B-tree node cache to the inode
677 * @inode: inode object
679 * nilfs_attach_btree_node_cache() attaches a B-tree node cache to @inode,
680 * or does nothing if the inode already has it. This function allocates
681 * an additional inode to maintain page cache of B-tree nodes one-on-one.
683 * Return Value: On success, 0 is returned. On errors, one of the following
684 * negative error code is returned.
686 * %-ENOMEM - Insufficient memory available.
688 int nilfs_attach_btree_node_cache(struct inode *inode)
690 struct nilfs_inode_info *ii = NILFS_I(inode);
691 struct inode *btnc_inode;
692 struct nilfs_iget_args args;
694 if (ii->i_assoc_inode)
697 args.ino = inode->i_ino;
698 args.root = ii->i_root;
699 args.cno = ii->i_cno;
700 args.for_gc = test_bit(NILFS_I_GCINODE, &ii->i_state) != 0;
701 args.for_btnc = true;
702 args.for_shadow = test_bit(NILFS_I_SHADOW, &ii->i_state) != 0;
704 btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
705 nilfs_iget_set, &args);
706 if (unlikely(!btnc_inode))
708 if (btnc_inode->i_state & I_NEW) {
709 nilfs_init_btnc_inode(btnc_inode);
710 unlock_new_inode(btnc_inode);
712 NILFS_I(btnc_inode)->i_assoc_inode = inode;
713 NILFS_I(btnc_inode)->i_bmap = ii->i_bmap;
714 ii->i_assoc_inode = btnc_inode;
720 * nilfs_detach_btree_node_cache - detach the B-tree node cache from the inode
721 * @inode: inode object
723 * nilfs_detach_btree_node_cache() detaches the B-tree node cache and its
724 * holder inode bound to @inode, or does nothing if @inode doesn't have it.
726 void nilfs_detach_btree_node_cache(struct inode *inode)
728 struct nilfs_inode_info *ii = NILFS_I(inode);
729 struct inode *btnc_inode = ii->i_assoc_inode;
732 NILFS_I(btnc_inode)->i_assoc_inode = NULL;
733 ii->i_assoc_inode = NULL;
739 * nilfs_iget_for_shadow - obtain inode for shadow mapping
740 * @inode: inode object that uses shadow mapping
742 * nilfs_iget_for_shadow() allocates a pair of inodes that holds page
743 * caches for shadow mapping. The page cache for data pages is set up
744 * in one inode and the one for b-tree node pages is set up in the
745 * other inode, which is attached to the former inode.
747 * Return Value: On success, a pointer to the inode for data pages is
748 * returned. On errors, one of the following negative error code is returned
751 * %-ENOMEM - Insufficient memory available.
753 struct inode *nilfs_iget_for_shadow(struct inode *inode)
755 struct nilfs_iget_args args = {
756 .ino = inode->i_ino, .root = NULL, .cno = 0, .for_gc = false,
757 .for_btnc = false, .for_shadow = true
759 struct inode *s_inode;
762 s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
763 nilfs_iget_set, &args);
764 if (unlikely(!s_inode))
765 return ERR_PTR(-ENOMEM);
766 if (!(s_inode->i_state & I_NEW))
769 NILFS_I(s_inode)->i_flags = 0;
770 memset(NILFS_I(s_inode)->i_bmap, 0, sizeof(struct nilfs_bmap));
771 mapping_set_gfp_mask(s_inode->i_mapping, GFP_NOFS);
773 err = nilfs_attach_btree_node_cache(s_inode);
775 iget_failed(s_inode);
778 unlock_new_inode(s_inode);
782 void nilfs_write_inode_common(struct inode *inode,
783 struct nilfs_inode *raw_inode, int has_bmap)
785 struct nilfs_inode_info *ii = NILFS_I(inode);
787 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
788 raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
789 raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
790 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
791 raw_inode->i_size = cpu_to_le64(inode->i_size);
792 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
793 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
794 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
795 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
796 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
798 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
799 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
801 if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
802 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
804 /* zero-fill unused portion in the case of super root block */
805 raw_inode->i_xattr = 0;
806 raw_inode->i_pad = 0;
807 memset((void *)raw_inode + sizeof(*raw_inode), 0,
808 nilfs->ns_inode_size - sizeof(*raw_inode));
812 nilfs_bmap_write(ii->i_bmap, raw_inode);
813 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
814 raw_inode->i_device_code =
815 cpu_to_le64(huge_encode_dev(inode->i_rdev));
817 * When extending inode, nilfs->ns_inode_size should be checked
818 * for substitutions of appended fields.
822 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
824 ino_t ino = inode->i_ino;
825 struct nilfs_inode_info *ii = NILFS_I(inode);
826 struct inode *ifile = ii->i_root->ifile;
827 struct nilfs_inode *raw_inode;
829 raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
831 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
832 memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
833 if (flags & I_DIRTY_DATASYNC)
834 set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
836 nilfs_write_inode_common(inode, raw_inode, 0);
838 * XXX: call with has_bmap = 0 is a workaround to avoid
839 * deadlock of bmap. This delays update of i_bmap to just
843 nilfs_ifile_unmap_inode(ifile, ino, ibh);
846 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
848 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
854 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
857 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
866 b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
867 ret = nilfs_bmap_truncate(ii->i_bmap, b);
868 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
869 if (!ret || (ret == -ENOMEM &&
870 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
874 nilfs_msg(ii->vfs_inode.i_sb, KERN_WARNING,
875 "error %d truncating bmap (ino=%lu)", ret,
876 ii->vfs_inode.i_ino);
879 void nilfs_truncate(struct inode *inode)
881 unsigned long blkoff;
882 unsigned int blocksize;
883 struct nilfs_transaction_info ti;
884 struct super_block *sb = inode->i_sb;
885 struct nilfs_inode_info *ii = NILFS_I(inode);
887 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
889 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
892 blocksize = sb->s_blocksize;
893 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
894 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
896 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
898 nilfs_truncate_bmap(ii, blkoff);
900 inode->i_mtime = inode->i_ctime = current_time(inode);
902 nilfs_set_transaction_flag(NILFS_TI_SYNC);
904 nilfs_mark_inode_dirty(inode);
905 nilfs_set_file_dirty(inode, 0);
906 nilfs_transaction_commit(sb);
908 * May construct a logical segment and may fail in sync mode.
909 * But truncate has no return value.
913 static void nilfs_clear_inode(struct inode *inode)
915 struct nilfs_inode_info *ii = NILFS_I(inode);
918 * Free resources allocated in nilfs_read_inode(), here.
920 BUG_ON(!list_empty(&ii->i_dirty));
924 if (nilfs_is_metadata_file_inode(inode))
925 nilfs_mdt_clear(inode);
927 if (test_bit(NILFS_I_BMAP, &ii->i_state))
928 nilfs_bmap_clear(ii->i_bmap);
930 if (!test_bit(NILFS_I_BTNC, &ii->i_state))
931 nilfs_detach_btree_node_cache(inode);
933 if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
934 nilfs_put_root(ii->i_root);
937 void nilfs_evict_inode(struct inode *inode)
939 struct nilfs_transaction_info ti;
940 struct super_block *sb = inode->i_sb;
941 struct nilfs_inode_info *ii = NILFS_I(inode);
942 struct the_nilfs *nilfs;
945 if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
946 truncate_inode_pages_final(&inode->i_data);
948 nilfs_clear_inode(inode);
951 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
953 truncate_inode_pages_final(&inode->i_data);
955 nilfs = sb->s_fs_info;
956 if (unlikely(sb_rdonly(sb) || !nilfs->ns_writer)) {
958 * If this inode is about to be disposed after the file system
959 * has been degraded to read-only due to file system corruption
960 * or after the writer has been detached, do not make any
961 * changes that cause writes, just clear it.
962 * Do this check after read-locking ns_segctor_sem by
963 * nilfs_transaction_begin() in order to avoid a race with
964 * the writer detach operation.
967 nilfs_clear_inode(inode);
968 nilfs_transaction_abort(sb);
972 /* TODO: some of the following operations may fail. */
973 nilfs_truncate_bmap(ii, 0);
974 nilfs_mark_inode_dirty(inode);
977 ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
979 atomic64_dec(&ii->i_root->inodes_count);
981 nilfs_clear_inode(inode);
984 nilfs_set_transaction_flag(NILFS_TI_SYNC);
985 nilfs_transaction_commit(sb);
987 * May construct a logical segment and may fail in sync mode.
988 * But delete_inode has no return value.
992 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
994 struct nilfs_transaction_info ti;
995 struct inode *inode = d_inode(dentry);
996 struct super_block *sb = inode->i_sb;
999 err = setattr_prepare(dentry, iattr);
1003 err = nilfs_transaction_begin(sb, &ti, 0);
1007 if ((iattr->ia_valid & ATTR_SIZE) &&
1008 iattr->ia_size != i_size_read(inode)) {
1009 inode_dio_wait(inode);
1010 truncate_setsize(inode, iattr->ia_size);
1011 nilfs_truncate(inode);
1014 setattr_copy(inode, iattr);
1015 mark_inode_dirty(inode);
1017 if (iattr->ia_valid & ATTR_MODE) {
1018 err = nilfs_acl_chmod(inode);
1023 return nilfs_transaction_commit(sb);
1026 nilfs_transaction_abort(sb);
1030 int nilfs_permission(struct inode *inode, int mask)
1032 struct nilfs_root *root = NILFS_I(inode)->i_root;
1034 if ((mask & MAY_WRITE) && root &&
1035 root->cno != NILFS_CPTREE_CURRENT_CNO)
1036 return -EROFS; /* snapshot is not writable */
1038 return generic_permission(inode, mask);
1041 int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
1043 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1044 struct nilfs_inode_info *ii = NILFS_I(inode);
1047 spin_lock(&nilfs->ns_inode_lock);
1048 if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) {
1049 spin_unlock(&nilfs->ns_inode_lock);
1050 err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
1054 spin_lock(&nilfs->ns_inode_lock);
1055 if (ii->i_bh == NULL)
1057 else if (unlikely(!buffer_uptodate(ii->i_bh))) {
1068 spin_unlock(&nilfs->ns_inode_lock);
1072 int nilfs_inode_dirty(struct inode *inode)
1074 struct nilfs_inode_info *ii = NILFS_I(inode);
1075 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1078 if (!list_empty(&ii->i_dirty)) {
1079 spin_lock(&nilfs->ns_inode_lock);
1080 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
1081 test_bit(NILFS_I_BUSY, &ii->i_state);
1082 spin_unlock(&nilfs->ns_inode_lock);
1087 int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty)
1089 struct nilfs_inode_info *ii = NILFS_I(inode);
1090 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1092 atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
1094 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
1097 spin_lock(&nilfs->ns_inode_lock);
1098 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
1099 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
1101 * Because this routine may race with nilfs_dispose_list(),
1102 * we have to check NILFS_I_QUEUED here, too.
1104 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
1106 * This will happen when somebody is freeing
1109 nilfs_msg(inode->i_sb, KERN_WARNING,
1110 "cannot set file dirty (ino=%lu): the file is being freed",
1112 spin_unlock(&nilfs->ns_inode_lock);
1114 * NILFS_I_DIRTY may remain for
1118 list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
1119 set_bit(NILFS_I_QUEUED, &ii->i_state);
1121 spin_unlock(&nilfs->ns_inode_lock);
1125 int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
1127 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1128 struct buffer_head *ibh;
1132 * Do not dirty inodes after the log writer has been detached
1133 * and its nilfs_root struct has been freed.
1135 if (unlikely(nilfs_purging(nilfs)))
1138 err = nilfs_load_inode_block(inode, &ibh);
1139 if (unlikely(err)) {
1140 nilfs_msg(inode->i_sb, KERN_WARNING,
1141 "cannot mark inode dirty (ino=%lu): error %d loading inode block",
1145 nilfs_update_inode(inode, ibh, flags);
1146 mark_buffer_dirty(ibh);
1147 nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
1153 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
1154 * @inode: inode of the file to be registered.
1156 * nilfs_dirty_inode() loads a inode block containing the specified
1157 * @inode and copies data from a nilfs_inode to a corresponding inode
1158 * entry in the inode block. This operation is excluded from the segment
1159 * construction. This function can be called both as a single operation
1160 * and as a part of indivisible file operations.
1162 void nilfs_dirty_inode(struct inode *inode, int flags)
1164 struct nilfs_transaction_info ti;
1165 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
1167 if (is_bad_inode(inode)) {
1168 nilfs_msg(inode->i_sb, KERN_WARNING,
1169 "tried to mark bad_inode dirty. ignored.");
1174 nilfs_mdt_mark_dirty(inode);
1177 nilfs_transaction_begin(inode->i_sb, &ti, 0);
1178 __nilfs_mark_inode_dirty(inode, flags);
1179 nilfs_transaction_commit(inode->i_sb); /* never fails */
1182 int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1183 __u64 start, __u64 len)
1185 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1186 __u64 logical = 0, phys = 0, size = 0;
1189 sector_t blkoff, end_blkoff;
1190 sector_t delalloc_blkoff;
1191 unsigned long delalloc_blklen;
1192 unsigned int blkbits = inode->i_blkbits;
1195 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1201 isize = i_size_read(inode);
1203 blkoff = start >> blkbits;
1204 end_blkoff = (start + len - 1) >> blkbits;
1206 delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
1211 unsigned int maxblocks;
1213 if (delalloc_blklen && blkoff == delalloc_blkoff) {
1215 /* End of the current extent */
1216 ret = fiemap_fill_next_extent(
1217 fieinfo, logical, phys, size, flags);
1221 if (blkoff > end_blkoff)
1224 flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
1225 logical = blkoff << blkbits;
1227 size = delalloc_blklen << blkbits;
1229 blkoff = delalloc_blkoff + delalloc_blklen;
1230 delalloc_blklen = nilfs_find_uncommitted_extent(
1231 inode, blkoff, &delalloc_blkoff);
1236 * Limit the number of blocks that we look up so as
1237 * not to get into the next delayed allocation extent.
1239 maxblocks = INT_MAX;
1240 if (delalloc_blklen)
1241 maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
1245 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1246 n = nilfs_bmap_lookup_contig(
1247 NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
1248 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1253 if (unlikely(n != -ENOENT))
1258 past_eof = ((blkoff << blkbits) >= isize);
1261 /* End of the current extent */
1264 flags |= FIEMAP_EXTENT_LAST;
1266 ret = fiemap_fill_next_extent(
1267 fieinfo, logical, phys, size, flags);
1272 if (blkoff > end_blkoff || past_eof)
1276 if (phys && blkphy << blkbits == phys + size) {
1277 /* The current extent goes on */
1278 size += n << blkbits;
1280 /* Terminate the current extent */
1281 ret = fiemap_fill_next_extent(
1282 fieinfo, logical, phys, size,
1284 if (ret || blkoff > end_blkoff)
1287 /* Start another extent */
1288 flags = FIEMAP_EXTENT_MERGED;
1289 logical = blkoff << blkbits;
1290 phys = blkphy << blkbits;
1291 size = n << blkbits;
1294 /* Start a new extent */
1295 flags = FIEMAP_EXTENT_MERGED;
1296 logical = blkoff << blkbits;
1297 phys = blkphy << blkbits;
1298 size = n << blkbits;
1305 /* If ret is 1 then we just hit the end of the extent array */
1309 inode_unlock(inode);