1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * inode.c - NILFS inode operations.
4  *
5  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6  *
7  * Written by Ryusuke Konishi.
8  *
9  */
10 
11 #include <linux/buffer_head.h>
12 #include <linux/gfp.h>
13 #include <linux/mpage.h>
14 #include <linux/pagemap.h>
15 #include <linux/writeback.h>
16 #include <linux/uio.h>
17 #include "nilfs.h"
18 #include "btnode.h"
19 #include "segment.h"
20 #include "page.h"
21 #include "mdt.h"
22 #include "cpfile.h"
23 #include "ifile.h"
24 
25 /**
26  * struct nilfs_iget_args - arguments used during comparison between inodes
27  * @ino: inode number
28  * @cno: checkpoint number
29  * @root: pointer on NILFS root object (mounted checkpoint)
30  * @for_gc: inode for GC flag
31  * @for_btnc: inode for B-tree node cache flag
32  * @for_shadow: inode for shadowed page cache flag
33  */
34 struct nilfs_iget_args {
35 	u64 ino;
36 	__u64 cno;
37 	struct nilfs_root *root;
38 	bool for_gc;
39 	bool for_btnc;
40 	bool for_shadow;
41 };
42 
43 static int nilfs_iget_test(struct inode *inode, void *opaque);
44 
nilfs_inode_add_blocks(struct inode * inode,int n)45 void nilfs_inode_add_blocks(struct inode *inode, int n)
46 {
47 	struct nilfs_root *root = NILFS_I(inode)->i_root;
48 
49 	inode_add_bytes(inode, i_blocksize(inode) * n);
50 	if (root)
51 		atomic64_add(n, &root->blocks_count);
52 }
53 
nilfs_inode_sub_blocks(struct inode * inode,int n)54 void nilfs_inode_sub_blocks(struct inode *inode, int n)
55 {
56 	struct nilfs_root *root = NILFS_I(inode)->i_root;
57 
58 	inode_sub_bytes(inode, i_blocksize(inode) * n);
59 	if (root)
60 		atomic64_sub(n, &root->blocks_count);
61 }
62 
63 /**
64  * nilfs_get_block() - get a file block on the filesystem (callback function)
65  * @inode - inode struct of the target file
66  * @blkoff - file block number
67  * @bh_result - buffer head to be mapped on
68  * @create - indicate whether allocating the block or not when it has not
69  *      been allocated yet.
70  *
71  * This function does not issue actual read request of the specified data
72  * block. It is done by VFS.
73  */
nilfs_get_block(struct inode * inode,sector_t blkoff,struct buffer_head * bh_result,int create)74 int nilfs_get_block(struct inode *inode, sector_t blkoff,
75 		    struct buffer_head *bh_result, int create)
76 {
77 	struct nilfs_inode_info *ii = NILFS_I(inode);
78 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
79 	__u64 blknum = 0;
80 	int err = 0, ret;
81 	unsigned int maxblocks = bh_result->b_size >> inode->i_blkbits;
82 
83 	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
84 	ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
85 	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
86 	if (ret >= 0) {	/* found */
87 		map_bh(bh_result, inode->i_sb, blknum);
88 		if (ret > 0)
89 			bh_result->b_size = (ret << inode->i_blkbits);
90 		goto out;
91 	}
92 	/* data block was not found */
93 	if (ret == -ENOENT && create) {
94 		struct nilfs_transaction_info ti;
95 
96 		bh_result->b_blocknr = 0;
97 		err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
98 		if (unlikely(err))
99 			goto out;
100 		err = nilfs_bmap_insert(ii->i_bmap, blkoff,
101 					(unsigned long)bh_result);
102 		if (unlikely(err != 0)) {
103 			if (err == -EEXIST) {
104 				/*
105 				 * The get_block() function could be called
106 				 * from multiple callers for an inode.
107 				 * However, the page having this block must
108 				 * be locked in this case.
109 				 */
110 				nilfs_msg(inode->i_sb, KERN_WARNING,
111 					  "%s (ino=%lu): a race condition while inserting a data block at offset=%llu",
112 					  __func__, inode->i_ino,
113 					  (unsigned long long)blkoff);
114 				err = 0;
115 			}
116 			nilfs_transaction_abort(inode->i_sb);
117 			goto out;
118 		}
119 		nilfs_mark_inode_dirty_sync(inode);
120 		nilfs_transaction_commit(inode->i_sb); /* never fails */
121 		/* Error handling should be detailed */
122 		set_buffer_new(bh_result);
123 		set_buffer_delay(bh_result);
124 		map_bh(bh_result, inode->i_sb, 0);
125 		/* Disk block number must be changed to proper value */
126 
127 	} else if (ret == -ENOENT) {
128 		/*
129 		 * not found is not error (e.g. hole); must return without
130 		 * the mapped state flag.
131 		 */
132 		;
133 	} else {
134 		err = ret;
135 	}
136 
137  out:
138 	return err;
139 }
140 
141 /**
142  * nilfs_readpage() - implement readpage() method of nilfs_aops {}
143  * address_space_operations.
144  * @file - file struct of the file to be read
145  * @page - the page to be read
146  */
nilfs_readpage(struct file * file,struct page * page)147 static int nilfs_readpage(struct file *file, struct page *page)
148 {
149 	return mpage_readpage(page, nilfs_get_block);
150 }
151 
152 /**
153  * nilfs_readpages() - implement readpages() method of nilfs_aops {}
154  * address_space_operations.
155  * @file - file struct of the file to be read
156  * @mapping - address_space struct used for reading multiple pages
157  * @pages - the pages to be read
158  * @nr_pages - number of pages to be read
159  */
nilfs_readpages(struct file * file,struct address_space * mapping,struct list_head * pages,unsigned int nr_pages)160 static int nilfs_readpages(struct file *file, struct address_space *mapping,
161 			   struct list_head *pages, unsigned int nr_pages)
162 {
163 	return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
164 }
165 
nilfs_writepages(struct address_space * mapping,struct writeback_control * wbc)166 static int nilfs_writepages(struct address_space *mapping,
167 			    struct writeback_control *wbc)
168 {
169 	struct inode *inode = mapping->host;
170 	int err = 0;
171 
172 	if (sb_rdonly(inode->i_sb)) {
173 		nilfs_clear_dirty_pages(mapping, false);
174 		return -EROFS;
175 	}
176 
177 	if (wbc->sync_mode == WB_SYNC_ALL)
178 		err = nilfs_construct_dsync_segment(inode->i_sb, inode,
179 						    wbc->range_start,
180 						    wbc->range_end);
181 	return err;
182 }
183 
nilfs_writepage(struct page * page,struct writeback_control * wbc)184 static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
185 {
186 	struct inode *inode = page->mapping->host;
187 	int err;
188 
189 	if (sb_rdonly(inode->i_sb)) {
190 		/*
191 		 * It means that filesystem was remounted in read-only
192 		 * mode because of error or metadata corruption. But we
193 		 * have dirty pages that try to be flushed in background.
194 		 * So, here we simply discard this dirty page.
195 		 */
196 		nilfs_clear_dirty_page(page, false);
197 		unlock_page(page);
198 		return -EROFS;
199 	}
200 
201 	redirty_page_for_writepage(wbc, page);
202 	unlock_page(page);
203 
204 	if (wbc->sync_mode == WB_SYNC_ALL) {
205 		err = nilfs_construct_segment(inode->i_sb);
206 		if (unlikely(err))
207 			return err;
208 	} else if (wbc->for_reclaim)
209 		nilfs_flush_segment(inode->i_sb, inode->i_ino);
210 
211 	return 0;
212 }
213 
nilfs_set_page_dirty(struct page * page)214 static int nilfs_set_page_dirty(struct page *page)
215 {
216 	struct inode *inode = page->mapping->host;
217 	int ret = __set_page_dirty_nobuffers(page);
218 
219 	if (page_has_buffers(page)) {
220 		unsigned int nr_dirty = 0;
221 		struct buffer_head *bh, *head;
222 
223 		/*
224 		 * This page is locked by callers, and no other thread
225 		 * concurrently marks its buffers dirty since they are
226 		 * only dirtied through routines in fs/buffer.c in
227 		 * which call sites of mark_buffer_dirty are protected
228 		 * by page lock.
229 		 */
230 		bh = head = page_buffers(page);
231 		do {
232 			/* Do not mark hole blocks dirty */
233 			if (buffer_dirty(bh) || !buffer_mapped(bh))
234 				continue;
235 
236 			set_buffer_dirty(bh);
237 			nr_dirty++;
238 		} while (bh = bh->b_this_page, bh != head);
239 
240 		if (nr_dirty)
241 			nilfs_set_file_dirty(inode, nr_dirty);
242 	} else if (ret) {
243 		unsigned int nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
244 
245 		nilfs_set_file_dirty(inode, nr_dirty);
246 	}
247 	return ret;
248 }
249 
nilfs_write_failed(struct address_space * mapping,loff_t to)250 void nilfs_write_failed(struct address_space *mapping, loff_t to)
251 {
252 	struct inode *inode = mapping->host;
253 
254 	if (to > inode->i_size) {
255 		truncate_pagecache(inode, inode->i_size);
256 		nilfs_truncate(inode);
257 	}
258 }
259 
nilfs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)260 static int nilfs_write_begin(struct file *file, struct address_space *mapping,
261 			     loff_t pos, unsigned len, unsigned flags,
262 			     struct page **pagep, void **fsdata)
263 
264 {
265 	struct inode *inode = mapping->host;
266 	int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
267 
268 	if (unlikely(err))
269 		return err;
270 
271 	err = block_write_begin(mapping, pos, len, flags, pagep,
272 				nilfs_get_block);
273 	if (unlikely(err)) {
274 		nilfs_write_failed(mapping, pos + len);
275 		nilfs_transaction_abort(inode->i_sb);
276 	}
277 	return err;
278 }
279 
nilfs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)280 static int nilfs_write_end(struct file *file, struct address_space *mapping,
281 			   loff_t pos, unsigned len, unsigned copied,
282 			   struct page *page, void *fsdata)
283 {
284 	struct inode *inode = mapping->host;
285 	unsigned int start = pos & (PAGE_SIZE - 1);
286 	unsigned int nr_dirty;
287 	int err;
288 
289 	nr_dirty = nilfs_page_count_clean_buffers(page, start,
290 						  start + copied);
291 	copied = generic_write_end(file, mapping, pos, len, copied, page,
292 				   fsdata);
293 	nilfs_set_file_dirty(inode, nr_dirty);
294 	err = nilfs_transaction_commit(inode->i_sb);
295 	return err ? : copied;
296 }
297 
298 static ssize_t
nilfs_direct_IO(struct kiocb * iocb,struct iov_iter * iter)299 nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
300 {
301 	struct inode *inode = file_inode(iocb->ki_filp);
302 
303 	if (iov_iter_rw(iter) == WRITE)
304 		return 0;
305 
306 	/* Needs synchronization with the cleaner */
307 	return blockdev_direct_IO(iocb, inode, iter, nilfs_get_block);
308 }
309 
310 const struct address_space_operations nilfs_aops = {
311 	.writepage		= nilfs_writepage,
312 	.readpage		= nilfs_readpage,
313 	.writepages		= nilfs_writepages,
314 	.set_page_dirty		= nilfs_set_page_dirty,
315 	.readpages		= nilfs_readpages,
316 	.write_begin		= nilfs_write_begin,
317 	.write_end		= nilfs_write_end,
318 	/* .releasepage		= nilfs_releasepage, */
319 	.invalidatepage		= block_invalidatepage,
320 	.direct_IO		= nilfs_direct_IO,
321 	.is_partially_uptodate  = block_is_partially_uptodate,
322 };
323 
nilfs_insert_inode_locked(struct inode * inode,struct nilfs_root * root,unsigned long ino)324 static int nilfs_insert_inode_locked(struct inode *inode,
325 				     struct nilfs_root *root,
326 				     unsigned long ino)
327 {
328 	struct nilfs_iget_args args = {
329 		.ino = ino, .root = root, .cno = 0, .for_gc = false,
330 		.for_btnc = false, .for_shadow = false
331 	};
332 
333 	return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
334 }
335 
nilfs_new_inode(struct inode * dir,umode_t mode)336 struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
337 {
338 	struct super_block *sb = dir->i_sb;
339 	struct the_nilfs *nilfs = sb->s_fs_info;
340 	struct inode *inode;
341 	struct nilfs_inode_info *ii;
342 	struct nilfs_root *root;
343 	struct buffer_head *bh;
344 	int err = -ENOMEM;
345 	ino_t ino;
346 
347 	inode = new_inode(sb);
348 	if (unlikely(!inode))
349 		goto failed;
350 
351 	mapping_set_gfp_mask(inode->i_mapping,
352 			   mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
353 
354 	root = NILFS_I(dir)->i_root;
355 	ii = NILFS_I(inode);
356 	ii->i_state = BIT(NILFS_I_NEW);
357 	ii->i_root = root;
358 
359 	err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
360 	if (unlikely(err))
361 		goto failed_ifile_create_inode;
362 	/* reference count of i_bh inherits from nilfs_mdt_read_block() */
363 
364 	if (unlikely(ino < NILFS_USER_INO)) {
365 		nilfs_msg(sb, KERN_WARNING,
366 			  "inode bitmap is inconsistent for reserved inodes");
367 		do {
368 			brelse(bh);
369 			err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
370 			if (unlikely(err))
371 				goto failed_ifile_create_inode;
372 		} while (ino < NILFS_USER_INO);
373 
374 		nilfs_msg(sb, KERN_INFO,
375 			  "repaired inode bitmap for reserved inodes");
376 	}
377 	ii->i_bh = bh;
378 
379 	atomic64_inc(&root->inodes_count);
380 	inode_init_owner(inode, dir, mode);
381 	inode->i_ino = ino;
382 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
383 
384 	if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
385 		err = nilfs_bmap_read(ii->i_bmap, NULL);
386 		if (err < 0)
387 			goto failed_after_creation;
388 
389 		set_bit(NILFS_I_BMAP, &ii->i_state);
390 		/* No lock is needed; iget() ensures it. */
391 	}
392 
393 	ii->i_flags = nilfs_mask_flags(
394 		mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
395 
396 	/* ii->i_file_acl = 0; */
397 	/* ii->i_dir_acl = 0; */
398 	ii->i_dir_start_lookup = 0;
399 	nilfs_set_inode_flags(inode);
400 	spin_lock(&nilfs->ns_next_gen_lock);
401 	inode->i_generation = nilfs->ns_next_generation++;
402 	spin_unlock(&nilfs->ns_next_gen_lock);
403 	if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
404 		err = -EIO;
405 		goto failed_after_creation;
406 	}
407 
408 	err = nilfs_init_acl(inode, dir);
409 	if (unlikely(err))
410 		/*
411 		 * Never occur.  When supporting nilfs_init_acl(),
412 		 * proper cancellation of above jobs should be considered.
413 		 */
414 		goto failed_after_creation;
415 
416 	return inode;
417 
418  failed_after_creation:
419 	clear_nlink(inode);
420 	unlock_new_inode(inode);
421 	iput(inode);  /*
422 		       * raw_inode will be deleted through
423 		       * nilfs_evict_inode().
424 		       */
425 	goto failed;
426 
427  failed_ifile_create_inode:
428 	make_bad_inode(inode);
429 	iput(inode);
430  failed:
431 	return ERR_PTR(err);
432 }
433 
nilfs_set_inode_flags(struct inode * inode)434 void nilfs_set_inode_flags(struct inode *inode)
435 {
436 	unsigned int flags = NILFS_I(inode)->i_flags;
437 	unsigned int new_fl = 0;
438 
439 	if (flags & FS_SYNC_FL)
440 		new_fl |= S_SYNC;
441 	if (flags & FS_APPEND_FL)
442 		new_fl |= S_APPEND;
443 	if (flags & FS_IMMUTABLE_FL)
444 		new_fl |= S_IMMUTABLE;
445 	if (flags & FS_NOATIME_FL)
446 		new_fl |= S_NOATIME;
447 	if (flags & FS_DIRSYNC_FL)
448 		new_fl |= S_DIRSYNC;
449 	inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
450 			S_NOATIME | S_DIRSYNC);
451 }
452 
nilfs_read_inode_common(struct inode * inode,struct nilfs_inode * raw_inode)453 int nilfs_read_inode_common(struct inode *inode,
454 			    struct nilfs_inode *raw_inode)
455 {
456 	struct nilfs_inode_info *ii = NILFS_I(inode);
457 	int err;
458 
459 	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
460 	i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
461 	i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
462 	set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
463 	inode->i_size = le64_to_cpu(raw_inode->i_size);
464 	inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
465 	inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
466 	inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
467 	inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
468 	inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
469 	inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
470 	if (nilfs_is_metadata_file_inode(inode) && !S_ISREG(inode->i_mode))
471 		return -EIO; /* this inode is for metadata and corrupted */
472 	if (inode->i_nlink == 0)
473 		return -ESTALE; /* this inode is deleted */
474 
475 	inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
476 	ii->i_flags = le32_to_cpu(raw_inode->i_flags);
477 #if 0
478 	ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
479 	ii->i_dir_acl = S_ISREG(inode->i_mode) ?
480 		0 : le32_to_cpu(raw_inode->i_dir_acl);
481 #endif
482 	ii->i_dir_start_lookup = 0;
483 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
484 
485 	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
486 	    S_ISLNK(inode->i_mode)) {
487 		err = nilfs_bmap_read(ii->i_bmap, raw_inode);
488 		if (err < 0)
489 			return err;
490 		set_bit(NILFS_I_BMAP, &ii->i_state);
491 		/* No lock is needed; iget() ensures it. */
492 	}
493 	return 0;
494 }
495 
__nilfs_read_inode(struct super_block * sb,struct nilfs_root * root,unsigned long ino,struct inode * inode)496 static int __nilfs_read_inode(struct super_block *sb,
497 			      struct nilfs_root *root, unsigned long ino,
498 			      struct inode *inode)
499 {
500 	struct the_nilfs *nilfs = sb->s_fs_info;
501 	struct buffer_head *bh;
502 	struct nilfs_inode *raw_inode;
503 	int err;
504 
505 	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
506 	err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
507 	if (unlikely(err))
508 		goto bad_inode;
509 
510 	raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
511 
512 	err = nilfs_read_inode_common(inode, raw_inode);
513 	if (err)
514 		goto failed_unmap;
515 
516 	if (S_ISREG(inode->i_mode)) {
517 		inode->i_op = &nilfs_file_inode_operations;
518 		inode->i_fop = &nilfs_file_operations;
519 		inode->i_mapping->a_ops = &nilfs_aops;
520 	} else if (S_ISDIR(inode->i_mode)) {
521 		inode->i_op = &nilfs_dir_inode_operations;
522 		inode->i_fop = &nilfs_dir_operations;
523 		inode->i_mapping->a_ops = &nilfs_aops;
524 	} else if (S_ISLNK(inode->i_mode)) {
525 		inode->i_op = &nilfs_symlink_inode_operations;
526 		inode_nohighmem(inode);
527 		inode->i_mapping->a_ops = &nilfs_aops;
528 	} else {
529 		inode->i_op = &nilfs_special_inode_operations;
530 		init_special_inode(
531 			inode, inode->i_mode,
532 			huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
533 	}
534 	nilfs_ifile_unmap_inode(root->ifile, ino, bh);
535 	brelse(bh);
536 	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
537 	nilfs_set_inode_flags(inode);
538 	mapping_set_gfp_mask(inode->i_mapping,
539 			   mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
540 	return 0;
541 
542  failed_unmap:
543 	nilfs_ifile_unmap_inode(root->ifile, ino, bh);
544 	brelse(bh);
545 
546  bad_inode:
547 	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
548 	return err;
549 }
550 
nilfs_iget_test(struct inode * inode,void * opaque)551 static int nilfs_iget_test(struct inode *inode, void *opaque)
552 {
553 	struct nilfs_iget_args *args = opaque;
554 	struct nilfs_inode_info *ii;
555 
556 	if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
557 		return 0;
558 
559 	ii = NILFS_I(inode);
560 	if (test_bit(NILFS_I_BTNC, &ii->i_state)) {
561 		if (!args->for_btnc)
562 			return 0;
563 	} else if (args->for_btnc) {
564 		return 0;
565 	}
566 	if (test_bit(NILFS_I_SHADOW, &ii->i_state)) {
567 		if (!args->for_shadow)
568 			return 0;
569 	} else if (args->for_shadow) {
570 		return 0;
571 	}
572 
573 	if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
574 		return !args->for_gc;
575 
576 	return args->for_gc && args->cno == ii->i_cno;
577 }
578 
nilfs_iget_set(struct inode * inode,void * opaque)579 static int nilfs_iget_set(struct inode *inode, void *opaque)
580 {
581 	struct nilfs_iget_args *args = opaque;
582 
583 	inode->i_ino = args->ino;
584 	NILFS_I(inode)->i_cno = args->cno;
585 	NILFS_I(inode)->i_root = args->root;
586 	if (args->root && args->ino == NILFS_ROOT_INO)
587 		nilfs_get_root(args->root);
588 
589 	if (args->for_gc)
590 		NILFS_I(inode)->i_state = BIT(NILFS_I_GCINODE);
591 	if (args->for_btnc)
592 		NILFS_I(inode)->i_state |= BIT(NILFS_I_BTNC);
593 	if (args->for_shadow)
594 		NILFS_I(inode)->i_state |= BIT(NILFS_I_SHADOW);
595 	return 0;
596 }
597 
nilfs_ilookup(struct super_block * sb,struct nilfs_root * root,unsigned long ino)598 struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
599 			    unsigned long ino)
600 {
601 	struct nilfs_iget_args args = {
602 		.ino = ino, .root = root, .cno = 0, .for_gc = false,
603 		.for_btnc = false, .for_shadow = false
604 	};
605 
606 	return ilookup5(sb, ino, nilfs_iget_test, &args);
607 }
608 
nilfs_iget_locked(struct super_block * sb,struct nilfs_root * root,unsigned long ino)609 struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
610 				unsigned long ino)
611 {
612 	struct nilfs_iget_args args = {
613 		.ino = ino, .root = root, .cno = 0, .for_gc = false,
614 		.for_btnc = false, .for_shadow = false
615 	};
616 
617 	return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
618 }
619 
nilfs_iget(struct super_block * sb,struct nilfs_root * root,unsigned long ino)620 struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
621 			 unsigned long ino)
622 {
623 	struct inode *inode;
624 	int err;
625 
626 	inode = nilfs_iget_locked(sb, root, ino);
627 	if (unlikely(!inode))
628 		return ERR_PTR(-ENOMEM);
629 	if (!(inode->i_state & I_NEW))
630 		return inode;
631 
632 	err = __nilfs_read_inode(sb, root, ino, inode);
633 	if (unlikely(err)) {
634 		iget_failed(inode);
635 		return ERR_PTR(err);
636 	}
637 	unlock_new_inode(inode);
638 	return inode;
639 }
640 
nilfs_iget_for_gc(struct super_block * sb,unsigned long ino,__u64 cno)641 struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
642 				__u64 cno)
643 {
644 	struct nilfs_iget_args args = {
645 		.ino = ino, .root = NULL, .cno = cno, .for_gc = true,
646 		.for_btnc = false, .for_shadow = false
647 	};
648 	struct inode *inode;
649 	int err;
650 
651 	inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
652 	if (unlikely(!inode))
653 		return ERR_PTR(-ENOMEM);
654 	if (!(inode->i_state & I_NEW))
655 		return inode;
656 
657 	err = nilfs_init_gcinode(inode);
658 	if (unlikely(err)) {
659 		iget_failed(inode);
660 		return ERR_PTR(err);
661 	}
662 	unlock_new_inode(inode);
663 	return inode;
664 }
665 
666 /**
667  * nilfs_attach_btree_node_cache - attach a B-tree node cache to the inode
668  * @inode: inode object
669  *
670  * nilfs_attach_btree_node_cache() attaches a B-tree node cache to @inode,
671  * or does nothing if the inode already has it.  This function allocates
672  * an additional inode to maintain page cache of B-tree nodes one-on-one.
673  *
674  * Return Value: On success, 0 is returned. On errors, one of the following
675  * negative error code is returned.
676  *
677  * %-ENOMEM - Insufficient memory available.
678  */
nilfs_attach_btree_node_cache(struct inode * inode)679 int nilfs_attach_btree_node_cache(struct inode *inode)
680 {
681 	struct nilfs_inode_info *ii = NILFS_I(inode);
682 	struct inode *btnc_inode;
683 	struct nilfs_iget_args args;
684 
685 	if (ii->i_assoc_inode)
686 		return 0;
687 
688 	args.ino = inode->i_ino;
689 	args.root = ii->i_root;
690 	args.cno = ii->i_cno;
691 	args.for_gc = test_bit(NILFS_I_GCINODE, &ii->i_state) != 0;
692 	args.for_btnc = true;
693 	args.for_shadow = test_bit(NILFS_I_SHADOW, &ii->i_state) != 0;
694 
695 	btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
696 				  nilfs_iget_set, &args);
697 	if (unlikely(!btnc_inode))
698 		return -ENOMEM;
699 	if (btnc_inode->i_state & I_NEW) {
700 		nilfs_init_btnc_inode(btnc_inode);
701 		unlock_new_inode(btnc_inode);
702 	}
703 	NILFS_I(btnc_inode)->i_assoc_inode = inode;
704 	NILFS_I(btnc_inode)->i_bmap = ii->i_bmap;
705 	ii->i_assoc_inode = btnc_inode;
706 
707 	return 0;
708 }
709 
710 /**
711  * nilfs_detach_btree_node_cache - detach the B-tree node cache from the inode
712  * @inode: inode object
713  *
714  * nilfs_detach_btree_node_cache() detaches the B-tree node cache and its
715  * holder inode bound to @inode, or does nothing if @inode doesn't have it.
716  */
nilfs_detach_btree_node_cache(struct inode * inode)717 void nilfs_detach_btree_node_cache(struct inode *inode)
718 {
719 	struct nilfs_inode_info *ii = NILFS_I(inode);
720 	struct inode *btnc_inode = ii->i_assoc_inode;
721 
722 	if (btnc_inode) {
723 		NILFS_I(btnc_inode)->i_assoc_inode = NULL;
724 		ii->i_assoc_inode = NULL;
725 		iput(btnc_inode);
726 	}
727 }
728 
729 /**
730  * nilfs_iget_for_shadow - obtain inode for shadow mapping
731  * @inode: inode object that uses shadow mapping
732  *
733  * nilfs_iget_for_shadow() allocates a pair of inodes that holds page
734  * caches for shadow mapping.  The page cache for data pages is set up
735  * in one inode and the one for b-tree node pages is set up in the
736  * other inode, which is attached to the former inode.
737  *
738  * Return Value: On success, a pointer to the inode for data pages is
739  * returned. On errors, one of the following negative error code is returned
740  * in a pointer type.
741  *
742  * %-ENOMEM - Insufficient memory available.
743  */
nilfs_iget_for_shadow(struct inode * inode)744 struct inode *nilfs_iget_for_shadow(struct inode *inode)
745 {
746 	struct nilfs_iget_args args = {
747 		.ino = inode->i_ino, .root = NULL, .cno = 0, .for_gc = false,
748 		.for_btnc = false, .for_shadow = true
749 	};
750 	struct inode *s_inode;
751 	int err;
752 
753 	s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
754 			       nilfs_iget_set, &args);
755 	if (unlikely(!s_inode))
756 		return ERR_PTR(-ENOMEM);
757 	if (!(s_inode->i_state & I_NEW))
758 		return inode;
759 
760 	NILFS_I(s_inode)->i_flags = 0;
761 	memset(NILFS_I(s_inode)->i_bmap, 0, sizeof(struct nilfs_bmap));
762 	mapping_set_gfp_mask(s_inode->i_mapping, GFP_NOFS);
763 
764 	err = nilfs_attach_btree_node_cache(s_inode);
765 	if (unlikely(err)) {
766 		iget_failed(s_inode);
767 		return ERR_PTR(err);
768 	}
769 	unlock_new_inode(s_inode);
770 	return s_inode;
771 }
772 
nilfs_write_inode_common(struct inode * inode,struct nilfs_inode * raw_inode,int has_bmap)773 void nilfs_write_inode_common(struct inode *inode,
774 			      struct nilfs_inode *raw_inode, int has_bmap)
775 {
776 	struct nilfs_inode_info *ii = NILFS_I(inode);
777 
778 	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
779 	raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
780 	raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
781 	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
782 	raw_inode->i_size = cpu_to_le64(inode->i_size);
783 	raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
784 	raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
785 	raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
786 	raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
787 	raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
788 
789 	raw_inode->i_flags = cpu_to_le32(ii->i_flags);
790 	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
791 
792 	if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
793 		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
794 
795 		/* zero-fill unused portion in the case of super root block */
796 		raw_inode->i_xattr = 0;
797 		raw_inode->i_pad = 0;
798 		memset((void *)raw_inode + sizeof(*raw_inode), 0,
799 		       nilfs->ns_inode_size - sizeof(*raw_inode));
800 	}
801 
802 	if (has_bmap)
803 		nilfs_bmap_write(ii->i_bmap, raw_inode);
804 	else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
805 		raw_inode->i_device_code =
806 			cpu_to_le64(huge_encode_dev(inode->i_rdev));
807 	/*
808 	 * When extending inode, nilfs->ns_inode_size should be checked
809 	 * for substitutions of appended fields.
810 	 */
811 }
812 
nilfs_update_inode(struct inode * inode,struct buffer_head * ibh,int flags)813 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
814 {
815 	ino_t ino = inode->i_ino;
816 	struct nilfs_inode_info *ii = NILFS_I(inode);
817 	struct inode *ifile = ii->i_root->ifile;
818 	struct nilfs_inode *raw_inode;
819 
820 	raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
821 
822 	if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
823 		memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
824 	if (flags & I_DIRTY_DATASYNC)
825 		set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
826 
827 	nilfs_write_inode_common(inode, raw_inode, 0);
828 		/*
829 		 * XXX: call with has_bmap = 0 is a workaround to avoid
830 		 * deadlock of bmap.  This delays update of i_bmap to just
831 		 * before writing.
832 		 */
833 
834 	nilfs_ifile_unmap_inode(ifile, ino, ibh);
835 }
836 
837 #define NILFS_MAX_TRUNCATE_BLOCKS	16384  /* 64MB for 4KB block */
838 
nilfs_truncate_bmap(struct nilfs_inode_info * ii,unsigned long from)839 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
840 				unsigned long from)
841 {
842 	__u64 b;
843 	int ret;
844 
845 	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
846 		return;
847 repeat:
848 	ret = nilfs_bmap_last_key(ii->i_bmap, &b);
849 	if (ret == -ENOENT)
850 		return;
851 	else if (ret < 0)
852 		goto failed;
853 
854 	if (b < from)
855 		return;
856 
857 	b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
858 	ret = nilfs_bmap_truncate(ii->i_bmap, b);
859 	nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
860 	if (!ret || (ret == -ENOMEM &&
861 		     nilfs_bmap_truncate(ii->i_bmap, b) == 0))
862 		goto repeat;
863 
864 failed:
865 	nilfs_msg(ii->vfs_inode.i_sb, KERN_WARNING,
866 		  "error %d truncating bmap (ino=%lu)", ret,
867 		  ii->vfs_inode.i_ino);
868 }
869 
nilfs_truncate(struct inode * inode)870 void nilfs_truncate(struct inode *inode)
871 {
872 	unsigned long blkoff;
873 	unsigned int blocksize;
874 	struct nilfs_transaction_info ti;
875 	struct super_block *sb = inode->i_sb;
876 	struct nilfs_inode_info *ii = NILFS_I(inode);
877 
878 	if (!test_bit(NILFS_I_BMAP, &ii->i_state))
879 		return;
880 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
881 		return;
882 
883 	blocksize = sb->s_blocksize;
884 	blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
885 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
886 
887 	block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
888 
889 	nilfs_truncate_bmap(ii, blkoff);
890 
891 	inode->i_mtime = inode->i_ctime = current_time(inode);
892 	if (IS_SYNC(inode))
893 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
894 
895 	nilfs_mark_inode_dirty(inode);
896 	nilfs_set_file_dirty(inode, 0);
897 	nilfs_transaction_commit(sb);
898 	/*
899 	 * May construct a logical segment and may fail in sync mode.
900 	 * But truncate has no return value.
901 	 */
902 }
903 
nilfs_clear_inode(struct inode * inode)904 static void nilfs_clear_inode(struct inode *inode)
905 {
906 	struct nilfs_inode_info *ii = NILFS_I(inode);
907 
908 	/*
909 	 * Free resources allocated in nilfs_read_inode(), here.
910 	 */
911 	BUG_ON(!list_empty(&ii->i_dirty));
912 	brelse(ii->i_bh);
913 	ii->i_bh = NULL;
914 
915 	if (nilfs_is_metadata_file_inode(inode))
916 		nilfs_mdt_clear(inode);
917 
918 	if (test_bit(NILFS_I_BMAP, &ii->i_state))
919 		nilfs_bmap_clear(ii->i_bmap);
920 
921 	if (!test_bit(NILFS_I_BTNC, &ii->i_state))
922 		nilfs_detach_btree_node_cache(inode);
923 
924 	if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
925 		nilfs_put_root(ii->i_root);
926 }
927 
nilfs_evict_inode(struct inode * inode)928 void nilfs_evict_inode(struct inode *inode)
929 {
930 	struct nilfs_transaction_info ti;
931 	struct super_block *sb = inode->i_sb;
932 	struct nilfs_inode_info *ii = NILFS_I(inode);
933 	struct the_nilfs *nilfs;
934 	int ret;
935 
936 	if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
937 		truncate_inode_pages_final(&inode->i_data);
938 		clear_inode(inode);
939 		nilfs_clear_inode(inode);
940 		return;
941 	}
942 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
943 
944 	truncate_inode_pages_final(&inode->i_data);
945 
946 	nilfs = sb->s_fs_info;
947 	if (unlikely(sb_rdonly(sb) || !nilfs->ns_writer)) {
948 		/*
949 		 * If this inode is about to be disposed after the file system
950 		 * has been degraded to read-only due to file system corruption
951 		 * or after the writer has been detached, do not make any
952 		 * changes that cause writes, just clear it.
953 		 * Do this check after read-locking ns_segctor_sem by
954 		 * nilfs_transaction_begin() in order to avoid a race with
955 		 * the writer detach operation.
956 		 */
957 		clear_inode(inode);
958 		nilfs_clear_inode(inode);
959 		nilfs_transaction_abort(sb);
960 		return;
961 	}
962 
963 	/* TODO: some of the following operations may fail.  */
964 	nilfs_truncate_bmap(ii, 0);
965 	nilfs_mark_inode_dirty(inode);
966 	clear_inode(inode);
967 
968 	ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
969 	if (!ret)
970 		atomic64_dec(&ii->i_root->inodes_count);
971 
972 	nilfs_clear_inode(inode);
973 
974 	if (IS_SYNC(inode))
975 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
976 	nilfs_transaction_commit(sb);
977 	/*
978 	 * May construct a logical segment and may fail in sync mode.
979 	 * But delete_inode has no return value.
980 	 */
981 }
982 
nilfs_setattr(struct dentry * dentry,struct iattr * iattr)983 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
984 {
985 	struct nilfs_transaction_info ti;
986 	struct inode *inode = d_inode(dentry);
987 	struct super_block *sb = inode->i_sb;
988 	int err;
989 
990 	err = setattr_prepare(dentry, iattr);
991 	if (err)
992 		return err;
993 
994 	err = nilfs_transaction_begin(sb, &ti, 0);
995 	if (unlikely(err))
996 		return err;
997 
998 	if ((iattr->ia_valid & ATTR_SIZE) &&
999 	    iattr->ia_size != i_size_read(inode)) {
1000 		inode_dio_wait(inode);
1001 		truncate_setsize(inode, iattr->ia_size);
1002 		nilfs_truncate(inode);
1003 	}
1004 
1005 	setattr_copy(inode, iattr);
1006 	mark_inode_dirty(inode);
1007 
1008 	if (iattr->ia_valid & ATTR_MODE) {
1009 		err = nilfs_acl_chmod(inode);
1010 		if (unlikely(err))
1011 			goto out_err;
1012 	}
1013 
1014 	return nilfs_transaction_commit(sb);
1015 
1016 out_err:
1017 	nilfs_transaction_abort(sb);
1018 	return err;
1019 }
1020 
nilfs_permission(struct inode * inode,int mask)1021 int nilfs_permission(struct inode *inode, int mask)
1022 {
1023 	struct nilfs_root *root = NILFS_I(inode)->i_root;
1024 
1025 	if ((mask & MAY_WRITE) && root &&
1026 	    root->cno != NILFS_CPTREE_CURRENT_CNO)
1027 		return -EROFS; /* snapshot is not writable */
1028 
1029 	return generic_permission(inode, mask);
1030 }
1031 
nilfs_load_inode_block(struct inode * inode,struct buffer_head ** pbh)1032 int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
1033 {
1034 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1035 	struct nilfs_inode_info *ii = NILFS_I(inode);
1036 	int err;
1037 
1038 	spin_lock(&nilfs->ns_inode_lock);
1039 	if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) {
1040 		spin_unlock(&nilfs->ns_inode_lock);
1041 		err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
1042 						  inode->i_ino, pbh);
1043 		if (unlikely(err))
1044 			return err;
1045 		spin_lock(&nilfs->ns_inode_lock);
1046 		if (ii->i_bh == NULL)
1047 			ii->i_bh = *pbh;
1048 		else if (unlikely(!buffer_uptodate(ii->i_bh))) {
1049 			__brelse(ii->i_bh);
1050 			ii->i_bh = *pbh;
1051 		} else {
1052 			brelse(*pbh);
1053 			*pbh = ii->i_bh;
1054 		}
1055 	} else
1056 		*pbh = ii->i_bh;
1057 
1058 	get_bh(*pbh);
1059 	spin_unlock(&nilfs->ns_inode_lock);
1060 	return 0;
1061 }
1062 
nilfs_inode_dirty(struct inode * inode)1063 int nilfs_inode_dirty(struct inode *inode)
1064 {
1065 	struct nilfs_inode_info *ii = NILFS_I(inode);
1066 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1067 	int ret = 0;
1068 
1069 	if (!list_empty(&ii->i_dirty)) {
1070 		spin_lock(&nilfs->ns_inode_lock);
1071 		ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
1072 			test_bit(NILFS_I_BUSY, &ii->i_state);
1073 		spin_unlock(&nilfs->ns_inode_lock);
1074 	}
1075 	return ret;
1076 }
1077 
nilfs_set_file_dirty(struct inode * inode,unsigned int nr_dirty)1078 int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty)
1079 {
1080 	struct nilfs_inode_info *ii = NILFS_I(inode);
1081 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1082 
1083 	atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
1084 
1085 	if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
1086 		return 0;
1087 
1088 	spin_lock(&nilfs->ns_inode_lock);
1089 	if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
1090 	    !test_bit(NILFS_I_BUSY, &ii->i_state)) {
1091 		/*
1092 		 * Because this routine may race with nilfs_dispose_list(),
1093 		 * we have to check NILFS_I_QUEUED here, too.
1094 		 */
1095 		if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
1096 			/*
1097 			 * This will happen when somebody is freeing
1098 			 * this inode.
1099 			 */
1100 			nilfs_msg(inode->i_sb, KERN_WARNING,
1101 				  "cannot set file dirty (ino=%lu): the file is being freed",
1102 				  inode->i_ino);
1103 			spin_unlock(&nilfs->ns_inode_lock);
1104 			return -EINVAL; /*
1105 					 * NILFS_I_DIRTY may remain for
1106 					 * freeing inode.
1107 					 */
1108 		}
1109 		list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
1110 		set_bit(NILFS_I_QUEUED, &ii->i_state);
1111 	}
1112 	spin_unlock(&nilfs->ns_inode_lock);
1113 	return 0;
1114 }
1115 
__nilfs_mark_inode_dirty(struct inode * inode,int flags)1116 int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
1117 {
1118 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1119 	struct buffer_head *ibh;
1120 	int err;
1121 
1122 	/*
1123 	 * Do not dirty inodes after the log writer has been detached
1124 	 * and its nilfs_root struct has been freed.
1125 	 */
1126 	if (unlikely(nilfs_purging(nilfs)))
1127 		return 0;
1128 
1129 	err = nilfs_load_inode_block(inode, &ibh);
1130 	if (unlikely(err)) {
1131 		nilfs_msg(inode->i_sb, KERN_WARNING,
1132 			  "cannot mark inode dirty (ino=%lu): error %d loading inode block",
1133 			  inode->i_ino, err);
1134 		return err;
1135 	}
1136 	nilfs_update_inode(inode, ibh, flags);
1137 	mark_buffer_dirty(ibh);
1138 	nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
1139 	brelse(ibh);
1140 	return 0;
1141 }
1142 
1143 /**
1144  * nilfs_dirty_inode - reflect changes on given inode to an inode block.
1145  * @inode: inode of the file to be registered.
1146  *
1147  * nilfs_dirty_inode() loads a inode block containing the specified
1148  * @inode and copies data from a nilfs_inode to a corresponding inode
1149  * entry in the inode block. This operation is excluded from the segment
1150  * construction. This function can be called both as a single operation
1151  * and as a part of indivisible file operations.
1152  */
nilfs_dirty_inode(struct inode * inode,int flags)1153 void nilfs_dirty_inode(struct inode *inode, int flags)
1154 {
1155 	struct nilfs_transaction_info ti;
1156 	struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
1157 
1158 	if (is_bad_inode(inode)) {
1159 		nilfs_msg(inode->i_sb, KERN_WARNING,
1160 			  "tried to mark bad_inode dirty. ignored.");
1161 		dump_stack();
1162 		return;
1163 	}
1164 	if (mdi) {
1165 		nilfs_mdt_mark_dirty(inode);
1166 		return;
1167 	}
1168 	nilfs_transaction_begin(inode->i_sb, &ti, 0);
1169 	__nilfs_mark_inode_dirty(inode, flags);
1170 	nilfs_transaction_commit(inode->i_sb); /* never fails */
1171 }
1172 
nilfs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,__u64 start,__u64 len)1173 int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1174 		 __u64 start, __u64 len)
1175 {
1176 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1177 	__u64 logical = 0, phys = 0, size = 0;
1178 	__u32 flags = 0;
1179 	loff_t isize;
1180 	sector_t blkoff, end_blkoff;
1181 	sector_t delalloc_blkoff;
1182 	unsigned long delalloc_blklen;
1183 	unsigned int blkbits = inode->i_blkbits;
1184 	int ret, n;
1185 
1186 	ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1187 	if (ret)
1188 		return ret;
1189 
1190 	inode_lock(inode);
1191 
1192 	isize = i_size_read(inode);
1193 
1194 	blkoff = start >> blkbits;
1195 	end_blkoff = (start + len - 1) >> blkbits;
1196 
1197 	delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
1198 							&delalloc_blkoff);
1199 
1200 	do {
1201 		__u64 blkphy;
1202 		unsigned int maxblocks;
1203 
1204 		if (delalloc_blklen && blkoff == delalloc_blkoff) {
1205 			if (size) {
1206 				/* End of the current extent */
1207 				ret = fiemap_fill_next_extent(
1208 					fieinfo, logical, phys, size, flags);
1209 				if (ret)
1210 					break;
1211 			}
1212 			if (blkoff > end_blkoff)
1213 				break;
1214 
1215 			flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
1216 			logical = blkoff << blkbits;
1217 			phys = 0;
1218 			size = delalloc_blklen << blkbits;
1219 
1220 			blkoff = delalloc_blkoff + delalloc_blklen;
1221 			delalloc_blklen = nilfs_find_uncommitted_extent(
1222 				inode, blkoff, &delalloc_blkoff);
1223 			continue;
1224 		}
1225 
1226 		/*
1227 		 * Limit the number of blocks that we look up so as
1228 		 * not to get into the next delayed allocation extent.
1229 		 */
1230 		maxblocks = INT_MAX;
1231 		if (delalloc_blklen)
1232 			maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
1233 					  maxblocks);
1234 		blkphy = 0;
1235 
1236 		down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1237 		n = nilfs_bmap_lookup_contig(
1238 			NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
1239 		up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1240 
1241 		if (n < 0) {
1242 			int past_eof;
1243 
1244 			if (unlikely(n != -ENOENT))
1245 				break; /* error */
1246 
1247 			/* HOLE */
1248 			blkoff++;
1249 			past_eof = ((blkoff << blkbits) >= isize);
1250 
1251 			if (size) {
1252 				/* End of the current extent */
1253 
1254 				if (past_eof)
1255 					flags |= FIEMAP_EXTENT_LAST;
1256 
1257 				ret = fiemap_fill_next_extent(
1258 					fieinfo, logical, phys, size, flags);
1259 				if (ret)
1260 					break;
1261 				size = 0;
1262 			}
1263 			if (blkoff > end_blkoff || past_eof)
1264 				break;
1265 		} else {
1266 			if (size) {
1267 				if (phys && blkphy << blkbits == phys + size) {
1268 					/* The current extent goes on */
1269 					size += n << blkbits;
1270 				} else {
1271 					/* Terminate the current extent */
1272 					ret = fiemap_fill_next_extent(
1273 						fieinfo, logical, phys, size,
1274 						flags);
1275 					if (ret || blkoff > end_blkoff)
1276 						break;
1277 
1278 					/* Start another extent */
1279 					flags = FIEMAP_EXTENT_MERGED;
1280 					logical = blkoff << blkbits;
1281 					phys = blkphy << blkbits;
1282 					size = n << blkbits;
1283 				}
1284 			} else {
1285 				/* Start a new extent */
1286 				flags = FIEMAP_EXTENT_MERGED;
1287 				logical = blkoff << blkbits;
1288 				phys = blkphy << blkbits;
1289 				size = n << blkbits;
1290 			}
1291 			blkoff += n;
1292 		}
1293 		cond_resched();
1294 	} while (true);
1295 
1296 	/* If ret is 1 then we just hit the end of the extent array */
1297 	if (ret == 1)
1298 		ret = 0;
1299 
1300 	inode_unlock(inode);
1301 	return ret;
1302 }
1303