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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
24 #include <linux/buffer_head.h>
25 #include <linux/mpage.h>
26 #include <linux/writeback.h>
36 * nilfs_get_block() - get a file block on the filesystem (callback function)
37 * @inode - inode struct of the target file
38 * @blkoff - file block number
39 * @bh_result - buffer head to be mapped on
40 * @create - indicate whether allocating the block or not when it has not
43 * This function does not issue actual read request of the specified data
44 * block. It is done by VFS.
45 * Bulk read for direct-io is not supported yet. (should be supported)
47 int nilfs_get_block(struct inode *inode, sector_t blkoff,
48 struct buffer_head *bh_result, int create)
50 struct nilfs_inode_info *ii = NILFS_I(inode);
51 unsigned long blknum = 0;
53 struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
55 /* This exclusion control is a workaround; should be revised */
56 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
57 ret = nilfs_bmap_lookup(ii->i_bmap, (unsigned long)blkoff, &blknum);
58 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
59 if (ret == 0) { /* found */
60 map_bh(bh_result, inode->i_sb, blknum);
63 if (unlikely(ret == 1)) {
64 printk(KERN_ERR "nilfs_get_block: bmap_lookup returns "
65 "buffer_head pointer (blkoff=%llu, blknum=%lu)\n",
66 (unsigned long long)blkoff, blknum);
69 /* data block was not found */
70 if (ret == -ENOENT && create) {
71 struct nilfs_transaction_info ti;
73 bh_result->b_blocknr = 0;
74 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
77 err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
78 (unsigned long)bh_result);
79 nilfs_transaction_end(inode->i_sb, !err);
80 if (unlikely(err != 0)) {
83 * The get_block() function could be called
84 * from multiple callers for an inode.
85 * However, the page having this block must
86 * be locked in this case.
89 "nilfs_get_block: a race condition "
90 "while inserting a data block. "
91 "(inode number=%lu, file block "
94 (unsigned long long)blkoff);
96 } else if (err == -EINVAL) {
97 nilfs_error(inode->i_sb, __func__,
98 "broken bmap (inode=%lu)\n",
104 /* Error handling should be detailed */
105 set_buffer_new(bh_result);
106 map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
108 } else if (ret == -ENOENT) {
109 /* not found is not error (e.g. hole); must return without
110 the mapped state flag. */
121 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
122 * address_space_operations.
123 * @file - file struct of the file to be read
124 * @page - the page to be read
126 static int nilfs_readpage(struct file *file, struct page *page)
128 return mpage_readpage(page, nilfs_get_block);
132 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
133 * address_space_operations.
134 * @file - file struct of the file to be read
135 * @mapping - address_space struct used for reading multiple pages
136 * @pages - the pages to be read
137 * @nr_pages - number of pages to be read
139 static int nilfs_readpages(struct file *file, struct address_space *mapping,
140 struct list_head *pages, unsigned nr_pages)
142 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
145 static int nilfs_writepages(struct address_space *mapping,
146 struct writeback_control *wbc)
148 /* This empty method is required not to call generic_writepages() */
152 static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
154 struct inode *inode = page->mapping->host;
157 redirty_page_for_writepage(wbc, page);
160 if (wbc->sync_mode == WB_SYNC_ALL) {
161 err = nilfs_construct_segment(inode->i_sb);
164 } else if (wbc->for_reclaim)
165 nilfs_flush_segment(inode->i_sb, inode->i_ino);
170 static int nilfs_set_page_dirty(struct page *page)
172 int ret = __set_page_dirty_buffers(page);
175 struct inode *inode = page->mapping->host;
176 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
177 unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
179 nilfs_set_file_dirty(sbi, inode, nr_dirty);
184 static int nilfs_write_begin(struct file *file, struct address_space *mapping,
185 loff_t pos, unsigned len, unsigned flags,
186 struct page **pagep, void **fsdata)
189 struct inode *inode = mapping->host;
190 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
196 err = block_write_begin(file, mapping, pos, len, flags, pagep,
197 fsdata, nilfs_get_block);
199 nilfs_transaction_end(inode->i_sb, 0);
203 static int nilfs_write_end(struct file *file, struct address_space *mapping,
204 loff_t pos, unsigned len, unsigned copied,
205 struct page *page, void *fsdata)
207 struct inode *inode = mapping->host;
208 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
212 nr_dirty = nilfs_page_count_clean_buffers(page, start,
214 copied = generic_write_end(file, mapping, pos, len, copied, page,
216 nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
217 err = nilfs_transaction_end(inode->i_sb, 1);
218 return err ? : copied;
222 nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
223 loff_t offset, unsigned long nr_segs)
225 struct file *file = iocb->ki_filp;
226 struct inode *inode = file->f_mapping->host;
230 err = nilfs_construct_dsync_segment(inode->i_sb, inode);
237 /* Needs synchronization with the cleaner */
238 size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
239 offset, nr_segs, nilfs_get_block, NULL);
243 struct address_space_operations nilfs_aops = {
244 .writepage = nilfs_writepage,
245 .readpage = nilfs_readpage,
246 /* .sync_page = nilfs_sync_page, */
247 .writepages = nilfs_writepages,
248 .set_page_dirty = nilfs_set_page_dirty,
249 .readpages = nilfs_readpages,
250 .write_begin = nilfs_write_begin,
251 .write_end = nilfs_write_end,
252 /* .releasepage = nilfs_releasepage, */
253 .invalidatepage = block_invalidatepage,
254 .direct_IO = nilfs_direct_IO,
257 struct inode *nilfs_new_inode(struct inode *dir, int mode)
259 struct super_block *sb = dir->i_sb;
260 struct nilfs_sb_info *sbi = NILFS_SB(sb);
262 struct nilfs_inode_info *ii;
266 inode = new_inode(sb);
267 if (unlikely(!inode))
270 mapping_set_gfp_mask(inode->i_mapping,
271 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
274 ii->i_state = 1 << NILFS_I_NEW;
276 err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
278 goto failed_ifile_create_inode;
279 /* reference count of i_bh inherits from nilfs_mdt_read_block() */
281 atomic_inc(&sbi->s_inodes_count);
283 inode->i_uid = current_fsuid();
284 if (dir->i_mode & S_ISGID) {
285 inode->i_gid = dir->i_gid;
289 inode->i_gid = current_fsgid();
291 inode->i_mode = mode;
293 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
295 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
296 err = nilfs_bmap_read(ii->i_bmap, NULL);
300 set_bit(NILFS_I_BMAP, &ii->i_state);
301 /* No lock is needed; iget() ensures it. */
304 ii->i_flags = NILFS_I(dir)->i_flags;
306 ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
308 ii->i_flags &= ~NILFS_DIRSYNC_FL;
310 /* ii->i_file_acl = 0; */
311 /* ii->i_dir_acl = 0; */
313 ii->i_dir_start_lookup = 0;
314 #ifdef CONFIG_NILFS_FS_POSIX_ACL
316 ii->i_default_acl = NULL;
319 nilfs_set_inode_flags(inode);
320 spin_lock(&sbi->s_next_gen_lock);
321 inode->i_generation = sbi->s_next_generation++;
322 spin_unlock(&sbi->s_next_gen_lock);
323 insert_inode_hash(inode);
325 err = nilfs_init_acl(inode, dir);
327 goto failed_acl; /* never occur. When supporting
328 nilfs_init_acl(), proper cancellation of
329 above jobs should be considered */
331 mark_inode_dirty(inode);
337 iput(inode); /* raw_inode will be deleted through
338 generic_delete_inode() */
341 failed_ifile_create_inode:
342 make_bad_inode(inode);
343 iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
349 void nilfs_free_inode(struct inode *inode)
351 struct super_block *sb = inode->i_sb;
352 struct nilfs_sb_info *sbi = NILFS_SB(sb);
355 /* XXX: check error code? Is there any thing I can do? */
356 (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
357 atomic_dec(&sbi->s_inodes_count);
360 void nilfs_set_inode_flags(struct inode *inode)
362 unsigned int flags = NILFS_I(inode)->i_flags;
364 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
366 if (flags & NILFS_SYNC_FL)
367 inode->i_flags |= S_SYNC;
368 if (flags & NILFS_APPEND_FL)
369 inode->i_flags |= S_APPEND;
370 if (flags & NILFS_IMMUTABLE_FL)
371 inode->i_flags |= S_IMMUTABLE;
372 #ifndef NILFS_ATIME_DISABLE
373 if (flags & NILFS_NOATIME_FL)
375 inode->i_flags |= S_NOATIME;
376 if (flags & NILFS_DIRSYNC_FL)
377 inode->i_flags |= S_DIRSYNC;
378 mapping_set_gfp_mask(inode->i_mapping,
379 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
382 int nilfs_read_inode_common(struct inode *inode,
383 struct nilfs_inode *raw_inode)
385 struct nilfs_inode_info *ii = NILFS_I(inode);
388 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
389 inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
390 inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
391 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
392 inode->i_size = le64_to_cpu(raw_inode->i_size);
393 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
394 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
395 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
396 inode->i_atime.tv_nsec = 0;
397 inode->i_ctime.tv_nsec = 0;
398 inode->i_mtime.tv_nsec = 0;
399 ii->i_dtime = le64_to_cpu(raw_inode->i_dtime);
400 if (inode->i_nlink == 0 && (inode->i_mode == 0 || ii->i_dtime))
401 return -EINVAL; /* this inode is deleted */
403 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
404 ii->i_flags = le32_to_cpu(raw_inode->i_flags);
406 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
407 ii->i_dir_acl = S_ISREG(inode->i_mode) ?
408 0 : le32_to_cpu(raw_inode->i_dir_acl);
411 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
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);
418 set_bit(NILFS_I_BMAP, &ii->i_state);
419 /* No lock is needed; iget() ensures it. */
424 static int nilfs_read_sketch_inode(struct inode *inode)
426 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
429 if (sbi->s_snapshot_cno) {
430 struct the_nilfs *nilfs = sbi->s_nilfs;
431 struct buffer_head *bh_cp;
432 struct nilfs_checkpoint *raw_cp;
434 err = nilfs_cpfile_get_checkpoint(
435 nilfs->ns_cpfile, sbi->s_snapshot_cno, 0, &raw_cp,
438 if (!nilfs_checkpoint_sketch(raw_cp))
440 nilfs_cpfile_put_checkpoint(
441 nilfs->ns_cpfile, sbi->s_snapshot_cno, bh_cp);
443 inode->i_flags |= S_NOCMTIME;
448 static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
451 struct nilfs_sb_info *sbi = NILFS_SB(sb);
452 struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
453 struct buffer_head *bh;
454 struct nilfs_inode *raw_inode;
457 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
458 err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
462 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
464 #ifdef CONFIG_NILFS_FS_POSIX_ACL
465 ii->i_acl = NILFS_ACL_NOT_CACHED;
466 ii->i_default_acl = NILFS_ACL_NOT_CACHED;
468 if (nilfs_read_inode_common(inode, raw_inode))
471 if (S_ISREG(inode->i_mode)) {
472 inode->i_op = &nilfs_file_inode_operations;
473 inode->i_fop = &nilfs_file_operations;
474 inode->i_mapping->a_ops = &nilfs_aops;
475 if (unlikely(inode->i_ino == NILFS_SKETCH_INO)) {
476 err = nilfs_read_sketch_inode(inode);
480 } else if (S_ISDIR(inode->i_mode)) {
481 inode->i_op = &nilfs_dir_inode_operations;
482 inode->i_fop = &nilfs_dir_operations;
483 inode->i_mapping->a_ops = &nilfs_aops;
484 } else if (S_ISLNK(inode->i_mode)) {
485 inode->i_op = &nilfs_symlink_inode_operations;
486 inode->i_mapping->a_ops = &nilfs_aops;
488 inode->i_op = &nilfs_special_inode_operations;
490 inode, inode->i_mode,
491 new_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
493 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
495 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
496 nilfs_set_inode_flags(inode);
500 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
504 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
508 struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
513 inode = iget_locked(sb, ino);
514 if (unlikely(!inode))
515 return ERR_PTR(-ENOMEM);
516 if (!(inode->i_state & I_NEW))
519 err = __nilfs_read_inode(sb, ino, inode);
524 unlock_new_inode(inode);
528 void nilfs_write_inode_common(struct inode *inode,
529 struct nilfs_inode *raw_inode, int has_bmap)
531 struct nilfs_inode_info *ii = NILFS_I(inode);
533 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
534 raw_inode->i_uid = cpu_to_le32(inode->i_uid);
535 raw_inode->i_gid = cpu_to_le32(inode->i_gid);
536 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
537 raw_inode->i_size = cpu_to_le64(inode->i_size);
538 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
539 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
540 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
542 raw_inode->i_dtime = cpu_to_le64(ii->i_dtime);
543 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
544 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
547 nilfs_bmap_write(ii->i_bmap, raw_inode);
548 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
549 raw_inode->i_device_code =
550 cpu_to_le64(new_encode_dev(inode->i_rdev));
551 /* When extending inode, nilfs->ns_inode_size should be checked
552 for substitutions of appended fields */
555 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
557 ino_t ino = inode->i_ino;
558 struct nilfs_inode_info *ii = NILFS_I(inode);
559 struct super_block *sb = inode->i_sb;
560 struct nilfs_sb_info *sbi = NILFS_SB(sb);
561 struct nilfs_inode *raw_inode;
563 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
565 /* The buffer is guarded with lock_buffer() by the caller */
566 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
567 memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
568 set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
570 nilfs_write_inode_common(inode, raw_inode, 0);
571 /* XXX: call with has_bmap = 0 is a workaround to avoid
572 deadlock of bmap. This delays update of i_bmap to just
574 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
577 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
579 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
585 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
588 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
597 b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
598 ret = nilfs_bmap_truncate(ii->i_bmap, b);
599 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
600 if (!ret || (ret == -ENOMEM &&
601 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
606 nilfs_error(ii->vfs_inode.i_sb, __func__,
607 "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
609 nilfs_warning(ii->vfs_inode.i_sb, __func__,
610 "failed to truncate bmap (ino=%lu, err=%d)",
611 ii->vfs_inode.i_ino, ret);
614 void nilfs_truncate(struct inode *inode)
616 unsigned long blkoff;
617 unsigned int blocksize;
618 struct nilfs_transaction_info ti;
619 struct super_block *sb = inode->i_sb;
620 struct nilfs_inode_info *ii = NILFS_I(inode);
623 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
625 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
628 blocksize = sb->s_blocksize;
629 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
630 ret = nilfs_transaction_begin(sb, &ti, 0);
633 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
635 nilfs_truncate_bmap(ii, blkoff);
637 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
639 nilfs_set_transaction_flag(NILFS_TI_SYNC);
641 nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
642 nilfs_transaction_end(sb, 1);
643 /* May construct a logical segment and may fail in sync mode.
644 But truncate has no return value. */
647 void nilfs_delete_inode(struct inode *inode)
649 struct nilfs_transaction_info ti;
650 struct super_block *sb = inode->i_sb;
651 struct nilfs_inode_info *ii = NILFS_I(inode);
654 if (unlikely(is_bad_inode(inode))) {
655 if (inode->i_data.nrpages)
656 truncate_inode_pages(&inode->i_data, 0);
660 err = nilfs_transaction_begin(sb, &ti, 0);
662 if (inode->i_data.nrpages)
663 truncate_inode_pages(&inode->i_data, 0);
665 nilfs_truncate_bmap(ii, 0);
666 nilfs_free_inode(inode);
667 /* nilfs_free_inode() marks inode buffer dirty */
669 nilfs_set_transaction_flag(NILFS_TI_SYNC);
670 nilfs_transaction_end(sb, 1);
671 /* May construct a logical segment and may fail in sync mode.
672 But delete_inode has no return value. */
675 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
677 struct nilfs_transaction_info ti;
678 struct inode *inode = dentry->d_inode;
679 struct super_block *sb = inode->i_sb;
682 err = inode_change_ok(inode, iattr);
686 err = nilfs_transaction_begin(sb, &ti, 0);
689 err = inode_setattr(inode, iattr);
690 if (!err && (iattr->ia_valid & ATTR_MODE))
691 err = nilfs_acl_chmod(inode);
692 err2 = nilfs_transaction_end(sb, 1);
696 int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
697 struct buffer_head **pbh)
699 struct nilfs_inode_info *ii = NILFS_I(inode);
702 spin_lock(&sbi->s_inode_lock);
703 /* Caller of this function MUST lock s_inode_lock */
704 if (ii->i_bh == NULL) {
705 spin_unlock(&sbi->s_inode_lock);
706 err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
710 spin_lock(&sbi->s_inode_lock);
711 if (ii->i_bh == NULL)
721 spin_unlock(&sbi->s_inode_lock);
725 int nilfs_inode_dirty(struct inode *inode)
727 struct nilfs_inode_info *ii = NILFS_I(inode);
728 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
731 if (!list_empty(&ii->i_dirty)) {
732 spin_lock(&sbi->s_inode_lock);
733 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
734 test_bit(NILFS_I_BUSY, &ii->i_state);
735 spin_unlock(&sbi->s_inode_lock);
740 int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
743 struct nilfs_inode_info *ii = NILFS_I(inode);
745 atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
747 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state) ||
748 unlikely(inode->i_ino == NILFS_SKETCH_INO))
751 spin_lock(&sbi->s_inode_lock);
752 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
753 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
754 /* Because this routine may race with nilfs_dispose_list(),
755 we have to check NILFS_I_QUEUED here, too. */
756 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
757 /* This will happen when somebody is freeing
759 nilfs_warning(sbi->s_super, __func__,
760 "cannot get inode (ino=%lu)\n",
762 spin_unlock(&sbi->s_inode_lock);
763 return -EINVAL; /* NILFS_I_DIRTY may remain for
766 list_del(&ii->i_dirty);
767 list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
768 set_bit(NILFS_I_QUEUED, &ii->i_state);
770 spin_unlock(&sbi->s_inode_lock);
774 int nilfs_mark_inode_dirty(struct inode *inode)
776 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
777 struct buffer_head *ibh;
780 err = nilfs_load_inode_block(sbi, inode, &ibh);
782 nilfs_warning(inode->i_sb, __func__,
783 "failed to reget inode block.\n");
787 nilfs_update_inode(inode, ibh);
789 nilfs_mdt_mark_buffer_dirty(ibh);
790 nilfs_mdt_mark_dirty(sbi->s_ifile);
796 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
797 * @inode: inode of the file to be registered.
799 * nilfs_dirty_inode() loads a inode block containing the specified
800 * @inode and copies data from a nilfs_inode to a corresponding inode
801 * entry in the inode block. This operation is excluded from the segment
802 * construction. This function can be called both as a single operation
803 * and as a part of indivisible file operations.
805 void nilfs_dirty_inode(struct inode *inode)
807 struct nilfs_transaction_info ti;
809 if (is_bad_inode(inode)) {
810 nilfs_warning(inode->i_sb, __func__,
811 "tried to mark bad_inode dirty. ignored.\n");
815 nilfs_transaction_begin(inode->i_sb, &ti, 0);
816 if (likely(inode->i_ino != NILFS_SKETCH_INO))
817 nilfs_mark_inode_dirty(inode);
818 nilfs_transaction_end(inode->i_sb, 1); /* never fails */