Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/btrfs/dir-item.c
26278 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
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
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
const struct btrfs_key *cpu_key,
26
u32 data_size,
27
const char *name,
28
int name_len)
29
{
30
int ret;
31
char *ptr;
32
struct extent_buffer *leaf;
33
34
ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
35
if (ret == -EEXIST) {
36
struct btrfs_dir_item *di;
37
di = btrfs_match_dir_item_name(path, name, name_len);
38
if (di)
39
return ERR_PTR(-EEXIST);
40
btrfs_extend_item(trans, path, data_size);
41
} else if (ret < 0)
42
return ERR_PTR(ret);
43
WARN_ON(ret > 0);
44
leaf = path->nodes[0];
45
ptr = btrfs_item_ptr(leaf, path->slots[0], char);
46
ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
47
ptr += btrfs_item_size(leaf, path->slots[0]) - data_size;
48
return (struct btrfs_dir_item *)ptr;
49
}
50
51
/*
52
* xattrs work a lot like directories, this inserts an xattr item
53
* into the tree
54
*/
55
int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
56
struct btrfs_root *root,
57
struct btrfs_path *path, u64 objectid,
58
const char *name, u16 name_len,
59
const void *data, u16 data_len)
60
{
61
int ret = 0;
62
struct btrfs_dir_item *dir_item;
63
unsigned long name_ptr, data_ptr;
64
struct btrfs_key key, location;
65
struct btrfs_disk_key disk_key;
66
struct extent_buffer *leaf;
67
u32 data_size;
68
69
if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
70
return -ENOSPC;
71
72
key.objectid = objectid;
73
key.type = BTRFS_XATTR_ITEM_KEY;
74
key.offset = btrfs_name_hash(name, name_len);
75
76
data_size = sizeof(*dir_item) + name_len + data_len;
77
dir_item = insert_with_overflow(trans, root, path, &key, data_size,
78
name, name_len);
79
if (IS_ERR(dir_item))
80
return PTR_ERR(dir_item);
81
memset(&location, 0, sizeof(location));
82
83
leaf = path->nodes[0];
84
btrfs_cpu_key_to_disk(&disk_key, &location);
85
btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
86
btrfs_set_dir_flags(leaf, dir_item, BTRFS_FT_XATTR);
87
btrfs_set_dir_name_len(leaf, dir_item, name_len);
88
btrfs_set_dir_transid(leaf, dir_item, trans->transid);
89
btrfs_set_dir_data_len(leaf, dir_item, data_len);
90
name_ptr = (unsigned long)(dir_item + 1);
91
data_ptr = (unsigned long)((char *)name_ptr + name_len);
92
93
write_extent_buffer(leaf, name, name_ptr, name_len);
94
write_extent_buffer(leaf, data, data_ptr, data_len);
95
96
return ret;
97
}
98
99
/*
100
* insert a directory item in the tree, doing all the magic for
101
* both indexes. 'dir' indicates which objectid to insert it into,
102
* 'location' is the key to stuff into the directory item, 'type' is the
103
* type of the inode we're pointing to, and 'index' is the sequence number
104
* to use for the second index (if one is created).
105
* Will return 0 or -ENOMEM
106
*/
107
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
108
const struct fscrypt_str *name, struct btrfs_inode *dir,
109
const struct btrfs_key *location, u8 type, u64 index)
110
{
111
int ret = 0;
112
int ret2 = 0;
113
struct btrfs_root *root = dir->root;
114
struct btrfs_path *path;
115
struct btrfs_dir_item *dir_item;
116
struct extent_buffer *leaf;
117
unsigned long name_ptr;
118
struct btrfs_key key;
119
struct btrfs_disk_key disk_key;
120
u32 data_size;
121
122
key.objectid = btrfs_ino(dir);
123
key.type = BTRFS_DIR_ITEM_KEY;
124
key.offset = btrfs_name_hash(name->name, name->len);
125
126
path = btrfs_alloc_path();
127
if (!path)
128
return -ENOMEM;
129
130
btrfs_cpu_key_to_disk(&disk_key, location);
131
132
data_size = sizeof(*dir_item) + name->len;
133
dir_item = insert_with_overflow(trans, root, path, &key, data_size,
134
name->name, name->len);
135
if (IS_ERR(dir_item)) {
136
ret = PTR_ERR(dir_item);
137
if (ret == -EEXIST)
138
goto second_insert;
139
goto out_free;
140
}
141
142
if (IS_ENCRYPTED(&dir->vfs_inode))
143
type |= BTRFS_FT_ENCRYPTED;
144
145
leaf = path->nodes[0];
146
btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
147
btrfs_set_dir_flags(leaf, dir_item, type);
148
btrfs_set_dir_data_len(leaf, dir_item, 0);
149
btrfs_set_dir_name_len(leaf, dir_item, name->len);
150
btrfs_set_dir_transid(leaf, dir_item, trans->transid);
151
name_ptr = (unsigned long)(dir_item + 1);
152
153
write_extent_buffer(leaf, name->name, name_ptr, name->len);
154
155
second_insert:
156
/* FIXME, use some real flag for selecting the extra index */
157
if (root == root->fs_info->tree_root) {
158
ret = 0;
159
goto out_free;
160
}
161
btrfs_release_path(path);
162
163
ret2 = btrfs_insert_delayed_dir_index(trans, name->name, name->len, dir,
164
&disk_key, type, index);
165
out_free:
166
btrfs_free_path(path);
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