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