mm: task dirty accounting fix
[safe/jmp/linux-2.6] / fs / btrfs / dir-item.c
index 5247a9a..926a0b2 100644 (file)
 #include "hash.h"
 #include "transaction.h"
 
+/*
+ * insert a name into a directory, doing overflow properly if there is a hash
+ * collision.  data_size indicates how big the item inserted should be.  On
+ * success a struct btrfs_dir_item pointer is returned, otherwise it is
+ * an ERR_PTR.
+ *
+ * The name is not copied into the dir item, you have to do that yourself.
+ */
 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
                                                   *trans,
                                                   struct btrfs_root *root,
@@ -55,6 +63,10 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
        return (struct btrfs_dir_item *)ptr;
 }
 
+/*
+ * xattrs work a lot like directories, this inserts an xattr item
+ * into the tree
+ */
 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
                            struct btrfs_root *root, const char *name,
                            u16 name_len, const void *data, u16 data_len,
@@ -71,8 +83,7 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
 
        key.objectid = dir;
        btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
-       ret = btrfs_name_hash(name, name_len, &key.offset);
-       BUG_ON(ret);
+       key.offset = btrfs_name_hash(name, name_len);
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
@@ -97,6 +108,7 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
        btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
        btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
        btrfs_set_dir_name_len(leaf, dir_item, name_len);
+       btrfs_set_dir_transid(leaf, dir_item, trans->transid);
        btrfs_set_dir_data_len(leaf, dir_item, data_len);
        name_ptr = (unsigned long)(dir_item + 1);
        data_ptr = (unsigned long)((char *)name_ptr + name_len);
@@ -109,9 +121,16 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
        return ret;
 }
 
+/*
+ * insert a directory item in the tree, doing all the magic for
+ * both indexes. 'dir' indicates which objectid to insert it into,
+ * 'location' is the key to stuff into the directory item, 'type' is the
+ * type of the inode we're pointing to, and 'index' is the sequence number
+ * to use for the second index (if one is created).
+ */
 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
                          *root, const char *name, int name_len, u64 dir,
-                         struct btrfs_key *location, u8 type)
+                         struct btrfs_key *location, u8 type, u64 index)
 {
        int ret = 0;
        int ret2 = 0;
@@ -125,8 +144,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
 
        key.objectid = dir;
        btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
-       ret = btrfs_name_hash(name, name_len, &key.offset);
-       BUG_ON(ret);
+       key.offset = btrfs_name_hash(name, name_len);
        path = btrfs_alloc_path();
        data_size = sizeof(*dir_item) + name_len;
        dir_item = insert_with_overflow(trans, root, path, &key, data_size,
@@ -144,6 +162,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
        btrfs_set_dir_type(leaf, dir_item, type);
        btrfs_set_dir_data_len(leaf, dir_item, 0);
        btrfs_set_dir_name_len(leaf, dir_item, name_len);
+       btrfs_set_dir_transid(leaf, dir_item, trans->transid);
        name_ptr = (unsigned long)(dir_item + 1);
 
        write_extent_buffer(leaf, name, name_ptr, name_len);
@@ -158,7 +177,7 @@ second_insert:
        btrfs_release_path(root, path);
 
        btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
-       key.offset = location->objectid;
+       key.offset = index;
        dir_item = insert_with_overflow(trans, root, path, &key, data_size,
                                        name, name_len);
        if (IS_ERR(dir_item)) {
@@ -171,6 +190,7 @@ second_insert:
        btrfs_set_dir_type(leaf, dir_item, type);
        btrfs_set_dir_data_len(leaf, dir_item, 0);
        btrfs_set_dir_name_len(leaf, dir_item, name_len);
+       btrfs_set_dir_transid(leaf, dir_item, trans->transid);
        name_ptr = (unsigned long)(dir_item + 1);
        write_extent_buffer(leaf, name, name_ptr, name_len);
        btrfs_mark_buffer_dirty(leaf);
@@ -183,6 +203,11 @@ out:
        return 0;
 }
 
+/*
+ * lookup a directory item based on name.  'dir' is the objectid
+ * we're searching in, and 'mod' tells us if you plan on deleting the
+ * item (use mod < 0) or changing the options (use mod > 0)
+ */
 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
                                             struct btrfs_root *root,
                                             struct btrfs_path *path, u64 dir,
@@ -199,8 +224,7 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
        key.objectid = dir;
        btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
 
-       ret = btrfs_name_hash(name, name_len, &key.offset);
-       BUG_ON(ret);
+       key.offset = btrfs_name_hash(name, name_len);
 
        ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
        if (ret < 0)
@@ -222,6 +246,14 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
        return btrfs_match_dir_item_name(root, path, name, name_len);
 }
 
+/*
+ * lookup a directory item based on index.  'dir' is the objectid
+ * we're searching in, and 'mod' tells us if you plan on deleting the
+ * item (use mod < 0) or changing the options (use mod > 0)
+ *
+ * The name is used to make sure the index really points to the name you were
+ * looking for.
+ */
 struct btrfs_dir_item *
 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
                            struct btrfs_root *root,
@@ -261,8 +293,7 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
 
        key.objectid = dir;
        btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
-       ret = btrfs_name_hash(name, name_len, &key.offset);
-       BUG_ON(ret);
+       key.offset = btrfs_name_hash(name, name_len);
        ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
        if (ret < 0)
                return ERR_PTR(ret);
@@ -283,6 +314,11 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
        return btrfs_match_dir_item_name(root, path, name, name_len);
 }
 
+/*
+ * helper function to look at the directory item pointed to by 'path'
+ * this walks through all the entries in a dir item and finds one
+ * for a specific name.
+ */
 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
                              struct btrfs_path *path,
                              const char *name, int name_len)
@@ -297,7 +333,7 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
        leaf = path->nodes[0];
        dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
        total_len = btrfs_item_size_nr(leaf, path->slots[0]);
-       while(cur < total_len) {
+       while (cur < total_len) {
                this_len = sizeof(*dir_item) +
                        btrfs_dir_name_len(leaf, dir_item) +
                        btrfs_dir_data_len(leaf, dir_item);
@@ -314,6 +350,10 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
        return NULL;
 }
 
+/*
+ * given a pointer into a directory item, delete it.  This
+ * handles items that have more than one entry in them.
+ */
 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
                              struct btrfs_root *root,
                              struct btrfs_path *path,
@@ -344,4 +384,3 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
        }
        return 0;
 }
-