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