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