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