blob: 60a750678a82b335ab7fd0e9432288d013c52e64 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason62e27492007-03-15 12:56:47 -040019#include "ctree.h"
20#include "disk-io.h"
21#include "hash.h"
Chris Masone089f052007-03-16 16:20:31 -040022#include "transaction.h"
Chris Mason62e27492007-03-15 12:56:47 -040023
Chris Masond352ac62008-09-29 15:18:18 -040024/*
25 * insert a name into a directory, doing overflow properly if there is a hash
26 * collision. data_size indicates how big the item inserted should be. On
27 * success a struct btrfs_dir_item pointer is returned, otherwise it is
28 * an ERR_PTR.
29 *
30 * The name is not copied into the dir item, you have to do that yourself.
31 */
Chris Mason35b7e472007-05-02 15:53:43 -040032static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
33 *trans,
34 struct btrfs_root *root,
35 struct btrfs_path *path,
36 struct btrfs_key *cpu_key,
Chris Masone06afa82007-05-23 15:44:28 -040037 u32 data_size,
38 const char *name,
39 int name_len)
Chris Mason7fcde0e2007-04-05 12:13:21 -040040{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040041 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7fcde0e2007-04-05 12:13:21 -040042 int ret;
Chris Mason7e381802007-04-19 15:36:27 -040043 char *ptr;
44 struct btrfs_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040045 struct extent_buffer *leaf;
Chris Mason7fcde0e2007-04-05 12:13:21 -040046
47 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason7e381802007-04-19 15:36:27 -040048 if (ret == -EEXIST) {
Chris Masone06afa82007-05-23 15:44:28 -040049 struct btrfs_dir_item *di;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040050 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
Chris Masone06afa82007-05-23 15:44:28 -040051 if (di)
52 return ERR_PTR(-EEXIST);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040053 btrfs_extend_item(fs_info, path, data_size);
Jeff Mahoney143bede2012-03-01 14:56:26 +010054 } else if (ret < 0)
Chris Mason54aa1f42007-06-22 14:16:25 -040055 return ERR_PTR(ret);
Chris Mason7e381802007-04-19 15:36:27 -040056 WARN_ON(ret > 0);
Chris Mason5f39d392007-10-15 16:14:19 -040057 leaf = path->nodes[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +010058 item = btrfs_item_nr(path->slots[0]);
Chris Mason7e381802007-04-19 15:36:27 -040059 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
Chris Mason5f39d392007-10-15 16:14:19 -040060 BUG_ON(data_size > btrfs_item_size(leaf, item));
61 ptr += btrfs_item_size(leaf, item) - data_size;
Chris Mason7e381802007-04-19 15:36:27 -040062 return (struct btrfs_dir_item *)ptr;
Chris Mason7fcde0e2007-04-05 12:13:21 -040063}
64
Chris Masond352ac62008-09-29 15:18:18 -040065/*
66 * xattrs work a lot like directories, this inserts an xattr item
67 * into the tree
68 */
Josef Bacik5103e942007-11-16 11:45:54 -050069int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
Yan, Zhengf34f57a2009-11-12 09:35:27 +000070 struct btrfs_root *root,
71 struct btrfs_path *path, u64 objectid,
72 const char *name, u16 name_len,
73 const void *data, u16 data_len)
Josef Bacik5103e942007-11-16 11:45:54 -050074{
75 int ret = 0;
Josef Bacik5103e942007-11-16 11:45:54 -050076 struct btrfs_dir_item *dir_item;
77 unsigned long name_ptr, data_ptr;
78 struct btrfs_key key, location;
79 struct btrfs_disk_key disk_key;
80 struct extent_buffer *leaf;
81 u32 data_size;
82
David Sterbab9d04c62017-02-17 19:42:43 +010083 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
84 return -ENOSPC;
Yan, Zhengf34f57a2009-11-12 09:35:27 +000085
86 key.objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +020087 key.type = BTRFS_XATTR_ITEM_KEY;
David Millerdf68b8a2008-02-15 10:40:52 -050088 key.offset = btrfs_name_hash(name, name_len);
Josef Bacik5103e942007-11-16 11:45:54 -050089
90 data_size = sizeof(*dir_item) + name_len + data_len;
91 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
92 name, name_len);
Josef Bacikfa092002011-05-27 12:06:11 -040093 if (IS_ERR(dir_item))
94 return PTR_ERR(dir_item);
Josef Bacik5103e942007-11-16 11:45:54 -050095 memset(&location, 0, sizeof(location));
96
97 leaf = path->nodes[0];
98 btrfs_cpu_key_to_disk(&disk_key, &location);
99 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
100 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
101 btrfs_set_dir_name_len(leaf, dir_item, name_len);
Chris Masone02119d2008-09-05 16:13:11 -0400102 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
Josef Bacik5103e942007-11-16 11:45:54 -0500103 btrfs_set_dir_data_len(leaf, dir_item, data_len);
104 name_ptr = (unsigned long)(dir_item + 1);
105 data_ptr = (unsigned long)((char *)name_ptr + name_len);
106
107 write_extent_buffer(leaf, name, name_ptr, name_len);
108 write_extent_buffer(leaf, data, data_ptr, data_len);
109 btrfs_mark_buffer_dirty(path->nodes[0]);
110
Josef Bacik5103e942007-11-16 11:45:54 -0500111 return ret;
112}
113
Chris Masond352ac62008-09-29 15:18:18 -0400114/*
115 * insert a directory item in the tree, doing all the magic for
116 * both indexes. 'dir' indicates which objectid to insert it into,
117 * 'location' is the key to stuff into the directory item, 'type' is the
118 * type of the inode we're pointing to, and 'index' is the sequence number
119 * to use for the second index (if one is created).
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100120 * Will return 0 or -ENOMEM
Chris Masond352ac62008-09-29 15:18:18 -0400121 */
Chris Masone089f052007-03-16 16:20:31 -0400122int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Miao Xie16cdcec2011-04-22 18:12:22 +0800123 *root, const char *name, int name_len,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200124 struct btrfs_inode *dir, struct btrfs_key *location,
Miao Xie16cdcec2011-04-22 18:12:22 +0800125 u8 type, u64 index)
Chris Mason62e27492007-03-15 12:56:47 -0400126{
127 int ret = 0;
Chris Masone06afa82007-05-23 15:44:28 -0400128 int ret2 = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400129 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -0400130 struct btrfs_dir_item *dir_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400131 struct extent_buffer *leaf;
132 unsigned long name_ptr;
Chris Mason62e27492007-03-15 12:56:47 -0400133 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400134 struct btrfs_disk_key disk_key;
Chris Mason62e27492007-03-15 12:56:47 -0400135 u32 data_size;
136
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200137 key.objectid = btrfs_ino(dir);
David Sterba962a2982014-06-04 18:41:45 +0200138 key.type = BTRFS_DIR_ITEM_KEY;
David Millerdf68b8a2008-02-15 10:40:52 -0500139 key.offset = btrfs_name_hash(name, name_len);
Chris Masonb9473432009-03-13 11:00:37 -0400140
Chris Mason5caf2a02007-04-02 11:20:42 -0400141 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +0800142 if (!path)
143 return -ENOMEM;
Chris Masonb9473432009-03-13 11:00:37 -0400144 path->leave_spinning = 1;
145
Miao Xie16cdcec2011-04-22 18:12:22 +0800146 btrfs_cpu_key_to_disk(&disk_key, location);
147
Chris Mason62e27492007-03-15 12:56:47 -0400148 data_size = sizeof(*dir_item) + name_len;
Chris Masone06afa82007-05-23 15:44:28 -0400149 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
150 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400151 if (IS_ERR(dir_item)) {
152 ret = PTR_ERR(dir_item);
Chris Masone06afa82007-05-23 15:44:28 -0400153 if (ret == -EEXIST)
154 goto second_insert;
Tsutomu Itohc2db1072011-03-01 06:48:31 +0000155 goto out_free;
Chris Mason7e381802007-04-19 15:36:27 -0400156 }
Chris Mason62e27492007-03-15 12:56:47 -0400157
Chris Mason5f39d392007-10-15 16:14:19 -0400158 leaf = path->nodes[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400159 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
160 btrfs_set_dir_type(leaf, dir_item, type);
Josef Bacik5103e942007-11-16 11:45:54 -0500161 btrfs_set_dir_data_len(leaf, dir_item, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400162 btrfs_set_dir_name_len(leaf, dir_item, name_len);
Chris Masone02119d2008-09-05 16:13:11 -0400163 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -0400164 name_ptr = (unsigned long)(dir_item + 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400165
Chris Mason5f39d392007-10-15 16:14:19 -0400166 write_extent_buffer(leaf, name, name_ptr, name_len);
167 btrfs_mark_buffer_dirty(leaf);
Chris Mason7e381802007-04-19 15:36:27 -0400168
Chris Masone06afa82007-05-23 15:44:28 -0400169second_insert:
Chris Mason7e381802007-04-19 15:36:27 -0400170 /* FIXME, use some real flag for selecting the extra index */
171 if (root == root->fs_info->tree_root) {
172 ret = 0;
Tsutomu Itohc2db1072011-03-01 06:48:31 +0000173 goto out_free;
Chris Mason7e381802007-04-19 15:36:27 -0400174 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200175 btrfs_release_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400176
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400177 ret2 = btrfs_insert_delayed_dir_index(trans, root->fs_info, name,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200178 name_len, dir, &disk_key, type, index);
Tsutomu Itohc2db1072011-03-01 06:48:31 +0000179out_free:
Chris Mason5caf2a02007-04-02 11:20:42 -0400180 btrfs_free_path(path);
Chris Masone06afa82007-05-23 15:44:28 -0400181 if (ret)
182 return ret;
183 if (ret2)
184 return ret2;
185 return 0;
Chris Mason62e27492007-03-15 12:56:47 -0400186}
187
Chris Masond352ac62008-09-29 15:18:18 -0400188/*
189 * lookup a directory item based on name. 'dir' is the objectid
190 * we're searching in, and 'mod' tells us if you plan on deleting the
191 * item (use mod < 0) or changing the options (use mod > 0)
192 */
Chris Mason7e381802007-04-19 15:36:27 -0400193struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
194 struct btrfs_root *root,
195 struct btrfs_path *path, u64 dir,
196 const char *name, int name_len,
197 int mod)
Chris Mason62e27492007-03-15 12:56:47 -0400198{
Chris Mason1d4f6402007-03-15 15:18:43 -0400199 int ret;
Chris Mason62e27492007-03-15 12:56:47 -0400200 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -0400201 int ins_len = mod < 0 ? -1 : 0;
202 int cow = mod != 0;
Chris Mason62e27492007-03-15 12:56:47 -0400203
204 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200205 key.type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -0400206
David Millerdf68b8a2008-02-15 10:40:52 -0500207 key.offset = btrfs_name_hash(name, name_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400208
Chris Mason7e381802007-04-19 15:36:27 -0400209 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
210 if (ret < 0)
211 return ERR_PTR(ret);
Li Zefan85d85a72011-07-14 03:17:52 +0000212 if (ret > 0)
Chris Mason7e381802007-04-19 15:36:27 -0400213 return NULL;
214
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400215 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
Chris Mason62e27492007-03-15 12:56:47 -0400216}
217
Chris Mason9c520572012-12-17 14:26:57 -0500218int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
219 const char *name, int name_len)
220{
221 int ret;
222 struct btrfs_key key;
223 struct btrfs_dir_item *di;
224 int data_size;
225 struct extent_buffer *leaf;
226 int slot;
227 struct btrfs_path *path;
228
229
230 path = btrfs_alloc_path();
231 if (!path)
232 return -ENOMEM;
233
234 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200235 key.type = BTRFS_DIR_ITEM_KEY;
Chris Mason9c520572012-12-17 14:26:57 -0500236 key.offset = btrfs_name_hash(name, name_len);
237
238 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
239
240 /* return back any errors */
241 if (ret < 0)
242 goto out;
243
244 /* nothing found, we're safe */
245 if (ret > 0) {
246 ret = 0;
247 goto out;
248 }
249
250 /* we found an item, look for our name in the item */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400251 di = btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
Chris Mason9c520572012-12-17 14:26:57 -0500252 if (di) {
253 /* our exact name was found */
254 ret = -EEXIST;
255 goto out;
256 }
257
258 /*
259 * see if there is room in the item to insert this
260 * name
261 */
Filipe David Borba Manana878f2d22013-11-27 16:06:16 +0000262 data_size = sizeof(*di) + name_len;
Chris Mason9c520572012-12-17 14:26:57 -0500263 leaf = path->nodes[0];
264 slot = path->slots[0];
265 if (data_size + btrfs_item_size_nr(leaf, slot) +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400266 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
Chris Mason9c520572012-12-17 14:26:57 -0500267 ret = -EOVERFLOW;
268 } else {
269 /* plenty of insertion room */
270 ret = 0;
271 }
272out:
273 btrfs_free_path(path);
274 return ret;
275}
276
Chris Masond352ac62008-09-29 15:18:18 -0400277/*
278 * lookup a directory item based on index. 'dir' is the objectid
279 * we're searching in, and 'mod' tells us if you plan on deleting the
280 * item (use mod < 0) or changing the options (use mod > 0)
281 *
282 * The name is used to make sure the index really points to the name you were
283 * looking for.
284 */
Chris Mason7e381802007-04-19 15:36:27 -0400285struct btrfs_dir_item *
286btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
287 struct btrfs_root *root,
288 struct btrfs_path *path, u64 dir,
289 u64 objectid, const char *name, int name_len,
290 int mod)
291{
292 int ret;
293 struct btrfs_key key;
294 int ins_len = mod < 0 ? -1 : 0;
295 int cow = mod != 0;
296
297 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200298 key.type = BTRFS_DIR_INDEX_KEY;
Chris Mason7e381802007-04-19 15:36:27 -0400299 key.offset = objectid;
300
301 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
302 if (ret < 0)
303 return ERR_PTR(ret);
304 if (ret > 0)
305 return ERR_PTR(-ENOENT);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400306 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400307}
308
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400309struct btrfs_dir_item *
310btrfs_search_dir_index_item(struct btrfs_root *root,
311 struct btrfs_path *path, u64 dirid,
312 const char *name, int name_len)
313{
314 struct extent_buffer *leaf;
315 struct btrfs_dir_item *di;
316 struct btrfs_key key;
317 u32 nritems;
318 int ret;
319
320 key.objectid = dirid;
321 key.type = BTRFS_DIR_INDEX_KEY;
322 key.offset = 0;
323
324 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
325 if (ret < 0)
326 return ERR_PTR(ret);
327
328 leaf = path->nodes[0];
329 nritems = btrfs_header_nritems(leaf);
330
331 while (1) {
332 if (path->slots[0] >= nritems) {
333 ret = btrfs_next_leaf(root, path);
334 if (ret < 0)
335 return ERR_PTR(ret);
336 if (ret > 0)
337 break;
338 leaf = path->nodes[0];
339 nritems = btrfs_header_nritems(leaf);
340 continue;
341 }
342
343 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
344 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
345 break;
346
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400347 di = btrfs_match_dir_item_name(root->fs_info, path,
348 name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400349 if (di)
350 return di;
351
352 path->slots[0]++;
353 }
354 return NULL;
355}
356
Josef Bacik5103e942007-11-16 11:45:54 -0500357struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root,
359 struct btrfs_path *path, u64 dir,
360 const char *name, u16 name_len,
361 int mod)
362{
363 int ret;
364 struct btrfs_key key;
365 int ins_len = mod < 0 ? -1 : 0;
366 int cow = mod != 0;
Josef Bacik5103e942007-11-16 11:45:54 -0500367
368 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200369 key.type = BTRFS_XATTR_ITEM_KEY;
David Millerdf68b8a2008-02-15 10:40:52 -0500370 key.offset = btrfs_name_hash(name, name_len);
Josef Bacik5103e942007-11-16 11:45:54 -0500371 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
372 if (ret < 0)
373 return ERR_PTR(ret);
Li Zefan85d85a72011-07-14 03:17:52 +0000374 if (ret > 0)
Josef Bacik5103e942007-11-16 11:45:54 -0500375 return NULL;
376
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400377 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
Josef Bacik5103e942007-11-16 11:45:54 -0500378}
379
Chris Masond352ac62008-09-29 15:18:18 -0400380/*
381 * helper function to look at the directory item pointed to by 'path'
382 * this walks through all the entries in a dir item and finds one
383 * for a specific name.
384 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400385struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000386 struct btrfs_path *path,
387 const char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -0400388{
Chris Mason62e27492007-03-15 12:56:47 -0400389 struct btrfs_dir_item *dir_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400390 unsigned long name_ptr;
Chris Mason7e381802007-04-19 15:36:27 -0400391 u32 total_len;
392 u32 cur = 0;
393 u32 this_len;
Chris Mason5f39d392007-10-15 16:14:19 -0400394 struct extent_buffer *leaf;
Chris Masona8a2ee02007-03-16 08:46:49 -0400395
Chris Mason5f39d392007-10-15 16:14:19 -0400396 leaf = path->nodes[0];
Chris Mason7e381802007-04-19 15:36:27 -0400397 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400398 if (verify_dir_item(fs_info, leaf, dir_item))
Josef Bacik22a94d42011-03-16 16:47:17 -0400399 return NULL;
400
Chris Mason5f39d392007-10-15 16:14:19 -0400401 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500402 while (cur < total_len) {
Chris Mason5f39d392007-10-15 16:14:19 -0400403 this_len = sizeof(*dir_item) +
Josef Bacik5103e942007-11-16 11:45:54 -0500404 btrfs_dir_name_len(leaf, dir_item) +
405 btrfs_dir_data_len(leaf, dir_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400406 name_ptr = (unsigned long)(dir_item + 1);
Chris Mason7e381802007-04-19 15:36:27 -0400407
Chris Mason5f39d392007-10-15 16:14:19 -0400408 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
409 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
Chris Mason7e381802007-04-19 15:36:27 -0400410 return dir_item;
411
412 cur += this_len;
413 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
414 this_len);
415 }
416 return NULL;
Chris Mason62e27492007-03-15 12:56:47 -0400417}
Chris Mason7e381802007-04-19 15:36:27 -0400418
Chris Masond352ac62008-09-29 15:18:18 -0400419/*
420 * given a pointer into a directory item, delete it. This
421 * handles items that have more than one entry in them.
422 */
Chris Mason7e381802007-04-19 15:36:27 -0400423int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
424 struct btrfs_root *root,
425 struct btrfs_path *path,
426 struct btrfs_dir_item *di)
427{
428
Chris Mason5f39d392007-10-15 16:14:19 -0400429 struct extent_buffer *leaf;
Chris Mason7e381802007-04-19 15:36:27 -0400430 u32 sub_item_len;
431 u32 item_len;
Chris Mason54aa1f42007-06-22 14:16:25 -0400432 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400433
Chris Mason5f39d392007-10-15 16:14:19 -0400434 leaf = path->nodes[0];
Josef Bacik5103e942007-11-16 11:45:54 -0500435 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
436 btrfs_dir_data_len(leaf, di);
Chris Mason5f39d392007-10-15 16:14:19 -0400437 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
438 if (sub_item_len == item_len) {
Chris Mason7e381802007-04-19 15:36:27 -0400439 ret = btrfs_del_item(trans, root, path);
Chris Mason7e381802007-04-19 15:36:27 -0400440 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400441 /* MARKER */
442 unsigned long ptr = (unsigned long)di;
443 unsigned long start;
444
445 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
446 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
Chris Mason7e381802007-04-19 15:36:27 -0400447 item_len - (ptr + sub_item_len - start));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400448 btrfs_truncate_item(root->fs_info, path,
449 item_len - sub_item_len, 1);
Chris Mason7e381802007-04-19 15:36:27 -0400450 }
Andi Kleen411fc6b2010-10-29 15:14:31 -0400451 return ret;
Chris Mason7e381802007-04-19 15:36:27 -0400452}
Josef Bacik22a94d42011-03-16 16:47:17 -0400453
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400454int verify_dir_item(struct btrfs_fs_info *fs_info,
Josef Bacik22a94d42011-03-16 16:47:17 -0400455 struct extent_buffer *leaf,
456 struct btrfs_dir_item *dir_item)
457{
458 u16 namelen = BTRFS_NAME_LEN;
459 u8 type = btrfs_dir_type(leaf, dir_item);
460
461 if (type >= BTRFS_FT_MAX) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400462 btrfs_crit(fs_info, "invalid dir item type: %d", (int)type);
Josef Bacik22a94d42011-03-16 16:47:17 -0400463 return 1;
464 }
465
466 if (type == BTRFS_FT_XATTR)
467 namelen = XATTR_NAME_MAX;
468
469 if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400470 btrfs_crit(fs_info, "invalid dir item name len: %u",
Josef Bacik22a94d42011-03-16 16:47:17 -0400471 (unsigned)btrfs_dir_data_len(leaf, dir_item));
472 return 1;
473 }
474
475 /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
Filipe David Borba Mananae46f5382013-10-28 16:28:30 +0000476 if ((btrfs_dir_data_len(leaf, dir_item) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400477 btrfs_dir_name_len(leaf, dir_item)) >
478 BTRFS_MAX_XATTR_SIZE(fs_info)) {
479 btrfs_crit(fs_info, "invalid dir item name + data len: %u + %u",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400480 (unsigned)btrfs_dir_name_len(leaf, dir_item),
481 (unsigned)btrfs_dir_data_len(leaf, dir_item));
Josef Bacik22a94d42011-03-16 16:47:17 -0400482 return 1;
483 }
484
485 return 0;
486}