1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_FS
3 
4 #include "bcachefs.h"
5 #include "alloc_foreground.h"
6 #include "bkey_buf.h"
7 #include "btree_update.h"
8 #include "buckets.h"
9 #include "clock.h"
10 #include "error.h"
11 #include "extents.h"
12 #include "extent_update.h"
13 #include "fs.h"
14 #include "fs-io.h"
15 #include "fs-io-buffered.h"
16 #include "fs-io-pagecache.h"
17 #include "fsck.h"
18 #include "inode.h"
19 #include "journal.h"
20 #include "io_misc.h"
21 #include "keylist.h"
22 #include "quota.h"
23 #include "reflink.h"
24 #include "trace.h"
25 
26 #include <linux/aio.h>
27 #include <linux/backing-dev.h>
28 #include <linux/falloc.h>
29 #include <linux/migrate.h>
30 #include <linux/mmu_context.h>
31 #include <linux/pagevec.h>
32 #include <linux/rmap.h>
33 #include <linux/sched/signal.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uio.h>
36 
37 #include <trace/events/writeback.h>
38 
39 struct nocow_flush {
40 	struct closure	*cl;
41 	struct bch_dev	*ca;
42 	struct bio	bio;
43 };
44 
nocow_flush_endio(struct bio * _bio)45 static void nocow_flush_endio(struct bio *_bio)
46 {
47 
48 	struct nocow_flush *bio = container_of(_bio, struct nocow_flush, bio);
49 
50 	closure_put(bio->cl);
51 	percpu_ref_put(&bio->ca->io_ref[WRITE]);
52 	bio_put(&bio->bio);
53 }
54 
bch2_inode_flush_nocow_writes_async(struct bch_fs * c,struct bch_inode_info * inode,struct closure * cl)55 void bch2_inode_flush_nocow_writes_async(struct bch_fs *c,
56 					 struct bch_inode_info *inode,
57 					 struct closure *cl)
58 {
59 	struct nocow_flush *bio;
60 	struct bch_dev *ca;
61 	struct bch_devs_mask devs;
62 	unsigned dev;
63 
64 	dev = find_first_bit(inode->ei_devs_need_flush.d, BCH_SB_MEMBERS_MAX);
65 	if (dev == BCH_SB_MEMBERS_MAX)
66 		return;
67 
68 	devs = inode->ei_devs_need_flush;
69 	memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush));
70 
71 	for_each_set_bit(dev, devs.d, BCH_SB_MEMBERS_MAX) {
72 		rcu_read_lock();
73 		ca = rcu_dereference(c->devs[dev]);
74 		if (ca && !percpu_ref_tryget(&ca->io_ref[WRITE]))
75 			ca = NULL;
76 		rcu_read_unlock();
77 
78 		if (!ca)
79 			continue;
80 
81 		bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev, 0,
82 						    REQ_OP_WRITE|REQ_PREFLUSH,
83 						    GFP_KERNEL,
84 						    &c->nocow_flush_bioset),
85 				   struct nocow_flush, bio);
86 		bio->cl			= cl;
87 		bio->ca			= ca;
88 		bio->bio.bi_end_io	= nocow_flush_endio;
89 		closure_bio_submit(&bio->bio, cl);
90 	}
91 }
92 
bch2_inode_flush_nocow_writes(struct bch_fs * c,struct bch_inode_info * inode)93 static int bch2_inode_flush_nocow_writes(struct bch_fs *c,
94 					 struct bch_inode_info *inode)
95 {
96 	struct closure cl;
97 
98 	closure_init_stack(&cl);
99 	bch2_inode_flush_nocow_writes_async(c, inode, &cl);
100 	closure_sync(&cl);
101 
102 	return 0;
103 }
104 
105 /* i_size updates: */
106 
107 struct inode_new_size {
108 	loff_t		new_size;
109 	u64		now;
110 	unsigned	fields;
111 };
112 
inode_set_size(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,void * p)113 static int inode_set_size(struct btree_trans *trans,
114 			  struct bch_inode_info *inode,
115 			  struct bch_inode_unpacked *bi,
116 			  void *p)
117 {
118 	struct inode_new_size *s = p;
119 
120 	bi->bi_size = s->new_size;
121 	if (s->fields & ATTR_ATIME)
122 		bi->bi_atime = s->now;
123 	if (s->fields & ATTR_MTIME)
124 		bi->bi_mtime = s->now;
125 	if (s->fields & ATTR_CTIME)
126 		bi->bi_ctime = s->now;
127 
128 	return 0;
129 }
130 
bch2_write_inode_size(struct bch_fs * c,struct bch_inode_info * inode,loff_t new_size,unsigned fields)131 int __must_check bch2_write_inode_size(struct bch_fs *c,
132 				       struct bch_inode_info *inode,
133 				       loff_t new_size, unsigned fields)
134 {
135 	struct inode_new_size s = {
136 		.new_size	= new_size,
137 		.now		= bch2_current_time(c),
138 		.fields		= fields,
139 	};
140 
141 	return bch2_write_inode(c, inode, inode_set_size, &s, fields);
142 }
143 
__bch2_i_sectors_acct(struct bch_fs * c,struct bch_inode_info * inode,struct quota_res * quota_res,s64 sectors)144 void __bch2_i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
145 			   struct quota_res *quota_res, s64 sectors)
146 {
147 	if (unlikely((s64) inode->v.i_blocks + sectors < 0)) {
148 		struct printbuf buf = PRINTBUF;
149 		bch2_log_msg_start(c, &buf);
150 		prt_printf(&buf, "inode %lu i_blocks underflow: %llu + %lli < 0 (ondisk %lli)",
151 			   inode->v.i_ino, (u64) inode->v.i_blocks, sectors,
152 			   inode->ei_inode.bi_sectors);
153 
154 		bool repeat = false, print = false, suppress = false;
155 		bch2_count_fsck_err(c, vfs_inode_i_blocks_underflow, buf.buf, &repeat, &print, &suppress);
156 		if (print)
157 			bch2_print_str(c, buf.buf);
158 		printbuf_exit(&buf);
159 
160 		if (sectors < 0)
161 			sectors = -inode->v.i_blocks;
162 		else
163 			sectors = 0;
164 	}
165 
166 	inode->v.i_blocks += sectors;
167 
168 #ifdef CONFIG_BCACHEFS_QUOTA
169 	if (quota_res &&
170 	    !test_bit(EI_INODE_SNAPSHOT, &inode->ei_flags) &&
171 	    sectors > 0) {
172 		BUG_ON(sectors > quota_res->sectors);
173 		BUG_ON(sectors > inode->ei_quota_reserved);
174 
175 		quota_res->sectors -= sectors;
176 		inode->ei_quota_reserved -= sectors;
177 	} else {
178 		bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, KEY_TYPE_QUOTA_WARN);
179 	}
180 #endif
181 }
182 
183 /* fsync: */
184 
bch2_get_inode_journal_seq_trans(struct btree_trans * trans,subvol_inum inum,u64 * seq)185 static int bch2_get_inode_journal_seq_trans(struct btree_trans *trans, subvol_inum inum,
186 					    u64 *seq)
187 {
188 	struct printbuf buf = PRINTBUF;
189 	struct bch_inode_unpacked u;
190 	struct btree_iter iter;
191 	int ret = bch2_inode_peek(trans, &iter, &u, inum, 0);
192 	if (ret)
193 		return ret;
194 
195 	u64 cur_seq = journal_cur_seq(&trans->c->journal);
196 	*seq = min(cur_seq, u.bi_journal_seq);
197 
198 	if (fsck_err_on(u.bi_journal_seq > cur_seq,
199 			trans, inode_journal_seq_in_future,
200 			"inode journal seq in future (currently at %llu)\n%s",
201 			cur_seq,
202 			(bch2_inode_unpacked_to_text(&buf, &u),
203 			buf.buf))) {
204 		u.bi_journal_seq = cur_seq;
205 		ret = bch2_inode_write(trans, &iter, &u);
206 	}
207 fsck_err:
208 	bch2_trans_iter_exit(trans, &iter);
209 	printbuf_exit(&buf);
210 	return ret;
211 }
212 
213 /*
214  * inode->ei_inode.bi_journal_seq won't be up to date since it's set in an
215  * insert trigger: look up the btree inode instead
216  */
bch2_flush_inode(struct bch_fs * c,struct bch_inode_info * inode)217 static int bch2_flush_inode(struct bch_fs *c,
218 			    struct bch_inode_info *inode)
219 {
220 	if (c->opts.journal_flush_disabled)
221 		return 0;
222 
223 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_fsync))
224 		return -EROFS;
225 
226 	u64 seq;
227 	int ret = bch2_trans_commit_do(c, NULL, NULL, 0,
228 			bch2_get_inode_journal_seq_trans(trans, inode_inum(inode), &seq)) ?:
229 		  bch2_journal_flush_seq(&c->journal, seq, TASK_INTERRUPTIBLE) ?:
230 		  bch2_inode_flush_nocow_writes(c, inode);
231 	bch2_write_ref_put(c, BCH_WRITE_REF_fsync);
232 	return ret;
233 }
234 
bch2_fsync(struct file * file,loff_t start,loff_t end,int datasync)235 int bch2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
236 {
237 	struct bch_inode_info *inode = file_bch_inode(file);
238 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
239 	int ret, err;
240 
241 	trace_bch2_fsync(file, datasync);
242 
243 	ret = file_write_and_wait_range(file, start, end);
244 	if (ret)
245 		goto out;
246 	ret = sync_inode_metadata(&inode->v, 1);
247 	if (ret)
248 		goto out;
249 	ret = bch2_flush_inode(c, inode);
250 out:
251 	ret = bch2_err_class(ret);
252 	if (ret == -EROFS)
253 		ret = -EIO;
254 
255 	err = file_check_and_advance_wb_err(file);
256 	if (!ret)
257 		ret = err;
258 
259 	return ret;
260 }
261 
262 /* truncate: */
263 
range_has_data(struct bch_fs * c,u32 subvol,struct bpos start,struct bpos end)264 static inline int range_has_data(struct bch_fs *c, u32 subvol,
265 				 struct bpos start,
266 				 struct bpos end)
267 {
268 	return bch2_trans_run(c,
269 		for_each_btree_key_in_subvolume_max(trans, iter, BTREE_ID_extents, start, end,
270 						    subvol, 0, k, ({
271 			bkey_extent_is_data(k.k) && !bkey_extent_is_unwritten(k);
272 		})));
273 }
274 
__bch2_truncate_folio(struct bch_inode_info * inode,pgoff_t index,loff_t start,loff_t end)275 static int __bch2_truncate_folio(struct bch_inode_info *inode,
276 				 pgoff_t index, loff_t start, loff_t end)
277 {
278 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
279 	struct address_space *mapping = inode->v.i_mapping;
280 	struct bch_folio *s;
281 	unsigned start_offset;
282 	unsigned end_offset;
283 	unsigned i;
284 	struct folio *folio;
285 	s64 i_sectors_delta = 0;
286 	int ret = 0;
287 	u64 end_pos;
288 
289 	folio = filemap_lock_folio(mapping, index);
290 	if (IS_ERR_OR_NULL(folio)) {
291 		/*
292 		 * XXX: we're doing two index lookups when we end up reading the
293 		 * folio
294 		 */
295 		ret = range_has_data(c, inode->ei_inum.subvol,
296 				POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT)),
297 				POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT) + PAGE_SECTORS));
298 		if (ret <= 0)
299 			return ret;
300 
301 		folio = __filemap_get_folio(mapping, index,
302 					    FGP_LOCK|FGP_CREAT, GFP_KERNEL);
303 		if (IS_ERR(folio)) {
304 			ret = -ENOMEM;
305 			goto out;
306 		}
307 	}
308 
309 	BUG_ON(start	>= folio_end_pos(folio));
310 	BUG_ON(end	<= folio_pos(folio));
311 
312 	start_offset	= max(start, folio_pos(folio)) - folio_pos(folio);
313 	end_offset	= min_t(u64, end, folio_end_pos(folio)) - folio_pos(folio);
314 
315 	/* Folio boundary? Nothing to do */
316 	if (start_offset == 0 &&
317 	    end_offset == folio_size(folio)) {
318 		ret = 0;
319 		goto unlock;
320 	}
321 
322 	s = bch2_folio_create(folio, 0);
323 	if (!s) {
324 		ret = -ENOMEM;
325 		goto unlock;
326 	}
327 
328 	if (!folio_test_uptodate(folio)) {
329 		ret = bch2_read_single_folio(folio, mapping);
330 		if (ret)
331 			goto unlock;
332 	}
333 
334 	ret = bch2_folio_set(c, inode_inum(inode), &folio, 1);
335 	if (ret)
336 		goto unlock;
337 
338 	for (i = round_up(start_offset, block_bytes(c)) >> 9;
339 	     i < round_down(end_offset, block_bytes(c)) >> 9;
340 	     i++) {
341 		s->s[i].nr_replicas	= 0;
342 
343 		i_sectors_delta -= s->s[i].state == SECTOR_dirty;
344 		bch2_folio_sector_set(folio, s, i, SECTOR_unallocated);
345 	}
346 
347 	bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta);
348 
349 	/*
350 	 * Caller needs to know whether this folio will be written out by
351 	 * writeback - doing an i_size update if necessary - or whether it will
352 	 * be responsible for the i_size update.
353 	 *
354 	 * Note that we shouldn't ever see a folio beyond EOF, but check and
355 	 * warn if so. This has been observed by failure to clean up folios
356 	 * after a short write and there's still a chance reclaim will fix
357 	 * things up.
358 	 */
359 	WARN_ON_ONCE(folio_pos(folio) >= inode->v.i_size);
360 	end_pos = folio_end_pos(folio);
361 	if (inode->v.i_size > folio_pos(folio))
362 		end_pos = min_t(u64, inode->v.i_size, end_pos);
363 	ret = s->s[folio_pos_to_s(folio, end_pos - 1)].state >= SECTOR_dirty;
364 
365 	folio_zero_segment(folio, start_offset, end_offset);
366 
367 	/*
368 	 * Bit of a hack - we don't want truncate to fail due to -ENOSPC.
369 	 *
370 	 * XXX: because we aren't currently tracking whether the folio has actual
371 	 * data in it (vs. just 0s, or only partially written) this wrong. ick.
372 	 */
373 	BUG_ON(bch2_get_folio_disk_reservation(c, inode, folio, false));
374 
375 	/*
376 	 * This removes any writeable userspace mappings; we need to force
377 	 * .page_mkwrite to be called again before any mmapped writes, to
378 	 * redirty the full page:
379 	 */
380 	folio_mkclean(folio);
381 	filemap_dirty_folio(mapping, folio);
382 unlock:
383 	folio_unlock(folio);
384 	folio_put(folio);
385 out:
386 	return ret;
387 }
388 
bch2_truncate_folio(struct bch_inode_info * inode,loff_t from)389 static int bch2_truncate_folio(struct bch_inode_info *inode, loff_t from)
390 {
391 	return __bch2_truncate_folio(inode, from >> PAGE_SHIFT,
392 				     from, ANYSINT_MAX(loff_t));
393 }
394 
bch2_truncate_folios(struct bch_inode_info * inode,loff_t start,loff_t end)395 static int bch2_truncate_folios(struct bch_inode_info *inode,
396 				loff_t start, loff_t end)
397 {
398 	int ret = __bch2_truncate_folio(inode, start >> PAGE_SHIFT,
399 					start, end);
400 
401 	if (ret >= 0 &&
402 	    start >> PAGE_SHIFT != end >> PAGE_SHIFT)
403 		ret = __bch2_truncate_folio(inode,
404 					(end - 1) >> PAGE_SHIFT,
405 					start, end);
406 	return ret;
407 }
408 
bch2_extend(struct mnt_idmap * idmap,struct bch_inode_info * inode,struct bch_inode_unpacked * inode_u,struct iattr * iattr)409 static int bch2_extend(struct mnt_idmap *idmap,
410 		       struct bch_inode_info *inode,
411 		       struct bch_inode_unpacked *inode_u,
412 		       struct iattr *iattr)
413 {
414 	struct address_space *mapping = inode->v.i_mapping;
415 	int ret;
416 
417 	/*
418 	 * sync appends:
419 	 *
420 	 * this has to be done _before_ extending i_size:
421 	 */
422 	ret = filemap_write_and_wait_range(mapping, inode_u->bi_size, S64_MAX);
423 	if (ret)
424 		return ret;
425 
426 	truncate_setsize(&inode->v, iattr->ia_size);
427 
428 	return bch2_setattr_nonsize(idmap, inode, iattr);
429 }
430 
bchfs_truncate(struct mnt_idmap * idmap,struct bch_inode_info * inode,struct iattr * iattr)431 int bchfs_truncate(struct mnt_idmap *idmap,
432 		  struct bch_inode_info *inode, struct iattr *iattr)
433 {
434 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
435 	struct address_space *mapping = inode->v.i_mapping;
436 	struct bch_inode_unpacked inode_u;
437 	s64 i_sectors_delta = 0;
438 	int ret = 0;
439 
440 	/*
441 	 * If the truncate call with change the size of the file, the
442 	 * cmtimes should be updated. If the size will not change, we
443 	 * do not need to update the cmtimes.
444 	 */
445 	if (iattr->ia_size != inode->v.i_size) {
446 		if (!(iattr->ia_valid & ATTR_MTIME))
447 			ktime_get_coarse_real_ts64(&iattr->ia_mtime);
448 		if (!(iattr->ia_valid & ATTR_CTIME))
449 			ktime_get_coarse_real_ts64(&iattr->ia_ctime);
450 		iattr->ia_valid |= ATTR_MTIME|ATTR_CTIME;
451 	}
452 
453 	inode_dio_wait(&inode->v);
454 	bch2_pagecache_block_get(inode);
455 
456 	ret = bch2_inode_find_by_inum(c, inode_inum(inode), &inode_u);
457 	if (ret)
458 		goto err;
459 
460 	/*
461 	 * check this before next assertion; on filesystem error our normal
462 	 * invariants are a bit broken (truncate has to truncate the page cache
463 	 * before the inode).
464 	 */
465 	ret = bch2_journal_error(&c->journal);
466 	if (ret)
467 		goto err;
468 
469 	WARN_ONCE(!test_bit(EI_INODE_ERROR, &inode->ei_flags) &&
470 		  inode->v.i_size < inode_u.bi_size,
471 		  "truncate spotted in mem i_size < btree i_size: %llu < %llu\n",
472 		  (u64) inode->v.i_size, inode_u.bi_size);
473 
474 	if (iattr->ia_size > inode->v.i_size) {
475 		ret = bch2_extend(idmap, inode, &inode_u, iattr);
476 		goto err;
477 	}
478 
479 	iattr->ia_valid &= ~ATTR_SIZE;
480 
481 	ret = bch2_truncate_folio(inode, iattr->ia_size);
482 	if (unlikely(ret < 0))
483 		goto err;
484 	ret = 0;
485 
486 	truncate_setsize(&inode->v, iattr->ia_size);
487 
488 	/*
489 	 * When extending, we're going to write the new i_size to disk
490 	 * immediately so we need to flush anything above the current on disk
491 	 * i_size first:
492 	 *
493 	 * Also, when extending we need to flush the page that i_size currently
494 	 * straddles - if it's mapped to userspace, we need to ensure that
495 	 * userspace has to redirty it and call .mkwrite -> set_page_dirty
496 	 * again to allocate the part of the page that was extended.
497 	 */
498 	if (iattr->ia_size > inode_u.bi_size)
499 		ret = filemap_write_and_wait_range(mapping,
500 				inode_u.bi_size,
501 				iattr->ia_size - 1);
502 	else if (iattr->ia_size & (PAGE_SIZE - 1))
503 		ret = filemap_write_and_wait_range(mapping,
504 				round_down(iattr->ia_size, PAGE_SIZE),
505 				iattr->ia_size - 1);
506 	if (ret)
507 		goto err;
508 
509 	ret = bch2_truncate(c, inode_inum(inode), iattr->ia_size, &i_sectors_delta);
510 	bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta);
511 
512 	if (unlikely(ret)) {
513 		/*
514 		 * If we error here, VFS caches are now inconsistent with btree
515 		 */
516 		set_bit(EI_INODE_ERROR, &inode->ei_flags);
517 		goto err;
518 	}
519 
520 	if (unlikely(!inode->v.i_size && inode->v.i_blocks &&
521 		     !bch2_journal_error(&c->journal))) {
522 		struct printbuf buf = PRINTBUF;
523 		bch2_log_msg_start(c, &buf);
524 		prt_printf(&buf,
525 			   "inode %lu truncated to 0 but i_blocks %llu (ondisk %lli)",
526 			   inode->v.i_ino, (u64) inode->v.i_blocks,
527 			   inode->ei_inode.bi_sectors);
528 
529 		bool repeat = false, print = false, suppress = false;
530 		bch2_count_fsck_err(c, vfs_inode_i_blocks_not_zero_at_truncate, buf.buf,
531 				    &repeat, &print, &suppress);
532 		if (print)
533 			bch2_print_str(c, buf.buf);
534 		printbuf_exit(&buf);
535 	}
536 
537 	ret = bch2_setattr_nonsize(idmap, inode, iattr);
538 err:
539 	bch2_pagecache_block_put(inode);
540 	return bch2_err_class(ret);
541 }
542 
543 /* fallocate: */
544 
inode_update_times_fn(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,void * p)545 static int inode_update_times_fn(struct btree_trans *trans,
546 				 struct bch_inode_info *inode,
547 				 struct bch_inode_unpacked *bi, void *p)
548 {
549 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
550 
551 	bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
552 	return 0;
553 }
554 
bchfs_fpunch(struct bch_inode_info * inode,loff_t offset,loff_t len)555 static noinline long bchfs_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len)
556 {
557 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
558 	u64 end		= offset + len;
559 	u64 block_start	= round_up(offset, block_bytes(c));
560 	u64 block_end	= round_down(end, block_bytes(c));
561 	bool truncated_last_page;
562 	int ret = 0;
563 
564 	ret = bch2_truncate_folios(inode, offset, end);
565 	if (unlikely(ret < 0))
566 		goto err;
567 
568 	truncated_last_page = ret;
569 
570 	truncate_pagecache_range(&inode->v, offset, end - 1);
571 
572 	if (block_start < block_end) {
573 		s64 i_sectors_delta = 0;
574 
575 		ret = bch2_fpunch(c, inode_inum(inode),
576 				  block_start >> 9, block_end >> 9,
577 				  &i_sectors_delta);
578 		bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta);
579 	}
580 
581 	mutex_lock(&inode->ei_update_lock);
582 	if (end >= inode->v.i_size && !truncated_last_page) {
583 		ret = bch2_write_inode_size(c, inode, inode->v.i_size,
584 					    ATTR_MTIME|ATTR_CTIME);
585 	} else {
586 		ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
587 				       ATTR_MTIME|ATTR_CTIME);
588 	}
589 	mutex_unlock(&inode->ei_update_lock);
590 err:
591 	return ret;
592 }
593 
bchfs_fcollapse_finsert(struct bch_inode_info * inode,loff_t offset,loff_t len,bool insert)594 static noinline long bchfs_fcollapse_finsert(struct bch_inode_info *inode,
595 				   loff_t offset, loff_t len,
596 				   bool insert)
597 {
598 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
599 	struct address_space *mapping = inode->v.i_mapping;
600 	s64 i_sectors_delta = 0;
601 	int ret = 0;
602 
603 	if ((offset | len) & (block_bytes(c) - 1))
604 		return -EINVAL;
605 
606 	if (insert) {
607 		if (offset >= inode->v.i_size)
608 			return -EINVAL;
609 	} else {
610 		if (offset + len >= inode->v.i_size)
611 			return -EINVAL;
612 	}
613 
614 	ret = bch2_write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX);
615 	if (ret)
616 		return ret;
617 
618 	if (insert)
619 		i_size_write(&inode->v, inode->v.i_size + len);
620 
621 	ret = bch2_fcollapse_finsert(c, inode_inum(inode), offset >> 9, len >> 9,
622 				     insert, &i_sectors_delta);
623 	if (!ret && !insert)
624 		i_size_write(&inode->v, inode->v.i_size - len);
625 	bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta);
626 
627 	return ret;
628 }
629 
__bchfs_fallocate(struct bch_inode_info * inode,int mode,u64 start_sector,u64 end_sector)630 static noinline int __bchfs_fallocate(struct bch_inode_info *inode, int mode,
631 			     u64 start_sector, u64 end_sector)
632 {
633 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
634 	struct btree_trans *trans = bch2_trans_get(c);
635 	struct btree_iter iter;
636 	struct bpos end_pos = POS(inode->v.i_ino, end_sector);
637 	struct bch_io_opts opts;
638 	int ret = 0;
639 
640 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
641 
642 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
643 			POS(inode->v.i_ino, start_sector),
644 			BTREE_ITER_slots|BTREE_ITER_intent);
645 
646 	while (!ret) {
647 		s64 i_sectors_delta = 0;
648 		struct quota_res quota_res = { 0 };
649 		struct bkey_s_c k;
650 		unsigned sectors;
651 		bool is_allocation;
652 		u64 hole_start, hole_end;
653 		u32 snapshot;
654 
655 		bch2_trans_begin(trans);
656 
657 		if (bkey_ge(iter.pos, end_pos))
658 			break;
659 
660 		ret = bch2_subvolume_get_snapshot(trans,
661 					inode->ei_inum.subvol, &snapshot);
662 		if (ret)
663 			goto bkey_err;
664 
665 		bch2_btree_iter_set_snapshot(trans, &iter, snapshot);
666 
667 		k = bch2_btree_iter_peek_slot(trans, &iter);
668 		if ((ret = bkey_err(k)))
669 			goto bkey_err;
670 
671 		hole_start	= iter.pos.offset;
672 		hole_end	= bpos_min(k.k->p, end_pos).offset;
673 		is_allocation	= bkey_extent_is_allocation(k.k);
674 
675 		/* already reserved */
676 		if (bkey_extent_is_reservation(k) &&
677 		    bch2_bkey_nr_ptrs_fully_allocated(k) >= opts.data_replicas) {
678 			bch2_btree_iter_advance(trans, &iter);
679 			continue;
680 		}
681 
682 		if (bkey_extent_is_data(k.k) &&
683 		    !(mode & FALLOC_FL_ZERO_RANGE)) {
684 			bch2_btree_iter_advance(trans, &iter);
685 			continue;
686 		}
687 
688 		if (!(mode & FALLOC_FL_ZERO_RANGE)) {
689 			/*
690 			 * Lock ordering - can't be holding btree locks while
691 			 * blocking on a folio lock:
692 			 */
693 			if (bch2_clamp_data_hole(&inode->v,
694 						 &hole_start,
695 						 &hole_end,
696 						 opts.data_replicas, true)) {
697 				ret = drop_locks_do(trans,
698 					(bch2_clamp_data_hole(&inode->v,
699 							      &hole_start,
700 							      &hole_end,
701 							      opts.data_replicas, false), 0));
702 				if (ret)
703 					goto bkey_err;
704 			}
705 			bch2_btree_iter_set_pos(trans, &iter, POS(iter.pos.inode, hole_start));
706 
707 			if (ret)
708 				goto bkey_err;
709 
710 			if (hole_start == hole_end)
711 				continue;
712 		}
713 
714 		sectors	= hole_end - hole_start;
715 
716 		if (!is_allocation) {
717 			ret = bch2_quota_reservation_add(c, inode,
718 					&quota_res, sectors, true);
719 			if (unlikely(ret))
720 				goto bkey_err;
721 		}
722 
723 		ret = bch2_extent_fallocate(trans, inode_inum(inode), &iter,
724 					    sectors, opts, &i_sectors_delta,
725 					    writepoint_hashed((unsigned long) current));
726 		if (ret)
727 			goto bkey_err;
728 
729 		bch2_i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
730 
731 		if (bch2_mark_pagecache_reserved(inode, &hole_start,
732 						 iter.pos.offset, true)) {
733 			ret = drop_locks_do(trans,
734 				bch2_mark_pagecache_reserved(inode, &hole_start,
735 							     iter.pos.offset, false));
736 			if (ret)
737 				goto bkey_err;
738 		}
739 bkey_err:
740 		bch2_quota_reservation_put(c, inode, &quota_res);
741 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
742 			ret = 0;
743 	}
744 
745 	if (bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)) {
746 		struct quota_res quota_res = { 0 };
747 		s64 i_sectors_delta = 0;
748 
749 		bch2_fpunch_at(trans, &iter, inode_inum(inode),
750 			       end_sector, &i_sectors_delta);
751 		bch2_i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
752 		bch2_quota_reservation_put(c, inode, &quota_res);
753 	}
754 
755 	bch2_trans_iter_exit(trans, &iter);
756 	bch2_trans_put(trans);
757 	return ret;
758 }
759 
bchfs_fallocate(struct bch_inode_info * inode,int mode,loff_t offset,loff_t len)760 static noinline long bchfs_fallocate(struct bch_inode_info *inode, int mode,
761 			    loff_t offset, loff_t len)
762 {
763 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
764 	u64 end		= offset + len;
765 	u64 block_start	= round_down(offset,	block_bytes(c));
766 	u64 block_end	= round_up(end,		block_bytes(c));
767 	bool truncated_last_page = false;
768 	int ret, ret2 = 0;
769 
770 	if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) {
771 		ret = inode_newsize_ok(&inode->v, end);
772 		if (ret)
773 			return ret;
774 	}
775 
776 	if (mode & FALLOC_FL_ZERO_RANGE) {
777 		ret = bch2_truncate_folios(inode, offset, end);
778 		if (unlikely(ret < 0))
779 			return ret;
780 
781 		truncated_last_page = ret;
782 
783 		truncate_pagecache_range(&inode->v, offset, end - 1);
784 
785 		block_start	= round_up(offset,	block_bytes(c));
786 		block_end	= round_down(end,	block_bytes(c));
787 	}
788 
789 	ret = __bchfs_fallocate(inode, mode, block_start >> 9, block_end >> 9);
790 
791 	/*
792 	 * On -ENOSPC in ZERO_RANGE mode, we still want to do the inode update,
793 	 * so that the VFS cache i_size is consistent with the btree i_size:
794 	 */
795 	if (ret &&
796 	    !(bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)))
797 		return ret;
798 
799 	if (mode & FALLOC_FL_KEEP_SIZE && end > inode->v.i_size)
800 		end = inode->v.i_size;
801 
802 	if (end >= inode->v.i_size &&
803 	    (((mode & FALLOC_FL_ZERO_RANGE) && !truncated_last_page) ||
804 	     !(mode & FALLOC_FL_KEEP_SIZE))) {
805 		spin_lock(&inode->v.i_lock);
806 		i_size_write(&inode->v, end);
807 		spin_unlock(&inode->v.i_lock);
808 
809 		mutex_lock(&inode->ei_update_lock);
810 		ret2 = bch2_write_inode_size(c, inode, end, 0);
811 		mutex_unlock(&inode->ei_update_lock);
812 	}
813 
814 	return ret ?: ret2;
815 }
816 
bch2_fallocate_dispatch(struct file * file,int mode,loff_t offset,loff_t len)817 long bch2_fallocate_dispatch(struct file *file, int mode,
818 			     loff_t offset, loff_t len)
819 {
820 	struct bch_inode_info *inode = file_bch_inode(file);
821 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
822 	long ret;
823 
824 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_fallocate))
825 		return -EROFS;
826 
827 	inode_lock(&inode->v);
828 	inode_dio_wait(&inode->v);
829 	bch2_pagecache_block_get(inode);
830 
831 	ret = file_modified(file);
832 	if (ret)
833 		goto err;
834 
835 	if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE)))
836 		ret = bchfs_fallocate(inode, mode, offset, len);
837 	else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE))
838 		ret = bchfs_fpunch(inode, offset, len);
839 	else if (mode == FALLOC_FL_INSERT_RANGE)
840 		ret = bchfs_fcollapse_finsert(inode, offset, len, true);
841 	else if (mode == FALLOC_FL_COLLAPSE_RANGE)
842 		ret = bchfs_fcollapse_finsert(inode, offset, len, false);
843 	else
844 		ret = -EOPNOTSUPP;
845 err:
846 	bch2_pagecache_block_put(inode);
847 	inode_unlock(&inode->v);
848 	bch2_write_ref_put(c, BCH_WRITE_REF_fallocate);
849 
850 	return bch2_err_class(ret);
851 }
852 
853 /*
854  * Take a quota reservation for unallocated blocks in a given file range
855  * Does not check pagecache
856  */
quota_reserve_range(struct bch_inode_info * inode,struct quota_res * res,u64 start,u64 end)857 static int quota_reserve_range(struct bch_inode_info *inode,
858 			       struct quota_res *res,
859 			       u64 start, u64 end)
860 {
861 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
862 	u64 sectors = end - start;
863 
864 	int ret = bch2_trans_run(c,
865 		for_each_btree_key_in_subvolume_max(trans, iter,
866 				BTREE_ID_extents,
867 				POS(inode->v.i_ino, start),
868 				POS(inode->v.i_ino, end - 1),
869 				inode->ei_inum.subvol, 0, k, ({
870 			if (bkey_extent_is_allocation(k.k)) {
871 				u64 s = min(end, k.k->p.offset) -
872 					max(start, bkey_start_offset(k.k));
873 				BUG_ON(s > sectors);
874 				sectors -= s;
875 			}
876 
877 			0;
878 		})));
879 
880 	return ret ?: bch2_quota_reservation_add(c, inode, res, sectors, true);
881 }
882 
bch2_remap_file_range(struct file * file_src,loff_t pos_src,struct file * file_dst,loff_t pos_dst,loff_t len,unsigned remap_flags)883 loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src,
884 			     struct file *file_dst, loff_t pos_dst,
885 			     loff_t len, unsigned remap_flags)
886 {
887 	struct bch_inode_info *src = file_bch_inode(file_src);
888 	struct bch_inode_info *dst = file_bch_inode(file_dst);
889 	struct bch_fs *c = src->v.i_sb->s_fs_info;
890 	struct quota_res quota_res = { 0 };
891 	s64 i_sectors_delta = 0;
892 	u64 aligned_len;
893 	loff_t ret = 0;
894 
895 	if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY))
896 		return -EINVAL;
897 
898 	if ((pos_src & (block_bytes(c) - 1)) ||
899 	    (pos_dst & (block_bytes(c) - 1)))
900 		return -EINVAL;
901 
902 	if (src == dst &&
903 	    abs(pos_src - pos_dst) < len)
904 		return -EINVAL;
905 
906 	lock_two_nondirectories(&src->v, &dst->v);
907 	bch2_lock_inodes(INODE_PAGECACHE_BLOCK, src, dst);
908 
909 	inode_dio_wait(&src->v);
910 	inode_dio_wait(&dst->v);
911 
912 	ret = generic_remap_file_range_prep(file_src, pos_src,
913 					    file_dst, pos_dst,
914 					    &len, remap_flags);
915 	if (ret < 0 || len == 0)
916 		goto err;
917 
918 	aligned_len = round_up((u64) len, block_bytes(c));
919 
920 	ret = bch2_write_invalidate_inode_pages_range(dst->v.i_mapping,
921 				pos_dst, pos_dst + len - 1);
922 	if (ret)
923 		goto err;
924 
925 	ret = quota_reserve_range(dst, &quota_res, pos_dst >> 9,
926 				  (pos_dst + aligned_len) >> 9);
927 	if (ret)
928 		goto err;
929 
930 	if (!(remap_flags & REMAP_FILE_DEDUP))
931 		file_update_time(file_dst);
932 
933 	bch2_mark_pagecache_unallocated(src, pos_src >> 9,
934 				   (pos_src + aligned_len) >> 9);
935 
936 	/*
937 	 * XXX: we'd like to be telling bch2_remap_range() if we have
938 	 * permission to write to the source file, and thus if io path option
939 	 * changes should be propagated through the copy, but we need mnt_idmap
940 	 * from the pathwalk, awkward
941 	 */
942 	ret = bch2_remap_range(c,
943 			       inode_inum(dst), pos_dst >> 9,
944 			       inode_inum(src), pos_src >> 9,
945 			       aligned_len >> 9,
946 			       pos_dst + len, &i_sectors_delta,
947 			       false);
948 	if (ret < 0)
949 		goto err;
950 
951 	/*
952 	 * due to alignment, we might have remapped slightly more than requsted
953 	 */
954 	ret = min((u64) ret << 9, (u64) len);
955 
956 	bch2_i_sectors_acct(c, dst, &quota_res, i_sectors_delta);
957 
958 	spin_lock(&dst->v.i_lock);
959 	if (pos_dst + ret > dst->v.i_size)
960 		i_size_write(&dst->v, pos_dst + ret);
961 	spin_unlock(&dst->v.i_lock);
962 
963 	if ((file_dst->f_flags & (__O_SYNC | O_DSYNC)) ||
964 	    IS_SYNC(file_inode(file_dst)))
965 		ret = bch2_flush_inode(c, dst);
966 err:
967 	bch2_quota_reservation_put(c, dst, &quota_res);
968 	bch2_unlock_inodes(INODE_PAGECACHE_BLOCK, src, dst);
969 	unlock_two_nondirectories(&src->v, &dst->v);
970 
971 	return bch2_err_class(ret);
972 }
973 
974 /* fseek: */
975 
bch2_seek_data(struct file * file,u64 offset)976 static loff_t bch2_seek_data(struct file *file, u64 offset)
977 {
978 	struct bch_inode_info *inode = file_bch_inode(file);
979 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
980 	subvol_inum inum = inode_inum(inode);
981 	u64 isize, next_data = MAX_LFS_FILESIZE;
982 
983 	isize = i_size_read(&inode->v);
984 	if (offset >= isize)
985 		return -ENXIO;
986 
987 	int ret = bch2_trans_run(c,
988 		for_each_btree_key_in_subvolume_max(trans, iter, BTREE_ID_extents,
989 				   POS(inode->v.i_ino, offset >> 9),
990 				   POS(inode->v.i_ino, U64_MAX),
991 				   inum.subvol, 0, k, ({
992 			if (bkey_extent_is_data(k.k)) {
993 				next_data = max(offset, bkey_start_offset(k.k) << 9);
994 				break;
995 			} else if (k.k->p.offset >> 9 > isize)
996 				break;
997 			0;
998 		})));
999 	if (ret)
1000 		return ret;
1001 
1002 	if (next_data > offset)
1003 		next_data = bch2_seek_pagecache_data(&inode->v,
1004 					offset, next_data, 0, false);
1005 
1006 	if (next_data >= isize)
1007 		return -ENXIO;
1008 
1009 	return vfs_setpos(file, next_data, MAX_LFS_FILESIZE);
1010 }
1011 
bch2_seek_hole(struct file * file,u64 offset)1012 static loff_t bch2_seek_hole(struct file *file, u64 offset)
1013 {
1014 	struct bch_inode_info *inode = file_bch_inode(file);
1015 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
1016 	subvol_inum inum = inode_inum(inode);
1017 	u64 isize, next_hole = MAX_LFS_FILESIZE;
1018 
1019 	isize = i_size_read(&inode->v);
1020 	if (offset >= isize)
1021 		return -ENXIO;
1022 
1023 	int ret = bch2_trans_run(c,
1024 		for_each_btree_key_in_subvolume_max(trans, iter, BTREE_ID_extents,
1025 				   POS(inode->v.i_ino, offset >> 9),
1026 				   POS(inode->v.i_ino, U64_MAX),
1027 				   inum.subvol, BTREE_ITER_slots, k, ({
1028 			if (k.k->p.inode != inode->v.i_ino ||
1029 			    !bkey_extent_is_data(k.k)) {
1030 				loff_t start_offset = k.k->p.inode == inode->v.i_ino
1031 					? max(offset, bkey_start_offset(k.k) << 9)
1032 					: offset;
1033 				loff_t end_offset = k.k->p.inode == inode->v.i_ino
1034 					? MAX_LFS_FILESIZE
1035 					: k.k->p.offset << 9;
1036 
1037 				/*
1038 				 * Found a hole in the btree, now make sure it's
1039 				 * a hole in the pagecache. We might have to
1040 				 * keep searching if this hole is entirely dirty
1041 				 * in the page cache:
1042 				 */
1043 				bch2_trans_unlock(trans);
1044 				loff_t pagecache_hole = bch2_seek_pagecache_hole(&inode->v,
1045 								start_offset, end_offset, 0, false);
1046 				if (pagecache_hole < end_offset) {
1047 					next_hole = pagecache_hole;
1048 					break;
1049 				}
1050 			} else {
1051 				offset = max(offset, bkey_start_offset(k.k) << 9);
1052 			}
1053 			0;
1054 		})));
1055 	if (ret)
1056 		return ret;
1057 
1058 	if (next_hole > isize)
1059 		next_hole = isize;
1060 
1061 	return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE);
1062 }
1063 
bch2_llseek(struct file * file,loff_t offset,int whence)1064 loff_t bch2_llseek(struct file *file, loff_t offset, int whence)
1065 {
1066 	loff_t ret;
1067 
1068 	switch (whence) {
1069 	case SEEK_SET:
1070 	case SEEK_CUR:
1071 	case SEEK_END:
1072 		ret = generic_file_llseek(file, offset, whence);
1073 		break;
1074 	case SEEK_DATA:
1075 		ret = bch2_seek_data(file, offset);
1076 		break;
1077 	case SEEK_HOLE:
1078 		ret = bch2_seek_hole(file, offset);
1079 		break;
1080 	default:
1081 		ret = -EINVAL;
1082 		break;
1083 	}
1084 
1085 	return bch2_err_class(ret);
1086 }
1087 
bch2_fs_fsio_exit(struct bch_fs * c)1088 void bch2_fs_fsio_exit(struct bch_fs *c)
1089 {
1090 	bioset_exit(&c->nocow_flush_bioset);
1091 }
1092 
bch2_fs_fsio_init(struct bch_fs * c)1093 int bch2_fs_fsio_init(struct bch_fs *c)
1094 {
1095 	if (bioset_init(&c->nocow_flush_bioset,
1096 			1, offsetof(struct nocow_flush, bio), 0))
1097 		return -BCH_ERR_ENOMEM_nocow_flush_bioset_init;
1098 
1099 	return 0;
1100 }
1101 
1102 #endif /* NO_BCACHEFS_FS */
1103