提交 ed2ff2cb 编写于 作者: C Chris Mason 提交者: David Woodhouse

Btrfs: pretend page cache & commit code

Signed-off-by: NChris Mason <chris.mason@oracle.com>
上级 79f95c82
...@@ -250,6 +250,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path, ...@@ -250,6 +250,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
tree_block_release(root, mid_buf); tree_block_release(root, mid_buf);
/* once for the root ptr */ /* once for the root ptr */
tree_block_release(root, mid_buf); tree_block_release(root, mid_buf);
clean_tree_block(root, mid_buf);
return free_extent(root, blocknr, 1); return free_extent(root, blocknr, 1);
} }
parent = &parent_buf->node; parent = &parent_buf->node;
...@@ -280,6 +281,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path, ...@@ -280,6 +281,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
if (right->header.nritems == 0) { if (right->header.nritems == 0) {
u64 blocknr = right_buf->blocknr; u64 blocknr = right_buf->blocknr;
tree_block_release(root, right_buf); tree_block_release(root, right_buf);
clean_tree_block(root, right_buf);
right_buf = NULL; right_buf = NULL;
right = NULL; right = NULL;
wret = del_ptr(root, path, level + 1, pslot + 1); wret = del_ptr(root, path, level + 1, pslot + 1);
...@@ -291,7 +293,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path, ...@@ -291,7 +293,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
} else { } else {
memcpy(parent->keys + pslot + 1, right->keys, memcpy(parent->keys + pslot + 1, right->keys,
sizeof(struct key)); sizeof(struct key));
wret = write_tree_block(root, parent_buf); wret = dirty_tree_block(root, parent_buf);
if (wret) if (wret)
ret = wret; ret = wret;
} }
...@@ -316,6 +318,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path, ...@@ -316,6 +318,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
/* we've managed to empty the middle node, drop it */ /* we've managed to empty the middle node, drop it */
u64 blocknr = mid_buf->blocknr; u64 blocknr = mid_buf->blocknr;
tree_block_release(root, mid_buf); tree_block_release(root, mid_buf);
clean_tree_block(root, mid_buf);
mid_buf = NULL; mid_buf = NULL;
mid = NULL; mid = NULL;
wret = del_ptr(root, path, level + 1, pslot); wret = del_ptr(root, path, level + 1, pslot);
...@@ -327,7 +330,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path, ...@@ -327,7 +330,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
} else { } else {
/* update the parent key to reflect our changes */ /* update the parent key to reflect our changes */
memcpy(parent->keys + pslot, mid->keys, sizeof(struct key)); memcpy(parent->keys + pslot, mid->keys, sizeof(struct key));
wret = write_tree_block(root, parent_buf); wret = dirty_tree_block(root, parent_buf);
if (wret) if (wret)
ret = wret; ret = wret;
} }
...@@ -458,7 +461,7 @@ static int fixup_low_keys(struct ctree_root *root, ...@@ -458,7 +461,7 @@ static int fixup_low_keys(struct ctree_root *root,
break; break;
t = &path->nodes[i]->node; t = &path->nodes[i]->node;
memcpy(t->keys + tslot, key, sizeof(*key)); memcpy(t->keys + tslot, key, sizeof(*key));
wret = write_tree_block(root, path->nodes[i]); wret = dirty_tree_block(root, path->nodes[i]);
if (wret) if (wret)
ret = wret; ret = wret;
if (tslot != 0) if (tslot != 0)
...@@ -508,11 +511,11 @@ static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf, ...@@ -508,11 +511,11 @@ static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf,
src->header.nritems -= push_items; src->header.nritems -= push_items;
dst->header.nritems += push_items; dst->header.nritems += push_items;
wret = write_tree_block(root, src_buf); wret = dirty_tree_block(root, src_buf);
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
wret = write_tree_block(root, dst_buf); wret = dirty_tree_block(root, dst_buf);
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
return ret; return ret;
...@@ -566,11 +569,11 @@ static int balance_node_right(struct ctree_root *root, ...@@ -566,11 +569,11 @@ static int balance_node_right(struct ctree_root *root,
src->header.nritems -= push_items; src->header.nritems -= push_items;
dst->header.nritems += push_items; dst->header.nritems += push_items;
wret = write_tree_block(root, src_buf); wret = dirty_tree_block(root, src_buf);
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
wret = write_tree_block(root, dst_buf); wret = dirty_tree_block(root, dst_buf);
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
return ret; return ret;
...@@ -612,7 +615,7 @@ static int insert_new_root(struct ctree_root *root, ...@@ -612,7 +615,7 @@ static int insert_new_root(struct ctree_root *root,
tree_block_release(root, root->node); tree_block_release(root, root->node);
root->node = t; root->node = t;
t->count++; t->count++;
write_tree_block(root, t); dirty_tree_block(root, t);
path->nodes[level] = t; path->nodes[level] = t;
path->slots[level] = 0; path->slots[level] = 0;
return 0; return 0;
...@@ -652,7 +655,7 @@ static int insert_ptr(struct ctree_root *root, ...@@ -652,7 +655,7 @@ static int insert_ptr(struct ctree_root *root,
lower->header.nritems++; lower->header.nritems++;
if (lower->keys[1].objectid == 0) if (lower->keys[1].objectid == 0)
BUG(); BUG();
write_tree_block(root, path->nodes[level]); dirty_tree_block(root, path->nodes[level]);
return 0; return 0;
} }
...@@ -698,10 +701,10 @@ static int split_node(struct ctree_root *root, struct ctree_path *path, ...@@ -698,10 +701,10 @@ static int split_node(struct ctree_root *root, struct ctree_path *path,
c->header.nritems = mid; c->header.nritems = mid;
ret = 0; ret = 0;
wret = write_tree_block(root, t); wret = dirty_tree_block(root, t);
if (wret) if (wret)
ret = wret; ret = wret;
wret = write_tree_block(root, split_buffer); wret = dirty_tree_block(root, split_buffer);
if (wret) if (wret)
ret = wret; ret = wret;
wret = insert_ptr(root, path, split->keys, split_buffer->blocknr, wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
...@@ -815,11 +818,11 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path, ...@@ -815,11 +818,11 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
} }
left->header.nritems -= push_items; left->header.nritems -= push_items;
write_tree_block(root, left_buf); dirty_tree_block(root, left_buf);
write_tree_block(root, right_buf); dirty_tree_block(root, right_buf);
memcpy(upper->node.keys + slot + 1, memcpy(upper->node.keys + slot + 1,
&right->items[0].key, sizeof(struct key)); &right->items[0].key, sizeof(struct key));
write_tree_block(root, upper); dirty_tree_block(root, upper);
/* then fixup the leaf pointer in the path */ /* then fixup the leaf pointer in the path */
if (path->slots[0] >= left->header.nritems) { if (path->slots[0] >= left->header.nritems) {
path->slots[0] -= left->header.nritems; path->slots[0] -= left->header.nritems;
...@@ -909,10 +912,10 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path, ...@@ -909,10 +912,10 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
push_space = right->items[i].offset; push_space = right->items[i].offset;
} }
wret = write_tree_block(root, t); wret = dirty_tree_block(root, t);
if (wret) if (wret)
ret = wret; ret = wret;
wret = write_tree_block(root, right_buf); wret = dirty_tree_block(root, right_buf);
if (wret) if (wret)
ret = wret; ret = wret;
...@@ -1019,10 +1022,10 @@ static int split_leaf(struct ctree_root *root, struct ctree_path *path, ...@@ -1019,10 +1022,10 @@ static int split_leaf(struct ctree_root *root, struct ctree_path *path,
right_buffer->blocknr, path->slots[1] + 1, 1); right_buffer->blocknr, path->slots[1] + 1, 1);
if (wret) if (wret)
ret = wret; ret = wret;
wret = write_tree_block(root, right_buffer); wret = dirty_tree_block(root, right_buffer);
if (wret) if (wret)
ret = wret; ret = wret;
wret = write_tree_block(root, l_buf); wret = dirty_tree_block(root, l_buf);
if (wret) if (wret)
ret = wret; ret = wret;
...@@ -1062,12 +1065,14 @@ int insert_item(struct ctree_root *root, struct key *key, ...@@ -1062,12 +1065,14 @@ int insert_item(struct ctree_root *root, struct key *key,
ret = search_slot(root, key, &path, data_size); ret = search_slot(root, key, &path, data_size);
if (ret == 0) { if (ret == 0) {
release_path(root, &path); release_path(root, &path);
return -EEXIST; ret = -EEXIST;
} wret = commit_transaction(root);
if (ret < 0) { if (wret)
release_path(root, &path); ret = wret;
return ret; return ret;
} }
if (ret < 0)
goto out;
slot_orig = path.slots[0]; slot_orig = path.slots[0];
leaf_buf = path.nodes[0]; leaf_buf = path.nodes[0];
...@@ -1113,14 +1118,18 @@ int insert_item(struct ctree_root *root, struct key *key, ...@@ -1113,14 +1118,18 @@ int insert_item(struct ctree_root *root, struct key *key,
if (slot == 0) if (slot == 0)
ret = fixup_low_keys(root, &path, key, 1); ret = fixup_low_keys(root, &path, key, 1);
wret = write_tree_block(root, leaf_buf); wret = dirty_tree_block(root, leaf_buf);
if (wret) if (wret)
ret = wret; ret = wret;
if (leaf_free_space(leaf) < 0) if (leaf_free_space(leaf) < 0)
BUG(); BUG();
check_leaf(&path, 0); check_leaf(&path, 0);
out:
release_path(root, &path); release_path(root, &path);
wret = commit_transaction(root);
if (wret)
ret = wret;
return ret; return ret;
} }
...@@ -1160,7 +1169,7 @@ static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level, ...@@ -1160,7 +1169,7 @@ static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
if (wret) if (wret)
ret = wret; ret = wret;
} }
wret = write_tree_block(root, parent); wret = dirty_tree_block(root, parent);
if (wret) if (wret)
ret = wret; ret = wret;
return ret; return ret;
...@@ -1203,8 +1212,9 @@ int del_item(struct ctree_root *root, struct ctree_path *path) ...@@ -1203,8 +1212,9 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
if (leaf->header.nritems == 0) { if (leaf->header.nritems == 0) {
if (leaf_buf == root->node) { if (leaf_buf == root->node) {
leaf->header.flags = node_level(0); leaf->header.flags = node_level(0);
write_tree_block(root, leaf_buf); dirty_tree_block(root, leaf_buf);
} else { } else {
clean_tree_block(root, leaf_buf);
wret = del_ptr(root, path, 1, path->slots[1]); wret = del_ptr(root, path, 1, path->slots[1]);
if (wret) if (wret)
ret = wret; ret = wret;
...@@ -1220,7 +1230,7 @@ int del_item(struct ctree_root *root, struct ctree_path *path) ...@@ -1220,7 +1230,7 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
if (wret) if (wret)
ret = wret; ret = wret;
} }
wret = write_tree_block(root, leaf_buf); wret = dirty_tree_block(root, leaf_buf);
if (wret) if (wret)
ret = wret; ret = wret;
...@@ -1242,6 +1252,7 @@ int del_item(struct ctree_root *root, struct ctree_path *path) ...@@ -1242,6 +1252,7 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
} }
if (leaf->header.nritems == 0) { if (leaf->header.nritems == 0) {
u64 blocknr = leaf_buf->blocknr; u64 blocknr = leaf_buf->blocknr;
clean_tree_block(root, leaf_buf);
wret = del_ptr(root, path, 1, slot); wret = del_ptr(root, path, 1, slot);
if (wret) if (wret)
ret = wret; ret = wret;
...@@ -1254,6 +1265,9 @@ int del_item(struct ctree_root *root, struct ctree_path *path) ...@@ -1254,6 +1265,9 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
} }
} }
} }
wret = commit_transaction(root);
if (wret)
ret = wret;
return ret; return ret;
} }
......
#ifndef __CTREE__ #ifndef __CTREE__
#define __CTREE__ #define __CTREE__
#include "list.h"
#define CTREE_BLOCKSIZE 1024 #define CTREE_BLOCKSIZE 1024
/* /*
...@@ -53,6 +55,9 @@ struct ctree_root { ...@@ -53,6 +55,9 @@ struct ctree_root {
struct key current_insert; struct key current_insert;
int fp; int fp;
struct radix_tree_root cache_radix; struct radix_tree_root cache_radix;
struct list_head trans;
struct list_head cache;
int cache_size;
}; };
/* /*
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include "disk-io.h" #include "disk-io.h"
static int allocated_blocks = 0; static int allocated_blocks = 0;
int cache_size = 0;
int cache_max = 10000;
static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf) static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{ {
...@@ -21,6 +23,25 @@ static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf) ...@@ -21,6 +23,25 @@ static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
return 0; return 0;
} }
static int free_some_buffers(struct ctree_root *root)
{
struct list_head *node, *next;
struct tree_buffer *b;
if (root->cache_size < cache_max)
return 0;
list_for_each_safe(node, next, &root->cache) {
b = list_entry(node, struct tree_buffer, cache);
if (b->count == 1) {
BUG_ON(!list_empty(&b->dirty));
list_del_init(&b->cache);
tree_block_release(root, b);
if (root->cache_size < cache_max)
return 0;
}
}
return 0;
}
struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr) struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
{ {
struct tree_buffer *buf; struct tree_buffer *buf;
...@@ -30,10 +51,14 @@ struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr) ...@@ -30,10 +51,14 @@ struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
return buf; return buf;
allocated_blocks++; allocated_blocks++;
buf->blocknr = blocknr; buf->blocknr = blocknr;
buf->count = 1; buf->count = 2;
INIT_LIST_HEAD(&buf->dirty);
free_some_buffers(root);
radix_tree_preload(GFP_KERNEL); radix_tree_preload(GFP_KERNEL);
ret = radix_tree_insert(&root->cache_radix, blocknr, buf); ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
radix_tree_preload_end(); radix_tree_preload_end();
list_add_tail(&buf->cache, &root->cache);
root->cache_size++;
if (ret) { if (ret) {
free(buf); free(buf);
return NULL; return NULL;
...@@ -57,7 +82,6 @@ struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr) ...@@ -57,7 +82,6 @@ struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr)
return buf; return buf;
} }
struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr) struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
{ {
loff_t offset = blocknr * CTREE_BLOCKSIZE; loff_t offset = blocknr * CTREE_BLOCKSIZE;
...@@ -82,6 +106,24 @@ struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr) ...@@ -82,6 +106,24 @@ struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
return buf; return buf;
} }
int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
if (!list_empty(&buf->dirty))
return 0;
list_add_tail(&buf->dirty, &root->trans);
buf->count++;
return 0;
}
int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
if (!list_empty(&buf->dirty)) {
list_del_init(&buf->dirty);
tree_block_release(root, buf);
}
return 0;
}
int write_tree_block(struct ctree_root *root, struct tree_buffer *buf) int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{ {
u64 blocknr = buf->blocknr; u64 blocknr = buf->blocknr;
...@@ -96,9 +138,37 @@ int write_tree_block(struct ctree_root *root, struct tree_buffer *buf) ...@@ -96,9 +138,37 @@ int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
return 0; return 0;
} }
static int __commit_transaction(struct ctree_root *root)
{
struct tree_buffer *b;
int ret = 0;
int wret;
while(!list_empty(&root->trans)) {
b = list_entry(root->trans.next, struct tree_buffer, dirty);
list_del_init(&b->dirty);
wret = write_tree_block(root, b);
if (wret)
ret = wret;
tree_block_release(root, b);
}
return ret;
}
int commit_transaction(struct ctree_root *root)
{
int ret;
ret = __commit_transaction(root);
if (!ret && root != root->extent_root)
ret = __commit_transaction(root->extent_root);
BUG_ON(ret);
return ret;
}
static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root, static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
struct ctree_root_info *info, int fp) struct ctree_root_info *info, int fp)
{ {
INIT_LIST_HEAD(&root->trans);
INIT_LIST_HEAD(&root->cache);
root->fp = fp; root->fp = fp;
root->node = NULL; root->node = NULL;
root->node = read_tree_block(root, info->tree_root); root->node = read_tree_block(root, info->tree_root);
...@@ -157,8 +227,23 @@ int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s) ...@@ -157,8 +227,23 @@ int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s)
return 0; return 0;
} }
static int drop_cache(struct ctree_root *root)
{
while(!list_empty(&root->cache)) {
struct tree_buffer *b = list_entry(root->cache.next,
struct tree_buffer, cache);
list_del_init(&b->cache);
tree_block_release(root, b);
}
return 0;
}
int close_ctree(struct ctree_root *root) int close_ctree(struct ctree_root *root)
{ {
drop_cache(root->extent_root);
drop_cache(root);
BUG_ON(!list_empty(&root->trans));
BUG_ON(!list_empty(&root->extent_root->trans));
close(root->fp); close(root->fp);
if (root->node) if (root->node)
tree_block_release(root, root->node); tree_block_release(root, root->node);
...@@ -182,6 +267,8 @@ void tree_block_release(struct ctree_root *root, struct tree_buffer *buf) ...@@ -182,6 +267,8 @@ void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
free(buf); free(buf);
BUG_ON(allocated_blocks == 0); BUG_ON(allocated_blocks == 0);
allocated_blocks--; allocated_blocks--;
BUG_ON(root->cache_size == 0);
root->cache_size--;
} }
} }
#ifndef __DISKIO__ #ifndef __DISKIO__
#define __DISKIO__ #define __DISKIO__
#include "list.h"
struct tree_buffer { struct tree_buffer {
u64 blocknr; u64 blocknr;
...@@ -8,11 +9,16 @@ struct tree_buffer { ...@@ -8,11 +9,16 @@ struct tree_buffer {
struct node node; struct node node;
struct leaf leaf; struct leaf leaf;
}; };
struct list_head dirty;
struct list_head cache;
}; };
struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr); struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr);
struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr); struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr);
int write_tree_block(struct ctree_root *root, struct tree_buffer *buf); int write_tree_block(struct ctree_root *root, struct tree_buffer *buf);
int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf);
int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf);
int commit_transaction(struct ctree_root *root);
struct ctree_root *open_ctree(char *filename, struct ctree_super_block *s); struct ctree_root *open_ctree(char *filename, struct ctree_super_block *s);
int close_ctree(struct ctree_root *root); int close_ctree(struct ctree_root *root);
void tree_block_release(struct ctree_root *root, struct tree_buffer *buf); void tree_block_release(struct ctree_root *root, struct tree_buffer *buf);
......
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
#else
extern void list_add(struct list_head *new, struct list_head *head);
#endif
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is
* in an undefined state.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
#else
extern void list_del(struct list_head *entry);
#endif
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
* Note: if 'old' was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
}
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)
/**
* __list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* This variant differs from list_for_each() in that it's the
* simplest possible list iteration code, no prefetching is done.
* Use this for code that knows the list to be very short (empty
* or 1 entry) most of the time.
*/
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member); \
prefetch(pos->member.prev), &pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_struct within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue.
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_continue
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_from
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_reverse
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member), \
n = list_entry(pos->member.prev, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.prev, typeof(*n), member))
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册