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