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