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