]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nilfs2/inode.c
nilfs2: separate initializer of metadata file inode
[net-next-2.6.git] / fs / nilfs2 / inode.c
CommitLineData
05fe58fd
RK
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>
5a0e3ad6 25#include <linux/gfp.h>
05fe58fd
RK
26#include <linux/mpage.h>
27#include <linux/writeback.h>
f30bf3e4 28#include <linux/uio.h>
05fe58fd 29#include "nilfs.h"
6fd1e5c9 30#include "btnode.h"
05fe58fd
RK
31#include "segment.h"
32#include "page.h"
33#include "mdt.h"
34#include "cpfile.h"
35#include "ifile.h"
36
0e14a359
RK
37struct nilfs_iget_args {
38 u64 ino;
39 __u64 cno;
40 int for_gc;
41};
05fe58fd
RK
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.
05fe58fd
RK
53 */
54int 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);
c3a7abf0 58 __u64 blknum = 0;
05fe58fd
RK
59 int err = 0, ret;
60 struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
c3a7abf0 61 unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
05fe58fd 62
c3a7abf0
RK
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 */
05fe58fd 67 map_bh(bh_result, inode->i_sb, blknum);
c3a7abf0
RK
68 if (ret > 0)
69 bh_result->b_size = (ret << inode->i_blkbits);
05fe58fd
RK
70 goto out;
71 }
05fe58fd
RK
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);
05fe58fd
RK
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 */
1f5abe7e 90 printk(KERN_WARNING
05fe58fd
RK
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);
1f5abe7e 97 err = 0;
05fe58fd
RK
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 }
47420c79 104 nilfs_transaction_abort(inode->i_sb);
05fe58fd
RK
105 goto out;
106 }
abdb318b 107 nilfs_mark_inode_dirty(inode);
47420c79 108 nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fd
RK
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 */
131static 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 */
144static 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
150static int nilfs_writepages(struct address_space *mapping,
151 struct writeback_control *wbc)
152{
f30bf3e4
RK
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;
05fe58fd
RK
161}
162
163static 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
181static 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
195static 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
155130a4
CH
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
47420c79 213 nilfs_transaction_abort(inode->i_sb);
155130a4 214 }
05fe58fd
RK
215 return err;
216}
217
218static 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);
47420c79 232 err = nilfs_transaction_commit(inode->i_sb);
05fe58fd
RK
233 return err ? : copied;
234}
235
236static ssize_t
237nilfs_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;
05fe58fd
RK
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);
eafdc7d1
CH
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
05fe58fd
RK
263 return size;
264}
265
7f09410b 266const struct address_space_operations nilfs_aops = {
05fe58fd
RK
267 .writepage = nilfs_writepage,
268 .readpage = nilfs_readpage,
e85dc1d5 269 .sync_page = block_sync_page,
05fe58fd
RK
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,
258ef67e 278 .is_partially_uptodate = block_is_partially_uptodate,
05fe58fd
RK
279};
280
281struct 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);
73459dcc 306 inode_init_owner(inode, dir, mode);
05fe58fd
RK
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; */
05fe58fd 327 ii->i_dir_start_lookup = 0;
05fe58fd
RK
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
05fe58fd
RK
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
357void 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
05fe58fd
RK
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
367void 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
389int 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);
61239230
RK
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)
05fe58fd
RK
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
3cc811bf 416 ii->i_dir_start_lookup = 0;
05fe58fd
RK
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
05fe58fd
RK
430static 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
1b2f5a64
RK
446 err = nilfs_read_inode_common(inode, raw_inode);
447 if (err)
05fe58fd
RK
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;
05fe58fd
RK
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,
cdce214e 465 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
05fe58fd
RK
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
0e14a359
RK
482static 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
497static 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
05fe58fd
RK
509struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
510{
0e14a359 511 struct nilfs_iget_args args = { .ino = ino, .cno = 0, .for_gc = 0 };
05fe58fd
RK
512 struct inode *inode;
513 int err;
514
0e14a359 515 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
05fe58fd
RK
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
530void 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);
61239230
RK
542 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
543 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
05fe58fd
RK
544 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
545
05fe58fd
RK
546 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
547 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
548
549 if (has_bmap)
550 nilfs_bmap_write(ii->i_bmap, raw_inode);
551 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
552 raw_inode->i_device_code =
cdce214e 553 cpu_to_le64(huge_encode_dev(inode->i_rdev));
05fe58fd
RK
554 /* When extending inode, nilfs->ns_inode_size should be checked
555 for substitutions of appended fields */
556}
557
558void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
559{
560 ino_t ino = inode->i_ino;
561 struct nilfs_inode_info *ii = NILFS_I(inode);
562 struct super_block *sb = inode->i_sb;
563 struct nilfs_sb_info *sbi = NILFS_SB(sb);
564 struct nilfs_inode *raw_inode;
565
566 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
567
05fe58fd
RK
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
581static 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
616void 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);
05fe58fd
RK
623
624 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
625 return;
626 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
627 return;
628
629 blocksize = sb->s_blocksize;
630 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
1f5abe7e 631 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
05fe58fd
RK
632
633 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
634
635 nilfs_truncate_bmap(ii, blkoff);
636
637 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
638 if (IS_SYNC(inode))
639 nilfs_set_transaction_flag(NILFS_TI_SYNC);
640
abdb318b 641 nilfs_mark_inode_dirty(inode);
05fe58fd 642 nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
47420c79 643 nilfs_transaction_commit(sb);
05fe58fd
RK
644 /* May construct a logical segment and may fail in sync mode.
645 But truncate has no return value. */
646}
647
6fd1e5c9
AV
648static void nilfs_clear_inode(struct inode *inode)
649{
650 struct nilfs_inode_info *ii = NILFS_I(inode);
651
652 /*
653 * Free resources allocated in nilfs_read_inode(), here.
654 */
655 BUG_ON(!list_empty(&ii->i_dirty));
656 brelse(ii->i_bh);
657 ii->i_bh = NULL;
658
659 if (test_bit(NILFS_I_BMAP, &ii->i_state))
660 nilfs_bmap_clear(ii->i_bmap);
661
662 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
663}
664
665void nilfs_evict_inode(struct inode *inode)
05fe58fd
RK
666{
667 struct nilfs_transaction_info ti;
668 struct super_block *sb = inode->i_sb;
669 struct nilfs_inode_info *ii = NILFS_I(inode);
05fe58fd 670
6fd1e5c9 671 if (inode->i_nlink || unlikely(is_bad_inode(inode))) {
05fe58fd
RK
672 if (inode->i_data.nrpages)
673 truncate_inode_pages(&inode->i_data, 0);
6fd1e5c9
AV
674 end_writeback(inode);
675 nilfs_clear_inode(inode);
05fe58fd
RK
676 return;
677 }
1f5abe7e
RK
678 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
679
05fe58fd
RK
680 if (inode->i_data.nrpages)
681 truncate_inode_pages(&inode->i_data, 0);
682
683 nilfs_truncate_bmap(ii, 0);
abdb318b 684 nilfs_mark_inode_dirty(inode);
6fd1e5c9
AV
685 end_writeback(inode);
686 nilfs_clear_inode(inode);
05fe58fd
RK
687 nilfs_free_inode(inode);
688 /* nilfs_free_inode() marks inode buffer dirty */
689 if (IS_SYNC(inode))
690 nilfs_set_transaction_flag(NILFS_TI_SYNC);
47420c79 691 nilfs_transaction_commit(sb);
05fe58fd
RK
692 /* May construct a logical segment and may fail in sync mode.
693 But delete_inode has no return value. */
694}
695
696int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
697{
698 struct nilfs_transaction_info ti;
699 struct inode *inode = dentry->d_inode;
700 struct super_block *sb = inode->i_sb;
47420c79 701 int err;
05fe58fd
RK
702
703 err = inode_change_ok(inode, iattr);
704 if (err)
705 return err;
706
707 err = nilfs_transaction_begin(sb, &ti, 0);
708 if (unlikely(err))
709 return err;
1025774c
CH
710
711 if ((iattr->ia_valid & ATTR_SIZE) &&
712 iattr->ia_size != i_size_read(inode)) {
713 err = vmtruncate(inode, iattr->ia_size);
714 if (unlikely(err))
715 goto out_err;
716 }
717
718 setattr_copy(inode, iattr);
719 mark_inode_dirty(inode);
720
721 if (iattr->ia_valid & ATTR_MODE) {
05fe58fd 722 err = nilfs_acl_chmod(inode);
1025774c
CH
723 if (unlikely(err))
724 goto out_err;
725 }
726
727 return nilfs_transaction_commit(sb);
47420c79 728
1025774c
CH
729out_err:
730 nilfs_transaction_abort(sb);
47420c79 731 return err;
05fe58fd
RK
732}
733
734int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
735 struct buffer_head **pbh)
736{
737 struct nilfs_inode_info *ii = NILFS_I(inode);
738 int err;
739
740 spin_lock(&sbi->s_inode_lock);
05fe58fd
RK
741 if (ii->i_bh == NULL) {
742 spin_unlock(&sbi->s_inode_lock);
743 err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
744 pbh);
745 if (unlikely(err))
746 return err;
747 spin_lock(&sbi->s_inode_lock);
748 if (ii->i_bh == NULL)
749 ii->i_bh = *pbh;
750 else {
751 brelse(*pbh);
752 *pbh = ii->i_bh;
753 }
754 } else
755 *pbh = ii->i_bh;
756
757 get_bh(*pbh);
758 spin_unlock(&sbi->s_inode_lock);
759 return 0;
760}
761
762int nilfs_inode_dirty(struct inode *inode)
763{
764 struct nilfs_inode_info *ii = NILFS_I(inode);
765 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
766 int ret = 0;
767
768 if (!list_empty(&ii->i_dirty)) {
769 spin_lock(&sbi->s_inode_lock);
770 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
771 test_bit(NILFS_I_BUSY, &ii->i_state);
772 spin_unlock(&sbi->s_inode_lock);
773 }
774 return ret;
775}
776
777int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
778 unsigned nr_dirty)
779{
780 struct nilfs_inode_info *ii = NILFS_I(inode);
781
782 atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
783
458c5b08 784 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
05fe58fd
RK
785 return 0;
786
787 spin_lock(&sbi->s_inode_lock);
788 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
789 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
790 /* Because this routine may race with nilfs_dispose_list(),
791 we have to check NILFS_I_QUEUED here, too. */
792 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
793 /* This will happen when somebody is freeing
794 this inode. */
795 nilfs_warning(sbi->s_super, __func__,
796 "cannot get inode (ino=%lu)\n",
797 inode->i_ino);
798 spin_unlock(&sbi->s_inode_lock);
799 return -EINVAL; /* NILFS_I_DIRTY may remain for
800 freeing inode */
801 }
802 list_del(&ii->i_dirty);
803 list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
804 set_bit(NILFS_I_QUEUED, &ii->i_state);
805 }
806 spin_unlock(&sbi->s_inode_lock);
807 return 0;
808}
809
810int nilfs_mark_inode_dirty(struct inode *inode)
811{
812 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
813 struct buffer_head *ibh;
814 int err;
815
816 err = nilfs_load_inode_block(sbi, inode, &ibh);
817 if (unlikely(err)) {
818 nilfs_warning(inode->i_sb, __func__,
819 "failed to reget inode block.\n");
820 return err;
821 }
05fe58fd 822 nilfs_update_inode(inode, ibh);
05fe58fd
RK
823 nilfs_mdt_mark_buffer_dirty(ibh);
824 nilfs_mdt_mark_dirty(sbi->s_ifile);
825 brelse(ibh);
826 return 0;
827}
828
829/**
830 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
831 * @inode: inode of the file to be registered.
832 *
833 * nilfs_dirty_inode() loads a inode block containing the specified
834 * @inode and copies data from a nilfs_inode to a corresponding inode
835 * entry in the inode block. This operation is excluded from the segment
836 * construction. This function can be called both as a single operation
837 * and as a part of indivisible file operations.
838 */
839void nilfs_dirty_inode(struct inode *inode)
840{
841 struct nilfs_transaction_info ti;
7d6cd92f 842 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
05fe58fd
RK
843
844 if (is_bad_inode(inode)) {
845 nilfs_warning(inode->i_sb, __func__,
846 "tried to mark bad_inode dirty. ignored.\n");
847 dump_stack();
848 return;
849 }
7d6cd92f
RK
850 if (mdi) {
851 nilfs_mdt_mark_dirty(inode);
852 return;
853 }
05fe58fd 854 nilfs_transaction_begin(inode->i_sb, &ti, 0);
458c5b08 855 nilfs_mark_inode_dirty(inode);
47420c79 856 nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fd 857}