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