Commit | Line | Data |
---|---|---|
6cbd5570 CM |
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 | ||
39279cc3 CM |
19 | #include <linux/buffer_head.h> |
20 | #include <linux/fs.h> | |
21 | #include <linux/pagemap.h> | |
22 | #include <linux/highmem.h> | |
23 | #include <linux/time.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/string.h> | |
26 | #include <linux/smp_lock.h> | |
27 | #include <linux/backing-dev.h> | |
28 | #include <linux/mpage.h> | |
29 | #include <linux/swap.h> | |
30 | #include <linux/writeback.h> | |
31 | #include <linux/statfs.h> | |
32 | #include <linux/compat.h> | |
9ebefb18 | 33 | #include <linux/bit_spinlock.h> |
92fee66d | 34 | #include <linux/version.h> |
5103e947 | 35 | #include <linux/xattr.h> |
39279cc3 CM |
36 | #include "ctree.h" |
37 | #include "disk-io.h" | |
38 | #include "transaction.h" | |
39 | #include "btrfs_inode.h" | |
40 | #include "ioctl.h" | |
41 | #include "print-tree.h" | |
42 | ||
43 | struct btrfs_iget_args { | |
44 | u64 ino; | |
45 | struct btrfs_root *root; | |
46 | }; | |
47 | ||
48 | static struct inode_operations btrfs_dir_inode_operations; | |
49 | static struct inode_operations btrfs_symlink_inode_operations; | |
50 | static struct inode_operations btrfs_dir_ro_inode_operations; | |
618e21d5 | 51 | static struct inode_operations btrfs_special_inode_operations; |
39279cc3 CM |
52 | static struct inode_operations btrfs_file_inode_operations; |
53 | static struct address_space_operations btrfs_aops; | |
54 | static struct address_space_operations btrfs_symlink_aops; | |
55 | static struct file_operations btrfs_dir_file_operations; | |
07157aac | 56 | static struct extent_map_ops btrfs_extent_map_ops; |
39279cc3 CM |
57 | |
58 | static struct kmem_cache *btrfs_inode_cachep; | |
59 | struct kmem_cache *btrfs_trans_handle_cachep; | |
60 | struct kmem_cache *btrfs_transaction_cachep; | |
61 | struct kmem_cache *btrfs_bit_radix_cachep; | |
62 | struct kmem_cache *btrfs_path_cachep; | |
63 | ||
64 | #define S_SHIFT 12 | |
65 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
66 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
67 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
68 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
69 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
70 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
71 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
72 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
73 | }; | |
74 | ||
b888db2b CM |
75 | static int run_delalloc_range(struct inode *inode, u64 start, u64 end) |
76 | { | |
77 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
78 | struct btrfs_trans_handle *trans; | |
79 | struct btrfs_key ins; | |
80 | u64 alloc_hint = 0; | |
db94535d | 81 | u64 num_bytes; |
b888db2b | 82 | int ret; |
db94535d | 83 | u64 blocksize = root->sectorsize; |
b888db2b CM |
84 | |
85 | mutex_lock(&root->fs_info->fs_mutex); | |
86 | trans = btrfs_start_transaction(root, 1); | |
87 | btrfs_set_trans_block_group(trans, inode); | |
88 | BUG_ON(!trans); | |
db94535d | 89 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
b888db2b | 90 | ret = btrfs_drop_extents(trans, root, inode, |
3326d1b0 | 91 | start, start + num_bytes, start, &alloc_hint); |
db94535d | 92 | |
179e29e4 CM |
93 | if (alloc_hint == EXTENT_MAP_INLINE) |
94 | goto out; | |
95 | ||
7bb86316 CM |
96 | ret = btrfs_alloc_extent(trans, root, num_bytes, |
97 | root->root_key.objectid, trans->transid, | |
98 | inode->i_ino, start, 0, | |
b888db2b CM |
99 | alloc_hint, (u64)-1, &ins, 1); |
100 | if (ret) { | |
101 | WARN_ON(1); | |
102 | goto out; | |
103 | } | |
104 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, | |
105 | start, ins.objectid, ins.offset, | |
106 | ins.offset); | |
107 | out: | |
108 | btrfs_end_transaction(trans, root); | |
109 | mutex_unlock(&root->fs_info->fs_mutex); | |
110 | return ret; | |
111 | } | |
112 | ||
07157aac CM |
113 | int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end) |
114 | { | |
115 | struct inode *inode = page->mapping->host; | |
116 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
117 | struct btrfs_trans_handle *trans; | |
118 | char *kaddr; | |
119 | int ret; | |
35ebb934 | 120 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
07157aac CM |
121 | size_t offset = start - page_start; |
122 | ||
123 | mutex_lock(&root->fs_info->fs_mutex); | |
124 | trans = btrfs_start_transaction(root, 1); | |
125 | btrfs_set_trans_block_group(trans, inode); | |
126 | kaddr = kmap(page); | |
f578d4bd | 127 | btrfs_csum_file_block(trans, root, inode, inode->i_ino, |
07157aac CM |
128 | start, kaddr + offset, end - start + 1); |
129 | kunmap(page); | |
130 | ret = btrfs_end_transaction(trans, root); | |
131 | BUG_ON(ret); | |
132 | mutex_unlock(&root->fs_info->fs_mutex); | |
133 | return ret; | |
134 | } | |
135 | ||
136 | int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end) | |
137 | { | |
138 | int ret = 0; | |
139 | struct inode *inode = page->mapping->host; | |
140 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
141 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
142 | struct btrfs_csum_item *item; | |
143 | struct btrfs_path *path = NULL; | |
ff79f819 | 144 | u32 csum; |
07157aac CM |
145 | |
146 | mutex_lock(&root->fs_info->fs_mutex); | |
147 | path = btrfs_alloc_path(); | |
148 | item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0); | |
149 | if (IS_ERR(item)) { | |
150 | ret = PTR_ERR(item); | |
151 | /* a csum that isn't present is a preallocated region. */ | |
152 | if (ret == -ENOENT || ret == -EFBIG) | |
153 | ret = 0; | |
ff79f819 | 154 | csum = 0; |
07157aac CM |
155 | goto out; |
156 | } | |
ff79f819 CM |
157 | read_extent_buffer(path->nodes[0], &csum, (unsigned long)item, |
158 | BTRFS_CRC32_SIZE); | |
159 | set_state_private(em_tree, start, csum); | |
07157aac CM |
160 | out: |
161 | if (path) | |
162 | btrfs_free_path(path); | |
163 | mutex_unlock(&root->fs_info->fs_mutex); | |
164 | return ret; | |
165 | } | |
166 | ||
167 | int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end) | |
168 | { | |
35ebb934 | 169 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
07157aac | 170 | struct inode *inode = page->mapping->host; |
07157aac CM |
171 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
172 | char *kaddr; | |
173 | u64 private; | |
174 | int ret; | |
ff79f819 CM |
175 | struct btrfs_root *root = BTRFS_I(inode)->root; |
176 | u32 csum = ~(u32)0; | |
bbf0d006 | 177 | unsigned long flags; |
07157aac CM |
178 | |
179 | ret = get_state_private(em_tree, start, &private); | |
bbf0d006 | 180 | local_irq_save(flags); |
07157aac CM |
181 | kaddr = kmap_atomic(page, KM_IRQ0); |
182 | if (ret) { | |
183 | goto zeroit; | |
184 | } | |
ff79f819 CM |
185 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
186 | btrfs_csum_final(csum, (char *)&csum); | |
187 | if (csum != private) { | |
07157aac CM |
188 | goto zeroit; |
189 | } | |
190 | kunmap_atomic(kaddr, KM_IRQ0); | |
bbf0d006 | 191 | local_irq_restore(flags); |
07157aac CM |
192 | return 0; |
193 | ||
194 | zeroit: | |
195 | printk("btrfs csum failed ino %lu off %llu\n", | |
196 | page->mapping->host->i_ino, (unsigned long long)start); | |
db94535d CM |
197 | memset(kaddr + offset, 1, end - start + 1); |
198 | flush_dcache_page(page); | |
07157aac | 199 | kunmap_atomic(kaddr, KM_IRQ0); |
bbf0d006 | 200 | local_irq_restore(flags); |
07157aac CM |
201 | return 0; |
202 | } | |
b888db2b | 203 | |
39279cc3 CM |
204 | void btrfs_read_locked_inode(struct inode *inode) |
205 | { | |
206 | struct btrfs_path *path; | |
5f39d397 | 207 | struct extent_buffer *leaf; |
39279cc3 | 208 | struct btrfs_inode_item *inode_item; |
5f39d397 | 209 | struct btrfs_inode_timespec *tspec; |
39279cc3 CM |
210 | struct btrfs_root *root = BTRFS_I(inode)->root; |
211 | struct btrfs_key location; | |
212 | u64 alloc_group_block; | |
618e21d5 | 213 | u32 rdev; |
39279cc3 CM |
214 | int ret; |
215 | ||
216 | path = btrfs_alloc_path(); | |
217 | BUG_ON(!path); | |
39279cc3 CM |
218 | mutex_lock(&root->fs_info->fs_mutex); |
219 | ||
220 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); | |
221 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); | |
5f39d397 | 222 | if (ret) |
39279cc3 | 223 | goto make_bad; |
39279cc3 | 224 | |
5f39d397 CM |
225 | leaf = path->nodes[0]; |
226 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
227 | struct btrfs_inode_item); | |
228 | ||
229 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); | |
230 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); | |
231 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); | |
232 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); | |
233 | inode->i_size = btrfs_inode_size(leaf, inode_item); | |
234 | ||
235 | tspec = btrfs_inode_atime(inode_item); | |
236 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
237 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
238 | ||
239 | tspec = btrfs_inode_mtime(inode_item); | |
240 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
241 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
242 | ||
243 | tspec = btrfs_inode_ctime(inode_item); | |
244 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
245 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
246 | ||
247 | inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); | |
248 | inode->i_generation = btrfs_inode_generation(leaf, inode_item); | |
618e21d5 | 249 | inode->i_rdev = 0; |
5f39d397 CM |
250 | rdev = btrfs_inode_rdev(leaf, inode_item); |
251 | ||
252 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); | |
39279cc3 CM |
253 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
254 | alloc_group_block); | |
255 | ||
256 | btrfs_free_path(path); | |
257 | inode_item = NULL; | |
258 | ||
259 | mutex_unlock(&root->fs_info->fs_mutex); | |
260 | ||
261 | switch (inode->i_mode & S_IFMT) { | |
39279cc3 CM |
262 | case S_IFREG: |
263 | inode->i_mapping->a_ops = &btrfs_aops; | |
07157aac | 264 | BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops; |
39279cc3 CM |
265 | inode->i_fop = &btrfs_file_operations; |
266 | inode->i_op = &btrfs_file_inode_operations; | |
267 | break; | |
268 | case S_IFDIR: | |
269 | inode->i_fop = &btrfs_dir_file_operations; | |
270 | if (root == root->fs_info->tree_root) | |
271 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
272 | else | |
273 | inode->i_op = &btrfs_dir_inode_operations; | |
274 | break; | |
275 | case S_IFLNK: | |
276 | inode->i_op = &btrfs_symlink_inode_operations; | |
277 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
278 | break; | |
618e21d5 JB |
279 | default: |
280 | init_special_inode(inode, inode->i_mode, rdev); | |
281 | break; | |
39279cc3 CM |
282 | } |
283 | return; | |
284 | ||
285 | make_bad: | |
286 | btrfs_release_path(root, path); | |
287 | btrfs_free_path(path); | |
288 | mutex_unlock(&root->fs_info->fs_mutex); | |
289 | make_bad_inode(inode); | |
290 | } | |
291 | ||
5f39d397 CM |
292 | static void fill_inode_item(struct extent_buffer *leaf, |
293 | struct btrfs_inode_item *item, | |
39279cc3 CM |
294 | struct inode *inode) |
295 | { | |
5f39d397 CM |
296 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
297 | btrfs_set_inode_gid(leaf, item, inode->i_gid); | |
298 | btrfs_set_inode_size(leaf, item, inode->i_size); | |
299 | btrfs_set_inode_mode(leaf, item, inode->i_mode); | |
300 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); | |
301 | ||
302 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), | |
303 | inode->i_atime.tv_sec); | |
304 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), | |
305 | inode->i_atime.tv_nsec); | |
306 | ||
307 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), | |
308 | inode->i_mtime.tv_sec); | |
309 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), | |
310 | inode->i_mtime.tv_nsec); | |
311 | ||
312 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), | |
313 | inode->i_ctime.tv_sec); | |
314 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), | |
315 | inode->i_ctime.tv_nsec); | |
316 | ||
317 | btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); | |
318 | btrfs_set_inode_generation(leaf, item, inode->i_generation); | |
319 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); | |
320 | btrfs_set_inode_block_group(leaf, item, | |
39279cc3 CM |
321 | BTRFS_I(inode)->block_group->key.objectid); |
322 | } | |
323 | ||
a52d9a80 | 324 | int btrfs_update_inode(struct btrfs_trans_handle *trans, |
39279cc3 CM |
325 | struct btrfs_root *root, |
326 | struct inode *inode) | |
327 | { | |
328 | struct btrfs_inode_item *inode_item; | |
329 | struct btrfs_path *path; | |
5f39d397 | 330 | struct extent_buffer *leaf; |
39279cc3 CM |
331 | int ret; |
332 | ||
333 | path = btrfs_alloc_path(); | |
334 | BUG_ON(!path); | |
39279cc3 CM |
335 | ret = btrfs_lookup_inode(trans, root, path, |
336 | &BTRFS_I(inode)->location, 1); | |
337 | if (ret) { | |
338 | if (ret > 0) | |
339 | ret = -ENOENT; | |
340 | goto failed; | |
341 | } | |
342 | ||
5f39d397 CM |
343 | leaf = path->nodes[0]; |
344 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
39279cc3 CM |
345 | struct btrfs_inode_item); |
346 | ||
5f39d397 CM |
347 | fill_inode_item(leaf, inode_item, inode); |
348 | btrfs_mark_buffer_dirty(leaf); | |
15ee9bc7 | 349 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
350 | ret = 0; |
351 | failed: | |
352 | btrfs_release_path(root, path); | |
353 | btrfs_free_path(path); | |
354 | return ret; | |
355 | } | |
356 | ||
357 | ||
358 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, | |
359 | struct btrfs_root *root, | |
360 | struct inode *dir, | |
361 | struct dentry *dentry) | |
362 | { | |
363 | struct btrfs_path *path; | |
364 | const char *name = dentry->d_name.name; | |
365 | int name_len = dentry->d_name.len; | |
366 | int ret = 0; | |
5f39d397 | 367 | struct extent_buffer *leaf; |
39279cc3 | 368 | struct btrfs_dir_item *di; |
5f39d397 | 369 | struct btrfs_key key; |
39279cc3 CM |
370 | |
371 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
372 | if (!path) { |
373 | ret = -ENOMEM; | |
374 | goto err; | |
375 | } | |
376 | ||
39279cc3 CM |
377 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
378 | name, name_len, -1); | |
379 | if (IS_ERR(di)) { | |
380 | ret = PTR_ERR(di); | |
381 | goto err; | |
382 | } | |
383 | if (!di) { | |
384 | ret = -ENOENT; | |
385 | goto err; | |
386 | } | |
5f39d397 CM |
387 | leaf = path->nodes[0]; |
388 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 389 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
390 | if (ret) |
391 | goto err; | |
39279cc3 CM |
392 | btrfs_release_path(root, path); |
393 | ||
394 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, | |
5f39d397 | 395 | key.objectid, name, name_len, -1); |
39279cc3 CM |
396 | if (IS_ERR(di)) { |
397 | ret = PTR_ERR(di); | |
398 | goto err; | |
399 | } | |
400 | if (!di) { | |
401 | ret = -ENOENT; | |
402 | goto err; | |
403 | } | |
404 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
39279cc3 CM |
405 | |
406 | dentry->d_inode->i_ctime = dir->i_ctime; | |
3954401f CM |
407 | if (!S_ISLNK(dentry->d_inode->i_mode)) { |
408 | ret = btrfs_del_inode_ref(trans, root, name, name_len, | |
409 | dentry->d_inode->i_ino, | |
410 | dentry->d_parent->d_inode->i_ino); | |
411 | if (ret) { | |
412 | printk("failed to delete reference to %.*s, " | |
413 | "inode %lu parent %lu\n", name_len, name, | |
414 | dentry->d_inode->i_ino, | |
415 | dentry->d_parent->d_inode->i_ino); | |
416 | } | |
417 | } | |
39279cc3 CM |
418 | err: |
419 | btrfs_free_path(path); | |
420 | if (!ret) { | |
421 | dir->i_size -= name_len * 2; | |
79c44584 | 422 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
39279cc3 CM |
423 | btrfs_update_inode(trans, root, dir); |
424 | drop_nlink(dentry->d_inode); | |
54aa1f4d | 425 | ret = btrfs_update_inode(trans, root, dentry->d_inode); |
39279cc3 CM |
426 | dir->i_sb->s_dirt = 1; |
427 | } | |
428 | return ret; | |
429 | } | |
430 | ||
431 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
432 | { | |
433 | struct btrfs_root *root; | |
434 | struct btrfs_trans_handle *trans; | |
435 | int ret; | |
d3c2fdcf | 436 | unsigned long nr; |
39279cc3 CM |
437 | |
438 | root = BTRFS_I(dir)->root; | |
439 | mutex_lock(&root->fs_info->fs_mutex); | |
440 | trans = btrfs_start_transaction(root, 1); | |
5f39d397 | 441 | |
39279cc3 CM |
442 | btrfs_set_trans_block_group(trans, dir); |
443 | ret = btrfs_unlink_trans(trans, root, dir, dentry); | |
d3c2fdcf | 444 | nr = trans->blocks_used; |
5f39d397 | 445 | |
39279cc3 CM |
446 | btrfs_end_transaction(trans, root); |
447 | mutex_unlock(&root->fs_info->fs_mutex); | |
d3c2fdcf | 448 | btrfs_btree_balance_dirty(root, nr); |
5f39d397 | 449 | |
39279cc3 CM |
450 | return ret; |
451 | } | |
452 | ||
453 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |
454 | { | |
455 | struct inode *inode = dentry->d_inode; | |
456 | int err; | |
457 | int ret; | |
458 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
39279cc3 | 459 | struct btrfs_trans_handle *trans; |
d3c2fdcf | 460 | unsigned long nr; |
39279cc3 | 461 | |
134d4512 Y |
462 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
463 | return -ENOTEMPTY; | |
464 | ||
39279cc3 CM |
465 | mutex_lock(&root->fs_info->fs_mutex); |
466 | trans = btrfs_start_transaction(root, 1); | |
467 | btrfs_set_trans_block_group(trans, dir); | |
39279cc3 CM |
468 | |
469 | /* now the directory is empty */ | |
470 | err = btrfs_unlink_trans(trans, root, dir, dentry); | |
471 | if (!err) { | |
472 | inode->i_size = 0; | |
473 | } | |
3954401f | 474 | |
d3c2fdcf | 475 | nr = trans->blocks_used; |
39279cc3 | 476 | ret = btrfs_end_transaction(trans, root); |
134d4512 | 477 | mutex_unlock(&root->fs_info->fs_mutex); |
d3c2fdcf | 478 | btrfs_btree_balance_dirty(root, nr); |
3954401f | 479 | |
39279cc3 CM |
480 | if (ret && !err) |
481 | err = ret; | |
482 | return err; | |
483 | } | |
484 | ||
485 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, | |
486 | struct btrfs_root *root, | |
487 | struct inode *inode) | |
488 | { | |
489 | struct btrfs_path *path; | |
490 | int ret; | |
491 | ||
492 | clear_inode(inode); | |
493 | ||
494 | path = btrfs_alloc_path(); | |
495 | BUG_ON(!path); | |
39279cc3 CM |
496 | ret = btrfs_lookup_inode(trans, root, path, |
497 | &BTRFS_I(inode)->location, -1); | |
54aa1f4d CM |
498 | if (ret > 0) |
499 | ret = -ENOENT; | |
500 | if (!ret) | |
501 | ret = btrfs_del_item(trans, root, path); | |
39279cc3 CM |
502 | btrfs_free_path(path); |
503 | return ret; | |
504 | } | |
505 | ||
39279cc3 CM |
506 | /* |
507 | * this can truncate away extent items, csum items and directory items. | |
508 | * It starts at a high offset and removes keys until it can't find | |
509 | * any higher than i_size. | |
510 | * | |
511 | * csum items that cross the new i_size are truncated to the new size | |
512 | * as well. | |
513 | */ | |
514 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, | |
515 | struct btrfs_root *root, | |
516 | struct inode *inode) | |
517 | { | |
518 | int ret; | |
519 | struct btrfs_path *path; | |
520 | struct btrfs_key key; | |
5f39d397 | 521 | struct btrfs_key found_key; |
39279cc3 | 522 | u32 found_type; |
5f39d397 | 523 | struct extent_buffer *leaf; |
39279cc3 CM |
524 | struct btrfs_file_extent_item *fi; |
525 | u64 extent_start = 0; | |
db94535d | 526 | u64 extent_num_bytes = 0; |
39279cc3 | 527 | u64 item_end = 0; |
7bb86316 | 528 | u64 root_gen = 0; |
d8d5f3e1 | 529 | u64 root_owner = 0; |
39279cc3 CM |
530 | int found_extent; |
531 | int del_item; | |
179e29e4 | 532 | int extent_type = -1; |
39279cc3 | 533 | |
a52d9a80 | 534 | btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1); |
39279cc3 | 535 | path = btrfs_alloc_path(); |
3c69faec | 536 | path->reada = -1; |
39279cc3 | 537 | BUG_ON(!path); |
5f39d397 | 538 | |
39279cc3 CM |
539 | /* FIXME, add redo link to tree so we don't leak on crash */ |
540 | key.objectid = inode->i_ino; | |
541 | key.offset = (u64)-1; | |
5f39d397 CM |
542 | key.type = (u8)-1; |
543 | ||
39279cc3 CM |
544 | while(1) { |
545 | btrfs_init_path(path); | |
546 | fi = NULL; | |
547 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
548 | if (ret < 0) { | |
549 | goto error; | |
550 | } | |
551 | if (ret > 0) { | |
552 | BUG_ON(path->slots[0] == 0); | |
553 | path->slots[0]--; | |
554 | } | |
5f39d397 CM |
555 | leaf = path->nodes[0]; |
556 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
557 | found_type = btrfs_key_type(&found_key); | |
39279cc3 | 558 | |
5f39d397 | 559 | if (found_key.objectid != inode->i_ino) |
39279cc3 | 560 | break; |
5f39d397 | 561 | |
39279cc3 CM |
562 | if (found_type != BTRFS_CSUM_ITEM_KEY && |
563 | found_type != BTRFS_DIR_ITEM_KEY && | |
564 | found_type != BTRFS_DIR_INDEX_KEY && | |
565 | found_type != BTRFS_EXTENT_DATA_KEY) | |
566 | break; | |
567 | ||
5f39d397 | 568 | item_end = found_key.offset; |
39279cc3 | 569 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 570 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 571 | struct btrfs_file_extent_item); |
179e29e4 CM |
572 | extent_type = btrfs_file_extent_type(leaf, fi); |
573 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 574 | item_end += |
db94535d | 575 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 CM |
576 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
577 | struct btrfs_item *item = btrfs_item_nr(leaf, | |
578 | path->slots[0]); | |
579 | item_end += btrfs_file_extent_inline_len(leaf, | |
580 | item); | |
39279cc3 | 581 | } |
008630c1 | 582 | item_end--; |
39279cc3 CM |
583 | } |
584 | if (found_type == BTRFS_CSUM_ITEM_KEY) { | |
585 | ret = btrfs_csum_truncate(trans, root, path, | |
586 | inode->i_size); | |
587 | BUG_ON(ret); | |
588 | } | |
008630c1 | 589 | if (item_end < inode->i_size) { |
b888db2b CM |
590 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
591 | found_type = BTRFS_INODE_ITEM_KEY; | |
592 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { | |
593 | found_type = BTRFS_CSUM_ITEM_KEY; | |
594 | } else if (found_type) { | |
595 | found_type--; | |
596 | } else { | |
597 | break; | |
39279cc3 | 598 | } |
a61721d5 | 599 | btrfs_set_key_type(&key, found_type); |
65555a06 | 600 | btrfs_release_path(root, path); |
b888db2b | 601 | continue; |
39279cc3 | 602 | } |
5f39d397 | 603 | if (found_key.offset >= inode->i_size) |
39279cc3 CM |
604 | del_item = 1; |
605 | else | |
606 | del_item = 0; | |
607 | found_extent = 0; | |
608 | ||
609 | /* FIXME, shrink the extent if the ref count is only 1 */ | |
179e29e4 CM |
610 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
611 | goto delete; | |
612 | ||
613 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 614 | u64 num_dec; |
db94535d | 615 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
39279cc3 | 616 | if (!del_item) { |
db94535d CM |
617 | u64 orig_num_bytes = |
618 | btrfs_file_extent_num_bytes(leaf, fi); | |
619 | extent_num_bytes = inode->i_size - | |
5f39d397 | 620 | found_key.offset + root->sectorsize - 1; |
db94535d CM |
621 | btrfs_set_file_extent_num_bytes(leaf, fi, |
622 | extent_num_bytes); | |
623 | num_dec = (orig_num_bytes - | |
624 | extent_num_bytes) >> 9; | |
bab9fb03 Y |
625 | if (extent_start != 0) { |
626 | inode->i_blocks -= num_dec; | |
627 | } | |
5f39d397 | 628 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 629 | } else { |
db94535d CM |
630 | extent_num_bytes = |
631 | btrfs_file_extent_disk_num_bytes(leaf, | |
632 | fi); | |
39279cc3 | 633 | /* FIXME blocksize != 4096 */ |
db94535d CM |
634 | num_dec = btrfs_file_extent_num_bytes(leaf, |
635 | fi) >> 9; | |
39279cc3 CM |
636 | if (extent_start != 0) { |
637 | found_extent = 1; | |
638 | inode->i_blocks -= num_dec; | |
639 | } | |
d8d5f3e1 CM |
640 | root_gen = btrfs_header_generation(leaf); |
641 | root_owner = btrfs_header_owner(leaf); | |
39279cc3 | 642 | } |
179e29e4 CM |
643 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE && |
644 | !del_item) { | |
645 | u32 newsize = inode->i_size - found_key.offset; | |
646 | newsize = btrfs_file_extent_calc_inline_size(newsize); | |
647 | ret = btrfs_truncate_item(trans, root, path, | |
648 | newsize, 1); | |
649 | BUG_ON(ret); | |
39279cc3 | 650 | } |
179e29e4 | 651 | delete: |
39279cc3 CM |
652 | if (del_item) { |
653 | ret = btrfs_del_item(trans, root, path); | |
54aa1f4d CM |
654 | if (ret) |
655 | goto error; | |
39279cc3 CM |
656 | } else { |
657 | break; | |
658 | } | |
659 | btrfs_release_path(root, path); | |
660 | if (found_extent) { | |
661 | ret = btrfs_free_extent(trans, root, extent_start, | |
7bb86316 | 662 | extent_num_bytes, |
d8d5f3e1 | 663 | root_owner, |
7bb86316 CM |
664 | root_gen, inode->i_ino, |
665 | found_key.offset, 0); | |
39279cc3 CM |
666 | BUG_ON(ret); |
667 | } | |
668 | } | |
669 | ret = 0; | |
670 | error: | |
671 | btrfs_release_path(root, path); | |
672 | btrfs_free_path(path); | |
673 | inode->i_sb->s_dirt = 1; | |
674 | return ret; | |
675 | } | |
676 | ||
b888db2b | 677 | static int btrfs_cow_one_page(struct inode *inode, struct page *page, |
a52d9a80 CM |
678 | size_t zero_start) |
679 | { | |
680 | char *kaddr; | |
681 | int ret = 0; | |
b888db2b | 682 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
35ebb934 | 683 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
b888db2b | 684 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
a52d9a80 | 685 | |
b3cfa35a | 686 | set_page_extent_mapped(page); |
a52d9a80 | 687 | |
b888db2b CM |
688 | lock_extent(em_tree, page_start, page_end, GFP_NOFS); |
689 | set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start, | |
690 | page_end, GFP_NOFS); | |
a52d9a80 | 691 | if (zero_start != PAGE_CACHE_SIZE) { |
b888db2b | 692 | kaddr = kmap(page); |
a52d9a80 CM |
693 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
694 | flush_dcache_page(page); | |
b888db2b | 695 | kunmap(page); |
a52d9a80 | 696 | } |
b888db2b CM |
697 | set_page_dirty(page); |
698 | unlock_extent(em_tree, page_start, page_end, GFP_NOFS); | |
a52d9a80 | 699 | |
a52d9a80 CM |
700 | return ret; |
701 | } | |
702 | ||
39279cc3 CM |
703 | /* |
704 | * taken from block_truncate_page, but does cow as it zeros out | |
705 | * any bytes left in the last page in the file. | |
706 | */ | |
707 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) | |
708 | { | |
709 | struct inode *inode = mapping->host; | |
db94535d CM |
710 | struct btrfs_root *root = BTRFS_I(inode)->root; |
711 | u32 blocksize = root->sectorsize; | |
39279cc3 CM |
712 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
713 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
714 | struct page *page; | |
39279cc3 | 715 | int ret = 0; |
a52d9a80 | 716 | u64 page_start; |
39279cc3 CM |
717 | |
718 | if ((offset & (blocksize - 1)) == 0) | |
719 | goto out; | |
720 | ||
db94535d | 721 | down_read(&root->snap_sem); |
39279cc3 CM |
722 | ret = -ENOMEM; |
723 | page = grab_cache_page(mapping, index); | |
724 | if (!page) | |
725 | goto out; | |
39279cc3 | 726 | if (!PageUptodate(page)) { |
9ebefb18 | 727 | ret = btrfs_readpage(NULL, page); |
39279cc3 CM |
728 | lock_page(page); |
729 | if (!PageUptodate(page)) { | |
730 | ret = -EIO; | |
731 | goto out; | |
732 | } | |
733 | } | |
35ebb934 | 734 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
a52d9a80 | 735 | |
b888db2b | 736 | ret = btrfs_cow_one_page(inode, page, offset); |
39279cc3 | 737 | |
39279cc3 CM |
738 | unlock_page(page); |
739 | page_cache_release(page); | |
011410bd | 740 | up_read(&BTRFS_I(inode)->root->snap_sem); |
39279cc3 CM |
741 | out: |
742 | return ret; | |
743 | } | |
744 | ||
745 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) | |
746 | { | |
747 | struct inode *inode = dentry->d_inode; | |
748 | int err; | |
749 | ||
750 | err = inode_change_ok(inode, attr); | |
751 | if (err) | |
752 | return err; | |
753 | ||
754 | if (S_ISREG(inode->i_mode) && | |
755 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { | |
756 | struct btrfs_trans_handle *trans; | |
757 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2bf5a725 CM |
758 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
759 | ||
5f39d397 | 760 | u64 mask = root->sectorsize - 1; |
39279cc3 | 761 | u64 pos = (inode->i_size + mask) & ~mask; |
2bf5a725 | 762 | u64 block_end = attr->ia_size | mask; |
39279cc3 | 763 | u64 hole_size; |
179e29e4 | 764 | u64 alloc_hint = 0; |
39279cc3 CM |
765 | |
766 | if (attr->ia_size <= pos) | |
767 | goto out; | |
768 | ||
769 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | |
770 | ||
2bf5a725 | 771 | lock_extent(em_tree, pos, block_end, GFP_NOFS); |
39279cc3 | 772 | hole_size = (attr->ia_size - pos + mask) & ~mask; |
39279cc3 CM |
773 | |
774 | mutex_lock(&root->fs_info->fs_mutex); | |
775 | trans = btrfs_start_transaction(root, 1); | |
776 | btrfs_set_trans_block_group(trans, inode); | |
2bf5a725 | 777 | err = btrfs_drop_extents(trans, root, inode, |
3326d1b0 CM |
778 | pos, pos + hole_size, pos, |
779 | &alloc_hint); | |
2bf5a725 | 780 | |
179e29e4 CM |
781 | if (alloc_hint != EXTENT_MAP_INLINE) { |
782 | err = btrfs_insert_file_extent(trans, root, | |
783 | inode->i_ino, | |
784 | pos, 0, 0, hole_size); | |
785 | } | |
39279cc3 CM |
786 | btrfs_end_transaction(trans, root); |
787 | mutex_unlock(&root->fs_info->fs_mutex); | |
2bf5a725 | 788 | unlock_extent(em_tree, pos, block_end, GFP_NOFS); |
54aa1f4d CM |
789 | if (err) |
790 | return err; | |
39279cc3 CM |
791 | } |
792 | out: | |
793 | err = inode_setattr(inode, attr); | |
794 | ||
795 | return err; | |
796 | } | |
797 | void btrfs_delete_inode(struct inode *inode) | |
798 | { | |
799 | struct btrfs_trans_handle *trans; | |
800 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d3c2fdcf | 801 | unsigned long nr; |
39279cc3 CM |
802 | int ret; |
803 | ||
804 | truncate_inode_pages(&inode->i_data, 0); | |
805 | if (is_bad_inode(inode)) { | |
806 | goto no_delete; | |
807 | } | |
5f39d397 | 808 | |
39279cc3 CM |
809 | inode->i_size = 0; |
810 | mutex_lock(&root->fs_info->fs_mutex); | |
811 | trans = btrfs_start_transaction(root, 1); | |
5f39d397 | 812 | |
39279cc3 CM |
813 | btrfs_set_trans_block_group(trans, inode); |
814 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
5103e947 JB |
815 | if (ret) |
816 | goto no_delete_lock; | |
817 | ret = btrfs_delete_xattrs(trans, root, inode); | |
54aa1f4d CM |
818 | if (ret) |
819 | goto no_delete_lock; | |
820 | ret = btrfs_free_inode(trans, root, inode); | |
821 | if (ret) | |
822 | goto no_delete_lock; | |
d3c2fdcf | 823 | nr = trans->blocks_used; |
5f39d397 | 824 | |
39279cc3 CM |
825 | btrfs_end_transaction(trans, root); |
826 | mutex_unlock(&root->fs_info->fs_mutex); | |
d3c2fdcf | 827 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 | 828 | return; |
54aa1f4d CM |
829 | |
830 | no_delete_lock: | |
d3c2fdcf | 831 | nr = trans->blocks_used; |
54aa1f4d CM |
832 | btrfs_end_transaction(trans, root); |
833 | mutex_unlock(&root->fs_info->fs_mutex); | |
d3c2fdcf | 834 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
835 | no_delete: |
836 | clear_inode(inode); | |
837 | } | |
838 | ||
839 | /* | |
840 | * this returns the key found in the dir entry in the location pointer. | |
841 | * If no dir entries were found, location->objectid is 0. | |
842 | */ | |
843 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
844 | struct btrfs_key *location) | |
845 | { | |
846 | const char *name = dentry->d_name.name; | |
847 | int namelen = dentry->d_name.len; | |
848 | struct btrfs_dir_item *di; | |
849 | struct btrfs_path *path; | |
850 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 851 | int ret = 0; |
39279cc3 | 852 | |
3954401f CM |
853 | if (namelen == 1 && strcmp(name, ".") == 0) { |
854 | location->objectid = dir->i_ino; | |
855 | location->type = BTRFS_INODE_ITEM_KEY; | |
856 | location->offset = 0; | |
857 | return 0; | |
858 | } | |
39279cc3 CM |
859 | path = btrfs_alloc_path(); |
860 | BUG_ON(!path); | |
3954401f CM |
861 | |
862 | if (namelen == 1 && strcmp(name, "..") == 0) { | |
863 | struct btrfs_key key; | |
864 | struct extent_buffer *leaf; | |
865 | u32 nritems; | |
866 | int slot; | |
867 | ||
868 | key.objectid = dir->i_ino; | |
869 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); | |
870 | key.offset = 0; | |
871 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
872 | BUG_ON(ret == 0); | |
873 | ret = 0; | |
874 | ||
875 | leaf = path->nodes[0]; | |
876 | slot = path->slots[0]; | |
877 | nritems = btrfs_header_nritems(leaf); | |
878 | if (slot >= nritems) | |
879 | goto out_err; | |
880 | ||
881 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
882 | if (key.objectid != dir->i_ino || | |
883 | key.type != BTRFS_INODE_REF_KEY) { | |
884 | goto out_err; | |
885 | } | |
886 | location->objectid = key.offset; | |
887 | location->type = BTRFS_INODE_ITEM_KEY; | |
888 | location->offset = 0; | |
889 | goto out; | |
890 | } | |
891 | ||
39279cc3 CM |
892 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
893 | namelen, 0); | |
0d9f7f3e Y |
894 | if (IS_ERR(di)) |
895 | ret = PTR_ERR(di); | |
39279cc3 | 896 | if (!di || IS_ERR(di)) { |
3954401f | 897 | goto out_err; |
39279cc3 | 898 | } |
5f39d397 | 899 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 900 | out: |
39279cc3 CM |
901 | btrfs_free_path(path); |
902 | return ret; | |
3954401f CM |
903 | out_err: |
904 | location->objectid = 0; | |
905 | goto out; | |
39279cc3 CM |
906 | } |
907 | ||
908 | /* | |
909 | * when we hit a tree root in a directory, the btrfs part of the inode | |
910 | * needs to be changed to reflect the root directory of the tree root. This | |
911 | * is kind of like crossing a mount point. | |
912 | */ | |
913 | static int fixup_tree_root_location(struct btrfs_root *root, | |
914 | struct btrfs_key *location, | |
58176a96 JB |
915 | struct btrfs_root **sub_root, |
916 | struct dentry *dentry) | |
39279cc3 CM |
917 | { |
918 | struct btrfs_path *path; | |
919 | struct btrfs_root_item *ri; | |
920 | ||
921 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) | |
922 | return 0; | |
923 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) | |
924 | return 0; | |
925 | ||
926 | path = btrfs_alloc_path(); | |
927 | BUG_ON(!path); | |
928 | mutex_lock(&root->fs_info->fs_mutex); | |
929 | ||
58176a96 JB |
930 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
931 | dentry->d_name.name, | |
932 | dentry->d_name.len); | |
39279cc3 CM |
933 | if (IS_ERR(*sub_root)) |
934 | return PTR_ERR(*sub_root); | |
935 | ||
936 | ri = &(*sub_root)->root_item; | |
937 | location->objectid = btrfs_root_dirid(ri); | |
39279cc3 CM |
938 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
939 | location->offset = 0; | |
940 | ||
941 | btrfs_free_path(path); | |
942 | mutex_unlock(&root->fs_info->fs_mutex); | |
943 | return 0; | |
944 | } | |
945 | ||
946 | static int btrfs_init_locked_inode(struct inode *inode, void *p) | |
947 | { | |
948 | struct btrfs_iget_args *args = p; | |
949 | inode->i_ino = args->ino; | |
950 | BTRFS_I(inode)->root = args->root; | |
b888db2b CM |
951 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, |
952 | inode->i_mapping, GFP_NOFS); | |
39279cc3 CM |
953 | return 0; |
954 | } | |
955 | ||
956 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
957 | { | |
958 | struct btrfs_iget_args *args = opaque; | |
959 | return (args->ino == inode->i_ino && | |
960 | args->root == BTRFS_I(inode)->root); | |
961 | } | |
962 | ||
963 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, | |
964 | struct btrfs_root *root) | |
965 | { | |
966 | struct inode *inode; | |
967 | struct btrfs_iget_args args; | |
968 | args.ino = objectid; | |
969 | args.root = root; | |
970 | ||
971 | inode = iget5_locked(s, objectid, btrfs_find_actor, | |
972 | btrfs_init_locked_inode, | |
973 | (void *)&args); | |
974 | return inode; | |
975 | } | |
976 | ||
977 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, | |
978 | struct nameidata *nd) | |
979 | { | |
980 | struct inode * inode; | |
981 | struct btrfs_inode *bi = BTRFS_I(dir); | |
982 | struct btrfs_root *root = bi->root; | |
983 | struct btrfs_root *sub_root = root; | |
984 | struct btrfs_key location; | |
985 | int ret; | |
986 | ||
987 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
988 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 989 | |
39279cc3 CM |
990 | mutex_lock(&root->fs_info->fs_mutex); |
991 | ret = btrfs_inode_by_name(dir, dentry, &location); | |
992 | mutex_unlock(&root->fs_info->fs_mutex); | |
5f39d397 | 993 | |
39279cc3 CM |
994 | if (ret < 0) |
995 | return ERR_PTR(ret); | |
5f39d397 | 996 | |
39279cc3 CM |
997 | inode = NULL; |
998 | if (location.objectid) { | |
58176a96 JB |
999 | ret = fixup_tree_root_location(root, &location, &sub_root, |
1000 | dentry); | |
39279cc3 CM |
1001 | if (ret < 0) |
1002 | return ERR_PTR(ret); | |
1003 | if (ret > 0) | |
1004 | return ERR_PTR(-ENOENT); | |
1005 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, | |
1006 | sub_root); | |
1007 | if (!inode) | |
1008 | return ERR_PTR(-EACCES); | |
1009 | if (inode->i_state & I_NEW) { | |
1010 | /* the inode and parent dir are two different roots */ | |
1011 | if (sub_root != root) { | |
1012 | igrab(inode); | |
1013 | sub_root->inode = inode; | |
1014 | } | |
1015 | BTRFS_I(inode)->root = sub_root; | |
1016 | memcpy(&BTRFS_I(inode)->location, &location, | |
1017 | sizeof(location)); | |
1018 | btrfs_read_locked_inode(inode); | |
1019 | unlock_new_inode(inode); | |
1020 | } | |
1021 | } | |
1022 | return d_splice_alias(inode, dentry); | |
1023 | } | |
1024 | ||
39279cc3 CM |
1025 | static unsigned char btrfs_filetype_table[] = { |
1026 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
1027 | }; | |
1028 | ||
1029 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
1030 | { | |
1031 | struct inode *inode = filp->f_path.dentry->d_inode; | |
1032 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1033 | struct btrfs_item *item; | |
1034 | struct btrfs_dir_item *di; | |
1035 | struct btrfs_key key; | |
5f39d397 | 1036 | struct btrfs_key found_key; |
39279cc3 CM |
1037 | struct btrfs_path *path; |
1038 | int ret; | |
1039 | u32 nritems; | |
5f39d397 | 1040 | struct extent_buffer *leaf; |
39279cc3 CM |
1041 | int slot; |
1042 | int advance; | |
1043 | unsigned char d_type; | |
1044 | int over = 0; | |
1045 | u32 di_cur; | |
1046 | u32 di_total; | |
1047 | u32 di_len; | |
1048 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
1049 | char tmp_name[32]; |
1050 | char *name_ptr; | |
1051 | int name_len; | |
39279cc3 CM |
1052 | |
1053 | /* FIXME, use a real flag for deciding about the key type */ | |
1054 | if (root->fs_info->tree_root == root) | |
1055 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 1056 | |
3954401f CM |
1057 | /* special case for "." */ |
1058 | if (filp->f_pos == 0) { | |
1059 | over = filldir(dirent, ".", 1, | |
1060 | 1, inode->i_ino, | |
1061 | DT_DIR); | |
1062 | if (over) | |
1063 | return 0; | |
1064 | filp->f_pos = 1; | |
1065 | } | |
1066 | ||
39279cc3 CM |
1067 | mutex_lock(&root->fs_info->fs_mutex); |
1068 | key.objectid = inode->i_ino; | |
3954401f CM |
1069 | path = btrfs_alloc_path(); |
1070 | path->reada = 2; | |
1071 | ||
1072 | /* special case for .., just use the back ref */ | |
1073 | if (filp->f_pos == 1) { | |
1074 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); | |
1075 | key.offset = 0; | |
1076 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1077 | BUG_ON(ret == 0); | |
1078 | leaf = path->nodes[0]; | |
1079 | slot = path->slots[0]; | |
1080 | nritems = btrfs_header_nritems(leaf); | |
1081 | if (slot >= nritems) { | |
1082 | btrfs_release_path(root, path); | |
1083 | goto read_dir_items; | |
1084 | } | |
1085 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1086 | btrfs_release_path(root, path); | |
1087 | if (found_key.objectid != key.objectid || | |
1088 | found_key.type != BTRFS_INODE_REF_KEY) | |
1089 | goto read_dir_items; | |
1090 | over = filldir(dirent, "..", 2, | |
1091 | 2, found_key.offset, DT_DIR); | |
1092 | if (over) | |
1093 | goto nopos; | |
1094 | filp->f_pos = 2; | |
1095 | } | |
1096 | ||
1097 | read_dir_items: | |
39279cc3 CM |
1098 | btrfs_set_key_type(&key, key_type); |
1099 | key.offset = filp->f_pos; | |
5f39d397 | 1100 | |
39279cc3 CM |
1101 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
1102 | if (ret < 0) | |
1103 | goto err; | |
1104 | advance = 0; | |
39279cc3 | 1105 | while(1) { |
5f39d397 CM |
1106 | leaf = path->nodes[0]; |
1107 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
1108 | slot = path->slots[0]; |
1109 | if (advance || slot >= nritems) { | |
1110 | if (slot >= nritems -1) { | |
39279cc3 CM |
1111 | ret = btrfs_next_leaf(root, path); |
1112 | if (ret) | |
1113 | break; | |
5f39d397 CM |
1114 | leaf = path->nodes[0]; |
1115 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
1116 | slot = path->slots[0]; |
1117 | } else { | |
1118 | slot++; | |
1119 | path->slots[0]++; | |
1120 | } | |
1121 | } | |
1122 | advance = 1; | |
5f39d397 CM |
1123 | item = btrfs_item_nr(leaf, slot); |
1124 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1125 | ||
1126 | if (found_key.objectid != key.objectid) | |
39279cc3 | 1127 | break; |
5f39d397 | 1128 | if (btrfs_key_type(&found_key) != key_type) |
39279cc3 | 1129 | break; |
5f39d397 | 1130 | if (found_key.offset < filp->f_pos) |
39279cc3 | 1131 | continue; |
5f39d397 CM |
1132 | |
1133 | filp->f_pos = found_key.offset; | |
39279cc3 CM |
1134 | advance = 1; |
1135 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
1136 | di_cur = 0; | |
5f39d397 | 1137 | di_total = btrfs_item_size(leaf, item); |
39279cc3 | 1138 | while(di_cur < di_total) { |
5f39d397 CM |
1139 | struct btrfs_key location; |
1140 | ||
1141 | name_len = btrfs_dir_name_len(leaf, di); | |
1142 | if (name_len < 32) { | |
1143 | name_ptr = tmp_name; | |
1144 | } else { | |
1145 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
1146 | BUG_ON(!name_ptr); | |
1147 | } | |
1148 | read_extent_buffer(leaf, name_ptr, | |
1149 | (unsigned long)(di + 1), name_len); | |
1150 | ||
1151 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
1152 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
1153 | ||
1154 | over = filldir(dirent, name_ptr, name_len, | |
1155 | found_key.offset, | |
1156 | location.objectid, | |
39279cc3 | 1157 | d_type); |
5f39d397 CM |
1158 | |
1159 | if (name_ptr != tmp_name) | |
1160 | kfree(name_ptr); | |
1161 | ||
39279cc3 CM |
1162 | if (over) |
1163 | goto nopos; | |
5103e947 JB |
1164 | di_len = btrfs_dir_name_len(leaf, di) + |
1165 | btrfs_dir_data_len(leaf, di) +sizeof(*di); | |
39279cc3 CM |
1166 | di_cur += di_len; |
1167 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
1168 | } | |
1169 | } | |
1170 | filp->f_pos++; | |
1171 | nopos: | |
1172 | ret = 0; | |
1173 | err: | |
1174 | btrfs_release_path(root, path); | |
1175 | btrfs_free_path(path); | |
1176 | mutex_unlock(&root->fs_info->fs_mutex); | |
1177 | return ret; | |
1178 | } | |
1179 | ||
1180 | int btrfs_write_inode(struct inode *inode, int wait) | |
1181 | { | |
1182 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1183 | struct btrfs_trans_handle *trans; | |
1184 | int ret = 0; | |
1185 | ||
1186 | if (wait) { | |
1187 | mutex_lock(&root->fs_info->fs_mutex); | |
1188 | trans = btrfs_start_transaction(root, 1); | |
1189 | btrfs_set_trans_block_group(trans, inode); | |
1190 | ret = btrfs_commit_transaction(trans, root); | |
1191 | mutex_unlock(&root->fs_info->fs_mutex); | |
1192 | } | |
1193 | return ret; | |
1194 | } | |
1195 | ||
1196 | /* | |
54aa1f4d | 1197 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
1198 | * inode changes. But, it is most likely to find the inode in cache. |
1199 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
1200 | * to keep or drop this code. | |
1201 | */ | |
1202 | void btrfs_dirty_inode(struct inode *inode) | |
1203 | { | |
1204 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1205 | struct btrfs_trans_handle *trans; | |
1206 | ||
1207 | mutex_lock(&root->fs_info->fs_mutex); | |
1208 | trans = btrfs_start_transaction(root, 1); | |
1209 | btrfs_set_trans_block_group(trans, inode); | |
1210 | btrfs_update_inode(trans, root, inode); | |
1211 | btrfs_end_transaction(trans, root); | |
1212 | mutex_unlock(&root->fs_info->fs_mutex); | |
39279cc3 CM |
1213 | } |
1214 | ||
1215 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |
1216 | struct btrfs_root *root, | |
1217 | u64 objectid, | |
1218 | struct btrfs_block_group_cache *group, | |
1219 | int mode) | |
1220 | { | |
1221 | struct inode *inode; | |
5f39d397 | 1222 | struct btrfs_inode_item *inode_item; |
39279cc3 | 1223 | struct btrfs_key *location; |
5f39d397 | 1224 | struct btrfs_path *path; |
39279cc3 CM |
1225 | int ret; |
1226 | int owner; | |
1227 | ||
5f39d397 CM |
1228 | path = btrfs_alloc_path(); |
1229 | BUG_ON(!path); | |
1230 | ||
39279cc3 CM |
1231 | inode = new_inode(root->fs_info->sb); |
1232 | if (!inode) | |
1233 | return ERR_PTR(-ENOMEM); | |
1234 | ||
b888db2b CM |
1235 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, |
1236 | inode->i_mapping, GFP_NOFS); | |
39279cc3 | 1237 | BTRFS_I(inode)->root = root; |
b888db2b | 1238 | |
39279cc3 CM |
1239 | if (mode & S_IFDIR) |
1240 | owner = 0; | |
1241 | else | |
1242 | owner = 1; | |
1243 | group = btrfs_find_block_group(root, group, 0, 0, owner); | |
1244 | BTRFS_I(inode)->block_group = group; | |
1245 | ||
5f39d397 CM |
1246 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
1247 | if (ret) | |
1248 | goto fail; | |
1249 | ||
39279cc3 CM |
1250 | inode->i_uid = current->fsuid; |
1251 | inode->i_gid = current->fsgid; | |
1252 | inode->i_mode = mode; | |
1253 | inode->i_ino = objectid; | |
1254 | inode->i_blocks = 0; | |
1255 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
5f39d397 CM |
1256 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
1257 | struct btrfs_inode_item); | |
1258 | fill_inode_item(path->nodes[0], inode_item, inode); | |
1259 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
1260 | btrfs_free_path(path); | |
1261 | ||
39279cc3 CM |
1262 | location = &BTRFS_I(inode)->location; |
1263 | location->objectid = objectid; | |
39279cc3 CM |
1264 | location->offset = 0; |
1265 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
1266 | ||
39279cc3 CM |
1267 | insert_inode_hash(inode); |
1268 | return inode; | |
5f39d397 CM |
1269 | fail: |
1270 | btrfs_free_path(path); | |
1271 | return ERR_PTR(ret); | |
39279cc3 CM |
1272 | } |
1273 | ||
1274 | static inline u8 btrfs_inode_type(struct inode *inode) | |
1275 | { | |
1276 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
1277 | } | |
1278 | ||
1279 | static int btrfs_add_link(struct btrfs_trans_handle *trans, | |
1280 | struct dentry *dentry, struct inode *inode) | |
1281 | { | |
1282 | int ret; | |
1283 | struct btrfs_key key; | |
1284 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; | |
79c44584 | 1285 | struct inode *parent_inode; |
5f39d397 | 1286 | |
39279cc3 | 1287 | key.objectid = inode->i_ino; |
39279cc3 CM |
1288 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
1289 | key.offset = 0; | |
1290 | ||
1291 | ret = btrfs_insert_dir_item(trans, root, | |
1292 | dentry->d_name.name, dentry->d_name.len, | |
1293 | dentry->d_parent->d_inode->i_ino, | |
1294 | &key, btrfs_inode_type(inode)); | |
1295 | if (ret == 0) { | |
3954401f CM |
1296 | if (!S_ISLNK(inode->i_mode)) { |
1297 | ret = btrfs_insert_inode_ref(trans, root, | |
1298 | dentry->d_name.name, | |
1299 | dentry->d_name.len, | |
1300 | inode->i_ino, | |
1301 | dentry->d_parent->d_inode->i_ino); | |
1302 | } | |
79c44584 CM |
1303 | parent_inode = dentry->d_parent->d_inode; |
1304 | parent_inode->i_size += dentry->d_name.len * 2; | |
1305 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; | |
39279cc3 CM |
1306 | ret = btrfs_update_inode(trans, root, |
1307 | dentry->d_parent->d_inode); | |
1308 | } | |
1309 | return ret; | |
1310 | } | |
1311 | ||
1312 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
1313 | struct dentry *dentry, struct inode *inode) | |
1314 | { | |
1315 | int err = btrfs_add_link(trans, dentry, inode); | |
1316 | if (!err) { | |
1317 | d_instantiate(dentry, inode); | |
1318 | return 0; | |
1319 | } | |
1320 | if (err > 0) | |
1321 | err = -EEXIST; | |
1322 | return err; | |
1323 | } | |
1324 | ||
618e21d5 JB |
1325 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1326 | int mode, dev_t rdev) | |
1327 | { | |
1328 | struct btrfs_trans_handle *trans; | |
1329 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1330 | struct inode *inode; | |
1331 | int err; | |
1332 | int drop_inode = 0; | |
1333 | u64 objectid; | |
d3c2fdcf | 1334 | unsigned long nr; |
618e21d5 JB |
1335 | |
1336 | if (!new_valid_dev(rdev)) | |
1337 | return -EINVAL; | |
1338 | ||
1339 | mutex_lock(&root->fs_info->fs_mutex); | |
1340 | trans = btrfs_start_transaction(root, 1); | |
1341 | btrfs_set_trans_block_group(trans, dir); | |
1342 | ||
1343 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
1344 | if (err) { | |
1345 | err = -ENOSPC; | |
1346 | goto out_unlock; | |
1347 | } | |
1348 | ||
1349 | inode = btrfs_new_inode(trans, root, objectid, | |
1350 | BTRFS_I(dir)->block_group, mode); | |
1351 | err = PTR_ERR(inode); | |
1352 | if (IS_ERR(inode)) | |
1353 | goto out_unlock; | |
1354 | ||
1355 | btrfs_set_trans_block_group(trans, inode); | |
1356 | err = btrfs_add_nondir(trans, dentry, inode); | |
1357 | if (err) | |
1358 | drop_inode = 1; | |
1359 | else { | |
1360 | inode->i_op = &btrfs_special_inode_operations; | |
1361 | init_special_inode(inode, inode->i_mode, rdev); | |
1b4ab1bb | 1362 | btrfs_update_inode(trans, root, inode); |
618e21d5 JB |
1363 | } |
1364 | dir->i_sb->s_dirt = 1; | |
1365 | btrfs_update_inode_block_group(trans, inode); | |
1366 | btrfs_update_inode_block_group(trans, dir); | |
1367 | out_unlock: | |
d3c2fdcf | 1368 | nr = trans->blocks_used; |
618e21d5 JB |
1369 | btrfs_end_transaction(trans, root); |
1370 | mutex_unlock(&root->fs_info->fs_mutex); | |
1371 | ||
1372 | if (drop_inode) { | |
1373 | inode_dec_link_count(inode); | |
1374 | iput(inode); | |
1375 | } | |
d3c2fdcf | 1376 | btrfs_btree_balance_dirty(root, nr); |
618e21d5 JB |
1377 | return err; |
1378 | } | |
1379 | ||
39279cc3 CM |
1380 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
1381 | int mode, struct nameidata *nd) | |
1382 | { | |
1383 | struct btrfs_trans_handle *trans; | |
1384 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1385 | struct inode *inode; | |
1386 | int err; | |
1387 | int drop_inode = 0; | |
d3c2fdcf | 1388 | unsigned long nr; |
39279cc3 CM |
1389 | u64 objectid; |
1390 | ||
1391 | mutex_lock(&root->fs_info->fs_mutex); | |
1392 | trans = btrfs_start_transaction(root, 1); | |
1393 | btrfs_set_trans_block_group(trans, dir); | |
1394 | ||
1395 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
1396 | if (err) { | |
1397 | err = -ENOSPC; | |
1398 | goto out_unlock; | |
1399 | } | |
1400 | ||
1401 | inode = btrfs_new_inode(trans, root, objectid, | |
1402 | BTRFS_I(dir)->block_group, mode); | |
1403 | err = PTR_ERR(inode); | |
1404 | if (IS_ERR(inode)) | |
1405 | goto out_unlock; | |
1406 | ||
1407 | btrfs_set_trans_block_group(trans, inode); | |
1408 | err = btrfs_add_nondir(trans, dentry, inode); | |
1409 | if (err) | |
1410 | drop_inode = 1; | |
1411 | else { | |
1412 | inode->i_mapping->a_ops = &btrfs_aops; | |
1413 | inode->i_fop = &btrfs_file_operations; | |
1414 | inode->i_op = &btrfs_file_inode_operations; | |
a52d9a80 CM |
1415 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, |
1416 | inode->i_mapping, GFP_NOFS); | |
07157aac | 1417 | BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops; |
39279cc3 CM |
1418 | } |
1419 | dir->i_sb->s_dirt = 1; | |
1420 | btrfs_update_inode_block_group(trans, inode); | |
1421 | btrfs_update_inode_block_group(trans, dir); | |
1422 | out_unlock: | |
d3c2fdcf | 1423 | nr = trans->blocks_used; |
39279cc3 CM |
1424 | btrfs_end_transaction(trans, root); |
1425 | mutex_unlock(&root->fs_info->fs_mutex); | |
1426 | ||
1427 | if (drop_inode) { | |
1428 | inode_dec_link_count(inode); | |
1429 | iput(inode); | |
1430 | } | |
d3c2fdcf | 1431 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
1432 | return err; |
1433 | } | |
1434 | ||
1435 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
1436 | struct dentry *dentry) | |
1437 | { | |
1438 | struct btrfs_trans_handle *trans; | |
1439 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1440 | struct inode *inode = old_dentry->d_inode; | |
d3c2fdcf | 1441 | unsigned long nr; |
39279cc3 CM |
1442 | int err; |
1443 | int drop_inode = 0; | |
1444 | ||
1445 | if (inode->i_nlink == 0) | |
1446 | return -ENOENT; | |
1447 | ||
1448 | inc_nlink(inode); | |
1449 | mutex_lock(&root->fs_info->fs_mutex); | |
1450 | trans = btrfs_start_transaction(root, 1); | |
5f39d397 | 1451 | |
39279cc3 CM |
1452 | btrfs_set_trans_block_group(trans, dir); |
1453 | atomic_inc(&inode->i_count); | |
1454 | err = btrfs_add_nondir(trans, dentry, inode); | |
5f39d397 | 1455 | |
39279cc3 CM |
1456 | if (err) |
1457 | drop_inode = 1; | |
5f39d397 | 1458 | |
39279cc3 CM |
1459 | dir->i_sb->s_dirt = 1; |
1460 | btrfs_update_inode_block_group(trans, dir); | |
54aa1f4d | 1461 | err = btrfs_update_inode(trans, root, inode); |
5f39d397 | 1462 | |
54aa1f4d CM |
1463 | if (err) |
1464 | drop_inode = 1; | |
39279cc3 | 1465 | |
d3c2fdcf | 1466 | nr = trans->blocks_used; |
39279cc3 CM |
1467 | btrfs_end_transaction(trans, root); |
1468 | mutex_unlock(&root->fs_info->fs_mutex); | |
1469 | ||
1470 | if (drop_inode) { | |
1471 | inode_dec_link_count(inode); | |
1472 | iput(inode); | |
1473 | } | |
d3c2fdcf | 1474 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
1475 | return err; |
1476 | } | |
1477 | ||
39279cc3 CM |
1478 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
1479 | { | |
1480 | struct inode *inode; | |
1481 | struct btrfs_trans_handle *trans; | |
1482 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1483 | int err = 0; | |
1484 | int drop_on_err = 0; | |
1485 | u64 objectid; | |
d3c2fdcf | 1486 | unsigned long nr = 1; |
39279cc3 CM |
1487 | |
1488 | mutex_lock(&root->fs_info->fs_mutex); | |
1489 | trans = btrfs_start_transaction(root, 1); | |
1490 | btrfs_set_trans_block_group(trans, dir); | |
5f39d397 | 1491 | |
39279cc3 CM |
1492 | if (IS_ERR(trans)) { |
1493 | err = PTR_ERR(trans); | |
1494 | goto out_unlock; | |
1495 | } | |
1496 | ||
1497 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
1498 | if (err) { | |
1499 | err = -ENOSPC; | |
1500 | goto out_unlock; | |
1501 | } | |
1502 | ||
1503 | inode = btrfs_new_inode(trans, root, objectid, | |
1504 | BTRFS_I(dir)->block_group, S_IFDIR | mode); | |
1505 | if (IS_ERR(inode)) { | |
1506 | err = PTR_ERR(inode); | |
1507 | goto out_fail; | |
1508 | } | |
5f39d397 | 1509 | |
39279cc3 CM |
1510 | drop_on_err = 1; |
1511 | inode->i_op = &btrfs_dir_inode_operations; | |
1512 | inode->i_fop = &btrfs_dir_file_operations; | |
1513 | btrfs_set_trans_block_group(trans, inode); | |
1514 | ||
3954401f | 1515 | inode->i_size = 0; |
39279cc3 CM |
1516 | err = btrfs_update_inode(trans, root, inode); |
1517 | if (err) | |
1518 | goto out_fail; | |
5f39d397 | 1519 | |
39279cc3 CM |
1520 | err = btrfs_add_link(trans, dentry, inode); |
1521 | if (err) | |
1522 | goto out_fail; | |
5f39d397 | 1523 | |
39279cc3 CM |
1524 | d_instantiate(dentry, inode); |
1525 | drop_on_err = 0; | |
1526 | dir->i_sb->s_dirt = 1; | |
1527 | btrfs_update_inode_block_group(trans, inode); | |
1528 | btrfs_update_inode_block_group(trans, dir); | |
1529 | ||
1530 | out_fail: | |
d3c2fdcf | 1531 | nr = trans->blocks_used; |
39279cc3 | 1532 | btrfs_end_transaction(trans, root); |
5f39d397 | 1533 | |
39279cc3 CM |
1534 | out_unlock: |
1535 | mutex_unlock(&root->fs_info->fs_mutex); | |
1536 | if (drop_on_err) | |
1537 | iput(inode); | |
d3c2fdcf | 1538 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
1539 | return err; |
1540 | } | |
1541 | ||
a52d9a80 CM |
1542 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
1543 | size_t page_offset, u64 start, u64 end, | |
1544 | int create) | |
1545 | { | |
1546 | int ret; | |
1547 | int err = 0; | |
db94535d | 1548 | u64 bytenr; |
a52d9a80 CM |
1549 | u64 extent_start = 0; |
1550 | u64 extent_end = 0; | |
1551 | u64 objectid = inode->i_ino; | |
1552 | u32 found_type; | |
1553 | int failed_insert = 0; | |
1554 | struct btrfs_path *path; | |
1555 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1556 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
1557 | struct extent_buffer *leaf; |
1558 | struct btrfs_key found_key; | |
a52d9a80 CM |
1559 | struct extent_map *em = NULL; |
1560 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
1561 | struct btrfs_trans_handle *trans = NULL; | |
1562 | ||
1563 | path = btrfs_alloc_path(); | |
1564 | BUG_ON(!path); | |
1565 | mutex_lock(&root->fs_info->fs_mutex); | |
1566 | ||
1567 | again: | |
1568 | em = lookup_extent_mapping(em_tree, start, end); | |
1569 | if (em) { | |
1570 | goto out; | |
1571 | } | |
1572 | if (!em) { | |
1573 | em = alloc_extent_map(GFP_NOFS); | |
1574 | if (!em) { | |
1575 | err = -ENOMEM; | |
1576 | goto out; | |
1577 | } | |
5f39d397 CM |
1578 | em->start = EXTENT_MAP_HOLE; |
1579 | em->end = EXTENT_MAP_HOLE; | |
a52d9a80 CM |
1580 | } |
1581 | em->bdev = inode->i_sb->s_bdev; | |
179e29e4 CM |
1582 | ret = btrfs_lookup_file_extent(trans, root, path, |
1583 | objectid, start, trans != NULL); | |
a52d9a80 CM |
1584 | if (ret < 0) { |
1585 | err = ret; | |
1586 | goto out; | |
1587 | } | |
1588 | ||
1589 | if (ret != 0) { | |
1590 | if (path->slots[0] == 0) | |
1591 | goto not_found; | |
1592 | path->slots[0]--; | |
1593 | } | |
1594 | ||
5f39d397 CM |
1595 | leaf = path->nodes[0]; |
1596 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 1597 | struct btrfs_file_extent_item); |
a52d9a80 | 1598 | /* are we inside the extent that was found? */ |
5f39d397 CM |
1599 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1600 | found_type = btrfs_key_type(&found_key); | |
1601 | if (found_key.objectid != objectid || | |
a52d9a80 CM |
1602 | found_type != BTRFS_EXTENT_DATA_KEY) { |
1603 | goto not_found; | |
1604 | } | |
1605 | ||
5f39d397 CM |
1606 | found_type = btrfs_file_extent_type(leaf, item); |
1607 | extent_start = found_key.offset; | |
a52d9a80 CM |
1608 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
1609 | extent_end = extent_start + | |
db94535d | 1610 | btrfs_file_extent_num_bytes(leaf, item); |
a52d9a80 | 1611 | err = 0; |
b888db2b | 1612 | if (start < extent_start || start >= extent_end) { |
a52d9a80 CM |
1613 | em->start = start; |
1614 | if (start < extent_start) { | |
b888db2b CM |
1615 | if (end < extent_start) |
1616 | goto not_found; | |
a52d9a80 CM |
1617 | em->end = extent_end - 1; |
1618 | } else { | |
1619 | em->end = end; | |
1620 | } | |
1621 | goto not_found_em; | |
1622 | } | |
db94535d CM |
1623 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
1624 | if (bytenr == 0) { | |
a52d9a80 CM |
1625 | em->start = extent_start; |
1626 | em->end = extent_end - 1; | |
5f39d397 CM |
1627 | em->block_start = EXTENT_MAP_HOLE; |
1628 | em->block_end = EXTENT_MAP_HOLE; | |
a52d9a80 CM |
1629 | goto insert; |
1630 | } | |
db94535d CM |
1631 | bytenr += btrfs_file_extent_offset(leaf, item); |
1632 | em->block_start = bytenr; | |
a52d9a80 | 1633 | em->block_end = em->block_start + |
db94535d | 1634 | btrfs_file_extent_num_bytes(leaf, item) - 1; |
a52d9a80 CM |
1635 | em->start = extent_start; |
1636 | em->end = extent_end - 1; | |
1637 | goto insert; | |
1638 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 1639 | unsigned long ptr; |
a52d9a80 | 1640 | char *map; |
3326d1b0 CM |
1641 | size_t size; |
1642 | size_t extent_offset; | |
1643 | size_t copy_size; | |
a52d9a80 | 1644 | |
5f39d397 CM |
1645 | size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf, |
1646 | path->slots[0])); | |
689f9346 | 1647 | extent_end = (extent_start + size - 1) | |
db94535d | 1648 | ((u64)root->sectorsize - 1); |
b888db2b | 1649 | if (start < extent_start || start >= extent_end) { |
a52d9a80 CM |
1650 | em->start = start; |
1651 | if (start < extent_start) { | |
b888db2b CM |
1652 | if (end < extent_start) |
1653 | goto not_found; | |
50b78c24 | 1654 | em->end = extent_end; |
a52d9a80 CM |
1655 | } else { |
1656 | em->end = end; | |
1657 | } | |
1658 | goto not_found_em; | |
1659 | } | |
689f9346 Y |
1660 | em->block_start = EXTENT_MAP_INLINE; |
1661 | em->block_end = EXTENT_MAP_INLINE; | |
1662 | ||
1663 | if (!page) { | |
1664 | em->start = extent_start; | |
1665 | em->end = extent_start + size - 1; | |
1666 | goto out; | |
1667 | } | |
5f39d397 | 1668 | |
35ebb934 | 1669 | extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) - |
689f9346 | 1670 | extent_start + page_offset; |
ae2f5411 | 1671 | copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset, |
3326d1b0 | 1672 | size - extent_offset); |
3326d1b0 CM |
1673 | em->start = extent_start + extent_offset; |
1674 | em->end = (em->start + copy_size -1) | | |
1675 | ((u64)root->sectorsize -1); | |
689f9346 Y |
1676 | map = kmap(page); |
1677 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; | |
179e29e4 CM |
1678 | if (create == 0 && !PageUptodate(page)) { |
1679 | read_extent_buffer(leaf, map + page_offset, ptr, | |
1680 | copy_size); | |
1681 | flush_dcache_page(page); | |
1682 | } else if (create && PageUptodate(page)) { | |
1683 | if (!trans) { | |
1684 | kunmap(page); | |
1685 | free_extent_map(em); | |
1686 | em = NULL; | |
1687 | btrfs_release_path(root, path); | |
1688 | trans = btrfs_start_transaction(root, 1); | |
1689 | goto again; | |
1690 | } | |
1691 | write_extent_buffer(leaf, map + page_offset, ptr, | |
1692 | copy_size); | |
1693 | btrfs_mark_buffer_dirty(leaf); | |
a52d9a80 | 1694 | } |
a52d9a80 | 1695 | kunmap(page); |
3326d1b0 | 1696 | set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS); |
a52d9a80 CM |
1697 | goto insert; |
1698 | } else { | |
1699 | printk("unkknown found_type %d\n", found_type); | |
1700 | WARN_ON(1); | |
1701 | } | |
1702 | not_found: | |
1703 | em->start = start; | |
1704 | em->end = end; | |
1705 | not_found_em: | |
5f39d397 CM |
1706 | em->block_start = EXTENT_MAP_HOLE; |
1707 | em->block_end = EXTENT_MAP_HOLE; | |
a52d9a80 CM |
1708 | insert: |
1709 | btrfs_release_path(root, path); | |
1710 | if (em->start > start || em->end < start) { | |
b888db2b | 1711 | printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end); |
a52d9a80 CM |
1712 | err = -EIO; |
1713 | goto out; | |
1714 | } | |
1715 | ret = add_extent_mapping(em_tree, em); | |
1716 | if (ret == -EEXIST) { | |
1717 | free_extent_map(em); | |
2bf5a725 | 1718 | em = NULL; |
a52d9a80 CM |
1719 | failed_insert++; |
1720 | if (failed_insert > 5) { | |
1721 | printk("failing to insert %Lu %Lu\n", start, end); | |
1722 | err = -EIO; | |
1723 | goto out; | |
1724 | } | |
a52d9a80 CM |
1725 | goto again; |
1726 | } | |
1727 | err = 0; | |
1728 | out: | |
1729 | btrfs_free_path(path); | |
1730 | if (trans) { | |
1731 | ret = btrfs_end_transaction(trans, root); | |
1732 | if (!err) | |
1733 | err = ret; | |
1734 | } | |
1735 | mutex_unlock(&root->fs_info->fs_mutex); | |
1736 | if (err) { | |
1737 | free_extent_map(em); | |
1738 | WARN_ON(1); | |
1739 | return ERR_PTR(err); | |
1740 | } | |
1741 | return em; | |
1742 | } | |
1743 | ||
d396c6f5 | 1744 | static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock) |
39279cc3 | 1745 | { |
d396c6f5 | 1746 | return extent_bmap(mapping, iblock, btrfs_get_extent); |
39279cc3 CM |
1747 | } |
1748 | ||
1749 | static int btrfs_prepare_write(struct file *file, struct page *page, | |
1750 | unsigned from, unsigned to) | |
1751 | { | |
a52d9a80 CM |
1752 | return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree, |
1753 | page->mapping->host, page, from, to, | |
1754 | btrfs_get_extent); | |
39279cc3 CM |
1755 | } |
1756 | ||
a52d9a80 | 1757 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 1758 | { |
a52d9a80 CM |
1759 | struct extent_map_tree *tree; |
1760 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
1761 | return extent_read_full_page(tree, page, btrfs_get_extent); | |
9ebefb18 | 1762 | } |
a52d9a80 | 1763 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 1764 | { |
a52d9a80 | 1765 | struct extent_map_tree *tree; |
b888db2b CM |
1766 | |
1767 | ||
1768 | if (current->flags & PF_MEMALLOC) { | |
1769 | redirty_page_for_writepage(wbc, page); | |
1770 | unlock_page(page); | |
1771 | return 0; | |
1772 | } | |
a52d9a80 CM |
1773 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
1774 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); | |
9ebefb18 CM |
1775 | } |
1776 | ||
b293f02e CM |
1777 | static int btrfs_writepages(struct address_space *mapping, |
1778 | struct writeback_control *wbc) | |
1779 | { | |
1780 | struct extent_map_tree *tree; | |
1781 | tree = &BTRFS_I(mapping->host)->extent_tree; | |
1782 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); | |
1783 | } | |
1784 | ||
3ab2fb5a CM |
1785 | static int |
1786 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
1787 | struct list_head *pages, unsigned nr_pages) | |
1788 | { | |
1789 | struct extent_map_tree *tree; | |
1790 | tree = &BTRFS_I(mapping->host)->extent_tree; | |
1791 | return extent_readpages(tree, mapping, pages, nr_pages, | |
1792 | btrfs_get_extent); | |
1793 | } | |
1794 | ||
a52d9a80 | 1795 | static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags) |
9ebefb18 | 1796 | { |
a52d9a80 CM |
1797 | struct extent_map_tree *tree; |
1798 | int ret; | |
8c2383c3 | 1799 | |
a52d9a80 CM |
1800 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
1801 | ret = try_release_extent_mapping(tree, page); | |
1802 | if (ret == 1) { | |
1803 | ClearPagePrivate(page); | |
1804 | set_page_private(page, 0); | |
1805 | page_cache_release(page); | |
39279cc3 | 1806 | } |
a52d9a80 | 1807 | return ret; |
39279cc3 CM |
1808 | } |
1809 | ||
a52d9a80 | 1810 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
39279cc3 | 1811 | { |
a52d9a80 | 1812 | struct extent_map_tree *tree; |
39279cc3 | 1813 | |
a52d9a80 CM |
1814 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
1815 | extent_invalidatepage(tree, page, offset); | |
1816 | btrfs_releasepage(page, GFP_NOFS); | |
39279cc3 CM |
1817 | } |
1818 | ||
9ebefb18 CM |
1819 | /* |
1820 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
1821 | * called from a page fault handler when a page is first dirtied. Hence we must | |
1822 | * be careful to check for EOF conditions here. We set the page up correctly | |
1823 | * for a written page which means we get ENOSPC checking when writing into | |
1824 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
1825 | * support these features. | |
1826 | * | |
1827 | * We are not allowed to take the i_mutex here so we have to play games to | |
1828 | * protect against truncate races as the page could now be beyond EOF. Because | |
1829 | * vmtruncate() writes the inode size before removing pages, once we have the | |
1830 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
1831 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
1832 | * unlock the page. | |
1833 | */ | |
1834 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) | |
1835 | { | |
1836 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; | |
1837 | unsigned long end; | |
1838 | loff_t size; | |
1839 | int ret = -EINVAL; | |
a52d9a80 | 1840 | u64 page_start; |
9ebefb18 | 1841 | |
011410bd | 1842 | down_read(&BTRFS_I(inode)->root->snap_sem); |
9ebefb18 CM |
1843 | lock_page(page); |
1844 | wait_on_page_writeback(page); | |
9ebefb18 | 1845 | size = i_size_read(inode); |
35ebb934 | 1846 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
a52d9a80 | 1847 | |
9ebefb18 | 1848 | if ((page->mapping != inode->i_mapping) || |
a52d9a80 | 1849 | (page_start > size)) { |
9ebefb18 CM |
1850 | /* page got truncated out from underneath us */ |
1851 | goto out_unlock; | |
1852 | } | |
1853 | ||
1854 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 1855 | if (page_start + PAGE_CACHE_SIZE > size) |
9ebefb18 CM |
1856 | end = size & ~PAGE_CACHE_MASK; |
1857 | else | |
1858 | end = PAGE_CACHE_SIZE; | |
1859 | ||
b888db2b | 1860 | ret = btrfs_cow_one_page(inode, page, end); |
9ebefb18 CM |
1861 | |
1862 | out_unlock: | |
011410bd | 1863 | up_read(&BTRFS_I(inode)->root->snap_sem); |
9ebefb18 CM |
1864 | unlock_page(page); |
1865 | return ret; | |
1866 | } | |
1867 | ||
39279cc3 CM |
1868 | static void btrfs_truncate(struct inode *inode) |
1869 | { | |
1870 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1871 | int ret; | |
1872 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 1873 | unsigned long nr; |
39279cc3 CM |
1874 | |
1875 | if (!S_ISREG(inode->i_mode)) | |
1876 | return; | |
1877 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
1878 | return; | |
1879 | ||
1880 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | |
1881 | ||
1882 | mutex_lock(&root->fs_info->fs_mutex); | |
1883 | trans = btrfs_start_transaction(root, 1); | |
1884 | btrfs_set_trans_block_group(trans, inode); | |
1885 | ||
1886 | /* FIXME, add redo link to tree so we don't leak on crash */ | |
1887 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
39279cc3 | 1888 | btrfs_update_inode(trans, root, inode); |
d3c2fdcf | 1889 | nr = trans->blocks_used; |
5f39d397 | 1890 | |
39279cc3 CM |
1891 | ret = btrfs_end_transaction(trans, root); |
1892 | BUG_ON(ret); | |
1893 | mutex_unlock(&root->fs_info->fs_mutex); | |
d3c2fdcf | 1894 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
1895 | } |
1896 | ||
1897 | int btrfs_commit_write(struct file *file, struct page *page, | |
1898 | unsigned from, unsigned to) | |
1899 | { | |
a52d9a80 CM |
1900 | return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree, |
1901 | page->mapping->host, page, from, to); | |
39279cc3 CM |
1902 | } |
1903 | ||
1904 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) | |
1905 | { | |
1906 | struct btrfs_trans_handle *trans; | |
1907 | struct btrfs_key key; | |
1908 | struct btrfs_root_item root_item; | |
1909 | struct btrfs_inode_item *inode_item; | |
5f39d397 | 1910 | struct extent_buffer *leaf; |
39279cc3 CM |
1911 | struct btrfs_root *new_root; |
1912 | struct inode *inode; | |
1913 | struct inode *dir; | |
1914 | int ret; | |
54aa1f4d | 1915 | int err; |
39279cc3 CM |
1916 | u64 objectid; |
1917 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
d3c2fdcf | 1918 | unsigned long nr = 1; |
39279cc3 CM |
1919 | |
1920 | mutex_lock(&root->fs_info->fs_mutex); | |
1921 | trans = btrfs_start_transaction(root, 1); | |
1922 | BUG_ON(!trans); | |
1923 | ||
7bb86316 CM |
1924 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
1925 | 0, &objectid); | |
1926 | if (ret) | |
1927 | goto fail; | |
1928 | ||
1929 | leaf = __btrfs_alloc_free_block(trans, root, root->leafsize, | |
1930 | objectid, trans->transid, 0, 0, | |
1931 | 0, 0); | |
5f39d397 CM |
1932 | if (IS_ERR(leaf)) |
1933 | return PTR_ERR(leaf); | |
1934 | ||
1935 | btrfs_set_header_nritems(leaf, 0); | |
1936 | btrfs_set_header_level(leaf, 0); | |
db94535d | 1937 | btrfs_set_header_bytenr(leaf, leaf->start); |
5f39d397 | 1938 | btrfs_set_header_generation(leaf, trans->transid); |
7bb86316 CM |
1939 | btrfs_set_header_owner(leaf, objectid); |
1940 | ||
5f39d397 CM |
1941 | write_extent_buffer(leaf, root->fs_info->fsid, |
1942 | (unsigned long)btrfs_header_fsid(leaf), | |
1943 | BTRFS_FSID_SIZE); | |
1944 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 CM |
1945 | |
1946 | inode_item = &root_item.inode; | |
1947 | memset(inode_item, 0, sizeof(*inode_item)); | |
5f39d397 CM |
1948 | inode_item->generation = cpu_to_le64(1); |
1949 | inode_item->size = cpu_to_le64(3); | |
1950 | inode_item->nlink = cpu_to_le32(1); | |
1951 | inode_item->nblocks = cpu_to_le64(1); | |
1952 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); | |
39279cc3 | 1953 | |
db94535d CM |
1954 | btrfs_set_root_bytenr(&root_item, leaf->start); |
1955 | btrfs_set_root_level(&root_item, 0); | |
39279cc3 | 1956 | btrfs_set_root_refs(&root_item, 1); |
5f39d397 CM |
1957 | btrfs_set_root_used(&root_item, 0); |
1958 | ||
5eda7b5e CM |
1959 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
1960 | root_item.drop_level = 0; | |
5f39d397 CM |
1961 | |
1962 | free_extent_buffer(leaf); | |
1963 | leaf = NULL; | |
39279cc3 | 1964 | |
39279cc3 CM |
1965 | btrfs_set_root_dirid(&root_item, new_dirid); |
1966 | ||
1967 | key.objectid = objectid; | |
1968 | key.offset = 1; | |
39279cc3 CM |
1969 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
1970 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
1971 | &root_item); | |
54aa1f4d CM |
1972 | if (ret) |
1973 | goto fail; | |
39279cc3 CM |
1974 | |
1975 | /* | |
1976 | * insert the directory item | |
1977 | */ | |
1978 | key.offset = (u64)-1; | |
1979 | dir = root->fs_info->sb->s_root->d_inode; | |
1980 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
1981 | name, namelen, dir->i_ino, &key, | |
1982 | BTRFS_FT_DIR); | |
54aa1f4d CM |
1983 | if (ret) |
1984 | goto fail; | |
39279cc3 | 1985 | |
3954401f CM |
1986 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
1987 | name, namelen, objectid, | |
1988 | root->fs_info->sb->s_root->d_inode->i_ino); | |
1989 | if (ret) | |
1990 | goto fail; | |
1991 | ||
39279cc3 | 1992 | ret = btrfs_commit_transaction(trans, root); |
54aa1f4d CM |
1993 | if (ret) |
1994 | goto fail_commit; | |
39279cc3 | 1995 | |
58176a96 | 1996 | new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen); |
39279cc3 CM |
1997 | BUG_ON(!new_root); |
1998 | ||
1999 | trans = btrfs_start_transaction(new_root, 1); | |
2000 | BUG_ON(!trans); | |
2001 | ||
2002 | inode = btrfs_new_inode(trans, new_root, new_dirid, | |
2003 | BTRFS_I(dir)->block_group, S_IFDIR | 0700); | |
54aa1f4d CM |
2004 | if (IS_ERR(inode)) |
2005 | goto fail; | |
39279cc3 CM |
2006 | inode->i_op = &btrfs_dir_inode_operations; |
2007 | inode->i_fop = &btrfs_dir_file_operations; | |
34088780 | 2008 | new_root->inode = inode; |
39279cc3 | 2009 | |
3954401f CM |
2010 | ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid, |
2011 | new_dirid); | |
39279cc3 | 2012 | inode->i_nlink = 1; |
3954401f | 2013 | inode->i_size = 0; |
39279cc3 | 2014 | ret = btrfs_update_inode(trans, new_root, inode); |
54aa1f4d CM |
2015 | if (ret) |
2016 | goto fail; | |
2017 | fail: | |
d3c2fdcf | 2018 | nr = trans->blocks_used; |
54aa1f4d CM |
2019 | err = btrfs_commit_transaction(trans, root); |
2020 | if (err && !ret) | |
2021 | ret = err; | |
2022 | fail_commit: | |
39279cc3 | 2023 | mutex_unlock(&root->fs_info->fs_mutex); |
d3c2fdcf | 2024 | btrfs_btree_balance_dirty(root, nr); |
54aa1f4d | 2025 | return ret; |
39279cc3 CM |
2026 | } |
2027 | ||
2028 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) | |
2029 | { | |
2030 | struct btrfs_trans_handle *trans; | |
2031 | struct btrfs_key key; | |
2032 | struct btrfs_root_item new_root_item; | |
5f39d397 | 2033 | struct extent_buffer *tmp; |
39279cc3 | 2034 | int ret; |
54aa1f4d | 2035 | int err; |
39279cc3 | 2036 | u64 objectid; |
d3c2fdcf | 2037 | unsigned long nr; |
39279cc3 CM |
2038 | |
2039 | if (!root->ref_cows) | |
2040 | return -EINVAL; | |
2041 | ||
011410bd CM |
2042 | down_write(&root->snap_sem); |
2043 | freeze_bdev(root->fs_info->sb->s_bdev); | |
2044 | thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb); | |
2045 | ||
39279cc3 CM |
2046 | mutex_lock(&root->fs_info->fs_mutex); |
2047 | trans = btrfs_start_transaction(root, 1); | |
2048 | BUG_ON(!trans); | |
2049 | ||
2050 | ret = btrfs_update_inode(trans, root, root->inode); | |
54aa1f4d CM |
2051 | if (ret) |
2052 | goto fail; | |
39279cc3 CM |
2053 | |
2054 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, | |
2055 | 0, &objectid); | |
54aa1f4d CM |
2056 | if (ret) |
2057 | goto fail; | |
39279cc3 CM |
2058 | |
2059 | memcpy(&new_root_item, &root->root_item, | |
2060 | sizeof(new_root_item)); | |
2061 | ||
2062 | key.objectid = objectid; | |
2063 | key.offset = 1; | |
39279cc3 | 2064 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
9691975d | 2065 | extent_buffer_get(root->node); |
83df7c1d | 2066 | btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); |
9691975d | 2067 | free_extent_buffer(tmp); |
db94535d CM |
2068 | btrfs_set_root_bytenr(&new_root_item, root->node->start); |
2069 | btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node)); | |
39279cc3 CM |
2070 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
2071 | &new_root_item); | |
54aa1f4d CM |
2072 | if (ret) |
2073 | goto fail; | |
39279cc3 CM |
2074 | |
2075 | /* | |
2076 | * insert the directory item | |
2077 | */ | |
2078 | key.offset = (u64)-1; | |
2079 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
2080 | name, namelen, | |
2081 | root->fs_info->sb->s_root->d_inode->i_ino, | |
2082 | &key, BTRFS_FT_DIR); | |
2083 | ||
54aa1f4d CM |
2084 | if (ret) |
2085 | goto fail; | |
39279cc3 | 2086 | |
3954401f CM |
2087 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
2088 | name, namelen, objectid, | |
2089 | root->fs_info->sb->s_root->d_inode->i_ino); | |
2090 | ||
2091 | if (ret) | |
2092 | goto fail; | |
2093 | ||
7bb86316 | 2094 | ret = btrfs_inc_root_ref(trans, root, objectid); |
54aa1f4d CM |
2095 | if (ret) |
2096 | goto fail; | |
54aa1f4d | 2097 | fail: |
d3c2fdcf | 2098 | nr = trans->blocks_used; |
54aa1f4d | 2099 | err = btrfs_commit_transaction(trans, root); |
5f39d397 | 2100 | |
54aa1f4d CM |
2101 | if (err && !ret) |
2102 | ret = err; | |
5f39d397 | 2103 | |
39279cc3 | 2104 | mutex_unlock(&root->fs_info->fs_mutex); |
011410bd | 2105 | up_write(&root->snap_sem); |
d3c2fdcf | 2106 | btrfs_btree_balance_dirty(root, nr); |
54aa1f4d | 2107 | return ret; |
39279cc3 CM |
2108 | } |
2109 | ||
86479a04 CM |
2110 | static unsigned long force_ra(struct address_space *mapping, |
2111 | struct file_ra_state *ra, struct file *file, | |
2112 | pgoff_t offset, pgoff_t last_index) | |
2113 | { | |
2114 | pgoff_t req_size; | |
2115 | ||
2116 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) | |
2117 | req_size = last_index - offset + 1; | |
2118 | offset = page_cache_readahead(mapping, ra, file, offset, req_size); | |
2119 | return offset; | |
2120 | #else | |
2121 | req_size = min(last_index - offset + 1, (pgoff_t)128); | |
2122 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); | |
2123 | return offset + req_size; | |
2124 | #endif | |
2125 | } | |
2126 | ||
2127 | int btrfs_defrag_file(struct file *file) { | |
2128 | struct inode *inode = file->f_path.dentry->d_inode; | |
2129 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
2130 | struct page *page; | |
2131 | unsigned long last_index; | |
2132 | unsigned long ra_index = 0; | |
2133 | u64 page_start; | |
2134 | u64 page_end; | |
2135 | unsigned long i; | |
2136 | ||
2137 | mutex_lock(&inode->i_mutex); | |
2138 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; | |
2139 | for (i = 0; i <= last_index; i++) { | |
2140 | if (i == ra_index) { | |
2141 | ra_index = force_ra(inode->i_mapping, &file->f_ra, | |
2142 | file, ra_index, last_index); | |
2143 | } | |
2144 | page = grab_cache_page(inode->i_mapping, i); | |
2145 | if (!page) | |
2146 | goto out_unlock; | |
2147 | if (!PageUptodate(page)) { | |
2148 | btrfs_readpage(NULL, page); | |
2149 | lock_page(page); | |
2150 | if (!PageUptodate(page)) { | |
2151 | unlock_page(page); | |
2152 | page_cache_release(page); | |
2153 | goto out_unlock; | |
2154 | } | |
2155 | } | |
35ebb934 | 2156 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
86479a04 CM |
2157 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
2158 | ||
2159 | lock_extent(em_tree, page_start, page_end, GFP_NOFS); | |
2160 | set_extent_delalloc(em_tree, page_start, | |
2161 | page_end, GFP_NOFS); | |
2162 | unlock_extent(em_tree, page_start, page_end, GFP_NOFS); | |
2163 | set_page_dirty(page); | |
2164 | unlock_page(page); | |
2165 | page_cache_release(page); | |
2166 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); | |
2167 | } | |
2168 | ||
2169 | out_unlock: | |
2170 | mutex_unlock(&inode->i_mutex); | |
2171 | return 0; | |
2172 | } | |
2173 | ||
d03581f4 | 2174 | static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg) |
39279cc3 | 2175 | { |
39279cc3 | 2176 | struct btrfs_ioctl_vol_args vol_args; |
39279cc3 | 2177 | struct btrfs_dir_item *di; |
39279cc3 | 2178 | struct btrfs_path *path; |
d03581f4 | 2179 | int namelen; |
39279cc3 CM |
2180 | u64 root_dirid; |
2181 | ||
d03581f4 CH |
2182 | if (copy_from_user(&vol_args, arg, sizeof(vol_args))) |
2183 | return -EFAULT; | |
5f39d397 | 2184 | |
d03581f4 CH |
2185 | namelen = strlen(vol_args.name); |
2186 | if (namelen > BTRFS_VOL_NAME_MAX) | |
2187 | return -EINVAL; | |
2188 | if (strchr(vol_args.name, '/')) | |
2189 | return -EINVAL; | |
2190 | ||
2191 | path = btrfs_alloc_path(); | |
2192 | if (!path) | |
2193 | return -ENOMEM; | |
2194 | ||
2195 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, | |
2196 | mutex_lock(&root->fs_info->fs_mutex); | |
2197 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, | |
2198 | path, root_dirid, | |
2199 | vol_args.name, namelen, 0); | |
2200 | mutex_unlock(&root->fs_info->fs_mutex); | |
2201 | btrfs_free_path(path); | |
2202 | if (di && !IS_ERR(di)) | |
2203 | return -EEXIST; | |
2204 | if (IS_ERR(di)) | |
2205 | return PTR_ERR(di); | |
2206 | ||
2207 | if (root == root->fs_info->tree_root) | |
2208 | return create_subvol(root, vol_args.name, namelen); | |
2209 | return create_snapshot(root, vol_args.name, namelen); | |
2210 | } | |
2211 | ||
2212 | static int btrfs_ioctl_defrag(struct file *file) | |
2213 | { | |
2214 | struct inode *inode = file->f_path.dentry->d_inode; | |
2215 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2216 | ||
2217 | switch (inode->i_mode & S_IFMT) { | |
2218 | case S_IFDIR: | |
39279cc3 | 2219 | mutex_lock(&root->fs_info->fs_mutex); |
d03581f4 CH |
2220 | btrfs_defrag_root(root, 0); |
2221 | btrfs_defrag_root(root->fs_info->extent_root, 0); | |
39279cc3 | 2222 | mutex_unlock(&root->fs_info->fs_mutex); |
39279cc3 | 2223 | break; |
d03581f4 CH |
2224 | case S_IFREG: |
2225 | btrfs_defrag_file(file); | |
2226 | break; | |
2227 | } | |
2228 | ||
2229 | return 0; | |
2230 | } | |
6702ed49 | 2231 | |
d03581f4 CH |
2232 | long btrfs_ioctl(struct file *file, unsigned int |
2233 | cmd, unsigned long arg) | |
2234 | { | |
2235 | struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root; | |
2236 | ||
2237 | switch (cmd) { | |
2238 | case BTRFS_IOC_SNAP_CREATE: | |
2239 | return btrfs_ioctl_snap_create(root, (void __user *)arg); | |
6702ed49 | 2240 | case BTRFS_IOC_DEFRAG: |
d03581f4 | 2241 | return btrfs_ioctl_defrag(file); |
39279cc3 | 2242 | } |
d03581f4 CH |
2243 | |
2244 | return -ENOTTY; | |
39279cc3 CM |
2245 | } |
2246 | ||
39279cc3 CM |
2247 | /* |
2248 | * Called inside transaction, so use GFP_NOFS | |
2249 | */ | |
2250 | struct inode *btrfs_alloc_inode(struct super_block *sb) | |
2251 | { | |
2252 | struct btrfs_inode *ei; | |
2253 | ||
2254 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
2255 | if (!ei) | |
2256 | return NULL; | |
15ee9bc7 | 2257 | ei->last_trans = 0; |
39279cc3 CM |
2258 | return &ei->vfs_inode; |
2259 | } | |
2260 | ||
2261 | void btrfs_destroy_inode(struct inode *inode) | |
2262 | { | |
2263 | WARN_ON(!list_empty(&inode->i_dentry)); | |
2264 | WARN_ON(inode->i_data.nrpages); | |
2265 | ||
2266 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
2267 | } | |
2268 | ||
44ec0b71 CM |
2269 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
2270 | static void init_once(struct kmem_cache * cachep, void *foo) | |
2271 | #else | |
39279cc3 CM |
2272 | static void init_once(void * foo, struct kmem_cache * cachep, |
2273 | unsigned long flags) | |
44ec0b71 | 2274 | #endif |
39279cc3 CM |
2275 | { |
2276 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
2277 | ||
2278 | inode_init_once(&ei->vfs_inode); | |
2279 | } | |
2280 | ||
2281 | void btrfs_destroy_cachep(void) | |
2282 | { | |
2283 | if (btrfs_inode_cachep) | |
2284 | kmem_cache_destroy(btrfs_inode_cachep); | |
2285 | if (btrfs_trans_handle_cachep) | |
2286 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
2287 | if (btrfs_transaction_cachep) | |
2288 | kmem_cache_destroy(btrfs_transaction_cachep); | |
2289 | if (btrfs_bit_radix_cachep) | |
2290 | kmem_cache_destroy(btrfs_bit_radix_cachep); | |
2291 | if (btrfs_path_cachep) | |
2292 | kmem_cache_destroy(btrfs_path_cachep); | |
2293 | } | |
2294 | ||
86479a04 | 2295 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
92fee66d | 2296 | unsigned long extra_flags, |
44ec0b71 CM |
2297 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
2298 | void (*ctor)(struct kmem_cache *, void *) | |
2299 | #else | |
92fee66d | 2300 | void (*ctor)(void *, struct kmem_cache *, |
44ec0b71 CM |
2301 | unsigned long) |
2302 | #endif | |
2303 | ) | |
92fee66d CM |
2304 | { |
2305 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | | |
2306 | SLAB_MEM_SPREAD | extra_flags), ctor | |
2307 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) | |
2308 | ,NULL | |
2309 | #endif | |
2310 | ); | |
2311 | } | |
2312 | ||
39279cc3 CM |
2313 | int btrfs_init_cachep(void) |
2314 | { | |
86479a04 | 2315 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
92fee66d CM |
2316 | sizeof(struct btrfs_inode), |
2317 | 0, init_once); | |
39279cc3 CM |
2318 | if (!btrfs_inode_cachep) |
2319 | goto fail; | |
86479a04 CM |
2320 | btrfs_trans_handle_cachep = |
2321 | btrfs_cache_create("btrfs_trans_handle_cache", | |
2322 | sizeof(struct btrfs_trans_handle), | |
2323 | 0, NULL); | |
39279cc3 CM |
2324 | if (!btrfs_trans_handle_cachep) |
2325 | goto fail; | |
86479a04 | 2326 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
39279cc3 | 2327 | sizeof(struct btrfs_transaction), |
92fee66d | 2328 | 0, NULL); |
39279cc3 CM |
2329 | if (!btrfs_transaction_cachep) |
2330 | goto fail; | |
86479a04 | 2331 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
23223584 | 2332 | sizeof(struct btrfs_path), |
92fee66d | 2333 | 0, NULL); |
39279cc3 CM |
2334 | if (!btrfs_path_cachep) |
2335 | goto fail; | |
86479a04 | 2336 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
92fee66d | 2337 | SLAB_DESTROY_BY_RCU, NULL); |
39279cc3 CM |
2338 | if (!btrfs_bit_radix_cachep) |
2339 | goto fail; | |
2340 | return 0; | |
2341 | fail: | |
2342 | btrfs_destroy_cachep(); | |
2343 | return -ENOMEM; | |
2344 | } | |
2345 | ||
2346 | static int btrfs_getattr(struct vfsmount *mnt, | |
2347 | struct dentry *dentry, struct kstat *stat) | |
2348 | { | |
2349 | struct inode *inode = dentry->d_inode; | |
2350 | generic_fillattr(inode, stat); | |
2351 | stat->blksize = 256 * 1024; | |
2352 | return 0; | |
2353 | } | |
2354 | ||
2355 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, | |
2356 | struct inode * new_dir,struct dentry *new_dentry) | |
2357 | { | |
2358 | struct btrfs_trans_handle *trans; | |
2359 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
2360 | struct inode *new_inode = new_dentry->d_inode; | |
2361 | struct inode *old_inode = old_dentry->d_inode; | |
2362 | struct timespec ctime = CURRENT_TIME; | |
2363 | struct btrfs_path *path; | |
39279cc3 CM |
2364 | int ret; |
2365 | ||
2366 | if (S_ISDIR(old_inode->i_mode) && new_inode && | |
2367 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { | |
2368 | return -ENOTEMPTY; | |
2369 | } | |
5f39d397 | 2370 | |
39279cc3 CM |
2371 | mutex_lock(&root->fs_info->fs_mutex); |
2372 | trans = btrfs_start_transaction(root, 1); | |
5f39d397 | 2373 | |
39279cc3 CM |
2374 | btrfs_set_trans_block_group(trans, new_dir); |
2375 | path = btrfs_alloc_path(); | |
2376 | if (!path) { | |
2377 | ret = -ENOMEM; | |
2378 | goto out_fail; | |
2379 | } | |
2380 | ||
2381 | old_dentry->d_inode->i_nlink++; | |
2382 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
2383 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
2384 | old_inode->i_ctime = ctime; | |
5f39d397 | 2385 | |
39279cc3 CM |
2386 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); |
2387 | if (ret) | |
2388 | goto out_fail; | |
2389 | ||
2390 | if (new_inode) { | |
2391 | new_inode->i_ctime = CURRENT_TIME; | |
2392 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); | |
2393 | if (ret) | |
2394 | goto out_fail; | |
39279cc3 CM |
2395 | } |
2396 | ret = btrfs_add_link(trans, new_dentry, old_inode); | |
2397 | if (ret) | |
2398 | goto out_fail; | |
2399 | ||
2400 | out_fail: | |
2401 | btrfs_free_path(path); | |
2402 | btrfs_end_transaction(trans, root); | |
2403 | mutex_unlock(&root->fs_info->fs_mutex); | |
2404 | return ret; | |
2405 | } | |
2406 | ||
2407 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, | |
2408 | const char *symname) | |
2409 | { | |
2410 | struct btrfs_trans_handle *trans; | |
2411 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2412 | struct btrfs_path *path; | |
2413 | struct btrfs_key key; | |
2414 | struct inode *inode; | |
2415 | int err; | |
2416 | int drop_inode = 0; | |
2417 | u64 objectid; | |
2418 | int name_len; | |
2419 | int datasize; | |
5f39d397 | 2420 | unsigned long ptr; |
39279cc3 | 2421 | struct btrfs_file_extent_item *ei; |
5f39d397 | 2422 | struct extent_buffer *leaf; |
d3c2fdcf | 2423 | unsigned long nr; |
39279cc3 CM |
2424 | |
2425 | name_len = strlen(symname) + 1; | |
2426 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) | |
2427 | return -ENAMETOOLONG; | |
2428 | mutex_lock(&root->fs_info->fs_mutex); | |
2429 | trans = btrfs_start_transaction(root, 1); | |
2430 | btrfs_set_trans_block_group(trans, dir); | |
2431 | ||
2432 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
2433 | if (err) { | |
2434 | err = -ENOSPC; | |
2435 | goto out_unlock; | |
2436 | } | |
2437 | ||
2438 | inode = btrfs_new_inode(trans, root, objectid, | |
2439 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO); | |
2440 | err = PTR_ERR(inode); | |
2441 | if (IS_ERR(inode)) | |
2442 | goto out_unlock; | |
2443 | ||
2444 | btrfs_set_trans_block_group(trans, inode); | |
2445 | err = btrfs_add_nondir(trans, dentry, inode); | |
2446 | if (err) | |
2447 | drop_inode = 1; | |
2448 | else { | |
2449 | inode->i_mapping->a_ops = &btrfs_aops; | |
2450 | inode->i_fop = &btrfs_file_operations; | |
2451 | inode->i_op = &btrfs_file_inode_operations; | |
a52d9a80 CM |
2452 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, |
2453 | inode->i_mapping, GFP_NOFS); | |
07157aac | 2454 | BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops; |
39279cc3 CM |
2455 | } |
2456 | dir->i_sb->s_dirt = 1; | |
2457 | btrfs_update_inode_block_group(trans, inode); | |
2458 | btrfs_update_inode_block_group(trans, dir); | |
2459 | if (drop_inode) | |
2460 | goto out_unlock; | |
2461 | ||
2462 | path = btrfs_alloc_path(); | |
2463 | BUG_ON(!path); | |
2464 | key.objectid = inode->i_ino; | |
2465 | key.offset = 0; | |
39279cc3 CM |
2466 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
2467 | datasize = btrfs_file_extent_calc_inline_size(name_len); | |
2468 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
2469 | datasize); | |
54aa1f4d CM |
2470 | if (err) { |
2471 | drop_inode = 1; | |
2472 | goto out_unlock; | |
2473 | } | |
5f39d397 CM |
2474 | leaf = path->nodes[0]; |
2475 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
2476 | struct btrfs_file_extent_item); | |
2477 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
2478 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 CM |
2479 | BTRFS_FILE_EXTENT_INLINE); |
2480 | ptr = btrfs_file_extent_inline_start(ei); | |
5f39d397 CM |
2481 | write_extent_buffer(leaf, symname, ptr, name_len); |
2482 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 2483 | btrfs_free_path(path); |
5f39d397 | 2484 | |
39279cc3 CM |
2485 | inode->i_op = &btrfs_symlink_inode_operations; |
2486 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
2487 | inode->i_size = name_len - 1; | |
54aa1f4d CM |
2488 | err = btrfs_update_inode(trans, root, inode); |
2489 | if (err) | |
2490 | drop_inode = 1; | |
39279cc3 CM |
2491 | |
2492 | out_unlock: | |
d3c2fdcf | 2493 | nr = trans->blocks_used; |
39279cc3 CM |
2494 | btrfs_end_transaction(trans, root); |
2495 | mutex_unlock(&root->fs_info->fs_mutex); | |
39279cc3 CM |
2496 | if (drop_inode) { |
2497 | inode_dec_link_count(inode); | |
2498 | iput(inode); | |
2499 | } | |
d3c2fdcf | 2500 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
2501 | return err; |
2502 | } | |
2503 | ||
2504 | static struct inode_operations btrfs_dir_inode_operations = { | |
2505 | .lookup = btrfs_lookup, | |
2506 | .create = btrfs_create, | |
2507 | .unlink = btrfs_unlink, | |
2508 | .link = btrfs_link, | |
2509 | .mkdir = btrfs_mkdir, | |
2510 | .rmdir = btrfs_rmdir, | |
2511 | .rename = btrfs_rename, | |
2512 | .symlink = btrfs_symlink, | |
2513 | .setattr = btrfs_setattr, | |
618e21d5 | 2514 | .mknod = btrfs_mknod, |
5103e947 JB |
2515 | .setxattr = generic_setxattr, |
2516 | .getxattr = generic_getxattr, | |
2517 | .listxattr = btrfs_listxattr, | |
2518 | .removexattr = generic_removexattr, | |
39279cc3 CM |
2519 | }; |
2520 | ||
2521 | static struct inode_operations btrfs_dir_ro_inode_operations = { | |
2522 | .lookup = btrfs_lookup, | |
2523 | }; | |
2524 | ||
2525 | static struct file_operations btrfs_dir_file_operations = { | |
2526 | .llseek = generic_file_llseek, | |
2527 | .read = generic_read_dir, | |
2528 | .readdir = btrfs_readdir, | |
34287aa3 | 2529 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 2530 | #ifdef CONFIG_COMPAT |
34287aa3 | 2531 | .compat_ioctl = btrfs_ioctl, |
39279cc3 CM |
2532 | #endif |
2533 | }; | |
2534 | ||
07157aac CM |
2535 | static struct extent_map_ops btrfs_extent_map_ops = { |
2536 | .fill_delalloc = run_delalloc_range, | |
2537 | .writepage_io_hook = btrfs_writepage_io_hook, | |
2538 | .readpage_io_hook = btrfs_readpage_io_hook, | |
2539 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, | |
2540 | }; | |
2541 | ||
39279cc3 CM |
2542 | static struct address_space_operations btrfs_aops = { |
2543 | .readpage = btrfs_readpage, | |
2544 | .writepage = btrfs_writepage, | |
b293f02e | 2545 | .writepages = btrfs_writepages, |
3ab2fb5a | 2546 | .readpages = btrfs_readpages, |
39279cc3 CM |
2547 | .sync_page = block_sync_page, |
2548 | .prepare_write = btrfs_prepare_write, | |
2549 | .commit_write = btrfs_commit_write, | |
2550 | .bmap = btrfs_bmap, | |
a52d9a80 CM |
2551 | .invalidatepage = btrfs_invalidatepage, |
2552 | .releasepage = btrfs_releasepage, | |
2553 | .set_page_dirty = __set_page_dirty_nobuffers, | |
39279cc3 CM |
2554 | }; |
2555 | ||
2556 | static struct address_space_operations btrfs_symlink_aops = { | |
2557 | .readpage = btrfs_readpage, | |
2558 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
2559 | .invalidatepage = btrfs_invalidatepage, |
2560 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
2561 | }; |
2562 | ||
2563 | static struct inode_operations btrfs_file_inode_operations = { | |
2564 | .truncate = btrfs_truncate, | |
2565 | .getattr = btrfs_getattr, | |
2566 | .setattr = btrfs_setattr, | |
5103e947 JB |
2567 | .setxattr = generic_setxattr, |
2568 | .getxattr = generic_getxattr, | |
2569 | .listxattr = btrfs_listxattr, | |
2570 | .removexattr = generic_removexattr, | |
39279cc3 CM |
2571 | }; |
2572 | ||
618e21d5 JB |
2573 | static struct inode_operations btrfs_special_inode_operations = { |
2574 | .getattr = btrfs_getattr, | |
2575 | .setattr = btrfs_setattr, | |
2576 | }; | |
2577 | ||
39279cc3 CM |
2578 | static struct inode_operations btrfs_symlink_inode_operations = { |
2579 | .readlink = generic_readlink, | |
2580 | .follow_link = page_follow_link_light, | |
2581 | .put_link = page_put_link, | |
2582 | }; |