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