btrfs-progs: add OPEN_CTREE_INVALIDATE_FST flag
[platform/upstream/btrfs-progs.git] / inode-item.c
1 /*
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
19 #include "ctree.h"
20 #include "disk-io.h"
21 #include "transaction.h"
22 #include "hash.h"
23
24 static int find_name_in_backref(struct btrfs_path *path, const char * name,
25                          int name_len, struct btrfs_inode_ref **ref_ret)
26 {
27         struct extent_buffer *leaf;
28         struct btrfs_inode_ref *ref;
29         unsigned long ptr;
30         unsigned long name_ptr;
31         u32 item_size;
32         u32 cur_offset = 0;
33         int len;
34
35         leaf = path->nodes[0];
36         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
37         ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
38         while (cur_offset < item_size) {
39                 ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
40                 len = btrfs_inode_ref_name_len(leaf, ref);
41                 name_ptr = (unsigned long)(ref + 1);
42                 cur_offset += len + sizeof(*ref);
43                 if (len != name_len)
44                         continue;
45                 if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) {
46                         *ref_ret = ref;
47                         return 1;
48                 }
49         }
50         return 0;
51 }
52
53 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
54                            struct btrfs_root *root,
55                            const char *name, int name_len,
56                            u64 inode_objectid, u64 ref_objectid, u64 index)
57 {
58         struct btrfs_path *path;
59         struct btrfs_key key;
60         struct btrfs_inode_ref *ref;
61         unsigned long ptr;
62         int ret;
63         int ins_len = name_len + sizeof(*ref);
64
65         key.objectid = inode_objectid;
66         key.offset = ref_objectid;
67         key.type = BTRFS_INODE_REF_KEY;
68
69         path = btrfs_alloc_path();
70         if (!path)
71                 return -ENOMEM;
72
73         ret = btrfs_insert_empty_item(trans, root, path, &key,
74                                       ins_len);
75         if (ret == -EEXIST) {
76                 u32 old_size;
77
78                 if (find_name_in_backref(path, name, name_len, &ref))
79                         goto out;
80
81                 old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
82                 ret = btrfs_extend_item(trans, root, path, ins_len);
83                 BUG_ON(ret);
84                 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
85                                      struct btrfs_inode_ref);
86                 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
87                 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
88                 btrfs_set_inode_ref_index(path->nodes[0], ref, index);
89                 ptr = (unsigned long)(ref + 1);
90                 ret = 0;
91         } else if (ret < 0) {
92                 if (ret == -EOVERFLOW)
93                         ret = -EMLINK;
94                 goto out;
95         } else {
96                 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
97                                      struct btrfs_inode_ref);
98                 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
99                 btrfs_set_inode_ref_index(path->nodes[0], ref, index);
100                 ptr = (unsigned long)(ref + 1);
101         }
102         write_extent_buffer(path->nodes[0], name, ptr, name_len);
103         btrfs_mark_buffer_dirty(path->nodes[0]);
104
105 out:
106         btrfs_free_path(path);
107
108         if (ret == -EMLINK) {
109                 if (btrfs_fs_incompat(root->fs_info,
110                                       BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF))
111                         ret = btrfs_insert_inode_extref(trans, root, name,
112                                                         name_len,
113                                                         inode_objectid,
114                                                         ref_objectid, index);
115         }
116         return ret;
117 }
118
119 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
120                        *root, struct btrfs_path *path,
121                        struct btrfs_key *location, int mod)
122 {
123         int ins_len = mod < 0 ? -1 : 0;
124         int cow = mod != 0;
125         int ret;
126         int slot;
127         struct extent_buffer *leaf;
128         struct btrfs_key found_key;
129
130         ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
131         if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY &&
132             location->offset == (u64)-1 && path->slots[0] != 0) {
133                 slot = path->slots[0] - 1;
134                 leaf = path->nodes[0];
135                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
136                 if (found_key.objectid == location->objectid &&
137                     found_key.type == location->type) {
138                         path->slots[0]--;
139                         return 0;
140                 }
141         }
142         return ret;
143 }
144
145 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
146                        *root, u64 objectid, struct btrfs_inode_item
147                        *inode_item)
148 {
149         int ret;
150         struct btrfs_key key;
151
152         key.objectid = objectid;
153         key.type = BTRFS_INODE_ITEM_KEY;
154         key.offset = 0;
155
156         ret = btrfs_insert_item(trans, root, &key, inode_item,
157                                 sizeof(*inode_item));
158         return ret;
159 }
160
161 struct btrfs_inode_ref *btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
162                 struct btrfs_root *root, struct btrfs_path *path,
163                 const char *name, int namelen, u64 ino, u64 parent_ino,
164                 u64 index, int ins_len)
165 {
166         struct btrfs_key key;
167         struct btrfs_inode_ref *ret_inode_ref = NULL;
168         int ret = 0;
169
170         key.objectid = ino;
171         key.type = BTRFS_INODE_REF_KEY;
172         key.offset = parent_ino;
173
174         ret = btrfs_search_slot(trans, root, &key, path, ins_len,
175                                 ins_len ? 1 : 0);
176         if (ret)
177                 goto out;
178
179         find_name_in_backref(path, name, namelen, &ret_inode_ref);
180 out:
181         if (ret < 0)
182                 return ERR_PTR(ret);
183         else
184                 return ret_inode_ref;
185 }
186
187 static int btrfs_find_name_in_ext_backref(struct btrfs_path *path,
188                 u64 parent_ino, const char *name, int namelen,
189                 struct btrfs_inode_extref **extref_ret)
190 {
191         struct extent_buffer *node;
192         struct btrfs_inode_extref *extref;
193         unsigned long ptr;
194         unsigned long name_ptr;
195         u32 item_size;
196         u32 cur_offset = 0;
197         int ref_name_len;
198         int slot;
199
200         node = path->nodes[0];
201         slot = path->slots[0];
202         item_size = btrfs_item_size_nr(node, slot);
203         ptr = btrfs_item_ptr_offset(node, slot);
204
205         /*
206          * Search all extended backrefs in this item. We're only looking
207          * through any collisions so most of the time this is just going to
208          * compare against one buffer. If all is well, we'll return success and
209          * the inode ref object.
210          */
211         while (cur_offset < item_size) {
212                 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
213                 name_ptr = (unsigned long)(&extref->name);
214                 ref_name_len = btrfs_inode_extref_name_len(node, extref);
215
216                 if (ref_name_len == namelen &&
217                     btrfs_inode_extref_parent(node, extref) == parent_ino &&
218                     (memcmp_extent_buffer(node, name, name_ptr, namelen) == 0))
219                 {
220                         if (extref_ret)
221                                 *extref_ret = extref;
222                         return 1;
223                 }
224
225                 cur_offset += ref_name_len + sizeof(*extref);
226         }
227
228         return 0;
229 }
230
231 struct btrfs_inode_extref *btrfs_lookup_inode_extref(struct btrfs_trans_handle
232                 *trans, struct btrfs_path *path, struct btrfs_root *root,
233                 u64 ino, u64 parent_ino, u64 index, const char *name,
234                 int namelen, int ins_len)
235 {
236         struct btrfs_key key;
237         struct btrfs_inode_extref *extref;
238         int ret = 0;
239
240         key.objectid = ino;
241         key.type = BTRFS_INODE_EXTREF_KEY;
242         key.offset = btrfs_extref_hash(parent_ino, name, namelen);
243
244         ret = btrfs_search_slot(trans, root, &key, path, ins_len,
245                         ins_len ? 1 : 0);
246         if (ret < 0)
247                 return ERR_PTR(ret);
248         if (ret > 0)
249                 return NULL;
250         if (!btrfs_find_name_in_ext_backref(path, parent_ino, name,
251                                             namelen, &extref))
252                 return NULL;
253
254         return extref;
255 }
256
257 int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
258                            struct btrfs_root *root,
259                            const char *name, int name_len,
260                            u64 inode_objectid, u64 ref_objectid,
261                            u64 *index)
262 {
263         struct btrfs_path *path;
264         struct btrfs_key key;
265         struct btrfs_inode_extref *extref;
266         struct extent_buffer *leaf;
267         int ret;
268         int del_len = name_len + sizeof(*extref);
269         unsigned long ptr;
270         unsigned long item_start;
271         u32 item_size;
272
273         key.objectid = inode_objectid;
274         key.type = BTRFS_INODE_EXTREF_KEY;
275         key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
276
277         path = btrfs_alloc_path();
278         if (!path)
279                 return -ENOMEM;
280
281         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
282         if (ret > 0)
283                 ret = -ENOENT;
284         if (ret < 0)
285                 goto out;
286
287         /*
288          * Sanity check - did we find the right item for this name?  This
289          * should always succeed so error here will make the FS readonly.
290          */
291         if (!btrfs_find_name_in_ext_backref(path, ref_objectid,
292                                             name, name_len, &extref)) {
293                 ret = -ENOENT;
294                 goto out;
295         }
296
297         leaf = path->nodes[0];
298         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
299         if (index)
300                 *index = btrfs_inode_extref_index(leaf, extref);
301
302         if (del_len == item_size) {
303                 /*
304                  * Common case only one ref in the item, remove the whole item.
305                  */
306                 ret = btrfs_del_item(trans, root, path);
307                 goto out;
308         }
309
310         ptr = (unsigned long)extref;
311         item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
312
313         memmove_extent_buffer(leaf, ptr, ptr + del_len,
314                               item_size - (ptr + del_len - item_start));
315
316         btrfs_truncate_item(trans, root, path, item_size - del_len, 1);
317
318 out:
319         btrfs_free_path(path);
320
321         return ret;
322 }
323
324 /*
325  * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
326  *
327  * The caller must have checked against BTRFS_LINK_MAX already.
328  */
329 int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
330                               struct btrfs_root *root,
331                               const char *name, int name_len,
332                               u64 inode_objectid, u64 ref_objectid, u64 index)
333 {
334         struct btrfs_inode_extref *extref;
335         int ret;
336         int ins_len = name_len + sizeof(*extref);
337         unsigned long ptr;
338         struct btrfs_path *path;
339         struct btrfs_key key;
340         struct extent_buffer *leaf;
341         struct btrfs_item *item;
342
343         key.objectid = inode_objectid;
344         key.type = BTRFS_INODE_EXTREF_KEY;
345         key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
346
347         path = btrfs_alloc_path();
348         if (!path)
349                 return -ENOMEM;
350
351         ret = btrfs_insert_empty_item(trans, root, path, &key,
352                                       ins_len);
353         if (ret == -EEXIST) {
354                 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
355                                                    name, name_len, NULL))
356                         goto out;
357
358                 btrfs_extend_item(trans, root, path, ins_len);
359                 ret = 0;
360         }
361
362         if (ret < 0)
363                 goto out;
364
365         leaf = path->nodes[0];
366         item = btrfs_item_nr(path->slots[0]);
367         ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
368         ptr += btrfs_item_size(leaf, item) - ins_len;
369         extref = (struct btrfs_inode_extref *)ptr;
370
371         btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len);
372         btrfs_set_inode_extref_index(path->nodes[0], extref, index);
373         btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid);
374
375         ptr = (unsigned long)&extref->name;
376         write_extent_buffer(path->nodes[0], name, ptr, name_len);
377         btrfs_mark_buffer_dirty(path->nodes[0]);
378
379 out:
380         btrfs_free_path(path);
381
382         return ret;
383 }
384
385 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
386                         struct btrfs_root *root, const char *name, int name_len,
387                         u64 ino, u64 parent_ino, u64 *index)
388 {
389         struct btrfs_path *path;
390         struct btrfs_key key;
391         struct btrfs_inode_ref *ref;
392         struct extent_buffer *leaf;
393         unsigned long ptr;
394         unsigned long item_start;
395         u32 item_size;
396         u32 sub_item_len;
397         int ret;
398         int search_ext_refs = 0;
399         int del_len = name_len + sizeof(*ref);
400
401         key.objectid = ino;
402         key.offset = parent_ino;
403         key.type = BTRFS_INODE_REF_KEY;
404
405         path = btrfs_alloc_path();
406         if (!path)
407                 return -ENOMEM;
408
409         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
410         if (ret > 0) {
411                 ret = -ENOENT;
412                 search_ext_refs = 1;
413                 goto out;
414         } else if (ret < 0) {
415                 goto out;
416         }
417         if (!find_name_in_backref(path, name, name_len, &ref)) {
418                 ret = -ENOENT;
419                 search_ext_refs = 1;
420                 goto out;
421         }
422         leaf = path->nodes[0];
423         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
424
425         if (index)
426                 *index = btrfs_inode_ref_index(leaf, ref);
427
428         if (del_len == item_size) {
429                 ret = btrfs_del_item(trans, root, path);
430                 goto out;
431         }
432         ptr = (unsigned long)ref;
433         sub_item_len = name_len + sizeof(*ref);
434         item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
435         memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
436                               item_size - (ptr + sub_item_len - item_start));
437         btrfs_truncate_item(trans, root, path, item_size - sub_item_len, 1);
438         btrfs_mark_buffer_dirty(path->nodes[0]);
439 out:
440         btrfs_free_path(path);
441
442         if (search_ext_refs &&
443             btrfs_fs_incompat(root->fs_info,
444                     BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)) {
445                 /*
446                  * No refs were found, or we could not find the name in our ref
447                  * array. Find and remove the extended inode ref then.
448                  */
449                 return btrfs_del_inode_extref(trans, root, name, name_len,
450                                               ino, parent_ino, index);
451         }
452
453         return ret;
454 }