1dd9e6a7d787f0c96c7d85ae0e53cb18e9b17790
[pandora-kernel.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 "segment.h"
31 #include "page.h"
32 #include "mdt.h"
33 #include "cpfile.h"
34 #include "ifile.h"
35
36
37 /**
38  * nilfs_get_block() - get a file block on the filesystem (callback function)
39  * @inode - inode struct of the target file
40  * @blkoff - file block number
41  * @bh_result - buffer head to be mapped on
42  * @create - indicate whether allocating the block or not when it has not
43  *      been allocated yet.
44  *
45  * This function does not issue actual read request of the specified data
46  * block. It is done by VFS.
47  */
48 int nilfs_get_block(struct inode *inode, sector_t blkoff,
49                     struct buffer_head *bh_result, int create)
50 {
51         struct nilfs_inode_info *ii = NILFS_I(inode);
52         __u64 blknum = 0;
53         int err = 0, ret;
54         struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
55         unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
56
57         down_read(&NILFS_MDT(dat)->mi_sem);
58         ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
59         up_read(&NILFS_MDT(dat)->mi_sem);
60         if (ret >= 0) { /* found */
61                 map_bh(bh_result, inode->i_sb, blknum);
62                 if (ret > 0)
63                         bh_result->b_size = (ret << inode->i_blkbits);
64                 goto out;
65         }
66         /* data block was not found */
67         if (ret == -ENOENT && create) {
68                 struct nilfs_transaction_info ti;
69
70                 bh_result->b_blocknr = 0;
71                 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
72                 if (unlikely(err))
73                         goto out;
74                 err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
75                                         (unsigned long)bh_result);
76                 if (unlikely(err != 0)) {
77                         if (err == -EEXIST) {
78                                 /*
79                                  * The get_block() function could be called
80                                  * from multiple callers for an inode.
81                                  * However, the page having this block must
82                                  * be locked in this case.
83                                  */
84                                 printk(KERN_WARNING
85                                        "nilfs_get_block: a race condition "
86                                        "while inserting a data block. "
87                                        "(inode number=%lu, file block "
88                                        "offset=%llu)\n",
89                                        inode->i_ino,
90                                        (unsigned long long)blkoff);
91                                 err = 0;
92                         } else if (err == -EINVAL) {
93                                 nilfs_error(inode->i_sb, __func__,
94                                             "broken bmap (inode=%lu)\n",
95                                             inode->i_ino);
96                                 err = -EIO;
97                         }
98                         nilfs_transaction_abort(inode->i_sb);
99                         goto out;
100                 }
101                 nilfs_mark_inode_dirty(inode);
102                 nilfs_transaction_commit(inode->i_sb); /* never fails */
103                 /* Error handling should be detailed */
104                 set_buffer_new(bh_result);
105                 map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
106                                                       to proper value */
107         } else if (ret == -ENOENT) {
108                 /* not found is not error (e.g. hole); must return without
109                    the mapped state flag. */
110                 ;
111         } else {
112                 err = ret;
113         }
114
115  out:
116         return err;
117 }
118
119 /**
120  * nilfs_readpage() - implement readpage() method of nilfs_aops {}
121  * address_space_operations.
122  * @file - file struct of the file to be read
123  * @page - the page to be read
124  */
125 static int nilfs_readpage(struct file *file, struct page *page)
126 {
127         return mpage_readpage(page, nilfs_get_block);
128 }
129
130 /**
131  * nilfs_readpages() - implement readpages() method of nilfs_aops {}
132  * address_space_operations.
133  * @file - file struct of the file to be read
134  * @mapping - address_space struct used for reading multiple pages
135  * @pages - the pages to be read
136  * @nr_pages - number of pages to be read
137  */
138 static int nilfs_readpages(struct file *file, struct address_space *mapping,
139                            struct list_head *pages, unsigned nr_pages)
140 {
141         return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
142 }
143
144 static int nilfs_writepages(struct address_space *mapping,
145                             struct writeback_control *wbc)
146 {
147         struct inode *inode = mapping->host;
148         int err = 0;
149
150         if (wbc->sync_mode == WB_SYNC_ALL)
151                 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
152                                                     wbc->range_start,
153                                                     wbc->range_end);
154         return err;
155 }
156
157 static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
158 {
159         struct inode *inode = page->mapping->host;
160         int err;
161
162         redirty_page_for_writepage(wbc, page);
163         unlock_page(page);
164
165         if (wbc->sync_mode == WB_SYNC_ALL) {
166                 err = nilfs_construct_segment(inode->i_sb);
167                 if (unlikely(err))
168                         return err;
169         } else if (wbc->for_reclaim)
170                 nilfs_flush_segment(inode->i_sb, inode->i_ino);
171
172         return 0;
173 }
174
175 static int nilfs_set_page_dirty(struct page *page)
176 {
177         int ret = __set_page_dirty_buffers(page);
178
179         if (ret) {
180                 struct inode *inode = page->mapping->host;
181                 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
182                 unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
183
184                 nilfs_set_file_dirty(sbi, inode, nr_dirty);
185         }
186         return ret;
187 }
188
189 static int nilfs_write_begin(struct file *file, struct address_space *mapping,
190                              loff_t pos, unsigned len, unsigned flags,
191                              struct page **pagep, void **fsdata)
192
193 {
194         struct inode *inode = mapping->host;
195         int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
196
197         if (unlikely(err))
198                 return err;
199
200         *pagep = NULL;
201         err = block_write_begin(file, mapping, pos, len, flags, pagep,
202                                 fsdata, nilfs_get_block);
203         if (unlikely(err))
204                 nilfs_transaction_abort(inode->i_sb);
205         return err;
206 }
207
208 static int nilfs_write_end(struct file *file, struct address_space *mapping,
209                            loff_t pos, unsigned len, unsigned copied,
210                            struct page *page, void *fsdata)
211 {
212         struct inode *inode = mapping->host;
213         unsigned start = pos & (PAGE_CACHE_SIZE - 1);
214         unsigned nr_dirty;
215         int err;
216
217         nr_dirty = nilfs_page_count_clean_buffers(page, start,
218                                                   start + copied);
219         copied = generic_write_end(file, mapping, pos, len, copied, page,
220                                    fsdata);
221         nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
222         err = nilfs_transaction_commit(inode->i_sb);
223         return err ? : copied;
224 }
225
226 static ssize_t
227 nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
228                 loff_t offset, unsigned long nr_segs)
229 {
230         struct file *file = iocb->ki_filp;
231         struct inode *inode = file->f_mapping->host;
232         ssize_t size;
233
234         if (rw == WRITE)
235                 return 0;
236
237         /* Needs synchronization with the cleaner */
238         size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
239                                   offset, nr_segs, nilfs_get_block, NULL);
240
241         /*
242          * In case of error extending write may have instantiated a few
243          * blocks outside i_size. Trim these off again.
244          */
245         if (unlikely((rw & WRITE) && size < 0)) {
246                 loff_t isize = i_size_read(inode);
247                 loff_t end = offset + iov_length(iov, nr_segs);
248
249                 if (end > isize)
250                         vmtruncate(inode, isize);
251         }
252
253         return size;
254 }
255
256 const struct address_space_operations nilfs_aops = {
257         .writepage              = nilfs_writepage,
258         .readpage               = nilfs_readpage,
259         .sync_page              = block_sync_page,
260         .writepages             = nilfs_writepages,
261         .set_page_dirty         = nilfs_set_page_dirty,
262         .readpages              = nilfs_readpages,
263         .write_begin            = nilfs_write_begin,
264         .write_end              = nilfs_write_end,
265         /* .releasepage         = nilfs_releasepage, */
266         .invalidatepage         = block_invalidatepage,
267         .direct_IO              = nilfs_direct_IO,
268         .is_partially_uptodate  = block_is_partially_uptodate,
269 };
270
271 struct inode *nilfs_new_inode(struct inode *dir, int mode)
272 {
273         struct super_block *sb = dir->i_sb;
274         struct nilfs_sb_info *sbi = NILFS_SB(sb);
275         struct inode *inode;
276         struct nilfs_inode_info *ii;
277         int err = -ENOMEM;
278         ino_t ino;
279
280         inode = new_inode(sb);
281         if (unlikely(!inode))
282                 goto failed;
283
284         mapping_set_gfp_mask(inode->i_mapping,
285                              mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
286
287         ii = NILFS_I(inode);
288         ii->i_state = 1 << NILFS_I_NEW;
289
290         err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
291         if (unlikely(err))
292                 goto failed_ifile_create_inode;
293         /* reference count of i_bh inherits from nilfs_mdt_read_block() */
294
295         atomic_inc(&sbi->s_inodes_count);
296         inode_init_owner(inode, dir, mode);
297         inode->i_ino = ino;
298         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
299
300         if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
301                 err = nilfs_bmap_read(ii->i_bmap, NULL);
302                 if (err < 0)
303                         goto failed_bmap;
304
305                 set_bit(NILFS_I_BMAP, &ii->i_state);
306                 /* No lock is needed; iget() ensures it. */
307         }
308
309         ii->i_flags = NILFS_I(dir)->i_flags;
310         if (S_ISLNK(mode))
311                 ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
312         if (!S_ISDIR(mode))
313                 ii->i_flags &= ~NILFS_DIRSYNC_FL;
314
315         /* ii->i_file_acl = 0; */
316         /* ii->i_dir_acl = 0; */
317         ii->i_dir_start_lookup = 0;
318         ii->i_cno = 0;
319         nilfs_set_inode_flags(inode);
320         spin_lock(&sbi->s_next_gen_lock);
321         inode->i_generation = sbi->s_next_generation++;
322         spin_unlock(&sbi->s_next_gen_lock);
323         insert_inode_hash(inode);
324
325         err = nilfs_init_acl(inode, dir);
326         if (unlikely(err))
327                 goto failed_acl; /* never occur. When supporting
328                                     nilfs_init_acl(), proper cancellation of
329                                     above jobs should be considered */
330
331         return inode;
332
333  failed_acl:
334  failed_bmap:
335         inode->i_nlink = 0;
336         iput(inode);  /* raw_inode will be deleted through
337                          generic_delete_inode() */
338         goto failed;
339
340  failed_ifile_create_inode:
341         make_bad_inode(inode);
342         iput(inode);  /* if i_nlink == 1, generic_forget_inode() will be
343                          called */
344  failed:
345         return ERR_PTR(err);
346 }
347
348 void nilfs_free_inode(struct inode *inode)
349 {
350         struct super_block *sb = inode->i_sb;
351         struct nilfs_sb_info *sbi = NILFS_SB(sb);
352
353         clear_inode(inode);
354         /* XXX: check error code? Is there any thing I can do? */
355         (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
356         atomic_dec(&sbi->s_inodes_count);
357 }
358
359 void nilfs_set_inode_flags(struct inode *inode)
360 {
361         unsigned int flags = NILFS_I(inode)->i_flags;
362
363         inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
364                             S_DIRSYNC);
365         if (flags & NILFS_SYNC_FL)
366                 inode->i_flags |= S_SYNC;
367         if (flags & NILFS_APPEND_FL)
368                 inode->i_flags |= S_APPEND;
369         if (flags & NILFS_IMMUTABLE_FL)
370                 inode->i_flags |= S_IMMUTABLE;
371 #ifndef NILFS_ATIME_DISABLE
372         if (flags & NILFS_NOATIME_FL)
373 #endif
374                 inode->i_flags |= S_NOATIME;
375         if (flags & NILFS_DIRSYNC_FL)
376                 inode->i_flags |= S_DIRSYNC;
377         mapping_set_gfp_mask(inode->i_mapping,
378                              mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
379 }
380
381 int nilfs_read_inode_common(struct inode *inode,
382                             struct nilfs_inode *raw_inode)
383 {
384         struct nilfs_inode_info *ii = NILFS_I(inode);
385         int err;
386
387         inode->i_mode = le16_to_cpu(raw_inode->i_mode);
388         inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
389         inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
390         inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
391         inode->i_size = le64_to_cpu(raw_inode->i_size);
392         inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
393         inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
394         inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
395         inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
396         inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
397         inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
398         if (inode->i_nlink == 0 && inode->i_mode == 0)
399                 return -EINVAL; /* this inode is deleted */
400
401         inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
402         ii->i_flags = le32_to_cpu(raw_inode->i_flags);
403 #if 0
404         ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
405         ii->i_dir_acl = S_ISREG(inode->i_mode) ?
406                 0 : le32_to_cpu(raw_inode->i_dir_acl);
407 #endif
408         ii->i_dir_start_lookup = 0;
409         ii->i_cno = 0;
410         inode->i_generation = le32_to_cpu(raw_inode->i_generation);
411
412         if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
413             S_ISLNK(inode->i_mode)) {
414                 err = nilfs_bmap_read(ii->i_bmap, raw_inode);
415                 if (err < 0)
416                         return err;
417                 set_bit(NILFS_I_BMAP, &ii->i_state);
418                 /* No lock is needed; iget() ensures it. */
419         }
420         return 0;
421 }
422
423 static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
424                               struct inode *inode)
425 {
426         struct nilfs_sb_info *sbi = NILFS_SB(sb);
427         struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
428         struct buffer_head *bh;
429         struct nilfs_inode *raw_inode;
430         int err;
431
432         down_read(&NILFS_MDT(dat)->mi_sem);     /* XXX */
433         err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
434         if (unlikely(err))
435                 goto bad_inode;
436
437         raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
438
439         err = nilfs_read_inode_common(inode, raw_inode);
440         if (err)
441                 goto failed_unmap;
442
443         if (S_ISREG(inode->i_mode)) {
444                 inode->i_op = &nilfs_file_inode_operations;
445                 inode->i_fop = &nilfs_file_operations;
446                 inode->i_mapping->a_ops = &nilfs_aops;
447         } else if (S_ISDIR(inode->i_mode)) {
448                 inode->i_op = &nilfs_dir_inode_operations;
449                 inode->i_fop = &nilfs_dir_operations;
450                 inode->i_mapping->a_ops = &nilfs_aops;
451         } else if (S_ISLNK(inode->i_mode)) {
452                 inode->i_op = &nilfs_symlink_inode_operations;
453                 inode->i_mapping->a_ops = &nilfs_aops;
454         } else {
455                 inode->i_op = &nilfs_special_inode_operations;
456                 init_special_inode(
457                         inode, inode->i_mode,
458                         huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
459         }
460         nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
461         brelse(bh);
462         up_read(&NILFS_MDT(dat)->mi_sem);       /* XXX */
463         nilfs_set_inode_flags(inode);
464         return 0;
465
466  failed_unmap:
467         nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
468         brelse(bh);
469
470  bad_inode:
471         up_read(&NILFS_MDT(dat)->mi_sem);       /* XXX */
472         return err;
473 }
474
475 struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
476 {
477         struct inode *inode;
478         int err;
479
480         inode = iget_locked(sb, ino);
481         if (unlikely(!inode))
482                 return ERR_PTR(-ENOMEM);
483         if (!(inode->i_state & I_NEW))
484                 return inode;
485
486         err = __nilfs_read_inode(sb, ino, inode);
487         if (unlikely(err)) {
488                 iget_failed(inode);
489                 return ERR_PTR(err);
490         }
491         unlock_new_inode(inode);
492         return inode;
493 }
494
495 void nilfs_write_inode_common(struct inode *inode,
496                               struct nilfs_inode *raw_inode, int has_bmap)
497 {
498         struct nilfs_inode_info *ii = NILFS_I(inode);
499
500         raw_inode->i_mode = cpu_to_le16(inode->i_mode);
501         raw_inode->i_uid = cpu_to_le32(inode->i_uid);
502         raw_inode->i_gid = cpu_to_le32(inode->i_gid);
503         raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
504         raw_inode->i_size = cpu_to_le64(inode->i_size);
505         raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
506         raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
507         raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
508         raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
509         raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
510
511         raw_inode->i_flags = cpu_to_le32(ii->i_flags);
512         raw_inode->i_generation = cpu_to_le32(inode->i_generation);
513
514         if (has_bmap)
515                 nilfs_bmap_write(ii->i_bmap, raw_inode);
516         else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
517                 raw_inode->i_device_code =
518                         cpu_to_le64(huge_encode_dev(inode->i_rdev));
519         /* When extending inode, nilfs->ns_inode_size should be checked
520            for substitutions of appended fields */
521 }
522
523 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
524 {
525         ino_t ino = inode->i_ino;
526         struct nilfs_inode_info *ii = NILFS_I(inode);
527         struct super_block *sb = inode->i_sb;
528         struct nilfs_sb_info *sbi = NILFS_SB(sb);
529         struct nilfs_inode *raw_inode;
530
531         raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
532
533         if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
534                 memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
535         set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
536
537         nilfs_write_inode_common(inode, raw_inode, 0);
538                 /* XXX: call with has_bmap = 0 is a workaround to avoid
539                    deadlock of bmap. This delays update of i_bmap to just
540                    before writing */
541         nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
542 }
543
544 #define NILFS_MAX_TRUNCATE_BLOCKS       16384  /* 64MB for 4KB block */
545
546 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
547                                 unsigned long from)
548 {
549         unsigned long b;
550         int ret;
551
552         if (!test_bit(NILFS_I_BMAP, &ii->i_state))
553                 return;
554  repeat:
555         ret = nilfs_bmap_last_key(ii->i_bmap, &b);
556         if (ret == -ENOENT)
557                 return;
558         else if (ret < 0)
559                 goto failed;
560
561         if (b < from)
562                 return;
563
564         b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
565         ret = nilfs_bmap_truncate(ii->i_bmap, b);
566         nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
567         if (!ret || (ret == -ENOMEM &&
568                      nilfs_bmap_truncate(ii->i_bmap, b) == 0))
569                 goto repeat;
570
571  failed:
572         if (ret == -EINVAL)
573                 nilfs_error(ii->vfs_inode.i_sb, __func__,
574                             "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
575         else
576                 nilfs_warning(ii->vfs_inode.i_sb, __func__,
577                               "failed to truncate bmap (ino=%lu, err=%d)",
578                               ii->vfs_inode.i_ino, ret);
579 }
580
581 void nilfs_truncate(struct inode *inode)
582 {
583         unsigned long blkoff;
584         unsigned int blocksize;
585         struct nilfs_transaction_info ti;
586         struct super_block *sb = inode->i_sb;
587         struct nilfs_inode_info *ii = NILFS_I(inode);
588
589         if (!test_bit(NILFS_I_BMAP, &ii->i_state))
590                 return;
591         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
592                 return;
593
594         blocksize = sb->s_blocksize;
595         blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
596         nilfs_transaction_begin(sb, &ti, 0); /* never fails */
597
598         block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
599
600         nilfs_truncate_bmap(ii, blkoff);
601
602         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
603         if (IS_SYNC(inode))
604                 nilfs_set_transaction_flag(NILFS_TI_SYNC);
605
606         nilfs_mark_inode_dirty(inode);
607         nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
608         nilfs_transaction_commit(sb);
609         /* May construct a logical segment and may fail in sync mode.
610            But truncate has no return value. */
611 }
612
613 void nilfs_delete_inode(struct inode *inode)
614 {
615         struct nilfs_transaction_info ti;
616         struct super_block *sb = inode->i_sb;
617         struct nilfs_inode_info *ii = NILFS_I(inode);
618
619         if (unlikely(is_bad_inode(inode))) {
620                 if (inode->i_data.nrpages)
621                         truncate_inode_pages(&inode->i_data, 0);
622                 clear_inode(inode);
623                 return;
624         }
625         nilfs_transaction_begin(sb, &ti, 0); /* never fails */
626
627         if (inode->i_data.nrpages)
628                 truncate_inode_pages(&inode->i_data, 0);
629
630         nilfs_truncate_bmap(ii, 0);
631         nilfs_mark_inode_dirty(inode);
632         nilfs_free_inode(inode);
633         /* nilfs_free_inode() marks inode buffer dirty */
634         if (IS_SYNC(inode))
635                 nilfs_set_transaction_flag(NILFS_TI_SYNC);
636         nilfs_transaction_commit(sb);
637         /* May construct a logical segment and may fail in sync mode.
638            But delete_inode has no return value. */
639 }
640
641 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
642 {
643         struct nilfs_transaction_info ti;
644         struct inode *inode = dentry->d_inode;
645         struct super_block *sb = inode->i_sb;
646         int err;
647
648         err = inode_change_ok(inode, iattr);
649         if (err)
650                 return err;
651
652         err = nilfs_transaction_begin(sb, &ti, 0);
653         if (unlikely(err))
654                 return err;
655         err = inode_setattr(inode, iattr);
656         if (!err && (iattr->ia_valid & ATTR_MODE))
657                 err = nilfs_acl_chmod(inode);
658         if (likely(!err))
659                 err = nilfs_transaction_commit(sb);
660         else
661                 nilfs_transaction_abort(sb);
662
663         return err;
664 }
665
666 int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
667                            struct buffer_head **pbh)
668 {
669         struct nilfs_inode_info *ii = NILFS_I(inode);
670         int err;
671
672         spin_lock(&sbi->s_inode_lock);
673         if (ii->i_bh == NULL) {
674                 spin_unlock(&sbi->s_inode_lock);
675                 err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
676                                                   pbh);
677                 if (unlikely(err))
678                         return err;
679                 spin_lock(&sbi->s_inode_lock);
680                 if (ii->i_bh == NULL)
681                         ii->i_bh = *pbh;
682                 else {
683                         brelse(*pbh);
684                         *pbh = ii->i_bh;
685                 }
686         } else
687                 *pbh = ii->i_bh;
688
689         get_bh(*pbh);
690         spin_unlock(&sbi->s_inode_lock);
691         return 0;
692 }
693
694 int nilfs_inode_dirty(struct inode *inode)
695 {
696         struct nilfs_inode_info *ii = NILFS_I(inode);
697         struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
698         int ret = 0;
699
700         if (!list_empty(&ii->i_dirty)) {
701                 spin_lock(&sbi->s_inode_lock);
702                 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
703                         test_bit(NILFS_I_BUSY, &ii->i_state);
704                 spin_unlock(&sbi->s_inode_lock);
705         }
706         return ret;
707 }
708
709 int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
710                          unsigned nr_dirty)
711 {
712         struct nilfs_inode_info *ii = NILFS_I(inode);
713
714         atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
715
716         if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
717                 return 0;
718
719         spin_lock(&sbi->s_inode_lock);
720         if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
721             !test_bit(NILFS_I_BUSY, &ii->i_state)) {
722                 /* Because this routine may race with nilfs_dispose_list(),
723                    we have to check NILFS_I_QUEUED here, too. */
724                 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
725                         /* This will happen when somebody is freeing
726                            this inode. */
727                         nilfs_warning(sbi->s_super, __func__,
728                                       "cannot get inode (ino=%lu)\n",
729                                       inode->i_ino);
730                         spin_unlock(&sbi->s_inode_lock);
731                         return -EINVAL; /* NILFS_I_DIRTY may remain for
732                                            freeing inode */
733                 }
734                 list_del(&ii->i_dirty);
735                 list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
736                 set_bit(NILFS_I_QUEUED, &ii->i_state);
737         }
738         spin_unlock(&sbi->s_inode_lock);
739         return 0;
740 }
741
742 int nilfs_mark_inode_dirty(struct inode *inode)
743 {
744         struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
745         struct buffer_head *ibh;
746         int err;
747
748         err = nilfs_load_inode_block(sbi, inode, &ibh);
749         if (unlikely(err)) {
750                 nilfs_warning(inode->i_sb, __func__,
751                               "failed to reget inode block.\n");
752                 return err;
753         }
754         nilfs_update_inode(inode, ibh);
755         nilfs_mdt_mark_buffer_dirty(ibh);
756         nilfs_mdt_mark_dirty(sbi->s_ifile);
757         brelse(ibh);
758         return 0;
759 }
760
761 /**
762  * nilfs_dirty_inode - reflect changes on given inode to an inode block.
763  * @inode: inode of the file to be registered.
764  *
765  * nilfs_dirty_inode() loads a inode block containing the specified
766  * @inode and copies data from a nilfs_inode to a corresponding inode
767  * entry in the inode block. This operation is excluded from the segment
768  * construction. This function can be called both as a single operation
769  * and as a part of indivisible file operations.
770  */
771 void nilfs_dirty_inode(struct inode *inode)
772 {
773         struct nilfs_transaction_info ti;
774
775         if (is_bad_inode(inode)) {
776                 nilfs_warning(inode->i_sb, __func__,
777                               "tried to mark bad_inode dirty. ignored.\n");
778                 dump_stack();
779                 return;
780         }
781         nilfs_transaction_begin(inode->i_sb, &ti, 0);
782         nilfs_mark_inode_dirty(inode);
783         nilfs_transaction_commit(inode->i_sb); /* never fails */
784 }