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 performence sensitive.
28 #include "transaction.h"
33 * Find a free inode index for later btrfs_add_link().
34 * Currently just search from the largest dir_index and +1.
36 static int btrfs_find_free_dir_index(struct btrfs_root *root, u64 dir_ino,
39 struct btrfs_path *path;
41 struct btrfs_key found_key;
48 path = btrfs_alloc_path();
52 key.objectid = dir_ino;
53 key.type = BTRFS_DIR_INDEX_KEY;
56 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
60 if (path->slots[0] == 0) {
61 ret = btrfs_prev_leaf(root, path);
66 * This shouldn't happen since there must be a leaf
67 * containing the DIR_ITEM.
68 * Can only happen when the previous leaf is corrupted.
76 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
77 if (found_key.objectid != dir_ino ||
78 found_key.type != BTRFS_DIR_INDEX_KEY)
80 ret_val = found_key.offset + 1;
82 btrfs_free_path(path);
88 /* Check the dir_item/index conflicts before insert */
89 int check_dir_conflict(struct btrfs_root *root, char *name, int namelen,
92 struct btrfs_path *path;
94 struct btrfs_inode_item *inode_item;
95 struct btrfs_dir_item *dir_item;
98 path = btrfs_alloc_path();
102 /* Given dir exists? */
104 key.type = BTRFS_INODE_ITEM_KEY;
106 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
115 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
116 struct btrfs_inode_item);
117 if (!(btrfs_inode_mode(path->nodes[0], inode_item) & S_IFDIR)) {
121 btrfs_release_path(path);
123 /* Name conflicting? */
124 dir_item = btrfs_lookup_dir_item(NULL, root, path, dir, name,
126 if (IS_ERR(dir_item)) {
127 ret = PTR_ERR(dir_item);
134 btrfs_release_path(path);
136 /* Index conflicting? */
137 dir_item = btrfs_lookup_dir_index(NULL, root, path, dir, name,
139 if (IS_ERR(dir_item) && PTR_ERR(dir_item) == -ENOENT)
141 if (IS_ERR(dir_item)) {
142 ret = PTR_ERR(dir_item);
151 btrfs_free_path(path);
156 * Add dir_item/index for 'parent_ino' if add_backref is true, also insert a
157 * backref from the ino to parent dir and update the nlink(Kernel version does
160 * Currently only supports adding link from an inode to another inode.
162 int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
163 u64 ino, u64 parent_ino, char *name, int namelen,
164 u8 type, u64 *index, int add_backref)
166 struct btrfs_path *path;
167 struct btrfs_key key;
168 struct btrfs_inode_item *inode_item;
174 path = btrfs_alloc_path();
178 if (index && *index) {
181 ret = btrfs_find_free_dir_index(root, parent_ino, &ret_index);
186 ret = check_dir_conflict(root, name, namelen, parent_ino, ret_index);
192 ret = btrfs_insert_inode_ref(trans, root, name, namelen,
193 ino, parent_ino, ret_index);
197 /* Update nlinks for the inode */
199 key.type = BTRFS_INODE_ITEM_KEY;
201 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
207 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
208 struct btrfs_inode_item);
209 nlink = btrfs_inode_nlink(path->nodes[0], inode_item);
211 btrfs_set_inode_nlink(path->nodes[0], inode_item, nlink);
212 btrfs_mark_buffer_dirty(path->nodes[0]);
213 btrfs_release_path(path);
216 /* Add dir_item and dir_index */
218 key.type = BTRFS_INODE_ITEM_KEY;
220 ret = btrfs_insert_dir_item(trans, root, name, namelen, parent_ino,
221 &key, type, ret_index);
225 /* Update inode size of the parent inode */
226 key.objectid = parent_ino;
227 key.type = BTRFS_INODE_ITEM_KEY;
229 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
232 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
233 struct btrfs_inode_item);
234 inode_size = btrfs_inode_size(path->nodes[0], inode_item);
235 inode_size += namelen * 2;
236 btrfs_set_inode_size(path->nodes[0], inode_item, inode_size);
237 btrfs_mark_buffer_dirty(path->nodes[0]);
238 btrfs_release_path(path);
241 btrfs_free_path(path);
242 if (ret == 0 && index)
247 int btrfs_add_orphan_item(struct btrfs_trans_handle *trans,
248 struct btrfs_root *root, struct btrfs_path *path,
251 struct btrfs_key key;
253 key.objectid = BTRFS_ORPHAN_OBJECTID;
254 key.type = BTRFS_ORPHAN_ITEM_KEY;
257 return btrfs_insert_empty_item(trans, root, path, &key, 0);
261 * Unlink an inode, which will remove its backref and corresponding dir_index/
262 * dir_item if any of them exists.
264 * If an inode's nlink is reduced to 0 and 'add_orphan' is true, it will be
265 * added to orphan inode and wairing to be deleted by next kernel mount.
267 int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root,
268 u64 ino, u64 parent_ino, u64 index, const char *name,
269 int namelen, int add_orphan)
271 struct btrfs_path *path;
272 struct btrfs_key key;
273 struct btrfs_inode_item *inode_item;
274 struct btrfs_inode_ref *inode_ref;
275 struct btrfs_dir_item *dir_item;
278 int del_inode_ref = 0;
279 int del_dir_item = 0;
280 int del_dir_index = 0;
283 path = btrfs_alloc_path();
287 /* check the ref and backref exists */
288 inode_ref = btrfs_lookup_inode_ref(trans, root, path, name, namelen,
289 ino, parent_ino, index, 0);
290 if (IS_ERR(inode_ref)) {
291 ret = PTR_ERR(inode_ref);
296 btrfs_release_path(path);
298 dir_item = btrfs_lookup_dir_item(NULL, root, path, parent_ino,
300 if (IS_ERR(dir_item)) {
301 ret = PTR_ERR(dir_item);
306 btrfs_release_path(path);
308 dir_item = btrfs_lookup_dir_index(NULL, root, path, parent_ino,
309 name, namelen, index, 0);
311 * Since lookup_dir_index() will return -ENOENT when not found,
312 * we need to do extra check.
314 if (IS_ERR(dir_item) && PTR_ERR(dir_item) == -ENOENT)
316 if (IS_ERR(dir_item)) {
317 ret = PTR_ERR(dir_item);
322 btrfs_release_path(path);
324 if (!del_inode_ref && !del_dir_item && !del_dir_index) {
325 /* All not found, shouldn't happen */
331 /* Only decrease nlink when deleting inode_ref */
333 key.type = BTRFS_INODE_ITEM_KEY;
335 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
341 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
342 struct btrfs_inode_item);
343 nlinks = btrfs_inode_nlink(path->nodes[0], inode_item);
346 btrfs_set_inode_nlink(path->nodes[0], inode_item, nlinks);
347 btrfs_mark_buffer_dirty(path->nodes[0]);
348 btrfs_release_path(path);
350 /* For nlinks == 0, add it to orphan list if needed */
351 if (nlinks == 0 && add_orphan) {
352 ret = btrfs_add_orphan_item(trans, root, path, ino);
353 btrfs_mark_buffer_dirty(path->nodes[0]);
354 btrfs_release_path(path);
357 ret = btrfs_del_inode_ref(trans, root, name, namelen, ino,
364 dir_item = btrfs_lookup_dir_index(trans, root, path,
365 parent_ino, name, namelen,
367 if (IS_ERR(dir_item)) {
368 ret = PTR_ERR(dir_item);
375 ret = btrfs_delete_one_dir_name(trans, root, path, dir_item);
378 btrfs_release_path(path);
380 /* Update inode size of the parent inode */
381 key.objectid = parent_ino;
382 key.type = BTRFS_INODE_ITEM_KEY;
384 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
387 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
388 struct btrfs_inode_item);
389 inode_size = btrfs_inode_size(path->nodes[0], inode_item);
390 if (inode_size >= namelen)
391 inode_size -= namelen;
392 btrfs_set_inode_size(path->nodes[0], inode_item, inode_size);
393 btrfs_mark_buffer_dirty(path->nodes[0]);
394 btrfs_release_path(path);
398 dir_item = btrfs_lookup_dir_item(trans, root, path, parent_ino,
400 if (IS_ERR(dir_item)) {
401 ret = PTR_ERR(dir_item);
408 ret = btrfs_delete_one_dir_name(trans, root, path, dir_item);
411 btrfs_release_path(path);
413 /* Update inode size of the parent inode */
414 key.objectid = parent_ino;
415 key.type = BTRFS_INODE_ITEM_KEY;
417 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
420 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
421 struct btrfs_inode_item);
422 inode_size = btrfs_inode_size(path->nodes[0], inode_item);
423 if (inode_size >= namelen)
424 inode_size -= namelen;
425 btrfs_set_inode_size(path->nodes[0], inode_item, inode_size);
426 btrfs_mark_buffer_dirty(path->nodes[0]);
427 btrfs_release_path(path);
431 btrfs_free_path(path);