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 void nilfs_write_inode_common(struct inode
*inode
,
531 struct nilfs_inode
*raw_inode
, int has_bmap
)
533 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
535 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
536 raw_inode
->i_uid
= cpu_to_le32(inode
->i_uid
);
537 raw_inode
->i_gid
= cpu_to_le32(inode
->i_gid
);
538 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
539 raw_inode
->i_size
= cpu_to_le64(inode
->i_size
);
540 raw_inode
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
541 raw_inode
->i_mtime
= cpu_to_le64(inode
->i_mtime
.tv_sec
);
542 raw_inode
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
543 raw_inode
->i_mtime_nsec
= cpu_to_le32(inode
->i_mtime
.tv_nsec
);
544 raw_inode
->i_blocks
= cpu_to_le64(inode
->i_blocks
);
546 raw_inode
->i_flags
= cpu_to_le32(ii
->i_flags
);
547 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
550 nilfs_bmap_write(ii
->i_bmap
, raw_inode
);
551 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
))
552 raw_inode
->i_device_code
=
553 cpu_to_le64(huge_encode_dev(inode
->i_rdev
));
554 /* When extending inode, nilfs->ns_inode_size should be checked
555 for substitutions of appended fields */
558 void nilfs_update_inode(struct inode
*inode
, struct buffer_head
*ibh
)
560 ino_t ino
= inode
->i_ino
;
561 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
562 struct super_block
*sb
= inode
->i_sb
;
563 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
564 struct nilfs_inode
*raw_inode
;
566 raw_inode
= nilfs_ifile_map_inode(sbi
->s_ifile
, ino
, ibh
);
568 if (test_and_clear_bit(NILFS_I_NEW
, &ii
->i_state
))
569 memset(raw_inode
, 0, NILFS_MDT(sbi
->s_ifile
)->mi_entry_size
);
570 set_bit(NILFS_I_INODE_DIRTY
, &ii
->i_state
);
572 nilfs_write_inode_common(inode
, raw_inode
, 0);
573 /* XXX: call with has_bmap = 0 is a workaround to avoid
574 deadlock of bmap. This delays update of i_bmap to just
576 nilfs_ifile_unmap_inode(sbi
->s_ifile
, ino
, ibh
);
579 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
581 static void nilfs_truncate_bmap(struct nilfs_inode_info
*ii
,
587 if (!test_bit(NILFS_I_BMAP
, &ii
->i_state
))
590 ret
= nilfs_bmap_last_key(ii
->i_bmap
, &b
);
599 b
-= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS
, b
- from
);
600 ret
= nilfs_bmap_truncate(ii
->i_bmap
, b
);
601 nilfs_relax_pressure_in_lock(ii
->vfs_inode
.i_sb
);
602 if (!ret
|| (ret
== -ENOMEM
&&
603 nilfs_bmap_truncate(ii
->i_bmap
, b
) == 0))
608 nilfs_error(ii
->vfs_inode
.i_sb
, __func__
,
609 "bmap is broken (ino=%lu)", ii
->vfs_inode
.i_ino
);
611 nilfs_warning(ii
->vfs_inode
.i_sb
, __func__
,
612 "failed to truncate bmap (ino=%lu, err=%d)",
613 ii
->vfs_inode
.i_ino
, ret
);
616 void nilfs_truncate(struct inode
*inode
)
618 unsigned long blkoff
;
619 unsigned int blocksize
;
620 struct nilfs_transaction_info ti
;
621 struct super_block
*sb
= inode
->i_sb
;
622 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
624 if (!test_bit(NILFS_I_BMAP
, &ii
->i_state
))
626 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
629 blocksize
= sb
->s_blocksize
;
630 blkoff
= (inode
->i_size
+ blocksize
- 1) >> sb
->s_blocksize_bits
;
631 nilfs_transaction_begin(sb
, &ti
, 0); /* never fails */
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_mark_inode_dirty(inode
);
642 nilfs_set_file_dirty(NILFS_SB(sb
), inode
, 0);
643 nilfs_transaction_commit(sb
);
644 /* May construct a logical segment and may fail in sync mode.
645 But truncate has no return value. */
648 static void nilfs_clear_inode(struct inode
*inode
)
650 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
653 * Free resources allocated in nilfs_read_inode(), here.
655 BUG_ON(!list_empty(&ii
->i_dirty
));
659 if (test_bit(NILFS_I_BMAP
, &ii
->i_state
))
660 nilfs_bmap_clear(ii
->i_bmap
);
662 nilfs_btnode_cache_clear(&ii
->i_btnode_cache
);
665 void nilfs_evict_inode(struct inode
*inode
)
667 struct nilfs_transaction_info ti
;
668 struct super_block
*sb
= inode
->i_sb
;
669 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
671 if (inode
->i_nlink
|| unlikely(is_bad_inode(inode
))) {
672 if (inode
->i_data
.nrpages
)
673 truncate_inode_pages(&inode
->i_data
, 0);
674 end_writeback(inode
);
675 nilfs_clear_inode(inode
);
678 nilfs_transaction_begin(sb
, &ti
, 0); /* never fails */
680 if (inode
->i_data
.nrpages
)
681 truncate_inode_pages(&inode
->i_data
, 0);
683 nilfs_truncate_bmap(ii
, 0);
684 nilfs_mark_inode_dirty(inode
);
685 end_writeback(inode
);
686 nilfs_clear_inode(inode
);
687 nilfs_free_inode(inode
);
688 /* nilfs_free_inode() marks inode buffer dirty */
690 nilfs_set_transaction_flag(NILFS_TI_SYNC
);
691 nilfs_transaction_commit(sb
);
692 /* May construct a logical segment and may fail in sync mode.
693 But delete_inode has no return value. */
696 int nilfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
698 struct nilfs_transaction_info ti
;
699 struct inode
*inode
= dentry
->d_inode
;
700 struct super_block
*sb
= inode
->i_sb
;
703 err
= inode_change_ok(inode
, iattr
);
707 err
= nilfs_transaction_begin(sb
, &ti
, 0);
711 if ((iattr
->ia_valid
& ATTR_SIZE
) &&
712 iattr
->ia_size
!= i_size_read(inode
)) {
713 err
= vmtruncate(inode
, iattr
->ia_size
);
718 setattr_copy(inode
, iattr
);
719 mark_inode_dirty(inode
);
721 if (iattr
->ia_valid
& ATTR_MODE
) {
722 err
= nilfs_acl_chmod(inode
);
727 return nilfs_transaction_commit(sb
);
730 nilfs_transaction_abort(sb
);
734 int nilfs_load_inode_block(struct nilfs_sb_info
*sbi
, struct inode
*inode
,
735 struct buffer_head
**pbh
)
737 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
740 spin_lock(&sbi
->s_inode_lock
);
741 if (ii
->i_bh
== NULL
) {
742 spin_unlock(&sbi
->s_inode_lock
);
743 err
= nilfs_ifile_get_inode_block(sbi
->s_ifile
, inode
->i_ino
,
747 spin_lock(&sbi
->s_inode_lock
);
748 if (ii
->i_bh
== NULL
)
758 spin_unlock(&sbi
->s_inode_lock
);
762 int nilfs_inode_dirty(struct inode
*inode
)
764 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
765 struct nilfs_sb_info
*sbi
= NILFS_SB(inode
->i_sb
);
768 if (!list_empty(&ii
->i_dirty
)) {
769 spin_lock(&sbi
->s_inode_lock
);
770 ret
= test_bit(NILFS_I_DIRTY
, &ii
->i_state
) ||
771 test_bit(NILFS_I_BUSY
, &ii
->i_state
);
772 spin_unlock(&sbi
->s_inode_lock
);
777 int nilfs_set_file_dirty(struct nilfs_sb_info
*sbi
, struct inode
*inode
,
780 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
782 atomic_add(nr_dirty
, &sbi
->s_nilfs
->ns_ndirtyblks
);
784 if (test_and_set_bit(NILFS_I_DIRTY
, &ii
->i_state
))
787 spin_lock(&sbi
->s_inode_lock
);
788 if (!test_bit(NILFS_I_QUEUED
, &ii
->i_state
) &&
789 !test_bit(NILFS_I_BUSY
, &ii
->i_state
)) {
790 /* Because this routine may race with nilfs_dispose_list(),
791 we have to check NILFS_I_QUEUED here, too. */
792 if (list_empty(&ii
->i_dirty
) && igrab(inode
) == NULL
) {
793 /* This will happen when somebody is freeing
795 nilfs_warning(sbi
->s_super
, __func__
,
796 "cannot get inode (ino=%lu)\n",
798 spin_unlock(&sbi
->s_inode_lock
);
799 return -EINVAL
; /* NILFS_I_DIRTY may remain for
802 list_del(&ii
->i_dirty
);
803 list_add_tail(&ii
->i_dirty
, &sbi
->s_dirty_files
);
804 set_bit(NILFS_I_QUEUED
, &ii
->i_state
);
806 spin_unlock(&sbi
->s_inode_lock
);
810 int nilfs_mark_inode_dirty(struct inode
*inode
)
812 struct nilfs_sb_info
*sbi
= NILFS_SB(inode
->i_sb
);
813 struct buffer_head
*ibh
;
816 err
= nilfs_load_inode_block(sbi
, inode
, &ibh
);
818 nilfs_warning(inode
->i_sb
, __func__
,
819 "failed to reget inode block.\n");
822 nilfs_update_inode(inode
, ibh
);
823 nilfs_mdt_mark_buffer_dirty(ibh
);
824 nilfs_mdt_mark_dirty(sbi
->s_ifile
);
830 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
831 * @inode: inode of the file to be registered.
833 * nilfs_dirty_inode() loads a inode block containing the specified
834 * @inode and copies data from a nilfs_inode to a corresponding inode
835 * entry in the inode block. This operation is excluded from the segment
836 * construction. This function can be called both as a single operation
837 * and as a part of indivisible file operations.
839 void nilfs_dirty_inode(struct inode
*inode
)
841 struct nilfs_transaction_info ti
;
842 struct nilfs_mdt_info
*mdi
= NILFS_MDT(inode
);
844 if (is_bad_inode(inode
)) {
845 nilfs_warning(inode
->i_sb
, __func__
,
846 "tried to mark bad_inode dirty. ignored.\n");
851 nilfs_mdt_mark_dirty(inode
);
854 nilfs_transaction_begin(inode
->i_sb
, &ti
, 0);
855 nilfs_mark_inode_dirty(inode
);
856 nilfs_transaction_commit(inode
->i_sb
); /* never fails */