aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-10-15 16:14:19 -0400
committerChris Mason <chris.mason@oracle.com>2008-09-25 11:03:56 -0400
commit5f39d397dfbe140a14edecd4e73c34ce23c4f9ee (patch)
tree280561e4b68d573590557dd27df656869eed8023 /fs/btrfs/inode.c
parent50b78c24d594e6985f36a9a8d8e2fb74e1664e90 (diff)
Btrfs: Create extent_buffer interface for large blocksizes
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c435
1 files changed, 267 insertions, 168 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index b03d40a907ca..fbe2836364e0 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -159,10 +159,8 @@ out:
159 159
160int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end) 160int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
161{ 161{
162 char csum[BTRFS_CRC32_SIZE];
163 size_t offset = start - (page->index << PAGE_CACHE_SHIFT); 162 size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
164 struct inode *inode = page->mapping->host; 163 struct inode *inode = page->mapping->host;
165 struct btrfs_root *root = BTRFS_I(inode)->root;
166 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 164 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
167 char *kaddr; 165 char *kaddr;
168 u64 private; 166 u64 private;
@@ -173,11 +171,15 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
173 if (ret) { 171 if (ret) {
174 goto zeroit; 172 goto zeroit;
175 } 173 }
174 /*
175 struct btrfs_root *root = BTRFS_I(inode)->root;
176 char csum[BTRFS_CRC32_SIZE];
176 ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum); 177 ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum);
177 BUG_ON(ret); 178 BUG_ON(ret);
178 if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) { 179 if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) {
179 goto zeroit; 180 goto zeroit;
180 } 181 }
182 */
181 kunmap_atomic(kaddr, KM_IRQ0); 183 kunmap_atomic(kaddr, KM_IRQ0);
182 return 0; 184 return 0;
183 185
@@ -192,7 +194,9 @@ zeroit:
192void btrfs_read_locked_inode(struct inode *inode) 194void btrfs_read_locked_inode(struct inode *inode)
193{ 195{
194 struct btrfs_path *path; 196 struct btrfs_path *path;
197 struct extent_buffer *leaf;
195 struct btrfs_inode_item *inode_item; 198 struct btrfs_inode_item *inode_item;
199 struct btrfs_inode_timespec *tspec;
196 struct btrfs_root *root = BTRFS_I(inode)->root; 200 struct btrfs_root *root = BTRFS_I(inode)->root;
197 struct btrfs_key location; 201 struct btrfs_key location;
198 u64 alloc_group_block; 202 u64 alloc_group_block;
@@ -205,29 +209,37 @@ void btrfs_read_locked_inode(struct inode *inode)
205 209
206 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); 210 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
207 ret = btrfs_lookup_inode(NULL, root, path, &location, 0); 211 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
208 if (ret) { 212 if (ret)
209 goto make_bad; 213 goto make_bad;
210 }
211 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
212 path->slots[0],
213 struct btrfs_inode_item);
214 214
215 inode->i_mode = btrfs_inode_mode(inode_item); 215 leaf = path->nodes[0];
216 inode->i_nlink = btrfs_inode_nlink(inode_item); 216 inode_item = btrfs_item_ptr(leaf, path->slots[0],
217 inode->i_uid = btrfs_inode_uid(inode_item); 217 struct btrfs_inode_item);
218 inode->i_gid = btrfs_inode_gid(inode_item); 218
219 inode->i_size = btrfs_inode_size(inode_item); 219 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
220 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); 220 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
221 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); 221 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
222 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); 222 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
223 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); 223 inode->i_size = btrfs_inode_size(leaf, inode_item);
224 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); 224
225 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); 225 tspec = btrfs_inode_atime(inode_item);
226 inode->i_blocks = btrfs_inode_nblocks(inode_item); 226 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
227 inode->i_generation = btrfs_inode_generation(inode_item); 227 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
228
229 tspec = btrfs_inode_mtime(inode_item);
230 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
231 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
232
233 tspec = btrfs_inode_ctime(inode_item);
234 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
235 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
236
237 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
238 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
228 inode->i_rdev = 0; 239 inode->i_rdev = 0;
229 rdev = btrfs_inode_rdev(inode_item); 240 rdev = btrfs_inode_rdev(leaf, inode_item);
230 alloc_group_block = btrfs_inode_block_group(inode_item); 241
242 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
231 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, 243 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
232 alloc_group_block); 244 alloc_group_block);
233 245
@@ -267,24 +279,35 @@ make_bad:
267 make_bad_inode(inode); 279 make_bad_inode(inode);
268} 280}
269 281
270static void fill_inode_item(struct btrfs_inode_item *item, 282static void fill_inode_item(struct extent_buffer *leaf,
283 struct btrfs_inode_item *item,
271 struct inode *inode) 284 struct inode *inode)
272{ 285{
273 btrfs_set_inode_uid(item, inode->i_uid); 286 btrfs_set_inode_uid(leaf, item, inode->i_uid);
274 btrfs_set_inode_gid(item, inode->i_gid); 287 btrfs_set_inode_gid(leaf, item, inode->i_gid);
275 btrfs_set_inode_size(item, inode->i_size); 288 btrfs_set_inode_size(leaf, item, inode->i_size);
276 btrfs_set_inode_mode(item, inode->i_mode); 289 btrfs_set_inode_mode(leaf, item, inode->i_mode);
277 btrfs_set_inode_nlink(item, inode->i_nlink); 290 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
278 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); 291
279 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); 292 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
280 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); 293 inode->i_atime.tv_sec);
281 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); 294 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
282 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); 295 inode->i_atime.tv_nsec);
283 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); 296
284 btrfs_set_inode_nblocks(item, inode->i_blocks); 297 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
285 btrfs_set_inode_generation(item, inode->i_generation); 298 inode->i_mtime.tv_sec);
286 btrfs_set_inode_rdev(item, inode->i_rdev); 299 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
287 btrfs_set_inode_block_group(item, 300 inode->i_mtime.tv_nsec);
301
302 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
303 inode->i_ctime.tv_sec);
304 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
305 inode->i_ctime.tv_nsec);
306
307 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
308 btrfs_set_inode_generation(leaf, item, inode->i_generation);
309 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
310 btrfs_set_inode_block_group(leaf, item,
288 BTRFS_I(inode)->block_group->key.objectid); 311 BTRFS_I(inode)->block_group->key.objectid);
289} 312}
290 313
@@ -294,6 +317,7 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
294{ 317{
295 struct btrfs_inode_item *inode_item; 318 struct btrfs_inode_item *inode_item;
296 struct btrfs_path *path; 319 struct btrfs_path *path;
320 struct extent_buffer *leaf;
297 int ret; 321 int ret;
298 322
299 path = btrfs_alloc_path(); 323 path = btrfs_alloc_path();
@@ -306,12 +330,12 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
306 goto failed; 330 goto failed;
307 } 331 }
308 332
309 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), 333 leaf = path->nodes[0];
310 path->slots[0], 334 inode_item = btrfs_item_ptr(leaf, path->slots[0],
311 struct btrfs_inode_item); 335 struct btrfs_inode_item);
312 336
313 fill_inode_item(inode_item, inode); 337 fill_inode_item(leaf, inode_item, inode);
314 btrfs_mark_buffer_dirty(path->nodes[0]); 338 btrfs_mark_buffer_dirty(leaf);
315 btrfs_set_inode_last_trans(trans, inode); 339 btrfs_set_inode_last_trans(trans, inode);
316 ret = 0; 340 ret = 0;
317failed: 341failed:
@@ -330,8 +354,9 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
330 const char *name = dentry->d_name.name; 354 const char *name = dentry->d_name.name;
331 int name_len = dentry->d_name.len; 355 int name_len = dentry->d_name.len;
332 int ret = 0; 356 int ret = 0;
333 u64 objectid; 357 struct extent_buffer *leaf;
334 struct btrfs_dir_item *di; 358 struct btrfs_dir_item *di;
359 struct btrfs_key key;
335 360
336 path = btrfs_alloc_path(); 361 path = btrfs_alloc_path();
337 if (!path) { 362 if (!path) {
@@ -349,14 +374,15 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
349 ret = -ENOENT; 374 ret = -ENOENT;
350 goto err; 375 goto err;
351 } 376 }
352 objectid = btrfs_disk_key_objectid(&di->location); 377 leaf = path->nodes[0];
378 btrfs_dir_item_key_to_cpu(leaf, di, &key);
353 ret = btrfs_delete_one_dir_name(trans, root, path, di); 379 ret = btrfs_delete_one_dir_name(trans, root, path, di);
354 if (ret) 380 if (ret)
355 goto err; 381 goto err;
356 btrfs_release_path(root, path); 382 btrfs_release_path(root, path);
357 383
358 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 384 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
359 objectid, name, name_len, -1); 385 key.objectid, name, name_len, -1);
360 if (IS_ERR(di)) { 386 if (IS_ERR(di)) {
361 ret = PTR_ERR(di); 387 ret = PTR_ERR(di);
362 goto err; 388 goto err;
@@ -391,12 +417,15 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
391 root = BTRFS_I(dir)->root; 417 root = BTRFS_I(dir)->root;
392 mutex_lock(&root->fs_info->fs_mutex); 418 mutex_lock(&root->fs_info->fs_mutex);
393 trans = btrfs_start_transaction(root, 1); 419 trans = btrfs_start_transaction(root, 1);
420
394 btrfs_set_trans_block_group(trans, dir); 421 btrfs_set_trans_block_group(trans, dir);
395 ret = btrfs_unlink_trans(trans, root, dir, dentry); 422 ret = btrfs_unlink_trans(trans, root, dir, dentry);
396 nr = trans->blocks_used; 423 nr = trans->blocks_used;
424
397 btrfs_end_transaction(trans, root); 425 btrfs_end_transaction(trans, root);
398 mutex_unlock(&root->fs_info->fs_mutex); 426 mutex_unlock(&root->fs_info->fs_mutex);
399 btrfs_btree_balance_dirty(root, nr); 427 btrfs_btree_balance_dirty(root, nr);
428
400 return ret; 429 return ret;
401} 430}
402 431
@@ -411,7 +440,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
411 struct btrfs_trans_handle *trans; 440 struct btrfs_trans_handle *trans;
412 struct btrfs_key found_key; 441 struct btrfs_key found_key;
413 int found_type; 442 int found_type;
414 struct btrfs_leaf *leaf; 443 struct extent_buffer *leaf;
415 char *goodnames = ".."; 444 char *goodnames = "..";
416 unsigned long nr; 445 unsigned long nr;
417 446
@@ -419,10 +448,11 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
419 BUG_ON(!path); 448 BUG_ON(!path);
420 mutex_lock(&root->fs_info->fs_mutex); 449 mutex_lock(&root->fs_info->fs_mutex);
421 trans = btrfs_start_transaction(root, 1); 450 trans = btrfs_start_transaction(root, 1);
451
422 btrfs_set_trans_block_group(trans, dir); 452 btrfs_set_trans_block_group(trans, dir);
423 key.objectid = inode->i_ino; 453 key.objectid = inode->i_ino;
424 key.offset = (u64)-1; 454 key.offset = (u64)-1;
425 key.flags = (u32)-1; 455 key.type = (u8)-1;
426 while(1) { 456 while(1) {
427 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 457 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
428 if (ret < 0) { 458 if (ret < 0) {
@@ -435,9 +465,8 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
435 goto out; 465 goto out;
436 } 466 }
437 path->slots[0]--; 467 path->slots[0]--;
438 leaf = btrfs_buffer_leaf(path->nodes[0]); 468 leaf = path->nodes[0];
439 btrfs_disk_key_to_cpu(&found_key, 469 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
440 &leaf->items[path->slots[0]].key);
441 found_type = btrfs_key_type(&found_key); 470 found_type = btrfs_key_type(&found_key);
442 if (found_key.objectid != inode->i_ino) { 471 if (found_key.objectid != inode->i_ino) {
443 err = -ENOENT; 472 err = -ENOENT;
@@ -513,9 +542,9 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
513 int ret; 542 int ret;
514 struct btrfs_path *path; 543 struct btrfs_path *path;
515 struct btrfs_key key; 544 struct btrfs_key key;
516 struct btrfs_disk_key *found_key; 545 struct btrfs_key found_key;
517 u32 found_type; 546 u32 found_type;
518 struct btrfs_leaf *leaf; 547 struct extent_buffer *leaf;
519 struct btrfs_file_extent_item *fi; 548 struct btrfs_file_extent_item *fi;
520 u64 extent_start = 0; 549 u64 extent_start = 0;
521 u64 extent_num_blocks = 0; 550 u64 extent_num_blocks = 0;
@@ -527,10 +556,12 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
527 path = btrfs_alloc_path(); 556 path = btrfs_alloc_path();
528 path->reada = -1; 557 path->reada = -1;
529 BUG_ON(!path); 558 BUG_ON(!path);
559
530 /* FIXME, add redo link to tree so we don't leak on crash */ 560 /* FIXME, add redo link to tree so we don't leak on crash */
531 key.objectid = inode->i_ino; 561 key.objectid = inode->i_ino;
532 key.offset = (u64)-1; 562 key.offset = (u64)-1;
533 key.flags = (u32)-1; 563 key.type = (u8)-1;
564
534 while(1) { 565 while(1) {
535 btrfs_init_path(path); 566 btrfs_init_path(path);
536 fi = NULL; 567 fi = NULL;
@@ -542,27 +573,28 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
542 BUG_ON(path->slots[0] == 0); 573 BUG_ON(path->slots[0] == 0);
543 path->slots[0]--; 574 path->slots[0]--;
544 } 575 }
545 leaf = btrfs_buffer_leaf(path->nodes[0]); 576 leaf = path->nodes[0];
546 found_key = &leaf->items[path->slots[0]].key; 577 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
547 found_type = btrfs_disk_key_type(found_key); 578 found_type = btrfs_key_type(&found_key);
548 579
549 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) 580 if (found_key.objectid != inode->i_ino)
550 break; 581 break;
582
551 if (found_type != BTRFS_CSUM_ITEM_KEY && 583 if (found_type != BTRFS_CSUM_ITEM_KEY &&
552 found_type != BTRFS_DIR_ITEM_KEY && 584 found_type != BTRFS_DIR_ITEM_KEY &&
553 found_type != BTRFS_DIR_INDEX_KEY && 585 found_type != BTRFS_DIR_INDEX_KEY &&
554 found_type != BTRFS_EXTENT_DATA_KEY) 586 found_type != BTRFS_EXTENT_DATA_KEY)
555 break; 587 break;
556 588
557 item_end = btrfs_disk_key_offset(found_key); 589 item_end = found_key.offset;
558 if (found_type == BTRFS_EXTENT_DATA_KEY) { 590 if (found_type == BTRFS_EXTENT_DATA_KEY) {
559 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), 591 fi = btrfs_item_ptr(leaf, path->slots[0],
560 path->slots[0],
561 struct btrfs_file_extent_item); 592 struct btrfs_file_extent_item);
562 if (btrfs_file_extent_type(fi) != 593 if (btrfs_file_extent_type(leaf, fi) !=
563 BTRFS_FILE_EXTENT_INLINE) { 594 BTRFS_FILE_EXTENT_INLINE) {
564 item_end += btrfs_file_extent_num_blocks(fi) << 595 item_end +=
565 inode->i_blkbits; 596 btrfs_file_extent_num_blocks(leaf, fi) <<
597 inode->i_blkbits;
566 } 598 }
567 } 599 }
568 if (found_type == BTRFS_CSUM_ITEM_KEY) { 600 if (found_type == BTRFS_CSUM_ITEM_KEY) {
@@ -583,7 +615,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
583 btrfs_set_key_type(&key, found_type); 615 btrfs_set_key_type(&key, found_type);
584 continue; 616 continue;
585 } 617 }
586 if (btrfs_disk_key_offset(found_key) >= inode->i_size) 618 if (found_key.offset >= inode->i_size)
587 del_item = 1; 619 del_item = 1;
588 else 620 else
589 del_item = 0; 621 del_item = 0;
@@ -591,30 +623,31 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
591 623
592 /* FIXME, shrink the extent if the ref count is only 1 */ 624 /* FIXME, shrink the extent if the ref count is only 1 */
593 if (found_type == BTRFS_EXTENT_DATA_KEY && 625 if (found_type == BTRFS_EXTENT_DATA_KEY &&
594 btrfs_file_extent_type(fi) != 626 btrfs_file_extent_type(leaf, fi) !=
595 BTRFS_FILE_EXTENT_INLINE) { 627 BTRFS_FILE_EXTENT_INLINE) {
596 u64 num_dec; 628 u64 num_dec;
597 extent_start = btrfs_file_extent_disk_blocknr(fi); 629 extent_start = btrfs_file_extent_disk_blocknr(leaf, fi);
598 if (!del_item) { 630 if (!del_item) {
599 u64 orig_num_blocks = 631 u64 orig_num_blocks =
600 btrfs_file_extent_num_blocks(fi); 632 btrfs_file_extent_num_blocks(leaf, fi);
601 extent_num_blocks = inode->i_size - 633 extent_num_blocks = inode->i_size -
602 btrfs_disk_key_offset(found_key) + 634 found_key.offset + root->sectorsize - 1;
603 root->blocksize - 1;
604 extent_num_blocks >>= inode->i_blkbits; 635 extent_num_blocks >>= inode->i_blkbits;
605 btrfs_set_file_extent_num_blocks(fi, 636 btrfs_set_file_extent_num_blocks(leaf, fi,
606 extent_num_blocks); 637 extent_num_blocks);
607 num_dec = (orig_num_blocks - 638 num_dec = (orig_num_blocks -
608 extent_num_blocks) << 3; 639 extent_num_blocks) << 3;
609 if (extent_start != 0) { 640 if (extent_start != 0) {
610 inode->i_blocks -= num_dec; 641 inode->i_blocks -= num_dec;
611 } 642 }
612 btrfs_mark_buffer_dirty(path->nodes[0]); 643 btrfs_mark_buffer_dirty(leaf);
613 } else { 644 } else {
614 extent_num_blocks = 645 extent_num_blocks =
615 btrfs_file_extent_disk_num_blocks(fi); 646 btrfs_file_extent_disk_num_blocks(leaf,
647 fi);
616 /* FIXME blocksize != 4096 */ 648 /* FIXME blocksize != 4096 */
617 num_dec = btrfs_file_extent_num_blocks(fi) << 3; 649 num_dec = btrfs_file_extent_num_blocks(leaf,
650 fi) << 3;
618 if (extent_start != 0) { 651 if (extent_start != 0) {
619 found_extent = 1; 652 found_extent = 1;
620 inode->i_blocks -= num_dec; 653 inode->i_blocks -= num_dec;
@@ -725,7 +758,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
725 struct btrfs_root *root = BTRFS_I(inode)->root; 758 struct btrfs_root *root = BTRFS_I(inode)->root;
726 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 759 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
727 760
728 u64 mask = root->blocksize - 1; 761 u64 mask = root->sectorsize - 1;
729 u64 pos = (inode->i_size + mask) & ~mask; 762 u64 pos = (inode->i_size + mask) & ~mask;
730 u64 block_end = attr->ia_size | mask; 763 u64 block_end = attr->ia_size | mask;
731 u64 hole_size; 764 u64 hole_size;
@@ -771,9 +804,11 @@ void btrfs_delete_inode(struct inode *inode)
771 if (is_bad_inode(inode)) { 804 if (is_bad_inode(inode)) {
772 goto no_delete; 805 goto no_delete;
773 } 806 }
807
774 inode->i_size = 0; 808 inode->i_size = 0;
775 mutex_lock(&root->fs_info->fs_mutex); 809 mutex_lock(&root->fs_info->fs_mutex);
776 trans = btrfs_start_transaction(root, 1); 810 trans = btrfs_start_transaction(root, 1);
811
777 btrfs_set_trans_block_group(trans, inode); 812 btrfs_set_trans_block_group(trans, inode);
778 ret = btrfs_truncate_in_trans(trans, root, inode); 813 ret = btrfs_truncate_in_trans(trans, root, inode);
779 if (ret) 814 if (ret)
@@ -782,6 +817,7 @@ void btrfs_delete_inode(struct inode *inode)
782 if (ret) 817 if (ret)
783 goto no_delete_lock; 818 goto no_delete_lock;
784 nr = trans->blocks_used; 819 nr = trans->blocks_used;
820
785 btrfs_end_transaction(trans, root); 821 btrfs_end_transaction(trans, root);
786 mutex_unlock(&root->fs_info->fs_mutex); 822 mutex_unlock(&root->fs_info->fs_mutex);
787 btrfs_btree_balance_dirty(root, nr); 823 btrfs_btree_balance_dirty(root, nr);
@@ -819,7 +855,7 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
819 ret = 0; 855 ret = 0;
820 goto out; 856 goto out;
821 } 857 }
822 btrfs_disk_key_to_cpu(location, &di->location); 858 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
823out: 859out:
824 btrfs_release_path(root, path); 860 btrfs_release_path(root, path);
825 btrfs_free_path(path); 861 btrfs_free_path(path);
@@ -856,7 +892,6 @@ static int fixup_tree_root_location(struct btrfs_root *root,
856 892
857 ri = &(*sub_root)->root_item; 893 ri = &(*sub_root)->root_item;
858 location->objectid = btrfs_root_dirid(ri); 894 location->objectid = btrfs_root_dirid(ri);
859 location->flags = 0;
860 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); 895 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
861 location->offset = 0; 896 location->offset = 0;
862 897
@@ -908,11 +943,14 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
908 943
909 if (dentry->d_name.len > BTRFS_NAME_LEN) 944 if (dentry->d_name.len > BTRFS_NAME_LEN)
910 return ERR_PTR(-ENAMETOOLONG); 945 return ERR_PTR(-ENAMETOOLONG);
946
911 mutex_lock(&root->fs_info->fs_mutex); 947 mutex_lock(&root->fs_info->fs_mutex);
912 ret = btrfs_inode_by_name(dir, dentry, &location); 948 ret = btrfs_inode_by_name(dir, dentry, &location);
913 mutex_unlock(&root->fs_info->fs_mutex); 949 mutex_unlock(&root->fs_info->fs_mutex);
950
914 if (ret < 0) 951 if (ret < 0)
915 return ERR_PTR(ret); 952 return ERR_PTR(ret);
953
916 inode = NULL; 954 inode = NULL;
917 if (location.objectid) { 955 if (location.objectid) {
918 ret = fixup_tree_root_location(root, &location, &sub_root, 956 ret = fixup_tree_root_location(root, &location, &sub_root,
@@ -952,10 +990,11 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
952 struct btrfs_item *item; 990 struct btrfs_item *item;
953 struct btrfs_dir_item *di; 991 struct btrfs_dir_item *di;
954 struct btrfs_key key; 992 struct btrfs_key key;
993 struct btrfs_key found_key;
955 struct btrfs_path *path; 994 struct btrfs_path *path;
956 int ret; 995 int ret;
957 u32 nritems; 996 u32 nritems;
958 struct btrfs_leaf *leaf; 997 struct extent_buffer *leaf;
959 int slot; 998 int slot;
960 int advance; 999 int advance;
961 unsigned char d_type; 1000 unsigned char d_type;
@@ -964,15 +1003,19 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
964 u32 di_total; 1003 u32 di_total;
965 u32 di_len; 1004 u32 di_len;
966 int key_type = BTRFS_DIR_INDEX_KEY; 1005 int key_type = BTRFS_DIR_INDEX_KEY;
1006 char tmp_name[32];
1007 char *name_ptr;
1008 int name_len;
967 1009
968 /* FIXME, use a real flag for deciding about the key type */ 1010 /* FIXME, use a real flag for deciding about the key type */
969 if (root->fs_info->tree_root == root) 1011 if (root->fs_info->tree_root == root)
970 key_type = BTRFS_DIR_ITEM_KEY; 1012 key_type = BTRFS_DIR_ITEM_KEY;
1013
971 mutex_lock(&root->fs_info->fs_mutex); 1014 mutex_lock(&root->fs_info->fs_mutex);
972 key.objectid = inode->i_ino; 1015 key.objectid = inode->i_ino;
973 key.flags = 0;
974 btrfs_set_key_type(&key, key_type); 1016 btrfs_set_key_type(&key, key_type);
975 key.offset = filp->f_pos; 1017 key.offset = filp->f_pos;
1018
976 path = btrfs_alloc_path(); 1019 path = btrfs_alloc_path();
977 path->reada = 2; 1020 path->reada = 2;
978 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1021 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
@@ -980,16 +1023,16 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
980 goto err; 1023 goto err;
981 advance = 0; 1024 advance = 0;
982 while(1) { 1025 while(1) {
983 leaf = btrfs_buffer_leaf(path->nodes[0]); 1026 leaf = path->nodes[0];
984 nritems = btrfs_header_nritems(&leaf->header); 1027 nritems = btrfs_header_nritems(leaf);
985 slot = path->slots[0]; 1028 slot = path->slots[0];
986 if (advance || slot >= nritems) { 1029 if (advance || slot >= nritems) {
987 if (slot >= nritems -1) { 1030 if (slot >= nritems -1) {
988 ret = btrfs_next_leaf(root, path); 1031 ret = btrfs_next_leaf(root, path);
989 if (ret) 1032 if (ret)
990 break; 1033 break;
991 leaf = btrfs_buffer_leaf(path->nodes[0]); 1034 leaf = path->nodes[0];
992 nritems = btrfs_header_nritems(&leaf->header); 1035 nritems = btrfs_header_nritems(leaf);
993 slot = path->slots[0]; 1036 slot = path->slots[0];
994 } else { 1037 } else {
995 slot++; 1038 slot++;
@@ -997,28 +1040,48 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
997 } 1040 }
998 } 1041 }
999 advance = 1; 1042 advance = 1;
1000 item = leaf->items + slot; 1043 item = btrfs_item_nr(leaf, slot);
1001 if (btrfs_disk_key_objectid(&item->key) != key.objectid) 1044 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1045
1046 if (found_key.objectid != key.objectid)
1002 break; 1047 break;
1003 if (btrfs_disk_key_type(&item->key) != key_type) 1048 if (btrfs_key_type(&found_key) != key_type)
1004 break; 1049 break;
1005 if (btrfs_disk_key_offset(&item->key) < filp->f_pos) 1050 if (found_key.offset < filp->f_pos)
1006 continue; 1051 continue;
1007 filp->f_pos = btrfs_disk_key_offset(&item->key); 1052
1053 filp->f_pos = found_key.offset;
1008 advance = 1; 1054 advance = 1;
1009 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 1055 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1010 di_cur = 0; 1056 di_cur = 0;
1011 di_total = btrfs_item_size(leaf->items + slot); 1057 di_total = btrfs_item_size(leaf, item);
1012 while(di_cur < di_total) { 1058 while(di_cur < di_total) {
1013 d_type = btrfs_filetype_table[btrfs_dir_type(di)]; 1059 struct btrfs_key location;
1014 over = filldir(dirent, (const char *)(di + 1), 1060
1015 btrfs_dir_name_len(di), 1061 name_len = btrfs_dir_name_len(leaf, di);
1016 btrfs_disk_key_offset(&item->key), 1062 if (name_len < 32) {
1017 btrfs_disk_key_objectid(&di->location), 1063 name_ptr = tmp_name;
1064 } else {
1065 name_ptr = kmalloc(name_len, GFP_NOFS);
1066 BUG_ON(!name_ptr);
1067 }
1068 read_extent_buffer(leaf, name_ptr,
1069 (unsigned long)(di + 1), name_len);
1070
1071 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1072 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1073
1074 over = filldir(dirent, name_ptr, name_len,
1075 found_key.offset,
1076 location.objectid,
1018 d_type); 1077 d_type);
1078
1079 if (name_ptr != tmp_name)
1080 kfree(name_ptr);
1081
1019 if (over) 1082 if (over)
1020 goto nopos; 1083 goto nopos;
1021 di_len = btrfs_dir_name_len(di) + sizeof(*di); 1084 di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
1022 di_cur += di_len; 1085 di_cur += di_len;
1023 di = (struct btrfs_dir_item *)((char *)di + di_len); 1086 di = (struct btrfs_dir_item *)((char *)di + di_len);
1024 } 1087 }
@@ -1075,11 +1138,15 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1075 int mode) 1138 int mode)
1076{ 1139{
1077 struct inode *inode; 1140 struct inode *inode;
1078 struct btrfs_inode_item inode_item; 1141 struct btrfs_inode_item *inode_item;
1079 struct btrfs_key *location; 1142 struct btrfs_key *location;
1143 struct btrfs_path *path;
1080 int ret; 1144 int ret;
1081 int owner; 1145 int owner;
1082 1146
1147 path = btrfs_alloc_path();
1148 BUG_ON(!path);
1149
1083 inode = new_inode(root->fs_info->sb); 1150 inode = new_inode(root->fs_info->sb);
1084 if (!inode) 1151 if (!inode)
1085 return ERR_PTR(-ENOMEM); 1152 return ERR_PTR(-ENOMEM);
@@ -1095,24 +1162,32 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1095 group = btrfs_find_block_group(root, group, 0, 0, owner); 1162 group = btrfs_find_block_group(root, group, 0, 0, owner);
1096 BTRFS_I(inode)->block_group = group; 1163 BTRFS_I(inode)->block_group = group;
1097 1164
1165 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1166 if (ret)
1167 goto fail;
1168
1098 inode->i_uid = current->fsuid; 1169 inode->i_uid = current->fsuid;
1099 inode->i_gid = current->fsgid; 1170 inode->i_gid = current->fsgid;
1100 inode->i_mode = mode; 1171 inode->i_mode = mode;
1101 inode->i_ino = objectid; 1172 inode->i_ino = objectid;
1102 inode->i_blocks = 0; 1173 inode->i_blocks = 0;
1103 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 1174 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1104 fill_inode_item(&inode_item, inode); 1175 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1176 struct btrfs_inode_item);
1177 fill_inode_item(path->nodes[0], inode_item, inode);
1178 btrfs_mark_buffer_dirty(path->nodes[0]);
1179 btrfs_free_path(path);
1180
1105 location = &BTRFS_I(inode)->location; 1181 location = &BTRFS_I(inode)->location;
1106 location->objectid = objectid; 1182 location->objectid = objectid;
1107 location->flags = 0;
1108 location->offset = 0; 1183 location->offset = 0;
1109 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); 1184 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1110 1185
1111 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1112 if (ret)
1113 return ERR_PTR(ret);
1114 insert_inode_hash(inode); 1186 insert_inode_hash(inode);
1115 return inode; 1187 return inode;
1188fail:
1189 btrfs_free_path(path);
1190 return ERR_PTR(ret);
1116} 1191}
1117 1192
1118static inline u8 btrfs_inode_type(struct inode *inode) 1193static inline u8 btrfs_inode_type(struct inode *inode)
@@ -1127,8 +1202,8 @@ static int btrfs_add_link(struct btrfs_trans_handle *trans,
1127 struct btrfs_key key; 1202 struct btrfs_key key;
1128 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; 1203 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1129 struct inode *parent_inode; 1204 struct inode *parent_inode;
1205
1130 key.objectid = inode->i_ino; 1206 key.objectid = inode->i_ino;
1131 key.flags = 0;
1132 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 1207 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1133 key.offset = 0; 1208 key.offset = 0;
1134 1209
@@ -1285,14 +1360,18 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1285 inc_nlink(inode); 1360 inc_nlink(inode);
1286 mutex_lock(&root->fs_info->fs_mutex); 1361 mutex_lock(&root->fs_info->fs_mutex);
1287 trans = btrfs_start_transaction(root, 1); 1362 trans = btrfs_start_transaction(root, 1);
1363
1288 btrfs_set_trans_block_group(trans, dir); 1364 btrfs_set_trans_block_group(trans, dir);
1289 atomic_inc(&inode->i_count); 1365 atomic_inc(&inode->i_count);
1290 err = btrfs_add_nondir(trans, dentry, inode); 1366 err = btrfs_add_nondir(trans, dentry, inode);
1367
1291 if (err) 1368 if (err)
1292 drop_inode = 1; 1369 drop_inode = 1;
1370
1293 dir->i_sb->s_dirt = 1; 1371 dir->i_sb->s_dirt = 1;
1294 btrfs_update_inode_block_group(trans, dir); 1372 btrfs_update_inode_block_group(trans, dir);
1295 err = btrfs_update_inode(trans, root, inode); 1373 err = btrfs_update_inode(trans, root, inode);
1374
1296 if (err) 1375 if (err)
1297 drop_inode = 1; 1376 drop_inode = 1;
1298 1377
@@ -1321,13 +1400,13 @@ static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1321 1400
1322 key.objectid = objectid; 1401 key.objectid = objectid;
1323 key.offset = 0; 1402 key.offset = 0;
1324 key.flags = 0;
1325 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 1403 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1326 1404
1327 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, 1405 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1328 &key, BTRFS_FT_DIR); 1406 &key, BTRFS_FT_DIR);
1329 if (ret) 1407 if (ret)
1330 goto error; 1408 goto error;
1409
1331 key.objectid = dirid; 1410 key.objectid = dirid;
1332 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, 1411 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1333 &key, BTRFS_FT_DIR); 1412 &key, BTRFS_FT_DIR);
@@ -1350,6 +1429,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1350 mutex_lock(&root->fs_info->fs_mutex); 1429 mutex_lock(&root->fs_info->fs_mutex);
1351 trans = btrfs_start_transaction(root, 1); 1430 trans = btrfs_start_transaction(root, 1);
1352 btrfs_set_trans_block_group(trans, dir); 1431 btrfs_set_trans_block_group(trans, dir);
1432
1353 if (IS_ERR(trans)) { 1433 if (IS_ERR(trans)) {
1354 err = PTR_ERR(trans); 1434 err = PTR_ERR(trans);
1355 goto out_unlock; 1435 goto out_unlock;
@@ -1367,6 +1447,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1367 err = PTR_ERR(inode); 1447 err = PTR_ERR(inode);
1368 goto out_fail; 1448 goto out_fail;
1369 } 1449 }
1450
1370 drop_on_err = 1; 1451 drop_on_err = 1;
1371 inode->i_op = &btrfs_dir_inode_operations; 1452 inode->i_op = &btrfs_dir_inode_operations;
1372 inode->i_fop = &btrfs_dir_file_operations; 1453 inode->i_fop = &btrfs_dir_file_operations;
@@ -1380,9 +1461,11 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1380 err = btrfs_update_inode(trans, root, inode); 1461 err = btrfs_update_inode(trans, root, inode);
1381 if (err) 1462 if (err)
1382 goto out_fail; 1463 goto out_fail;
1464
1383 err = btrfs_add_link(trans, dentry, inode); 1465 err = btrfs_add_link(trans, dentry, inode);
1384 if (err) 1466 if (err)
1385 goto out_fail; 1467 goto out_fail;
1468
1386 d_instantiate(dentry, inode); 1469 d_instantiate(dentry, inode);
1387 drop_on_err = 0; 1470 drop_on_err = 0;
1388 dir->i_sb->s_dirt = 1; 1471 dir->i_sb->s_dirt = 1;
@@ -1392,6 +1475,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1392out_fail: 1475out_fail:
1393 nr = trans->blocks_used; 1476 nr = trans->blocks_used;
1394 btrfs_end_transaction(trans, root); 1477 btrfs_end_transaction(trans, root);
1478
1395out_unlock: 1479out_unlock:
1396 mutex_unlock(&root->fs_info->fs_mutex); 1480 mutex_unlock(&root->fs_info->fs_mutex);
1397 if (drop_on_err) 1481 if (drop_on_err)
@@ -1415,8 +1499,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1415 struct btrfs_path *path; 1499 struct btrfs_path *path;
1416 struct btrfs_root *root = BTRFS_I(inode)->root; 1500 struct btrfs_root *root = BTRFS_I(inode)->root;
1417 struct btrfs_file_extent_item *item; 1501 struct btrfs_file_extent_item *item;
1418 struct btrfs_leaf *leaf; 1502 struct extent_buffer *leaf;
1419 struct btrfs_disk_key *found_key; 1503 struct btrfs_key found_key;
1420 struct extent_map *em = NULL; 1504 struct extent_map *em = NULL;
1421 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1505 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1422 struct btrfs_trans_handle *trans = NULL; 1506 struct btrfs_trans_handle *trans = NULL;
@@ -1436,8 +1520,8 @@ again:
1436 err = -ENOMEM; 1520 err = -ENOMEM;
1437 goto out; 1521 goto out;
1438 } 1522 }
1439 em->start = 0; 1523 em->start = EXTENT_MAP_HOLE;
1440 em->end = 0; 1524 em->end = EXTENT_MAP_HOLE;
1441 } 1525 }
1442 em->bdev = inode->i_sb->s_bdev; 1526 em->bdev = inode->i_sb->s_bdev;
1443 ret = btrfs_lookup_file_extent(NULL, root, path, 1527 ret = btrfs_lookup_file_extent(NULL, root, path,
@@ -1453,25 +1537,27 @@ again:
1453 path->slots[0]--; 1537 path->slots[0]--;
1454 } 1538 }
1455 1539
1456 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 1540 leaf = path->nodes[0];
1541 item = btrfs_item_ptr(leaf, path->slots[0],
1457 struct btrfs_file_extent_item); 1542 struct btrfs_file_extent_item);
1458 leaf = btrfs_buffer_leaf(path->nodes[0]); 1543
1459 blocknr = btrfs_file_extent_disk_blocknr(item); 1544 blocknr = btrfs_file_extent_disk_blocknr(leaf, item);
1460 blocknr += btrfs_file_extent_offset(item); 1545 blocknr += btrfs_file_extent_offset(leaf, item);
1461 1546
1462 /* are we inside the extent that was found? */ 1547 /* are we inside the extent that was found? */
1463 found_key = &leaf->items[path->slots[0]].key; 1548 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1464 found_type = btrfs_disk_key_type(found_key); 1549 found_type = btrfs_key_type(&found_key);
1465 if (btrfs_disk_key_objectid(found_key) != objectid || 1550 if (found_key.objectid != objectid ||
1466 found_type != BTRFS_EXTENT_DATA_KEY) { 1551 found_type != BTRFS_EXTENT_DATA_KEY) {
1467 goto not_found; 1552 goto not_found;
1468 } 1553 }
1469 1554
1470 found_type = btrfs_file_extent_type(item); 1555 found_type = btrfs_file_extent_type(leaf, item);
1471 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); 1556 extent_start = found_key.offset;
1472 if (found_type == BTRFS_FILE_EXTENT_REG) { 1557 if (found_type == BTRFS_FILE_EXTENT_REG) {
1473 extent_end = extent_start + 1558 extent_end = extent_start +
1474 (btrfs_file_extent_num_blocks(item) << inode->i_blkbits); 1559 (btrfs_file_extent_num_blocks(leaf, item) <<
1560 inode->i_blkbits);
1475 err = 0; 1561 err = 0;
1476 if (start < extent_start || start >= extent_end) { 1562 if (start < extent_start || start >= extent_end) {
1477 em->start = start; 1563 em->start = start;
@@ -1484,28 +1570,29 @@ again:
1484 } 1570 }
1485 goto not_found_em; 1571 goto not_found_em;
1486 } 1572 }
1487 if (btrfs_file_extent_disk_blocknr(item) == 0) { 1573 if (btrfs_file_extent_disk_blocknr(leaf, item) == 0) {
1488 em->start = extent_start; 1574 em->start = extent_start;
1489 em->end = extent_end - 1; 1575 em->end = extent_end - 1;
1490 em->block_start = 0; 1576 em->block_start = EXTENT_MAP_HOLE;
1491 em->block_end = 0; 1577 em->block_end = EXTENT_MAP_HOLE;
1492 goto insert; 1578 goto insert;
1493 } 1579 }
1494 em->block_start = blocknr << inode->i_blkbits; 1580 em->block_start = blocknr << inode->i_blkbits;
1495 em->block_end = em->block_start + 1581 em->block_end = em->block_start +
1496 (btrfs_file_extent_num_blocks(item) << 1582 (btrfs_file_extent_num_blocks(leaf, item) <<
1497 inode->i_blkbits) - 1; 1583 inode->i_blkbits) - 1;
1498 em->start = extent_start; 1584 em->start = extent_start;
1499 em->end = extent_end - 1; 1585 em->end = extent_end - 1;
1500 goto insert; 1586 goto insert;
1501 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { 1587 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1502 char *ptr; 1588 unsigned long ptr;
1503 char *map; 1589 char *map;
1504 u32 size; 1590 u32 size;
1505 1591
1506 size = btrfs_file_extent_inline_len(leaf->items + 1592 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1507 path->slots[0]); 1593 path->slots[0]));
1508 extent_end = extent_start | ((u64)root->blocksize - 1); 1594
1595 extent_end = extent_start | ((u64)root->sectorsize - 1);
1509 if (start < extent_start || start >= extent_end) { 1596 if (start < extent_start || start >= extent_end) {
1510 em->start = start; 1597 em->start = start;
1511 if (start < extent_start) { 1598 if (start < extent_start) {
@@ -1517,18 +1604,21 @@ again:
1517 } 1604 }
1518 goto not_found_em; 1605 goto not_found_em;
1519 } 1606 }
1607
1520 em->block_start = EXTENT_MAP_INLINE; 1608 em->block_start = EXTENT_MAP_INLINE;
1521 em->block_end = EXTENT_MAP_INLINE; 1609 em->block_end = EXTENT_MAP_INLINE;
1522 em->start = extent_start; 1610 em->start = extent_start;
1523 em->end = extent_end; 1611 em->end = extent_end;
1612
1524 if (!page) { 1613 if (!page) {
1525 goto insert; 1614 goto insert;
1526 } 1615 }
1616
1527 ptr = btrfs_file_extent_inline_start(item); 1617 ptr = btrfs_file_extent_inline_start(item);
1528 map = kmap(page); 1618 map = kmap(page);
1529 memcpy(map + page_offset, ptr, size); 1619 read_extent_buffer(leaf, map + page_offset, ptr, size);
1530 memset(map + page_offset + size, 0, 1620 memset(map + page_offset + size, 0,
1531 root->blocksize - (page_offset + size)); 1621 root->sectorsize - (page_offset + size));
1532 flush_dcache_page(page); 1622 flush_dcache_page(page);
1533 kunmap(page); 1623 kunmap(page);
1534 set_extent_uptodate(em_tree, extent_start, 1624 set_extent_uptodate(em_tree, extent_start,
@@ -1542,8 +1632,8 @@ not_found:
1542 em->start = start; 1632 em->start = start;
1543 em->end = end; 1633 em->end = end;
1544not_found_em: 1634not_found_em:
1545 em->block_start = 0; 1635 em->block_start = EXTENT_MAP_HOLE;
1546 em->block_end = 0; 1636 em->block_end = EXTENT_MAP_HOLE;
1547insert: 1637insert:
1548 btrfs_release_path(root, path); 1638 btrfs_release_path(root, path);
1549 if (em->start > start || em->end < start) { 1639 if (em->start > start || em->end < start) {
@@ -1712,6 +1802,7 @@ static void btrfs_truncate(struct inode *inode)
1712 ret = btrfs_truncate_in_trans(trans, root, inode); 1802 ret = btrfs_truncate_in_trans(trans, root, inode);
1713 btrfs_update_inode(trans, root, inode); 1803 btrfs_update_inode(trans, root, inode);
1714 nr = trans->blocks_used; 1804 nr = trans->blocks_used;
1805
1715 ret = btrfs_end_transaction(trans, root); 1806 ret = btrfs_end_transaction(trans, root);
1716 BUG_ON(ret); 1807 BUG_ON(ret);
1717 mutex_unlock(&root->fs_info->fs_mutex); 1808 mutex_unlock(&root->fs_info->fs_mutex);
@@ -1731,8 +1822,7 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1731 struct btrfs_key key; 1822 struct btrfs_key key;
1732 struct btrfs_root_item root_item; 1823 struct btrfs_root_item root_item;
1733 struct btrfs_inode_item *inode_item; 1824 struct btrfs_inode_item *inode_item;
1734 struct buffer_head *subvol; 1825 struct extent_buffer *leaf;
1735 struct btrfs_leaf *leaf;
1736 struct btrfs_root *new_root; 1826 struct btrfs_root *new_root;
1737 struct inode *inode; 1827 struct inode *inode;
1738 struct inode *dir; 1828 struct inode *dir;
@@ -1746,34 +1836,37 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1746 trans = btrfs_start_transaction(root, 1); 1836 trans = btrfs_start_transaction(root, 1);
1747 BUG_ON(!trans); 1837 BUG_ON(!trans);
1748 1838
1749 subvol = btrfs_alloc_free_block(trans, root, 0, 0); 1839 leaf = btrfs_alloc_free_block(trans, root, 0, 0);
1750 if (IS_ERR(subvol)) 1840 if (IS_ERR(leaf))
1751 return PTR_ERR(subvol); 1841 return PTR_ERR(leaf);
1752 leaf = btrfs_buffer_leaf(subvol); 1842
1753 btrfs_set_header_nritems(&leaf->header, 0); 1843 btrfs_set_header_nritems(leaf, 0);
1754 btrfs_set_header_level(&leaf->header, 0); 1844 btrfs_set_header_level(leaf, 0);
1755 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); 1845 btrfs_set_header_blocknr(leaf, extent_buffer_blocknr(leaf));
1756 btrfs_set_header_generation(&leaf->header, trans->transid); 1846 btrfs_set_header_generation(leaf, trans->transid);
1757 btrfs_set_header_owner(&leaf->header, root->root_key.objectid); 1847 btrfs_set_header_owner(leaf, root->root_key.objectid);
1758 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, 1848 write_extent_buffer(leaf, root->fs_info->fsid,
1759 sizeof(leaf->header.fsid)); 1849 (unsigned long)btrfs_header_fsid(leaf),
1760 btrfs_mark_buffer_dirty(subvol); 1850 BTRFS_FSID_SIZE);
1851 btrfs_mark_buffer_dirty(leaf);
1761 1852
1762 inode_item = &root_item.inode; 1853 inode_item = &root_item.inode;
1763 memset(inode_item, 0, sizeof(*inode_item)); 1854 memset(inode_item, 0, sizeof(*inode_item));
1764 btrfs_set_inode_generation(inode_item, 1); 1855 inode_item->generation = cpu_to_le64(1);
1765 btrfs_set_inode_size(inode_item, 3); 1856 inode_item->size = cpu_to_le64(3);
1766 btrfs_set_inode_nlink(inode_item, 1); 1857 inode_item->nlink = cpu_to_le32(1);
1767 btrfs_set_inode_nblocks(inode_item, 1); 1858 inode_item->nblocks = cpu_to_le64(1);
1768 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); 1859 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
1769 1860
1770 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); 1861 btrfs_set_root_blocknr(&root_item, extent_buffer_blocknr(leaf));
1771 btrfs_set_root_refs(&root_item, 1); 1862 btrfs_set_root_refs(&root_item, 1);
1772 btrfs_set_root_blocks_used(&root_item, 0); 1863 btrfs_set_root_used(&root_item, 0);
1864
1773 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); 1865 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1774 root_item.drop_level = 0; 1866 root_item.drop_level = 0;
1775 brelse(subvol); 1867
1776 subvol = NULL; 1868 free_extent_buffer(leaf);
1869 leaf = NULL;
1777 1870
1778 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, 1871 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1779 0, &objectid); 1872 0, &objectid);
@@ -1784,7 +1877,6 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1784 1877
1785 key.objectid = objectid; 1878 key.objectid = objectid;
1786 key.offset = 1; 1879 key.offset = 1;
1787 key.flags = 0;
1788 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 1880 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1789 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 1881 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1790 &root_item); 1882 &root_item);
@@ -1845,7 +1937,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1845 struct btrfs_trans_handle *trans; 1937 struct btrfs_trans_handle *trans;
1846 struct btrfs_key key; 1938 struct btrfs_key key;
1847 struct btrfs_root_item new_root_item; 1939 struct btrfs_root_item new_root_item;
1848 struct buffer_head *tmp; 1940 struct extent_buffer *tmp;
1849 int ret; 1941 int ret;
1850 int err; 1942 int err;
1851 u64 objectid; 1943 u64 objectid;
@@ -1876,10 +1968,11 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1876 1968
1877 key.objectid = objectid; 1969 key.objectid = objectid;
1878 key.offset = 1; 1970 key.offset = 1;
1879 key.flags = 0;
1880 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 1971 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1972
1881 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); 1973 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
1882 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); 1974 btrfs_set_root_blocknr(&new_root_item,
1975 extent_buffer_blocknr(root->node));
1883 1976
1884 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 1977 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1885 &new_root_item); 1978 &new_root_item);
@@ -1904,8 +1997,10 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1904fail: 1997fail:
1905 nr = trans->blocks_used; 1998 nr = trans->blocks_used;
1906 err = btrfs_commit_transaction(trans, root); 1999 err = btrfs_commit_transaction(trans, root);
2000
1907 if (err && !ret) 2001 if (err && !ret)
1908 ret = err; 2002 ret = err;
2003
1909 mutex_unlock(&root->fs_info->fs_mutex); 2004 mutex_unlock(&root->fs_info->fs_mutex);
1910 up_write(&root->snap_sem); 2005 up_write(&root->snap_sem);
1911 btrfs_btree_balance_dirty(root, nr); 2006 btrfs_btree_balance_dirty(root, nr);
@@ -1986,7 +2081,7 @@ static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
1986 2081
1987 if (copy_from_user(&vol_args, arg, sizeof(vol_args))) 2082 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
1988 return -EFAULT; 2083 return -EFAULT;
1989 2084
1990 namelen = strlen(vol_args.name); 2085 namelen = strlen(vol_args.name);
1991 if (namelen > BTRFS_VOL_NAME_MAX) 2086 if (namelen > BTRFS_VOL_NAME_MAX)
1992 return -EINVAL; 2087 return -EINVAL;
@@ -2164,8 +2259,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2164 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { 2259 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2165 return -ENOTEMPTY; 2260 return -ENOTEMPTY;
2166 } 2261 }
2262
2167 mutex_lock(&root->fs_info->fs_mutex); 2263 mutex_lock(&root->fs_info->fs_mutex);
2168 trans = btrfs_start_transaction(root, 1); 2264 trans = btrfs_start_transaction(root, 1);
2265
2169 btrfs_set_trans_block_group(trans, new_dir); 2266 btrfs_set_trans_block_group(trans, new_dir);
2170 path = btrfs_alloc_path(); 2267 path = btrfs_alloc_path();
2171 if (!path) { 2268 if (!path) {
@@ -2177,9 +2274,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2177 old_dir->i_ctime = old_dir->i_mtime = ctime; 2274 old_dir->i_ctime = old_dir->i_mtime = ctime;
2178 new_dir->i_ctime = new_dir->i_mtime = ctime; 2275 new_dir->i_ctime = new_dir->i_mtime = ctime;
2179 old_inode->i_ctime = ctime; 2276 old_inode->i_ctime = ctime;
2277
2180 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { 2278 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2181 struct btrfs_key *location = &BTRFS_I(new_dir)->location; 2279 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2182 u64 old_parent_oid; 2280 struct btrfs_key old_parent_key;
2183 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino, 2281 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2184 "..", 2, -1); 2282 "..", 2, -1);
2185 if (IS_ERR(di)) { 2283 if (IS_ERR(di)) {
@@ -2190,7 +2288,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2190 ret = -ENOENT; 2288 ret = -ENOENT;
2191 goto out_fail; 2289 goto out_fail;
2192 } 2290 }
2193 old_parent_oid = btrfs_disk_key_objectid(&di->location); 2291 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
2194 ret = btrfs_del_item(trans, root, path); 2292 ret = btrfs_del_item(trans, root, path);
2195 if (ret) { 2293 if (ret) {
2196 goto out_fail; 2294 goto out_fail;
@@ -2199,7 +2297,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2199 2297
2200 di = btrfs_lookup_dir_index_item(trans, root, path, 2298 di = btrfs_lookup_dir_index_item(trans, root, path,
2201 old_inode->i_ino, 2299 old_inode->i_ino,
2202 old_parent_oid, 2300 old_parent_key.objectid,
2203 "..", 2, -1); 2301 "..", 2, -1);
2204 if (IS_ERR(di)) { 2302 if (IS_ERR(di)) {
2205 ret = PTR_ERR(di); 2303 ret = PTR_ERR(di);
@@ -2257,8 +2355,9 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2257 u64 objectid; 2355 u64 objectid;
2258 int name_len; 2356 int name_len;
2259 int datasize; 2357 int datasize;
2260 char *ptr; 2358 unsigned long ptr;
2261 struct btrfs_file_extent_item *ei; 2359 struct btrfs_file_extent_item *ei;
2360 struct extent_buffer *leaf;
2262 unsigned long nr; 2361 unsigned long nr;
2263 2362
2264 name_len = strlen(symname) + 1; 2363 name_len = strlen(symname) + 1;
@@ -2302,7 +2401,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2302 BUG_ON(!path); 2401 BUG_ON(!path);
2303 key.objectid = inode->i_ino; 2402 key.objectid = inode->i_ino;
2304 key.offset = 0; 2403 key.offset = 0;
2305 key.flags = 0;
2306 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 2404 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2307 datasize = btrfs_file_extent_calc_inline_size(name_len); 2405 datasize = btrfs_file_extent_calc_inline_size(name_len);
2308 err = btrfs_insert_empty_item(trans, root, path, &key, 2406 err = btrfs_insert_empty_item(trans, root, path, &key,
@@ -2311,16 +2409,17 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2311 drop_inode = 1; 2409 drop_inode = 1;
2312 goto out_unlock; 2410 goto out_unlock;
2313 } 2411 }
2314 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), 2412 leaf = path->nodes[0];
2315 path->slots[0], struct btrfs_file_extent_item); 2413 ei = btrfs_item_ptr(leaf, path->slots[0],
2316 btrfs_set_file_extent_generation(ei, trans->transid); 2414 struct btrfs_file_extent_item);
2317 btrfs_set_file_extent_type(ei, 2415 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2416 btrfs_set_file_extent_type(leaf, ei,
2318 BTRFS_FILE_EXTENT_INLINE); 2417 BTRFS_FILE_EXTENT_INLINE);
2319 ptr = btrfs_file_extent_inline_start(ei); 2418 ptr = btrfs_file_extent_inline_start(ei);
2320 btrfs_memcpy(root, path->nodes[0]->b_data, 2419 write_extent_buffer(leaf, symname, ptr, name_len);
2321 ptr, symname, name_len); 2420 btrfs_mark_buffer_dirty(leaf);
2322 btrfs_mark_buffer_dirty(path->nodes[0]);
2323 btrfs_free_path(path); 2421 btrfs_free_path(path);
2422
2324 inode->i_op = &btrfs_symlink_inode_operations; 2423 inode->i_op = &btrfs_symlink_inode_operations;
2325 inode->i_mapping->a_ops = &btrfs_symlink_aops; 2424 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2326 inode->i_size = name_len - 1; 2425 inode->i_size = name_len - 1;