-
Notifications
You must be signed in to change notification settings - Fork 2
/
libext2fs-handle-inline-data-in-read-write-function
300 lines (289 loc) · 8.96 KB
/
libext2fs-handle-inline-data-in-read-write-function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
libext2fs: handle inline data in read/write function
From: Zheng Liu <[email protected]>
Ext2fs_read/write_inline_data functions copy inline_data from/to
user's buffer directly. When we want to write some data to a file
with inline_data, ext2fs_try_to_write_inline_data is called. In this
function, it firstly check the length of data. If the data can be
saved in innode, this function will create a new extend attribute to
record information of inline_data, and copy all data into this inode.
The following commands in debugfs can handle inline_data feature after
applying this patch:
- dump
- cat
- rdump
- write
Signed-off-by: Zheng Liu <[email protected]>
Signed-off-by: Theodore Ts'o <[email protected]>
---
lib/ext2fs/ext2_err.et.in | 3 +
lib/ext2fs/ext2fs.h | 7 ++
lib/ext2fs/fileio.c | 19 ++++-
lib/ext2fs/inline_data.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 205 insertions(+), 1 deletion(-)
diff --git a/lib/ext2fs/ext2_err.et.in b/lib/ext2fs/ext2_err.et.in
index a299561..8c30b48 100644
--- a/lib/ext2fs/ext2_err.et.in
+++ b/lib/ext2fs/ext2_err.et.in
@@ -482,4 +482,7 @@ ec EXT2_ET_FILE_EXISTS,
ec EXT2_ET_BAD_EXTRA_SIZE,
"Bad inode extra isizevalue"
+ec EXT2_ET_INLINE_DATA_NO_SPACE,
+ "No free space in inline data"
+
end
diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index 9ee3925..a757058 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -1364,6 +1364,13 @@ extern errcode_t ext2fs_inline_data_mkdir(ext2_filsys fs, ext2_ino_t parent,
ext2_ino_t ino);
extern errcode_t ext2fs_convert_inline_data(ext2_filsys fs, ext2_ino_t ino,
void *priv_data);
+extern errcode_t ext2fs_read_inline_data(ext2_filsys fs, ext2_ino_t ino,
+ char *buf);
+extern errcode_t ext2fs_write_inline_data(ext2_filsys fs, ext2_ino_t ino,
+ char *buf);
+extern errcode_t ext2fs_try_to_write_inline_data(ext2_filsys fs, ext2_ino_t ino,
+ const void *buf, unsigned int nbytes,
+ unsigned int *written);
/* inode.c */
extern void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
diff --git a/lib/ext2fs/fileio.c b/lib/ext2fs/fileio.c
index 1f7002c..d751666 100644
--- a/lib/ext2fs/fileio.c
+++ b/lib/ext2fs/fileio.c
@@ -163,7 +163,8 @@ static errcode_t sync_buffer_position(ext2_file_t file)
b = file->pos / file->fs->blocksize;
if (b != file->blockno) {
- retval = ext2fs_file_flush(file);
+ if (!ext2fs_inode_has_inline_data(file->fs, file->ino))
+ retval = ext2fs_file_flush(file);
if (retval)
return retval;
file->flags &= ~EXT2_FILE_BUF_VALID;
@@ -186,6 +187,12 @@ static errcode_t load_buffer(ext2_file_t file, int dontfill)
ext2_filsys fs = file->fs;
errcode_t retval;
+ /* We first handle inline_data */
+ if (file->inode.i_flags & EXT4_INLINE_DATA_FL) {
+ retval = ext2fs_read_inline_data(fs, file->ino, file->buf);
+ return retval;
+ }
+
if (!(file->flags & EXT2_FILE_BUF_VALID)) {
retval = ext2fs_bmap2(fs, file->ino, &file->inode,
BMAP_BUFFER, 0, file->blockno, 0,
@@ -280,6 +287,16 @@ errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
if (!(file->flags & EXT2_FILE_WRITE))
return EXT2_ET_FILE_RO;
+ if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_INLINE_DATA) {
+ retval = ext2fs_try_to_write_inline_data(fs, file->ino, buf,
+ nbytes, written);
+ if (!retval)
+ return retval;
+ if (retval != EXT2_ET_INLINE_DATA_NO_SPACE)
+ return retval;
+ retval = 0;
+ }
+
while (nbytes > 0) {
retval = sync_buffer_position(file);
if (retval)
diff --git a/lib/ext2fs/inline_data.c b/lib/ext2fs/inline_data.c
index 2779b70..1cc214a 100644
--- a/lib/ext2fs/inline_data.c
+++ b/lib/ext2fs/inline_data.c
@@ -25,6 +25,8 @@ static int ext2fs_iget_extra_inode(ext2_filsys fs, struct ext2_inode_large *inod
struct inline_data *data);
static void *ext2fs_get_inline_xattr_pos(struct ext2_inode_large *inode,
struct inline_data *data);
+static unsigned int ext2fs_get_max_inline_size(ext2_filsys fs,
+ struct ext2_inode_large *inode);
static void ext2fs_inline_data_finish_convert(ext2_filsys fs, ext2_ino_t ino,
char *target, char *buf,
int inline_size);
@@ -319,6 +321,73 @@ out:
return retval & BLOCK_ERROR ? ctx->errcode : 0;
}
+errcode_t ext2fs_read_inline_data(ext2_filsys fs, ext2_ino_t ino, char *buf)
+{
+ struct ext2_inode_large *inode;
+ struct inline_data data;
+ errcode_t retval = 0;
+ unsigned int inline_size;
+
+ retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode);
+ if (retval)
+ return retval;
+
+ retval = ext2fs_read_inode_full(fs, ino, (void *)inode,
+ EXT2_INODE_SIZE(fs->super));
+ if (retval)
+ goto err;
+
+ retval = ext2fs_iget_extra_inode(fs, inode, &data);
+ if (retval)
+ goto err;
+
+ inline_size = data.inline_size;
+
+ memcpy(buf, (void *)inode->i_block, EXT4_MIN_INLINE_DATA_SIZE);
+ if (inline_size > EXT4_MIN_INLINE_DATA_SIZE)
+ memcpy(buf + EXT4_MIN_INLINE_DATA_SIZE,
+ ext2fs_get_inline_xattr_pos(inode, &data),
+ inline_size - EXT4_MIN_INLINE_DATA_SIZE);
+
+err:
+ ext2fs_free_mem(&inode);
+ return retval;
+}
+
+errcode_t ext2fs_write_inline_data(ext2_filsys fs, ext2_ino_t ino, char *buf)
+{
+ struct ext2_inode_large *inode;
+ struct inline_data data;
+ errcode_t retval = 0;
+ unsigned int inline_size;
+
+ retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode);
+ if (retval)
+ return retval;
+
+ retval = ext2fs_read_inode_full(fs, ino, (void *)inode,
+ EXT2_INODE_SIZE(fs->super));
+ if (retval)
+ goto err;
+
+ retval = ext2fs_iget_extra_inode(fs, inode, &data);
+ if (retval)
+ goto err;
+
+ inline_size = data.inline_size;
+
+ memcpy((void *)inode->i_block, buf, EXT4_MIN_INLINE_DATA_SIZE);
+ if (inline_size > EXT4_MIN_INLINE_DATA_SIZE)
+ memcpy(ext2fs_get_inline_xattr_pos(inode, &data),
+ buf + EXT4_MIN_INLINE_DATA_SIZE,
+ inline_size - EXT4_MIN_INLINE_DATA_SIZE);
+
+ retval = ext2fs_write_inode_full(fs, ino, (void *)inode,
+ EXT2_INODE_SIZE(fs->super));
+err:
+ ext2fs_free_mem(&inode);
+ return retval;
+}
errcode_t ext2fs_convert_inline_data(ext2_filsys fs,
ext2_ino_t ino,
void *priv_data)
@@ -524,3 +593,111 @@ out:
ext2fs_free_mem(&inode);
return retval;
}
+
+static unsigned int ext2fs_get_max_inline_size(ext2_filsys fs,
+ struct ext2_inode_large *inode)
+{
+ struct ext2_ext_attr_entry *entry;
+ struct ext2_ext_attr_ibody_header *header;
+ struct inline_data data;
+ errcode_t retval = 0;
+ size_t freesize, min_offs;
+
+ min_offs = EXT2_INODE_SIZE(fs->super) -
+ EXT2_GOOD_OLD_INODE_SIZE -
+ inode->i_extra_isize -
+ sizeof(struct ext2_ext_attr_ibody_header);
+
+ header = IHDR(inode);
+ entry = IFIRST(header);
+
+ for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
+ entry = EXT2_EXT_ATTR_NEXT(entry)) {
+ if (!entry->e_value_block && entry->e_value_size) {
+ size_t offs = ext2fs_le16_to_cpu(entry->e_value_offs);
+ if (offs < min_offs)
+ min_offs = offs;
+ }
+ }
+ freesize = min_offs -
+ ((char *)entry - (char *)IFIRST(header)) - sizeof(__u32);
+
+ /*
+ * We try to get inline data offset, but maybe it doesn't be
+ * created. So we ignore this error.
+ */
+ retval = ext2fs_iget_extra_inode(fs, inode, &data);
+ if (retval && retval != EXT2_ET_BAD_EXT_ATTR_MAGIC)
+ return 0;
+
+ if (data.inline_off) {
+ entry = (struct ext2_ext_attr_entry *)
+ ((char *)inode + data.inline_off);
+ freesize += ext2fs_le32_to_cpu(entry->e_value_size);
+ goto out;
+ }
+
+ freesize -= EXT2_EXT_ATTR_LEN(strlen(EXT4_EXT_ATTR_SYSTEM_DATA));
+
+ if (freesize > EXT2_EXT_ATTR_ROUND)
+ freesize = EXT2_EXT_ATTR_SIZE(freesize - EXT2_EXT_ATTR_ROUND);
+ else
+ freesize = 0;
+
+out:
+ return freesize + EXT4_MIN_INLINE_DATA_SIZE;
+}
+
+errcode_t ext2fs_try_to_write_inline_data(ext2_filsys fs, ext2_ino_t ino,
+ const void *buf, unsigned int nbytes,
+ unsigned int *written)
+{
+ struct ext2_inode_large *inode;
+ struct inline_data data;
+ errcode_t retval = 0;
+ unsigned int inline_size = 0;
+
+ retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode);
+ if (retval)
+ return retval;
+ retval = ext2fs_read_inode_full(fs, ino, (void *)inode,
+ EXT2_INODE_SIZE(fs->super));
+ if (retval)
+ goto out;
+
+ if (nbytes > ext2fs_get_max_inline_size(fs, inode)) {
+ retval = EXT2_ET_INLINE_DATA_NO_SPACE;
+ goto out;
+ }
+
+ retval = ext2fs_create_inline_data(fs, inode, nbytes);
+ if (retval)
+ goto out;
+
+ retval = ext2fs_iget_extra_inode(fs, inode, &data);
+ if (retval)
+ goto out;
+
+ inline_size = data.inline_size;
+
+ memcpy((void *)inode->i_block, buf, EXT4_MIN_INLINE_DATA_SIZE);
+ if (inline_size > EXT4_MIN_INLINE_DATA_SIZE)
+ memcpy(ext2fs_get_inline_xattr_pos(inode, &data),
+ (const char *) buf + EXT4_MIN_INLINE_DATA_SIZE,
+ inline_size - EXT4_MIN_INLINE_DATA_SIZE);
+
+ inode->i_flags &= ~EXT4_EXTENTS_FL;
+ inode->i_flags |= EXT4_INLINE_DATA_FL;
+
+ retval = ext2fs_write_inode_full(fs, ino, (void *)inode,
+ EXT2_INODE_SIZE(fs->super));
+
+ if (!retval)
+ *written = nbytes;
+ else
+ *written = 0;
+
+out:
+ ext2fs_free_mem(&inode);
+ return retval;
+}