blob: 7306fc7c49627c744b4d63864966820ccfcb11a0 [file] [log] [blame]
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070026#include <linux/mpage.h>
27#include <linux/writeback.h>
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -070028#include <linux/uio.h>
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070029#include "nilfs.h"
Al Viro6fd1e5c2010-06-07 11:55:00 -040030#include "btnode.h"
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070031#include "segment.h"
32#include "page.h"
33#include "mdt.h"
34#include "cpfile.h"
35#include "ifile.h"
36
Ryusuke Konishi0e14a352010-08-20 21:20:29 +090037struct nilfs_iget_args {
38 u64 ino;
39 __u64 cno;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +090040 struct nilfs_root *root;
Ryusuke Konishi0e14a352010-08-20 21:20:29 +090041 int for_gc;
42};
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070043
44/**
45 * nilfs_get_block() - get a file block on the filesystem (callback function)
46 * @inode - inode struct of the target file
47 * @blkoff - file block number
48 * @bh_result - buffer head to be mapped on
49 * @create - indicate whether allocating the block or not when it has not
50 * been allocated yet.
51 *
52 * This function does not issue actual read request of the specified data
53 * block. It is done by VFS.
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070054 */
55int nilfs_get_block(struct inode *inode, sector_t blkoff,
56 struct buffer_head *bh_result, int create)
57{
58 struct nilfs_inode_info *ii = NILFS_I(inode);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090059 __u64 blknum = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070060 int err = 0, ret;
61 struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090062 unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070063
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090064 down_read(&NILFS_MDT(dat)->mi_sem);
65 ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
66 up_read(&NILFS_MDT(dat)->mi_sem);
67 if (ret >= 0) { /* found */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070068 map_bh(bh_result, inode->i_sb, blknum);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090069 if (ret > 0)
70 bh_result->b_size = (ret << inode->i_blkbits);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070071 goto out;
72 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070073 /* data block was not found */
74 if (ret == -ENOENT && create) {
75 struct nilfs_transaction_info ti;
76
77 bh_result->b_blocknr = 0;
78 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
79 if (unlikely(err))
80 goto out;
81 err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
82 (unsigned long)bh_result);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070083 if (unlikely(err != 0)) {
84 if (err == -EEXIST) {
85 /*
86 * The get_block() function could be called
87 * from multiple callers for an inode.
88 * However, the page having this block must
89 * be locked in this case.
90 */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -070091 printk(KERN_WARNING
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070092 "nilfs_get_block: a race condition "
93 "while inserting a data block. "
94 "(inode number=%lu, file block "
95 "offset=%llu)\n",
96 inode->i_ino,
97 (unsigned long long)blkoff);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -070098 err = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070099 } else if (err == -EINVAL) {
100 nilfs_error(inode->i_sb, __func__,
101 "broken bmap (inode=%lu)\n",
102 inode->i_ino);
103 err = -EIO;
104 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700105 nilfs_transaction_abort(inode->i_sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700106 goto out;
107 }
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900108 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700109 nilfs_transaction_commit(inode->i_sb); /* never fails */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700110 /* Error handling should be detailed */
111 set_buffer_new(bh_result);
112 map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
113 to proper value */
114 } else if (ret == -ENOENT) {
115 /* not found is not error (e.g. hole); must return without
116 the mapped state flag. */
117 ;
118 } else {
119 err = ret;
120 }
121
122 out:
123 return err;
124}
125
126/**
127 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
128 * address_space_operations.
129 * @file - file struct of the file to be read
130 * @page - the page to be read
131 */
132static int nilfs_readpage(struct file *file, struct page *page)
133{
134 return mpage_readpage(page, nilfs_get_block);
135}
136
137/**
138 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
139 * address_space_operations.
140 * @file - file struct of the file to be read
141 * @mapping - address_space struct used for reading multiple pages
142 * @pages - the pages to be read
143 * @nr_pages - number of pages to be read
144 */
145static int nilfs_readpages(struct file *file, struct address_space *mapping,
146 struct list_head *pages, unsigned nr_pages)
147{
148 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
149}
150
151static int nilfs_writepages(struct address_space *mapping,
152 struct writeback_control *wbc)
153{
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700154 struct inode *inode = mapping->host;
155 int err = 0;
156
157 if (wbc->sync_mode == WB_SYNC_ALL)
158 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
159 wbc->range_start,
160 wbc->range_end);
161 return err;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700162}
163
164static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
165{
166 struct inode *inode = page->mapping->host;
167 int err;
168
169 redirty_page_for_writepage(wbc, page);
170 unlock_page(page);
171
172 if (wbc->sync_mode == WB_SYNC_ALL) {
173 err = nilfs_construct_segment(inode->i_sb);
174 if (unlikely(err))
175 return err;
176 } else if (wbc->for_reclaim)
177 nilfs_flush_segment(inode->i_sb, inode->i_ino);
178
179 return 0;
180}
181
182static int nilfs_set_page_dirty(struct page *page)
183{
184 int ret = __set_page_dirty_buffers(page);
185
186 if (ret) {
187 struct inode *inode = page->mapping->host;
188 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
189 unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
190
191 nilfs_set_file_dirty(sbi, inode, nr_dirty);
192 }
193 return ret;
194}
195
196static int nilfs_write_begin(struct file *file, struct address_space *mapping,
197 loff_t pos, unsigned len, unsigned flags,
198 struct page **pagep, void **fsdata)
199
200{
201 struct inode *inode = mapping->host;
202 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
203
204 if (unlikely(err))
205 return err;
206
Christoph Hellwig155130a2010-06-04 11:29:58 +0200207 err = block_write_begin(mapping, pos, len, flags, pagep,
208 nilfs_get_block);
209 if (unlikely(err)) {
210 loff_t isize = mapping->host->i_size;
211 if (pos + len > isize)
212 vmtruncate(mapping->host, isize);
213
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700214 nilfs_transaction_abort(inode->i_sb);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200215 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700216 return err;
217}
218
219static int nilfs_write_end(struct file *file, struct address_space *mapping,
220 loff_t pos, unsigned len, unsigned copied,
221 struct page *page, void *fsdata)
222{
223 struct inode *inode = mapping->host;
224 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
225 unsigned nr_dirty;
226 int err;
227
228 nr_dirty = nilfs_page_count_clean_buffers(page, start,
229 start + copied);
230 copied = generic_write_end(file, mapping, pos, len, copied, page,
231 fsdata);
232 nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700233 err = nilfs_transaction_commit(inode->i_sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700234 return err ? : copied;
235}
236
237static ssize_t
238nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
239 loff_t offset, unsigned long nr_segs)
240{
241 struct file *file = iocb->ki_filp;
242 struct inode *inode = file->f_mapping->host;
243 ssize_t size;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700244
245 if (rw == WRITE)
246 return 0;
247
248 /* Needs synchronization with the cleaner */
249 size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
250 offset, nr_segs, nilfs_get_block, NULL);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200251
252 /*
253 * In case of error extending write may have instantiated a few
254 * blocks outside i_size. Trim these off again.
255 */
256 if (unlikely((rw & WRITE) && size < 0)) {
257 loff_t isize = i_size_read(inode);
258 loff_t end = offset + iov_length(iov, nr_segs);
259
260 if (end > isize)
261 vmtruncate(inode, isize);
262 }
263
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700264 return size;
265}
266
Alexey Dobriyan7f094102009-09-21 17:01:10 -0700267const struct address_space_operations nilfs_aops = {
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700268 .writepage = nilfs_writepage,
269 .readpage = nilfs_readpage,
Ryusuke Konishie85dc1d2009-05-27 07:27:12 +0900270 .sync_page = block_sync_page,
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700271 .writepages = nilfs_writepages,
272 .set_page_dirty = nilfs_set_page_dirty,
273 .readpages = nilfs_readpages,
274 .write_begin = nilfs_write_begin,
275 .write_end = nilfs_write_end,
276 /* .releasepage = nilfs_releasepage, */
277 .invalidatepage = block_invalidatepage,
278 .direct_IO = nilfs_direct_IO,
Hisashi Hifumi258ef672009-05-13 11:19:40 +0900279 .is_partially_uptodate = block_is_partially_uptodate,
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700280};
281
282struct inode *nilfs_new_inode(struct inode *dir, int mode)
283{
284 struct super_block *sb = dir->i_sb;
285 struct nilfs_sb_info *sbi = NILFS_SB(sb);
286 struct inode *inode;
287 struct nilfs_inode_info *ii;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900288 struct nilfs_root *root;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700289 int err = -ENOMEM;
290 ino_t ino;
291
292 inode = new_inode(sb);
293 if (unlikely(!inode))
294 goto failed;
295
296 mapping_set_gfp_mask(inode->i_mapping,
297 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
298
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900299 root = NILFS_I(dir)->i_root;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700300 ii = NILFS_I(inode);
301 ii->i_state = 1 << NILFS_I_NEW;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900302 ii->i_root = root;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700303
304 err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
305 if (unlikely(err))
306 goto failed_ifile_create_inode;
307 /* reference count of i_bh inherits from nilfs_mdt_read_block() */
308
309 atomic_inc(&sbi->s_inodes_count);
Dmitry Monakhov73459dc2010-03-04 17:32:15 +0300310 inode_init_owner(inode, dir, mode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700311 inode->i_ino = ino;
312 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
313
314 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
315 err = nilfs_bmap_read(ii->i_bmap, NULL);
316 if (err < 0)
317 goto failed_bmap;
318
319 set_bit(NILFS_I_BMAP, &ii->i_state);
320 /* No lock is needed; iget() ensures it. */
321 }
322
323 ii->i_flags = NILFS_I(dir)->i_flags;
324 if (S_ISLNK(mode))
325 ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
326 if (!S_ISDIR(mode))
327 ii->i_flags &= ~NILFS_DIRSYNC_FL;
328
329 /* ii->i_file_acl = 0; */
330 /* ii->i_dir_acl = 0; */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700331 ii->i_dir_start_lookup = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700332 nilfs_set_inode_flags(inode);
333 spin_lock(&sbi->s_next_gen_lock);
334 inode->i_generation = sbi->s_next_generation++;
335 spin_unlock(&sbi->s_next_gen_lock);
336 insert_inode_hash(inode);
337
338 err = nilfs_init_acl(inode, dir);
339 if (unlikely(err))
340 goto failed_acl; /* never occur. When supporting
341 nilfs_init_acl(), proper cancellation of
342 above jobs should be considered */
343
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700344 return inode;
345
346 failed_acl:
347 failed_bmap:
348 inode->i_nlink = 0;
349 iput(inode); /* raw_inode will be deleted through
350 generic_delete_inode() */
351 goto failed;
352
353 failed_ifile_create_inode:
354 make_bad_inode(inode);
355 iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
356 called */
357 failed:
358 return ERR_PTR(err);
359}
360
361void nilfs_free_inode(struct inode *inode)
362{
363 struct super_block *sb = inode->i_sb;
364 struct nilfs_sb_info *sbi = NILFS_SB(sb);
365
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700366 /* XXX: check error code? Is there any thing I can do? */
367 (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
368 atomic_dec(&sbi->s_inodes_count);
369}
370
371void nilfs_set_inode_flags(struct inode *inode)
372{
373 unsigned int flags = NILFS_I(inode)->i_flags;
374
375 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
376 S_DIRSYNC);
377 if (flags & NILFS_SYNC_FL)
378 inode->i_flags |= S_SYNC;
379 if (flags & NILFS_APPEND_FL)
380 inode->i_flags |= S_APPEND;
381 if (flags & NILFS_IMMUTABLE_FL)
382 inode->i_flags |= S_IMMUTABLE;
383#ifndef NILFS_ATIME_DISABLE
384 if (flags & NILFS_NOATIME_FL)
385#endif
386 inode->i_flags |= S_NOATIME;
387 if (flags & NILFS_DIRSYNC_FL)
388 inode->i_flags |= S_DIRSYNC;
389 mapping_set_gfp_mask(inode->i_mapping,
390 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
391}
392
393int nilfs_read_inode_common(struct inode *inode,
394 struct nilfs_inode *raw_inode)
395{
396 struct nilfs_inode_info *ii = NILFS_I(inode);
397 int err;
398
399 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
400 inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
401 inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
402 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
403 inode->i_size = le64_to_cpu(raw_inode->i_size);
404 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
405 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
406 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
Ryusuke Konishi61239232009-04-06 19:02:00 -0700407 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
408 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
409 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
410 if (inode->i_nlink == 0 && inode->i_mode == 0)
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700411 return -EINVAL; /* this inode is deleted */
412
413 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
414 ii->i_flags = le32_to_cpu(raw_inode->i_flags);
415#if 0
416 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
417 ii->i_dir_acl = S_ISREG(inode->i_mode) ?
418 0 : le32_to_cpu(raw_inode->i_dir_acl);
419#endif
Ryusuke Konishi3cc811b2009-09-28 13:02:46 +0900420 ii->i_dir_start_lookup = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700421 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
422
423 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
424 S_ISLNK(inode->i_mode)) {
425 err = nilfs_bmap_read(ii->i_bmap, raw_inode);
426 if (err < 0)
427 return err;
428 set_bit(NILFS_I_BMAP, &ii->i_state);
429 /* No lock is needed; iget() ensures it. */
430 }
431 return 0;
432}
433
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700434static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
435 struct inode *inode)
436{
437 struct nilfs_sb_info *sbi = NILFS_SB(sb);
438 struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
439 struct buffer_head *bh;
440 struct nilfs_inode *raw_inode;
441 int err;
442
443 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
444 err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
445 if (unlikely(err))
446 goto bad_inode;
447
448 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
449
Ryusuke Konishi1b2f5a62009-08-22 19:10:07 +0900450 err = nilfs_read_inode_common(inode, raw_inode);
451 if (err)
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700452 goto failed_unmap;
453
454 if (S_ISREG(inode->i_mode)) {
455 inode->i_op = &nilfs_file_inode_operations;
456 inode->i_fop = &nilfs_file_operations;
457 inode->i_mapping->a_ops = &nilfs_aops;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700458 } else if (S_ISDIR(inode->i_mode)) {
459 inode->i_op = &nilfs_dir_inode_operations;
460 inode->i_fop = &nilfs_dir_operations;
461 inode->i_mapping->a_ops = &nilfs_aops;
462 } else if (S_ISLNK(inode->i_mode)) {
463 inode->i_op = &nilfs_symlink_inode_operations;
464 inode->i_mapping->a_ops = &nilfs_aops;
465 } else {
466 inode->i_op = &nilfs_special_inode_operations;
467 init_special_inode(
468 inode, inode->i_mode,
Ryusuke Konishicdce2142010-05-09 15:31:22 +0900469 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700470 }
471 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
472 brelse(bh);
473 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
474 nilfs_set_inode_flags(inode);
475 return 0;
476
477 failed_unmap:
478 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
479 brelse(bh);
480
481 bad_inode:
482 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
483 return err;
484}
485
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900486static int nilfs_iget_test(struct inode *inode, void *opaque)
487{
488 struct nilfs_iget_args *args = opaque;
489 struct nilfs_inode_info *ii;
490
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900491 if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900492 return 0;
493
494 ii = NILFS_I(inode);
495 if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
496 return !args->for_gc;
497
498 return args->for_gc && args->cno == ii->i_cno;
499}
500
501static int nilfs_iget_set(struct inode *inode, void *opaque)
502{
503 struct nilfs_iget_args *args = opaque;
504
505 inode->i_ino = args->ino;
506 if (args->for_gc) {
507 NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE;
508 NILFS_I(inode)->i_cno = args->cno;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900509 NILFS_I(inode)->i_root = NULL;
510 } else {
511 if (args->root && args->ino == NILFS_ROOT_INO)
512 nilfs_get_root(args->root);
513 NILFS_I(inode)->i_root = args->root;
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900514 }
515 return 0;
516}
517
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900518struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
519 unsigned long ino)
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700520{
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900521 struct nilfs_iget_args args = {
522 .ino = ino, .root = root, .cno = 0, .for_gc = 0
523 };
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700524 struct inode *inode;
525 int err;
526
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900527 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700528 if (unlikely(!inode))
529 return ERR_PTR(-ENOMEM);
530 if (!(inode->i_state & I_NEW))
531 return inode;
532
533 err = __nilfs_read_inode(sb, ino, inode);
534 if (unlikely(err)) {
535 iget_failed(inode);
536 return ERR_PTR(err);
537 }
538 unlock_new_inode(inode);
539 return inode;
540}
541
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900542struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
543 __u64 cno)
544{
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900545 struct nilfs_iget_args args = {
546 .ino = ino, .root = NULL, .cno = cno, .for_gc = 1
547 };
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900548 struct inode *inode;
549 int err;
550
551 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
552 if (unlikely(!inode))
553 return ERR_PTR(-ENOMEM);
554 if (!(inode->i_state & I_NEW))
555 return inode;
556
557 err = nilfs_init_gcinode(inode);
558 if (unlikely(err)) {
559 iget_failed(inode);
560 return ERR_PTR(err);
561 }
562 unlock_new_inode(inode);
563 return inode;
564}
565
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700566void nilfs_write_inode_common(struct inode *inode,
567 struct nilfs_inode *raw_inode, int has_bmap)
568{
569 struct nilfs_inode_info *ii = NILFS_I(inode);
570
571 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
572 raw_inode->i_uid = cpu_to_le32(inode->i_uid);
573 raw_inode->i_gid = cpu_to_le32(inode->i_gid);
574 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
575 raw_inode->i_size = cpu_to_le64(inode->i_size);
576 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
577 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
Ryusuke Konishi61239232009-04-06 19:02:00 -0700578 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
579 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700580 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
581
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700582 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
583 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
584
585 if (has_bmap)
586 nilfs_bmap_write(ii->i_bmap, raw_inode);
587 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
588 raw_inode->i_device_code =
Ryusuke Konishicdce2142010-05-09 15:31:22 +0900589 cpu_to_le64(huge_encode_dev(inode->i_rdev));
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700590 /* When extending inode, nilfs->ns_inode_size should be checked
591 for substitutions of appended fields */
592}
593
594void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
595{
596 ino_t ino = inode->i_ino;
597 struct nilfs_inode_info *ii = NILFS_I(inode);
598 struct super_block *sb = inode->i_sb;
599 struct nilfs_sb_info *sbi = NILFS_SB(sb);
600 struct nilfs_inode *raw_inode;
601
602 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
603
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700604 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
605 memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
606 set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
607
608 nilfs_write_inode_common(inode, raw_inode, 0);
609 /* XXX: call with has_bmap = 0 is a workaround to avoid
610 deadlock of bmap. This delays update of i_bmap to just
611 before writing */
612 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
613}
614
615#define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
616
617static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
618 unsigned long from)
619{
620 unsigned long b;
621 int ret;
622
623 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
624 return;
625 repeat:
626 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
627 if (ret == -ENOENT)
628 return;
629 else if (ret < 0)
630 goto failed;
631
632 if (b < from)
633 return;
634
635 b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
636 ret = nilfs_bmap_truncate(ii->i_bmap, b);
637 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
638 if (!ret || (ret == -ENOMEM &&
639 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
640 goto repeat;
641
642 failed:
643 if (ret == -EINVAL)
644 nilfs_error(ii->vfs_inode.i_sb, __func__,
645 "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
646 else
647 nilfs_warning(ii->vfs_inode.i_sb, __func__,
648 "failed to truncate bmap (ino=%lu, err=%d)",
649 ii->vfs_inode.i_ino, ret);
650}
651
652void nilfs_truncate(struct inode *inode)
653{
654 unsigned long blkoff;
655 unsigned int blocksize;
656 struct nilfs_transaction_info ti;
657 struct super_block *sb = inode->i_sb;
658 struct nilfs_inode_info *ii = NILFS_I(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700659
660 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
661 return;
662 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
663 return;
664
665 blocksize = sb->s_blocksize;
666 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700667 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700668
669 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
670
671 nilfs_truncate_bmap(ii, blkoff);
672
673 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
674 if (IS_SYNC(inode))
675 nilfs_set_transaction_flag(NILFS_TI_SYNC);
676
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900677 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700678 nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700679 nilfs_transaction_commit(sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700680 /* May construct a logical segment and may fail in sync mode.
681 But truncate has no return value. */
682}
683
Al Viro6fd1e5c2010-06-07 11:55:00 -0400684static void nilfs_clear_inode(struct inode *inode)
685{
686 struct nilfs_inode_info *ii = NILFS_I(inode);
687
688 /*
689 * Free resources allocated in nilfs_read_inode(), here.
690 */
691 BUG_ON(!list_empty(&ii->i_dirty));
692 brelse(ii->i_bh);
693 ii->i_bh = NULL;
694
695 if (test_bit(NILFS_I_BMAP, &ii->i_state))
696 nilfs_bmap_clear(ii->i_bmap);
697
698 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900699
700 if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
701 nilfs_put_root(ii->i_root);
Al Viro6fd1e5c2010-06-07 11:55:00 -0400702}
703
704void nilfs_evict_inode(struct inode *inode)
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700705{
706 struct nilfs_transaction_info ti;
707 struct super_block *sb = inode->i_sb;
708 struct nilfs_inode_info *ii = NILFS_I(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700709
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900710 if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700711 if (inode->i_data.nrpages)
712 truncate_inode_pages(&inode->i_data, 0);
Al Viro6fd1e5c2010-06-07 11:55:00 -0400713 end_writeback(inode);
714 nilfs_clear_inode(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700715 return;
716 }
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700717 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
718
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700719 if (inode->i_data.nrpages)
720 truncate_inode_pages(&inode->i_data, 0);
721
722 nilfs_truncate_bmap(ii, 0);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900723 nilfs_mark_inode_dirty(inode);
Al Viro6fd1e5c2010-06-07 11:55:00 -0400724 end_writeback(inode);
725 nilfs_clear_inode(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700726 nilfs_free_inode(inode);
727 /* nilfs_free_inode() marks inode buffer dirty */
728 if (IS_SYNC(inode))
729 nilfs_set_transaction_flag(NILFS_TI_SYNC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700730 nilfs_transaction_commit(sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700731 /* May construct a logical segment and may fail in sync mode.
732 But delete_inode has no return value. */
733}
734
735int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
736{
737 struct nilfs_transaction_info ti;
738 struct inode *inode = dentry->d_inode;
739 struct super_block *sb = inode->i_sb;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700740 int err;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700741
742 err = inode_change_ok(inode, iattr);
743 if (err)
744 return err;
745
746 err = nilfs_transaction_begin(sb, &ti, 0);
747 if (unlikely(err))
748 return err;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700749
Christoph Hellwig10257742010-06-04 11:30:02 +0200750 if ((iattr->ia_valid & ATTR_SIZE) &&
751 iattr->ia_size != i_size_read(inode)) {
752 err = vmtruncate(inode, iattr->ia_size);
753 if (unlikely(err))
754 goto out_err;
755 }
756
757 setattr_copy(inode, iattr);
758 mark_inode_dirty(inode);
759
760 if (iattr->ia_valid & ATTR_MODE) {
761 err = nilfs_acl_chmod(inode);
762 if (unlikely(err))
763 goto out_err;
764 }
765
766 return nilfs_transaction_commit(sb);
767
768out_err:
769 nilfs_transaction_abort(sb);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700770 return err;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700771}
772
773int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
774 struct buffer_head **pbh)
775{
776 struct nilfs_inode_info *ii = NILFS_I(inode);
777 int err;
778
779 spin_lock(&sbi->s_inode_lock);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700780 if (ii->i_bh == NULL) {
781 spin_unlock(&sbi->s_inode_lock);
782 err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
783 pbh);
784 if (unlikely(err))
785 return err;
786 spin_lock(&sbi->s_inode_lock);
787 if (ii->i_bh == NULL)
788 ii->i_bh = *pbh;
789 else {
790 brelse(*pbh);
791 *pbh = ii->i_bh;
792 }
793 } else
794 *pbh = ii->i_bh;
795
796 get_bh(*pbh);
797 spin_unlock(&sbi->s_inode_lock);
798 return 0;
799}
800
801int nilfs_inode_dirty(struct inode *inode)
802{
803 struct nilfs_inode_info *ii = NILFS_I(inode);
804 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
805 int ret = 0;
806
807 if (!list_empty(&ii->i_dirty)) {
808 spin_lock(&sbi->s_inode_lock);
809 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
810 test_bit(NILFS_I_BUSY, &ii->i_state);
811 spin_unlock(&sbi->s_inode_lock);
812 }
813 return ret;
814}
815
816int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
817 unsigned nr_dirty)
818{
819 struct nilfs_inode_info *ii = NILFS_I(inode);
820
821 atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
822
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700823 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700824 return 0;
825
826 spin_lock(&sbi->s_inode_lock);
827 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
828 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
829 /* Because this routine may race with nilfs_dispose_list(),
830 we have to check NILFS_I_QUEUED here, too. */
831 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
832 /* This will happen when somebody is freeing
833 this inode. */
834 nilfs_warning(sbi->s_super, __func__,
835 "cannot get inode (ino=%lu)\n",
836 inode->i_ino);
837 spin_unlock(&sbi->s_inode_lock);
838 return -EINVAL; /* NILFS_I_DIRTY may remain for
839 freeing inode */
840 }
841 list_del(&ii->i_dirty);
842 list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
843 set_bit(NILFS_I_QUEUED, &ii->i_state);
844 }
845 spin_unlock(&sbi->s_inode_lock);
846 return 0;
847}
848
849int nilfs_mark_inode_dirty(struct inode *inode)
850{
851 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
852 struct buffer_head *ibh;
853 int err;
854
855 err = nilfs_load_inode_block(sbi, inode, &ibh);
856 if (unlikely(err)) {
857 nilfs_warning(inode->i_sb, __func__,
858 "failed to reget inode block.\n");
859 return err;
860 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700861 nilfs_update_inode(inode, ibh);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700862 nilfs_mdt_mark_buffer_dirty(ibh);
863 nilfs_mdt_mark_dirty(sbi->s_ifile);
864 brelse(ibh);
865 return 0;
866}
867
868/**
869 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
870 * @inode: inode of the file to be registered.
871 *
872 * nilfs_dirty_inode() loads a inode block containing the specified
873 * @inode and copies data from a nilfs_inode to a corresponding inode
874 * entry in the inode block. This operation is excluded from the segment
875 * construction. This function can be called both as a single operation
876 * and as a part of indivisible file operations.
877 */
878void nilfs_dirty_inode(struct inode *inode)
879{
880 struct nilfs_transaction_info ti;
Ryusuke Konishi7d6cd922010-08-21 00:30:39 +0900881 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700882
883 if (is_bad_inode(inode)) {
884 nilfs_warning(inode->i_sb, __func__,
885 "tried to mark bad_inode dirty. ignored.\n");
886 dump_stack();
887 return;
888 }
Ryusuke Konishi7d6cd922010-08-21 00:30:39 +0900889 if (mdi) {
890 nilfs_mdt_mark_dirty(inode);
891 return;
892 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700893 nilfs_transaction_begin(inode->i_sb, &ti, 0);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700894 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700895 nilfs_transaction_commit(inode->i_sb); /* never fails */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700896}