(n->n_klist != NULL);
269}
270EXPORT_SYMBOL_GPL(klist_node_attached);
271
272/**
273 * klist_iter_init_node - Initialize a klist_iter structure.
274 * @k: klist we're iterating.
275 * @i: klist_iter we're filling.
276 * @n: node to start with.
277 *
278 * Similar to klist_iter_init(), but starts the action off with @n,
279 * instead of with the list head.
280 */
/* 初始化迭代器节点,使用链表节点n */
281void klist_iter_init_node(struct klist *k, struct klist_iter *i,
282 struct klist_node *n)
283{
284 i->i_klist = k;
285 i->i_cur = n;
286 if (n)
287 kref_get(&n->n_ref);
288}
289EXPORT_SYMBOL_GPL(klist_iter_init_node);
290
291/**
292 * klist_iter_init - Iniitalize a klist_iter structure.
293 * @k: klist we're iterating.
294 * @i: klist_iter structure we're filling.
295 *
296 * Similar to klist_iter_init_node(), but start with the list head.
297 */
/* 初始化迭代器节点,使用链表头 */
298void klist_iter_init(struct klist *k, struct klist_iter *i)
299{
300 klist_iter_init_node(k, i, NULL);
301}
302EXPORT_SYMBOL_GPL(klist_iter_init);
303
304/**
305 * klist_iter_exit - Finish a list iteration.
306 * @i: Iterator structure.
307 *
308 * Must be called when done iterating over list, as it decrements the
309 * refcount of the current node. Necessary in case iteration exited before
310 * the end of the list was reached, and always good form.
311 */
/* 结束链表迭代,必须在结束迭代链表时调用 */
312void klist_iter_exit(struct klist_iter *i)
313{
314 if (i->i_cur) {
315 klist_put(i->i_cur, false);
316 i->i_cur = NULL;
317 }
318}
319EXPORT_SYMBOL_GPL(klist_iter_exit);
320
/* 由链表入口获取节点 */
321static struct klist_node *to_klist_node(struct list_head *n)
322{
323 return container_of(n, struct klist_node, n_node);
324}
325
326/**
327 * klist_next - Ante up next node in list.
328 * @i: Iterator structure.
329 *
330 * First grab list lock. Decrement the reference count of the previous
331 * node, if there was one. Grab the next node, increment its reference
332 * count, drop the lock, and return that next node.
333 */
/* “预下”链表中下一节点 */
334struct klist_node *klist_next(struct klist_iter *i)
335{
336 void (*put)(struct klist_node *) = i->i_klist->put;
337 struct klist_node *last = i->i_cur;
338 struct klist_node *next;
339
/* 抢占锁 */
340 spin_lock(&i->i_klist->k_lock);
341
/* 获取下一节点 */
342 if (last) {
343 next = to_klist_node(last->n_node.next);
/* 减上一节点引用次数 */
344 if (!klist_dec_and_del(last))
345 put = NULL;
346 } else
347 next = to_klist_node(i->i_klist->k_list.next);
348
349 i->i_cur = NULL;
/* 链表中有节点“没死”,增加引用次数 */
350 while (next != to_klist_node(&i->i_klist->k_list)) {
351 if (likely(!knode_dead(next))) {
352 kref_get(&next->n_ref);
353 i->i_cur = next;
354 break;
355 }
356 next = to_klist_node(next->n_node.next);
357 }
358
/* 丢弃锁 */
359 spin_unlock(&i->i_klist->k_lock);
360
361 if (put && last)
362 put(last);
363 return i->i_cur;
364}
365EXPORT_SYMBOL_GPL(k
| 评论 |
|
|