xref: /openbmc/linux/fs/btrfs/async-thread.h (revision 0bd9289c28c3b6a38f5a05a812afae0274674fa2)
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 #ifndef __BTRFS_ASYNC_THREAD_
218b712842SChris Mason #define __BTRFS_ASYNC_THREAD_
228b712842SChris Mason 
238b712842SChris Mason struct btrfs_worker_thread;
248b712842SChris Mason 
258b712842SChris Mason /*
268b712842SChris Mason  * This is similar to a workqueue, but it is meant to spread the operations
278b712842SChris Mason  * across all available cpus instead of just the CPU that was used to
288b712842SChris Mason  * queue the work.  There is also some batching introduced to try and
298b712842SChris Mason  * cut down on context switches.
308b712842SChris Mason  *
318b712842SChris Mason  * By default threads are added on demand up to 2 * the number of cpus.
328b712842SChris Mason  * Changing struct btrfs_workers->max_workers is one way to prevent
338b712842SChris Mason  * demand creation of kthreads.
348b712842SChris Mason  *
358b712842SChris Mason  * the basic model of these worker threads is to embed a btrfs_work
368b712842SChris Mason  * structure in your own data struct, and use container_of in a
378b712842SChris Mason  * work function to get back to your data struct.
388b712842SChris Mason  */
398b712842SChris Mason struct btrfs_work {
408b712842SChris Mason 	/*
414a69a410SChris Mason 	 * func should be set to the function you want called
428b712842SChris Mason 	 * your work struct is passed as the only arg
434a69a410SChris Mason 	 *
444a69a410SChris Mason 	 * ordered_func must be set for work sent to an ordered work queue,
454a69a410SChris Mason 	 * and it is called to complete a given work item in the same
464a69a410SChris Mason 	 * order they were sent to the queue.
478b712842SChris Mason 	 */
488b712842SChris Mason 	void (*func)(struct btrfs_work *work);
494a69a410SChris Mason 	void (*ordered_func)(struct btrfs_work *work);
504a69a410SChris Mason 	void (*ordered_free)(struct btrfs_work *work);
518b712842SChris Mason 
528b712842SChris Mason 	/*
538b712842SChris Mason 	 * flags should be set to zero.  It is used to make sure the
548b712842SChris Mason 	 * struct is only inserted once into the list.
558b712842SChris Mason 	 */
568b712842SChris Mason 	unsigned long flags;
578b712842SChris Mason 
588b712842SChris Mason 	/* don't touch these */
598b712842SChris Mason 	struct btrfs_worker_thread *worker;
608b712842SChris Mason 	struct list_head list;
614a69a410SChris Mason 	struct list_head order_list;
628b712842SChris Mason };
638b712842SChris Mason 
648b712842SChris Mason struct btrfs_workers {
658b712842SChris Mason 	/* current number of running workers */
668b712842SChris Mason 	int num_workers;
678b712842SChris Mason 
6861d92c32SChris Mason 	int num_workers_starting;
6961d92c32SChris Mason 
708b712842SChris Mason 	/* max number of workers allowed.  changed by btrfs_start_workers */
718b712842SChris Mason 	int max_workers;
728b712842SChris Mason 
7335d8ba66SChris Mason 	/* once a worker has this many requests or fewer, it is idle */
7435d8ba66SChris Mason 	int idle_thresh;
7535d8ba66SChris Mason 
764a69a410SChris Mason 	/* force completions in the order they were queued */
774a69a410SChris Mason 	int ordered;
784a69a410SChris Mason 
799042846bSChris Mason 	/* more workers required, but in an interrupt handler */
809042846bSChris Mason 	int atomic_start_pending;
819042846bSChris Mason 
829042846bSChris Mason 	/*
839042846bSChris Mason 	 * are we allowed to sleep while starting workers or are we required
8461d92c32SChris Mason 	 * to start them at a later time?  If we can't sleep, this indicates
8561d92c32SChris Mason 	 * which queue we need to use to schedule thread creation.
869042846bSChris Mason 	 */
8761d92c32SChris Mason 	struct btrfs_workers *atomic_worker_start;
889042846bSChris Mason 
89d352ac68SChris Mason 	/* list with all the work threads.  The workers on the idle thread
90d352ac68SChris Mason 	 * may be actively servicing jobs, but they haven't yet hit the
91d352ac68SChris Mason 	 * idle thresh limit above.
92d352ac68SChris Mason 	 */
938b712842SChris Mason 	struct list_head worker_list;
9435d8ba66SChris Mason 	struct list_head idle_list;
958b712842SChris Mason 
964a69a410SChris Mason 	/*
974a69a410SChris Mason 	 * when operating in ordered mode, this maintains the list
984a69a410SChris Mason 	 * of work items waiting for completion
994a69a410SChris Mason 	 */
1004a69a410SChris Mason 	struct list_head order_list;
101d313d7a3SChris Mason 	struct list_head prio_order_list;
1024a69a410SChris Mason 
1038b712842SChris Mason 	/* lock for finding the next worker thread to queue on */
1048b712842SChris Mason 	spinlock_t lock;
1055443be45SChris Mason 
1064e3f9c50SChris Mason 	/* lock for the ordered lists */
1074e3f9c50SChris Mason 	spinlock_t order_lock;
1084e3f9c50SChris Mason 
109d352ac68SChris Mason 	/* extra name for this worker, used for current->name */
1105443be45SChris Mason 	char *name;
111964fb15aSIlya Dryomov 
112964fb15aSIlya Dryomov 	int stopping;
1138b712842SChris Mason };
1148b712842SChris Mason 
1150dc3b84aSJosef Bacik void btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work);
1160dc3b84aSJosef Bacik int btrfs_start_workers(struct btrfs_workers *workers);
117143bede5SJeff Mahoney void btrfs_stop_workers(struct btrfs_workers *workers);
11861d92c32SChris Mason void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max,
11961d92c32SChris Mason 			struct btrfs_workers *async_starter);
120143bede5SJeff Mahoney void btrfs_requeue_work(struct btrfs_work *work);
121d313d7a3SChris Mason void btrfs_set_work_high_prio(struct btrfs_work *work);
12208a9ff32SQu Wenruo 
12308a9ff32SQu Wenruo struct btrfs_workqueue_struct;
1241ca08976SQu Wenruo /* Internal use only */
1251ca08976SQu Wenruo struct __btrfs_workqueue_struct;
12608a9ff32SQu Wenruo 
12708a9ff32SQu Wenruo struct btrfs_work_struct {
12808a9ff32SQu Wenruo 	void (*func)(struct btrfs_work_struct *arg);
12908a9ff32SQu Wenruo 	void (*ordered_func)(struct btrfs_work_struct *arg);
13008a9ff32SQu Wenruo 	void (*ordered_free)(struct btrfs_work_struct *arg);
13108a9ff32SQu Wenruo 
13208a9ff32SQu Wenruo 	/* Don't touch things below */
13308a9ff32SQu Wenruo 	struct work_struct normal_work;
13408a9ff32SQu Wenruo 	struct list_head ordered_list;
1351ca08976SQu Wenruo 	struct __btrfs_workqueue_struct *wq;
13608a9ff32SQu Wenruo 	unsigned long flags;
13708a9ff32SQu Wenruo };
13808a9ff32SQu Wenruo 
13908a9ff32SQu Wenruo struct btrfs_workqueue_struct *btrfs_alloc_workqueue(char *name,
14008a9ff32SQu Wenruo 						     int flags,
141*0bd9289cSQu Wenruo 						     int max_active,
142*0bd9289cSQu Wenruo 						     int thresh);
14308a9ff32SQu Wenruo void btrfs_init_work(struct btrfs_work_struct *work,
14408a9ff32SQu Wenruo 		     void (*func)(struct btrfs_work_struct *),
14508a9ff32SQu Wenruo 		     void (*ordered_func)(struct btrfs_work_struct *),
14608a9ff32SQu Wenruo 		     void (*ordered_free)(struct btrfs_work_struct *));
14708a9ff32SQu Wenruo void btrfs_queue_work(struct btrfs_workqueue_struct *wq,
14808a9ff32SQu Wenruo 		      struct btrfs_work_struct *work);
14908a9ff32SQu Wenruo void btrfs_destroy_workqueue(struct btrfs_workqueue_struct *wq);
15008a9ff32SQu Wenruo void btrfs_workqueue_set_max(struct btrfs_workqueue_struct *wq, int max);
1511ca08976SQu Wenruo void btrfs_set_work_high_priority(struct btrfs_work_struct *work);
1528b712842SChris Mason #endif
153