]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/nilfs2/inode.c
nilfs2: remove own inode hash used for GC
[net-next-2.6.git] / fs / nilfs2 / inode.c
1 /*
2  * inode.c - NILFS inode operations.
3  *
4  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Written by Ryusuke Konishi <ryusuke@osrg.net>
21  *
22  */
23
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>
29 #include "nilfs.h"
30 #include "btnode.h"
31 #include "segment.h"
32 #include "page.h"
33 #include "mdt.h"
34 #include "cpfile.h"
35 #include "ifile.h"
36
37 struct nilfs_iget_args {
38         u64 ino;
39         __u64 cno;
40         int for_gc;
41 };
42
43 /**
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
49  *      been allocated yet.
50  *
51  * This function does not issue actual read request of the specified data
52  * block. It is done by VFS.
53  */
54 int nilfs_get_block(struct inode *inode, sector_t blkoff,
55                     struct buffer_head *bh_result, int create)
56 {
57         struct nilfs_inode_info *ii = NILFS_I(inode);
58         __u64 blknum = 0;
59         int err = 0, ret;
60         struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
61         unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
62
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);
68                 if (ret > 0)
69                         bh_result->b_size = (ret << inode->i_blkbits);
70                 goto out;
71         }
72         /* data block was not found */
73         if (ret == -ENOENT && create) {
74                 struct nilfs_transaction_info ti;
75
76                 bh_result->b_blocknr = 0;
77                 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
78                 if (unlikely(err))
79                         goto out;
80                 err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
81                                         (unsigned long)bh_result);
82                 if (unlikely(err != 0)) {
83                         if (err == -EEXIST) {
84                                 /*
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.
89                                  */
90                                 printk(KERN_WARNING
91                                        "nilfs_get_block: a race condition "
92                                        "while inserting a data block. "
93                                        "(inode number=%lu, file block "
94                                        "offset=%llu)\n",
95                                        inode->i_ino,
96                                        (unsigned long long)blkoff);
97                                 err = 0;
98                         } else if (err == -EINVAL) {
99                                 nilfs_error(inode->i_sb, __func__,
100                                             "broken bmap (inode=%lu)\n",
101                                             inode->i_ino);
102                                 err = -EIO;
103                         }
104                         nilfs_transaction_abort(inode->i_sb);
105                         goto out;
106                 }
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
112                                                       to proper value */
113         } else if (ret == -ENOENT) {
114                 /* not found is not error (e.g. hole); must return without
115                    the mapped state flag. */
116                 ;
117         } else {
118                 err = ret;
119         }
120
121  out:
122         return err;
123 }
124
125 /**
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
130  */
131 static int nilfs_readpage(struct file *file, struct page *page)
132 {
133         return mpage_readpage(page, nilfs_get_block);
134 }
135
136 /**
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
143  */
144 static int nilfs_readpages(struct file *file, struct address_space *mapping,
145                            struct list_head *pages, unsigned nr_pages)
146 {
147         return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
148 }
149
150 static int nilfs_writepages(struct address_space *mapping,
151                             struct writeback_control *wbc)
152 {
153         struct inode *inode = mapping->host;
154         int err = 0;
155
156         if (wbc->sync_mode == WB_SYNC_ALL)
157                 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
158                                                     wbc->range_start,
159                                                     wbc->range_end);
160         return err;
161 }
162
163 static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
164 {
165         struct inode *inode = page->mapping->host;
166         int err;
167
168         redirty_page_for_writepage(wbc, page);
169         unlock_page(page);
170
171         if (wbc->sync_mode == WB_SYNC_ALL) {
172                 err = nilfs_construct_segment(inode->i_sb);
173                 if (unlikely(err))
174                         return err;
175         } else if (wbc->for_reclaim)
176                 nilfs_flush_segment(inode->i_sb, inode->i_ino);
177
178         return 0;
179 }
180
181 static int nilfs_set_page_dirty(struct page *page)
182 {
183         int ret = __set_page_dirty_buffers(page);
184
185         if (ret) {
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);
189
190                 nilfs_set_file_dirty(sbi, inode, nr_dirty);
191         }
192         return ret;
193 }
194
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)
198
199 {
200         struct inode *inode = mapping->host;
201         int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
202
203         if (unlikely(err))
204                 return err;
205
206         err = block_write_begin(mapping, pos, len, flags, pagep,
207                                 nilfs_get_block);
208         if (unlikely(err)) {
209                 loff_t isize = mapping->host->i_size;
210                 if (pos + len > isize)
211                         vmtruncate(mapping->host, isize);
212
213                 nilfs_transaction_abort(inode->i_sb);
214         }
215         return err;
216 }
217
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)
221 {
222         struct inode *inode = mapping->host;
223         unsigned start = pos & (PAGE_CACHE_SIZE - 1);
224         unsigned nr_dirty;
225         int err;
226
227         nr_dirty = nilfs_page_count_clean_buffers(page, start,
228                                                   start + copied);
229         copied = generic_write_end(file, mapping, pos, len, copied, page,
230                                    fsdata);
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;
234 }
235
236 static ssize_t
237 nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
238                 loff_t offset, unsigned long nr_segs)
239 {
240         struct file *file = iocb->ki_filp;
241         struct inode *inode = file->f_mapping->host;
242         ssize_t size;
243
244         if (rw == WRITE)
245                 return 0;
246
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);
250
251         /*
252          * In case of error extending write may have instantiated a few
253          * blocks outside i_size. Trim these off again.
254          */
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);
258
259                 if (end > isize)
260                         vmtruncate(inode, isize);
261         }
262
263         return size;
264 }
265
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,
279 };
280
281 struct inode *nilfs_new_inode(struct inode *dir, int mode)
282 {
283         struct super_block *sb = dir->i_sb;
284         struct nilfs_sb_info *sbi = NILFS_SB(sb);
285         struct inode *inode;
286         struct nilfs_inode_info *ii;
287         int err = -ENOMEM;
288         ino_t ino;
289
290         inode = new_inode(sb);
291         if (unlikely(!inode))
292                 goto failed;
293
294         mapping_set_gfp_mask(inode->i_mapping,
295                              mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
296
297         ii = NILFS_I(inode);
298         ii->i_state = 1 << NILFS_I_NEW;
299
300         err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
301         if (unlikely(err))
302                 goto failed_ifile_create_inode;
303         /* reference count of i_bh inherits from nilfs_mdt_read_block() */
304
305         atomic_inc(&sbi->s_inodes_count);
306         inode_init_owner(inode, dir, mode);
307         inode->i_ino = ino;
308         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
309
310         if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
311                 err = nilfs_bmap_read(ii->i_bmap, NULL);
312                 if (err < 0)
313                         goto failed_bmap;
314
315                 set_bit(NILFS_I_BMAP, &ii->i_state);
316                 /* No lock is needed; iget() ensures it. */
317         }
318
319         ii->i_flags = NILFS_I(dir)->i_flags;
320         if (S_ISLNK(mode))
321                 ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
322         if (!S_ISDIR(mode))
323                 ii->i_flags &= ~NILFS_DIRSYNC_FL;
324
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);
333
334         err = nilfs_init_acl(inode, dir);
335         if (unlikely(err))
336                 goto failed_acl; /* never occur. When supporting
337                                     nilfs_init_acl(), proper cancellation of
338                                     above jobs should be considered */
339
340         return inode;
341
342  failed_acl:
343  failed_bmap:
344         inode->i_nlink = 0;
345         iput(inode);  /* raw_inode will be deleted through
346                          generic_delete_inode() */
347         goto failed;
348
349  failed_ifile_create_inode:
350         make_bad_inode(inode);
351         iput(inode);  /* if i_nlink == 1, generic_forget_inode() will be
352                          called */
353  failed:
354         return ERR_PTR(err);
355 }
356
357 void nilfs_free_inode(struct inode *inode)
358 {
359         struct super_block *sb = inode->i_sb;
360         struct nilfs_sb_info *sbi = NILFS_SB(sb);
361
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);
365 }
366
367 void nilfs_set_inode_flags(struct inode *inode)
368 {
369         unsigned int flags = NILFS_I(inode)->i_flags;
370
371         inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
372                             S_DIRSYNC);
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)
381 #endif
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);
387 }
388
389 int nilfs_read_inode_common(struct inode *inode,
390                             struct nilfs_inode *raw_inode)
391 {
392         struct nilfs_inode_info *ii = NILFS_I(inode);
393         int err;
394
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 */
408
409         inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
410         ii->i_flags = le32_to_cpu(raw_inode->i_flags);
411 #if 0
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);
415 #endif
416         ii->i_dir_start_lookup = 0;
417         inode->i_generation = le32_to_cpu(raw_inode->i_generation);
418
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);
422                 if (err < 0)
423                         return err;
424                 set_bit(NILFS_I_BMAP, &ii->i_state);
425                 /* No lock is needed; iget() ensures it. */
426         }
427         return 0;
428 }
429
430 static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
431                               struct inode *inode)
432 {
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;
437         int err;
438
439         down_read(&NILFS_MDT(dat)->mi_sem);     /* XXX */
440         err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
441         if (unlikely(err))
442                 goto bad_inode;
443
444         raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
445
446         err = nilfs_read_inode_common(inode, raw_inode);
447         if (err)
448                 goto failed_unmap;
449
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;
461         } else {
462                 inode->i_op = &nilfs_special_inode_operations;
463                 init_special_inode(
464                         inode, inode->i_mode,
465                         huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
466         }
467         nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
468         brelse(bh);
469         up_read(&NILFS_MDT(dat)->mi_sem);       /* XXX */
470         nilfs_set_inode_flags(inode);
471         return 0;
472
473  failed_unmap:
474         nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
475         brelse(bh);
476
477  bad_inode:
478         up_read(&NILFS_MDT(dat)->mi_sem);       /* XXX */
479         return err;
480 }
481
482 static int nilfs_iget_test(struct inode *inode, void *opaque)
483 {
484         struct nilfs_iget_args *args = opaque;
485         struct nilfs_inode_info *ii;
486
487         if (args->ino != inode->i_ino)
488                 return 0;
489
490         ii = NILFS_I(inode);
491         if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
492                 return !args->for_gc;
493
494         return args->for_gc && args->cno == ii->i_cno;
495 }
496
497 static int nilfs_iget_set(struct inode *inode, void *opaque)
498 {
499         struct nilfs_iget_args *args = opaque;
500
501         inode->i_ino = args->ino;
502         if (args->for_gc) {
503                 NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE;
504                 NILFS_I(inode)->i_cno = args->cno;
505         }
506         return 0;
507 }
508
509 struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
510 {
511         struct nilfs_iget_args args = { .ino = ino, .cno = 0, .for_gc = 0 };
512         struct inode *inode;
513         int err;
514
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))
519                 return inode;
520
521         err = __nilfs_read_inode(sb, ino, inode);
522         if (unlikely(err)) {
523                 iget_failed(inode);
524                 return ERR_PTR(err);
525         }
526         unlock_new_inode(inode);
527         return inode;
528 }
529
530 struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
531                                 __u64 cno)
532 {
533         struct nilfs_iget_args args = { .ino = ino, .cno = cno, .for_gc = 1 };
534         struct inode *inode;
535         int err;
536
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))
541                 return inode;
542
543         err = nilfs_init_gcinode(inode);
544         if (unlikely(err)) {
545                 iget_failed(inode);
546                 return ERR_PTR(err);
547         }
548         unlock_new_inode(inode);
549         return inode;
550 }
551
552 void nilfs_write_inode_common(struct inode *inode,
553                               struct nilfs_inode *raw_inode, int has_bmap)
554 {
555         struct nilfs_inode_info *ii = NILFS_I(inode);
556
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);
567
568         raw_inode->i_flags = cpu_to_le32(ii->i_flags);
569         raw_inode->i_generation = cpu_to_le32(inode->i_generation);
570
571         if (has_bmap)
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 */
578 }
579
580 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
581 {
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;
587
588         raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
589
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);
593
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
597                    before writing */
598         nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
599 }
600
601 #define NILFS_MAX_TRUNCATE_BLOCKS       16384  /* 64MB for 4KB block */
602
603 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
604                                 unsigned long from)
605 {
606         unsigned long b;
607         int ret;
608
609         if (!test_bit(NILFS_I_BMAP, &ii->i_state))
610                 return;
611  repeat:
612         ret = nilfs_bmap_last_key(ii->i_bmap, &b);
613         if (ret == -ENOENT)
614                 return;
615         else if (ret < 0)
616                 goto failed;
617
618         if (b < from)
619                 return;
620
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))
626                 goto repeat;
627
628  failed:
629         if (ret == -EINVAL)
630                 nilfs_error(ii->vfs_inode.i_sb, __func__,
631                             "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
632         else
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);
636 }
637
638 void nilfs_truncate(struct inode *inode)
639 {
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);
645
646         if (!test_bit(NILFS_I_BMAP, &ii->i_state))
647                 return;
648         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
649                 return;
650
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 */
654
655         block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
656
657         nilfs_truncate_bmap(ii, blkoff);
658
659         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
660         if (IS_SYNC(inode))
661                 nilfs_set_transaction_flag(NILFS_TI_SYNC);
662
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. */
668 }
669
670 static void nilfs_clear_inode(struct inode *inode)
671 {
672         struct nilfs_inode_info *ii = NILFS_I(inode);
673
674         /*
675          * Free resources allocated in nilfs_read_inode(), here.
676          */
677         BUG_ON(!list_empty(&ii->i_dirty));
678         brelse(ii->i_bh);
679         ii->i_bh = NULL;
680
681         if (test_bit(NILFS_I_BMAP, &ii->i_state))
682                 nilfs_bmap_clear(ii->i_bmap);
683
684         nilfs_btnode_cache_clear(&ii->i_btnode_cache);
685 }
686
687 void nilfs_evict_inode(struct inode *inode)
688 {
689         struct nilfs_transaction_info ti;
690         struct super_block *sb = inode->i_sb;
691         struct nilfs_inode_info *ii = NILFS_I(inode);
692
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);
698                 return;
699         }
700         nilfs_transaction_begin(sb, &ti, 0); /* never fails */
701
702         if (inode->i_data.nrpages)
703                 truncate_inode_pages(&inode->i_data, 0);
704
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 */
711         if (IS_SYNC(inode))
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. */
716 }
717
718 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
719 {
720         struct nilfs_transaction_info ti;
721         struct inode *inode = dentry->d_inode;
722         struct super_block *sb = inode->i_sb;
723         int err;
724
725         err = inode_change_ok(inode, iattr);
726         if (err)
727                 return err;
728
729         err = nilfs_transaction_begin(sb, &ti, 0);
730         if (unlikely(err))
731                 return err;
732
733         if ((iattr->ia_valid & ATTR_SIZE) &&
734             iattr->ia_size != i_size_read(inode)) {
735                 err = vmtruncate(inode, iattr->ia_size);
736                 if (unlikely(err))
737                         goto out_err;
738         }
739
740         setattr_copy(inode, iattr);
741         mark_inode_dirty(inode);
742
743         if (iattr->ia_valid & ATTR_MODE) {
744                 err = nilfs_acl_chmod(inode);
745                 if (unlikely(err))
746                         goto out_err;
747         }
748
749         return nilfs_transaction_commit(sb);
750
751 out_err:
752         nilfs_transaction_abort(sb);
753         return err;
754 }
755
756 int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
757                            struct buffer_head **pbh)
758 {
759         struct nilfs_inode_info *ii = NILFS_I(inode);
760         int err;
761
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,
766                                                   pbh);
767                 if (unlikely(err))
768                         return err;
769                 spin_lock(&sbi->s_inode_lock);
770                 if (ii->i_bh == NULL)
771                         ii->i_bh = *pbh;
772                 else {
773                         brelse(*pbh);
774                         *pbh = ii->i_bh;
775                 }
776         } else
777                 *pbh = ii->i_bh;
778
779         get_bh(*pbh);
780         spin_unlock(&sbi->s_inode_lock);
781         return 0;
782 }
783
784 int nilfs_inode_dirty(struct inode *inode)
785 {
786         struct nilfs_inode_info *ii = NILFS_I(inode);
787         struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
788         int ret = 0;
789
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);
795         }
796         return ret;
797 }
798
799 int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
800                          unsigned nr_dirty)
801 {
802         struct nilfs_inode_info *ii = NILFS_I(inode);
803
804         atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
805
806         if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
807                 return 0;
808
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
816                            this inode. */
817                         nilfs_warning(sbi->s_super, __func__,
818                                       "cannot get inode (ino=%lu)\n",
819                                       inode->i_ino);
820                         spin_unlock(&sbi->s_inode_lock);
821                         return -EINVAL; /* NILFS_I_DIRTY may remain for
822                                            freeing inode */
823                 }
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);
827         }
828         spin_unlock(&sbi->s_inode_lock);
829         return 0;
830 }
831
832 int nilfs_mark_inode_dirty(struct inode *inode)
833 {
834         struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
835         struct buffer_head *ibh;
836         int err;
837
838         err = nilfs_load_inode_block(sbi, inode, &ibh);
839         if (unlikely(err)) {
840                 nilfs_warning(inode->i_sb, __func__,
841                               "failed to reget inode block.\n");
842                 return err;
843         }
844         nilfs_update_inode(inode, ibh);
845         nilfs_mdt_mark_buffer_dirty(ibh);
846         nilfs_mdt_mark_dirty(sbi->s_ifile);
847         brelse(ibh);
848         return 0;
849 }
850
851 /**
852  * nilfs_dirty_inode - reflect changes on given inode to an inode block.
853  * @inode: inode of the file to be registered.
854  *
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.
860  */
861 void nilfs_dirty_inode(struct inode *inode)
862 {
863         struct nilfs_transaction_info ti;
864         struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
865
866         if (is_bad_inode(inode)) {
867                 nilfs_warning(inode->i_sb, __func__,
868                               "tried to mark bad_inode dirty. ignored.\n");
869                 dump_stack();
870                 return;
871         }
872         if (mdi) {
873                 nilfs_mdt_mark_dirty(inode);
874                 return;
875         }
876         nilfs_transaction_begin(inode->i_sb, &ti, 0);
877         nilfs_mark_inode_dirty(inode);
878         nilfs_transaction_commit(inode->i_sb); /* never fails */
879 }