Ticket #13612: btrfs.patchset5

File btrfs.patchset5, 8.9 KB (added by hyche, 7 years ago)
Line 
1From 9d381147a07d3bf0fa20b35397b8782bfe50292a Mon Sep 17 00:00:00 2001
2From: hyche <cvghy116@gmail.com>
3Date: Fri, 15 Dec 2017 06:31:21 +0700
4Subject: [PATCH 1/6] btrfs: Make SearchSlot() look cleaner
5
6---
7 src/add-ons/kernel/file_systems/btrfs/BTree.cpp | 16 ++++++----------
8 1 file changed, 6 insertions(+), 10 deletions(-)
9
10diff --git a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
11index e632f7b..70e955d 100644
12--- a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
13+++ b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
14@@ -125,19 +125,15 @@ BTree::Node::SearchSlot(const btrfs_key& key, int* slot, btree_traversing type)
15 // m-1 m m+1
16 // k : comp < 0
17 // k : comp > 0
18- if (type == BTREE_BACKWARD) {
19- *slot = mid - 1;
20- if (comp > 0)
21- *slot = mid;
22- } else if (type == BTREE_FORWARD) {
23- *slot = mid;
24- if (comp > 0)
25- *slot = mid + 1;
26- }
27+ if (type == BTREE_BACKWARD && comp < 0)
28+ mid--;
29+ else if (type == BTREE_FORWARD && comp > 0)
30+ mid++;
31
32- if (type == BTREE_EXACT || *slot < 0)
33+ if (type == BTREE_EXACT || mid < 0)
34 return B_ENTRY_NOT_FOUND;
35
36+ *slot = mid;
37 TRACE("SearchSlot() found slot %" B_PRId32 " comp %" B_PRId32 "\n",
38 *slot, comp);
39 return B_OK;
40--
412.7.4
42
43
44From de1dc0e900a9555b2ffd40a393bfd001e9a3df09 Mon Sep 17 00:00:00 2001
45From: hyche <cvghy116@gmail.com>
46Date: Fri, 15 Dec 2017 06:33:47 +0700
47Subject: [PATCH 2/6] btrfs: Init status, prevent from unitialized check
48
49---
50 src/add-ons/kernel/file_systems/btrfs/BTree.cpp | 2 +-
51 1 file changed, 1 insertion(+), 1 deletion(-)
52
53diff --git a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
54index 70e955d..2ab2ad7 100644
55--- a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
56+++ b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
57@@ -1001,7 +1001,7 @@ status_t
58 TreeIterator::_GetEntry(btree_traversing type, void** _value, uint32* _size,
59 uint32* _offset)
60 {
61- status_t status;
62+ status_t status = B_OK;
63 if (fIteratorStatus == B_NO_INIT) {
64 status = _Traverse(type);
65 if (status != B_OK)
66--
672.7.4
68
69
70From 7906d45ce220ee7e0a748010bd6ec11218de28f4 Mon Sep 17 00:00:00 2001
71From: hyche <cvghy116@gmail.com>
72Date: Fri, 15 Dec 2017 07:23:56 +0700
73Subject: [PATCH 3/6] btrfs: Fix wrong function name in log error
74
75---
76 src/add-ons/kernel/file_systems/btrfs/BTree.cpp | 3 ++-
77 1 file changed, 2 insertions(+), 1 deletion(-)
78
79diff --git a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
80index 2ab2ad7..1d7756c 100644
81--- a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
82+++ b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
83@@ -905,7 +905,8 @@ BTree::SetRoot(off_t logical, fsblock_t* block)
84 fRootBlock = *block;
85 } else {
86 if (fVolume->FindBlock(logical, fRootBlock) != B_OK) {
87- ERROR("Find() unmapped block %" B_PRId64 "\n", fRootBlock);
88+ ERROR("SetRoot() unmapped block %" B_PRId64 " %" B_PRId64 "\n",
89+ logical, fRootBlock);
90 return B_ERROR;
91 }
92 }
93--
942.7.4
95
96
97From 7675ffe36049abbbdbe4a61b393864418ee1b9e6 Mon Sep 17 00:00:00 2001
98From: hyche <cvghy116@gmail.com>
99Date: Sat, 16 Dec 2017 03:45:30 +0700
100Subject: [PATCH 4/6] btrfs: Fix btrfs_dir_entry doesn't read data from disk
101 correctly.
102
103---
104 src/add-ons/kernel/file_systems/btrfs/btrfs.h | 6 +++---
105 1 file changed, 3 insertions(+), 3 deletions(-)
106
107diff --git a/src/add-ons/kernel/file_systems/btrfs/btrfs.h b/src/add-ons/kernel/file_systems/btrfs/btrfs.h
108index 3d0e7bf..9320e20 100644
109--- a/src/add-ons/kernel/file_systems/btrfs/btrfs.h
110+++ b/src/add-ons/kernel/file_systems/btrfs/btrfs.h
111@@ -356,8 +356,8 @@ struct btrfs_dir_entry {
112 uint16 data_length;
113 uint16 name_length;
114 uint8 type;
115- uint8* name;
116- uint8* data; // attribute data
117+ uint8 name[];
118+ // if attribute data exists, it goes here
119 uint16 DataLength() const { return B_LENDIAN_TO_HOST_INT16(data_length); }
120 uint16 NameLength() const { return B_LENDIAN_TO_HOST_INT16(name_length); }
121 ino_t InodeID() const { return location.ObjectID(); }
122@@ -369,7 +369,7 @@ struct btrfs_dir_entry {
123 {
124 data_length = B_HOST_TO_LENDIAN_INT16(dataLength);
125 if (data != NULL)
126- memcpy(this->data, data, dataLength);
127+ memcpy(&name[name_length], data, dataLength);
128 }
129 void SetName(const char* name, uint16 nameLength)
130 {
131--
1322.7.4
133
134
135From 05831bf55574d7a1738f8a6d3cfb25bed0d86826 Mon Sep 17 00:00:00 2001
136From: hyche <cvghy116@gmail.com>
137Date: Sat, 16 Dec 2017 03:47:17 +0700
138Subject: [PATCH 5/6] btrfs: Fix stack corruption on MakeReference() because it
139 needs extra space for storing data, and flexible array member doesn't do
140 this.
141
142I thought it was...
143---
144 src/add-ons/kernel/file_systems/btrfs/Inode.cpp | 45 +++++++++++++------------
145 1 file changed, 24 insertions(+), 21 deletions(-)
146
147diff --git a/src/add-ons/kernel/file_systems/btrfs/Inode.cpp b/src/add-ons/kernel/file_systems/btrfs/Inode.cpp
148index be9b204..4a1799e 100644
149--- a/src/add-ons/kernel/file_systems/btrfs/Inode.cpp
150+++ b/src/add-ons/kernel/file_systems/btrfs/Inode.cpp
151@@ -440,44 +440,46 @@ Inode::MakeReference(Transaction& transaction, BTree::Path* path,
152 BTree* tree = fVolume->FSTree();
153 uint16 nameLength = strlen(name);
154 uint64 index = parent->FindNextIndex(path);
155- void* data[1];
156
157 // insert inode_ref
158- btrfs_entry entry;
159- btrfs_inode_ref inodeRef;
160-
161- inodeRef.index = index;
162- inodeRef.SetName(name, nameLength);
163- data[0] = (void*)&inodeRef;
164+ btrfs_inode_ref* inodeRef = (btrfs_inode_ref*)malloc(sizeof(btrfs_inode_ref)
165+ + nameLength);
166+ inodeRef->index = index;
167+ inodeRef->SetName(name, nameLength);
168
169+ btrfs_entry entry;
170 entry.key.SetObjectID(fID);
171 entry.key.SetType(BTRFS_KEY_TYPE_INODE_REF);
172 entry.key.SetOffset(parent->ID());
173- entry.SetSize(inodeRef.Length());
174+ entry.SetSize(inodeRef->Length());
175
176- status_t status = tree->InsertEntries(transaction, path, &entry, data, 1);
177+ status_t status = tree->InsertEntries(transaction, path, &entry,
178+ (void**)&inodeRef, 1);
179+ free(inodeRef);
180 if (status != B_OK)
181 return status;
182
183 // insert dir_entry
184 uint32 hash = calculate_crc((uint32)~1, (uint8*)name, nameLength);
185- btrfs_dir_entry directoryEntry;
186- directoryEntry.location.SetObjectID(fID);
187- directoryEntry.location.SetType(BTRFS_KEY_TYPE_INODE_ITEM);
188- directoryEntry.location.SetOffset(0);
189- directoryEntry.SetTransactionID(transaction.SystemID());
190+ btrfs_dir_entry* directoryEntry =
191+ (btrfs_dir_entry*)malloc(sizeof(btrfs_dir_entry) + nameLength);
192+ directoryEntry->location.SetObjectID(fID);
193+ directoryEntry->location.SetType(BTRFS_KEY_TYPE_INODE_ITEM);
194+ directoryEntry->location.SetOffset(0);
195+ directoryEntry->SetTransactionID(transaction.SystemID());
196 // TODO: xattribute, 0 for standard directory
197- directoryEntry.SetAttributeData(NULL, 0);
198- directoryEntry.SetName(name, nameLength);
199- directoryEntry.type = get_filetype(mode);
200- data[0] = (void*)&directoryEntry;
201+ directoryEntry->SetName(name, nameLength);
202+ directoryEntry->SetAttributeData(NULL, 0);
203+ directoryEntry->type = get_filetype(mode);
204
205 entry.key.SetObjectID(parent->ID());
206 entry.key.SetType(BTRFS_KEY_TYPE_DIR_ITEM);
207 entry.key.SetOffset(hash);
208- entry.SetSize(directoryEntry.Length());
209+ entry.SetSize(directoryEntry->Length());
210
211- status = tree->InsertEntries(transaction, path, &entry, data, 1);
212+ status = tree->InsertEntries(transaction, path, &entry,
213+ (void**)&directoryEntry, 1);
214+ free(directoryEntry);
215 if (status != B_OK)
216 return status;
217
218@@ -485,7 +487,8 @@ Inode::MakeReference(Transaction& transaction, BTree::Path* path,
219 entry.key.SetType(BTRFS_KEY_TYPE_DIR_INDEX);
220 entry.key.SetOffset(index);
221
222- status = tree->InsertEntries(transaction, path, &entry, data, 1);
223+ status = tree->InsertEntries(transaction, path, &entry,
224+ (void**)&directoryEntry, 1);
225 if (status != B_OK)
226 return status;
227
228--
2292.7.4
230
231
232From a38c1b20bb8f96e0036c89f7c3478363b4dd25b3 Mon Sep 17 00:00:00 2001
233From: hyche <cvghy116@gmail.com>
234Date: Sat, 16 Dec 2017 04:29:05 +0700
235Subject: [PATCH 6/6] btrfs: Add error log for _Find()
236
237---
238 src/add-ons/kernel/file_systems/btrfs/BTree.cpp | 7 ++++++-
239 1 file changed, 6 insertions(+), 1 deletion(-)
240
241diff --git a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
242index 1d7756c..d86531f 100644
243--- a/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
244+++ b/src/add-ons/kernel/file_systems/btrfs/BTree.cpp
245@@ -681,8 +681,13 @@ BTree::_Find(Path* path, btrfs_key& wanted, void** _value, uint32* _size,
246 if (status != B_OK)
247 return status;
248
249- if (found.Type() != wanted.Type() && wanted.Type() != BTRFS_KEY_TYPE_ANY)
250+ if (found.Type() != wanted.Type() && wanted.Type() != BTRFS_KEY_TYPE_ANY) {
251+ ERROR("Find() not found wanted: %" B_PRIu64 " %" B_PRIu8 " %"
252+ B_PRIu64 " found: %" B_PRIu64 " %" B_PRIu8 " %" B_PRIu64 "\n",
253+ wanted.ObjectID(), wanted.Type(), wanted.Offset(), found.ObjectID(),
254+ found.Type(), found.Offset());
255 return B_ENTRY_NOT_FOUND;
256+ }
257
258 wanted = found;
259 return B_OK;
260--
2612.7.4
262