Blame view

fs/nilfs2/inode.c 29.5 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
  #include <linux/mpage.h>
56d7acc79   Andreas Rohner   nilfs2: fix data ...
27
  #include <linux/pagemap.h>
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
28
  #include <linux/writeback.h>
e2e40f2c1   Christoph Hellwig   fs: move struct k...
29
  #include <linux/uio.h>
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
30
  #include "nilfs.h"
6fd1e5c99   Al Viro   convert nilfs2 to...
31
  #include "btnode.h"
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
32
33
34
35
36
  #include "segment.h"
  #include "page.h"
  #include "mdt.h"
  #include "cpfile.h"
  #include "ifile.h"
f5974c8f8   Vyacheslav Dubeyko   nilfs2: add omitt...
37
38
39
40
41
42
43
  /**
   * struct nilfs_iget_args - arguments used during comparison between inodes
   * @ino: inode number
   * @cno: checkpoint number
   * @root: pointer on NILFS root object (mounted checkpoint)
   * @for_gc: inode for GC flag
   */
0e14a3595   Ryusuke Konishi   nilfs2: use iget5...
44
45
46
  struct nilfs_iget_args {
  	u64 ino;
  	__u64 cno;
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
47
  	struct nilfs_root *root;
0e14a3595   Ryusuke Konishi   nilfs2: use iget5...
48
49
  	int for_gc;
  };
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
50

705304a86   Ryusuke Konishi   nilfs2: fix the n...
51
  static int nilfs_iget_test(struct inode *inode, void *opaque);
be667377a   Ryusuke Konishi   nilfs2: record us...
52
53
54
55
56
57
  void nilfs_inode_add_blocks(struct inode *inode, int n)
  {
  	struct nilfs_root *root = NILFS_I(inode)->i_root;
  
  	inode_add_bytes(inode, (1 << inode->i_blkbits) * n);
  	if (root)
e5f7f8484   Vyacheslav Dubeyko   ] nilfs2: use ato...
58
  		atomic64_add(n, &root->blocks_count);
be667377a   Ryusuke Konishi   nilfs2: record us...
59
60
61
62
63
64
65
66
  }
  
  void nilfs_inode_sub_blocks(struct inode *inode, int n)
  {
  	struct nilfs_root *root = NILFS_I(inode)->i_root;
  
  	inode_sub_bytes(inode, (1 << inode->i_blkbits) * n);
  	if (root)
e5f7f8484   Vyacheslav Dubeyko   ] nilfs2: use ato...
67
  		atomic64_sub(n, &root->blocks_count);
be667377a   Ryusuke Konishi   nilfs2: record us...
68
  }
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
69
70
71
72
73
74
75
76
77
78
  /**
   * 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...
79
80
81
82
83
   */
  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);
0ef28f9ae   Ryusuke Konishi   nilfs2: get rid o...
84
  	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
85
  	__u64 blknum = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
86
  	int err = 0, ret;
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
87
  	unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
88

0ef28f9ae   Ryusuke Konishi   nilfs2: get rid o...
89
  	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
90
  	ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
0ef28f9ae   Ryusuke Konishi   nilfs2: get rid o...
91
  	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
92
  	if (ret >= 0) {	/* found */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
93
  		map_bh(bh_result, inode->i_sb, blknum);
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
94
95
  		if (ret > 0)
  			bh_result->b_size = (ret << inode->i_blkbits);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
96
97
  		goto out;
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
98
99
100
101
102
103
104
105
  	/* 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;
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
106
  		err = nilfs_bmap_insert(ii->i_bmap, blkoff,
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
107
  					(unsigned long)bh_result);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
108
109
110
111
112
113
114
115
  		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...
116
  				printk(KERN_WARNING
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
117
118
119
120
121
122
123
  				       "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...
124
  				err = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
125
  			}
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
126
  			nilfs_transaction_abort(inode->i_sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
127
128
  			goto out;
  		}
b9f661407   Andreas Rohner   nilfs2: improve t...
129
  		nilfs_mark_inode_dirty_sync(inode);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
130
  		nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
131
132
  		/* Error handling should be detailed */
  		set_buffer_new(bh_result);
27e6c7a3c   Ryusuke Konishi   nilfs2: mark buff...
133
  		set_buffer_delay(bh_result);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  		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...
176
177
  	struct inode *inode = mapping->host;
  	int err = 0;
8c26c4e26   Vyacheslav Dubeyko   nilfs2: fix issue...
178
179
180
181
  	if (inode->i_sb->s_flags & MS_RDONLY) {
  		nilfs_clear_dirty_pages(mapping, false);
  		return -EROFS;
  	}
f30bf3e40   Ryusuke Konishi   nilfs2: fix misse...
182
183
184
185
186
  	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...
187
188
189
190
191
192
  }
  
  static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
  {
  	struct inode *inode = page->mapping->host;
  	int err;
eb53b6db7   Vyacheslav Dubeyko   nilfs2: remove un...
193
  	if (inode->i_sb->s_flags & MS_RDONLY) {
8c26c4e26   Vyacheslav Dubeyko   nilfs2: fix issue...
194
195
196
197
198
199
200
201
202
203
  		/*
  		 * It means that filesystem was remounted in read-only
  		 * mode because of error or metadata corruption. But we
  		 * have dirty pages that try to be flushed in background.
  		 * So, here we simply discard this dirty page.
  		 */
  		nilfs_clear_dirty_page(page, false);
  		unlock_page(page);
  		return -EROFS;
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  	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)
  {
56d7acc79   Andreas Rohner   nilfs2: fix data ...
219
  	struct inode *inode = page->mapping->host;
136e8770c   Ryusuke Konishi   nilfs2: fix issue...
220
  	int ret = __set_page_dirty_nobuffers(page);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
221

136e8770c   Ryusuke Konishi   nilfs2: fix issue...
222
  	if (page_has_buffers(page)) {
136e8770c   Ryusuke Konishi   nilfs2: fix issue...
223
224
  		unsigned nr_dirty = 0;
  		struct buffer_head *bh, *head;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
225

136e8770c   Ryusuke Konishi   nilfs2: fix issue...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  		/*
  		 * This page is locked by callers, and no other thread
  		 * concurrently marks its buffers dirty since they are
  		 * only dirtied through routines in fs/buffer.c in
  		 * which call sites of mark_buffer_dirty are protected
  		 * by page lock.
  		 */
  		bh = head = page_buffers(page);
  		do {
  			/* Do not mark hole blocks dirty */
  			if (buffer_dirty(bh) || !buffer_mapped(bh))
  				continue;
  
  			set_buffer_dirty(bh);
  			nr_dirty++;
  		} while (bh = bh->b_this_page, bh != head);
  
  		if (nr_dirty)
  			nilfs_set_file_dirty(inode, nr_dirty);
56d7acc79   Andreas Rohner   nilfs2: fix data ...
245
  	} else if (ret) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
246
  		unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
56d7acc79   Andreas Rohner   nilfs2: fix data ...
247
248
  
  		nilfs_set_file_dirty(inode, nr_dirty);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
249
250
251
  	}
  	return ret;
  }
2d1b399b2   Marco Stornelli   nilfs2: drop vmtr...
252
253
254
255
256
  void nilfs_write_failed(struct address_space *mapping, loff_t to)
  {
  	struct inode *inode = mapping->host;
  
  	if (to > inode->i_size) {
7caef2676   Kirill A. Shutemov   truncate: drop 'o...
257
  		truncate_pagecache(inode, inode->i_size);
2d1b399b2   Marco Stornelli   nilfs2: drop vmtr...
258
259
260
  		nilfs_truncate(inode);
  	}
  }
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
261
262
263
264
265
266
267
268
269
270
  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_...
271
272
273
  	err = block_write_begin(mapping, pos, len, flags, pagep,
  				nilfs_get_block);
  	if (unlikely(err)) {
2d1b399b2   Marco Stornelli   nilfs2: drop vmtr...
274
  		nilfs_write_failed(mapping, pos + len);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
275
  		nilfs_transaction_abort(inode->i_sb);
155130a4f   Christoph Hellwig   get rid of block_...
276
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
277
278
279
280
281
282
283
284
  	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;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
285
  	unsigned start = pos & (PAGE_SIZE - 1);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
286
287
288
289
290
291
292
  	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);
bcbc8c648   Ryusuke Konishi   nilfs2: do not pa...
293
  	nilfs_set_file_dirty(inode, nr_dirty);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
294
  	err = nilfs_transaction_commit(inode->i_sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
295
296
297
298
  	return err ? : copied;
  }
  
  static ssize_t
22c6186ec   Omar Sandoval   direct_IO: remove...
299
  nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
300
  {
6b6dabc8d   Al Viro   nilfs2_direct_IO(...
301
  	struct inode *inode = file_inode(iocb->ki_filp);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
302

6f6737631   Omar Sandoval   direct_IO: use io...
303
  	if (iov_iter_rw(iter) == WRITE)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
304
305
306
  		return 0;
  
  	/* Needs synchronization with the cleaner */
6b6dabc8d   Al Viro   nilfs2_direct_IO(...
307
  	return blockdev_direct_IO(iocb, inode, iter, offset, nilfs_get_block);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
308
  }
7f09410bb   Alexey Dobriyan   const: mark remai...
309
  const struct address_space_operations nilfs_aops = {
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
310
311
  	.writepage		= nilfs_writepage,
  	.readpage		= nilfs_readpage,
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
312
313
314
315
316
317
318
319
  	.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...
320
  	.is_partially_uptodate  = block_is_partially_uptodate,
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
321
  };
705304a86   Ryusuke Konishi   nilfs2: fix the n...
322
323
324
325
326
327
328
329
330
331
  static int nilfs_insert_inode_locked(struct inode *inode,
  				     struct nilfs_root *root,
  				     unsigned long ino)
  {
  	struct nilfs_iget_args args = {
  		.ino = ino, .root = root, .cno = 0, .for_gc = 0
  	};
  
  	return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
  }
c6e49e3f2   Al Viro   nilfs: propagate ...
332
  struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
333
334
  {
  	struct super_block *sb = dir->i_sb;
e3154e974   Ryusuke Konishi   nilfs2: get rid o...
335
  	struct the_nilfs *nilfs = sb->s_fs_info;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
336
337
  	struct inode *inode;
  	struct nilfs_inode_info *ii;
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
338
  	struct nilfs_root *root;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
339
340
341
342
343
344
345
346
  	int err = -ENOMEM;
  	ino_t ino;
  
  	inode = new_inode(sb);
  	if (unlikely(!inode))
  		goto failed;
  
  	mapping_set_gfp_mask(inode->i_mapping,
c62d25556   Michal Hocko   mm, fs: introduce...
347
  			   mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
348

4d8d9293d   Ryusuke Konishi   nilfs2: set point...
349
  	root = NILFS_I(dir)->i_root;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
350
351
  	ii = NILFS_I(inode);
  	ii->i_state = 1 << NILFS_I_NEW;
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
352
  	ii->i_root = root;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
353

e912a5b66   Ryusuke Konishi   nilfs2: use root ...
354
  	err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
355
356
357
  	if (unlikely(err))
  		goto failed_ifile_create_inode;
  	/* reference count of i_bh inherits from nilfs_mdt_read_block() */
e5f7f8484   Vyacheslav Dubeyko   ] nilfs2: use ato...
358
  	atomic64_inc(&root->inodes_count);
73459dcc6   Dmitry Monakhov   nilfs2: replace i...
359
  	inode_init_owner(inode, dir, mode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
360
361
362
363
364
365
  	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)
705304a86   Ryusuke Konishi   nilfs2: fix the n...
366
  			goto failed_after_creation;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
367
368
369
370
  
  		set_bit(NILFS_I_BMAP, &ii->i_state);
  		/* No lock is needed; iget() ensures it. */
  	}
b253a3e4f   Ryusuke Konishi   nilfs2: tighten r...
371
372
  	ii->i_flags = nilfs_mask_flags(
  		mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
373
374
375
  
  	/* ii->i_file_acl = 0; */
  	/* ii->i_dir_acl = 0; */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
376
  	ii->i_dir_start_lookup = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
377
  	nilfs_set_inode_flags(inode);
9b1fc4e49   Ryusuke Konishi   nilfs2: move next...
378
379
380
  	spin_lock(&nilfs->ns_next_gen_lock);
  	inode->i_generation = nilfs->ns_next_generation++;
  	spin_unlock(&nilfs->ns_next_gen_lock);
705304a86   Ryusuke Konishi   nilfs2: fix the n...
381
382
383
384
  	if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
  		err = -EIO;
  		goto failed_after_creation;
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
385
386
387
  
  	err = nilfs_init_acl(inode, dir);
  	if (unlikely(err))
705304a86   Ryusuke Konishi   nilfs2: fix the n...
388
  		goto failed_after_creation; /* never occur. When supporting
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
389
390
  				    nilfs_init_acl(), proper cancellation of
  				    above jobs should be considered */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
391
  	return inode;
705304a86   Ryusuke Konishi   nilfs2: fix the n...
392
   failed_after_creation:
6d6b77f16   Miklos Szeredi   filesystems: add ...
393
  	clear_nlink(inode);
705304a86   Ryusuke Konishi   nilfs2: fix the n...
394
  	unlock_new_inode(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
395
  	iput(inode);  /* raw_inode will be deleted through
705304a86   Ryusuke Konishi   nilfs2: fix the n...
396
  			 nilfs_evict_inode() */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
397
398
399
400
401
402
403
404
405
  	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);
  }
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
406
407
408
  void nilfs_set_inode_flags(struct inode *inode)
  {
  	unsigned int flags = NILFS_I(inode)->i_flags;
faea2c531   Ryusuke Konishi   nilfs2: use inode...
409
  	unsigned int new_fl = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
410

f0c9f242f   Ryusuke Konishi   nilfs2: use commo...
411
  	if (flags & FS_SYNC_FL)
faea2c531   Ryusuke Konishi   nilfs2: use inode...
412
  		new_fl |= S_SYNC;
f0c9f242f   Ryusuke Konishi   nilfs2: use commo...
413
  	if (flags & FS_APPEND_FL)
faea2c531   Ryusuke Konishi   nilfs2: use inode...
414
  		new_fl |= S_APPEND;
f0c9f242f   Ryusuke Konishi   nilfs2: use commo...
415
  	if (flags & FS_IMMUTABLE_FL)
faea2c531   Ryusuke Konishi   nilfs2: use inode...
416
  		new_fl |= S_IMMUTABLE;
f0c9f242f   Ryusuke Konishi   nilfs2: use commo...
417
  	if (flags & FS_NOATIME_FL)
faea2c531   Ryusuke Konishi   nilfs2: use inode...
418
  		new_fl |= S_NOATIME;
f0c9f242f   Ryusuke Konishi   nilfs2: use commo...
419
  	if (flags & FS_DIRSYNC_FL)
faea2c531   Ryusuke Konishi   nilfs2: use inode...
420
421
422
  		new_fl |= S_DIRSYNC;
  	inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
  			S_NOATIME | S_DIRSYNC);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
423
424
425
426
427
428
429
430
431
  }
  
  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);
305d3d0db   Eric W. Biederman   userns: Convert n...
432
433
  	i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
  	i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
bfe868486   Miklos Szeredi   filesystems: add ...
434
  	set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
435
436
437
438
  	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...
439
440
441
  	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);
705304a86   Ryusuke Konishi   nilfs2: fix the n...
442
443
  	if (inode->i_nlink == 0)
  		return -ESTALE; /* this inode is deleted */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
444
445
446
447
448
449
450
451
  
  	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...
452
  	ii->i_dir_start_lookup = 0;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
453
454
455
456
457
458
459
460
461
462
463
464
  	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;
  }
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
465
466
  static int __nilfs_read_inode(struct super_block *sb,
  			      struct nilfs_root *root, unsigned long ino,
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
467
468
  			      struct inode *inode)
  {
e3154e974   Ryusuke Konishi   nilfs2: get rid o...
469
  	struct the_nilfs *nilfs = sb->s_fs_info;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
470
471
472
  	struct buffer_head *bh;
  	struct nilfs_inode *raw_inode;
  	int err;
365e215ce   Ryusuke Konishi   nilfs2: unfold ni...
473
  	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
474
  	err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
475
476
  	if (unlikely(err))
  		goto bad_inode;
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
477
  	raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
478

1b2f5a641   Ryusuke Konishi   nilfs2: fix ignor...
479
480
  	err = nilfs_read_inode_common(inode, raw_inode);
  	if (err)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
481
482
483
484
485
486
  		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...
487
488
489
490
491
492
  	} 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;
21fc61c73   Al Viro   don't put symlink...
493
  		inode_nohighmem(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
494
495
496
497
498
  		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_...
499
  			huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
500
  	}
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
501
  	nilfs_ifile_unmap_inode(root->ifile, ino, bh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
502
  	brelse(bh);
365e215ce   Ryusuke Konishi   nilfs2: unfold ni...
503
  	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
504
  	nilfs_set_inode_flags(inode);
0ce187c4f   Ryusuke Konishi   nilfs2: put out g...
505
  	mapping_set_gfp_mask(inode->i_mapping,
c62d25556   Michal Hocko   mm, fs: introduce...
506
  			   mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
507
508
509
  	return 0;
  
   failed_unmap:
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
510
  	nilfs_ifile_unmap_inode(root->ifile, ino, bh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
511
512
513
  	brelse(bh);
  
   bad_inode:
365e215ce   Ryusuke Konishi   nilfs2: unfold ni...
514
  	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
515
516
  	return err;
  }
0e14a3595   Ryusuke Konishi   nilfs2: use iget5...
517
518
519
520
  static int nilfs_iget_test(struct inode *inode, void *opaque)
  {
  	struct nilfs_iget_args *args = opaque;
  	struct nilfs_inode_info *ii;
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
521
  	if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
0e14a3595   Ryusuke Konishi   nilfs2: use iget5...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  		return 0;
  
  	ii = NILFS_I(inode);
  	if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
  		return !args->for_gc;
  
  	return args->for_gc && args->cno == ii->i_cno;
  }
  
  static int nilfs_iget_set(struct inode *inode, void *opaque)
  {
  	struct nilfs_iget_args *args = opaque;
  
  	inode->i_ino = args->ino;
  	if (args->for_gc) {
  		NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE;
  		NILFS_I(inode)->i_cno = args->cno;
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
539
540
541
542
543
  		NILFS_I(inode)->i_root = NULL;
  	} else {
  		if (args->root && args->ino == NILFS_ROOT_INO)
  			nilfs_get_root(args->root);
  		NILFS_I(inode)->i_root = args->root;
0e14a3595   Ryusuke Konishi   nilfs2: use iget5...
544
545
546
  	}
  	return 0;
  }
032dbb3b5   Ryusuke Konishi   nilfs2: see state...
547
548
549
550
551
552
553
554
555
  struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
  			    unsigned long ino)
  {
  	struct nilfs_iget_args args = {
  		.ino = ino, .root = root, .cno = 0, .for_gc = 0
  	};
  
  	return ilookup5(sb, ino, nilfs_iget_test, &args);
  }
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
556
557
  struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
  				unsigned long ino)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
558
  {
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
559
560
561
  	struct nilfs_iget_args args = {
  		.ino = ino, .root = root, .cno = 0, .for_gc = 0
  	};
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
562
563
564
565
566
567
568
  
  	return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
  }
  
  struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
  			 unsigned long ino)
  {
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
569
570
  	struct inode *inode;
  	int err;
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
571
  	inode = nilfs_iget_locked(sb, root, ino);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
572
573
574
575
  	if (unlikely(!inode))
  		return ERR_PTR(-ENOMEM);
  	if (!(inode->i_state & I_NEW))
  		return inode;
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
576
  	err = __nilfs_read_inode(sb, root, ino, inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
577
578
579
580
581
582
583
  	if (unlikely(err)) {
  		iget_failed(inode);
  		return ERR_PTR(err);
  	}
  	unlock_new_inode(inode);
  	return inode;
  }
263d90cef   Ryusuke Konishi   nilfs2: remove ow...
584
585
586
  struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
  				__u64 cno)
  {
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
587
588
589
  	struct nilfs_iget_args args = {
  		.ino = ino, .root = NULL, .cno = cno, .for_gc = 1
  	};
263d90cef   Ryusuke Konishi   nilfs2: remove ow...
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
  	struct inode *inode;
  	int err;
  
  	inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
  	if (unlikely(!inode))
  		return ERR_PTR(-ENOMEM);
  	if (!(inode->i_state & I_NEW))
  		return inode;
  
  	err = nilfs_init_gcinode(inode);
  	if (unlikely(err)) {
  		iget_failed(inode);
  		return ERR_PTR(err);
  	}
  	unlock_new_inode(inode);
  	return inode;
  }
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
607
608
609
610
611
612
  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);
305d3d0db   Eric W. Biederman   userns: Convert n...
613
614
  	raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
  	raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
615
616
617
618
  	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...
619
620
  	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...
621
  	raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
622
623
  	raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
56eb55388   Ryusuke Konishi   nilfs2: zero fill...
624
625
626
627
628
629
630
631
632
  	if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
  		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  
  		/* zero-fill unused portion in the case of super root block */
  		raw_inode->i_xattr = 0;
  		raw_inode->i_pad = 0;
  		memset((void *)raw_inode + sizeof(*raw_inode), 0,
  		       nilfs->ns_inode_size - sizeof(*raw_inode));
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
633
634
635
636
  	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_...
637
  			cpu_to_le64(huge_encode_dev(inode->i_rdev));
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
638
639
640
  	/* When extending inode, nilfs->ns_inode_size should be checked
  	   for substitutions of appended fields */
  }
b9f661407   Andreas Rohner   nilfs2: improve t...
641
  void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
642
643
644
  {
  	ino_t ino = inode->i_ino;
  	struct nilfs_inode_info *ii = NILFS_I(inode);
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
645
  	struct inode *ifile = ii->i_root->ifile;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
646
  	struct nilfs_inode *raw_inode;
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
647
  	raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
648

05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
649
  	if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
650
  		memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
b9f661407   Andreas Rohner   nilfs2: improve t...
651
652
  	if (flags & I_DIRTY_DATASYNC)
  		set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
653
654
655
656
657
  
  	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 */
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
658
  	nilfs_ifile_unmap_inode(ifile, ino, ibh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
659
660
661
662
663
664
665
  }
  
  #define NILFS_MAX_TRUNCATE_BLOCKS	16384  /* 64MB for 4KB block */
  
  static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
  				unsigned long from)
  {
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
666
  	__u64 b;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
667
668
669
670
  	int ret;
  
  	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  		return;
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
671
  repeat:
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
672
673
674
675
676
677
678
679
  	ret = nilfs_bmap_last_key(ii->i_bmap, &b);
  	if (ret == -ENOENT)
  		return;
  	else if (ret < 0)
  		goto failed;
  
  	if (b < from)
  		return;
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
680
  	b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
681
682
683
684
685
  	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;
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
686
687
688
689
  failed:
  	nilfs_warning(ii->vfs_inode.i_sb, __func__,
  		      "failed to truncate bmap (ino=%lu, err=%d)",
  		      ii->vfs_inode.i_ino, ret);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
690
691
692
693
694
695
696
697
698
  }
  
  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...
699
700
701
702
703
704
705
706
  
  	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...
707
  	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
708
709
710
711
712
713
714
715
  
  	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...
716
  	nilfs_mark_inode_dirty(inode);
bcbc8c648   Ryusuke Konishi   nilfs2: do not pa...
717
  	nilfs_set_file_dirty(inode, 0);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
718
  	nilfs_transaction_commit(sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
719
720
721
  	/* May construct a logical segment and may fail in sync mode.
  	   But truncate has no return value. */
  }
6fd1e5c99   Al Viro   convert nilfs2 to...
722
723
724
  static void nilfs_clear_inode(struct inode *inode)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
518d1a6a1   Ryusuke Konishi   nilfs2: allow nil...
725
  	struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
6fd1e5c99   Al Viro   convert nilfs2 to...
726
727
728
729
730
731
732
  
  	/*
  	 * Free resources allocated in nilfs_read_inode(), here.
  	 */
  	BUG_ON(!list_empty(&ii->i_dirty));
  	brelse(ii->i_bh);
  	ii->i_bh = NULL;
518d1a6a1   Ryusuke Konishi   nilfs2: allow nil...
733
734
  	if (mdi && mdi->mi_palloc_cache)
  		nilfs_palloc_destroy_cache(inode);
6fd1e5c99   Al Viro   convert nilfs2 to...
735
736
737
738
  	if (test_bit(NILFS_I_BMAP, &ii->i_state))
  		nilfs_bmap_clear(ii->i_bmap);
  
  	nilfs_btnode_cache_clear(&ii->i_btnode_cache);
4d8d9293d   Ryusuke Konishi   nilfs2: set point...
739
740
741
  
  	if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
  		nilfs_put_root(ii->i_root);
6fd1e5c99   Al Viro   convert nilfs2 to...
742
743
744
  }
  
  void nilfs_evict_inode(struct inode *inode)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
745
746
747
748
  {
  	struct nilfs_transaction_info ti;
  	struct super_block *sb = inode->i_sb;
  	struct nilfs_inode_info *ii = NILFS_I(inode);
25b18d39c   Ryusuke Konishi   nilfs2: decrement...
749
  	int ret;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
750

4d8d9293d   Ryusuke Konishi   nilfs2: set point...
751
  	if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
91b0abe36   Johannes Weiner   mm + fs: store sh...
752
  		truncate_inode_pages_final(&inode->i_data);
dbd5768f8   Jan Kara   vfs: Rename end_w...
753
  		clear_inode(inode);
6fd1e5c99   Al Viro   convert nilfs2 to...
754
  		nilfs_clear_inode(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
755
756
  		return;
  	}
1f5abe7e7   Ryusuke Konishi   nilfs2: replace B...
757
  	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
91b0abe36   Johannes Weiner   mm + fs: store sh...
758
  	truncate_inode_pages_final(&inode->i_data);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
759

e912a5b66   Ryusuke Konishi   nilfs2: use root ...
760
  	/* TODO: some of the following operations may fail.  */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
761
  	nilfs_truncate_bmap(ii, 0);
abdb318b7   Jiro SEKIBA   nilfs2: replace m...
762
  	nilfs_mark_inode_dirty(inode);
dbd5768f8   Jan Kara   vfs: Rename end_w...
763
  	clear_inode(inode);
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
764

25b18d39c   Ryusuke Konishi   nilfs2: decrement...
765
766
  	ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
  	if (!ret)
e5f7f8484   Vyacheslav Dubeyko   ] nilfs2: use ato...
767
  		atomic64_dec(&ii->i_root->inodes_count);
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
768

6fd1e5c99   Al Viro   convert nilfs2 to...
769
  	nilfs_clear_inode(inode);
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
770

05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
771
772
  	if (IS_SYNC(inode))
  		nilfs_set_transaction_flag(NILFS_TI_SYNC);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
773
  	nilfs_transaction_commit(sb);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
774
775
776
777
778
779
780
  	/* 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;
2b0143b5c   David Howells   VFS: normal files...
781
  	struct inode *inode = d_inode(dentry);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
782
  	struct super_block *sb = inode->i_sb;
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
783
  	int err;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
784
785
786
787
788
789
790
791
  
  	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
792
793
794
  
  	if ((iattr->ia_valid & ATTR_SIZE) &&
  	    iattr->ia_size != i_size_read(inode)) {
562c72aa5   Christoph Hellwig   fs: move inode_di...
795
  		inode_dio_wait(inode);
2d1b399b2   Marco Stornelli   nilfs2: drop vmtr...
796
797
  		truncate_setsize(inode, iattr->ia_size);
  		nilfs_truncate(inode);
1025774ce   Christoph Hellwig   remove inode_setattr
798
799
800
801
802
803
  	}
  
  	setattr_copy(inode, iattr);
  	mark_inode_dirty(inode);
  
  	if (iattr->ia_valid & ATTR_MODE) {
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
804
  		err = nilfs_acl_chmod(inode);
1025774ce   Christoph Hellwig   remove inode_setattr
805
806
807
808
809
  		if (unlikely(err))
  			goto out_err;
  	}
  
  	return nilfs_transaction_commit(sb);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
810

1025774ce   Christoph Hellwig   remove inode_setattr
811
812
  out_err:
  	nilfs_transaction_abort(sb);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
813
  	return err;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
814
  }
10556cb21   Al Viro   ->permission() sa...
815
  int nilfs_permission(struct inode *inode, int mask)
dc3d3b810   Ryusuke Konishi   nilfs2: deny writ...
816
  {
730e908f3   Al Viro   nilfs2_permission...
817
  	struct nilfs_root *root = NILFS_I(inode)->i_root;
dc3d3b810   Ryusuke Konishi   nilfs2: deny writ...
818
819
820
  	if ((mask & MAY_WRITE) && root &&
  	    root->cno != NILFS_CPTREE_CURRENT_CNO)
  		return -EROFS; /* snapshot is not writable */
2830ba7f3   Al Viro   ->permission() sa...
821
  	return generic_permission(inode, mask);
dc3d3b810   Ryusuke Konishi   nilfs2: deny writ...
822
  }
bcbc8c648   Ryusuke Konishi   nilfs2: do not pa...
823
  int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
824
  {
e3154e974   Ryusuke Konishi   nilfs2: get rid o...
825
  	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
826
827
  	struct nilfs_inode_info *ii = NILFS_I(inode);
  	int err;
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
828
  	spin_lock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
829
  	if (ii->i_bh == NULL) {
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
830
  		spin_unlock(&nilfs->ns_inode_lock);
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
831
832
  		err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
  						  inode->i_ino, pbh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
833
834
  		if (unlikely(err))
  			return err;
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
835
  		spin_lock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
836
837
838
839
840
841
842
843
844
845
  		if (ii->i_bh == NULL)
  			ii->i_bh = *pbh;
  		else {
  			brelse(*pbh);
  			*pbh = ii->i_bh;
  		}
  	} else
  		*pbh = ii->i_bh;
  
  	get_bh(*pbh);
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
846
  	spin_unlock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
847
848
849
850
851
852
  	return 0;
  }
  
  int nilfs_inode_dirty(struct inode *inode)
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
e3154e974   Ryusuke Konishi   nilfs2: get rid o...
853
  	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
854
855
856
  	int ret = 0;
  
  	if (!list_empty(&ii->i_dirty)) {
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
857
  		spin_lock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
858
859
  		ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  			test_bit(NILFS_I_BUSY, &ii->i_state);
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
860
  		spin_unlock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
861
862
863
  	}
  	return ret;
  }
bcbc8c648   Ryusuke Konishi   nilfs2: do not pa...
864
  int nilfs_set_file_dirty(struct inode *inode, unsigned nr_dirty)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
865
866
  {
  	struct nilfs_inode_info *ii = NILFS_I(inode);
e3154e974   Ryusuke Konishi   nilfs2: get rid o...
867
  	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
868

693dd3212   Ryusuke Konishi   nilfs2: move s_in...
869
  	atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
870

458c5b082   Ryusuke Konishi   nilfs2: clean up ...
871
  	if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
872
  		return 0;
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
873
  	spin_lock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
874
875
876
877
878
879
880
  	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. */
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
881
  			nilfs_warning(inode->i_sb, __func__,
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
882
883
884
  				      "cannot get inode (ino=%lu)
  ",
  				      inode->i_ino);
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
885
  			spin_unlock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
886
887
888
  			return -EINVAL; /* NILFS_I_DIRTY may remain for
  					   freeing inode */
  		}
eaae0f37d   Nicolas Kaiser   nilfs2: merge lis...
889
  		list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
890
891
  		set_bit(NILFS_I_QUEUED, &ii->i_state);
  	}
693dd3212   Ryusuke Konishi   nilfs2: move s_in...
892
  	spin_unlock(&nilfs->ns_inode_lock);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
893
894
  	return 0;
  }
b9f661407   Andreas Rohner   nilfs2: improve t...
895
  int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
896
  {
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
897
898
  	struct buffer_head *ibh;
  	int err;
bcbc8c648   Ryusuke Konishi   nilfs2: do not pa...
899
  	err = nilfs_load_inode_block(inode, &ibh);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
900
901
902
903
904
905
  	if (unlikely(err)) {
  		nilfs_warning(inode->i_sb, __func__,
  			      "failed to reget inode block.
  ");
  		return err;
  	}
b9f661407   Andreas Rohner   nilfs2: improve t...
906
  	nilfs_update_inode(inode, ibh, flags);
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
907
  	mark_buffer_dirty(ibh);
e912a5b66   Ryusuke Konishi   nilfs2: use root ...
908
  	nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
909
910
911
912
913
914
915
916
917
918
919
920
921
922
  	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.
   */
aa3857295   Christoph Hellwig   fs: pass exact ty...
923
  void nilfs_dirty_inode(struct inode *inode, int flags)
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
924
925
  {
  	struct nilfs_transaction_info ti;
7d6cd92fe   Ryusuke Konishi   nilfs2: allow nil...
926
  	struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
927
928
929
930
931
932
933
934
  
  	if (is_bad_inode(inode)) {
  		nilfs_warning(inode->i_sb, __func__,
  			      "tried to mark bad_inode dirty. ignored.
  ");
  		dump_stack();
  		return;
  	}
7d6cd92fe   Ryusuke Konishi   nilfs2: allow nil...
935
936
937
938
  	if (mdi) {
  		nilfs_mdt_mark_dirty(inode);
  		return;
  	}
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
939
  	nilfs_transaction_begin(inode->i_sb, &ti, 0);
b9f661407   Andreas Rohner   nilfs2: improve t...
940
  	__nilfs_mark_inode_dirty(inode, flags);
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
941
  	nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fdc   Ryusuke Konishi   nilfs2: inode ope...
942
  }
622daaff0   Ryusuke Konishi   nilfs2: fiemap su...
943
944
945
946
  
  int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  		 __u64 start, __u64 len)
  {
0ef28f9ae   Ryusuke Konishi   nilfs2: get rid o...
947
  	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
622daaff0   Ryusuke Konishi   nilfs2: fiemap su...
948
949
950
951
952
953
954
955
956
957
958
959
  	__u64 logical = 0, phys = 0, size = 0;
  	__u32 flags = 0;
  	loff_t isize;
  	sector_t blkoff, end_blkoff;
  	sector_t delalloc_blkoff;
  	unsigned long delalloc_blklen;
  	unsigned int blkbits = inode->i_blkbits;
  	int ret, n;
  
  	ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  	if (ret)
  		return ret;
5955102c9   Al Viro   wrappers for ->i_...
960
  	inode_lock(inode);
622daaff0   Ryusuke Konishi   nilfs2: fiemap su...
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
  
  	isize = i_size_read(inode);
  
  	blkoff = start >> blkbits;
  	end_blkoff = (start + len - 1) >> blkbits;
  
  	delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
  							&delalloc_blkoff);
  
  	do {
  		__u64 blkphy;
  		unsigned int maxblocks;
  
  		if (delalloc_blklen && blkoff == delalloc_blkoff) {
  			if (size) {
  				/* End of the current extent */
  				ret = fiemap_fill_next_extent(
  					fieinfo, logical, phys, size, flags);
  				if (ret)
  					break;
  			}
  			if (blkoff > end_blkoff)
  				break;
  
  			flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
  			logical = blkoff << blkbits;
  			phys = 0;
  			size = delalloc_blklen << blkbits;
  
  			blkoff = delalloc_blkoff + delalloc_blklen;
  			delalloc_blklen = nilfs_find_uncommitted_extent(
  				inode, blkoff, &delalloc_blkoff);
  			continue;
  		}
  
  		/*
  		 * Limit the number of blocks that we look up so as
  		 * not to get into the next delayed allocation extent.
  		 */
  		maxblocks = INT_MAX;
  		if (delalloc_blklen)
  			maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
  					  maxblocks);
  		blkphy = 0;
  
  		down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  		n = nilfs_bmap_lookup_contig(
  			NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
  		up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  
  		if (n < 0) {
  			int past_eof;
  
  			if (unlikely(n != -ENOENT))
  				break; /* error */
  
  			/* HOLE */
  			blkoff++;
  			past_eof = ((blkoff << blkbits) >= isize);
  
  			if (size) {
  				/* End of the current extent */
  
  				if (past_eof)
  					flags |= FIEMAP_EXTENT_LAST;
  
  				ret = fiemap_fill_next_extent(
  					fieinfo, logical, phys, size, flags);
  				if (ret)
  					break;
  				size = 0;
  			}
  			if (blkoff > end_blkoff || past_eof)
  				break;
  		} else {
  			if (size) {
  				if (phys && blkphy << blkbits == phys + size) {
  					/* The current extent goes on */
  					size += n << blkbits;
  				} else {
  					/* Terminate the current extent */
  					ret = fiemap_fill_next_extent(
  						fieinfo, logical, phys, size,
  						flags);
  					if (ret || blkoff > end_blkoff)
  						break;
  
  					/* Start another extent */
  					flags = FIEMAP_EXTENT_MERGED;
  					logical = blkoff << blkbits;
  					phys = blkphy << blkbits;
  					size = n << blkbits;
  				}
  			} else {
  				/* Start a new extent */
  				flags = FIEMAP_EXTENT_MERGED;
  				logical = blkoff << blkbits;
  				phys = blkphy << blkbits;
  				size = n << blkbits;
  			}
  			blkoff += n;
  		}
  		cond_resched();
  	} while (true);
  
  	/* If ret is 1 then we just hit the end of the extent array */
  	if (ret == 1)
  		ret = 0;
5955102c9   Al Viro   wrappers for ->i_...
1069
  	inode_unlock(inode);
622daaff0   Ryusuke Konishi   nilfs2: fiemap su...
1070
1071
  	return ret;
  }