btrfs-progs: convert: move link_subvol out of main
[platform/upstream/btrfs-progs.git] / inode.c
1 /*
2  * Copyright (C) 2014 Fujitsu.  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 /*
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.
24  */
25 #include <sys/stat.h>
26
27 #include "ctree.h"
28 #include "transaction.h"
29 #include "disk-io.h"
30 #include "time.h"
31 #include "messages.h"
32
33 /*
34  * Find a free inode index for later btrfs_add_link().
35  * Currently just search from the largest dir_index and +1.
36  */
37 static int btrfs_find_free_dir_index(struct btrfs_root *root, u64 dir_ino,
38                                      u64 *ret_ino)
39 {
40         struct btrfs_path *path;
41         struct btrfs_key key;
42         struct btrfs_key found_key;
43         u64 ret_val = 2;
44         int ret = 0;
45
46         if (!ret_ino)
47                 return 0;
48
49         path = btrfs_alloc_path();
50         if (!path)
51                 return -ENOMEM;
52
53         key.objectid = dir_ino;
54         key.type = BTRFS_DIR_INDEX_KEY;
55         key.offset = (u64)-1;
56
57         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
58         if (ret < 0)
59                 goto out;
60         ret = 0;
61         if (path->slots[0] == 0) {
62                 ret = btrfs_prev_leaf(root, path);
63                 if (ret < 0)
64                         goto out;
65                 if (ret > 0) {
66                         /*
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.
70                          */
71                         ret = -EIO;
72                         goto out;
73                 }
74         } else {
75                 path->slots[0]--;
76         }
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)
80                 goto out;
81         ret_val = found_key.offset + 1;
82 out:
83         btrfs_free_path(path);
84         if (ret == 0)
85                 *ret_ino = ret_val;
86         return ret;
87 }
88
89 /* Check the dir_item/index conflicts before insert */
90 int check_dir_conflict(struct btrfs_root *root, char *name, int namelen,
91                        u64 dir, u64 index)
92 {
93         struct btrfs_path *path;
94         struct btrfs_key key;
95         struct btrfs_inode_item *inode_item;
96         struct btrfs_dir_item *dir_item;
97         int ret = 0;
98
99         path = btrfs_alloc_path();
100         if (!path)
101                 return -ENOMEM;
102
103         /* Given dir exists? */
104         key.objectid = dir;
105         key.type = BTRFS_INODE_ITEM_KEY;
106         key.offset = 0;
107         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
108         if (ret < 0)
109                 goto out;
110         if (ret > 0) {
111                 ret = -ENOENT;
112                 goto out;
113         }
114
115         /* Is it a dir? */
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)) {
119                 ret = -ENOTDIR;
120                 goto out;
121         }
122         btrfs_release_path(path);
123
124         /* Name conflicting? */
125         dir_item = btrfs_lookup_dir_item(NULL, root, path, dir, name,
126                                          namelen, 0);
127         if (IS_ERR(dir_item)) {
128                 ret = PTR_ERR(dir_item);
129                 goto out;
130         }
131         if (dir_item) {
132                 ret = -EEXIST;
133                 goto out;
134         }
135         btrfs_release_path(path);
136
137         /* Index conflicting? */
138         dir_item = btrfs_lookup_dir_index(NULL, root, path, dir, name,
139                                           namelen, index, 0);
140         if (IS_ERR(dir_item) && PTR_ERR(dir_item) == -ENOENT)
141                 dir_item = NULL;
142         if (IS_ERR(dir_item)) {
143                 ret = PTR_ERR(dir_item);
144                 goto out;
145         }
146         if (dir_item) {
147                 ret = -EEXIST;
148                 goto out;
149         }
150
151 out:
152         btrfs_free_path(path);
153         return ret;
154 }
155
156 /*
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
159  * not do this thing)
160  *
161  * Currently only supports adding link from an inode to another inode.
162  */
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)
166 {
167         struct btrfs_path *path;
168         struct btrfs_key key;
169         struct btrfs_inode_item *inode_item;
170         u32 nlink;
171         u64 inode_size;
172         u64 ret_index = 0;
173         int ret = 0;
174
175         path = btrfs_alloc_path();
176         if (!path)
177                 return -ENOMEM;
178
179         if (index && *index) {
180                 ret_index = *index;
181         } else {
182                 ret = btrfs_find_free_dir_index(root, parent_ino, &ret_index);
183                 if (ret < 0)
184                         goto out;
185         }
186
187         ret = check_dir_conflict(root, name, namelen, parent_ino, ret_index);
188         if (ret < 0)
189                 goto out;
190
191         /* Add inode ref */
192         if (add_backref) {
193                 ret = btrfs_insert_inode_ref(trans, root, name, namelen,
194                                              ino, parent_ino, ret_index);
195                 if (ret < 0)
196                         goto out;
197
198                 /* Update nlinks for the inode */
199                 key.objectid = ino;
200                 key.type = BTRFS_INODE_ITEM_KEY;
201                 key.offset = 0;
202                 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
203                 if (ret) {
204                         if (ret > 0)
205                                 ret = -ENOENT;
206                         goto out;
207                 }
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);
211                 nlink++;
212                 btrfs_set_inode_nlink(path->nodes[0], inode_item, nlink);
213                 btrfs_mark_buffer_dirty(path->nodes[0]);
214                 btrfs_release_path(path);
215         }
216
217         /* Add dir_item and dir_index */
218         key.objectid = ino;
219         key.type = BTRFS_INODE_ITEM_KEY;
220         key.offset = 0;
221         ret = btrfs_insert_dir_item(trans, root, name, namelen, parent_ino,
222                                     &key, type, ret_index);
223         if (ret < 0)
224                 goto out;
225
226         /* Update inode size of the parent inode */
227         key.objectid = parent_ino;
228         key.type = BTRFS_INODE_ITEM_KEY;
229         key.offset = 0;
230         ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
231         if (ret)
232                 goto out;
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);
240
241 out:
242         btrfs_free_path(path);
243         if (ret == 0 && index)
244                 *index = ret_index;
245         return ret;
246 }
247
248 int btrfs_add_orphan_item(struct btrfs_trans_handle *trans,
249                           struct btrfs_root *root, struct btrfs_path *path,
250                           u64 ino)
251 {
252         struct btrfs_key key;
253
254         key.objectid = BTRFS_ORPHAN_OBJECTID;
255         key.type = BTRFS_ORPHAN_ITEM_KEY;
256         key.offset = ino;
257
258         return btrfs_insert_empty_item(trans, root, path, &key, 0);
259 }
260
261 /*
262  * Unlink an inode, which will remove its backref and corresponding dir_index/
263  * dir_item if any of them exists.
264  *
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.
267  */
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)
271 {
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;
277         u64 inode_size;
278         u32 nlinks;
279         int del_inode_ref = 0;
280         int del_dir_item = 0;
281         int del_dir_index = 0;
282         int ret = 0;
283
284         path = btrfs_alloc_path();
285         if (!path)
286                 return -ENOMEM;
287
288         /* check the ref and backref exists */
289         inode_ref = btrfs_lookup_inode_ref(trans, root, path, name, namelen,
290                                            ino, parent_ino, 0);
291         if (IS_ERR(inode_ref)) {
292                 ret = PTR_ERR(inode_ref);
293                 goto out;
294         }
295         if (inode_ref)
296                 del_inode_ref = 1;
297         btrfs_release_path(path);
298
299         dir_item = btrfs_lookup_dir_item(NULL, root, path, parent_ino,
300                                          name, namelen, 0);
301         if (IS_ERR(dir_item)) {
302                 ret = PTR_ERR(dir_item);
303                 goto out;
304         }
305         if (dir_item)
306                 del_dir_item = 1;
307         btrfs_release_path(path);
308
309         dir_item = btrfs_lookup_dir_index(NULL, root, path, parent_ino,
310                                           name, namelen, index, 0);
311         /*
312          * Since lookup_dir_index() will return -ENOENT when not found,
313          * we need to do extra check.
314          */
315         if (IS_ERR(dir_item) && PTR_ERR(dir_item) == -ENOENT)
316                 dir_item = NULL;
317         if (IS_ERR(dir_item)) {
318                 ret = PTR_ERR(dir_item);
319                 goto out;
320         }
321         if (dir_item)
322                 del_dir_index = 1;
323         btrfs_release_path(path);
324
325         if (!del_inode_ref && !del_dir_item && !del_dir_index) {
326                 /* All not found, shouldn't happen */
327                 ret = -ENOENT;
328                 goto out;
329         }
330
331         if (del_inode_ref) {
332                 /* Only decrease nlink when deleting inode_ref */
333                 key.objectid = ino;
334                 key.type = BTRFS_INODE_ITEM_KEY;
335                 key.offset = 0;
336                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
337                 if (ret) {
338                         if (ret > 0)
339                                 ret = -ENOENT;
340                         goto out;
341                 }
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);
345                 if (nlinks > 0)
346                         nlinks--;
347                 btrfs_set_inode_nlink(path->nodes[0], inode_item, nlinks);
348                 btrfs_mark_buffer_dirty(path->nodes[0]);
349                 btrfs_release_path(path);
350
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);
354                         if (ret < 0)
355                                 goto out;
356                         btrfs_mark_buffer_dirty(path->nodes[0]);
357                         btrfs_release_path(path);
358                 }
359
360                 ret = btrfs_del_inode_ref(trans, root, name, namelen, ino,
361                                           parent_ino, &index);
362                 if (ret < 0)
363                         goto out;
364         }
365
366         if (del_dir_index) {
367                 dir_item = btrfs_lookup_dir_index(trans, root, path,
368                                                   parent_ino, name, namelen,
369                                                   index, -1);
370                 if (IS_ERR(dir_item)) {
371                         ret = PTR_ERR(dir_item);
372                         goto out;
373                 }
374                 if (!dir_item) {
375                         ret = -ENOENT;
376                         goto out;
377                 }
378                 ret = btrfs_delete_one_dir_name(trans, root, path, dir_item);
379                 if (ret)
380                         goto out;
381                 btrfs_release_path(path);
382
383                 /* Update inode size of the parent inode */
384                 key.objectid = parent_ino;
385                 key.type = BTRFS_INODE_ITEM_KEY;
386                 key.offset = 0;
387                 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
388                 if (ret)
389                         goto out;
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);
398         }
399
400         if (del_dir_item) {
401                 dir_item = btrfs_lookup_dir_item(trans, root, path, parent_ino,
402                                                  name, namelen, -1);
403                 if (IS_ERR(dir_item)) {
404                         ret = PTR_ERR(dir_item);
405                         goto out;
406                 }
407                 if (!dir_item) {
408                         ret = -ENOENT;
409                         goto out;
410                 }
411                 ret = btrfs_delete_one_dir_name(trans, root, path, dir_item);
412                 if (ret < 0)
413                         goto out;
414                 btrfs_release_path(path);
415
416                 /* Update inode size of the parent inode */
417                 key.objectid = parent_ino;
418                 key.type = BTRFS_INODE_ITEM_KEY;
419                 key.offset = 0;
420                 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
421                 if (ret)
422                         goto out;
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);
431         }
432
433 out:
434         btrfs_free_path(path);
435         return ret;
436 }
437
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,
441                             u32 mode, u32 nlink)
442 {
443         time_t now = time(NULL);
444
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);
457 }
458
459 /*
460  * Unlike kernel btrfs_new_inode(), we only create the INODE_ITEM, without
461  * its backref.
462  * The backref is added by btrfs_add_link().
463  */
464 int btrfs_new_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root,
465                 u64 ino, u32 mode)
466 {
467         struct btrfs_inode_item inode_item = {0};
468         int ret = 0;
469
470         fill_inode_item(trans, &inode_item, mode, 0);
471         ret = btrfs_insert_inode(trans, root, ino, &inode_item);
472         return ret;
473 }
474
475 /*
476  * Change inode flags to given value
477  */
478 int btrfs_change_inode_flags(struct btrfs_trans_handle *trans,
479                              struct btrfs_root *root, u64 ino, u64 flags)
480 {
481         struct btrfs_inode_item *item;
482         struct btrfs_path *path;
483         struct btrfs_key key;
484         int ret;
485
486         path = btrfs_alloc_path();
487         if (!path)
488                 return -ENOMEM;
489
490         key.objectid = ino;
491         key.type = BTRFS_INODE_ITEM_KEY;
492         key.offset = 0;
493
494         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
495         if (ret > 0) {
496                 ret = -ENOENT;
497                 goto out;
498         }
499         if (ret < 0)
500                 goto out;
501
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]);
506 out:
507         btrfs_free_path(path);
508         return ret;
509 }
510
511 /*
512  * Make a dir under the parent inode 'parent_ino' with 'name'
513  * and 'mode', The owner will be root/root.
514  */
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)
517 {
518         struct btrfs_dir_item *dir_item;
519         struct btrfs_path *path;
520         u64 ret_ino = 0;
521         int ret = 0;
522
523         path = btrfs_alloc_path();
524         if (!path)
525                 return -ENOMEM;
526
527         if (ino && *ino)
528                 ret_ino = *ino;
529
530         dir_item = btrfs_lookup_dir_item(NULL, root, path, parent_ino,
531                                          name, namelen, 0);
532         if (IS_ERR(dir_item)) {
533                 ret = PTR_ERR(dir_item);
534                 goto out;
535         }
536
537         if (dir_item) {
538                 struct btrfs_key found_key;
539
540                 /*
541                  * Already have conflicting name, check if it is a dir.
542                  * Either way, no need to continue.
543                  */
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)
547                         ret = -EEXIST;
548                 goto out;
549         }
550
551         if (!ret_ino)
552                 /*
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.
556                  */
557                 ret = btrfs_find_free_objectid(NULL, root, parent_ino,
558                                                &ret_ino);
559         if (ret)
560                 goto out;
561         ret = btrfs_new_inode(trans, root, ret_ino, mode | S_IFDIR);
562         if (ret)
563                 goto out;
564         ret = btrfs_add_link(trans, root, ret_ino, parent_ino, name, namelen,
565                              BTRFS_FT_DIR, NULL, 1);
566         if (ret)
567                 goto out;
568 out:
569         btrfs_free_path(path);
570         if (ret == 0 && ino)
571                 *ino = ret_ino;
572         return ret;
573 }
574
575 struct btrfs_root *btrfs_mksubvol(struct btrfs_root *root,
576                                   const char *base, u64 root_objectid)
577 {
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);
587         u64 index = 2;
588         char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
589         int len;
590         int i;
591         int ret;
592
593         len = strlen(base);
594         if (len == 0 || len > BTRFS_NAME_LEN)
595                 return NULL;
596
597         btrfs_init_path(&path);
598         key.objectid = dirid;
599         key.type = BTRFS_DIR_INDEX_KEY;
600         key.offset = (u64)-1;
601
602         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
603         if (ret <= 0) {
604                 error("search for DIR_INDEX dirid %llu failed: %d",
605                                 (unsigned long long)dirid, ret);
606                 goto fail;
607         }
608
609         if (path.slots[0] > 0) {
610                 path.slots[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;
614         }
615         btrfs_release_path(&path);
616
617         trans = btrfs_start_transaction(root, 1);
618         if (IS_ERR(trans)) {
619                 error("unable to start transaction");
620                 goto fail;
621         }
622
623         key.objectid = dirid;
624         key.offset = 0;
625         key.type =  BTRFS_INODE_ITEM_KEY;
626
627         ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
628         if (ret) {
629                 error("search for INODE_ITEM %llu failed: %d",
630                                 (unsigned long long)dirid, ret);
631                 goto fail;
632         }
633         leaf = path.nodes[0];
634         inode_item = btrfs_item_ptr(leaf, path.slots[0],
635                                     struct btrfs_inode_item);
636
637         key.objectid = root_objectid;
638         key.offset = (u64)-1;
639         key.type = BTRFS_ROOT_ITEM_KEY;
640
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);
645                 if (ret != -EEXIST)
646                         break;
647                 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
648                 if (len < 1 || len > BTRFS_NAME_LEN) {
649                         ret = -EINVAL;
650                         break;
651                 }
652         }
653         if (ret)
654                 goto fail;
655
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);
660
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);
666         if (ret) {
667                 error("unable to add root backref for %llu: %d",
668                                 root->root_key.objectid, ret);
669                 goto fail;
670         }
671
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);
676         if (ret) {
677                 error("unable to add root ref for %llu: %d",
678                                 root->root_key.objectid, ret);
679                 goto fail;
680         }
681
682         ret = btrfs_commit_transaction(trans, root);
683         if (ret) {
684                 error("transaction commit failed: %d", ret);
685                 goto fail;
686         }
687
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));
691                 new_root = NULL;
692         }
693 fail:
694         btrfs_init_path(&path);
695         return new_root;
696 }