1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_FS
3
4 #include "bcachefs.h"
5 #include "acl.h"
6 #include "bkey_buf.h"
7 #include "btree_update.h"
8 #include "buckets.h"
9 #include "chardev.h"
10 #include "dirent.h"
11 #include "errcode.h"
12 #include "extents.h"
13 #include "fs.h"
14 #include "fs-io.h"
15 #include "fs-ioctl.h"
16 #include "fs-io-buffered.h"
17 #include "fs-io-direct.h"
18 #include "fs-io-pagecache.h"
19 #include "fsck.h"
20 #include "inode.h"
21 #include "io_read.h"
22 #include "journal.h"
23 #include "keylist.h"
24 #include "namei.h"
25 #include "quota.h"
26 #include "rebalance.h"
27 #include "snapshot.h"
28 #include "super.h"
29 #include "xattr.h"
30 #include "trace.h"
31
32 #include <linux/aio.h>
33 #include <linux/backing-dev.h>
34 #include <linux/exportfs.h>
35 #include <linux/fiemap.h>
36 #include <linux/fileattr.h>
37 #include <linux/fs_context.h>
38 #include <linux/module.h>
39 #include <linux/pagemap.h>
40 #include <linux/posix_acl.h>
41 #include <linux/random.h>
42 #include <linux/seq_file.h>
43 #include <linux/siphash.h>
44 #include <linux/statfs.h>
45 #include <linux/string.h>
46 #include <linux/xattr.h>
47
48 static struct kmem_cache *bch2_inode_cache;
49
50 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
51 struct bch_inode_info *,
52 struct bch_inode_unpacked *,
53 struct bch_subvolume *);
54
55 /* Set VFS inode flags from bcachefs inode: */
bch2_inode_flags_to_vfs(struct bch_fs * c,struct bch_inode_info * inode)56 static inline void bch2_inode_flags_to_vfs(struct bch_fs *c, struct bch_inode_info *inode)
57 {
58 static const __maybe_unused unsigned bch_flags_to_vfs[] = {
59 [__BCH_INODE_sync] = S_SYNC,
60 [__BCH_INODE_immutable] = S_IMMUTABLE,
61 [__BCH_INODE_append] = S_APPEND,
62 [__BCH_INODE_noatime] = S_NOATIME,
63 };
64
65 set_flags(bch_flags_to_vfs, inode->ei_inode.bi_flags, inode->v.i_flags);
66
67 if (bch2_inode_casefold(c, &inode->ei_inode))
68 inode->v.i_flags |= S_CASEFOLD;
69 else
70 inode->v.i_flags &= ~S_CASEFOLD;
71 }
72
bch2_inode_update_after_write(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,unsigned fields)73 void bch2_inode_update_after_write(struct btree_trans *trans,
74 struct bch_inode_info *inode,
75 struct bch_inode_unpacked *bi,
76 unsigned fields)
77 {
78 struct bch_fs *c = trans->c;
79
80 BUG_ON(bi->bi_inum != inode->v.i_ino);
81
82 bch2_assert_pos_locked(trans, BTREE_ID_inodes, POS(0, bi->bi_inum));
83
84 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
85 i_uid_write(&inode->v, bi->bi_uid);
86 i_gid_write(&inode->v, bi->bi_gid);
87 inode->v.i_mode = bi->bi_mode;
88
89 if (fields & ATTR_SIZE)
90 i_size_write(&inode->v, bi->bi_size);
91
92 if (fields & ATTR_ATIME)
93 inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
94 if (fields & ATTR_MTIME)
95 inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime));
96 if (fields & ATTR_CTIME)
97 inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime));
98
99 inode->ei_inode = *bi;
100
101 bch2_inode_flags_to_vfs(c, inode);
102 }
103
bch2_write_inode(struct bch_fs * c,struct bch_inode_info * inode,inode_set_fn set,void * p,unsigned fields)104 int __must_check bch2_write_inode(struct bch_fs *c,
105 struct bch_inode_info *inode,
106 inode_set_fn set,
107 void *p, unsigned fields)
108 {
109 struct btree_trans *trans = bch2_trans_get(c);
110 struct btree_iter iter = {};
111 struct bch_inode_unpacked inode_u;
112 int ret;
113 retry:
114 bch2_trans_begin(trans);
115
116 ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode), BTREE_ITER_intent);
117 if (ret)
118 goto err;
119
120 struct bch_extent_rebalance old_r = bch2_inode_rebalance_opts_get(c, &inode_u);
121
122 ret = (set ? set(trans, inode, &inode_u, p) : 0);
123 if (ret)
124 goto err;
125
126 struct bch_extent_rebalance new_r = bch2_inode_rebalance_opts_get(c, &inode_u);
127 bool rebalance_changed = memcmp(&old_r, &new_r, sizeof(new_r));
128
129 if (rebalance_changed) {
130 ret = bch2_set_rebalance_needs_scan_trans(trans, inode_u.bi_inum);
131 if (ret)
132 goto err;
133 }
134
135 ret = bch2_inode_write(trans, &iter, &inode_u) ?:
136 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
137
138 /*
139 * the btree node lock protects inode->ei_inode, not ei_update_lock;
140 * this is important for inode updates via bchfs_write_index_update
141 */
142 if (!ret)
143 bch2_inode_update_after_write(trans, inode, &inode_u, fields);
144 err:
145 bch2_trans_iter_exit(trans, &iter);
146
147 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
148 goto retry;
149
150 if (rebalance_changed)
151 bch2_rebalance_wakeup(c);
152
153 bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
154 "%s: inode %llu:%llu not found when updating",
155 bch2_err_str(ret),
156 inode_inum(inode).subvol,
157 inode_inum(inode).inum);
158
159 bch2_trans_put(trans);
160 return ret < 0 ? ret : 0;
161 }
162
bch2_fs_quota_transfer(struct bch_fs * c,struct bch_inode_info * inode,struct bch_qid new_qid,unsigned qtypes,enum quota_acct_mode mode)163 int bch2_fs_quota_transfer(struct bch_fs *c,
164 struct bch_inode_info *inode,
165 struct bch_qid new_qid,
166 unsigned qtypes,
167 enum quota_acct_mode mode)
168 {
169 unsigned i;
170 int ret;
171
172 qtypes &= enabled_qtypes(c);
173
174 for (i = 0; i < QTYP_NR; i++)
175 if (new_qid.q[i] == inode->ei_qid.q[i])
176 qtypes &= ~(1U << i);
177
178 if (!qtypes)
179 return 0;
180
181 mutex_lock(&inode->ei_quota_lock);
182
183 ret = bch2_quota_transfer(c, qtypes, new_qid,
184 inode->ei_qid,
185 inode->v.i_blocks +
186 inode->ei_quota_reserved,
187 mode);
188 if (!ret)
189 for (i = 0; i < QTYP_NR; i++)
190 if (qtypes & (1 << i))
191 inode->ei_qid.q[i] = new_qid.q[i];
192
193 mutex_unlock(&inode->ei_quota_lock);
194
195 return ret;
196 }
197
bch2_vfs_inode_hash_fn(const void * data,u32 len,u32 seed)198 static u32 bch2_vfs_inode_hash_fn(const void *data, u32 len, u32 seed)
199 {
200 const subvol_inum *inum = data;
201 siphash_key_t k = { .key[0] = seed };
202
203 return siphash_2u64(inum->subvol, inum->inum, &k);
204 }
205
bch2_vfs_inode_obj_hash_fn(const void * data,u32 len,u32 seed)206 static u32 bch2_vfs_inode_obj_hash_fn(const void *data, u32 len, u32 seed)
207 {
208 const struct bch_inode_info *inode = data;
209
210 return bch2_vfs_inode_hash_fn(&inode->ei_inum, sizeof(inode->ei_inum), seed);
211 }
212
bch2_vfs_inode_cmp_fn(struct rhashtable_compare_arg * arg,const void * obj)213 static int bch2_vfs_inode_cmp_fn(struct rhashtable_compare_arg *arg,
214 const void *obj)
215 {
216 const struct bch_inode_info *inode = obj;
217 const subvol_inum *v = arg->key;
218
219 return !subvol_inum_eq(inode->ei_inum, *v);
220 }
221
222 static const struct rhashtable_params bch2_vfs_inodes_params = {
223 .head_offset = offsetof(struct bch_inode_info, hash),
224 .key_offset = offsetof(struct bch_inode_info, ei_inum),
225 .key_len = sizeof(subvol_inum),
226 .hashfn = bch2_vfs_inode_hash_fn,
227 .obj_hashfn = bch2_vfs_inode_obj_hash_fn,
228 .obj_cmpfn = bch2_vfs_inode_cmp_fn,
229 .automatic_shrinking = true,
230 };
231
232 static const struct rhashtable_params bch2_vfs_inodes_by_inum_params = {
233 .head_offset = offsetof(struct bch_inode_info, by_inum_hash),
234 .key_offset = offsetof(struct bch_inode_info, ei_inum.inum),
235 .key_len = sizeof(u64),
236 .automatic_shrinking = true,
237 };
238
bch2_inode_or_descendents_is_open(struct btree_trans * trans,struct bpos p)239 int bch2_inode_or_descendents_is_open(struct btree_trans *trans, struct bpos p)
240 {
241 struct bch_fs *c = trans->c;
242 struct rhltable *ht = &c->vfs_inodes_by_inum_table;
243 u64 inum = p.offset;
244 DARRAY(u32) subvols;
245 int ret = 0;
246
247 if (!test_bit(BCH_FS_started, &c->flags))
248 return false;
249
250 darray_init(&subvols);
251 restart_from_top:
252
253 /*
254 * Tweaked version of __rhashtable_lookup(); we need to get a list of
255 * subvolumes in which the given inode number is open.
256 *
257 * For this to work, we don't include the subvolume ID in the key that
258 * we hash - all inodes with the same inode number regardless of
259 * subvolume will hash to the same slot.
260 *
261 * This will be less than ideal if the same file is ever open
262 * simultaneously in many different snapshots:
263 */
264 rcu_read_lock();
265 struct rhash_lock_head __rcu *const *bkt;
266 struct rhash_head *he;
267 unsigned int hash;
268 struct bucket_table *tbl = rht_dereference_rcu(ht->ht.tbl, &ht->ht);
269 restart:
270 hash = rht_key_hashfn(&ht->ht, tbl, &inum, bch2_vfs_inodes_by_inum_params);
271 bkt = rht_bucket(tbl, hash);
272 do {
273 struct bch_inode_info *inode;
274
275 rht_for_each_entry_rcu_from(inode, he, rht_ptr_rcu(bkt), tbl, hash, hash) {
276 if (inode->ei_inum.inum == inum) {
277 ret = darray_push_gfp(&subvols, inode->ei_inum.subvol,
278 GFP_NOWAIT|__GFP_NOWARN);
279 if (ret) {
280 rcu_read_unlock();
281 ret = darray_make_room(&subvols, 1);
282 if (ret)
283 goto err;
284 subvols.nr = 0;
285 goto restart_from_top;
286 }
287 }
288 }
289 /* An object might have been moved to a different hash chain,
290 * while we walk along it - better check and retry.
291 */
292 } while (he != RHT_NULLS_MARKER(bkt));
293
294 /* Ensure we see any new tables. */
295 smp_rmb();
296
297 tbl = rht_dereference_rcu(tbl->future_tbl, &ht->ht);
298 if (unlikely(tbl))
299 goto restart;
300 rcu_read_unlock();
301
302 darray_for_each(subvols, i) {
303 u32 snap;
304 ret = bch2_subvolume_get_snapshot(trans, *i, &snap);
305 if (ret)
306 goto err;
307
308 ret = bch2_snapshot_is_ancestor(c, snap, p.snapshot);
309 if (ret)
310 break;
311 }
312 err:
313 darray_exit(&subvols);
314 return ret;
315 }
316
__bch2_inode_hash_find(struct bch_fs * c,subvol_inum inum)317 static struct bch_inode_info *__bch2_inode_hash_find(struct bch_fs *c, subvol_inum inum)
318 {
319 return rhashtable_lookup_fast(&c->vfs_inodes_table, &inum, bch2_vfs_inodes_params);
320 }
321
__wait_on_freeing_inode(struct bch_fs * c,struct bch_inode_info * inode,subvol_inum inum)322 static void __wait_on_freeing_inode(struct bch_fs *c,
323 struct bch_inode_info *inode,
324 subvol_inum inum)
325 {
326 wait_queue_head_t *wq;
327 struct wait_bit_queue_entry wait;
328
329 wq = inode_bit_waitqueue(&wait, &inode->v, __I_NEW);
330 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
331 spin_unlock(&inode->v.i_lock);
332
333 if (__bch2_inode_hash_find(c, inum) == inode)
334 schedule_timeout(HZ * 10);
335 finish_wait(wq, &wait.wq_entry);
336 }
337
bch2_inode_hash_find(struct bch_fs * c,struct btree_trans * trans,subvol_inum inum)338 static struct bch_inode_info *bch2_inode_hash_find(struct bch_fs *c, struct btree_trans *trans,
339 subvol_inum inum)
340 {
341 struct bch_inode_info *inode;
342 repeat:
343 inode = __bch2_inode_hash_find(c, inum);
344 if (inode) {
345 spin_lock(&inode->v.i_lock);
346 if (!test_bit(EI_INODE_HASHED, &inode->ei_flags)) {
347 spin_unlock(&inode->v.i_lock);
348 return NULL;
349 }
350 if ((inode->v.i_state & (I_FREEING|I_WILL_FREE))) {
351 if (!trans) {
352 __wait_on_freeing_inode(c, inode, inum);
353 } else {
354 int ret = drop_locks_do(trans,
355 (__wait_on_freeing_inode(c, inode, inum), 0));
356 if (ret)
357 return ERR_PTR(ret);
358 }
359 goto repeat;
360 }
361 __iget(&inode->v);
362 spin_unlock(&inode->v.i_lock);
363 }
364
365 return inode;
366 }
367
bch2_inode_hash_remove(struct bch_fs * c,struct bch_inode_info * inode)368 static void bch2_inode_hash_remove(struct bch_fs *c, struct bch_inode_info *inode)
369 {
370 spin_lock(&inode->v.i_lock);
371 bool remove = test_and_clear_bit(EI_INODE_HASHED, &inode->ei_flags);
372 spin_unlock(&inode->v.i_lock);
373
374 if (remove) {
375 int ret = rhltable_remove(&c->vfs_inodes_by_inum_table,
376 &inode->by_inum_hash, bch2_vfs_inodes_by_inum_params);
377 BUG_ON(ret);
378
379 ret = rhashtable_remove_fast(&c->vfs_inodes_table,
380 &inode->hash, bch2_vfs_inodes_params);
381 BUG_ON(ret);
382 inode->v.i_hash.pprev = NULL;
383 /*
384 * This pairs with the bch2_inode_hash_find() ->
385 * __wait_on_freeing_inode() path
386 */
387 inode_wake_up_bit(&inode->v, __I_NEW);
388 }
389 }
390
bch2_inode_hash_insert(struct bch_fs * c,struct btree_trans * trans,struct bch_inode_info * inode)391 static struct bch_inode_info *bch2_inode_hash_insert(struct bch_fs *c,
392 struct btree_trans *trans,
393 struct bch_inode_info *inode)
394 {
395 struct bch_inode_info *old = inode;
396
397 set_bit(EI_INODE_HASHED, &inode->ei_flags);
398 retry:
399 if (unlikely(rhashtable_lookup_insert_key(&c->vfs_inodes_table,
400 &inode->ei_inum,
401 &inode->hash,
402 bch2_vfs_inodes_params))) {
403 old = bch2_inode_hash_find(c, trans, inode->ei_inum);
404 if (!old)
405 goto retry;
406
407 clear_bit(EI_INODE_HASHED, &inode->ei_flags);
408
409 /*
410 * bcachefs doesn't use I_NEW; we have no use for it since we
411 * only insert fully created inodes in the inode hash table. But
412 * discard_new_inode() expects it to be set...
413 */
414 inode->v.i_state |= I_NEW;
415 /*
416 * We don't want bch2_evict_inode() to delete the inode on disk,
417 * we just raced and had another inode in cache. Normally new
418 * inodes don't have nlink == 0 - except tmpfiles do...
419 */
420 set_nlink(&inode->v, 1);
421 discard_new_inode(&inode->v);
422 return old;
423 } else {
424 int ret = rhltable_insert(&c->vfs_inodes_by_inum_table,
425 &inode->by_inum_hash,
426 bch2_vfs_inodes_by_inum_params);
427 BUG_ON(ret);
428
429 inode_fake_hash(&inode->v);
430
431 inode_sb_list_add(&inode->v);
432
433 mutex_lock(&c->vfs_inodes_lock);
434 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
435 mutex_unlock(&c->vfs_inodes_lock);
436 return inode;
437 }
438 }
439
440 #define memalloc_flags_do(_flags, _do) \
441 ({ \
442 unsigned _saved_flags = memalloc_flags_save(_flags); \
443 typeof(_do) _ret = _do; \
444 memalloc_noreclaim_restore(_saved_flags); \
445 _ret; \
446 })
447
bch2_alloc_inode(struct super_block * sb)448 static struct inode *bch2_alloc_inode(struct super_block *sb)
449 {
450 BUG();
451 }
452
__bch2_new_inode(struct bch_fs * c,gfp_t gfp)453 static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c, gfp_t gfp)
454 {
455 struct bch_inode_info *inode = alloc_inode_sb(c->vfs_sb,
456 bch2_inode_cache, gfp);
457 if (!inode)
458 return NULL;
459
460 inode_init_once(&inode->v);
461 mutex_init(&inode->ei_update_lock);
462 two_state_lock_init(&inode->ei_pagecache_lock);
463 INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
464 inode->ei_flags = 0;
465 mutex_init(&inode->ei_quota_lock);
466 memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush));
467
468 if (unlikely(inode_init_always_gfp(c->vfs_sb, &inode->v, gfp))) {
469 kmem_cache_free(bch2_inode_cache, inode);
470 return NULL;
471 }
472
473 return inode;
474 }
475
476 /*
477 * Allocate a new inode, dropping/retaking btree locks if necessary:
478 */
bch2_new_inode(struct btree_trans * trans)479 static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
480 {
481 struct bch_inode_info *inode = __bch2_new_inode(trans->c, GFP_NOWAIT);
482
483 if (unlikely(!inode)) {
484 int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c, GFP_NOFS)) ? 0 : -ENOMEM);
485 if (ret && inode) {
486 __destroy_inode(&inode->v);
487 kmem_cache_free(bch2_inode_cache, inode);
488 }
489 if (ret)
490 return ERR_PTR(ret);
491 }
492
493 return inode;
494 }
495
bch2_inode_hash_init_insert(struct btree_trans * trans,subvol_inum inum,struct bch_inode_unpacked * bi,struct bch_subvolume * subvol)496 static struct bch_inode_info *bch2_inode_hash_init_insert(struct btree_trans *trans,
497 subvol_inum inum,
498 struct bch_inode_unpacked *bi,
499 struct bch_subvolume *subvol)
500 {
501 struct bch_inode_info *inode = bch2_new_inode(trans);
502 if (IS_ERR(inode))
503 return inode;
504
505 bch2_vfs_inode_init(trans, inum, inode, bi, subvol);
506
507 return bch2_inode_hash_insert(trans->c, trans, inode);
508
509 }
510
bch2_vfs_inode_get(struct bch_fs * c,subvol_inum inum)511 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
512 {
513 struct bch_inode_info *inode = bch2_inode_hash_find(c, NULL, inum);
514 if (inode)
515 return &inode->v;
516
517 struct btree_trans *trans = bch2_trans_get(c);
518
519 struct bch_inode_unpacked inode_u;
520 struct bch_subvolume subvol;
521 int ret = lockrestart_do(trans,
522 bch2_subvolume_get(trans, inum.subvol, true, &subvol) ?:
523 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?:
524 PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol));
525 bch2_trans_put(trans);
526
527 return ret ? ERR_PTR(ret) : &inode->v;
528 }
529
530 struct bch_inode_info *
__bch2_create(struct mnt_idmap * idmap,struct bch_inode_info * dir,struct dentry * dentry,umode_t mode,dev_t rdev,subvol_inum snapshot_src,unsigned flags)531 __bch2_create(struct mnt_idmap *idmap,
532 struct bch_inode_info *dir, struct dentry *dentry,
533 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
534 unsigned flags)
535 {
536 struct bch_fs *c = dir->v.i_sb->s_fs_info;
537 struct btree_trans *trans;
538 struct bch_inode_unpacked dir_u;
539 struct bch_inode_info *inode;
540 struct bch_inode_unpacked inode_u;
541 struct posix_acl *default_acl = NULL, *acl = NULL;
542 subvol_inum inum;
543 struct bch_subvolume subvol;
544 u64 journal_seq = 0;
545 kuid_t kuid;
546 kgid_t kgid;
547 int ret;
548
549 /*
550 * preallocate acls + vfs inode before btree transaction, so that
551 * nothing can fail after the transaction succeeds:
552 */
553 #ifdef CONFIG_BCACHEFS_POSIX_ACL
554 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
555 if (ret)
556 return ERR_PTR(ret);
557 #endif
558 inode = __bch2_new_inode(c, GFP_NOFS);
559 if (unlikely(!inode)) {
560 inode = ERR_PTR(-ENOMEM);
561 goto err;
562 }
563
564 bch2_inode_init_early(c, &inode_u);
565
566 if (!(flags & BCH_CREATE_TMPFILE))
567 mutex_lock(&dir->ei_update_lock);
568
569 trans = bch2_trans_get(c);
570 retry:
571 bch2_trans_begin(trans);
572
573 kuid = mapped_fsuid(idmap, i_user_ns(&dir->v));
574 kgid = mapped_fsgid(idmap, i_user_ns(&dir->v));
575 ret = bch2_subvol_is_ro_trans(trans, dir->ei_inum.subvol) ?:
576 bch2_create_trans(trans,
577 inode_inum(dir), &dir_u, &inode_u,
578 !(flags & BCH_CREATE_TMPFILE)
579 ? &dentry->d_name : NULL,
580 from_kuid(i_user_ns(&dir->v), kuid),
581 from_kgid(i_user_ns(&dir->v), kgid),
582 mode, rdev,
583 default_acl, acl, snapshot_src, flags) ?:
584 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
585 KEY_TYPE_QUOTA_PREALLOC);
586 if (unlikely(ret))
587 goto err_before_quota;
588
589 inum.subvol = inode_u.bi_subvol ?: dir->ei_inum.subvol;
590 inum.inum = inode_u.bi_inum;
591
592 ret = bch2_subvolume_get(trans, inum.subvol, true, &subvol) ?:
593 bch2_trans_commit(trans, NULL, &journal_seq, 0);
594 if (unlikely(ret)) {
595 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
596 KEY_TYPE_QUOTA_WARN);
597 err_before_quota:
598 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
599 goto retry;
600 goto err_trans;
601 }
602
603 if (!(flags & BCH_CREATE_TMPFILE)) {
604 bch2_inode_update_after_write(trans, dir, &dir_u,
605 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
606 mutex_unlock(&dir->ei_update_lock);
607 }
608
609 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
610
611 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
612 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
613
614 /*
615 * we must insert the new inode into the inode cache before calling
616 * bch2_trans_exit() and dropping locks, else we could race with another
617 * thread pulling the inode in and modifying it:
618 *
619 * also, calling bch2_inode_hash_insert() without passing in the
620 * transaction object is sketchy - if we could ever end up in
621 * __wait_on_freeing_inode(), we'd risk deadlock.
622 *
623 * But that shouldn't be possible, since we still have the inode locked
624 * that we just created, and we _really_ can't take a transaction
625 * restart here.
626 */
627 inode = bch2_inode_hash_insert(c, NULL, inode);
628 bch2_trans_put(trans);
629 err:
630 posix_acl_release(default_acl);
631 posix_acl_release(acl);
632 return inode;
633 err_trans:
634 if (!(flags & BCH_CREATE_TMPFILE))
635 mutex_unlock(&dir->ei_update_lock);
636
637 bch2_trans_put(trans);
638 make_bad_inode(&inode->v);
639 iput(&inode->v);
640 inode = ERR_PTR(ret);
641 goto err;
642 }
643
644 /* methods */
645
bch2_lookup_trans(struct btree_trans * trans,subvol_inum dir,struct bch_hash_info * dir_hash_info,const struct qstr * name)646 static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans,
647 subvol_inum dir, struct bch_hash_info *dir_hash_info,
648 const struct qstr *name)
649 {
650 struct bch_fs *c = trans->c;
651 subvol_inum inum = {};
652 struct printbuf buf = PRINTBUF;
653
654 struct qstr lookup_name;
655 int ret = bch2_maybe_casefold(trans, dir_hash_info, name, &lookup_name);
656 if (ret)
657 return ERR_PTR(ret);
658
659 struct btree_iter dirent_iter = {};
660 struct bkey_s_c k = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc,
661 dir_hash_info, dir, &lookup_name, 0);
662 ret = bkey_err(k);
663 if (ret)
664 return ERR_PTR(ret);
665
666 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
667
668 ret = bch2_dirent_read_target(trans, dir, d, &inum);
669 if (ret > 0)
670 ret = -ENOENT;
671 if (ret)
672 goto err;
673
674 struct bch_inode_info *inode = bch2_inode_hash_find(c, trans, inum);
675 if (inode)
676 goto out;
677
678 /*
679 * Note: if check/repair needs it, we commit before
680 * bch2_inode_hash_init_insert(), as after that point we can't take a
681 * restart - not in the top level loop with a commit_do(), like we
682 * usually do:
683 */
684
685 struct bch_subvolume subvol;
686 struct bch_inode_unpacked inode_u;
687 ret = bch2_subvolume_get(trans, inum.subvol, true, &subvol) ?:
688 bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?:
689 bch2_check_dirent_target(trans, &dirent_iter, d, &inode_u, false) ?:
690 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc) ?:
691 PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol));
692
693 /*
694 * don't remove it: check_inodes might find another inode that points
695 * back to this dirent
696 */
697 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
698 c, "dirent to missing inode:\n%s",
699 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf));
700 if (ret)
701 goto err;
702 out:
703 bch2_trans_iter_exit(trans, &dirent_iter);
704 printbuf_exit(&buf);
705 return inode;
706 err:
707 inode = ERR_PTR(ret);
708 goto out;
709 }
710
bch2_lookup(struct inode * vdir,struct dentry * dentry,unsigned int flags)711 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
712 unsigned int flags)
713 {
714 struct bch_fs *c = vdir->i_sb->s_fs_info;
715 struct bch_inode_info *dir = to_bch_ei(vdir);
716 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
717
718 struct bch_inode_info *inode;
719 bch2_trans_do(c,
720 PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir),
721 &hash, &dentry->d_name)));
722 if (IS_ERR(inode))
723 inode = NULL;
724
725 if (!inode && IS_CASEFOLDED(vdir)) {
726 /*
727 * Do not cache a negative dentry in casefolded directories
728 * as it would need to be invalidated in the following situation:
729 * - Lookup file "blAH" in a casefolded directory
730 * - Creation of file "BLAH" in a casefolded directory
731 * - Lookup file "blAH" in a casefolded directory
732 * which would fail if we had a negative dentry.
733 *
734 * We should come back to this when VFS has a method to handle
735 * this edgecase.
736 */
737 return NULL;
738 }
739
740 return d_splice_alias(&inode->v, dentry);
741 }
742
bch2_mknod(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,umode_t mode,dev_t rdev)743 static int bch2_mknod(struct mnt_idmap *idmap,
744 struct inode *vdir, struct dentry *dentry,
745 umode_t mode, dev_t rdev)
746 {
747 struct bch_inode_info *inode =
748 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
749 (subvol_inum) { 0 }, 0);
750
751 if (IS_ERR(inode))
752 return bch2_err_class(PTR_ERR(inode));
753
754 d_instantiate(dentry, &inode->v);
755 return 0;
756 }
757
bch2_create(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,umode_t mode,bool excl)758 static int bch2_create(struct mnt_idmap *idmap,
759 struct inode *vdir, struct dentry *dentry,
760 umode_t mode, bool excl)
761 {
762 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
763 }
764
__bch2_link(struct bch_fs * c,struct bch_inode_info * inode,struct bch_inode_info * dir,struct dentry * dentry)765 static int __bch2_link(struct bch_fs *c,
766 struct bch_inode_info *inode,
767 struct bch_inode_info *dir,
768 struct dentry *dentry)
769 {
770 struct bch_inode_unpacked dir_u, inode_u;
771 int ret;
772
773 mutex_lock(&inode->ei_update_lock);
774 struct btree_trans *trans = bch2_trans_get(c);
775
776 ret = commit_do(trans, NULL, NULL, 0,
777 bch2_link_trans(trans,
778 inode_inum(dir), &dir_u,
779 inode_inum(inode), &inode_u,
780 &dentry->d_name));
781
782 if (likely(!ret)) {
783 bch2_inode_update_after_write(trans, dir, &dir_u,
784 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
785 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
786 }
787
788 bch2_trans_put(trans);
789 mutex_unlock(&inode->ei_update_lock);
790 return ret;
791 }
792
bch2_link(struct dentry * old_dentry,struct inode * vdir,struct dentry * dentry)793 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
794 struct dentry *dentry)
795 {
796 struct bch_fs *c = vdir->i_sb->s_fs_info;
797 struct bch_inode_info *dir = to_bch_ei(vdir);
798 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
799 int ret;
800
801 lockdep_assert_held(&inode->v.i_rwsem);
802
803 ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
804 bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
805 __bch2_link(c, inode, dir, dentry);
806 if (unlikely(ret))
807 return bch2_err_class(ret);
808
809 ihold(&inode->v);
810 d_instantiate(dentry, &inode->v);
811 return 0;
812 }
813
__bch2_unlink(struct inode * vdir,struct dentry * dentry,bool deleting_snapshot)814 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
815 bool deleting_snapshot)
816 {
817 struct bch_fs *c = vdir->i_sb->s_fs_info;
818 struct bch_inode_info *dir = to_bch_ei(vdir);
819 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
820 struct bch_inode_unpacked dir_u, inode_u;
821 int ret;
822
823 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
824
825 struct btree_trans *trans = bch2_trans_get(c);
826
827 ret = commit_do(trans, NULL, NULL,
828 BCH_TRANS_COMMIT_no_enospc,
829 bch2_unlink_trans(trans,
830 inode_inum(dir), &dir_u,
831 &inode_u, &dentry->d_name,
832 deleting_snapshot));
833 if (unlikely(ret))
834 goto err;
835
836 bch2_inode_update_after_write(trans, dir, &dir_u,
837 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
838 bch2_inode_update_after_write(trans, inode, &inode_u,
839 ATTR_MTIME);
840
841 if (inode_u.bi_subvol) {
842 /*
843 * Subvolume deletion is asynchronous, but we still want to tell
844 * the VFS that it's been deleted here:
845 */
846 set_nlink(&inode->v, 0);
847 }
848
849 if (IS_CASEFOLDED(vdir))
850 d_invalidate(dentry);
851 err:
852 bch2_trans_put(trans);
853 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
854
855 return ret;
856 }
857
bch2_unlink(struct inode * vdir,struct dentry * dentry)858 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
859 {
860 struct bch_inode_info *dir= to_bch_ei(vdir);
861 struct bch_fs *c = dir->v.i_sb->s_fs_info;
862
863 int ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
864 __bch2_unlink(vdir, dentry, false);
865 return bch2_err_class(ret);
866 }
867
bch2_symlink(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,const char * symname)868 static int bch2_symlink(struct mnt_idmap *idmap,
869 struct inode *vdir, struct dentry *dentry,
870 const char *symname)
871 {
872 struct bch_fs *c = vdir->i_sb->s_fs_info;
873 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
874 int ret;
875
876 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
877 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
878 if (IS_ERR(inode))
879 return bch2_err_class(PTR_ERR(inode));
880
881 inode_lock(&inode->v);
882 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
883 inode_unlock(&inode->v);
884
885 if (unlikely(ret))
886 goto err;
887
888 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
889 if (unlikely(ret))
890 goto err;
891
892 ret = __bch2_link(c, inode, dir, dentry);
893 if (unlikely(ret))
894 goto err;
895
896 d_instantiate(dentry, &inode->v);
897 return 0;
898 err:
899 iput(&inode->v);
900 return bch2_err_class(ret);
901 }
902
bch2_mkdir(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,umode_t mode)903 static struct dentry *bch2_mkdir(struct mnt_idmap *idmap,
904 struct inode *vdir, struct dentry *dentry, umode_t mode)
905 {
906 return ERR_PTR(bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0));
907 }
908
bch2_rename2(struct mnt_idmap * idmap,struct inode * src_vdir,struct dentry * src_dentry,struct inode * dst_vdir,struct dentry * dst_dentry,unsigned flags)909 static int bch2_rename2(struct mnt_idmap *idmap,
910 struct inode *src_vdir, struct dentry *src_dentry,
911 struct inode *dst_vdir, struct dentry *dst_dentry,
912 unsigned flags)
913 {
914 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
915 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
916 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
917 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
918 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
919 struct bch_inode_unpacked dst_dir_u, src_dir_u;
920 struct bch_inode_unpacked src_inode_u, dst_inode_u, *whiteout_inode_u;
921 struct btree_trans *trans;
922 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
923 ? BCH_RENAME_EXCHANGE
924 : dst_dentry->d_inode
925 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
926 bool whiteout = !!(flags & RENAME_WHITEOUT);
927 int ret;
928
929 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE|RENAME_WHITEOUT))
930 return -EINVAL;
931
932 if (mode == BCH_RENAME_OVERWRITE) {
933 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
934 0, LLONG_MAX);
935 if (ret)
936 return ret;
937 }
938
939 bch2_lock_inodes(INODE_UPDATE_LOCK,
940 src_dir,
941 dst_dir,
942 src_inode,
943 dst_inode);
944
945 trans = bch2_trans_get(c);
946
947 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_inum.subvol) ?:
948 bch2_subvol_is_ro_trans(trans, dst_dir->ei_inum.subvol);
949 if (ret)
950 goto err_tx_restart;
951
952 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
953 ret = bch2_fs_quota_transfer(c, src_inode,
954 dst_dir->ei_qid,
955 1 << QTYP_PRJ,
956 KEY_TYPE_QUOTA_PREALLOC);
957 if (ret)
958 goto err;
959 }
960
961 if (mode == BCH_RENAME_EXCHANGE &&
962 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
963 ret = bch2_fs_quota_transfer(c, dst_inode,
964 src_dir->ei_qid,
965 1 << QTYP_PRJ,
966 KEY_TYPE_QUOTA_PREALLOC);
967 if (ret)
968 goto err;
969 }
970 retry:
971 bch2_trans_begin(trans);
972
973 ret = bch2_rename_trans(trans,
974 inode_inum(src_dir), &src_dir_u,
975 inode_inum(dst_dir), &dst_dir_u,
976 &src_inode_u,
977 &dst_inode_u,
978 &src_dentry->d_name,
979 &dst_dentry->d_name,
980 mode);
981 if (unlikely(ret))
982 goto err_tx_restart;
983
984 if (whiteout) {
985 whiteout_inode_u = bch2_trans_kmalloc_nomemzero(trans, sizeof(*whiteout_inode_u));
986 ret = PTR_ERR_OR_ZERO(whiteout_inode_u);
987 if (unlikely(ret))
988 goto err_tx_restart;
989 bch2_inode_init_early(c, whiteout_inode_u);
990
991 ret = bch2_create_trans(trans,
992 inode_inum(src_dir), &src_dir_u,
993 whiteout_inode_u,
994 &src_dentry->d_name,
995 from_kuid(i_user_ns(&src_dir->v), current_fsuid()),
996 from_kgid(i_user_ns(&src_dir->v), current_fsgid()),
997 S_IFCHR|WHITEOUT_MODE, 0,
998 NULL, NULL, (subvol_inum) { 0 }, 0) ?:
999 bch2_quota_acct(c, bch_qid(whiteout_inode_u), Q_INO, 1,
1000 KEY_TYPE_QUOTA_PREALLOC);
1001 if (unlikely(ret))
1002 goto err_tx_restart;
1003 }
1004
1005 ret = bch2_trans_commit(trans, NULL, NULL, 0);
1006 if (unlikely(ret)) {
1007 err_tx_restart:
1008 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1009 goto retry;
1010 goto err;
1011 }
1012
1013 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
1014 BUG_ON(dst_inode &&
1015 dst_inode->v.i_ino != dst_inode_u.bi_inum);
1016
1017 bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
1018 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
1019
1020 if (src_dir != dst_dir)
1021 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
1022 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
1023
1024 bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
1025 ATTR_CTIME);
1026
1027 if (dst_inode)
1028 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
1029 ATTR_CTIME);
1030 err:
1031 bch2_trans_put(trans);
1032
1033 bch2_fs_quota_transfer(c, src_inode,
1034 bch_qid(&src_inode->ei_inode),
1035 1 << QTYP_PRJ,
1036 KEY_TYPE_QUOTA_NOCHECK);
1037 if (dst_inode)
1038 bch2_fs_quota_transfer(c, dst_inode,
1039 bch_qid(&dst_inode->ei_inode),
1040 1 << QTYP_PRJ,
1041 KEY_TYPE_QUOTA_NOCHECK);
1042
1043 bch2_unlock_inodes(INODE_UPDATE_LOCK,
1044 src_dir,
1045 dst_dir,
1046 src_inode,
1047 dst_inode);
1048
1049 return bch2_err_class(ret);
1050 }
1051
bch2_setattr_copy(struct mnt_idmap * idmap,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,struct iattr * attr)1052 static void bch2_setattr_copy(struct mnt_idmap *idmap,
1053 struct bch_inode_info *inode,
1054 struct bch_inode_unpacked *bi,
1055 struct iattr *attr)
1056 {
1057 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1058 unsigned int ia_valid = attr->ia_valid;
1059 kuid_t kuid;
1060 kgid_t kgid;
1061
1062 if (ia_valid & ATTR_UID) {
1063 kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
1064 bi->bi_uid = from_kuid(i_user_ns(&inode->v), kuid);
1065 }
1066 if (ia_valid & ATTR_GID) {
1067 kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
1068 bi->bi_gid = from_kgid(i_user_ns(&inode->v), kgid);
1069 }
1070
1071 if (ia_valid & ATTR_SIZE)
1072 bi->bi_size = attr->ia_size;
1073
1074 if (ia_valid & ATTR_ATIME)
1075 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
1076 if (ia_valid & ATTR_MTIME)
1077 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
1078 if (ia_valid & ATTR_CTIME)
1079 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
1080
1081 if (ia_valid & ATTR_MODE) {
1082 umode_t mode = attr->ia_mode;
1083 kgid_t gid = ia_valid & ATTR_GID
1084 ? kgid
1085 : inode->v.i_gid;
1086
1087 if (!in_group_or_capable(idmap, &inode->v,
1088 make_vfsgid(idmap, i_user_ns(&inode->v), gid)))
1089 mode &= ~S_ISGID;
1090 bi->bi_mode = mode;
1091 }
1092 }
1093
bch2_setattr_nonsize(struct mnt_idmap * idmap,struct bch_inode_info * inode,struct iattr * attr)1094 int bch2_setattr_nonsize(struct mnt_idmap *idmap,
1095 struct bch_inode_info *inode,
1096 struct iattr *attr)
1097 {
1098 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1099 struct bch_qid qid;
1100 struct btree_trans *trans;
1101 struct btree_iter inode_iter = {};
1102 struct bch_inode_unpacked inode_u;
1103 struct posix_acl *acl = NULL;
1104 kuid_t kuid;
1105 kgid_t kgid;
1106 int ret;
1107
1108 mutex_lock(&inode->ei_update_lock);
1109
1110 qid = inode->ei_qid;
1111
1112 if (attr->ia_valid & ATTR_UID) {
1113 kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
1114 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), kuid);
1115 }
1116
1117 if (attr->ia_valid & ATTR_GID) {
1118 kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
1119 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), kgid);
1120 }
1121
1122 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
1123 KEY_TYPE_QUOTA_PREALLOC);
1124 if (ret)
1125 goto err;
1126
1127 trans = bch2_trans_get(c);
1128 retry:
1129 bch2_trans_begin(trans);
1130 kfree(acl);
1131 acl = NULL;
1132
1133 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
1134 BTREE_ITER_intent);
1135 if (ret)
1136 goto btree_err;
1137
1138 bch2_setattr_copy(idmap, inode, &inode_u, attr);
1139
1140 if (attr->ia_valid & ATTR_MODE) {
1141 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
1142 inode_u.bi_mode, &acl);
1143 if (ret)
1144 goto btree_err;
1145 }
1146
1147 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
1148 bch2_trans_commit(trans, NULL, NULL,
1149 BCH_TRANS_COMMIT_no_enospc);
1150 btree_err:
1151 bch2_trans_iter_exit(trans, &inode_iter);
1152
1153 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1154 goto retry;
1155 if (unlikely(ret))
1156 goto err_trans;
1157
1158 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
1159
1160 if (acl)
1161 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
1162 err_trans:
1163 bch2_trans_put(trans);
1164 err:
1165 mutex_unlock(&inode->ei_update_lock);
1166
1167 return bch2_err_class(ret);
1168 }
1169
bch2_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned query_flags)1170 static int bch2_getattr(struct mnt_idmap *idmap,
1171 const struct path *path, struct kstat *stat,
1172 u32 request_mask, unsigned query_flags)
1173 {
1174 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
1175 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1176 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, &inode->v);
1177 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, &inode->v);
1178
1179 stat->dev = inode->v.i_sb->s_dev;
1180 stat->ino = inode->v.i_ino;
1181 stat->mode = inode->v.i_mode;
1182 stat->nlink = inode->v.i_nlink;
1183 stat->uid = vfsuid_into_kuid(vfsuid);
1184 stat->gid = vfsgid_into_kgid(vfsgid);
1185 stat->rdev = inode->v.i_rdev;
1186 stat->size = i_size_read(&inode->v);
1187 stat->atime = inode_get_atime(&inode->v);
1188 stat->mtime = inode_get_mtime(&inode->v);
1189 stat->ctime = inode_get_ctime(&inode->v);
1190 stat->blksize = block_bytes(c);
1191 stat->blocks = inode->v.i_blocks;
1192
1193 stat->subvol = inode->ei_inum.subvol;
1194 stat->result_mask |= STATX_SUBVOL;
1195
1196 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->v.i_mode)) {
1197 stat->result_mask |= STATX_DIOALIGN;
1198 /*
1199 * this is incorrect; we should be tracking this in superblock,
1200 * and checking the alignment of open devices
1201 */
1202 stat->dio_mem_align = SECTOR_SIZE;
1203 stat->dio_offset_align = block_bytes(c);
1204 }
1205
1206 if (request_mask & STATX_BTIME) {
1207 stat->result_mask |= STATX_BTIME;
1208 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
1209 }
1210
1211 if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
1212 stat->attributes |= STATX_ATTR_IMMUTABLE;
1213 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
1214
1215 if (inode->ei_inode.bi_flags & BCH_INODE_append)
1216 stat->attributes |= STATX_ATTR_APPEND;
1217 stat->attributes_mask |= STATX_ATTR_APPEND;
1218
1219 if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
1220 stat->attributes |= STATX_ATTR_NODUMP;
1221 stat->attributes_mask |= STATX_ATTR_NODUMP;
1222
1223 return 0;
1224 }
1225
bch2_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)1226 static int bch2_setattr(struct mnt_idmap *idmap,
1227 struct dentry *dentry, struct iattr *iattr)
1228 {
1229 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
1230 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1231 int ret;
1232
1233 lockdep_assert_held(&inode->v.i_rwsem);
1234
1235 ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
1236 setattr_prepare(idmap, dentry, iattr);
1237 if (ret)
1238 return ret;
1239
1240 return iattr->ia_valid & ATTR_SIZE
1241 ? bchfs_truncate(idmap, inode, iattr)
1242 : bch2_setattr_nonsize(idmap, inode, iattr);
1243 }
1244
bch2_tmpfile(struct mnt_idmap * idmap,struct inode * vdir,struct file * file,umode_t mode)1245 static int bch2_tmpfile(struct mnt_idmap *idmap,
1246 struct inode *vdir, struct file *file, umode_t mode)
1247 {
1248 struct bch_inode_info *inode =
1249 __bch2_create(idmap, to_bch_ei(vdir),
1250 file->f_path.dentry, mode, 0,
1251 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
1252
1253 if (IS_ERR(inode))
1254 return bch2_err_class(PTR_ERR(inode));
1255
1256 d_mark_tmpfile(file, &inode->v);
1257 d_instantiate(file->f_path.dentry, &inode->v);
1258 return finish_open_simple(file, 0);
1259 }
1260
1261 struct bch_fiemap_extent {
1262 struct bkey_buf kbuf;
1263 unsigned flags;
1264 };
1265
bch2_fill_extent(struct bch_fs * c,struct fiemap_extent_info * info,struct bch_fiemap_extent * fe)1266 static int bch2_fill_extent(struct bch_fs *c,
1267 struct fiemap_extent_info *info,
1268 struct bch_fiemap_extent *fe)
1269 {
1270 struct bkey_s_c k = bkey_i_to_s_c(fe->kbuf.k);
1271 unsigned flags = fe->flags;
1272
1273 BUG_ON(!k.k->size);
1274
1275 if (bkey_extent_is_direct_data(k.k)) {
1276 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1277 const union bch_extent_entry *entry;
1278 struct extent_ptr_decoded p;
1279 int ret;
1280
1281 if (k.k->type == KEY_TYPE_reflink_v)
1282 flags |= FIEMAP_EXTENT_SHARED;
1283
1284 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1285 int flags2 = 0;
1286 u64 offset = p.ptr.offset;
1287
1288 if (p.ptr.unwritten)
1289 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
1290
1291 if (p.crc.compression_type)
1292 flags2 |= FIEMAP_EXTENT_ENCODED;
1293 else
1294 offset += p.crc.offset;
1295
1296 if ((offset & (block_sectors(c) - 1)) ||
1297 (k.k->size & (block_sectors(c) - 1)))
1298 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
1299
1300 ret = fiemap_fill_next_extent(info,
1301 bkey_start_offset(k.k) << 9,
1302 offset << 9,
1303 k.k->size << 9, flags|flags2);
1304 if (ret)
1305 return ret;
1306 }
1307
1308 return 0;
1309 } else if (bkey_extent_is_inline_data(k.k)) {
1310 return fiemap_fill_next_extent(info,
1311 bkey_start_offset(k.k) << 9,
1312 0, k.k->size << 9,
1313 flags|
1314 FIEMAP_EXTENT_DATA_INLINE);
1315 } else if (k.k->type == KEY_TYPE_reservation) {
1316 return fiemap_fill_next_extent(info,
1317 bkey_start_offset(k.k) << 9,
1318 0, k.k->size << 9,
1319 flags|
1320 FIEMAP_EXTENT_DELALLOC|
1321 FIEMAP_EXTENT_UNWRITTEN);
1322 } else {
1323 BUG();
1324 }
1325 }
1326
1327 /*
1328 * Scan a range of an inode for data in pagecache.
1329 *
1330 * Intended to be retryable, so don't modify the output params until success is
1331 * imminent.
1332 */
1333 static int
bch2_fiemap_hole_pagecache(struct inode * vinode,u64 * start,u64 * end,bool nonblock)1334 bch2_fiemap_hole_pagecache(struct inode *vinode, u64 *start, u64 *end,
1335 bool nonblock)
1336 {
1337 loff_t dstart, dend;
1338
1339 dstart = bch2_seek_pagecache_data(vinode, *start, *end, 0, nonblock);
1340 if (dstart < 0)
1341 return dstart;
1342
1343 if (dstart == *end) {
1344 *start = dstart;
1345 return 0;
1346 }
1347
1348 dend = bch2_seek_pagecache_hole(vinode, dstart, *end, 0, nonblock);
1349 if (dend < 0)
1350 return dend;
1351
1352 /* race */
1353 BUG_ON(dstart == dend);
1354
1355 *start = dstart;
1356 *end = dend;
1357 return 0;
1358 }
1359
1360 /*
1361 * Scan a range of pagecache that corresponds to a file mapping hole in the
1362 * extent btree. If data is found, fake up an extent key so it looks like a
1363 * delalloc extent to the rest of the fiemap processing code.
1364 */
1365 static int
bch2_next_fiemap_pagecache_extent(struct btree_trans * trans,struct bch_inode_info * inode,u64 start,u64 end,struct bch_fiemap_extent * cur)1366 bch2_next_fiemap_pagecache_extent(struct btree_trans *trans, struct bch_inode_info *inode,
1367 u64 start, u64 end, struct bch_fiemap_extent *cur)
1368 {
1369 struct bch_fs *c = trans->c;
1370 struct bkey_i_extent *delextent;
1371 struct bch_extent_ptr ptr = {};
1372 loff_t dstart = start << 9, dend = end << 9;
1373 int ret;
1374
1375 /*
1376 * We hold btree locks here so we cannot block on folio locks without
1377 * dropping trans locks first. Run a nonblocking scan for the common
1378 * case of no folios over holes and fall back on failure.
1379 *
1380 * Note that dropping locks like this is technically racy against
1381 * writeback inserting to the extent tree, but a non-sync fiemap scan is
1382 * fundamentally racy with writeback anyways. Therefore, just report the
1383 * range as delalloc regardless of whether we have to cycle trans locks.
1384 */
1385 ret = bch2_fiemap_hole_pagecache(&inode->v, &dstart, &dend, true);
1386 if (ret == -EAGAIN)
1387 ret = drop_locks_do(trans,
1388 bch2_fiemap_hole_pagecache(&inode->v, &dstart, &dend, false));
1389 if (ret < 0)
1390 return ret;
1391
1392 /*
1393 * Create a fake extent key in the buffer. We have to add a dummy extent
1394 * pointer for the fill code to add an extent entry. It's explicitly
1395 * zeroed to reflect delayed allocation (i.e. phys offset 0).
1396 */
1397 bch2_bkey_buf_realloc(&cur->kbuf, c, sizeof(*delextent) / sizeof(u64));
1398 delextent = bkey_extent_init(cur->kbuf.k);
1399 delextent->k.p = POS(inode->ei_inum.inum, dend >> 9);
1400 delextent->k.size = (dend - dstart) >> 9;
1401 bch2_bkey_append_ptr(&delextent->k_i, ptr);
1402
1403 cur->flags = FIEMAP_EXTENT_DELALLOC;
1404
1405 return 0;
1406 }
1407
bch2_next_fiemap_extent(struct btree_trans * trans,struct bch_inode_info * inode,u64 start,u64 end,struct bch_fiemap_extent * cur)1408 static int bch2_next_fiemap_extent(struct btree_trans *trans,
1409 struct bch_inode_info *inode,
1410 u64 start, u64 end,
1411 struct bch_fiemap_extent *cur)
1412 {
1413 u32 snapshot;
1414 int ret = bch2_subvolume_get_snapshot(trans, inode->ei_inum.subvol, &snapshot);
1415 if (ret)
1416 return ret;
1417
1418 struct btree_iter iter;
1419 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
1420 SPOS(inode->ei_inum.inum, start, snapshot), 0);
1421
1422 struct bkey_s_c k =
1423 bch2_btree_iter_peek_max(trans, &iter, POS(inode->ei_inum.inum, end));
1424 ret = bkey_err(k);
1425 if (ret)
1426 goto err;
1427
1428 u64 pagecache_end = k.k ? max(start, bkey_start_offset(k.k)) : end;
1429
1430 ret = bch2_next_fiemap_pagecache_extent(trans, inode, start, pagecache_end, cur);
1431 if (ret)
1432 goto err;
1433
1434 struct bpos pagecache_start = bkey_start_pos(&cur->kbuf.k->k);
1435
1436 /*
1437 * Does the pagecache or the btree take precedence?
1438 *
1439 * It _should_ be the pagecache, so that we correctly report delalloc
1440 * extents when dirty in the pagecache (we're COW, after all).
1441 *
1442 * But we'd have to add per-sector writeback tracking to
1443 * bch_folio_state, otherwise we report delalloc extents for clean
1444 * cached data in the pagecache.
1445 *
1446 * We should do this, but even then fiemap won't report stable mappings:
1447 * on bcachefs data moves around in the background (copygc, rebalance)
1448 * and we don't provide a way for userspace to lock that out.
1449 */
1450 if (k.k &&
1451 bkey_le(bpos_max(iter.pos, bkey_start_pos(k.k)),
1452 pagecache_start)) {
1453 bch2_bkey_buf_reassemble(&cur->kbuf, trans->c, k);
1454 bch2_cut_front(iter.pos, cur->kbuf.k);
1455 bch2_cut_back(POS(inode->ei_inum.inum, end), cur->kbuf.k);
1456 cur->flags = 0;
1457 } else if (k.k) {
1458 bch2_cut_back(bkey_start_pos(k.k), cur->kbuf.k);
1459 }
1460
1461 if (cur->kbuf.k->k.type == KEY_TYPE_reflink_p) {
1462 unsigned sectors = cur->kbuf.k->k.size;
1463 s64 offset_into_extent = 0;
1464 enum btree_id data_btree = BTREE_ID_extents;
1465 ret = bch2_read_indirect_extent(trans, &data_btree, &offset_into_extent,
1466 &cur->kbuf);
1467 if (ret)
1468 goto err;
1469
1470 struct bkey_i *k = cur->kbuf.k;
1471 sectors = min_t(unsigned, sectors, k->k.size - offset_into_extent);
1472
1473 bch2_cut_front(POS(k->k.p.inode,
1474 bkey_start_offset(&k->k) + offset_into_extent),
1475 k);
1476 bch2_key_resize(&k->k, sectors);
1477 k->k.p = iter.pos;
1478 k->k.p.offset += k->k.size;
1479 }
1480 err:
1481 bch2_trans_iter_exit(trans, &iter);
1482 return ret;
1483 }
1484
bch2_fiemap(struct inode * vinode,struct fiemap_extent_info * info,u64 start,u64 len)1485 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
1486 u64 start, u64 len)
1487 {
1488 struct bch_fs *c = vinode->i_sb->s_fs_info;
1489 struct bch_inode_info *ei = to_bch_ei(vinode);
1490 struct btree_trans *trans;
1491 struct bch_fiemap_extent cur, prev;
1492 int ret = 0;
1493
1494 ret = fiemap_prep(&ei->v, info, start, &len, 0);
1495 if (ret)
1496 return ret;
1497
1498 if (start + len < start)
1499 return -EINVAL;
1500
1501 start >>= 9;
1502 u64 end = (start + len) >> 9;
1503
1504 bch2_bkey_buf_init(&cur.kbuf);
1505 bch2_bkey_buf_init(&prev.kbuf);
1506 bkey_init(&prev.kbuf.k->k);
1507
1508 trans = bch2_trans_get(c);
1509
1510 while (start < end) {
1511 ret = lockrestart_do(trans,
1512 bch2_next_fiemap_extent(trans, ei, start, end, &cur));
1513 if (ret)
1514 goto err;
1515
1516 BUG_ON(bkey_start_offset(&cur.kbuf.k->k) < start);
1517 BUG_ON(cur.kbuf.k->k.p.offset > end);
1518
1519 if (bkey_start_offset(&cur.kbuf.k->k) == end)
1520 break;
1521
1522 start = cur.kbuf.k->k.p.offset;
1523
1524 if (!bkey_deleted(&prev.kbuf.k->k)) {
1525 bch2_trans_unlock(trans);
1526 ret = bch2_fill_extent(c, info, &prev);
1527 if (ret)
1528 goto err;
1529 }
1530
1531 bch2_bkey_buf_copy(&prev.kbuf, c, cur.kbuf.k);
1532 prev.flags = cur.flags;
1533 }
1534
1535 if (!bkey_deleted(&prev.kbuf.k->k)) {
1536 bch2_trans_unlock(trans);
1537 prev.flags |= FIEMAP_EXTENT_LAST;
1538 ret = bch2_fill_extent(c, info, &prev);
1539 }
1540 err:
1541 bch2_trans_put(trans);
1542 bch2_bkey_buf_exit(&cur.kbuf, c);
1543 bch2_bkey_buf_exit(&prev.kbuf, c);
1544
1545 return bch2_err_class(ret < 0 ? ret : 0);
1546 }
1547
1548 static const struct vm_operations_struct bch_vm_ops = {
1549 .fault = bch2_page_fault,
1550 .map_pages = filemap_map_pages,
1551 .page_mkwrite = bch2_page_mkwrite,
1552 };
1553
bch2_mmap(struct file * file,struct vm_area_struct * vma)1554 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1555 {
1556 file_accessed(file);
1557
1558 vma->vm_ops = &bch_vm_ops;
1559 return 0;
1560 }
1561
1562 /* Directories: */
1563
bch2_dir_llseek(struct file * file,loff_t offset,int whence)1564 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1565 {
1566 return generic_file_llseek_size(file, offset, whence,
1567 S64_MAX, S64_MAX);
1568 }
1569
bch2_vfs_readdir(struct file * file,struct dir_context * ctx)1570 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1571 {
1572 struct bch_inode_info *inode = file_bch_inode(file);
1573 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1574 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
1575
1576 if (!dir_emit_dots(file, ctx))
1577 return 0;
1578
1579 int ret = bch2_readdir(c, inode_inum(inode), &hash, ctx);
1580
1581 bch_err_fn(c, ret);
1582 return bch2_err_class(ret);
1583 }
1584
bch2_open(struct inode * vinode,struct file * file)1585 static int bch2_open(struct inode *vinode, struct file *file)
1586 {
1587 if (file->f_flags & (O_WRONLY|O_RDWR)) {
1588 struct bch_inode_info *inode = to_bch_ei(vinode);
1589 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1590
1591 int ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol);
1592 if (ret)
1593 return ret;
1594 }
1595
1596 file->f_mode |= FMODE_CAN_ODIRECT;
1597
1598 return generic_file_open(vinode, file);
1599 }
1600
1601 /* bcachefs inode flags -> FS_IOC_GETFLAGS: */
1602 static const __maybe_unused unsigned bch_flags_to_uflags[] = {
1603 [__BCH_INODE_sync] = FS_SYNC_FL,
1604 [__BCH_INODE_immutable] = FS_IMMUTABLE_FL,
1605 [__BCH_INODE_append] = FS_APPEND_FL,
1606 [__BCH_INODE_nodump] = FS_NODUMP_FL,
1607 [__BCH_INODE_noatime] = FS_NOATIME_FL,
1608 };
1609
1610 /* bcachefs inode flags -> FS_IOC_FSGETXATTR: */
1611 static const __maybe_unused unsigned bch_flags_to_xflags[] = {
1612 [__BCH_INODE_sync] = FS_XFLAG_SYNC,
1613 [__BCH_INODE_immutable] = FS_XFLAG_IMMUTABLE,
1614 [__BCH_INODE_append] = FS_XFLAG_APPEND,
1615 [__BCH_INODE_nodump] = FS_XFLAG_NODUMP,
1616 [__BCH_INODE_noatime] = FS_XFLAG_NOATIME,
1617 };
1618
bch2_fileattr_get(struct dentry * dentry,struct fileattr * fa)1619 static int bch2_fileattr_get(struct dentry *dentry,
1620 struct fileattr *fa)
1621 {
1622 struct bch_inode_info *inode = to_bch_ei(d_inode(dentry));
1623 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1624
1625 fileattr_fill_xflags(fa, map_flags(bch_flags_to_xflags, inode->ei_inode.bi_flags));
1626
1627 if (inode->ei_inode.bi_fields_set & (1 << Inode_opt_project))
1628 fa->fsx_xflags |= FS_XFLAG_PROJINHERIT;
1629
1630 if (bch2_inode_casefold(c, &inode->ei_inode))
1631 fa->flags |= FS_CASEFOLD_FL;
1632
1633 fa->fsx_projid = inode->ei_qid.q[QTYP_PRJ];
1634 return 0;
1635 }
1636
1637 struct flags_set {
1638 unsigned mask;
1639 unsigned flags;
1640 unsigned projid;
1641 bool set_project;
1642 bool set_casefold;
1643 bool casefold;
1644 };
1645
fssetxattr_inode_update_fn(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,void * p)1646 static int fssetxattr_inode_update_fn(struct btree_trans *trans,
1647 struct bch_inode_info *inode,
1648 struct bch_inode_unpacked *bi,
1649 void *p)
1650 {
1651 struct bch_fs *c = trans->c;
1652 struct flags_set *s = p;
1653
1654 /*
1655 * We're relying on btree locking here for exclusion with other ioctl
1656 * calls - use the flags in the btree (@bi), not inode->i_flags:
1657 */
1658 if (!S_ISREG(bi->bi_mode) &&
1659 !S_ISDIR(bi->bi_mode) &&
1660 (s->flags & (BCH_INODE_nodump|BCH_INODE_noatime)) != s->flags)
1661 return -EINVAL;
1662
1663 if (s->casefold != bch2_inode_casefold(c, bi)) {
1664 int ret = bch2_inode_set_casefold(trans, inode_inum(inode), bi, s->casefold);
1665 if (ret)
1666 return ret;
1667 }
1668
1669 if (s->set_project) {
1670 bi->bi_project = s->projid;
1671 bi->bi_fields_set |= BIT(Inode_opt_project);
1672 }
1673
1674 bi->bi_flags &= ~s->mask;
1675 bi->bi_flags |= s->flags;
1676
1677 bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v));
1678 return 0;
1679 }
1680
bch2_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct fileattr * fa)1681 static int bch2_fileattr_set(struct mnt_idmap *idmap,
1682 struct dentry *dentry,
1683 struct fileattr *fa)
1684 {
1685 struct bch_inode_info *inode = to_bch_ei(d_inode(dentry));
1686 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1687 struct flags_set s = {};
1688 int ret;
1689
1690 if (fa->fsx_valid) {
1691 fa->fsx_xflags &= ~FS_XFLAG_PROJINHERIT;
1692
1693 s.mask = map_defined(bch_flags_to_xflags);
1694 s.flags |= map_flags_rev(bch_flags_to_xflags, fa->fsx_xflags);
1695 if (fa->fsx_xflags)
1696 return -EOPNOTSUPP;
1697
1698 if (fa->fsx_projid >= U32_MAX)
1699 return -EINVAL;
1700
1701 /*
1702 * inode fields accessible via the xattr interface are stored with a +1
1703 * bias, so that 0 means unset:
1704 */
1705 if ((inode->ei_inode.bi_project ||
1706 fa->fsx_projid) &&
1707 inode->ei_inode.bi_project != fa->fsx_projid + 1) {
1708 s.projid = fa->fsx_projid + 1;
1709 s.set_project = true;
1710 }
1711 }
1712
1713 if (fa->flags_valid) {
1714 s.mask = map_defined(bch_flags_to_uflags);
1715
1716 s.set_casefold = true;
1717 s.casefold = (fa->flags & FS_CASEFOLD_FL) != 0;
1718 fa->flags &= ~FS_CASEFOLD_FL;
1719
1720 s.flags |= map_flags_rev(bch_flags_to_uflags, fa->flags);
1721 if (fa->flags)
1722 return -EOPNOTSUPP;
1723 }
1724
1725 mutex_lock(&inode->ei_update_lock);
1726 ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
1727 (s.set_project
1728 ? bch2_set_projid(c, inode, fa->fsx_projid)
1729 : 0) ?:
1730 bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
1731 ATTR_CTIME);
1732 mutex_unlock(&inode->ei_update_lock);
1733
1734 return bch2_err_class(ret);
1735 }
1736
1737 static const struct file_operations bch_file_operations = {
1738 .open = bch2_open,
1739 .llseek = bch2_llseek,
1740 .read_iter = bch2_read_iter,
1741 .write_iter = bch2_write_iter,
1742 .mmap = bch2_mmap,
1743 .get_unmapped_area = thp_get_unmapped_area,
1744 .fsync = bch2_fsync,
1745 .splice_read = filemap_splice_read,
1746 .splice_write = iter_file_splice_write,
1747 .fallocate = bch2_fallocate_dispatch,
1748 .unlocked_ioctl = bch2_fs_file_ioctl,
1749 #ifdef CONFIG_COMPAT
1750 .compat_ioctl = bch2_compat_fs_ioctl,
1751 #endif
1752 .remap_file_range = bch2_remap_file_range,
1753 };
1754
1755 static const struct inode_operations bch_file_inode_operations = {
1756 .getattr = bch2_getattr,
1757 .setattr = bch2_setattr,
1758 .fiemap = bch2_fiemap,
1759 .listxattr = bch2_xattr_list,
1760 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1761 .get_inode_acl = bch2_get_acl,
1762 .set_acl = bch2_set_acl,
1763 #endif
1764 .fileattr_get = bch2_fileattr_get,
1765 .fileattr_set = bch2_fileattr_set,
1766 };
1767
1768 static const struct inode_operations bch_dir_inode_operations = {
1769 .lookup = bch2_lookup,
1770 .create = bch2_create,
1771 .link = bch2_link,
1772 .unlink = bch2_unlink,
1773 .symlink = bch2_symlink,
1774 .mkdir = bch2_mkdir,
1775 .rmdir = bch2_unlink,
1776 .mknod = bch2_mknod,
1777 .rename = bch2_rename2,
1778 .getattr = bch2_getattr,
1779 .setattr = bch2_setattr,
1780 .tmpfile = bch2_tmpfile,
1781 .listxattr = bch2_xattr_list,
1782 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1783 .get_inode_acl = bch2_get_acl,
1784 .set_acl = bch2_set_acl,
1785 #endif
1786 .fileattr_get = bch2_fileattr_get,
1787 .fileattr_set = bch2_fileattr_set,
1788 };
1789
1790 static const struct file_operations bch_dir_file_operations = {
1791 .llseek = bch2_dir_llseek,
1792 .read = generic_read_dir,
1793 .iterate_shared = bch2_vfs_readdir,
1794 .fsync = bch2_fsync,
1795 .unlocked_ioctl = bch2_fs_file_ioctl,
1796 #ifdef CONFIG_COMPAT
1797 .compat_ioctl = bch2_compat_fs_ioctl,
1798 #endif
1799 };
1800
1801 static const struct inode_operations bch_symlink_inode_operations = {
1802 .get_link = page_get_link,
1803 .getattr = bch2_getattr,
1804 .setattr = bch2_setattr,
1805 .listxattr = bch2_xattr_list,
1806 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1807 .get_inode_acl = bch2_get_acl,
1808 .set_acl = bch2_set_acl,
1809 #endif
1810 .fileattr_get = bch2_fileattr_get,
1811 .fileattr_set = bch2_fileattr_set,
1812 };
1813
1814 static const struct inode_operations bch_special_inode_operations = {
1815 .getattr = bch2_getattr,
1816 .setattr = bch2_setattr,
1817 .listxattr = bch2_xattr_list,
1818 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1819 .get_inode_acl = bch2_get_acl,
1820 .set_acl = bch2_set_acl,
1821 #endif
1822 .fileattr_get = bch2_fileattr_get,
1823 .fileattr_set = bch2_fileattr_set,
1824 };
1825
1826 static const struct address_space_operations bch_address_space_operations = {
1827 .read_folio = bch2_read_folio,
1828 .writepages = bch2_writepages,
1829 .readahead = bch2_readahead,
1830 .dirty_folio = filemap_dirty_folio,
1831 .write_begin = bch2_write_begin,
1832 .write_end = bch2_write_end,
1833 .invalidate_folio = bch2_invalidate_folio,
1834 .release_folio = bch2_release_folio,
1835 #ifdef CONFIG_MIGRATION
1836 .migrate_folio = filemap_migrate_folio,
1837 #endif
1838 .error_remove_folio = generic_error_remove_folio,
1839 };
1840
1841 struct bcachefs_fid {
1842 u64 inum;
1843 u32 subvol;
1844 u32 gen;
1845 } __packed;
1846
1847 struct bcachefs_fid_with_parent {
1848 struct bcachefs_fid fid;
1849 struct bcachefs_fid dir;
1850 } __packed;
1851
bcachefs_fid_valid(int fh_len,int fh_type)1852 static int bcachefs_fid_valid(int fh_len, int fh_type)
1853 {
1854 switch (fh_type) {
1855 case FILEID_BCACHEFS_WITHOUT_PARENT:
1856 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1857 case FILEID_BCACHEFS_WITH_PARENT:
1858 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1859 default:
1860 return false;
1861 }
1862 }
1863
bch2_inode_to_fid(struct bch_inode_info * inode)1864 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1865 {
1866 return (struct bcachefs_fid) {
1867 .inum = inode->ei_inum.inum,
1868 .subvol = inode->ei_inum.subvol,
1869 .gen = inode->ei_inode.bi_generation,
1870 };
1871 }
1872
bch2_encode_fh(struct inode * vinode,u32 * fh,int * len,struct inode * vdir)1873 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1874 struct inode *vdir)
1875 {
1876 struct bch_inode_info *inode = to_bch_ei(vinode);
1877 struct bch_inode_info *dir = to_bch_ei(vdir);
1878 int min_len;
1879
1880 if (!S_ISDIR(inode->v.i_mode) && dir) {
1881 struct bcachefs_fid_with_parent *fid = (void *) fh;
1882
1883 min_len = sizeof(*fid) / sizeof(u32);
1884 if (*len < min_len) {
1885 *len = min_len;
1886 return FILEID_INVALID;
1887 }
1888
1889 fid->fid = bch2_inode_to_fid(inode);
1890 fid->dir = bch2_inode_to_fid(dir);
1891
1892 *len = min_len;
1893 return FILEID_BCACHEFS_WITH_PARENT;
1894 } else {
1895 struct bcachefs_fid *fid = (void *) fh;
1896
1897 min_len = sizeof(*fid) / sizeof(u32);
1898 if (*len < min_len) {
1899 *len = min_len;
1900 return FILEID_INVALID;
1901 }
1902 *fid = bch2_inode_to_fid(inode);
1903
1904 *len = min_len;
1905 return FILEID_BCACHEFS_WITHOUT_PARENT;
1906 }
1907 }
1908
bch2_nfs_get_inode(struct super_block * sb,struct bcachefs_fid fid)1909 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1910 struct bcachefs_fid fid)
1911 {
1912 struct bch_fs *c = sb->s_fs_info;
1913 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1914 .subvol = fid.subvol,
1915 .inum = fid.inum,
1916 });
1917 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1918 iput(vinode);
1919 vinode = ERR_PTR(-ESTALE);
1920 }
1921 return vinode;
1922 }
1923
bch2_fh_to_dentry(struct super_block * sb,struct fid * _fid,int fh_len,int fh_type)1924 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1925 int fh_len, int fh_type)
1926 {
1927 struct bcachefs_fid *fid = (void *) _fid;
1928
1929 if (!bcachefs_fid_valid(fh_len, fh_type))
1930 return NULL;
1931
1932 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1933 }
1934
bch2_fh_to_parent(struct super_block * sb,struct fid * _fid,int fh_len,int fh_type)1935 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1936 int fh_len, int fh_type)
1937 {
1938 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1939
1940 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1941 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1942 return NULL;
1943
1944 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1945 }
1946
bch2_get_parent(struct dentry * child)1947 static struct dentry *bch2_get_parent(struct dentry *child)
1948 {
1949 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1950 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1951 subvol_inum parent_inum = {
1952 .subvol = inode->ei_inode.bi_parent_subvol ?:
1953 inode->ei_inum.subvol,
1954 .inum = inode->ei_inode.bi_dir,
1955 };
1956
1957 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1958 }
1959
bch2_get_name(struct dentry * parent,char * name,struct dentry * child)1960 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1961 {
1962 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1963 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1964 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1965 struct btree_trans *trans;
1966 struct btree_iter iter1;
1967 struct btree_iter iter2;
1968 struct bkey_s_c k;
1969 struct bkey_s_c_dirent d;
1970 struct bch_inode_unpacked inode_u;
1971 subvol_inum target;
1972 u32 snapshot;
1973 struct qstr dirent_name;
1974 unsigned name_len = 0;
1975 int ret;
1976
1977 if (!S_ISDIR(dir->v.i_mode))
1978 return -EINVAL;
1979
1980 trans = bch2_trans_get(c);
1981
1982 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
1983 POS(dir->ei_inode.bi_inum, 0), 0);
1984 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
1985 POS(dir->ei_inode.bi_inum, 0), 0);
1986 retry:
1987 bch2_trans_begin(trans);
1988
1989 ret = bch2_subvolume_get_snapshot(trans, dir->ei_inum.subvol, &snapshot);
1990 if (ret)
1991 goto err;
1992
1993 bch2_btree_iter_set_snapshot(trans, &iter1, snapshot);
1994 bch2_btree_iter_set_snapshot(trans, &iter2, snapshot);
1995
1996 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
1997 if (ret)
1998 goto err;
1999
2000 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
2001 bch2_btree_iter_set_pos(trans, &iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
2002
2003 k = bch2_btree_iter_peek_slot(trans, &iter1);
2004 ret = bkey_err(k);
2005 if (ret)
2006 goto err;
2007
2008 if (k.k->type != KEY_TYPE_dirent) {
2009 ret = bch_err_throw(c, ENOENT_dirent_doesnt_match_inode);
2010 goto err;
2011 }
2012
2013 d = bkey_s_c_to_dirent(k);
2014 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
2015 if (ret > 0)
2016 ret = bch_err_throw(c, ENOENT_dirent_doesnt_match_inode);
2017 if (ret)
2018 goto err;
2019
2020 if (subvol_inum_eq(target, inode->ei_inum))
2021 goto found;
2022 } else {
2023 /*
2024 * File with multiple hardlinks and our backref is to the wrong
2025 * directory - linear search:
2026 */
2027 for_each_btree_key_continue_norestart(trans, iter2, 0, k, ret) {
2028 if (k.k->p.inode > dir->ei_inode.bi_inum)
2029 break;
2030
2031 if (k.k->type != KEY_TYPE_dirent)
2032 continue;
2033
2034 d = bkey_s_c_to_dirent(k);
2035 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
2036 if (ret < 0)
2037 break;
2038 if (ret)
2039 continue;
2040
2041 if (subvol_inum_eq(target, inode->ei_inum))
2042 goto found;
2043 }
2044 }
2045
2046 ret = -ENOENT;
2047 goto err;
2048 found:
2049 dirent_name = bch2_dirent_get_name(d);
2050
2051 name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
2052 memcpy(name, dirent_name.name, name_len);
2053 name[name_len] = '\0';
2054 err:
2055 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
2056 goto retry;
2057
2058 bch2_trans_iter_exit(trans, &iter1);
2059 bch2_trans_iter_exit(trans, &iter2);
2060 bch2_trans_put(trans);
2061
2062 return ret;
2063 }
2064
2065 static const struct export_operations bch_export_ops = {
2066 .encode_fh = bch2_encode_fh,
2067 .fh_to_dentry = bch2_fh_to_dentry,
2068 .fh_to_parent = bch2_fh_to_parent,
2069 .get_parent = bch2_get_parent,
2070 .get_name = bch2_get_name,
2071 };
2072
bch2_vfs_inode_init(struct btree_trans * trans,subvol_inum inum,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,struct bch_subvolume * subvol)2073 static void bch2_vfs_inode_init(struct btree_trans *trans,
2074 subvol_inum inum,
2075 struct bch_inode_info *inode,
2076 struct bch_inode_unpacked *bi,
2077 struct bch_subvolume *subvol)
2078 {
2079 inode->v.i_ino = inum.inum;
2080 inode->ei_inum = inum;
2081 inode->ei_inode.bi_inum = inum.inum;
2082 bch2_inode_update_after_write(trans, inode, bi, ~0);
2083
2084 inode->v.i_blocks = bi->bi_sectors;
2085 inode->v.i_rdev = bi->bi_dev;
2086 inode->v.i_generation = bi->bi_generation;
2087 inode->v.i_size = bi->bi_size;
2088
2089 inode->ei_flags = 0;
2090 inode->ei_quota_reserved = 0;
2091 inode->ei_qid = bch_qid(bi);
2092
2093 if (BCH_SUBVOLUME_SNAP(subvol))
2094 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
2095
2096 inode->v.i_mapping->a_ops = &bch_address_space_operations;
2097
2098 switch (inode->v.i_mode & S_IFMT) {
2099 case S_IFREG:
2100 inode->v.i_op = &bch_file_inode_operations;
2101 inode->v.i_fop = &bch_file_operations;
2102 break;
2103 case S_IFDIR:
2104 inode->v.i_op = &bch_dir_inode_operations;
2105 inode->v.i_fop = &bch_dir_file_operations;
2106 break;
2107 case S_IFLNK:
2108 inode_nohighmem(&inode->v);
2109 inode->v.i_op = &bch_symlink_inode_operations;
2110 break;
2111 default:
2112 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
2113 inode->v.i_op = &bch_special_inode_operations;
2114 break;
2115 }
2116
2117 mapping_set_folio_min_order(inode->v.i_mapping,
2118 get_order(trans->c->opts.block_size));
2119 }
2120
bch2_free_inode(struct inode * vinode)2121 static void bch2_free_inode(struct inode *vinode)
2122 {
2123 kmem_cache_free(bch2_inode_cache, to_bch_ei(vinode));
2124 }
2125
inode_update_times_fn(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,void * p)2126 static int inode_update_times_fn(struct btree_trans *trans,
2127 struct bch_inode_info *inode,
2128 struct bch_inode_unpacked *bi,
2129 void *p)
2130 {
2131 struct bch_fs *c = inode->v.i_sb->s_fs_info;
2132
2133 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
2134 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
2135 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
2136
2137 return 0;
2138 }
2139
bch2_vfs_write_inode(struct inode * vinode,struct writeback_control * wbc)2140 static int bch2_vfs_write_inode(struct inode *vinode,
2141 struct writeback_control *wbc)
2142 {
2143 struct bch_fs *c = vinode->i_sb->s_fs_info;
2144 struct bch_inode_info *inode = to_bch_ei(vinode);
2145 int ret;
2146
2147 mutex_lock(&inode->ei_update_lock);
2148 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
2149 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
2150 mutex_unlock(&inode->ei_update_lock);
2151
2152 return bch2_err_class(ret);
2153 }
2154
bch2_evict_inode(struct inode * vinode)2155 static void bch2_evict_inode(struct inode *vinode)
2156 {
2157 struct bch_fs *c = vinode->i_sb->s_fs_info;
2158 struct bch_inode_info *inode = to_bch_ei(vinode);
2159 bool delete = !inode->v.i_nlink && !is_bad_inode(&inode->v);
2160
2161 /*
2162 * evict() has waited for outstanding writeback, we'll do no more IO
2163 * through this inode: it's safe to remove from VFS inode hashtable here
2164 *
2165 * Do that now so that other threads aren't blocked from pulling it back
2166 * in, there's no reason for them to be:
2167 */
2168 if (!delete)
2169 bch2_inode_hash_remove(c, inode);
2170
2171 truncate_inode_pages_final(&inode->v.i_data);
2172
2173 clear_inode(&inode->v);
2174
2175 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
2176
2177 if (delete) {
2178 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
2179 KEY_TYPE_QUOTA_WARN);
2180 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
2181 KEY_TYPE_QUOTA_WARN);
2182 int ret = bch2_inode_rm(c, inode_inum(inode));
2183 if (ret && !bch2_err_matches(ret, EROFS)) {
2184 bch_err_msg(c, ret, "VFS incorrectly tried to delete inode %llu:%llu",
2185 inode->ei_inum.subvol,
2186 inode->ei_inum.inum);
2187 bch2_sb_error_count(c, BCH_FSCK_ERR_vfs_bad_inode_rm);
2188 }
2189
2190 /*
2191 * If we are deleting, we need it present in the vfs hash table
2192 * so that fsck can check if unlinked inodes are still open:
2193 */
2194 bch2_inode_hash_remove(c, inode);
2195 }
2196
2197 mutex_lock(&c->vfs_inodes_lock);
2198 list_del_init(&inode->ei_vfs_inode_list);
2199 mutex_unlock(&c->vfs_inodes_lock);
2200 }
2201
bch2_evict_subvolume_inodes(struct bch_fs * c,snapshot_id_list * s)2202 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
2203 {
2204 struct bch_inode_info *inode;
2205 DARRAY(struct bch_inode_info *) grabbed;
2206 bool clean_pass = false, this_pass_clean;
2207
2208 /*
2209 * Initially, we scan for inodes without I_DONTCACHE, then mark them to
2210 * be pruned with d_mark_dontcache().
2211 *
2212 * Once we've had a clean pass where we didn't find any inodes without
2213 * I_DONTCACHE, we wait for them to be freed:
2214 */
2215
2216 darray_init(&grabbed);
2217 darray_make_room(&grabbed, 1024);
2218 again:
2219 cond_resched();
2220 this_pass_clean = true;
2221
2222 mutex_lock(&c->vfs_inodes_lock);
2223 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
2224 if (!snapshot_list_has_id(s, inode->ei_inum.subvol))
2225 continue;
2226
2227 if (!(inode->v.i_state & I_DONTCACHE) &&
2228 !(inode->v.i_state & I_FREEING) &&
2229 igrab(&inode->v)) {
2230 this_pass_clean = false;
2231
2232 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
2233 iput(&inode->v);
2234 break;
2235 }
2236 } else if (clean_pass && this_pass_clean) {
2237 struct wait_bit_queue_entry wqe;
2238 struct wait_queue_head *wq_head;
2239
2240 wq_head = inode_bit_waitqueue(&wqe, &inode->v, __I_NEW);
2241 prepare_to_wait_event(wq_head, &wqe.wq_entry,
2242 TASK_UNINTERRUPTIBLE);
2243 mutex_unlock(&c->vfs_inodes_lock);
2244
2245 schedule();
2246 finish_wait(wq_head, &wqe.wq_entry);
2247 goto again;
2248 }
2249 }
2250 mutex_unlock(&c->vfs_inodes_lock);
2251
2252 darray_for_each(grabbed, i) {
2253 inode = *i;
2254 d_mark_dontcache(&inode->v);
2255 d_prune_aliases(&inode->v);
2256 iput(&inode->v);
2257 }
2258 grabbed.nr = 0;
2259
2260 if (!clean_pass || !this_pass_clean) {
2261 clean_pass = this_pass_clean;
2262 goto again;
2263 }
2264
2265 darray_exit(&grabbed);
2266 }
2267
bch2_statfs(struct dentry * dentry,struct kstatfs * buf)2268 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
2269 {
2270 struct super_block *sb = dentry->d_sb;
2271 struct bch_fs *c = sb->s_fs_info;
2272 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
2273 unsigned shift = sb->s_blocksize_bits - 9;
2274 /*
2275 * this assumes inodes take up 64 bytes, which is a decent average
2276 * number:
2277 */
2278 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
2279
2280 buf->f_type = BCACHEFS_STATFS_MAGIC;
2281 buf->f_bsize = sb->s_blocksize;
2282 buf->f_blocks = usage.capacity >> shift;
2283 buf->f_bfree = usage.free >> shift;
2284 buf->f_bavail = avail_factor(usage.free) >> shift;
2285
2286 buf->f_files = usage.nr_inodes + avail_inodes;
2287 buf->f_ffree = avail_inodes;
2288
2289 buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b);
2290 buf->f_namelen = BCH_NAME_MAX;
2291
2292 return 0;
2293 }
2294
bch2_sync_fs(struct super_block * sb,int wait)2295 static int bch2_sync_fs(struct super_block *sb, int wait)
2296 {
2297 struct bch_fs *c = sb->s_fs_info;
2298 int ret;
2299
2300 trace_bch2_sync_fs(sb, wait);
2301
2302 if (c->opts.journal_flush_disabled)
2303 return 0;
2304
2305 if (!wait) {
2306 bch2_journal_flush_async(&c->journal, NULL);
2307 return 0;
2308 }
2309
2310 ret = bch2_journal_flush(&c->journal);
2311 return bch2_err_class(ret);
2312 }
2313
bch2_path_to_fs(const char * path)2314 static struct bch_fs *bch2_path_to_fs(const char *path)
2315 {
2316 struct bch_fs *c;
2317 dev_t dev;
2318 int ret;
2319
2320 ret = lookup_bdev(path, &dev);
2321 if (ret)
2322 return ERR_PTR(ret);
2323
2324 c = bch2_dev_to_fs(dev);
2325 if (c)
2326 closure_put(&c->cl);
2327 return c ?: ERR_PTR(-ENOENT);
2328 }
2329
bch2_show_devname(struct seq_file * seq,struct dentry * root)2330 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
2331 {
2332 struct bch_fs *c = root->d_sb->s_fs_info;
2333 bool first = true;
2334
2335 guard(rcu)();
2336 for_each_online_member_rcu(c, ca) {
2337 if (!first)
2338 seq_putc(seq, ':');
2339 first = false;
2340 seq_puts(seq, ca->disk_sb.sb_name);
2341 }
2342
2343 return 0;
2344 }
2345
bch2_show_options(struct seq_file * seq,struct dentry * root)2346 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
2347 {
2348 struct bch_fs *c = root->d_sb->s_fs_info;
2349 struct printbuf buf = PRINTBUF;
2350
2351 bch2_opts_to_text(&buf, c->opts, c, c->disk_sb.sb,
2352 OPT_MOUNT, OPT_HIDDEN, OPT_SHOW_MOUNT_STYLE);
2353 printbuf_nul_terminate(&buf);
2354 seq_printf(seq, ",%s", buf.buf);
2355
2356 int ret = buf.allocation_failure ? -ENOMEM : 0;
2357 printbuf_exit(&buf);
2358 return ret;
2359 }
2360
bch2_put_super(struct super_block * sb)2361 static void bch2_put_super(struct super_block *sb)
2362 {
2363 struct bch_fs *c = sb->s_fs_info;
2364
2365 __bch2_fs_stop(c);
2366 }
2367
2368 /*
2369 * bcachefs doesn't currently integrate intwrite freeze protection but the
2370 * internal write references serve the same purpose. Therefore reuse the
2371 * read-only transition code to perform the quiesce. The caveat is that we don't
2372 * currently have the ability to block tasks that want a write reference while
2373 * the superblock is frozen. This is fine for now, but we should either add
2374 * blocking support or find a way to integrate sb_start_intwrite() and friends.
2375 */
bch2_freeze(struct super_block * sb)2376 static int bch2_freeze(struct super_block *sb)
2377 {
2378 struct bch_fs *c = sb->s_fs_info;
2379
2380 down_write(&c->state_lock);
2381 bch2_fs_read_only(c);
2382 up_write(&c->state_lock);
2383 return 0;
2384 }
2385
bch2_unfreeze(struct super_block * sb)2386 static int bch2_unfreeze(struct super_block *sb)
2387 {
2388 struct bch_fs *c = sb->s_fs_info;
2389 int ret;
2390
2391 if (test_bit(BCH_FS_emergency_ro, &c->flags))
2392 return 0;
2393
2394 down_write(&c->state_lock);
2395 ret = bch2_fs_read_write(c);
2396 up_write(&c->state_lock);
2397 return ret;
2398 }
2399
2400 static const struct super_operations bch_super_operations = {
2401 .alloc_inode = bch2_alloc_inode,
2402 .free_inode = bch2_free_inode,
2403 .write_inode = bch2_vfs_write_inode,
2404 .evict_inode = bch2_evict_inode,
2405 .sync_fs = bch2_sync_fs,
2406 .statfs = bch2_statfs,
2407 .show_devname = bch2_show_devname,
2408 .show_options = bch2_show_options,
2409 .put_super = bch2_put_super,
2410 .freeze_fs = bch2_freeze,
2411 .unfreeze_fs = bch2_unfreeze,
2412 };
2413
bch2_set_super(struct super_block * s,void * data)2414 static int bch2_set_super(struct super_block *s, void *data)
2415 {
2416 s->s_fs_info = data;
2417 return 0;
2418 }
2419
bch2_noset_super(struct super_block * s,void * data)2420 static int bch2_noset_super(struct super_block *s, void *data)
2421 {
2422 return -EBUSY;
2423 }
2424
2425 typedef DARRAY(struct bch_fs *) darray_fs;
2426
bch2_test_super(struct super_block * s,void * data)2427 static int bch2_test_super(struct super_block *s, void *data)
2428 {
2429 struct bch_fs *c = s->s_fs_info;
2430 darray_fs *d = data;
2431
2432 if (!c)
2433 return false;
2434
2435 darray_for_each(*d, i)
2436 if (c != *i)
2437 return false;
2438 return true;
2439 }
2440
bch2_fs_get_tree(struct fs_context * fc)2441 static int bch2_fs_get_tree(struct fs_context *fc)
2442 {
2443 struct bch_fs *c;
2444 struct super_block *sb;
2445 struct inode *vinode;
2446 struct bch2_opts_parse *opts_parse = fc->fs_private;
2447 struct bch_opts opts = opts_parse->opts;
2448 darray_const_str devs;
2449 darray_fs devs_to_fs = {};
2450 int ret;
2451
2452 opt_set(opts, read_only, (fc->sb_flags & SB_RDONLY) != 0);
2453 opt_set(opts, nostart, true);
2454
2455 if (!fc->source || strlen(fc->source) == 0)
2456 return -EINVAL;
2457
2458 ret = bch2_split_devs(fc->source, &devs);
2459 if (ret)
2460 return ret;
2461
2462 darray_for_each(devs, i) {
2463 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
2464 if (ret)
2465 goto err;
2466 }
2467
2468 sb = sget(fc->fs_type, bch2_test_super, bch2_noset_super, fc->sb_flags|SB_NOSEC, &devs_to_fs);
2469 if (!IS_ERR(sb))
2470 goto got_sb;
2471
2472 c = bch2_fs_open(&devs, &opts);
2473 ret = PTR_ERR_OR_ZERO(c);
2474 if (ret)
2475 goto err;
2476
2477 if (opt_defined(opts, discard))
2478 set_bit(BCH_FS_discard_mount_opt_set, &c->flags);
2479
2480 /* Some options can't be parsed until after the fs is started: */
2481 opts = bch2_opts_empty();
2482 ret = bch2_parse_mount_opts(c, &opts, NULL, opts_parse->parse_later.buf, false);
2483 if (ret)
2484 goto err_stop_fs;
2485
2486 bch2_opts_apply(&c->opts, opts);
2487
2488 ret = bch2_fs_start(c);
2489 if (ret)
2490 goto err_stop_fs;
2491
2492 /*
2493 * We might be doing a RO mount because other options required it, or we
2494 * have no alloc info and it's a small image with no room to regenerate
2495 * it
2496 */
2497 if (c->opts.read_only)
2498 fc->sb_flags |= SB_RDONLY;
2499
2500 sb = sget(fc->fs_type, NULL, bch2_set_super, fc->sb_flags|SB_NOSEC, c);
2501 ret = PTR_ERR_OR_ZERO(sb);
2502 if (ret)
2503 goto err_stop_fs;
2504 got_sb:
2505 c = sb->s_fs_info;
2506
2507 if (sb->s_root) {
2508 if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY) {
2509 ret = -EBUSY;
2510 goto err_put_super;
2511 }
2512 goto out;
2513 }
2514
2515 sb->s_blocksize = block_bytes(c);
2516 sb->s_blocksize_bits = ilog2(block_bytes(c));
2517 sb->s_maxbytes = MAX_LFS_FILESIZE;
2518 sb->s_op = &bch_super_operations;
2519 sb->s_export_op = &bch_export_ops;
2520 #ifdef CONFIG_BCACHEFS_QUOTA
2521 sb->s_qcop = &bch2_quotactl_operations;
2522 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
2523 #endif
2524 sb->s_xattr = bch2_xattr_handlers;
2525 sb->s_magic = BCACHEFS_STATFS_MAGIC;
2526 sb->s_time_gran = c->sb.nsec_per_time_unit;
2527 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
2528 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
2529 super_set_uuid(sb, c->sb.user_uuid.b, sizeof(c->sb.user_uuid));
2530
2531 if (c->sb.multi_device)
2532 super_set_sysfs_name_uuid(sb);
2533 else
2534 strscpy(sb->s_sysfs_name, c->name, sizeof(sb->s_sysfs_name));
2535
2536 sb->s_shrink->seeks = 0;
2537 c->vfs_sb = sb;
2538 strscpy(sb->s_id, c->name, sizeof(sb->s_id));
2539
2540 ret = super_setup_bdi(sb);
2541 if (ret)
2542 goto err_put_super;
2543
2544 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
2545
2546 scoped_guard(rcu) {
2547 for_each_online_member_rcu(c, ca) {
2548 struct block_device *bdev = ca->disk_sb.bdev;
2549
2550 /* XXX: create an anonymous device for multi device filesystems */
2551 sb->s_bdev = bdev;
2552 sb->s_dev = bdev->bd_dev;
2553 break;
2554 }
2555 }
2556
2557 c->dev = sb->s_dev;
2558
2559 #ifdef CONFIG_BCACHEFS_POSIX_ACL
2560 if (c->opts.acl)
2561 sb->s_flags |= SB_POSIXACL;
2562 #endif
2563
2564 sb->s_shrink->seeks = 0;
2565
2566 #ifdef CONFIG_UNICODE
2567 if (bch2_fs_casefold_enabled(c))
2568 sb->s_encoding = c->cf_encoding;
2569 generic_set_sb_d_ops(sb);
2570 #endif
2571
2572 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
2573 ret = PTR_ERR_OR_ZERO(vinode);
2574 bch_err_msg(c, ret, "mounting: error getting root inode");
2575 if (ret)
2576 goto err_put_super;
2577
2578 sb->s_root = d_make_root(vinode);
2579 if (!sb->s_root) {
2580 bch_err(c, "error mounting: error allocating root dentry");
2581 ret = -ENOMEM;
2582 goto err_put_super;
2583 }
2584
2585 sb->s_flags |= SB_ACTIVE;
2586 out:
2587 fc->root = dget(sb->s_root);
2588 err:
2589 darray_exit(&devs_to_fs);
2590 bch2_darray_str_exit(&devs);
2591 if (ret)
2592 pr_err("error: %s", bch2_err_str(ret));
2593 /*
2594 * On an inconsistency error in recovery we might see an -EROFS derived
2595 * errorcode (from the journal), but we don't want to return that to
2596 * userspace as that causes util-linux to retry the mount RO - which is
2597 * confusing:
2598 */
2599 if (bch2_err_matches(ret, EROFS) && ret != -EROFS)
2600 ret = -EIO;
2601 return bch2_err_class(ret);
2602
2603 err_stop_fs:
2604 bch2_fs_stop(c);
2605 goto err;
2606
2607 err_put_super:
2608 if (!sb->s_root)
2609 __bch2_fs_stop(c);
2610 deactivate_locked_super(sb);
2611 goto err;
2612 }
2613
bch2_kill_sb(struct super_block * sb)2614 static void bch2_kill_sb(struct super_block *sb)
2615 {
2616 struct bch_fs *c = sb->s_fs_info;
2617
2618 generic_shutdown_super(sb);
2619 bch2_fs_free(c);
2620 }
2621
bch2_fs_context_free(struct fs_context * fc)2622 static void bch2_fs_context_free(struct fs_context *fc)
2623 {
2624 struct bch2_opts_parse *opts = fc->fs_private;
2625
2626 if (opts) {
2627 printbuf_exit(&opts->parse_later);
2628 kfree(opts);
2629 }
2630 }
2631
bch2_fs_parse_param(struct fs_context * fc,struct fs_parameter * param)2632 static int bch2_fs_parse_param(struct fs_context *fc,
2633 struct fs_parameter *param)
2634 {
2635 /*
2636 * the "source" param, i.e., the name of the device(s) to mount,
2637 * is handled by the VFS layer.
2638 */
2639 if (!strcmp(param->key, "source"))
2640 return -ENOPARAM;
2641
2642 struct bch2_opts_parse *opts = fc->fs_private;
2643 struct bch_fs *c = NULL;
2644
2645 /* for reconfigure, we already have a struct bch_fs */
2646 if (fc->root)
2647 c = fc->root->d_sb->s_fs_info;
2648
2649 int ret = bch2_parse_one_mount_opt(c, &opts->opts,
2650 &opts->parse_later, param->key,
2651 param->string);
2652 if (ret)
2653 pr_err("Error parsing option %s: %s", param->key, bch2_err_str(ret));
2654
2655 return bch2_err_class(ret);
2656 }
2657
bch2_fs_reconfigure(struct fs_context * fc)2658 static int bch2_fs_reconfigure(struct fs_context *fc)
2659 {
2660 struct super_block *sb = fc->root->d_sb;
2661 struct bch2_opts_parse *opts = fc->fs_private;
2662 struct bch_fs *c = sb->s_fs_info;
2663 int ret = 0;
2664
2665 opt_set(opts->opts, read_only, (fc->sb_flags & SB_RDONLY) != 0);
2666
2667 if (opts->opts.read_only != c->opts.read_only) {
2668 down_write(&c->state_lock);
2669
2670 if (opts->opts.read_only) {
2671 bch2_fs_read_only(c);
2672
2673 sb->s_flags |= SB_RDONLY;
2674 } else {
2675 ret = bch2_fs_read_write(c);
2676 if (ret) {
2677 bch_err(c, "error going rw: %i", ret);
2678 up_write(&c->state_lock);
2679 ret = -EINVAL;
2680 goto err;
2681 }
2682
2683 sb->s_flags &= ~SB_RDONLY;
2684 }
2685
2686 c->opts.read_only = opts->opts.read_only;
2687
2688 up_write(&c->state_lock);
2689 }
2690
2691 if (opt_defined(opts->opts, errors))
2692 c->opts.errors = opts->opts.errors;
2693 err:
2694 return bch2_err_class(ret);
2695 }
2696
2697 static const struct fs_context_operations bch2_context_ops = {
2698 .free = bch2_fs_context_free,
2699 .parse_param = bch2_fs_parse_param,
2700 .get_tree = bch2_fs_get_tree,
2701 .reconfigure = bch2_fs_reconfigure,
2702 };
2703
bch2_init_fs_context(struct fs_context * fc)2704 static int bch2_init_fs_context(struct fs_context *fc)
2705 {
2706 struct bch2_opts_parse *opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2707
2708 if (!opts)
2709 return -ENOMEM;
2710
2711 opts->parse_later = PRINTBUF;
2712
2713 fc->ops = &bch2_context_ops;
2714 fc->fs_private = opts;
2715
2716 return 0;
2717 }
2718
bch2_fs_vfs_exit(struct bch_fs * c)2719 void bch2_fs_vfs_exit(struct bch_fs *c)
2720 {
2721 if (c->vfs_inodes_by_inum_table.ht.tbl)
2722 rhltable_destroy(&c->vfs_inodes_by_inum_table);
2723 if (c->vfs_inodes_table.tbl)
2724 rhashtable_destroy(&c->vfs_inodes_table);
2725 }
2726
bch2_fs_vfs_init(struct bch_fs * c)2727 int bch2_fs_vfs_init(struct bch_fs *c)
2728 {
2729 return rhashtable_init(&c->vfs_inodes_table, &bch2_vfs_inodes_params) ?:
2730 rhltable_init(&c->vfs_inodes_by_inum_table, &bch2_vfs_inodes_by_inum_params);
2731 }
2732
2733 static struct file_system_type bcache_fs_type = {
2734 .owner = THIS_MODULE,
2735 .name = "bcachefs",
2736 .init_fs_context = bch2_init_fs_context,
2737 .kill_sb = bch2_kill_sb,
2738 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_LBS,
2739 };
2740
2741 MODULE_ALIAS_FS("bcachefs");
2742
bch2_vfs_exit(void)2743 void bch2_vfs_exit(void)
2744 {
2745 unregister_filesystem(&bcache_fs_type);
2746 kmem_cache_destroy(bch2_inode_cache);
2747 }
2748
bch2_vfs_init(void)2749 int __init bch2_vfs_init(void)
2750 {
2751 int ret = -ENOMEM;
2752
2753 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT |
2754 SLAB_ACCOUNT);
2755 if (!bch2_inode_cache)
2756 goto err;
2757
2758 ret = register_filesystem(&bcache_fs_type);
2759 if (ret)
2760 goto err;
2761
2762 return 0;
2763 err:
2764 bch2_vfs_exit();
2765 return ret;
2766 }
2767
2768 #endif /* NO_BCACHEFS_FS */
2769