inode.c (4cb5300bc839b8a943eb19c9f27f25470e22d0ca) inode.c (aa38572954ade525817fe88c54faebf85e5a61c0)
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 23 unchanged lines hidden (view full) ---

32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
35#include <linux/bit_spinlock.h>
36#include <linux/xattr.h>
37#include <linux/posix_acl.h>
38#include <linux/falloc.h>
39#include <linux/slab.h>
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 23 unchanged lines hidden (view full) ---

32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
35#include <linux/bit_spinlock.h>
36#include <linux/xattr.h>
37#include <linux/posix_acl.h>
38#include <linux/falloc.h>
39#include <linux/slab.h>
40#include <linux/ratelimit.h>
41#include "compat.h"
42#include "ctree.h"
43#include "disk-io.h"
44#include "transaction.h"
45#include "btrfs_inode.h"
46#include "ioctl.h"
47#include "print-tree.h"
48#include "volumes.h"
49#include "ordered-data.h"
50#include "xattr.h"
51#include "tree-log.h"
52#include "compression.h"
53#include "locking.h"
54#include "free-space-cache.h"
40#include "compat.h"
41#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
47#include "volumes.h"
48#include "ordered-data.h"
49#include "xattr.h"
50#include "tree-log.h"
51#include "compression.h"
52#include "locking.h"
53#include "free-space-cache.h"
55#include "inode-map.h"
56
57struct btrfs_iget_args {
58 u64 ino;
59 struct btrfs_root *root;
60};
61
62static const struct inode_operations btrfs_dir_inode_operations;
63static const struct inode_operations btrfs_symlink_inode_operations;

--- 71 unchanged lines hidden (view full) ---

135
136 path = btrfs_alloc_path();
137 if (!path)
138 return -ENOMEM;
139
140 path->leave_spinning = 1;
141 btrfs_set_trans_block_group(trans, inode);
142
54
55struct btrfs_iget_args {
56 u64 ino;
57 struct btrfs_root *root;
58};
59
60static const struct inode_operations btrfs_dir_inode_operations;
61static const struct inode_operations btrfs_symlink_inode_operations;

--- 71 unchanged lines hidden (view full) ---

133
134 path = btrfs_alloc_path();
135 if (!path)
136 return -ENOMEM;
137
138 path->leave_spinning = 1;
139 btrfs_set_trans_block_group(trans, inode);
140
143 key.objectid = btrfs_ino(inode);
141 key.objectid = inode->i_ino;
144 key.offset = start;
145 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
146 datasize = btrfs_file_extent_calc_inline_size(cur_size);
147
148 inode_add_bytes(inode, size);
149 ret = btrfs_insert_empty_item(trans, root, path, &key,
150 datasize);
151 BUG_ON(ret);

--- 185 unchanged lines hidden (view full) ---

337 unsigned long total_compressed = 0;
338 unsigned long total_in = 0;
339 unsigned long max_compressed = 128 * 1024;
340 unsigned long max_uncompressed = 128 * 1024;
341 int i;
342 int will_compress;
343 int compress_type = root->fs_info->compress_type;
344
142 key.offset = start;
143 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
144 datasize = btrfs_file_extent_calc_inline_size(cur_size);
145
146 inode_add_bytes(inode, size);
147 ret = btrfs_insert_empty_item(trans, root, path, &key,
148 datasize);
149 BUG_ON(ret);

--- 185 unchanged lines hidden (view full) ---

335 unsigned long total_compressed = 0;
336 unsigned long total_in = 0;
337 unsigned long max_compressed = 128 * 1024;
338 unsigned long max_uncompressed = 128 * 1024;
339 int i;
340 int will_compress;
341 int compress_type = root->fs_info->compress_type;
342
345 /* if this is a small write inside eof, kick off a defragbot */
346 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
347 btrfs_add_inode_defrag(NULL, inode);
348
349 actual_end = min_t(u64, isize, end + 1);
350again:
351 will_compress = 0;
352 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
353 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
354
355 /*
356 * we don't want to send crud past the end of i_size through

--- 293 unchanged lines hidden (view full) ---

650 /*
651 * here we're doing allocation and writeback of the
652 * compressed pages
653 */
654 btrfs_drop_extent_cache(inode, async_extent->start,
655 async_extent->start +
656 async_extent->ram_size - 1, 0);
657
343 actual_end = min_t(u64, isize, end + 1);
344again:
345 will_compress = 0;
346 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
347 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
348
349 /*
350 * we don't want to send crud past the end of i_size through

--- 293 unchanged lines hidden (view full) ---

644 /*
645 * here we're doing allocation and writeback of the
646 * compressed pages
647 */
648 btrfs_drop_extent_cache(inode, async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1, 0);
651
658 em = alloc_extent_map();
652 em = alloc_extent_map(GFP_NOFS);
659 BUG_ON(!em);
660 em->start = async_extent->start;
661 em->len = async_extent->ram_size;
662 em->orig_start = em->start;
663
664 em->block_start = ins.objectid;
665 em->block_len = ins.offset;
666 em->bdev = root->fs_info->fs_devices->latest_bdev;

--- 79 unchanged lines hidden (view full) ---

746 free_extent_map(em);
747 }
748 }
749 read_unlock(&em_tree->lock);
750
751 return alloc_hint;
752}
753
653 BUG_ON(!em);
654 em->start = async_extent->start;
655 em->len = async_extent->ram_size;
656 em->orig_start = em->start;
657
658 em->block_start = ins.objectid;
659 em->block_len = ins.offset;
660 em->bdev = root->fs_info->fs_devices->latest_bdev;

--- 79 unchanged lines hidden (view full) ---

740 free_extent_map(em);
741 }
742 }
743 read_unlock(&em_tree->lock);
744
745 return alloc_hint;
746}
747
754static inline bool is_free_space_inode(struct btrfs_root *root,
755 struct inode *inode)
756{
757 if (root == root->fs_info->tree_root ||
758 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
759 return true;
760 return false;
761}
762
763/*
764 * when extent_io.c finds a delayed allocation range in the file,
765 * the call backs end up in this code. The basic idea is to
766 * allocate extents on disk for the range, and create ordered data structs
767 * in ram to track those extents.
768 *
769 * locked_page is the page that writepage had locked already. We use
770 * it to make sure we don't do extra locks or unlocks.

--- 16 unchanged lines hidden (view full) ---

787 u64 disk_num_bytes;
788 u64 cur_alloc_size;
789 u64 blocksize = root->sectorsize;
790 struct btrfs_key ins;
791 struct extent_map *em;
792 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
793 int ret = 0;
794
748/*
749 * when extent_io.c finds a delayed allocation range in the file,
750 * the call backs end up in this code. The basic idea is to
751 * allocate extents on disk for the range, and create ordered data structs
752 * in ram to track those extents.
753 *
754 * locked_page is the page that writepage had locked already. We use
755 * it to make sure we don't do extra locks or unlocks.

--- 16 unchanged lines hidden (view full) ---

772 u64 disk_num_bytes;
773 u64 cur_alloc_size;
774 u64 blocksize = root->sectorsize;
775 struct btrfs_key ins;
776 struct extent_map *em;
777 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
778 int ret = 0;
779
795 BUG_ON(is_free_space_inode(root, inode));
780 BUG_ON(root == root->fs_info->tree_root);
796 trans = btrfs_join_transaction(root, 1);
797 BUG_ON(IS_ERR(trans));
798 btrfs_set_trans_block_group(trans, inode);
799 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
800
801 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
802 num_bytes = max(blocksize, num_bytes);
803 disk_num_bytes = num_bytes;
804 ret = 0;
805
781 trans = btrfs_join_transaction(root, 1);
782 BUG_ON(IS_ERR(trans));
783 btrfs_set_trans_block_group(trans, inode);
784 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
785
786 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
787 num_bytes = max(blocksize, num_bytes);
788 disk_num_bytes = num_bytes;
789 ret = 0;
790
806 /* if this is a small write inside eof, kick off defrag */
807 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
808 btrfs_add_inode_defrag(trans, inode);
809
810 if (start == 0) {
811 /* lets try to make an inline extent */
812 ret = cow_file_range_inline(trans, root, inode,
813 start, end, 0, 0, NULL);
814 if (ret == 0) {
815 extent_clear_unlock_delalloc(inode,
816 &BTRFS_I(inode)->io_tree,
817 start, end, NULL,

--- 22 unchanged lines hidden (view full) ---

840 unsigned long op;
841
842 cur_alloc_size = disk_num_bytes;
843 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
844 root->sectorsize, 0, alloc_hint,
845 (u64)-1, &ins, 1);
846 BUG_ON(ret);
847
791 if (start == 0) {
792 /* lets try to make an inline extent */
793 ret = cow_file_range_inline(trans, root, inode,
794 start, end, 0, 0, NULL);
795 if (ret == 0) {
796 extent_clear_unlock_delalloc(inode,
797 &BTRFS_I(inode)->io_tree,
798 start, end, NULL,

--- 22 unchanged lines hidden (view full) ---

821 unsigned long op;
822
823 cur_alloc_size = disk_num_bytes;
824 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
825 root->sectorsize, 0, alloc_hint,
826 (u64)-1, &ins, 1);
827 BUG_ON(ret);
828
848 em = alloc_extent_map();
829 em = alloc_extent_map(GFP_NOFS);
849 BUG_ON(!em);
850 em->start = start;
851 em->orig_start = em->start;
852 ram_size = ins.offset;
853 em->len = ins.offset;
854
855 em->block_start = ins.objectid;
856 em->block_len = ins.offset;

--- 165 unchanged lines hidden (view full) ---

1022static noinline int csum_exist_in_range(struct btrfs_root *root,
1023 u64 bytenr, u64 num_bytes)
1024{
1025 int ret;
1026 struct btrfs_ordered_sum *sums;
1027 LIST_HEAD(list);
1028
1029 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
830 BUG_ON(!em);
831 em->start = start;
832 em->orig_start = em->start;
833 ram_size = ins.offset;
834 em->len = ins.offset;
835
836 em->block_start = ins.objectid;
837 em->block_len = ins.offset;

--- 165 unchanged lines hidden (view full) ---

1003static noinline int csum_exist_in_range(struct btrfs_root *root,
1004 u64 bytenr, u64 num_bytes)
1005{
1006 int ret;
1007 struct btrfs_ordered_sum *sums;
1008 LIST_HEAD(list);
1009
1010 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1030 bytenr + num_bytes - 1, &list, 0);
1011 bytenr + num_bytes - 1, &list);
1031 if (ret == 0 && list_empty(&list))
1032 return 0;
1033
1034 while (!list_empty(&list)) {
1035 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1036 list_del(&sums->list);
1037 kfree(sums);
1038 }

--- 24 unchanged lines hidden (view full) ---

1063 u64 extent_offset;
1064 u64 disk_bytenr;
1065 u64 num_bytes;
1066 int extent_type;
1067 int ret;
1068 int type;
1069 int nocow;
1070 int check_prev = 1;
1012 if (ret == 0 && list_empty(&list))
1013 return 0;
1014
1015 while (!list_empty(&list)) {
1016 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1017 list_del(&sums->list);
1018 kfree(sums);
1019 }

--- 24 unchanged lines hidden (view full) ---

1044 u64 extent_offset;
1045 u64 disk_bytenr;
1046 u64 num_bytes;
1047 int extent_type;
1048 int ret;
1049 int type;
1050 int nocow;
1051 int check_prev = 1;
1071 bool nolock;
1072 u64 ino = btrfs_ino(inode);
1052 bool nolock = false;
1073
1074 path = btrfs_alloc_path();
1075 BUG_ON(!path);
1053
1054 path = btrfs_alloc_path();
1055 BUG_ON(!path);
1076
1077 nolock = is_free_space_inode(root, inode);
1078
1079 if (nolock)
1056 if (root == root->fs_info->tree_root) {
1057 nolock = true;
1080 trans = btrfs_join_transaction_nolock(root, 1);
1058 trans = btrfs_join_transaction_nolock(root, 1);
1081 else
1059 } else {
1082 trans = btrfs_join_transaction(root, 1);
1060 trans = btrfs_join_transaction(root, 1);
1061 }
1083 BUG_ON(IS_ERR(trans));
1084
1085 cow_start = (u64)-1;
1086 cur_offset = start;
1087 while (1) {
1062 BUG_ON(IS_ERR(trans));
1063
1064 cow_start = (u64)-1;
1065 cur_offset = start;
1066 while (1) {
1088 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1067 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1089 cur_offset, 0);
1090 BUG_ON(ret < 0);
1091 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1092 leaf = path->nodes[0];
1093 btrfs_item_key_to_cpu(leaf, &found_key,
1094 path->slots[0] - 1);
1068 cur_offset, 0);
1069 BUG_ON(ret < 0);
1070 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1071 leaf = path->nodes[0];
1072 btrfs_item_key_to_cpu(leaf, &found_key,
1073 path->slots[0] - 1);
1095 if (found_key.objectid == ino &&
1074 if (found_key.objectid == inode->i_ino &&
1096 found_key.type == BTRFS_EXTENT_DATA_KEY)
1097 path->slots[0]--;
1098 }
1099 check_prev = 0;
1100next_slot:
1101 leaf = path->nodes[0];
1102 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1103 ret = btrfs_next_leaf(root, path);

--- 4 unchanged lines hidden (view full) ---

1108 leaf = path->nodes[0];
1109 }
1110
1111 nocow = 0;
1112 disk_bytenr = 0;
1113 num_bytes = 0;
1114 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1115
1075 found_key.type == BTRFS_EXTENT_DATA_KEY)
1076 path->slots[0]--;
1077 }
1078 check_prev = 0;
1079next_slot:
1080 leaf = path->nodes[0];
1081 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1082 ret = btrfs_next_leaf(root, path);

--- 4 unchanged lines hidden (view full) ---

1087 leaf = path->nodes[0];
1088 }
1089
1090 nocow = 0;
1091 disk_bytenr = 0;
1092 num_bytes = 0;
1093 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1094
1116 if (found_key.objectid > ino ||
1095 if (found_key.objectid > inode->i_ino ||
1117 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1118 found_key.offset > end)
1119 break;
1120
1121 if (found_key.offset > cur_offset) {
1122 extent_end = found_key.offset;
1123 extent_type = 0;
1124 goto out_check;

--- 18 unchanged lines hidden (view full) ---

1143 if (btrfs_file_extent_compression(leaf, fi) ||
1144 btrfs_file_extent_encryption(leaf, fi) ||
1145 btrfs_file_extent_other_encoding(leaf, fi))
1146 goto out_check;
1147 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1148 goto out_check;
1149 if (btrfs_extent_readonly(root, disk_bytenr))
1150 goto out_check;
1096 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1097 found_key.offset > end)
1098 break;
1099
1100 if (found_key.offset > cur_offset) {
1101 extent_end = found_key.offset;
1102 extent_type = 0;
1103 goto out_check;

--- 18 unchanged lines hidden (view full) ---

1122 if (btrfs_file_extent_compression(leaf, fi) ||
1123 btrfs_file_extent_encryption(leaf, fi) ||
1124 btrfs_file_extent_other_encoding(leaf, fi))
1125 goto out_check;
1126 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1127 goto out_check;
1128 if (btrfs_extent_readonly(root, disk_bytenr))
1129 goto out_check;
1151 if (btrfs_cross_ref_exist(trans, root, ino,
1130 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1152 found_key.offset -
1153 extent_offset, disk_bytenr))
1154 goto out_check;
1155 disk_bytenr += extent_offset;
1156 disk_bytenr += cur_offset - found_key.offset;
1157 num_bytes = min(end + 1, extent_end) - cur_offset;
1158 /*
1159 * force cow if csum exists in the range.

--- 20 unchanged lines hidden (view full) ---

1180 cow_start = cur_offset;
1181 cur_offset = extent_end;
1182 if (cur_offset > end)
1183 break;
1184 path->slots[0]++;
1185 goto next_slot;
1186 }
1187
1131 found_key.offset -
1132 extent_offset, disk_bytenr))
1133 goto out_check;
1134 disk_bytenr += extent_offset;
1135 disk_bytenr += cur_offset - found_key.offset;
1136 num_bytes = min(end + 1, extent_end) - cur_offset;
1137 /*
1138 * force cow if csum exists in the range.

--- 20 unchanged lines hidden (view full) ---

1159 cow_start = cur_offset;
1160 cur_offset = extent_end;
1161 if (cur_offset > end)
1162 break;
1163 path->slots[0]++;
1164 goto next_slot;
1165 }
1166
1188 btrfs_release_path(path);
1167 btrfs_release_path(root, path);
1189 if (cow_start != (u64)-1) {
1190 ret = cow_file_range(inode, locked_page, cow_start,
1191 found_key.offset - 1, page_started,
1192 nr_written, 1);
1193 BUG_ON(ret);
1194 cow_start = (u64)-1;
1195 }
1196
1197 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1198 struct extent_map *em;
1199 struct extent_map_tree *em_tree;
1200 em_tree = &BTRFS_I(inode)->extent_tree;
1168 if (cow_start != (u64)-1) {
1169 ret = cow_file_range(inode, locked_page, cow_start,
1170 found_key.offset - 1, page_started,
1171 nr_written, 1);
1172 BUG_ON(ret);
1173 cow_start = (u64)-1;
1174 }
1175
1176 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1177 struct extent_map *em;
1178 struct extent_map_tree *em_tree;
1179 em_tree = &BTRFS_I(inode)->extent_tree;
1201 em = alloc_extent_map();
1180 em = alloc_extent_map(GFP_NOFS);
1202 BUG_ON(!em);
1203 em->start = cur_offset;
1204 em->orig_start = em->start;
1205 em->len = num_bytes;
1206 em->block_len = num_bytes;
1207 em->block_start = disk_bytenr;
1208 em->bdev = root->fs_info->fs_devices->latest_bdev;
1209 set_bit(EXTENT_FLAG_PINNED, &em->flags);

--- 28 unchanged lines hidden (view full) ---

1238 cur_offset, cur_offset + num_bytes - 1,
1239 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1240 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1241 EXTENT_SET_PRIVATE2);
1242 cur_offset = extent_end;
1243 if (cur_offset > end)
1244 break;
1245 }
1181 BUG_ON(!em);
1182 em->start = cur_offset;
1183 em->orig_start = em->start;
1184 em->len = num_bytes;
1185 em->block_len = num_bytes;
1186 em->block_start = disk_bytenr;
1187 em->bdev = root->fs_info->fs_devices->latest_bdev;
1188 set_bit(EXTENT_FLAG_PINNED, &em->flags);

--- 28 unchanged lines hidden (view full) ---

1217 cur_offset, cur_offset + num_bytes - 1,
1218 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1219 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1220 EXTENT_SET_PRIVATE2);
1221 cur_offset = extent_end;
1222 if (cur_offset > end)
1223 break;
1224 }
1246 btrfs_release_path(path);
1225 btrfs_release_path(root, path);
1247
1248 if (cur_offset <= end && cow_start == (u64)-1)
1249 cow_start = cur_offset;
1250 if (cow_start != (u64)-1) {
1251 ret = cow_file_range(inode, locked_page, cow_start, end,
1252 page_started, nr_written, 1);
1253 BUG_ON(ret);
1254 }

--- 71 unchanged lines hidden (view full) ---

1326 * have pending delalloc work to be done.
1327 */
1328static int btrfs_set_bit_hook(struct inode *inode,
1329 struct extent_state *state, int *bits)
1330{
1331
1332 /*
1333 * set_bit and clear bit hooks normally require _irqsave/restore
1226
1227 if (cur_offset <= end && cow_start == (u64)-1)
1228 cow_start = cur_offset;
1229 if (cow_start != (u64)-1) {
1230 ret = cow_file_range(inode, locked_page, cow_start, end,
1231 page_started, nr_written, 1);
1232 BUG_ON(ret);
1233 }

--- 71 unchanged lines hidden (view full) ---

1305 * have pending delalloc work to be done.
1306 */
1307static int btrfs_set_bit_hook(struct inode *inode,
1308 struct extent_state *state, int *bits)
1309{
1310
1311 /*
1312 * set_bit and clear bit hooks normally require _irqsave/restore
1334 * but in this case, we are only testing for the DELALLOC
1313 * but in this case, we are only testeing for the DELALLOC
1335 * bit, which is only set or cleared with irqs on
1336 */
1337 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1338 struct btrfs_root *root = BTRFS_I(inode)->root;
1339 u64 len = state->end + 1 - state->start;
1314 * bit, which is only set or cleared with irqs on
1315 */
1316 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1317 struct btrfs_root *root = BTRFS_I(inode)->root;
1318 u64 len = state->end + 1 - state->start;
1340 bool do_list = !is_free_space_inode(root, inode);
1319 int do_list = (root->root_key.objectid !=
1320 BTRFS_ROOT_TREE_OBJECTID);
1341
1342 if (*bits & EXTENT_FIRST_DELALLOC)
1343 *bits &= ~EXTENT_FIRST_DELALLOC;
1344 else
1345 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1346
1347 spin_lock(&root->fs_info->delalloc_lock);
1348 BTRFS_I(inode)->delalloc_bytes += len;

--- 10 unchanged lines hidden (view full) ---

1359/*
1360 * extent_io.c clear_bit_hook, see set_bit_hook for why
1361 */
1362static int btrfs_clear_bit_hook(struct inode *inode,
1363 struct extent_state *state, int *bits)
1364{
1365 /*
1366 * set_bit and clear bit hooks normally require _irqsave/restore
1321
1322 if (*bits & EXTENT_FIRST_DELALLOC)
1323 *bits &= ~EXTENT_FIRST_DELALLOC;
1324 else
1325 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1326
1327 spin_lock(&root->fs_info->delalloc_lock);
1328 BTRFS_I(inode)->delalloc_bytes += len;

--- 10 unchanged lines hidden (view full) ---

1339/*
1340 * extent_io.c clear_bit_hook, see set_bit_hook for why
1341 */
1342static int btrfs_clear_bit_hook(struct inode *inode,
1343 struct extent_state *state, int *bits)
1344{
1345 /*
1346 * set_bit and clear bit hooks normally require _irqsave/restore
1367 * but in this case, we are only testing for the DELALLOC
1347 * but in this case, we are only testeing for the DELALLOC
1368 * bit, which is only set or cleared with irqs on
1369 */
1370 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1371 struct btrfs_root *root = BTRFS_I(inode)->root;
1372 u64 len = state->end + 1 - state->start;
1348 * bit, which is only set or cleared with irqs on
1349 */
1350 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1351 struct btrfs_root *root = BTRFS_I(inode)->root;
1352 u64 len = state->end + 1 - state->start;
1373 bool do_list = !is_free_space_inode(root, inode);
1353 int do_list = (root->root_key.objectid !=
1354 BTRFS_ROOT_TREE_OBJECTID);
1374
1375 if (*bits & EXTENT_FIRST_DELALLOC)
1376 *bits &= ~EXTENT_FIRST_DELALLOC;
1377 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1378 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1379
1380 if (*bits & EXTENT_DO_ACCOUNTING)
1381 btrfs_delalloc_release_metadata(inode, len);

--- 90 unchanged lines hidden (view full) ---

1472 u64 bio_offset)
1473{
1474 struct btrfs_root *root = BTRFS_I(inode)->root;
1475 int ret = 0;
1476 int skip_sum;
1477
1478 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1479
1355
1356 if (*bits & EXTENT_FIRST_DELALLOC)
1357 *bits &= ~EXTENT_FIRST_DELALLOC;
1358 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1359 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1360
1361 if (*bits & EXTENT_DO_ACCOUNTING)
1362 btrfs_delalloc_release_metadata(inode, len);

--- 90 unchanged lines hidden (view full) ---

1453 u64 bio_offset)
1454{
1455 struct btrfs_root *root = BTRFS_I(inode)->root;
1456 int ret = 0;
1457 int skip_sum;
1458
1459 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1460
1480 if (is_free_space_inode(root, inode))
1461 if (root == root->fs_info->tree_root)
1481 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1482 else
1483 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1484 BUG_ON(ret);
1485
1486 if (!(rw & REQ_WRITE)) {
1487 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1488 return btrfs_submit_compressed_read(inode, bio,

--- 169 unchanged lines hidden (view full) ---

1658 * So, tell btrfs_drop_extents to leave this extent in the cache.
1659 * the caller is expected to unpin it and allow it to be merged
1660 * with the others.
1661 */
1662 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1663 &hint, 0);
1664 BUG_ON(ret);
1665
1462 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1463 else
1464 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1465 BUG_ON(ret);
1466
1467 if (!(rw & REQ_WRITE)) {
1468 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1469 return btrfs_submit_compressed_read(inode, bio,

--- 169 unchanged lines hidden (view full) ---

1639 * So, tell btrfs_drop_extents to leave this extent in the cache.
1640 * the caller is expected to unpin it and allow it to be merged
1641 * with the others.
1642 */
1643 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1644 &hint, 0);
1645 BUG_ON(ret);
1646
1666 ins.objectid = btrfs_ino(inode);
1647 ins.objectid = inode->i_ino;
1667 ins.offset = file_pos;
1668 ins.type = BTRFS_EXTENT_DATA_KEY;
1669 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1670 BUG_ON(ret);
1671 leaf = path->nodes[0];
1672 fi = btrfs_item_ptr(leaf, path->slots[0],
1673 struct btrfs_file_extent_item);
1674 btrfs_set_file_extent_generation(leaf, fi, trans->transid);

--- 14 unchanged lines hidden (view full) ---

1689
1690 inode_add_bytes(inode, num_bytes);
1691
1692 ins.objectid = disk_bytenr;
1693 ins.offset = disk_num_bytes;
1694 ins.type = BTRFS_EXTENT_ITEM_KEY;
1695 ret = btrfs_alloc_reserved_file_extent(trans, root,
1696 root->root_key.objectid,
1648 ins.offset = file_pos;
1649 ins.type = BTRFS_EXTENT_DATA_KEY;
1650 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1651 BUG_ON(ret);
1652 leaf = path->nodes[0];
1653 fi = btrfs_item_ptr(leaf, path->slots[0],
1654 struct btrfs_file_extent_item);
1655 btrfs_set_file_extent_generation(leaf, fi, trans->transid);

--- 14 unchanged lines hidden (view full) ---

1670
1671 inode_add_bytes(inode, num_bytes);
1672
1673 ins.objectid = disk_bytenr;
1674 ins.offset = disk_num_bytes;
1675 ins.type = BTRFS_EXTENT_ITEM_KEY;
1676 ret = btrfs_alloc_reserved_file_extent(trans, root,
1677 root->root_key.objectid,
1697 btrfs_ino(inode), file_pos, &ins);
1678 inode->i_ino, file_pos, &ins);
1698 BUG_ON(ret);
1699 btrfs_free_path(path);
1700
1701 return 0;
1702}
1703
1704/*
1705 * helper function for btrfs_finish_ordered_io, this

--- 9 unchanged lines hidden (view full) ---

1715{
1716 struct btrfs_root *root = BTRFS_I(inode)->root;
1717 struct btrfs_trans_handle *trans = NULL;
1718 struct btrfs_ordered_extent *ordered_extent = NULL;
1719 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1720 struct extent_state *cached_state = NULL;
1721 int compress_type = 0;
1722 int ret;
1679 BUG_ON(ret);
1680 btrfs_free_path(path);
1681
1682 return 0;
1683}
1684
1685/*
1686 * helper function for btrfs_finish_ordered_io, this

--- 9 unchanged lines hidden (view full) ---

1696{
1697 struct btrfs_root *root = BTRFS_I(inode)->root;
1698 struct btrfs_trans_handle *trans = NULL;
1699 struct btrfs_ordered_extent *ordered_extent = NULL;
1700 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1701 struct extent_state *cached_state = NULL;
1702 int compress_type = 0;
1703 int ret;
1723 bool nolock;
1704 bool nolock = false;
1724
1725 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1726 end - start + 1);
1727 if (!ret)
1728 return 0;
1729 BUG_ON(!ordered_extent);
1730
1705
1706 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1707 end - start + 1);
1708 if (!ret)
1709 return 0;
1710 BUG_ON(!ordered_extent);
1711
1731 nolock = is_free_space_inode(root, inode);
1712 nolock = (root == root->fs_info->tree_root);
1732
1733 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1734 BUG_ON(!list_empty(&ordered_extent->list));
1735 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1736 if (!ret) {
1737 if (nolock)
1738 trans = btrfs_join_transaction_nolock(root, 1);
1739 else

--- 129 unchanged lines hidden (view full) ---

1869 read_lock(&em_tree->lock);
1870 em = lookup_extent_mapping(em_tree, start, failrec->len);
1871 if (em->start > start || em->start + em->len < start) {
1872 free_extent_map(em);
1873 em = NULL;
1874 }
1875 read_unlock(&em_tree->lock);
1876
1713
1714 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1715 BUG_ON(!list_empty(&ordered_extent->list));
1716 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1717 if (!ret) {
1718 if (nolock)
1719 trans = btrfs_join_transaction_nolock(root, 1);
1720 else

--- 129 unchanged lines hidden (view full) ---

1850 read_lock(&em_tree->lock);
1851 em = lookup_extent_mapping(em_tree, start, failrec->len);
1852 if (em->start > start || em->start + em->len < start) {
1853 free_extent_map(em);
1854 em = NULL;
1855 }
1856 read_unlock(&em_tree->lock);
1857
1877 if (IS_ERR_OR_NULL(em)) {
1858 if (!em || IS_ERR(em)) {
1878 kfree(failrec);
1879 return -EIO;
1880 }
1881 logical = start - em->start;
1882 logical = em->block_start + logical;
1883 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1884 logical = em->block_start;
1885 failrec->bio_flags = EXTENT_BIO_COMPRESSED;

--- 132 unchanged lines hidden (view full) ---

2018good:
2019 /* if the io failure tree for this inode is non-empty,
2020 * check to see if we've recovered from a failed IO
2021 */
2022 btrfs_clean_io_failures(inode, start);
2023 return 0;
2024
2025zeroit:
1859 kfree(failrec);
1860 return -EIO;
1861 }
1862 logical = start - em->start;
1863 logical = em->block_start + logical;
1864 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1865 logical = em->block_start;
1866 failrec->bio_flags = EXTENT_BIO_COMPRESSED;

--- 132 unchanged lines hidden (view full) ---

1999good:
2000 /* if the io failure tree for this inode is non-empty,
2001 * check to see if we've recovered from a failed IO
2002 */
2003 btrfs_clean_io_failures(inode, start);
2004 return 0;
2005
2006zeroit:
2026 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2027 "private %llu\n",
2028 (unsigned long long)btrfs_ino(page->mapping->host),
2007 if (printk_ratelimit()) {
2008 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
2009 "private %llu\n", page->mapping->host->i_ino,
2029 (unsigned long long)start, csum,
2030 (unsigned long long)private);
2010 (unsigned long long)start, csum,
2011 (unsigned long long)private);
2012 }
2031 memset(kaddr + offset, 1, end - start + 1);
2032 flush_dcache_page(page);
2033 kunmap_atomic(kaddr, KM_USER0);
2034 if (private == 0)
2035 return 0;
2036 return -EIO;
2037}
2038

--- 218 unchanged lines hidden (view full) ---

2257 /* grab metadata reservation from transaction handle */
2258 if (reserve) {
2259 ret = btrfs_orphan_reserve_metadata(trans, inode);
2260 BUG_ON(ret);
2261 }
2262
2263 /* insert an orphan item to track this unlinked/truncated file */
2264 if (insert >= 1) {
2013 memset(kaddr + offset, 1, end - start + 1);
2014 flush_dcache_page(page);
2015 kunmap_atomic(kaddr, KM_USER0);
2016 if (private == 0)
2017 return 0;
2018 return -EIO;
2019}
2020

--- 218 unchanged lines hidden (view full) ---

2239 /* grab metadata reservation from transaction handle */
2240 if (reserve) {
2241 ret = btrfs_orphan_reserve_metadata(trans, inode);
2242 BUG_ON(ret);
2243 }
2244
2245 /* insert an orphan item to track this unlinked/truncated file */
2246 if (insert >= 1) {
2265 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2247 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2266 BUG_ON(ret);
2267 }
2268
2269 /* insert an orphan item to track subvolume contains orphan files */
2270 if (insert >= 2) {
2271 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2272 root->root_key.objectid);
2273 BUG_ON(ret);

--- 20 unchanged lines hidden (view full) ---

2294
2295 if (BTRFS_I(inode)->orphan_meta_reserved) {
2296 BTRFS_I(inode)->orphan_meta_reserved = 0;
2297 release_rsv = 1;
2298 }
2299 spin_unlock(&root->orphan_lock);
2300
2301 if (trans && delete_item) {
2248 BUG_ON(ret);
2249 }
2250
2251 /* insert an orphan item to track subvolume contains orphan files */
2252 if (insert >= 2) {
2253 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2254 root->root_key.objectid);
2255 BUG_ON(ret);

--- 20 unchanged lines hidden (view full) ---

2276
2277 if (BTRFS_I(inode)->orphan_meta_reserved) {
2278 BTRFS_I(inode)->orphan_meta_reserved = 0;
2279 release_rsv = 1;
2280 }
2281 spin_unlock(&root->orphan_lock);
2282
2283 if (trans && delete_item) {
2302 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2284 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2303 BUG_ON(ret);
2304 }
2305
2306 if (release_rsv)
2307 btrfs_orphan_release_metadata(inode);
2308
2309 return 0;
2310}

--- 48 unchanged lines hidden (view full) ---

2359
2360 /* make sure the item matches what we want */
2361 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2362 break;
2363 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2364 break;
2365
2366 /* release the path since we're done with it */
2285 BUG_ON(ret);
2286 }
2287
2288 if (release_rsv)
2289 btrfs_orphan_release_metadata(inode);
2290
2291 return 0;
2292}

--- 48 unchanged lines hidden (view full) ---

2341
2342 /* make sure the item matches what we want */
2343 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2344 break;
2345 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2346 break;
2347
2348 /* release the path since we're done with it */
2367 btrfs_release_path(path);
2349 btrfs_release_path(root, path);
2368
2369 /*
2370 * this is where we are basically btrfs_lookup, without the
2371 * crossing root thing. we store the inode number in the
2372 * offset of the orphan item.
2373 */
2374 found_key.objectid = found_key.offset;
2375 found_key.type = BTRFS_INODE_ITEM_KEY;

--- 180 unchanged lines hidden (view full) ---

2556 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2557
2558 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2559
2560 /*
2561 * try to precache a NULL acl entry for files that don't have
2562 * any xattrs or acls
2563 */
2350
2351 /*
2352 * this is where we are basically btrfs_lookup, without the
2353 * crossing root thing. we store the inode number in the
2354 * offset of the orphan item.
2355 */
2356 found_key.objectid = found_key.offset;
2357 found_key.type = BTRFS_INODE_ITEM_KEY;

--- 180 unchanged lines hidden (view full) ---

2538 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2539
2540 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2541
2542 /*
2543 * try to precache a NULL acl entry for files that don't have
2544 * any xattrs or acls
2545 */
2564 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2565 btrfs_ino(inode));
2546 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
2566 if (!maybe_acls)
2567 cache_no_acl(inode);
2568
2569 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2570 alloc_group_block, 0);
2571 btrfs_free_path(path);
2572 inode_item = NULL;
2573

--- 87 unchanged lines hidden (view full) ---

2661noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2662 struct btrfs_root *root, struct inode *inode)
2663{
2664 struct btrfs_inode_item *inode_item;
2665 struct btrfs_path *path;
2666 struct extent_buffer *leaf;
2667 int ret;
2668
2547 if (!maybe_acls)
2548 cache_no_acl(inode);
2549
2550 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2551 alloc_group_block, 0);
2552 btrfs_free_path(path);
2553 inode_item = NULL;
2554

--- 87 unchanged lines hidden (view full) ---

2642noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *root, struct inode *inode)
2644{
2645 struct btrfs_inode_item *inode_item;
2646 struct btrfs_path *path;
2647 struct extent_buffer *leaf;
2648 int ret;
2649
2669 /*
2670 * If root is tree root, it means this inode is used to
2671 * store free space information. And these inodes are updated
2672 * when committing the transaction, so they needn't delaye to
2673 * be updated, or deadlock will occured.
2674 */
2675 if (!is_free_space_inode(root, inode)) {
2676 ret = btrfs_delayed_update_inode(trans, root, inode);
2677 if (!ret)
2678 btrfs_set_inode_last_trans(trans, inode);
2679 return ret;
2680 }
2681
2682 path = btrfs_alloc_path();
2650 path = btrfs_alloc_path();
2683 if (!path)
2684 return -ENOMEM;
2685
2651 BUG_ON(!path);
2686 path->leave_spinning = 1;
2652 path->leave_spinning = 1;
2687 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2688 1);
2653 ret = btrfs_lookup_inode(trans, root, path,
2654 &BTRFS_I(inode)->location, 1);
2689 if (ret) {
2690 if (ret > 0)
2691 ret = -ENOENT;
2692 goto failed;
2693 }
2694
2695 btrfs_unlock_up_safe(path, 1);
2696 leaf = path->nodes[0];
2697 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2655 if (ret) {
2656 if (ret > 0)
2657 ret = -ENOENT;
2658 goto failed;
2659 }
2660
2661 btrfs_unlock_up_safe(path, 1);
2662 leaf = path->nodes[0];
2663 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2698 struct btrfs_inode_item);
2664 struct btrfs_inode_item);
2699
2700 fill_inode_item(trans, leaf, inode_item, inode);
2701 btrfs_mark_buffer_dirty(leaf);
2702 btrfs_set_inode_last_trans(trans, inode);
2703 ret = 0;
2704failed:
2705 btrfs_free_path(path);
2706 return ret;
2707}
2708
2665
2666 fill_inode_item(trans, leaf, inode_item, inode);
2667 btrfs_mark_buffer_dirty(leaf);
2668 btrfs_set_inode_last_trans(trans, inode);
2669 ret = 0;
2670failed:
2671 btrfs_free_path(path);
2672 return ret;
2673}
2674
2675
2709/*
2710 * unlink helper that gets used here in inode.c and in the tree logging
2711 * recovery code. It remove a link in a directory with a given name, and
2712 * also drops the back refs in the inode to the directory
2713 */
2714static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2715 struct btrfs_root *root,
2716 struct inode *dir, struct inode *inode,
2717 const char *name, int name_len)
2718{
2719 struct btrfs_path *path;
2720 int ret = 0;
2721 struct extent_buffer *leaf;
2722 struct btrfs_dir_item *di;
2723 struct btrfs_key key;
2724 u64 index;
2676/*
2677 * unlink helper that gets used here in inode.c and in the tree logging
2678 * recovery code. It remove a link in a directory with a given name, and
2679 * also drops the back refs in the inode to the directory
2680 */
2681static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2682 struct btrfs_root *root,
2683 struct inode *dir, struct inode *inode,
2684 const char *name, int name_len)
2685{
2686 struct btrfs_path *path;
2687 int ret = 0;
2688 struct extent_buffer *leaf;
2689 struct btrfs_dir_item *di;
2690 struct btrfs_key key;
2691 u64 index;
2725 u64 ino = btrfs_ino(inode);
2726 u64 dir_ino = btrfs_ino(dir);
2727
2728 path = btrfs_alloc_path();
2729 if (!path) {
2730 ret = -ENOMEM;
2731 goto out;
2732 }
2733
2734 path->leave_spinning = 1;
2692
2693 path = btrfs_alloc_path();
2694 if (!path) {
2695 ret = -ENOMEM;
2696 goto out;
2697 }
2698
2699 path->leave_spinning = 1;
2735 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2700 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2736 name, name_len, -1);
2737 if (IS_ERR(di)) {
2738 ret = PTR_ERR(di);
2739 goto err;
2740 }
2741 if (!di) {
2742 ret = -ENOENT;
2743 goto err;
2744 }
2745 leaf = path->nodes[0];
2746 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2747 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2748 if (ret)
2749 goto err;
2701 name, name_len, -1);
2702 if (IS_ERR(di)) {
2703 ret = PTR_ERR(di);
2704 goto err;
2705 }
2706 if (!di) {
2707 ret = -ENOENT;
2708 goto err;
2709 }
2710 leaf = path->nodes[0];
2711 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2712 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2713 if (ret)
2714 goto err;
2750 btrfs_release_path(path);
2715 btrfs_release_path(root, path);
2751
2716
2752 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2753 dir_ino, &index);
2717 ret = btrfs_del_inode_ref(trans, root, name, name_len,
2718 inode->i_ino,
2719 dir->i_ino, &index);
2754 if (ret) {
2755 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2720 if (ret) {
2721 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2756 "inode %llu parent %llu\n", name_len, name,
2757 (unsigned long long)ino, (unsigned long long)dir_ino);
2722 "inode %lu parent %lu\n", name_len, name,
2723 inode->i_ino, dir->i_ino);
2758 goto err;
2759 }
2760
2724 goto err;
2725 }
2726
2761 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2762 if (ret)
2727 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2728 index, name, name_len, -1);
2729 if (IS_ERR(di)) {
2730 ret = PTR_ERR(di);
2763 goto err;
2731 goto err;
2732 }
2733 if (!di) {
2734 ret = -ENOENT;
2735 goto err;
2736 }
2737 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2738 btrfs_release_path(root, path);
2764
2765 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2739
2740 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2766 inode, dir_ino);
2741 inode, dir->i_ino);
2767 BUG_ON(ret != 0 && ret != -ENOENT);
2768
2769 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2770 dir, index);
2771 if (ret == -ENOENT)
2772 ret = 0;
2773err:
2774 btrfs_free_path(path);

--- 61 unchanged lines hidden (view full) ---

2836 struct btrfs_path *path;
2837 struct btrfs_inode_ref *ref;
2838 struct btrfs_dir_item *di;
2839 struct inode *inode = dentry->d_inode;
2840 u64 index;
2841 int check_link = 1;
2842 int err = -ENOSPC;
2843 int ret;
2742 BUG_ON(ret != 0 && ret != -ENOENT);
2743
2744 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2745 dir, index);
2746 if (ret == -ENOENT)
2747 ret = 0;
2748err:
2749 btrfs_free_path(path);

--- 61 unchanged lines hidden (view full) ---

2811 struct btrfs_path *path;
2812 struct btrfs_inode_ref *ref;
2813 struct btrfs_dir_item *di;
2814 struct inode *inode = dentry->d_inode;
2815 u64 index;
2816 int check_link = 1;
2817 int err = -ENOSPC;
2818 int ret;
2844 u64 ino = btrfs_ino(inode);
2845 u64 dir_ino = btrfs_ino(dir);
2846
2847 trans = btrfs_start_transaction(root, 10);
2848 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2849 return trans;
2850
2819
2820 trans = btrfs_start_transaction(root, 10);
2821 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2822 return trans;
2823
2851 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2824 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2852 return ERR_PTR(-ENOSPC);
2853
2854 /* check if there is someone else holds reference */
2855 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2856 return ERR_PTR(-ENOSPC);
2857
2858 if (atomic_read(&inode->i_count) > 2)
2859 return ERR_PTR(-ENOSPC);

--- 24 unchanged lines hidden (view full) ---

2884 goto out;
2885 }
2886 if (ret == 0) {
2887 if (check_path_shared(root, path))
2888 goto out;
2889 } else {
2890 check_link = 0;
2891 }
2825 return ERR_PTR(-ENOSPC);
2826
2827 /* check if there is someone else holds reference */
2828 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2829 return ERR_PTR(-ENOSPC);
2830
2831 if (atomic_read(&inode->i_count) > 2)
2832 return ERR_PTR(-ENOSPC);

--- 24 unchanged lines hidden (view full) ---

2857 goto out;
2858 }
2859 if (ret == 0) {
2860 if (check_path_shared(root, path))
2861 goto out;
2862 } else {
2863 check_link = 0;
2864 }
2892 btrfs_release_path(path);
2865 btrfs_release_path(root, path);
2893
2894 ret = btrfs_lookup_inode(trans, root, path,
2895 &BTRFS_I(inode)->location, 0);
2896 if (ret < 0) {
2897 err = ret;
2898 goto out;
2899 }
2900 if (ret == 0) {
2901 if (check_path_shared(root, path))
2902 goto out;
2903 } else {
2904 check_link = 0;
2905 }
2866
2867 ret = btrfs_lookup_inode(trans, root, path,
2868 &BTRFS_I(inode)->location, 0);
2869 if (ret < 0) {
2870 err = ret;
2871 goto out;
2872 }
2873 if (ret == 0) {
2874 if (check_path_shared(root, path))
2875 goto out;
2876 } else {
2877 check_link = 0;
2878 }
2906 btrfs_release_path(path);
2879 btrfs_release_path(root, path);
2907
2908 if (ret == 0 && S_ISREG(inode->i_mode)) {
2909 ret = btrfs_lookup_file_extent(trans, root, path,
2880
2881 if (ret == 0 && S_ISREG(inode->i_mode)) {
2882 ret = btrfs_lookup_file_extent(trans, root, path,
2910 ino, (u64)-1, 0);
2883 inode->i_ino, (u64)-1, 0);
2911 if (ret < 0) {
2912 err = ret;
2913 goto out;
2914 }
2915 BUG_ON(ret == 0);
2916 if (check_path_shared(root, path))
2917 goto out;
2884 if (ret < 0) {
2885 err = ret;
2886 goto out;
2887 }
2888 BUG_ON(ret == 0);
2889 if (check_path_shared(root, path))
2890 goto out;
2918 btrfs_release_path(path);
2891 btrfs_release_path(root, path);
2919 }
2920
2921 if (!check_link) {
2922 err = 0;
2923 goto out;
2924 }
2925
2892 }
2893
2894 if (!check_link) {
2895 err = 0;
2896 goto out;
2897 }
2898
2926 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2899 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2927 dentry->d_name.name, dentry->d_name.len, 0);
2928 if (IS_ERR(di)) {
2929 err = PTR_ERR(di);
2930 goto out;
2931 }
2932 if (di) {
2933 if (check_path_shared(root, path))
2934 goto out;
2935 } else {
2936 err = 0;
2937 goto out;
2938 }
2900 dentry->d_name.name, dentry->d_name.len, 0);
2901 if (IS_ERR(di)) {
2902 err = PTR_ERR(di);
2903 goto out;
2904 }
2905 if (di) {
2906 if (check_path_shared(root, path))
2907 goto out;
2908 } else {
2909 err = 0;
2910 goto out;
2911 }
2939 btrfs_release_path(path);
2912 btrfs_release_path(root, path);
2940
2941 ref = btrfs_lookup_inode_ref(trans, root, path,
2942 dentry->d_name.name, dentry->d_name.len,
2913
2914 ref = btrfs_lookup_inode_ref(trans, root, path,
2915 dentry->d_name.name, dentry->d_name.len,
2943 ino, dir_ino, 0);
2916 inode->i_ino, dir->i_ino, 0);
2944 if (IS_ERR(ref)) {
2945 err = PTR_ERR(ref);
2946 goto out;
2947 }
2948 BUG_ON(!ref);
2949 if (check_path_shared(root, path))
2950 goto out;
2951 index = btrfs_inode_ref_index(path->nodes[0], ref);
2917 if (IS_ERR(ref)) {
2918 err = PTR_ERR(ref);
2919 goto out;
2920 }
2921 BUG_ON(!ref);
2922 if (check_path_shared(root, path))
2923 goto out;
2924 index = btrfs_inode_ref_index(path->nodes[0], ref);
2952 btrfs_release_path(path);
2925 btrfs_release_path(root, path);
2953
2926
2954 /*
2955 * This is a commit root search, if we can lookup inode item and other
2956 * relative items in the commit root, it means the transaction of
2957 * dir/file creation has been committed, and the dir index item that we
2958 * delay to insert has also been inserted into the commit root. So
2959 * we needn't worry about the delayed insertion of the dir index item
2960 * here.
2961 */
2962 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2927 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2963 dentry->d_name.name, dentry->d_name.len, 0);
2964 if (IS_ERR(di)) {
2965 err = PTR_ERR(di);
2966 goto out;
2967 }
2968 BUG_ON(ret == -ENOENT);
2969 if (check_path_shared(root, path))
2970 goto out;

--- 58 unchanged lines hidden (view full) ---

3029 const char *name, int name_len)
3030{
3031 struct btrfs_path *path;
3032 struct extent_buffer *leaf;
3033 struct btrfs_dir_item *di;
3034 struct btrfs_key key;
3035 u64 index;
3036 int ret;
2928 dentry->d_name.name, dentry->d_name.len, 0);
2929 if (IS_ERR(di)) {
2930 err = PTR_ERR(di);
2931 goto out;
2932 }
2933 BUG_ON(ret == -ENOENT);
2934 if (check_path_shared(root, path))
2935 goto out;

--- 58 unchanged lines hidden (view full) ---

2994 const char *name, int name_len)
2995{
2996 struct btrfs_path *path;
2997 struct extent_buffer *leaf;
2998 struct btrfs_dir_item *di;
2999 struct btrfs_key key;
3000 u64 index;
3001 int ret;
3037 u64 dir_ino = btrfs_ino(dir);
3038
3039 path = btrfs_alloc_path();
3040 if (!path)
3041 return -ENOMEM;
3042
3002
3003 path = btrfs_alloc_path();
3004 if (!path)
3005 return -ENOMEM;
3006
3043 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3007 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
3044 name, name_len, -1);
3008 name, name_len, -1);
3045 BUG_ON(IS_ERR_OR_NULL(di));
3009 BUG_ON(!di || IS_ERR(di));
3046
3047 leaf = path->nodes[0];
3048 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3049 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3050 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3051 BUG_ON(ret);
3010
3011 leaf = path->nodes[0];
3012 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3013 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3014 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3015 BUG_ON(ret);
3052 btrfs_release_path(path);
3016 btrfs_release_path(root, path);
3053
3054 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3055 objectid, root->root_key.objectid,
3017
3018 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3019 objectid, root->root_key.objectid,
3056 dir_ino, &index, name, name_len);
3020 dir->i_ino, &index, name, name_len);
3057 if (ret < 0) {
3058 BUG_ON(ret != -ENOENT);
3021 if (ret < 0) {
3022 BUG_ON(ret != -ENOENT);
3059 di = btrfs_search_dir_index_item(root, path, dir_ino,
3023 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
3060 name, name_len);
3024 name, name_len);
3061 BUG_ON(IS_ERR_OR_NULL(di));
3025 BUG_ON(!di || IS_ERR(di));
3062
3063 leaf = path->nodes[0];
3064 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3026
3027 leaf = path->nodes[0];
3028 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3065 btrfs_release_path(path);
3029 btrfs_release_path(root, path);
3066 index = key.offset;
3067 }
3030 index = key.offset;
3031 }
3068 btrfs_release_path(path);
3069
3032
3070 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3033 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
3034 index, name, name_len, -1);
3035 BUG_ON(!di || IS_ERR(di));
3036
3037 leaf = path->nodes[0];
3038 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3039 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3040 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3071 BUG_ON(ret);
3041 BUG_ON(ret);
3042 btrfs_release_path(root, path);
3072
3073 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3074 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3075 ret = btrfs_update_inode(trans, root, dir);
3076 BUG_ON(ret);
3077
3043
3044 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3045 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3046 ret = btrfs_update_inode(trans, root, dir);
3047 BUG_ON(ret);
3048
3049 btrfs_free_path(path);
3078 return 0;
3079}
3080
3081static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3082{
3083 struct inode *inode = dentry->d_inode;
3084 int err = 0;
3085 struct btrfs_root *root = BTRFS_I(dir)->root;
3086 struct btrfs_trans_handle *trans;
3087 unsigned long nr = 0;
3088
3089 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3050 return 0;
3051}
3052
3053static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3054{
3055 struct inode *inode = dentry->d_inode;
3056 int err = 0;
3057 struct btrfs_root *root = BTRFS_I(dir)->root;
3058 struct btrfs_trans_handle *trans;
3059 unsigned long nr = 0;
3060
3061 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3090 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3062 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
3091 return -ENOTEMPTY;
3092
3093 trans = __unlink_start_trans(dir, dentry);
3094 if (IS_ERR(trans))
3095 return PTR_ERR(trans);
3096
3097 btrfs_set_trans_block_group(trans, dir);
3098
3063 return -ENOTEMPTY;
3064
3065 trans = __unlink_start_trans(dir, dentry);
3066 if (IS_ERR(trans))
3067 return PTR_ERR(trans);
3068
3069 btrfs_set_trans_block_group(trans, dir);
3070
3099 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3071 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3100 err = btrfs_unlink_subvol(trans, root, dir,
3101 BTRFS_I(inode)->location.objectid,
3102 dentry->d_name.name,
3103 dentry->d_name.len);
3104 goto out;
3105 }
3106
3107 err = btrfs_orphan_add(trans, inode);

--- 8 unchanged lines hidden (view full) ---

3116out:
3117 nr = trans->blocks_used;
3118 __unlink_end_trans(trans, root);
3119 btrfs_btree_balance_dirty(root, nr);
3120
3121 return err;
3122}
3123
3072 err = btrfs_unlink_subvol(trans, root, dir,
3073 BTRFS_I(inode)->location.objectid,
3074 dentry->d_name.name,
3075 dentry->d_name.len);
3076 goto out;
3077 }
3078
3079 err = btrfs_orphan_add(trans, inode);

--- 8 unchanged lines hidden (view full) ---

3088out:
3089 nr = trans->blocks_used;
3090 __unlink_end_trans(trans, root);
3091 btrfs_btree_balance_dirty(root, nr);
3092
3093 return err;
3094}
3095
3096#if 0
3124/*
3097/*
3098 * when truncating bytes in a file, it is possible to avoid reading
3099 * the leaves that contain only checksum items. This can be the
3100 * majority of the IO required to delete a large file, but it must
3101 * be done carefully.
3102 *
3103 * The keys in the level just above the leaves are checked to make sure
3104 * the lowest key in a given leaf is a csum key, and starts at an offset
3105 * after the new size.
3106 *
3107 * Then the key for the next leaf is checked to make sure it also has
3108 * a checksum item for the same file. If it does, we know our target leaf
3109 * contains only checksum items, and it can be safely freed without reading
3110 * it.
3111 *
3112 * This is just an optimization targeted at large files. It may do
3113 * nothing. It will return 0 unless things went badly.
3114 */
3115static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3116 struct btrfs_root *root,
3117 struct btrfs_path *path,
3118 struct inode *inode, u64 new_size)
3119{
3120 struct btrfs_key key;
3121 int ret;
3122 int nritems;
3123 struct btrfs_key found_key;
3124 struct btrfs_key other_key;
3125 struct btrfs_leaf_ref *ref;
3126 u64 leaf_gen;
3127 u64 leaf_start;
3128
3129 path->lowest_level = 1;
3130 key.objectid = inode->i_ino;
3131 key.type = BTRFS_CSUM_ITEM_KEY;
3132 key.offset = new_size;
3133again:
3134 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3135 if (ret < 0)
3136 goto out;
3137
3138 if (path->nodes[1] == NULL) {
3139 ret = 0;
3140 goto out;
3141 }
3142 ret = 0;
3143 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3144 nritems = btrfs_header_nritems(path->nodes[1]);
3145
3146 if (!nritems)
3147 goto out;
3148
3149 if (path->slots[1] >= nritems)
3150 goto next_node;
3151
3152 /* did we find a key greater than anything we want to delete? */
3153 if (found_key.objectid > inode->i_ino ||
3154 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3155 goto out;
3156
3157 /* we check the next key in the node to make sure the leave contains
3158 * only checksum items. This comparison doesn't work if our
3159 * leaf is the last one in the node
3160 */
3161 if (path->slots[1] + 1 >= nritems) {
3162next_node:
3163 /* search forward from the last key in the node, this
3164 * will bring us into the next node in the tree
3165 */
3166 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3167
3168 /* unlikely, but we inc below, so check to be safe */
3169 if (found_key.offset == (u64)-1)
3170 goto out;
3171
3172 /* search_forward needs a path with locks held, do the
3173 * search again for the original key. It is possible
3174 * this will race with a balance and return a path that
3175 * we could modify, but this drop is just an optimization
3176 * and is allowed to miss some leaves.
3177 */
3178 btrfs_release_path(root, path);
3179 found_key.offset++;
3180
3181 /* setup a max key for search_forward */
3182 other_key.offset = (u64)-1;
3183 other_key.type = key.type;
3184 other_key.objectid = key.objectid;
3185
3186 path->keep_locks = 1;
3187 ret = btrfs_search_forward(root, &found_key, &other_key,
3188 path, 0, 0);
3189 path->keep_locks = 0;
3190 if (ret || found_key.objectid != key.objectid ||
3191 found_key.type != key.type) {
3192 ret = 0;
3193 goto out;
3194 }
3195
3196 key.offset = found_key.offset;
3197 btrfs_release_path(root, path);
3198 cond_resched();
3199 goto again;
3200 }
3201
3202 /* we know there's one more slot after us in the tree,
3203 * read that key so we can verify it is also a checksum item
3204 */
3205 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3206
3207 if (found_key.objectid < inode->i_ino)
3208 goto next_key;
3209
3210 if (found_key.type != key.type || found_key.offset < new_size)
3211 goto next_key;
3212
3213 /*
3214 * if the key for the next leaf isn't a csum key from this objectid,
3215 * we can't be sure there aren't good items inside this leaf.
3216 * Bail out
3217 */
3218 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3219 goto out;
3220
3221 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3222 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
3223 /*
3224 * it is safe to delete this leaf, it contains only
3225 * csum items from this inode at an offset >= new_size
3226 */
3227 ret = btrfs_del_leaf(trans, root, path, leaf_start);
3228 BUG_ON(ret);
3229
3230 if (root->ref_cows && leaf_gen < trans->transid) {
3231 ref = btrfs_alloc_leaf_ref(root, 0);
3232 if (ref) {
3233 ref->root_gen = root->root_key.offset;
3234 ref->bytenr = leaf_start;
3235 ref->owner = 0;
3236 ref->generation = leaf_gen;
3237 ref->nritems = 0;
3238
3239 btrfs_sort_leaf_ref(ref);
3240
3241 ret = btrfs_add_leaf_ref(root, ref, 0);
3242 WARN_ON(ret);
3243 btrfs_free_leaf_ref(root, ref);
3244 } else {
3245 WARN_ON(1);
3246 }
3247 }
3248next_key:
3249 btrfs_release_path(root, path);
3250
3251 if (other_key.objectid == inode->i_ino &&
3252 other_key.type == key.type && other_key.offset > key.offset) {
3253 key.offset = other_key.offset;
3254 cond_resched();
3255 goto again;
3256 }
3257 ret = 0;
3258out:
3259 /* fixup any changes we've made to the path */
3260 path->lowest_level = 0;
3261 path->keep_locks = 0;
3262 btrfs_release_path(root, path);
3263 return ret;
3264}
3265
3266#endif
3267
3268/*
3125 * this can truncate away extent items, csum items and directory items.
3126 * It starts at a high offset and removes keys until it can't find
3127 * any higher than new_size
3128 *
3129 * csum items that cross the new i_size are truncated to the new size
3130 * as well.
3131 *
3132 * min_type is the minimum key type to truncate down to. If set to 0, this

--- 18 unchanged lines hidden (view full) ---

3151 int found_extent;
3152 int del_item;
3153 int pending_del_nr = 0;
3154 int pending_del_slot = 0;
3155 int extent_type = -1;
3156 int encoding;
3157 int ret;
3158 int err = 0;
3269 * this can truncate away extent items, csum items and directory items.
3270 * It starts at a high offset and removes keys until it can't find
3271 * any higher than new_size
3272 *
3273 * csum items that cross the new i_size are truncated to the new size
3274 * as well.
3275 *
3276 * min_type is the minimum key type to truncate down to. If set to 0, this

--- 18 unchanged lines hidden (view full) ---

3295 int found_extent;
3296 int del_item;
3297 int pending_del_nr = 0;
3298 int pending_del_slot = 0;
3299 int extent_type = -1;
3300 int encoding;
3301 int ret;
3302 int err = 0;
3159 u64 ino = btrfs_ino(inode);
3160
3161 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3162
3163 if (root->ref_cows || root == root->fs_info->tree_root)
3164 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3165
3303
3304 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3305
3306 if (root->ref_cows || root == root->fs_info->tree_root)
3307 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3308
3166 /*
3167 * This function is also used to drop the items in the log tree before
3168 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3169 * it is used to drop the loged items. So we shouldn't kill the delayed
3170 * items.
3171 */
3172 if (min_type == 0 && root == BTRFS_I(inode)->root)
3173 btrfs_kill_delayed_inode_items(inode);
3174
3175 path = btrfs_alloc_path();
3176 BUG_ON(!path);
3177 path->reada = -1;
3178
3309 path = btrfs_alloc_path();
3310 BUG_ON(!path);
3311 path->reada = -1;
3312
3179 key.objectid = ino;
3313 key.objectid = inode->i_ino;
3180 key.offset = (u64)-1;
3181 key.type = (u8)-1;
3182
3183search_again:
3184 path->leave_spinning = 1;
3185 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3186 if (ret < 0) {
3187 err = ret;

--- 11 unchanged lines hidden (view full) ---

3199
3200 while (1) {
3201 fi = NULL;
3202 leaf = path->nodes[0];
3203 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3204 found_type = btrfs_key_type(&found_key);
3205 encoding = 0;
3206
3314 key.offset = (u64)-1;
3315 key.type = (u8)-1;
3316
3317search_again:
3318 path->leave_spinning = 1;
3319 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3320 if (ret < 0) {
3321 err = ret;

--- 11 unchanged lines hidden (view full) ---

3333
3334 while (1) {
3335 fi = NULL;
3336 leaf = path->nodes[0];
3337 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3338 found_type = btrfs_key_type(&found_key);
3339 encoding = 0;
3340
3207 if (found_key.objectid != ino)
3341 if (found_key.objectid != inode->i_ino)
3208 break;
3209
3210 if (found_type < min_type)
3211 break;
3212
3213 item_end = found_key.offset;
3214 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3215 fi = btrfs_item_ptr(leaf, path->slots[0],

--- 73 unchanged lines hidden (view full) ---

3289 if (root->ref_cows) {
3290 inode_sub_bytes(inode, item_end + 1 -
3291 new_size);
3292 }
3293 size =
3294 btrfs_file_extent_calc_inline_size(size);
3295 ret = btrfs_truncate_item(trans, root, path,
3296 size, 1);
3342 break;
3343
3344 if (found_type < min_type)
3345 break;
3346
3347 item_end = found_key.offset;
3348 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3349 fi = btrfs_item_ptr(leaf, path->slots[0],

--- 73 unchanged lines hidden (view full) ---

3423 if (root->ref_cows) {
3424 inode_sub_bytes(inode, item_end + 1 -
3425 new_size);
3426 }
3427 size =
3428 btrfs_file_extent_calc_inline_size(size);
3429 ret = btrfs_truncate_item(trans, root, path,
3430 size, 1);
3431 BUG_ON(ret);
3297 } else if (root->ref_cows) {
3298 inode_sub_bytes(inode, item_end + 1 -
3299 found_key.offset);
3300 }
3301 }
3302delete:
3303 if (del_item) {
3304 if (!pending_del_nr) {

--- 12 unchanged lines hidden (view full) ---

3317 break;
3318 }
3319 if (found_extent && (root->ref_cows ||
3320 root == root->fs_info->tree_root)) {
3321 btrfs_set_path_blocking(path);
3322 ret = btrfs_free_extent(trans, root, extent_start,
3323 extent_num_bytes, 0,
3324 btrfs_header_owner(leaf),
3432 } else if (root->ref_cows) {
3433 inode_sub_bytes(inode, item_end + 1 -
3434 found_key.offset);
3435 }
3436 }
3437delete:
3438 if (del_item) {
3439 if (!pending_del_nr) {

--- 12 unchanged lines hidden (view full) ---

3452 break;
3453 }
3454 if (found_extent && (root->ref_cows ||
3455 root == root->fs_info->tree_root)) {
3456 btrfs_set_path_blocking(path);
3457 ret = btrfs_free_extent(trans, root, extent_start,
3458 extent_num_bytes, 0,
3459 btrfs_header_owner(leaf),
3325 ino, extent_offset);
3460 inode->i_ino, extent_offset);
3326 BUG_ON(ret);
3327 }
3328
3329 if (found_type == BTRFS_INODE_ITEM_KEY)
3330 break;
3331
3332 if (path->slots[0] == 0 ||
3333 path->slots[0] != pending_del_slot) {
3461 BUG_ON(ret);
3462 }
3463
3464 if (found_type == BTRFS_INODE_ITEM_KEY)
3465 break;
3466
3467 if (path->slots[0] == 0 ||
3468 path->slots[0] != pending_del_slot) {
3334 if (root->ref_cows &&
3335 BTRFS_I(inode)->location.objectid !=
3336 BTRFS_FREE_INO_OBJECTID) {
3469 if (root->ref_cows) {
3337 err = -EAGAIN;
3338 goto out;
3339 }
3340 if (pending_del_nr) {
3341 ret = btrfs_del_items(trans, root, path,
3342 pending_del_slot,
3343 pending_del_nr);
3344 BUG_ON(ret);
3345 pending_del_nr = 0;
3346 }
3470 err = -EAGAIN;
3471 goto out;
3472 }
3473 if (pending_del_nr) {
3474 ret = btrfs_del_items(trans, root, path,
3475 pending_del_slot,
3476 pending_del_nr);
3477 BUG_ON(ret);
3478 pending_del_nr = 0;
3479 }
3347 btrfs_release_path(path);
3480 btrfs_release_path(root, path);
3348 goto search_again;
3349 } else {
3350 path->slots[0]--;
3351 }
3352 }
3353out:
3354 if (pending_del_nr) {
3355 ret = btrfs_del_items(trans, root, path, pending_del_slot,

--- 141 unchanged lines hidden (view full) ---

3497 &cached_state, GFP_NOFS);
3498 btrfs_put_ordered_extent(ordered);
3499 }
3500
3501 cur_offset = hole_start;
3502 while (1) {
3503 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3504 block_end - cur_offset, 0);
3481 goto search_again;
3482 } else {
3483 path->slots[0]--;
3484 }
3485 }
3486out:
3487 if (pending_del_nr) {
3488 ret = btrfs_del_items(trans, root, path, pending_del_slot,

--- 141 unchanged lines hidden (view full) ---

3630 &cached_state, GFP_NOFS);
3631 btrfs_put_ordered_extent(ordered);
3632 }
3633
3634 cur_offset = hole_start;
3635 while (1) {
3636 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3637 block_end - cur_offset, 0);
3505 BUG_ON(IS_ERR_OR_NULL(em));
3638 BUG_ON(IS_ERR(em) || !em);
3506 last_byte = min(extent_map_end(em), block_end);
3507 last_byte = (last_byte + mask) & ~mask;
3508 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3509 u64 hint_byte = 0;
3510 hole_size = last_byte - cur_offset;
3511
3512 trans = btrfs_start_transaction(root, 2);
3513 if (IS_ERR(trans)) {

--- 4 unchanged lines hidden (view full) ---

3518
3519 err = btrfs_drop_extents(trans, inode, cur_offset,
3520 cur_offset + hole_size,
3521 &hint_byte, 1);
3522 if (err)
3523 break;
3524
3525 err = btrfs_insert_file_extent(trans, root,
3639 last_byte = min(extent_map_end(em), block_end);
3640 last_byte = (last_byte + mask) & ~mask;
3641 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3642 u64 hint_byte = 0;
3643 hole_size = last_byte - cur_offset;
3644
3645 trans = btrfs_start_transaction(root, 2);
3646 if (IS_ERR(trans)) {

--- 4 unchanged lines hidden (view full) ---

3651
3652 err = btrfs_drop_extents(trans, inode, cur_offset,
3653 cur_offset + hole_size,
3654 &hint_byte, 1);
3655 if (err)
3656 break;
3657
3658 err = btrfs_insert_file_extent(trans, root,
3526 btrfs_ino(inode), cur_offset, 0,
3659 inode->i_ino, cur_offset, 0,
3527 0, hole_size, 0, hole_size,
3528 0, 0, 0);
3529 if (err)
3530 break;
3531
3532 btrfs_drop_extent_cache(inode, hole_start,
3533 last_byte - 1, 0);
3534

--- 85 unchanged lines hidden (view full) ---

3620 struct btrfs_root *root = BTRFS_I(inode)->root;
3621 unsigned long nr;
3622 int ret;
3623
3624 trace_btrfs_inode_evict(inode);
3625
3626 truncate_inode_pages(&inode->i_data, 0);
3627 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3660 0, hole_size, 0, hole_size,
3661 0, 0, 0);
3662 if (err)
3663 break;
3664
3665 btrfs_drop_extent_cache(inode, hole_start,
3666 last_byte - 1, 0);
3667

--- 85 unchanged lines hidden (view full) ---

3753 struct btrfs_root *root = BTRFS_I(inode)->root;
3754 unsigned long nr;
3755 int ret;
3756
3757 trace_btrfs_inode_evict(inode);
3758
3759 truncate_inode_pages(&inode->i_data, 0);
3760 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3628 is_free_space_inode(root, inode)))
3761 root == root->fs_info->tree_root))
3629 goto no_delete;
3630
3631 if (is_bad_inode(inode)) {
3632 btrfs_orphan_del(NULL, inode);
3633 goto no_delete;
3634 }
3635 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3636 btrfs_wait_ordered_range(inode, 0, (u64)-1);

--- 36 unchanged lines hidden (view full) ---

3673
3674 }
3675
3676 if (ret == 0) {
3677 ret = btrfs_orphan_del(trans, inode);
3678 BUG_ON(ret);
3679 }
3680
3762 goto no_delete;
3763
3764 if (is_bad_inode(inode)) {
3765 btrfs_orphan_del(NULL, inode);
3766 goto no_delete;
3767 }
3768 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3769 btrfs_wait_ordered_range(inode, 0, (u64)-1);

--- 36 unchanged lines hidden (view full) ---

3806
3807 }
3808
3809 if (ret == 0) {
3810 ret = btrfs_orphan_del(trans, inode);
3811 BUG_ON(ret);
3812 }
3813
3681 if (!(root == root->fs_info->tree_root ||
3682 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3683 btrfs_return_ino(root, btrfs_ino(inode));
3684
3685 nr = trans->blocks_used;
3686 btrfs_end_transaction(trans, root);
3687 btrfs_btree_balance_dirty(root, nr);
3688no_delete:
3689 end_writeback(inode);
3690 return;
3691}
3692

--- 9 unchanged lines hidden (view full) ---

3702 struct btrfs_dir_item *di;
3703 struct btrfs_path *path;
3704 struct btrfs_root *root = BTRFS_I(dir)->root;
3705 int ret = 0;
3706
3707 path = btrfs_alloc_path();
3708 BUG_ON(!path);
3709
3814 nr = trans->blocks_used;
3815 btrfs_end_transaction(trans, root);
3816 btrfs_btree_balance_dirty(root, nr);
3817no_delete:
3818 end_writeback(inode);
3819 return;
3820}
3821

--- 9 unchanged lines hidden (view full) ---

3831 struct btrfs_dir_item *di;
3832 struct btrfs_path *path;
3833 struct btrfs_root *root = BTRFS_I(dir)->root;
3834 int ret = 0;
3835
3836 path = btrfs_alloc_path();
3837 BUG_ON(!path);
3838
3710 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3839 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3711 namelen, 0);
3712 if (IS_ERR(di))
3713 ret = PTR_ERR(di);
3714
3840 namelen, 0);
3841 if (IS_ERR(di))
3842 ret = PTR_ERR(di);
3843
3715 if (IS_ERR_OR_NULL(di))
3844 if (!di || IS_ERR(di))
3716 goto out_err;
3717
3718 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3719out:
3720 btrfs_free_path(path);
3721 return ret;
3722out_err:
3723 location->objectid = 0;

--- 31 unchanged lines hidden (view full) ---

3755 if (ret) {
3756 if (ret < 0)
3757 err = ret;
3758 goto out;
3759 }
3760
3761 leaf = path->nodes[0];
3762 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3845 goto out_err;
3846
3847 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3848out:
3849 btrfs_free_path(path);
3850 return ret;
3851out_err:
3852 location->objectid = 0;

--- 31 unchanged lines hidden (view full) ---

3884 if (ret) {
3885 if (ret < 0)
3886 err = ret;
3887 goto out;
3888 }
3889
3890 leaf = path->nodes[0];
3891 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3763 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3892 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3764 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3765 goto out;
3766
3767 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3768 (unsigned long)(ref + 1),
3769 dentry->d_name.len);
3770 if (ret)
3771 goto out;
3772
3893 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3894 goto out;
3895
3896 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3897 (unsigned long)(ref + 1),
3898 dentry->d_name.len);
3899 if (ret)
3900 goto out;
3901
3773 btrfs_release_path(path);
3902 btrfs_release_path(root->fs_info->tree_root, path);
3774
3775 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3776 if (IS_ERR(new_root)) {
3777 err = PTR_ERR(new_root);
3778 goto out;
3779 }
3780
3781 if (btrfs_root_refs(&new_root->root_item) == 0) {

--- 12 unchanged lines hidden (view full) ---

3794}
3795
3796static void inode_tree_add(struct inode *inode)
3797{
3798 struct btrfs_root *root = BTRFS_I(inode)->root;
3799 struct btrfs_inode *entry;
3800 struct rb_node **p;
3801 struct rb_node *parent;
3903
3904 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3905 if (IS_ERR(new_root)) {
3906 err = PTR_ERR(new_root);
3907 goto out;
3908 }
3909
3910 if (btrfs_root_refs(&new_root->root_item) == 0) {

--- 12 unchanged lines hidden (view full) ---

3923}
3924
3925static void inode_tree_add(struct inode *inode)
3926{
3927 struct btrfs_root *root = BTRFS_I(inode)->root;
3928 struct btrfs_inode *entry;
3929 struct rb_node **p;
3930 struct rb_node *parent;
3802 u64 ino = btrfs_ino(inode);
3803again:
3804 p = &root->inode_tree.rb_node;
3805 parent = NULL;
3806
3807 if (inode_unhashed(inode))
3808 return;
3809
3810 spin_lock(&root->inode_lock);
3811 while (*p) {
3812 parent = *p;
3813 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3814
3931again:
3932 p = &root->inode_tree.rb_node;
3933 parent = NULL;
3934
3935 if (inode_unhashed(inode))
3936 return;
3937
3938 spin_lock(&root->inode_lock);
3939 while (*p) {
3940 parent = *p;
3941 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3942
3815 if (ino < btrfs_ino(&entry->vfs_inode))
3943 if (inode->i_ino < entry->vfs_inode.i_ino)
3816 p = &parent->rb_left;
3944 p = &parent->rb_left;
3817 else if (ino > btrfs_ino(&entry->vfs_inode))
3945 else if (inode->i_ino > entry->vfs_inode.i_ino)
3818 p = &parent->rb_right;
3819 else {
3820 WARN_ON(!(entry->vfs_inode.i_state &
3821 (I_WILL_FREE | I_FREEING)));
3822 rb_erase(parent, &root->inode_tree);
3823 RB_CLEAR_NODE(parent);
3824 spin_unlock(&root->inode_lock);
3825 goto again;

--- 47 unchanged lines hidden (view full) ---

3873 spin_lock(&root->inode_lock);
3874again:
3875 node = root->inode_tree.rb_node;
3876 prev = NULL;
3877 while (node) {
3878 prev = node;
3879 entry = rb_entry(node, struct btrfs_inode, rb_node);
3880
3946 p = &parent->rb_right;
3947 else {
3948 WARN_ON(!(entry->vfs_inode.i_state &
3949 (I_WILL_FREE | I_FREEING)));
3950 rb_erase(parent, &root->inode_tree);
3951 RB_CLEAR_NODE(parent);
3952 spin_unlock(&root->inode_lock);
3953 goto again;

--- 47 unchanged lines hidden (view full) ---

4001 spin_lock(&root->inode_lock);
4002again:
4003 node = root->inode_tree.rb_node;
4004 prev = NULL;
4005 while (node) {
4006 prev = node;
4007 entry = rb_entry(node, struct btrfs_inode, rb_node);
4008
3881 if (objectid < btrfs_ino(&entry->vfs_inode))
4009 if (objectid < entry->vfs_inode.i_ino)
3882 node = node->rb_left;
4010 node = node->rb_left;
3883 else if (objectid > btrfs_ino(&entry->vfs_inode))
4011 else if (objectid > entry->vfs_inode.i_ino)
3884 node = node->rb_right;
3885 else
3886 break;
3887 }
3888 if (!node) {
3889 while (prev) {
3890 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4012 node = node->rb_right;
4013 else
4014 break;
4015 }
4016 if (!node) {
4017 while (prev) {
4018 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3891 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4019 if (objectid <= entry->vfs_inode.i_ino) {
3892 node = prev;
3893 break;
3894 }
3895 prev = rb_next(prev);
3896 }
3897 }
3898 while (node) {
3899 entry = rb_entry(node, struct btrfs_inode, rb_node);
4020 node = prev;
4021 break;
4022 }
4023 prev = rb_next(prev);
4024 }
4025 }
4026 while (node) {
4027 entry = rb_entry(node, struct btrfs_inode, rb_node);
3900 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4028 objectid = entry->vfs_inode.i_ino + 1;
3901 inode = igrab(&entry->vfs_inode);
3902 if (inode) {
3903 spin_unlock(&root->inode_lock);
3904 if (atomic_read(&inode->i_count) > 1)
3905 d_prune_aliases(inode);
3906 /*
3907 * btrfs_drop_inode will have it removed from
3908 * the inode cache when its usage count

--- 21 unchanged lines hidden (view full) ---

3930 BTRFS_I(inode)->root = args->root;
3931 btrfs_set_inode_space_info(args->root, inode);
3932 return 0;
3933}
3934
3935static int btrfs_find_actor(struct inode *inode, void *opaque)
3936{
3937 struct btrfs_iget_args *args = opaque;
4029 inode = igrab(&entry->vfs_inode);
4030 if (inode) {
4031 spin_unlock(&root->inode_lock);
4032 if (atomic_read(&inode->i_count) > 1)
4033 d_prune_aliases(inode);
4034 /*
4035 * btrfs_drop_inode will have it removed from
4036 * the inode cache when its usage count

--- 21 unchanged lines hidden (view full) ---

4058 BTRFS_I(inode)->root = args->root;
4059 btrfs_set_inode_space_info(args->root, inode);
4060 return 0;
4061}
4062
4063static int btrfs_find_actor(struct inode *inode, void *opaque)
4064{
4065 struct btrfs_iget_args *args = opaque;
3938 return args->ino == btrfs_ino(inode) &&
4066 return args->ino == inode->i_ino &&
3939 args->root == BTRFS_I(inode)->root;
3940}
3941
3942static struct inode *btrfs_iget_locked(struct super_block *s,
3943 u64 objectid,
3944 struct btrfs_root *root)
3945{
3946 struct inode *inode;

--- 128 unchanged lines hidden (view full) ---

4075
4076 inode = btrfs_lookup_dentry(dir, dentry);
4077 if (IS_ERR(inode))
4078 return ERR_CAST(inode);
4079
4080 return d_splice_alias(inode, dentry);
4081}
4082
4067 args->root == BTRFS_I(inode)->root;
4068}
4069
4070static struct inode *btrfs_iget_locked(struct super_block *s,
4071 u64 objectid,
4072 struct btrfs_root *root)
4073{
4074 struct inode *inode;

--- 128 unchanged lines hidden (view full) ---

4203
4204 inode = btrfs_lookup_dentry(dir, dentry);
4205 if (IS_ERR(inode))
4206 return ERR_CAST(inode);
4207
4208 return d_splice_alias(inode, dentry);
4209}
4210
4083unsigned char btrfs_filetype_table[] = {
4211static unsigned char btrfs_filetype_table[] = {
4084 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4085};
4086
4087static int btrfs_real_readdir(struct file *filp, void *dirent,
4088 filldir_t filldir)
4089{
4090 struct inode *inode = filp->f_dentry->d_inode;
4091 struct btrfs_root *root = BTRFS_I(inode)->root;
4092 struct btrfs_item *item;
4093 struct btrfs_dir_item *di;
4094 struct btrfs_key key;
4095 struct btrfs_key found_key;
4096 struct btrfs_path *path;
4212 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4213};
4214
4215static int btrfs_real_readdir(struct file *filp, void *dirent,
4216 filldir_t filldir)
4217{
4218 struct inode *inode = filp->f_dentry->d_inode;
4219 struct btrfs_root *root = BTRFS_I(inode)->root;
4220 struct btrfs_item *item;
4221 struct btrfs_dir_item *di;
4222 struct btrfs_key key;
4223 struct btrfs_key found_key;
4224 struct btrfs_path *path;
4097 struct list_head ins_list;
4098 struct list_head del_list;
4099 int ret;
4100 struct extent_buffer *leaf;
4101 int slot;
4102 unsigned char d_type;
4103 int over = 0;
4104 u32 di_cur;
4105 u32 di_total;
4106 u32 di_len;
4107 int key_type = BTRFS_DIR_INDEX_KEY;
4108 char tmp_name[32];
4109 char *name_ptr;
4110 int name_len;
4225 int ret;
4226 struct extent_buffer *leaf;
4227 int slot;
4228 unsigned char d_type;
4229 int over = 0;
4230 u32 di_cur;
4231 u32 di_total;
4232 u32 di_len;
4233 int key_type = BTRFS_DIR_INDEX_KEY;
4234 char tmp_name[32];
4235 char *name_ptr;
4236 int name_len;
4111 int is_curr = 0; /* filp->f_pos points to the current index? */
4112
4113 /* FIXME, use a real flag for deciding about the key type */
4114 if (root->fs_info->tree_root == root)
4115 key_type = BTRFS_DIR_ITEM_KEY;
4116
4117 /* special case for "." */
4118 if (filp->f_pos == 0) {
4237
4238 /* FIXME, use a real flag for deciding about the key type */
4239 if (root->fs_info->tree_root == root)
4240 key_type = BTRFS_DIR_ITEM_KEY;
4241
4242 /* special case for "." */
4243 if (filp->f_pos == 0) {
4119 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4244 over = filldir(dirent, ".", 1,
4245 1, inode->i_ino,
4246 DT_DIR);
4120 if (over)
4121 return 0;
4122 filp->f_pos = 1;
4123 }
4124 /* special case for .., just use the back ref */
4125 if (filp->f_pos == 1) {
4126 u64 pino = parent_ino(filp->f_path.dentry);
4127 over = filldir(dirent, "..", 2,
4128 2, pino, DT_DIR);
4129 if (over)
4130 return 0;
4131 filp->f_pos = 2;
4132 }
4133 path = btrfs_alloc_path();
4247 if (over)
4248 return 0;
4249 filp->f_pos = 1;
4250 }
4251 /* special case for .., just use the back ref */
4252 if (filp->f_pos == 1) {
4253 u64 pino = parent_ino(filp->f_path.dentry);
4254 over = filldir(dirent, "..", 2,
4255 2, pino, DT_DIR);
4256 if (over)
4257 return 0;
4258 filp->f_pos = 2;
4259 }
4260 path = btrfs_alloc_path();
4134 if (!path)
4135 return -ENOMEM;
4136 path->reada = 2;
4137
4261 path->reada = 2;
4262
4138 if (key_type == BTRFS_DIR_INDEX_KEY) {
4139 INIT_LIST_HEAD(&ins_list);
4140 INIT_LIST_HEAD(&del_list);
4141 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4142 }
4143
4144 btrfs_set_key_type(&key, key_type);
4145 key.offset = filp->f_pos;
4263 btrfs_set_key_type(&key, key_type);
4264 key.offset = filp->f_pos;
4146 key.objectid = btrfs_ino(inode);
4265 key.objectid = inode->i_ino;
4147
4148 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4149 if (ret < 0)
4150 goto err;
4151
4152 while (1) {
4153 leaf = path->nodes[0];
4154 slot = path->slots[0];

--- 10 unchanged lines hidden (view full) ---

4165 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4166
4167 if (found_key.objectid != key.objectid)
4168 break;
4169 if (btrfs_key_type(&found_key) != key_type)
4170 break;
4171 if (found_key.offset < filp->f_pos)
4172 goto next;
4266
4267 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4268 if (ret < 0)
4269 goto err;
4270
4271 while (1) {
4272 leaf = path->nodes[0];
4273 slot = path->slots[0];

--- 10 unchanged lines hidden (view full) ---

4284 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4285
4286 if (found_key.objectid != key.objectid)
4287 break;
4288 if (btrfs_key_type(&found_key) != key_type)
4289 break;
4290 if (found_key.offset < filp->f_pos)
4291 goto next;
4173 if (key_type == BTRFS_DIR_INDEX_KEY &&
4174 btrfs_should_delete_dir_index(&del_list,
4175 found_key.offset))
4176 goto next;
4177
4178 filp->f_pos = found_key.offset;
4292
4293 filp->f_pos = found_key.offset;
4179 is_curr = 1;
4180
4181 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4182 di_cur = 0;
4183 di_total = btrfs_item_size(leaf, item);
4184
4185 while (di_cur < di_total) {
4186 struct btrfs_key location;
4187

--- 38 unchanged lines hidden (view full) ---

4226 btrfs_dir_data_len(leaf, di) + sizeof(*di);
4227 di_cur += di_len;
4228 di = (struct btrfs_dir_item *)((char *)di + di_len);
4229 }
4230next:
4231 path->slots[0]++;
4232 }
4233
4294
4295 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4296 di_cur = 0;
4297 di_total = btrfs_item_size(leaf, item);
4298
4299 while (di_cur < di_total) {
4300 struct btrfs_key location;
4301

--- 38 unchanged lines hidden (view full) ---

4340 btrfs_dir_data_len(leaf, di) + sizeof(*di);
4341 di_cur += di_len;
4342 di = (struct btrfs_dir_item *)((char *)di + di_len);
4343 }
4344next:
4345 path->slots[0]++;
4346 }
4347
4234 if (key_type == BTRFS_DIR_INDEX_KEY) {
4235 if (is_curr)
4236 filp->f_pos++;
4237 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4238 &ins_list);
4239 if (ret)
4240 goto nopos;
4241 }
4242
4243 /* Reached end of directory/root. Bump pos past the last item. */
4244 if (key_type == BTRFS_DIR_INDEX_KEY)
4245 /*
4246 * 32-bit glibc will use getdents64, but then strtol -
4247 * so the last number we can serve is this.
4248 */
4249 filp->f_pos = 0x7fffffff;
4250 else
4251 filp->f_pos++;
4252nopos:
4253 ret = 0;
4254err:
4348 /* Reached end of directory/root. Bump pos past the last item. */
4349 if (key_type == BTRFS_DIR_INDEX_KEY)
4350 /*
4351 * 32-bit glibc will use getdents64, but then strtol -
4352 * so the last number we can serve is this.
4353 */
4354 filp->f_pos = 0x7fffffff;
4355 else
4356 filp->f_pos++;
4357nopos:
4358 ret = 0;
4359err:
4255 if (key_type == BTRFS_DIR_INDEX_KEY)
4256 btrfs_put_delayed_items(&ins_list, &del_list);
4257 btrfs_free_path(path);
4258 return ret;
4259}
4260
4261int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4262{
4263 struct btrfs_root *root = BTRFS_I(inode)->root;
4264 struct btrfs_trans_handle *trans;
4265 int ret = 0;
4266 bool nolock = false;
4267
4268 if (BTRFS_I(inode)->dummy_inode)
4269 return 0;
4270
4271 smp_mb();
4360 btrfs_free_path(path);
4361 return ret;
4362}
4363
4364int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4365{
4366 struct btrfs_root *root = BTRFS_I(inode)->root;
4367 struct btrfs_trans_handle *trans;
4368 int ret = 0;
4369 bool nolock = false;
4370
4371 if (BTRFS_I(inode)->dummy_inode)
4372 return 0;
4373
4374 smp_mb();
4272 if (root->fs_info->closing && is_free_space_inode(root, inode))
4273 nolock = true;
4375 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4274
4275 if (wbc->sync_mode == WB_SYNC_ALL) {
4276 if (nolock)
4277 trans = btrfs_join_transaction_nolock(root, 1);
4278 else
4279 trans = btrfs_join_transaction(root, 1);
4280 if (IS_ERR(trans))
4281 return PTR_ERR(trans);

--- 7 unchanged lines hidden (view full) ---

4289}
4290
4291/*
4292 * This is somewhat expensive, updating the tree every time the
4293 * inode changes. But, it is most likely to find the inode in cache.
4294 * FIXME, needs more benchmarking...there are no reasons other than performance
4295 * to keep or drop this code.
4296 */
4376
4377 if (wbc->sync_mode == WB_SYNC_ALL) {
4378 if (nolock)
4379 trans = btrfs_join_transaction_nolock(root, 1);
4380 else
4381 trans = btrfs_join_transaction(root, 1);
4382 if (IS_ERR(trans))
4383 return PTR_ERR(trans);

--- 7 unchanged lines hidden (view full) ---

4391}
4392
4393/*
4394 * This is somewhat expensive, updating the tree every time the
4395 * inode changes. But, it is most likely to find the inode in cache.
4396 * FIXME, needs more benchmarking...there are no reasons other than performance
4397 * to keep or drop this code.
4398 */
4297void btrfs_dirty_inode(struct inode *inode)
4399void btrfs_dirty_inode(struct inode *inode, int flags)
4298{
4299 struct btrfs_root *root = BTRFS_I(inode)->root;
4300 struct btrfs_trans_handle *trans;
4301 int ret;
4302
4303 if (BTRFS_I(inode)->dummy_inode)
4304 return;
4305
4306 trans = btrfs_join_transaction(root, 1);
4307 BUG_ON(IS_ERR(trans));
4308 btrfs_set_trans_block_group(trans, inode);
4309
4310 ret = btrfs_update_inode(trans, root, inode);
4311 if (ret && ret == -ENOSPC) {
4312 /* whoops, lets try again with the full transaction */
4313 btrfs_end_transaction(trans, root);
4314 trans = btrfs_start_transaction(root, 1);
4315 if (IS_ERR(trans)) {
4400{
4401 struct btrfs_root *root = BTRFS_I(inode)->root;
4402 struct btrfs_trans_handle *trans;
4403 int ret;
4404
4405 if (BTRFS_I(inode)->dummy_inode)
4406 return;
4407
4408 trans = btrfs_join_transaction(root, 1);
4409 BUG_ON(IS_ERR(trans));
4410 btrfs_set_trans_block_group(trans, inode);
4411
4412 ret = btrfs_update_inode(trans, root, inode);
4413 if (ret && ret == -ENOSPC) {
4414 /* whoops, lets try again with the full transaction */
4415 btrfs_end_transaction(trans, root);
4416 trans = btrfs_start_transaction(root, 1);
4417 if (IS_ERR(trans)) {
4316 printk_ratelimited(KERN_ERR "btrfs: fail to "
4317 "dirty inode %llu error %ld\n",
4318 (unsigned long long)btrfs_ino(inode),
4319 PTR_ERR(trans));
4418 if (printk_ratelimit()) {
4419 printk(KERN_ERR "btrfs: fail to "
4420 "dirty inode %lu error %ld\n",
4421 inode->i_ino, PTR_ERR(trans));
4422 }
4320 return;
4321 }
4322 btrfs_set_trans_block_group(trans, inode);
4323
4324 ret = btrfs_update_inode(trans, root, inode);
4325 if (ret) {
4423 return;
4424 }
4425 btrfs_set_trans_block_group(trans, inode);
4426
4427 ret = btrfs_update_inode(trans, root, inode);
4428 if (ret) {
4326 printk_ratelimited(KERN_ERR "btrfs: fail to "
4327 "dirty inode %llu error %d\n",
4328 (unsigned long long)btrfs_ino(inode),
4329 ret);
4429 if (printk_ratelimit()) {
4430 printk(KERN_ERR "btrfs: fail to "
4431 "dirty inode %lu error %d\n",
4432 inode->i_ino, ret);
4433 }
4330 }
4331 }
4332 btrfs_end_transaction(trans, root);
4434 }
4435 }
4436 btrfs_end_transaction(trans, root);
4333 if (BTRFS_I(inode)->delayed_node)
4334 btrfs_balance_delayed_items(root);
4335}
4336
4337/*
4338 * find the highest existing sequence number in a directory
4339 * and then set the in-memory index_cnt variable to reflect
4340 * free sequence numbers
4341 */
4342static int btrfs_set_inode_index_count(struct inode *inode)
4343{
4344 struct btrfs_root *root = BTRFS_I(inode)->root;
4345 struct btrfs_key key, found_key;
4346 struct btrfs_path *path;
4347 struct extent_buffer *leaf;
4348 int ret;
4349
4437}
4438
4439/*
4440 * find the highest existing sequence number in a directory
4441 * and then set the in-memory index_cnt variable to reflect
4442 * free sequence numbers
4443 */
4444static int btrfs_set_inode_index_count(struct inode *inode)
4445{
4446 struct btrfs_root *root = BTRFS_I(inode)->root;
4447 struct btrfs_key key, found_key;
4448 struct btrfs_path *path;
4449 struct extent_buffer *leaf;
4450 int ret;
4451
4350 key.objectid = btrfs_ino(inode);
4452 key.objectid = inode->i_ino;
4351 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4352 key.offset = (u64)-1;
4353
4354 path = btrfs_alloc_path();
4355 if (!path)
4356 return -ENOMEM;
4357
4358 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);

--- 15 unchanged lines hidden (view full) ---

4374 goto out;
4375 }
4376
4377 path->slots[0]--;
4378
4379 leaf = path->nodes[0];
4380 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4381
4453 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4454 key.offset = (u64)-1;
4455
4456 path = btrfs_alloc_path();
4457 if (!path)
4458 return -ENOMEM;
4459
4460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);

--- 15 unchanged lines hidden (view full) ---

4476 goto out;
4477 }
4478
4479 path->slots[0]--;
4480
4481 leaf = path->nodes[0];
4482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4483
4382 if (found_key.objectid != btrfs_ino(inode) ||
4484 if (found_key.objectid != inode->i_ino ||
4383 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4384 BTRFS_I(inode)->index_cnt = 2;
4385 goto out;
4386 }
4387
4388 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4389out:
4390 btrfs_free_path(path);

--- 4 unchanged lines hidden (view full) ---

4395 * helper to find a free sequence number in a given directory. This current
4396 * code is very simple, later versions will do smarter things in the btree
4397 */
4398int btrfs_set_inode_index(struct inode *dir, u64 *index)
4399{
4400 int ret = 0;
4401
4402 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4485 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4486 BTRFS_I(inode)->index_cnt = 2;
4487 goto out;
4488 }
4489
4490 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4491out:
4492 btrfs_free_path(path);

--- 4 unchanged lines hidden (view full) ---

4497 * helper to find a free sequence number in a given directory. This current
4498 * code is very simple, later versions will do smarter things in the btree
4499 */
4500int btrfs_set_inode_index(struct inode *dir, u64 *index)
4501{
4502 int ret = 0;
4503
4504 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4403 ret = btrfs_inode_delayed_dir_index_count(dir);
4404 if (ret) {
4405 ret = btrfs_set_inode_index_count(dir);
4406 if (ret)
4407 return ret;
4408 }
4505 ret = btrfs_set_inode_index_count(dir);
4506 if (ret)
4507 return ret;
4409 }
4410
4411 *index = BTRFS_I(dir)->index_cnt;
4412 BTRFS_I(dir)->index_cnt++;
4413
4414 return ret;
4415}
4416

--- 19 unchanged lines hidden (view full) ---

4436 BUG_ON(!path);
4437
4438 inode = new_inode(root->fs_info->sb);
4439 if (!inode) {
4440 btrfs_free_path(path);
4441 return ERR_PTR(-ENOMEM);
4442 }
4443
4508 }
4509
4510 *index = BTRFS_I(dir)->index_cnt;
4511 BTRFS_I(dir)->index_cnt++;
4512
4513 return ret;
4514}
4515

--- 19 unchanged lines hidden (view full) ---

4535 BUG_ON(!path);
4536
4537 inode = new_inode(root->fs_info->sb);
4538 if (!inode) {
4539 btrfs_free_path(path);
4540 return ERR_PTR(-ENOMEM);
4541 }
4542
4444 /*
4445 * we have to initialize this early, so we can reclaim the inode
4446 * number if we fail afterwards in this function.
4447 */
4448 inode->i_ino = objectid;
4449
4450 if (dir) {
4451 trace_btrfs_inode_request(dir);
4452
4453 ret = btrfs_set_inode_index(dir, index);
4454 if (ret) {
4455 btrfs_free_path(path);
4456 iput(inode);
4457 return ERR_PTR(ret);

--- 29 unchanged lines hidden (view full) ---

4487 sizes[1] = name_len + sizeof(*ref);
4488
4489 path->leave_spinning = 1;
4490 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4491 if (ret != 0)
4492 goto fail;
4493
4494 inode_init_owner(inode, dir, mode);
4543 if (dir) {
4544 trace_btrfs_inode_request(dir);
4545
4546 ret = btrfs_set_inode_index(dir, index);
4547 if (ret) {
4548 btrfs_free_path(path);
4549 iput(inode);
4550 return ERR_PTR(ret);

--- 29 unchanged lines hidden (view full) ---

4580 sizes[1] = name_len + sizeof(*ref);
4581
4582 path->leave_spinning = 1;
4583 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4584 if (ret != 0)
4585 goto fail;
4586
4587 inode_init_owner(inode, dir, mode);
4588 inode->i_ino = objectid;
4495 inode_set_bytes(inode, 0);
4496 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4497 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4498 struct btrfs_inode_item);
4499 fill_inode_item(trans, path->nodes[0], inode_item, inode);
4500
4501 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4502 struct btrfs_inode_ref);

--- 47 unchanged lines hidden (view full) ---

4550 */
4551int btrfs_add_link(struct btrfs_trans_handle *trans,
4552 struct inode *parent_inode, struct inode *inode,
4553 const char *name, int name_len, int add_backref, u64 index)
4554{
4555 int ret = 0;
4556 struct btrfs_key key;
4557 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4589 inode_set_bytes(inode, 0);
4590 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4591 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4592 struct btrfs_inode_item);
4593 fill_inode_item(trans, path->nodes[0], inode_item, inode);
4594
4595 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4596 struct btrfs_inode_ref);

--- 47 unchanged lines hidden (view full) ---

4644 */
4645int btrfs_add_link(struct btrfs_trans_handle *trans,
4646 struct inode *parent_inode, struct inode *inode,
4647 const char *name, int name_len, int add_backref, u64 index)
4648{
4649 int ret = 0;
4650 struct btrfs_key key;
4651 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4558 u64 ino = btrfs_ino(inode);
4559 u64 parent_ino = btrfs_ino(parent_inode);
4560
4652
4561 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4653 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4562 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4563 } else {
4654 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4655 } else {
4564 key.objectid = ino;
4656 key.objectid = inode->i_ino;
4565 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4566 key.offset = 0;
4567 }
4568
4657 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4658 key.offset = 0;
4659 }
4660
4569 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4661 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4570 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4571 key.objectid, root->root_key.objectid,
4662 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4663 key.objectid, root->root_key.objectid,
4572 parent_ino, index, name, name_len);
4664 parent_inode->i_ino,
4665 index, name, name_len);
4573 } else if (add_backref) {
4666 } else if (add_backref) {
4574 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4575 parent_ino, index);
4667 ret = btrfs_insert_inode_ref(trans, root,
4668 name, name_len, inode->i_ino,
4669 parent_inode->i_ino, index);
4576 }
4577
4578 if (ret == 0) {
4579 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4670 }
4671
4672 if (ret == 0) {
4673 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4580 parent_inode, &key,
4674 parent_inode->i_ino, &key,
4581 btrfs_inode_type(inode), index);
4582 BUG_ON(ret);
4583
4584 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4585 name_len * 2);
4586 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4587 ret = btrfs_update_inode(trans, root, parent_inode);
4588 }

--- 26 unchanged lines hidden (view full) ---

4615 int drop_inode = 0;
4616 u64 objectid;
4617 unsigned long nr = 0;
4618 u64 index = 0;
4619
4620 if (!new_valid_dev(rdev))
4621 return -EINVAL;
4622
4675 btrfs_inode_type(inode), index);
4676 BUG_ON(ret);
4677
4678 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4679 name_len * 2);
4680 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4681 ret = btrfs_update_inode(trans, root, parent_inode);
4682 }

--- 26 unchanged lines hidden (view full) ---

4709 int drop_inode = 0;
4710 u64 objectid;
4711 unsigned long nr = 0;
4712 u64 index = 0;
4713
4714 if (!new_valid_dev(rdev))
4715 return -EINVAL;
4716
4717 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4718 if (err)
4719 return err;
4720
4623 /*
4624 * 2 for inode item and ref
4625 * 2 for dir items
4626 * 1 for xattr if selinux is on
4627 */
4628 trans = btrfs_start_transaction(root, 5);
4629 if (IS_ERR(trans))
4630 return PTR_ERR(trans);
4631
4632 btrfs_set_trans_block_group(trans, dir);
4633
4721 /*
4722 * 2 for inode item and ref
4723 * 2 for dir items
4724 * 1 for xattr if selinux is on
4725 */
4726 trans = btrfs_start_transaction(root, 5);
4727 if (IS_ERR(trans))
4728 return PTR_ERR(trans);
4729
4730 btrfs_set_trans_block_group(trans, dir);
4731
4634 err = btrfs_find_free_ino(root, &objectid);
4635 if (err)
4636 goto out_unlock;
4637
4638 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4732 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4639 dentry->d_name.len, btrfs_ino(dir), objectid,
4733 dentry->d_name.len, dir->i_ino, objectid,
4640 BTRFS_I(dir)->block_group, mode, &index);
4641 if (IS_ERR(inode)) {
4642 err = PTR_ERR(inode);
4643 goto out_unlock;
4644 }
4645
4646 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4647 if (err) {

--- 30 unchanged lines hidden (view full) ---

4678 struct btrfs_root *root = BTRFS_I(dir)->root;
4679 struct inode *inode = NULL;
4680 int drop_inode = 0;
4681 int err;
4682 unsigned long nr = 0;
4683 u64 objectid;
4684 u64 index = 0;
4685
4734 BTRFS_I(dir)->block_group, mode, &index);
4735 if (IS_ERR(inode)) {
4736 err = PTR_ERR(inode);
4737 goto out_unlock;
4738 }
4739
4740 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4741 if (err) {

--- 30 unchanged lines hidden (view full) ---

4772 struct btrfs_root *root = BTRFS_I(dir)->root;
4773 struct inode *inode = NULL;
4774 int drop_inode = 0;
4775 int err;
4776 unsigned long nr = 0;
4777 u64 objectid;
4778 u64 index = 0;
4779
4780 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4781 if (err)
4782 return err;
4686 /*
4687 * 2 for inode item and ref
4688 * 2 for dir items
4689 * 1 for xattr if selinux is on
4690 */
4691 trans = btrfs_start_transaction(root, 5);
4692 if (IS_ERR(trans))
4693 return PTR_ERR(trans);
4694
4695 btrfs_set_trans_block_group(trans, dir);
4696
4783 /*
4784 * 2 for inode item and ref
4785 * 2 for dir items
4786 * 1 for xattr if selinux is on
4787 */
4788 trans = btrfs_start_transaction(root, 5);
4789 if (IS_ERR(trans))
4790 return PTR_ERR(trans);
4791
4792 btrfs_set_trans_block_group(trans, dir);
4793
4697 err = btrfs_find_free_ino(root, &objectid);
4698 if (err)
4699 goto out_unlock;
4700
4701 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4794 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4702 dentry->d_name.len, btrfs_ino(dir), objectid,
4795 dentry->d_name.len, dir->i_ino, objectid,
4703 BTRFS_I(dir)->block_group, mode, &index);
4704 if (IS_ERR(inode)) {
4705 err = PTR_ERR(inode);
4706 goto out_unlock;
4707 }
4708
4709 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4710 if (err) {

--- 94 unchanged lines hidden (view full) ---

4805 struct btrfs_trans_handle *trans;
4806 struct btrfs_root *root = BTRFS_I(dir)->root;
4807 int err = 0;
4808 int drop_on_err = 0;
4809 u64 objectid = 0;
4810 u64 index = 0;
4811 unsigned long nr = 1;
4812
4796 BTRFS_I(dir)->block_group, mode, &index);
4797 if (IS_ERR(inode)) {
4798 err = PTR_ERR(inode);
4799 goto out_unlock;
4800 }
4801
4802 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4803 if (err) {

--- 94 unchanged lines hidden (view full) ---

4898 struct btrfs_trans_handle *trans;
4899 struct btrfs_root *root = BTRFS_I(dir)->root;
4900 int err = 0;
4901 int drop_on_err = 0;
4902 u64 objectid = 0;
4903 u64 index = 0;
4904 unsigned long nr = 1;
4905
4906 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4907 if (err)
4908 return err;
4909
4813 /*
4814 * 2 items for inode and ref
4815 * 2 items for dir items
4816 * 1 for xattr if selinux is on
4817 */
4818 trans = btrfs_start_transaction(root, 5);
4819 if (IS_ERR(trans))
4820 return PTR_ERR(trans);
4821 btrfs_set_trans_block_group(trans, dir);
4822
4910 /*
4911 * 2 items for inode and ref
4912 * 2 items for dir items
4913 * 1 for xattr if selinux is on
4914 */
4915 trans = btrfs_start_transaction(root, 5);
4916 if (IS_ERR(trans))
4917 return PTR_ERR(trans);
4918 btrfs_set_trans_block_group(trans, dir);
4919
4823 err = btrfs_find_free_ino(root, &objectid);
4824 if (err)
4825 goto out_fail;
4826
4827 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4920 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4828 dentry->d_name.len, btrfs_ino(dir), objectid,
4921 dentry->d_name.len, dir->i_ino, objectid,
4829 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4830 &index);
4831 if (IS_ERR(inode)) {
4832 err = PTR_ERR(inode);
4833 goto out_fail;
4834 }
4835
4836 drop_on_err = 1;

--- 106 unchanged lines hidden (view full) ---

4943 size_t pg_offset, u64 start, u64 len,
4944 int create)
4945{
4946 int ret;
4947 int err = 0;
4948 u64 bytenr;
4949 u64 extent_start = 0;
4950 u64 extent_end = 0;
4922 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4923 &index);
4924 if (IS_ERR(inode)) {
4925 err = PTR_ERR(inode);
4926 goto out_fail;
4927 }
4928
4929 drop_on_err = 1;

--- 106 unchanged lines hidden (view full) ---

5036 size_t pg_offset, u64 start, u64 len,
5037 int create)
5038{
5039 int ret;
5040 int err = 0;
5041 u64 bytenr;
5042 u64 extent_start = 0;
5043 u64 extent_end = 0;
4951 u64 objectid = btrfs_ino(inode);
5044 u64 objectid = inode->i_ino;
4952 u32 found_type;
4953 struct btrfs_path *path = NULL;
4954 struct btrfs_root *root = BTRFS_I(inode)->root;
4955 struct btrfs_file_extent_item *item;
4956 struct extent_buffer *leaf;
4957 struct btrfs_key found_key;
4958 struct extent_map *em = NULL;
4959 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;

--- 11 unchanged lines hidden (view full) ---

4971 if (em) {
4972 if (em->start > start || em->start + em->len <= start)
4973 free_extent_map(em);
4974 else if (em->block_start == EXTENT_MAP_INLINE && page)
4975 free_extent_map(em);
4976 else
4977 goto out;
4978 }
5045 u32 found_type;
5046 struct btrfs_path *path = NULL;
5047 struct btrfs_root *root = BTRFS_I(inode)->root;
5048 struct btrfs_file_extent_item *item;
5049 struct extent_buffer *leaf;
5050 struct btrfs_key found_key;
5051 struct extent_map *em = NULL;
5052 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;

--- 11 unchanged lines hidden (view full) ---

5064 if (em) {
5065 if (em->start > start || em->start + em->len <= start)
5066 free_extent_map(em);
5067 else if (em->block_start == EXTENT_MAP_INLINE && page)
5068 free_extent_map(em);
5069 else
5070 goto out;
5071 }
4979 em = alloc_extent_map();
5072 em = alloc_extent_map(GFP_NOFS);
4980 if (!em) {
4981 err = -ENOMEM;
4982 goto out;
4983 }
4984 em->bdev = root->fs_info->fs_devices->latest_bdev;
4985 em->start = EXTENT_MAP_HOLE;
4986 em->orig_start = EXTENT_MAP_HOLE;
4987 em->len = (u64)-1;

--- 137 unchanged lines hidden (view full) ---

5125 }
5126 flush_dcache_page(page);
5127 } else if (create && PageUptodate(page)) {
5128 WARN_ON(1);
5129 if (!trans) {
5130 kunmap(page);
5131 free_extent_map(em);
5132 em = NULL;
5073 if (!em) {
5074 err = -ENOMEM;
5075 goto out;
5076 }
5077 em->bdev = root->fs_info->fs_devices->latest_bdev;
5078 em->start = EXTENT_MAP_HOLE;
5079 em->orig_start = EXTENT_MAP_HOLE;
5080 em->len = (u64)-1;

--- 137 unchanged lines hidden (view full) ---

5218 }
5219 flush_dcache_page(page);
5220 } else if (create && PageUptodate(page)) {
5221 WARN_ON(1);
5222 if (!trans) {
5223 kunmap(page);
5224 free_extent_map(em);
5225 em = NULL;
5133 btrfs_release_path(path);
5226 btrfs_release_path(root, path);
5134 trans = btrfs_join_transaction(root, 1);
5135 if (IS_ERR(trans))
5136 return ERR_CAST(trans);
5137 goto again;
5138 }
5139 map = kmap(page);
5140 write_extent_buffer(leaf, map + pg_offset, ptr,
5141 copy_size);

--- 9 unchanged lines hidden (view full) ---

5151 }
5152not_found:
5153 em->start = start;
5154 em->len = len;
5155not_found_em:
5156 em->block_start = EXTENT_MAP_HOLE;
5157 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5158insert:
5227 trans = btrfs_join_transaction(root, 1);
5228 if (IS_ERR(trans))
5229 return ERR_CAST(trans);
5230 goto again;
5231 }
5232 map = kmap(page);
5233 write_extent_buffer(leaf, map + pg_offset, ptr,
5234 copy_size);

--- 9 unchanged lines hidden (view full) ---

5244 }
5245not_found:
5246 em->start = start;
5247 em->len = len;
5248not_found_em:
5249 em->block_start = EXTENT_MAP_HOLE;
5250 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5251insert:
5159 btrfs_release_path(path);
5252 btrfs_release_path(root, path);
5160 if (em->start > start || extent_map_end(em) <= start) {
5161 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5162 "[%llu %llu]\n", (unsigned long long)em->start,
5163 (unsigned long long)em->len,
5164 (unsigned long long)start,
5165 (unsigned long long)len);
5166 err = -EIO;
5167 goto out;

--- 116 unchanged lines hidden (view full) ---

5284 */
5285 range_start = max(start,range_start);
5286 found = found_end - range_start;
5287
5288 if (found > 0) {
5289 u64 hole_start = start;
5290 u64 hole_len = len;
5291
5253 if (em->start > start || extent_map_end(em) <= start) {
5254 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5255 "[%llu %llu]\n", (unsigned long long)em->start,
5256 (unsigned long long)em->len,
5257 (unsigned long long)start,
5258 (unsigned long long)len);
5259 err = -EIO;
5260 goto out;

--- 116 unchanged lines hidden (view full) ---

5377 */
5378 range_start = max(start,range_start);
5379 found = found_end - range_start;
5380
5381 if (found > 0) {
5382 u64 hole_start = start;
5383 u64 hole_len = len;
5384
5292 em = alloc_extent_map();
5385 em = alloc_extent_map(GFP_NOFS);
5293 if (!em) {
5294 err = -ENOMEM;
5295 goto out;
5296 }
5297 /*
5298 * when btrfs_get_extent can't find anything it
5299 * returns one huge hole
5300 *

--- 73 unchanged lines hidden (view full) ---

5374 insert = true;
5375 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5376 }
5377
5378 trans = btrfs_join_transaction(root, 0);
5379 if (IS_ERR(trans))
5380 return ERR_CAST(trans);
5381
5386 if (!em) {
5387 err = -ENOMEM;
5388 goto out;
5389 }
5390 /*
5391 * when btrfs_get_extent can't find anything it
5392 * returns one huge hole
5393 *

--- 73 unchanged lines hidden (view full) ---

5467 insert = true;
5468 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5469 }
5470
5471 trans = btrfs_join_transaction(root, 0);
5472 if (IS_ERR(trans))
5473 return ERR_CAST(trans);
5474
5382 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5383 btrfs_add_inode_defrag(trans, inode);
5384
5385 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5386
5387 alloc_hint = get_extent_allocation_hint(inode, start, len);
5388 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5389 alloc_hint, (u64)-1, &ins, 1);
5390 if (ret) {
5391 em = ERR_PTR(ret);
5392 goto out;
5393 }
5394
5395 if (!em) {
5475 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5476
5477 alloc_hint = get_extent_allocation_hint(inode, start, len);
5478 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5479 alloc_hint, (u64)-1, &ins, 1);
5480 if (ret) {
5481 em = ERR_PTR(ret);
5482 goto out;
5483 }
5484
5485 if (!em) {
5396 em = alloc_extent_map();
5486 em = alloc_extent_map(GFP_NOFS);
5397 if (!em) {
5398 em = ERR_PTR(-ENOMEM);
5399 goto out;
5400 }
5401 }
5402
5403 em->start = start;
5404 em->orig_start = em->start;

--- 49 unchanged lines hidden (view full) ---

5454 u64 num_bytes;
5455 int slot;
5456 int found_type;
5457
5458 path = btrfs_alloc_path();
5459 if (!path)
5460 return -ENOMEM;
5461
5487 if (!em) {
5488 em = ERR_PTR(-ENOMEM);
5489 goto out;
5490 }
5491 }
5492
5493 em->start = start;
5494 em->orig_start = em->start;

--- 49 unchanged lines hidden (view full) ---

5544 u64 num_bytes;
5545 int slot;
5546 int found_type;
5547
5548 path = btrfs_alloc_path();
5549 if (!path)
5550 return -ENOMEM;
5551
5462 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5552 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5463 offset, 0);
5464 if (ret < 0)
5465 goto out;
5466
5467 slot = path->slots[0];
5468 if (ret == 1) {
5469 if (slot == 0) {
5470 /* can't find the item, must cow */
5471 ret = 0;
5472 goto out;
5473 }
5474 slot--;
5475 }
5476 ret = 0;
5477 leaf = path->nodes[0];
5478 btrfs_item_key_to_cpu(leaf, &key, slot);
5553 offset, 0);
5554 if (ret < 0)
5555 goto out;
5556
5557 slot = path->slots[0];
5558 if (ret == 1) {
5559 if (slot == 0) {
5560 /* can't find the item, must cow */
5561 ret = 0;
5562 goto out;
5563 }
5564 slot--;
5565 }
5566 ret = 0;
5567 leaf = path->nodes[0];
5568 btrfs_item_key_to_cpu(leaf, &key, slot);
5479 if (key.objectid != btrfs_ino(inode) ||
5569 if (key.objectid != inode->i_ino ||
5480 key.type != BTRFS_EXTENT_DATA_KEY) {
5481 /* not our file or wrong item type, must cow */
5482 goto out;
5483 }
5484
5485 if (key.offset > offset) {
5486 /* Wrong offset, must cow */
5487 goto out;

--- 17 unchanged lines hidden (view full) ---

5505
5506 if (btrfs_extent_readonly(root, disk_bytenr))
5507 goto out;
5508
5509 /*
5510 * look for other files referencing this extent, if we
5511 * find any we must cow
5512 */
5570 key.type != BTRFS_EXTENT_DATA_KEY) {
5571 /* not our file or wrong item type, must cow */
5572 goto out;
5573 }
5574
5575 if (key.offset > offset) {
5576 /* Wrong offset, must cow */
5577 goto out;

--- 17 unchanged lines hidden (view full) ---

5595
5596 if (btrfs_extent_readonly(root, disk_bytenr))
5597 goto out;
5598
5599 /*
5600 * look for other files referencing this extent, if we
5601 * find any we must cow
5602 */
5513 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5603 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5514 key.offset - backref_offset, disk_bytenr))
5515 goto out;
5516
5517 /*
5518 * adjust disk_bytenr and num_bytes to cover just the bytes
5519 * in this extent we are about to write. If there
5520 * are any csums in that range we have to cow in order
5521 * to keep the csums correct

--- 173 unchanged lines hidden (view full) ---

5695 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5696 csum, bvec->bv_len);
5697 btrfs_csum_final(csum, (char *)&csum);
5698 kunmap_atomic(kaddr, KM_IRQ0);
5699 local_irq_restore(flags);
5700
5701 flush_dcache_page(bvec->bv_page);
5702 if (csum != *private) {
5604 key.offset - backref_offset, disk_bytenr))
5605 goto out;
5606
5607 /*
5608 * adjust disk_bytenr and num_bytes to cover just the bytes
5609 * in this extent we are about to write. If there
5610 * are any csums in that range we have to cow in order
5611 * to keep the csums correct

--- 173 unchanged lines hidden (view full) ---

5785 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5786 csum, bvec->bv_len);
5787 btrfs_csum_final(csum, (char *)&csum);
5788 kunmap_atomic(kaddr, KM_IRQ0);
5789 local_irq_restore(flags);
5790
5791 flush_dcache_page(bvec->bv_page);
5792 if (csum != *private) {
5703 printk(KERN_ERR "btrfs csum failed ino %llu off"
5793 printk(KERN_ERR "btrfs csum failed ino %lu off"
5704 " %llu csum %u private %u\n",
5794 " %llu csum %u private %u\n",
5705 (unsigned long long)btrfs_ino(inode),
5706 (unsigned long long)start,
5795 inode->i_ino, (unsigned long long)start,
5707 csum, *private);
5708 err = -EIO;
5709 }
5710 }
5711
5712 start += bvec->bv_len;
5713 private++;
5714 bvec++;

--- 130 unchanged lines hidden (view full) ---

5845 return 0;
5846}
5847
5848static void btrfs_end_dio_bio(struct bio *bio, int err)
5849{
5850 struct btrfs_dio_private *dip = bio->bi_private;
5851
5852 if (err) {
5796 csum, *private);
5797 err = -EIO;
5798 }
5799 }
5800
5801 start += bvec->bv_len;
5802 private++;
5803 bvec++;

--- 130 unchanged lines hidden (view full) ---

5934 return 0;
5935}
5936
5937static void btrfs_end_dio_bio(struct bio *bio, int err)
5938{
5939 struct btrfs_dio_private *dip = bio->bi_private;
5940
5941 if (err) {
5853 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
5942 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
5854 "sector %#Lx len %u err no %d\n",
5943 "sector %#Lx len %u err no %d\n",
5855 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
5944 dip->inode->i_ino, bio->bi_rw,
5856 (unsigned long long)bio->bi_sector, bio->bi_size, err);
5857 dip->errors = 1;
5858
5859 /*
5860 * before atomic variable goto zero, we must make sure
5861 * dip->errors is perceived to be set.
5862 */
5863 smp_mb__before_atomic_dec();

--- 824 unchanged lines hidden (view full) ---

6688 ei->last_unlink_trans = 0;
6689
6690 atomic_set(&ei->outstanding_extents, 0);
6691 atomic_set(&ei->reserved_extents, 0);
6692
6693 ei->ordered_data_close = 0;
6694 ei->orphan_meta_reserved = 0;
6695 ei->dummy_inode = 0;
5945 (unsigned long long)bio->bi_sector, bio->bi_size, err);
5946 dip->errors = 1;
5947
5948 /*
5949 * before atomic variable goto zero, we must make sure
5950 * dip->errors is perceived to be set.
5951 */
5952 smp_mb__before_atomic_dec();

--- 824 unchanged lines hidden (view full) ---

6777 ei->last_unlink_trans = 0;
6778
6779 atomic_set(&ei->outstanding_extents, 0);
6780 atomic_set(&ei->reserved_extents, 0);
6781
6782 ei->ordered_data_close = 0;
6783 ei->orphan_meta_reserved = 0;
6784 ei->dummy_inode = 0;
6696 ei->in_defrag = 0;
6697 ei->force_compress = BTRFS_COMPRESS_NONE;
6698
6785 ei->force_compress = BTRFS_COMPRESS_NONE;
6786
6699 ei->delayed_node = NULL;
6700
6701 inode = &ei->vfs_inode;
6787 inode = &ei->vfs_inode;
6702 extent_map_tree_init(&ei->extent_tree);
6703 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6704 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
6788 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6789 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6790 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6705 mutex_init(&ei->log_mutex);
6706 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6707 INIT_LIST_HEAD(&ei->i_orphan);
6708 INIT_LIST_HEAD(&ei->delalloc_inodes);
6709 INIT_LIST_HEAD(&ei->ordered_operations);
6710 RB_CLEAR_NODE(&ei->rb_node);
6711
6712 return inode;

--- 47 unchanged lines hidden (view full) ---

6760 btrfs_put_block_group(block_group);
6761 } else if (block_group) {
6762 btrfs_put_block_group(block_group);
6763 }
6764 }
6765
6766 spin_lock(&root->orphan_lock);
6767 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6791 mutex_init(&ei->log_mutex);
6792 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6793 INIT_LIST_HEAD(&ei->i_orphan);
6794 INIT_LIST_HEAD(&ei->delalloc_inodes);
6795 INIT_LIST_HEAD(&ei->ordered_operations);
6796 RB_CLEAR_NODE(&ei->rb_node);
6797
6798 return inode;

--- 47 unchanged lines hidden (view full) ---

6846 btrfs_put_block_group(block_group);
6847 } else if (block_group) {
6848 btrfs_put_block_group(block_group);
6849 }
6850 }
6851
6852 spin_lock(&root->orphan_lock);
6853 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6768 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6769 (unsigned long long)btrfs_ino(inode));
6854 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6855 inode->i_ino);
6770 list_del_init(&BTRFS_I(inode)->i_orphan);
6771 }
6772 spin_unlock(&root->orphan_lock);
6773
6774 while (1) {
6775 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6776 if (!ordered)
6777 break;

--- 5 unchanged lines hidden (view full) ---

6783 btrfs_remove_ordered_extent(inode, ordered);
6784 btrfs_put_ordered_extent(ordered);
6785 btrfs_put_ordered_extent(ordered);
6786 }
6787 }
6788 inode_tree_del(inode);
6789 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6790free:
6856 list_del_init(&BTRFS_I(inode)->i_orphan);
6857 }
6858 spin_unlock(&root->orphan_lock);
6859
6860 while (1) {
6861 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6862 if (!ordered)
6863 break;

--- 5 unchanged lines hidden (view full) ---

6869 btrfs_remove_ordered_extent(inode, ordered);
6870 btrfs_put_ordered_extent(ordered);
6871 btrfs_put_ordered_extent(ordered);
6872 }
6873 }
6874 inode_tree_del(inode);
6875 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6876free:
6791 btrfs_remove_delayed_node(inode);
6792 call_rcu(&inode->i_rcu, btrfs_i_callback);
6793}
6794
6795int btrfs_drop_inode(struct inode *inode)
6796{
6797 struct btrfs_root *root = BTRFS_I(inode)->root;
6798
6799 if (btrfs_root_refs(&root->root_item) == 0 &&
6877 call_rcu(&inode->i_rcu, btrfs_i_callback);
6878}
6879
6880int btrfs_drop_inode(struct inode *inode)
6881{
6882 struct btrfs_root *root = BTRFS_I(inode)->root;
6883
6884 if (btrfs_root_refs(&root->root_item) == 0 &&
6800 !is_free_space_inode(root, inode))
6885 root != root->fs_info->tree_root)
6801 return 1;
6802 else
6803 return generic_drop_inode(inode);
6804}
6805
6806static void init_once(void *foo)
6807{
6808 struct btrfs_inode *ei = (struct btrfs_inode *) foo;

--- 92 unchanged lines hidden (view full) ---

6901 struct btrfs_root *root = BTRFS_I(old_dir)->root;
6902 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
6903 struct inode *new_inode = new_dentry->d_inode;
6904 struct inode *old_inode = old_dentry->d_inode;
6905 struct timespec ctime = CURRENT_TIME;
6906 u64 index = 0;
6907 u64 root_objectid;
6908 int ret;
6886 return 1;
6887 else
6888 return generic_drop_inode(inode);
6889}
6890
6891static void init_once(void *foo)
6892{
6893 struct btrfs_inode *ei = (struct btrfs_inode *) foo;

--- 92 unchanged lines hidden (view full) ---

6986 struct btrfs_root *root = BTRFS_I(old_dir)->root;
6987 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
6988 struct inode *new_inode = new_dentry->d_inode;
6989 struct inode *old_inode = old_dentry->d_inode;
6990 struct timespec ctime = CURRENT_TIME;
6991 u64 index = 0;
6992 u64 root_objectid;
6993 int ret;
6909 u64 old_ino = btrfs_ino(old_inode);
6910
6994
6911 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6995 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6912 return -EPERM;
6913
6914 /* we only allow rename subvolume link between subvolumes */
6996 return -EPERM;
6997
6998 /* we only allow rename subvolume link between subvolumes */
6915 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
6999 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
6916 return -EXDEV;
6917
7000 return -EXDEV;
7001
6918 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6919 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7002 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7003 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6920 return -ENOTEMPTY;
6921
6922 if (S_ISDIR(old_inode->i_mode) && new_inode &&
6923 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
6924 return -ENOTEMPTY;
6925 /*
6926 * we're using rename to replace one file with another.
6927 * and the replacement file is large. Start IO on it now so
6928 * we don't add too much work to the end of the transaction
6929 */
6930 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
6931 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6932 filemap_flush(old_inode->i_mapping);
6933
6934 /* close the racy window with snapshot create/destroy ioctl */
7004 return -ENOTEMPTY;
7005
7006 if (S_ISDIR(old_inode->i_mode) && new_inode &&
7007 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7008 return -ENOTEMPTY;
7009 /*
7010 * we're using rename to replace one file with another.
7011 * and the replacement file is large. Start IO on it now so
7012 * we don't add too much work to the end of the transaction
7013 */
7014 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
7015 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7016 filemap_flush(old_inode->i_mapping);
7017
7018 /* close the racy window with snapshot create/destroy ioctl */
6935 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7019 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6936 down_read(&root->fs_info->subvol_sem);
6937 /*
6938 * We want to reserve the absolute worst case amount of items. So if
6939 * both inodes are subvols and we need to unlink them then that would
6940 * require 4 item modifications, but if they are both normal inodes it
6941 * would require 5 item modifications, so we'll assume their normal
6942 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6943 * should cover the worst case number of items we'll modify.

--- 8 unchanged lines hidden (view full) ---

6952
6953 if (dest != root)
6954 btrfs_record_root_in_trans(trans, dest);
6955
6956 ret = btrfs_set_inode_index(new_dir, &index);
6957 if (ret)
6958 goto out_fail;
6959
7020 down_read(&root->fs_info->subvol_sem);
7021 /*
7022 * We want to reserve the absolute worst case amount of items. So if
7023 * both inodes are subvols and we need to unlink them then that would
7024 * require 4 item modifications, but if they are both normal inodes it
7025 * would require 5 item modifications, so we'll assume their normal
7026 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7027 * should cover the worst case number of items we'll modify.

--- 8 unchanged lines hidden (view full) ---

7036
7037 if (dest != root)
7038 btrfs_record_root_in_trans(trans, dest);
7039
7040 ret = btrfs_set_inode_index(new_dir, &index);
7041 if (ret)
7042 goto out_fail;
7043
6960 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7044 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6961 /* force full log commit if subvolume involved. */
6962 root->fs_info->last_trans_log_full_commit = trans->transid;
6963 } else {
6964 ret = btrfs_insert_inode_ref(trans, dest,
6965 new_dentry->d_name.name,
6966 new_dentry->d_name.len,
7045 /* force full log commit if subvolume involved. */
7046 root->fs_info->last_trans_log_full_commit = trans->transid;
7047 } else {
7048 ret = btrfs_insert_inode_ref(trans, dest,
7049 new_dentry->d_name.name,
7050 new_dentry->d_name.len,
6967 old_ino,
6968 btrfs_ino(new_dir), index);
7051 old_inode->i_ino,
7052 new_dir->i_ino, index);
6969 if (ret)
6970 goto out_fail;
6971 /*
6972 * this is an ugly little race, but the rename is required
6973 * to make sure that if we crash, the inode is either at the
6974 * old name or the new one. pinning the log transaction lets
6975 * us make sure we don't allow a log commit to come in after
6976 * we unlink the name but before we add the new name back in.
6977 */
6978 btrfs_pin_log_trans(root);
6979 }
6980 /*
6981 * make sure the inode gets flushed if it is replacing
6982 * something.
6983 */
7053 if (ret)
7054 goto out_fail;
7055 /*
7056 * this is an ugly little race, but the rename is required
7057 * to make sure that if we crash, the inode is either at the
7058 * old name or the new one. pinning the log transaction lets
7059 * us make sure we don't allow a log commit to come in after
7060 * we unlink the name but before we add the new name back in.
7061 */
7062 btrfs_pin_log_trans(root);
7063 }
7064 /*
7065 * make sure the inode gets flushed if it is replacing
7066 * something.
7067 */
6984 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7068 if (new_inode && new_inode->i_size &&
7069 old_inode && S_ISREG(old_inode->i_mode)) {
6985 btrfs_add_ordered_operation(trans, root, old_inode);
7070 btrfs_add_ordered_operation(trans, root, old_inode);
7071 }
6986
6987 old_dir->i_ctime = old_dir->i_mtime = ctime;
6988 new_dir->i_ctime = new_dir->i_mtime = ctime;
6989 old_inode->i_ctime = ctime;
6990
6991 if (old_dentry->d_parent != new_dentry->d_parent)
6992 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6993
7072
7073 old_dir->i_ctime = old_dir->i_mtime = ctime;
7074 new_dir->i_ctime = new_dir->i_mtime = ctime;
7075 old_inode->i_ctime = ctime;
7076
7077 if (old_dentry->d_parent != new_dentry->d_parent)
7078 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7079
6994 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7080 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6995 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6996 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6997 old_dentry->d_name.name,
6998 old_dentry->d_name.len);
6999 } else {
7000 ret = __btrfs_unlink_inode(trans, root, old_dir,
7001 old_dentry->d_inode,
7002 old_dentry->d_name.name,
7003 old_dentry->d_name.len);
7004 if (!ret)
7005 ret = btrfs_update_inode(trans, root, old_inode);
7006 }
7007 BUG_ON(ret);
7008
7009 if (new_inode) {
7010 new_inode->i_ctime = CURRENT_TIME;
7081 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7082 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7083 old_dentry->d_name.name,
7084 old_dentry->d_name.len);
7085 } else {
7086 ret = __btrfs_unlink_inode(trans, root, old_dir,
7087 old_dentry->d_inode,
7088 old_dentry->d_name.name,
7089 old_dentry->d_name.len);
7090 if (!ret)
7091 ret = btrfs_update_inode(trans, root, old_inode);
7092 }
7093 BUG_ON(ret);
7094
7095 if (new_inode) {
7096 new_inode->i_ctime = CURRENT_TIME;
7011 if (unlikely(btrfs_ino(new_inode) ==
7097 if (unlikely(new_inode->i_ino ==
7012 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7013 root_objectid = BTRFS_I(new_inode)->location.objectid;
7014 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7015 root_objectid,
7016 new_dentry->d_name.name,
7017 new_dentry->d_name.len);
7018 BUG_ON(new_inode->i_nlink == 0);
7019 } else {

--- 11 unchanged lines hidden (view full) ---

7031
7032 fixup_inode_flags(new_dir, old_inode);
7033
7034 ret = btrfs_add_link(trans, new_dir, old_inode,
7035 new_dentry->d_name.name,
7036 new_dentry->d_name.len, 0, index);
7037 BUG_ON(ret);
7038
7098 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7099 root_objectid = BTRFS_I(new_inode)->location.objectid;
7100 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7101 root_objectid,
7102 new_dentry->d_name.name,
7103 new_dentry->d_name.len);
7104 BUG_ON(new_inode->i_nlink == 0);
7105 } else {

--- 11 unchanged lines hidden (view full) ---

7117
7118 fixup_inode_flags(new_dir, old_inode);
7119
7120 ret = btrfs_add_link(trans, new_dir, old_inode,
7121 new_dentry->d_name.name,
7122 new_dentry->d_name.len, 0, index);
7123 BUG_ON(ret);
7124
7039 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7125 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
7040 struct dentry *parent = dget_parent(new_dentry);
7041 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7042 dput(parent);
7043 btrfs_end_log_trans(root);
7044 }
7045out_fail:
7046 btrfs_end_transaction_throttle(trans, root);
7047out_notrans:
7126 struct dentry *parent = dget_parent(new_dentry);
7127 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7128 dput(parent);
7129 btrfs_end_log_trans(root);
7130 }
7131out_fail:
7132 btrfs_end_transaction_throttle(trans, root);
7133out_notrans:
7048 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7134 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
7049 up_read(&root->fs_info->subvol_sem);
7050
7051 return ret;
7052}
7053
7054/*
7055 * some fairly slow code that needs optimization. This walks the list
7056 * of all the inodes with pending delalloc and forces them to disk.

--- 37 unchanged lines hidden (view full) ---

7094 wait_event(root->fs_info->async_submit_wait,
7095 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7096 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7097 }
7098 atomic_dec(&root->fs_info->async_submit_draining);
7099 return 0;
7100}
7101
7135 up_read(&root->fs_info->subvol_sem);
7136
7137 return ret;
7138}
7139
7140/*
7141 * some fairly slow code that needs optimization. This walks the list
7142 * of all the inodes with pending delalloc and forces them to disk.

--- 37 unchanged lines hidden (view full) ---

7180 wait_event(root->fs_info->async_submit_wait,
7181 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7182 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7183 }
7184 atomic_dec(&root->fs_info->async_submit_draining);
7185 return 0;
7186}
7187
7188int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7189 int sync)
7190{
7191 struct btrfs_inode *binode;
7192 struct inode *inode = NULL;
7193
7194 spin_lock(&root->fs_info->delalloc_lock);
7195 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7196 binode = list_entry(root->fs_info->delalloc_inodes.next,
7197 struct btrfs_inode, delalloc_inodes);
7198 inode = igrab(&binode->vfs_inode);
7199 if (inode) {
7200 list_move_tail(&binode->delalloc_inodes,
7201 &root->fs_info->delalloc_inodes);
7202 break;
7203 }
7204
7205 list_del_init(&binode->delalloc_inodes);
7206 cond_resched_lock(&root->fs_info->delalloc_lock);
7207 }
7208 spin_unlock(&root->fs_info->delalloc_lock);
7209
7210 if (inode) {
7211 if (sync) {
7212 filemap_write_and_wait(inode->i_mapping);
7213 /*
7214 * We have to do this because compression doesn't
7215 * actually set PG_writeback until it submits the pages
7216 * for IO, which happens in an async thread, so we could
7217 * race and not actually wait for any writeback pages
7218 * because they've not been submitted yet. Technically
7219 * this could still be the case for the ordered stuff
7220 * since the async thread may not have started to do its
7221 * work yet. If this becomes the case then we need to
7222 * figure out a way to make sure that in writepage we
7223 * wait for any async pages to be submitted before
7224 * returning so that fdatawait does what its supposed to
7225 * do.
7226 */
7227 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7228 } else {
7229 filemap_flush(inode->i_mapping);
7230 }
7231 if (delay_iput)
7232 btrfs_add_delayed_iput(inode);
7233 else
7234 iput(inode);
7235 return 1;
7236 }
7237 return 0;
7238}
7239
7102static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7103 const char *symname)
7104{
7105 struct btrfs_trans_handle *trans;
7106 struct btrfs_root *root = BTRFS_I(dir)->root;
7107 struct btrfs_path *path;
7108 struct btrfs_key key;
7109 struct inode *inode = NULL;

--- 7 unchanged lines hidden (view full) ---

7117 struct btrfs_file_extent_item *ei;
7118 struct extent_buffer *leaf;
7119 unsigned long nr = 0;
7120
7121 name_len = strlen(symname) + 1;
7122 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7123 return -ENAMETOOLONG;
7124
7240static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7241 const char *symname)
7242{
7243 struct btrfs_trans_handle *trans;
7244 struct btrfs_root *root = BTRFS_I(dir)->root;
7245 struct btrfs_path *path;
7246 struct btrfs_key key;
7247 struct inode *inode = NULL;

--- 7 unchanged lines hidden (view full) ---

7255 struct btrfs_file_extent_item *ei;
7256 struct extent_buffer *leaf;
7257 unsigned long nr = 0;
7258
7259 name_len = strlen(symname) + 1;
7260 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7261 return -ENAMETOOLONG;
7262
7263 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7264 if (err)
7265 return err;
7125 /*
7126 * 2 items for inode item and ref
7127 * 2 items for dir items
7128 * 1 item for xattr if selinux is on
7129 */
7130 trans = btrfs_start_transaction(root, 5);
7131 if (IS_ERR(trans))
7132 return PTR_ERR(trans);
7133
7134 btrfs_set_trans_block_group(trans, dir);
7135
7266 /*
7267 * 2 items for inode item and ref
7268 * 2 items for dir items
7269 * 1 item for xattr if selinux is on
7270 */
7271 trans = btrfs_start_transaction(root, 5);
7272 if (IS_ERR(trans))
7273 return PTR_ERR(trans);
7274
7275 btrfs_set_trans_block_group(trans, dir);
7276
7136 err = btrfs_find_free_ino(root, &objectid);
7137 if (err)
7138 goto out_unlock;
7139
7140 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7277 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7141 dentry->d_name.len, btrfs_ino(dir), objectid,
7278 dentry->d_name.len, dir->i_ino, objectid,
7142 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7143 &index);
7144 if (IS_ERR(inode)) {
7145 err = PTR_ERR(inode);
7146 goto out_unlock;
7147 }
7148
7149 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);

--- 15 unchanged lines hidden (view full) ---

7165 }
7166 btrfs_update_inode_block_group(trans, inode);
7167 btrfs_update_inode_block_group(trans, dir);
7168 if (drop_inode)
7169 goto out_unlock;
7170
7171 path = btrfs_alloc_path();
7172 BUG_ON(!path);
7279 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7280 &index);
7281 if (IS_ERR(inode)) {
7282 err = PTR_ERR(inode);
7283 goto out_unlock;
7284 }
7285
7286 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);

--- 15 unchanged lines hidden (view full) ---

7302 }
7303 btrfs_update_inode_block_group(trans, inode);
7304 btrfs_update_inode_block_group(trans, dir);
7305 if (drop_inode)
7306 goto out_unlock;
7307
7308 path = btrfs_alloc_path();
7309 BUG_ON(!path);
7173 key.objectid = btrfs_ino(inode);
7310 key.objectid = inode->i_ino;
7174 key.offset = 0;
7175 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7176 datasize = btrfs_file_extent_calc_inline_size(name_len);
7177 err = btrfs_insert_empty_item(trans, root, path, &key,
7178 datasize);
7179 if (err) {
7180 drop_inode = 1;
7311 key.offset = 0;
7312 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7313 datasize = btrfs_file_extent_calc_inline_size(name_len);
7314 err = btrfs_insert_empty_item(trans, root, path, &key,
7315 datasize);
7316 if (err) {
7317 drop_inode = 1;
7181 btrfs_free_path(path);
7182 goto out_unlock;
7183 }
7184 leaf = path->nodes[0];
7185 ei = btrfs_item_ptr(leaf, path->slots[0],
7186 struct btrfs_file_extent_item);
7187 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7188 btrfs_set_file_extent_type(leaf, ei,
7189 BTRFS_FILE_EXTENT_INLINE);

--- 244 unchanged lines hidden ---
7318 goto out_unlock;
7319 }
7320 leaf = path->nodes[0];
7321 ei = btrfs_item_ptr(leaf, path->slots[0],
7322 struct btrfs_file_extent_item);
7323 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7324 btrfs_set_file_extent_type(leaf, ei,
7325 BTRFS_FILE_EXTENT_INLINE);

--- 244 unchanged lines hidden ---