Blame view

fs/nilfs2/inode.c 22.6 KB
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /*
   * inode.c - NILFS inode operations.
   *
   * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   *
   * Written by Ryusuke Konishi <ryusuke@osrg.net>
   *
   */
  
  #include <linux/buffer_head.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
25
  #include <linux/gfp.h>
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
26
27
  #include <linux/mpage.h>
  #include <linux/writeback.h>
f30bf3e40   Ryusuke Konishi   nilfs2: fix misse...
28
  #include <linux/uio.h>
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
29
  #include "nilfs.h"
6fd1e5c99   Al Viro   convert nilfs2 to...
30
  #include "btnode.h"
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include "segment.h"
  #include "page.h"
  #include "mdt.h"
  #include "cpfile.h"
  #include "ifile.h"
  
  
  /**
   * nilfs_get_block() - get a file block on the filesystem (callback function)
   * @inode - inode struct of the target file
   * @blkoff - file block number
   * @bh_result - buffer head to be mapped on
   * @create - indicate whether allocating the block or not when it has not
   *      been allocated yet.
   *
   * This function does not issue actual read request of the specified data
   * block. It is done by VFS.
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
48
49
50
51
52
   */
  int nilfs_get_block(struct inode *inode, sector_t blkoff,
  		    struct buffer_head *bh_result, int create)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
53
  	__u64 blknum = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
54
55
  	int err = 0, ret;
  	struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
56
  	unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
57

c3a7abf06   Ryusuke Konishi   nilfs2: support c...
58
59
60
61
  	down_read(&NILFS_MDT(dat)->mi_sem);
  	ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
  	up_read(&NILFS_MDT(dat)->mi_sem);
  	if (ret >= 0) {	/* found */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
62
  		map_bh(bh_result, inode->i_sb, blknum);
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
63
64
  		if (ret > 0)
  			bh_result->b_size = (ret << inode->i_blkbits);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
65
66
  		goto out;
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
67
68
69
70
71
72
73
74
75
76
  	/* data block was not found */
  	if (ret == -ENOENT && create) {
  		struct nilfs_transaction_info ti;
  
  		bh_result->b_blocknr = 0;
  		err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
  		if (unlikely(err))
  			goto out;
  		err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
  					(unsigned long)bh_result);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
77
78
79
80
81
82
83
84
  		if (unlikely(err != 0)) {
  			if (err == -EEXIST) {
  				/*
  				 * The get_block() function could be called
  				 * from multiple callers for an inode.
  				 * However, the page having this block must
  				 * be locked in this case.
  				 */
1f5abe7e7   Ryusuke Konishi   nilfs2: replace B...
85
  				printk(KERN_WARNING
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
86
87
88
89
90
91
92
  				       "nilfs_get_block: a race condition "
  				       "while inserting a data block. "
  				       "(inode number=%lu, file block "
  				       "offset=%llu)
  ",
  				       inode->i_ino,
  				       (unsigned long long)blkoff);
1f5abe7e7   Ryusuke Konishi   nilfs2: replace B...
93
  				err = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
94
95
96
97
98
99
100
  			} else if (err == -EINVAL) {
  				nilfs_error(inode->i_sb, __func__,
  					    "broken bmap (inode=%lu)
  ",
  					    inode->i_ino);
  				err = -EIO;
  			}
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
101
  			nilfs_transaction_abort(inode->i_sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
102
103
  			goto out;
  		}
abdb318b7   Jiro SEKIBA   nilfs2: replace m...
104
  		nilfs_mark_inode_dirty(inode);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
105
  		nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  		/* Error handling should be detailed */
  		set_buffer_new(bh_result);
  		map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
  						      to proper value */
  	} else if (ret == -ENOENT) {
  		/* not found is not error (e.g. hole); must return without
  		   the mapped state flag. */
  		;
  	} else {
  		err = ret;
  	}
  
   out:
  	return err;
  }
  
  /**
   * nilfs_readpage() - implement readpage() method of nilfs_aops {}
   * address_space_operations.
   * @file - file struct of the file to be read
   * @page - the page to be read
   */
  static int nilfs_readpage(struct file *file, struct page *page)
  {
  	return mpage_readpage(page, nilfs_get_block);
  }
  
  /**
   * nilfs_readpages() - implement readpages() method of nilfs_aops {}
   * address_space_operations.
   * @file - file struct of the file to be read
   * @mapping - address_space struct used for reading multiple pages
   * @pages - the pages to be read
   * @nr_pages - number of pages to be read
   */
  static int nilfs_readpages(struct file *file, struct address_space *mapping,
  			   struct list_head *pages, unsigned nr_pages)
  {
  	return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
  }
  
  static int nilfs_writepages(struct address_space *mapping,
  			    struct writeback_control *wbc)
  {
f30bf3e40   Ryusuke Konishi   nilfs2: fix misse...
150
151
152
153
154
155
156
157
  	struct inode *inode = mapping->host;
  	int err = 0;
  
  	if (wbc->sync_mode == WB_SYNC_ALL)
  		err = nilfs_construct_dsync_segment(inode->i_sb, inode,
  						    wbc->range_start,
  						    wbc->range_end);
  	return err;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  }
  
  static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
  {
  	struct inode *inode = page->mapping->host;
  	int err;
  
  	redirty_page_for_writepage(wbc, page);
  	unlock_page(page);
  
  	if (wbc->sync_mode == WB_SYNC_ALL) {
  		err = nilfs_construct_segment(inode->i_sb);
  		if (unlikely(err))
  			return err;
  	} else if (wbc->for_reclaim)
  		nilfs_flush_segment(inode->i_sb, inode->i_ino);
  
  	return 0;
  }
  
  static int nilfs_set_page_dirty(struct page *page)
  {
  	int ret = __set_page_dirty_buffers(page);
  
  	if (ret) {
  		struct inode *inode = page->mapping->host;
  		struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  		unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
  
  		nilfs_set_file_dirty(sbi, inode, nr_dirty);
  	}
  	return ret;
  }
  
  static int nilfs_write_begin(struct file *file, struct address_space *mapping,
  			     loff_t pos, unsigned len, unsigned flags,
  			     struct page **pagep, void **fsdata)
  
  {
  	struct inode *inode = mapping->host;
  	int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
  
  	if (unlikely(err))
  		return err;
155130a4f   Christoph Hellwig   get rid of block_...
202
203
204
205
206
207
  	err = block_write_begin(mapping, pos, len, flags, pagep,
  				nilfs_get_block);
  	if (unlikely(err)) {
  		loff_t isize = mapping->host->i_size;
  		if (pos + len > isize)
  			vmtruncate(mapping->host, isize);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
208
  		nilfs_transaction_abort(inode->i_sb);
155130a4f   Christoph Hellwig   get rid of block_...
209
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	return err;
  }
  
  static int nilfs_write_end(struct file *file, struct address_space *mapping,
  			   loff_t pos, unsigned len, unsigned copied,
  			   struct page *page, void *fsdata)
  {
  	struct inode *inode = mapping->host;
  	unsigned start = pos & (PAGE_CACHE_SIZE - 1);
  	unsigned nr_dirty;
  	int err;
  
  	nr_dirty = nilfs_page_count_clean_buffers(page, start,
  						  start + copied);
  	copied = generic_write_end(file, mapping, pos, len, copied, page,
  				   fsdata);
  	nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
227
  	err = nilfs_transaction_commit(inode->i_sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
228
229
230
231
232
233
234
235
236
237
  	return err ? : copied;
  }
  
  static ssize_t
  nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  		loff_t offset, unsigned long nr_segs)
  {
  	struct file *file = iocb->ki_filp;
  	struct inode *inode = file->f_mapping->host;
  	ssize_t size;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
238
239
240
241
242
243
244
  
  	if (rw == WRITE)
  		return 0;
  
  	/* Needs synchronization with the cleaner */
  	size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  				  offset, nr_segs, nilfs_get_block, NULL);
eafdc7d19   Christoph Hellwig   sort out blockdev...
245
246
247
248
249
250
251
252
253
254
255
256
  
  	/*
  	 * In case of error extending write may have instantiated a few
  	 * blocks outside i_size. Trim these off again.
  	 */
  	if (unlikely((rw & WRITE) && size < 0)) {
  		loff_t isize = i_size_read(inode);
  		loff_t end = offset + iov_length(iov, nr_segs);
  
  		if (end > isize)
  			vmtruncate(inode, isize);
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
257
258
  	return size;
  }
7f09410bb   Alexey Dobriyan   const: mark remai...
259
  const struct address_space_operations nilfs_aops = {
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
260
261
  	.writepage		= nilfs_writepage,
  	.readpage		= nilfs_readpage,
e85dc1d52   Ryusuke Konishi   nilfs2: enable sy...
262
  	.sync_page		= block_sync_page,
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
263
264
265
266
267
268
269
270
  	.writepages		= nilfs_writepages,
  	.set_page_dirty		= nilfs_set_page_dirty,
  	.readpages		= nilfs_readpages,
  	.write_begin		= nilfs_write_begin,
  	.write_end		= nilfs_write_end,
  	/* .releasepage		= nilfs_releasepage, */
  	.invalidatepage		= block_invalidatepage,
  	.direct_IO		= nilfs_direct_IO,
258ef67e2   Hisashi Hifumi   NILFS2: Pagecache...
271
  	.is_partially_uptodate  = block_is_partially_uptodate,
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  };
  
  struct inode *nilfs_new_inode(struct inode *dir, int mode)
  {
  	struct super_block *sb = dir->i_sb;
  	struct nilfs_sb_info *sbi = NILFS_SB(sb);
  	struct inode *inode;
  	struct nilfs_inode_info *ii;
  	int err = -ENOMEM;
  	ino_t ino;
  
  	inode = new_inode(sb);
  	if (unlikely(!inode))
  		goto failed;
  
  	mapping_set_gfp_mask(inode->i_mapping,
  			     mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  
  	ii = NILFS_I(inode);
  	ii->i_state = 1 << NILFS_I_NEW;
  
  	err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
  	if (unlikely(err))
  		goto failed_ifile_create_inode;
  	/* reference count of i_bh inherits from nilfs_mdt_read_block() */
  
  	atomic_inc(&sbi->s_inodes_count);
73459dcc6   Dmitry Monakhov   nilfs2: replace i...
299
  	inode_init_owner(inode, dir, mode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  	inode->i_ino = ino;
  	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  
  	if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  		err = nilfs_bmap_read(ii->i_bmap, NULL);
  		if (err < 0)
  			goto failed_bmap;
  
  		set_bit(NILFS_I_BMAP, &ii->i_state);
  		/* No lock is needed; iget() ensures it. */
  	}
  
  	ii->i_flags = NILFS_I(dir)->i_flags;
  	if (S_ISLNK(mode))
  		ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
  	if (!S_ISDIR(mode))
  		ii->i_flags &= ~NILFS_DIRSYNC_FL;
  
  	/* ii->i_file_acl = 0; */
  	/* ii->i_dir_acl = 0; */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
320
  	ii->i_dir_start_lookup = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
321
322
323
324
325
326
327
328
329
330
331
332
  	ii->i_cno = 0;
  	nilfs_set_inode_flags(inode);
  	spin_lock(&sbi->s_next_gen_lock);
  	inode->i_generation = sbi->s_next_generation++;
  	spin_unlock(&sbi->s_next_gen_lock);
  	insert_inode_hash(inode);
  
  	err = nilfs_init_acl(inode, dir);
  	if (unlikely(err))
  		goto failed_acl; /* never occur. When supporting
  				    nilfs_init_acl(), proper cancellation of
  				    above jobs should be considered */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  	return inode;
  
   failed_acl:
   failed_bmap:
  	inode->i_nlink = 0;
  	iput(inode);  /* raw_inode will be deleted through
  			 generic_delete_inode() */
  	goto failed;
  
   failed_ifile_create_inode:
  	make_bad_inode(inode);
  	iput(inode);  /* if i_nlink == 1, generic_forget_inode() will be
  			 called */
   failed:
  	return ERR_PTR(err);
  }
  
  void nilfs_free_inode(struct inode *inode)
  {
  	struct super_block *sb = inode->i_sb;
  	struct nilfs_sb_info *sbi = NILFS_SB(sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  	/* XXX: check error code? Is there any thing I can do? */
  	(void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
  	atomic_dec(&sbi->s_inodes_count);
  }
  
  void nilfs_set_inode_flags(struct inode *inode)
  {
  	unsigned int flags = NILFS_I(inode)->i_flags;
  
  	inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  			    S_DIRSYNC);
  	if (flags & NILFS_SYNC_FL)
  		inode->i_flags |= S_SYNC;
  	if (flags & NILFS_APPEND_FL)
  		inode->i_flags |= S_APPEND;
  	if (flags & NILFS_IMMUTABLE_FL)
  		inode->i_flags |= S_IMMUTABLE;
  #ifndef NILFS_ATIME_DISABLE
  	if (flags & NILFS_NOATIME_FL)
  #endif
  		inode->i_flags |= S_NOATIME;
  	if (flags & NILFS_DIRSYNC_FL)
  		inode->i_flags |= S_DIRSYNC;
  	mapping_set_gfp_mask(inode->i_mapping,
  			     mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  }
  
  int nilfs_read_inode_common(struct inode *inode,
  			    struct nilfs_inode *raw_inode)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  	int err;
  
  	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  	inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
  	inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
  	inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  	inode->i_size = le64_to_cpu(raw_inode->i_size);
  	inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  	inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
  	inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
612392307   Ryusuke Konishi   nilfs2: support n...
395
396
397
398
  	inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  	inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
  	inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  	if (inode->i_nlink == 0 && inode->i_mode == 0)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
399
400
401
402
403
404
405
406
407
  		return -EINVAL; /* this inode is deleted */
  
  	inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
  	ii->i_flags = le32_to_cpu(raw_inode->i_flags);
  #if 0
  	ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  	ii->i_dir_acl = S_ISREG(inode->i_mode) ?
  		0 : le32_to_cpu(raw_inode->i_dir_acl);
  #endif
3cc811bff   Ryusuke Konishi   nilfs2: fix missi...
408
  	ii->i_dir_start_lookup = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
409
410
411
412
413
414
415
416
417
418
419
420
421
  	ii->i_cno = 0;
  	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  
  	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  	    S_ISLNK(inode->i_mode)) {
  		err = nilfs_bmap_read(ii->i_bmap, raw_inode);
  		if (err < 0)
  			return err;
  		set_bit(NILFS_I_BMAP, &ii->i_state);
  		/* No lock is needed; iget() ensures it. */
  	}
  	return 0;
  }
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
  static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
  			      struct inode *inode)
  {
  	struct nilfs_sb_info *sbi = NILFS_SB(sb);
  	struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
  	struct buffer_head *bh;
  	struct nilfs_inode *raw_inode;
  	int err;
  
  	down_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
  	err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
  	if (unlikely(err))
  		goto bad_inode;
  
  	raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
1b2f5a641   Ryusuke Konishi   nilfs2: fix ignor...
437
438
  	err = nilfs_read_inode_common(inode, raw_inode);
  	if (err)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
439
440
441
442
443
444
  		goto failed_unmap;
  
  	if (S_ISREG(inode->i_mode)) {
  		inode->i_op = &nilfs_file_inode_operations;
  		inode->i_fop = &nilfs_file_operations;
  		inode->i_mapping->a_ops = &nilfs_aops;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
445
446
447
448
449
450
451
452
453
454
455
  	} else if (S_ISDIR(inode->i_mode)) {
  		inode->i_op = &nilfs_dir_inode_operations;
  		inode->i_fop = &nilfs_dir_operations;
  		inode->i_mapping->a_ops = &nilfs_aops;
  	} else if (S_ISLNK(inode->i_mode)) {
  		inode->i_op = &nilfs_symlink_inode_operations;
  		inode->i_mapping->a_ops = &nilfs_aops;
  	} else {
  		inode->i_op = &nilfs_special_inode_operations;
  		init_special_inode(
  			inode, inode->i_mode,
cdce214e3   Ryusuke Konishi   nilfs2: use huge_...
456
  			huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  	}
  	nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  	brelse(bh);
  	up_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
  	nilfs_set_inode_flags(inode);
  	return 0;
  
   failed_unmap:
  	nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  	brelse(bh);
  
   bad_inode:
  	up_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
  	return err;
  }
  
  struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
  {
  	struct inode *inode;
  	int err;
  
  	inode = iget_locked(sb, ino);
  	if (unlikely(!inode))
  		return ERR_PTR(-ENOMEM);
  	if (!(inode->i_state & I_NEW))
  		return inode;
  
  	err = __nilfs_read_inode(sb, ino, inode);
  	if (unlikely(err)) {
  		iget_failed(inode);
  		return ERR_PTR(err);
  	}
  	unlock_new_inode(inode);
  	return inode;
  }
  
  void nilfs_write_inode_common(struct inode *inode,
  			      struct nilfs_inode *raw_inode, int has_bmap)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  
  	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  	raw_inode->i_uid = cpu_to_le32(inode->i_uid);
  	raw_inode->i_gid = cpu_to_le32(inode->i_gid);
  	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  	raw_inode->i_size = cpu_to_le64(inode->i_size);
  	raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  	raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
612392307   Ryusuke Konishi   nilfs2: support n...
505
506
  	raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  	raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
507
  	raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
508
509
510
511
512
513
514
  	raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  
  	if (has_bmap)
  		nilfs_bmap_write(ii->i_bmap, raw_inode);
  	else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  		raw_inode->i_device_code =
cdce214e3   Ryusuke Konishi   nilfs2: use huge_...
515
  			cpu_to_le64(huge_encode_dev(inode->i_rdev));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
516
517
518
519
520
521
522
523
524
525
526
527
528
  	/* When extending inode, nilfs->ns_inode_size should be checked
  	   for substitutions of appended fields */
  }
  
  void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
  {
  	ino_t ino = inode->i_ino;
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  	struct super_block *sb = inode->i_sb;
  	struct nilfs_sb_info *sbi = NILFS_SB(sb);
  	struct nilfs_inode *raw_inode;
  
  	raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  	if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
  		memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
  	set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
  
  	nilfs_write_inode_common(inode, raw_inode, 0);
  		/* XXX: call with has_bmap = 0 is a workaround to avoid
  		   deadlock of bmap. This delays update of i_bmap to just
  		   before writing */
  	nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
  }
  
  #define NILFS_MAX_TRUNCATE_BLOCKS	16384  /* 64MB for 4KB block */
  
  static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
  				unsigned long from)
  {
  	unsigned long b;
  	int ret;
  
  	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  		return;
   repeat:
  	ret = nilfs_bmap_last_key(ii->i_bmap, &b);
  	if (ret == -ENOENT)
  		return;
  	else if (ret < 0)
  		goto failed;
  
  	if (b < from)
  		return;
  
  	b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
  	ret = nilfs_bmap_truncate(ii->i_bmap, b);
  	nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
  	if (!ret || (ret == -ENOMEM &&
  		     nilfs_bmap_truncate(ii->i_bmap, b) == 0))
  		goto repeat;
  
   failed:
  	if (ret == -EINVAL)
  		nilfs_error(ii->vfs_inode.i_sb, __func__,
  			    "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
  	else
  		nilfs_warning(ii->vfs_inode.i_sb, __func__,
  			      "failed to truncate bmap (ino=%lu, err=%d)",
  			      ii->vfs_inode.i_ino, ret);
  }
  
  void nilfs_truncate(struct inode *inode)
  {
  	unsigned long blkoff;
  	unsigned int blocksize;
  	struct nilfs_transaction_info ti;
  	struct super_block *sb = inode->i_sb;
  	struct nilfs_inode_info *ii = NILFS_I(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
584
585
586
587
588
589
590
591
  
  	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  		return;
  	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  		return;
  
  	blocksize = sb->s_blocksize;
  	blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
1f5abe7e7   Ryusuke Konishi   nilfs2: replace B...
592
  	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
593
594
595
596
597
598
599
600
  
  	block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
  
  	nilfs_truncate_bmap(ii, blkoff);
  
  	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  	if (IS_SYNC(inode))
  		nilfs_set_transaction_flag(NILFS_TI_SYNC);
abdb318b7   Jiro SEKIBA   nilfs2: replace m...
601
  	nilfs_mark_inode_dirty(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
602
  	nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
603
  	nilfs_transaction_commit(sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
604
605
606
  	/* May construct a logical segment and may fail in sync mode.
  	   But truncate has no return value. */
  }
6fd1e5c99   Al Viro   convert nilfs2 to...
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  static void nilfs_clear_inode(struct inode *inode)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  
  	/*
  	 * Free resources allocated in nilfs_read_inode(), here.
  	 */
  	BUG_ON(!list_empty(&ii->i_dirty));
  	brelse(ii->i_bh);
  	ii->i_bh = NULL;
  
  	if (test_bit(NILFS_I_BMAP, &ii->i_state))
  		nilfs_bmap_clear(ii->i_bmap);
  
  	nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  }
  
  void nilfs_evict_inode(struct inode *inode)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
625
626
627
628
  {
  	struct nilfs_transaction_info ti;
  	struct super_block *sb = inode->i_sb;
  	struct nilfs_inode_info *ii = NILFS_I(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
629

6fd1e5c99   Al Viro   convert nilfs2 to...
630
  	if (inode->i_nlink || unlikely(is_bad_inode(inode))) {
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
631
632
  		if (inode->i_data.nrpages)
  			truncate_inode_pages(&inode->i_data, 0);
6fd1e5c99   Al Viro   convert nilfs2 to...
633
634
  		end_writeback(inode);
  		nilfs_clear_inode(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
635
636
  		return;
  	}
1f5abe7e7   Ryusuke Konishi   nilfs2: replace B...
637
  	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
638
639
640
641
  	if (inode->i_data.nrpages)
  		truncate_inode_pages(&inode->i_data, 0);
  
  	nilfs_truncate_bmap(ii, 0);
abdb318b7   Jiro SEKIBA   nilfs2: replace m...
642
  	nilfs_mark_inode_dirty(inode);
6fd1e5c99   Al Viro   convert nilfs2 to...
643
644
  	end_writeback(inode);
  	nilfs_clear_inode(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
645
646
647
648
  	nilfs_free_inode(inode);
  	/* nilfs_free_inode() marks inode buffer dirty */
  	if (IS_SYNC(inode))
  		nilfs_set_transaction_flag(NILFS_TI_SYNC);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
649
  	nilfs_transaction_commit(sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
650
651
652
653
654
655
656
657
658
  	/* May construct a logical segment and may fail in sync mode.
  	   But delete_inode has no return value. */
  }
  
  int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
  {
  	struct nilfs_transaction_info ti;
  	struct inode *inode = dentry->d_inode;
  	struct super_block *sb = inode->i_sb;
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
659
  	int err;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
660
661
662
663
664
665
666
667
  
  	err = inode_change_ok(inode, iattr);
  	if (err)
  		return err;
  
  	err = nilfs_transaction_begin(sb, &ti, 0);
  	if (unlikely(err))
  		return err;
1025774ce   Christoph Hellwig   remove inode_setattr
668
669
670
671
672
673
674
675
676
677
678
679
  
  	if ((iattr->ia_valid & ATTR_SIZE) &&
  	    iattr->ia_size != i_size_read(inode)) {
  		err = vmtruncate(inode, iattr->ia_size);
  		if (unlikely(err))
  			goto out_err;
  	}
  
  	setattr_copy(inode, iattr);
  	mark_inode_dirty(inode);
  
  	if (iattr->ia_valid & ATTR_MODE) {
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
680
  		err = nilfs_acl_chmod(inode);
1025774ce   Christoph Hellwig   remove inode_setattr
681
682
683
684
685
  		if (unlikely(err))
  			goto out_err;
  	}
  
  	return nilfs_transaction_commit(sb);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
686

1025774ce   Christoph Hellwig   remove inode_setattr
687
688
  out_err:
  	nilfs_transaction_abort(sb);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
689
  	return err;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
690
691
692
693
694
695
696
697
698
  }
  
  int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
  			   struct buffer_head **pbh)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  	int err;
  
  	spin_lock(&sbi->s_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
  	if (ii->i_bh == NULL) {
  		spin_unlock(&sbi->s_inode_lock);
  		err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
  						  pbh);
  		if (unlikely(err))
  			return err;
  		spin_lock(&sbi->s_inode_lock);
  		if (ii->i_bh == NULL)
  			ii->i_bh = *pbh;
  		else {
  			brelse(*pbh);
  			*pbh = ii->i_bh;
  		}
  	} else
  		*pbh = ii->i_bh;
  
  	get_bh(*pbh);
  	spin_unlock(&sbi->s_inode_lock);
  	return 0;
  }
  
  int nilfs_inode_dirty(struct inode *inode)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  	struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  	int ret = 0;
  
  	if (!list_empty(&ii->i_dirty)) {
  		spin_lock(&sbi->s_inode_lock);
  		ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  			test_bit(NILFS_I_BUSY, &ii->i_state);
  		spin_unlock(&sbi->s_inode_lock);
  	}
  	return ret;
  }
  
  int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
  			 unsigned nr_dirty)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  
  	atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
458c5b082   Ryusuke Konishi   nilfs2: clean up ...
741
  	if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
  		return 0;
  
  	spin_lock(&sbi->s_inode_lock);
  	if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
  	    !test_bit(NILFS_I_BUSY, &ii->i_state)) {
  		/* Because this routine may race with nilfs_dispose_list(),
  		   we have to check NILFS_I_QUEUED here, too. */
  		if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
  			/* This will happen when somebody is freeing
  			   this inode. */
  			nilfs_warning(sbi->s_super, __func__,
  				      "cannot get inode (ino=%lu)
  ",
  				      inode->i_ino);
  			spin_unlock(&sbi->s_inode_lock);
  			return -EINVAL; /* NILFS_I_DIRTY may remain for
  					   freeing inode */
  		}
  		list_del(&ii->i_dirty);
  		list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
  		set_bit(NILFS_I_QUEUED, &ii->i_state);
  	}
  	spin_unlock(&sbi->s_inode_lock);
  	return 0;
  }
  
  int nilfs_mark_inode_dirty(struct inode *inode)
  {
  	struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  	struct buffer_head *ibh;
  	int err;
  
  	err = nilfs_load_inode_block(sbi, inode, &ibh);
  	if (unlikely(err)) {
  		nilfs_warning(inode->i_sb, __func__,
  			      "failed to reget inode block.
  ");
  		return err;
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
781
  	nilfs_update_inode(inode, ibh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
  	nilfs_mdt_mark_buffer_dirty(ibh);
  	nilfs_mdt_mark_dirty(sbi->s_ifile);
  	brelse(ibh);
  	return 0;
  }
  
  /**
   * nilfs_dirty_inode - reflect changes on given inode to an inode block.
   * @inode: inode of the file to be registered.
   *
   * nilfs_dirty_inode() loads a inode block containing the specified
   * @inode and copies data from a nilfs_inode to a corresponding inode
   * entry in the inode block. This operation is excluded from the segment
   * construction. This function can be called both as a single operation
   * and as a part of indivisible file operations.
   */
  void nilfs_dirty_inode(struct inode *inode)
  {
  	struct nilfs_transaction_info ti;
  
  	if (is_bad_inode(inode)) {
  		nilfs_warning(inode->i_sb, __func__,
  			      "tried to mark bad_inode dirty. ignored.
  ");
  		dump_stack();
  		return;
  	}
  	nilfs_transaction_begin(inode->i_sb, &ti, 0);
458c5b082   Ryusuke Konishi   nilfs2: clean up ...
810
  	nilfs_mark_inode_dirty(inode);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
811
  	nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
812
  }