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