Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2011 STRATO.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/btrfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "ulist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "backref.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "extent_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "block-group.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) /* TODO XXX FIXME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *  - subvol delete -> delete when ref goes to 0? delete limits also?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  *  - reorganize keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *  - compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  *  - sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  *  - copy also limits on subvol creation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *  - limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  *  - caches for ulists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *  - performance benchmarks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *  - check all ioctl parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * Helpers to access qgroup reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * Callers should ensure the lock context and type are valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	u64 ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		ret += qgroup->rsv.values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #ifdef CONFIG_BTRFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	if (type == BTRFS_QGROUP_RSV_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		return "data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		return "meta_pertrans";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		return "meta_prealloc";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 			   struct btrfs_qgroup *qgroup, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 			   enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	qgroup->rsv.values[type] += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 			       struct btrfs_qgroup *qgroup, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 			       enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	if (qgroup->rsv.values[type] >= num_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		qgroup->rsv.values[type] -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #ifdef CONFIG_BTRFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	WARN_RATELIMIT(1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		"qgroup %llu %s reserved space underflow, have %llu to free %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		qgroup->qgroupid, qgroup_rsv_type_str(type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 		qgroup->rsv.values[type], num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	qgroup->rsv.values[type] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 				     struct btrfs_qgroup *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 				     struct btrfs_qgroup *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 					 struct btrfs_qgroup *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 					  struct btrfs_qgroup *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 					   int mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	if (qg->old_refcnt < seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		qg->old_refcnt = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	qg->old_refcnt += mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 					   int mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	if (qg->new_refcnt < seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		qg->new_refcnt = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	qg->new_refcnt += mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	if (qg->old_refcnt < seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	return qg->old_refcnt - seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	if (qg->new_refcnt < seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	return qg->new_refcnt - seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  * glue structure to represent the relations between qgroups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) struct btrfs_qgroup_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	struct list_head next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	struct list_head next_member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	struct btrfs_qgroup *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	struct btrfs_qgroup *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	return (u64)(uintptr_t)qg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	return (struct btrfs_qgroup *)(uintptr_t)n->aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		   int init_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) /* must be called with qgroup_ioctl_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 					   u64 qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	struct rb_node *n = fs_info->qgroup_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		qgroup = rb_entry(n, struct btrfs_qgroup, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		if (qgroup->qgroupid < qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		else if (qgroup->qgroupid > qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 			n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			return qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) /* must be called with qgroup_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 					  u64 qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	struct rb_node **p = &fs_info->qgroup_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		qgroup = rb_entry(parent, struct btrfs_qgroup, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		if (qgroup->qgroupid < qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		else if (qgroup->qgroupid > qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 			return qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	qgroup->qgroupid = qgroupid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	INIT_LIST_HEAD(&qgroup->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	INIT_LIST_HEAD(&qgroup->members);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	INIT_LIST_HEAD(&qgroup->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	rb_link_node(&qgroup->node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	return qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) static void __del_qgroup_rb(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 			    struct btrfs_qgroup *qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct btrfs_qgroup_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	list_del(&qgroup->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	while (!list_empty(&qgroup->groups)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		list = list_first_entry(&qgroup->groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 					struct btrfs_qgroup_list, next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		list_del(&list->next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		list_del(&list->next_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		kfree(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	while (!list_empty(&qgroup->members)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		list = list_first_entry(&qgroup->members,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 					struct btrfs_qgroup_list, next_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		list_del(&list->next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		list_del(&list->next_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		kfree(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) /* must be called with qgroup_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	rb_erase(&qgroup->node, &fs_info->qgroup_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	__del_qgroup_rb(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) /* must be called with qgroup_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) static int add_relation_rb(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 			   u64 memberid, u64 parentid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	struct btrfs_qgroup *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	struct btrfs_qgroup *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct btrfs_qgroup_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	member = find_qgroup_rb(fs_info, memberid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	parent = find_qgroup_rb(fs_info, parentid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	if (!member || !parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	list = kzalloc(sizeof(*list), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (!list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	list->group = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	list->member = member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	list_add_tail(&list->next_group, &member->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	list_add_tail(&list->next_member, &parent->members);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) /* must be called with qgroup_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) static int del_relation_rb(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			   u64 memberid, u64 parentid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	struct btrfs_qgroup *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	struct btrfs_qgroup *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct btrfs_qgroup_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	member = find_qgroup_rb(fs_info, memberid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	parent = find_qgroup_rb(fs_info, parentid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	if (!member || !parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	list_for_each_entry(list, &member->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		if (list->group == parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 			list_del(&list->next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			list_del(&list->next_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			kfree(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			       u64 rfer, u64 excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	qgroup = find_qgroup_rb(fs_info, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	if (qgroup->rfer != rfer || qgroup->excl != excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  * The full config is read in one go, only called from open_ctree()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  * It doesn't use any locking, as at this point we're still single-threaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	struct btrfs_root *quota_root = fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	u64 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	u64 rescan_progress = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (!fs_info->qgroup_ulist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	ret = btrfs_sysfs_add_qgroups(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	/* default this to quota off, in case no status key is found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	fs_info->qgroup_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	 * pass 1: read status, all qgroup infos and limits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	key.type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		btrfs_item_key_to_cpu(l, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 			struct btrfs_qgroup_status_item *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			ptr = btrfs_item_ptr(l, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 					     struct btrfs_qgroup_status_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 			if (btrfs_qgroup_status_version(l, ptr) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			    BTRFS_QGROUP_STATUS_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 				btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 				 "old qgroup version, quota disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 			if (btrfs_qgroup_status_generation(l, ptr) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 			    fs_info->generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 				flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 				btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 					"qgroup generation mismatch, marked as inconsistent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 									  ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			goto next1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		    found_key.type != BTRFS_QGROUP_LIMIT_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 			goto next1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		qgroup = find_qgroup_rb(fs_info, found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		    (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			btrfs_err(fs_info, "inconsistent qgroup config");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		if (!qgroup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 			qgroup = add_qgroup_rb(fs_info, found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 			if (IS_ERR(qgroup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 				ret = PTR_ERR(qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		switch (found_key.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		case BTRFS_QGROUP_INFO_KEY: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			struct btrfs_qgroup_info_item *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			ptr = btrfs_item_ptr(l, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 					     struct btrfs_qgroup_info_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			/* generation currently unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		case BTRFS_QGROUP_LIMIT_KEY: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			struct btrfs_qgroup_limit_item *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			ptr = btrfs_item_ptr(l, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 					     struct btrfs_qgroup_limit_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 			qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) next1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		ret = btrfs_next_item(quota_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	 * pass 2: read all qgroup relations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	key.type = BTRFS_QGROUP_RELATION_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		btrfs_item_key_to_cpu(l, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			goto next2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		if (found_key.objectid > found_key.offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			/* parent <- member, not needed to build config */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			/* FIXME should we omit the key completely? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			goto next2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		ret = add_relation_rb(fs_info, found_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 				      found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		if (ret == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 				"orphan qgroup relation 0x%llx->0x%llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 				found_key.objectid, found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			ret = 0;	/* ignore the error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) next2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		ret = btrfs_next_item(quota_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	fs_info->qgroup_flags |= flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		 ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		ulist_free(fs_info->qgroup_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		fs_info->qgroup_ulist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		btrfs_sysfs_del_qgroups(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	return ret < 0 ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  * Called in close_ctree() when quota is still enabled.  This verifies we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  * leak some reserved space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  * Return false if no reserved space is left.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  * Return true if some reserved space is leaked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	 * Since we're unmounting, there is no race and no need to grab qgroup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	 * lock.  And here we don't go post-order to provide a more user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	 * friendly sorted result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		qgroup = rb_entry(node, struct btrfs_qgroup, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			if (qgroup->rsv.values[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 				ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 				btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		"qgroup %hu/%llu has unreleased space, type %d rsv %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 				   btrfs_qgroup_level(qgroup->qgroupid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 				   btrfs_qgroup_subvolid(qgroup->qgroupid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 				   i, qgroup->rsv.values[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556)  * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557)  * first two are in single-threaded paths.And for the third one, we have set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558)  * quota_root to be null with qgroup_lock held before, so it is safe to clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559)  * up the in-memory structures without qgroup_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	while ((n = rb_first(&fs_info->qgroup_tree))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		qgroup = rb_entry(n, struct btrfs_qgroup, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		rb_erase(n, &fs_info->qgroup_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		__del_qgroup_rb(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		kfree(qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	 * We call btrfs_free_qgroup_config() when unmounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	 * filesystem and disabling quota, so we set qgroup_ulist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	 * to be null here to avoid double free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	ulist_free(fs_info->qgroup_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	fs_info->qgroup_ulist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	btrfs_sysfs_del_qgroups(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 				    u64 dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	struct btrfs_root *quota_root = trans->fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	key.objectid = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	key.type = BTRFS_QGROUP_RELATION_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	key.offset = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 				    u64 dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	struct btrfs_root *quota_root = trans->fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	key.objectid = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	key.type = BTRFS_QGROUP_RELATION_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	key.offset = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	ret = btrfs_del_item(trans, quota_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) static int add_qgroup_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			   struct btrfs_root *quota_root, u64 qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	struct btrfs_qgroup_info_item *qgroup_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	struct btrfs_qgroup_limit_item *qgroup_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	if (btrfs_is_testing(quota_root->fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	key.type = BTRFS_QGROUP_INFO_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	key.offset = qgroupid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	 * Avoid a transaction abort by catching -EEXIST here. In that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	 * case, we proceed by re-initializing the existing structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	 * on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 				      sizeof(*qgroup_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (ret && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 				 struct btrfs_qgroup_info_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	key.type = BTRFS_QGROUP_LIMIT_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 				      sizeof(*qgroup_limit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	if (ret && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 				  struct btrfs_qgroup_limit_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	struct btrfs_root *quota_root = trans->fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	key.type = BTRFS_QGROUP_INFO_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	key.offset = qgroupid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	ret = btrfs_del_item(trans, quota_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	key.type = BTRFS_QGROUP_LIMIT_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	ret = btrfs_del_item(trans, quota_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 				    struct btrfs_qgroup *qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	struct btrfs_root *quota_root = trans->fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	struct btrfs_qgroup_limit_item *qgroup_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	key.type = BTRFS_QGROUP_LIMIT_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	key.offset = qgroup->qgroupid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	btrfs_mark_buffer_dirty(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 				   struct btrfs_qgroup *qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	struct btrfs_root *quota_root = fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	struct btrfs_qgroup_info_item *qgroup_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (btrfs_is_testing(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	key.type = BTRFS_QGROUP_INFO_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	key.offset = qgroup->qgroupid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	btrfs_mark_buffer_dirty(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	struct btrfs_root *quota_root = fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	struct btrfs_qgroup_status_item *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	key.type = BTRFS_QGROUP_STATUS_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	btrfs_set_qgroup_status_rescan(l, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 				fs_info->qgroup_rescan_progress.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	btrfs_mark_buffer_dirty(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  * called with qgroup_lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 				  struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	struct extent_buffer *leaf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	int nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	key.type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		nr = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		if (!nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		 * delete the leaf one by one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		 * since the whole tree is going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		 * to be deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		path->slots[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		ret = btrfs_del_items(trans, root, path, 0, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	struct btrfs_root *quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct btrfs_root *tree_root = fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	struct btrfs_qgroup_status_item *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	struct btrfs_qgroup *qgroup = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	struct btrfs_trans_handle *trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	struct ulist *ulist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	 * We need to have subvol_sem write locked, to prevent races between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	 * concurrent tasks trying to enable quotas, because we will unlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	 * and relock qgroup_ioctl_lock before setting fs_info->quota_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	 * and before setting BTRFS_FS_QUOTA_ENABLED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	lockdep_assert_held_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	if (fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	ulist = ulist_alloc(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	if (!ulist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	ret = btrfs_sysfs_add_qgroups(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	 * Unlock qgroup_ioctl_lock before starting the transaction. This is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	 * avoid lock acquisition inversion problems (reported by lockdep) between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	 * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	 * start a transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	 * After we started the transaction lock qgroup_ioctl_lock again and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	 * check if someone else created the quota root in the meanwhile. If so,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	 * just return success and release the transaction handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	 * Also we don't need to worry about someone else calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	 * btrfs_sysfs_add_qgroups() after we unlock and getting an error because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	 * that function returns 0 (success) when the sysfs entries already exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	 * 1 for quota root item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	 * 1 for BTRFS_QGROUP_STATUS item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	 * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * per subvolume. However those are not currently reserved since it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * would be a lot of overkill.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	trans = btrfs_start_transaction(tree_root, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	if (fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	fs_info->qgroup_ulist = ulist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	ulist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	 * initially create the quota tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	if (IS_ERR(quota_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		ret =  PTR_ERR(quota_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		goto out_free_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	key.type = BTRFS_QGROUP_STATUS_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 				      sizeof(*ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	ptr = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 				 struct btrfs_qgroup_status_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 				BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	key.type = BTRFS_ROOT_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		goto out_add_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		if (found_key.type == BTRFS_ROOT_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			/* Release locks on tree_root before we access quota_root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			ret = add_qgroup_item(trans, quota_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 					      found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 				btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 				goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			qgroup = add_qgroup_rb(fs_info, found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			if (IS_ERR(qgroup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 				ret = PTR_ERR(qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 				btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 				goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 				btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 				goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 			ret = btrfs_search_slot_for_read(tree_root, &found_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 							 path, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 				btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 				goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 			if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 				 * Shouldn't happen, but in case it does we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 				 * don't need to do the btrfs_next_item, just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 				 * continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		ret = btrfs_next_item(tree_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) out_add_root:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	if (IS_ERR(qgroup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		ret = PTR_ERR(qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	 * a deadlock with tasks concurrently doing other qgroup operations, such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	 * adding/removing qgroups or adding/deleting qgroup relations for example,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	 * because all qgroup operations first start or join a transaction and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	 * lock the qgroup_ioctl_lock mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	 * We are safe from a concurrent task trying to enable quotas, by calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	 * this function, since we are serialized by fs_info->subvol_sem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		goto out_free_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	 * Set quota enabled flag after committing the transaction, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	 * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	 * creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	fs_info->quota_root = quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	ret = qgroup_rescan_init(fs_info, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	        qgroup_rescan_zero_tracking(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		fs_info->qgroup_rescan_running = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	        btrfs_queue_work(fs_info->qgroup_rescan_workers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	                         &fs_info->qgroup_rescan_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) out_free_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) out_free_root:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		btrfs_put_root(quota_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		ulist_free(fs_info->qgroup_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		fs_info->qgroup_ulist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		btrfs_sysfs_del_qgroups(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (ret && trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	else if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		ret = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	ulist_free(ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	struct btrfs_root *quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	struct btrfs_trans_handle *trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	 * We need to have subvol_sem write locked, to prevent races between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	 * concurrent tasks trying to disable quotas, because we will unlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	 * and relock qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	lockdep_assert_held_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	if (!fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	 * Unlock the qgroup_ioctl_lock mutex before waiting for the rescan worker to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	 * complete. Otherwise we can deadlock because btrfs_remove_qgroup() needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	 * to lock that mutex while holding a transaction handle and the rescan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	 * worker needs to commit a transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	 * Request qgroup rescan worker to complete and wait for it. This wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	 * must be done before transaction start for quota disable since it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	 * deadlock with transaction by the qgroup rescan worker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	btrfs_qgroup_wait_for_completion(fs_info, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	 * 1 For the root item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	 * We should also reserve enough items for the quota tree deletion in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	 * btrfs_clean_quota_tree but this is not done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	 * Also, we must always start a transaction without holding the mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	 * qgroup_ioctl_lock, see btrfs_quota_enable().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	trans = btrfs_start_transaction(fs_info->tree_root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	if (!fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	quota_root = fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	fs_info->quota_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	btrfs_free_qgroup_config(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	ret = btrfs_clean_quota_tree(trans, quota_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	ret = btrfs_del_root(trans, &quota_root->root_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	list_del(&quota_root->dirty_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	btrfs_tree_lock(quota_root->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	btrfs_clean_tree_block(quota_root->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	btrfs_tree_unlock(quota_root->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	btrfs_put_root(quota_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	if (ret && trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	else if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		ret = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static void qgroup_dirty(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 			 struct btrfs_qgroup *qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if (list_empty(&qgroup->dirty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  * The easy accounting, we're updating qgroup relationship whose child qgroup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  * only has exclusive extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)  * In this case, all exclusive extents will also be exclusive for parent, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)  * excl/rfer just get added/removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)  * So is qgroup reservation space, which should also be added/removed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)  * parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)  * Or when child tries to release reservation space, parent will underflow its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)  * reservation (for relationship adding case).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)  * Caller should hold fs_info->qgroup_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 				    struct ulist *tmp, u64 ref_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 				    struct btrfs_qgroup *src, int sign)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	struct btrfs_qgroup_list *glist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	u64 num_bytes = src->excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	qgroup = find_qgroup_rb(fs_info, ref_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	qgroup->rfer += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	qgroup->rfer_cmpr += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	WARN_ON(sign < 0 && qgroup->excl < num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	qgroup->excl += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	qgroup->excl_cmpr += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	if (sign > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	qgroup_dirty(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	/* Get all of the parent groups that contain this qgroup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	list_for_each_entry(glist, &qgroup->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		ret = ulist_add(tmp, glist->group->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 				qgroup_to_aux(glist->group), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	/* Iterate all of the parents and adjust their reference counts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	while ((unode = ulist_next(tmp, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		qgroup = unode_aux_to_qgroup(unode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		qgroup->rfer += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		qgroup->rfer_cmpr += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		WARN_ON(sign < 0 && qgroup->excl < num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		qgroup->excl += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		if (sign > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		qgroup->excl_cmpr += sign * num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		qgroup_dirty(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		/* Add any parents of the parents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		list_for_each_entry(glist, &qgroup->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			ret = ulist_add(tmp, glist->group->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 					qgroup_to_aux(glist->group), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)  * Quick path for updating qgroup with only excl refs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  * In that case, just update all parent will be enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  * Or we needs to do a full rescan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)  * Caller should also hold fs_info->qgroup_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)  * Return 0 for quick update, return >0 for need to full rescan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)  * and mark INCONSISTENT flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)  * Return < 0 for other error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) static int quick_update_accounting(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 				   struct ulist *tmp, u64 src, u64 dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 				   int sign)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	qgroup = find_qgroup_rb(fs_info, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	if (qgroup->excl == qgroup->rfer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		err = __qgroup_excl_accounting(fs_info, tmp, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 					       qgroup, sign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			      u64 dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	struct btrfs_qgroup *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	struct btrfs_qgroup *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	struct btrfs_qgroup_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	struct ulist *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	unsigned int nofs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	/* Check the level of src and dst first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	/* We hold a transaction handle open, must do a NOFS allocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	nofs_flag = memalloc_nofs_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	tmp = ulist_alloc(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	memalloc_nofs_restore(nofs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	if (!fs_info->quota_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	member = find_qgroup_rb(fs_info, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	parent = find_qgroup_rb(fs_info, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	if (!member || !parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	/* check if such qgroup relation exist firstly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	list_for_each_entry(list, &member->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		if (list->group == parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	ret = add_qgroup_relation_item(trans, src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	ret = add_qgroup_relation_item(trans, dst, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		del_qgroup_relation_item(trans, src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	ret = add_relation_rb(fs_info, src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	ulist_free(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 				 u64 dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	struct btrfs_qgroup *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	struct btrfs_qgroup *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	struct btrfs_qgroup_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	struct ulist *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	unsigned int nofs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	/* We hold a transaction handle open, must do a NOFS allocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	nofs_flag = memalloc_nofs_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	tmp = ulist_alloc(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	memalloc_nofs_restore(nofs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (!fs_info->quota_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	member = find_qgroup_rb(fs_info, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	parent = find_qgroup_rb(fs_info, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	 * The parent/member pair doesn't exist, then try to delete the dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	 * relation items only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	if (!member || !parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		goto delete_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	/* check if such qgroup relation exist firstly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	list_for_each_entry(list, &member->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		if (list->group == parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) delete_item:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	ret = del_qgroup_relation_item(trans, src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (ret < 0 && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	ret2 = del_qgroup_relation_item(trans, dst, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	if (ret2 < 0 && ret2 != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	/* At least one deletion succeeded, return 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	if (!ret || !ret2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		del_relation_rb(fs_info, src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	ulist_free(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			      u64 dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	ret = __del_qgroup_relation(trans, src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	struct btrfs_root *quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	if (!fs_info->quota_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	quota_root = fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	qgroup = find_qgroup_rb(fs_info, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	if (qgroup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	ret = add_qgroup_item(trans, quota_root, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	qgroup = add_qgroup_rb(fs_info, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	if (IS_ERR(qgroup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		ret = PTR_ERR(qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	struct btrfs_qgroup_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	if (!fs_info->quota_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	qgroup = find_qgroup_rb(fs_info, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	if (!qgroup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	/* Check if there are no children of this qgroup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	if (!list_empty(&qgroup->members)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	ret = del_qgroup_item(trans, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	if (ret && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	while (!list_empty(&qgroup->groups)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		list = list_first_entry(&qgroup->groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 					struct btrfs_qgroup_list, next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		ret = __del_qgroup_relation(trans, qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 					    list->group->qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	del_qgroup_rb(fs_info, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	 * Remove the qgroup from sysfs now without holding the qgroup_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	 * spinlock, since the sysfs_remove_group() function needs to take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	 * the mutex kernfs_mutex through kernfs_remove_by_name_ns().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	kfree(qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		       struct btrfs_qgroup_limit *limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	/* Sometimes we would want to clear the limit on this qgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	 * To meet this requirement, we treat the -1 as a special value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	 * which tell kernel to clear the limit on this qgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	const u64 CLEAR_VALUE = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (!fs_info->quota_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	qgroup = find_qgroup_rb(fs_info, qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	if (!qgroup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		if (limit->max_rfer == CLEAR_VALUE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 			qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 			limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			qgroup->max_rfer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 			qgroup->max_rfer = limit->max_rfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		if (limit->max_excl == CLEAR_VALUE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 			qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 			limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 			qgroup->max_excl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 			qgroup->max_excl = limit->max_excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		if (limit->rsv_rfer == CLEAR_VALUE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 			qgroup->rsv_rfer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 			qgroup->rsv_rfer = limit->rsv_rfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		if (limit->rsv_excl == CLEAR_VALUE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 			qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 			limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 			qgroup->rsv_excl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 			qgroup->rsv_excl = limit->rsv_excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	qgroup->lim_flags |= limit->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	ret = update_qgroup_limit_item(trans, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		btrfs_info(fs_info, "unable to update quota limit for %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		       qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 				struct btrfs_delayed_ref_root *delayed_refs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 				struct btrfs_qgroup_extent_record *record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	struct rb_node *parent_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	struct btrfs_qgroup_extent_record *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	u64 bytenr = record->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	lockdep_assert_held(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	trace_btrfs_qgroup_trace_extent(fs_info, record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		parent_node = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 				 node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		if (bytenr < entry->bytenr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 			p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		} else if (bytenr > entry->bytenr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 			p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 			if (record->data_rsv && !entry->data_rsv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 				entry->data_rsv = record->data_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 				entry->data_rsv_refroot =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 					record->data_rsv_refroot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	rb_link_node(&record->node, parent_node, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 				   struct btrfs_qgroup_extent_record *qrecord)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	struct ulist *old_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	u64 bytenr = qrecord->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) "error accounting new delayed refs extent (err code: %d), quota inconsistent",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	 * Here we don't need to get the lock of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	 * trans->transaction->delayed_refs, since inserted qrecord won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	 * be deleted, only qrecord->node may be modified (new qrecord insert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	 * So modifying qrecord->old_roots is safe here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	qrecord->old_roots = old_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 			      u64 num_bytes, gfp_t gfp_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	struct btrfs_qgroup_extent_record *record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	    || bytenr == 0 || num_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	record = kzalloc(sizeof(*record), gfp_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	if (!record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	record->bytenr = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	record->num_bytes = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	record->old_roots = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		kfree(record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	return btrfs_qgroup_trace_extent_post(fs_info, record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 				  struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	int nr = btrfs_header_nritems(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	int i, extent_type, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	u64 bytenr, num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	/* We can be called directly from walk_up_proc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		btrfs_item_key_to_cpu(eb, &key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		if (key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		/* filter out non qgroup-accountable extents  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		extent_type = btrfs_file_extent_type(eb, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		if (!bytenr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 						GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848)  * Walk up the tree from the bottom, freeing leaves and any interior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849)  * nodes which have had all slots visited. If a node (leaf or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850)  * interior) is freed, the node above it will have it's slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)  * incremented. The root node will never be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)  * At the end of this function, we should have a path which has all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)  * slots incremented to the next position for a search. If we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)  * read a new node it will be NULL and the node above it will have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856)  * correct slot selected for a later read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)  * If we increment the root nodes slot counter past the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)  * elements, 1 is returned to signal completion of the search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	int level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	int nr, slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	if (root_level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	while (level <= root_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		eb = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		nr = btrfs_header_nritems(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		path->slots[level]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		slot = path->slots[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		if (slot >= nr || level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 			 * Don't free the root -  we will detect this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 			 * condition after our loop and return a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 			 * positive value for caller to stop walking the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 			if (level != root_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 				btrfs_tree_unlock_rw(eb, path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 				path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 				free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 				path->nodes[level] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 				path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 			 * We have a valid slot to walk back down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 			 * from. Stop here so caller can process these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 			 * new nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 		level++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	eb = path->nodes[root_level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	if (path->slots[root_level] >= btrfs_header_nritems(eb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)  * Helper function to trace a subtree tree block swap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911)  * The swap will happen in highest tree block, but there may be a lot of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)  * tree blocks involved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)  * For example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915)  *  OO = Old tree blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)  *  NN = New tree blocks allocated during balance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)  *           File tree (257)                  Reloc tree for 257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)  * L2              OO                                NN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)  *               /    \                            /    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  * L1          OO      OO (a)                    OO      NN (a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)  *            / \     / \                       / \     / \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)  * L0       OO   OO OO   OO                   OO   OO NN   NN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)  *                  (b)  (c)                          (b)  (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)  * When calling qgroup_trace_extent_swap(), we will pass:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927)  * @src_eb = OO(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)  * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)  * @dst_level = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)  * @root_level = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)  * In that case, qgroup_trace_extent_swap() will search from OO(a) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)  * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)  * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937)  * 1) Tree search from @src_eb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)  *    It should acts as a simplified btrfs_search_slot().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)  *    The key for search can be extracted from @dst_path->nodes[dst_level]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)  *    (first key).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)  * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)  *    NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)  *    They should be marked during previous (@dst_level = 1) iteration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)  * 3) Mark file extents in leaves dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)  *    We don't have good way to pick out new file extents only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)  *    So we still follow the old method by scanning all file extents in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)  *    the leave.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)  * This function can free us from keeping two paths, thus later we only need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)  * to care about how to iterate all new tree blocks in reloc tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 				    struct extent_buffer *src_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 				    struct btrfs_path *dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 				    int dst_level, int root_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 				    bool trace_leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	struct btrfs_path *src_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	u32 nodesize = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	int cur_level = root_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	BUG_ON(dst_level > root_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	/* Level mismatch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	if (btrfs_header_level(src_eb) != root_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	src_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	if (!src_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	if (dst_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	/* For src_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	atomic_inc(&src_eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	src_path->nodes[root_level] = src_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	src_path->slots[root_level] = dst_path->slots[root_level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	src_path->locks[root_level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	/* A simplified version of btrfs_search_slot() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	while (cur_level >= dst_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		struct btrfs_key src_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		struct btrfs_key dst_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		if (src_path->nodes[cur_level] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 			struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 			struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 			int parent_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 			u64 child_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 			u64 child_bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 			eb = src_path->nodes[cur_level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 			parent_slot = src_path->slots[cur_level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 			child_bytenr = btrfs_node_blockptr(eb, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 			child_gen = btrfs_node_ptr_generation(eb, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 			btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 			eb = read_tree_block(fs_info, child_bytenr, child_gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 					     cur_level, &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 			if (IS_ERR(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 				ret = PTR_ERR(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 			} else if (!extent_buffer_uptodate(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 				free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 				ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 			src_path->nodes[cur_level] = eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 			btrfs_tree_read_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 			btrfs_set_lock_blocking_read(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 			src_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		src_path->slots[cur_level] = dst_path->slots[cur_level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		if (cur_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 			btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 					&dst_key, dst_path->slots[cur_level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 			btrfs_node_key_to_cpu(src_path->nodes[cur_level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 					&src_key, src_path->slots[cur_level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 			btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 					&dst_key, dst_path->slots[cur_level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 			btrfs_item_key_to_cpu(src_path->nodes[cur_level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 					&src_key, src_path->slots[cur_level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		/* Content mismatch, something went wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		if (btrfs_comp_cpu_keys(&dst_key, &src_key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 			ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		cur_level--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	 * Now both @dst_path and @src_path have been populated, record the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	 * blocks for qgroup accounting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 			nodesize, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	ret = btrfs_qgroup_trace_extent(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 			dst_path->nodes[dst_level]->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 			nodesize, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	/* Record leaf file extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	if (dst_level == 0 && trace_leaf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	btrfs_free_path(src_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072)  * Helper function to do recursive generation-aware depth-first search, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073)  * locate all new tree blocks in a subtree of reloc tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075)  * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)  *         reloc tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077)  * L2         NN (a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078)  *          /    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)  * L1    OO        NN (b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)  *      /  \      /  \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)  * L0  OO  OO    OO  NN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)  *               (c) (d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)  * If we pass:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)  * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)  * @cur_level = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)  * @root_level = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)  * above tree blocks along with their counter parts in file tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)  * While during search, old tree blocks OO(c) will be skipped as tree block swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)  * won't affect OO(c).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 					   struct extent_buffer *src_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 					   struct btrfs_path *dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 					   int cur_level, int root_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 					   u64 last_snapshot, bool trace_leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	bool need_cleanup = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	/* Level sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	    root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	    root_level < cur_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 			"%s: bad levels, cur_level=%d root_level=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 			__func__, cur_level, root_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	/* Read the tree block if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	if (dst_path->nodes[cur_level] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		int parent_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		u64 child_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 		u64 child_bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		 * dst_path->nodes[root_level] must be initialized before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 		 * calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		if (cur_level == root_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 			btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	"%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 				__func__, root_level, root_level, cur_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 			return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		 * We need to get child blockptr/gen from parent before we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		 * read it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		eb = dst_path->nodes[cur_level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		parent_slot = dst_path->slots[cur_level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 		child_bytenr = btrfs_node_blockptr(eb, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		child_gen = btrfs_node_ptr_generation(eb, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		/* This node is old, no need to trace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		if (child_gen < last_snapshot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		eb = read_tree_block(fs_info, child_bytenr, child_gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 				     cur_level, &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		if (IS_ERR(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 			ret = PTR_ERR(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		} else if (!extent_buffer_uptodate(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 			free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		dst_path->nodes[cur_level] = eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		dst_path->slots[cur_level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		btrfs_tree_read_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 		btrfs_set_lock_blocking_read(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		dst_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		need_cleanup = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	/* Now record this tree block and its counter part for qgroups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 				       root_level, trace_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	eb = dst_path->nodes[cur_level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	if (cur_level > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 		/* Iterate all child tree blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		for (i = 0; i < btrfs_header_nritems(eb); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 			/* Skip old tree blocks as they won't be swapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 			if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 			dst_path->slots[cur_level] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 			/* Recursive call (at most 7 times) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 			ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 					dst_path, cur_level - 1, root_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 					last_snapshot, trace_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	if (need_cleanup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		/* Clean up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 		btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 				     dst_path->locks[cur_level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		free_extent_buffer(dst_path->nodes[cur_level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		dst_path->nodes[cur_level] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 		dst_path->slots[cur_level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 		dst_path->locks[cur_level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 				struct extent_buffer *src_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 				struct extent_buffer *dst_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 				u64 last_snapshot, bool trace_leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	struct btrfs_path *dst_path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	/* Wrong parameter order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 		btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 		"%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 			     btrfs_header_generation(src_eb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 			     btrfs_header_generation(dst_eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 		return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	level = btrfs_header_level(dst_eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	dst_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	if (!dst_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	/* For dst_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	atomic_inc(&dst_eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	dst_path->nodes[level] = dst_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	dst_path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	dst_path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	/* Do the generation aware breadth-first search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 					      level, last_snapshot, trace_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	btrfs_free_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 			       struct extent_buffer *root_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 			       u64 root_gen, int root_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 	struct extent_buffer *eb = root_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 	BUG_ON(root_eb == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	if (!extent_buffer_uptodate(root_eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	if (root_level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 		ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	 * Walk down the tree.  Missing extent blocks are filled in as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	 * we go. Metadata is accounted every time we read a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	 * extent block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	 * When we reach a leaf, we account for file extent items in it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	 * walk back up the tree (adjusting slot pointers as we go)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	 * and restart the search process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	atomic_inc(&root_eb->refs);	/* For path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	path->nodes[root_level] = root_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	path->slots[root_level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) walk_down:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	level = root_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	while (level >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 		if (path->nodes[level] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 			struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 			int parent_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 			u64 child_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 			u64 child_bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 			 * We need to get child blockptr/gen from parent before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 			 * we can read it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 			  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 			eb = path->nodes[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 			parent_slot = path->slots[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 			child_bytenr = btrfs_node_blockptr(eb, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 			child_gen = btrfs_node_ptr_generation(eb, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 			btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 			eb = read_tree_block(fs_info, child_bytenr, child_gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 					     level, &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 			if (IS_ERR(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 				ret = PTR_ERR(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 			} else if (!extent_buffer_uptodate(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 				free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 				ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 			path->nodes[level] = eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 			path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 			btrfs_tree_read_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 			btrfs_set_lock_blocking_read(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 			path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 			ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 							fs_info->nodesize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 							GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 		if (level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 			ret = btrfs_qgroup_trace_leaf_items(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 							    path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 			/* Nonzero return here means we completed our search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 			ret = adjust_slots_upwards(path, root_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 			/* Restart search with new slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 			goto walk_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 		level--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) #define UPDATE_NEW	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) #define UPDATE_OLD	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375)  * Walk all of the roots that points to the bytenr and adjust their refcnts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 				struct ulist *roots, struct ulist *tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 				struct ulist *qgroups, u64 seq, int update_old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	struct ulist_node *tmp_unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	struct ulist_iterator tmp_uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	struct btrfs_qgroup *qg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	if (!roots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	while ((unode = ulist_next(roots, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		qg = find_qgroup_rb(fs_info, unode->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 		if (!qg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		ulist_reinit(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 				GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 		ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 		ULIST_ITER_INIT(&tmp_uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 		while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 			struct btrfs_qgroup_list *glist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 			qg = unode_aux_to_qgroup(tmp_unode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 			if (update_old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 				btrfs_qgroup_update_old_refcnt(qg, seq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 				btrfs_qgroup_update_new_refcnt(qg, seq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 			list_for_each_entry(glist, &qg->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 				ret = ulist_add(qgroups, glist->group->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 						qgroup_to_aux(glist->group),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 						GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 				if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 				ret = ulist_add(tmp, glist->group->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 						qgroup_to_aux(glist->group),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 						GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 				if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431)  * Update qgroup rfer/excl counters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432)  * Rfer update is easy, codes can explain themselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434)  * Excl update is tricky, the update is split into 2 parts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435)  * Part 1: Possible exclusive <-> sharing detect:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436)  *	|	A	|	!A	|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437)  *  -------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)  *  B	|	*	|	-	|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439)  *  -------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440)  *  !B	|	+	|	**	|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441)  *  -------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443)  * Conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444)  * A:	cur_old_roots < nr_old_roots	(not exclusive before)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445)  * !A:	cur_old_roots == nr_old_roots	(possible exclusive before)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446)  * B:	cur_new_roots < nr_new_roots	(not exclusive now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447)  * !B:	cur_new_roots == nr_new_roots	(possible exclusive now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449)  * Results:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450)  * +: Possible sharing -> exclusive	-: Possible exclusive -> sharing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451)  * *: Definitely not changed.		**: Possible unchanged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453)  * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455)  * To make the logic clear, we first use condition A and B to split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456)  * combination into 4 results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)  * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459)  * only on variant maybe 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461)  * Lastly, check result **, since there are 2 variants maybe 0, split them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462)  * again(2x2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463)  * But this time we don't need to consider other things, the codes and logic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464)  * is easy to understand now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 				  struct ulist *qgroups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 				  u64 nr_old_roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 				  u64 nr_new_roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 				  u64 num_bytes, u64 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	struct btrfs_qgroup *qg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	u64 cur_new_count, cur_old_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	while ((unode = ulist_next(qgroups, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 		bool dirty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		qg = unode_aux_to_qgroup(unode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 		cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 		cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 		trace_qgroup_update_counters(fs_info, qg, cur_old_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 					     cur_new_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 		/* Rfer update part */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 		if (cur_old_count == 0 && cur_new_count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 			qg->rfer += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 			qg->rfer_cmpr += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 			dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 		if (cur_old_count > 0 && cur_new_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 			qg->rfer -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 			qg->rfer_cmpr -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 			dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		/* Excl update part */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		/* Exclusive/none -> shared case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		if (cur_old_count == nr_old_roots &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 		    cur_new_count < nr_new_roots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 			/* Exclusive -> shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 			if (cur_old_count != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 				qg->excl -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 				qg->excl_cmpr -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 				dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 		/* Shared -> exclusive/none case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		if (cur_old_count < nr_old_roots &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 		    cur_new_count == nr_new_roots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 			/* Shared->exclusive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 			if (cur_new_count != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 				qg->excl += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 				qg->excl_cmpr += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 				dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 		/* Exclusive/none -> exclusive/none case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 		if (cur_old_count == nr_old_roots &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		    cur_new_count == nr_new_roots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 			if (cur_old_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 				/* None -> exclusive/none */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 				if (cur_new_count != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 					/* None -> exclusive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 					qg->excl += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 					qg->excl_cmpr += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 					dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 				/* None -> none, nothing changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 				/* Exclusive -> exclusive/none */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 				if (cur_new_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 					/* Exclusive -> none */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 					qg->excl -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 					qg->excl_cmpr -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 					dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 				/* Exclusive -> exclusive, nothing changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 		if (dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 			qgroup_dirty(fs_info, qg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556)  * Check if the @roots potentially is a list of fs tree roots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558)  * Return 0 for definitely not a fs/subvol tree roots ulist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559)  * Return 1 for possible fs/subvol tree roots in the list (considering an empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560)  *          one as well)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) static int maybe_fs_roots(struct ulist *roots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	/* Empty one, still possible for fs roots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 	if (!roots || roots->nnodes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	unode = ulist_next(roots, &uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	if (!unode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	 * If it contains fs tree roots, then it must belong to fs/subvol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	 * trees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	return is_fstree(unode->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 				u64 num_bytes, struct ulist *old_roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 				struct ulist *new_roots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	struct ulist *qgroups = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	struct ulist *tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	u64 nr_new_roots = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	u64 nr_old_roots = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	 * If quotas get disabled meanwhile, the resouces need to be freed and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	 * we can't just exit here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	if (new_roots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 		if (!maybe_fs_roots(new_roots))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 		nr_new_roots = new_roots->nnodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	if (old_roots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 		if (!maybe_fs_roots(old_roots))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		nr_old_roots = old_roots->nnodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	/* Quick exit, either not fs tree roots, or won't affect any qgroup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	if (nr_old_roots == 0 && nr_new_roots == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	BUG_ON(!fs_info->quota_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 	trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 					num_bytes, nr_old_roots, nr_new_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	qgroups = ulist_alloc(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	if (!qgroups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	tmp = ulist_alloc(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	if (!tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 	if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 		if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 			mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 	mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	seq = fs_info->qgroup_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	/* Update old refcnts using old_roots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 				   UPDATE_OLD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	/* Update new refcnts using new_roots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 				   UPDATE_NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 			       num_bytes, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	 * Bump qgroup_seq to avoid seq overlap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	ulist_free(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 	ulist_free(qgroups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 	ulist_free(old_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 	ulist_free(new_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	struct btrfs_qgroup_extent_record *record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	struct ulist *new_roots = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	u64 num_dirty_extents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	u64 qgroup_to_skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	qgroup_to_skip = delayed_refs->qgroup_to_skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 		record = rb_entry(node, struct btrfs_qgroup_extent_record,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 				  node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 		num_dirty_extents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 		trace_btrfs_qgroup_account_extents(fs_info, record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 			 * Old roots should be searched when inserting qgroup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 			 * extent record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 			if (WARN_ON(!record->old_roots)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 				/* Search commit root to find old_roots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 				ret = btrfs_find_all_roots(NULL, fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 						record->bytenr, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 						&record->old_roots, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 				if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 					goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 			/* Free the reserved data space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 			btrfs_qgroup_free_refroot(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 					record->data_rsv_refroot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 					record->data_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 					BTRFS_QGROUP_RSV_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 			 * Use SEQ_LAST as time_seq to do special search, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 			 * doesn't lock tree or delayed_refs and search current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 			 * root. It's safe inside commit_transaction().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 			ret = btrfs_find_all_roots(trans, fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 				record->bytenr, SEQ_LAST, &new_roots, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 			if (qgroup_to_skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 				ulist_del(new_roots, qgroup_to_skip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 				ulist_del(record->old_roots, qgroup_to_skip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 					  0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 			ret = btrfs_qgroup_account_extent(trans, record->bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 							  record->num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 							  record->old_roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 							  new_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 			record->old_roots = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 			new_roots = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 		ulist_free(record->old_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 		ulist_free(new_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 		new_roots = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 		rb_erase(node, &delayed_refs->dirty_extent_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 		kfree(record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	trace_qgroup_num_dirty_extents(fs_info, trans->transid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 				       num_dirty_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750)  * called from commit_transaction. Writes all changed qgroups to disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	if (!fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	while (!list_empty(&fs_info->dirty_qgroups)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 		struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 		qgroup = list_first_entry(&fs_info->dirty_qgroups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 					  struct btrfs_qgroup, dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 		list_del_init(&qgroup->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 		spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 		ret = update_qgroup_info_item(trans, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 			fs_info->qgroup_flags |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 					BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 		ret = update_qgroup_limit_item(trans, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 			fs_info->qgroup_flags |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 					BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 		fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 	ret = update_qgroup_status_item(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791)  * Copy the accounting information between qgroups. This is necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792)  * when a snapshot or a subvolume is created. Throwing an error will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793)  * cause a transaction abort so we take extra care here to only error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794)  * when a readonly fs is a reasonable outcome.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 			 u64 objectid, struct btrfs_qgroup_inherit *inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	u64 *i_qgroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	bool committing = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	struct btrfs_root *quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 	struct btrfs_qgroup *srcgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	struct btrfs_qgroup *dstgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	bool need_rescan = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	u32 level_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	u64 nums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	 * There are only two callers of this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	 * One in create_subvol() in the ioctl context, which needs to hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	 * the qgroup_ioctl_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	 * The other one in create_pending_snapshot() where no other qgroup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	 * code can modify the fs as they all need to either start a new trans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	 * or hold a trans handler, thus we don't need to hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	 * qgroup_ioctl_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	 * This would avoid long and complex lock chain and make lockdep happy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	spin_lock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 	if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 		committing = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	spin_unlock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	if (!committing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 		mutex_lock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	quota_root = fs_info->quota_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	if (!quota_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	if (inherit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 		i_qgroups = (u64 *)(inherit + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 		nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 		       2 * inherit->num_excl_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 		for (i = 0; i < nums; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 			srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 			 * Zero out invalid groups so we can ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 			 * them later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 			if (!srcgroup ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 			    ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 				*i_qgroups = 0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 			++i_qgroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	 * create a tracking group for the subvol itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 	ret = add_qgroup_item(trans, quota_root, objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 	 * add qgroup to all inherited groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	if (inherit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 		i_qgroups = (u64 *)(inherit + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 		for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 			if (*i_qgroups == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 			ret = add_qgroup_relation_item(trans, objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 						       *i_qgroups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 			if (ret && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 			ret = add_qgroup_relation_item(trans, *i_qgroups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 						       objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 			if (ret && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	dstgroup = add_qgroup_rb(fs_info, objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 	if (IS_ERR(dstgroup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 		ret = PTR_ERR(dstgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 		dstgroup->lim_flags = inherit->lim.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 		dstgroup->max_rfer = inherit->lim.max_rfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 		dstgroup->max_excl = inherit->lim.max_excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 		dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 		dstgroup->rsv_excl = inherit->lim.rsv_excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		ret = update_qgroup_limit_item(trans, dstgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 			fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 			btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 				   "unable to update quota limit for %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 				   dstgroup->qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	if (srcid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		srcgroup = find_qgroup_rb(fs_info, srcid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 		if (!srcgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		 * We call inherit after we clone the root in order to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 		 * our counts don't go crazy, so at this point the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 		 * difference between the two roots should be the root node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 		level_size = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 		dstgroup->rfer = srcgroup->rfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 		dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 		dstgroup->excl = level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 		dstgroup->excl_cmpr = level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 		srcgroup->excl = level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 		srcgroup->excl_cmpr = level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 		/* inherit the limit info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 		dstgroup->lim_flags = srcgroup->lim_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 		dstgroup->max_rfer = srcgroup->max_rfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 		dstgroup->max_excl = srcgroup->max_excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 		dstgroup->rsv_rfer = srcgroup->rsv_rfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 		dstgroup->rsv_excl = srcgroup->rsv_excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 		qgroup_dirty(fs_info, dstgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		qgroup_dirty(fs_info, srcgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 	if (!inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 	i_qgroups = (u64 *)(inherit + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 	for (i = 0; i < inherit->num_qgroups; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 		if (*i_qgroups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 			ret = add_relation_rb(fs_info, objectid, *i_qgroups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 				goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 		++i_qgroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 		 * If we're doing a snapshot, and adding the snapshot to a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 		 * qgroup, the numbers are guaranteed to be incorrect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 		if (srcid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 			need_rescan = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	for (i = 0; i <  inherit->num_ref_copies; ++i, i_qgroups += 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 		struct btrfs_qgroup *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 		struct btrfs_qgroup *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 		if (!i_qgroups[0] || !i_qgroups[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 		src = find_qgroup_rb(fs_info, i_qgroups[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 		dst = find_qgroup_rb(fs_info, i_qgroups[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 		if (!src || !dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		dst->rfer = src->rfer - level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 		dst->rfer_cmpr = src->rfer_cmpr - level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 		/* Manually tweaking numbers certainly needs a rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 		need_rescan = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	for (i = 0; i <  inherit->num_excl_copies; ++i, i_qgroups += 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 		struct btrfs_qgroup *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 		struct btrfs_qgroup *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 		if (!i_qgroups[0] || !i_qgroups[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 		src = find_qgroup_rb(fs_info, i_qgroups[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 		dst = find_qgroup_rb(fs_info, i_qgroups[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 		if (!src || !dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 		dst->excl = src->excl + level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		dst->excl_cmpr = src->excl_cmpr + level_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 		need_rescan = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 		ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 	if (!committing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 		mutex_unlock(&fs_info->qgroup_ioctl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	if (need_rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 	if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 	    qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 	    qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 			  enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 	u64 ref_root = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 	if (!is_fstree(ref_root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	if (num_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	    capable(CAP_SYS_RESOURCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 		enforce = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 	if (!fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	qgroup = find_qgroup_rb(fs_info, ref_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	 * in a first step, we check all affected qgroups if any limits would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	 * be exceeded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	ulist_reinit(fs_info->qgroup_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 			qgroup_to_aux(qgroup), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 	while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 		struct btrfs_qgroup *qg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 		struct btrfs_qgroup_list *glist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 		qg = unode_aux_to_qgroup(unode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 		if (enforce && !qgroup_check_limits(qg, num_bytes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 			ret = -EDQUOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 		list_for_each_entry(glist, &qg->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 			ret = ulist_add(fs_info->qgroup_ulist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 					glist->group->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 					qgroup_to_aux(glist->group), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	 * no limits exceeded, now record the reservation into all qgroups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		struct btrfs_qgroup *qg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 		qg = unode_aux_to_qgroup(unode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 		qgroup_rsv_add(fs_info, qg, num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102)  * Free @num_bytes of reserved space with @type for qgroup.  (Normally level 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103)  * qgroup).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105)  * Will handle all higher level qgroup too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107)  * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108)  * This special case is only used for META_PERTRANS type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 			       u64 ref_root, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 			       enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	if (!is_fstree(ref_root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	if (num_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 	if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 		WARN(1, "%s: Invalid type to free", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	if (!fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	qgroup = find_qgroup_rb(fs_info, ref_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 	if (num_bytes == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 		 * We're freeing all pertrans rsv, get reserved value from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 		 * level 0 qgroup as real num_bytes to free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 		num_bytes = qgroup->rsv.values[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	ulist_reinit(fs_info->qgroup_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 			qgroup_to_aux(qgroup), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 		struct btrfs_qgroup *qg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 		struct btrfs_qgroup_list *glist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 		qg = unode_aux_to_qgroup(unode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 		qgroup_rsv_release(fs_info, qg, num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 		list_for_each_entry(glist, &qg->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 			ret = ulist_add(fs_info->qgroup_ulist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 					glist->group->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 					qgroup_to_aux(glist->group), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173)  * Check if the leaf is the last leaf. Which means all node pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174)  * are at their last position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) static bool is_last_leaf(struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 	for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 		if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188)  * returns < 0 on error, 0 when more leafs are to be scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189)  * returns 1 when done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 			      struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	struct btrfs_key found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	struct extent_buffer *scratch_leaf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 	struct ulist *roots = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	bool done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	ret = btrfs_search_slot_for_read(fs_info->extent_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 					 &fs_info->qgroup_rescan_progress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 					 path, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 		"current progress key (%llu %u %llu), search_slot ret %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 		fs_info->qgroup_rescan_progress.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 		fs_info->qgroup_rescan_progress.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 		fs_info->qgroup_rescan_progress.offset, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 		 * The rescan is about to end, we will not be scanning any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 		 * further blocks. We cannot unset the RESCAN flag here, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 		 * we want to commit the transaction if everything went well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 		 * To make the live accounting work in this phase, we set our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 		 * scan progress pointer such that every real extent objectid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 		 * will be smaller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 		fs_info->qgroup_rescan_progress.objectid = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 		mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 	done = is_last_leaf(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 	btrfs_item_key_to_cpu(path->nodes[0], &found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 			      btrfs_header_nritems(path->nodes[0]) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 	fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 	scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 	if (!scratch_leaf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 		mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 	mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 		btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 		if (found.type != BTRFS_EXTENT_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 		    found.type != BTRFS_METADATA_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 		if (found.type == BTRFS_METADATA_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 			num_bytes = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 			num_bytes = found.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 		ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 					   &roots, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 		/* For rescan, just pass old_roots as NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 		ret = btrfs_qgroup_account_extent(trans, found.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 						  num_bytes, NULL, roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	if (scratch_leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 		free_extent_buffer(scratch_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 	if (done && !ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 		fs_info->qgroup_rescan_progress.objectid = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	return btrfs_fs_closing(fs_info) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 		test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 						     qgroup_rescan_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 	struct btrfs_trans_handle *trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 	bool stopped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 	 * Rescan should only search for commit root, and any later difference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 	 * should be recorded by qgroup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 	path->search_commit_root = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	path->skip_locking = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 	while (!err && !(stopped = rescan_should_stop(fs_info))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 		trans = btrfs_start_transaction(fs_info->fs_root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 			err = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 		if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 			err = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 			err = qgroup_rescan_leaf(trans, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 		if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 			btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 			btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 	mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 	if (err > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 	    fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 		fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 	} else if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	 * only update status, since the previous part has already updated the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 	 * qgroup info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 	trans = btrfs_start_transaction(fs_info->quota_root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 		err = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 		trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 		btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 			  "fail to start transaction for status update: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 			  err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 	if (!stopped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 		fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 	if (trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 		ret = update_qgroup_status_item(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 			err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 			btrfs_err(fs_info, "fail to update qgroup status: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 				  err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 	fs_info->qgroup_rescan_running = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 	complete_all(&fs_info->qgroup_rescan_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 	mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 	if (!trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 	if (stopped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 		btrfs_info(fs_info, "qgroup scan paused");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	} else if (err >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 		btrfs_info(fs_info, "qgroup scan completed%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 			err > 0 ? " (inconsistency flag cleared)" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 		btrfs_err(fs_info, "qgroup scan failed with %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375)  * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376)  * memory required for the rescan context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 		   int init_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 	if (!init_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 		/* we're resuming qgroup rescan at mount time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 		if (!(fs_info->qgroup_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 		      BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 			"qgroup rescan init failed, qgroup rescan is not queued");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 		} else if (!(fs_info->qgroup_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 			     BTRFS_QGROUP_STATUS_FLAG_ON)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 			"qgroup rescan init failed, qgroup is not enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 	mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 	if (init_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 		if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 				   "qgroup rescan is already in progress");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 			ret = -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 		} else if (!(fs_info->qgroup_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 			     BTRFS_QGROUP_STATUS_FLAG_ON)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 			"qgroup rescan init failed, qgroup is not enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 		} else if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 			/* Quota disable is in progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 			ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 			mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	memset(&fs_info->qgroup_rescan_progress, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 		sizeof(fs_info->qgroup_rescan_progress));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 	fs_info->qgroup_rescan_progress.objectid = progress_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 	init_completion(&fs_info->qgroup_rescan_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 	btrfs_init_work(&fs_info->qgroup_rescan_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 			btrfs_qgroup_rescan_worker, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 	struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 	/* clear all current qgroup tracking information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 	for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 		qgroup = rb_entry(n, struct btrfs_qgroup, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 		qgroup->rfer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 		qgroup->rfer_cmpr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 		qgroup->excl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 		qgroup->excl_cmpr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 		qgroup_dirty(fs_info, qgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	ret = qgroup_rescan_init(fs_info, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 	 * We have set the rescan_progress to 0, which means no more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 	 * delayed refs will be accounted by btrfs_qgroup_account_ref.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 	 * However, btrfs_qgroup_account_ref may be right after its call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 	 * to btrfs_find_all_roots, in which case it would still do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 	 * accounting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	 * To solve this, we're committing the transaction, which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 	 * ensure we run all delayed refs and only after that, we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 	 * going to clear all tracking information for a clean start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	trans = btrfs_join_transaction(fs_info->fs_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 		fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 		fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 	qgroup_rescan_zero_tracking(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 	mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 	fs_info->qgroup_rescan_running = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 	btrfs_queue_work(fs_info->qgroup_rescan_workers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 			 &fs_info->qgroup_rescan_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 				     bool interruptible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 	int running;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 	mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 	running = fs_info->qgroup_rescan_running;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 	mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	if (!running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 	if (interruptible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 		ret = wait_for_completion_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 					&fs_info->qgroup_rescan_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 		wait_for_completion(&fs_info->qgroup_rescan_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522)  * this is only called from open_ctree where we're still single threaded, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523)  * locking is omitted here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 	if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 		mutex_lock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 		fs_info->qgroup_rescan_running = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 		btrfs_queue_work(fs_info->qgroup_rescan_workers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 				 &fs_info->qgroup_rescan_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 		mutex_unlock(&fs_info->qgroup_rescan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) #define rbtree_iterate_from_safe(node, next, start)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538)        for (node = start; node && ({ next = rb_next(node); 1;}); node = next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) static int qgroup_unreserve_range(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 				  struct extent_changeset *reserved, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 				  u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 	struct rb_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 	struct ulist_node *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 	node = reserved->range_changed.root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 		entry = rb_entry(node, struct ulist_node, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 		if (entry->val < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 			node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 			node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	if (entry->val > start && rb_prev(&entry->rb_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 		entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 				 rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	rbtree_iterate_from_safe(node, next, &entry->rb_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 		u64 entry_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 		u64 entry_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 		u64 entry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 		int clear_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 		entry = rb_entry(node, struct ulist_node, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 		entry_start = entry->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 		entry_end = entry->aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 		entry_len = entry_end - entry_start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 		if (entry_start >= start + len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 		if (entry_start + entry_len <= start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 		 * Now the entry is in [start, start + len), revert the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 		 * EXTENT_QGROUP_RESERVED bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 		clear_ret = clear_extent_bits(&inode->io_tree, entry_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 					      entry_end, EXTENT_QGROUP_RESERVED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 		if (!ret && clear_ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 			ret = clear_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 		ulist_del(&reserved->range_changed, entry->val, entry->aux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 		if (likely(reserved->bytes_changed >= entry_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 			reserved->bytes_changed -= entry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 			reserved->bytes_changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601)  * Try to free some space for qgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603)  * For qgroup, there are only 3 ways to free qgroup space:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604)  * - Flush nodatacow write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605)  *   Any nodatacow write will free its reserved data space at run_delalloc_range().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606)  *   In theory, we should only flush nodatacow inodes, but it's not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607)  *   possible, so we need to flush the whole root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609)  * - Wait for ordered extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610)  *   When ordered extents are finished, their reserved metadata is finally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611)  *   converted to per_trans status, which can be freed by later commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612)  *   transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614)  * - Commit transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615)  *   This would free the meta_per_trans space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616)  *   In theory this shouldn't provide much space, but any more qgroup space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617)  *   is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) static int try_flush_qgroup(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 	bool can_commit = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 	 * If current process holds a transaction, we shouldn't flush, as we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 	 * assume all space reservation happens before a transaction handle is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 	 * held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 	 * But there are cases like btrfs_delayed_item_reserve_metadata() where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 	 * we try to reserve space with one transction handle already held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 	 * In that case we can't commit transaction, but at least try to end it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 	 * and hope the started data writes can free some space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 	if (current->journal_info &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 	    current->journal_info != BTRFS_SEND_TRANS_STUB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 		can_commit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 	 * We don't want to run flush again and again, so if there is a running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 	 * one, we won't try to start a new flush, but exit directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 	if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 		 * We are already holding a transaction, thus we can block other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 		 * threads from flushing.  So exit right now. This increases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 		 * the chance of EDQUOT for heavy load and near limit cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 		 * But we can argue that if we're already near limit, EDQUOT is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 		 * unavoidable anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 		if (!can_commit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 		wait_event(root->qgroup_flush_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 			!test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 	ret = btrfs_start_delalloc_snapshot(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 	btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 	trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 	if (can_commit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 		ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 		ret = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 	clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 	wake_up(&root->qgroup_flush_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) static int qgroup_reserve_data(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 			struct extent_changeset **reserved_ret, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 			u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 	struct extent_changeset *reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 	bool new_reserved = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	u64 orig_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	u64 to_reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 	    !is_fstree(root->root_key.objectid) || len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 	/* @reserved parameter is mandatory for qgroup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 	if (WARN_ON(!reserved_ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 	if (!*reserved_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 		new_reserved = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 		*reserved_ret = extent_changeset_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 		if (!*reserved_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 	reserved = *reserved_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 	/* Record already reserved space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 	orig_reserved = reserved->bytes_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 	ret = set_record_extent_bits(&inode->io_tree, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 			start + len -1, EXTENT_QGROUP_RESERVED, reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 	/* Newly reserved space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 	to_reserve = reserved->bytes_changed - orig_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 	trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 					to_reserve, QGROUP_RESERVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 	ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 	qgroup_unreserve_range(inode, reserved, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	if (new_reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 		extent_changeset_release(reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 		kfree(reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 		*reserved_ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734)  * Reserve qgroup space for range [start, start + len).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736)  * This function will either reserve space from related qgroups or do nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737)  * if the range is already reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739)  * Return 0 for successful reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740)  * Return <0 for error (including -EQUOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742)  * NOTE: This function may sleep for memory allocation, dirty page flushing and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743)  *	 commit transaction. So caller should not hold any dirty page locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 			struct extent_changeset **reserved_ret, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 			u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 	ret = qgroup_reserve_data(inode, reserved_ret, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 	if (ret <= 0 && ret != -EDQUOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	ret = try_flush_qgroup(inode->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	return qgroup_reserve_data(inode, reserved_ret, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) /* Free ranges specified by @reserved, normally in error path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) static int qgroup_free_reserved_data(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 			struct extent_changeset *reserved, u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 	struct extent_changeset changeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 	int freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 	extent_changeset_init(&changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 	len = round_up(start + len, root->fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	start = round_down(start, root->fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 	while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 		u64 range_start = unode->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 		/* unode->aux is the inclusive end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 		u64 range_len = unode->aux - range_start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 		u64 free_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 		u64 free_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 		extent_changeset_release(&changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 		/* Only free range in range [start, start + len) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 		if (range_start >= start + len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 		    range_start + range_len <= start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 		free_start = max(range_start, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 		free_len = min(start + len, range_start + range_len) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 			   free_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		 * TODO: To also modify reserved->ranges_reserved to reflect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 		 * the modification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 		 * However as long as we free qgroup reserved according to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 		 * EXTENT_QGROUP_RESERVED, we won't double free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 		 * So not need to rush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 		ret = clear_record_extent_bits(&inode->io_tree, free_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 				free_start + free_len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 				EXTENT_QGROUP_RESERVED, &changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 		freed += changeset.bytes_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 	btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid, freed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 				  BTRFS_QGROUP_RSV_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 	ret = freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 	extent_changeset_release(&changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) static int __btrfs_qgroup_release_data(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 			struct extent_changeset *reserved, u64 start, u64 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 			int free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 	struct extent_changeset changeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 	int trace_op = QGROUP_RELEASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 	/* In release case, we shouldn't have @reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 	WARN_ON(!free && reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 	if (free && reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 		return qgroup_free_reserved_data(inode, reserved, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 	extent_changeset_init(&changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 	ret = clear_record_extent_bits(&inode->io_tree, start, start + len -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 				       EXTENT_QGROUP_RESERVED, &changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 	if (free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 		trace_op = QGROUP_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 	trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 					changeset.bytes_changed, trace_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 	if (free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 		btrfs_qgroup_free_refroot(inode->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 				inode->root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 				changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 	ret = changeset.bytes_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 	extent_changeset_release(&changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852)  * Free a reserved space range from io_tree and related qgroups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854)  * Should be called when a range of pages get invalidated before reaching disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855)  * Or for error cleanup case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856)  * if @reserved is given, only reserved range in [@start, @start + @len) will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857)  * be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859)  * For data written to disk, use btrfs_qgroup_release_data().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861)  * NOTE: This function may sleep for memory allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) int btrfs_qgroup_free_data(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 			struct extent_changeset *reserved, u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 	return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870)  * Release a reserved space range from io_tree only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872)  * Should be called when a range of pages get written to disk and corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873)  * FILE_EXTENT is inserted into corresponding root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875)  * Since new qgroup accounting framework will only update qgroup numbers at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876)  * commit_transaction() time, its reserved space shouldn't be freed from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877)  * related qgroups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879)  * But we should release the range from io_tree, to allow further write to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880)  * COWed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882)  * NOTE: This function may sleep for memory allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 	return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 			      enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) 	if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 	    type != BTRFS_QGROUP_RSV_META_PERTRANS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 	if (num_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 	spin_lock(&root->qgroup_meta_rsv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 	if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 		root->qgroup_meta_rsv_prealloc += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 		root->qgroup_meta_rsv_pertrans += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 	spin_unlock(&root->qgroup_meta_rsv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 			     enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 	if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 	    type != BTRFS_QGROUP_RSV_META_PERTRANS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 	if (num_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 	spin_lock(&root->qgroup_meta_rsv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 	if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 		num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 				  num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 		root->qgroup_meta_rsv_prealloc -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 		num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 				  num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 		root->qgroup_meta_rsv_pertrans -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 	spin_unlock(&root->qgroup_meta_rsv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 	return num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 			      enum btrfs_qgroup_rsv_type type, bool enforce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	    !is_fstree(root->root_key.objectid) || num_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 	BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 	trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 	ret = qgroup_reserve(root, num_bytes, enforce, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	 * Record what we have reserved into root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 	 * To avoid quota disabled->enabled underflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 	 * In that case, we may try to free space we haven't reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 	 * (since quota was disabled), so record what we reserved into root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 	 * And ensure later release won't underflow this number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	add_root_meta_rsv(root, num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 				enum btrfs_qgroup_rsv_type type, bool enforce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 	ret = btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 	if (ret <= 0 && ret != -EDQUOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 	ret = try_flush_qgroup(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 	return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 	    !is_fstree(root->root_key.objectid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 	/* TODO: Update trace point to handle such free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) 	trace_qgroup_meta_free_all_pertrans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 	/* Special value -1 means to free all reserved space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 	btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, (u64)-1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 				  BTRFS_QGROUP_RSV_META_PERTRANS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) 			      enum btrfs_qgroup_rsv_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 	    !is_fstree(root->root_key.objectid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 	 * reservation for META_PREALLOC can happen before quota is enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 	 * which can lead to underflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 	 * Here ensure we will only free what we really have reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 	num_bytes = sub_root_meta_rsv(root, num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 	trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 	btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 				  num_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 				int num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 	struct btrfs_qgroup *qgroup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 	struct ulist_iterator uiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 	if (num_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 	if (!fs_info->quota_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 	spin_lock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 	qgroup = find_qgroup_rb(fs_info, ref_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 	if (!qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 	ulist_reinit(fs_info->qgroup_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 	ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 		       qgroup_to_aux(qgroup), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) 	ULIST_ITER_INIT(&uiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 	while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 		struct btrfs_qgroup *qg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 		struct btrfs_qgroup_list *glist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 		qg = unode_aux_to_qgroup(unode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 		qgroup_rsv_release(fs_info, qg, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 				BTRFS_QGROUP_RSV_META_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 		qgroup_rsv_add(fs_info, qg, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 				BTRFS_QGROUP_RSV_META_PERTRANS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 		list_for_each_entry(glist, &qg->groups, next_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 			ret = ulist_add(fs_info->qgroup_ulist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 					glist->group->qgroupid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 					qgroup_to_aux(glist->group), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	spin_unlock(&fs_info->qgroup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) 	    !is_fstree(root->root_key.objectid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) 	/* Same as btrfs_qgroup_free_meta_prealloc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) 	num_bytes = sub_root_meta_rsv(root, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) 				      BTRFS_QGROUP_RSV_META_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) 	trace_qgroup_meta_convert(root, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) 	qgroup_convert_meta(fs_info, root->root_key.objectid, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067)  * Check qgroup reserved space leaking, normally at destroy inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068)  * time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 	struct extent_changeset changeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 	struct ulist_node *unode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 	struct ulist_iterator iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 	extent_changeset_init(&changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 	ret = clear_record_extent_bits(&inode->io_tree, 0, (u64)-1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 			EXTENT_QGROUP_RESERVED, &changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 	WARN_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 	if (WARN_ON(changeset.bytes_changed)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 		ULIST_ITER_INIT(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 		while ((unode = ulist_next(&changeset.range_changed, &iter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 			btrfs_warn(inode->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 		"leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 				btrfs_ino(inode), unode->val, unode->aux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 		btrfs_qgroup_free_refroot(inode->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 				inode->root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 				changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 	extent_changeset_release(&changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) void btrfs_qgroup_init_swapped_blocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 	struct btrfs_qgroup_swapped_blocks *swapped_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 	spin_lock_init(&swapped_blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 	for (i = 0; i < BTRFS_MAX_LEVEL; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 		swapped_blocks->blocks[i] = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 	swapped_blocks->swapped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109)  * Delete all swapped blocks record of @root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110)  * Every record here means we skipped a full subtree scan for qgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112)  * Gets called when committing one transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) 	struct btrfs_qgroup_swapped_blocks *swapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 	swapped_blocks = &root->swapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 	spin_lock(&swapped_blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 	if (!swapped_blocks->swapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) 		struct rb_root *cur_root = &swapped_blocks->blocks[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 		struct btrfs_qgroup_swapped_block *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 		struct btrfs_qgroup_swapped_block *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 		rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 						     node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 			kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 		swapped_blocks->blocks[i] = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 	swapped_blocks->swapped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 	spin_unlock(&swapped_blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140)  * Add subtree roots record into @subvol_root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142)  * @subvol_root:	tree root of the subvolume tree get swapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143)  * @bg:			block group under balance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144)  * @subvol_parent/slot:	pointer to the subtree root in subvolume tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145)  * @reloc_parent/slot:	pointer to the subtree root in reloc tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146)  *			BOTH POINTERS ARE BEFORE TREE SWAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147)  * @last_snapshot:	last snapshot generation of the subvolume tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 		struct btrfs_root *subvol_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 		struct btrfs_block_group *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 		struct extent_buffer *subvol_parent, int subvol_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 		struct extent_buffer *reloc_parent, int reloc_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 		u64 last_snapshot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 	struct btrfs_fs_info *fs_info = subvol_root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 	struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 	struct btrfs_qgroup_swapped_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 	struct rb_node **cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 	int level = btrfs_header_level(subvol_parent) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 	if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) 	    btrfs_node_ptr_generation(reloc_parent, reloc_slot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 		btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) 		"%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 			__func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 			btrfs_node_ptr_generation(subvol_parent, subvol_slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 			btrfs_node_ptr_generation(reloc_parent, reloc_slot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 		return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 	block = kmalloc(sizeof(*block), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 	if (!block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 	 * @reloc_parent/slot is still before swap, while @block is going to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 	 * record the bytenr after swap, so we do the swap here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 	block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 	block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 							     reloc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 	block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 	block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 							    subvol_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 	block->last_snapshot = last_snapshot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 	block->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 	 * If we have bg == NULL, we're called from btrfs_recover_relocation(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 	 * no one else can modify tree blocks thus we qgroup will not change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 	 * no matter the value of trace_leaf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 	if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 		block->trace_leaf = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 		block->trace_leaf = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 	/* Insert @block into @blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 	spin_lock(&blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 	cur = &blocks->blocks[level].rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 	while (*cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 		struct btrfs_qgroup_swapped_block *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 		parent = *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 		entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 				 node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 		if (entry->subvol_bytenr < block->subvol_bytenr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 			cur = &(*cur)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 		} else if (entry->subvol_bytenr > block->subvol_bytenr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 			cur = &(*cur)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 			if (entry->subvol_generation !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 					block->subvol_generation ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 			    entry->reloc_bytenr != block->reloc_bytenr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 			    entry->reloc_generation !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 					block->reloc_generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 				 * Duplicated but mismatch entry found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 				 * Shouldn't happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 				 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 				 * Marking qgroup inconsistent should be enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 				 * for end users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 				WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 				ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 			kfree(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) 	rb_link_node(&block->node, parent, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) 	rb_insert_color(&block->node, &blocks->blocks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 	blocks->swapped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 	spin_unlock(&blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 		fs_info->qgroup_flags |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 			BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254)  * Check if the tree block is a subtree root, and if so do the needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255)  * delayed subtree trace for qgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257)  * This is called during btrfs_cow_block().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 					 struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 					 struct extent_buffer *subvol_eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 	struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	struct btrfs_qgroup_swapped_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 	struct extent_buffer *reloc_eb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 	bool swapped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 	int level = btrfs_header_level(subvol_eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 	if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 	spin_lock(&blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 	if (!blocks->swapped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 		spin_unlock(&blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 	node = blocks->blocks[level].rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) 		block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 		if (block->subvol_bytenr < subvol_eb->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) 			node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) 		} else if (block->subvol_bytenr > subvol_eb->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 			node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 	if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 		spin_unlock(&blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 	/* Found one, remove it from @blocks first and update blocks->swapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 	rb_erase(&block->node, &blocks->blocks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 		if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 			swapped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 	blocks->swapped = swapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 	spin_unlock(&blocks->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 	/* Read out reloc subtree root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 	reloc_eb = read_tree_block(fs_info, block->reloc_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 				   block->reloc_generation, block->level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 				   &block->first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 	if (IS_ERR(reloc_eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 		ret = PTR_ERR(reloc_eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 		reloc_eb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 		goto free_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 	if (!extent_buffer_uptodate(reloc_eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 		goto free_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 	ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 			block->last_snapshot, block->trace_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) free_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 	kfree(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 	free_extent_buffer(reloc_eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 		btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 			     "failed to account subtree at bytenr %llu: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 			     subvol_eb->start, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 		fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) 	struct btrfs_qgroup_extent_record *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 	struct btrfs_qgroup_extent_record *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) 	struct rb_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 	root = &trans->delayed_refs.dirty_extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 	rbtree_postorder_for_each_entry_safe(entry, next, root, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 		ulist_free(entry->old_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 		kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) }