blob: 4d78b932c320a2e7254ceeb9efe1fe24b604199e [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);
438
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400439 if (ext4_has_feature_extents(inode->i_sb)) {
Tao Ma67cf5b02012-12-10 14:04:46 -0500440 if (S_ISDIR(inode->i_mode) ||
441 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
442 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
443 ext4_ext_tree_init(handle, inode);
444 }
445 }
446 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
447
448 get_bh(is.iloc.bh);
449 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
450
451 EXT4_I(inode)->i_inline_off = 0;
452 EXT4_I(inode)->i_inline_size = 0;
453 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
454out:
455 brelse(is.iloc.bh);
456 if (error == -ENODATA)
457 error = 0;
458 return error;
459}
460
Tao Ma46c7f252012-12-10 14:04:52 -0500461static int ext4_read_inline_page(struct inode *inode, struct page *page)
462{
463 void *kaddr;
464 int ret = 0;
465 size_t len;
466 struct ext4_iloc iloc;
467
468 BUG_ON(!PageLocked(page));
469 BUG_ON(!ext4_has_inline_data(inode));
470 BUG_ON(page->index);
471
472 if (!EXT4_I(inode)->i_inline_off) {
473 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
474 inode->i_ino);
475 goto out;
476 }
477
478 ret = ext4_get_inode_loc(inode, &iloc);
479 if (ret)
480 goto out;
481
482 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
483 kaddr = kmap_atomic(page);
484 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
485 flush_dcache_page(page);
486 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300487 zero_user_segment(page, len, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500488 SetPageUptodate(page);
489 brelse(iloc.bh);
490
491out:
492 return ret;
493}
494
495int ext4_readpage_inline(struct inode *inode, struct page *page)
496{
497 int ret = 0;
498
499 down_read(&EXT4_I(inode)->xattr_sem);
500 if (!ext4_has_inline_data(inode)) {
501 up_read(&EXT4_I(inode)->xattr_sem);
502 return -EAGAIN;
503 }
504
Mohan Srinivasan009e6082017-02-10 14:26:23 -0800505 if (trace_android_fs_dataread_start_enabled()) {
506 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
507
508 path = android_fstrace_get_pathname(pathbuf,
509 MAX_TRACE_PATHBUF_LEN,
510 inode);
511 trace_android_fs_dataread_start(inode, page_offset(page),
512 PAGE_SIZE, current->pid,
513 path, current->comm);
514 }
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800515
Tao Ma46c7f252012-12-10 14:04:52 -0500516 /*
517 * Current inline data can only exist in the 1st page,
518 * So for all the other pages, just set them uptodate.
519 */
520 if (!page->index)
521 ret = ext4_read_inline_page(inode, page);
522 else if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300523 zero_user_segment(page, 0, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500524 SetPageUptodate(page);
525 }
526
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800527 trace_android_fs_dataread_end(inode, page_offset(page), PAGE_SIZE);
528
Tao Ma46c7f252012-12-10 14:04:52 -0500529 up_read(&EXT4_I(inode)->xattr_sem);
530
531 unlock_page(page);
532 return ret >= 0 ? 0 : ret;
533}
534
Tao Maf19d5872012-12-10 14:05:51 -0500535static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
536 struct inode *inode,
537 unsigned flags)
538{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500539 int ret, needed_blocks, no_expand;
Tao Maf19d5872012-12-10 14:05:51 -0500540 handle_t *handle = NULL;
541 int retries = 0, sem_held = 0;
542 struct page *page = NULL;
543 unsigned from, to;
544 struct ext4_iloc iloc;
545
546 if (!ext4_has_inline_data(inode)) {
547 /*
548 * clear the flag so that no new write
549 * will trap here again.
550 */
551 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
552 return 0;
553 }
554
555 needed_blocks = ext4_writepage_trans_blocks(inode);
556
557 ret = ext4_get_inode_loc(inode, &iloc);
558 if (ret)
559 return ret;
560
561retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500562 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Maf19d5872012-12-10 14:05:51 -0500563 if (IS_ERR(handle)) {
564 ret = PTR_ERR(handle);
565 handle = NULL;
566 goto out;
567 }
568
569 /* We cannot recurse into the filesystem as the transaction is already
570 * started */
571 flags |= AOP_FLAG_NOFS;
572
573 page = grab_cache_page_write_begin(mapping, 0, flags);
574 if (!page) {
575 ret = -ENOMEM;
576 goto out;
577 }
578
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500579 ext4_write_lock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500580 sem_held = 1;
581 /* If some one has already done this for us, just exit. */
582 if (!ext4_has_inline_data(inode)) {
583 ret = 0;
584 goto out;
585 }
586
587 from = 0;
588 to = ext4_get_inline_size(inode);
589 if (!PageUptodate(page)) {
590 ret = ext4_read_inline_page(inode, page);
591 if (ret < 0)
592 goto out;
593 }
594
595 ret = ext4_destroy_inline_data_nolock(handle, inode);
596 if (ret)
597 goto out;
598
Jan Kara705965b2016-03-08 23:08:10 -0500599 if (ext4_should_dioread_nolock(inode)) {
600 ret = __block_write_begin(page, from, to,
601 ext4_get_block_unwritten);
602 } else
Tao Maf19d5872012-12-10 14:05:51 -0500603 ret = __block_write_begin(page, from, to, ext4_get_block);
604
605 if (!ret && ext4_should_journal_data(inode)) {
606 ret = ext4_walk_page_buffers(handle, page_buffers(page),
607 from, to, NULL,
608 do_journal_get_write_access);
609 }
610
611 if (ret) {
612 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300613 put_page(page);
Darrick J. Wong684de5742014-09-11 11:45:12 -0400614 page = NULL;
Tao Maf19d5872012-12-10 14:05:51 -0500615 ext4_orphan_add(handle, inode);
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500616 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500617 sem_held = 0;
618 ext4_journal_stop(handle);
619 handle = NULL;
620 ext4_truncate_failed_write(inode);
621 /*
622 * If truncate failed early the inode might
623 * still be on the orphan list; we need to
624 * make sure the inode is removed from the
625 * orphan list in that case.
626 */
627 if (inode->i_nlink)
628 ext4_orphan_del(NULL, inode);
629 }
630
631 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
632 goto retry;
633
Darrick J. Wong684de5742014-09-11 11:45:12 -0400634 if (page)
635 block_commit_write(page, from, to);
Tao Maf19d5872012-12-10 14:05:51 -0500636out:
637 if (page) {
638 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300639 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500640 }
641 if (sem_held)
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500642 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500643 if (handle)
644 ext4_journal_stop(handle);
645 brelse(iloc.bh);
646 return ret;
647}
648
649/*
650 * Try to write data in the inode.
651 * If the inode has inline data, check whether the new write can be
652 * in the inode also. If not, create the page the handle, move the data
653 * to the page make it update and let the later codes create extent for it.
654 */
655int ext4_try_to_write_inline_data(struct address_space *mapping,
656 struct inode *inode,
657 loff_t pos, unsigned len,
658 unsigned flags,
659 struct page **pagep)
660{
661 int ret;
662 handle_t *handle;
663 struct page *page;
664 struct ext4_iloc iloc;
665
666 if (pos + len > ext4_get_max_inline_size(inode))
667 goto convert;
668
669 ret = ext4_get_inode_loc(inode, &iloc);
670 if (ret)
671 return ret;
672
673 /*
674 * The possible write could happen in the inode,
675 * so try to reserve the space in inode first.
676 */
Theodore Ts'o9924a922013-02-08 21:59:22 -0500677 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Tao Maf19d5872012-12-10 14:05:51 -0500678 if (IS_ERR(handle)) {
679 ret = PTR_ERR(handle);
680 handle = NULL;
681 goto out;
682 }
683
684 ret = ext4_prepare_inline_data(handle, inode, pos + len);
685 if (ret && ret != -ENOSPC)
686 goto out;
687
688 /* We don't have space in inline inode, so convert it to extent. */
689 if (ret == -ENOSPC) {
690 ext4_journal_stop(handle);
691 brelse(iloc.bh);
692 goto convert;
693 }
694
695 flags |= AOP_FLAG_NOFS;
696
697 page = grab_cache_page_write_begin(mapping, 0, flags);
698 if (!page) {
699 ret = -ENOMEM;
700 goto out;
701 }
702
703 *pagep = page;
704 down_read(&EXT4_I(inode)->xattr_sem);
705 if (!ext4_has_inline_data(inode)) {
706 ret = 0;
707 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300708 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500709 goto out_up_read;
710 }
711
712 if (!PageUptodate(page)) {
713 ret = ext4_read_inline_page(inode, page);
714 if (ret < 0)
715 goto out_up_read;
716 }
717
718 ret = 1;
719 handle = NULL;
720out_up_read:
721 up_read(&EXT4_I(inode)->xattr_sem);
722out:
723 if (handle)
724 ext4_journal_stop(handle);
725 brelse(iloc.bh);
726 return ret;
727convert:
728 return ext4_convert_inline_data_to_extent(mapping,
729 inode, flags);
730}
731
732int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
733 unsigned copied, struct page *page)
734{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500735 int ret, no_expand;
Tao Maf19d5872012-12-10 14:05:51 -0500736 void *kaddr;
737 struct ext4_iloc iloc;
738
739 if (unlikely(copied < len)) {
740 if (!PageUptodate(page)) {
741 copied = 0;
742 goto out;
743 }
744 }
745
746 ret = ext4_get_inode_loc(inode, &iloc);
747 if (ret) {
748 ext4_std_error(inode->i_sb, ret);
749 copied = 0;
750 goto out;
751 }
752
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500753 ext4_write_lock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500754 BUG_ON(!ext4_has_inline_data(inode));
755
756 kaddr = kmap_atomic(page);
757 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
758 kunmap_atomic(kaddr);
759 SetPageUptodate(page);
760 /* clear page dirty so that writepages wouldn't work for us. */
761 ClearPageDirty(page);
762
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500763 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500764 brelse(iloc.bh);
765out:
766 return copied;
767}
768
Tao Ma3fdcfb62012-12-10 14:05:57 -0500769struct buffer_head *
770ext4_journalled_write_inline_data(struct inode *inode,
771 unsigned len,
772 struct page *page)
773{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500774 int ret, no_expand;
Tao Ma3fdcfb62012-12-10 14:05:57 -0500775 void *kaddr;
776 struct ext4_iloc iloc;
777
778 ret = ext4_get_inode_loc(inode, &iloc);
779 if (ret) {
780 ext4_std_error(inode->i_sb, ret);
781 return NULL;
782 }
783
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500784 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma3fdcfb62012-12-10 14:05:57 -0500785 kaddr = kmap_atomic(page);
786 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
787 kunmap_atomic(kaddr);
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500788 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma3fdcfb62012-12-10 14:05:57 -0500789
790 return iloc.bh;
791}
792
Tao Ma9c3569b2012-12-10 14:05:57 -0500793/*
794 * Try to make the page cache and handle ready for the inline data case.
795 * We can call this function in 2 cases:
796 * 1. The inode is created and the first write exceeds inline size. We can
797 * clear the inode state safely.
798 * 2. The inode has inline data, then we need to read the data, make it
799 * update and dirty so that ext4_da_writepages can handle it. We don't
800 * need to start the journal since the file's metatdata isn't changed now.
801 */
802static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
803 struct inode *inode,
804 unsigned flags,
805 void **fsdata)
806{
807 int ret = 0, inline_size;
808 struct page *page;
809
810 page = grab_cache_page_write_begin(mapping, 0, flags);
811 if (!page)
812 return -ENOMEM;
813
814 down_read(&EXT4_I(inode)->xattr_sem);
815 if (!ext4_has_inline_data(inode)) {
816 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
817 goto out;
818 }
819
820 inline_size = ext4_get_inline_size(inode);
821
822 if (!PageUptodate(page)) {
823 ret = ext4_read_inline_page(inode, page);
824 if (ret < 0)
825 goto out;
826 }
827
828 ret = __block_write_begin(page, 0, inline_size,
829 ext4_da_get_block_prep);
830 if (ret) {
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500831 up_read(&EXT4_I(inode)->xattr_sem);
832 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300833 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500834 ext4_truncate_failed_write(inode);
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500835 return ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500836 }
837
838 SetPageDirty(page);
839 SetPageUptodate(page);
840 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
841 *fsdata = (void *)CONVERT_INLINE_DATA;
842
843out:
844 up_read(&EXT4_I(inode)->xattr_sem);
845 if (page) {
846 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300847 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500848 }
849 return ret;
850}
851
852/*
853 * Prepare the write for the inline data.
854 * If the the data can be written into the inode, we just read
855 * the page and make it uptodate, and start the journal.
856 * Otherwise read the page, makes it dirty so that it can be
857 * handle in writepages(the i_disksize update is left to the
858 * normal ext4_da_write_end).
859 */
860int ext4_da_write_inline_data_begin(struct address_space *mapping,
861 struct inode *inode,
862 loff_t pos, unsigned len,
863 unsigned flags,
864 struct page **pagep,
865 void **fsdata)
866{
867 int ret, inline_size;
868 handle_t *handle;
869 struct page *page;
870 struct ext4_iloc iloc;
Jan Karabc0ca9df2014-01-06 14:02:23 -0500871 int retries;
Tao Ma9c3569b2012-12-10 14:05:57 -0500872
873 ret = ext4_get_inode_loc(inode, &iloc);
874 if (ret)
875 return ret;
876
Jan Karabc0ca9df2014-01-06 14:02:23 -0500877retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500878 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Tao Ma9c3569b2012-12-10 14:05:57 -0500879 if (IS_ERR(handle)) {
880 ret = PTR_ERR(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500881 goto out;
882 }
883
884 inline_size = ext4_get_max_inline_size(inode);
885
886 ret = -ENOSPC;
887 if (inline_size >= pos + len) {
888 ret = ext4_prepare_inline_data(handle, inode, pos + len);
889 if (ret && ret != -ENOSPC)
Jan Kara52e44772014-01-06 14:03:23 -0500890 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500891 }
892
Dmitry Monakhov5cc28a92014-12-02 16:09:50 -0500893 /*
894 * We cannot recurse into the filesystem as the transaction
895 * is already started.
896 */
897 flags |= AOP_FLAG_NOFS;
898
Tao Ma9c3569b2012-12-10 14:05:57 -0500899 if (ret == -ENOSPC) {
900 ret = ext4_da_convert_inline_data_to_extent(mapping,
901 inode,
902 flags,
903 fsdata);
Jan Karabc0ca9df2014-01-06 14:02:23 -0500904 ext4_journal_stop(handle);
Jan Karabc0ca9df2014-01-06 14:02:23 -0500905 if (ret == -ENOSPC &&
906 ext4_should_retry_alloc(inode->i_sb, &retries))
907 goto retry_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500908 goto out;
909 }
910
Tao Ma9c3569b2012-12-10 14:05:57 -0500911
912 page = grab_cache_page_write_begin(mapping, 0, flags);
913 if (!page) {
914 ret = -ENOMEM;
Jan Kara52e44772014-01-06 14:03:23 -0500915 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500916 }
917
918 down_read(&EXT4_I(inode)->xattr_sem);
919 if (!ext4_has_inline_data(inode)) {
920 ret = 0;
921 goto out_release_page;
922 }
923
924 if (!PageUptodate(page)) {
925 ret = ext4_read_inline_page(inode, page);
926 if (ret < 0)
927 goto out_release_page;
928 }
929
930 up_read(&EXT4_I(inode)->xattr_sem);
931 *pagep = page;
Tao Ma9c3569b2012-12-10 14:05:57 -0500932 brelse(iloc.bh);
933 return 1;
934out_release_page:
935 up_read(&EXT4_I(inode)->xattr_sem);
936 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300937 put_page(page);
Jan Kara52e44772014-01-06 14:03:23 -0500938out_journal:
939 ext4_journal_stop(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500940out:
Tao Ma9c3569b2012-12-10 14:05:57 -0500941 brelse(iloc.bh);
942 return ret;
943}
944
945int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
946 unsigned len, unsigned copied,
947 struct page *page)
948{
949 int i_size_changed = 0;
Theodore Ts'o0b37d0c2017-02-04 23:04:00 -0500950 int ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500951
Theodore Ts'o0b37d0c2017-02-04 23:04:00 -0500952 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
953 if (ret < 0) {
954 unlock_page(page);
955 put_page(page);
956 return ret;
957 }
958 copied = ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500959
960 /*
961 * No need to use i_size_read() here, the i_size
962 * cannot change under us because we hold i_mutex.
963 *
964 * But it's important to update i_size while still holding page lock:
965 * page writeout could otherwise come in and zero beyond i_size.
966 */
967 if (pos+copied > inode->i_size) {
968 i_size_write(inode, pos+copied);
969 i_size_changed = 1;
970 }
971 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300972 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500973
974 /*
975 * Don't mark the inode dirty under page lock. First, it unnecessarily
976 * makes the holding time of page lock longer. Second, it forces lock
977 * ordering of page lock and transaction start for journaling
978 * filesystems.
979 */
980 if (i_size_changed)
981 mark_inode_dirty(inode);
982
983 return copied;
984}
Tao Maf19d5872012-12-10 14:05:51 -0500985
Tao Ma3c47d542012-12-10 14:05:59 -0500986#ifdef INLINE_DIR_DEBUG
987void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
988 void *inline_start, int inline_size)
989{
990 int offset;
991 unsigned short de_len;
992 struct ext4_dir_entry_2 *de = inline_start;
993 void *dlimit = inline_start + inline_size;
994
995 trace_printk("inode %lu\n", dir->i_ino);
996 offset = 0;
997 while ((void *)de < dlimit) {
998 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
Rasmus Villemoes80cfb712015-04-02 16:42:43 -0400999 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
Tao Ma3c47d542012-12-10 14:05:59 -05001000 offset, de_len, de->name_len, de->name,
1001 de->name_len, le32_to_cpu(de->inode));
1002 if (ext4_check_dir_entry(dir, NULL, de, bh,
1003 inline_start, inline_size, offset))
1004 BUG();
1005
1006 offset += de_len;
1007 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1008 }
1009}
1010#else
1011#define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1012#endif
1013
1014/*
1015 * Add a new entry into a inline dir.
1016 * It will return -ENOSPC if no space is available, and -EIO
1017 * and -EEXIST if directory entry already exists.
1018 */
1019static int ext4_add_dirent_to_inline(handle_t *handle,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001020 struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001021 struct inode *dir,
Tao Ma3c47d542012-12-10 14:05:59 -05001022 struct inode *inode,
1023 struct ext4_iloc *iloc,
1024 void *inline_start, int inline_size)
1025{
Tao Ma3c47d542012-12-10 14:05:59 -05001026 int err;
1027 struct ext4_dir_entry_2 *de;
1028
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001029 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1030 inline_size, fname, &de);
Tao Ma3c47d542012-12-10 14:05:59 -05001031 if (err)
1032 return err;
1033
liang xie5d601252014-05-12 22:06:43 -04001034 BUFFER_TRACE(iloc->bh, "get_write_access");
Tao Ma3c47d542012-12-10 14:05:59 -05001035 err = ext4_journal_get_write_access(handle, iloc->bh);
1036 if (err)
1037 return err;
Hyojun Kim63da4202017-10-06 17:10:08 -07001038 ext4_insert_dentry(inode, de, inline_size, fname);
Tao Ma3c47d542012-12-10 14:05:59 -05001039
1040 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1041
1042 /*
1043 * XXX shouldn't update any times until successful
1044 * completion of syscall, but too many callers depend
1045 * on this.
1046 *
1047 * XXX similarly, too many callers depend on
1048 * ext4_new_inode() setting the times, but error
1049 * recovery deletes the inode, so the worst that can
1050 * happen is that the times are slightly out of date
1051 * and/or different from the directory change time.
1052 */
1053 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1054 ext4_update_dx_flag(dir);
1055 dir->i_version++;
1056 ext4_mark_inode_dirty(handle, dir);
1057 return 1;
1058}
1059
1060static void *ext4_get_inline_xattr_pos(struct inode *inode,
1061 struct ext4_iloc *iloc)
1062{
1063 struct ext4_xattr_entry *entry;
1064 struct ext4_xattr_ibody_header *header;
1065
1066 BUG_ON(!EXT4_I(inode)->i_inline_off);
1067
1068 header = IHDR(inode, ext4_raw_inode(iloc));
1069 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1070 EXT4_I(inode)->i_inline_off);
1071
1072 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1073}
1074
1075/* Set the final de to cover the whole block. */
1076static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1077{
1078 struct ext4_dir_entry_2 *de, *prev_de;
1079 void *limit;
1080 int de_len;
1081
1082 de = (struct ext4_dir_entry_2 *)de_buf;
1083 if (old_size) {
1084 limit = de_buf + old_size;
1085 do {
1086 prev_de = de;
1087 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1088 de_buf += de_len;
1089 de = (struct ext4_dir_entry_2 *)de_buf;
1090 } while (de_buf < limit);
1091
1092 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1093 old_size, new_size);
1094 } else {
1095 /* this is just created, so create an empty entry. */
1096 de->inode = 0;
1097 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1098 }
1099}
1100
1101static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1102 struct ext4_iloc *iloc)
1103{
1104 int ret;
1105 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1106 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1107
1108 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1109 return -ENOSPC;
1110
1111 ret = ext4_update_inline_data(handle, dir,
1112 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1113 if (ret)
1114 return ret;
1115
1116 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1117 EXT4_I(dir)->i_inline_size -
1118 EXT4_MIN_INLINE_DATA_SIZE);
1119 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1120 return 0;
1121}
1122
1123static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1124 struct ext4_iloc *iloc,
1125 void *buf, int inline_size)
1126{
1127 ext4_create_inline_data(handle, inode, inline_size);
1128 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1129 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1130}
1131
1132static int ext4_finish_convert_inline_dir(handle_t *handle,
1133 struct inode *inode,
1134 struct buffer_head *dir_block,
1135 void *buf,
1136 int inline_size)
1137{
1138 int err, csum_size = 0, header_size = 0;
1139 struct ext4_dir_entry_2 *de;
1140 struct ext4_dir_entry_tail *t;
1141 void *target = dir_block->b_data;
1142
1143 /*
1144 * First create "." and ".." and then copy the dir information
1145 * back to the block.
1146 */
1147 de = (struct ext4_dir_entry_2 *)target;
1148 de = ext4_init_dot_dotdot(inode, de,
1149 inode->i_sb->s_blocksize, csum_size,
1150 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1151 header_size = (void *)de - target;
1152
1153 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1154 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1155
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001156 if (ext4_has_metadata_csum(inode->i_sb))
Tao Ma3c47d542012-12-10 14:05:59 -05001157 csum_size = sizeof(struct ext4_dir_entry_tail);
1158
1159 inode->i_size = inode->i_sb->s_blocksize;
1160 i_size_write(inode, inode->i_sb->s_blocksize);
1161 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1162 ext4_update_final_de(dir_block->b_data,
1163 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1164 inode->i_sb->s_blocksize - csum_size);
1165
1166 if (csum_size) {
1167 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1168 inode->i_sb->s_blocksize);
1169 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1170 }
1171 set_buffer_uptodate(dir_block);
1172 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1173 if (err)
Eric Biggersf572ba92017-03-15 14:52:02 -04001174 return err;
Tao Ma3c47d542012-12-10 14:05:59 -05001175 set_buffer_verified(dir_block);
Eric Biggersf572ba92017-03-15 14:52:02 -04001176 return ext4_mark_inode_dirty(handle, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05001177}
1178
1179static int ext4_convert_inline_data_nolock(handle_t *handle,
1180 struct inode *inode,
1181 struct ext4_iloc *iloc)
1182{
1183 int error;
1184 void *buf = NULL;
1185 struct buffer_head *data_bh = NULL;
1186 struct ext4_map_blocks map;
1187 int inline_size;
1188
1189 inline_size = ext4_get_inline_size(inode);
1190 buf = kmalloc(inline_size, GFP_NOFS);
1191 if (!buf) {
1192 error = -ENOMEM;
1193 goto out;
1194 }
1195
1196 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1197 if (error < 0)
1198 goto out;
1199
Darrick J. Wong40b163f2014-07-28 13:06:26 -04001200 /*
1201 * Make sure the inline directory entries pass checks before we try to
1202 * convert them, so that we avoid touching stuff that needs fsck.
1203 */
1204 if (S_ISDIR(inode->i_mode)) {
1205 error = ext4_check_all_de(inode, iloc->bh,
1206 buf + EXT4_INLINE_DOTDOT_SIZE,
1207 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1208 if (error)
1209 goto out;
1210 }
1211
Tao Ma3c47d542012-12-10 14:05:59 -05001212 error = ext4_destroy_inline_data_nolock(handle, inode);
1213 if (error)
1214 goto out;
1215
1216 map.m_lblk = 0;
1217 map.m_len = 1;
1218 map.m_flags = 0;
1219 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1220 if (error < 0)
1221 goto out_restore;
1222 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1223 error = -EIO;
1224 goto out_restore;
1225 }
1226
1227 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1228 if (!data_bh) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001229 error = -ENOMEM;
Tao Ma3c47d542012-12-10 14:05:59 -05001230 goto out_restore;
1231 }
1232
1233 lock_buffer(data_bh);
1234 error = ext4_journal_get_create_access(handle, data_bh);
1235 if (error) {
1236 unlock_buffer(data_bh);
1237 error = -EIO;
1238 goto out_restore;
1239 }
1240 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1241
1242 if (!S_ISDIR(inode->i_mode)) {
1243 memcpy(data_bh->b_data, buf, inline_size);
1244 set_buffer_uptodate(data_bh);
1245 error = ext4_handle_dirty_metadata(handle,
1246 inode, data_bh);
1247 } else {
1248 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1249 buf, inline_size);
1250 }
1251
1252 unlock_buffer(data_bh);
1253out_restore:
1254 if (error)
1255 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1256
1257out:
1258 brelse(data_bh);
1259 kfree(buf);
1260 return error;
1261}
1262
1263/*
1264 * Try to add the new entry to the inline data.
1265 * If succeeds, return 0. If not, extended the inline dir and copied data to
1266 * the new created block.
1267 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001268int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001269 struct inode *dir, struct inode *inode)
Tao Ma3c47d542012-12-10 14:05:59 -05001270{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001271 int ret, inline_size, no_expand;
Tao Ma3c47d542012-12-10 14:05:59 -05001272 void *inline_start;
1273 struct ext4_iloc iloc;
Tao Ma3c47d542012-12-10 14:05:59 -05001274
1275 ret = ext4_get_inode_loc(dir, &iloc);
1276 if (ret)
1277 return ret;
1278
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001279 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma3c47d542012-12-10 14:05:59 -05001280 if (!ext4_has_inline_data(dir))
1281 goto out;
1282
1283 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1284 EXT4_INLINE_DOTDOT_SIZE;
1285 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1286
Theodore Ts'o56a04912016-01-08 16:00:31 -05001287 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
Tao Ma3c47d542012-12-10 14:05:59 -05001288 inline_start, inline_size);
1289 if (ret != -ENOSPC)
1290 goto out;
1291
1292 /* check whether it can be inserted to inline xattr space. */
1293 inline_size = EXT4_I(dir)->i_inline_size -
1294 EXT4_MIN_INLINE_DATA_SIZE;
1295 if (!inline_size) {
1296 /* Try to use the xattr space.*/
1297 ret = ext4_update_inline_dir(handle, dir, &iloc);
1298 if (ret && ret != -ENOSPC)
1299 goto out;
1300
1301 inline_size = EXT4_I(dir)->i_inline_size -
1302 EXT4_MIN_INLINE_DATA_SIZE;
1303 }
1304
1305 if (inline_size) {
1306 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1307
Theodore Ts'o56a04912016-01-08 16:00:31 -05001308 ret = ext4_add_dirent_to_inline(handle, fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001309 inode, &iloc, inline_start,
1310 inline_size);
Tao Ma3c47d542012-12-10 14:05:59 -05001311
1312 if (ret != -ENOSPC)
1313 goto out;
1314 }
1315
1316 /*
1317 * The inline space is filled up, so create a new block for it.
1318 * As the extent tree will be created, we have to save the inline
1319 * dir first.
1320 */
1321 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1322
1323out:
1324 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001325 ext4_write_unlock_xattr(dir, &no_expand);
Tao Ma3c47d542012-12-10 14:05:59 -05001326 brelse(iloc.bh);
1327 return ret;
1328}
1329
Tao Ma8af0f082013-04-19 17:53:09 -04001330/*
1331 * This function fills a red-black tree with information from an
1332 * inlined dir. It returns the number directory entries loaded
1333 * into the tree. If there is an error it is returned in err.
1334 */
1335int htree_inlinedir_to_tree(struct file *dir_file,
1336 struct inode *dir, ext4_lblk_t block,
1337 struct dx_hash_info *hinfo,
1338 __u32 start_hash, __u32 start_minor_hash,
1339 int *has_inline_data)
1340{
1341 int err = 0, count = 0;
1342 unsigned int parent_ino;
1343 int pos;
1344 struct ext4_dir_entry_2 *de;
1345 struct inode *inode = file_inode(dir_file);
1346 int ret, inline_size = 0;
1347 struct ext4_iloc iloc;
1348 void *dir_buf = NULL;
1349 struct ext4_dir_entry_2 fake;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001350 struct fscrypt_str tmp_str;
Tao Ma8af0f082013-04-19 17:53:09 -04001351
1352 ret = ext4_get_inode_loc(inode, &iloc);
1353 if (ret)
1354 return ret;
1355
1356 down_read(&EXT4_I(inode)->xattr_sem);
1357 if (!ext4_has_inline_data(inode)) {
1358 up_read(&EXT4_I(inode)->xattr_sem);
1359 *has_inline_data = 0;
1360 goto out;
1361 }
1362
1363 inline_size = ext4_get_inline_size(inode);
1364 dir_buf = kmalloc(inline_size, GFP_NOFS);
1365 if (!dir_buf) {
1366 ret = -ENOMEM;
1367 up_read(&EXT4_I(inode)->xattr_sem);
1368 goto out;
1369 }
1370
1371 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1372 up_read(&EXT4_I(inode)->xattr_sem);
1373 if (ret < 0)
1374 goto out;
1375
1376 pos = 0;
1377 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1378 while (pos < inline_size) {
1379 /*
1380 * As inlined dir doesn't store any information about '.' and
1381 * only the inode number of '..' is stored, we have to handle
1382 * them differently.
1383 */
1384 if (pos == 0) {
1385 fake.inode = cpu_to_le32(inode->i_ino);
1386 fake.name_len = 1;
1387 strcpy(fake.name, ".");
1388 fake.rec_len = ext4_rec_len_to_disk(
1389 EXT4_DIR_REC_LEN(fake.name_len),
1390 inline_size);
1391 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1392 de = &fake;
1393 pos = EXT4_INLINE_DOTDOT_OFFSET;
1394 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1395 fake.inode = cpu_to_le32(parent_ino);
1396 fake.name_len = 2;
1397 strcpy(fake.name, "..");
1398 fake.rec_len = ext4_rec_len_to_disk(
1399 EXT4_DIR_REC_LEN(fake.name_len),
1400 inline_size);
1401 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1402 de = &fake;
1403 pos = EXT4_INLINE_DOTDOT_SIZE;
1404 } else {
1405 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1406 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1407 if (ext4_check_dir_entry(inode, dir_file, de,
1408 iloc.bh, dir_buf,
1409 inline_size, pos)) {
1410 ret = count;
1411 goto out;
1412 }
1413 }
1414
1415 ext4fs_dirhash(de->name, de->name_len, hinfo);
1416 if ((hinfo->hash < start_hash) ||
1417 ((hinfo->hash == start_hash) &&
1418 (hinfo->minor_hash < start_minor_hash)))
1419 continue;
1420 if (de->inode == 0)
1421 continue;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001422 tmp_str.name = de->name;
1423 tmp_str.len = de->name_len;
1424 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1425 hinfo->minor_hash, de, &tmp_str);
Tao Ma8af0f082013-04-19 17:53:09 -04001426 if (err) {
1427 count = err;
1428 goto out;
1429 }
1430 count++;
1431 }
1432 ret = count;
1433out:
1434 kfree(dir_buf);
1435 brelse(iloc.bh);
1436 return ret;
1437}
1438
Tao Mac4d8b022013-04-19 17:55:33 -04001439/*
1440 * So this function is called when the volume is mkfsed with
1441 * dir_index disabled. In order to keep f_pos persistent
1442 * after we convert from an inlined dir to a blocked based,
1443 * we just pretend that we are a normal dir and return the
1444 * offset as if '.' and '..' really take place.
1445 *
1446 */
Al Viro725bebb2013-05-17 16:08:53 -04001447int ext4_read_inline_dir(struct file *file,
1448 struct dir_context *ctx,
Tao Ma65d165d2012-12-10 14:05:59 -05001449 int *has_inline_data)
1450{
Tao Ma65d165d2012-12-10 14:05:59 -05001451 unsigned int offset, parent_ino;
Al Viro725bebb2013-05-17 16:08:53 -04001452 int i;
Tao Ma65d165d2012-12-10 14:05:59 -05001453 struct ext4_dir_entry_2 *de;
1454 struct super_block *sb;
Al Viro725bebb2013-05-17 16:08:53 -04001455 struct inode *inode = file_inode(file);
Tao Ma65d165d2012-12-10 14:05:59 -05001456 int ret, inline_size = 0;
1457 struct ext4_iloc iloc;
1458 void *dir_buf = NULL;
Tao Mac4d8b022013-04-19 17:55:33 -04001459 int dotdot_offset, dotdot_size, extra_offset, extra_size;
Tao Ma65d165d2012-12-10 14:05:59 -05001460
1461 ret = ext4_get_inode_loc(inode, &iloc);
1462 if (ret)
1463 return ret;
1464
1465 down_read(&EXT4_I(inode)->xattr_sem);
1466 if (!ext4_has_inline_data(inode)) {
1467 up_read(&EXT4_I(inode)->xattr_sem);
1468 *has_inline_data = 0;
1469 goto out;
1470 }
1471
1472 inline_size = ext4_get_inline_size(inode);
1473 dir_buf = kmalloc(inline_size, GFP_NOFS);
1474 if (!dir_buf) {
1475 ret = -ENOMEM;
1476 up_read(&EXT4_I(inode)->xattr_sem);
1477 goto out;
1478 }
1479
1480 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1481 up_read(&EXT4_I(inode)->xattr_sem);
1482 if (ret < 0)
1483 goto out;
1484
BoxiLiu48ffdab2013-10-30 08:07:20 -04001485 ret = 0;
Tao Ma65d165d2012-12-10 14:05:59 -05001486 sb = inode->i_sb;
Tao Ma65d165d2012-12-10 14:05:59 -05001487 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
Al Viro725bebb2013-05-17 16:08:53 -04001488 offset = ctx->pos;
Tao Ma65d165d2012-12-10 14:05:59 -05001489
Tao Mac4d8b022013-04-19 17:55:33 -04001490 /*
1491 * dotdot_offset and dotdot_size is the real offset and
1492 * size for ".." and "." if the dir is block based while
1493 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1494 * So we will use extra_offset and extra_size to indicate them
1495 * during the inline dir iteration.
1496 */
1497 dotdot_offset = EXT4_DIR_REC_LEN(1);
1498 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1499 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1500 extra_size = extra_offset + inline_size;
1501
Al Viro725bebb2013-05-17 16:08:53 -04001502 /*
1503 * If the version has changed since the last call to
1504 * readdir(2), then we might be pointing to an invalid
1505 * dirent right now. Scan from the start of the inline
1506 * dir to make sure.
1507 */
1508 if (file->f_version != inode->i_version) {
1509 for (i = 0; i < extra_size && i < offset;) {
1510 /*
1511 * "." is with offset 0 and
1512 * ".." is dotdot_offset.
1513 */
1514 if (!i) {
1515 i = dotdot_offset;
1516 continue;
1517 } else if (i == dotdot_offset) {
1518 i = dotdot_size;
Tao Mac4d8b022013-04-19 17:55:33 -04001519 continue;
1520 }
Al Viro725bebb2013-05-17 16:08:53 -04001521 /* for other entry, the real offset in
1522 * the buf has to be tuned accordingly.
1523 */
Tao Mac4d8b022013-04-19 17:55:33 -04001524 de = (struct ext4_dir_entry_2 *)
Al Viro725bebb2013-05-17 16:08:53 -04001525 (dir_buf + i - extra_offset);
1526 /* It's too expensive to do a full
1527 * dirent test each time round this
1528 * loop, but we do have to test at
1529 * least that it is non-zero. A
1530 * failure will be detected in the
1531 * dirent test below. */
1532 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1533 < EXT4_DIR_REC_LEN(1))
1534 break;
1535 i += ext4_rec_len_from_disk(de->rec_len,
1536 extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001537 }
Al Viro725bebb2013-05-17 16:08:53 -04001538 offset = i;
1539 ctx->pos = offset;
1540 file->f_version = inode->i_version;
1541 }
1542
1543 while (ctx->pos < extra_size) {
1544 if (ctx->pos == 0) {
1545 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1546 goto out;
1547 ctx->pos = dotdot_offset;
1548 continue;
1549 }
1550
1551 if (ctx->pos == dotdot_offset) {
1552 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1553 goto out;
1554 ctx->pos = dotdot_size;
1555 continue;
1556 }
1557
1558 de = (struct ext4_dir_entry_2 *)
1559 (dir_buf + ctx->pos - extra_offset);
1560 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1561 extra_size, ctx->pos))
1562 goto out;
1563 if (le32_to_cpu(de->inode)) {
1564 if (!dir_emit(ctx, de->name, de->name_len,
1565 le32_to_cpu(de->inode),
1566 get_dtype(sb, de->file_type)))
1567 goto out;
1568 }
1569 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001570 }
1571out:
1572 kfree(dir_buf);
1573 brelse(iloc.bh);
1574 return ret;
1575}
1576
Tao Ma32f7f222012-12-10 14:06:01 -05001577struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1578 struct ext4_dir_entry_2 **parent_de,
1579 int *retval)
1580{
1581 struct ext4_iloc iloc;
1582
1583 *retval = ext4_get_inode_loc(inode, &iloc);
1584 if (*retval)
1585 return NULL;
1586
1587 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1588
1589 return iloc.bh;
1590}
1591
Tao Ma3c47d542012-12-10 14:05:59 -05001592/*
1593 * Try to create the inline data for the new dir.
1594 * If it succeeds, return 0, otherwise return the error.
1595 * In case of ENOSPC, the caller should create the normal disk layout dir.
1596 */
1597int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1598 struct inode *inode)
1599{
1600 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1601 struct ext4_iloc iloc;
1602 struct ext4_dir_entry_2 *de;
1603
1604 ret = ext4_get_inode_loc(inode, &iloc);
1605 if (ret)
1606 return ret;
1607
1608 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1609 if (ret)
1610 goto out;
1611
1612 /*
1613 * For inline dir, we only save the inode information for the ".."
1614 * and create a fake dentry to cover the left space.
1615 */
1616 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1617 de->inode = cpu_to_le32(parent->i_ino);
1618 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1619 de->inode = 0;
1620 de->rec_len = ext4_rec_len_to_disk(
1621 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1622 inline_size);
1623 set_nlink(inode, 2);
1624 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1625out:
1626 brelse(iloc.bh);
1627 return ret;
1628}
1629
Tao Mae8e948e2012-12-10 14:06:00 -05001630struct buffer_head *ext4_find_inline_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001631 struct ext4_filename *fname,
Tao Mae8e948e2012-12-10 14:06:00 -05001632 const struct qstr *d_name,
1633 struct ext4_dir_entry_2 **res_dir,
1634 int *has_inline_data)
1635{
1636 int ret;
1637 struct ext4_iloc iloc;
1638 void *inline_start;
1639 int inline_size;
1640
1641 if (ext4_get_inode_loc(dir, &iloc))
1642 return NULL;
1643
1644 down_read(&EXT4_I(dir)->xattr_sem);
1645 if (!ext4_has_inline_data(dir)) {
1646 *has_inline_data = 0;
1647 goto out;
1648 }
1649
1650 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1651 EXT4_INLINE_DOTDOT_SIZE;
1652 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001653 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1654 dir, fname, d_name, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001655 if (ret == 1)
1656 goto out_find;
1657 if (ret < 0)
1658 goto out;
1659
1660 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1661 goto out;
1662
1663 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1664 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1665
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001666 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1667 dir, fname, d_name, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001668 if (ret == 1)
1669 goto out_find;
1670
1671out:
1672 brelse(iloc.bh);
1673 iloc.bh = NULL;
1674out_find:
1675 up_read(&EXT4_I(dir)->xattr_sem);
1676 return iloc.bh;
1677}
1678
Tao Ma9f40fe52012-12-10 14:06:00 -05001679int ext4_delete_inline_entry(handle_t *handle,
1680 struct inode *dir,
1681 struct ext4_dir_entry_2 *de_del,
1682 struct buffer_head *bh,
1683 int *has_inline_data)
1684{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001685 int err, inline_size, no_expand;
Tao Ma9f40fe52012-12-10 14:06:00 -05001686 struct ext4_iloc iloc;
1687 void *inline_start;
1688
1689 err = ext4_get_inode_loc(dir, &iloc);
1690 if (err)
1691 return err;
1692
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001693 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma9f40fe52012-12-10 14:06:00 -05001694 if (!ext4_has_inline_data(dir)) {
1695 *has_inline_data = 0;
1696 goto out;
1697 }
1698
1699 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1700 EXT4_MIN_INLINE_DATA_SIZE) {
1701 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1702 EXT4_INLINE_DOTDOT_SIZE;
1703 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1704 EXT4_INLINE_DOTDOT_SIZE;
1705 } else {
1706 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1707 inline_size = ext4_get_inline_size(dir) -
1708 EXT4_MIN_INLINE_DATA_SIZE;
1709 }
1710
liang xie5d601252014-05-12 22:06:43 -04001711 BUFFER_TRACE(bh, "get_write_access");
Tao Ma9f40fe52012-12-10 14:06:00 -05001712 err = ext4_journal_get_write_access(handle, bh);
1713 if (err)
1714 goto out;
1715
1716 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1717 inline_start, inline_size, 0);
1718 if (err)
1719 goto out;
1720
Tao Ma9f40fe52012-12-10 14:06:00 -05001721 err = ext4_mark_inode_dirty(handle, dir);
1722 if (unlikely(err))
1723 goto out;
1724
1725 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1726out:
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001727 ext4_write_unlock_xattr(dir, &no_expand);
Tao Ma9f40fe52012-12-10 14:06:00 -05001728 brelse(iloc.bh);
1729 if (err != -ENOENT)
1730 ext4_std_error(dir->i_sb, err);
1731 return err;
1732}
1733
Tao Ma61f86632012-12-10 14:06:01 -05001734/*
1735 * Get the inline dentry at offset.
1736 */
1737static inline struct ext4_dir_entry_2 *
1738ext4_get_inline_entry(struct inode *inode,
1739 struct ext4_iloc *iloc,
1740 unsigned int offset,
1741 void **inline_start,
1742 int *inline_size)
1743{
1744 void *inline_pos;
1745
1746 BUG_ON(offset > ext4_get_inline_size(inode));
1747
1748 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1749 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1750 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1751 } else {
1752 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1753 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1754 *inline_size = ext4_get_inline_size(inode) -
1755 EXT4_MIN_INLINE_DATA_SIZE;
1756 }
1757
1758 if (inline_start)
1759 *inline_start = inline_pos;
1760 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1761}
1762
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001763bool empty_inline_dir(struct inode *dir, int *has_inline_data)
Tao Ma61f86632012-12-10 14:06:01 -05001764{
1765 int err, inline_size;
1766 struct ext4_iloc iloc;
1767 void *inline_pos;
1768 unsigned int offset;
1769 struct ext4_dir_entry_2 *de;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001770 bool ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001771
1772 err = ext4_get_inode_loc(dir, &iloc);
1773 if (err) {
1774 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1775 err, dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001776 return true;
Tao Ma61f86632012-12-10 14:06:01 -05001777 }
1778
1779 down_read(&EXT4_I(dir)->xattr_sem);
1780 if (!ext4_has_inline_data(dir)) {
1781 *has_inline_data = 0;
1782 goto out;
1783 }
1784
1785 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1786 if (!le32_to_cpu(de->inode)) {
1787 ext4_warning(dir->i_sb,
1788 "bad inline directory (dir #%lu) - no `..'",
1789 dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001790 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001791 goto out;
1792 }
1793
1794 offset = EXT4_INLINE_DOTDOT_SIZE;
1795 while (offset < dir->i_size) {
1796 de = ext4_get_inline_entry(dir, &iloc, offset,
1797 &inline_pos, &inline_size);
1798 if (ext4_check_dir_entry(dir, NULL, de,
1799 iloc.bh, inline_pos,
1800 inline_size, offset)) {
1801 ext4_warning(dir->i_sb,
1802 "bad inline directory (dir #%lu) - "
1803 "inode %u, rec_len %u, name_len %d"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001804 "inline size %d",
Tao Ma61f86632012-12-10 14:06:01 -05001805 dir->i_ino, le32_to_cpu(de->inode),
1806 le16_to_cpu(de->rec_len), de->name_len,
1807 inline_size);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001808 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001809 goto out;
1810 }
1811 if (le32_to_cpu(de->inode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001812 ret = false;
Tao Ma61f86632012-12-10 14:06:01 -05001813 goto out;
1814 }
1815 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1816 }
1817
1818out:
1819 up_read(&EXT4_I(dir)->xattr_sem);
1820 brelse(iloc.bh);
1821 return ret;
1822}
1823
Tao Ma67cf5b02012-12-10 14:04:46 -05001824int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1825{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001826 int ret, no_expand;
Tao Ma67cf5b02012-12-10 14:04:46 -05001827
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001828 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001829 ret = ext4_destroy_inline_data_nolock(handle, inode);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001830 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001831
1832 return ret;
1833}
Tao Ma94191982012-12-10 14:06:02 -05001834
1835int ext4_inline_data_fiemap(struct inode *inode,
1836 struct fiemap_extent_info *fieinfo,
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001837 int *has_inline, __u64 start, __u64 len)
Tao Ma94191982012-12-10 14:06:02 -05001838{
1839 __u64 physical = 0;
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001840 __u64 inline_len;
1841 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1842 FIEMAP_EXTENT_LAST;
Tao Ma94191982012-12-10 14:06:02 -05001843 int error = 0;
1844 struct ext4_iloc iloc;
1845
1846 down_read(&EXT4_I(inode)->xattr_sem);
1847 if (!ext4_has_inline_data(inode)) {
1848 *has_inline = 0;
1849 goto out;
1850 }
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001851 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1852 i_size_read(inode));
1853 if (start >= inline_len)
1854 goto out;
1855 if (start + len < inline_len)
1856 inline_len = start + len;
1857 inline_len -= start;
Tao Ma94191982012-12-10 14:06:02 -05001858
1859 error = ext4_get_inode_loc(inode, &iloc);
1860 if (error)
1861 goto out;
1862
Jan Karaeaf37932013-05-31 19:33:42 -04001863 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
Tao Ma94191982012-12-10 14:06:02 -05001864 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1865 physical += offsetof(struct ext4_inode, i_block);
Tao Ma94191982012-12-10 14:06:02 -05001866
1867 if (physical)
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001868 error = fiemap_fill_next_extent(fieinfo, start, physical,
1869 inline_len, flags);
Tao Ma94191982012-12-10 14:06:02 -05001870 brelse(iloc.bh);
1871out:
1872 up_read(&EXT4_I(inode)->xattr_sem);
1873 return (error < 0 ? error : 0);
1874}
Tao Ma0d812f72012-12-10 14:06:02 -05001875
1876/*
1877 * Called during xattr set, and if we can sparse space 'needed',
1878 * just create the extent tree evict the data to the outer block.
1879 *
1880 * We use jbd2 instead of page cache to move data to the 1st block
1881 * so that the whole transaction can be committed as a whole and
1882 * the data isn't lost because of the delayed page cache write.
1883 */
1884int ext4_try_to_evict_inline_data(handle_t *handle,
1885 struct inode *inode,
1886 int needed)
1887{
1888 int error;
1889 struct ext4_xattr_entry *entry;
Tao Ma0d812f72012-12-10 14:06:02 -05001890 struct ext4_inode *raw_inode;
1891 struct ext4_iloc iloc;
1892
1893 error = ext4_get_inode_loc(inode, &iloc);
1894 if (error)
1895 return error;
1896
1897 raw_inode = ext4_raw_inode(&iloc);
Tao Ma0d812f72012-12-10 14:06:02 -05001898 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
1899 EXT4_I(inode)->i_inline_off);
1900 if (EXT4_XATTR_LEN(entry->e_name_len) +
1901 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) {
1902 error = -ENOSPC;
1903 goto out;
1904 }
1905
1906 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
1907out:
1908 brelse(iloc.bh);
1909 return error;
1910}
Tao Maaef1c852012-12-10 14:06:02 -05001911
1912void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1913{
1914 handle_t *handle;
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001915 int inline_size, value_len, needed_blocks, no_expand;
Tao Maaef1c852012-12-10 14:06:02 -05001916 size_t i_size;
1917 void *value = NULL;
1918 struct ext4_xattr_ibody_find is = {
1919 .s = { .not_found = -ENODATA, },
1920 };
1921 struct ext4_xattr_info i = {
1922 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1923 .name = EXT4_XATTR_SYSTEM_DATA,
1924 };
1925
1926
1927 needed_blocks = ext4_writepage_trans_blocks(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -05001928 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
Tao Maaef1c852012-12-10 14:06:02 -05001929 if (IS_ERR(handle))
1930 return;
1931
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001932 ext4_write_lock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05001933 if (!ext4_has_inline_data(inode)) {
1934 *has_inline = 0;
1935 ext4_journal_stop(handle);
1936 return;
1937 }
1938
1939 if (ext4_orphan_add(handle, inode))
1940 goto out;
1941
1942 if (ext4_get_inode_loc(inode, &is.iloc))
1943 goto out;
1944
1945 down_write(&EXT4_I(inode)->i_data_sem);
1946 i_size = inode->i_size;
1947 inline_size = ext4_get_inline_size(inode);
1948 EXT4_I(inode)->i_disksize = i_size;
1949
1950 if (i_size < inline_size) {
1951 /* Clear the content in the xattr space. */
1952 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1953 if (ext4_xattr_ibody_find(inode, &i, &is))
1954 goto out_error;
1955
1956 BUG_ON(is.s.not_found);
1957
1958 value_len = le32_to_cpu(is.s.here->e_value_size);
1959 value = kmalloc(value_len, GFP_NOFS);
1960 if (!value)
1961 goto out_error;
1962
1963 if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
1964 value, value_len))
1965 goto out_error;
1966
1967 i.value = value;
1968 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1969 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1970 if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
1971 goto out_error;
1972 }
1973
1974 /* Clear the content within i_blocks. */
Theodore Ts'o09c455a2014-01-07 12:58:19 -05001975 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1976 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1977 memset(p + i_size, 0,
1978 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1979 }
Tao Maaef1c852012-12-10 14:06:02 -05001980
1981 EXT4_I(inode)->i_inline_size = i_size <
1982 EXT4_MIN_INLINE_DATA_SIZE ?
1983 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1984 }
1985
1986out_error:
1987 up_write(&EXT4_I(inode)->i_data_sem);
1988out:
1989 brelse(is.iloc.bh);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001990 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05001991 kfree(value);
1992 if (inode->i_nlink)
1993 ext4_orphan_del(handle, inode);
1994
1995 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
1996 ext4_mark_inode_dirty(handle, inode);
1997 if (IS_SYNC(inode))
1998 ext4_handle_sync(handle);
1999
2000 ext4_journal_stop(handle);
2001 return;
2002}
Tao Ma0c8d4142012-12-10 14:06:03 -05002003
2004int ext4_convert_inline_data(struct inode *inode)
2005{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05002006 int error, needed_blocks, no_expand;
Tao Ma0c8d4142012-12-10 14:06:03 -05002007 handle_t *handle;
2008 struct ext4_iloc iloc;
2009
2010 if (!ext4_has_inline_data(inode)) {
2011 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2012 return 0;
2013 }
2014
2015 needed_blocks = ext4_writepage_trans_blocks(inode);
2016
2017 iloc.bh = NULL;
2018 error = ext4_get_inode_loc(inode, &iloc);
2019 if (error)
2020 return error;
2021
Theodore Ts'o9924a922013-02-08 21:59:22 -05002022 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Ma0c8d4142012-12-10 14:06:03 -05002023 if (IS_ERR(handle)) {
2024 error = PTR_ERR(handle);
2025 goto out_free;
2026 }
2027
Theodore Ts'oda1e4022017-01-11 21:50:46 -05002028 ext4_write_lock_xattr(inode, &no_expand);
2029 if (ext4_has_inline_data(inode))
2030 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2031 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma0c8d4142012-12-10 14:06:03 -05002032 ext4_journal_stop(handle);
2033out_free:
2034 brelse(iloc.bh);
2035 return error;
2036}