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