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/gfp.h>
26 #include <linux/mpage.h>
27 #include <linux/writeback.h>
28 #include <linux/uio.h>
37 struct nilfs_iget_args {
44 * nilfs_get_block() - get a file block on the filesystem (callback function)
45 * @inode - inode struct of the target file
46 * @blkoff - file block number
47 * @bh_result - buffer head to be mapped on
48 * @create - indicate whether allocating the block or not when it has not
51 * This function does not issue actual read request of the specified data
52 * block. It is done by VFS.
54 int nilfs_get_block(struct inode *inode, sector_t blkoff,
55 struct buffer_head *bh_result, int create)
57 struct nilfs_inode_info *ii = NILFS_I(inode);
60 struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
61 unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
63 down_read(&NILFS_MDT(dat)->mi_sem);
64 ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
65 up_read(&NILFS_MDT(dat)->mi_sem);
66 if (ret >= 0) { /* found */
67 map_bh(bh_result, inode->i_sb, blknum);
69 bh_result->b_size = (ret << inode->i_blkbits);
72 /* data block was not found */
73 if (ret == -ENOENT && create) {
74 struct nilfs_transaction_info ti;
76 bh_result->b_blocknr = 0;
77 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
80 err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
81 (unsigned long)bh_result);
82 if (unlikely(err != 0)) {
85 * The get_block() function could be called
86 * from multiple callers for an inode.
87 * However, the page having this block must
88 * be locked in this case.
91 "nilfs_get_block: a race condition "
92 "while inserting a data block. "
93 "(inode number=%lu, file block "
96 (unsigned long long)blkoff);
98 } else if (err == -EINVAL) {
99 nilfs_error(inode->i_sb, __func__,
100 "broken bmap (inode=%lu)\n",
104 nilfs_transaction_abort(inode->i_sb);
107 nilfs_mark_inode_dirty(inode);
108 nilfs_transaction_commit(inode->i_sb); /* never fails */
109 /* Error handling should be detailed */
110 set_buffer_new(bh_result);
111 map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
113 } else if (ret == -ENOENT) {
114 /* not found is not error (e.g. hole); must return without
115 the mapped state flag. */
126 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
127 * address_space_operations.
128 * @file - file struct of the file to be read
129 * @page - the page to be read
131 static int nilfs_readpage(struct file *file, struct page *page)
133 return mpage_readpage(page, nilfs_get_block);
137 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
138 * address_space_operations.
139 * @file - file struct of the file to be read
140 * @mapping - address_space struct used for reading multiple pages
141 * @pages - the pages to be read
142 * @nr_pages - number of pages to be read
144 static int nilfs_readpages(struct file *file, struct address_space *mapping,
145 struct list_head *pages, unsigned nr_pages)
147 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
150 static int nilfs_writepages(struct address_space *mapping,
151 struct writeback_control *wbc)
153 struct inode *inode = mapping->host;
156 if (wbc->sync_mode == WB_SYNC_ALL)
157 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
163 static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
165 struct inode *inode = page->mapping->host;
168 redirty_page_for_writepage(wbc, page);
171 if (wbc->sync_mode == WB_SYNC_ALL) {
172 err = nilfs_construct_segment(inode->i_sb);
175 } else if (wbc->for_reclaim)
176 nilfs_flush_segment(inode->i_sb, inode->i_ino);
181 static int nilfs_set_page_dirty(struct page *page)
183 int ret = __set_page_dirty_buffers(page);
186 struct inode *inode = page->mapping->host;
187 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
188 unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
190 nilfs_set_file_dirty(sbi, inode, nr_dirty);
195 static int nilfs_write_begin(struct file *file, struct address_space *mapping,
196 loff_t pos, unsigned len, unsigned flags,
197 struct page **pagep, void **fsdata)
200 struct inode *inode = mapping->host;
201 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
206 err = block_write_begin(mapping, pos, len, flags, pagep,
209 loff_t isize = mapping->host->i_size;
210 if (pos + len > isize)
211 vmtruncate(mapping->host, isize);
213 nilfs_transaction_abort(inode->i_sb);
218 static int nilfs_write_end(struct file *file, struct address_space *mapping,
219 loff_t pos, unsigned len, unsigned copied,
220 struct page *page, void *fsdata)
222 struct inode *inode = mapping->host;
223 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
227 nr_dirty = nilfs_page_count_clean_buffers(page, start,
229 copied = generic_write_end(file, mapping, pos, len, copied, page,
231 nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
232 err = nilfs_transaction_commit(inode->i_sb);
233 return err ? : copied;
237 nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
238 loff_t offset, unsigned long nr_segs)
240 struct file *file = iocb->ki_filp;
241 struct inode *inode = file->f_mapping->host;
247 /* Needs synchronization with the cleaner */
248 size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
249 offset, nr_segs, nilfs_get_block, NULL);
252 * In case of error extending write may have instantiated a few
253 * blocks outside i_size. Trim these off again.
255 if (unlikely((rw & WRITE) && size < 0)) {
256 loff_t isize = i_size_read(inode);
257 loff_t end = offset + iov_length(iov, nr_segs);
260 vmtruncate(inode, isize);
266 const struct address_space_operations nilfs_aops = {
267 .writepage = nilfs_writepage,
268 .readpage = nilfs_readpage,
269 .sync_page = block_sync_page,
270 .writepages = nilfs_writepages,
271 .set_page_dirty = nilfs_set_page_dirty,
272 .readpages = nilfs_readpages,
273 .write_begin = nilfs_write_begin,
274 .write_end = nilfs_write_end,
275 /* .releasepage = nilfs_releasepage, */
276 .invalidatepage = block_invalidatepage,
277 .direct_IO = nilfs_direct_IO,
278 .is_partially_uptodate = block_is_partially_uptodate,
281 struct inode *nilfs_new_inode(struct inode *dir, int mode)
283 struct super_block *sb = dir->i_sb;
284 struct nilfs_sb_info *sbi = NILFS_SB(sb);
286 struct nilfs_inode_info *ii;
290 inode = new_inode(sb);
291 if (unlikely(!inode))
294 mapping_set_gfp_mask(inode->i_mapping,
295 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
298 ii->i_state = 1 << NILFS_I_NEW;
300 err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
302 goto failed_ifile_create_inode;
303 /* reference count of i_bh inherits from nilfs_mdt_read_block() */
305 atomic_inc(&sbi->s_inodes_count);
306 inode_init_owner(inode, dir, mode);
308 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
310 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
311 err = nilfs_bmap_read(ii->i_bmap, NULL);
315 set_bit(NILFS_I_BMAP, &ii->i_state);
316 /* No lock is needed; iget() ensures it. */
319 ii->i_flags = NILFS_I(dir)->i_flags;
321 ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
323 ii->i_flags &= ~NILFS_DIRSYNC_FL;
325 /* ii->i_file_acl = 0; */
326 /* ii->i_dir_acl = 0; */
327 ii->i_dir_start_lookup = 0;
328 nilfs_set_inode_flags(inode);
329 spin_lock(&sbi->s_next_gen_lock);
330 inode->i_generation = sbi->s_next_generation++;
331 spin_unlock(&sbi->s_next_gen_lock);
332 insert_inode_hash(inode);
334 err = nilfs_init_acl(inode, dir);
336 goto failed_acl; /* never occur. When supporting
337 nilfs_init_acl(), proper cancellation of
338 above jobs should be considered */
345 iput(inode); /* raw_inode will be deleted through
346 generic_delete_inode() */
349 failed_ifile_create_inode:
350 make_bad_inode(inode);
351 iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
357 void nilfs_free_inode(struct inode *inode)
359 struct super_block *sb = inode->i_sb;
360 struct nilfs_sb_info *sbi = NILFS_SB(sb);
362 /* XXX: check error code? Is there any thing I can do? */
363 (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
364 atomic_dec(&sbi->s_inodes_count);
367 void nilfs_set_inode_flags(struct inode *inode)
369 unsigned int flags = NILFS_I(inode)->i_flags;
371 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
373 if (flags & NILFS_SYNC_FL)
374 inode->i_flags |= S_SYNC;
375 if (flags & NILFS_APPEND_FL)
376 inode->i_flags |= S_APPEND;
377 if (flags & NILFS_IMMUTABLE_FL)
378 inode->i_flags |= S_IMMUTABLE;
379 #ifndef NILFS_ATIME_DISABLE
380 if (flags & NILFS_NOATIME_FL)
382 inode->i_flags |= S_NOATIME;
383 if (flags & NILFS_DIRSYNC_FL)
384 inode->i_flags |= S_DIRSYNC;
385 mapping_set_gfp_mask(inode->i_mapping,
386 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
389 int nilfs_read_inode_common(struct inode *inode,
390 struct nilfs_inode *raw_inode)
392 struct nilfs_inode_info *ii = NILFS_I(inode);
395 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
396 inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
397 inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
398 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
399 inode->i_size = le64_to_cpu(raw_inode->i_size);
400 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
401 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
402 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
403 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
404 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
405 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
406 if (inode->i_nlink == 0 && inode->i_mode == 0)
407 return -EINVAL; /* this inode is deleted */
409 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
410 ii->i_flags = le32_to_cpu(raw_inode->i_flags);
412 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
413 ii->i_dir_acl = S_ISREG(inode->i_mode) ?
414 0 : le32_to_cpu(raw_inode->i_dir_acl);
416 ii->i_dir_start_lookup = 0;
417 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
419 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
420 S_ISLNK(inode->i_mode)) {
421 err = nilfs_bmap_read(ii->i_bmap, raw_inode);
424 set_bit(NILFS_I_BMAP, &ii->i_state);
425 /* No lock is needed; iget() ensures it. */
430 static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
433 struct nilfs_sb_info *sbi = NILFS_SB(sb);
434 struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
435 struct buffer_head *bh;
436 struct nilfs_inode *raw_inode;
439 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
440 err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
444 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
446 err = nilfs_read_inode_common(inode, raw_inode);
450 if (S_ISREG(inode->i_mode)) {
451 inode->i_op = &nilfs_file_inode_operations;
452 inode->i_fop = &nilfs_file_operations;
453 inode->i_mapping->a_ops = &nilfs_aops;
454 } else if (S_ISDIR(inode->i_mode)) {
455 inode->i_op = &nilfs_dir_inode_operations;
456 inode->i_fop = &nilfs_dir_operations;
457 inode->i_mapping->a_ops = &nilfs_aops;
458 } else if (S_ISLNK(inode->i_mode)) {
459 inode->i_op = &nilfs_symlink_inode_operations;
460 inode->i_mapping->a_ops = &nilfs_aops;
462 inode->i_op = &nilfs_special_inode_operations;
464 inode, inode->i_mode,
465 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
467 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
469 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
470 nilfs_set_inode_flags(inode);
474 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
478 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
482 static int nilfs_iget_test(struct inode *inode, void *opaque)
484 struct nilfs_iget_args *args = opaque;
485 struct nilfs_inode_info *ii;
487 if (args->ino != inode->i_ino)
491 if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
492 return !args->for_gc;
494 return args->for_gc && args->cno == ii->i_cno;
497 static int nilfs_iget_set(struct inode *inode, void *opaque)
499 struct nilfs_iget_args *args = opaque;
501 inode->i_ino = args->ino;
503 NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE;
504 NILFS_I(inode)->i_cno = args->cno;
509 struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
511 struct nilfs_iget_args args = { .ino = ino, .cno = 0, .for_gc = 0 };
515 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
516 if (unlikely(!inode))
517 return ERR_PTR(-ENOMEM);
518 if (!(inode->i_state & I_NEW))
521 err = __nilfs_read_inode(sb, ino, inode);
526 unlock_new_inode(inode);
530 struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
533 struct nilfs_iget_args args = { .ino = ino, .cno = cno, .for_gc = 1 };
537 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
538 if (unlikely(!inode))
539 return ERR_PTR(-ENOMEM);
540 if (!(inode->i_state & I_NEW))
543 err = nilfs_init_gcinode(inode);
548 unlock_new_inode(inode);
552 void nilfs_write_inode_common(struct inode *inode,
553 struct nilfs_inode *raw_inode, int has_bmap)
555 struct nilfs_inode_info *ii = NILFS_I(inode);
557 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
558 raw_inode->i_uid = cpu_to_le32(inode->i_uid);
559 raw_inode->i_gid = cpu_to_le32(inode->i_gid);
560 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
561 raw_inode->i_size = cpu_to_le64(inode->i_size);
562 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
563 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
564 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
565 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
566 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
568 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
569 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
572 nilfs_bmap_write(ii->i_bmap, raw_inode);
573 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
574 raw_inode->i_device_code =
575 cpu_to_le64(huge_encode_dev(inode->i_rdev));
576 /* When extending inode, nilfs->ns_inode_size should be checked
577 for substitutions of appended fields */
580 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
582 ino_t ino = inode->i_ino;
583 struct nilfs_inode_info *ii = NILFS_I(inode);
584 struct super_block *sb = inode->i_sb;
585 struct nilfs_sb_info *sbi = NILFS_SB(sb);
586 struct nilfs_inode *raw_inode;
588 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
590 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
591 memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
592 set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
594 nilfs_write_inode_common(inode, raw_inode, 0);
595 /* XXX: call with has_bmap = 0 is a workaround to avoid
596 deadlock of bmap. This delays update of i_bmap to just
598 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
601 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
603 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
609 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
612 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
621 b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
622 ret = nilfs_bmap_truncate(ii->i_bmap, b);
623 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
624 if (!ret || (ret == -ENOMEM &&
625 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
630 nilfs_error(ii->vfs_inode.i_sb, __func__,
631 "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
633 nilfs_warning(ii->vfs_inode.i_sb, __func__,
634 "failed to truncate bmap (ino=%lu, err=%d)",
635 ii->vfs_inode.i_ino, ret);
638 void nilfs_truncate(struct inode *inode)
640 unsigned long blkoff;
641 unsigned int blocksize;
642 struct nilfs_transaction_info ti;
643 struct super_block *sb = inode->i_sb;
644 struct nilfs_inode_info *ii = NILFS_I(inode);
646 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
648 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
651 blocksize = sb->s_blocksize;
652 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
653 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
655 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
657 nilfs_truncate_bmap(ii, blkoff);
659 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
661 nilfs_set_transaction_flag(NILFS_TI_SYNC);
663 nilfs_mark_inode_dirty(inode);
664 nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
665 nilfs_transaction_commit(sb);
666 /* May construct a logical segment and may fail in sync mode.
667 But truncate has no return value. */
670 static void nilfs_clear_inode(struct inode *inode)
672 struct nilfs_inode_info *ii = NILFS_I(inode);
675 * Free resources allocated in nilfs_read_inode(), here.
677 BUG_ON(!list_empty(&ii->i_dirty));
681 if (test_bit(NILFS_I_BMAP, &ii->i_state))
682 nilfs_bmap_clear(ii->i_bmap);
684 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
687 void nilfs_evict_inode(struct inode *inode)
689 struct nilfs_transaction_info ti;
690 struct super_block *sb = inode->i_sb;
691 struct nilfs_inode_info *ii = NILFS_I(inode);
693 if (inode->i_nlink || unlikely(is_bad_inode(inode))) {
694 if (inode->i_data.nrpages)
695 truncate_inode_pages(&inode->i_data, 0);
696 end_writeback(inode);
697 nilfs_clear_inode(inode);
700 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
702 if (inode->i_data.nrpages)
703 truncate_inode_pages(&inode->i_data, 0);
705 nilfs_truncate_bmap(ii, 0);
706 nilfs_mark_inode_dirty(inode);
707 end_writeback(inode);
708 nilfs_clear_inode(inode);
709 nilfs_free_inode(inode);
710 /* nilfs_free_inode() marks inode buffer dirty */
712 nilfs_set_transaction_flag(NILFS_TI_SYNC);
713 nilfs_transaction_commit(sb);
714 /* May construct a logical segment and may fail in sync mode.
715 But delete_inode has no return value. */
718 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
720 struct nilfs_transaction_info ti;
721 struct inode *inode = dentry->d_inode;
722 struct super_block *sb = inode->i_sb;
725 err = inode_change_ok(inode, iattr);
729 err = nilfs_transaction_begin(sb, &ti, 0);
733 if ((iattr->ia_valid & ATTR_SIZE) &&
734 iattr->ia_size != i_size_read(inode)) {
735 err = vmtruncate(inode, iattr->ia_size);
740 setattr_copy(inode, iattr);
741 mark_inode_dirty(inode);
743 if (iattr->ia_valid & ATTR_MODE) {
744 err = nilfs_acl_chmod(inode);
749 return nilfs_transaction_commit(sb);
752 nilfs_transaction_abort(sb);
756 int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
757 struct buffer_head **pbh)
759 struct nilfs_inode_info *ii = NILFS_I(inode);
762 spin_lock(&sbi->s_inode_lock);
763 if (ii->i_bh == NULL) {
764 spin_unlock(&sbi->s_inode_lock);
765 err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
769 spin_lock(&sbi->s_inode_lock);
770 if (ii->i_bh == NULL)
780 spin_unlock(&sbi->s_inode_lock);
784 int nilfs_inode_dirty(struct inode *inode)
786 struct nilfs_inode_info *ii = NILFS_I(inode);
787 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
790 if (!list_empty(&ii->i_dirty)) {
791 spin_lock(&sbi->s_inode_lock);
792 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
793 test_bit(NILFS_I_BUSY, &ii->i_state);
794 spin_unlock(&sbi->s_inode_lock);
799 int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
802 struct nilfs_inode_info *ii = NILFS_I(inode);
804 atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
806 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
809 spin_lock(&sbi->s_inode_lock);
810 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
811 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
812 /* Because this routine may race with nilfs_dispose_list(),
813 we have to check NILFS_I_QUEUED here, too. */
814 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
815 /* This will happen when somebody is freeing
817 nilfs_warning(sbi->s_super, __func__,
818 "cannot get inode (ino=%lu)\n",
820 spin_unlock(&sbi->s_inode_lock);
821 return -EINVAL; /* NILFS_I_DIRTY may remain for
824 list_del(&ii->i_dirty);
825 list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
826 set_bit(NILFS_I_QUEUED, &ii->i_state);
828 spin_unlock(&sbi->s_inode_lock);
832 int nilfs_mark_inode_dirty(struct inode *inode)
834 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
835 struct buffer_head *ibh;
838 err = nilfs_load_inode_block(sbi, inode, &ibh);
840 nilfs_warning(inode->i_sb, __func__,
841 "failed to reget inode block.\n");
844 nilfs_update_inode(inode, ibh);
845 nilfs_mdt_mark_buffer_dirty(ibh);
846 nilfs_mdt_mark_dirty(sbi->s_ifile);
852 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
853 * @inode: inode of the file to be registered.
855 * nilfs_dirty_inode() loads a inode block containing the specified
856 * @inode and copies data from a nilfs_inode to a corresponding inode
857 * entry in the inode block. This operation is excluded from the segment
858 * construction. This function can be called both as a single operation
859 * and as a part of indivisible file operations.
861 void nilfs_dirty_inode(struct inode *inode)
863 struct nilfs_transaction_info ti;
864 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
866 if (is_bad_inode(inode)) {
867 nilfs_warning(inode->i_sb, __func__,
868 "tried to mark bad_inode dirty. ignored.\n");
873 nilfs_mdt_mark_dirty(inode);
876 nilfs_transaction_begin(inode->i_sb, &ti, 0);
877 nilfs_mark_inode_dirty(inode);
878 nilfs_transaction_commit(inode->i_sb); /* never fails */