2 * Copyright (C) 2014 Fujitsu. All rights reserved.
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.
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.
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.
20 * Unlike inode.c in kernel, which can use most of the kernel infrastructure
21 * like inode/dentry things, in user-land, we can only use inode number to
22 * do directly operation on extent buffer, which may cause extra searching,
23 * but should not be a huge problem since progs is less performance sensitive.
28 #include "transaction.h"
34 * Find a free inode index for later btrfs_add_link().
35 * Currently just search from the largest dir_index and +1.
37 static int btrfs_find_free_dir_index(struct btrfs_root *root, u64 dir_ino,
40 struct btrfs_path *path;
42 struct btrfs_key found_key;
49 path = btrfs_alloc_path();
53 key.objectid = dir_ino;
54 key.type = BTRFS_DIR_INDEX_KEY;
57 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
61 if (path->slots[0] == 0) {
62 ret = btrfs_prev_leaf(root, path);
67 * This shouldn't happen since there must be a leaf
68 * containing the DIR_ITEM.
69 * Can only happen when the previous leaf is corrupted.
77 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
78 if (found_key.objectid != dir_ino ||
79 found_key.type != BTRFS_DIR_INDEX_KEY)
81 ret_val = found_key.offset + 1;
83 btrfs_free_path(path);
89 /* Check the dir_item/index conflicts before insert */
90 int check_dir_conflict(struct btrfs_root *root, char *name, int namelen,
93 struct btrfs_path *path;
95 struct btrfs_inode_item *inode_item;
96 struct btrfs_dir_item *dir_item;
99 path = btrfs_alloc_path();
103 /* Given dir exists? */
105 key.type = BTRFS_INODE_ITEM_KEY;
107 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
116 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
117 struct btrfs_inode_item);
118 if (!(btrfs_inode_mode(path->nodes[0], inode_item) & S_IFDIR)) {
122 btrfs_release_path(path);
124 /* Name conflicting? */
125 dir_item = btrfs_lookup_dir_item(NULL, root, path, dir, name,
127 if (IS_ERR(dir_item)) {
128 ret = PTR_ERR(dir_item);
135 btrfs_release_path(path);
137 /* Index conflicting? */
138 dir_item = btrfs_lookup_dir_index(NULL, root, path, dir, name,
140 if (IS_ERR(dir_item) && PTR_ERR(dir_item) == -ENOENT)
142 if (IS_ERR(dir_item)) {
143 ret = PTR_ERR(dir_item);
152 btrfs_free_path(path);
157 * Add dir_item/index for 'parent_ino' if add_backref is true, also insert a
158 * backref from the ino to parent dir and update the nlink(Kernel version does
161 * Currently only supports adding link from an inode to another inode.
163 int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
164 u64 ino, u64 parent_ino, char *name, int namelen,
165 u8 type, u64 *index, int add_backref)
167 struct btrfs_path *path;
168 struct btrfs_key key;
169 struct btrfs_inode_item *inode_item;
175 path = btrfs_alloc_path();
179 if (index && *index) {
182 ret = btrfs_find_free_dir_index(root, parent_ino, &ret_index);
187 ret = check_dir_conflict(root, name, namelen, parent_ino, ret_index);
193 ret = btrfs_insert_inode_ref(trans, root, name, namelen,
194 ino, parent_ino, ret_index);
198 /* Update nlinks for the inode */
200 key.type = BTRFS_INODE_ITEM_KEY;
202 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
208 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
209 struct btrfs_inode_item);
210 nlink = btrfs_inode_nlink(path->nodes[0], inode_item);
212 btrfs_set_inode_nlink(path->nodes[0], inode_item, nlink);
213 btrfs_mark_buffer_dirty(path->nodes[0]);
214 btrfs_release_path(path);
217 /* Add dir_item and dir_index */
219 key.type = BTRFS_INODE_ITEM_KEY;
221 ret = btrfs_insert_dir_item(trans, root, name, namelen, parent_ino,
222 &key, type, ret_index);
226 /* Update inode size of the parent inode */
227 key.objectid = parent_ino;
228 key.type = BTRFS_INODE_ITEM_KEY;
230 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
233 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
234 struct btrfs_inode_item);
235 inode_size = btrfs_inode_size(path->nodes[0], inode_item);
236 inode_size += namelen * 2;
237 btrfs_set_inode_size(path->nodes[0], inode_item, inode_size);
238 btrfs_mark_buffer_dirty(path->nodes[0]);
239 btrfs_release_path(path);
242 btrfs_free_path(path);
243 if (ret == 0 && index)
248 int btrfs_add_orphan_item(struct btrfs_trans_handle *trans,
249 struct btrfs_root *root, struct btrfs_path *path,
252 struct btrfs_key key;
254 key.objectid = BTRFS_ORPHAN_OBJECTID;
255 key.type = BTRFS_ORPHAN_ITEM_KEY;
258 return btrfs_insert_empty_item(trans, root, path, &key, 0);
262 * Unlink an inode, which will remove its backref and corresponding dir_index/
263 * dir_item if any of them exists.
265 * If an inode's nlink is reduced to 0 and 'add_orphan' is true, it will be
266 * added to orphan inode and waiting to be deleted by next kernel mount.
268 int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root,
269 u64 ino, u64 parent_ino, u64 index, const char *name,
270 int namelen, int add_orphan)
272 struct btrfs_path *path;
273 struct btrfs_key key;
274 struct btrfs_inode_item *inode_item;
275 struct btrfs_inode_ref *inode_ref;
276 struct btrfs_dir_item *dir_item;
279 int del_inode_ref = 0;
280 int del_dir_item = 0;
281 int del_dir_index = 0;
284 path = btrfs_alloc_path();
288 /* check the ref and backref exists */
289 inode_ref = btrfs_lookup_inode_ref(trans, root, path, name, namelen,
291 if (IS_ERR(inode_ref)) {
292 ret = PTR_ERR(inode_ref);
297 btrfs_release_path(path);
299 dir_item = btrfs_lookup_dir_item(NULL, root, path, parent_ino,
301 if (IS_ERR(dir_item)) {
302 ret = PTR_ERR(dir_item);
307 btrfs_release_path(path);
309 dir_item = btrfs_lookup_dir_index(NULL, root, path, parent_ino,
310 name, namelen, index, 0);
312 * Since lookup_dir_index() will return -ENOENT when not found,
313 * we need to do extra check.
315 if (IS_ERR(dir_item) && PTR_ERR(dir_item) == -ENOENT)
317 if (IS_ERR(dir_item)) {
318 ret = PTR_ERR(dir_item);
323 btrfs_release_path(path);
325 if (!del_inode_ref && !del_dir_item && !del_dir_index) {
326 /* All not found, shouldn't happen */
332 /* Only decrease nlink when deleting inode_ref */
334 key.type = BTRFS_INODE_ITEM_KEY;
336 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
342 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
343 struct btrfs_inode_item);
344 nlinks = btrfs_inode_nlink(path->nodes[0], inode_item);
347 btrfs_set_inode_nlink(path->nodes[0], inode_item, nlinks);
348 btrfs_mark_buffer_dirty(path->nodes[0]);
349 btrfs_release_path(path);
351 /* For nlinks == 0, add it to orphan list if needed */
352 if (nlinks == 0 && add_orphan) {
353 ret = btrfs_add_orphan_item(trans, root, path, ino);
356 btrfs_mark_buffer_dirty(path->nodes[0]);
357 btrfs_release_path(path);
360 ret = btrfs_del_inode_ref(trans, root, name, namelen, ino,
367 dir_item = btrfs_lookup_dir_index(trans, root, path,
368 parent_ino, name, namelen,
370 if (IS_ERR(dir_item)) {
371 ret = PTR_ERR(dir_item);
378 ret = btrfs_delete_one_dir_name(trans, root, path, dir_item);
381 btrfs_release_path(path);
383 /* Update inode size of the parent inode */
384 key.objectid = parent_ino;
385 key.type = BTRFS_INODE_ITEM_KEY;
387 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
390 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
391 struct btrfs_inode_item);
392 inode_size = btrfs_inode_size(path->nodes[0], inode_item);
393 if (inode_size >= namelen)
394 inode_size -= namelen;
395 btrfs_set_inode_size(path->nodes[0], inode_item, inode_size);
396 btrfs_mark_buffer_dirty(path->nodes[0]);
397 btrfs_release_path(path);
401 dir_item = btrfs_lookup_dir_item(trans, root, path, parent_ino,
403 if (IS_ERR(dir_item)) {
404 ret = PTR_ERR(dir_item);
411 ret = btrfs_delete_one_dir_name(trans, root, path, dir_item);
414 btrfs_release_path(path);
416 /* Update inode size of the parent inode */
417 key.objectid = parent_ino;
418 key.type = BTRFS_INODE_ITEM_KEY;
420 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
423 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
424 struct btrfs_inode_item);
425 inode_size = btrfs_inode_size(path->nodes[0], inode_item);
426 if (inode_size >= namelen)
427 inode_size -= namelen;
428 btrfs_set_inode_size(path->nodes[0], inode_item, inode_size);
429 btrfs_mark_buffer_dirty(path->nodes[0]);
430 btrfs_release_path(path);
434 btrfs_free_path(path);
438 /* Fill inode item with 'mode'. Uid/gid to root/root */
439 static void fill_inode_item(struct btrfs_trans_handle *trans,
440 struct btrfs_inode_item *inode_item,
443 time_t now = time(NULL);
445 btrfs_set_stack_inode_generation(inode_item, trans->transid);
446 btrfs_set_stack_inode_uid(inode_item, 0);
447 btrfs_set_stack_inode_gid(inode_item, 0);
448 btrfs_set_stack_inode_size(inode_item, 0);
449 btrfs_set_stack_inode_mode(inode_item, mode);
450 btrfs_set_stack_inode_nlink(inode_item, nlink);
451 btrfs_set_stack_timespec_sec(&inode_item->atime, now);
452 btrfs_set_stack_timespec_nsec(&inode_item->atime, 0);
453 btrfs_set_stack_timespec_sec(&inode_item->mtime, now);
454 btrfs_set_stack_timespec_nsec(&inode_item->mtime, 0);
455 btrfs_set_stack_timespec_sec(&inode_item->ctime, now);
456 btrfs_set_stack_timespec_nsec(&inode_item->ctime, 0);
460 * Unlike kernel btrfs_new_inode(), we only create the INODE_ITEM, without
462 * The backref is added by btrfs_add_link().
464 int btrfs_new_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root,
467 struct btrfs_inode_item inode_item = {0};
470 fill_inode_item(trans, &inode_item, mode, 0);
471 ret = btrfs_insert_inode(trans, root, ino, &inode_item);
476 * Change inode flags to given value
478 int btrfs_change_inode_flags(struct btrfs_trans_handle *trans,
479 struct btrfs_root *root, u64 ino, u64 flags)
481 struct btrfs_inode_item *item;
482 struct btrfs_path *path;
483 struct btrfs_key key;
486 path = btrfs_alloc_path();
491 key.type = BTRFS_INODE_ITEM_KEY;
494 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
502 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
503 struct btrfs_inode_item);
504 btrfs_set_inode_flags(path->nodes[0], item, flags);
505 btrfs_mark_buffer_dirty(path->nodes[0]);
507 btrfs_free_path(path);
512 * Make a dir under the parent inode 'parent_ino' with 'name'
513 * and 'mode', The owner will be root/root.
515 int btrfs_mkdir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
516 char *name, int namelen, u64 parent_ino, u64 *ino, int mode)
518 struct btrfs_dir_item *dir_item;
519 struct btrfs_path *path;
523 path = btrfs_alloc_path();
530 dir_item = btrfs_lookup_dir_item(NULL, root, path, parent_ino,
532 if (IS_ERR(dir_item)) {
533 ret = PTR_ERR(dir_item);
538 struct btrfs_key found_key;
541 * Already have conflicting name, check if it is a dir.
542 * Either way, no need to continue.
544 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &found_key);
545 ret_ino = found_key.objectid;
546 if (btrfs_dir_type(path->nodes[0], dir_item) != BTRFS_FT_DIR)
553 * This is *UNSAFE* if some leaf is corrupted,
554 * only used as a fallback method. Caller should either
555 * ensure the fs is OK or pass ino with unused inode number.
557 ret = btrfs_find_free_objectid(NULL, root, parent_ino,
561 ret = btrfs_new_inode(trans, root, ret_ino, mode | S_IFDIR);
564 ret = btrfs_add_link(trans, root, ret_ino, parent_ino, name, namelen,
565 BTRFS_FT_DIR, NULL, 1);
569 btrfs_free_path(path);
575 struct btrfs_root *btrfs_mksubvol(struct btrfs_root *root,
576 const char *base, u64 root_objectid)
578 struct btrfs_trans_handle *trans;
579 struct btrfs_fs_info *fs_info = root->fs_info;
580 struct btrfs_root *tree_root = fs_info->tree_root;
581 struct btrfs_root *new_root = NULL;
582 struct btrfs_path path;
583 struct btrfs_inode_item *inode_item;
584 struct extent_buffer *leaf;
585 struct btrfs_key key;
586 u64 dirid = btrfs_root_dirid(&root->root_item);
588 char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
594 if (len == 0 || len > BTRFS_NAME_LEN)
597 btrfs_init_path(&path);
598 key.objectid = dirid;
599 key.type = BTRFS_DIR_INDEX_KEY;
600 key.offset = (u64)-1;
602 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
604 error("search for DIR_INDEX dirid %llu failed: %d",
605 (unsigned long long)dirid, ret);
609 if (path.slots[0] > 0) {
611 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
612 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
613 index = key.offset + 1;
615 btrfs_release_path(&path);
617 trans = btrfs_start_transaction(root, 1);
619 error("unable to start transaction");
623 key.objectid = dirid;
625 key.type = BTRFS_INODE_ITEM_KEY;
627 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
629 error("search for INODE_ITEM %llu failed: %d",
630 (unsigned long long)dirid, ret);
633 leaf = path.nodes[0];
634 inode_item = btrfs_item_ptr(leaf, path.slots[0],
635 struct btrfs_inode_item);
637 key.objectid = root_objectid;
638 key.offset = (u64)-1;
639 key.type = BTRFS_ROOT_ITEM_KEY;
641 memcpy(buf, base, len);
642 for (i = 0; i < 1024; i++) {
643 ret = btrfs_insert_dir_item(trans, root, buf, len,
644 dirid, &key, BTRFS_FT_DIR, index);
647 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
648 if (len < 1 || len > BTRFS_NAME_LEN) {
656 btrfs_set_inode_size(leaf, inode_item, len * 2 +
657 btrfs_inode_size(leaf, inode_item));
658 btrfs_mark_buffer_dirty(leaf);
659 btrfs_release_path(&path);
661 /* add the backref first */
662 ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
663 BTRFS_ROOT_BACKREF_KEY,
664 root->root_key.objectid,
665 dirid, index, buf, len);
667 error("unable to add root backref for %llu: %d",
668 root->root_key.objectid, ret);
672 /* now add the forward ref */
673 ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
674 BTRFS_ROOT_REF_KEY, root_objectid,
675 dirid, index, buf, len);
677 error("unable to add root ref for %llu: %d",
678 root->root_key.objectid, ret);
682 ret = btrfs_commit_transaction(trans, root);
684 error("transaction commit failed: %d", ret);
688 new_root = btrfs_read_fs_root(fs_info, &key);
689 if (IS_ERR(new_root)) {
690 error("unable to fs read root: %lu", PTR_ERR(new_root));
694 btrfs_init_path(&path);