| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 1 | /* | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 2 |  * klist.c - Routines for manipulating klists. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 3 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 4 |  * Copyright (C) 2005 Patrick Mochel | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 5 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 6 |  * This file is released under the GPL v2. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 7 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 8 |  * This klist interface provides a couple of structures that wrap around | 
 | 9 |  * struct list_head to provide explicit list "head" (struct klist) and list | 
 | 10 |  * "node" (struct klist_node) objects. For struct klist, a spinlock is | 
 | 11 |  * included that protects access to the actual list itself. struct | 
 | 12 |  * klist_node provides a pointer to the klist that owns it and a kref | 
 | 13 |  * reference count that indicates the number of current users of that node | 
 | 14 |  * in the list. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 15 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 16 |  * The entire point is to provide an interface for iterating over a list | 
 | 17 |  * that is safe and allows for modification of the list during the | 
 | 18 |  * iteration (e.g. insertion and removal), including modification of the | 
 | 19 |  * current node on the list. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 20 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 21 |  * It works using a 3rd object type - struct klist_iter - that is declared | 
 | 22 |  * and initialized before an iteration. klist_next() is used to acquire the | 
 | 23 |  * next element in the list. It returns NULL if there are no more items. | 
 | 24 |  * Internally, that routine takes the klist's lock, decrements the | 
 | 25 |  * reference count of the previous klist_node and increments the count of | 
 | 26 |  * the next klist_node. It then drops the lock and returns. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 27 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 28 |  * There are primitives for adding and removing nodes to/from a klist. | 
 | 29 |  * When deleting, klist_del() will simply decrement the reference count. | 
 | 30 |  * Only when the count goes to 0 is the node removed from the list. | 
 | 31 |  * klist_remove() will try to delete the node from the list and block until | 
 | 32 |  * it is actually removed. This is useful for objects (like devices) that | 
 | 33 |  * have been removed from the system and must be freed (but must wait until | 
 | 34 |  * all accessors have finished). | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 35 |  */ | 
 | 36 |  | 
 | 37 | #include <linux/klist.h> | 
 | 38 | #include <linux/module.h> | 
 | 39 |  | 
 | 40 |  | 
 | 41 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 42 |  * klist_init - Initialize a klist structure. | 
 | 43 |  * @k: The klist we're initializing. | 
 | 44 |  * @get: The get function for the embedding object (NULL if none) | 
 | 45 |  * @put: The put function for the embedding object (NULL if none) | 
| James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 46 |  * | 
 | 47 |  * Initialises the klist structure.  If the klist_node structures are | 
 | 48 |  * going to be embedded in refcounted objects (necessary for safe | 
 | 49 |  * deletion) then the get/put arguments are used to initialise | 
 | 50 |  * functions that take and release references on the embedding | 
 | 51 |  * objects. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 52 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 53 | void klist_init(struct klist *k, void (*get)(struct klist_node *), | 
| James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 54 | 		void (*put)(struct klist_node *)) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 55 | { | 
 | 56 | 	INIT_LIST_HEAD(&k->k_list); | 
 | 57 | 	spin_lock_init(&k->k_lock); | 
| James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 58 | 	k->get = get; | 
 | 59 | 	k->put = put; | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 60 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 61 | EXPORT_SYMBOL_GPL(klist_init); | 
 | 62 |  | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 63 | static void add_head(struct klist *k, struct klist_node *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 64 | { | 
 | 65 | 	spin_lock(&k->k_lock); | 
 | 66 | 	list_add(&n->n_node, &k->k_list); | 
 | 67 | 	spin_unlock(&k->k_lock); | 
 | 68 | } | 
 | 69 |  | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 70 | static void add_tail(struct klist *k, struct klist_node *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 71 | { | 
 | 72 | 	spin_lock(&k->k_lock); | 
 | 73 | 	list_add_tail(&n->n_node, &k->k_list); | 
 | 74 | 	spin_unlock(&k->k_lock); | 
 | 75 | } | 
 | 76 |  | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 77 | static void klist_node_init(struct klist *k, struct klist_node *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 78 | { | 
 | 79 | 	INIT_LIST_HEAD(&n->n_node); | 
 | 80 | 	init_completion(&n->n_removed); | 
 | 81 | 	kref_init(&n->n_ref); | 
 | 82 | 	n->n_klist = k; | 
| James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 83 | 	if (k->get) | 
 | 84 | 		k->get(n); | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 85 | } | 
 | 86 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 87 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 88 |  * klist_add_head - Initialize a klist_node and add it to front. | 
 | 89 |  * @n: node we're adding. | 
 | 90 |  * @k: klist it's going on. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 91 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 92 | void klist_add_head(struct klist_node *n, struct klist *k) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 93 | { | 
 | 94 | 	klist_node_init(k, n); | 
 | 95 | 	add_head(k, n); | 
 | 96 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 97 | EXPORT_SYMBOL_GPL(klist_add_head); | 
 | 98 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 99 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 100 |  * klist_add_tail - Initialize a klist_node and add it to back. | 
 | 101 |  * @n: node we're adding. | 
 | 102 |  * @k: klist it's going on. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 103 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 104 | void klist_add_tail(struct klist_node *n, struct klist *k) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 105 | { | 
 | 106 | 	klist_node_init(k, n); | 
 | 107 | 	add_tail(k, n); | 
 | 108 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 109 | EXPORT_SYMBOL_GPL(klist_add_tail); | 
 | 110 |  | 
| Tejun Heo | 93dd400 | 2008-04-22 18:58:46 +0900 | [diff] [blame] | 111 | /** | 
 | 112 |  * klist_add_after - Init a klist_node and add it after an existing node | 
 | 113 |  * @n: node we're adding. | 
 | 114 |  * @pos: node to put @n after | 
 | 115 |  */ | 
 | 116 | void klist_add_after(struct klist_node *n, struct klist_node *pos) | 
 | 117 | { | 
 | 118 | 	struct klist *k = pos->n_klist; | 
 | 119 |  | 
 | 120 | 	klist_node_init(k, n); | 
 | 121 | 	spin_lock(&k->k_lock); | 
 | 122 | 	list_add(&n->n_node, &pos->n_node); | 
 | 123 | 	spin_unlock(&k->k_lock); | 
 | 124 | } | 
 | 125 | EXPORT_SYMBOL_GPL(klist_add_after); | 
 | 126 |  | 
 | 127 | /** | 
 | 128 |  * klist_add_before - Init a klist_node and add it before an existing node | 
 | 129 |  * @n: node we're adding. | 
 | 130 |  * @pos: node to put @n after | 
 | 131 |  */ | 
 | 132 | void klist_add_before(struct klist_node *n, struct klist_node *pos) | 
 | 133 | { | 
 | 134 | 	struct klist *k = pos->n_klist; | 
 | 135 |  | 
 | 136 | 	klist_node_init(k, n); | 
 | 137 | 	spin_lock(&k->k_lock); | 
 | 138 | 	list_add_tail(&n->n_node, &pos->n_node); | 
 | 139 | 	spin_unlock(&k->k_lock); | 
 | 140 | } | 
 | 141 | EXPORT_SYMBOL_GPL(klist_add_before); | 
 | 142 |  | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 143 | static void klist_release(struct kref *kref) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 144 | { | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 145 | 	struct klist_node *n = container_of(kref, struct klist_node, n_ref); | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 146 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 147 | 	list_del(&n->n_node); | 
 | 148 | 	complete(&n->n_removed); | 
| mochel@digitalimplant.org | 8b0c250 | 2005-03-24 12:58:57 -0800 | [diff] [blame] | 149 | 	n->n_klist = NULL; | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 150 | } | 
 | 151 |  | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 152 | static int klist_dec_and_del(struct klist_node *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 153 | { | 
 | 154 | 	return kref_put(&n->n_ref, klist_release); | 
 | 155 | } | 
 | 156 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 157 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 158 |  * klist_del - Decrement the reference count of node and try to remove. | 
 | 159 |  * @n: node we're deleting. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 160 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 161 | void klist_del(struct klist_node *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 162 | { | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 163 | 	struct klist *k = n->n_klist; | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 164 | 	void (*put)(struct klist_node *) = k->put; | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 165 |  | 
 | 166 | 	spin_lock(&k->k_lock); | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 167 | 	if (!klist_dec_and_del(n)) | 
 | 168 | 		put = NULL; | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 169 | 	spin_unlock(&k->k_lock); | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 170 | 	if (put) | 
 | 171 | 		put(n); | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 172 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 173 | EXPORT_SYMBOL_GPL(klist_del); | 
 | 174 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 175 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 176 |  * klist_remove - Decrement the refcount of node and wait for it to go away. | 
 | 177 |  * @n: node we're removing. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 178 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 179 | void klist_remove(struct klist_node *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 180 | { | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 181 | 	klist_del(n); | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 182 | 	wait_for_completion(&n->n_removed); | 
 | 183 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 184 | EXPORT_SYMBOL_GPL(klist_remove); | 
 | 185 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 186 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 187 |  * klist_node_attached - Say whether a node is bound to a list or not. | 
 | 188 |  * @n: Node that we're testing. | 
| mochel@digitalimplant.org | 8b0c250 | 2005-03-24 12:58:57 -0800 | [diff] [blame] | 189 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 190 | int klist_node_attached(struct klist_node *n) | 
| mochel@digitalimplant.org | 8b0c250 | 2005-03-24 12:58:57 -0800 | [diff] [blame] | 191 | { | 
 | 192 | 	return (n->n_klist != NULL); | 
 | 193 | } | 
| mochel@digitalimplant.org | 8b0c250 | 2005-03-24 12:58:57 -0800 | [diff] [blame] | 194 | EXPORT_SYMBOL_GPL(klist_node_attached); | 
 | 195 |  | 
| mochel@digitalimplant.org | 8b0c250 | 2005-03-24 12:58:57 -0800 | [diff] [blame] | 196 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 197 |  * klist_iter_init_node - Initialize a klist_iter structure. | 
 | 198 |  * @k: klist we're iterating. | 
 | 199 |  * @i: klist_iter we're filling. | 
 | 200 |  * @n: node to start with. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 201 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 202 |  * Similar to klist_iter_init(), but starts the action off with @n, | 
 | 203 |  * instead of with the list head. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 204 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 205 | void klist_iter_init_node(struct klist *k, struct klist_iter *i, | 
 | 206 | 			  struct klist_node *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 207 | { | 
 | 208 | 	i->i_klist = k; | 
 | 209 | 	i->i_head = &k->k_list; | 
 | 210 | 	i->i_cur = n; | 
| Frank Pavlic | e22dafb | 2005-11-26 20:48:40 -0800 | [diff] [blame] | 211 | 	if (n) | 
 | 212 | 		kref_get(&n->n_ref); | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 213 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 214 | EXPORT_SYMBOL_GPL(klist_iter_init_node); | 
 | 215 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 216 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 217 |  * klist_iter_init - Iniitalize a klist_iter structure. | 
 | 218 |  * @k: klist we're iterating. | 
 | 219 |  * @i: klist_iter structure we're filling. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 220 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 221 |  * Similar to klist_iter_init_node(), but start with the list head. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 222 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 223 | void klist_iter_init(struct klist *k, struct klist_iter *i) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 224 | { | 
 | 225 | 	klist_iter_init_node(k, i, NULL); | 
 | 226 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(klist_iter_init); | 
 | 228 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 229 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 230 |  * klist_iter_exit - Finish a list iteration. | 
 | 231 |  * @i: Iterator structure. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 232 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 233 |  * Must be called when done iterating over list, as it decrements the | 
 | 234 |  * refcount of the current node. Necessary in case iteration exited before | 
 | 235 |  * the end of the list was reached, and always good form. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 236 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 237 | void klist_iter_exit(struct klist_iter *i) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 238 | { | 
 | 239 | 	if (i->i_cur) { | 
 | 240 | 		klist_del(i->i_cur); | 
 | 241 | 		i->i_cur = NULL; | 
 | 242 | 	} | 
 | 243 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(klist_iter_exit); | 
 | 245 |  | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 246 | static struct klist_node *to_klist_node(struct list_head *n) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 247 | { | 
 | 248 | 	return container_of(n, struct klist_node, n_node); | 
 | 249 | } | 
 | 250 |  | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 251 | /** | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 252 |  * klist_next - Ante up next node in list. | 
 | 253 |  * @i: Iterator structure. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 254 |  * | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 255 |  * First grab list lock. Decrement the reference count of the previous | 
 | 256 |  * node, if there was one. Grab the next node, increment its reference | 
 | 257 |  * count, drop the lock, and return that next node. | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 258 |  */ | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 259 | struct klist_node *klist_next(struct klist_iter *i) | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 260 | { | 
| Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 261 | 	struct list_head *next; | 
 | 262 | 	struct klist_node *lnode = i->i_cur; | 
 | 263 | 	struct klist_node *knode = NULL; | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 264 | 	void (*put)(struct klist_node *) = i->i_klist->put; | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 265 |  | 
 | 266 | 	spin_lock(&i->i_klist->k_lock); | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 267 | 	if (lnode) { | 
 | 268 | 		next = lnode->n_node.next; | 
 | 269 | 		if (!klist_dec_and_del(lnode)) | 
 | 270 | 			put = NULL; | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 271 | 	} else | 
 | 272 | 		next = i->i_head->next; | 
 | 273 |  | 
 | 274 | 	if (next != i->i_head) { | 
 | 275 | 		knode = to_klist_node(next); | 
 | 276 | 		kref_get(&knode->n_ref); | 
 | 277 | 	} | 
 | 278 | 	i->i_cur = knode; | 
 | 279 | 	spin_unlock(&i->i_klist->k_lock); | 
| Alan Stern | 7e9f4b2 | 2006-09-18 16:28:06 -0400 | [diff] [blame] | 280 | 	if (put && lnode) | 
 | 281 | 		put(lnode); | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 282 | 	return knode; | 
 | 283 | } | 
| mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(klist_next); |