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 #ifdef CONFIG_UNICODE
726 if (!inode && IS_CASEFOLDED(vdir)) {
727 /*
728 * Do not cache a negative dentry in casefolded directories
729 * as it would need to be invalidated in the following situation:
730 * - Lookup file "blAH" in a casefolded directory
731 * - Creation of file "BLAH" in a casefolded directory
732 * - Lookup file "blAH" in a casefolded directory
733 * which would fail if we had a negative dentry.
734 *
735 * We should come back to this when VFS has a method to handle
736 * this edgecase.
737 */
738 return NULL;
739 }
740 #endif
741
742 return d_splice_alias(&inode->v, dentry);
743 }
744
bch2_mknod(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,umode_t mode,dev_t rdev)745 static int bch2_mknod(struct mnt_idmap *idmap,
746 struct inode *vdir, struct dentry *dentry,
747 umode_t mode, dev_t rdev)
748 {
749 struct bch_inode_info *inode =
750 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
751 (subvol_inum) { 0 }, 0);
752
753 if (IS_ERR(inode))
754 return bch2_err_class(PTR_ERR(inode));
755
756 d_instantiate(dentry, &inode->v);
757 return 0;
758 }
759
bch2_create(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,umode_t mode,bool excl)760 static int bch2_create(struct mnt_idmap *idmap,
761 struct inode *vdir, struct dentry *dentry,
762 umode_t mode, bool excl)
763 {
764 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
765 }
766
__bch2_link(struct bch_fs * c,struct bch_inode_info * inode,struct bch_inode_info * dir,struct dentry * dentry)767 static int __bch2_link(struct bch_fs *c,
768 struct bch_inode_info *inode,
769 struct bch_inode_info *dir,
770 struct dentry *dentry)
771 {
772 struct bch_inode_unpacked dir_u, inode_u;
773 int ret;
774
775 mutex_lock(&inode->ei_update_lock);
776 struct btree_trans *trans = bch2_trans_get(c);
777
778 ret = commit_do(trans, NULL, NULL, 0,
779 bch2_link_trans(trans,
780 inode_inum(dir), &dir_u,
781 inode_inum(inode), &inode_u,
782 &dentry->d_name));
783
784 if (likely(!ret)) {
785 bch2_inode_update_after_write(trans, dir, &dir_u,
786 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
787 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
788 }
789
790 bch2_trans_put(trans);
791 mutex_unlock(&inode->ei_update_lock);
792 return ret;
793 }
794
bch2_link(struct dentry * old_dentry,struct inode * vdir,struct dentry * dentry)795 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
796 struct dentry *dentry)
797 {
798 struct bch_fs *c = vdir->i_sb->s_fs_info;
799 struct bch_inode_info *dir = to_bch_ei(vdir);
800 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
801 int ret;
802
803 lockdep_assert_held(&inode->v.i_rwsem);
804
805 ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
806 bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
807 __bch2_link(c, inode, dir, dentry);
808 if (unlikely(ret))
809 return bch2_err_class(ret);
810
811 ihold(&inode->v);
812 d_instantiate(dentry, &inode->v);
813 return 0;
814 }
815
__bch2_unlink(struct inode * vdir,struct dentry * dentry,bool deleting_snapshot)816 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
817 bool deleting_snapshot)
818 {
819 struct bch_fs *c = vdir->i_sb->s_fs_info;
820 struct bch_inode_info *dir = to_bch_ei(vdir);
821 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
822 struct bch_inode_unpacked dir_u, inode_u;
823 int ret;
824
825 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
826
827 struct btree_trans *trans = bch2_trans_get(c);
828
829 ret = commit_do(trans, NULL, NULL,
830 BCH_TRANS_COMMIT_no_enospc,
831 bch2_unlink_trans(trans,
832 inode_inum(dir), &dir_u,
833 &inode_u, &dentry->d_name,
834 deleting_snapshot));
835 if (unlikely(ret))
836 goto err;
837
838 bch2_inode_update_after_write(trans, dir, &dir_u,
839 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
840 bch2_inode_update_after_write(trans, inode, &inode_u,
841 ATTR_MTIME);
842
843 if (inode_u.bi_subvol) {
844 /*
845 * Subvolume deletion is asynchronous, but we still want to tell
846 * the VFS that it's been deleted here:
847 */
848 set_nlink(&inode->v, 0);
849 }
850
851 if (IS_CASEFOLDED(vdir))
852 d_invalidate(dentry);
853 err:
854 bch2_trans_put(trans);
855 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
856
857 return ret;
858 }
859
bch2_unlink(struct inode * vdir,struct dentry * dentry)860 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
861 {
862 struct bch_inode_info *dir= to_bch_ei(vdir);
863 struct bch_fs *c = dir->v.i_sb->s_fs_info;
864
865 int ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
866 __bch2_unlink(vdir, dentry, false);
867 return bch2_err_class(ret);
868 }
869
bch2_symlink(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,const char * symname)870 static int bch2_symlink(struct mnt_idmap *idmap,
871 struct inode *vdir, struct dentry *dentry,
872 const char *symname)
873 {
874 struct bch_fs *c = vdir->i_sb->s_fs_info;
875 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
876 int ret;
877
878 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
879 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
880 if (IS_ERR(inode))
881 return bch2_err_class(PTR_ERR(inode));
882
883 inode_lock(&inode->v);
884 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
885 inode_unlock(&inode->v);
886
887 if (unlikely(ret))
888 goto err;
889
890 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
891 if (unlikely(ret))
892 goto err;
893
894 ret = __bch2_link(c, inode, dir, dentry);
895 if (unlikely(ret))
896 goto err;
897
898 d_instantiate(dentry, &inode->v);
899 return 0;
900 err:
901 iput(&inode->v);
902 return bch2_err_class(ret);
903 }
904
bch2_mkdir(struct mnt_idmap * idmap,struct inode * vdir,struct dentry * dentry,umode_t mode)905 static struct dentry *bch2_mkdir(struct mnt_idmap *idmap,
906 struct inode *vdir, struct dentry *dentry, umode_t mode)
907 {
908 return ERR_PTR(bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0));
909 }
910
bch2_rename2(struct mnt_idmap * idmap,struct inode * src_vdir,struct dentry * src_dentry,struct inode * dst_vdir,struct dentry * dst_dentry,unsigned flags)911 static int bch2_rename2(struct mnt_idmap *idmap,
912 struct inode *src_vdir, struct dentry *src_dentry,
913 struct inode *dst_vdir, struct dentry *dst_dentry,
914 unsigned flags)
915 {
916 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
917 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
918 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
919 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
920 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
921 struct bch_inode_unpacked dst_dir_u, src_dir_u;
922 struct bch_inode_unpacked src_inode_u, dst_inode_u, *whiteout_inode_u;
923 struct btree_trans *trans;
924 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
925 ? BCH_RENAME_EXCHANGE
926 : dst_dentry->d_inode
927 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
928 bool whiteout = !!(flags & RENAME_WHITEOUT);
929 int ret;
930
931 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE|RENAME_WHITEOUT))
932 return -EINVAL;
933
934 if (mode == BCH_RENAME_OVERWRITE) {
935 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
936 0, LLONG_MAX);
937 if (ret)
938 return ret;
939 }
940
941 bch2_lock_inodes(INODE_UPDATE_LOCK,
942 src_dir,
943 dst_dir,
944 src_inode,
945 dst_inode);
946
947 trans = bch2_trans_get(c);
948
949 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_inum.subvol) ?:
950 bch2_subvol_is_ro_trans(trans, dst_dir->ei_inum.subvol);
951 if (ret)
952 goto err_tx_restart;
953
954 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
955 ret = bch2_fs_quota_transfer(c, src_inode,
956 dst_dir->ei_qid,
957 1 << QTYP_PRJ,
958 KEY_TYPE_QUOTA_PREALLOC);
959 if (ret)
960 goto err;
961 }
962
963 if (mode == BCH_RENAME_EXCHANGE &&
964 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
965 ret = bch2_fs_quota_transfer(c, dst_inode,
966 src_dir->ei_qid,
967 1 << QTYP_PRJ,
968 KEY_TYPE_QUOTA_PREALLOC);
969 if (ret)
970 goto err;
971 }
972 retry:
973 bch2_trans_begin(trans);
974
975 ret = bch2_rename_trans(trans,
976 inode_inum(src_dir), &src_dir_u,
977 inode_inum(dst_dir), &dst_dir_u,
978 &src_inode_u,
979 &dst_inode_u,
980 &src_dentry->d_name,
981 &dst_dentry->d_name,
982 mode);
983 if (unlikely(ret))
984 goto err_tx_restart;
985
986 if (whiteout) {
987 whiteout_inode_u = bch2_trans_kmalloc_nomemzero(trans, sizeof(*whiteout_inode_u));
988 ret = PTR_ERR_OR_ZERO(whiteout_inode_u);
989 if (unlikely(ret))
990 goto err_tx_restart;
991 bch2_inode_init_early(c, whiteout_inode_u);
992
993 ret = bch2_create_trans(trans,
994 inode_inum(src_dir), &src_dir_u,
995 whiteout_inode_u,
996 &src_dentry->d_name,
997 from_kuid(i_user_ns(&src_dir->v), current_fsuid()),
998 from_kgid(i_user_ns(&src_dir->v), current_fsgid()),
999 S_IFCHR|WHITEOUT_MODE, 0,
1000 NULL, NULL, (subvol_inum) { 0 }, 0) ?:
1001 bch2_quota_acct(c, bch_qid(whiteout_inode_u), Q_INO, 1,
1002 KEY_TYPE_QUOTA_PREALLOC);
1003 if (unlikely(ret))
1004 goto err_tx_restart;
1005 }
1006
1007 ret = bch2_trans_commit(trans, NULL, NULL, 0);
1008 if (unlikely(ret)) {
1009 err_tx_restart:
1010 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1011 goto retry;
1012 goto err;
1013 }
1014
1015 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
1016 BUG_ON(dst_inode &&
1017 dst_inode->v.i_ino != dst_inode_u.bi_inum);
1018
1019 bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
1020 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
1021
1022 if (src_dir != dst_dir)
1023 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
1024 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
1025
1026 bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
1027 ATTR_CTIME);
1028
1029 if (dst_inode)
1030 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
1031 ATTR_CTIME);
1032 err:
1033 bch2_trans_put(trans);
1034
1035 bch2_fs_quota_transfer(c, src_inode,
1036 bch_qid(&src_inode->ei_inode),
1037 1 << QTYP_PRJ,
1038 KEY_TYPE_QUOTA_NOCHECK);
1039 if (dst_inode)
1040 bch2_fs_quota_transfer(c, dst_inode,
1041 bch_qid(&dst_inode->ei_inode),
1042 1 << QTYP_PRJ,
1043 KEY_TYPE_QUOTA_NOCHECK);
1044
1045 bch2_unlock_inodes(INODE_UPDATE_LOCK,
1046 src_dir,
1047 dst_dir,
1048 src_inode,
1049 dst_inode);
1050
1051 return bch2_err_class(ret);
1052 }
1053
bch2_setattr_copy(struct mnt_idmap * idmap,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,struct iattr * attr)1054 static void bch2_setattr_copy(struct mnt_idmap *idmap,
1055 struct bch_inode_info *inode,
1056 struct bch_inode_unpacked *bi,
1057 struct iattr *attr)
1058 {
1059 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1060 unsigned int ia_valid = attr->ia_valid;
1061 kuid_t kuid;
1062 kgid_t kgid;
1063
1064 if (ia_valid & ATTR_UID) {
1065 kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
1066 bi->bi_uid = from_kuid(i_user_ns(&inode->v), kuid);
1067 }
1068 if (ia_valid & ATTR_GID) {
1069 kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
1070 bi->bi_gid = from_kgid(i_user_ns(&inode->v), kgid);
1071 }
1072
1073 if (ia_valid & ATTR_SIZE)
1074 bi->bi_size = attr->ia_size;
1075
1076 if (ia_valid & ATTR_ATIME)
1077 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
1078 if (ia_valid & ATTR_MTIME)
1079 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
1080 if (ia_valid & ATTR_CTIME)
1081 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
1082
1083 if (ia_valid & ATTR_MODE) {
1084 umode_t mode = attr->ia_mode;
1085 kgid_t gid = ia_valid & ATTR_GID
1086 ? kgid
1087 : inode->v.i_gid;
1088
1089 if (!in_group_or_capable(idmap, &inode->v,
1090 make_vfsgid(idmap, i_user_ns(&inode->v), gid)))
1091 mode &= ~S_ISGID;
1092 bi->bi_mode = mode;
1093 }
1094 }
1095
bch2_setattr_nonsize(struct mnt_idmap * idmap,struct bch_inode_info * inode,struct iattr * attr)1096 int bch2_setattr_nonsize(struct mnt_idmap *idmap,
1097 struct bch_inode_info *inode,
1098 struct iattr *attr)
1099 {
1100 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1101 struct bch_qid qid;
1102 struct btree_trans *trans;
1103 struct btree_iter inode_iter = {};
1104 struct bch_inode_unpacked inode_u;
1105 struct posix_acl *acl = NULL;
1106 kuid_t kuid;
1107 kgid_t kgid;
1108 int ret;
1109
1110 mutex_lock(&inode->ei_update_lock);
1111
1112 qid = inode->ei_qid;
1113
1114 if (attr->ia_valid & ATTR_UID) {
1115 kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
1116 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), kuid);
1117 }
1118
1119 if (attr->ia_valid & ATTR_GID) {
1120 kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
1121 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), kgid);
1122 }
1123
1124 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
1125 KEY_TYPE_QUOTA_PREALLOC);
1126 if (ret)
1127 goto err;
1128
1129 trans = bch2_trans_get(c);
1130 retry:
1131 bch2_trans_begin(trans);
1132 kfree(acl);
1133 acl = NULL;
1134
1135 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
1136 BTREE_ITER_intent);
1137 if (ret)
1138 goto btree_err;
1139
1140 bch2_setattr_copy(idmap, inode, &inode_u, attr);
1141
1142 if (attr->ia_valid & ATTR_MODE) {
1143 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
1144 inode_u.bi_mode, &acl);
1145 if (ret)
1146 goto btree_err;
1147 }
1148
1149 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
1150 bch2_trans_commit(trans, NULL, NULL,
1151 BCH_TRANS_COMMIT_no_enospc);
1152 btree_err:
1153 bch2_trans_iter_exit(trans, &inode_iter);
1154
1155 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1156 goto retry;
1157 if (unlikely(ret))
1158 goto err_trans;
1159
1160 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
1161
1162 if (acl)
1163 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
1164 err_trans:
1165 bch2_trans_put(trans);
1166 err:
1167 mutex_unlock(&inode->ei_update_lock);
1168
1169 return bch2_err_class(ret);
1170 }
1171
bch2_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned query_flags)1172 static int bch2_getattr(struct mnt_idmap *idmap,
1173 const struct path *path, struct kstat *stat,
1174 u32 request_mask, unsigned query_flags)
1175 {
1176 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
1177 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1178 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, &inode->v);
1179 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, &inode->v);
1180
1181 stat->dev = inode->v.i_sb->s_dev;
1182 stat->ino = inode->v.i_ino;
1183 stat->mode = inode->v.i_mode;
1184 stat->nlink = inode->v.i_nlink;
1185 stat->uid = vfsuid_into_kuid(vfsuid);
1186 stat->gid = vfsgid_into_kgid(vfsgid);
1187 stat->rdev = inode->v.i_rdev;
1188 stat->size = i_size_read(&inode->v);
1189 stat->atime = inode_get_atime(&inode->v);
1190 stat->mtime = inode_get_mtime(&inode->v);
1191 stat->ctime = inode_get_ctime(&inode->v);
1192 stat->blksize = block_bytes(c);
1193 stat->blocks = inode->v.i_blocks;
1194
1195 stat->subvol = inode->ei_inum.subvol;
1196 stat->result_mask |= STATX_SUBVOL;
1197
1198 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->v.i_mode)) {
1199 stat->result_mask |= STATX_DIOALIGN;
1200 /*
1201 * this is incorrect; we should be tracking this in superblock,
1202 * and checking the alignment of open devices
1203 */
1204 stat->dio_mem_align = SECTOR_SIZE;
1205 stat->dio_offset_align = block_bytes(c);
1206 }
1207
1208 if (request_mask & STATX_BTIME) {
1209 stat->result_mask |= STATX_BTIME;
1210 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
1211 }
1212
1213 if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
1214 stat->attributes |= STATX_ATTR_IMMUTABLE;
1215 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
1216
1217 if (inode->ei_inode.bi_flags & BCH_INODE_append)
1218 stat->attributes |= STATX_ATTR_APPEND;
1219 stat->attributes_mask |= STATX_ATTR_APPEND;
1220
1221 if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
1222 stat->attributes |= STATX_ATTR_NODUMP;
1223 stat->attributes_mask |= STATX_ATTR_NODUMP;
1224
1225 return 0;
1226 }
1227
bch2_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)1228 static int bch2_setattr(struct mnt_idmap *idmap,
1229 struct dentry *dentry, struct iattr *iattr)
1230 {
1231 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
1232 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1233 int ret;
1234
1235 lockdep_assert_held(&inode->v.i_rwsem);
1236
1237 ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
1238 setattr_prepare(idmap, dentry, iattr);
1239 if (ret)
1240 return ret;
1241
1242 return iattr->ia_valid & ATTR_SIZE
1243 ? bchfs_truncate(idmap, inode, iattr)
1244 : bch2_setattr_nonsize(idmap, inode, iattr);
1245 }
1246
bch2_tmpfile(struct mnt_idmap * idmap,struct inode * vdir,struct file * file,umode_t mode)1247 static int bch2_tmpfile(struct mnt_idmap *idmap,
1248 struct inode *vdir, struct file *file, umode_t mode)
1249 {
1250 struct bch_inode_info *inode =
1251 __bch2_create(idmap, to_bch_ei(vdir),
1252 file->f_path.dentry, mode, 0,
1253 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
1254
1255 if (IS_ERR(inode))
1256 return bch2_err_class(PTR_ERR(inode));
1257
1258 d_mark_tmpfile(file, &inode->v);
1259 d_instantiate(file->f_path.dentry, &inode->v);
1260 return finish_open_simple(file, 0);
1261 }
1262
1263 struct bch_fiemap_extent {
1264 struct bkey_buf kbuf;
1265 unsigned flags;
1266 };
1267
bch2_fill_extent(struct bch_fs * c,struct fiemap_extent_info * info,struct bch_fiemap_extent * fe)1268 static int bch2_fill_extent(struct bch_fs *c,
1269 struct fiemap_extent_info *info,
1270 struct bch_fiemap_extent *fe)
1271 {
1272 struct bkey_s_c k = bkey_i_to_s_c(fe->kbuf.k);
1273 unsigned flags = fe->flags;
1274
1275 BUG_ON(!k.k->size);
1276
1277 if (bkey_extent_is_direct_data(k.k)) {
1278 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1279 const union bch_extent_entry *entry;
1280 struct extent_ptr_decoded p;
1281 int ret;
1282
1283 if (k.k->type == KEY_TYPE_reflink_v)
1284 flags |= FIEMAP_EXTENT_SHARED;
1285
1286 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1287 int flags2 = 0;
1288 u64 offset = p.ptr.offset;
1289
1290 if (p.ptr.unwritten)
1291 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
1292
1293 if (p.crc.compression_type)
1294 flags2 |= FIEMAP_EXTENT_ENCODED;
1295 else
1296 offset += p.crc.offset;
1297
1298 if ((offset & (block_sectors(c) - 1)) ||
1299 (k.k->size & (block_sectors(c) - 1)))
1300 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
1301
1302 ret = fiemap_fill_next_extent(info,
1303 bkey_start_offset(k.k) << 9,
1304 offset << 9,
1305 k.k->size << 9, flags|flags2);
1306 if (ret)
1307 return ret;
1308 }
1309
1310 return 0;
1311 } else if (bkey_extent_is_inline_data(k.k)) {
1312 return fiemap_fill_next_extent(info,
1313 bkey_start_offset(k.k) << 9,
1314 0, k.k->size << 9,
1315 flags|
1316 FIEMAP_EXTENT_DATA_INLINE);
1317 } else if (k.k->type == KEY_TYPE_reservation) {
1318 return fiemap_fill_next_extent(info,
1319 bkey_start_offset(k.k) << 9,
1320 0, k.k->size << 9,
1321 flags|
1322 FIEMAP_EXTENT_DELALLOC|
1323 FIEMAP_EXTENT_UNWRITTEN);
1324 } else {
1325 BUG();
1326 }
1327 }
1328
1329 /*
1330 * Scan a range of an inode for data in pagecache.
1331 *
1332 * Intended to be retryable, so don't modify the output params until success is
1333 * imminent.
1334 */
1335 static int
bch2_fiemap_hole_pagecache(struct inode * vinode,u64 * start,u64 * end,bool nonblock)1336 bch2_fiemap_hole_pagecache(struct inode *vinode, u64 *start, u64 *end,
1337 bool nonblock)
1338 {
1339 loff_t dstart, dend;
1340
1341 dstart = bch2_seek_pagecache_data(vinode, *start, *end, 0, nonblock);
1342 if (dstart < 0)
1343 return dstart;
1344
1345 if (dstart == *end) {
1346 *start = dstart;
1347 return 0;
1348 }
1349
1350 dend = bch2_seek_pagecache_hole(vinode, dstart, *end, 0, nonblock);
1351 if (dend < 0)
1352 return dend;
1353
1354 /* race */
1355 BUG_ON(dstart == dend);
1356
1357 *start = dstart;
1358 *end = dend;
1359 return 0;
1360 }
1361
1362 /*
1363 * Scan a range of pagecache that corresponds to a file mapping hole in the
1364 * extent btree. If data is found, fake up an extent key so it looks like a
1365 * delalloc extent to the rest of the fiemap processing code.
1366 */
1367 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)1368 bch2_next_fiemap_pagecache_extent(struct btree_trans *trans, struct bch_inode_info *inode,
1369 u64 start, u64 end, struct bch_fiemap_extent *cur)
1370 {
1371 struct bch_fs *c = trans->c;
1372 struct bkey_i_extent *delextent;
1373 struct bch_extent_ptr ptr = {};
1374 loff_t dstart = start << 9, dend = end << 9;
1375 int ret;
1376
1377 /*
1378 * We hold btree locks here so we cannot block on folio locks without
1379 * dropping trans locks first. Run a nonblocking scan for the common
1380 * case of no folios over holes and fall back on failure.
1381 *
1382 * Note that dropping locks like this is technically racy against
1383 * writeback inserting to the extent tree, but a non-sync fiemap scan is
1384 * fundamentally racy with writeback anyways. Therefore, just report the
1385 * range as delalloc regardless of whether we have to cycle trans locks.
1386 */
1387 ret = bch2_fiemap_hole_pagecache(&inode->v, &dstart, &dend, true);
1388 if (ret == -EAGAIN)
1389 ret = drop_locks_do(trans,
1390 bch2_fiemap_hole_pagecache(&inode->v, &dstart, &dend, false));
1391 if (ret < 0)
1392 return ret;
1393
1394 /*
1395 * Create a fake extent key in the buffer. We have to add a dummy extent
1396 * pointer for the fill code to add an extent entry. It's explicitly
1397 * zeroed to reflect delayed allocation (i.e. phys offset 0).
1398 */
1399 bch2_bkey_buf_realloc(&cur->kbuf, c, sizeof(*delextent) / sizeof(u64));
1400 delextent = bkey_extent_init(cur->kbuf.k);
1401 delextent->k.p = POS(inode->ei_inum.inum, dend >> 9);
1402 delextent->k.size = (dend - dstart) >> 9;
1403 bch2_bkey_append_ptr(&delextent->k_i, ptr);
1404
1405 cur->flags = FIEMAP_EXTENT_DELALLOC;
1406
1407 return 0;
1408 }
1409
bch2_next_fiemap_extent(struct btree_trans * trans,struct bch_inode_info * inode,u64 start,u64 end,struct bch_fiemap_extent * cur)1410 static int bch2_next_fiemap_extent(struct btree_trans *trans,
1411 struct bch_inode_info *inode,
1412 u64 start, u64 end,
1413 struct bch_fiemap_extent *cur)
1414 {
1415 u32 snapshot;
1416 int ret = bch2_subvolume_get_snapshot(trans, inode->ei_inum.subvol, &snapshot);
1417 if (ret)
1418 return ret;
1419
1420 struct btree_iter iter;
1421 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
1422 SPOS(inode->ei_inum.inum, start, snapshot), 0);
1423
1424 struct bkey_s_c k =
1425 bch2_btree_iter_peek_max(trans, &iter, POS(inode->ei_inum.inum, end));
1426 ret = bkey_err(k);
1427 if (ret)
1428 goto err;
1429
1430 u64 pagecache_end = k.k ? max(start, bkey_start_offset(k.k)) : end;
1431
1432 ret = bch2_next_fiemap_pagecache_extent(trans, inode, start, pagecache_end, cur);
1433 if (ret)
1434 goto err;
1435
1436 struct bpos pagecache_start = bkey_start_pos(&cur->kbuf.k->k);
1437
1438 /*
1439 * Does the pagecache or the btree take precedence?
1440 *
1441 * It _should_ be the pagecache, so that we correctly report delalloc
1442 * extents when dirty in the pagecache (we're COW, after all).
1443 *
1444 * But we'd have to add per-sector writeback tracking to
1445 * bch_folio_state, otherwise we report delalloc extents for clean
1446 * cached data in the pagecache.
1447 *
1448 * We should do this, but even then fiemap won't report stable mappings:
1449 * on bcachefs data moves around in the background (copygc, rebalance)
1450 * and we don't provide a way for userspace to lock that out.
1451 */
1452 if (k.k &&
1453 bkey_le(bpos_max(iter.pos, bkey_start_pos(k.k)),
1454 pagecache_start)) {
1455 bch2_bkey_buf_reassemble(&cur->kbuf, trans->c, k);
1456 bch2_cut_front(iter.pos, cur->kbuf.k);
1457 bch2_cut_back(POS(inode->ei_inum.inum, end), cur->kbuf.k);
1458 cur->flags = 0;
1459 } else if (k.k) {
1460 bch2_cut_back(bkey_start_pos(k.k), cur->kbuf.k);
1461 }
1462
1463 if (cur->kbuf.k->k.type == KEY_TYPE_reflink_p) {
1464 unsigned sectors = cur->kbuf.k->k.size;
1465 s64 offset_into_extent = 0;
1466 enum btree_id data_btree = BTREE_ID_extents;
1467 ret = bch2_read_indirect_extent(trans, &data_btree, &offset_into_extent,
1468 &cur->kbuf);
1469 if (ret)
1470 goto err;
1471
1472 struct bkey_i *k = cur->kbuf.k;
1473 sectors = min_t(unsigned, sectors, k->k.size - offset_into_extent);
1474
1475 bch2_cut_front(POS(k->k.p.inode,
1476 bkey_start_offset(&k->k) + offset_into_extent),
1477 k);
1478 bch2_key_resize(&k->k, sectors);
1479 k->k.p = iter.pos;
1480 k->k.p.offset += k->k.size;
1481 }
1482 err:
1483 bch2_trans_iter_exit(trans, &iter);
1484 return ret;
1485 }
1486
bch2_fiemap(struct inode * vinode,struct fiemap_extent_info * info,u64 start,u64 len)1487 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
1488 u64 start, u64 len)
1489 {
1490 struct bch_fs *c = vinode->i_sb->s_fs_info;
1491 struct bch_inode_info *ei = to_bch_ei(vinode);
1492 struct btree_trans *trans;
1493 struct bch_fiemap_extent cur, prev;
1494 int ret = 0;
1495
1496 ret = fiemap_prep(&ei->v, info, start, &len, 0);
1497 if (ret)
1498 return ret;
1499
1500 if (start + len < start)
1501 return -EINVAL;
1502
1503 start >>= 9;
1504 u64 end = (start + len) >> 9;
1505
1506 bch2_bkey_buf_init(&cur.kbuf);
1507 bch2_bkey_buf_init(&prev.kbuf);
1508 bkey_init(&prev.kbuf.k->k);
1509
1510 trans = bch2_trans_get(c);
1511
1512 while (start < end) {
1513 ret = lockrestart_do(trans,
1514 bch2_next_fiemap_extent(trans, ei, start, end, &cur));
1515 if (ret)
1516 goto err;
1517
1518 BUG_ON(bkey_start_offset(&cur.kbuf.k->k) < start);
1519 BUG_ON(cur.kbuf.k->k.p.offset > end);
1520
1521 if (bkey_start_offset(&cur.kbuf.k->k) == end)
1522 break;
1523
1524 start = cur.kbuf.k->k.p.offset;
1525
1526 if (!bkey_deleted(&prev.kbuf.k->k)) {
1527 bch2_trans_unlock(trans);
1528 ret = bch2_fill_extent(c, info, &prev);
1529 if (ret)
1530 goto err;
1531 }
1532
1533 bch2_bkey_buf_copy(&prev.kbuf, c, cur.kbuf.k);
1534 prev.flags = cur.flags;
1535 }
1536
1537 if (!bkey_deleted(&prev.kbuf.k->k)) {
1538 bch2_trans_unlock(trans);
1539 prev.flags |= FIEMAP_EXTENT_LAST;
1540 ret = bch2_fill_extent(c, info, &prev);
1541 }
1542 err:
1543 bch2_trans_put(trans);
1544 bch2_bkey_buf_exit(&cur.kbuf, c);
1545 bch2_bkey_buf_exit(&prev.kbuf, c);
1546
1547 return bch2_err_class(ret < 0 ? ret : 0);
1548 }
1549
1550 static const struct vm_operations_struct bch_vm_ops = {
1551 .fault = bch2_page_fault,
1552 .map_pages = filemap_map_pages,
1553 .page_mkwrite = bch2_page_mkwrite,
1554 };
1555
bch2_mmap(struct file * file,struct vm_area_struct * vma)1556 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1557 {
1558 file_accessed(file);
1559
1560 vma->vm_ops = &bch_vm_ops;
1561 return 0;
1562 }
1563
1564 /* Directories: */
1565
bch2_dir_llseek(struct file * file,loff_t offset,int whence)1566 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1567 {
1568 return generic_file_llseek_size(file, offset, whence,
1569 S64_MAX, S64_MAX);
1570 }
1571
bch2_vfs_readdir(struct file * file,struct dir_context * ctx)1572 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1573 {
1574 struct bch_inode_info *inode = file_bch_inode(file);
1575 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1576 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
1577
1578 if (!dir_emit_dots(file, ctx))
1579 return 0;
1580
1581 int ret = bch2_readdir(c, inode_inum(inode), &hash, ctx);
1582
1583 bch_err_fn(c, ret);
1584 return bch2_err_class(ret);
1585 }
1586
bch2_open(struct inode * vinode,struct file * file)1587 static int bch2_open(struct inode *vinode, struct file *file)
1588 {
1589 if (file->f_flags & (O_WRONLY|O_RDWR)) {
1590 struct bch_inode_info *inode = to_bch_ei(vinode);
1591 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1592
1593 int ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol);
1594 if (ret)
1595 return ret;
1596 }
1597
1598 file->f_mode |= FMODE_CAN_ODIRECT;
1599
1600 return generic_file_open(vinode, file);
1601 }
1602
1603 /* bcachefs inode flags -> FS_IOC_GETFLAGS: */
1604 static const __maybe_unused unsigned bch_flags_to_uflags[] = {
1605 [__BCH_INODE_sync] = FS_SYNC_FL,
1606 [__BCH_INODE_immutable] = FS_IMMUTABLE_FL,
1607 [__BCH_INODE_append] = FS_APPEND_FL,
1608 [__BCH_INODE_nodump] = FS_NODUMP_FL,
1609 [__BCH_INODE_noatime] = FS_NOATIME_FL,
1610 };
1611
1612 /* bcachefs inode flags -> FS_IOC_FSGETXATTR: */
1613 static const __maybe_unused unsigned bch_flags_to_xflags[] = {
1614 [__BCH_INODE_sync] = FS_XFLAG_SYNC,
1615 [__BCH_INODE_immutable] = FS_XFLAG_IMMUTABLE,
1616 [__BCH_INODE_append] = FS_XFLAG_APPEND,
1617 [__BCH_INODE_nodump] = FS_XFLAG_NODUMP,
1618 [__BCH_INODE_noatime] = FS_XFLAG_NOATIME,
1619 };
1620
bch2_fileattr_get(struct dentry * dentry,struct fileattr * fa)1621 static int bch2_fileattr_get(struct dentry *dentry,
1622 struct fileattr *fa)
1623 {
1624 struct bch_inode_info *inode = to_bch_ei(d_inode(dentry));
1625 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1626
1627 fileattr_fill_xflags(fa, map_flags(bch_flags_to_xflags, inode->ei_inode.bi_flags));
1628
1629 if (inode->ei_inode.bi_fields_set & (1 << Inode_opt_project))
1630 fa->fsx_xflags |= FS_XFLAG_PROJINHERIT;
1631
1632 if (bch2_inode_casefold(c, &inode->ei_inode))
1633 fa->flags |= FS_CASEFOLD_FL;
1634
1635 fa->fsx_projid = inode->ei_qid.q[QTYP_PRJ];
1636 return 0;
1637 }
1638
1639 struct flags_set {
1640 unsigned mask;
1641 unsigned flags;
1642 unsigned projid;
1643 bool set_project;
1644 bool set_casefold;
1645 bool casefold;
1646 };
1647
fssetxattr_inode_update_fn(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,void * p)1648 static int fssetxattr_inode_update_fn(struct btree_trans *trans,
1649 struct bch_inode_info *inode,
1650 struct bch_inode_unpacked *bi,
1651 void *p)
1652 {
1653 struct bch_fs *c = trans->c;
1654 struct flags_set *s = p;
1655
1656 /*
1657 * We're relying on btree locking here for exclusion with other ioctl
1658 * calls - use the flags in the btree (@bi), not inode->i_flags:
1659 */
1660 if (!S_ISREG(bi->bi_mode) &&
1661 !S_ISDIR(bi->bi_mode) &&
1662 (s->flags & (BCH_INODE_nodump|BCH_INODE_noatime)) != s->flags)
1663 return -EINVAL;
1664
1665 if (s->casefold != bch2_inode_casefold(c, bi)) {
1666 int ret = bch2_inode_set_casefold(trans, inode_inum(inode), bi, s->casefold);
1667 if (ret)
1668 return ret;
1669 }
1670
1671 if (s->set_project) {
1672 bi->bi_project = s->projid;
1673 bi->bi_fields_set |= BIT(Inode_opt_project);
1674 }
1675
1676 bi->bi_flags &= ~s->mask;
1677 bi->bi_flags |= s->flags;
1678
1679 bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v));
1680 return 0;
1681 }
1682
bch2_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct fileattr * fa)1683 static int bch2_fileattr_set(struct mnt_idmap *idmap,
1684 struct dentry *dentry,
1685 struct fileattr *fa)
1686 {
1687 struct bch_inode_info *inode = to_bch_ei(d_inode(dentry));
1688 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1689 struct flags_set s = {};
1690 int ret;
1691
1692 if (fa->fsx_valid) {
1693 fa->fsx_xflags &= ~FS_XFLAG_PROJINHERIT;
1694
1695 s.mask = map_defined(bch_flags_to_xflags);
1696 s.flags |= map_flags_rev(bch_flags_to_xflags, fa->fsx_xflags);
1697 if (fa->fsx_xflags)
1698 return -EOPNOTSUPP;
1699
1700 if (fa->fsx_projid >= U32_MAX)
1701 return -EINVAL;
1702
1703 /*
1704 * inode fields accessible via the xattr interface are stored with a +1
1705 * bias, so that 0 means unset:
1706 */
1707 if ((inode->ei_inode.bi_project ||
1708 fa->fsx_projid) &&
1709 inode->ei_inode.bi_project != fa->fsx_projid + 1) {
1710 s.projid = fa->fsx_projid + 1;
1711 s.set_project = true;
1712 }
1713 }
1714
1715 if (fa->flags_valid) {
1716 s.mask = map_defined(bch_flags_to_uflags);
1717
1718 s.set_casefold = true;
1719 s.casefold = (fa->flags & FS_CASEFOLD_FL) != 0;
1720 fa->flags &= ~FS_CASEFOLD_FL;
1721
1722 s.flags |= map_flags_rev(bch_flags_to_uflags, fa->flags);
1723 if (fa->flags)
1724 return -EOPNOTSUPP;
1725 }
1726
1727 mutex_lock(&inode->ei_update_lock);
1728 ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
1729 (s.set_project
1730 ? bch2_set_projid(c, inode, fa->fsx_projid)
1731 : 0) ?:
1732 bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
1733 ATTR_CTIME);
1734 mutex_unlock(&inode->ei_update_lock);
1735
1736 return bch2_err_class(ret);
1737 }
1738
1739 static const struct file_operations bch_file_operations = {
1740 .open = bch2_open,
1741 .llseek = bch2_llseek,
1742 .read_iter = bch2_read_iter,
1743 .write_iter = bch2_write_iter,
1744 .mmap = bch2_mmap,
1745 .get_unmapped_area = thp_get_unmapped_area,
1746 .fsync = bch2_fsync,
1747 .splice_read = filemap_splice_read,
1748 .splice_write = iter_file_splice_write,
1749 .fallocate = bch2_fallocate_dispatch,
1750 .unlocked_ioctl = bch2_fs_file_ioctl,
1751 #ifdef CONFIG_COMPAT
1752 .compat_ioctl = bch2_compat_fs_ioctl,
1753 #endif
1754 .remap_file_range = bch2_remap_file_range,
1755 };
1756
1757 static const struct inode_operations bch_file_inode_operations = {
1758 .getattr = bch2_getattr,
1759 .setattr = bch2_setattr,
1760 .fiemap = bch2_fiemap,
1761 .listxattr = bch2_xattr_list,
1762 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1763 .get_inode_acl = bch2_get_acl,
1764 .set_acl = bch2_set_acl,
1765 #endif
1766 .fileattr_get = bch2_fileattr_get,
1767 .fileattr_set = bch2_fileattr_set,
1768 };
1769
1770 static const struct inode_operations bch_dir_inode_operations = {
1771 .lookup = bch2_lookup,
1772 .create = bch2_create,
1773 .link = bch2_link,
1774 .unlink = bch2_unlink,
1775 .symlink = bch2_symlink,
1776 .mkdir = bch2_mkdir,
1777 .rmdir = bch2_unlink,
1778 .mknod = bch2_mknod,
1779 .rename = bch2_rename2,
1780 .getattr = bch2_getattr,
1781 .setattr = bch2_setattr,
1782 .tmpfile = bch2_tmpfile,
1783 .listxattr = bch2_xattr_list,
1784 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1785 .get_inode_acl = bch2_get_acl,
1786 .set_acl = bch2_set_acl,
1787 #endif
1788 .fileattr_get = bch2_fileattr_get,
1789 .fileattr_set = bch2_fileattr_set,
1790 };
1791
1792 static const struct file_operations bch_dir_file_operations = {
1793 .llseek = bch2_dir_llseek,
1794 .read = generic_read_dir,
1795 .iterate_shared = bch2_vfs_readdir,
1796 .fsync = bch2_fsync,
1797 .unlocked_ioctl = bch2_fs_file_ioctl,
1798 #ifdef CONFIG_COMPAT
1799 .compat_ioctl = bch2_compat_fs_ioctl,
1800 #endif
1801 };
1802
1803 static const struct inode_operations bch_symlink_inode_operations = {
1804 .get_link = page_get_link,
1805 .getattr = bch2_getattr,
1806 .setattr = bch2_setattr,
1807 .listxattr = bch2_xattr_list,
1808 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1809 .get_inode_acl = bch2_get_acl,
1810 .set_acl = bch2_set_acl,
1811 #endif
1812 .fileattr_get = bch2_fileattr_get,
1813 .fileattr_set = bch2_fileattr_set,
1814 };
1815
1816 static const struct inode_operations bch_special_inode_operations = {
1817 .getattr = bch2_getattr,
1818 .setattr = bch2_setattr,
1819 .listxattr = bch2_xattr_list,
1820 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1821 .get_inode_acl = bch2_get_acl,
1822 .set_acl = bch2_set_acl,
1823 #endif
1824 .fileattr_get = bch2_fileattr_get,
1825 .fileattr_set = bch2_fileattr_set,
1826 };
1827
1828 static const struct address_space_operations bch_address_space_operations = {
1829 .read_folio = bch2_read_folio,
1830 .writepages = bch2_writepages,
1831 .readahead = bch2_readahead,
1832 .dirty_folio = filemap_dirty_folio,
1833 .write_begin = bch2_write_begin,
1834 .write_end = bch2_write_end,
1835 .invalidate_folio = bch2_invalidate_folio,
1836 .release_folio = bch2_release_folio,
1837 #ifdef CONFIG_MIGRATION
1838 .migrate_folio = filemap_migrate_folio,
1839 #endif
1840 .error_remove_folio = generic_error_remove_folio,
1841 };
1842
1843 struct bcachefs_fid {
1844 u64 inum;
1845 u32 subvol;
1846 u32 gen;
1847 } __packed;
1848
1849 struct bcachefs_fid_with_parent {
1850 struct bcachefs_fid fid;
1851 struct bcachefs_fid dir;
1852 } __packed;
1853
bcachefs_fid_valid(int fh_len,int fh_type)1854 static int bcachefs_fid_valid(int fh_len, int fh_type)
1855 {
1856 switch (fh_type) {
1857 case FILEID_BCACHEFS_WITHOUT_PARENT:
1858 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1859 case FILEID_BCACHEFS_WITH_PARENT:
1860 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1861 default:
1862 return false;
1863 }
1864 }
1865
bch2_inode_to_fid(struct bch_inode_info * inode)1866 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1867 {
1868 return (struct bcachefs_fid) {
1869 .inum = inode->ei_inum.inum,
1870 .subvol = inode->ei_inum.subvol,
1871 .gen = inode->ei_inode.bi_generation,
1872 };
1873 }
1874
bch2_encode_fh(struct inode * vinode,u32 * fh,int * len,struct inode * vdir)1875 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1876 struct inode *vdir)
1877 {
1878 struct bch_inode_info *inode = to_bch_ei(vinode);
1879 struct bch_inode_info *dir = to_bch_ei(vdir);
1880 int min_len;
1881
1882 if (!S_ISDIR(inode->v.i_mode) && dir) {
1883 struct bcachefs_fid_with_parent *fid = (void *) fh;
1884
1885 min_len = sizeof(*fid) / sizeof(u32);
1886 if (*len < min_len) {
1887 *len = min_len;
1888 return FILEID_INVALID;
1889 }
1890
1891 fid->fid = bch2_inode_to_fid(inode);
1892 fid->dir = bch2_inode_to_fid(dir);
1893
1894 *len = min_len;
1895 return FILEID_BCACHEFS_WITH_PARENT;
1896 } else {
1897 struct bcachefs_fid *fid = (void *) fh;
1898
1899 min_len = sizeof(*fid) / sizeof(u32);
1900 if (*len < min_len) {
1901 *len = min_len;
1902 return FILEID_INVALID;
1903 }
1904 *fid = bch2_inode_to_fid(inode);
1905
1906 *len = min_len;
1907 return FILEID_BCACHEFS_WITHOUT_PARENT;
1908 }
1909 }
1910
bch2_nfs_get_inode(struct super_block * sb,struct bcachefs_fid fid)1911 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1912 struct bcachefs_fid fid)
1913 {
1914 struct bch_fs *c = sb->s_fs_info;
1915 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1916 .subvol = fid.subvol,
1917 .inum = fid.inum,
1918 });
1919 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1920 iput(vinode);
1921 vinode = ERR_PTR(-ESTALE);
1922 }
1923 return vinode;
1924 }
1925
bch2_fh_to_dentry(struct super_block * sb,struct fid * _fid,int fh_len,int fh_type)1926 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1927 int fh_len, int fh_type)
1928 {
1929 struct bcachefs_fid *fid = (void *) _fid;
1930
1931 if (!bcachefs_fid_valid(fh_len, fh_type))
1932 return NULL;
1933
1934 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1935 }
1936
bch2_fh_to_parent(struct super_block * sb,struct fid * _fid,int fh_len,int fh_type)1937 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1938 int fh_len, int fh_type)
1939 {
1940 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1941
1942 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1943 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1944 return NULL;
1945
1946 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1947 }
1948
bch2_get_parent(struct dentry * child)1949 static struct dentry *bch2_get_parent(struct dentry *child)
1950 {
1951 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1952 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1953 subvol_inum parent_inum = {
1954 .subvol = inode->ei_inode.bi_parent_subvol ?:
1955 inode->ei_inum.subvol,
1956 .inum = inode->ei_inode.bi_dir,
1957 };
1958
1959 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1960 }
1961
bch2_get_name(struct dentry * parent,char * name,struct dentry * child)1962 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1963 {
1964 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1965 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1966 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1967 struct btree_trans *trans;
1968 struct btree_iter iter1;
1969 struct btree_iter iter2;
1970 struct bkey_s_c k;
1971 struct bkey_s_c_dirent d;
1972 struct bch_inode_unpacked inode_u;
1973 subvol_inum target;
1974 u32 snapshot;
1975 struct qstr dirent_name;
1976 unsigned name_len = 0;
1977 int ret;
1978
1979 if (!S_ISDIR(dir->v.i_mode))
1980 return -EINVAL;
1981
1982 trans = bch2_trans_get(c);
1983
1984 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
1985 POS(dir->ei_inode.bi_inum, 0), 0);
1986 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
1987 POS(dir->ei_inode.bi_inum, 0), 0);
1988 retry:
1989 bch2_trans_begin(trans);
1990
1991 ret = bch2_subvolume_get_snapshot(trans, dir->ei_inum.subvol, &snapshot);
1992 if (ret)
1993 goto err;
1994
1995 bch2_btree_iter_set_snapshot(trans, &iter1, snapshot);
1996 bch2_btree_iter_set_snapshot(trans, &iter2, snapshot);
1997
1998 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
1999 if (ret)
2000 goto err;
2001
2002 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
2003 bch2_btree_iter_set_pos(trans, &iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
2004
2005 k = bch2_btree_iter_peek_slot(trans, &iter1);
2006 ret = bkey_err(k);
2007 if (ret)
2008 goto err;
2009
2010 if (k.k->type != KEY_TYPE_dirent) {
2011 ret = bch_err_throw(c, ENOENT_dirent_doesnt_match_inode);
2012 goto err;
2013 }
2014
2015 d = bkey_s_c_to_dirent(k);
2016 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
2017 if (ret > 0)
2018 ret = bch_err_throw(c, ENOENT_dirent_doesnt_match_inode);
2019 if (ret)
2020 goto err;
2021
2022 if (subvol_inum_eq(target, inode->ei_inum))
2023 goto found;
2024 } else {
2025 /*
2026 * File with multiple hardlinks and our backref is to the wrong
2027 * directory - linear search:
2028 */
2029 for_each_btree_key_continue_norestart(trans, iter2, 0, k, ret) {
2030 if (k.k->p.inode > dir->ei_inode.bi_inum)
2031 break;
2032
2033 if (k.k->type != KEY_TYPE_dirent)
2034 continue;
2035
2036 d = bkey_s_c_to_dirent(k);
2037 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
2038 if (ret < 0)
2039 break;
2040 if (ret)
2041 continue;
2042
2043 if (subvol_inum_eq(target, inode->ei_inum))
2044 goto found;
2045 }
2046 }
2047
2048 ret = -ENOENT;
2049 goto err;
2050 found:
2051 dirent_name = bch2_dirent_get_name(d);
2052
2053 name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
2054 memcpy(name, dirent_name.name, name_len);
2055 name[name_len] = '\0';
2056 err:
2057 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
2058 goto retry;
2059
2060 bch2_trans_iter_exit(trans, &iter1);
2061 bch2_trans_iter_exit(trans, &iter2);
2062 bch2_trans_put(trans);
2063
2064 return ret;
2065 }
2066
2067 static const struct export_operations bch_export_ops = {
2068 .encode_fh = bch2_encode_fh,
2069 .fh_to_dentry = bch2_fh_to_dentry,
2070 .fh_to_parent = bch2_fh_to_parent,
2071 .get_parent = bch2_get_parent,
2072 .get_name = bch2_get_name,
2073 };
2074
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)2075 static void bch2_vfs_inode_init(struct btree_trans *trans,
2076 subvol_inum inum,
2077 struct bch_inode_info *inode,
2078 struct bch_inode_unpacked *bi,
2079 struct bch_subvolume *subvol)
2080 {
2081 inode->v.i_ino = inum.inum;
2082 inode->ei_inum = inum;
2083 inode->ei_inode.bi_inum = inum.inum;
2084 bch2_inode_update_after_write(trans, inode, bi, ~0);
2085
2086 inode->v.i_blocks = bi->bi_sectors;
2087 inode->v.i_rdev = bi->bi_dev;
2088 inode->v.i_generation = bi->bi_generation;
2089 inode->v.i_size = bi->bi_size;
2090
2091 inode->ei_flags = 0;
2092 inode->ei_quota_reserved = 0;
2093 inode->ei_qid = bch_qid(bi);
2094
2095 if (BCH_SUBVOLUME_SNAP(subvol))
2096 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
2097
2098 inode->v.i_mapping->a_ops = &bch_address_space_operations;
2099
2100 switch (inode->v.i_mode & S_IFMT) {
2101 case S_IFREG:
2102 inode->v.i_op = &bch_file_inode_operations;
2103 inode->v.i_fop = &bch_file_operations;
2104 break;
2105 case S_IFDIR:
2106 inode->v.i_op = &bch_dir_inode_operations;
2107 inode->v.i_fop = &bch_dir_file_operations;
2108 break;
2109 case S_IFLNK:
2110 inode_nohighmem(&inode->v);
2111 inode->v.i_op = &bch_symlink_inode_operations;
2112 break;
2113 default:
2114 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
2115 inode->v.i_op = &bch_special_inode_operations;
2116 break;
2117 }
2118
2119 mapping_set_folio_min_order(inode->v.i_mapping,
2120 get_order(trans->c->opts.block_size));
2121 }
2122
bch2_free_inode(struct inode * vinode)2123 static void bch2_free_inode(struct inode *vinode)
2124 {
2125 kmem_cache_free(bch2_inode_cache, to_bch_ei(vinode));
2126 }
2127
inode_update_times_fn(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,void * p)2128 static int inode_update_times_fn(struct btree_trans *trans,
2129 struct bch_inode_info *inode,
2130 struct bch_inode_unpacked *bi,
2131 void *p)
2132 {
2133 struct bch_fs *c = inode->v.i_sb->s_fs_info;
2134
2135 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
2136 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
2137 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
2138
2139 return 0;
2140 }
2141
bch2_vfs_write_inode(struct inode * vinode,struct writeback_control * wbc)2142 static int bch2_vfs_write_inode(struct inode *vinode,
2143 struct writeback_control *wbc)
2144 {
2145 struct bch_fs *c = vinode->i_sb->s_fs_info;
2146 struct bch_inode_info *inode = to_bch_ei(vinode);
2147 int ret;
2148
2149 mutex_lock(&inode->ei_update_lock);
2150 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
2151 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
2152 mutex_unlock(&inode->ei_update_lock);
2153
2154 return bch2_err_class(ret);
2155 }
2156
bch2_evict_inode(struct inode * vinode)2157 static void bch2_evict_inode(struct inode *vinode)
2158 {
2159 struct bch_fs *c = vinode->i_sb->s_fs_info;
2160 struct bch_inode_info *inode = to_bch_ei(vinode);
2161 bool delete = !inode->v.i_nlink && !is_bad_inode(&inode->v);
2162
2163 /*
2164 * evict() has waited for outstanding writeback, we'll do no more IO
2165 * through this inode: it's safe to remove from VFS inode hashtable here
2166 *
2167 * Do that now so that other threads aren't blocked from pulling it back
2168 * in, there's no reason for them to be:
2169 */
2170 if (!delete)
2171 bch2_inode_hash_remove(c, inode);
2172
2173 truncate_inode_pages_final(&inode->v.i_data);
2174
2175 clear_inode(&inode->v);
2176
2177 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
2178
2179 if (delete) {
2180 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
2181 KEY_TYPE_QUOTA_WARN);
2182 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
2183 KEY_TYPE_QUOTA_WARN);
2184 int ret = bch2_inode_rm(c, inode_inum(inode));
2185 if (ret && !bch2_err_matches(ret, EROFS)) {
2186 bch_err_msg(c, ret, "VFS incorrectly tried to delete inode %llu:%llu",
2187 inode->ei_inum.subvol,
2188 inode->ei_inum.inum);
2189 bch2_sb_error_count(c, BCH_FSCK_ERR_vfs_bad_inode_rm);
2190 }
2191
2192 /*
2193 * If we are deleting, we need it present in the vfs hash table
2194 * so that fsck can check if unlinked inodes are still open:
2195 */
2196 bch2_inode_hash_remove(c, inode);
2197 }
2198
2199 mutex_lock(&c->vfs_inodes_lock);
2200 list_del_init(&inode->ei_vfs_inode_list);
2201 mutex_unlock(&c->vfs_inodes_lock);
2202 }
2203
bch2_evict_subvolume_inodes(struct bch_fs * c,snapshot_id_list * s)2204 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
2205 {
2206 struct bch_inode_info *inode;
2207 DARRAY(struct bch_inode_info *) grabbed;
2208 bool clean_pass = false, this_pass_clean;
2209
2210 /*
2211 * Initially, we scan for inodes without I_DONTCACHE, then mark them to
2212 * be pruned with d_mark_dontcache().
2213 *
2214 * Once we've had a clean pass where we didn't find any inodes without
2215 * I_DONTCACHE, we wait for them to be freed:
2216 */
2217
2218 darray_init(&grabbed);
2219 darray_make_room(&grabbed, 1024);
2220 again:
2221 cond_resched();
2222 this_pass_clean = true;
2223
2224 mutex_lock(&c->vfs_inodes_lock);
2225 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
2226 if (!snapshot_list_has_id(s, inode->ei_inum.subvol))
2227 continue;
2228
2229 if (!(inode->v.i_state & I_DONTCACHE) &&
2230 !(inode->v.i_state & I_FREEING) &&
2231 igrab(&inode->v)) {
2232 this_pass_clean = false;
2233
2234 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
2235 iput(&inode->v);
2236 break;
2237 }
2238 } else if (clean_pass && this_pass_clean) {
2239 struct wait_bit_queue_entry wqe;
2240 struct wait_queue_head *wq_head;
2241
2242 wq_head = inode_bit_waitqueue(&wqe, &inode->v, __I_NEW);
2243 prepare_to_wait_event(wq_head, &wqe.wq_entry,
2244 TASK_UNINTERRUPTIBLE);
2245 mutex_unlock(&c->vfs_inodes_lock);
2246
2247 schedule();
2248 finish_wait(wq_head, &wqe.wq_entry);
2249 goto again;
2250 }
2251 }
2252 mutex_unlock(&c->vfs_inodes_lock);
2253
2254 darray_for_each(grabbed, i) {
2255 inode = *i;
2256 d_mark_dontcache(&inode->v);
2257 d_prune_aliases(&inode->v);
2258 iput(&inode->v);
2259 }
2260 grabbed.nr = 0;
2261
2262 if (!clean_pass || !this_pass_clean) {
2263 clean_pass = this_pass_clean;
2264 goto again;
2265 }
2266
2267 darray_exit(&grabbed);
2268 }
2269
bch2_statfs(struct dentry * dentry,struct kstatfs * buf)2270 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
2271 {
2272 struct super_block *sb = dentry->d_sb;
2273 struct bch_fs *c = sb->s_fs_info;
2274 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
2275 unsigned shift = sb->s_blocksize_bits - 9;
2276 /*
2277 * this assumes inodes take up 64 bytes, which is a decent average
2278 * number:
2279 */
2280 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
2281
2282 buf->f_type = BCACHEFS_STATFS_MAGIC;
2283 buf->f_bsize = sb->s_blocksize;
2284 buf->f_blocks = usage.capacity >> shift;
2285 buf->f_bfree = usage.free >> shift;
2286 buf->f_bavail = avail_factor(usage.free) >> shift;
2287
2288 buf->f_files = usage.nr_inodes + avail_inodes;
2289 buf->f_ffree = avail_inodes;
2290
2291 buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b);
2292 buf->f_namelen = BCH_NAME_MAX;
2293
2294 return 0;
2295 }
2296
bch2_sync_fs(struct super_block * sb,int wait)2297 static int bch2_sync_fs(struct super_block *sb, int wait)
2298 {
2299 struct bch_fs *c = sb->s_fs_info;
2300 int ret;
2301
2302 trace_bch2_sync_fs(sb, wait);
2303
2304 if (c->opts.journal_flush_disabled)
2305 return 0;
2306
2307 if (!wait) {
2308 bch2_journal_flush_async(&c->journal, NULL);
2309 return 0;
2310 }
2311
2312 ret = bch2_journal_flush(&c->journal);
2313 return bch2_err_class(ret);
2314 }
2315
bch2_path_to_fs(const char * path)2316 static struct bch_fs *bch2_path_to_fs(const char *path)
2317 {
2318 struct bch_fs *c;
2319 dev_t dev;
2320 int ret;
2321
2322 ret = lookup_bdev(path, &dev);
2323 if (ret)
2324 return ERR_PTR(ret);
2325
2326 c = bch2_dev_to_fs(dev);
2327 if (c)
2328 closure_put(&c->cl);
2329 return c ?: ERR_PTR(-ENOENT);
2330 }
2331
bch2_show_devname(struct seq_file * seq,struct dentry * root)2332 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
2333 {
2334 struct bch_fs *c = root->d_sb->s_fs_info;
2335 bool first = true;
2336
2337 guard(rcu)();
2338 for_each_online_member_rcu(c, ca) {
2339 if (!first)
2340 seq_putc(seq, ':');
2341 first = false;
2342 seq_puts(seq, ca->disk_sb.sb_name);
2343 }
2344
2345 return 0;
2346 }
2347
bch2_show_options(struct seq_file * seq,struct dentry * root)2348 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
2349 {
2350 struct bch_fs *c = root->d_sb->s_fs_info;
2351 struct printbuf buf = PRINTBUF;
2352
2353 bch2_opts_to_text(&buf, c->opts, c, c->disk_sb.sb,
2354 OPT_MOUNT, OPT_HIDDEN, OPT_SHOW_MOUNT_STYLE);
2355 printbuf_nul_terminate(&buf);
2356 seq_printf(seq, ",%s", buf.buf);
2357
2358 int ret = buf.allocation_failure ? -ENOMEM : 0;
2359 printbuf_exit(&buf);
2360 return ret;
2361 }
2362
bch2_put_super(struct super_block * sb)2363 static void bch2_put_super(struct super_block *sb)
2364 {
2365 struct bch_fs *c = sb->s_fs_info;
2366
2367 __bch2_fs_stop(c);
2368 }
2369
2370 /*
2371 * bcachefs doesn't currently integrate intwrite freeze protection but the
2372 * internal write references serve the same purpose. Therefore reuse the
2373 * read-only transition code to perform the quiesce. The caveat is that we don't
2374 * currently have the ability to block tasks that want a write reference while
2375 * the superblock is frozen. This is fine for now, but we should either add
2376 * blocking support or find a way to integrate sb_start_intwrite() and friends.
2377 */
bch2_freeze(struct super_block * sb)2378 static int bch2_freeze(struct super_block *sb)
2379 {
2380 struct bch_fs *c = sb->s_fs_info;
2381
2382 down_write(&c->state_lock);
2383 bch2_fs_read_only(c);
2384 up_write(&c->state_lock);
2385 return 0;
2386 }
2387
bch2_unfreeze(struct super_block * sb)2388 static int bch2_unfreeze(struct super_block *sb)
2389 {
2390 struct bch_fs *c = sb->s_fs_info;
2391 int ret;
2392
2393 if (test_bit(BCH_FS_emergency_ro, &c->flags))
2394 return 0;
2395
2396 down_write(&c->state_lock);
2397 ret = bch2_fs_read_write(c);
2398 up_write(&c->state_lock);
2399 return ret;
2400 }
2401
2402 static const struct super_operations bch_super_operations = {
2403 .alloc_inode = bch2_alloc_inode,
2404 .free_inode = bch2_free_inode,
2405 .write_inode = bch2_vfs_write_inode,
2406 .evict_inode = bch2_evict_inode,
2407 .sync_fs = bch2_sync_fs,
2408 .statfs = bch2_statfs,
2409 .show_devname = bch2_show_devname,
2410 .show_options = bch2_show_options,
2411 .put_super = bch2_put_super,
2412 .freeze_fs = bch2_freeze,
2413 .unfreeze_fs = bch2_unfreeze,
2414 };
2415
bch2_set_super(struct super_block * s,void * data)2416 static int bch2_set_super(struct super_block *s, void *data)
2417 {
2418 s->s_fs_info = data;
2419 return 0;
2420 }
2421
bch2_noset_super(struct super_block * s,void * data)2422 static int bch2_noset_super(struct super_block *s, void *data)
2423 {
2424 return -EBUSY;
2425 }
2426
2427 typedef DARRAY(struct bch_fs *) darray_fs;
2428
bch2_test_super(struct super_block * s,void * data)2429 static int bch2_test_super(struct super_block *s, void *data)
2430 {
2431 struct bch_fs *c = s->s_fs_info;
2432 darray_fs *d = data;
2433
2434 if (!c)
2435 return false;
2436
2437 darray_for_each(*d, i)
2438 if (c != *i)
2439 return false;
2440 return true;
2441 }
2442
bch2_fs_get_tree(struct fs_context * fc)2443 static int bch2_fs_get_tree(struct fs_context *fc)
2444 {
2445 struct bch_fs *c;
2446 struct super_block *sb;
2447 struct inode *vinode;
2448 struct bch2_opts_parse *opts_parse = fc->fs_private;
2449 struct bch_opts opts = opts_parse->opts;
2450 darray_const_str devs;
2451 darray_fs devs_to_fs = {};
2452 int ret;
2453
2454 opt_set(opts, read_only, (fc->sb_flags & SB_RDONLY) != 0);
2455 opt_set(opts, nostart, true);
2456
2457 if (!fc->source || strlen(fc->source) == 0)
2458 return -EINVAL;
2459
2460 ret = bch2_split_devs(fc->source, &devs);
2461 if (ret)
2462 return ret;
2463
2464 darray_for_each(devs, i) {
2465 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
2466 if (ret)
2467 goto err;
2468 }
2469
2470 sb = sget(fc->fs_type, bch2_test_super, bch2_noset_super, fc->sb_flags|SB_NOSEC, &devs_to_fs);
2471 if (!IS_ERR(sb))
2472 goto got_sb;
2473
2474 c = bch2_fs_open(&devs, &opts);
2475 ret = PTR_ERR_OR_ZERO(c);
2476 if (ret)
2477 goto err;
2478
2479 if (opt_defined(opts, discard))
2480 set_bit(BCH_FS_discard_mount_opt_set, &c->flags);
2481
2482 /* Some options can't be parsed until after the fs is started: */
2483 opts = bch2_opts_empty();
2484 ret = bch2_parse_mount_opts(c, &opts, NULL, opts_parse->parse_later.buf, false);
2485 if (ret)
2486 goto err_stop_fs;
2487
2488 bch2_opts_apply(&c->opts, opts);
2489
2490 ret = bch2_fs_start(c);
2491 if (ret)
2492 goto err_stop_fs;
2493
2494 /*
2495 * We might be doing a RO mount because other options required it, or we
2496 * have no alloc info and it's a small image with no room to regenerate
2497 * it
2498 */
2499 if (c->opts.read_only)
2500 fc->sb_flags |= SB_RDONLY;
2501
2502 sb = sget(fc->fs_type, NULL, bch2_set_super, fc->sb_flags|SB_NOSEC, c);
2503 ret = PTR_ERR_OR_ZERO(sb);
2504 if (ret)
2505 goto err_stop_fs;
2506 got_sb:
2507 c = sb->s_fs_info;
2508
2509 if (sb->s_root) {
2510 if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY) {
2511 ret = -EBUSY;
2512 goto err_put_super;
2513 }
2514 goto out;
2515 }
2516
2517 sb->s_blocksize = block_bytes(c);
2518 sb->s_blocksize_bits = ilog2(block_bytes(c));
2519 sb->s_maxbytes = MAX_LFS_FILESIZE;
2520 sb->s_op = &bch_super_operations;
2521 sb->s_export_op = &bch_export_ops;
2522 #ifdef CONFIG_BCACHEFS_QUOTA
2523 sb->s_qcop = &bch2_quotactl_operations;
2524 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
2525 #endif
2526 sb->s_xattr = bch2_xattr_handlers;
2527 sb->s_magic = BCACHEFS_STATFS_MAGIC;
2528 sb->s_time_gran = c->sb.nsec_per_time_unit;
2529 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
2530 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
2531 super_set_uuid(sb, c->sb.user_uuid.b, sizeof(c->sb.user_uuid));
2532
2533 if (c->sb.multi_device)
2534 super_set_sysfs_name_uuid(sb);
2535 else
2536 strscpy(sb->s_sysfs_name, c->name, sizeof(sb->s_sysfs_name));
2537
2538 sb->s_shrink->seeks = 0;
2539 c->vfs_sb = sb;
2540 strscpy(sb->s_id, c->name, sizeof(sb->s_id));
2541
2542 ret = super_setup_bdi(sb);
2543 if (ret)
2544 goto err_put_super;
2545
2546 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
2547
2548 scoped_guard(rcu) {
2549 for_each_online_member_rcu(c, ca) {
2550 struct block_device *bdev = ca->disk_sb.bdev;
2551
2552 /* XXX: create an anonymous device for multi device filesystems */
2553 sb->s_bdev = bdev;
2554 sb->s_dev = bdev->bd_dev;
2555 break;
2556 }
2557 }
2558
2559 c->dev = sb->s_dev;
2560
2561 #ifdef CONFIG_BCACHEFS_POSIX_ACL
2562 if (c->opts.acl)
2563 sb->s_flags |= SB_POSIXACL;
2564 #endif
2565
2566 sb->s_shrink->seeks = 0;
2567
2568 #ifdef CONFIG_UNICODE
2569 sb->s_encoding = c->cf_encoding;
2570 #endif
2571 generic_set_sb_d_ops(sb);
2572
2573 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
2574 ret = PTR_ERR_OR_ZERO(vinode);
2575 bch_err_msg(c, ret, "mounting: error getting root inode");
2576 if (ret)
2577 goto err_put_super;
2578
2579 sb->s_root = d_make_root(vinode);
2580 if (!sb->s_root) {
2581 bch_err(c, "error mounting: error allocating root dentry");
2582 ret = -ENOMEM;
2583 goto err_put_super;
2584 }
2585
2586 sb->s_flags |= SB_ACTIVE;
2587 out:
2588 fc->root = dget(sb->s_root);
2589 err:
2590 darray_exit(&devs_to_fs);
2591 bch2_darray_str_exit(&devs);
2592 if (ret)
2593 pr_err("error: %s", bch2_err_str(ret));
2594 /*
2595 * On an inconsistency error in recovery we might see an -EROFS derived
2596 * errorcode (from the journal), but we don't want to return that to
2597 * userspace as that causes util-linux to retry the mount RO - which is
2598 * confusing:
2599 */
2600 if (bch2_err_matches(ret, EROFS) && ret != -EROFS)
2601 ret = -EIO;
2602 return bch2_err_class(ret);
2603
2604 err_stop_fs:
2605 bch2_fs_stop(c);
2606 goto err;
2607
2608 err_put_super:
2609 if (!sb->s_root)
2610 __bch2_fs_stop(c);
2611 deactivate_locked_super(sb);
2612 goto err;
2613 }
2614
bch2_kill_sb(struct super_block * sb)2615 static void bch2_kill_sb(struct super_block *sb)
2616 {
2617 struct bch_fs *c = sb->s_fs_info;
2618
2619 generic_shutdown_super(sb);
2620 bch2_fs_free(c);
2621 }
2622
bch2_fs_context_free(struct fs_context * fc)2623 static void bch2_fs_context_free(struct fs_context *fc)
2624 {
2625 struct bch2_opts_parse *opts = fc->fs_private;
2626
2627 if (opts) {
2628 printbuf_exit(&opts->parse_later);
2629 kfree(opts);
2630 }
2631 }
2632
bch2_fs_parse_param(struct fs_context * fc,struct fs_parameter * param)2633 static int bch2_fs_parse_param(struct fs_context *fc,
2634 struct fs_parameter *param)
2635 {
2636 /*
2637 * the "source" param, i.e., the name of the device(s) to mount,
2638 * is handled by the VFS layer.
2639 */
2640 if (!strcmp(param->key, "source"))
2641 return -ENOPARAM;
2642
2643 struct bch2_opts_parse *opts = fc->fs_private;
2644 struct bch_fs *c = NULL;
2645
2646 /* for reconfigure, we already have a struct bch_fs */
2647 if (fc->root)
2648 c = fc->root->d_sb->s_fs_info;
2649
2650 int ret = bch2_parse_one_mount_opt(c, &opts->opts,
2651 &opts->parse_later, param->key,
2652 param->string);
2653 if (ret)
2654 pr_err("Error parsing option %s: %s", param->key, bch2_err_str(ret));
2655
2656 return bch2_err_class(ret);
2657 }
2658
bch2_fs_reconfigure(struct fs_context * fc)2659 static int bch2_fs_reconfigure(struct fs_context *fc)
2660 {
2661 struct super_block *sb = fc->root->d_sb;
2662 struct bch2_opts_parse *opts = fc->fs_private;
2663 struct bch_fs *c = sb->s_fs_info;
2664 int ret = 0;
2665
2666 opt_set(opts->opts, read_only, (fc->sb_flags & SB_RDONLY) != 0);
2667
2668 if (opts->opts.read_only != c->opts.read_only) {
2669 down_write(&c->state_lock);
2670
2671 if (opts->opts.read_only) {
2672 bch2_fs_read_only(c);
2673
2674 sb->s_flags |= SB_RDONLY;
2675 } else {
2676 ret = bch2_fs_read_write(c);
2677 if (ret) {
2678 bch_err(c, "error going rw: %i", ret);
2679 up_write(&c->state_lock);
2680 ret = -EINVAL;
2681 goto err;
2682 }
2683
2684 sb->s_flags &= ~SB_RDONLY;
2685 }
2686
2687 c->opts.read_only = opts->opts.read_only;
2688
2689 up_write(&c->state_lock);
2690 }
2691
2692 if (opt_defined(opts->opts, errors))
2693 c->opts.errors = opts->opts.errors;
2694 err:
2695 return bch2_err_class(ret);
2696 }
2697
2698 static const struct fs_context_operations bch2_context_ops = {
2699 .free = bch2_fs_context_free,
2700 .parse_param = bch2_fs_parse_param,
2701 .get_tree = bch2_fs_get_tree,
2702 .reconfigure = bch2_fs_reconfigure,
2703 };
2704
bch2_init_fs_context(struct fs_context * fc)2705 static int bch2_init_fs_context(struct fs_context *fc)
2706 {
2707 struct bch2_opts_parse *opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2708
2709 if (!opts)
2710 return -ENOMEM;
2711
2712 opts->parse_later = PRINTBUF;
2713
2714 fc->ops = &bch2_context_ops;
2715 fc->fs_private = opts;
2716
2717 return 0;
2718 }
2719
bch2_fs_vfs_exit(struct bch_fs * c)2720 void bch2_fs_vfs_exit(struct bch_fs *c)
2721 {
2722 if (c->vfs_inodes_by_inum_table.ht.tbl)
2723 rhltable_destroy(&c->vfs_inodes_by_inum_table);
2724 if (c->vfs_inodes_table.tbl)
2725 rhashtable_destroy(&c->vfs_inodes_table);
2726 }
2727
bch2_fs_vfs_init(struct bch_fs * c)2728 int bch2_fs_vfs_init(struct bch_fs *c)
2729 {
2730 return rhashtable_init(&c->vfs_inodes_table, &bch2_vfs_inodes_params) ?:
2731 rhltable_init(&c->vfs_inodes_by_inum_table, &bch2_vfs_inodes_by_inum_params);
2732 }
2733
2734 static struct file_system_type bcache_fs_type = {
2735 .owner = THIS_MODULE,
2736 .name = "bcachefs",
2737 .init_fs_context = bch2_init_fs_context,
2738 .kill_sb = bch2_kill_sb,
2739 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_LBS,
2740 };
2741
2742 MODULE_ALIAS_FS("bcachefs");
2743
bch2_vfs_exit(void)2744 void bch2_vfs_exit(void)
2745 {
2746 unregister_filesystem(&bcache_fs_type);
2747 kmem_cache_destroy(bch2_inode_cache);
2748 }
2749
bch2_vfs_init(void)2750 int __init bch2_vfs_init(void)
2751 {
2752 int ret = -ENOMEM;
2753
2754 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT |
2755 SLAB_ACCOUNT);
2756 if (!bch2_inode_cache)
2757 goto err;
2758
2759 ret = register_filesystem(&bcache_fs_type);
2760 if (ret)
2761 goto err;
2762
2763 return 0;
2764 err:
2765 bch2_vfs_exit();
2766 return ret;
2767 }
2768
2769 #endif /* NO_BCACHEFS_FS */
2770