]>
Commit | Line | Data |
---|---|---|
67cf5b09 TM |
1 | /* |
2 | * Copyright (c) 2012 Taobao. | |
3 | * Written by Tao Ma <boyu.mt@taobao.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of version 2.1 of the GNU Lesser General Public License | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | */ | |
4bdfc873 | 14 | |
7046ae35 | 15 | #include <linux/iomap.h> |
4bdfc873 MH |
16 | #include <linux/fiemap.h> |
17 | ||
67cf5b09 TM |
18 | #include "ext4_jbd2.h" |
19 | #include "ext4.h" | |
20 | #include "xattr.h" | |
f19d5870 | 21 | #include "truncate.h" |
67cf5b09 TM |
22 | |
23 | #define EXT4_XATTR_SYSTEM_DATA "data" | |
24 | #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS)) | |
8af0f082 TM |
25 | #define EXT4_INLINE_DOTDOT_OFFSET 2 |
26 | #define EXT4_INLINE_DOTDOT_SIZE 4 | |
67cf5b09 | 27 | |
c197855e | 28 | static int ext4_get_inline_size(struct inode *inode) |
67cf5b09 TM |
29 | { |
30 | if (EXT4_I(inode)->i_inline_off) | |
31 | return EXT4_I(inode)->i_inline_size; | |
32 | ||
33 | return 0; | |
34 | } | |
35 | ||
36 | static int get_max_inline_xattr_value_size(struct inode *inode, | |
37 | struct ext4_iloc *iloc) | |
38 | { | |
39 | struct ext4_xattr_ibody_header *header; | |
40 | struct ext4_xattr_entry *entry; | |
41 | struct ext4_inode *raw_inode; | |
42 | int free, min_offs; | |
43 | ||
44 | min_offs = EXT4_SB(inode->i_sb)->s_inode_size - | |
45 | EXT4_GOOD_OLD_INODE_SIZE - | |
46 | EXT4_I(inode)->i_extra_isize - | |
47 | sizeof(struct ext4_xattr_ibody_header); | |
48 | ||
49 | /* | |
50 | * We need to subtract another sizeof(__u32) since an in-inode xattr | |
51 | * needs an empty 4 bytes to indicate the gap between the xattr entry | |
52 | * and the name/value pair. | |
53 | */ | |
54 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) | |
55 | return EXT4_XATTR_SIZE(min_offs - | |
56 | EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) - | |
57 | EXT4_XATTR_ROUND - sizeof(__u32)); | |
58 | ||
59 | raw_inode = ext4_raw_inode(iloc); | |
60 | header = IHDR(inode, raw_inode); | |
61 | entry = IFIRST(header); | |
62 | ||
63 | /* Compute min_offs. */ | |
64 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { | |
e50e5129 | 65 | if (!entry->e_value_inum && entry->e_value_size) { |
67cf5b09 TM |
66 | size_t offs = le16_to_cpu(entry->e_value_offs); |
67 | if (offs < min_offs) | |
68 | min_offs = offs; | |
69 | } | |
70 | } | |
71 | free = min_offs - | |
72 | ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32); | |
73 | ||
74 | if (EXT4_I(inode)->i_inline_off) { | |
75 | entry = (struct ext4_xattr_entry *) | |
76 | ((void *)raw_inode + EXT4_I(inode)->i_inline_off); | |
77 | ||
c4932dbe | 78 | free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)); |
67cf5b09 TM |
79 | goto out; |
80 | } | |
81 | ||
82 | free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)); | |
83 | ||
84 | if (free > EXT4_XATTR_ROUND) | |
85 | free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND); | |
86 | else | |
87 | free = 0; | |
88 | ||
89 | out: | |
90 | return free; | |
91 | } | |
92 | ||
93 | /* | |
94 | * Get the maximum size we now can store in an inode. | |
95 | * If we can't find the space for a xattr entry, don't use the space | |
96 | * of the extents since we have no space to indicate the inline data. | |
97 | */ | |
98 | int ext4_get_max_inline_size(struct inode *inode) | |
99 | { | |
100 | int error, max_inline_size; | |
101 | struct ext4_iloc iloc; | |
102 | ||
103 | if (EXT4_I(inode)->i_extra_isize == 0) | |
104 | return 0; | |
105 | ||
106 | error = ext4_get_inode_loc(inode, &iloc); | |
107 | if (error) { | |
108 | ext4_error_inode(inode, __func__, __LINE__, 0, | |
109 | "can't get inode location %lu", | |
110 | inode->i_ino); | |
111 | return 0; | |
112 | } | |
113 | ||
114 | down_read(&EXT4_I(inode)->xattr_sem); | |
115 | max_inline_size = get_max_inline_xattr_value_size(inode, &iloc); | |
116 | up_read(&EXT4_I(inode)->xattr_sem); | |
117 | ||
118 | brelse(iloc.bh); | |
119 | ||
120 | if (!max_inline_size) | |
121 | return 0; | |
122 | ||
123 | return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE; | |
124 | } | |
125 | ||
67cf5b09 TM |
126 | /* |
127 | * this function does not take xattr_sem, which is OK because it is | |
128 | * currently only used in a code path coming form ext4_iget, before | |
129 | * the new inode has been unlocked | |
130 | */ | |
131 | int ext4_find_inline_data_nolock(struct inode *inode) | |
132 | { | |
133 | struct ext4_xattr_ibody_find is = { | |
134 | .s = { .not_found = -ENODATA, }, | |
135 | }; | |
136 | struct ext4_xattr_info i = { | |
137 | .name_index = EXT4_XATTR_INDEX_SYSTEM, | |
138 | .name = EXT4_XATTR_SYSTEM_DATA, | |
139 | }; | |
140 | int error; | |
141 | ||
142 | if (EXT4_I(inode)->i_extra_isize == 0) | |
143 | return 0; | |
144 | ||
145 | error = ext4_get_inode_loc(inode, &is.iloc); | |
146 | if (error) | |
147 | return error; | |
148 | ||
149 | error = ext4_xattr_ibody_find(inode, &i, &is); | |
150 | if (error) | |
151 | goto out; | |
152 | ||
153 | if (!is.s.not_found) { | |
e12f99d4 TT |
154 | if (is.s.here->e_value_inum) { |
155 | EXT4_ERROR_INODE(inode, "inline data xattr refers " | |
156 | "to an external xattr inode"); | |
157 | error = -EFSCORRUPTED; | |
158 | goto out; | |
159 | } | |
67cf5b09 TM |
160 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - |
161 | (void *)ext4_raw_inode(&is.iloc)); | |
162 | EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + | |
163 | le32_to_cpu(is.s.here->e_value_size); | |
164 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
165 | } | |
166 | out: | |
167 | brelse(is.iloc.bh); | |
168 | return error; | |
169 | } | |
170 | ||
171 | static int ext4_read_inline_data(struct inode *inode, void *buffer, | |
172 | unsigned int len, | |
173 | struct ext4_iloc *iloc) | |
174 | { | |
175 | struct ext4_xattr_entry *entry; | |
176 | struct ext4_xattr_ibody_header *header; | |
177 | int cp_len = 0; | |
178 | struct ext4_inode *raw_inode; | |
179 | ||
180 | if (!len) | |
181 | return 0; | |
182 | ||
183 | BUG_ON(len > EXT4_I(inode)->i_inline_size); | |
184 | ||
185 | cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ? | |
186 | len : EXT4_MIN_INLINE_DATA_SIZE; | |
187 | ||
188 | raw_inode = ext4_raw_inode(iloc); | |
189 | memcpy(buffer, (void *)(raw_inode->i_block), cp_len); | |
190 | ||
191 | len -= cp_len; | |
192 | buffer += cp_len; | |
193 | ||
194 | if (!len) | |
195 | goto out; | |
196 | ||
197 | header = IHDR(inode, raw_inode); | |
198 | entry = (struct ext4_xattr_entry *)((void *)raw_inode + | |
199 | EXT4_I(inode)->i_inline_off); | |
200 | len = min_t(unsigned int, len, | |
201 | (unsigned int)le32_to_cpu(entry->e_value_size)); | |
202 | ||
203 | memcpy(buffer, | |
204 | (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len); | |
205 | cp_len += len; | |
206 | ||
207 | out: | |
208 | return cp_len; | |
209 | } | |
210 | ||
211 | /* | |
212 | * write the buffer to the inline inode. | |
213 | * If 'create' is set, we don't need to do the extra copy in the xattr | |
0d812f77 TM |
214 | * value since it is already handled by ext4_xattr_ibody_inline_set. |
215 | * That saves us one memcpy. | |
67cf5b09 | 216 | */ |
c197855e SH |
217 | static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc, |
218 | void *buffer, loff_t pos, unsigned int len) | |
67cf5b09 TM |
219 | { |
220 | struct ext4_xattr_entry *entry; | |
221 | struct ext4_xattr_ibody_header *header; | |
222 | struct ext4_inode *raw_inode; | |
223 | int cp_len = 0; | |
224 | ||
0db1ff22 TT |
225 | if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) |
226 | return; | |
227 | ||
67cf5b09 TM |
228 | BUG_ON(!EXT4_I(inode)->i_inline_off); |
229 | BUG_ON(pos + len > EXT4_I(inode)->i_inline_size); | |
230 | ||
231 | raw_inode = ext4_raw_inode(iloc); | |
232 | buffer += pos; | |
233 | ||
234 | if (pos < EXT4_MIN_INLINE_DATA_SIZE) { | |
235 | cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ? | |
236 | EXT4_MIN_INLINE_DATA_SIZE - pos : len; | |
237 | memcpy((void *)raw_inode->i_block + pos, buffer, cp_len); | |
238 | ||
239 | len -= cp_len; | |
240 | buffer += cp_len; | |
241 | pos += cp_len; | |
242 | } | |
243 | ||
244 | if (!len) | |
245 | return; | |
246 | ||
247 | pos -= EXT4_MIN_INLINE_DATA_SIZE; | |
248 | header = IHDR(inode, raw_inode); | |
249 | entry = (struct ext4_xattr_entry *)((void *)raw_inode + | |
250 | EXT4_I(inode)->i_inline_off); | |
251 | ||
252 | memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos, | |
253 | buffer, len); | |
254 | } | |
255 | ||
256 | static int ext4_create_inline_data(handle_t *handle, | |
257 | struct inode *inode, unsigned len) | |
258 | { | |
259 | int error; | |
260 | void *value = NULL; | |
261 | struct ext4_xattr_ibody_find is = { | |
262 | .s = { .not_found = -ENODATA, }, | |
263 | }; | |
264 | struct ext4_xattr_info i = { | |
265 | .name_index = EXT4_XATTR_INDEX_SYSTEM, | |
266 | .name = EXT4_XATTR_SYSTEM_DATA, | |
267 | }; | |
268 | ||
269 | error = ext4_get_inode_loc(inode, &is.iloc); | |
270 | if (error) | |
271 | return error; | |
272 | ||
5d601255 | 273 | BUFFER_TRACE(is.iloc.bh, "get_write_access"); |
67cf5b09 TM |
274 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
275 | if (error) | |
276 | goto out; | |
277 | ||
278 | if (len > EXT4_MIN_INLINE_DATA_SIZE) { | |
bd9926e8 | 279 | value = EXT4_ZERO_XATTR_VALUE; |
67cf5b09 TM |
280 | len -= EXT4_MIN_INLINE_DATA_SIZE; |
281 | } else { | |
282 | value = ""; | |
283 | len = 0; | |
284 | } | |
285 | ||
286 | /* Insert the the xttr entry. */ | |
287 | i.value = value; | |
288 | i.value_len = len; | |
289 | ||
290 | error = ext4_xattr_ibody_find(inode, &i, &is); | |
291 | if (error) | |
292 | goto out; | |
293 | ||
294 | BUG_ON(!is.s.not_found); | |
295 | ||
0d812f77 | 296 | error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is); |
67cf5b09 TM |
297 | if (error) { |
298 | if (error == -ENOSPC) | |
299 | ext4_clear_inode_state(inode, | |
300 | EXT4_STATE_MAY_INLINE_DATA); | |
301 | goto out; | |
302 | } | |
303 | ||
304 | memset((void *)ext4_raw_inode(&is.iloc)->i_block, | |
305 | 0, EXT4_MIN_INLINE_DATA_SIZE); | |
306 | ||
307 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - | |
308 | (void *)ext4_raw_inode(&is.iloc)); | |
309 | EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE; | |
310 | ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); | |
311 | ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA); | |
312 | get_bh(is.iloc.bh); | |
313 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); | |
314 | ||
315 | out: | |
316 | brelse(is.iloc.bh); | |
317 | return error; | |
318 | } | |
319 | ||
320 | static int ext4_update_inline_data(handle_t *handle, struct inode *inode, | |
321 | unsigned int len) | |
322 | { | |
323 | int error; | |
324 | void *value = NULL; | |
325 | struct ext4_xattr_ibody_find is = { | |
326 | .s = { .not_found = -ENODATA, }, | |
327 | }; | |
328 | struct ext4_xattr_info i = { | |
329 | .name_index = EXT4_XATTR_INDEX_SYSTEM, | |
330 | .name = EXT4_XATTR_SYSTEM_DATA, | |
331 | }; | |
332 | ||
333 | /* If the old space is ok, write the data directly. */ | |
334 | if (len <= EXT4_I(inode)->i_inline_size) | |
335 | return 0; | |
336 | ||
337 | error = ext4_get_inode_loc(inode, &is.iloc); | |
338 | if (error) | |
339 | return error; | |
340 | ||
341 | error = ext4_xattr_ibody_find(inode, &i, &is); | |
342 | if (error) | |
343 | goto out; | |
344 | ||
345 | BUG_ON(is.s.not_found); | |
346 | ||
347 | len -= EXT4_MIN_INLINE_DATA_SIZE; | |
348 | value = kzalloc(len, GFP_NOFS); | |
578620f4 DC |
349 | if (!value) { |
350 | error = -ENOMEM; | |
67cf5b09 | 351 | goto out; |
578620f4 | 352 | } |
67cf5b09 TM |
353 | |
354 | error = ext4_xattr_ibody_get(inode, i.name_index, i.name, | |
355 | value, len); | |
356 | if (error == -ENODATA) | |
357 | goto out; | |
358 | ||
5d601255 | 359 | BUFFER_TRACE(is.iloc.bh, "get_write_access"); |
67cf5b09 TM |
360 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
361 | if (error) | |
362 | goto out; | |
363 | ||
364 | /* Update the xttr entry. */ | |
365 | i.value = value; | |
366 | i.value_len = len; | |
367 | ||
0d812f77 | 368 | error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is); |
67cf5b09 TM |
369 | if (error) |
370 | goto out; | |
371 | ||
372 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - | |
373 | (void *)ext4_raw_inode(&is.iloc)); | |
374 | EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + | |
375 | le32_to_cpu(is.s.here->e_value_size); | |
376 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
377 | get_bh(is.iloc.bh); | |
378 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); | |
379 | ||
380 | out: | |
381 | kfree(value); | |
382 | brelse(is.iloc.bh); | |
383 | return error; | |
384 | } | |
385 | ||
c197855e SH |
386 | static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode, |
387 | unsigned int len) | |
67cf5b09 | 388 | { |
c755e251 | 389 | int ret, size, no_expand; |
67cf5b09 TM |
390 | struct ext4_inode_info *ei = EXT4_I(inode); |
391 | ||
392 | if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) | |
393 | return -ENOSPC; | |
394 | ||
395 | size = ext4_get_max_inline_size(inode); | |
396 | if (size < len) | |
397 | return -ENOSPC; | |
398 | ||
c755e251 | 399 | ext4_write_lock_xattr(inode, &no_expand); |
67cf5b09 TM |
400 | |
401 | if (ei->i_inline_off) | |
402 | ret = ext4_update_inline_data(handle, inode, len); | |
403 | else | |
404 | ret = ext4_create_inline_data(handle, inode, len); | |
405 | ||
c755e251 | 406 | ext4_write_unlock_xattr(inode, &no_expand); |
67cf5b09 TM |
407 | return ret; |
408 | } | |
409 | ||
410 | static int ext4_destroy_inline_data_nolock(handle_t *handle, | |
411 | struct inode *inode) | |
412 | { | |
413 | struct ext4_inode_info *ei = EXT4_I(inode); | |
414 | struct ext4_xattr_ibody_find is = { | |
415 | .s = { .not_found = 0, }, | |
416 | }; | |
417 | struct ext4_xattr_info i = { | |
418 | .name_index = EXT4_XATTR_INDEX_SYSTEM, | |
419 | .name = EXT4_XATTR_SYSTEM_DATA, | |
420 | .value = NULL, | |
421 | .value_len = 0, | |
422 | }; | |
423 | int error; | |
424 | ||
425 | if (!ei->i_inline_off) | |
426 | return 0; | |
427 | ||
428 | error = ext4_get_inode_loc(inode, &is.iloc); | |
429 | if (error) | |
430 | return error; | |
431 | ||
432 | error = ext4_xattr_ibody_find(inode, &i, &is); | |
433 | if (error) | |
434 | goto out; | |
435 | ||
5d601255 | 436 | BUFFER_TRACE(is.iloc.bh, "get_write_access"); |
67cf5b09 TM |
437 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
438 | if (error) | |
439 | goto out; | |
440 | ||
0d812f77 | 441 | error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is); |
67cf5b09 TM |
442 | if (error) |
443 | goto out; | |
444 | ||
445 | memset((void *)ext4_raw_inode(&is.iloc)->i_block, | |
446 | 0, EXT4_MIN_INLINE_DATA_SIZE); | |
94571f26 | 447 | memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE); |
67cf5b09 | 448 | |
e2b911c5 | 449 | if (ext4_has_feature_extents(inode->i_sb)) { |
67cf5b09 TM |
450 | if (S_ISDIR(inode->i_mode) || |
451 | S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) { | |
452 | ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); | |
453 | ext4_ext_tree_init(handle, inode); | |
454 | } | |
455 | } | |
456 | ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA); | |
457 | ||
458 | get_bh(is.iloc.bh); | |
459 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); | |
460 | ||
461 | EXT4_I(inode)->i_inline_off = 0; | |
462 | EXT4_I(inode)->i_inline_size = 0; | |
463 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
464 | out: | |
465 | brelse(is.iloc.bh); | |
466 | if (error == -ENODATA) | |
467 | error = 0; | |
468 | return error; | |
469 | } | |
470 | ||
46c7f254 TM |
471 | static int ext4_read_inline_page(struct inode *inode, struct page *page) |
472 | { | |
473 | void *kaddr; | |
474 | int ret = 0; | |
475 | size_t len; | |
476 | struct ext4_iloc iloc; | |
477 | ||
478 | BUG_ON(!PageLocked(page)); | |
479 | BUG_ON(!ext4_has_inline_data(inode)); | |
480 | BUG_ON(page->index); | |
481 | ||
482 | if (!EXT4_I(inode)->i_inline_off) { | |
483 | ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.", | |
484 | inode->i_ino); | |
485 | goto out; | |
486 | } | |
487 | ||
488 | ret = ext4_get_inode_loc(inode, &iloc); | |
489 | if (ret) | |
490 | goto out; | |
491 | ||
492 | len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode)); | |
493 | kaddr = kmap_atomic(page); | |
494 | ret = ext4_read_inline_data(inode, kaddr, len, &iloc); | |
495 | flush_dcache_page(page); | |
496 | kunmap_atomic(kaddr); | |
09cbfeaf | 497 | zero_user_segment(page, len, PAGE_SIZE); |
46c7f254 TM |
498 | SetPageUptodate(page); |
499 | brelse(iloc.bh); | |
500 | ||
501 | out: | |
502 | return ret; | |
503 | } | |
504 | ||
505 | int ext4_readpage_inline(struct inode *inode, struct page *page) | |
506 | { | |
507 | int ret = 0; | |
508 | ||
509 | down_read(&EXT4_I(inode)->xattr_sem); | |
510 | if (!ext4_has_inline_data(inode)) { | |
511 | up_read(&EXT4_I(inode)->xattr_sem); | |
512 | return -EAGAIN; | |
513 | } | |
514 | ||
515 | /* | |
516 | * Current inline data can only exist in the 1st page, | |
517 | * So for all the other pages, just set them uptodate. | |
518 | */ | |
519 | if (!page->index) | |
520 | ret = ext4_read_inline_page(inode, page); | |
521 | else if (!PageUptodate(page)) { | |
09cbfeaf | 522 | zero_user_segment(page, 0, PAGE_SIZE); |
46c7f254 TM |
523 | SetPageUptodate(page); |
524 | } | |
525 | ||
526 | up_read(&EXT4_I(inode)->xattr_sem); | |
527 | ||
528 | unlock_page(page); | |
529 | return ret >= 0 ? 0 : ret; | |
530 | } | |
531 | ||
f19d5870 TM |
532 | static int ext4_convert_inline_data_to_extent(struct address_space *mapping, |
533 | struct inode *inode, | |
534 | unsigned flags) | |
535 | { | |
c755e251 | 536 | int ret, needed_blocks, no_expand; |
f19d5870 TM |
537 | handle_t *handle = NULL; |
538 | int retries = 0, sem_held = 0; | |
539 | struct page *page = NULL; | |
540 | unsigned from, to; | |
541 | struct ext4_iloc iloc; | |
542 | ||
543 | if (!ext4_has_inline_data(inode)) { | |
544 | /* | |
545 | * clear the flag so that no new write | |
546 | * will trap here again. | |
547 | */ | |
548 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
549 | return 0; | |
550 | } | |
551 | ||
552 | needed_blocks = ext4_writepage_trans_blocks(inode); | |
553 | ||
554 | ret = ext4_get_inode_loc(inode, &iloc); | |
555 | if (ret) | |
556 | return ret; | |
557 | ||
558 | retry: | |
9924a92a | 559 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); |
f19d5870 TM |
560 | if (IS_ERR(handle)) { |
561 | ret = PTR_ERR(handle); | |
562 | handle = NULL; | |
563 | goto out; | |
564 | } | |
565 | ||
566 | /* We cannot recurse into the filesystem as the transaction is already | |
567 | * started */ | |
568 | flags |= AOP_FLAG_NOFS; | |
569 | ||
570 | page = grab_cache_page_write_begin(mapping, 0, flags); | |
571 | if (!page) { | |
572 | ret = -ENOMEM; | |
573 | goto out; | |
574 | } | |
575 | ||
c755e251 | 576 | ext4_write_lock_xattr(inode, &no_expand); |
f19d5870 TM |
577 | sem_held = 1; |
578 | /* If some one has already done this for us, just exit. */ | |
579 | if (!ext4_has_inline_data(inode)) { | |
580 | ret = 0; | |
581 | goto out; | |
582 | } | |
583 | ||
584 | from = 0; | |
585 | to = ext4_get_inline_size(inode); | |
586 | if (!PageUptodate(page)) { | |
587 | ret = ext4_read_inline_page(inode, page); | |
588 | if (ret < 0) | |
589 | goto out; | |
590 | } | |
591 | ||
592 | ret = ext4_destroy_inline_data_nolock(handle, inode); | |
593 | if (ret) | |
594 | goto out; | |
595 | ||
705965bd JK |
596 | if (ext4_should_dioread_nolock(inode)) { |
597 | ret = __block_write_begin(page, from, to, | |
598 | ext4_get_block_unwritten); | |
599 | } else | |
f19d5870 TM |
600 | ret = __block_write_begin(page, from, to, ext4_get_block); |
601 | ||
602 | if (!ret && ext4_should_journal_data(inode)) { | |
603 | ret = ext4_walk_page_buffers(handle, page_buffers(page), | |
604 | from, to, NULL, | |
605 | do_journal_get_write_access); | |
606 | } | |
607 | ||
608 | if (ret) { | |
609 | unlock_page(page); | |
09cbfeaf | 610 | put_page(page); |
684de574 | 611 | page = NULL; |
f19d5870 | 612 | ext4_orphan_add(handle, inode); |
c755e251 | 613 | ext4_write_unlock_xattr(inode, &no_expand); |
f19d5870 TM |
614 | sem_held = 0; |
615 | ext4_journal_stop(handle); | |
616 | handle = NULL; | |
617 | ext4_truncate_failed_write(inode); | |
618 | /* | |
619 | * If truncate failed early the inode might | |
620 | * still be on the orphan list; we need to | |
621 | * make sure the inode is removed from the | |
622 | * orphan list in that case. | |
623 | */ | |
624 | if (inode->i_nlink) | |
625 | ext4_orphan_del(NULL, inode); | |
626 | } | |
627 | ||
628 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) | |
629 | goto retry; | |
630 | ||
684de574 DW |
631 | if (page) |
632 | block_commit_write(page, from, to); | |
f19d5870 TM |
633 | out: |
634 | if (page) { | |
635 | unlock_page(page); | |
09cbfeaf | 636 | put_page(page); |
f19d5870 TM |
637 | } |
638 | if (sem_held) | |
c755e251 | 639 | ext4_write_unlock_xattr(inode, &no_expand); |
f19d5870 TM |
640 | if (handle) |
641 | ext4_journal_stop(handle); | |
642 | brelse(iloc.bh); | |
643 | return ret; | |
644 | } | |
645 | ||
646 | /* | |
647 | * Try to write data in the inode. | |
648 | * If the inode has inline data, check whether the new write can be | |
649 | * in the inode also. If not, create the page the handle, move the data | |
650 | * to the page make it update and let the later codes create extent for it. | |
651 | */ | |
652 | int ext4_try_to_write_inline_data(struct address_space *mapping, | |
653 | struct inode *inode, | |
654 | loff_t pos, unsigned len, | |
655 | unsigned flags, | |
656 | struct page **pagep) | |
657 | { | |
658 | int ret; | |
659 | handle_t *handle; | |
660 | struct page *page; | |
661 | struct ext4_iloc iloc; | |
662 | ||
663 | if (pos + len > ext4_get_max_inline_size(inode)) | |
664 | goto convert; | |
665 | ||
666 | ret = ext4_get_inode_loc(inode, &iloc); | |
667 | if (ret) | |
668 | return ret; | |
669 | ||
670 | /* | |
671 | * The possible write could happen in the inode, | |
672 | * so try to reserve the space in inode first. | |
673 | */ | |
9924a92a | 674 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
f19d5870 TM |
675 | if (IS_ERR(handle)) { |
676 | ret = PTR_ERR(handle); | |
677 | handle = NULL; | |
678 | goto out; | |
679 | } | |
680 | ||
681 | ret = ext4_prepare_inline_data(handle, inode, pos + len); | |
682 | if (ret && ret != -ENOSPC) | |
683 | goto out; | |
684 | ||
685 | /* We don't have space in inline inode, so convert it to extent. */ | |
686 | if (ret == -ENOSPC) { | |
687 | ext4_journal_stop(handle); | |
688 | brelse(iloc.bh); | |
689 | goto convert; | |
690 | } | |
691 | ||
692 | flags |= AOP_FLAG_NOFS; | |
693 | ||
694 | page = grab_cache_page_write_begin(mapping, 0, flags); | |
695 | if (!page) { | |
696 | ret = -ENOMEM; | |
697 | goto out; | |
698 | } | |
699 | ||
700 | *pagep = page; | |
701 | down_read(&EXT4_I(inode)->xattr_sem); | |
702 | if (!ext4_has_inline_data(inode)) { | |
703 | ret = 0; | |
704 | unlock_page(page); | |
09cbfeaf | 705 | put_page(page); |
f19d5870 TM |
706 | goto out_up_read; |
707 | } | |
708 | ||
709 | if (!PageUptodate(page)) { | |
710 | ret = ext4_read_inline_page(inode, page); | |
711 | if (ret < 0) | |
712 | goto out_up_read; | |
713 | } | |
714 | ||
715 | ret = 1; | |
716 | handle = NULL; | |
717 | out_up_read: | |
718 | up_read(&EXT4_I(inode)->xattr_sem); | |
719 | out: | |
720 | if (handle) | |
721 | ext4_journal_stop(handle); | |
722 | brelse(iloc.bh); | |
723 | return ret; | |
724 | convert: | |
725 | return ext4_convert_inline_data_to_extent(mapping, | |
726 | inode, flags); | |
727 | } | |
728 | ||
729 | int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len, | |
730 | unsigned copied, struct page *page) | |
731 | { | |
c755e251 | 732 | int ret, no_expand; |
f19d5870 TM |
733 | void *kaddr; |
734 | struct ext4_iloc iloc; | |
735 | ||
736 | if (unlikely(copied < len)) { | |
737 | if (!PageUptodate(page)) { | |
738 | copied = 0; | |
739 | goto out; | |
740 | } | |
741 | } | |
742 | ||
743 | ret = ext4_get_inode_loc(inode, &iloc); | |
744 | if (ret) { | |
745 | ext4_std_error(inode->i_sb, ret); | |
746 | copied = 0; | |
747 | goto out; | |
748 | } | |
749 | ||
c755e251 | 750 | ext4_write_lock_xattr(inode, &no_expand); |
f19d5870 TM |
751 | BUG_ON(!ext4_has_inline_data(inode)); |
752 | ||
753 | kaddr = kmap_atomic(page); | |
754 | ext4_write_inline_data(inode, &iloc, kaddr, pos, len); | |
755 | kunmap_atomic(kaddr); | |
756 | SetPageUptodate(page); | |
757 | /* clear page dirty so that writepages wouldn't work for us. */ | |
758 | ClearPageDirty(page); | |
759 | ||
c755e251 | 760 | ext4_write_unlock_xattr(inode, &no_expand); |
f19d5870 TM |
761 | brelse(iloc.bh); |
762 | out: | |
763 | return copied; | |
764 | } | |
765 | ||
3fdcfb66 TM |
766 | struct buffer_head * |
767 | ext4_journalled_write_inline_data(struct inode *inode, | |
768 | unsigned len, | |
769 | struct page *page) | |
770 | { | |
c755e251 | 771 | int ret, no_expand; |
3fdcfb66 TM |
772 | void *kaddr; |
773 | struct ext4_iloc iloc; | |
774 | ||
775 | ret = ext4_get_inode_loc(inode, &iloc); | |
776 | if (ret) { | |
777 | ext4_std_error(inode->i_sb, ret); | |
778 | return NULL; | |
779 | } | |
780 | ||
c755e251 | 781 | ext4_write_lock_xattr(inode, &no_expand); |
3fdcfb66 TM |
782 | kaddr = kmap_atomic(page); |
783 | ext4_write_inline_data(inode, &iloc, kaddr, 0, len); | |
784 | kunmap_atomic(kaddr); | |
c755e251 | 785 | ext4_write_unlock_xattr(inode, &no_expand); |
3fdcfb66 TM |
786 | |
787 | return iloc.bh; | |
788 | } | |
789 | ||
9c3569b5 TM |
790 | /* |
791 | * Try to make the page cache and handle ready for the inline data case. | |
792 | * We can call this function in 2 cases: | |
793 | * 1. The inode is created and the first write exceeds inline size. We can | |
794 | * clear the inode state safely. | |
795 | * 2. The inode has inline data, then we need to read the data, make it | |
796 | * update and dirty so that ext4_da_writepages can handle it. We don't | |
797 | * need to start the journal since the file's metatdata isn't changed now. | |
798 | */ | |
799 | static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping, | |
800 | struct inode *inode, | |
801 | unsigned flags, | |
802 | void **fsdata) | |
803 | { | |
804 | int ret = 0, inline_size; | |
805 | struct page *page; | |
806 | ||
807 | page = grab_cache_page_write_begin(mapping, 0, flags); | |
808 | if (!page) | |
809 | return -ENOMEM; | |
810 | ||
811 | down_read(&EXT4_I(inode)->xattr_sem); | |
812 | if (!ext4_has_inline_data(inode)) { | |
813 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
814 | goto out; | |
815 | } | |
816 | ||
817 | inline_size = ext4_get_inline_size(inode); | |
818 | ||
819 | if (!PageUptodate(page)) { | |
820 | ret = ext4_read_inline_page(inode, page); | |
821 | if (ret < 0) | |
822 | goto out; | |
823 | } | |
824 | ||
825 | ret = __block_write_begin(page, 0, inline_size, | |
826 | ext4_da_get_block_prep); | |
827 | if (ret) { | |
50db71ab DM |
828 | up_read(&EXT4_I(inode)->xattr_sem); |
829 | unlock_page(page); | |
09cbfeaf | 830 | put_page(page); |
9c3569b5 | 831 | ext4_truncate_failed_write(inode); |
50db71ab | 832 | return ret; |
9c3569b5 TM |
833 | } |
834 | ||
835 | SetPageDirty(page); | |
836 | SetPageUptodate(page); | |
837 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
838 | *fsdata = (void *)CONVERT_INLINE_DATA; | |
839 | ||
840 | out: | |
841 | up_read(&EXT4_I(inode)->xattr_sem); | |
842 | if (page) { | |
843 | unlock_page(page); | |
09cbfeaf | 844 | put_page(page); |
9c3569b5 TM |
845 | } |
846 | return ret; | |
847 | } | |
848 | ||
849 | /* | |
850 | * Prepare the write for the inline data. | |
851 | * If the the data can be written into the inode, we just read | |
852 | * the page and make it uptodate, and start the journal. | |
853 | * Otherwise read the page, makes it dirty so that it can be | |
854 | * handle in writepages(the i_disksize update is left to the | |
855 | * normal ext4_da_write_end). | |
856 | */ | |
857 | int ext4_da_write_inline_data_begin(struct address_space *mapping, | |
858 | struct inode *inode, | |
859 | loff_t pos, unsigned len, | |
860 | unsigned flags, | |
861 | struct page **pagep, | |
862 | void **fsdata) | |
863 | { | |
864 | int ret, inline_size; | |
865 | handle_t *handle; | |
866 | struct page *page; | |
867 | struct ext4_iloc iloc; | |
bc0ca9df | 868 | int retries; |
9c3569b5 TM |
869 | |
870 | ret = ext4_get_inode_loc(inode, &iloc); | |
871 | if (ret) | |
872 | return ret; | |
873 | ||
bc0ca9df | 874 | retry_journal: |
9924a92a | 875 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
9c3569b5 TM |
876 | if (IS_ERR(handle)) { |
877 | ret = PTR_ERR(handle); | |
9c3569b5 TM |
878 | goto out; |
879 | } | |
880 | ||
881 | inline_size = ext4_get_max_inline_size(inode); | |
882 | ||
883 | ret = -ENOSPC; | |
884 | if (inline_size >= pos + len) { | |
885 | ret = ext4_prepare_inline_data(handle, inode, pos + len); | |
886 | if (ret && ret != -ENOSPC) | |
52e44777 | 887 | goto out_journal; |
9c3569b5 TM |
888 | } |
889 | ||
5cc28a9e DM |
890 | /* |
891 | * We cannot recurse into the filesystem as the transaction | |
892 | * is already started. | |
893 | */ | |
894 | flags |= AOP_FLAG_NOFS; | |
895 | ||
9c3569b5 TM |
896 | if (ret == -ENOSPC) { |
897 | ret = ext4_da_convert_inline_data_to_extent(mapping, | |
898 | inode, | |
899 | flags, | |
900 | fsdata); | |
bc0ca9df | 901 | ext4_journal_stop(handle); |
bc0ca9df JK |
902 | if (ret == -ENOSPC && |
903 | ext4_should_retry_alloc(inode->i_sb, &retries)) | |
904 | goto retry_journal; | |
9c3569b5 TM |
905 | goto out; |
906 | } | |
907 | ||
9c3569b5 TM |
908 | |
909 | page = grab_cache_page_write_begin(mapping, 0, flags); | |
910 | if (!page) { | |
911 | ret = -ENOMEM; | |
52e44777 | 912 | goto out_journal; |
9c3569b5 TM |
913 | } |
914 | ||
915 | down_read(&EXT4_I(inode)->xattr_sem); | |
916 | if (!ext4_has_inline_data(inode)) { | |
917 | ret = 0; | |
918 | goto out_release_page; | |
919 | } | |
920 | ||
921 | if (!PageUptodate(page)) { | |
922 | ret = ext4_read_inline_page(inode, page); | |
923 | if (ret < 0) | |
924 | goto out_release_page; | |
925 | } | |
926 | ||
927 | up_read(&EXT4_I(inode)->xattr_sem); | |
928 | *pagep = page; | |
9c3569b5 TM |
929 | brelse(iloc.bh); |
930 | return 1; | |
931 | out_release_page: | |
932 | up_read(&EXT4_I(inode)->xattr_sem); | |
933 | unlock_page(page); | |
09cbfeaf | 934 | put_page(page); |
52e44777 JK |
935 | out_journal: |
936 | ext4_journal_stop(handle); | |
9c3569b5 | 937 | out: |
9c3569b5 TM |
938 | brelse(iloc.bh); |
939 | return ret; | |
940 | } | |
941 | ||
942 | int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos, | |
943 | unsigned len, unsigned copied, | |
944 | struct page *page) | |
945 | { | |
946 | int i_size_changed = 0; | |
eb5efbcb | 947 | int ret; |
9c3569b5 | 948 | |
eb5efbcb TT |
949 | ret = ext4_write_inline_data_end(inode, pos, len, copied, page); |
950 | if (ret < 0) { | |
951 | unlock_page(page); | |
952 | put_page(page); | |
953 | return ret; | |
954 | } | |
955 | copied = ret; | |
9c3569b5 TM |
956 | |
957 | /* | |
958 | * No need to use i_size_read() here, the i_size | |
959 | * cannot change under us because we hold i_mutex. | |
960 | * | |
961 | * But it's important to update i_size while still holding page lock: | |
962 | * page writeout could otherwise come in and zero beyond i_size. | |
963 | */ | |
964 | if (pos+copied > inode->i_size) { | |
965 | i_size_write(inode, pos+copied); | |
966 | i_size_changed = 1; | |
967 | } | |
968 | unlock_page(page); | |
09cbfeaf | 969 | put_page(page); |
9c3569b5 TM |
970 | |
971 | /* | |
972 | * Don't mark the inode dirty under page lock. First, it unnecessarily | |
973 | * makes the holding time of page lock longer. Second, it forces lock | |
974 | * ordering of page lock and transaction start for journaling | |
975 | * filesystems. | |
976 | */ | |
977 | if (i_size_changed) | |
978 | mark_inode_dirty(inode); | |
979 | ||
980 | return copied; | |
981 | } | |
f19d5870 | 982 | |
3c47d541 TM |
983 | #ifdef INLINE_DIR_DEBUG |
984 | void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh, | |
985 | void *inline_start, int inline_size) | |
986 | { | |
987 | int offset; | |
988 | unsigned short de_len; | |
989 | struct ext4_dir_entry_2 *de = inline_start; | |
990 | void *dlimit = inline_start + inline_size; | |
991 | ||
992 | trace_printk("inode %lu\n", dir->i_ino); | |
993 | offset = 0; | |
994 | while ((void *)de < dlimit) { | |
995 | de_len = ext4_rec_len_from_disk(de->rec_len, inline_size); | |
80cfb71e | 996 | trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n", |
3c47d541 TM |
997 | offset, de_len, de->name_len, de->name, |
998 | de->name_len, le32_to_cpu(de->inode)); | |
999 | if (ext4_check_dir_entry(dir, NULL, de, bh, | |
1000 | inline_start, inline_size, offset)) | |
1001 | BUG(); | |
1002 | ||
1003 | offset += de_len; | |
1004 | de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); | |
1005 | } | |
1006 | } | |
1007 | #else | |
1008 | #define ext4_show_inline_dir(dir, bh, inline_start, inline_size) | |
1009 | #endif | |
1010 | ||
1011 | /* | |
1012 | * Add a new entry into a inline dir. | |
1013 | * It will return -ENOSPC if no space is available, and -EIO | |
1014 | * and -EEXIST if directory entry already exists. | |
1015 | */ | |
1016 | static int ext4_add_dirent_to_inline(handle_t *handle, | |
5b643f9c | 1017 | struct ext4_filename *fname, |
56a04915 | 1018 | struct inode *dir, |
3c47d541 TM |
1019 | struct inode *inode, |
1020 | struct ext4_iloc *iloc, | |
1021 | void *inline_start, int inline_size) | |
1022 | { | |
3c47d541 TM |
1023 | int err; |
1024 | struct ext4_dir_entry_2 *de; | |
1025 | ||
5b643f9c TT |
1026 | err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start, |
1027 | inline_size, fname, &de); | |
3c47d541 TM |
1028 | if (err) |
1029 | return err; | |
1030 | ||
5d601255 | 1031 | BUFFER_TRACE(iloc->bh, "get_write_access"); |
3c47d541 TM |
1032 | err = ext4_journal_get_write_access(handle, iloc->bh); |
1033 | if (err) | |
1034 | return err; | |
1bc0af60 | 1035 | ext4_insert_dentry(inode, de, inline_size, fname); |
3c47d541 TM |
1036 | |
1037 | ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size); | |
1038 | ||
1039 | /* | |
1040 | * XXX shouldn't update any times until successful | |
1041 | * completion of syscall, but too many callers depend | |
1042 | * on this. | |
1043 | * | |
1044 | * XXX similarly, too many callers depend on | |
1045 | * ext4_new_inode() setting the times, but error | |
1046 | * recovery deletes the inode, so the worst that can | |
1047 | * happen is that the times are slightly out of date | |
1048 | * and/or different from the directory change time. | |
1049 | */ | |
eeca7ea1 | 1050 | dir->i_mtime = dir->i_ctime = current_time(dir); |
3c47d541 TM |
1051 | ext4_update_dx_flag(dir); |
1052 | dir->i_version++; | |
3c47d541 TM |
1053 | return 1; |
1054 | } | |
1055 | ||
1056 | static void *ext4_get_inline_xattr_pos(struct inode *inode, | |
1057 | struct ext4_iloc *iloc) | |
1058 | { | |
1059 | struct ext4_xattr_entry *entry; | |
1060 | struct ext4_xattr_ibody_header *header; | |
1061 | ||
1062 | BUG_ON(!EXT4_I(inode)->i_inline_off); | |
1063 | ||
1064 | header = IHDR(inode, ext4_raw_inode(iloc)); | |
1065 | entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) + | |
1066 | EXT4_I(inode)->i_inline_off); | |
1067 | ||
1068 | return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs); | |
1069 | } | |
1070 | ||
1071 | /* Set the final de to cover the whole block. */ | |
1072 | static void ext4_update_final_de(void *de_buf, int old_size, int new_size) | |
1073 | { | |
1074 | struct ext4_dir_entry_2 *de, *prev_de; | |
1075 | void *limit; | |
1076 | int de_len; | |
1077 | ||
1078 | de = (struct ext4_dir_entry_2 *)de_buf; | |
1079 | if (old_size) { | |
1080 | limit = de_buf + old_size; | |
1081 | do { | |
1082 | prev_de = de; | |
1083 | de_len = ext4_rec_len_from_disk(de->rec_len, old_size); | |
1084 | de_buf += de_len; | |
1085 | de = (struct ext4_dir_entry_2 *)de_buf; | |
1086 | } while (de_buf < limit); | |
1087 | ||
1088 | prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size - | |
1089 | old_size, new_size); | |
1090 | } else { | |
1091 | /* this is just created, so create an empty entry. */ | |
1092 | de->inode = 0; | |
1093 | de->rec_len = ext4_rec_len_to_disk(new_size, new_size); | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | static int ext4_update_inline_dir(handle_t *handle, struct inode *dir, | |
1098 | struct ext4_iloc *iloc) | |
1099 | { | |
1100 | int ret; | |
1101 | int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE; | |
1102 | int new_size = get_max_inline_xattr_value_size(dir, iloc); | |
1103 | ||
1104 | if (new_size - old_size <= EXT4_DIR_REC_LEN(1)) | |
1105 | return -ENOSPC; | |
1106 | ||
1107 | ret = ext4_update_inline_data(handle, dir, | |
1108 | new_size + EXT4_MIN_INLINE_DATA_SIZE); | |
1109 | if (ret) | |
1110 | return ret; | |
1111 | ||
1112 | ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size, | |
1113 | EXT4_I(dir)->i_inline_size - | |
1114 | EXT4_MIN_INLINE_DATA_SIZE); | |
1115 | dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size; | |
1116 | return 0; | |
1117 | } | |
1118 | ||
1119 | static void ext4_restore_inline_data(handle_t *handle, struct inode *inode, | |
1120 | struct ext4_iloc *iloc, | |
1121 | void *buf, int inline_size) | |
1122 | { | |
1123 | ext4_create_inline_data(handle, inode, inline_size); | |
1124 | ext4_write_inline_data(inode, iloc, buf, 0, inline_size); | |
1125 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
1126 | } | |
1127 | ||
1128 | static int ext4_finish_convert_inline_dir(handle_t *handle, | |
1129 | struct inode *inode, | |
1130 | struct buffer_head *dir_block, | |
1131 | void *buf, | |
1132 | int inline_size) | |
1133 | { | |
1134 | int err, csum_size = 0, header_size = 0; | |
1135 | struct ext4_dir_entry_2 *de; | |
1136 | struct ext4_dir_entry_tail *t; | |
1137 | void *target = dir_block->b_data; | |
1138 | ||
1139 | /* | |
1140 | * First create "." and ".." and then copy the dir information | |
1141 | * back to the block. | |
1142 | */ | |
1143 | de = (struct ext4_dir_entry_2 *)target; | |
1144 | de = ext4_init_dot_dotdot(inode, de, | |
1145 | inode->i_sb->s_blocksize, csum_size, | |
1146 | le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1); | |
1147 | header_size = (void *)de - target; | |
1148 | ||
1149 | memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE, | |
1150 | inline_size - EXT4_INLINE_DOTDOT_SIZE); | |
1151 | ||
9aa5d32b | 1152 | if (ext4_has_metadata_csum(inode->i_sb)) |
3c47d541 TM |
1153 | csum_size = sizeof(struct ext4_dir_entry_tail); |
1154 | ||
1155 | inode->i_size = inode->i_sb->s_blocksize; | |
1156 | i_size_write(inode, inode->i_sb->s_blocksize); | |
1157 | EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize; | |
1158 | ext4_update_final_de(dir_block->b_data, | |
1159 | inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size, | |
1160 | inode->i_sb->s_blocksize - csum_size); | |
1161 | ||
1162 | if (csum_size) { | |
1163 | t = EXT4_DIRENT_TAIL(dir_block->b_data, | |
1164 | inode->i_sb->s_blocksize); | |
1165 | initialize_dirent_tail(t, inode->i_sb->s_blocksize); | |
1166 | } | |
1167 | set_buffer_uptodate(dir_block); | |
1168 | err = ext4_handle_dirty_dirent_node(handle, inode, dir_block); | |
1169 | if (err) | |
b9cf625d | 1170 | return err; |
3c47d541 | 1171 | set_buffer_verified(dir_block); |
b9cf625d | 1172 | return ext4_mark_inode_dirty(handle, inode); |
3c47d541 TM |
1173 | } |
1174 | ||
1175 | static int ext4_convert_inline_data_nolock(handle_t *handle, | |
1176 | struct inode *inode, | |
1177 | struct ext4_iloc *iloc) | |
1178 | { | |
1179 | int error; | |
1180 | void *buf = NULL; | |
1181 | struct buffer_head *data_bh = NULL; | |
1182 | struct ext4_map_blocks map; | |
1183 | int inline_size; | |
1184 | ||
1185 | inline_size = ext4_get_inline_size(inode); | |
1186 | buf = kmalloc(inline_size, GFP_NOFS); | |
1187 | if (!buf) { | |
1188 | error = -ENOMEM; | |
1189 | goto out; | |
1190 | } | |
1191 | ||
1192 | error = ext4_read_inline_data(inode, buf, inline_size, iloc); | |
1193 | if (error < 0) | |
1194 | goto out; | |
1195 | ||
40b163f1 DW |
1196 | /* |
1197 | * Make sure the inline directory entries pass checks before we try to | |
1198 | * convert them, so that we avoid touching stuff that needs fsck. | |
1199 | */ | |
1200 | if (S_ISDIR(inode->i_mode)) { | |
1201 | error = ext4_check_all_de(inode, iloc->bh, | |
1202 | buf + EXT4_INLINE_DOTDOT_SIZE, | |
1203 | inline_size - EXT4_INLINE_DOTDOT_SIZE); | |
1204 | if (error) | |
1205 | goto out; | |
1206 | } | |
1207 | ||
3c47d541 TM |
1208 | error = ext4_destroy_inline_data_nolock(handle, inode); |
1209 | if (error) | |
1210 | goto out; | |
1211 | ||
1212 | map.m_lblk = 0; | |
1213 | map.m_len = 1; | |
1214 | map.m_flags = 0; | |
1215 | error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE); | |
1216 | if (error < 0) | |
1217 | goto out_restore; | |
1218 | if (!(map.m_flags & EXT4_MAP_MAPPED)) { | |
1219 | error = -EIO; | |
1220 | goto out_restore; | |
1221 | } | |
1222 | ||
1223 | data_bh = sb_getblk(inode->i_sb, map.m_pblk); | |
1224 | if (!data_bh) { | |
860d21e2 | 1225 | error = -ENOMEM; |
3c47d541 TM |
1226 | goto out_restore; |
1227 | } | |
1228 | ||
1229 | lock_buffer(data_bh); | |
1230 | error = ext4_journal_get_create_access(handle, data_bh); | |
1231 | if (error) { | |
1232 | unlock_buffer(data_bh); | |
1233 | error = -EIO; | |
1234 | goto out_restore; | |
1235 | } | |
1236 | memset(data_bh->b_data, 0, inode->i_sb->s_blocksize); | |
1237 | ||
1238 | if (!S_ISDIR(inode->i_mode)) { | |
1239 | memcpy(data_bh->b_data, buf, inline_size); | |
1240 | set_buffer_uptodate(data_bh); | |
1241 | error = ext4_handle_dirty_metadata(handle, | |
1242 | inode, data_bh); | |
1243 | } else { | |
1244 | error = ext4_finish_convert_inline_dir(handle, inode, data_bh, | |
1245 | buf, inline_size); | |
1246 | } | |
1247 | ||
1248 | unlock_buffer(data_bh); | |
1249 | out_restore: | |
1250 | if (error) | |
1251 | ext4_restore_inline_data(handle, inode, iloc, buf, inline_size); | |
1252 | ||
1253 | out: | |
1254 | brelse(data_bh); | |
1255 | kfree(buf); | |
1256 | return error; | |
1257 | } | |
1258 | ||
1259 | /* | |
1260 | * Try to add the new entry to the inline data. | |
1261 | * If succeeds, return 0. If not, extended the inline dir and copied data to | |
1262 | * the new created block. | |
1263 | */ | |
5b643f9c | 1264 | int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname, |
56a04915 | 1265 | struct inode *dir, struct inode *inode) |
3c47d541 | 1266 | { |
c755e251 | 1267 | int ret, inline_size, no_expand; |
3c47d541 TM |
1268 | void *inline_start; |
1269 | struct ext4_iloc iloc; | |
3c47d541 TM |
1270 | |
1271 | ret = ext4_get_inode_loc(dir, &iloc); | |
1272 | if (ret) | |
1273 | return ret; | |
1274 | ||
c755e251 | 1275 | ext4_write_lock_xattr(dir, &no_expand); |
3c47d541 TM |
1276 | if (!ext4_has_inline_data(dir)) |
1277 | goto out; | |
1278 | ||
1279 | inline_start = (void *)ext4_raw_inode(&iloc)->i_block + | |
1280 | EXT4_INLINE_DOTDOT_SIZE; | |
1281 | inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE; | |
1282 | ||
56a04915 | 1283 | ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc, |
3c47d541 TM |
1284 | inline_start, inline_size); |
1285 | if (ret != -ENOSPC) | |
1286 | goto out; | |
1287 | ||
1288 | /* check whether it can be inserted to inline xattr space. */ | |
1289 | inline_size = EXT4_I(dir)->i_inline_size - | |
1290 | EXT4_MIN_INLINE_DATA_SIZE; | |
1291 | if (!inline_size) { | |
1292 | /* Try to use the xattr space.*/ | |
1293 | ret = ext4_update_inline_dir(handle, dir, &iloc); | |
1294 | if (ret && ret != -ENOSPC) | |
1295 | goto out; | |
1296 | ||
1297 | inline_size = EXT4_I(dir)->i_inline_size - | |
1298 | EXT4_MIN_INLINE_DATA_SIZE; | |
1299 | } | |
1300 | ||
1301 | if (inline_size) { | |
1302 | inline_start = ext4_get_inline_xattr_pos(dir, &iloc); | |
1303 | ||
56a04915 | 1304 | ret = ext4_add_dirent_to_inline(handle, fname, dir, |
5b643f9c TT |
1305 | inode, &iloc, inline_start, |
1306 | inline_size); | |
3c47d541 TM |
1307 | |
1308 | if (ret != -ENOSPC) | |
1309 | goto out; | |
1310 | } | |
1311 | ||
1312 | /* | |
1313 | * The inline space is filled up, so create a new block for it. | |
1314 | * As the extent tree will be created, we have to save the inline | |
1315 | * dir first. | |
1316 | */ | |
1317 | ret = ext4_convert_inline_data_nolock(handle, dir, &iloc); | |
1318 | ||
1319 | out: | |
c755e251 | 1320 | ext4_write_unlock_xattr(dir, &no_expand); |
b907f2d5 | 1321 | ext4_mark_inode_dirty(handle, dir); |
3c47d541 TM |
1322 | brelse(iloc.bh); |
1323 | return ret; | |
1324 | } | |
1325 | ||
8af0f082 TM |
1326 | /* |
1327 | * This function fills a red-black tree with information from an | |
1328 | * inlined dir. It returns the number directory entries loaded | |
1329 | * into the tree. If there is an error it is returned in err. | |
1330 | */ | |
1331 | int htree_inlinedir_to_tree(struct file *dir_file, | |
1332 | struct inode *dir, ext4_lblk_t block, | |
1333 | struct dx_hash_info *hinfo, | |
1334 | __u32 start_hash, __u32 start_minor_hash, | |
1335 | int *has_inline_data) | |
1336 | { | |
1337 | int err = 0, count = 0; | |
1338 | unsigned int parent_ino; | |
1339 | int pos; | |
1340 | struct ext4_dir_entry_2 *de; | |
1341 | struct inode *inode = file_inode(dir_file); | |
1342 | int ret, inline_size = 0; | |
1343 | struct ext4_iloc iloc; | |
1344 | void *dir_buf = NULL; | |
1345 | struct ext4_dir_entry_2 fake; | |
a7550b30 | 1346 | struct fscrypt_str tmp_str; |
8af0f082 TM |
1347 | |
1348 | ret = ext4_get_inode_loc(inode, &iloc); | |
1349 | if (ret) | |
1350 | return ret; | |
1351 | ||
1352 | down_read(&EXT4_I(inode)->xattr_sem); | |
1353 | if (!ext4_has_inline_data(inode)) { | |
1354 | up_read(&EXT4_I(inode)->xattr_sem); | |
1355 | *has_inline_data = 0; | |
1356 | goto out; | |
1357 | } | |
1358 | ||
1359 | inline_size = ext4_get_inline_size(inode); | |
1360 | dir_buf = kmalloc(inline_size, GFP_NOFS); | |
1361 | if (!dir_buf) { | |
1362 | ret = -ENOMEM; | |
1363 | up_read(&EXT4_I(inode)->xattr_sem); | |
1364 | goto out; | |
1365 | } | |
1366 | ||
1367 | ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc); | |
1368 | up_read(&EXT4_I(inode)->xattr_sem); | |
1369 | if (ret < 0) | |
1370 | goto out; | |
1371 | ||
1372 | pos = 0; | |
1373 | parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode); | |
1374 | while (pos < inline_size) { | |
1375 | /* | |
1376 | * As inlined dir doesn't store any information about '.' and | |
1377 | * only the inode number of '..' is stored, we have to handle | |
1378 | * them differently. | |
1379 | */ | |
1380 | if (pos == 0) { | |
1381 | fake.inode = cpu_to_le32(inode->i_ino); | |
1382 | fake.name_len = 1; | |
1383 | strcpy(fake.name, "."); | |
1384 | fake.rec_len = ext4_rec_len_to_disk( | |
1385 | EXT4_DIR_REC_LEN(fake.name_len), | |
1386 | inline_size); | |
1387 | ext4_set_de_type(inode->i_sb, &fake, S_IFDIR); | |
1388 | de = &fake; | |
1389 | pos = EXT4_INLINE_DOTDOT_OFFSET; | |
1390 | } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) { | |
1391 | fake.inode = cpu_to_le32(parent_ino); | |
1392 | fake.name_len = 2; | |
1393 | strcpy(fake.name, ".."); | |
1394 | fake.rec_len = ext4_rec_len_to_disk( | |
1395 | EXT4_DIR_REC_LEN(fake.name_len), | |
1396 | inline_size); | |
1397 | ext4_set_de_type(inode->i_sb, &fake, S_IFDIR); | |
1398 | de = &fake; | |
1399 | pos = EXT4_INLINE_DOTDOT_SIZE; | |
1400 | } else { | |
1401 | de = (struct ext4_dir_entry_2 *)(dir_buf + pos); | |
1402 | pos += ext4_rec_len_from_disk(de->rec_len, inline_size); | |
1403 | if (ext4_check_dir_entry(inode, dir_file, de, | |
1404 | iloc.bh, dir_buf, | |
1405 | inline_size, pos)) { | |
1406 | ret = count; | |
1407 | goto out; | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | ext4fs_dirhash(de->name, de->name_len, hinfo); | |
1412 | if ((hinfo->hash < start_hash) || | |
1413 | ((hinfo->hash == start_hash) && | |
1414 | (hinfo->minor_hash < start_minor_hash))) | |
1415 | continue; | |
1416 | if (de->inode == 0) | |
1417 | continue; | |
2f61830a TT |
1418 | tmp_str.name = de->name; |
1419 | tmp_str.len = de->name_len; | |
1420 | err = ext4_htree_store_dirent(dir_file, hinfo->hash, | |
1421 | hinfo->minor_hash, de, &tmp_str); | |
8af0f082 TM |
1422 | if (err) { |
1423 | count = err; | |
1424 | goto out; | |
1425 | } | |
1426 | count++; | |
1427 | } | |
1428 | ret = count; | |
1429 | out: | |
1430 | kfree(dir_buf); | |
1431 | brelse(iloc.bh); | |
1432 | return ret; | |
1433 | } | |
1434 | ||
c4d8b023 TM |
1435 | /* |
1436 | * So this function is called when the volume is mkfsed with | |
1437 | * dir_index disabled. In order to keep f_pos persistent | |
1438 | * after we convert from an inlined dir to a blocked based, | |
1439 | * we just pretend that we are a normal dir and return the | |
1440 | * offset as if '.' and '..' really take place. | |
1441 | * | |
1442 | */ | |
725bebb2 AV |
1443 | int ext4_read_inline_dir(struct file *file, |
1444 | struct dir_context *ctx, | |
65d165d9 TM |
1445 | int *has_inline_data) |
1446 | { | |
65d165d9 | 1447 | unsigned int offset, parent_ino; |
725bebb2 | 1448 | int i; |
65d165d9 TM |
1449 | struct ext4_dir_entry_2 *de; |
1450 | struct super_block *sb; | |
725bebb2 | 1451 | struct inode *inode = file_inode(file); |
65d165d9 TM |
1452 | int ret, inline_size = 0; |
1453 | struct ext4_iloc iloc; | |
1454 | void *dir_buf = NULL; | |
c4d8b023 | 1455 | int dotdot_offset, dotdot_size, extra_offset, extra_size; |
65d165d9 TM |
1456 | |
1457 | ret = ext4_get_inode_loc(inode, &iloc); | |
1458 | if (ret) | |
1459 | return ret; | |
1460 | ||
1461 | down_read(&EXT4_I(inode)->xattr_sem); | |
1462 | if (!ext4_has_inline_data(inode)) { | |
1463 | up_read(&EXT4_I(inode)->xattr_sem); | |
1464 | *has_inline_data = 0; | |
1465 | goto out; | |
1466 | } | |
1467 | ||
1468 | inline_size = ext4_get_inline_size(inode); | |
1469 | dir_buf = kmalloc(inline_size, GFP_NOFS); | |
1470 | if (!dir_buf) { | |
1471 | ret = -ENOMEM; | |
1472 | up_read(&EXT4_I(inode)->xattr_sem); | |
1473 | goto out; | |
1474 | } | |
1475 | ||
1476 | ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc); | |
1477 | up_read(&EXT4_I(inode)->xattr_sem); | |
1478 | if (ret < 0) | |
1479 | goto out; | |
1480 | ||
48ffdab1 | 1481 | ret = 0; |
65d165d9 | 1482 | sb = inode->i_sb; |
65d165d9 | 1483 | parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode); |
725bebb2 | 1484 | offset = ctx->pos; |
c4d8b023 TM |
1485 | |
1486 | /* | |
1487 | * dotdot_offset and dotdot_size is the real offset and | |
1488 | * size for ".." and "." if the dir is block based while | |
1489 | * the real size for them are only EXT4_INLINE_DOTDOT_SIZE. | |
1490 | * So we will use extra_offset and extra_size to indicate them | |
1491 | * during the inline dir iteration. | |
1492 | */ | |
1493 | dotdot_offset = EXT4_DIR_REC_LEN(1); | |
1494 | dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2); | |
1495 | extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE; | |
1496 | extra_size = extra_offset + inline_size; | |
65d165d9 | 1497 | |
725bebb2 AV |
1498 | /* |
1499 | * If the version has changed since the last call to | |
1500 | * readdir(2), then we might be pointing to an invalid | |
1501 | * dirent right now. Scan from the start of the inline | |
1502 | * dir to make sure. | |
1503 | */ | |
1504 | if (file->f_version != inode->i_version) { | |
1505 | for (i = 0; i < extra_size && i < offset;) { | |
1506 | /* | |
1507 | * "." is with offset 0 and | |
1508 | * ".." is dotdot_offset. | |
1509 | */ | |
1510 | if (!i) { | |
1511 | i = dotdot_offset; | |
1512 | continue; | |
1513 | } else if (i == dotdot_offset) { | |
1514 | i = dotdot_size; | |
c4d8b023 TM |
1515 | continue; |
1516 | } | |
725bebb2 AV |
1517 | /* for other entry, the real offset in |
1518 | * the buf has to be tuned accordingly. | |
1519 | */ | |
1520 | de = (struct ext4_dir_entry_2 *) | |
1521 | (dir_buf + i - extra_offset); | |
1522 | /* It's too expensive to do a full | |
1523 | * dirent test each time round this | |
1524 | * loop, but we do have to test at | |
1525 | * least that it is non-zero. A | |
1526 | * failure will be detected in the | |
1527 | * dirent test below. */ | |
1528 | if (ext4_rec_len_from_disk(de->rec_len, extra_size) | |
1529 | < EXT4_DIR_REC_LEN(1)) | |
1530 | break; | |
1531 | i += ext4_rec_len_from_disk(de->rec_len, | |
1532 | extra_size); | |
1533 | } | |
1534 | offset = i; | |
1535 | ctx->pos = offset; | |
1536 | file->f_version = inode->i_version; | |
1537 | } | |
65d165d9 | 1538 | |
725bebb2 AV |
1539 | while (ctx->pos < extra_size) { |
1540 | if (ctx->pos == 0) { | |
1541 | if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR)) | |
1542 | goto out; | |
1543 | ctx->pos = dotdot_offset; | |
1544 | continue; | |
1545 | } | |
65d165d9 | 1546 | |
725bebb2 AV |
1547 | if (ctx->pos == dotdot_offset) { |
1548 | if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR)) | |
1549 | goto out; | |
1550 | ctx->pos = dotdot_size; | |
1551 | continue; | |
1552 | } | |
65d165d9 | 1553 | |
725bebb2 AV |
1554 | de = (struct ext4_dir_entry_2 *) |
1555 | (dir_buf + ctx->pos - extra_offset); | |
1556 | if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf, | |
1557 | extra_size, ctx->pos)) | |
1558 | goto out; | |
1559 | if (le32_to_cpu(de->inode)) { | |
1560 | if (!dir_emit(ctx, de->name, de->name_len, | |
1561 | le32_to_cpu(de->inode), | |
1562 | get_dtype(sb, de->file_type))) | |
65d165d9 | 1563 | goto out; |
65d165d9 | 1564 | } |
725bebb2 | 1565 | ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size); |
65d165d9 TM |
1566 | } |
1567 | out: | |
1568 | kfree(dir_buf); | |
1569 | brelse(iloc.bh); | |
1570 | return ret; | |
1571 | } | |
1572 | ||
32f7f22c TM |
1573 | struct buffer_head *ext4_get_first_inline_block(struct inode *inode, |
1574 | struct ext4_dir_entry_2 **parent_de, | |
1575 | int *retval) | |
1576 | { | |
1577 | struct ext4_iloc iloc; | |
1578 | ||
1579 | *retval = ext4_get_inode_loc(inode, &iloc); | |
1580 | if (*retval) | |
1581 | return NULL; | |
1582 | ||
1583 | *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; | |
1584 | ||
1585 | return iloc.bh; | |
1586 | } | |
1587 | ||
3c47d541 TM |
1588 | /* |
1589 | * Try to create the inline data for the new dir. | |
1590 | * If it succeeds, return 0, otherwise return the error. | |
1591 | * In case of ENOSPC, the caller should create the normal disk layout dir. | |
1592 | */ | |
1593 | int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent, | |
1594 | struct inode *inode) | |
1595 | { | |
1596 | int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE; | |
1597 | struct ext4_iloc iloc; | |
1598 | struct ext4_dir_entry_2 *de; | |
1599 | ||
1600 | ret = ext4_get_inode_loc(inode, &iloc); | |
1601 | if (ret) | |
1602 | return ret; | |
1603 | ||
1604 | ret = ext4_prepare_inline_data(handle, inode, inline_size); | |
1605 | if (ret) | |
1606 | goto out; | |
1607 | ||
1608 | /* | |
1609 | * For inline dir, we only save the inode information for the ".." | |
1610 | * and create a fake dentry to cover the left space. | |
1611 | */ | |
1612 | de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; | |
1613 | de->inode = cpu_to_le32(parent->i_ino); | |
1614 | de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE); | |
1615 | de->inode = 0; | |
1616 | de->rec_len = ext4_rec_len_to_disk( | |
1617 | inline_size - EXT4_INLINE_DOTDOT_SIZE, | |
1618 | inline_size); | |
1619 | set_nlink(inode, 2); | |
1620 | inode->i_size = EXT4_I(inode)->i_disksize = inline_size; | |
1621 | out: | |
1622 | brelse(iloc.bh); | |
1623 | return ret; | |
1624 | } | |
1625 | ||
e8e948e7 | 1626 | struct buffer_head *ext4_find_inline_entry(struct inode *dir, |
5b643f9c | 1627 | struct ext4_filename *fname, |
e8e948e7 TM |
1628 | struct ext4_dir_entry_2 **res_dir, |
1629 | int *has_inline_data) | |
1630 | { | |
1631 | int ret; | |
1632 | struct ext4_iloc iloc; | |
1633 | void *inline_start; | |
1634 | int inline_size; | |
1635 | ||
1636 | if (ext4_get_inode_loc(dir, &iloc)) | |
1637 | return NULL; | |
1638 | ||
1639 | down_read(&EXT4_I(dir)->xattr_sem); | |
1640 | if (!ext4_has_inline_data(dir)) { | |
1641 | *has_inline_data = 0; | |
1642 | goto out; | |
1643 | } | |
1644 | ||
1645 | inline_start = (void *)ext4_raw_inode(&iloc)->i_block + | |
1646 | EXT4_INLINE_DOTDOT_SIZE; | |
1647 | inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE; | |
5b643f9c | 1648 | ret = ext4_search_dir(iloc.bh, inline_start, inline_size, |
d6b97550 | 1649 | dir, fname, 0, res_dir); |
e8e948e7 TM |
1650 | if (ret == 1) |
1651 | goto out_find; | |
1652 | if (ret < 0) | |
1653 | goto out; | |
1654 | ||
1655 | if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE) | |
1656 | goto out; | |
1657 | ||
1658 | inline_start = ext4_get_inline_xattr_pos(dir, &iloc); | |
1659 | inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE; | |
1660 | ||
5b643f9c | 1661 | ret = ext4_search_dir(iloc.bh, inline_start, inline_size, |
d6b97550 | 1662 | dir, fname, 0, res_dir); |
e8e948e7 TM |
1663 | if (ret == 1) |
1664 | goto out_find; | |
1665 | ||
1666 | out: | |
1667 | brelse(iloc.bh); | |
1668 | iloc.bh = NULL; | |
1669 | out_find: | |
1670 | up_read(&EXT4_I(dir)->xattr_sem); | |
1671 | return iloc.bh; | |
1672 | } | |
1673 | ||
9f40fe54 TM |
1674 | int ext4_delete_inline_entry(handle_t *handle, |
1675 | struct inode *dir, | |
1676 | struct ext4_dir_entry_2 *de_del, | |
1677 | struct buffer_head *bh, | |
1678 | int *has_inline_data) | |
1679 | { | |
c755e251 | 1680 | int err, inline_size, no_expand; |
9f40fe54 TM |
1681 | struct ext4_iloc iloc; |
1682 | void *inline_start; | |
1683 | ||
1684 | err = ext4_get_inode_loc(dir, &iloc); | |
1685 | if (err) | |
1686 | return err; | |
1687 | ||
c755e251 | 1688 | ext4_write_lock_xattr(dir, &no_expand); |
9f40fe54 TM |
1689 | if (!ext4_has_inline_data(dir)) { |
1690 | *has_inline_data = 0; | |
1691 | goto out; | |
1692 | } | |
1693 | ||
1694 | if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) < | |
1695 | EXT4_MIN_INLINE_DATA_SIZE) { | |
1696 | inline_start = (void *)ext4_raw_inode(&iloc)->i_block + | |
1697 | EXT4_INLINE_DOTDOT_SIZE; | |
1698 | inline_size = EXT4_MIN_INLINE_DATA_SIZE - | |
1699 | EXT4_INLINE_DOTDOT_SIZE; | |
1700 | } else { | |
1701 | inline_start = ext4_get_inline_xattr_pos(dir, &iloc); | |
1702 | inline_size = ext4_get_inline_size(dir) - | |
1703 | EXT4_MIN_INLINE_DATA_SIZE; | |
1704 | } | |
1705 | ||
5d601255 | 1706 | BUFFER_TRACE(bh, "get_write_access"); |
9f40fe54 TM |
1707 | err = ext4_journal_get_write_access(handle, bh); |
1708 | if (err) | |
1709 | goto out; | |
1710 | ||
1711 | err = ext4_generic_delete_entry(handle, dir, de_del, bh, | |
1712 | inline_start, inline_size, 0); | |
1713 | if (err) | |
1714 | goto out; | |
1715 | ||
9f40fe54 TM |
1716 | ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size); |
1717 | out: | |
c755e251 | 1718 | ext4_write_unlock_xattr(dir, &no_expand); |
b907f2d5 TT |
1719 | if (likely(err == 0)) |
1720 | err = ext4_mark_inode_dirty(handle, dir); | |
9f40fe54 TM |
1721 | brelse(iloc.bh); |
1722 | if (err != -ENOENT) | |
1723 | ext4_std_error(dir->i_sb, err); | |
1724 | return err; | |
1725 | } | |
1726 | ||
61f86638 TM |
1727 | /* |
1728 | * Get the inline dentry at offset. | |
1729 | */ | |
1730 | static inline struct ext4_dir_entry_2 * | |
1731 | ext4_get_inline_entry(struct inode *inode, | |
1732 | struct ext4_iloc *iloc, | |
1733 | unsigned int offset, | |
1734 | void **inline_start, | |
1735 | int *inline_size) | |
1736 | { | |
1737 | void *inline_pos; | |
1738 | ||
1739 | BUG_ON(offset > ext4_get_inline_size(inode)); | |
1740 | ||
1741 | if (offset < EXT4_MIN_INLINE_DATA_SIZE) { | |
1742 | inline_pos = (void *)ext4_raw_inode(iloc)->i_block; | |
1743 | *inline_size = EXT4_MIN_INLINE_DATA_SIZE; | |
1744 | } else { | |
1745 | inline_pos = ext4_get_inline_xattr_pos(inode, iloc); | |
1746 | offset -= EXT4_MIN_INLINE_DATA_SIZE; | |
1747 | *inline_size = ext4_get_inline_size(inode) - | |
1748 | EXT4_MIN_INLINE_DATA_SIZE; | |
1749 | } | |
1750 | ||
1751 | if (inline_start) | |
1752 | *inline_start = inline_pos; | |
1753 | return (struct ext4_dir_entry_2 *)(inline_pos + offset); | |
1754 | } | |
1755 | ||
a7550b30 | 1756 | bool empty_inline_dir(struct inode *dir, int *has_inline_data) |
61f86638 TM |
1757 | { |
1758 | int err, inline_size; | |
1759 | struct ext4_iloc iloc; | |
1760 | void *inline_pos; | |
1761 | unsigned int offset; | |
1762 | struct ext4_dir_entry_2 *de; | |
a7550b30 | 1763 | bool ret = true; |
61f86638 TM |
1764 | |
1765 | err = ext4_get_inode_loc(dir, &iloc); | |
1766 | if (err) { | |
1767 | EXT4_ERROR_INODE(dir, "error %d getting inode %lu block", | |
1768 | err, dir->i_ino); | |
a7550b30 | 1769 | return true; |
61f86638 TM |
1770 | } |
1771 | ||
1772 | down_read(&EXT4_I(dir)->xattr_sem); | |
1773 | if (!ext4_has_inline_data(dir)) { | |
1774 | *has_inline_data = 0; | |
1775 | goto out; | |
1776 | } | |
1777 | ||
1778 | de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; | |
1779 | if (!le32_to_cpu(de->inode)) { | |
1780 | ext4_warning(dir->i_sb, | |
1781 | "bad inline directory (dir #%lu) - no `..'", | |
1782 | dir->i_ino); | |
a7550b30 | 1783 | ret = true; |
61f86638 TM |
1784 | goto out; |
1785 | } | |
1786 | ||
1787 | offset = EXT4_INLINE_DOTDOT_SIZE; | |
1788 | while (offset < dir->i_size) { | |
1789 | de = ext4_get_inline_entry(dir, &iloc, offset, | |
1790 | &inline_pos, &inline_size); | |
1791 | if (ext4_check_dir_entry(dir, NULL, de, | |
1792 | iloc.bh, inline_pos, | |
1793 | inline_size, offset)) { | |
1794 | ext4_warning(dir->i_sb, | |
1795 | "bad inline directory (dir #%lu) - " | |
1796 | "inode %u, rec_len %u, name_len %d" | |
8d2ae1cb | 1797 | "inline size %d", |
61f86638 TM |
1798 | dir->i_ino, le32_to_cpu(de->inode), |
1799 | le16_to_cpu(de->rec_len), de->name_len, | |
1800 | inline_size); | |
a7550b30 | 1801 | ret = true; |
61f86638 TM |
1802 | goto out; |
1803 | } | |
1804 | if (le32_to_cpu(de->inode)) { | |
a7550b30 | 1805 | ret = false; |
61f86638 TM |
1806 | goto out; |
1807 | } | |
1808 | offset += ext4_rec_len_from_disk(de->rec_len, inline_size); | |
1809 | } | |
1810 | ||
1811 | out: | |
1812 | up_read(&EXT4_I(dir)->xattr_sem); | |
1813 | brelse(iloc.bh); | |
1814 | return ret; | |
1815 | } | |
1816 | ||
67cf5b09 TM |
1817 | int ext4_destroy_inline_data(handle_t *handle, struct inode *inode) |
1818 | { | |
c755e251 | 1819 | int ret, no_expand; |
67cf5b09 | 1820 | |
c755e251 | 1821 | ext4_write_lock_xattr(inode, &no_expand); |
67cf5b09 | 1822 | ret = ext4_destroy_inline_data_nolock(handle, inode); |
c755e251 | 1823 | ext4_write_unlock_xattr(inode, &no_expand); |
67cf5b09 TM |
1824 | |
1825 | return ret; | |
1826 | } | |
94191985 | 1827 | |
7046ae35 AG |
1828 | int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap) |
1829 | { | |
1830 | __u64 addr; | |
1831 | int error = -EAGAIN; | |
1832 | struct ext4_iloc iloc; | |
1833 | ||
1834 | down_read(&EXT4_I(inode)->xattr_sem); | |
1835 | if (!ext4_has_inline_data(inode)) | |
1836 | goto out; | |
1837 | ||
1838 | error = ext4_get_inode_loc(inode, &iloc); | |
1839 | if (error) | |
1840 | goto out; | |
1841 | ||
1842 | addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits; | |
1843 | addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data; | |
1844 | addr += offsetof(struct ext4_inode, i_block); | |
1845 | ||
1846 | brelse(iloc.bh); | |
1847 | ||
1848 | iomap->addr = addr; | |
1849 | iomap->offset = 0; | |
1850 | iomap->length = min_t(loff_t, ext4_get_inline_size(inode), | |
1851 | i_size_read(inode)); | |
1852 | iomap->type = 0; | |
1853 | iomap->flags = IOMAP_F_DATA_INLINE; | |
1854 | ||
1855 | out: | |
1856 | up_read(&EXT4_I(inode)->xattr_sem); | |
1857 | return error; | |
1858 | } | |
1859 | ||
94191985 TM |
1860 | int ext4_inline_data_fiemap(struct inode *inode, |
1861 | struct fiemap_extent_info *fieinfo, | |
d952d69e | 1862 | int *has_inline, __u64 start, __u64 len) |
94191985 TM |
1863 | { |
1864 | __u64 physical = 0; | |
d952d69e DM |
1865 | __u64 inline_len; |
1866 | __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | | |
1867 | FIEMAP_EXTENT_LAST; | |
94191985 TM |
1868 | int error = 0; |
1869 | struct ext4_iloc iloc; | |
1870 | ||
1871 | down_read(&EXT4_I(inode)->xattr_sem); | |
1872 | if (!ext4_has_inline_data(inode)) { | |
1873 | *has_inline = 0; | |
1874 | goto out; | |
1875 | } | |
d952d69e DM |
1876 | inline_len = min_t(size_t, ext4_get_inline_size(inode), |
1877 | i_size_read(inode)); | |
1878 | if (start >= inline_len) | |
1879 | goto out; | |
1880 | if (start + len < inline_len) | |
1881 | inline_len = start + len; | |
1882 | inline_len -= start; | |
94191985 TM |
1883 | |
1884 | error = ext4_get_inode_loc(inode, &iloc); | |
1885 | if (error) | |
1886 | goto out; | |
1887 | ||
eaf37937 | 1888 | physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits; |
94191985 TM |
1889 | physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data; |
1890 | physical += offsetof(struct ext4_inode, i_block); | |
94191985 TM |
1891 | |
1892 | if (physical) | |
d952d69e DM |
1893 | error = fiemap_fill_next_extent(fieinfo, start, physical, |
1894 | inline_len, flags); | |
94191985 TM |
1895 | brelse(iloc.bh); |
1896 | out: | |
1897 | up_read(&EXT4_I(inode)->xattr_sem); | |
1898 | return (error < 0 ? error : 0); | |
1899 | } | |
0d812f77 TM |
1900 | |
1901 | /* | |
1902 | * Called during xattr set, and if we can sparse space 'needed', | |
1903 | * just create the extent tree evict the data to the outer block. | |
1904 | * | |
1905 | * We use jbd2 instead of page cache to move data to the 1st block | |
1906 | * so that the whole transaction can be committed as a whole and | |
1907 | * the data isn't lost because of the delayed page cache write. | |
1908 | */ | |
1909 | int ext4_try_to_evict_inline_data(handle_t *handle, | |
1910 | struct inode *inode, | |
1911 | int needed) | |
1912 | { | |
1913 | int error; | |
1914 | struct ext4_xattr_entry *entry; | |
0d812f77 TM |
1915 | struct ext4_inode *raw_inode; |
1916 | struct ext4_iloc iloc; | |
1917 | ||
1918 | error = ext4_get_inode_loc(inode, &iloc); | |
1919 | if (error) | |
1920 | return error; | |
1921 | ||
1922 | raw_inode = ext4_raw_inode(&iloc); | |
0d812f77 TM |
1923 | entry = (struct ext4_xattr_entry *)((void *)raw_inode + |
1924 | EXT4_I(inode)->i_inline_off); | |
1925 | if (EXT4_XATTR_LEN(entry->e_name_len) + | |
1926 | EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) { | |
1927 | error = -ENOSPC; | |
1928 | goto out; | |
1929 | } | |
1930 | ||
1931 | error = ext4_convert_inline_data_nolock(handle, inode, &iloc); | |
1932 | out: | |
1933 | brelse(iloc.bh); | |
1934 | return error; | |
1935 | } | |
aef1c851 | 1936 | |
01daf945 | 1937 | int ext4_inline_data_truncate(struct inode *inode, int *has_inline) |
aef1c851 TM |
1938 | { |
1939 | handle_t *handle; | |
01daf945 | 1940 | int inline_size, value_len, needed_blocks, no_expand, err = 0; |
aef1c851 TM |
1941 | size_t i_size; |
1942 | void *value = NULL; | |
1943 | struct ext4_xattr_ibody_find is = { | |
1944 | .s = { .not_found = -ENODATA, }, | |
1945 | }; | |
1946 | struct ext4_xattr_info i = { | |
1947 | .name_index = EXT4_XATTR_INDEX_SYSTEM, | |
1948 | .name = EXT4_XATTR_SYSTEM_DATA, | |
1949 | }; | |
1950 | ||
1951 | ||
1952 | needed_blocks = ext4_writepage_trans_blocks(inode); | |
9924a92a | 1953 | handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks); |
aef1c851 | 1954 | if (IS_ERR(handle)) |
01daf945 | 1955 | return PTR_ERR(handle); |
aef1c851 | 1956 | |
c755e251 | 1957 | ext4_write_lock_xattr(inode, &no_expand); |
aef1c851 TM |
1958 | if (!ext4_has_inline_data(inode)) { |
1959 | *has_inline = 0; | |
1960 | ext4_journal_stop(handle); | |
01daf945 | 1961 | return 0; |
aef1c851 TM |
1962 | } |
1963 | ||
01daf945 | 1964 | if ((err = ext4_orphan_add(handle, inode)) != 0) |
aef1c851 TM |
1965 | goto out; |
1966 | ||
01daf945 | 1967 | if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0) |
aef1c851 TM |
1968 | goto out; |
1969 | ||
1970 | down_write(&EXT4_I(inode)->i_data_sem); | |
1971 | i_size = inode->i_size; | |
1972 | inline_size = ext4_get_inline_size(inode); | |
1973 | EXT4_I(inode)->i_disksize = i_size; | |
1974 | ||
1975 | if (i_size < inline_size) { | |
1976 | /* Clear the content in the xattr space. */ | |
1977 | if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) { | |
01daf945 | 1978 | if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0) |
aef1c851 TM |
1979 | goto out_error; |
1980 | ||
1981 | BUG_ON(is.s.not_found); | |
1982 | ||
1983 | value_len = le32_to_cpu(is.s.here->e_value_size); | |
1984 | value = kmalloc(value_len, GFP_NOFS); | |
01daf945 TT |
1985 | if (!value) { |
1986 | err = -ENOMEM; | |
aef1c851 | 1987 | goto out_error; |
01daf945 | 1988 | } |
aef1c851 | 1989 | |
01daf945 TT |
1990 | err = ext4_xattr_ibody_get(inode, i.name_index, |
1991 | i.name, value, value_len); | |
1992 | if (err <= 0) | |
aef1c851 TM |
1993 | goto out_error; |
1994 | ||
1995 | i.value = value; | |
1996 | i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ? | |
1997 | i_size - EXT4_MIN_INLINE_DATA_SIZE : 0; | |
01daf945 TT |
1998 | err = ext4_xattr_ibody_inline_set(handle, inode, |
1999 | &i, &is); | |
2000 | if (err) | |
aef1c851 TM |
2001 | goto out_error; |
2002 | } | |
2003 | ||
2004 | /* Clear the content within i_blocks. */ | |
09c455aa TT |
2005 | if (i_size < EXT4_MIN_INLINE_DATA_SIZE) { |
2006 | void *p = (void *) ext4_raw_inode(&is.iloc)->i_block; | |
2007 | memset(p + i_size, 0, | |
2008 | EXT4_MIN_INLINE_DATA_SIZE - i_size); | |
2009 | } | |
aef1c851 TM |
2010 | |
2011 | EXT4_I(inode)->i_inline_size = i_size < | |
2012 | EXT4_MIN_INLINE_DATA_SIZE ? | |
2013 | EXT4_MIN_INLINE_DATA_SIZE : i_size; | |
2014 | } | |
2015 | ||
2016 | out_error: | |
2017 | up_write(&EXT4_I(inode)->i_data_sem); | |
2018 | out: | |
2019 | brelse(is.iloc.bh); | |
c755e251 | 2020 | ext4_write_unlock_xattr(inode, &no_expand); |
aef1c851 TM |
2021 | kfree(value); |
2022 | if (inode->i_nlink) | |
2023 | ext4_orphan_del(handle, inode); | |
2024 | ||
01daf945 TT |
2025 | if (err == 0) { |
2026 | inode->i_mtime = inode->i_ctime = current_time(inode); | |
2027 | err = ext4_mark_inode_dirty(handle, inode); | |
2028 | if (IS_SYNC(inode)) | |
2029 | ext4_handle_sync(handle); | |
2030 | } | |
aef1c851 | 2031 | ext4_journal_stop(handle); |
01daf945 | 2032 | return err; |
aef1c851 | 2033 | } |
0c8d414f TM |
2034 | |
2035 | int ext4_convert_inline_data(struct inode *inode) | |
2036 | { | |
c755e251 | 2037 | int error, needed_blocks, no_expand; |
0c8d414f TM |
2038 | handle_t *handle; |
2039 | struct ext4_iloc iloc; | |
2040 | ||
2041 | if (!ext4_has_inline_data(inode)) { | |
2042 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); | |
2043 | return 0; | |
2044 | } | |
2045 | ||
2046 | needed_blocks = ext4_writepage_trans_blocks(inode); | |
2047 | ||
2048 | iloc.bh = NULL; | |
2049 | error = ext4_get_inode_loc(inode, &iloc); | |
2050 | if (error) | |
2051 | return error; | |
2052 | ||
9924a92a | 2053 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); |
0c8d414f TM |
2054 | if (IS_ERR(handle)) { |
2055 | error = PTR_ERR(handle); | |
2056 | goto out_free; | |
2057 | } | |
2058 | ||
c755e251 TT |
2059 | ext4_write_lock_xattr(inode, &no_expand); |
2060 | if (ext4_has_inline_data(inode)) | |
2061 | error = ext4_convert_inline_data_nolock(handle, inode, &iloc); | |
2062 | ext4_write_unlock_xattr(inode, &no_expand); | |
0c8d414f TM |
2063 | ext4_journal_stop(handle); |
2064 | out_free: | |
2065 | brelse(iloc.bh); | |
2066 | return error; | |
2067 | } |