blob: 7e777624072a4ca1baf11014ad6a75c9df225710 [file] [log] [blame]
Tao Ma67cf5b02012-12-10 14:04:46 -05001/*
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 */
Michael Halcrow4bdfc872015-04-12 00:56:28 -040014
15#include <linux/fiemap.h>
16
Tao Ma67cf5b02012-12-10 14:04:46 -050017#include "ext4_jbd2.h"
18#include "ext4.h"
19#include "xattr.h"
Tao Maf19d5872012-12-10 14:05:51 -050020#include "truncate.h"
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -080021#include <trace/events/android_fs.h>
Tao Ma67cf5b02012-12-10 14:04:46 -050022
23#define EXT4_XATTR_SYSTEM_DATA "data"
24#define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
Tao Ma8af0f082013-04-19 17:53:09 -040025#define EXT4_INLINE_DOTDOT_OFFSET 2
26#define EXT4_INLINE_DOTDOT_SIZE 4
Tao Ma67cf5b02012-12-10 14:04:46 -050027
Stephen Hemmingerc1978552014-05-12 10:50:23 -040028static int ext4_get_inline_size(struct inode *inode)
Tao Ma67cf5b02012-12-10 14:04:46 -050029{
30 if (EXT4_I(inode)->i_inline_off)
31 return EXT4_I(inode)->i_inline_size;
32
33 return 0;
34}
35
36static 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)) {
65 if (!entry->e_value_block && entry->e_value_size) {
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
boxi liuc4932db2013-07-01 08:12:37 -040078 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
Tao Ma67cf5b02012-12-10 14:04:46 -050079 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
89out:
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 */
98int 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
Tao Ma67cf5b02012-12-10 14:04:46 -0500126/*
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 */
131int 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) {
154 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
155 (void *)ext4_raw_inode(&is.iloc));
156 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
157 le32_to_cpu(is.s.here->e_value_size);
158 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
159 }
160out:
161 brelse(is.iloc.bh);
162 return error;
163}
164
165static int ext4_read_inline_data(struct inode *inode, void *buffer,
166 unsigned int len,
167 struct ext4_iloc *iloc)
168{
169 struct ext4_xattr_entry *entry;
170 struct ext4_xattr_ibody_header *header;
171 int cp_len = 0;
172 struct ext4_inode *raw_inode;
173
174 if (!len)
175 return 0;
176
177 BUG_ON(len > EXT4_I(inode)->i_inline_size);
178
179 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
180 len : EXT4_MIN_INLINE_DATA_SIZE;
181
182 raw_inode = ext4_raw_inode(iloc);
183 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
184
185 len -= cp_len;
186 buffer += cp_len;
187
188 if (!len)
189 goto out;
190
191 header = IHDR(inode, raw_inode);
192 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
193 EXT4_I(inode)->i_inline_off);
194 len = min_t(unsigned int, len,
195 (unsigned int)le32_to_cpu(entry->e_value_size));
196
197 memcpy(buffer,
198 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
199 cp_len += len;
200
201out:
202 return cp_len;
203}
204
205/*
206 * write the buffer to the inline inode.
207 * If 'create' is set, we don't need to do the extra copy in the xattr
Tao Ma0d812f72012-12-10 14:06:02 -0500208 * value since it is already handled by ext4_xattr_ibody_inline_set.
209 * That saves us one memcpy.
Tao Ma67cf5b02012-12-10 14:04:46 -0500210 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -0400211static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
212 void *buffer, loff_t pos, unsigned int len)
Tao Ma67cf5b02012-12-10 14:04:46 -0500213{
214 struct ext4_xattr_entry *entry;
215 struct ext4_xattr_ibody_header *header;
216 struct ext4_inode *raw_inode;
217 int cp_len = 0;
218
219 BUG_ON(!EXT4_I(inode)->i_inline_off);
220 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
221
222 raw_inode = ext4_raw_inode(iloc);
223 buffer += pos;
224
225 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
226 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
227 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
228 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
229
230 len -= cp_len;
231 buffer += cp_len;
232 pos += cp_len;
233 }
234
235 if (!len)
236 return;
237
238 pos -= EXT4_MIN_INLINE_DATA_SIZE;
239 header = IHDR(inode, raw_inode);
240 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
241 EXT4_I(inode)->i_inline_off);
242
243 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
244 buffer, len);
245}
246
247static int ext4_create_inline_data(handle_t *handle,
248 struct inode *inode, unsigned len)
249{
250 int error;
251 void *value = NULL;
252 struct ext4_xattr_ibody_find is = {
253 .s = { .not_found = -ENODATA, },
254 };
255 struct ext4_xattr_info i = {
256 .name_index = EXT4_XATTR_INDEX_SYSTEM,
257 .name = EXT4_XATTR_SYSTEM_DATA,
258 };
259
260 error = ext4_get_inode_loc(inode, &is.iloc);
261 if (error)
262 return error;
263
liang xie5d601252014-05-12 22:06:43 -0400264 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500265 error = ext4_journal_get_write_access(handle, is.iloc.bh);
266 if (error)
267 goto out;
268
269 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500270 value = EXT4_ZERO_XATTR_VALUE;
Tao Ma67cf5b02012-12-10 14:04:46 -0500271 len -= EXT4_MIN_INLINE_DATA_SIZE;
272 } else {
273 value = "";
274 len = 0;
275 }
276
277 /* Insert the the xttr entry. */
278 i.value = value;
279 i.value_len = len;
280
281 error = ext4_xattr_ibody_find(inode, &i, &is);
282 if (error)
283 goto out;
284
285 BUG_ON(!is.s.not_found);
286
Tao Ma0d812f72012-12-10 14:06:02 -0500287 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500288 if (error) {
289 if (error == -ENOSPC)
290 ext4_clear_inode_state(inode,
291 EXT4_STATE_MAY_INLINE_DATA);
292 goto out;
293 }
294
295 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
296 0, EXT4_MIN_INLINE_DATA_SIZE);
297
298 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
299 (void *)ext4_raw_inode(&is.iloc));
300 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
301 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
302 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
303 get_bh(is.iloc.bh);
304 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
305
306out:
307 brelse(is.iloc.bh);
308 return error;
309}
310
311static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
312 unsigned int len)
313{
314 int error;
315 void *value = NULL;
316 struct ext4_xattr_ibody_find is = {
317 .s = { .not_found = -ENODATA, },
318 };
319 struct ext4_xattr_info i = {
320 .name_index = EXT4_XATTR_INDEX_SYSTEM,
321 .name = EXT4_XATTR_SYSTEM_DATA,
322 };
323
324 /* If the old space is ok, write the data directly. */
325 if (len <= EXT4_I(inode)->i_inline_size)
326 return 0;
327
328 error = ext4_get_inode_loc(inode, &is.iloc);
329 if (error)
330 return error;
331
332 error = ext4_xattr_ibody_find(inode, &i, &is);
333 if (error)
334 goto out;
335
336 BUG_ON(is.s.not_found);
337
338 len -= EXT4_MIN_INLINE_DATA_SIZE;
339 value = kzalloc(len, GFP_NOFS);
Dan Carpenteracf3efd2016-12-10 09:56:01 -0500340 if (!value) {
341 error = -ENOMEM;
Tao Ma67cf5b02012-12-10 14:04:46 -0500342 goto out;
Dan Carpenteracf3efd2016-12-10 09:56:01 -0500343 }
Tao Ma67cf5b02012-12-10 14:04:46 -0500344
345 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
346 value, len);
347 if (error == -ENODATA)
348 goto out;
349
liang xie5d601252014-05-12 22:06:43 -0400350 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500351 error = ext4_journal_get_write_access(handle, is.iloc.bh);
352 if (error)
353 goto out;
354
355 /* Update the xttr entry. */
356 i.value = value;
357 i.value_len = len;
358
Tao Ma0d812f72012-12-10 14:06:02 -0500359 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500360 if (error)
361 goto out;
362
363 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
364 (void *)ext4_raw_inode(&is.iloc));
365 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
366 le32_to_cpu(is.s.here->e_value_size);
367 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
368 get_bh(is.iloc.bh);
369 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
370
371out:
372 kfree(value);
373 brelse(is.iloc.bh);
374 return error;
375}
376
Stephen Hemmingerc1978552014-05-12 10:50:23 -0400377static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
378 unsigned int len)
Tao Ma67cf5b02012-12-10 14:04:46 -0500379{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500380 int ret, size, no_expand;
Tao Ma67cf5b02012-12-10 14:04:46 -0500381 struct ext4_inode_info *ei = EXT4_I(inode);
382
383 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
384 return -ENOSPC;
385
386 size = ext4_get_max_inline_size(inode);
387 if (size < len)
388 return -ENOSPC;
389
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500390 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -0500391
392 if (ei->i_inline_off)
393 ret = ext4_update_inline_data(handle, inode, len);
394 else
395 ret = ext4_create_inline_data(handle, inode, len);
396
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500397 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -0500398 return ret;
399}
400
401static int ext4_destroy_inline_data_nolock(handle_t *handle,
402 struct inode *inode)
403{
404 struct ext4_inode_info *ei = EXT4_I(inode);
405 struct ext4_xattr_ibody_find is = {
406 .s = { .not_found = 0, },
407 };
408 struct ext4_xattr_info i = {
409 .name_index = EXT4_XATTR_INDEX_SYSTEM,
410 .name = EXT4_XATTR_SYSTEM_DATA,
411 .value = NULL,
412 .value_len = 0,
413 };
414 int error;
415
416 if (!ei->i_inline_off)
417 return 0;
418
419 error = ext4_get_inode_loc(inode, &is.iloc);
420 if (error)
421 return error;
422
423 error = ext4_xattr_ibody_find(inode, &i, &is);
424 if (error)
425 goto out;
426
liang xie5d601252014-05-12 22:06:43 -0400427 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500428 error = ext4_journal_get_write_access(handle, is.iloc.bh);
429 if (error)
430 goto out;
431
Tao Ma0d812f72012-12-10 14:06:02 -0500432 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500433 if (error)
434 goto out;
435
436 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
437 0, EXT4_MIN_INLINE_DATA_SIZE);
Theodore Ts'oa5e063d2018-06-15 12:28:16 -0400438 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
Tao Ma67cf5b02012-12-10 14:04:46 -0500439
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400440 if (ext4_has_feature_extents(inode->i_sb)) {
Tao Ma67cf5b02012-12-10 14:04:46 -0500441 if (S_ISDIR(inode->i_mode) ||
442 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
443 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
444 ext4_ext_tree_init(handle, inode);
445 }
446 }
447 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
448
449 get_bh(is.iloc.bh);
450 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
451
452 EXT4_I(inode)->i_inline_off = 0;
453 EXT4_I(inode)->i_inline_size = 0;
454 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
455out:
456 brelse(is.iloc.bh);
457 if (error == -ENODATA)
458 error = 0;
459 return error;
460}
461
Tao Ma46c7f252012-12-10 14:04:52 -0500462static int ext4_read_inline_page(struct inode *inode, struct page *page)
463{
464 void *kaddr;
465 int ret = 0;
466 size_t len;
467 struct ext4_iloc iloc;
468
469 BUG_ON(!PageLocked(page));
470 BUG_ON(!ext4_has_inline_data(inode));
471 BUG_ON(page->index);
472
473 if (!EXT4_I(inode)->i_inline_off) {
474 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
475 inode->i_ino);
476 goto out;
477 }
478
479 ret = ext4_get_inode_loc(inode, &iloc);
480 if (ret)
481 goto out;
482
483 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
484 kaddr = kmap_atomic(page);
485 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
486 flush_dcache_page(page);
487 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300488 zero_user_segment(page, len, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500489 SetPageUptodate(page);
490 brelse(iloc.bh);
491
492out:
493 return ret;
494}
495
496int ext4_readpage_inline(struct inode *inode, struct page *page)
497{
498 int ret = 0;
499
500 down_read(&EXT4_I(inode)->xattr_sem);
501 if (!ext4_has_inline_data(inode)) {
502 up_read(&EXT4_I(inode)->xattr_sem);
503 return -EAGAIN;
504 }
505
Mohan Srinivasan009e6082017-02-10 14:26:23 -0800506 if (trace_android_fs_dataread_start_enabled()) {
507 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
508
509 path = android_fstrace_get_pathname(pathbuf,
510 MAX_TRACE_PATHBUF_LEN,
511 inode);
512 trace_android_fs_dataread_start(inode, page_offset(page),
513 PAGE_SIZE, current->pid,
514 path, current->comm);
515 }
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800516
Tao Ma46c7f252012-12-10 14:04:52 -0500517 /*
518 * Current inline data can only exist in the 1st page,
519 * So for all the other pages, just set them uptodate.
520 */
521 if (!page->index)
522 ret = ext4_read_inline_page(inode, page);
523 else if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300524 zero_user_segment(page, 0, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500525 SetPageUptodate(page);
526 }
527
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800528 trace_android_fs_dataread_end(inode, page_offset(page), PAGE_SIZE);
529
Tao Ma46c7f252012-12-10 14:04:52 -0500530 up_read(&EXT4_I(inode)->xattr_sem);
531
532 unlock_page(page);
533 return ret >= 0 ? 0 : ret;
534}
535
Tao Maf19d5872012-12-10 14:05:51 -0500536static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
537 struct inode *inode,
538 unsigned flags)
539{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500540 int ret, needed_blocks, no_expand;
Tao Maf19d5872012-12-10 14:05:51 -0500541 handle_t *handle = NULL;
542 int retries = 0, sem_held = 0;
543 struct page *page = NULL;
544 unsigned from, to;
545 struct ext4_iloc iloc;
546
547 if (!ext4_has_inline_data(inode)) {
548 /*
549 * clear the flag so that no new write
550 * will trap here again.
551 */
552 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
553 return 0;
554 }
555
556 needed_blocks = ext4_writepage_trans_blocks(inode);
557
558 ret = ext4_get_inode_loc(inode, &iloc);
559 if (ret)
560 return ret;
561
562retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500563 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Maf19d5872012-12-10 14:05:51 -0500564 if (IS_ERR(handle)) {
565 ret = PTR_ERR(handle);
566 handle = NULL;
567 goto out;
568 }
569
570 /* We cannot recurse into the filesystem as the transaction is already
571 * started */
572 flags |= AOP_FLAG_NOFS;
573
574 page = grab_cache_page_write_begin(mapping, 0, flags);
575 if (!page) {
576 ret = -ENOMEM;
577 goto out;
578 }
579
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500580 ext4_write_lock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500581 sem_held = 1;
582 /* If some one has already done this for us, just exit. */
583 if (!ext4_has_inline_data(inode)) {
584 ret = 0;
585 goto out;
586 }
587
588 from = 0;
589 to = ext4_get_inline_size(inode);
590 if (!PageUptodate(page)) {
591 ret = ext4_read_inline_page(inode, page);
592 if (ret < 0)
593 goto out;
594 }
595
596 ret = ext4_destroy_inline_data_nolock(handle, inode);
597 if (ret)
598 goto out;
599
Jan Kara705965b2016-03-08 23:08:10 -0500600 if (ext4_should_dioread_nolock(inode)) {
601 ret = __block_write_begin(page, from, to,
602 ext4_get_block_unwritten);
603 } else
Tao Maf19d5872012-12-10 14:05:51 -0500604 ret = __block_write_begin(page, from, to, ext4_get_block);
605
606 if (!ret && ext4_should_journal_data(inode)) {
607 ret = ext4_walk_page_buffers(handle, page_buffers(page),
608 from, to, NULL,
609 do_journal_get_write_access);
610 }
611
612 if (ret) {
613 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300614 put_page(page);
Darrick J. Wong684de5742014-09-11 11:45:12 -0400615 page = NULL;
Tao Maf19d5872012-12-10 14:05:51 -0500616 ext4_orphan_add(handle, inode);
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500617 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500618 sem_held = 0;
619 ext4_journal_stop(handle);
620 handle = NULL;
621 ext4_truncate_failed_write(inode);
622 /*
623 * If truncate failed early the inode might
624 * still be on the orphan list; we need to
625 * make sure the inode is removed from the
626 * orphan list in that case.
627 */
628 if (inode->i_nlink)
629 ext4_orphan_del(NULL, inode);
630 }
631
632 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
633 goto retry;
634
Darrick J. Wong684de5742014-09-11 11:45:12 -0400635 if (page)
636 block_commit_write(page, from, to);
Tao Maf19d5872012-12-10 14:05:51 -0500637out:
638 if (page) {
639 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300640 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500641 }
642 if (sem_held)
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500643 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500644 if (handle)
645 ext4_journal_stop(handle);
646 brelse(iloc.bh);
647 return ret;
648}
649
650/*
651 * Try to write data in the inode.
652 * If the inode has inline data, check whether the new write can be
653 * in the inode also. If not, create the page the handle, move the data
654 * to the page make it update and let the later codes create extent for it.
655 */
656int ext4_try_to_write_inline_data(struct address_space *mapping,
657 struct inode *inode,
658 loff_t pos, unsigned len,
659 unsigned flags,
660 struct page **pagep)
661{
662 int ret;
663 handle_t *handle;
664 struct page *page;
665 struct ext4_iloc iloc;
666
667 if (pos + len > ext4_get_max_inline_size(inode))
668 goto convert;
669
670 ret = ext4_get_inode_loc(inode, &iloc);
671 if (ret)
672 return ret;
673
674 /*
675 * The possible write could happen in the inode,
676 * so try to reserve the space in inode first.
677 */
Theodore Ts'o9924a922013-02-08 21:59:22 -0500678 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Tao Maf19d5872012-12-10 14:05:51 -0500679 if (IS_ERR(handle)) {
680 ret = PTR_ERR(handle);
681 handle = NULL;
682 goto out;
683 }
684
685 ret = ext4_prepare_inline_data(handle, inode, pos + len);
686 if (ret && ret != -ENOSPC)
687 goto out;
688
689 /* We don't have space in inline inode, so convert it to extent. */
690 if (ret == -ENOSPC) {
691 ext4_journal_stop(handle);
692 brelse(iloc.bh);
693 goto convert;
694 }
695
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400696 ret = ext4_journal_get_write_access(handle, iloc.bh);
697 if (ret)
698 goto out;
699
Tao Maf19d5872012-12-10 14:05:51 -0500700 flags |= AOP_FLAG_NOFS;
701
702 page = grab_cache_page_write_begin(mapping, 0, flags);
703 if (!page) {
704 ret = -ENOMEM;
705 goto out;
706 }
707
708 *pagep = page;
709 down_read(&EXT4_I(inode)->xattr_sem);
710 if (!ext4_has_inline_data(inode)) {
711 ret = 0;
712 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300713 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500714 goto out_up_read;
715 }
716
717 if (!PageUptodate(page)) {
718 ret = ext4_read_inline_page(inode, page);
Maurizio Lombardieaec1032018-12-04 00:06:53 -0500719 if (ret < 0) {
720 unlock_page(page);
721 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500722 goto out_up_read;
Maurizio Lombardieaec1032018-12-04 00:06:53 -0500723 }
Tao Maf19d5872012-12-10 14:05:51 -0500724 }
725
726 ret = 1;
727 handle = NULL;
728out_up_read:
729 up_read(&EXT4_I(inode)->xattr_sem);
730out:
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400731 if (handle && (ret != 1))
Tao Maf19d5872012-12-10 14:05:51 -0500732 ext4_journal_stop(handle);
733 brelse(iloc.bh);
734 return ret;
735convert:
736 return ext4_convert_inline_data_to_extent(mapping,
737 inode, flags);
738}
739
740int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
741 unsigned copied, struct page *page)
742{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500743 int ret, no_expand;
Tao Maf19d5872012-12-10 14:05:51 -0500744 void *kaddr;
745 struct ext4_iloc iloc;
746
747 if (unlikely(copied < len)) {
748 if (!PageUptodate(page)) {
749 copied = 0;
750 goto out;
751 }
752 }
753
754 ret = ext4_get_inode_loc(inode, &iloc);
755 if (ret) {
756 ext4_std_error(inode->i_sb, ret);
757 copied = 0;
758 goto out;
759 }
760
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500761 ext4_write_lock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500762 BUG_ON(!ext4_has_inline_data(inode));
763
764 kaddr = kmap_atomic(page);
765 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
766 kunmap_atomic(kaddr);
767 SetPageUptodate(page);
768 /* clear page dirty so that writepages wouldn't work for us. */
769 ClearPageDirty(page);
770
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500771 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500772 brelse(iloc.bh);
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400773 mark_inode_dirty(inode);
Tao Maf19d5872012-12-10 14:05:51 -0500774out:
775 return copied;
776}
777
Tao Ma3fdcfb62012-12-10 14:05:57 -0500778struct buffer_head *
779ext4_journalled_write_inline_data(struct inode *inode,
780 unsigned len,
781 struct page *page)
782{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500783 int ret, no_expand;
Tao Ma3fdcfb62012-12-10 14:05:57 -0500784 void *kaddr;
785 struct ext4_iloc iloc;
786
787 ret = ext4_get_inode_loc(inode, &iloc);
788 if (ret) {
789 ext4_std_error(inode->i_sb, ret);
790 return NULL;
791 }
792
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500793 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma3fdcfb62012-12-10 14:05:57 -0500794 kaddr = kmap_atomic(page);
795 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
796 kunmap_atomic(kaddr);
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500797 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma3fdcfb62012-12-10 14:05:57 -0500798
799 return iloc.bh;
800}
801
Tao Ma9c3569b2012-12-10 14:05:57 -0500802/*
803 * Try to make the page cache and handle ready for the inline data case.
804 * We can call this function in 2 cases:
805 * 1. The inode is created and the first write exceeds inline size. We can
806 * clear the inode state safely.
807 * 2. The inode has inline data, then we need to read the data, make it
808 * update and dirty so that ext4_da_writepages can handle it. We don't
809 * need to start the journal since the file's metatdata isn't changed now.
810 */
811static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
812 struct inode *inode,
813 unsigned flags,
814 void **fsdata)
815{
816 int ret = 0, inline_size;
817 struct page *page;
818
819 page = grab_cache_page_write_begin(mapping, 0, flags);
820 if (!page)
821 return -ENOMEM;
822
823 down_read(&EXT4_I(inode)->xattr_sem);
824 if (!ext4_has_inline_data(inode)) {
825 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
826 goto out;
827 }
828
829 inline_size = ext4_get_inline_size(inode);
830
831 if (!PageUptodate(page)) {
832 ret = ext4_read_inline_page(inode, page);
833 if (ret < 0)
834 goto out;
835 }
836
837 ret = __block_write_begin(page, 0, inline_size,
838 ext4_da_get_block_prep);
839 if (ret) {
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500840 up_read(&EXT4_I(inode)->xattr_sem);
841 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300842 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500843 ext4_truncate_failed_write(inode);
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500844 return ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500845 }
846
847 SetPageDirty(page);
848 SetPageUptodate(page);
849 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
850 *fsdata = (void *)CONVERT_INLINE_DATA;
851
852out:
853 up_read(&EXT4_I(inode)->xattr_sem);
854 if (page) {
855 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300856 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500857 }
858 return ret;
859}
860
861/*
862 * Prepare the write for the inline data.
863 * If the the data can be written into the inode, we just read
864 * the page and make it uptodate, and start the journal.
865 * Otherwise read the page, makes it dirty so that it can be
866 * handle in writepages(the i_disksize update is left to the
867 * normal ext4_da_write_end).
868 */
869int ext4_da_write_inline_data_begin(struct address_space *mapping,
870 struct inode *inode,
871 loff_t pos, unsigned len,
872 unsigned flags,
873 struct page **pagep,
874 void **fsdata)
875{
876 int ret, inline_size;
877 handle_t *handle;
878 struct page *page;
879 struct ext4_iloc iloc;
Lukas Czerner4c196202018-10-02 21:18:45 -0400880 int retries = 0;
Tao Ma9c3569b2012-12-10 14:05:57 -0500881
882 ret = ext4_get_inode_loc(inode, &iloc);
883 if (ret)
884 return ret;
885
Jan Karabc0ca9df2014-01-06 14:02:23 -0500886retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500887 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Tao Ma9c3569b2012-12-10 14:05:57 -0500888 if (IS_ERR(handle)) {
889 ret = PTR_ERR(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500890 goto out;
891 }
892
893 inline_size = ext4_get_max_inline_size(inode);
894
895 ret = -ENOSPC;
896 if (inline_size >= pos + len) {
897 ret = ext4_prepare_inline_data(handle, inode, pos + len);
898 if (ret && ret != -ENOSPC)
Jan Kara52e44772014-01-06 14:03:23 -0500899 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500900 }
901
Dmitry Monakhov5cc28a92014-12-02 16:09:50 -0500902 /*
903 * We cannot recurse into the filesystem as the transaction
904 * is already started.
905 */
906 flags |= AOP_FLAG_NOFS;
907
Tao Ma9c3569b2012-12-10 14:05:57 -0500908 if (ret == -ENOSPC) {
Theodore Ts'o7dd55892018-06-16 23:41:59 -0400909 ext4_journal_stop(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500910 ret = ext4_da_convert_inline_data_to_extent(mapping,
911 inode,
912 flags,
913 fsdata);
Jan Karabc0ca9df2014-01-06 14:02:23 -0500914 if (ret == -ENOSPC &&
915 ext4_should_retry_alloc(inode->i_sb, &retries))
916 goto retry_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500917 goto out;
918 }
919
Tao Ma9c3569b2012-12-10 14:05:57 -0500920 page = grab_cache_page_write_begin(mapping, 0, flags);
921 if (!page) {
922 ret = -ENOMEM;
Jan Kara52e44772014-01-06 14:03:23 -0500923 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500924 }
925
926 down_read(&EXT4_I(inode)->xattr_sem);
927 if (!ext4_has_inline_data(inode)) {
928 ret = 0;
929 goto out_release_page;
930 }
931
932 if (!PageUptodate(page)) {
933 ret = ext4_read_inline_page(inode, page);
934 if (ret < 0)
935 goto out_release_page;
936 }
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400937 ret = ext4_journal_get_write_access(handle, iloc.bh);
938 if (ret)
939 goto out_release_page;
Tao Ma9c3569b2012-12-10 14:05:57 -0500940
941 up_read(&EXT4_I(inode)->xattr_sem);
942 *pagep = page;
Tao Ma9c3569b2012-12-10 14:05:57 -0500943 brelse(iloc.bh);
944 return 1;
945out_release_page:
946 up_read(&EXT4_I(inode)->xattr_sem);
947 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300948 put_page(page);
Jan Kara52e44772014-01-06 14:03:23 -0500949out_journal:
950 ext4_journal_stop(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500951out:
Tao Ma9c3569b2012-12-10 14:05:57 -0500952 brelse(iloc.bh);
953 return ret;
954}
955
956int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
957 unsigned len, unsigned copied,
958 struct page *page)
959{
Theodore Ts'o0b37d0c2017-02-04 23:04:00 -0500960 int ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500961
Theodore Ts'o0b37d0c2017-02-04 23:04:00 -0500962 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
963 if (ret < 0) {
964 unlock_page(page);
965 put_page(page);
966 return ret;
967 }
968 copied = ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500969
970 /*
971 * No need to use i_size_read() here, the i_size
972 * cannot change under us because we hold i_mutex.
973 *
974 * But it's important to update i_size while still holding page lock:
975 * page writeout could otherwise come in and zero beyond i_size.
976 */
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400977 if (pos+copied > inode->i_size)
Tao Ma9c3569b2012-12-10 14:05:57 -0500978 i_size_write(inode, pos+copied);
Tao Ma9c3569b2012-12-10 14:05:57 -0500979 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300980 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500981
982 /*
983 * Don't mark the inode dirty under page lock. First, it unnecessarily
984 * makes the holding time of page lock longer. Second, it forces lock
985 * ordering of page lock and transaction start for journaling
986 * filesystems.
987 */
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400988 mark_inode_dirty(inode);
Tao Ma9c3569b2012-12-10 14:05:57 -0500989
990 return copied;
991}
Tao Maf19d5872012-12-10 14:05:51 -0500992
Tao Ma3c47d542012-12-10 14:05:59 -0500993#ifdef INLINE_DIR_DEBUG
994void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
995 void *inline_start, int inline_size)
996{
997 int offset;
998 unsigned short de_len;
999 struct ext4_dir_entry_2 *de = inline_start;
1000 void *dlimit = inline_start + inline_size;
1001
1002 trace_printk("inode %lu\n", dir->i_ino);
1003 offset = 0;
1004 while ((void *)de < dlimit) {
1005 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
Rasmus Villemoes80cfb712015-04-02 16:42:43 -04001006 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
Tao Ma3c47d542012-12-10 14:05:59 -05001007 offset, de_len, de->name_len, de->name,
1008 de->name_len, le32_to_cpu(de->inode));
1009 if (ext4_check_dir_entry(dir, NULL, de, bh,
1010 inline_start, inline_size, offset))
1011 BUG();
1012
1013 offset += de_len;
1014 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1015 }
1016}
1017#else
1018#define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1019#endif
1020
1021/*
1022 * Add a new entry into a inline dir.
1023 * It will return -ENOSPC if no space is available, and -EIO
1024 * and -EEXIST if directory entry already exists.
1025 */
1026static int ext4_add_dirent_to_inline(handle_t *handle,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001027 struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001028 struct inode *dir,
Tao Ma3c47d542012-12-10 14:05:59 -05001029 struct inode *inode,
1030 struct ext4_iloc *iloc,
1031 void *inline_start, int inline_size)
1032{
Tao Ma3c47d542012-12-10 14:05:59 -05001033 int err;
1034 struct ext4_dir_entry_2 *de;
1035
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001036 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1037 inline_size, fname, &de);
Tao Ma3c47d542012-12-10 14:05:59 -05001038 if (err)
1039 return err;
1040
liang xie5d601252014-05-12 22:06:43 -04001041 BUFFER_TRACE(iloc->bh, "get_write_access");
Tao Ma3c47d542012-12-10 14:05:59 -05001042 err = ext4_journal_get_write_access(handle, iloc->bh);
1043 if (err)
1044 return err;
Hyojun Kim63da4202017-10-06 17:10:08 -07001045 ext4_insert_dentry(inode, de, inline_size, fname);
Tao Ma3c47d542012-12-10 14:05:59 -05001046
1047 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1048
1049 /*
1050 * XXX shouldn't update any times until successful
1051 * completion of syscall, but too many callers depend
1052 * on this.
1053 *
1054 * XXX similarly, too many callers depend on
1055 * ext4_new_inode() setting the times, but error
1056 * recovery deletes the inode, so the worst that can
1057 * happen is that the times are slightly out of date
1058 * and/or different from the directory change time.
1059 */
1060 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1061 ext4_update_dx_flag(dir);
1062 dir->i_version++;
1063 ext4_mark_inode_dirty(handle, dir);
1064 return 1;
1065}
1066
1067static void *ext4_get_inline_xattr_pos(struct inode *inode,
1068 struct ext4_iloc *iloc)
1069{
1070 struct ext4_xattr_entry *entry;
1071 struct ext4_xattr_ibody_header *header;
1072
1073 BUG_ON(!EXT4_I(inode)->i_inline_off);
1074
1075 header = IHDR(inode, ext4_raw_inode(iloc));
1076 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1077 EXT4_I(inode)->i_inline_off);
1078
1079 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1080}
1081
1082/* Set the final de to cover the whole block. */
1083static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1084{
1085 struct ext4_dir_entry_2 *de, *prev_de;
1086 void *limit;
1087 int de_len;
1088
1089 de = (struct ext4_dir_entry_2 *)de_buf;
1090 if (old_size) {
1091 limit = de_buf + old_size;
1092 do {
1093 prev_de = de;
1094 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1095 de_buf += de_len;
1096 de = (struct ext4_dir_entry_2 *)de_buf;
1097 } while (de_buf < limit);
1098
1099 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1100 old_size, new_size);
1101 } else {
1102 /* this is just created, so create an empty entry. */
1103 de->inode = 0;
1104 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1105 }
1106}
1107
1108static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1109 struct ext4_iloc *iloc)
1110{
1111 int ret;
1112 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1113 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1114
1115 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1116 return -ENOSPC;
1117
1118 ret = ext4_update_inline_data(handle, dir,
1119 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1120 if (ret)
1121 return ret;
1122
1123 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1124 EXT4_I(dir)->i_inline_size -
1125 EXT4_MIN_INLINE_DATA_SIZE);
1126 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1127 return 0;
1128}
1129
1130static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1131 struct ext4_iloc *iloc,
1132 void *buf, int inline_size)
1133{
1134 ext4_create_inline_data(handle, inode, inline_size);
1135 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1136 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1137}
1138
1139static int ext4_finish_convert_inline_dir(handle_t *handle,
1140 struct inode *inode,
1141 struct buffer_head *dir_block,
1142 void *buf,
1143 int inline_size)
1144{
1145 int err, csum_size = 0, header_size = 0;
1146 struct ext4_dir_entry_2 *de;
1147 struct ext4_dir_entry_tail *t;
1148 void *target = dir_block->b_data;
1149
1150 /*
1151 * First create "." and ".." and then copy the dir information
1152 * back to the block.
1153 */
1154 de = (struct ext4_dir_entry_2 *)target;
1155 de = ext4_init_dot_dotdot(inode, de,
1156 inode->i_sb->s_blocksize, csum_size,
1157 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1158 header_size = (void *)de - target;
1159
1160 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1161 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1162
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001163 if (ext4_has_metadata_csum(inode->i_sb))
Tao Ma3c47d542012-12-10 14:05:59 -05001164 csum_size = sizeof(struct ext4_dir_entry_tail);
1165
1166 inode->i_size = inode->i_sb->s_blocksize;
1167 i_size_write(inode, inode->i_sb->s_blocksize);
1168 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1169 ext4_update_final_de(dir_block->b_data,
1170 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1171 inode->i_sb->s_blocksize - csum_size);
1172
1173 if (csum_size) {
1174 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1175 inode->i_sb->s_blocksize);
1176 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1177 }
1178 set_buffer_uptodate(dir_block);
1179 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1180 if (err)
Eric Biggersf572ba92017-03-15 14:52:02 -04001181 return err;
Tao Ma3c47d542012-12-10 14:05:59 -05001182 set_buffer_verified(dir_block);
Eric Biggersf572ba92017-03-15 14:52:02 -04001183 return ext4_mark_inode_dirty(handle, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05001184}
1185
1186static int ext4_convert_inline_data_nolock(handle_t *handle,
1187 struct inode *inode,
1188 struct ext4_iloc *iloc)
1189{
1190 int error;
1191 void *buf = NULL;
1192 struct buffer_head *data_bh = NULL;
1193 struct ext4_map_blocks map;
1194 int inline_size;
1195
1196 inline_size = ext4_get_inline_size(inode);
1197 buf = kmalloc(inline_size, GFP_NOFS);
1198 if (!buf) {
1199 error = -ENOMEM;
1200 goto out;
1201 }
1202
1203 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1204 if (error < 0)
1205 goto out;
1206
Darrick J. Wong40b163f2014-07-28 13:06:26 -04001207 /*
1208 * Make sure the inline directory entries pass checks before we try to
1209 * convert them, so that we avoid touching stuff that needs fsck.
1210 */
1211 if (S_ISDIR(inode->i_mode)) {
1212 error = ext4_check_all_de(inode, iloc->bh,
1213 buf + EXT4_INLINE_DOTDOT_SIZE,
1214 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1215 if (error)
1216 goto out;
1217 }
1218
Tao Ma3c47d542012-12-10 14:05:59 -05001219 error = ext4_destroy_inline_data_nolock(handle, inode);
1220 if (error)
1221 goto out;
1222
1223 map.m_lblk = 0;
1224 map.m_len = 1;
1225 map.m_flags = 0;
1226 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1227 if (error < 0)
1228 goto out_restore;
1229 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1230 error = -EIO;
1231 goto out_restore;
1232 }
1233
1234 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1235 if (!data_bh) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001236 error = -ENOMEM;
Tao Ma3c47d542012-12-10 14:05:59 -05001237 goto out_restore;
1238 }
1239
1240 lock_buffer(data_bh);
1241 error = ext4_journal_get_create_access(handle, data_bh);
1242 if (error) {
1243 unlock_buffer(data_bh);
1244 error = -EIO;
1245 goto out_restore;
1246 }
1247 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1248
1249 if (!S_ISDIR(inode->i_mode)) {
1250 memcpy(data_bh->b_data, buf, inline_size);
1251 set_buffer_uptodate(data_bh);
1252 error = ext4_handle_dirty_metadata(handle,
1253 inode, data_bh);
1254 } else {
1255 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1256 buf, inline_size);
1257 }
1258
1259 unlock_buffer(data_bh);
1260out_restore:
1261 if (error)
1262 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1263
1264out:
1265 brelse(data_bh);
1266 kfree(buf);
1267 return error;
1268}
1269
1270/*
1271 * Try to add the new entry to the inline data.
1272 * If succeeds, return 0. If not, extended the inline dir and copied data to
1273 * the new created block.
1274 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001275int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001276 struct inode *dir, struct inode *inode)
Tao Ma3c47d542012-12-10 14:05:59 -05001277{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001278 int ret, inline_size, no_expand;
Tao Ma3c47d542012-12-10 14:05:59 -05001279 void *inline_start;
1280 struct ext4_iloc iloc;
Tao Ma3c47d542012-12-10 14:05:59 -05001281
1282 ret = ext4_get_inode_loc(dir, &iloc);
1283 if (ret)
1284 return ret;
1285
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001286 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma3c47d542012-12-10 14:05:59 -05001287 if (!ext4_has_inline_data(dir))
1288 goto out;
1289
1290 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1291 EXT4_INLINE_DOTDOT_SIZE;
1292 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1293
Theodore Ts'o56a04912016-01-08 16:00:31 -05001294 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
Tao Ma3c47d542012-12-10 14:05:59 -05001295 inline_start, inline_size);
1296 if (ret != -ENOSPC)
1297 goto out;
1298
1299 /* check whether it can be inserted to inline xattr space. */
1300 inline_size = EXT4_I(dir)->i_inline_size -
1301 EXT4_MIN_INLINE_DATA_SIZE;
1302 if (!inline_size) {
1303 /* Try to use the xattr space.*/
1304 ret = ext4_update_inline_dir(handle, dir, &iloc);
1305 if (ret && ret != -ENOSPC)
1306 goto out;
1307
1308 inline_size = EXT4_I(dir)->i_inline_size -
1309 EXT4_MIN_INLINE_DATA_SIZE;
1310 }
1311
1312 if (inline_size) {
1313 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1314
Theodore Ts'o56a04912016-01-08 16:00:31 -05001315 ret = ext4_add_dirent_to_inline(handle, fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001316 inode, &iloc, inline_start,
1317 inline_size);
Tao Ma3c47d542012-12-10 14:05:59 -05001318
1319 if (ret != -ENOSPC)
1320 goto out;
1321 }
1322
1323 /*
1324 * The inline space is filled up, so create a new block for it.
1325 * As the extent tree will be created, we have to save the inline
1326 * dir first.
1327 */
1328 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1329
1330out:
1331 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001332 ext4_write_unlock_xattr(dir, &no_expand);
Tao Ma3c47d542012-12-10 14:05:59 -05001333 brelse(iloc.bh);
1334 return ret;
1335}
1336
Tao Ma8af0f082013-04-19 17:53:09 -04001337/*
1338 * This function fills a red-black tree with information from an
1339 * inlined dir. It returns the number directory entries loaded
1340 * into the tree. If there is an error it is returned in err.
1341 */
1342int htree_inlinedir_to_tree(struct file *dir_file,
1343 struct inode *dir, ext4_lblk_t block,
1344 struct dx_hash_info *hinfo,
1345 __u32 start_hash, __u32 start_minor_hash,
1346 int *has_inline_data)
1347{
1348 int err = 0, count = 0;
1349 unsigned int parent_ino;
1350 int pos;
1351 struct ext4_dir_entry_2 *de;
1352 struct inode *inode = file_inode(dir_file);
1353 int ret, inline_size = 0;
1354 struct ext4_iloc iloc;
1355 void *dir_buf = NULL;
1356 struct ext4_dir_entry_2 fake;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001357 struct fscrypt_str tmp_str;
Tao Ma8af0f082013-04-19 17:53:09 -04001358
1359 ret = ext4_get_inode_loc(inode, &iloc);
1360 if (ret)
1361 return ret;
1362
1363 down_read(&EXT4_I(inode)->xattr_sem);
1364 if (!ext4_has_inline_data(inode)) {
1365 up_read(&EXT4_I(inode)->xattr_sem);
1366 *has_inline_data = 0;
1367 goto out;
1368 }
1369
1370 inline_size = ext4_get_inline_size(inode);
1371 dir_buf = kmalloc(inline_size, GFP_NOFS);
1372 if (!dir_buf) {
1373 ret = -ENOMEM;
1374 up_read(&EXT4_I(inode)->xattr_sem);
1375 goto out;
1376 }
1377
1378 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1379 up_read(&EXT4_I(inode)->xattr_sem);
1380 if (ret < 0)
1381 goto out;
1382
1383 pos = 0;
1384 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1385 while (pos < inline_size) {
1386 /*
1387 * As inlined dir doesn't store any information about '.' and
1388 * only the inode number of '..' is stored, we have to handle
1389 * them differently.
1390 */
1391 if (pos == 0) {
1392 fake.inode = cpu_to_le32(inode->i_ino);
1393 fake.name_len = 1;
1394 strcpy(fake.name, ".");
1395 fake.rec_len = ext4_rec_len_to_disk(
1396 EXT4_DIR_REC_LEN(fake.name_len),
1397 inline_size);
1398 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1399 de = &fake;
1400 pos = EXT4_INLINE_DOTDOT_OFFSET;
1401 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1402 fake.inode = cpu_to_le32(parent_ino);
1403 fake.name_len = 2;
1404 strcpy(fake.name, "..");
1405 fake.rec_len = ext4_rec_len_to_disk(
1406 EXT4_DIR_REC_LEN(fake.name_len),
1407 inline_size);
1408 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1409 de = &fake;
1410 pos = EXT4_INLINE_DOTDOT_SIZE;
1411 } else {
1412 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1413 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1414 if (ext4_check_dir_entry(inode, dir_file, de,
1415 iloc.bh, dir_buf,
1416 inline_size, pos)) {
1417 ret = count;
1418 goto out;
1419 }
1420 }
1421
1422 ext4fs_dirhash(de->name, de->name_len, hinfo);
1423 if ((hinfo->hash < start_hash) ||
1424 ((hinfo->hash == start_hash) &&
1425 (hinfo->minor_hash < start_minor_hash)))
1426 continue;
1427 if (de->inode == 0)
1428 continue;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001429 tmp_str.name = de->name;
1430 tmp_str.len = de->name_len;
1431 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1432 hinfo->minor_hash, de, &tmp_str);
Tao Ma8af0f082013-04-19 17:53:09 -04001433 if (err) {
Colin Ian King24328d52019-08-12 14:29:38 -04001434 ret = err;
Tao Ma8af0f082013-04-19 17:53:09 -04001435 goto out;
1436 }
1437 count++;
1438 }
1439 ret = count;
1440out:
1441 kfree(dir_buf);
1442 brelse(iloc.bh);
1443 return ret;
1444}
1445
Tao Mac4d8b022013-04-19 17:55:33 -04001446/*
1447 * So this function is called when the volume is mkfsed with
1448 * dir_index disabled. In order to keep f_pos persistent
1449 * after we convert from an inlined dir to a blocked based,
1450 * we just pretend that we are a normal dir and return the
1451 * offset as if '.' and '..' really take place.
1452 *
1453 */
Al Viro725bebb2013-05-17 16:08:53 -04001454int ext4_read_inline_dir(struct file *file,
1455 struct dir_context *ctx,
Tao Ma65d165d2012-12-10 14:05:59 -05001456 int *has_inline_data)
1457{
Tao Ma65d165d2012-12-10 14:05:59 -05001458 unsigned int offset, parent_ino;
Al Viro725bebb2013-05-17 16:08:53 -04001459 int i;
Tao Ma65d165d2012-12-10 14:05:59 -05001460 struct ext4_dir_entry_2 *de;
1461 struct super_block *sb;
Al Viro725bebb2013-05-17 16:08:53 -04001462 struct inode *inode = file_inode(file);
Tao Ma65d165d2012-12-10 14:05:59 -05001463 int ret, inline_size = 0;
1464 struct ext4_iloc iloc;
1465 void *dir_buf = NULL;
Tao Mac4d8b022013-04-19 17:55:33 -04001466 int dotdot_offset, dotdot_size, extra_offset, extra_size;
Tao Ma65d165d2012-12-10 14:05:59 -05001467
1468 ret = ext4_get_inode_loc(inode, &iloc);
1469 if (ret)
1470 return ret;
1471
1472 down_read(&EXT4_I(inode)->xattr_sem);
1473 if (!ext4_has_inline_data(inode)) {
1474 up_read(&EXT4_I(inode)->xattr_sem);
1475 *has_inline_data = 0;
1476 goto out;
1477 }
1478
1479 inline_size = ext4_get_inline_size(inode);
1480 dir_buf = kmalloc(inline_size, GFP_NOFS);
1481 if (!dir_buf) {
1482 ret = -ENOMEM;
1483 up_read(&EXT4_I(inode)->xattr_sem);
1484 goto out;
1485 }
1486
1487 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1488 up_read(&EXT4_I(inode)->xattr_sem);
1489 if (ret < 0)
1490 goto out;
1491
BoxiLiu48ffdab2013-10-30 08:07:20 -04001492 ret = 0;
Tao Ma65d165d2012-12-10 14:05:59 -05001493 sb = inode->i_sb;
Tao Ma65d165d2012-12-10 14:05:59 -05001494 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
Al Viro725bebb2013-05-17 16:08:53 -04001495 offset = ctx->pos;
Tao Ma65d165d2012-12-10 14:05:59 -05001496
Tao Mac4d8b022013-04-19 17:55:33 -04001497 /*
1498 * dotdot_offset and dotdot_size is the real offset and
1499 * size for ".." and "." if the dir is block based while
1500 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1501 * So we will use extra_offset and extra_size to indicate them
1502 * during the inline dir iteration.
1503 */
1504 dotdot_offset = EXT4_DIR_REC_LEN(1);
1505 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1506 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1507 extra_size = extra_offset + inline_size;
1508
Al Viro725bebb2013-05-17 16:08:53 -04001509 /*
1510 * If the version has changed since the last call to
1511 * readdir(2), then we might be pointing to an invalid
1512 * dirent right now. Scan from the start of the inline
1513 * dir to make sure.
1514 */
1515 if (file->f_version != inode->i_version) {
1516 for (i = 0; i < extra_size && i < offset;) {
1517 /*
1518 * "." is with offset 0 and
1519 * ".." is dotdot_offset.
1520 */
1521 if (!i) {
1522 i = dotdot_offset;
1523 continue;
1524 } else if (i == dotdot_offset) {
1525 i = dotdot_size;
Tao Mac4d8b022013-04-19 17:55:33 -04001526 continue;
1527 }
Al Viro725bebb2013-05-17 16:08:53 -04001528 /* for other entry, the real offset in
1529 * the buf has to be tuned accordingly.
1530 */
Tao Mac4d8b022013-04-19 17:55:33 -04001531 de = (struct ext4_dir_entry_2 *)
Al Viro725bebb2013-05-17 16:08:53 -04001532 (dir_buf + i - extra_offset);
1533 /* It's too expensive to do a full
1534 * dirent test each time round this
1535 * loop, but we do have to test at
1536 * least that it is non-zero. A
1537 * failure will be detected in the
1538 * dirent test below. */
1539 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1540 < EXT4_DIR_REC_LEN(1))
1541 break;
1542 i += ext4_rec_len_from_disk(de->rec_len,
1543 extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001544 }
Al Viro725bebb2013-05-17 16:08:53 -04001545 offset = i;
1546 ctx->pos = offset;
1547 file->f_version = inode->i_version;
1548 }
1549
1550 while (ctx->pos < extra_size) {
1551 if (ctx->pos == 0) {
1552 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1553 goto out;
1554 ctx->pos = dotdot_offset;
1555 continue;
1556 }
1557
1558 if (ctx->pos == dotdot_offset) {
1559 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1560 goto out;
1561 ctx->pos = dotdot_size;
1562 continue;
1563 }
1564
1565 de = (struct ext4_dir_entry_2 *)
1566 (dir_buf + ctx->pos - extra_offset);
1567 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1568 extra_size, ctx->pos))
1569 goto out;
1570 if (le32_to_cpu(de->inode)) {
1571 if (!dir_emit(ctx, de->name, de->name_len,
1572 le32_to_cpu(de->inode),
1573 get_dtype(sb, de->file_type)))
1574 goto out;
1575 }
1576 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001577 }
1578out:
1579 kfree(dir_buf);
1580 brelse(iloc.bh);
1581 return ret;
1582}
1583
Tao Ma32f7f222012-12-10 14:06:01 -05001584struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1585 struct ext4_dir_entry_2 **parent_de,
1586 int *retval)
1587{
1588 struct ext4_iloc iloc;
1589
1590 *retval = ext4_get_inode_loc(inode, &iloc);
1591 if (*retval)
1592 return NULL;
1593
1594 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1595
1596 return iloc.bh;
1597}
1598
Tao Ma3c47d542012-12-10 14:05:59 -05001599/*
1600 * Try to create the inline data for the new dir.
1601 * If it succeeds, return 0, otherwise return the error.
1602 * In case of ENOSPC, the caller should create the normal disk layout dir.
1603 */
1604int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1605 struct inode *inode)
1606{
1607 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1608 struct ext4_iloc iloc;
1609 struct ext4_dir_entry_2 *de;
1610
1611 ret = ext4_get_inode_loc(inode, &iloc);
1612 if (ret)
1613 return ret;
1614
1615 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1616 if (ret)
1617 goto out;
1618
1619 /*
1620 * For inline dir, we only save the inode information for the ".."
1621 * and create a fake dentry to cover the left space.
1622 */
1623 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1624 de->inode = cpu_to_le32(parent->i_ino);
1625 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1626 de->inode = 0;
1627 de->rec_len = ext4_rec_len_to_disk(
1628 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1629 inline_size);
1630 set_nlink(inode, 2);
1631 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1632out:
1633 brelse(iloc.bh);
1634 return ret;
1635}
1636
Tao Mae8e948e2012-12-10 14:06:00 -05001637struct buffer_head *ext4_find_inline_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001638 struct ext4_filename *fname,
Tao Mae8e948e2012-12-10 14:06:00 -05001639 struct ext4_dir_entry_2 **res_dir,
1640 int *has_inline_data)
1641{
1642 int ret;
1643 struct ext4_iloc iloc;
1644 void *inline_start;
1645 int inline_size;
1646
1647 if (ext4_get_inode_loc(dir, &iloc))
1648 return NULL;
1649
1650 down_read(&EXT4_I(dir)->xattr_sem);
1651 if (!ext4_has_inline_data(dir)) {
1652 *has_inline_data = 0;
1653 goto out;
1654 }
1655
1656 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1657 EXT4_INLINE_DOTDOT_SIZE;
1658 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001659 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
Eric Biggers14404512017-05-24 18:10:49 -04001660 dir, fname, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001661 if (ret == 1)
1662 goto out_find;
1663 if (ret < 0)
1664 goto out;
1665
1666 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1667 goto out;
1668
1669 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1670 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1671
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001672 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
Eric Biggers14404512017-05-24 18:10:49 -04001673 dir, fname, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001674 if (ret == 1)
1675 goto out_find;
1676
1677out:
1678 brelse(iloc.bh);
1679 iloc.bh = NULL;
1680out_find:
1681 up_read(&EXT4_I(dir)->xattr_sem);
1682 return iloc.bh;
1683}
1684
Tao Ma9f40fe52012-12-10 14:06:00 -05001685int ext4_delete_inline_entry(handle_t *handle,
1686 struct inode *dir,
1687 struct ext4_dir_entry_2 *de_del,
1688 struct buffer_head *bh,
1689 int *has_inline_data)
1690{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001691 int err, inline_size, no_expand;
Tao Ma9f40fe52012-12-10 14:06:00 -05001692 struct ext4_iloc iloc;
1693 void *inline_start;
1694
1695 err = ext4_get_inode_loc(dir, &iloc);
1696 if (err)
1697 return err;
1698
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001699 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma9f40fe52012-12-10 14:06:00 -05001700 if (!ext4_has_inline_data(dir)) {
1701 *has_inline_data = 0;
1702 goto out;
1703 }
1704
1705 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1706 EXT4_MIN_INLINE_DATA_SIZE) {
1707 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1708 EXT4_INLINE_DOTDOT_SIZE;
1709 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1710 EXT4_INLINE_DOTDOT_SIZE;
1711 } else {
1712 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1713 inline_size = ext4_get_inline_size(dir) -
1714 EXT4_MIN_INLINE_DATA_SIZE;
1715 }
1716
liang xie5d601252014-05-12 22:06:43 -04001717 BUFFER_TRACE(bh, "get_write_access");
Tao Ma9f40fe52012-12-10 14:06:00 -05001718 err = ext4_journal_get_write_access(handle, bh);
1719 if (err)
1720 goto out;
1721
1722 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1723 inline_start, inline_size, 0);
1724 if (err)
1725 goto out;
1726
Tao Ma9f40fe52012-12-10 14:06:00 -05001727 err = ext4_mark_inode_dirty(handle, dir);
1728 if (unlikely(err))
1729 goto out;
1730
1731 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1732out:
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001733 ext4_write_unlock_xattr(dir, &no_expand);
Tao Ma9f40fe52012-12-10 14:06:00 -05001734 brelse(iloc.bh);
1735 if (err != -ENOENT)
1736 ext4_std_error(dir->i_sb, err);
1737 return err;
1738}
1739
Tao Ma61f86632012-12-10 14:06:01 -05001740/*
1741 * Get the inline dentry at offset.
1742 */
1743static inline struct ext4_dir_entry_2 *
1744ext4_get_inline_entry(struct inode *inode,
1745 struct ext4_iloc *iloc,
1746 unsigned int offset,
1747 void **inline_start,
1748 int *inline_size)
1749{
1750 void *inline_pos;
1751
1752 BUG_ON(offset > ext4_get_inline_size(inode));
1753
1754 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1755 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1756 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1757 } else {
1758 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1759 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1760 *inline_size = ext4_get_inline_size(inode) -
1761 EXT4_MIN_INLINE_DATA_SIZE;
1762 }
1763
1764 if (inline_start)
1765 *inline_start = inline_pos;
1766 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1767}
1768
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001769bool empty_inline_dir(struct inode *dir, int *has_inline_data)
Tao Ma61f86632012-12-10 14:06:01 -05001770{
1771 int err, inline_size;
1772 struct ext4_iloc iloc;
Theodore Ts'oacf32cf2018-08-27 09:22:45 -04001773 size_t inline_len;
Tao Ma61f86632012-12-10 14:06:01 -05001774 void *inline_pos;
1775 unsigned int offset;
1776 struct ext4_dir_entry_2 *de;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001777 bool ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001778
1779 err = ext4_get_inode_loc(dir, &iloc);
1780 if (err) {
1781 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1782 err, dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001783 return true;
Tao Ma61f86632012-12-10 14:06:01 -05001784 }
1785
1786 down_read(&EXT4_I(dir)->xattr_sem);
1787 if (!ext4_has_inline_data(dir)) {
1788 *has_inline_data = 0;
1789 goto out;
1790 }
1791
1792 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1793 if (!le32_to_cpu(de->inode)) {
1794 ext4_warning(dir->i_sb,
1795 "bad inline directory (dir #%lu) - no `..'",
1796 dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001797 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001798 goto out;
1799 }
1800
Theodore Ts'oacf32cf2018-08-27 09:22:45 -04001801 inline_len = ext4_get_inline_size(dir);
Tao Ma61f86632012-12-10 14:06:01 -05001802 offset = EXT4_INLINE_DOTDOT_SIZE;
Theodore Ts'oacf32cf2018-08-27 09:22:45 -04001803 while (offset < inline_len) {
Tao Ma61f86632012-12-10 14:06:01 -05001804 de = ext4_get_inline_entry(dir, &iloc, offset,
1805 &inline_pos, &inline_size);
1806 if (ext4_check_dir_entry(dir, NULL, de,
1807 iloc.bh, inline_pos,
1808 inline_size, offset)) {
1809 ext4_warning(dir->i_sb,
1810 "bad inline directory (dir #%lu) - "
1811 "inode %u, rec_len %u, name_len %d"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001812 "inline size %d",
Tao Ma61f86632012-12-10 14:06:01 -05001813 dir->i_ino, le32_to_cpu(de->inode),
1814 le16_to_cpu(de->rec_len), de->name_len,
1815 inline_size);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001816 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001817 goto out;
1818 }
1819 if (le32_to_cpu(de->inode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001820 ret = false;
Tao Ma61f86632012-12-10 14:06:01 -05001821 goto out;
1822 }
1823 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1824 }
1825
1826out:
1827 up_read(&EXT4_I(dir)->xattr_sem);
1828 brelse(iloc.bh);
1829 return ret;
1830}
1831
Tao Ma67cf5b02012-12-10 14:04:46 -05001832int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1833{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001834 int ret, no_expand;
Tao Ma67cf5b02012-12-10 14:04:46 -05001835
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001836 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001837 ret = ext4_destroy_inline_data_nolock(handle, inode);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001838 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001839
1840 return ret;
1841}
Tao Ma94191982012-12-10 14:06:02 -05001842
1843int ext4_inline_data_fiemap(struct inode *inode,
1844 struct fiemap_extent_info *fieinfo,
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001845 int *has_inline, __u64 start, __u64 len)
Tao Ma94191982012-12-10 14:06:02 -05001846{
1847 __u64 physical = 0;
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001848 __u64 inline_len;
1849 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1850 FIEMAP_EXTENT_LAST;
Tao Ma94191982012-12-10 14:06:02 -05001851 int error = 0;
1852 struct ext4_iloc iloc;
1853
1854 down_read(&EXT4_I(inode)->xattr_sem);
1855 if (!ext4_has_inline_data(inode)) {
1856 *has_inline = 0;
1857 goto out;
1858 }
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001859 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1860 i_size_read(inode));
1861 if (start >= inline_len)
1862 goto out;
1863 if (start + len < inline_len)
1864 inline_len = start + len;
1865 inline_len -= start;
Tao Ma94191982012-12-10 14:06:02 -05001866
1867 error = ext4_get_inode_loc(inode, &iloc);
1868 if (error)
1869 goto out;
1870
Jan Karaeaf37932013-05-31 19:33:42 -04001871 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
Tao Ma94191982012-12-10 14:06:02 -05001872 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1873 physical += offsetof(struct ext4_inode, i_block);
Tao Ma94191982012-12-10 14:06:02 -05001874
Tao Ma94191982012-12-10 14:06:02 -05001875 brelse(iloc.bh);
1876out:
1877 up_read(&EXT4_I(inode)->xattr_sem);
Theodore Ts'oeb24a3b2018-12-25 00:56:33 -05001878 if (physical)
1879 error = fiemap_fill_next_extent(fieinfo, start, physical,
1880 inline_len, flags);
Tao Ma94191982012-12-10 14:06:02 -05001881 return (error < 0 ? error : 0);
1882}
Tao Ma0d812f72012-12-10 14:06:02 -05001883
Tao Maaef1c852012-12-10 14:06:02 -05001884void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1885{
1886 handle_t *handle;
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001887 int inline_size, value_len, needed_blocks, no_expand;
Tao Maaef1c852012-12-10 14:06:02 -05001888 size_t i_size;
1889 void *value = NULL;
1890 struct ext4_xattr_ibody_find is = {
1891 .s = { .not_found = -ENODATA, },
1892 };
1893 struct ext4_xattr_info i = {
1894 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1895 .name = EXT4_XATTR_SYSTEM_DATA,
1896 };
1897
1898
1899 needed_blocks = ext4_writepage_trans_blocks(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -05001900 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
Tao Maaef1c852012-12-10 14:06:02 -05001901 if (IS_ERR(handle))
1902 return;
1903
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001904 ext4_write_lock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05001905 if (!ext4_has_inline_data(inode)) {
1906 *has_inline = 0;
1907 ext4_journal_stop(handle);
1908 return;
1909 }
1910
1911 if (ext4_orphan_add(handle, inode))
1912 goto out;
1913
1914 if (ext4_get_inode_loc(inode, &is.iloc))
1915 goto out;
1916
1917 down_write(&EXT4_I(inode)->i_data_sem);
1918 i_size = inode->i_size;
1919 inline_size = ext4_get_inline_size(inode);
1920 EXT4_I(inode)->i_disksize = i_size;
1921
1922 if (i_size < inline_size) {
1923 /* Clear the content in the xattr space. */
1924 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1925 if (ext4_xattr_ibody_find(inode, &i, &is))
1926 goto out_error;
1927
1928 BUG_ON(is.s.not_found);
1929
1930 value_len = le32_to_cpu(is.s.here->e_value_size);
1931 value = kmalloc(value_len, GFP_NOFS);
1932 if (!value)
1933 goto out_error;
1934
1935 if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
1936 value, value_len))
1937 goto out_error;
1938
1939 i.value = value;
1940 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1941 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1942 if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
1943 goto out_error;
1944 }
1945
1946 /* Clear the content within i_blocks. */
Theodore Ts'o09c455a2014-01-07 12:58:19 -05001947 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1948 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1949 memset(p + i_size, 0,
1950 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1951 }
Tao Maaef1c852012-12-10 14:06:02 -05001952
1953 EXT4_I(inode)->i_inline_size = i_size <
1954 EXT4_MIN_INLINE_DATA_SIZE ?
1955 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1956 }
1957
1958out_error:
1959 up_write(&EXT4_I(inode)->i_data_sem);
1960out:
1961 brelse(is.iloc.bh);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001962 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05001963 kfree(value);
1964 if (inode->i_nlink)
1965 ext4_orphan_del(handle, inode);
1966
1967 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
1968 ext4_mark_inode_dirty(handle, inode);
1969 if (IS_SYNC(inode))
1970 ext4_handle_sync(handle);
1971
1972 ext4_journal_stop(handle);
1973 return;
1974}
Tao Ma0c8d4142012-12-10 14:06:03 -05001975
1976int ext4_convert_inline_data(struct inode *inode)
1977{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001978 int error, needed_blocks, no_expand;
Tao Ma0c8d4142012-12-10 14:06:03 -05001979 handle_t *handle;
1980 struct ext4_iloc iloc;
1981
1982 if (!ext4_has_inline_data(inode)) {
1983 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1984 return 0;
1985 }
1986
1987 needed_blocks = ext4_writepage_trans_blocks(inode);
1988
1989 iloc.bh = NULL;
1990 error = ext4_get_inode_loc(inode, &iloc);
1991 if (error)
1992 return error;
1993
Theodore Ts'o9924a922013-02-08 21:59:22 -05001994 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Ma0c8d4142012-12-10 14:06:03 -05001995 if (IS_ERR(handle)) {
1996 error = PTR_ERR(handle);
1997 goto out_free;
1998 }
1999
Theodore Ts'oda1e4022017-01-11 21:50:46 -05002000 ext4_write_lock_xattr(inode, &no_expand);
2001 if (ext4_has_inline_data(inode))
2002 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2003 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma0c8d4142012-12-10 14:06:03 -05002004 ext4_journal_stop(handle);
2005out_free:
2006 brelse(iloc.bh);
2007 return error;
2008}