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