blob: 82cfdbc43e1cd4ca4b9825fe5f44dab288615e52 [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;
40 int for_gc;
41};
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070042
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.
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070053 */
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);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090058 __u64 blknum = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070059 int err = 0, ret;
60 struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090061 unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070062
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090063 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 */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070067 map_bh(bh_result, inode->i_sb, blknum);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090068 if (ret > 0)
69 bh_result->b_size = (ret << inode->i_blkbits);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070070 goto out;
71 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070072 /* 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);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070082 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 */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -070090 printk(KERN_WARNING
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070091 "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);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -070097 err = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -070098 } 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 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700104 nilfs_transaction_abort(inode->i_sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700105 goto out;
106 }
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900107 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700108 nilfs_transaction_commit(inode->i_sb); /* never fails */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700109 /* 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{
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700153 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;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700161}
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
Christoph Hellwig155130a2010-06-04 11:29:58 +0200206 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
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700213 nilfs_transaction_abort(inode->i_sb);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200214 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700215 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);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700232 err = nilfs_transaction_commit(inode->i_sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700233 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;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700243
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);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200250
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
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700263 return size;
264}
265
Alexey Dobriyan7f094102009-09-21 17:01:10 -0700266const struct address_space_operations nilfs_aops = {
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700267 .writepage = nilfs_writepage,
268 .readpage = nilfs_readpage,
Ryusuke Konishie85dc1d2009-05-27 07:27:12 +0900269 .sync_page = block_sync_page,
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700270 .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,
Hisashi Hifumi258ef672009-05-13 11:19:40 +0900278 .is_partially_uptodate = block_is_partially_uptodate,
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700279};
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);
Dmitry Monakhov73459dc2010-03-04 17:32:15 +0300306 inode_init_owner(inode, dir, mode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700307 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; */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700327 ii->i_dir_start_lookup = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700328 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
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700340 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
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700362 /* 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);
Ryusuke Konishi61239232009-04-06 19:02:00 -0700403 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)
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700407 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
Ryusuke Konishi3cc811b2009-09-28 13:02:46 +0900416 ii->i_dir_start_lookup = 0;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700417 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
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700430static 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
Ryusuke Konishi1b2f5a62009-08-22 19:10:07 +0900446 err = nilfs_read_inode_common(inode, raw_inode);
447 if (err)
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700448 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;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700454 } 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,
Ryusuke Konishicdce2142010-05-09 15:31:22 +0900465 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700466 }
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
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900482static 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
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700509struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
510{
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900511 struct nilfs_iget_args args = { .ino = ino, .cno = 0, .for_gc = 0 };
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700512 struct inode *inode;
513 int err;
514
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900515 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700516 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
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900530struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
531 __u64 cno)
532{
533 struct nilfs_iget_args args = { .ino = ino, .cno = cno, .for_gc = 1 };
534 struct inode *inode;
535 int err;
536
537 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
538 if (unlikely(!inode))
539 return ERR_PTR(-ENOMEM);
540 if (!(inode->i_state & I_NEW))
541 return inode;
542
543 err = nilfs_init_gcinode(inode);
544 if (unlikely(err)) {
545 iget_failed(inode);
546 return ERR_PTR(err);
547 }
548 unlock_new_inode(inode);
549 return inode;
550}
551
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700552void nilfs_write_inode_common(struct inode *inode,
553 struct nilfs_inode *raw_inode, int has_bmap)
554{
555 struct nilfs_inode_info *ii = NILFS_I(inode);
556
557 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
558 raw_inode->i_uid = cpu_to_le32(inode->i_uid);
559 raw_inode->i_gid = cpu_to_le32(inode->i_gid);
560 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
561 raw_inode->i_size = cpu_to_le64(inode->i_size);
562 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
563 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
Ryusuke Konishi61239232009-04-06 19:02:00 -0700564 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
565 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700566 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
567
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700568 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
569 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
570
571 if (has_bmap)
572 nilfs_bmap_write(ii->i_bmap, raw_inode);
573 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
574 raw_inode->i_device_code =
Ryusuke Konishicdce2142010-05-09 15:31:22 +0900575 cpu_to_le64(huge_encode_dev(inode->i_rdev));
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700576 /* When extending inode, nilfs->ns_inode_size should be checked
577 for substitutions of appended fields */
578}
579
580void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
581{
582 ino_t ino = inode->i_ino;
583 struct nilfs_inode_info *ii = NILFS_I(inode);
584 struct super_block *sb = inode->i_sb;
585 struct nilfs_sb_info *sbi = NILFS_SB(sb);
586 struct nilfs_inode *raw_inode;
587
588 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
589
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700590 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
591 memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
592 set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
593
594 nilfs_write_inode_common(inode, raw_inode, 0);
595 /* XXX: call with has_bmap = 0 is a workaround to avoid
596 deadlock of bmap. This delays update of i_bmap to just
597 before writing */
598 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
599}
600
601#define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
602
603static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
604 unsigned long from)
605{
606 unsigned long b;
607 int ret;
608
609 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
610 return;
611 repeat:
612 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
613 if (ret == -ENOENT)
614 return;
615 else if (ret < 0)
616 goto failed;
617
618 if (b < from)
619 return;
620
621 b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
622 ret = nilfs_bmap_truncate(ii->i_bmap, b);
623 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
624 if (!ret || (ret == -ENOMEM &&
625 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
626 goto repeat;
627
628 failed:
629 if (ret == -EINVAL)
630 nilfs_error(ii->vfs_inode.i_sb, __func__,
631 "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
632 else
633 nilfs_warning(ii->vfs_inode.i_sb, __func__,
634 "failed to truncate bmap (ino=%lu, err=%d)",
635 ii->vfs_inode.i_ino, ret);
636}
637
638void nilfs_truncate(struct inode *inode)
639{
640 unsigned long blkoff;
641 unsigned int blocksize;
642 struct nilfs_transaction_info ti;
643 struct super_block *sb = inode->i_sb;
644 struct nilfs_inode_info *ii = NILFS_I(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700645
646 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
647 return;
648 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
649 return;
650
651 blocksize = sb->s_blocksize;
652 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700653 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700654
655 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
656
657 nilfs_truncate_bmap(ii, blkoff);
658
659 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
660 if (IS_SYNC(inode))
661 nilfs_set_transaction_flag(NILFS_TI_SYNC);
662
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900663 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700664 nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700665 nilfs_transaction_commit(sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700666 /* May construct a logical segment and may fail in sync mode.
667 But truncate has no return value. */
668}
669
Al Viro6fd1e5c2010-06-07 11:55:00 -0400670static void nilfs_clear_inode(struct inode *inode)
671{
672 struct nilfs_inode_info *ii = NILFS_I(inode);
673
674 /*
675 * Free resources allocated in nilfs_read_inode(), here.
676 */
677 BUG_ON(!list_empty(&ii->i_dirty));
678 brelse(ii->i_bh);
679 ii->i_bh = NULL;
680
681 if (test_bit(NILFS_I_BMAP, &ii->i_state))
682 nilfs_bmap_clear(ii->i_bmap);
683
684 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
685}
686
687void nilfs_evict_inode(struct inode *inode)
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700688{
689 struct nilfs_transaction_info ti;
690 struct super_block *sb = inode->i_sb;
691 struct nilfs_inode_info *ii = NILFS_I(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700692
Al Viro6fd1e5c2010-06-07 11:55:00 -0400693 if (inode->i_nlink || unlikely(is_bad_inode(inode))) {
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700694 if (inode->i_data.nrpages)
695 truncate_inode_pages(&inode->i_data, 0);
Al Viro6fd1e5c2010-06-07 11:55:00 -0400696 end_writeback(inode);
697 nilfs_clear_inode(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700698 return;
699 }
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700700 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
701
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700702 if (inode->i_data.nrpages)
703 truncate_inode_pages(&inode->i_data, 0);
704
705 nilfs_truncate_bmap(ii, 0);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900706 nilfs_mark_inode_dirty(inode);
Al Viro6fd1e5c2010-06-07 11:55:00 -0400707 end_writeback(inode);
708 nilfs_clear_inode(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700709 nilfs_free_inode(inode);
710 /* nilfs_free_inode() marks inode buffer dirty */
711 if (IS_SYNC(inode))
712 nilfs_set_transaction_flag(NILFS_TI_SYNC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700713 nilfs_transaction_commit(sb);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700714 /* May construct a logical segment and may fail in sync mode.
715 But delete_inode has no return value. */
716}
717
718int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
719{
720 struct nilfs_transaction_info ti;
721 struct inode *inode = dentry->d_inode;
722 struct super_block *sb = inode->i_sb;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700723 int err;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700724
725 err = inode_change_ok(inode, iattr);
726 if (err)
727 return err;
728
729 err = nilfs_transaction_begin(sb, &ti, 0);
730 if (unlikely(err))
731 return err;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700732
Christoph Hellwig10257742010-06-04 11:30:02 +0200733 if ((iattr->ia_valid & ATTR_SIZE) &&
734 iattr->ia_size != i_size_read(inode)) {
735 err = vmtruncate(inode, iattr->ia_size);
736 if (unlikely(err))
737 goto out_err;
738 }
739
740 setattr_copy(inode, iattr);
741 mark_inode_dirty(inode);
742
743 if (iattr->ia_valid & ATTR_MODE) {
744 err = nilfs_acl_chmod(inode);
745 if (unlikely(err))
746 goto out_err;
747 }
748
749 return nilfs_transaction_commit(sb);
750
751out_err:
752 nilfs_transaction_abort(sb);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700753 return err;
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700754}
755
756int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
757 struct buffer_head **pbh)
758{
759 struct nilfs_inode_info *ii = NILFS_I(inode);
760 int err;
761
762 spin_lock(&sbi->s_inode_lock);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700763 if (ii->i_bh == NULL) {
764 spin_unlock(&sbi->s_inode_lock);
765 err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
766 pbh);
767 if (unlikely(err))
768 return err;
769 spin_lock(&sbi->s_inode_lock);
770 if (ii->i_bh == NULL)
771 ii->i_bh = *pbh;
772 else {
773 brelse(*pbh);
774 *pbh = ii->i_bh;
775 }
776 } else
777 *pbh = ii->i_bh;
778
779 get_bh(*pbh);
780 spin_unlock(&sbi->s_inode_lock);
781 return 0;
782}
783
784int nilfs_inode_dirty(struct inode *inode)
785{
786 struct nilfs_inode_info *ii = NILFS_I(inode);
787 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
788 int ret = 0;
789
790 if (!list_empty(&ii->i_dirty)) {
791 spin_lock(&sbi->s_inode_lock);
792 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
793 test_bit(NILFS_I_BUSY, &ii->i_state);
794 spin_unlock(&sbi->s_inode_lock);
795 }
796 return ret;
797}
798
799int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
800 unsigned nr_dirty)
801{
802 struct nilfs_inode_info *ii = NILFS_I(inode);
803
804 atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
805
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700806 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700807 return 0;
808
809 spin_lock(&sbi->s_inode_lock);
810 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
811 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
812 /* Because this routine may race with nilfs_dispose_list(),
813 we have to check NILFS_I_QUEUED here, too. */
814 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
815 /* This will happen when somebody is freeing
816 this inode. */
817 nilfs_warning(sbi->s_super, __func__,
818 "cannot get inode (ino=%lu)\n",
819 inode->i_ino);
820 spin_unlock(&sbi->s_inode_lock);
821 return -EINVAL; /* NILFS_I_DIRTY may remain for
822 freeing inode */
823 }
824 list_del(&ii->i_dirty);
825 list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
826 set_bit(NILFS_I_QUEUED, &ii->i_state);
827 }
828 spin_unlock(&sbi->s_inode_lock);
829 return 0;
830}
831
832int nilfs_mark_inode_dirty(struct inode *inode)
833{
834 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
835 struct buffer_head *ibh;
836 int err;
837
838 err = nilfs_load_inode_block(sbi, inode, &ibh);
839 if (unlikely(err)) {
840 nilfs_warning(inode->i_sb, __func__,
841 "failed to reget inode block.\n");
842 return err;
843 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700844 nilfs_update_inode(inode, ibh);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700845 nilfs_mdt_mark_buffer_dirty(ibh);
846 nilfs_mdt_mark_dirty(sbi->s_ifile);
847 brelse(ibh);
848 return 0;
849}
850
851/**
852 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
853 * @inode: inode of the file to be registered.
854 *
855 * nilfs_dirty_inode() loads a inode block containing the specified
856 * @inode and copies data from a nilfs_inode to a corresponding inode
857 * entry in the inode block. This operation is excluded from the segment
858 * construction. This function can be called both as a single operation
859 * and as a part of indivisible file operations.
860 */
861void nilfs_dirty_inode(struct inode *inode)
862{
863 struct nilfs_transaction_info ti;
Ryusuke Konishi7d6cd922010-08-21 00:30:39 +0900864 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700865
866 if (is_bad_inode(inode)) {
867 nilfs_warning(inode->i_sb, __func__,
868 "tried to mark bad_inode dirty. ignored.\n");
869 dump_stack();
870 return;
871 }
Ryusuke Konishi7d6cd922010-08-21 00:30:39 +0900872 if (mdi) {
873 nilfs_mdt_mark_dirty(inode);
874 return;
875 }
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700876 nilfs_transaction_begin(inode->i_sb, &ti, 0);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700877 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700878 nilfs_transaction_commit(inode->i_sb); /* never fails */
Ryusuke Konishi05fe58f2009-04-06 19:01:32 -0700879}