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>
27 #include <linux/uio.h>
37 * nilfs_get_block() - get a file block on the filesystem (callback function)
38 * @inode - inode struct of the target file
39 * @blkoff - file block number
40 * @bh_result - buffer head to be mapped on
41 * @create - indicate whether allocating the block or not when it has not
44 * This function does not issue actual read request of the specified data
45 * block. It is done by VFS.
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
);
53 struct inode
*dat
= nilfs_dat_inode(NILFS_I_NILFS(inode
));
54 unsigned maxblocks
= bh_result
->b_size
>> inode
->i_blkbits
;
56 down_read(&NILFS_MDT(dat
)->mi_sem
);
57 ret
= nilfs_bmap_lookup_contig(ii
->i_bmap
, blkoff
, &blknum
, maxblocks
);
58 up_read(&NILFS_MDT(dat
)->mi_sem
);
59 if (ret
>= 0) { /* found */
60 map_bh(bh_result
, inode
->i_sb
, blknum
);
62 bh_result
->b_size
= (ret
<< inode
->i_blkbits
);
65 /* data block was not found */
66 if (ret
== -ENOENT
&& create
) {
67 struct nilfs_transaction_info ti
;
69 bh_result
->b_blocknr
= 0;
70 err
= nilfs_transaction_begin(inode
->i_sb
, &ti
, 1);
73 err
= nilfs_bmap_insert(ii
->i_bmap
, (unsigned long)blkoff
,
74 (unsigned long)bh_result
);
75 if (unlikely(err
!= 0)) {
78 * The get_block() function could be called
79 * from multiple callers for an inode.
80 * However, the page having this block must
81 * be locked in this case.
84 "nilfs_get_block: a race condition "
85 "while inserting a data block. "
86 "(inode number=%lu, file block "
89 (unsigned long long)blkoff
);
91 } else if (err
== -EINVAL
) {
92 nilfs_error(inode
->i_sb
, __func__
,
93 "broken bmap (inode=%lu)\n",
97 nilfs_transaction_abort(inode
->i_sb
);
100 nilfs_mark_inode_dirty(inode
);
101 nilfs_transaction_commit(inode
->i_sb
); /* never fails */
102 /* Error handling should be detailed */
103 set_buffer_new(bh_result
);
104 map_bh(bh_result
, inode
->i_sb
, 0); /* dbn must be changed
106 } else if (ret
== -ENOENT
) {
107 /* not found is not error (e.g. hole); must return without
108 the mapped state flag. */
119 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
120 * address_space_operations.
121 * @file - file struct of the file to be read
122 * @page - the page to be read
124 static int nilfs_readpage(struct file
*file
, struct page
*page
)
126 return mpage_readpage(page
, nilfs_get_block
);
130 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
131 * address_space_operations.
132 * @file - file struct of the file to be read
133 * @mapping - address_space struct used for reading multiple pages
134 * @pages - the pages to be read
135 * @nr_pages - number of pages to be read
137 static int nilfs_readpages(struct file
*file
, struct address_space
*mapping
,
138 struct list_head
*pages
, unsigned nr_pages
)
140 return mpage_readpages(mapping
, pages
, nr_pages
, nilfs_get_block
);
143 static int nilfs_writepages(struct address_space
*mapping
,
144 struct writeback_control
*wbc
)
146 struct inode
*inode
= mapping
->host
;
149 if (wbc
->sync_mode
== WB_SYNC_ALL
)
150 err
= nilfs_construct_dsync_segment(inode
->i_sb
, inode
,
156 static int nilfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
158 struct inode
*inode
= page
->mapping
->host
;
161 redirty_page_for_writepage(wbc
, page
);
164 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
165 err
= nilfs_construct_segment(inode
->i_sb
);
168 } else if (wbc
->for_reclaim
)
169 nilfs_flush_segment(inode
->i_sb
, inode
->i_ino
);
174 static int nilfs_set_page_dirty(struct page
*page
)
176 int ret
= __set_page_dirty_buffers(page
);
179 struct inode
*inode
= page
->mapping
->host
;
180 struct nilfs_sb_info
*sbi
= NILFS_SB(inode
->i_sb
);
181 unsigned nr_dirty
= 1 << (PAGE_SHIFT
- inode
->i_blkbits
);
183 nilfs_set_file_dirty(sbi
, inode
, nr_dirty
);
188 static int nilfs_write_begin(struct file
*file
, struct address_space
*mapping
,
189 loff_t pos
, unsigned len
, unsigned flags
,
190 struct page
**pagep
, void **fsdata
)
193 struct inode
*inode
= mapping
->host
;
194 int err
= nilfs_transaction_begin(inode
->i_sb
, NULL
, 1);
200 err
= block_write_begin(file
, mapping
, pos
, len
, flags
, pagep
,
201 fsdata
, nilfs_get_block
);
203 nilfs_transaction_abort(inode
->i_sb
);
207 static int nilfs_write_end(struct file
*file
, struct address_space
*mapping
,
208 loff_t pos
, unsigned len
, unsigned copied
,
209 struct page
*page
, void *fsdata
)
211 struct inode
*inode
= mapping
->host
;
212 unsigned start
= pos
& (PAGE_CACHE_SIZE
- 1);
216 nr_dirty
= nilfs_page_count_clean_buffers(page
, start
,
218 copied
= generic_write_end(file
, mapping
, pos
, len
, copied
, page
,
220 nilfs_set_file_dirty(NILFS_SB(inode
->i_sb
), inode
, nr_dirty
);
221 err
= nilfs_transaction_commit(inode
->i_sb
);
222 return err
? : copied
;
226 nilfs_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
227 loff_t offset
, unsigned long nr_segs
)
229 struct file
*file
= iocb
->ki_filp
;
230 struct inode
*inode
= file
->f_mapping
->host
;
236 /* Needs synchronization with the cleaner */
237 size
= blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
238 offset
, nr_segs
, nilfs_get_block
, NULL
);
242 const struct address_space_operations nilfs_aops
= {
243 .writepage
= nilfs_writepage
,
244 .readpage
= nilfs_readpage
,
245 .sync_page
= block_sync_page
,
246 .writepages
= nilfs_writepages
,
247 .set_page_dirty
= nilfs_set_page_dirty
,
248 .readpages
= nilfs_readpages
,
249 .write_begin
= nilfs_write_begin
,
250 .write_end
= nilfs_write_end
,
251 /* .releasepage = nilfs_releasepage, */
252 .invalidatepage
= block_invalidatepage
,
253 .direct_IO
= nilfs_direct_IO
,
254 .is_partially_uptodate
= block_is_partially_uptodate
,
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; */
312 ii
->i_dir_start_lookup
= 0;
314 nilfs_set_inode_flags(inode
);
315 spin_lock(&sbi
->s_next_gen_lock
);
316 inode
->i_generation
= sbi
->s_next_generation
++;
317 spin_unlock(&sbi
->s_next_gen_lock
);
318 insert_inode_hash(inode
);
320 err
= nilfs_init_acl(inode
, dir
);
322 goto failed_acl
; /* never occur. When supporting
323 nilfs_init_acl(), proper cancellation of
324 above jobs should be considered */
331 iput(inode
); /* raw_inode will be deleted through
332 generic_delete_inode() */
335 failed_ifile_create_inode
:
336 make_bad_inode(inode
);
337 iput(inode
); /* if i_nlink == 1, generic_forget_inode() will be
343 void nilfs_free_inode(struct inode
*inode
)
345 struct super_block
*sb
= inode
->i_sb
;
346 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
349 /* XXX: check error code? Is there any thing I can do? */
350 (void) nilfs_ifile_delete_inode(sbi
->s_ifile
, inode
->i_ino
);
351 atomic_dec(&sbi
->s_inodes_count
);
354 void nilfs_set_inode_flags(struct inode
*inode
)
356 unsigned int flags
= NILFS_I(inode
)->i_flags
;
358 inode
->i_flags
&= ~(S_SYNC
| S_APPEND
| S_IMMUTABLE
| S_NOATIME
|
360 if (flags
& NILFS_SYNC_FL
)
361 inode
->i_flags
|= S_SYNC
;
362 if (flags
& NILFS_APPEND_FL
)
363 inode
->i_flags
|= S_APPEND
;
364 if (flags
& NILFS_IMMUTABLE_FL
)
365 inode
->i_flags
|= S_IMMUTABLE
;
366 #ifndef NILFS_ATIME_DISABLE
367 if (flags
& NILFS_NOATIME_FL
)
369 inode
->i_flags
|= S_NOATIME
;
370 if (flags
& NILFS_DIRSYNC_FL
)
371 inode
->i_flags
|= S_DIRSYNC
;
372 mapping_set_gfp_mask(inode
->i_mapping
,
373 mapping_gfp_mask(inode
->i_mapping
) & ~__GFP_FS
);
376 int nilfs_read_inode_common(struct inode
*inode
,
377 struct nilfs_inode
*raw_inode
)
379 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
382 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
383 inode
->i_uid
= (uid_t
)le32_to_cpu(raw_inode
->i_uid
);
384 inode
->i_gid
= (gid_t
)le32_to_cpu(raw_inode
->i_gid
);
385 inode
->i_nlink
= le16_to_cpu(raw_inode
->i_links_count
);
386 inode
->i_size
= le64_to_cpu(raw_inode
->i_size
);
387 inode
->i_atime
.tv_sec
= le64_to_cpu(raw_inode
->i_mtime
);
388 inode
->i_ctime
.tv_sec
= le64_to_cpu(raw_inode
->i_ctime
);
389 inode
->i_mtime
.tv_sec
= le64_to_cpu(raw_inode
->i_mtime
);
390 inode
->i_atime
.tv_nsec
= le32_to_cpu(raw_inode
->i_mtime_nsec
);
391 inode
->i_ctime
.tv_nsec
= le32_to_cpu(raw_inode
->i_ctime_nsec
);
392 inode
->i_mtime
.tv_nsec
= le32_to_cpu(raw_inode
->i_mtime_nsec
);
393 if (inode
->i_nlink
== 0 && inode
->i_mode
== 0)
394 return -EINVAL
; /* this inode is deleted */
396 inode
->i_blocks
= le64_to_cpu(raw_inode
->i_blocks
);
397 ii
->i_flags
= le32_to_cpu(raw_inode
->i_flags
);
399 ii
->i_file_acl
= le32_to_cpu(raw_inode
->i_file_acl
);
400 ii
->i_dir_acl
= S_ISREG(inode
->i_mode
) ?
401 0 : le32_to_cpu(raw_inode
->i_dir_acl
);
403 ii
->i_dir_start_lookup
= 0;
405 inode
->i_generation
= le32_to_cpu(raw_inode
->i_generation
);
407 if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
408 S_ISLNK(inode
->i_mode
)) {
409 err
= nilfs_bmap_read(ii
->i_bmap
, raw_inode
);
412 set_bit(NILFS_I_BMAP
, &ii
->i_state
);
413 /* No lock is needed; iget() ensures it. */
418 static int __nilfs_read_inode(struct super_block
*sb
, unsigned long ino
,
421 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
422 struct inode
*dat
= nilfs_dat_inode(sbi
->s_nilfs
);
423 struct buffer_head
*bh
;
424 struct nilfs_inode
*raw_inode
;
427 down_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
428 err
= nilfs_ifile_get_inode_block(sbi
->s_ifile
, ino
, &bh
);
432 raw_inode
= nilfs_ifile_map_inode(sbi
->s_ifile
, ino
, bh
);
434 err
= nilfs_read_inode_common(inode
, raw_inode
);
438 if (S_ISREG(inode
->i_mode
)) {
439 inode
->i_op
= &nilfs_file_inode_operations
;
440 inode
->i_fop
= &nilfs_file_operations
;
441 inode
->i_mapping
->a_ops
= &nilfs_aops
;
442 } else if (S_ISDIR(inode
->i_mode
)) {
443 inode
->i_op
= &nilfs_dir_inode_operations
;
444 inode
->i_fop
= &nilfs_dir_operations
;
445 inode
->i_mapping
->a_ops
= &nilfs_aops
;
446 } else if (S_ISLNK(inode
->i_mode
)) {
447 inode
->i_op
= &nilfs_symlink_inode_operations
;
448 inode
->i_mapping
->a_ops
= &nilfs_aops
;
450 inode
->i_op
= &nilfs_special_inode_operations
;
452 inode
, inode
->i_mode
,
453 new_decode_dev(le64_to_cpu(raw_inode
->i_device_code
)));
455 nilfs_ifile_unmap_inode(sbi
->s_ifile
, ino
, bh
);
457 up_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
458 nilfs_set_inode_flags(inode
);
462 nilfs_ifile_unmap_inode(sbi
->s_ifile
, ino
, bh
);
466 up_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
470 struct inode
*nilfs_iget(struct super_block
*sb
, unsigned long ino
)
475 inode
= iget_locked(sb
, ino
);
476 if (unlikely(!inode
))
477 return ERR_PTR(-ENOMEM
);
478 if (!(inode
->i_state
& I_NEW
))
481 err
= __nilfs_read_inode(sb
, ino
, inode
);
486 unlock_new_inode(inode
);
490 void nilfs_write_inode_common(struct inode
*inode
,
491 struct nilfs_inode
*raw_inode
, int has_bmap
)
493 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
495 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
496 raw_inode
->i_uid
= cpu_to_le32(inode
->i_uid
);
497 raw_inode
->i_gid
= cpu_to_le32(inode
->i_gid
);
498 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
499 raw_inode
->i_size
= cpu_to_le64(inode
->i_size
);
500 raw_inode
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
501 raw_inode
->i_mtime
= cpu_to_le64(inode
->i_mtime
.tv_sec
);
502 raw_inode
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
503 raw_inode
->i_mtime_nsec
= cpu_to_le32(inode
->i_mtime
.tv_nsec
);
504 raw_inode
->i_blocks
= cpu_to_le64(inode
->i_blocks
);
506 raw_inode
->i_flags
= cpu_to_le32(ii
->i_flags
);
507 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
510 nilfs_bmap_write(ii
->i_bmap
, raw_inode
);
511 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
))
512 raw_inode
->i_device_code
=
513 cpu_to_le64(new_encode_dev(inode
->i_rdev
));
514 /* When extending inode, nilfs->ns_inode_size should be checked
515 for substitutions of appended fields */
518 void nilfs_update_inode(struct inode
*inode
, struct buffer_head
*ibh
)
520 ino_t ino
= inode
->i_ino
;
521 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
522 struct super_block
*sb
= inode
->i_sb
;
523 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
524 struct nilfs_inode
*raw_inode
;
526 raw_inode
= nilfs_ifile_map_inode(sbi
->s_ifile
, ino
, ibh
);
528 if (test_and_clear_bit(NILFS_I_NEW
, &ii
->i_state
))
529 memset(raw_inode
, 0, NILFS_MDT(sbi
->s_ifile
)->mi_entry_size
);
530 set_bit(NILFS_I_INODE_DIRTY
, &ii
->i_state
);
532 nilfs_write_inode_common(inode
, raw_inode
, 0);
533 /* XXX: call with has_bmap = 0 is a workaround to avoid
534 deadlock of bmap. This delays update of i_bmap to just
536 nilfs_ifile_unmap_inode(sbi
->s_ifile
, ino
, ibh
);
539 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
541 static void nilfs_truncate_bmap(struct nilfs_inode_info
*ii
,
547 if (!test_bit(NILFS_I_BMAP
, &ii
->i_state
))
550 ret
= nilfs_bmap_last_key(ii
->i_bmap
, &b
);
559 b
-= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS
, b
- from
);
560 ret
= nilfs_bmap_truncate(ii
->i_bmap
, b
);
561 nilfs_relax_pressure_in_lock(ii
->vfs_inode
.i_sb
);
562 if (!ret
|| (ret
== -ENOMEM
&&
563 nilfs_bmap_truncate(ii
->i_bmap
, b
) == 0))
568 nilfs_error(ii
->vfs_inode
.i_sb
, __func__
,
569 "bmap is broken (ino=%lu)", ii
->vfs_inode
.i_ino
);
571 nilfs_warning(ii
->vfs_inode
.i_sb
, __func__
,
572 "failed to truncate bmap (ino=%lu, err=%d)",
573 ii
->vfs_inode
.i_ino
, ret
);
576 void nilfs_truncate(struct inode
*inode
)
578 unsigned long blkoff
;
579 unsigned int blocksize
;
580 struct nilfs_transaction_info ti
;
581 struct super_block
*sb
= inode
->i_sb
;
582 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
584 if (!test_bit(NILFS_I_BMAP
, &ii
->i_state
))
586 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
589 blocksize
= sb
->s_blocksize
;
590 blkoff
= (inode
->i_size
+ blocksize
- 1) >> sb
->s_blocksize_bits
;
591 nilfs_transaction_begin(sb
, &ti
, 0); /* never fails */
593 block_truncate_page(inode
->i_mapping
, inode
->i_size
, nilfs_get_block
);
595 nilfs_truncate_bmap(ii
, blkoff
);
597 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
599 nilfs_set_transaction_flag(NILFS_TI_SYNC
);
601 nilfs_mark_inode_dirty(inode
);
602 nilfs_set_file_dirty(NILFS_SB(sb
), inode
, 0);
603 nilfs_transaction_commit(sb
);
604 /* May construct a logical segment and may fail in sync mode.
605 But truncate has no return value. */
608 void nilfs_delete_inode(struct inode
*inode
)
610 struct nilfs_transaction_info ti
;
611 struct super_block
*sb
= inode
->i_sb
;
612 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
614 if (unlikely(is_bad_inode(inode
))) {
615 if (inode
->i_data
.nrpages
)
616 truncate_inode_pages(&inode
->i_data
, 0);
620 nilfs_transaction_begin(sb
, &ti
, 0); /* never fails */
622 if (inode
->i_data
.nrpages
)
623 truncate_inode_pages(&inode
->i_data
, 0);
625 nilfs_truncate_bmap(ii
, 0);
626 nilfs_mark_inode_dirty(inode
);
627 nilfs_free_inode(inode
);
628 /* nilfs_free_inode() marks inode buffer dirty */
630 nilfs_set_transaction_flag(NILFS_TI_SYNC
);
631 nilfs_transaction_commit(sb
);
632 /* May construct a logical segment and may fail in sync mode.
633 But delete_inode has no return value. */
636 int nilfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
638 struct nilfs_transaction_info ti
;
639 struct inode
*inode
= dentry
->d_inode
;
640 struct super_block
*sb
= inode
->i_sb
;
643 err
= inode_change_ok(inode
, iattr
);
647 err
= nilfs_transaction_begin(sb
, &ti
, 0);
650 err
= inode_setattr(inode
, iattr
);
651 if (!err
&& (iattr
->ia_valid
& ATTR_MODE
))
652 err
= nilfs_acl_chmod(inode
);
654 err
= nilfs_transaction_commit(sb
);
656 nilfs_transaction_abort(sb
);
661 int nilfs_load_inode_block(struct nilfs_sb_info
*sbi
, struct inode
*inode
,
662 struct buffer_head
**pbh
)
664 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
667 spin_lock(&sbi
->s_inode_lock
);
668 if (ii
->i_bh
== NULL
) {
669 spin_unlock(&sbi
->s_inode_lock
);
670 err
= nilfs_ifile_get_inode_block(sbi
->s_ifile
, inode
->i_ino
,
674 spin_lock(&sbi
->s_inode_lock
);
675 if (ii
->i_bh
== NULL
)
685 spin_unlock(&sbi
->s_inode_lock
);
689 int nilfs_inode_dirty(struct inode
*inode
)
691 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
692 struct nilfs_sb_info
*sbi
= NILFS_SB(inode
->i_sb
);
695 if (!list_empty(&ii
->i_dirty
)) {
696 spin_lock(&sbi
->s_inode_lock
);
697 ret
= test_bit(NILFS_I_DIRTY
, &ii
->i_state
) ||
698 test_bit(NILFS_I_BUSY
, &ii
->i_state
);
699 spin_unlock(&sbi
->s_inode_lock
);
704 int nilfs_set_file_dirty(struct nilfs_sb_info
*sbi
, struct inode
*inode
,
707 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
709 atomic_add(nr_dirty
, &sbi
->s_nilfs
->ns_ndirtyblks
);
711 if (test_and_set_bit(NILFS_I_DIRTY
, &ii
->i_state
))
714 spin_lock(&sbi
->s_inode_lock
);
715 if (!test_bit(NILFS_I_QUEUED
, &ii
->i_state
) &&
716 !test_bit(NILFS_I_BUSY
, &ii
->i_state
)) {
717 /* Because this routine may race with nilfs_dispose_list(),
718 we have to check NILFS_I_QUEUED here, too. */
719 if (list_empty(&ii
->i_dirty
) && igrab(inode
) == NULL
) {
720 /* This will happen when somebody is freeing
722 nilfs_warning(sbi
->s_super
, __func__
,
723 "cannot get inode (ino=%lu)\n",
725 spin_unlock(&sbi
->s_inode_lock
);
726 return -EINVAL
; /* NILFS_I_DIRTY may remain for
729 list_del(&ii
->i_dirty
);
730 list_add_tail(&ii
->i_dirty
, &sbi
->s_dirty_files
);
731 set_bit(NILFS_I_QUEUED
, &ii
->i_state
);
733 spin_unlock(&sbi
->s_inode_lock
);
737 int nilfs_mark_inode_dirty(struct inode
*inode
)
739 struct nilfs_sb_info
*sbi
= NILFS_SB(inode
->i_sb
);
740 struct buffer_head
*ibh
;
743 err
= nilfs_load_inode_block(sbi
, inode
, &ibh
);
745 nilfs_warning(inode
->i_sb
, __func__
,
746 "failed to reget inode block.\n");
749 nilfs_update_inode(inode
, ibh
);
750 nilfs_mdt_mark_buffer_dirty(ibh
);
751 nilfs_mdt_mark_dirty(sbi
->s_ifile
);
757 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
758 * @inode: inode of the file to be registered.
760 * nilfs_dirty_inode() loads a inode block containing the specified
761 * @inode and copies data from a nilfs_inode to a corresponding inode
762 * entry in the inode block. This operation is excluded from the segment
763 * construction. This function can be called both as a single operation
764 * and as a part of indivisible file operations.
766 void nilfs_dirty_inode(struct inode
*inode
)
768 struct nilfs_transaction_info ti
;
770 if (is_bad_inode(inode
)) {
771 nilfs_warning(inode
->i_sb
, __func__
,
772 "tried to mark bad_inode dirty. ignored.\n");
776 nilfs_transaction_begin(inode
->i_sb
, &ti
, 0);
777 nilfs_mark_inode_dirty(inode
);
778 nilfs_transaction_commit(inode
->i_sb
); /* never fails */