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