xref: /openbmc/linux/fs/btrfs/async-thread.c (revision cb001095)
18b712842SChris Mason /*
28b712842SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
308a9ff32SQu Wenruo  * Copyright (C) 2014 Fujitsu.  All rights reserved.
48b712842SChris Mason  *
58b712842SChris Mason  * This program is free software; you can redistribute it and/or
68b712842SChris Mason  * modify it under the terms of the GNU General Public
78b712842SChris Mason  * License v2 as published by the Free Software Foundation.
88b712842SChris Mason  *
98b712842SChris Mason  * This program is distributed in the hope that it will be useful,
108b712842SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
118b712842SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
128b712842SChris Mason  * General Public License for more details.
138b712842SChris Mason  *
148b712842SChris Mason  * You should have received a copy of the GNU General Public
158b712842SChris Mason  * License along with this program; if not, write to the
168b712842SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
178b712842SChris Mason  * Boston, MA 021110-1307, USA.
188b712842SChris Mason  */
198b712842SChris Mason 
208b712842SChris Mason #include <linux/kthread.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
228b712842SChris Mason #include <linux/list.h>
238b712842SChris Mason #include <linux/spinlock.h>
248b712842SChris Mason #include <linux/freezer.h>
258b712842SChris Mason #include "async-thread.h"
2652483bc2SQu Wenruo #include "ctree.h"
278b712842SChris Mason 
28a046e9c8SQu Wenruo #define WORK_DONE_BIT 0
29a046e9c8SQu Wenruo #define WORK_ORDER_DONE_BIT 1
30a046e9c8SQu Wenruo #define WORK_HIGH_PRIO_BIT 2
314a69a410SChris Mason 
320bd9289cSQu Wenruo #define NO_THRESHOLD (-1)
330bd9289cSQu Wenruo #define DFT_THRESHOLD (32)
340bd9289cSQu Wenruo 
35d458b054SQu Wenruo struct __btrfs_workqueue {
3608a9ff32SQu Wenruo 	struct workqueue_struct *normal_wq;
37cb001095SJeff Mahoney 
38cb001095SJeff Mahoney 	/* File system this workqueue services */
39cb001095SJeff Mahoney 	struct btrfs_fs_info *fs_info;
40cb001095SJeff Mahoney 
4108a9ff32SQu Wenruo 	/* List head pointing to ordered work list */
4208a9ff32SQu Wenruo 	struct list_head ordered_list;
4308a9ff32SQu Wenruo 
4408a9ff32SQu Wenruo 	/* Spinlock for ordered_list */
4508a9ff32SQu Wenruo 	spinlock_t list_lock;
460bd9289cSQu Wenruo 
470bd9289cSQu Wenruo 	/* Thresholding related variants */
480bd9289cSQu Wenruo 	atomic_t pending;
49c6dd6ea5SQu Wenruo 
50c6dd6ea5SQu Wenruo 	/* Up limit of concurrency workers */
51c6dd6ea5SQu Wenruo 	int limit_active;
52c6dd6ea5SQu Wenruo 
53c6dd6ea5SQu Wenruo 	/* Current number of concurrency workers */
54c6dd6ea5SQu Wenruo 	int current_active;
55c6dd6ea5SQu Wenruo 
56c6dd6ea5SQu Wenruo 	/* Threshold to change current_active */
570bd9289cSQu Wenruo 	int thresh;
580bd9289cSQu Wenruo 	unsigned int count;
590bd9289cSQu Wenruo 	spinlock_t thres_lock;
6008a9ff32SQu Wenruo };
6108a9ff32SQu Wenruo 
62d458b054SQu Wenruo struct btrfs_workqueue {
63d458b054SQu Wenruo 	struct __btrfs_workqueue *normal;
64d458b054SQu Wenruo 	struct __btrfs_workqueue *high;
651ca08976SQu Wenruo };
661ca08976SQu Wenruo 
679e0af237SLiu Bo static void normal_work_helper(struct btrfs_work *work);
689e0af237SLiu Bo 
699e0af237SLiu Bo #define BTRFS_WORK_HELPER(name)					\
709e0af237SLiu Bo void btrfs_##name(struct work_struct *arg)				\
719e0af237SLiu Bo {									\
729e0af237SLiu Bo 	struct btrfs_work *work = container_of(arg, struct btrfs_work,	\
739e0af237SLiu Bo 					       normal_work);		\
749e0af237SLiu Bo 	normal_work_helper(work);					\
759e0af237SLiu Bo }
769e0af237SLiu Bo 
77cb001095SJeff Mahoney struct btrfs_fs_info *
78cb001095SJeff Mahoney btrfs_workqueue_owner(struct __btrfs_workqueue *wq)
79cb001095SJeff Mahoney {
80cb001095SJeff Mahoney 	return wq->fs_info;
81cb001095SJeff Mahoney }
82cb001095SJeff Mahoney 
83cb001095SJeff Mahoney struct btrfs_fs_info *
84cb001095SJeff Mahoney btrfs_work_owner(struct btrfs_work *work)
85cb001095SJeff Mahoney {
86cb001095SJeff Mahoney 	return work->wq->fs_info;
87cb001095SJeff Mahoney }
88cb001095SJeff Mahoney 
899e0af237SLiu Bo BTRFS_WORK_HELPER(worker_helper);
909e0af237SLiu Bo BTRFS_WORK_HELPER(delalloc_helper);
919e0af237SLiu Bo BTRFS_WORK_HELPER(flush_delalloc_helper);
929e0af237SLiu Bo BTRFS_WORK_HELPER(cache_helper);
939e0af237SLiu Bo BTRFS_WORK_HELPER(submit_helper);
949e0af237SLiu Bo BTRFS_WORK_HELPER(fixup_helper);
959e0af237SLiu Bo BTRFS_WORK_HELPER(endio_helper);
969e0af237SLiu Bo BTRFS_WORK_HELPER(endio_meta_helper);
979e0af237SLiu Bo BTRFS_WORK_HELPER(endio_meta_write_helper);
989e0af237SLiu Bo BTRFS_WORK_HELPER(endio_raid56_helper);
998b110e39SMiao Xie BTRFS_WORK_HELPER(endio_repair_helper);
1009e0af237SLiu Bo BTRFS_WORK_HELPER(rmw_helper);
1019e0af237SLiu Bo BTRFS_WORK_HELPER(endio_write_helper);
1029e0af237SLiu Bo BTRFS_WORK_HELPER(freespace_write_helper);
1039e0af237SLiu Bo BTRFS_WORK_HELPER(delayed_meta_helper);
1049e0af237SLiu Bo BTRFS_WORK_HELPER(readahead_helper);
1059e0af237SLiu Bo BTRFS_WORK_HELPER(qgroup_rescan_helper);
1069e0af237SLiu Bo BTRFS_WORK_HELPER(extent_refs_helper);
1079e0af237SLiu Bo BTRFS_WORK_HELPER(scrub_helper);
1089e0af237SLiu Bo BTRFS_WORK_HELPER(scrubwrc_helper);
1099e0af237SLiu Bo BTRFS_WORK_HELPER(scrubnc_helper);
11020b2e302SZhao Lei BTRFS_WORK_HELPER(scrubparity_helper);
1119e0af237SLiu Bo 
1129e0af237SLiu Bo static struct __btrfs_workqueue *
113cb001095SJeff Mahoney __btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info, const char *name,
114cb001095SJeff Mahoney 			unsigned int flags, int limit_active, int thresh)
11508a9ff32SQu Wenruo {
11661dd5ae6SDavid Sterba 	struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
11708a9ff32SQu Wenruo 
1185d99a998SDavid Sterba 	if (!ret)
11908a9ff32SQu Wenruo 		return NULL;
12008a9ff32SQu Wenruo 
121cb001095SJeff Mahoney 	ret->fs_info = fs_info;
122c6dd6ea5SQu Wenruo 	ret->limit_active = limit_active;
1230bd9289cSQu Wenruo 	atomic_set(&ret->pending, 0);
1240bd9289cSQu Wenruo 	if (thresh == 0)
1250bd9289cSQu Wenruo 		thresh = DFT_THRESHOLD;
1260bd9289cSQu Wenruo 	/* For low threshold, disabling threshold is a better choice */
1270bd9289cSQu Wenruo 	if (thresh < DFT_THRESHOLD) {
128c6dd6ea5SQu Wenruo 		ret->current_active = limit_active;
1290bd9289cSQu Wenruo 		ret->thresh = NO_THRESHOLD;
1300bd9289cSQu Wenruo 	} else {
131c6dd6ea5SQu Wenruo 		/*
132c6dd6ea5SQu Wenruo 		 * For threshold-able wq, let its concurrency grow on demand.
133c6dd6ea5SQu Wenruo 		 * Use minimal max_active at alloc time to reduce resource
134c6dd6ea5SQu Wenruo 		 * usage.
135c6dd6ea5SQu Wenruo 		 */
136c6dd6ea5SQu Wenruo 		ret->current_active = 1;
1370bd9289cSQu Wenruo 		ret->thresh = thresh;
1380bd9289cSQu Wenruo 	}
1390bd9289cSQu Wenruo 
1401ca08976SQu Wenruo 	if (flags & WQ_HIGHPRI)
1411ca08976SQu Wenruo 		ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
142c6dd6ea5SQu Wenruo 						 ret->current_active, "btrfs",
143c6dd6ea5SQu Wenruo 						 name);
1441ca08976SQu Wenruo 	else
1451ca08976SQu Wenruo 		ret->normal_wq = alloc_workqueue("%s-%s", flags,
146c6dd6ea5SQu Wenruo 						 ret->current_active, "btrfs",
1470bd9289cSQu Wenruo 						 name);
1485d99a998SDavid Sterba 	if (!ret->normal_wq) {
14908a9ff32SQu Wenruo 		kfree(ret);
15008a9ff32SQu Wenruo 		return NULL;
15108a9ff32SQu Wenruo 	}
15208a9ff32SQu Wenruo 
15308a9ff32SQu Wenruo 	INIT_LIST_HEAD(&ret->ordered_list);
15408a9ff32SQu Wenruo 	spin_lock_init(&ret->list_lock);
1550bd9289cSQu Wenruo 	spin_lock_init(&ret->thres_lock);
156c3a46891SQu Wenruo 	trace_btrfs_workqueue_alloc(ret, name, flags & WQ_HIGHPRI);
15708a9ff32SQu Wenruo 	return ret;
15808a9ff32SQu Wenruo }
15908a9ff32SQu Wenruo 
1601ca08976SQu Wenruo static inline void
161d458b054SQu Wenruo __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
1621ca08976SQu Wenruo 
163cb001095SJeff Mahoney struct btrfs_workqueue *btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info,
164cb001095SJeff Mahoney 					      const char *name,
1656f011058SDavid Sterba 					      unsigned int flags,
166c6dd6ea5SQu Wenruo 					      int limit_active,
1670bd9289cSQu Wenruo 					      int thresh)
1681ca08976SQu Wenruo {
16961dd5ae6SDavid Sterba 	struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
1701ca08976SQu Wenruo 
1715d99a998SDavid Sterba 	if (!ret)
1721ca08976SQu Wenruo 		return NULL;
1731ca08976SQu Wenruo 
174cb001095SJeff Mahoney 	ret->normal = __btrfs_alloc_workqueue(fs_info, name,
175cb001095SJeff Mahoney 					      flags & ~WQ_HIGHPRI,
176c6dd6ea5SQu Wenruo 					      limit_active, thresh);
1775d99a998SDavid Sterba 	if (!ret->normal) {
1781ca08976SQu Wenruo 		kfree(ret);
1791ca08976SQu Wenruo 		return NULL;
1801ca08976SQu Wenruo 	}
1811ca08976SQu Wenruo 
1821ca08976SQu Wenruo 	if (flags & WQ_HIGHPRI) {
183cb001095SJeff Mahoney 		ret->high = __btrfs_alloc_workqueue(fs_info, name, flags,
184cb001095SJeff Mahoney 						    limit_active, thresh);
1855d99a998SDavid Sterba 		if (!ret->high) {
1861ca08976SQu Wenruo 			__btrfs_destroy_workqueue(ret->normal);
1871ca08976SQu Wenruo 			kfree(ret);
1881ca08976SQu Wenruo 			return NULL;
1891ca08976SQu Wenruo 		}
1901ca08976SQu Wenruo 	}
1911ca08976SQu Wenruo 	return ret;
1921ca08976SQu Wenruo }
1931ca08976SQu Wenruo 
1940bd9289cSQu Wenruo /*
1950bd9289cSQu Wenruo  * Hook for threshold which will be called in btrfs_queue_work.
1960bd9289cSQu Wenruo  * This hook WILL be called in IRQ handler context,
1970bd9289cSQu Wenruo  * so workqueue_set_max_active MUST NOT be called in this hook
1980bd9289cSQu Wenruo  */
199d458b054SQu Wenruo static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
2000bd9289cSQu Wenruo {
2010bd9289cSQu Wenruo 	if (wq->thresh == NO_THRESHOLD)
2020bd9289cSQu Wenruo 		return;
2030bd9289cSQu Wenruo 	atomic_inc(&wq->pending);
2040bd9289cSQu Wenruo }
2050bd9289cSQu Wenruo 
2060bd9289cSQu Wenruo /*
2070bd9289cSQu Wenruo  * Hook for threshold which will be called before executing the work,
2080bd9289cSQu Wenruo  * This hook is called in kthread content.
2090bd9289cSQu Wenruo  * So workqueue_set_max_active is called here.
2100bd9289cSQu Wenruo  */
211d458b054SQu Wenruo static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
2120bd9289cSQu Wenruo {
213c6dd6ea5SQu Wenruo 	int new_current_active;
2140bd9289cSQu Wenruo 	long pending;
2150bd9289cSQu Wenruo 	int need_change = 0;
2160bd9289cSQu Wenruo 
2170bd9289cSQu Wenruo 	if (wq->thresh == NO_THRESHOLD)
2180bd9289cSQu Wenruo 		return;
2190bd9289cSQu Wenruo 
2200bd9289cSQu Wenruo 	atomic_dec(&wq->pending);
2210bd9289cSQu Wenruo 	spin_lock(&wq->thres_lock);
2220bd9289cSQu Wenruo 	/*
2230bd9289cSQu Wenruo 	 * Use wq->count to limit the calling frequency of
2240bd9289cSQu Wenruo 	 * workqueue_set_max_active.
2250bd9289cSQu Wenruo 	 */
2260bd9289cSQu Wenruo 	wq->count++;
2270bd9289cSQu Wenruo 	wq->count %= (wq->thresh / 4);
2280bd9289cSQu Wenruo 	if (!wq->count)
2290bd9289cSQu Wenruo 		goto  out;
230c6dd6ea5SQu Wenruo 	new_current_active = wq->current_active;
2310bd9289cSQu Wenruo 
2320bd9289cSQu Wenruo 	/*
2330bd9289cSQu Wenruo 	 * pending may be changed later, but it's OK since we really
2340bd9289cSQu Wenruo 	 * don't need it so accurate to calculate new_max_active.
2350bd9289cSQu Wenruo 	 */
2360bd9289cSQu Wenruo 	pending = atomic_read(&wq->pending);
2370bd9289cSQu Wenruo 	if (pending > wq->thresh)
238c6dd6ea5SQu Wenruo 		new_current_active++;
2390bd9289cSQu Wenruo 	if (pending < wq->thresh / 2)
240c6dd6ea5SQu Wenruo 		new_current_active--;
241c6dd6ea5SQu Wenruo 	new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
242c6dd6ea5SQu Wenruo 	if (new_current_active != wq->current_active)  {
2430bd9289cSQu Wenruo 		need_change = 1;
244c6dd6ea5SQu Wenruo 		wq->current_active = new_current_active;
2450bd9289cSQu Wenruo 	}
2460bd9289cSQu Wenruo out:
2470bd9289cSQu Wenruo 	spin_unlock(&wq->thres_lock);
2480bd9289cSQu Wenruo 
2490bd9289cSQu Wenruo 	if (need_change) {
250c6dd6ea5SQu Wenruo 		workqueue_set_max_active(wq->normal_wq, wq->current_active);
2510bd9289cSQu Wenruo 	}
2520bd9289cSQu Wenruo }
2530bd9289cSQu Wenruo 
254d458b054SQu Wenruo static void run_ordered_work(struct __btrfs_workqueue *wq)
25508a9ff32SQu Wenruo {
25608a9ff32SQu Wenruo 	struct list_head *list = &wq->ordered_list;
257d458b054SQu Wenruo 	struct btrfs_work *work;
25808a9ff32SQu Wenruo 	spinlock_t *lock = &wq->list_lock;
25908a9ff32SQu Wenruo 	unsigned long flags;
26008a9ff32SQu Wenruo 
26108a9ff32SQu Wenruo 	while (1) {
26208a9ff32SQu Wenruo 		spin_lock_irqsave(lock, flags);
26308a9ff32SQu Wenruo 		if (list_empty(list))
26408a9ff32SQu Wenruo 			break;
265d458b054SQu Wenruo 		work = list_entry(list->next, struct btrfs_work,
26608a9ff32SQu Wenruo 				  ordered_list);
26708a9ff32SQu Wenruo 		if (!test_bit(WORK_DONE_BIT, &work->flags))
26808a9ff32SQu Wenruo 			break;
26908a9ff32SQu Wenruo 
27008a9ff32SQu Wenruo 		/*
27108a9ff32SQu Wenruo 		 * we are going to call the ordered done function, but
27208a9ff32SQu Wenruo 		 * we leave the work item on the list as a barrier so
27308a9ff32SQu Wenruo 		 * that later work items that are done don't have their
27408a9ff32SQu Wenruo 		 * functions called before this one returns
27508a9ff32SQu Wenruo 		 */
27608a9ff32SQu Wenruo 		if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
27708a9ff32SQu Wenruo 			break;
27852483bc2SQu Wenruo 		trace_btrfs_ordered_sched(work);
27908a9ff32SQu Wenruo 		spin_unlock_irqrestore(lock, flags);
28008a9ff32SQu Wenruo 		work->ordered_func(work);
28108a9ff32SQu Wenruo 
28208a9ff32SQu Wenruo 		/* now take the lock again and drop our item from the list */
28308a9ff32SQu Wenruo 		spin_lock_irqsave(lock, flags);
28408a9ff32SQu Wenruo 		list_del(&work->ordered_list);
28508a9ff32SQu Wenruo 		spin_unlock_irqrestore(lock, flags);
28608a9ff32SQu Wenruo 
28708a9ff32SQu Wenruo 		/*
28808a9ff32SQu Wenruo 		 * we don't want to call the ordered free functions
28908a9ff32SQu Wenruo 		 * with the lock held though
29008a9ff32SQu Wenruo 		 */
29108a9ff32SQu Wenruo 		work->ordered_free(work);
29252483bc2SQu Wenruo 		trace_btrfs_all_work_done(work);
29308a9ff32SQu Wenruo 	}
29408a9ff32SQu Wenruo 	spin_unlock_irqrestore(lock, flags);
29508a9ff32SQu Wenruo }
29608a9ff32SQu Wenruo 
2979e0af237SLiu Bo static void normal_work_helper(struct btrfs_work *work)
29808a9ff32SQu Wenruo {
299d458b054SQu Wenruo 	struct __btrfs_workqueue *wq;
30008a9ff32SQu Wenruo 	int need_order = 0;
30108a9ff32SQu Wenruo 
30208a9ff32SQu Wenruo 	/*
30308a9ff32SQu Wenruo 	 * We should not touch things inside work in the following cases:
30408a9ff32SQu Wenruo 	 * 1) after work->func() if it has no ordered_free
30508a9ff32SQu Wenruo 	 *    Since the struct is freed in work->func().
30608a9ff32SQu Wenruo 	 * 2) after setting WORK_DONE_BIT
30708a9ff32SQu Wenruo 	 *    The work may be freed in other threads almost instantly.
30808a9ff32SQu Wenruo 	 * So we save the needed things here.
30908a9ff32SQu Wenruo 	 */
31008a9ff32SQu Wenruo 	if (work->ordered_func)
31108a9ff32SQu Wenruo 		need_order = 1;
31208a9ff32SQu Wenruo 	wq = work->wq;
31308a9ff32SQu Wenruo 
31452483bc2SQu Wenruo 	trace_btrfs_work_sched(work);
3150bd9289cSQu Wenruo 	thresh_exec_hook(wq);
31608a9ff32SQu Wenruo 	work->func(work);
31708a9ff32SQu Wenruo 	if (need_order) {
31808a9ff32SQu Wenruo 		set_bit(WORK_DONE_BIT, &work->flags);
31908a9ff32SQu Wenruo 		run_ordered_work(wq);
32008a9ff32SQu Wenruo 	}
32152483bc2SQu Wenruo 	if (!need_order)
32252483bc2SQu Wenruo 		trace_btrfs_all_work_done(work);
32308a9ff32SQu Wenruo }
32408a9ff32SQu Wenruo 
3259e0af237SLiu Bo void btrfs_init_work(struct btrfs_work *work, btrfs_work_func_t uniq_func,
3266db8914fSQu Wenruo 		     btrfs_func_t func,
3276db8914fSQu Wenruo 		     btrfs_func_t ordered_func,
3286db8914fSQu Wenruo 		     btrfs_func_t ordered_free)
32908a9ff32SQu Wenruo {
33008a9ff32SQu Wenruo 	work->func = func;
33108a9ff32SQu Wenruo 	work->ordered_func = ordered_func;
33208a9ff32SQu Wenruo 	work->ordered_free = ordered_free;
3339e0af237SLiu Bo 	INIT_WORK(&work->normal_work, uniq_func);
33408a9ff32SQu Wenruo 	INIT_LIST_HEAD(&work->ordered_list);
33508a9ff32SQu Wenruo 	work->flags = 0;
33608a9ff32SQu Wenruo }
33708a9ff32SQu Wenruo 
338d458b054SQu Wenruo static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
339d458b054SQu Wenruo 				      struct btrfs_work *work)
34008a9ff32SQu Wenruo {
34108a9ff32SQu Wenruo 	unsigned long flags;
34208a9ff32SQu Wenruo 
34308a9ff32SQu Wenruo 	work->wq = wq;
3440bd9289cSQu Wenruo 	thresh_queue_hook(wq);
34508a9ff32SQu Wenruo 	if (work->ordered_func) {
34608a9ff32SQu Wenruo 		spin_lock_irqsave(&wq->list_lock, flags);
34708a9ff32SQu Wenruo 		list_add_tail(&work->ordered_list, &wq->ordered_list);
34808a9ff32SQu Wenruo 		spin_unlock_irqrestore(&wq->list_lock, flags);
34908a9ff32SQu Wenruo 	}
35052483bc2SQu Wenruo 	trace_btrfs_work_queued(work);
3510a95b851SQu Wenruo 	queue_work(wq->normal_wq, &work->normal_work);
35208a9ff32SQu Wenruo }
35308a9ff32SQu Wenruo 
354d458b054SQu Wenruo void btrfs_queue_work(struct btrfs_workqueue *wq,
355d458b054SQu Wenruo 		      struct btrfs_work *work)
3561ca08976SQu Wenruo {
357d458b054SQu Wenruo 	struct __btrfs_workqueue *dest_wq;
3581ca08976SQu Wenruo 
3591ca08976SQu Wenruo 	if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
3601ca08976SQu Wenruo 		dest_wq = wq->high;
3611ca08976SQu Wenruo 	else
3621ca08976SQu Wenruo 		dest_wq = wq->normal;
3631ca08976SQu Wenruo 	__btrfs_queue_work(dest_wq, work);
3641ca08976SQu Wenruo }
3651ca08976SQu Wenruo 
3661ca08976SQu Wenruo static inline void
367d458b054SQu Wenruo __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
36808a9ff32SQu Wenruo {
36908a9ff32SQu Wenruo 	destroy_workqueue(wq->normal_wq);
370c3a46891SQu Wenruo 	trace_btrfs_workqueue_destroy(wq);
37108a9ff32SQu Wenruo 	kfree(wq);
37208a9ff32SQu Wenruo }
37308a9ff32SQu Wenruo 
374d458b054SQu Wenruo void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
3751ca08976SQu Wenruo {
3761ca08976SQu Wenruo 	if (!wq)
3771ca08976SQu Wenruo 		return;
3781ca08976SQu Wenruo 	if (wq->high)
3791ca08976SQu Wenruo 		__btrfs_destroy_workqueue(wq->high);
3801ca08976SQu Wenruo 	__btrfs_destroy_workqueue(wq->normal);
381ef66af10SFilipe Manana 	kfree(wq);
3821ca08976SQu Wenruo }
3831ca08976SQu Wenruo 
384c6dd6ea5SQu Wenruo void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
38508a9ff32SQu Wenruo {
386800ee224SSergei Trofimovich 	if (!wq)
387800ee224SSergei Trofimovich 		return;
388c6dd6ea5SQu Wenruo 	wq->normal->limit_active = limit_active;
3891ca08976SQu Wenruo 	if (wq->high)
390c6dd6ea5SQu Wenruo 		wq->high->limit_active = limit_active;
3911ca08976SQu Wenruo }
3921ca08976SQu Wenruo 
393d458b054SQu Wenruo void btrfs_set_work_high_priority(struct btrfs_work *work)
3941ca08976SQu Wenruo {
3951ca08976SQu Wenruo 	set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
39608a9ff32SQu Wenruo }
397