Btrfs: remove device tree
[linux-2.6-block.git] / fs / btrfs / super.c
CommitLineData
2e635a27 1#include <linux/module.h>
e20d96d6 2#include <linux/buffer_head.h>
2e635a27
CM
3#include <linux/fs.h>
4#include <linux/pagemap.h>
5#include <linux/highmem.h>
6#include <linux/time.h>
7#include <linux/init.h>
8#include <linux/string.h>
9#include <linux/smp_lock.h>
10#include <linux/backing-dev.h>
dee26a9f 11#include <linux/mpage.h>
75dfe396
CM
12#include <linux/swap.h>
13#include <linux/writeback.h>
8fd17795 14#include <linux/statfs.h>
08607c1b 15#include <linux/compat.h>
2e635a27 16#include "ctree.h"
e20d96d6 17#include "disk-io.h"
d5719762 18#include "transaction.h"
2c90e5d6 19#include "btrfs_inode.h"
c5739bba 20#include "ioctl.h"
3a686375 21#include "print-tree.h"
2e635a27 22
c5739bba
CM
23struct btrfs_iget_args {
24 u64 ino;
25 struct btrfs_root *root;
26};
27
2e635a27 28#define BTRFS_SUPER_MAGIC 0x9123682E
e20d96d6
CM
29
30static struct inode_operations btrfs_dir_inode_operations;
2b8d99a7 31static struct inode_operations btrfs_symlink_inode_operations;
d6e4a428 32static struct inode_operations btrfs_dir_ro_inode_operations;
e20d96d6
CM
33static struct super_operations btrfs_super_ops;
34static struct file_operations btrfs_dir_file_operations;
dee26a9f
CM
35static struct inode_operations btrfs_file_inode_operations;
36static struct address_space_operations btrfs_aops;
2b8d99a7 37static struct address_space_operations btrfs_symlink_aops;
dee26a9f 38static struct file_operations btrfs_file_operations;
e20d96d6 39
1de037a4
CM
40static int drop_extents(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 struct inode *inode,
43 u64 start, u64 end, u64 *hint_block);
44static int btrfs_get_block(struct inode *inode, sector_t iblock,
45 struct buffer_head *result, int create);
46
fabb5681
CM
47
48#define S_SHIFT 12
49static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
50 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
51 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
52 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
53 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
54 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
55 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
56 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
57};
58
e20d96d6 59static void btrfs_read_locked_inode(struct inode *inode)
2e635a27 60{
5caf2a00 61 struct btrfs_path *path;
e20d96d6 62 struct btrfs_inode_item *inode_item;
d6e4a428
CM
63 struct btrfs_root *root = BTRFS_I(inode)->root;
64 struct btrfs_key location;
31f3c99b
CM
65 struct btrfs_block_group_cache *alloc_group;
66 u64 alloc_group_block;
e20d96d6 67 int ret;
f4b9aa8d 68
5caf2a00
CM
69 path = btrfs_alloc_path();
70 BUG_ON(!path);
71 btrfs_init_path(path);
f4b9aa8d
CM
72 mutex_lock(&root->fs_info->fs_mutex);
73
d6e4a428
CM
74 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
75 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
e20d96d6 76 if (ret) {
7cfcc17e 77 btrfs_free_path(path);
d6e4a428 78 goto make_bad;
e20d96d6 79 }
5caf2a00
CM
80 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
81 path->slots[0],
e20d96d6 82 struct btrfs_inode_item);
2e635a27 83
e20d96d6
CM
84 inode->i_mode = btrfs_inode_mode(inode_item);
85 inode->i_nlink = btrfs_inode_nlink(inode_item);
86 inode->i_uid = btrfs_inode_uid(inode_item);
87 inode->i_gid = btrfs_inode_gid(inode_item);
88 inode->i_size = btrfs_inode_size(inode_item);
89 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
90 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
91 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
92 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
93 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
94 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
95 inode->i_blocks = btrfs_inode_nblocks(inode_item);
96 inode->i_generation = btrfs_inode_generation(inode_item);
31f3c99b
CM
97 alloc_group_block = btrfs_inode_block_group(inode_item);
98 ret = radix_tree_gang_lookup(&root->fs_info->block_group_radix,
99 (void **)&alloc_group,
100 alloc_group_block, 1);
101 BUG_ON(!ret);
102 BTRFS_I(inode)->block_group = alloc_group;
5caf2a00 103
5caf2a00
CM
104 btrfs_free_path(path);
105 inode_item = NULL;
106
f4b9aa8d 107 mutex_unlock(&root->fs_info->fs_mutex);
1b05da2e 108
e20d96d6
CM
109 switch (inode->i_mode & S_IFMT) {
110#if 0
111 default:
112 init_special_inode(inode, inode->i_mode,
113 btrfs_inode_rdev(inode_item));
114 break;
115#endif
116 case S_IFREG:
dee26a9f
CM
117 inode->i_mapping->a_ops = &btrfs_aops;
118 inode->i_fop = &btrfs_file_operations;
119 inode->i_op = &btrfs_file_inode_operations;
e20d96d6
CM
120 break;
121 case S_IFDIR:
e20d96d6 122 inode->i_fop = &btrfs_dir_file_operations;
d6e4a428
CM
123 if (root == root->fs_info->tree_root)
124 inode->i_op = &btrfs_dir_ro_inode_operations;
125 else
126 inode->i_op = &btrfs_dir_inode_operations;
e20d96d6
CM
127 break;
128 case S_IFLNK:
2b8d99a7
CM
129 inode->i_op = &btrfs_symlink_inode_operations;
130 inode->i_mapping->a_ops = &btrfs_symlink_aops;
e20d96d6 131 break;
2e635a27 132 }
e20d96d6 133 return;
d6e4a428
CM
134
135make_bad:
136 btrfs_release_path(root, path);
137 btrfs_free_path(path);
138 mutex_unlock(&root->fs_info->fs_mutex);
139 make_bad_inode(inode);
2e635a27
CM
140}
141
f68cad0f
CM
142static void fill_inode_item(struct btrfs_inode_item *item,
143 struct inode *inode)
144{
145 btrfs_set_inode_uid(item, inode->i_uid);
146 btrfs_set_inode_gid(item, inode->i_gid);
147 btrfs_set_inode_size(item, inode->i_size);
148 btrfs_set_inode_mode(item, inode->i_mode);
149 btrfs_set_inode_nlink(item, inode->i_nlink);
150 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
151 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
152 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
153 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
154 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
155 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
156 btrfs_set_inode_nblocks(item, inode->i_blocks);
157 btrfs_set_inode_generation(item, inode->i_generation);
31f3c99b
CM
158 btrfs_set_inode_block_group(item,
159 BTRFS_I(inode)->block_group->key.objectid);
f68cad0f
CM
160}
161
f68cad0f
CM
162static int btrfs_update_inode(struct btrfs_trans_handle *trans,
163 struct btrfs_root *root,
164 struct inode *inode)
165{
166 struct btrfs_inode_item *inode_item;
167 struct btrfs_path *path;
168 int ret;
169
170 path = btrfs_alloc_path();
171 BUG_ON(!path);
172 btrfs_init_path(path);
173 ret = btrfs_lookup_inode(trans, root, path,
174 &BTRFS_I(inode)->location, 1);
175 if (ret) {
176 if (ret > 0)
177 ret = -ENOENT;
178 goto failed;
179 }
180
181 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
182 path->slots[0],
183 struct btrfs_inode_item);
184
185 fill_inode_item(inode_item, inode);
186 btrfs_mark_buffer_dirty(path->nodes[0]);
187 ret = 0;
188failed:
189 btrfs_release_path(root, path);
190 btrfs_free_path(path);
191 return ret;
192}
193
194
5f443fd2
CM
195static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
196 struct btrfs_root *root,
197 struct inode *dir,
198 struct dentry *dentry)
134e9731 199{
5caf2a00 200 struct btrfs_path *path;
134e9731
CM
201 const char *name = dentry->d_name.name;
202 int name_len = dentry->d_name.len;
7e38180e 203 int ret = 0;
134e9731
CM
204 u64 objectid;
205 struct btrfs_dir_item *di;
206
5caf2a00
CM
207 path = btrfs_alloc_path();
208 BUG_ON(!path);
209 btrfs_init_path(path);
7e38180e 210 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
134e9731 211 name, name_len, -1);
7e38180e
CM
212 if (IS_ERR(di)) {
213 ret = PTR_ERR(di);
134e9731 214 goto err;
7e38180e
CM
215 }
216 if (!di) {
134e9731
CM
217 ret = -ENOENT;
218 goto err;
219 }
d6e4a428 220 objectid = btrfs_disk_key_objectid(&di->location);
7e38180e
CM
221 ret = btrfs_delete_one_dir_name(trans, root, path, di);
222 BUG_ON(ret);
223 btrfs_release_path(root, path);
134e9731 224
7e38180e
CM
225 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
226 objectid, name, name_len, -1);
227 if (IS_ERR(di)) {
228 ret = PTR_ERR(di);
229 goto err;
230 }
231 if (!di) {
232 ret = -ENOENT;
233 goto err;
234 }
235 ret = btrfs_delete_one_dir_name(trans, root, path, di);
5f26f772
CM
236 BUG_ON(ret);
237
134e9731
CM
238 dentry->d_inode->i_ctime = dir->i_ctime;
239err:
5caf2a00 240 btrfs_free_path(path);
f68cad0f 241 if (!ret) {
5f26f772 242 dir->i_size -= name_len * 2;
f68cad0f
CM
243 btrfs_update_inode(trans, root, dir);
244 drop_nlink(dentry->d_inode);
245 btrfs_update_inode(trans, root, dentry->d_inode);
cd1bc465 246 dir->i_sb->s_dirt = 1;
d4dbff95 247 }
134e9731
CM
248 return ret;
249}
250
5f443fd2
CM
251static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
252{
253 struct btrfs_root *root;
254 struct btrfs_trans_handle *trans;
255 int ret;
256
d6e4a428 257 root = BTRFS_I(dir)->root;
5f443fd2
CM
258 mutex_lock(&root->fs_info->fs_mutex);
259 trans = btrfs_start_transaction(root, 1);
31f3c99b 260 btrfs_set_trans_block_group(trans, dir);
5f443fd2
CM
261 ret = btrfs_unlink_trans(trans, root, dir, dentry);
262 btrfs_end_transaction(trans, root);
263 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 264 btrfs_btree_balance_dirty(root);
5f443fd2
CM
265 return ret;
266}
267
268static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
269{
270 struct inode *inode = dentry->d_inode;
271 int err;
272 int ret;
d6e4a428 273 struct btrfs_root *root = BTRFS_I(dir)->root;
5caf2a00 274 struct btrfs_path *path;
5f443fd2
CM
275 struct btrfs_key key;
276 struct btrfs_trans_handle *trans;
5f26f772
CM
277 struct btrfs_key found_key;
278 int found_type;
5f443fd2 279 struct btrfs_leaf *leaf;
5f26f772 280 char *goodnames = "..";
5f443fd2 281
5caf2a00
CM
282 path = btrfs_alloc_path();
283 BUG_ON(!path);
284 btrfs_init_path(path);
5f443fd2
CM
285 mutex_lock(&root->fs_info->fs_mutex);
286 trans = btrfs_start_transaction(root, 1);
31f3c99b 287 btrfs_set_trans_block_group(trans, dir);
5f443fd2
CM
288 key.objectid = inode->i_ino;
289 key.offset = (u64)-1;
5f26f772
CM
290 key.flags = (u32)-1;
291 while(1) {
292 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
293 if (ret < 0) {
294 err = ret;
295 goto out;
296 }
297 BUG_ON(ret == 0);
298 if (path->slots[0] == 0) {
299 err = -ENOENT;
300 goto out;
301 }
302 path->slots[0]--;
303 leaf = btrfs_buffer_leaf(path->nodes[0]);
304 btrfs_disk_key_to_cpu(&found_key,
305 &leaf->items[path->slots[0]].key);
306 found_type = btrfs_key_type(&found_key);
307 if (found_key.objectid != inode->i_ino) {
308 err = -ENOENT;
309 goto out;
310 }
311 if ((found_type != BTRFS_DIR_ITEM_KEY &&
312 found_type != BTRFS_DIR_INDEX_KEY) ||
313 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
314 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
315 err = -ENOTEMPTY;
316 goto out;
317 }
318 ret = btrfs_del_item(trans, root, path);
319 BUG_ON(ret);
5f443fd2 320
5f26f772
CM
321 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
322 break;
323 btrfs_release_path(root, path);
5f443fd2 324 }
5f26f772 325 ret = 0;
5caf2a00 326 btrfs_release_path(root, path);
5f443fd2
CM
327
328 /* now the directory is empty */
329 err = btrfs_unlink_trans(trans, root, dir, dentry);
330 if (!err) {
331 inode->i_size = 0;
332 }
333out:
7cfcc17e
CM
334 btrfs_release_path(root, path);
335 btrfs_free_path(path);
5f443fd2
CM
336 mutex_unlock(&root->fs_info->fs_mutex);
337 ret = btrfs_end_transaction(trans, root);
35b7e476 338 btrfs_btree_balance_dirty(root);
5f443fd2
CM
339 if (ret && !err)
340 err = ret;
341 return err;
342}
343
134e9731
CM
344static int btrfs_free_inode(struct btrfs_trans_handle *trans,
345 struct btrfs_root *root,
346 struct inode *inode)
347{
5caf2a00 348 struct btrfs_path *path;
134e9731 349 int ret;
5caf2a00 350
134e9731 351 clear_inode(inode);
5caf2a00
CM
352
353 path = btrfs_alloc_path();
354 BUG_ON(!path);
355 btrfs_init_path(path);
d6e4a428
CM
356 ret = btrfs_lookup_inode(trans, root, path,
357 &BTRFS_I(inode)->location, -1);
134e9731 358 BUG_ON(ret);
5caf2a00 359 ret = btrfs_del_item(trans, root, path);
134e9731 360 BUG_ON(ret);
5caf2a00 361 btrfs_free_path(path);
134e9731
CM
362 return ret;
363}
364
de428b63
CM
365static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
366 u64 objectid)
367{
368 struct btrfs_node *node;
369 int i;
370 int nritems;
371 u64 item_objectid;
372 u64 blocknr;
373 int slot;
374 int ret;
375
376 if (!path->nodes[1])
377 return;
378 node = btrfs_buffer_node(path->nodes[1]);
379 slot = path->slots[1];
380 if (slot == 0)
381 return;
382 nritems = btrfs_header_nritems(&node->header);
383 for (i = slot - 1; i >= 0; i--) {
384 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
385 if (item_objectid != objectid)
386 break;
387 blocknr = btrfs_node_blockptr(node, i);
388 ret = readahead_tree_block(root, blocknr);
389 if (ret)
390 break;
391 }
392}
393
f4b9aa8d
CM
394static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
395 struct btrfs_root *root,
396 struct inode *inode)
397{
398 int ret;
5caf2a00 399 struct btrfs_path *path;
f4b9aa8d
CM
400 struct btrfs_key key;
401 struct btrfs_disk_key *found_key;
e06afa83 402 u32 found_type;
f4b9aa8d 403 struct btrfs_leaf *leaf;
1de037a4 404 struct btrfs_file_extent_item *fi;
f254e52c
CM
405 u64 extent_start = 0;
406 u64 extent_num_blocks = 0;
1de037a4 407 u64 item_end = 0;
f254e52c 408 int found_extent;
1de037a4 409 int del_item;
f4b9aa8d 410
5caf2a00
CM
411 path = btrfs_alloc_path();
412 BUG_ON(!path);
f4b9aa8d
CM
413 /* FIXME, add redo link to tree so we don't leak on crash */
414 key.objectid = inode->i_ino;
415 key.offset = (u64)-1;
e06afa83 416 key.flags = (u32)-1;
f4b9aa8d 417 while(1) {
5caf2a00 418 btrfs_init_path(path);
1de037a4 419 fi = NULL;
5caf2a00 420 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
f4b9aa8d 421 if (ret < 0) {
f4b9aa8d
CM
422 goto error;
423 }
424 if (ret > 0) {
5caf2a00
CM
425 BUG_ON(path->slots[0] == 0);
426 path->slots[0]--;
f4b9aa8d 427 }
de428b63 428 reada_truncate(root, path, inode->i_ino);
5caf2a00
CM
429 leaf = btrfs_buffer_leaf(path->nodes[0]);
430 found_key = &leaf->items[path->slots[0]].key;
e06afa83 431 found_type = btrfs_disk_key_type(found_key);
f4b9aa8d
CM
432 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
433 break;
e06afa83
CM
434 if (found_type != BTRFS_CSUM_ITEM_KEY &&
435 found_type != BTRFS_DIR_ITEM_KEY &&
436 found_type != BTRFS_DIR_INDEX_KEY &&
437 found_type != BTRFS_EXTENT_DATA_KEY)
f4b9aa8d 438 break;
1de037a4
CM
439 item_end = btrfs_disk_key_offset(found_key);
440 if (found_type == BTRFS_EXTENT_DATA_KEY) {
5caf2a00
CM
441 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
442 path->slots[0],
f254e52c 443 struct btrfs_file_extent_item);
236454df
CM
444 if (btrfs_file_extent_type(fi) !=
445 BTRFS_FILE_EXTENT_INLINE) {
1de037a4
CM
446 item_end += btrfs_file_extent_num_blocks(fi) <<
447 inode->i_blkbits;
448 }
449 }
450 if (found_type == BTRFS_CSUM_ITEM_KEY) {
451 ret = btrfs_csum_truncate(trans, root, path,
452 inode->i_size);
453 BUG_ON(ret);
454 }
455 if (item_end < inode->i_size) {
456 if (found_type) {
457 btrfs_set_key_type(&key, found_type - 1);
458 continue;
459 }
460 break;
461 }
462 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
463 del_item = 1;
464 else
465 del_item = 0;
466 found_extent = 0;
467
468 if (found_type == BTRFS_EXTENT_DATA_KEY &&
469 btrfs_file_extent_type(fi) !=
470 BTRFS_FILE_EXTENT_INLINE) {
471 u64 num_dec;
472 if (!del_item) {
473 u64 orig_num_blocks =
474 btrfs_file_extent_num_blocks(fi);
475 extent_num_blocks = inode->i_size -
476 btrfs_disk_key_offset(found_key) +
477 root->blocksize - 1;
478 extent_num_blocks >>= inode->i_blkbits;
479 btrfs_set_file_extent_num_blocks(fi,
480 extent_num_blocks);
481 inode->i_blocks -= (orig_num_blocks -
482 extent_num_blocks) << 3;
483 mark_buffer_dirty(path->nodes[0]);
484 } else {
236454df
CM
485 extent_start =
486 btrfs_file_extent_disk_blocknr(fi);
487 extent_num_blocks =
488 btrfs_file_extent_disk_num_blocks(fi);
489 /* FIXME blocksize != 4096 */
3a686375
CM
490 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
491 if (extent_start != 0) {
492 found_extent = 1;
493 inode->i_blocks -= num_dec;
494 }
236454df 495 }
f254e52c 496 }
1de037a4
CM
497 if (del_item) {
498 ret = btrfs_del_item(trans, root, path);
499 BUG_ON(ret);
500 } else {
501 break;
502 }
5caf2a00 503 btrfs_release_path(root, path);
f254e52c
CM
504 if (found_extent) {
505 ret = btrfs_free_extent(trans, root, extent_start,
506 extent_num_blocks, 0);
507 BUG_ON(ret);
508 }
f4b9aa8d 509 }
f4b9aa8d
CM
510 ret = 0;
511error:
5caf2a00
CM
512 btrfs_release_path(root, path);
513 btrfs_free_path(path);
cd1bc465 514 inode->i_sb->s_dirt = 1;
f4b9aa8d
CM
515 return ret;
516}
517
1de037a4
CM
518static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
519{
520 struct inode *inode = mapping->host;
521 unsigned blocksize = 1 << inode->i_blkbits;
522 pgoff_t index = from >> PAGE_CACHE_SHIFT;
523 unsigned offset = from & (PAGE_CACHE_SIZE-1);
524 struct page *page;
525 char *kaddr;
526 int ret = 0;
527 struct btrfs_root *root = BTRFS_I(inode)->root;
528 u64 alloc_hint;
529 struct btrfs_key ins;
530 struct btrfs_trans_handle *trans;
531
532 if ((offset & (blocksize - 1)) == 0)
533 goto out;
534
535 ret = -ENOMEM;
536 page = grab_cache_page(mapping, index);
537 if (!page)
538 goto out;
539
540 if (!PageUptodate(page)) {
541 ret = mpage_readpage(page, btrfs_get_block);
542 lock_page(page);
543 if (!PageUptodate(page)) {
544 ret = -EIO;
545 goto out;
546 }
547 }
548 mutex_lock(&root->fs_info->fs_mutex);
549 trans = btrfs_start_transaction(root, 1);
550 btrfs_set_trans_block_group(trans, inode);
551
552 ret = drop_extents(trans, root, inode, page->index << PAGE_CACHE_SHIFT,
553 (page->index + 1) << PAGE_CACHE_SHIFT, &alloc_hint);
554 BUG_ON(ret);
555 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
556 alloc_hint, (u64)-1, &ins, 1);
557 BUG_ON(ret);
558 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
559 page->index << PAGE_CACHE_SHIFT,
560 ins.objectid, 1, 1);
561 BUG_ON(ret);
562 SetPageChecked(page);
563 kaddr = kmap(page);
564 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
565 flush_dcache_page(page);
566 btrfs_csum_file_block(trans, root, inode->i_ino,
567 page->index << PAGE_CACHE_SHIFT,
568 kaddr, PAGE_CACHE_SIZE);
569 kunmap(page);
570 btrfs_end_transaction(trans, root);
571 mutex_unlock(&root->fs_info->fs_mutex);
572
573 set_page_dirty(page);
574 unlock_page(page);
575 page_cache_release(page);
576out:
577 return ret;
578}
579
3a686375
CM
580static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
581{
582 struct inode *inode = dentry->d_inode;
583 int err;
584
585 err = inode_change_ok(inode, attr);
586 if (err)
587 return err;
588
589 if (S_ISREG(inode->i_mode) &&
590 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
591 struct btrfs_trans_handle *trans;
592 struct btrfs_root *root = BTRFS_I(inode)->root;
593 u64 mask = root->blocksize - 1;
594 u64 pos = (inode->i_size + mask) & ~mask;
595 u64 hole_size;
596
1de037a4 597 if (attr->ia_size <= pos)
3a686375 598 goto out;
1de037a4
CM
599
600 btrfs_truncate_page(inode->i_mapping, inode->i_size);
601
3a686375
CM
602 hole_size = (attr->ia_size - pos + mask) & ~mask;
603 hole_size >>= inode->i_blkbits;
604
605 mutex_lock(&root->fs_info->fs_mutex);
606 trans = btrfs_start_transaction(root, 1);
607 btrfs_set_trans_block_group(trans, inode);
608 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
609 pos, 0, 0, hole_size);
610 BUG_ON(err);
611 btrfs_end_transaction(trans, root);
612 mutex_unlock(&root->fs_info->fs_mutex);
613 }
1de037a4 614out:
3a686375
CM
615 err = inode_setattr(inode, attr);
616
3a686375
CM
617 return err;
618}
134e9731
CM
619static void btrfs_delete_inode(struct inode *inode)
620{
621 struct btrfs_trans_handle *trans;
d6e4a428 622 struct btrfs_root *root = BTRFS_I(inode)->root;
f4b9aa8d
CM
623 int ret;
624
134e9731
CM
625 truncate_inode_pages(&inode->i_data, 0);
626 if (is_bad_inode(inode)) {
627 goto no_delete;
628 }
629 inode->i_size = 0;
134e9731
CM
630 mutex_lock(&root->fs_info->fs_mutex);
631 trans = btrfs_start_transaction(root, 1);
31f3c99b 632 btrfs_set_trans_block_group(trans, inode);
e06afa83
CM
633 ret = btrfs_truncate_in_trans(trans, root, inode);
634 BUG_ON(ret);
134e9731
CM
635 btrfs_free_inode(trans, root, inode);
636 btrfs_end_transaction(trans, root);
637 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 638 btrfs_btree_balance_dirty(root);
134e9731
CM
639 return;
640no_delete:
641 clear_inode(inode);
642}
643
e20d96d6 644static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
d6e4a428 645 struct btrfs_key *location)
e20d96d6
CM
646{
647 const char *name = dentry->d_name.name;
648 int namelen = dentry->d_name.len;
649 struct btrfs_dir_item *di;
5caf2a00 650 struct btrfs_path *path;
d6e4a428 651 struct btrfs_root *root = BTRFS_I(dir)->root;
e20d96d6
CM
652 int ret;
653
5caf2a00
CM
654 path = btrfs_alloc_path();
655 BUG_ON(!path);
656 btrfs_init_path(path);
7e38180e 657 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
e20d96d6 658 namelen, 0);
7e38180e 659 if (!di || IS_ERR(di)) {
d6e4a428 660 location->objectid = 0;
2c90e5d6 661 ret = 0;
e20d96d6
CM
662 goto out;
663 }
d6e4a428 664 btrfs_disk_key_to_cpu(location, &di->location);
e20d96d6 665out:
5caf2a00
CM
666 btrfs_release_path(root, path);
667 btrfs_free_path(path);
e20d96d6
CM
668 return ret;
669}
670
35b7e476 671static int fixup_tree_root_location(struct btrfs_root *root,
d6e4a428
CM
672 struct btrfs_key *location,
673 struct btrfs_root **sub_root)
674{
675 struct btrfs_path *path;
676 struct btrfs_root_item *ri;
d6e4a428
CM
677
678 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
679 return 0;
680 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
681 return 0;
682
683 path = btrfs_alloc_path();
684 BUG_ON(!path);
685 mutex_lock(&root->fs_info->fs_mutex);
686
0f7d52f4
CM
687 *sub_root = btrfs_read_fs_root(root->fs_info, location);
688 if (IS_ERR(*sub_root))
689 return PTR_ERR(*sub_root);
690
691 ri = &(*sub_root)->root_item;
d6e4a428
CM
692 location->objectid = btrfs_root_dirid(ri);
693 location->flags = 0;
694 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
695 location->offset = 0;
0f7d52f4 696
d6e4a428
CM
697 btrfs_free_path(path);
698 mutex_unlock(&root->fs_info->fs_mutex);
0f7d52f4 699 return 0;
d6e4a428
CM
700}
701
35b7e476 702static int btrfs_init_locked_inode(struct inode *inode, void *p)
c5739bba
CM
703{
704 struct btrfs_iget_args *args = p;
705 inode->i_ino = args->ino;
706 BTRFS_I(inode)->root = args->root;
707 return 0;
708}
709
35b7e476 710static int btrfs_find_actor(struct inode *inode, void *opaque)
c5739bba
CM
711{
712 struct btrfs_iget_args *args = opaque;
713 return (args->ino == inode->i_ino &&
714 args->root == BTRFS_I(inode)->root);
715}
716
35b7e476
CM
717static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
718 struct btrfs_root *root)
c5739bba
CM
719{
720 struct inode *inode;
721 struct btrfs_iget_args args;
722 args.ino = objectid;
723 args.root = root;
724
725 inode = iget5_locked(s, objectid, btrfs_find_actor,
726 btrfs_init_locked_inode,
727 (void *)&args);
728 return inode;
729}
d6e4a428 730
e20d96d6
CM
731static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
732 struct nameidata *nd)
733{
734 struct inode * inode;
d6e4a428
CM
735 struct btrfs_inode *bi = BTRFS_I(dir);
736 struct btrfs_root *root = bi->root;
737 struct btrfs_root *sub_root = root;
738 struct btrfs_key location;
e20d96d6
CM
739 int ret;
740
741 if (dentry->d_name.len > BTRFS_NAME_LEN)
742 return ERR_PTR(-ENAMETOOLONG);
22b0ebda 743 mutex_lock(&root->fs_info->fs_mutex);
d6e4a428 744 ret = btrfs_inode_by_name(dir, dentry, &location);
22b0ebda 745 mutex_unlock(&root->fs_info->fs_mutex);
e20d96d6
CM
746 if (ret < 0)
747 return ERR_PTR(ret);
748 inode = NULL;
d6e4a428
CM
749 if (location.objectid) {
750 ret = fixup_tree_root_location(root, &location, &sub_root);
751 if (ret < 0)
752 return ERR_PTR(ret);
753 if (ret > 0)
754 return ERR_PTR(-ENOENT);
c5739bba
CM
755 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
756 sub_root);
e20d96d6
CM
757 if (!inode)
758 return ERR_PTR(-EACCES);
d6e4a428 759 if (inode->i_state & I_NEW) {
0f7d52f4 760 if (sub_root != root) {
c5739bba 761printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
0f7d52f4
CM
762 igrab(inode);
763 sub_root->inode = inode;
764 }
d6e4a428
CM
765 BTRFS_I(inode)->root = sub_root;
766 memcpy(&BTRFS_I(inode)->location, &location,
767 sizeof(location));
768 btrfs_read_locked_inode(inode);
769 unlock_new_inode(inode);
770 }
e20d96d6
CM
771 }
772 return d_splice_alias(inode, dentry);
773}
774
de428b63
CM
775static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
776 u64 objectid)
090d1875
CM
777{
778 struct btrfs_node *node;
779 int i;
de428b63 780 u32 nritems;
090d1875
CM
781 u64 item_objectid;
782 u64 blocknr;
783 int slot;
de428b63 784 int ret;
090d1875
CM
785
786 if (!path->nodes[1])
787 return;
788 node = btrfs_buffer_node(path->nodes[1]);
789 slot = path->slots[1];
090d1875 790 nritems = btrfs_header_nritems(&node->header);
de428b63 791 for (i = slot + 1; i < nritems; i++) {
090d1875
CM
792 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
793 if (item_objectid != objectid)
794 break;
795 blocknr = btrfs_node_blockptr(node, i);
de428b63
CM
796 ret = readahead_tree_block(root, blocknr);
797 if (ret)
798 break;
090d1875
CM
799 }
800}
fabb5681
CM
801static unsigned char btrfs_filetype_table[] = {
802 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
803};
090d1875 804
e20d96d6
CM
805static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
806{
807 struct inode *inode = filp->f_path.dentry->d_inode;
d6e4a428 808 struct btrfs_root *root = BTRFS_I(inode)->root;
e20d96d6
CM
809 struct btrfs_item *item;
810 struct btrfs_dir_item *di;
811 struct btrfs_key key;
5caf2a00 812 struct btrfs_path *path;
e20d96d6
CM
813 int ret;
814 u32 nritems;
815 struct btrfs_leaf *leaf;
816 int slot;
817 int advance;
fabb5681 818 unsigned char d_type;
7f5c1516 819 int over = 0;
7e38180e
CM
820 u32 di_cur;
821 u32 di_total;
822 u32 di_len;
823 int key_type = BTRFS_DIR_INDEX_KEY;
d6e4a428
CM
824
825 /* FIXME, use a real flag for deciding about the key type */
826 if (root->fs_info->tree_root == root)
827 key_type = BTRFS_DIR_ITEM_KEY;
22b0ebda 828 mutex_lock(&root->fs_info->fs_mutex);
e20d96d6 829 key.objectid = inode->i_ino;
e20d96d6 830 key.flags = 0;
d6e4a428 831 btrfs_set_key_type(&key, key_type);
e20d96d6 832 key.offset = filp->f_pos;
5caf2a00
CM
833 path = btrfs_alloc_path();
834 btrfs_init_path(path);
835 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1b05da2e 836 if (ret < 0)
e20d96d6 837 goto err;
7f5c1516 838 advance = 0;
de428b63 839 reada_leaves(root, path, inode->i_ino);
e20d96d6 840 while(1) {
5caf2a00 841 leaf = btrfs_buffer_leaf(path->nodes[0]);
e20d96d6 842 nritems = btrfs_header_nritems(&leaf->header);
5caf2a00 843 slot = path->slots[0];
dee26a9f
CM
844 if (advance || slot >= nritems) {
845 if (slot >= nritems -1) {
de428b63 846 reada_leaves(root, path, inode->i_ino);
5caf2a00 847 ret = btrfs_next_leaf(root, path);
e20d96d6
CM
848 if (ret)
849 break;
5caf2a00 850 leaf = btrfs_buffer_leaf(path->nodes[0]);
e20d96d6 851 nritems = btrfs_header_nritems(&leaf->header);
5caf2a00 852 slot = path->slots[0];
e20d96d6
CM
853 } else {
854 slot++;
5caf2a00 855 path->slots[0]++;
e20d96d6
CM
856 }
857 }
858 advance = 1;
859 item = leaf->items + slot;
e20d96d6
CM
860 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
861 break;
d6e4a428 862 if (btrfs_disk_key_type(&item->key) != key_type)
a429e513 863 break;
7f5c1516
CM
864 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
865 continue;
7fcde0e3 866 filp->f_pos = btrfs_disk_key_offset(&item->key);
dee26a9f 867 advance = 1;
e20d96d6 868 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
7e38180e
CM
869 di_cur = 0;
870 di_total = btrfs_item_size(leaf->items + slot);
871 while(di_cur < di_total) {
fabb5681 872 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
7e38180e
CM
873 over = filldir(dirent, (const char *)(di + 1),
874 btrfs_dir_name_len(di),
875 btrfs_disk_key_offset(&item->key),
876 btrfs_disk_key_objectid(&di->location),
877 d_type);
878 if (over)
879 goto nopos;
880 di_len = btrfs_dir_name_len(di) + sizeof(*di);
881 di_cur += di_len;
882 di = (struct btrfs_dir_item *)((char *)di + di_len);
883 }
e20d96d6 884 }
7fcde0e3
CM
885 filp->f_pos++;
886nopos:
e20d96d6
CM
887 ret = 0;
888err:
5caf2a00
CM
889 btrfs_release_path(root, path);
890 btrfs_free_path(path);
22b0ebda 891 mutex_unlock(&root->fs_info->fs_mutex);
e20d96d6
CM
892 return ret;
893}
894
895static void btrfs_put_super (struct super_block * sb)
896{
897 struct btrfs_root *root = btrfs_sb(sb);
898 int ret;
899
900 ret = close_ctree(root);
901 if (ret) {
902 printk("close ctree returns %d\n", ret);
903 }
904 sb->s_fs_info = NULL;
905}
2e635a27
CM
906
907static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
908{
909 struct inode * inode;
e20d96d6
CM
910 struct dentry * root_dentry;
911 struct btrfs_super_block *disk_super;
0f7d52f4 912 struct btrfs_root *tree_root;
d6e4a428 913 struct btrfs_inode *bi;
2e635a27
CM
914
915 sb->s_maxbytes = MAX_LFS_FILESIZE;
2e635a27 916 sb->s_magic = BTRFS_SUPER_MAGIC;
e20d96d6 917 sb->s_op = &btrfs_super_ops;
2e635a27 918 sb->s_time_gran = 1;
e20d96d6 919
0f7d52f4 920 tree_root = open_ctree(sb);
d98237b3 921
0f7d52f4 922 if (!tree_root) {
e20d96d6
CM
923 printk("btrfs: open_ctree failed\n");
924 return -EIO;
925 }
0f7d52f4
CM
926 sb->s_fs_info = tree_root;
927 disk_super = tree_root->fs_info->disk_super;
e20d96d6
CM
928 printk("read in super total blocks %Lu root %Lu\n",
929 btrfs_super_total_blocks(disk_super),
930 btrfs_super_root_dir(disk_super));
931
c5739bba
CM
932 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
933 tree_root);
d6e4a428
CM
934 bi = BTRFS_I(inode);
935 bi->location.objectid = inode->i_ino;
936 bi->location.offset = 0;
937 bi->location.flags = 0;
0f7d52f4 938 bi->root = tree_root;
d6e4a428
CM
939 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
940
2e635a27
CM
941 if (!inode)
942 return -ENOMEM;
e20d96d6
CM
943 if (inode->i_state & I_NEW) {
944 btrfs_read_locked_inode(inode);
945 unlock_new_inode(inode);
946 }
2e635a27 947
e20d96d6
CM
948 root_dentry = d_alloc_root(inode);
949 if (!root_dentry) {
2e635a27
CM
950 iput(inode);
951 return -ENOMEM;
952 }
e20d96d6 953 sb->s_root = root_dentry;
08607c1b 954 btrfs_transaction_queue_work(tree_root, HZ * 30);
2e635a27
CM
955 return 0;
956}
957
4730a4bc
CM
958static int btrfs_write_inode(struct inode *inode, int wait)
959{
d6e4a428 960 struct btrfs_root *root = BTRFS_I(inode)->root;
4730a4bc 961 struct btrfs_trans_handle *trans;
b5133862
CM
962 int ret = 0;
963
964 if (wait) {
965 mutex_lock(&root->fs_info->fs_mutex);
966 trans = btrfs_start_transaction(root, 1);
31f3c99b 967 btrfs_set_trans_block_group(trans, inode);
b5133862
CM
968 ret = btrfs_commit_transaction(trans, root);
969 mutex_unlock(&root->fs_info->fs_mutex);
970 }
971 return ret;
972}
973
974static void btrfs_dirty_inode(struct inode *inode)
975{
976 struct btrfs_root *root = BTRFS_I(inode)->root;
977 struct btrfs_trans_handle *trans;
4730a4bc
CM
978
979 mutex_lock(&root->fs_info->fs_mutex);
980 trans = btrfs_start_transaction(root, 1);
31f3c99b 981 btrfs_set_trans_block_group(trans, inode);
b5133862
CM
982 btrfs_update_inode(trans, root, inode);
983 btrfs_end_transaction(trans, root);
4730a4bc 984 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 985 btrfs_btree_balance_dirty(root);
4730a4bc
CM
986}
987
d5719762 988static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2619ba1f 989 struct btrfs_root *root,
31f3c99b
CM
990 u64 objectid,
991 struct btrfs_block_group_cache *group,
992 int mode)
d5719762
CM
993{
994 struct inode *inode;
995 struct btrfs_inode_item inode_item;
1b05da2e 996 struct btrfs_key *location;
d5719762 997 int ret;
de428b63 998 int owner;
d5719762 999
2619ba1f 1000 inode = new_inode(root->fs_info->sb);
d5719762
CM
1001 if (!inode)
1002 return ERR_PTR(-ENOMEM);
1003
2619ba1f 1004 BTRFS_I(inode)->root = root;
de428b63
CM
1005 if (mode & S_IFDIR)
1006 owner = 0;
1007 else
1008 owner = 1;
1009 group = btrfs_find_block_group(root, group, 0, 0, owner);
31f3c99b 1010 BTRFS_I(inode)->block_group = group;
d5719762
CM
1011
1012 inode->i_uid = current->fsuid;
1013 inode->i_gid = current->fsgid;
1014 inode->i_mode = mode;
1015 inode->i_ino = objectid;
1016 inode->i_blocks = 0;
c5739bba 1017 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
d5719762 1018 fill_inode_item(&inode_item, inode);
1b05da2e
CM
1019 location = &BTRFS_I(inode)->location;
1020 location->objectid = objectid;
1021 location->flags = 0;
1022 location->offset = 0;
1023 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
d5719762
CM
1024
1025 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1026 BUG_ON(ret);
1027
1028 insert_inode_hash(inode);
d5719762
CM
1029 return inode;
1030}
1031
fabb5681
CM
1032static inline u8 btrfs_inode_type(struct inode *inode)
1033{
1034 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1035}
1036
d5719762
CM
1037static int btrfs_add_link(struct btrfs_trans_handle *trans,
1038 struct dentry *dentry, struct inode *inode)
1039{
1040 int ret;
d6e4a428
CM
1041 struct btrfs_key key;
1042 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1043 key.objectid = inode->i_ino;
1044 key.flags = 0;
1045 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1046 key.offset = 0;
1047
1048 ret = btrfs_insert_dir_item(trans, root,
d5719762
CM
1049 dentry->d_name.name, dentry->d_name.len,
1050 dentry->d_parent->d_inode->i_ino,
fabb5681 1051 &key, btrfs_inode_type(inode));
4730a4bc 1052 if (ret == 0) {
5f26f772 1053 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
d6e4a428 1054 ret = btrfs_update_inode(trans, root,
4730a4bc
CM
1055 dentry->d_parent->d_inode);
1056 }
d5719762
CM
1057 return ret;
1058}
1059
1060static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1061 struct dentry *dentry, struct inode *inode)
1062{
1063 int err = btrfs_add_link(trans, dentry, inode);
1064 if (!err) {
1065 d_instantiate(dentry, inode);
1066 return 0;
1067 }
2c90e5d6
CM
1068 if (err > 0)
1069 err = -EEXIST;
d5719762
CM
1070 return err;
1071}
1072
1073static int btrfs_create(struct inode *dir, struct dentry *dentry,
1074 int mode, struct nameidata *nd)
1075{
1076 struct btrfs_trans_handle *trans;
d6e4a428 1077 struct btrfs_root *root = BTRFS_I(dir)->root;
d5719762
CM
1078 struct inode *inode;
1079 int err;
134e9731 1080 int drop_inode = 0;
2619ba1f 1081 u64 objectid;
d5719762 1082
d561c025 1083 mutex_lock(&root->fs_info->fs_mutex);
d5719762 1084 trans = btrfs_start_transaction(root, 1);
31f3c99b 1085 btrfs_set_trans_block_group(trans, dir);
2619ba1f
CM
1086
1087 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1088 if (err) {
1089 err = -ENOSPC;
1090 goto out_unlock;
1091 }
1092
31f3c99b
CM
1093 inode = btrfs_new_inode(trans, root, objectid,
1094 BTRFS_I(dir)->block_group, mode);
d5719762
CM
1095 err = PTR_ERR(inode);
1096 if (IS_ERR(inode))
d561c025 1097 goto out_unlock;
31f3c99b
CM
1098
1099 btrfs_set_trans_block_group(trans, inode);
d5719762 1100 err = btrfs_add_nondir(trans, dentry, inode);
134e9731
CM
1101 if (err)
1102 drop_inode = 1;
dee26a9f
CM
1103 else {
1104 inode->i_mapping->a_ops = &btrfs_aops;
1105 inode->i_fop = &btrfs_file_operations;
1106 inode->i_op = &btrfs_file_inode_operations;
1107 }
d5719762 1108 dir->i_sb->s_dirt = 1;
31f3c99b
CM
1109 btrfs_update_inode_block_group(trans, inode);
1110 btrfs_update_inode_block_group(trans, dir);
d561c025 1111out_unlock:
22b0ebda 1112 btrfs_end_transaction(trans, root);
d561c025 1113 mutex_unlock(&root->fs_info->fs_mutex);
2c90e5d6 1114
134e9731
CM
1115 if (drop_inode) {
1116 inode_dec_link_count(inode);
1117 iput(inode);
1118 }
35b7e476 1119 btrfs_btree_balance_dirty(root);
d5719762
CM
1120 return err;
1121}
1122
2b8d99a7
CM
1123static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1124 struct dentry *dentry)
1125{
1126 struct btrfs_trans_handle *trans;
1127 struct btrfs_root *root = BTRFS_I(dir)->root;
1128 struct inode *inode = old_dentry->d_inode;
1129 int err;
1130 int drop_inode = 0;
1131
1132 if (inode->i_nlink == 0)
1133 return -ENOENT;
1134
1135 inc_nlink(inode);
1136 mutex_lock(&root->fs_info->fs_mutex);
1137 trans = btrfs_start_transaction(root, 1);
1138 btrfs_set_trans_block_group(trans, dir);
1139 atomic_inc(&inode->i_count);
1140 err = btrfs_add_nondir(trans, dentry, inode);
1141 if (err)
1142 drop_inode = 1;
1143 dir->i_sb->s_dirt = 1;
1144 btrfs_update_inode_block_group(trans, dir);
1145 btrfs_update_inode(trans, root, inode);
1146
1147 btrfs_end_transaction(trans, root);
1148 mutex_unlock(&root->fs_info->fs_mutex);
1149
1150 if (drop_inode) {
1151 inode_dec_link_count(inode);
1152 iput(inode);
1153 }
1154 btrfs_btree_balance_dirty(root);
1155 return err;
1156}
1157
f7922033 1158static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
2619ba1f
CM
1159 struct btrfs_root *root,
1160 u64 objectid, u64 dirid)
f7922033 1161{
f7922033
CM
1162 int ret;
1163 char buf[2];
d6e4a428
CM
1164 struct btrfs_key key;
1165
f7922033
CM
1166 buf[0] = '.';
1167 buf[1] = '.';
1168
2619ba1f 1169 key.objectid = objectid;
d6e4a428
CM
1170 key.offset = 0;
1171 key.flags = 0;
1172 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1173
2619ba1f 1174 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
fabb5681 1175 &key, BTRFS_FT_DIR);
f7922033
CM
1176 if (ret)
1177 goto error;
2619ba1f
CM
1178 key.objectid = dirid;
1179 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
fabb5681 1180 &key, BTRFS_FT_DIR);
4730a4bc
CM
1181 if (ret)
1182 goto error;
f7922033
CM
1183error:
1184 return ret;
1185}
1186
1187static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1188{
1189 struct inode *inode;
1190 struct btrfs_trans_handle *trans;
d6e4a428 1191 struct btrfs_root *root = BTRFS_I(dir)->root;
f7922033
CM
1192 int err = 0;
1193 int drop_on_err = 0;
2619ba1f 1194 u64 objectid;
f7922033
CM
1195
1196 mutex_lock(&root->fs_info->fs_mutex);
1197 trans = btrfs_start_transaction(root, 1);
31f3c99b 1198 btrfs_set_trans_block_group(trans, dir);
f7922033
CM
1199 if (IS_ERR(trans)) {
1200 err = PTR_ERR(trans);
1201 goto out_unlock;
1202 }
2619ba1f
CM
1203
1204 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1205 if (err) {
1206 err = -ENOSPC;
1207 goto out_unlock;
1208 }
1209
31f3c99b
CM
1210 inode = btrfs_new_inode(trans, root, objectid,
1211 BTRFS_I(dir)->block_group, S_IFDIR | mode);
f7922033
CM
1212 if (IS_ERR(inode)) {
1213 err = PTR_ERR(inode);
1214 goto out_fail;
1215 }
1216 drop_on_err = 1;
1217 inode->i_op = &btrfs_dir_inode_operations;
1218 inode->i_fop = &btrfs_dir_file_operations;
31f3c99b 1219 btrfs_set_trans_block_group(trans, inode);
f7922033 1220
2619ba1f
CM
1221 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1222 if (err)
1223 goto out_fail;
1224
1225 inode->i_size = 6;
1226 err = btrfs_update_inode(trans, root, inode);
f7922033
CM
1227 if (err)
1228 goto out_fail;
1229 err = btrfs_add_link(trans, dentry, inode);
1230 if (err)
1231 goto out_fail;
1232 d_instantiate(dentry, inode);
f7922033 1233 drop_on_err = 0;
cd1bc465 1234 dir->i_sb->s_dirt = 1;
31f3c99b
CM
1235 btrfs_update_inode_block_group(trans, inode);
1236 btrfs_update_inode_block_group(trans, dir);
f7922033
CM
1237
1238out_fail:
1239 btrfs_end_transaction(trans, root);
1240out_unlock:
1241 mutex_unlock(&root->fs_info->fs_mutex);
1242 if (drop_on_err)
1243 iput(inode);
35b7e476 1244 btrfs_btree_balance_dirty(root);
f7922033
CM
1245 return err;
1246}
1247
8fd17795
CM
1248static int btrfs_sync_file(struct file *file,
1249 struct dentry *dentry, int datasync)
1250{
1251 struct inode *inode = dentry->d_inode;
1252 struct btrfs_root *root = BTRFS_I(inode)->root;
1253 int ret;
1254 struct btrfs_trans_handle *trans;
1255
1256 mutex_lock(&root->fs_info->fs_mutex);
1257 trans = btrfs_start_transaction(root, 1);
1258 if (!trans) {
1259 ret = -ENOMEM;
1260 goto out;
1261 }
1262 ret = btrfs_commit_transaction(trans, root);
1263 mutex_unlock(&root->fs_info->fs_mutex);
1264out:
1265 return ret > 0 ? EIO : ret;
1266}
1267
d5719762
CM
1268static int btrfs_sync_fs(struct super_block *sb, int wait)
1269{
1270 struct btrfs_trans_handle *trans;
1271 struct btrfs_root *root;
1272 int ret;
d98237b3 1273 root = btrfs_sb(sb);
df2ce34c 1274
d5719762 1275 sb->s_dirt = 0;
d561c025 1276 if (!wait) {
7cfcc17e 1277 filemap_flush(root->fs_info->btree_inode->i_mapping);
d561c025
CM
1278 return 0;
1279 }
d561c025 1280 mutex_lock(&root->fs_info->fs_mutex);
d5719762
CM
1281 trans = btrfs_start_transaction(root, 1);
1282 ret = btrfs_commit_transaction(trans, root);
1283 sb->s_dirt = 0;
1284 BUG_ON(ret);
1285printk("btrfs sync_fs\n");
d561c025 1286 mutex_unlock(&root->fs_info->fs_mutex);
d5719762
CM
1287 return 0;
1288}
1289
fabb5681
CM
1290#define BTRFS_GET_BLOCK_NO_CREATE 0
1291#define BTRFS_GET_BLOCK_CREATE 1
1292#define BTRFS_GET_BLOCK_NO_DIRECT 2
1293
75dfe396 1294static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
dee26a9f
CM
1295 struct buffer_head *result, int create)
1296{
1297 int ret;
1298 int err = 0;
1299 u64 blocknr;
1300 u64 extent_start = 0;
1301 u64 extent_end = 0;
1302 u64 objectid = inode->i_ino;
236454df 1303 u32 found_type;
1de037a4 1304 u64 alloc_hint = 0;
5caf2a00 1305 struct btrfs_path *path;
d6e4a428 1306 struct btrfs_root *root = BTRFS_I(inode)->root;
dee26a9f
CM
1307 struct btrfs_file_extent_item *item;
1308 struct btrfs_leaf *leaf;
1309 struct btrfs_disk_key *found_key;
1de037a4 1310 struct btrfs_trans_handle *trans = NULL;
dee26a9f 1311
5caf2a00
CM
1312 path = btrfs_alloc_path();
1313 BUG_ON(!path);
1314 btrfs_init_path(path);
fabb5681 1315 if (create & BTRFS_GET_BLOCK_CREATE) {
6567e837 1316 WARN_ON(1);
1de037a4
CM
1317 /* this almost but not quite works */
1318 trans = btrfs_start_transaction(root, 1);
1319 if (!trans) {
1320 err = -ENOMEM;
1321 goto out;
1322 }
1323 ret = drop_extents(trans, root, inode,
1324 iblock << inode->i_blkbits,
1325 (iblock + 1) << inode->i_blkbits,
1326 &alloc_hint);
1327 BUG_ON(ret);
6567e837 1328 }
dee26a9f 1329
236454df 1330 ret = btrfs_lookup_file_extent(NULL, root, path,
9773a788 1331 inode->i_ino,
236454df 1332 iblock << inode->i_blkbits, 0);
dee26a9f 1333 if (ret < 0) {
dee26a9f
CM
1334 err = ret;
1335 goto out;
1336 }
1337
1338 if (ret != 0) {
5caf2a00
CM
1339 if (path->slots[0] == 0) {
1340 btrfs_release_path(root, path);
1de037a4 1341 goto not_found;
dee26a9f 1342 }
5caf2a00 1343 path->slots[0]--;
dee26a9f
CM
1344 }
1345
5caf2a00 1346 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
dee26a9f 1347 struct btrfs_file_extent_item);
5caf2a00 1348 leaf = btrfs_buffer_leaf(path->nodes[0]);
dee26a9f
CM
1349 blocknr = btrfs_file_extent_disk_blocknr(item);
1350 blocknr += btrfs_file_extent_offset(item);
1351
dee26a9f 1352 /* are we inside the extent that was found? */
5caf2a00 1353 found_key = &leaf->items[path->slots[0]].key;
236454df 1354 found_type = btrfs_disk_key_type(found_key);
dee26a9f 1355 if (btrfs_disk_key_objectid(found_key) != objectid ||
236454df 1356 found_type != BTRFS_EXTENT_DATA_KEY) {
dee26a9f
CM
1357 extent_end = 0;
1358 extent_start = 0;
1de037a4 1359 goto not_found;
dee26a9f 1360 }
236454df
CM
1361 found_type = btrfs_file_extent_type(item);
1362 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1363 if (found_type == BTRFS_FILE_EXTENT_REG) {
1364 extent_start = extent_start >> inode->i_blkbits;
1365 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
3a686375 1366 err = 0;
1de037a4 1367 if (btrfs_file_extent_disk_blocknr(item) == 0)
3a686375 1368 goto out;
236454df 1369 if (iblock >= extent_start && iblock < extent_end) {
236454df
CM
1370 btrfs_map_bh_to_logical(root, result, blocknr +
1371 iblock - extent_start);
1372 goto out;
1373 }
1374 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1375 char *ptr;
1376 char *map;
1377 u32 size;
fabb5681
CM
1378
1379 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1380 err = -EINVAL;
1381 goto out;
1382 }
236454df
CM
1383 size = btrfs_file_extent_inline_len(leaf->items +
1384 path->slots[0]);
1385 extent_end = (extent_start + size) >> inode->i_blkbits;
1386 extent_start >>= inode->i_blkbits;
1387 if (iblock < extent_start || iblock > extent_end) {
1de037a4 1388 goto not_found;
236454df
CM
1389 }
1390 ptr = btrfs_file_extent_inline_start(item);
1391 map = kmap(result->b_page);
1392 memcpy(map, ptr, size);
1393 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1394 flush_dcache_page(result->b_page);
1395 kunmap(result->b_page);
1396 set_buffer_uptodate(result);
1397 SetPageChecked(result->b_page);
1398 btrfs_map_bh_to_logical(root, result, 0);
dee26a9f 1399 }
1de037a4 1400not_found:
fabb5681 1401 if (create & BTRFS_GET_BLOCK_CREATE) {
1de037a4
CM
1402 struct btrfs_key ins;
1403 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1404 1, alloc_hint, (u64)-1,
1405 &ins, 1);
1406 BUG_ON(ret);
1407 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1408 iblock << inode->i_blkbits,
1409 ins.objectid, ins.offset,
1410 ins.offset);
1411 BUG_ON(ret);
1412 SetPageChecked(result->b_page);
1413 btrfs_map_bh_to_logical(root, result, ins.objectid);
1414 }
dee26a9f 1415out:
1de037a4
CM
1416 if (trans)
1417 err = btrfs_end_transaction(trans, root);
5caf2a00 1418 btrfs_free_path(path);
75dfe396
CM
1419 return err;
1420}
1421
1422static int btrfs_get_block(struct inode *inode, sector_t iblock,
1423 struct buffer_head *result, int create)
1424{
1425 int err;
d6e4a428 1426 struct btrfs_root *root = BTRFS_I(inode)->root;
75dfe396 1427 mutex_lock(&root->fs_info->fs_mutex);
e8f05c45 1428 err = btrfs_get_block_lock(inode, iblock, result, create);
dee26a9f
CM
1429 mutex_unlock(&root->fs_info->fs_mutex);
1430 return err;
1431}
1432
fabb5681
CM
1433static 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
1443static 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
dee26a9f
CM
1448static int btrfs_prepare_write(struct file *file, struct page *page,
1449 unsigned from, unsigned to)
1450{
1de037a4 1451 return block_prepare_write(page, from, to, btrfs_get_block);
dee26a9f
CM
1452}
1453
d561c025
CM
1454static void btrfs_write_super(struct super_block *sb)
1455{
08607c1b 1456 sb->s_dirt = 0;
d561c025
CM
1457}
1458
dee26a9f
CM
1459static int btrfs_readpage(struct file *file, struct page *page)
1460{
1461 return mpage_readpage(page, btrfs_get_block);
1462}
1463
236454df
CM
1464/*
1465 * While block_write_full_page is writing back the dirty buffers under
1466 * the page lock, whoever dirtied the buffers may decide to clean them
1467 * again at any time. We handle that by only looking at the buffer
1468 * state inside lock_buffer().
1469 *
1470 * If block_write_full_page() is called for regular writeback
1471 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1472 * locked buffer. This only can happen if someone has written the buffer
1473 * directly, with submit_bh(). At the address_space level PageWriteback
1474 * prevents this contention from occurring.
1475 */
1476static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1477 struct writeback_control *wbc)
1478{
1479 int err;
1480 sector_t block;
1481 sector_t last_block;
1482 struct buffer_head *bh, *head;
1483 const unsigned blocksize = 1 << inode->i_blkbits;
1484 int nr_underway = 0;
1485
1486 BUG_ON(!PageLocked(page));
1487
1488 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1489
1490 if (!page_has_buffers(page)) {
1491 create_empty_buffers(page, blocksize,
1492 (1 << BH_Dirty)|(1 << BH_Uptodate));
1493 }
1494
1495 /*
1496 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1497 * here, and the (potentially unmapped) buffers may become dirty at
1498 * any time. If a buffer becomes dirty here after we've inspected it
1499 * then we just miss that fact, and the page stays dirty.
1500 *
1501 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1502 * handle that here by just cleaning them.
1503 */
1504
1505 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1506 head = page_buffers(page);
1507 bh = head;
1508
1509 /*
1510 * Get all the dirty buffers mapped to disk addresses and
1511 * handle any aliases from the underlying blockdev's mapping.
1512 */
1513 do {
1514 if (block > last_block) {
1515 /*
1516 * mapped buffers outside i_size will occur, because
1517 * this page can be outside i_size when there is a
1518 * truncate in progress.
1519 */
1520 /*
1521 * The buffer was zeroed by block_write_full_page()
1522 */
1523 clear_buffer_dirty(bh);
1524 set_buffer_uptodate(bh);
1525 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1526 WARN_ON(bh->b_size != blocksize);
1527 err = btrfs_get_block(inode, block, bh, 0);
35b7e476
CM
1528 if (err) {
1529printk("writepage going to recovery err %d\n", err);
236454df 1530 goto recover;
35b7e476 1531 }
236454df
CM
1532 if (buffer_new(bh)) {
1533 /* blockdev mappings never come here */
1534 clear_buffer_new(bh);
236454df
CM
1535 }
1536 }
1537 bh = bh->b_this_page;
1538 block++;
1539 } while (bh != head);
1540
1541 do {
1542 if (!buffer_mapped(bh))
1543 continue;
1544 /*
1545 * If it's a fully non-blocking write attempt and we cannot
1546 * lock the buffer then redirty the page. Note that this can
1547 * potentially cause a busy-wait loop from pdflush and kswapd
1548 * activity, but those code paths have their own higher-level
1549 * throttling.
1550 */
1551 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1552 lock_buffer(bh);
1553 } else if (test_set_buffer_locked(bh)) {
1554 redirty_page_for_writepage(wbc, page);
1555 continue;
1556 }
1557 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1558 mark_buffer_async_write(bh);
1559 } else {
1560 unlock_buffer(bh);
1561 }
1562 } while ((bh = bh->b_this_page) != head);
1563
1564 /*
1565 * The page and its buffers are protected by PageWriteback(), so we can
1566 * drop the bh refcounts early.
1567 */
1568 BUG_ON(PageWriteback(page));
1569 set_page_writeback(page);
1570
1571 do {
1572 struct buffer_head *next = bh->b_this_page;
1573 if (buffer_async_write(bh)) {
1574 submit_bh(WRITE, bh);
1575 nr_underway++;
1576 }
1577 bh = next;
1578 } while (bh != head);
1579 unlock_page(page);
1580
1581 err = 0;
1582done:
1583 if (nr_underway == 0) {
1584 /*
1585 * The page was marked dirty, but the buffers were
1586 * clean. Someone wrote them back by hand with
1587 * ll_rw_block/submit_bh. A rare case.
1588 */
1589 int uptodate = 1;
1590 do {
1591 if (!buffer_uptodate(bh)) {
1592 uptodate = 0;
1593 break;
1594 }
1595 bh = bh->b_this_page;
1596 } while (bh != head);
1597 if (uptodate)
1598 SetPageUptodate(page);
1599 end_page_writeback(page);
236454df
CM
1600 }
1601 return err;
1602
1603recover:
1604 /*
1605 * ENOSPC, or some other error. We may already have added some
1606 * blocks to the file, so we need to write these out to avoid
1607 * exposing stale data.
1608 * The page is currently locked and not marked for writeback
1609 */
1610 bh = head;
1611 /* Recovery: lock and submit the mapped buffers */
1612 do {
1613 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1614 lock_buffer(bh);
1615 mark_buffer_async_write(bh);
1616 } else {
1617 /*
1618 * The buffer may have been set dirty during
1619 * attachment to a dirty page.
1620 */
1621 clear_buffer_dirty(bh);
1622 }
1623 } while ((bh = bh->b_this_page) != head);
1624 SetPageError(page);
1625 BUG_ON(PageWriteback(page));
1626 set_page_writeback(page);
1627 do {
1628 struct buffer_head *next = bh->b_this_page;
1629 if (buffer_async_write(bh)) {
1630 clear_buffer_dirty(bh);
1631 submit_bh(WRITE, bh);
1632 nr_underway++;
1633 }
1634 bh = next;
1635 } while (bh != head);
1636 unlock_page(page);
1637 goto done;
1638}
1639
1640/*
1641 * The generic ->writepage function for buffer-backed address_spaces
1642 */
dee26a9f
CM
1643static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1644{
236454df
CM
1645 struct inode * const inode = page->mapping->host;
1646 loff_t i_size = i_size_read(inode);
1647 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1648 unsigned offset;
1649 void *kaddr;
1650
1651 /* Is the page fully inside i_size? */
1652 if (page->index < end_index)
1653 return __btrfs_write_full_page(inode, page, wbc);
1654
1655 /* Is the page fully outside i_size? (truncate in progress) */
1656 offset = i_size & (PAGE_CACHE_SIZE-1);
1657 if (page->index >= end_index+1 || !offset) {
1658 /*
1659 * The page may have dirty, unmapped buffers. For example,
1660 * they may have been added in ext3_writepage(). Make them
1661 * freeable here, so the page does not leak.
1662 */
1663 block_invalidatepage(page, 0);
1664 unlock_page(page);
1665 return 0; /* don't care */
1666 }
1667
1668 /*
1669 * The page straddles i_size. It must be zeroed out on each and every
1670 * writepage invokation because it may be mmapped. "A file is mapped
1671 * in multiples of the page size. For a file that is not a multiple of
1672 * the page size, the remaining memory is zeroed when mapped, and
1673 * writes to that region are not written out to the file."
1674 */
1675 kaddr = kmap_atomic(page, KM_USER0);
1676 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1677 flush_dcache_page(page);
1678 kunmap_atomic(kaddr, KM_USER0);
1679 return __btrfs_write_full_page(inode, page, wbc);
dee26a9f 1680}
d561c025 1681
f4b9aa8d
CM
1682static void btrfs_truncate(struct inode *inode)
1683{
d6e4a428 1684 struct btrfs_root *root = BTRFS_I(inode)->root;
f4b9aa8d
CM
1685 int ret;
1686 struct btrfs_trans_handle *trans;
1687
1688 if (!S_ISREG(inode->i_mode))
1689 return;
1690 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1691 return;
1692
1de037a4 1693 btrfs_truncate_page(inode->i_mapping, inode->i_size);
f4b9aa8d 1694
f4b9aa8d
CM
1695 mutex_lock(&root->fs_info->fs_mutex);
1696 trans = btrfs_start_transaction(root, 1);
31f3c99b 1697 btrfs_set_trans_block_group(trans, inode);
1de037a4
CM
1698
1699 /* FIXME, add redo link to tree so we don't leak on crash */
f4b9aa8d
CM
1700 ret = btrfs_truncate_in_trans(trans, root, inode);
1701 BUG_ON(ret);
35b7e476 1702 btrfs_update_inode(trans, root, inode);
f4b9aa8d
CM
1703 ret = btrfs_end_transaction(trans, root);
1704 BUG_ON(ret);
1705 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 1706 btrfs_btree_balance_dirty(root);
f4b9aa8d
CM
1707}
1708
236454df
CM
1709static int btrfs_commit_write(struct file *file, struct page *page,
1710 unsigned from, unsigned to)
1711{
1712 struct inode *inode = page->mapping->host;
1713 struct buffer_head *bh;
1714 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1715
1716 SetPageUptodate(page);
1717 bh = page_buffers(page);
1de037a4 1718 set_buffer_uptodate(bh);
236454df
CM
1719 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1720 set_page_dirty(page);
1721 }
1722 if (pos > inode->i_size) {
1723 i_size_write(inode, pos);
1724 mark_inode_dirty(inode);
1725 }
1726 return 0;
1727}
1728
75dfe396
CM
1729static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1730 struct page **prepared_pages,
1731 const char __user * buf)
1732{
1733 long page_fault = 0;
1734 int i;
1735 int offset = pos & (PAGE_CACHE_SIZE - 1);
1736
1737 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1738 size_t count = min_t(size_t,
1739 PAGE_CACHE_SIZE - offset, write_bytes);
1740 struct page *page = prepared_pages[i];
1741 fault_in_pages_readable(buf, count);
1742
1743 /* Copy data from userspace to the current page */
1744 kmap(page);
1745 page_fault = __copy_from_user(page_address(page) + offset,
1746 buf, count);
1747 /* Flush processor's dcache for this page */
1748 flush_dcache_page(page);
1749 kunmap(page);
1750 buf += count;
1751 write_bytes -= count;
1752
1753 if (page_fault)
1754 break;
1755 }
1756 return page_fault ? -EFAULT : 0;
1757}
1758
1759static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1760{
1761 size_t i;
1762 for (i = 0; i < num_pages; i++) {
1763 if (!pages[i])
1764 break;
1765 unlock_page(pages[i]);
1766 mark_page_accessed(pages[i]);
1767 page_cache_release(pages[i]);
1768 }
1769}
1770static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1771 struct btrfs_root *root,
1772 struct file *file,
1773 struct page **pages,
1774 size_t num_pages,
1775 loff_t pos,
1776 size_t write_bytes)
1777{
1778 int i;
1779 int offset;
1780 int err = 0;
1781 int ret;
1782 int this_write;
f254e52c 1783 struct inode *inode = file->f_path.dentry->d_inode;
236454df
CM
1784 struct buffer_head *bh;
1785 struct btrfs_file_extent_item *ei;
75dfe396
CM
1786
1787 for (i = 0; i < num_pages; i++) {
1788 offset = pos & (PAGE_CACHE_SIZE -1);
1789 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
f254e52c
CM
1790 /* FIXME, one block at a time */
1791
1792 mutex_lock(&root->fs_info->fs_mutex);
1793 trans = btrfs_start_transaction(root, 1);
31f3c99b 1794 btrfs_set_trans_block_group(trans, inode);
236454df
CM
1795
1796 bh = page_buffers(pages[i]);
1797 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1798 struct btrfs_key key;
1799 struct btrfs_path *path;
1800 char *ptr;
1801 u32 datasize;
1802
1803 path = btrfs_alloc_path();
1804 BUG_ON(!path);
1805 key.objectid = inode->i_ino;
1806 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1807 key.flags = 0;
1808 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1809 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1810 datasize = offset +
1811 btrfs_file_extent_calc_inline_size(write_bytes);
1812 ret = btrfs_insert_empty_item(trans, root, path, &key,
1813 datasize);
1814 BUG_ON(ret);
1815 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1816 path->slots[0], struct btrfs_file_extent_item);
1817 btrfs_set_file_extent_generation(ei, trans->transid);
1818 btrfs_set_file_extent_type(ei,
1819 BTRFS_FILE_EXTENT_INLINE);
1820 ptr = btrfs_file_extent_inline_start(ei);
098f59c2
CM
1821 btrfs_memcpy(root, path->nodes[0]->b_data,
1822 ptr, bh->b_data, offset + write_bytes);
236454df
CM
1823 mark_buffer_dirty(path->nodes[0]);
1824 btrfs_free_path(path);
3a686375 1825 } else if (buffer_mapped(bh)) {
236454df 1826 btrfs_csum_file_block(trans, root, inode->i_ino,
f254e52c
CM
1827 pages[i]->index << PAGE_CACHE_SHIFT,
1828 kmap(pages[i]), PAGE_CACHE_SIZE);
236454df
CM
1829 kunmap(pages[i]);
1830 }
f254e52c 1831 SetPageChecked(pages[i]);
e37c9e69 1832 // btrfs_update_inode_block_group(trans, inode);
f254e52c
CM
1833 ret = btrfs_end_transaction(trans, root);
1834 BUG_ON(ret);
1835 mutex_unlock(&root->fs_info->fs_mutex);
1836
236454df 1837 ret = btrfs_commit_write(file, pages[i], offset,
75dfe396
CM
1838 offset + this_write);
1839 pos += this_write;
1840 if (ret) {
1841 err = ret;
1842 goto failed;
1843 }
1844 WARN_ON(this_write > write_bytes);
1845 write_bytes -= this_write;
1846 }
1847failed:
1848 return err;
1849}
1850
b18c6685
CM
1851static int drop_extents(struct btrfs_trans_handle *trans,
1852 struct btrfs_root *root,
1853 struct inode *inode,
de428b63 1854 u64 start, u64 end, u64 *hint_block)
b18c6685
CM
1855{
1856 int ret;
1857 struct btrfs_key key;
1858 struct btrfs_leaf *leaf;
1859 int slot;
1860 struct btrfs_file_extent_item *extent;
236454df 1861 u64 extent_end = 0;
b18c6685
CM
1862 int keep;
1863 struct btrfs_file_extent_item old;
1864 struct btrfs_path *path;
1865 u64 search_start = start;
1866 int bookend;
236454df
CM
1867 int found_type;
1868 int found_extent;
1869 int found_inline;
1870
b18c6685
CM
1871 path = btrfs_alloc_path();
1872 if (!path)
1873 return -ENOMEM;
a429e513
CM
1874 while(1) {
1875 btrfs_release_path(root, path);
1876 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1877 search_start, -1);
1878 if (ret < 0)
70b2befd 1879 goto out;
a429e513
CM
1880 if (ret > 0) {
1881 if (path->slots[0] == 0) {
236454df 1882 ret = 0;
a429e513
CM
1883 goto out;
1884 }
1885 path->slots[0]--;
70b2befd 1886 }
b18c6685
CM
1887 keep = 0;
1888 bookend = 0;
236454df
CM
1889 found_extent = 0;
1890 found_inline = 0;
1891 extent = NULL;
b18c6685
CM
1892 leaf = btrfs_buffer_leaf(path->nodes[0]);
1893 slot = path->slots[0];
1894 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
b18c6685
CM
1895 if (key.offset >= end || key.objectid != inode->i_ino) {
1896 ret = 0;
1897 goto out;
1898 }
236454df
CM
1899 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1900 ret = 0;
1901 goto out;
1902 }
1903 extent = btrfs_item_ptr(leaf, slot,
1904 struct btrfs_file_extent_item);
1905 found_type = btrfs_file_extent_type(extent);
1906 if (found_type == BTRFS_FILE_EXTENT_REG) {
1907 extent_end = key.offset +
1908 (btrfs_file_extent_num_blocks(extent) <<
1909 inode->i_blkbits);
1910 found_extent = 1;
1911 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1912 found_inline = 1;
1913 extent_end = key.offset +
1914 btrfs_file_extent_inline_len(leaf->items + slot);
1915 }
1916
1917 if (!found_extent && !found_inline) {
1918 ret = 0;
a429e513 1919 goto out;
236454df
CM
1920 }
1921
1922 if (search_start >= extent_end) {
1923 ret = 0;
a429e513 1924 goto out;
236454df
CM
1925 }
1926
3a686375
CM
1927 if (found_inline) {
1928 u64 mask = root->blocksize - 1;
1929 search_start = (extent_end + mask) & ~mask;
1930 } else
1931 search_start = extent_end;
b18c6685
CM
1932
1933 if (end < extent_end && end >= key.offset) {
236454df 1934 if (found_extent) {
3a686375
CM
1935 u64 disk_blocknr =
1936 btrfs_file_extent_disk_blocknr(extent);
1937 u64 disk_num_blocks =
1938 btrfs_file_extent_disk_num_blocks(extent);
236454df 1939 memcpy(&old, extent, sizeof(old));
3a686375
CM
1940 if (disk_blocknr != 0) {
1941 ret = btrfs_inc_extent_ref(trans, root,
1942 disk_blocknr, disk_num_blocks);
1943 BUG_ON(ret);
1944 }
236454df
CM
1945 }
1946 WARN_ON(found_inline);
b18c6685
CM
1947 bookend = 1;
1948 }
1949
1950 if (start > key.offset) {
1951 u64 new_num;
a429e513 1952 u64 old_num;
b18c6685
CM
1953 /* truncate existing extent */
1954 keep = 1;
1955 WARN_ON(start & (root->blocksize - 1));
236454df
CM
1956 if (found_extent) {
1957 new_num = (start - key.offset) >>
1958 inode->i_blkbits;
1959 old_num = btrfs_file_extent_num_blocks(extent);
de428b63
CM
1960 *hint_block =
1961 btrfs_file_extent_disk_blocknr(extent);
3a686375
CM
1962 if (btrfs_file_extent_disk_blocknr(extent)) {
1963 inode->i_blocks -=
1964 (old_num - new_num) << 3;
1965 }
236454df
CM
1966 btrfs_set_file_extent_num_blocks(extent,
1967 new_num);
1968 mark_buffer_dirty(path->nodes[0]);
1969 } else {
1970 WARN_ON(1);
236454df 1971 }
b18c6685
CM
1972 }
1973 if (!keep) {
236454df
CM
1974 u64 disk_blocknr = 0;
1975 u64 disk_num_blocks = 0;
1976 u64 extent_num_blocks = 0;
1977 if (found_extent) {
1978 disk_blocknr =
1979 btrfs_file_extent_disk_blocknr(extent);
1980 disk_num_blocks =
1981 btrfs_file_extent_disk_num_blocks(extent);
1982 extent_num_blocks =
1983 btrfs_file_extent_num_blocks(extent);
de428b63
CM
1984 *hint_block =
1985 btrfs_file_extent_disk_blocknr(extent);
236454df 1986 }
b18c6685
CM
1987 ret = btrfs_del_item(trans, root, path);
1988 BUG_ON(ret);
1989 btrfs_release_path(root, path);
098f59c2 1990 extent = NULL;
3a686375 1991 if (found_extent && disk_blocknr != 0) {
098f59c2 1992 inode->i_blocks -= extent_num_blocks << 3;
236454df
CM
1993 ret = btrfs_free_extent(trans, root,
1994 disk_blocknr,
1995 disk_num_blocks, 0);
1996 }
b18c6685
CM
1997
1998 BUG_ON(ret);
1999 if (!bookend && search_start >= end) {
2000 ret = 0;
2001 goto out;
2002 }
2003 if (!bookend)
a429e513 2004 continue;
b18c6685 2005 }
236454df 2006 if (bookend && found_extent) {
b18c6685
CM
2007 /* create bookend */
2008 struct btrfs_key ins;
b18c6685
CM
2009 ins.objectid = inode->i_ino;
2010 ins.offset = end;
2011 ins.flags = 0;
2012 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
2013
2014 btrfs_release_path(root, path);
b18c6685
CM
2015 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2016 sizeof(*extent));
2017 BUG_ON(ret);
2018 extent = btrfs_item_ptr(
2019 btrfs_buffer_leaf(path->nodes[0]),
2020 path->slots[0],
2021 struct btrfs_file_extent_item);
2022 btrfs_set_file_extent_disk_blocknr(extent,
2023 btrfs_file_extent_disk_blocknr(&old));
2024 btrfs_set_file_extent_disk_num_blocks(extent,
2025 btrfs_file_extent_disk_num_blocks(&old));
2026
2027 btrfs_set_file_extent_offset(extent,
2028 btrfs_file_extent_offset(&old) +
2029 ((end - key.offset) >> inode->i_blkbits));
2030 WARN_ON(btrfs_file_extent_num_blocks(&old) <
3a686375 2031 (extent_end - end) >> inode->i_blkbits);
b18c6685 2032 btrfs_set_file_extent_num_blocks(extent,
3a686375 2033 (extent_end - end) >> inode->i_blkbits);
b18c6685 2034
236454df
CM
2035 btrfs_set_file_extent_type(extent,
2036 BTRFS_FILE_EXTENT_REG);
b18c6685
CM
2037 btrfs_set_file_extent_generation(extent,
2038 btrfs_file_extent_generation(&old));
b18c6685 2039 btrfs_mark_buffer_dirty(path->nodes[0]);
3a686375
CM
2040 if (btrfs_file_extent_disk_blocknr(&old) != 0) {
2041 inode->i_blocks +=
2042 btrfs_file_extent_num_blocks(extent) << 3;
2043 }
b18c6685 2044 ret = 0;
70b2befd 2045 goto out;
b18c6685 2046 }
b18c6685 2047 }
b18c6685 2048out:
b18c6685
CM
2049 btrfs_free_path(path);
2050 return ret;
2051}
2052
2053static int prepare_pages(struct btrfs_root *root,
75dfe396
CM
2054 struct file *file,
2055 struct page **pages,
2056 size_t num_pages,
2057 loff_t pos,
2932f3ec
CM
2058 unsigned long first_index,
2059 unsigned long last_index,
6567e837
CM
2060 size_t write_bytes,
2061 u64 alloc_extent_start)
75dfe396
CM
2062{
2063 int i;
2064 unsigned long index = pos >> PAGE_CACHE_SHIFT;
2065 struct inode *inode = file->f_path.dentry->d_inode;
2066 int offset;
2067 int err = 0;
75dfe396 2068 int this_write;
6567e837
CM
2069 struct buffer_head *bh;
2070 struct buffer_head *head;
75dfe396
CM
2071 loff_t isize = i_size_read(inode);
2072
2073 memset(pages, 0, num_pages * sizeof(struct page *));
2074
2075 for (i = 0; i < num_pages; i++) {
2076 pages[i] = grab_cache_page(inode->i_mapping, index + i);
2077 if (!pages[i]) {
2078 err = -ENOMEM;
2079 goto failed_release;
2080 }
35b7e476
CM
2081 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
2082 wait_on_page_writeback(pages[i]);
75dfe396
CM
2083 offset = pos & (PAGE_CACHE_SIZE -1);
2084 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
35b7e476
CM
2085 if (!page_has_buffers(pages[i])) {
2086 create_empty_buffers(pages[i],
2087 root->fs_info->sb->s_blocksize,
2088 (1 << BH_Uptodate));
2089 }
6567e837
CM
2090 head = page_buffers(pages[i]);
2091 bh = head;
2092 do {
2093 err = btrfs_map_bh_to_logical(root, bh,
2094 alloc_extent_start);
2095 BUG_ON(err);
2096 if (err)
2097 goto failed_truncate;
2098 bh = bh->b_this_page;
236454df
CM
2099 if (alloc_extent_start)
2100 alloc_extent_start++;
6567e837 2101 } while (bh != head);
75dfe396 2102 pos += this_write;
75dfe396
CM
2103 WARN_ON(this_write > write_bytes);
2104 write_bytes -= this_write;
2105 }
2106 return 0;
2107
2108failed_release:
2109 btrfs_drop_pages(pages, num_pages);
2110 return err;
2111
2112failed_truncate:
2113 btrfs_drop_pages(pages, num_pages);
2114 if (pos > isize)
2115 vmtruncate(inode, isize);
2116 return err;
2117}
2118
2119static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
2120 size_t count, loff_t *ppos)
2121{
2122 loff_t pos;
2123 size_t num_written = 0;
2124 int err = 0;
2125 int ret = 0;
75dfe396 2126 struct inode *inode = file->f_path.dentry->d_inode;
d6e4a428 2127 struct btrfs_root *root = BTRFS_I(inode)->root;
b18c6685 2128 struct page *pages[8];
35b7e476 2129 struct page *pinned[2];
2932f3ec
CM
2130 unsigned long first_index;
2131 unsigned long last_index;
6567e837
CM
2132 u64 start_pos;
2133 u64 num_blocks;
2134 u64 alloc_extent_start;
de428b63 2135 u64 hint_block;
6567e837 2136 struct btrfs_trans_handle *trans;
b18c6685 2137 struct btrfs_key ins;
35b7e476
CM
2138 pinned[0] = NULL;
2139 pinned[1] = NULL;
75dfe396
CM
2140 if (file->f_flags & O_DIRECT)
2141 return -EINVAL;
2142 pos = *ppos;
75dfe396
CM
2143 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2144 current->backing_dev_info = inode->i_mapping->backing_dev_info;
2145 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2146 if (err)
2147 goto out;
2148 if (count == 0)
2149 goto out;
2150 err = remove_suid(file->f_path.dentry);
2151 if (err)
2152 goto out;
2153 file_update_time(file);
a429e513
CM
2154
2155 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
2156 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
2157 inode->i_blkbits;
2158
75dfe396 2159 mutex_lock(&inode->i_mutex);
2932f3ec
CM
2160 first_index = pos >> PAGE_CACHE_SHIFT;
2161 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
6567e837 2162
1de037a4 2163 if ((pos & (PAGE_CACHE_SIZE - 1))) {
a429e513
CM
2164 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
2165 if (!PageUptodate(pinned[0])) {
2166 ret = mpage_readpage(pinned[0], btrfs_get_block);
2167 BUG_ON(ret);
35b7e476 2168 wait_on_page_locked(pinned[0]);
a429e513
CM
2169 } else {
2170 unlock_page(pinned[0]);
2171 }
2172 }
1de037a4 2173 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
a429e513
CM
2174 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
2175 if (!PageUptodate(pinned[1])) {
2176 ret = mpage_readpage(pinned[1], btrfs_get_block);
2177 BUG_ON(ret);
35b7e476 2178 wait_on_page_locked(pinned[1]);
a429e513
CM
2179 } else {
2180 unlock_page(pinned[1]);
2181 }
2182 }
2183
6567e837
CM
2184 mutex_lock(&root->fs_info->fs_mutex);
2185 trans = btrfs_start_transaction(root, 1);
2186 if (!trans) {
2187 err = -ENOMEM;
b18c6685 2188 mutex_unlock(&root->fs_info->fs_mutex);
6567e837
CM
2189 goto out_unlock;
2190 }
31f3c99b 2191 btrfs_set_trans_block_group(trans, inode);
a429e513
CM
2192 /* FIXME blocksize != 4096 */
2193 inode->i_blocks += num_blocks << 3;
de428b63 2194 hint_block = 0;
b18c6685 2195 if (start_pos < inode->i_size) {
a429e513 2196 /* FIXME blocksize != pagesize */
b18c6685
CM
2197 ret = drop_extents(trans, root, inode,
2198 start_pos,
2199 (pos + count + root->blocksize -1) &
de428b63 2200 ~((u64)root->blocksize - 1), &hint_block);
236454df 2201 BUG_ON(ret);
b18c6685 2202 }
3a686375
CM
2203 if (inode->i_size < start_pos) {
2204 u64 last_pos_in_file;
2205 u64 hole_size;
2206 u64 mask = root->blocksize - 1;
2207 last_pos_in_file = (inode->i_size + mask) & ~mask;
2208 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
2209 hole_size >>= inode->i_blkbits;
2210 if (last_pos_in_file < start_pos) {
2211 ret = btrfs_insert_file_extent(trans, root,
2212 inode->i_ino,
2213 last_pos_in_file,
2214 0, 0, hole_size);
2215 }
2216 BUG_ON(ret);
2217 }
236454df
CM
2218 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
2219 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
4d775673 2220 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
de428b63
CM
2221 num_blocks, hint_block, (u64)-1,
2222 &ins, 1);
236454df
CM
2223 BUG_ON(ret);
2224 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
3a686375
CM
2225 start_pos, ins.objectid, ins.offset,
2226 ins.offset);
236454df
CM
2227 BUG_ON(ret);
2228 } else {
2229 ins.offset = 0;
2230 ins.objectid = 0;
2231 }
6567e837 2232 BUG_ON(ret);
b18c6685 2233 alloc_extent_start = ins.objectid;
e37c9e69 2234 // btrfs_update_inode_block_group(trans, inode);
b18c6685 2235 ret = btrfs_end_transaction(trans, root);
6567e837
CM
2236 mutex_unlock(&root->fs_info->fs_mutex);
2237
75dfe396
CM
2238 while(count > 0) {
2239 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
2240 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
2241 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
2242 PAGE_CACHE_SHIFT;
b18c6685
CM
2243
2244 memset(pages, 0, sizeof(pages));
2245 ret = prepare_pages(root, file, pages, num_pages,
6567e837
CM
2246 pos, first_index, last_index,
2247 write_bytes, alloc_extent_start);
75dfe396 2248 BUG_ON(ret);
b18c6685 2249
6567e837 2250 /* FIXME blocks != pagesize */
236454df
CM
2251 if (alloc_extent_start)
2252 alloc_extent_start += num_pages;
75dfe396
CM
2253 ret = btrfs_copy_from_user(pos, num_pages,
2254 write_bytes, pages, buf);
2255 BUG_ON(ret);
2256
f254e52c 2257 ret = dirty_and_release_pages(NULL, root, file, pages,
70b2befd 2258 num_pages, pos, write_bytes);
75dfe396
CM
2259 BUG_ON(ret);
2260 btrfs_drop_pages(pages, num_pages);
2261
75dfe396
CM
2262 buf += write_bytes;
2263 count -= write_bytes;
2264 pos += write_bytes;
2265 num_written += write_bytes;
2266
2267 balance_dirty_pages_ratelimited(inode->i_mapping);
35b7e476 2268 btrfs_btree_balance_dirty(root);
75dfe396
CM
2269 cond_resched();
2270 }
6567e837 2271out_unlock:
75dfe396
CM
2272 mutex_unlock(&inode->i_mutex);
2273out:
a429e513
CM
2274 if (pinned[0])
2275 page_cache_release(pinned[0]);
2276 if (pinned[1])
2277 page_cache_release(pinned[1]);
75dfe396
CM
2278 *ppos = pos;
2279 current->backing_dev_info = NULL;
a429e513 2280 mark_inode_dirty(inode);
75dfe396
CM
2281 return num_written ? num_written : err;
2282}
2283
f254e52c
CM
2284static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
2285 unsigned long offset, unsigned long size)
2286{
2287 char *kaddr;
2288 unsigned long left, count = desc->count;
d6e4a428 2289 struct inode *inode = page->mapping->host;
f254e52c
CM
2290
2291 if (size > count)
2292 size = count;
2293
2294 if (!PageChecked(page)) {
2295 /* FIXME, do it per block */
d6e4a428 2296 struct btrfs_root *root = BTRFS_I(inode)->root;
1de037a4
CM
2297 int ret;
2298 struct buffer_head *bh;
2299
2300 if (page_has_buffers(page)) {
2301 bh = page_buffers(page);
2302 if (!buffer_mapped(bh)) {
2303 SetPageChecked(page);
2304 goto checked;
2305 }
2306 }
236454df 2307
1de037a4 2308 ret = btrfs_csum_verify_file_block(root,
236454df
CM
2309 page->mapping->host->i_ino,
2310 page->index << PAGE_CACHE_SHIFT,
2311 kmap(page), PAGE_CACHE_SIZE);
f254e52c 2312 if (ret) {
3a686375 2313 if (ret != -ENOENT) {
1de037a4 2314 printk("failed to verify ino %lu page %lu ret %d\n",
3a686375 2315 page->mapping->host->i_ino,
1de037a4
CM
2316 page->index, ret);
2317 memset(page_address(page), 1, PAGE_CACHE_SIZE);
3a686375
CM
2318 flush_dcache_page(page);
2319 }
f254e52c
CM
2320 }
2321 SetPageChecked(page);
2322 kunmap(page);
2323 }
1de037a4 2324checked:
f254e52c
CM
2325 /*
2326 * Faults on the destination of a read are common, so do it before
2327 * taking the kmap.
2328 */
2329 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2330 kaddr = kmap_atomic(page, KM_USER0);
2331 left = __copy_to_user_inatomic(desc->arg.buf,
2332 kaddr + offset, size);
2333 kunmap_atomic(kaddr, KM_USER0);
2334 if (left == 0)
2335 goto success;
2336 }
2337
2338 /* Do it the slow way */
2339 kaddr = kmap(page);
2340 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2341 kunmap(page);
2342
2343 if (left) {
2344 size -= left;
2345 desc->error = -EFAULT;
2346 }
2347success:
2348 desc->count = count - size;
2349 desc->written += size;
2350 desc->arg.buf += size;
2351 return size;
2352}
2353
2354/**
2355 * btrfs_file_aio_read - filesystem read routine
2356 * @iocb: kernel I/O control block
2357 * @iov: io vector request
2358 * @nr_segs: number of segments in the iovec
2359 * @pos: current file position
2360 */
2361static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2362 unsigned long nr_segs, loff_t pos)
2363{
2364 struct file *filp = iocb->ki_filp;
2365 ssize_t retval;
2366 unsigned long seg;
2367 size_t count;
2368 loff_t *ppos = &iocb->ki_pos;
2369
2370 count = 0;
2371 for (seg = 0; seg < nr_segs; seg++) {
2372 const struct iovec *iv = &iov[seg];
2373
2374 /*
2375 * If any segment has a negative length, or the cumulative
2376 * length ever wraps negative then return -EINVAL.
2377 */
2378 count += iv->iov_len;
2379 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2380 return -EINVAL;
2381 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2382 continue;
2383 if (seg == 0)
2384 return -EFAULT;
2385 nr_segs = seg;
2386 count -= iv->iov_len; /* This segment is no good */
2387 break;
2388 }
2389 retval = 0;
2390 if (count) {
2391 for (seg = 0; seg < nr_segs; seg++) {
2392 read_descriptor_t desc;
2393
2394 desc.written = 0;
2395 desc.arg.buf = iov[seg].iov_base;
2396 desc.count = iov[seg].iov_len;
2397 if (desc.count == 0)
2398 continue;
2399 desc.error = 0;
2400 do_generic_file_read(filp, ppos, &desc,
2401 btrfs_read_actor);
2402 retval += desc.written;
2403 if (desc.error) {
2404 retval = retval ?: desc.error;
2405 break;
2406 }
2407 }
2408 }
2409 return retval;
2410}
2411
2619ba1f
CM
2412static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2413{
2414 struct btrfs_trans_handle *trans;
2415 struct btrfs_key key;
2416 struct btrfs_root_item root_item;
2417 struct btrfs_inode_item *inode_item;
2418 struct buffer_head *subvol;
2419 struct btrfs_leaf *leaf;
2420 struct btrfs_root *new_root;
2421 struct inode *inode;
31f3c99b 2422 struct inode *dir;
2619ba1f
CM
2423 int ret;
2424 u64 objectid;
2425 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2426
2427 mutex_lock(&root->fs_info->fs_mutex);
2428 trans = btrfs_start_transaction(root, 1);
2429 BUG_ON(!trans);
2430
31f3c99b 2431 subvol = btrfs_alloc_free_block(trans, root, 0);
5e82849e
CM
2432 if (subvol == NULL)
2433 return -ENOSPC;
2619ba1f
CM
2434 leaf = btrfs_buffer_leaf(subvol);
2435 btrfs_set_header_nritems(&leaf->header, 0);
2436 btrfs_set_header_level(&leaf->header, 0);
7eccb903 2437 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2619ba1f 2438 btrfs_set_header_generation(&leaf->header, trans->transid);
4d775673 2439 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2619ba1f
CM
2440 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2441 sizeof(leaf->header.fsid));
4d775673 2442 mark_buffer_dirty(subvol);
2619ba1f
CM
2443
2444 inode_item = &root_item.inode;
2445 memset(inode_item, 0, sizeof(*inode_item));
2446 btrfs_set_inode_generation(inode_item, 1);
2447 btrfs_set_inode_size(inode_item, 3);
2448 btrfs_set_inode_nlink(inode_item, 1);
2449 btrfs_set_inode_nblocks(inode_item, 1);
2450 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2451
7eccb903 2452 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2619ba1f 2453 btrfs_set_root_refs(&root_item, 1);
5e82849e
CM
2454 brelse(subvol);
2455 subvol = NULL;
2619ba1f 2456
2619ba1f
CM
2457 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2458 0, &objectid);
2459 BUG_ON(ret);
2460
2461 btrfs_set_root_dirid(&root_item, new_dirid);
2462
2463 key.objectid = objectid;
2464 key.offset = 1;
2465 key.flags = 0;
2466 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2467 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2468 &root_item);
2469 BUG_ON(ret);
2470
2471 /*
2472 * insert the directory item
2473 */
2474 key.offset = (u64)-1;
31f3c99b 2475 dir = root->fs_info->sb->s_root->d_inode;
2619ba1f 2476 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
fabb5681
CM
2477 name, namelen, dir->i_ino, &key,
2478 BTRFS_FT_DIR);
2619ba1f
CM
2479 BUG_ON(ret);
2480
2481 ret = btrfs_commit_transaction(trans, root);
2482 BUG_ON(ret);
2483
2484 new_root = btrfs_read_fs_root(root->fs_info, &key);
2485 BUG_ON(!new_root);
2486
2487 trans = btrfs_start_transaction(new_root, 1);
2488 BUG_ON(!trans);
2489
31f3c99b
CM
2490 inode = btrfs_new_inode(trans, new_root, new_dirid,
2491 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2619ba1f
CM
2492 inode->i_op = &btrfs_dir_inode_operations;
2493 inode->i_fop = &btrfs_dir_file_operations;
2494
2495 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2496 BUG_ON(ret);
2497
2498 inode->i_nlink = 1;
2499 inode->i_size = 6;
2500 ret = btrfs_update_inode(trans, new_root, inode);
2501 BUG_ON(ret);
2502
2503 ret = btrfs_commit_transaction(trans, new_root);
2504 BUG_ON(ret);
2505
2506 iput(inode);
2507
2508 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 2509 btrfs_btree_balance_dirty(root);
2619ba1f
CM
2510 return 0;
2511}
2512
c5739bba
CM
2513static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2514{
2515 struct btrfs_trans_handle *trans;
2516 struct btrfs_key key;
2517 struct btrfs_root_item new_root_item;
2518 int ret;
2519 u64 objectid;
2520
2619ba1f
CM
2521 if (!root->ref_cows)
2522 return -EINVAL;
2523
c5739bba
CM
2524 mutex_lock(&root->fs_info->fs_mutex);
2525 trans = btrfs_start_transaction(root, 1);
2526 BUG_ON(!trans);
2527
2528 ret = btrfs_update_inode(trans, root, root->inode);
2529 BUG_ON(ret);
2530
1b05da2e
CM
2531 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2532 0, &objectid);
c5739bba
CM
2533 BUG_ON(ret);
2534
c5739bba
CM
2535 memcpy(&new_root_item, &root->root_item,
2536 sizeof(new_root_item));
2537
c5739bba
CM
2538 key.objectid = objectid;
2539 key.offset = 1;
2540 key.flags = 0;
2541 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7eccb903 2542 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
c5739bba
CM
2543
2544 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2545 &new_root_item);
2546 BUG_ON(ret);
2547
c5739bba
CM
2548 /*
2549 * insert the directory item
2550 */
2551 key.offset = (u64)-1;
2552 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2553 name, namelen,
2554 root->fs_info->sb->s_root->d_inode->i_ino,
fabb5681 2555 &key, BTRFS_FT_DIR);
c5739bba
CM
2556
2557 BUG_ON(ret);
2558
2559 ret = btrfs_inc_root_ref(trans, root);
2560 BUG_ON(ret);
2561
2562 ret = btrfs_commit_transaction(trans, root);
2563 BUG_ON(ret);
2564 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 2565 btrfs_btree_balance_dirty(root);
c5739bba
CM
2566 return 0;
2567}
2568
2569static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2570 cmd, unsigned long arg)
2571{
2572 struct btrfs_root *root = BTRFS_I(inode)->root;
2573 struct btrfs_ioctl_vol_args vol_args;
8352d8a4 2574 int ret = 0;
7e38180e 2575 struct btrfs_dir_item *di;
c5739bba 2576 int namelen;
2619ba1f
CM
2577 struct btrfs_path *path;
2578 u64 root_dirid;
c5739bba 2579
c5739bba
CM
2580 switch (cmd) {
2581 case BTRFS_IOC_SNAP_CREATE:
2582 if (copy_from_user(&vol_args,
2583 (struct btrfs_ioctl_vol_args __user *)arg,
2584 sizeof(vol_args)))
2585 return -EFAULT;
2586 namelen = strlen(vol_args.name);
2587 if (namelen > BTRFS_VOL_NAME_MAX)
2588 return -EINVAL;
2619ba1f
CM
2589 path = btrfs_alloc_path();
2590 if (!path)
2591 return -ENOMEM;
2d13d8d0 2592 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2619ba1f 2593 mutex_lock(&root->fs_info->fs_mutex);
7e38180e 2594 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2619ba1f
CM
2595 path, root_dirid,
2596 vol_args.name, namelen, 0);
2597 mutex_unlock(&root->fs_info->fs_mutex);
2d13d8d0 2598 btrfs_free_path(path);
7e38180e 2599 if (di && !IS_ERR(di))
2619ba1f
CM
2600 return -EEXIST;
2601
2602 if (root == root->fs_info->tree_root)
2603 ret = create_subvol(root, vol_args.name, namelen);
2604 else
2605 ret = create_snapshot(root, vol_args.name, namelen);
c5739bba
CM
2606 WARN_ON(ret);
2607 break;
2608 default:
2609 return -ENOTTY;
2610 }
8352d8a4 2611 return ret;
c5739bba
CM
2612}
2613
08607c1b
CM
2614#ifdef CONFIG_COMPAT
2615static long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2616 unsigned long arg)
2617{
2618 struct inode *inode = file->f_path.dentry->d_inode;
2619 int ret;
2620 lock_kernel();
2621 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2622 unlock_kernel();
2623 return ret;
2624
2625}
2626#endif
2627
2c90e5d6
CM
2628static struct kmem_cache *btrfs_inode_cachep;
2629struct kmem_cache *btrfs_trans_handle_cachep;
2630struct kmem_cache *btrfs_transaction_cachep;
2631struct kmem_cache *btrfs_bit_radix_cachep;
2632struct kmem_cache *btrfs_path_cachep;
2633
2634/*
2635 * Called inside transaction, so use GFP_NOFS
2636 */
2637static struct inode *btrfs_alloc_inode(struct super_block *sb)
2638{
2639 struct btrfs_inode *ei;
2640
2641 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2642 if (!ei)
2643 return NULL;
2c90e5d6
CM
2644 return &ei->vfs_inode;
2645}
2646
2647static void btrfs_destroy_inode(struct inode *inode)
2648{
2c90e5d6 2649 WARN_ON(!list_empty(&inode->i_dentry));
2c90e5d6
CM
2650 WARN_ON(inode->i_data.nrpages);
2651
2c90e5d6
CM
2652 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2653}
2654
2655static void init_once(void * foo, struct kmem_cache * cachep,
2656 unsigned long flags)
2657{
2658 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2659
f9f3c6b6 2660 if ((flags & (SLAB_CTOR_CONSTRUCTOR)) ==
2c90e5d6
CM
2661 SLAB_CTOR_CONSTRUCTOR) {
2662 inode_init_once(&ei->vfs_inode);
2663 }
2664}
2665
2666static int init_inodecache(void)
2667{
2668 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2669 sizeof(struct btrfs_inode),
2670 0, (SLAB_RECLAIM_ACCOUNT|
2671 SLAB_MEM_SPREAD),
2672 init_once, NULL);
2673 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2674 sizeof(struct btrfs_trans_handle),
2675 0, (SLAB_RECLAIM_ACCOUNT|
2676 SLAB_MEM_SPREAD),
2677 NULL, NULL);
2678 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2679 sizeof(struct btrfs_transaction),
2680 0, (SLAB_RECLAIM_ACCOUNT|
2681 SLAB_MEM_SPREAD),
2682 NULL, NULL);
2683 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2684 sizeof(struct btrfs_transaction),
2685 0, (SLAB_RECLAIM_ACCOUNT|
2686 SLAB_MEM_SPREAD),
2687 NULL, NULL);
2688 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2689 256,
2690 0, (SLAB_RECLAIM_ACCOUNT|
2691 SLAB_MEM_SPREAD |
2692 SLAB_DESTROY_BY_RCU),
2693 NULL, NULL);
2694 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2695 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2696 return -ENOMEM;
2697 return 0;
2698}
2699
2700static void destroy_inodecache(void)
2701{
2702 kmem_cache_destroy(btrfs_inode_cachep);
2703 kmem_cache_destroy(btrfs_trans_handle_cachep);
2704 kmem_cache_destroy(btrfs_transaction_cachep);
2705 kmem_cache_destroy(btrfs_bit_radix_cachep);
2706 kmem_cache_destroy(btrfs_path_cachep);
2707}
2708
2e635a27
CM
2709static int btrfs_get_sb(struct file_system_type *fs_type,
2710 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2711{
2712 return get_sb_bdev(fs_type, flags, dev_name, data,
2713 btrfs_fill_super, mnt);
2714}
2715
236454df
CM
2716static int btrfs_getattr(struct vfsmount *mnt,
2717 struct dentry *dentry, struct kstat *stat)
2718{
2719 struct inode *inode = dentry->d_inode;
2720 generic_fillattr(inode, stat);
2721 stat->blksize = 256 * 1024;
2722 return 0;
2723}
2724
8fd17795
CM
2725static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2726{
2727 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2728 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2729
2730 buf->f_namelen = BTRFS_NAME_LEN;
2731 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2732 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2733 buf->f_bavail = buf->f_bfree;
2734 buf->f_bsize = dentry->d_sb->s_blocksize;
2735 buf->f_type = BTRFS_SUPER_MAGIC;
2736 return 0;
2737}
b5133862 2738
e06afa83
CM
2739static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2740 struct inode * new_dir,struct dentry *new_dentry)
2741{
2742 struct btrfs_trans_handle *trans;
2743 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2744 struct inode *new_inode = new_dentry->d_inode;
2745 struct inode *old_inode = old_dentry->d_inode;
2746 struct timespec ctime = CURRENT_TIME;
2747 struct btrfs_path *path;
2748 struct btrfs_dir_item *di;
2749 int ret;
2750
2751 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2752 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2753 return -ENOTEMPTY;
2754 }
2755 mutex_lock(&root->fs_info->fs_mutex);
2756 trans = btrfs_start_transaction(root, 1);
2757 btrfs_set_trans_block_group(trans, new_dir);
2758 path = btrfs_alloc_path();
2759 if (!path) {
2760 ret = -ENOMEM;
2761 goto out_fail;
2762 }
2763
2764 old_dentry->d_inode->i_nlink++;
2765 old_dir->i_ctime = old_dir->i_mtime = ctime;
2766 new_dir->i_ctime = new_dir->i_mtime = ctime;
2767 old_inode->i_ctime = ctime;
2768 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2769 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2770 u64 old_parent_oid;
2771 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2772 "..", 2, -1);
2773 if (IS_ERR(di)) {
2774 ret = PTR_ERR(di);
2775 goto out_fail;
2776 }
2777 if (!di) {
2778 ret = -ENOENT;
2779 goto out_fail;
2780 }
2781 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2782 ret = btrfs_del_item(trans, root, path);
2783 if (ret) {
2784 ret = -EIO;
2785 goto out_fail;
2786 }
2787 btrfs_release_path(root, path);
2788
2789 di = btrfs_lookup_dir_index_item(trans, root, path,
2790 old_inode->i_ino,
2791 old_parent_oid,
2792 "..", 2, -1);
2793 if (IS_ERR(di)) {
2794 ret = PTR_ERR(di);
2795 goto out_fail;
2796 }
2797 if (!di) {
2798 ret = -ENOENT;
2799 goto out_fail;
2800 }
2801 ret = btrfs_del_item(trans, root, path);
2802 if (ret) {
2803 ret = -EIO;
2804 goto out_fail;
2805 }
2806 btrfs_release_path(root, path);
2807
2808 ret = btrfs_insert_dir_item(trans, root, "..", 2,
fabb5681
CM
2809 old_inode->i_ino, location,
2810 BTRFS_FT_DIR);
e06afa83
CM
2811 if (ret)
2812 goto out_fail;
2813 }
2814
2815
e06afa83
CM
2816 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2817 if (ret)
2818 goto out_fail;
2819
2820 if (new_inode) {
2821 new_inode->i_ctime = CURRENT_TIME;
2b8d99a7
CM
2822 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2823 if (ret)
2824 goto out_fail;
e06afa83
CM
2825 if (S_ISDIR(new_inode->i_mode))
2826 clear_nlink(new_inode);
2827 else
2828 drop_nlink(new_inode);
2829 btrfs_update_inode(trans, root, new_inode);
2830 }
2b8d99a7
CM
2831 ret = btrfs_add_link(trans, new_dentry, old_inode);
2832 if (ret)
2833 goto out_fail;
2834
e06afa83
CM
2835out_fail:
2836 btrfs_free_path(path);
2837 btrfs_end_transaction(trans, root);
2838 mutex_unlock(&root->fs_info->fs_mutex);
2839 return ret;
2840}
2841
2b8d99a7
CM
2842static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2843 const char *symname)
2844{
2845 struct btrfs_trans_handle *trans;
2846 struct btrfs_root *root = BTRFS_I(dir)->root;
2847 struct btrfs_path *path;
2848 struct btrfs_key key;
2849 struct inode *inode;
2850 int err;
2851 int drop_inode = 0;
2852 u64 objectid;
2853 int name_len;
2854 int datasize;
2855 char *ptr;
2856 struct btrfs_file_extent_item *ei;
2857
2858 name_len = strlen(symname) + 1;
2859 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2860 return -ENAMETOOLONG;
2861 mutex_lock(&root->fs_info->fs_mutex);
2862 trans = btrfs_start_transaction(root, 1);
2863 btrfs_set_trans_block_group(trans, dir);
2864
2865 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2866 if (err) {
2867 err = -ENOSPC;
2868 goto out_unlock;
2869 }
2870
2871 inode = btrfs_new_inode(trans, root, objectid,
2872 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2873 err = PTR_ERR(inode);
2874 if (IS_ERR(inode))
2875 goto out_unlock;
2876
2877 btrfs_set_trans_block_group(trans, inode);
2878 err = btrfs_add_nondir(trans, dentry, inode);
2879 if (err)
2880 drop_inode = 1;
2881 else {
2882 inode->i_mapping->a_ops = &btrfs_aops;
2883 inode->i_fop = &btrfs_file_operations;
2884 inode->i_op = &btrfs_file_inode_operations;
2885 }
2886 dir->i_sb->s_dirt = 1;
2887 btrfs_update_inode_block_group(trans, inode);
2888 btrfs_update_inode_block_group(trans, dir);
2889 if (drop_inode)
2890 goto out_unlock;
2891
2892 path = btrfs_alloc_path();
2893 BUG_ON(!path);
2894 key.objectid = inode->i_ino;
2895 key.offset = 0;
2896 key.flags = 0;
2897 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2898 datasize = btrfs_file_extent_calc_inline_size(name_len);
2899 err = btrfs_insert_empty_item(trans, root, path, &key,
2900 datasize);
2901 BUG_ON(err);
2902 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2903 path->slots[0], struct btrfs_file_extent_item);
2904 btrfs_set_file_extent_generation(ei, trans->transid);
2905 btrfs_set_file_extent_type(ei,
2906 BTRFS_FILE_EXTENT_INLINE);
2907 ptr = btrfs_file_extent_inline_start(ei);
2908 btrfs_memcpy(root, path->nodes[0]->b_data,
2909 ptr, symname, name_len);
2910 mark_buffer_dirty(path->nodes[0]);
2911 btrfs_free_path(path);
2912 inode->i_op = &btrfs_symlink_inode_operations;
2913 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2914 inode->i_size = name_len - 1;
2915 btrfs_update_inode(trans, root, inode);
2916 err = 0;
2917
2918out_unlock:
2919 btrfs_end_transaction(trans, root);
2920 mutex_unlock(&root->fs_info->fs_mutex);
2921
2922 if (drop_inode) {
2923 inode_dec_link_count(inode);
2924 iput(inode);
2925 }
2926 btrfs_btree_balance_dirty(root);
2927 return err;
2928}
2929
2e635a27
CM
2930static struct file_system_type btrfs_fs_type = {
2931 .owner = THIS_MODULE,
2932 .name = "btrfs",
2933 .get_sb = btrfs_get_sb,
2934 .kill_sb = kill_block_super,
2935 .fs_flags = FS_REQUIRES_DEV,
2936};
2937
e20d96d6 2938static struct super_operations btrfs_super_ops = {
134e9731 2939 .delete_inode = btrfs_delete_inode,
e20d96d6
CM
2940 .put_super = btrfs_put_super,
2941 .read_inode = btrfs_read_locked_inode,
d5719762
CM
2942 .write_super = btrfs_write_super,
2943 .sync_fs = btrfs_sync_fs,
4730a4bc 2944 .write_inode = btrfs_write_inode,
b5133862 2945 .dirty_inode = btrfs_dirty_inode,
2c90e5d6
CM
2946 .alloc_inode = btrfs_alloc_inode,
2947 .destroy_inode = btrfs_destroy_inode,
8fd17795 2948 .statfs = btrfs_statfs,
e20d96d6
CM
2949};
2950
2951static struct inode_operations btrfs_dir_inode_operations = {
2952 .lookup = btrfs_lookup,
d5719762 2953 .create = btrfs_create,
134e9731 2954 .unlink = btrfs_unlink,
2b8d99a7 2955 .link = btrfs_link,
f7922033 2956 .mkdir = btrfs_mkdir,
5f443fd2 2957 .rmdir = btrfs_rmdir,
e06afa83 2958 .rename = btrfs_rename,
2b8d99a7 2959 .symlink = btrfs_symlink,
3a686375 2960 .setattr = btrfs_setattr,
e20d96d6
CM
2961};
2962
d6e4a428
CM
2963static struct inode_operations btrfs_dir_ro_inode_operations = {
2964 .lookup = btrfs_lookup,
2965};
2966
e20d96d6
CM
2967static struct file_operations btrfs_dir_file_operations = {
2968 .llseek = generic_file_llseek,
2969 .read = generic_read_dir,
2970 .readdir = btrfs_readdir,
c5739bba 2971 .ioctl = btrfs_ioctl,
08607c1b
CM
2972#ifdef CONFIG_COMPAT
2973 .compat_ioctl = btrfs_compat_ioctl,
2974#endif
e20d96d6
CM
2975};
2976
dee26a9f
CM
2977static struct address_space_operations btrfs_aops = {
2978 .readpage = btrfs_readpage,
dee26a9f
CM
2979 .writepage = btrfs_writepage,
2980 .sync_page = block_sync_page,
2981 .prepare_write = btrfs_prepare_write,
75dfe396 2982 .commit_write = btrfs_commit_write,
fabb5681 2983 .bmap = btrfs_bmap,
dee26a9f
CM
2984};
2985
2b8d99a7
CM
2986static struct address_space_operations btrfs_symlink_aops = {
2987 .readpage = btrfs_readpage,
2988 .writepage = btrfs_writepage,
2989};
2990
dee26a9f 2991static struct inode_operations btrfs_file_inode_operations = {
f4b9aa8d 2992 .truncate = btrfs_truncate,
236454df 2993 .getattr = btrfs_getattr,
3a686375 2994 .setattr = btrfs_setattr,
dee26a9f
CM
2995};
2996
2997static struct file_operations btrfs_file_operations = {
2998 .llseek = generic_file_llseek,
2999 .read = do_sync_read,
e8f05c45
CM
3000 .aio_read = btrfs_file_aio_read,
3001 .write = btrfs_file_write,
dee26a9f
CM
3002 .mmap = generic_file_mmap,
3003 .open = generic_file_open,
c5739bba 3004 .ioctl = btrfs_ioctl,
8fd17795 3005 .fsync = btrfs_sync_file,
08607c1b
CM
3006#ifdef CONFIG_COMPAT
3007 .compat_ioctl = btrfs_compat_ioctl,
3008#endif
dee26a9f 3009};
e20d96d6 3010
2b8d99a7
CM
3011static struct inode_operations btrfs_symlink_inode_operations = {
3012 .readlink = generic_readlink,
3013 .follow_link = page_follow_link_light,
3014 .put_link = page_put_link,
3015};
3016
2e635a27
CM
3017static int __init init_btrfs_fs(void)
3018{
2c90e5d6 3019 int err;
2e635a27 3020 printk("btrfs loaded!\n");
08607c1b 3021 btrfs_init_transaction_sys();
2c90e5d6
CM
3022 err = init_inodecache();
3023 if (err)
3024 return err;
2e635a27 3025 return register_filesystem(&btrfs_fs_type);
d6e4a428
CM
3026 destroy_inodecache();
3027 return err;
2e635a27
CM
3028}
3029
3030static void __exit exit_btrfs_fs(void)
3031{
08607c1b 3032 btrfs_exit_transaction_sys();
2c90e5d6 3033 destroy_inodecache();
2e635a27
CM
3034 unregister_filesystem(&btrfs_fs_type);
3035 printk("btrfs unloaded\n");
3036}
3037
3038module_init(init_btrfs_fs)
3039module_exit(exit_btrfs_fs)
3040
3041MODULE_LICENSE("GPL");