LCOV - code coverage report
Current view: top level - fs/btrfs - dir-item.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 202 221 91.4 %
Date: 2023-07-31 20:08:12 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (C) 2007 Oracle.  All rights reserved.
       4             :  */
       5             : 
       6             : #include "messages.h"
       7             : #include "ctree.h"
       8             : #include "disk-io.h"
       9             : #include "transaction.h"
      10             : #include "accessors.h"
      11             : #include "dir-item.h"
      12             : 
      13             : /*
      14             :  * insert a name into a directory, doing overflow properly if there is a hash
      15             :  * collision.  data_size indicates how big the item inserted should be.  On
      16             :  * success a struct btrfs_dir_item pointer is returned, otherwise it is
      17             :  * an ERR_PTR.
      18             :  *
      19             :  * The name is not copied into the dir item, you have to do that yourself.
      20             :  */
      21     3827726 : static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
      22             :                                                    *trans,
      23             :                                                    struct btrfs_root *root,
      24             :                                                    struct btrfs_path *path,
      25             :                                                    struct btrfs_key *cpu_key,
      26             :                                                    u32 data_size,
      27             :                                                    const char *name,
      28             :                                                    int name_len)
      29             : {
      30     3827726 :         struct btrfs_fs_info *fs_info = root->fs_info;
      31     3827726 :         int ret;
      32     3827726 :         char *ptr;
      33     3827726 :         struct extent_buffer *leaf;
      34             : 
      35     3827726 :         ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
      36     3828497 :         if (ret == -EEXIST) {
      37       61773 :                 struct btrfs_dir_item *di;
      38       61773 :                 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
      39       61777 :                 if (di)
      40             :                         return ERR_PTR(-EEXIST);
      41         311 :                 btrfs_extend_item(path, data_size);
      42     3766724 :         } else if (ret < 0)
      43           0 :                 return ERR_PTR(ret);
      44     3767035 :         WARN_ON(ret > 0);
      45     3767035 :         leaf = path->nodes[0];
      46     3767035 :         ptr = btrfs_item_ptr(leaf, path->slots[0], char);
      47     3766939 :         ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
      48     3766887 :         ptr += btrfs_item_size(leaf, path->slots[0]) - data_size;
      49     3766860 :         return (struct btrfs_dir_item *)ptr;
      50             : }
      51             : 
      52             : /*
      53             :  * xattrs work a lot like directories, this inserts an xattr item
      54             :  * into the tree
      55             :  */
      56      411906 : int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
      57             :                             struct btrfs_root *root,
      58             :                             struct btrfs_path *path, u64 objectid,
      59             :                             const char *name, u16 name_len,
      60             :                             const void *data, u16 data_len)
      61             : {
      62      411906 :         int ret = 0;
      63      411906 :         struct btrfs_dir_item *dir_item;
      64      411906 :         unsigned long name_ptr, data_ptr;
      65      411906 :         struct btrfs_key key, location;
      66      411906 :         struct btrfs_disk_key disk_key;
      67      411906 :         struct extent_buffer *leaf;
      68      411906 :         u32 data_size;
      69             : 
      70      411906 :         if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
      71             :                 return -ENOSPC;
      72             : 
      73      411906 :         key.objectid = objectid;
      74      411906 :         key.type = BTRFS_XATTR_ITEM_KEY;
      75      411906 :         key.offset = btrfs_name_hash(name, name_len);
      76             : 
      77      411671 :         data_size = sizeof(*dir_item) + name_len + data_len;
      78      411671 :         dir_item = insert_with_overflow(trans, root, path, &key, data_size,
      79             :                                         name, name_len);
      80      412312 :         if (IS_ERR(dir_item))
      81       61465 :                 return PTR_ERR(dir_item);
      82      350847 :         memset(&location, 0, sizeof(location));
      83             : 
      84      350847 :         leaf = path->nodes[0];
      85      350847 :         btrfs_cpu_key_to_disk(&disk_key, &location);
      86      350729 :         btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
      87      350788 :         btrfs_set_dir_flags(leaf, dir_item, BTRFS_FT_XATTR);
      88      350758 :         btrfs_set_dir_name_len(leaf, dir_item, name_len);
      89      350746 :         btrfs_set_dir_transid(leaf, dir_item, trans->transid);
      90      350704 :         btrfs_set_dir_data_len(leaf, dir_item, data_len);
      91      350713 :         name_ptr = (unsigned long)(dir_item + 1);
      92      350713 :         data_ptr = (unsigned long)((char *)name_ptr + name_len);
      93             : 
      94      350713 :         write_extent_buffer(leaf, name, name_ptr, name_len);
      95      350757 :         write_extent_buffer(leaf, data, data_ptr, data_len);
      96      350835 :         btrfs_mark_buffer_dirty(path->nodes[0]);
      97             : 
      98      350835 :         return ret;
      99             : }
     100             : 
     101             : /*
     102             :  * insert a directory item in the tree, doing all the magic for
     103             :  * both indexes. 'dir' indicates which objectid to insert it into,
     104             :  * 'location' is the key to stuff into the directory item, 'type' is the
     105             :  * type of the inode we're pointing to, and 'index' is the sequence number
     106             :  * to use for the second index (if one is created).
     107             :  * Will return 0 or -ENOMEM
     108             :  */
     109     3416330 : int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
     110             :                           const struct fscrypt_str *name, struct btrfs_inode *dir,
     111             :                           struct btrfs_key *location, u8 type, u64 index)
     112             : {
     113     3416330 :         int ret = 0;
     114     3416330 :         int ret2 = 0;
     115     3416330 :         struct btrfs_root *root = dir->root;
     116     3416330 :         struct btrfs_path *path;
     117     3416330 :         struct btrfs_dir_item *dir_item;
     118     3416330 :         struct extent_buffer *leaf;
     119     3416330 :         unsigned long name_ptr;
     120     3416330 :         struct btrfs_key key;
     121     3416330 :         struct btrfs_disk_key disk_key;
     122     3416330 :         u32 data_size;
     123             : 
     124     3416330 :         key.objectid = btrfs_ino(dir);
     125     3416330 :         key.type = BTRFS_DIR_ITEM_KEY;
     126     3416330 :         key.offset = btrfs_name_hash(name->name, name->len);
     127             : 
     128     3416322 :         path = btrfs_alloc_path();
     129     3416316 :         if (!path)
     130             :                 return -ENOMEM;
     131             : 
     132     3416316 :         btrfs_cpu_key_to_disk(&disk_key, location);
     133             : 
     134     3416298 :         data_size = sizeof(*dir_item) + name->len;
     135     3416298 :         dir_item = insert_with_overflow(trans, root, path, &key, data_size,
     136     3416298 :                                         name->name, name->len);
     137     3416172 :         if (IS_ERR(dir_item)) {
     138           0 :                 ret = PTR_ERR(dir_item);
     139           0 :                 if (ret == -EEXIST)
     140           0 :                         goto second_insert;
     141           0 :                 goto out_free;
     142             :         }
     143             : 
     144     3416172 :         if (IS_ENCRYPTED(&dir->vfs_inode))
     145           0 :                 type |= BTRFS_FT_ENCRYPTED;
     146             : 
     147     3416172 :         leaf = path->nodes[0];
     148     3416172 :         btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
     149     3416150 :         btrfs_set_dir_flags(leaf, dir_item, type);
     150     3415970 :         btrfs_set_dir_data_len(leaf, dir_item, 0);
     151     3415986 :         btrfs_set_dir_name_len(leaf, dir_item, name->len);
     152     3415973 :         btrfs_set_dir_transid(leaf, dir_item, trans->transid);
     153     3415905 :         name_ptr = (unsigned long)(dir_item + 1);
     154             : 
     155     3415905 :         write_extent_buffer(leaf, name->name, name_ptr, name->len);
     156     3416075 :         btrfs_mark_buffer_dirty(leaf);
     157             : 
     158     3416234 : second_insert:
     159             :         /* FIXME, use some real flag for selecting the extra index */
     160     3416234 :         if (root == root->fs_info->tree_root) {
     161           0 :                 ret = 0;
     162           0 :                 goto out_free;
     163             :         }
     164     3416234 :         btrfs_release_path(path);
     165             : 
     166     3416318 :         ret2 = btrfs_insert_delayed_dir_index(trans, name->name, name->len, dir,
     167             :                                               &disk_key, type, index);
     168     3416323 : out_free:
     169     3416323 :         btrfs_free_path(path);
     170     3416273 :         if (ret)
     171             :                 return ret;
     172     3416273 :         if (ret2)
     173           0 :                 return ret2;
     174             :         return 0;
     175             : }
     176             : 
     177    14267873 : static struct btrfs_dir_item *btrfs_lookup_match_dir(
     178             :                         struct btrfs_trans_handle *trans,
     179             :                         struct btrfs_root *root, struct btrfs_path *path,
     180             :                         struct btrfs_key *key, const char *name,
     181             :                         int name_len, int mod)
     182             : {
     183    14267873 :         const int ins_len = (mod < 0 ? -1 : 0);
     184    14267873 :         const int cow = (mod != 0);
     185    14267873 :         int ret;
     186             : 
     187    14267873 :         ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
     188    14286055 :         if (ret < 0)
     189           4 :                 return ERR_PTR(ret);
     190    14286051 :         if (ret > 0)
     191             :                 return ERR_PTR(-ENOENT);
     192             : 
     193     3568140 :         return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
     194             : }
     195             : 
     196             : /*
     197             :  * Lookup for a directory item by name.
     198             :  *
     199             :  * @trans:      The transaction handle to use. Can be NULL if @mod is 0.
     200             :  * @root:       The root of the target tree.
     201             :  * @path:       Path to use for the search.
     202             :  * @dir:        The inode number (objectid) of the directory.
     203             :  * @name:       The name associated to the directory entry we are looking for.
     204             :  * @name_len:   The length of the name.
     205             :  * @mod:        Used to indicate if the tree search is meant for a read only
     206             :  *              lookup, for a modification lookup or for a deletion lookup, so
     207             :  *              its value should be 0, 1 or -1, respectively.
     208             :  *
     209             :  * Returns: NULL if the dir item does not exists, an error pointer if an error
     210             :  * happened, or a pointer to a dir item if a dir item exists for the given name.
     211             :  */
     212     9028214 : struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
     213             :                                              struct btrfs_root *root,
     214             :                                              struct btrfs_path *path, u64 dir,
     215             :                                              const struct fscrypt_str *name,
     216             :                                              int mod)
     217             : {
     218     9028214 :         struct btrfs_key key;
     219     9028214 :         struct btrfs_dir_item *di;
     220             : 
     221     9028214 :         key.objectid = dir;
     222     9028214 :         key.type = BTRFS_DIR_ITEM_KEY;
     223     9028214 :         key.offset = btrfs_name_hash(name->name, name->len);
     224             : 
     225     9026630 :         di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
     226     9026630 :                                     name->len, mod);
     227     9037157 :         if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
     228     5639708 :                 return NULL;
     229             : 
     230             :         return di;
     231             : }
     232             : 
     233      457083 : int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
     234             :                                    const struct fscrypt_str *name)
     235             : {
     236      457083 :         int ret;
     237      457083 :         struct btrfs_key key;
     238      457083 :         struct btrfs_dir_item *di;
     239      457083 :         int data_size;
     240      457083 :         struct extent_buffer *leaf;
     241      457083 :         int slot;
     242      457083 :         struct btrfs_path *path;
     243             : 
     244      457083 :         path = btrfs_alloc_path();
     245      457083 :         if (!path)
     246             :                 return -ENOMEM;
     247             : 
     248      457083 :         key.objectid = dir;
     249      457083 :         key.type = BTRFS_DIR_ITEM_KEY;
     250      457083 :         key.offset = btrfs_name_hash(name->name, name->len);
     251             : 
     252      457082 :         di = btrfs_lookup_match_dir(NULL, root, path, &key, name->name,
     253      457082 :                                     name->len, 0);
     254      457087 :         if (IS_ERR(di)) {
     255      413670 :                 ret = PTR_ERR(di);
     256             :                 /* Nothing found, we're safe */
     257      413670 :                 if (ret == -ENOENT) {
     258      413670 :                         ret = 0;
     259      413670 :                         goto out;
     260             :                 }
     261             : 
     262           0 :                 if (ret < 0)
     263           0 :                         goto out;
     264             :         }
     265             : 
     266             :         /* we found an item, look for our name in the item */
     267       43417 :         if (di) {
     268             :                 /* our exact name was found */
     269       43108 :                 ret = -EEXIST;
     270       43108 :                 goto out;
     271             :         }
     272             : 
     273             :         /* See if there is room in the item to insert this name. */
     274         309 :         data_size = sizeof(*di) + name->len;
     275         309 :         leaf = path->nodes[0];
     276         309 :         slot = path->slots[0];
     277         309 :         if (data_size + btrfs_item_size(leaf, slot) +
     278         309 :             sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
     279             :                 ret = -EOVERFLOW;
     280             :         } else {
     281             :                 /* plenty of insertion room */
     282         309 :                 ret = 0;
     283             :         }
     284      457087 : out:
     285      457087 :         btrfs_free_path(path);
     286      457087 :         return ret;
     287             : }
     288             : 
     289             : /*
     290             :  * Lookup for a directory index item by name and index number.
     291             :  *
     292             :  * @trans:      The transaction handle to use. Can be NULL if @mod is 0.
     293             :  * @root:       The root of the target tree.
     294             :  * @path:       Path to use for the search.
     295             :  * @dir:        The inode number (objectid) of the directory.
     296             :  * @index:      The index number.
     297             :  * @name:       The name associated to the directory entry we are looking for.
     298             :  * @name_len:   The length of the name.
     299             :  * @mod:        Used to indicate if the tree search is meant for a read only
     300             :  *              lookup, for a modification lookup or for a deletion lookup, so
     301             :  *              its value should be 0, 1 or -1, respectively.
     302             :  *
     303             :  * Returns: NULL if the dir index item does not exists, an error pointer if an
     304             :  * error happened, or a pointer to a dir item if the dir index item exists and
     305             :  * matches the criteria (name and index number).
     306             :  */
     307             : struct btrfs_dir_item *
     308        3246 : btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
     309             :                             struct btrfs_root *root,
     310             :                             struct btrfs_path *path, u64 dir,
     311             :                             u64 index, const struct fscrypt_str *name, int mod)
     312             : {
     313        3246 :         struct btrfs_dir_item *di;
     314        3246 :         struct btrfs_key key;
     315             : 
     316        3246 :         key.objectid = dir;
     317        3246 :         key.type = BTRFS_DIR_INDEX_KEY;
     318        3246 :         key.offset = index;
     319             : 
     320        3246 :         di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
     321        3246 :                                     name->len, mod);
     322        3246 :         if (di == ERR_PTR(-ENOENT))
     323         977 :                 return NULL;
     324             : 
     325             :         return di;
     326             : }
     327             : 
     328             : struct btrfs_dir_item *
     329           4 : btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path,
     330             :                             u64 dirid, const struct fscrypt_str *name)
     331             : {
     332           4 :         struct btrfs_dir_item *di;
     333           4 :         struct btrfs_key key;
     334           4 :         int ret;
     335             : 
     336           4 :         key.objectid = dirid;
     337           4 :         key.type = BTRFS_DIR_INDEX_KEY;
     338           4 :         key.offset = 0;
     339             : 
     340           4 :         btrfs_for_each_slot(root, &key, &key, path, ret) {
     341           4 :                 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
     342             :                         break;
     343             : 
     344           4 :                 di = btrfs_match_dir_item_name(root->fs_info, path,
     345           4 :                                                name->name, name->len);
     346           4 :                 if (di)
     347           4 :                         return di;
     348             :         }
     349             :         /* Adjust return code if the key was not found in the next leaf. */
     350           0 :         if (ret > 0)
     351             :                 ret = 0;
     352             : 
     353           0 :         return ERR_PTR(ret);
     354             : }
     355             : 
     356     4778569 : struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
     357             :                                           struct btrfs_root *root,
     358             :                                           struct btrfs_path *path, u64 dir,
     359             :                                           const char *name, u16 name_len,
     360             :                                           int mod)
     361             : {
     362     4778569 :         struct btrfs_key key;
     363     4778569 :         struct btrfs_dir_item *di;
     364             : 
     365     4778569 :         key.objectid = dir;
     366     4778569 :         key.type = BTRFS_XATTR_ITEM_KEY;
     367     4778569 :         key.offset = btrfs_name_hash(name, name_len);
     368             : 
     369     4773546 :         di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
     370     4788186 :         if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
     371     4662028 :                 return NULL;
     372             : 
     373             :         return di;
     374             : }
     375             : 
     376             : /*
     377             :  * helper function to look at the directory item pointed to by 'path'
     378             :  * this walks through all the entries in a dir item and finds one
     379             :  * for a specific name.
     380             :  */
     381     3691387 : struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
     382             :                                                  struct btrfs_path *path,
     383             :                                                  const char *name, int name_len)
     384             : {
     385     3691387 :         struct btrfs_dir_item *dir_item;
     386     3691387 :         unsigned long name_ptr;
     387     3691387 :         u32 total_len;
     388     3691387 :         u32 cur = 0;
     389     3691387 :         u32 this_len;
     390     3691387 :         struct extent_buffer *leaf;
     391             : 
     392     3691387 :         leaf = path->nodes[0];
     393     3691387 :         dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
     394             : 
     395     3691357 :         total_len = btrfs_item_size(leaf, path->slots[0]);
     396     3835071 :         while (cur < total_len) {
     397     3834157 :                 this_len = sizeof(*dir_item) +
     398     3834160 :                         btrfs_dir_name_len(leaf, dir_item) +
     399     3834145 :                         btrfs_dir_data_len(leaf, dir_item);
     400     3834145 :                 name_ptr = (unsigned long)(dir_item + 1);
     401             : 
     402     7668252 :                 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
     403     3834128 :                     memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
     404     3690421 :                         return dir_item;
     405             : 
     406      143714 :                 cur += this_len;
     407      143714 :                 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
     408             :                                                      this_len);
     409             :         }
     410             :         return NULL;
     411             : }
     412             : 
     413             : /*
     414             :  * given a pointer into a directory item, delete it.  This
     415             :  * handles items that have more than one entry in them.
     416             :  */
     417     1966296 : int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
     418             :                               struct btrfs_root *root,
     419             :                               struct btrfs_path *path,
     420             :                               struct btrfs_dir_item *di)
     421             : {
     422             : 
     423     1966296 :         struct extent_buffer *leaf;
     424     1966296 :         u32 sub_item_len;
     425     1966296 :         u32 item_len;
     426     1966296 :         int ret = 0;
     427             : 
     428     1966296 :         leaf = path->nodes[0];
     429     1966296 :         sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
     430     1966297 :                 btrfs_dir_data_len(leaf, di);
     431     1966297 :         item_len = btrfs_item_size(leaf, path->slots[0]);
     432     1966297 :         if (sub_item_len == item_len) {
     433     1966297 :                 ret = btrfs_del_item(trans, root, path);
     434             :         } else {
     435             :                 /* MARKER */
     436           0 :                 unsigned long ptr = (unsigned long)di;
     437           0 :                 unsigned long start;
     438             : 
     439           0 :                 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
     440           0 :                 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
     441           0 :                         item_len - (ptr + sub_item_len - start));
     442           0 :                 btrfs_truncate_item(path, item_len - sub_item_len, 1);
     443             :         }
     444     1966289 :         return ret;
     445             : }

Generated by: LCOV version 1.14