blob: 436baf7cdca3c3dff47005bee1b19311310a1c85 [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"
Tao Ma67cf5b02012-12-10 14:04:46 -050021
22#define EXT4_XATTR_SYSTEM_DATA "data"
23#define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
Tao Ma8af0f082013-04-19 17:53:09 -040024#define EXT4_INLINE_DOTDOT_OFFSET 2
25#define EXT4_INLINE_DOTDOT_SIZE 4
Tao Ma67cf5b02012-12-10 14:04:46 -050026
Stephen Hemmingerc1978552014-05-12 10:50:23 -040027static int ext4_get_inline_size(struct inode *inode)
Tao Ma67cf5b02012-12-10 14:04:46 -050028{
29 if (EXT4_I(inode)->i_inline_off)
30 return EXT4_I(inode)->i_inline_size;
31
32 return 0;
33}
34
35static int get_max_inline_xattr_value_size(struct inode *inode,
36 struct ext4_iloc *iloc)
37{
38 struct ext4_xattr_ibody_header *header;
39 struct ext4_xattr_entry *entry;
40 struct ext4_inode *raw_inode;
41 int free, min_offs;
42
43 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
44 EXT4_GOOD_OLD_INODE_SIZE -
45 EXT4_I(inode)->i_extra_isize -
46 sizeof(struct ext4_xattr_ibody_header);
47
48 /*
49 * We need to subtract another sizeof(__u32) since an in-inode xattr
50 * needs an empty 4 bytes to indicate the gap between the xattr entry
51 * and the name/value pair.
52 */
53 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
54 return EXT4_XATTR_SIZE(min_offs -
55 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
56 EXT4_XATTR_ROUND - sizeof(__u32));
57
58 raw_inode = ext4_raw_inode(iloc);
59 header = IHDR(inode, raw_inode);
60 entry = IFIRST(header);
61
62 /* Compute min_offs. */
63 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
64 if (!entry->e_value_block && entry->e_value_size) {
65 size_t offs = le16_to_cpu(entry->e_value_offs);
66 if (offs < min_offs)
67 min_offs = offs;
68 }
69 }
70 free = min_offs -
71 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
72
73 if (EXT4_I(inode)->i_inline_off) {
74 entry = (struct ext4_xattr_entry *)
75 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
76
boxi liuc4932db2013-07-01 08:12:37 -040077 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
Tao Ma67cf5b02012-12-10 14:04:46 -050078 goto out;
79 }
80
81 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
82
83 if (free > EXT4_XATTR_ROUND)
84 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
85 else
86 free = 0;
87
88out:
89 return free;
90}
91
92/*
93 * Get the maximum size we now can store in an inode.
94 * If we can't find the space for a xattr entry, don't use the space
95 * of the extents since we have no space to indicate the inline data.
96 */
97int ext4_get_max_inline_size(struct inode *inode)
98{
99 int error, max_inline_size;
100 struct ext4_iloc iloc;
101
102 if (EXT4_I(inode)->i_extra_isize == 0)
103 return 0;
104
105 error = ext4_get_inode_loc(inode, &iloc);
106 if (error) {
107 ext4_error_inode(inode, __func__, __LINE__, 0,
108 "can't get inode location %lu",
109 inode->i_ino);
110 return 0;
111 }
112
113 down_read(&EXT4_I(inode)->xattr_sem);
114 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
115 up_read(&EXT4_I(inode)->xattr_sem);
116
117 brelse(iloc.bh);
118
119 if (!max_inline_size)
120 return 0;
121
122 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
123}
124
Tao Ma67cf5b02012-12-10 14:04:46 -0500125/*
126 * this function does not take xattr_sem, which is OK because it is
127 * currently only used in a code path coming form ext4_iget, before
128 * the new inode has been unlocked
129 */
130int ext4_find_inline_data_nolock(struct inode *inode)
131{
132 struct ext4_xattr_ibody_find is = {
133 .s = { .not_found = -ENODATA, },
134 };
135 struct ext4_xattr_info i = {
136 .name_index = EXT4_XATTR_INDEX_SYSTEM,
137 .name = EXT4_XATTR_SYSTEM_DATA,
138 };
139 int error;
140
141 if (EXT4_I(inode)->i_extra_isize == 0)
142 return 0;
143
144 error = ext4_get_inode_loc(inode, &is.iloc);
145 if (error)
146 return error;
147
148 error = ext4_xattr_ibody_find(inode, &i, &is);
149 if (error)
150 goto out;
151
152 if (!is.s.not_found) {
153 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
154 (void *)ext4_raw_inode(&is.iloc));
155 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
156 le32_to_cpu(is.s.here->e_value_size);
157 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
158 }
159out:
160 brelse(is.iloc.bh);
161 return error;
162}
163
164static int ext4_read_inline_data(struct inode *inode, void *buffer,
165 unsigned int len,
166 struct ext4_iloc *iloc)
167{
168 struct ext4_xattr_entry *entry;
169 struct ext4_xattr_ibody_header *header;
170 int cp_len = 0;
171 struct ext4_inode *raw_inode;
172
173 if (!len)
174 return 0;
175
176 BUG_ON(len > EXT4_I(inode)->i_inline_size);
177
178 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
179 len : EXT4_MIN_INLINE_DATA_SIZE;
180
181 raw_inode = ext4_raw_inode(iloc);
182 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
183
184 len -= cp_len;
185 buffer += cp_len;
186
187 if (!len)
188 goto out;
189
190 header = IHDR(inode, raw_inode);
191 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
192 EXT4_I(inode)->i_inline_off);
193 len = min_t(unsigned int, len,
194 (unsigned int)le32_to_cpu(entry->e_value_size));
195
196 memcpy(buffer,
197 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
198 cp_len += len;
199
200out:
201 return cp_len;
202}
203
204/*
205 * write the buffer to the inline inode.
206 * If 'create' is set, we don't need to do the extra copy in the xattr
Tao Ma0d812f72012-12-10 14:06:02 -0500207 * value since it is already handled by ext4_xattr_ibody_inline_set.
208 * That saves us one memcpy.
Tao Ma67cf5b02012-12-10 14:04:46 -0500209 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -0400210static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
211 void *buffer, loff_t pos, unsigned int len)
Tao Ma67cf5b02012-12-10 14:04:46 -0500212{
213 struct ext4_xattr_entry *entry;
214 struct ext4_xattr_ibody_header *header;
215 struct ext4_inode *raw_inode;
216 int cp_len = 0;
217
218 BUG_ON(!EXT4_I(inode)->i_inline_off);
219 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
220
221 raw_inode = ext4_raw_inode(iloc);
222 buffer += pos;
223
224 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
225 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
226 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
227 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
228
229 len -= cp_len;
230 buffer += cp_len;
231 pos += cp_len;
232 }
233
234 if (!len)
235 return;
236
237 pos -= EXT4_MIN_INLINE_DATA_SIZE;
238 header = IHDR(inode, raw_inode);
239 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
240 EXT4_I(inode)->i_inline_off);
241
242 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
243 buffer, len);
244}
245
246static int ext4_create_inline_data(handle_t *handle,
247 struct inode *inode, unsigned len)
248{
249 int error;
250 void *value = NULL;
251 struct ext4_xattr_ibody_find is = {
252 .s = { .not_found = -ENODATA, },
253 };
254 struct ext4_xattr_info i = {
255 .name_index = EXT4_XATTR_INDEX_SYSTEM,
256 .name = EXT4_XATTR_SYSTEM_DATA,
257 };
258
259 error = ext4_get_inode_loc(inode, &is.iloc);
260 if (error)
261 return error;
262
liang xie5d601252014-05-12 22:06:43 -0400263 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500264 error = ext4_journal_get_write_access(handle, is.iloc.bh);
265 if (error)
266 goto out;
267
268 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500269 value = EXT4_ZERO_XATTR_VALUE;
Tao Ma67cf5b02012-12-10 14:04:46 -0500270 len -= EXT4_MIN_INLINE_DATA_SIZE;
271 } else {
272 value = "";
273 len = 0;
274 }
275
276 /* Insert the the xttr entry. */
277 i.value = value;
278 i.value_len = len;
279
280 error = ext4_xattr_ibody_find(inode, &i, &is);
281 if (error)
282 goto out;
283
284 BUG_ON(!is.s.not_found);
285
Tao Ma0d812f72012-12-10 14:06:02 -0500286 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500287 if (error) {
288 if (error == -ENOSPC)
289 ext4_clear_inode_state(inode,
290 EXT4_STATE_MAY_INLINE_DATA);
291 goto out;
292 }
293
294 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
295 0, EXT4_MIN_INLINE_DATA_SIZE);
296
297 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
298 (void *)ext4_raw_inode(&is.iloc));
299 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
300 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
301 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
302 get_bh(is.iloc.bh);
303 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
304
305out:
306 brelse(is.iloc.bh);
307 return error;
308}
309
310static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
311 unsigned int len)
312{
313 int error;
314 void *value = NULL;
315 struct ext4_xattr_ibody_find is = {
316 .s = { .not_found = -ENODATA, },
317 };
318 struct ext4_xattr_info i = {
319 .name_index = EXT4_XATTR_INDEX_SYSTEM,
320 .name = EXT4_XATTR_SYSTEM_DATA,
321 };
322
323 /* If the old space is ok, write the data directly. */
324 if (len <= EXT4_I(inode)->i_inline_size)
325 return 0;
326
327 error = ext4_get_inode_loc(inode, &is.iloc);
328 if (error)
329 return error;
330
331 error = ext4_xattr_ibody_find(inode, &i, &is);
332 if (error)
333 goto out;
334
335 BUG_ON(is.s.not_found);
336
337 len -= EXT4_MIN_INLINE_DATA_SIZE;
338 value = kzalloc(len, GFP_NOFS);
Dan Carpenteracf3efd2016-12-10 09:56:01 -0500339 if (!value) {
340 error = -ENOMEM;
Tao Ma67cf5b02012-12-10 14:04:46 -0500341 goto out;
Dan Carpenteracf3efd2016-12-10 09:56:01 -0500342 }
Tao Ma67cf5b02012-12-10 14:04:46 -0500343
344 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
345 value, len);
346 if (error == -ENODATA)
347 goto out;
348
liang xie5d601252014-05-12 22:06:43 -0400349 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500350 error = ext4_journal_get_write_access(handle, is.iloc.bh);
351 if (error)
352 goto out;
353
354 /* Update the xttr entry. */
355 i.value = value;
356 i.value_len = len;
357
Tao Ma0d812f72012-12-10 14:06:02 -0500358 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500359 if (error)
360 goto out;
361
362 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
363 (void *)ext4_raw_inode(&is.iloc));
364 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
365 le32_to_cpu(is.s.here->e_value_size);
366 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
367 get_bh(is.iloc.bh);
368 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
369
370out:
371 kfree(value);
372 brelse(is.iloc.bh);
373 return error;
374}
375
Stephen Hemmingerc1978552014-05-12 10:50:23 -0400376static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
377 unsigned int len)
Tao Ma67cf5b02012-12-10 14:04:46 -0500378{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500379 int ret, size, no_expand;
Tao Ma67cf5b02012-12-10 14:04:46 -0500380 struct ext4_inode_info *ei = EXT4_I(inode);
381
382 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
383 return -ENOSPC;
384
385 size = ext4_get_max_inline_size(inode);
386 if (size < len)
387 return -ENOSPC;
388
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500389 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -0500390
391 if (ei->i_inline_off)
392 ret = ext4_update_inline_data(handle, inode, len);
393 else
394 ret = ext4_create_inline_data(handle, inode, len);
395
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500396 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -0500397 return ret;
398}
399
400static int ext4_destroy_inline_data_nolock(handle_t *handle,
401 struct inode *inode)
402{
403 struct ext4_inode_info *ei = EXT4_I(inode);
404 struct ext4_xattr_ibody_find is = {
405 .s = { .not_found = 0, },
406 };
407 struct ext4_xattr_info i = {
408 .name_index = EXT4_XATTR_INDEX_SYSTEM,
409 .name = EXT4_XATTR_SYSTEM_DATA,
410 .value = NULL,
411 .value_len = 0,
412 };
413 int error;
414
415 if (!ei->i_inline_off)
416 return 0;
417
418 error = ext4_get_inode_loc(inode, &is.iloc);
419 if (error)
420 return error;
421
422 error = ext4_xattr_ibody_find(inode, &i, &is);
423 if (error)
424 goto out;
425
liang xie5d601252014-05-12 22:06:43 -0400426 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500427 error = ext4_journal_get_write_access(handle, is.iloc.bh);
428 if (error)
429 goto out;
430
Tao Ma0d812f72012-12-10 14:06:02 -0500431 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500432 if (error)
433 goto out;
434
435 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
436 0, EXT4_MIN_INLINE_DATA_SIZE);
Theodore Ts'oa5e063d2018-06-15 12:28:16 -0400437 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
Tao Ma67cf5b02012-12-10 14:04:46 -0500438
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
505 /*
506 * Current inline data can only exist in the 1st page,
507 * So for all the other pages, just set them uptodate.
508 */
509 if (!page->index)
510 ret = ext4_read_inline_page(inode, page);
511 else if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300512 zero_user_segment(page, 0, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500513 SetPageUptodate(page);
514 }
515
516 up_read(&EXT4_I(inode)->xattr_sem);
517
518 unlock_page(page);
519 return ret >= 0 ? 0 : ret;
520}
521
Tao Maf19d5872012-12-10 14:05:51 -0500522static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
523 struct inode *inode,
524 unsigned flags)
525{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500526 int ret, needed_blocks, no_expand;
Tao Maf19d5872012-12-10 14:05:51 -0500527 handle_t *handle = NULL;
528 int retries = 0, sem_held = 0;
529 struct page *page = NULL;
530 unsigned from, to;
531 struct ext4_iloc iloc;
532
533 if (!ext4_has_inline_data(inode)) {
534 /*
535 * clear the flag so that no new write
536 * will trap here again.
537 */
538 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
539 return 0;
540 }
541
542 needed_blocks = ext4_writepage_trans_blocks(inode);
543
544 ret = ext4_get_inode_loc(inode, &iloc);
545 if (ret)
546 return ret;
547
548retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500549 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Maf19d5872012-12-10 14:05:51 -0500550 if (IS_ERR(handle)) {
551 ret = PTR_ERR(handle);
552 handle = NULL;
553 goto out;
554 }
555
556 /* We cannot recurse into the filesystem as the transaction is already
557 * started */
558 flags |= AOP_FLAG_NOFS;
559
560 page = grab_cache_page_write_begin(mapping, 0, flags);
561 if (!page) {
562 ret = -ENOMEM;
563 goto out;
564 }
565
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500566 ext4_write_lock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500567 sem_held = 1;
568 /* If some one has already done this for us, just exit. */
569 if (!ext4_has_inline_data(inode)) {
570 ret = 0;
571 goto out;
572 }
573
574 from = 0;
575 to = ext4_get_inline_size(inode);
576 if (!PageUptodate(page)) {
577 ret = ext4_read_inline_page(inode, page);
578 if (ret < 0)
579 goto out;
580 }
581
582 ret = ext4_destroy_inline_data_nolock(handle, inode);
583 if (ret)
584 goto out;
585
Jan Kara705965b2016-03-08 23:08:10 -0500586 if (ext4_should_dioread_nolock(inode)) {
587 ret = __block_write_begin(page, from, to,
588 ext4_get_block_unwritten);
589 } else
Tao Maf19d5872012-12-10 14:05:51 -0500590 ret = __block_write_begin(page, from, to, ext4_get_block);
591
592 if (!ret && ext4_should_journal_data(inode)) {
593 ret = ext4_walk_page_buffers(handle, page_buffers(page),
594 from, to, NULL,
595 do_journal_get_write_access);
596 }
597
598 if (ret) {
599 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300600 put_page(page);
Darrick J. Wong684de5742014-09-11 11:45:12 -0400601 page = NULL;
Tao Maf19d5872012-12-10 14:05:51 -0500602 ext4_orphan_add(handle, inode);
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500603 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500604 sem_held = 0;
605 ext4_journal_stop(handle);
606 handle = NULL;
607 ext4_truncate_failed_write(inode);
608 /*
609 * If truncate failed early the inode might
610 * still be on the orphan list; we need to
611 * make sure the inode is removed from the
612 * orphan list in that case.
613 */
614 if (inode->i_nlink)
615 ext4_orphan_del(NULL, inode);
616 }
617
618 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
619 goto retry;
620
Darrick J. Wong684de5742014-09-11 11:45:12 -0400621 if (page)
622 block_commit_write(page, from, to);
Tao Maf19d5872012-12-10 14:05:51 -0500623out:
624 if (page) {
625 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300626 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500627 }
628 if (sem_held)
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500629 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500630 if (handle)
631 ext4_journal_stop(handle);
632 brelse(iloc.bh);
633 return ret;
634}
635
636/*
637 * Try to write data in the inode.
638 * If the inode has inline data, check whether the new write can be
639 * in the inode also. If not, create the page the handle, move the data
640 * to the page make it update and let the later codes create extent for it.
641 */
642int ext4_try_to_write_inline_data(struct address_space *mapping,
643 struct inode *inode,
644 loff_t pos, unsigned len,
645 unsigned flags,
646 struct page **pagep)
647{
648 int ret;
649 handle_t *handle;
650 struct page *page;
651 struct ext4_iloc iloc;
652
653 if (pos + len > ext4_get_max_inline_size(inode))
654 goto convert;
655
656 ret = ext4_get_inode_loc(inode, &iloc);
657 if (ret)
658 return ret;
659
660 /*
661 * The possible write could happen in the inode,
662 * so try to reserve the space in inode first.
663 */
Theodore Ts'o9924a922013-02-08 21:59:22 -0500664 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Tao Maf19d5872012-12-10 14:05:51 -0500665 if (IS_ERR(handle)) {
666 ret = PTR_ERR(handle);
667 handle = NULL;
668 goto out;
669 }
670
671 ret = ext4_prepare_inline_data(handle, inode, pos + len);
672 if (ret && ret != -ENOSPC)
673 goto out;
674
675 /* We don't have space in inline inode, so convert it to extent. */
676 if (ret == -ENOSPC) {
677 ext4_journal_stop(handle);
678 brelse(iloc.bh);
679 goto convert;
680 }
681
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400682 ret = ext4_journal_get_write_access(handle, iloc.bh);
683 if (ret)
684 goto out;
685
Tao Maf19d5872012-12-10 14:05:51 -0500686 flags |= AOP_FLAG_NOFS;
687
688 page = grab_cache_page_write_begin(mapping, 0, flags);
689 if (!page) {
690 ret = -ENOMEM;
691 goto out;
692 }
693
694 *pagep = page;
695 down_read(&EXT4_I(inode)->xattr_sem);
696 if (!ext4_has_inline_data(inode)) {
697 ret = 0;
698 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300699 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500700 goto out_up_read;
701 }
702
703 if (!PageUptodate(page)) {
704 ret = ext4_read_inline_page(inode, page);
705 if (ret < 0)
706 goto out_up_read;
707 }
708
709 ret = 1;
710 handle = NULL;
711out_up_read:
712 up_read(&EXT4_I(inode)->xattr_sem);
713out:
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400714 if (handle && (ret != 1))
Tao Maf19d5872012-12-10 14:05:51 -0500715 ext4_journal_stop(handle);
716 brelse(iloc.bh);
717 return ret;
718convert:
719 return ext4_convert_inline_data_to_extent(mapping,
720 inode, flags);
721}
722
723int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
724 unsigned copied, struct page *page)
725{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500726 int ret, no_expand;
Tao Maf19d5872012-12-10 14:05:51 -0500727 void *kaddr;
728 struct ext4_iloc iloc;
729
730 if (unlikely(copied < len)) {
731 if (!PageUptodate(page)) {
732 copied = 0;
733 goto out;
734 }
735 }
736
737 ret = ext4_get_inode_loc(inode, &iloc);
738 if (ret) {
739 ext4_std_error(inode->i_sb, ret);
740 copied = 0;
741 goto out;
742 }
743
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500744 ext4_write_lock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500745 BUG_ON(!ext4_has_inline_data(inode));
746
747 kaddr = kmap_atomic(page);
748 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
749 kunmap_atomic(kaddr);
750 SetPageUptodate(page);
751 /* clear page dirty so that writepages wouldn't work for us. */
752 ClearPageDirty(page);
753
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500754 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maf19d5872012-12-10 14:05:51 -0500755 brelse(iloc.bh);
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400756 mark_inode_dirty(inode);
Tao Maf19d5872012-12-10 14:05:51 -0500757out:
758 return copied;
759}
760
Tao Ma3fdcfb62012-12-10 14:05:57 -0500761struct buffer_head *
762ext4_journalled_write_inline_data(struct inode *inode,
763 unsigned len,
764 struct page *page)
765{
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500766 int ret, no_expand;
Tao Ma3fdcfb62012-12-10 14:05:57 -0500767 void *kaddr;
768 struct ext4_iloc iloc;
769
770 ret = ext4_get_inode_loc(inode, &iloc);
771 if (ret) {
772 ext4_std_error(inode->i_sb, ret);
773 return NULL;
774 }
775
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500776 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma3fdcfb62012-12-10 14:05:57 -0500777 kaddr = kmap_atomic(page);
778 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
779 kunmap_atomic(kaddr);
Theodore Ts'oda1e4022017-01-11 21:50:46 -0500780 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma3fdcfb62012-12-10 14:05:57 -0500781
782 return iloc.bh;
783}
784
Tao Ma9c3569b2012-12-10 14:05:57 -0500785/*
786 * Try to make the page cache and handle ready for the inline data case.
787 * We can call this function in 2 cases:
788 * 1. The inode is created and the first write exceeds inline size. We can
789 * clear the inode state safely.
790 * 2. The inode has inline data, then we need to read the data, make it
791 * update and dirty so that ext4_da_writepages can handle it. We don't
792 * need to start the journal since the file's metatdata isn't changed now.
793 */
794static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
795 struct inode *inode,
796 unsigned flags,
797 void **fsdata)
798{
799 int ret = 0, inline_size;
800 struct page *page;
801
802 page = grab_cache_page_write_begin(mapping, 0, flags);
803 if (!page)
804 return -ENOMEM;
805
806 down_read(&EXT4_I(inode)->xattr_sem);
807 if (!ext4_has_inline_data(inode)) {
808 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
809 goto out;
810 }
811
812 inline_size = ext4_get_inline_size(inode);
813
814 if (!PageUptodate(page)) {
815 ret = ext4_read_inline_page(inode, page);
816 if (ret < 0)
817 goto out;
818 }
819
820 ret = __block_write_begin(page, 0, inline_size,
821 ext4_da_get_block_prep);
822 if (ret) {
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500823 up_read(&EXT4_I(inode)->xattr_sem);
824 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300825 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500826 ext4_truncate_failed_write(inode);
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500827 return ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500828 }
829
830 SetPageDirty(page);
831 SetPageUptodate(page);
832 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
833 *fsdata = (void *)CONVERT_INLINE_DATA;
834
835out:
836 up_read(&EXT4_I(inode)->xattr_sem);
837 if (page) {
838 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300839 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500840 }
841 return ret;
842}
843
844/*
845 * Prepare the write for the inline data.
846 * If the the data can be written into the inode, we just read
847 * the page and make it uptodate, and start the journal.
848 * Otherwise read the page, makes it dirty so that it can be
849 * handle in writepages(the i_disksize update is left to the
850 * normal ext4_da_write_end).
851 */
852int ext4_da_write_inline_data_begin(struct address_space *mapping,
853 struct inode *inode,
854 loff_t pos, unsigned len,
855 unsigned flags,
856 struct page **pagep,
857 void **fsdata)
858{
859 int ret, inline_size;
860 handle_t *handle;
861 struct page *page;
862 struct ext4_iloc iloc;
Jan Karabc0ca9df2014-01-06 14:02:23 -0500863 int retries;
Tao Ma9c3569b2012-12-10 14:05:57 -0500864
865 ret = ext4_get_inode_loc(inode, &iloc);
866 if (ret)
867 return ret;
868
Jan Karabc0ca9df2014-01-06 14:02:23 -0500869retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500870 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Tao Ma9c3569b2012-12-10 14:05:57 -0500871 if (IS_ERR(handle)) {
872 ret = PTR_ERR(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500873 goto out;
874 }
875
876 inline_size = ext4_get_max_inline_size(inode);
877
878 ret = -ENOSPC;
879 if (inline_size >= pos + len) {
880 ret = ext4_prepare_inline_data(handle, inode, pos + len);
881 if (ret && ret != -ENOSPC)
Jan Kara52e44772014-01-06 14:03:23 -0500882 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500883 }
884
Dmitry Monakhov5cc28a92014-12-02 16:09:50 -0500885 /*
886 * We cannot recurse into the filesystem as the transaction
887 * is already started.
888 */
889 flags |= AOP_FLAG_NOFS;
890
Tao Ma9c3569b2012-12-10 14:05:57 -0500891 if (ret == -ENOSPC) {
892 ret = ext4_da_convert_inline_data_to_extent(mapping,
893 inode,
894 flags,
895 fsdata);
Jan Karabc0ca9df2014-01-06 14:02:23 -0500896 ext4_journal_stop(handle);
Jan Karabc0ca9df2014-01-06 14:02:23 -0500897 if (ret == -ENOSPC &&
898 ext4_should_retry_alloc(inode->i_sb, &retries))
899 goto retry_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500900 goto out;
901 }
902
Tao Ma9c3569b2012-12-10 14:05:57 -0500903 page = grab_cache_page_write_begin(mapping, 0, flags);
904 if (!page) {
905 ret = -ENOMEM;
Jan Kara52e44772014-01-06 14:03:23 -0500906 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500907 }
908
909 down_read(&EXT4_I(inode)->xattr_sem);
910 if (!ext4_has_inline_data(inode)) {
911 ret = 0;
912 goto out_release_page;
913 }
914
915 if (!PageUptodate(page)) {
916 ret = ext4_read_inline_page(inode, page);
917 if (ret < 0)
918 goto out_release_page;
919 }
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400920 ret = ext4_journal_get_write_access(handle, iloc.bh);
921 if (ret)
922 goto out_release_page;
Tao Ma9c3569b2012-12-10 14:05:57 -0500923
924 up_read(&EXT4_I(inode)->xattr_sem);
925 *pagep = page;
Tao Ma9c3569b2012-12-10 14:05:57 -0500926 brelse(iloc.bh);
927 return 1;
928out_release_page:
929 up_read(&EXT4_I(inode)->xattr_sem);
930 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300931 put_page(page);
Jan Kara52e44772014-01-06 14:03:23 -0500932out_journal:
933 ext4_journal_stop(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500934out:
Tao Ma9c3569b2012-12-10 14:05:57 -0500935 brelse(iloc.bh);
936 return ret;
937}
938
939int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
940 unsigned len, unsigned copied,
941 struct page *page)
942{
Theodore Ts'o0b37d0c2017-02-04 23:04:00 -0500943 int ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500944
Theodore Ts'o0b37d0c2017-02-04 23:04:00 -0500945 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
946 if (ret < 0) {
947 unlock_page(page);
948 put_page(page);
949 return ret;
950 }
951 copied = ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500952
953 /*
954 * No need to use i_size_read() here, the i_size
955 * cannot change under us because we hold i_mutex.
956 *
957 * But it's important to update i_size while still holding page lock:
958 * page writeout could otherwise come in and zero beyond i_size.
959 */
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400960 if (pos+copied > inode->i_size)
Tao Ma9c3569b2012-12-10 14:05:57 -0500961 i_size_write(inode, pos+copied);
Tao Ma9c3569b2012-12-10 14:05:57 -0500962 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300963 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500964
965 /*
966 * Don't mark the inode dirty under page lock. First, it unnecessarily
967 * makes the holding time of page lock longer. Second, it forces lock
968 * ordering of page lock and transaction start for journaling
969 * filesystems.
970 */
Theodore Ts'o5eed5972018-07-10 01:07:43 -0400971 mark_inode_dirty(inode);
Tao Ma9c3569b2012-12-10 14:05:57 -0500972
973 return copied;
974}
Tao Maf19d5872012-12-10 14:05:51 -0500975
Tao Ma3c47d542012-12-10 14:05:59 -0500976#ifdef INLINE_DIR_DEBUG
977void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
978 void *inline_start, int inline_size)
979{
980 int offset;
981 unsigned short de_len;
982 struct ext4_dir_entry_2 *de = inline_start;
983 void *dlimit = inline_start + inline_size;
984
985 trace_printk("inode %lu\n", dir->i_ino);
986 offset = 0;
987 while ((void *)de < dlimit) {
988 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
Rasmus Villemoes80cfb712015-04-02 16:42:43 -0400989 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
Tao Ma3c47d542012-12-10 14:05:59 -0500990 offset, de_len, de->name_len, de->name,
991 de->name_len, le32_to_cpu(de->inode));
992 if (ext4_check_dir_entry(dir, NULL, de, bh,
993 inline_start, inline_size, offset))
994 BUG();
995
996 offset += de_len;
997 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
998 }
999}
1000#else
1001#define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1002#endif
1003
1004/*
1005 * Add a new entry into a inline dir.
1006 * It will return -ENOSPC if no space is available, and -EIO
1007 * and -EEXIST if directory entry already exists.
1008 */
1009static int ext4_add_dirent_to_inline(handle_t *handle,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001010 struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001011 struct inode *dir,
Tao Ma3c47d542012-12-10 14:05:59 -05001012 struct inode *inode,
1013 struct ext4_iloc *iloc,
1014 void *inline_start, int inline_size)
1015{
Tao Ma3c47d542012-12-10 14:05:59 -05001016 int err;
1017 struct ext4_dir_entry_2 *de;
1018
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001019 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1020 inline_size, fname, &de);
Tao Ma3c47d542012-12-10 14:05:59 -05001021 if (err)
1022 return err;
1023
liang xie5d601252014-05-12 22:06:43 -04001024 BUFFER_TRACE(iloc->bh, "get_write_access");
Tao Ma3c47d542012-12-10 14:05:59 -05001025 err = ext4_journal_get_write_access(handle, iloc->bh);
1026 if (err)
1027 return err;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001028 ext4_insert_dentry(dir, inode, de, inline_size, fname);
Tao Ma3c47d542012-12-10 14:05:59 -05001029
1030 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1031
1032 /*
1033 * XXX shouldn't update any times until successful
1034 * completion of syscall, but too many callers depend
1035 * on this.
1036 *
1037 * XXX similarly, too many callers depend on
1038 * ext4_new_inode() setting the times, but error
1039 * recovery deletes the inode, so the worst that can
1040 * happen is that the times are slightly out of date
1041 * and/or different from the directory change time.
1042 */
1043 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1044 ext4_update_dx_flag(dir);
1045 dir->i_version++;
1046 ext4_mark_inode_dirty(handle, dir);
1047 return 1;
1048}
1049
1050static void *ext4_get_inline_xattr_pos(struct inode *inode,
1051 struct ext4_iloc *iloc)
1052{
1053 struct ext4_xattr_entry *entry;
1054 struct ext4_xattr_ibody_header *header;
1055
1056 BUG_ON(!EXT4_I(inode)->i_inline_off);
1057
1058 header = IHDR(inode, ext4_raw_inode(iloc));
1059 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1060 EXT4_I(inode)->i_inline_off);
1061
1062 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1063}
1064
1065/* Set the final de to cover the whole block. */
1066static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1067{
1068 struct ext4_dir_entry_2 *de, *prev_de;
1069 void *limit;
1070 int de_len;
1071
1072 de = (struct ext4_dir_entry_2 *)de_buf;
1073 if (old_size) {
1074 limit = de_buf + old_size;
1075 do {
1076 prev_de = de;
1077 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1078 de_buf += de_len;
1079 de = (struct ext4_dir_entry_2 *)de_buf;
1080 } while (de_buf < limit);
1081
1082 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1083 old_size, new_size);
1084 } else {
1085 /* this is just created, so create an empty entry. */
1086 de->inode = 0;
1087 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1088 }
1089}
1090
1091static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1092 struct ext4_iloc *iloc)
1093{
1094 int ret;
1095 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1096 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1097
1098 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1099 return -ENOSPC;
1100
1101 ret = ext4_update_inline_data(handle, dir,
1102 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1103 if (ret)
1104 return ret;
1105
1106 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1107 EXT4_I(dir)->i_inline_size -
1108 EXT4_MIN_INLINE_DATA_SIZE);
1109 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1110 return 0;
1111}
1112
1113static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1114 struct ext4_iloc *iloc,
1115 void *buf, int inline_size)
1116{
1117 ext4_create_inline_data(handle, inode, inline_size);
1118 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1119 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1120}
1121
1122static int ext4_finish_convert_inline_dir(handle_t *handle,
1123 struct inode *inode,
1124 struct buffer_head *dir_block,
1125 void *buf,
1126 int inline_size)
1127{
1128 int err, csum_size = 0, header_size = 0;
1129 struct ext4_dir_entry_2 *de;
1130 struct ext4_dir_entry_tail *t;
1131 void *target = dir_block->b_data;
1132
1133 /*
1134 * First create "." and ".." and then copy the dir information
1135 * back to the block.
1136 */
1137 de = (struct ext4_dir_entry_2 *)target;
1138 de = ext4_init_dot_dotdot(inode, de,
1139 inode->i_sb->s_blocksize, csum_size,
1140 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1141 header_size = (void *)de - target;
1142
1143 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1144 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1145
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001146 if (ext4_has_metadata_csum(inode->i_sb))
Tao Ma3c47d542012-12-10 14:05:59 -05001147 csum_size = sizeof(struct ext4_dir_entry_tail);
1148
1149 inode->i_size = inode->i_sb->s_blocksize;
1150 i_size_write(inode, inode->i_sb->s_blocksize);
1151 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1152 ext4_update_final_de(dir_block->b_data,
1153 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1154 inode->i_sb->s_blocksize - csum_size);
1155
1156 if (csum_size) {
1157 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1158 inode->i_sb->s_blocksize);
1159 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1160 }
1161 set_buffer_uptodate(dir_block);
1162 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1163 if (err)
Eric Biggersf572ba92017-03-15 14:52:02 -04001164 return err;
Tao Ma3c47d542012-12-10 14:05:59 -05001165 set_buffer_verified(dir_block);
Eric Biggersf572ba92017-03-15 14:52:02 -04001166 return ext4_mark_inode_dirty(handle, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05001167}
1168
1169static int ext4_convert_inline_data_nolock(handle_t *handle,
1170 struct inode *inode,
1171 struct ext4_iloc *iloc)
1172{
1173 int error;
1174 void *buf = NULL;
1175 struct buffer_head *data_bh = NULL;
1176 struct ext4_map_blocks map;
1177 int inline_size;
1178
1179 inline_size = ext4_get_inline_size(inode);
1180 buf = kmalloc(inline_size, GFP_NOFS);
1181 if (!buf) {
1182 error = -ENOMEM;
1183 goto out;
1184 }
1185
1186 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1187 if (error < 0)
1188 goto out;
1189
Darrick J. Wong40b163f2014-07-28 13:06:26 -04001190 /*
1191 * Make sure the inline directory entries pass checks before we try to
1192 * convert them, so that we avoid touching stuff that needs fsck.
1193 */
1194 if (S_ISDIR(inode->i_mode)) {
1195 error = ext4_check_all_de(inode, iloc->bh,
1196 buf + EXT4_INLINE_DOTDOT_SIZE,
1197 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1198 if (error)
1199 goto out;
1200 }
1201
Tao Ma3c47d542012-12-10 14:05:59 -05001202 error = ext4_destroy_inline_data_nolock(handle, inode);
1203 if (error)
1204 goto out;
1205
1206 map.m_lblk = 0;
1207 map.m_len = 1;
1208 map.m_flags = 0;
1209 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1210 if (error < 0)
1211 goto out_restore;
1212 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1213 error = -EIO;
1214 goto out_restore;
1215 }
1216
1217 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1218 if (!data_bh) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001219 error = -ENOMEM;
Tao Ma3c47d542012-12-10 14:05:59 -05001220 goto out_restore;
1221 }
1222
1223 lock_buffer(data_bh);
1224 error = ext4_journal_get_create_access(handle, data_bh);
1225 if (error) {
1226 unlock_buffer(data_bh);
1227 error = -EIO;
1228 goto out_restore;
1229 }
1230 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1231
1232 if (!S_ISDIR(inode->i_mode)) {
1233 memcpy(data_bh->b_data, buf, inline_size);
1234 set_buffer_uptodate(data_bh);
1235 error = ext4_handle_dirty_metadata(handle,
1236 inode, data_bh);
1237 } else {
1238 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1239 buf, inline_size);
1240 }
1241
1242 unlock_buffer(data_bh);
1243out_restore:
1244 if (error)
1245 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1246
1247out:
1248 brelse(data_bh);
1249 kfree(buf);
1250 return error;
1251}
1252
1253/*
1254 * Try to add the new entry to the inline data.
1255 * If succeeds, return 0. If not, extended the inline dir and copied data to
1256 * the new created block.
1257 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001258int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001259 struct inode *dir, struct inode *inode)
Tao Ma3c47d542012-12-10 14:05:59 -05001260{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001261 int ret, inline_size, no_expand;
Tao Ma3c47d542012-12-10 14:05:59 -05001262 void *inline_start;
1263 struct ext4_iloc iloc;
Tao Ma3c47d542012-12-10 14:05:59 -05001264
1265 ret = ext4_get_inode_loc(dir, &iloc);
1266 if (ret)
1267 return ret;
1268
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001269 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma3c47d542012-12-10 14:05:59 -05001270 if (!ext4_has_inline_data(dir))
1271 goto out;
1272
1273 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1274 EXT4_INLINE_DOTDOT_SIZE;
1275 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1276
Theodore Ts'o56a04912016-01-08 16:00:31 -05001277 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
Tao Ma3c47d542012-12-10 14:05:59 -05001278 inline_start, inline_size);
1279 if (ret != -ENOSPC)
1280 goto out;
1281
1282 /* check whether it can be inserted to inline xattr space. */
1283 inline_size = EXT4_I(dir)->i_inline_size -
1284 EXT4_MIN_INLINE_DATA_SIZE;
1285 if (!inline_size) {
1286 /* Try to use the xattr space.*/
1287 ret = ext4_update_inline_dir(handle, dir, &iloc);
1288 if (ret && ret != -ENOSPC)
1289 goto out;
1290
1291 inline_size = EXT4_I(dir)->i_inline_size -
1292 EXT4_MIN_INLINE_DATA_SIZE;
1293 }
1294
1295 if (inline_size) {
1296 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1297
Theodore Ts'o56a04912016-01-08 16:00:31 -05001298 ret = ext4_add_dirent_to_inline(handle, fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001299 inode, &iloc, inline_start,
1300 inline_size);
Tao Ma3c47d542012-12-10 14:05:59 -05001301
1302 if (ret != -ENOSPC)
1303 goto out;
1304 }
1305
1306 /*
1307 * The inline space is filled up, so create a new block for it.
1308 * As the extent tree will be created, we have to save the inline
1309 * dir first.
1310 */
1311 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1312
1313out:
1314 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001315 ext4_write_unlock_xattr(dir, &no_expand);
Tao Ma3c47d542012-12-10 14:05:59 -05001316 brelse(iloc.bh);
1317 return ret;
1318}
1319
Tao Ma8af0f082013-04-19 17:53:09 -04001320/*
1321 * This function fills a red-black tree with information from an
1322 * inlined dir. It returns the number directory entries loaded
1323 * into the tree. If there is an error it is returned in err.
1324 */
1325int htree_inlinedir_to_tree(struct file *dir_file,
1326 struct inode *dir, ext4_lblk_t block,
1327 struct dx_hash_info *hinfo,
1328 __u32 start_hash, __u32 start_minor_hash,
1329 int *has_inline_data)
1330{
1331 int err = 0, count = 0;
1332 unsigned int parent_ino;
1333 int pos;
1334 struct ext4_dir_entry_2 *de;
1335 struct inode *inode = file_inode(dir_file);
1336 int ret, inline_size = 0;
1337 struct ext4_iloc iloc;
1338 void *dir_buf = NULL;
1339 struct ext4_dir_entry_2 fake;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001340 struct fscrypt_str tmp_str;
Tao Ma8af0f082013-04-19 17:53:09 -04001341
1342 ret = ext4_get_inode_loc(inode, &iloc);
1343 if (ret)
1344 return ret;
1345
1346 down_read(&EXT4_I(inode)->xattr_sem);
1347 if (!ext4_has_inline_data(inode)) {
1348 up_read(&EXT4_I(inode)->xattr_sem);
1349 *has_inline_data = 0;
1350 goto out;
1351 }
1352
1353 inline_size = ext4_get_inline_size(inode);
1354 dir_buf = kmalloc(inline_size, GFP_NOFS);
1355 if (!dir_buf) {
1356 ret = -ENOMEM;
1357 up_read(&EXT4_I(inode)->xattr_sem);
1358 goto out;
1359 }
1360
1361 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1362 up_read(&EXT4_I(inode)->xattr_sem);
1363 if (ret < 0)
1364 goto out;
1365
1366 pos = 0;
1367 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1368 while (pos < inline_size) {
1369 /*
1370 * As inlined dir doesn't store any information about '.' and
1371 * only the inode number of '..' is stored, we have to handle
1372 * them differently.
1373 */
1374 if (pos == 0) {
1375 fake.inode = cpu_to_le32(inode->i_ino);
1376 fake.name_len = 1;
1377 strcpy(fake.name, ".");
1378 fake.rec_len = ext4_rec_len_to_disk(
1379 EXT4_DIR_REC_LEN(fake.name_len),
1380 inline_size);
1381 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1382 de = &fake;
1383 pos = EXT4_INLINE_DOTDOT_OFFSET;
1384 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1385 fake.inode = cpu_to_le32(parent_ino);
1386 fake.name_len = 2;
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_SIZE;
1394 } else {
1395 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1396 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1397 if (ext4_check_dir_entry(inode, dir_file, de,
1398 iloc.bh, dir_buf,
1399 inline_size, pos)) {
1400 ret = count;
1401 goto out;
1402 }
1403 }
1404
1405 ext4fs_dirhash(de->name, de->name_len, hinfo);
1406 if ((hinfo->hash < start_hash) ||
1407 ((hinfo->hash == start_hash) &&
1408 (hinfo->minor_hash < start_minor_hash)))
1409 continue;
1410 if (de->inode == 0)
1411 continue;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001412 tmp_str.name = de->name;
1413 tmp_str.len = de->name_len;
1414 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1415 hinfo->minor_hash, de, &tmp_str);
Tao Ma8af0f082013-04-19 17:53:09 -04001416 if (err) {
1417 count = err;
1418 goto out;
1419 }
1420 count++;
1421 }
1422 ret = count;
1423out:
1424 kfree(dir_buf);
1425 brelse(iloc.bh);
1426 return ret;
1427}
1428
Tao Mac4d8b022013-04-19 17:55:33 -04001429/*
1430 * So this function is called when the volume is mkfsed with
1431 * dir_index disabled. In order to keep f_pos persistent
1432 * after we convert from an inlined dir to a blocked based,
1433 * we just pretend that we are a normal dir and return the
1434 * offset as if '.' and '..' really take place.
1435 *
1436 */
Al Viro725bebb2013-05-17 16:08:53 -04001437int ext4_read_inline_dir(struct file *file,
1438 struct dir_context *ctx,
Tao Ma65d165d2012-12-10 14:05:59 -05001439 int *has_inline_data)
1440{
Tao Ma65d165d2012-12-10 14:05:59 -05001441 unsigned int offset, parent_ino;
Al Viro725bebb2013-05-17 16:08:53 -04001442 int i;
Tao Ma65d165d2012-12-10 14:05:59 -05001443 struct ext4_dir_entry_2 *de;
1444 struct super_block *sb;
Al Viro725bebb2013-05-17 16:08:53 -04001445 struct inode *inode = file_inode(file);
Tao Ma65d165d2012-12-10 14:05:59 -05001446 int ret, inline_size = 0;
1447 struct ext4_iloc iloc;
1448 void *dir_buf = NULL;
Tao Mac4d8b022013-04-19 17:55:33 -04001449 int dotdot_offset, dotdot_size, extra_offset, extra_size;
Tao Ma65d165d2012-12-10 14:05:59 -05001450
1451 ret = ext4_get_inode_loc(inode, &iloc);
1452 if (ret)
1453 return ret;
1454
1455 down_read(&EXT4_I(inode)->xattr_sem);
1456 if (!ext4_has_inline_data(inode)) {
1457 up_read(&EXT4_I(inode)->xattr_sem);
1458 *has_inline_data = 0;
1459 goto out;
1460 }
1461
1462 inline_size = ext4_get_inline_size(inode);
1463 dir_buf = kmalloc(inline_size, GFP_NOFS);
1464 if (!dir_buf) {
1465 ret = -ENOMEM;
1466 up_read(&EXT4_I(inode)->xattr_sem);
1467 goto out;
1468 }
1469
1470 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1471 up_read(&EXT4_I(inode)->xattr_sem);
1472 if (ret < 0)
1473 goto out;
1474
BoxiLiu48ffdab2013-10-30 08:07:20 -04001475 ret = 0;
Tao Ma65d165d2012-12-10 14:05:59 -05001476 sb = inode->i_sb;
Tao Ma65d165d2012-12-10 14:05:59 -05001477 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
Al Viro725bebb2013-05-17 16:08:53 -04001478 offset = ctx->pos;
Tao Ma65d165d2012-12-10 14:05:59 -05001479
Tao Mac4d8b022013-04-19 17:55:33 -04001480 /*
1481 * dotdot_offset and dotdot_size is the real offset and
1482 * size for ".." and "." if the dir is block based while
1483 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1484 * So we will use extra_offset and extra_size to indicate them
1485 * during the inline dir iteration.
1486 */
1487 dotdot_offset = EXT4_DIR_REC_LEN(1);
1488 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1489 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1490 extra_size = extra_offset + inline_size;
1491
Al Viro725bebb2013-05-17 16:08:53 -04001492 /*
1493 * If the version has changed since the last call to
1494 * readdir(2), then we might be pointing to an invalid
1495 * dirent right now. Scan from the start of the inline
1496 * dir to make sure.
1497 */
1498 if (file->f_version != inode->i_version) {
1499 for (i = 0; i < extra_size && i < offset;) {
1500 /*
1501 * "." is with offset 0 and
1502 * ".." is dotdot_offset.
1503 */
1504 if (!i) {
1505 i = dotdot_offset;
1506 continue;
1507 } else if (i == dotdot_offset) {
1508 i = dotdot_size;
Tao Mac4d8b022013-04-19 17:55:33 -04001509 continue;
1510 }
Al Viro725bebb2013-05-17 16:08:53 -04001511 /* for other entry, the real offset in
1512 * the buf has to be tuned accordingly.
1513 */
Tao Mac4d8b022013-04-19 17:55:33 -04001514 de = (struct ext4_dir_entry_2 *)
Al Viro725bebb2013-05-17 16:08:53 -04001515 (dir_buf + i - extra_offset);
1516 /* It's too expensive to do a full
1517 * dirent test each time round this
1518 * loop, but we do have to test at
1519 * least that it is non-zero. A
1520 * failure will be detected in the
1521 * dirent test below. */
1522 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1523 < EXT4_DIR_REC_LEN(1))
1524 break;
1525 i += ext4_rec_len_from_disk(de->rec_len,
1526 extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001527 }
Al Viro725bebb2013-05-17 16:08:53 -04001528 offset = i;
1529 ctx->pos = offset;
1530 file->f_version = inode->i_version;
1531 }
1532
1533 while (ctx->pos < extra_size) {
1534 if (ctx->pos == 0) {
1535 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1536 goto out;
1537 ctx->pos = dotdot_offset;
1538 continue;
1539 }
1540
1541 if (ctx->pos == dotdot_offset) {
1542 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1543 goto out;
1544 ctx->pos = dotdot_size;
1545 continue;
1546 }
1547
1548 de = (struct ext4_dir_entry_2 *)
1549 (dir_buf + ctx->pos - extra_offset);
1550 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1551 extra_size, ctx->pos))
1552 goto out;
1553 if (le32_to_cpu(de->inode)) {
1554 if (!dir_emit(ctx, de->name, de->name_len,
1555 le32_to_cpu(de->inode),
1556 get_dtype(sb, de->file_type)))
1557 goto out;
1558 }
1559 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001560 }
1561out:
1562 kfree(dir_buf);
1563 brelse(iloc.bh);
1564 return ret;
1565}
1566
Tao Ma32f7f222012-12-10 14:06:01 -05001567struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1568 struct ext4_dir_entry_2 **parent_de,
1569 int *retval)
1570{
1571 struct ext4_iloc iloc;
1572
1573 *retval = ext4_get_inode_loc(inode, &iloc);
1574 if (*retval)
1575 return NULL;
1576
1577 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1578
1579 return iloc.bh;
1580}
1581
Tao Ma3c47d542012-12-10 14:05:59 -05001582/*
1583 * Try to create the inline data for the new dir.
1584 * If it succeeds, return 0, otherwise return the error.
1585 * In case of ENOSPC, the caller should create the normal disk layout dir.
1586 */
1587int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1588 struct inode *inode)
1589{
1590 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1591 struct ext4_iloc iloc;
1592 struct ext4_dir_entry_2 *de;
1593
1594 ret = ext4_get_inode_loc(inode, &iloc);
1595 if (ret)
1596 return ret;
1597
1598 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1599 if (ret)
1600 goto out;
1601
1602 /*
1603 * For inline dir, we only save the inode information for the ".."
1604 * and create a fake dentry to cover the left space.
1605 */
1606 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1607 de->inode = cpu_to_le32(parent->i_ino);
1608 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1609 de->inode = 0;
1610 de->rec_len = ext4_rec_len_to_disk(
1611 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1612 inline_size);
1613 set_nlink(inode, 2);
1614 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1615out:
1616 brelse(iloc.bh);
1617 return ret;
1618}
1619
Tao Mae8e948e2012-12-10 14:06:00 -05001620struct buffer_head *ext4_find_inline_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001621 struct ext4_filename *fname,
Tao Mae8e948e2012-12-10 14:06:00 -05001622 const struct qstr *d_name,
1623 struct ext4_dir_entry_2 **res_dir,
1624 int *has_inline_data)
1625{
1626 int ret;
1627 struct ext4_iloc iloc;
1628 void *inline_start;
1629 int inline_size;
1630
1631 if (ext4_get_inode_loc(dir, &iloc))
1632 return NULL;
1633
1634 down_read(&EXT4_I(dir)->xattr_sem);
1635 if (!ext4_has_inline_data(dir)) {
1636 *has_inline_data = 0;
1637 goto out;
1638 }
1639
1640 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1641 EXT4_INLINE_DOTDOT_SIZE;
1642 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001643 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1644 dir, fname, d_name, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001645 if (ret == 1)
1646 goto out_find;
1647 if (ret < 0)
1648 goto out;
1649
1650 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1651 goto out;
1652
1653 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1654 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1655
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001656 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1657 dir, fname, d_name, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001658 if (ret == 1)
1659 goto out_find;
1660
1661out:
1662 brelse(iloc.bh);
1663 iloc.bh = NULL;
1664out_find:
1665 up_read(&EXT4_I(dir)->xattr_sem);
1666 return iloc.bh;
1667}
1668
Tao Ma9f40fe52012-12-10 14:06:00 -05001669int ext4_delete_inline_entry(handle_t *handle,
1670 struct inode *dir,
1671 struct ext4_dir_entry_2 *de_del,
1672 struct buffer_head *bh,
1673 int *has_inline_data)
1674{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001675 int err, inline_size, no_expand;
Tao Ma9f40fe52012-12-10 14:06:00 -05001676 struct ext4_iloc iloc;
1677 void *inline_start;
1678
1679 err = ext4_get_inode_loc(dir, &iloc);
1680 if (err)
1681 return err;
1682
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001683 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma9f40fe52012-12-10 14:06:00 -05001684 if (!ext4_has_inline_data(dir)) {
1685 *has_inline_data = 0;
1686 goto out;
1687 }
1688
1689 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1690 EXT4_MIN_INLINE_DATA_SIZE) {
1691 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1692 EXT4_INLINE_DOTDOT_SIZE;
1693 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1694 EXT4_INLINE_DOTDOT_SIZE;
1695 } else {
1696 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1697 inline_size = ext4_get_inline_size(dir) -
1698 EXT4_MIN_INLINE_DATA_SIZE;
1699 }
1700
liang xie5d601252014-05-12 22:06:43 -04001701 BUFFER_TRACE(bh, "get_write_access");
Tao Ma9f40fe52012-12-10 14:06:00 -05001702 err = ext4_journal_get_write_access(handle, bh);
1703 if (err)
1704 goto out;
1705
1706 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1707 inline_start, inline_size, 0);
1708 if (err)
1709 goto out;
1710
Tao Ma9f40fe52012-12-10 14:06:00 -05001711 err = ext4_mark_inode_dirty(handle, dir);
1712 if (unlikely(err))
1713 goto out;
1714
1715 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1716out:
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001717 ext4_write_unlock_xattr(dir, &no_expand);
Tao Ma9f40fe52012-12-10 14:06:00 -05001718 brelse(iloc.bh);
1719 if (err != -ENOENT)
1720 ext4_std_error(dir->i_sb, err);
1721 return err;
1722}
1723
Tao Ma61f86632012-12-10 14:06:01 -05001724/*
1725 * Get the inline dentry at offset.
1726 */
1727static inline struct ext4_dir_entry_2 *
1728ext4_get_inline_entry(struct inode *inode,
1729 struct ext4_iloc *iloc,
1730 unsigned int offset,
1731 void **inline_start,
1732 int *inline_size)
1733{
1734 void *inline_pos;
1735
1736 BUG_ON(offset > ext4_get_inline_size(inode));
1737
1738 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1739 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1740 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1741 } else {
1742 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1743 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1744 *inline_size = ext4_get_inline_size(inode) -
1745 EXT4_MIN_INLINE_DATA_SIZE;
1746 }
1747
1748 if (inline_start)
1749 *inline_start = inline_pos;
1750 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1751}
1752
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001753bool empty_inline_dir(struct inode *dir, int *has_inline_data)
Tao Ma61f86632012-12-10 14:06:01 -05001754{
1755 int err, inline_size;
1756 struct ext4_iloc iloc;
1757 void *inline_pos;
1758 unsigned int offset;
1759 struct ext4_dir_entry_2 *de;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001760 bool ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001761
1762 err = ext4_get_inode_loc(dir, &iloc);
1763 if (err) {
1764 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1765 err, dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001766 return true;
Tao Ma61f86632012-12-10 14:06:01 -05001767 }
1768
1769 down_read(&EXT4_I(dir)->xattr_sem);
1770 if (!ext4_has_inline_data(dir)) {
1771 *has_inline_data = 0;
1772 goto out;
1773 }
1774
1775 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1776 if (!le32_to_cpu(de->inode)) {
1777 ext4_warning(dir->i_sb,
1778 "bad inline directory (dir #%lu) - no `..'",
1779 dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001780 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001781 goto out;
1782 }
1783
1784 offset = EXT4_INLINE_DOTDOT_SIZE;
1785 while (offset < dir->i_size) {
1786 de = ext4_get_inline_entry(dir, &iloc, offset,
1787 &inline_pos, &inline_size);
1788 if (ext4_check_dir_entry(dir, NULL, de,
1789 iloc.bh, inline_pos,
1790 inline_size, offset)) {
1791 ext4_warning(dir->i_sb,
1792 "bad inline directory (dir #%lu) - "
1793 "inode %u, rec_len %u, name_len %d"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001794 "inline size %d",
Tao Ma61f86632012-12-10 14:06:01 -05001795 dir->i_ino, le32_to_cpu(de->inode),
1796 le16_to_cpu(de->rec_len), de->name_len,
1797 inline_size);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001798 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001799 goto out;
1800 }
1801 if (le32_to_cpu(de->inode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001802 ret = false;
Tao Ma61f86632012-12-10 14:06:01 -05001803 goto out;
1804 }
1805 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1806 }
1807
1808out:
1809 up_read(&EXT4_I(dir)->xattr_sem);
1810 brelse(iloc.bh);
1811 return ret;
1812}
1813
Tao Ma67cf5b02012-12-10 14:04:46 -05001814int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1815{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001816 int ret, no_expand;
Tao Ma67cf5b02012-12-10 14:04:46 -05001817
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001818 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001819 ret = ext4_destroy_inline_data_nolock(handle, inode);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001820 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001821
1822 return ret;
1823}
Tao Ma94191982012-12-10 14:06:02 -05001824
1825int ext4_inline_data_fiemap(struct inode *inode,
1826 struct fiemap_extent_info *fieinfo,
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001827 int *has_inline, __u64 start, __u64 len)
Tao Ma94191982012-12-10 14:06:02 -05001828{
1829 __u64 physical = 0;
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001830 __u64 inline_len;
1831 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1832 FIEMAP_EXTENT_LAST;
Tao Ma94191982012-12-10 14:06:02 -05001833 int error = 0;
1834 struct ext4_iloc iloc;
1835
1836 down_read(&EXT4_I(inode)->xattr_sem);
1837 if (!ext4_has_inline_data(inode)) {
1838 *has_inline = 0;
1839 goto out;
1840 }
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001841 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1842 i_size_read(inode));
1843 if (start >= inline_len)
1844 goto out;
1845 if (start + len < inline_len)
1846 inline_len = start + len;
1847 inline_len -= start;
Tao Ma94191982012-12-10 14:06:02 -05001848
1849 error = ext4_get_inode_loc(inode, &iloc);
1850 if (error)
1851 goto out;
1852
Jan Karaeaf37932013-05-31 19:33:42 -04001853 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
Tao Ma94191982012-12-10 14:06:02 -05001854 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1855 physical += offsetof(struct ext4_inode, i_block);
Tao Ma94191982012-12-10 14:06:02 -05001856
1857 if (physical)
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001858 error = fiemap_fill_next_extent(fieinfo, start, physical,
1859 inline_len, flags);
Tao Ma94191982012-12-10 14:06:02 -05001860 brelse(iloc.bh);
1861out:
1862 up_read(&EXT4_I(inode)->xattr_sem);
1863 return (error < 0 ? error : 0);
1864}
Tao Ma0d812f72012-12-10 14:06:02 -05001865
1866/*
1867 * Called during xattr set, and if we can sparse space 'needed',
1868 * just create the extent tree evict the data to the outer block.
1869 *
1870 * We use jbd2 instead of page cache to move data to the 1st block
1871 * so that the whole transaction can be committed as a whole and
1872 * the data isn't lost because of the delayed page cache write.
1873 */
1874int ext4_try_to_evict_inline_data(handle_t *handle,
1875 struct inode *inode,
1876 int needed)
1877{
1878 int error;
1879 struct ext4_xattr_entry *entry;
Tao Ma0d812f72012-12-10 14:06:02 -05001880 struct ext4_inode *raw_inode;
1881 struct ext4_iloc iloc;
1882
1883 error = ext4_get_inode_loc(inode, &iloc);
1884 if (error)
1885 return error;
1886
1887 raw_inode = ext4_raw_inode(&iloc);
Tao Ma0d812f72012-12-10 14:06:02 -05001888 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
1889 EXT4_I(inode)->i_inline_off);
1890 if (EXT4_XATTR_LEN(entry->e_name_len) +
1891 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) {
1892 error = -ENOSPC;
1893 goto out;
1894 }
1895
1896 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
1897out:
1898 brelse(iloc.bh);
1899 return error;
1900}
Tao Maaef1c852012-12-10 14:06:02 -05001901
1902void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1903{
1904 handle_t *handle;
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001905 int inline_size, value_len, needed_blocks, no_expand;
Tao Maaef1c852012-12-10 14:06:02 -05001906 size_t i_size;
1907 void *value = NULL;
1908 struct ext4_xattr_ibody_find is = {
1909 .s = { .not_found = -ENODATA, },
1910 };
1911 struct ext4_xattr_info i = {
1912 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1913 .name = EXT4_XATTR_SYSTEM_DATA,
1914 };
1915
1916
1917 needed_blocks = ext4_writepage_trans_blocks(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -05001918 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
Tao Maaef1c852012-12-10 14:06:02 -05001919 if (IS_ERR(handle))
1920 return;
1921
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001922 ext4_write_lock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05001923 if (!ext4_has_inline_data(inode)) {
1924 *has_inline = 0;
1925 ext4_journal_stop(handle);
1926 return;
1927 }
1928
1929 if (ext4_orphan_add(handle, inode))
1930 goto out;
1931
1932 if (ext4_get_inode_loc(inode, &is.iloc))
1933 goto out;
1934
1935 down_write(&EXT4_I(inode)->i_data_sem);
1936 i_size = inode->i_size;
1937 inline_size = ext4_get_inline_size(inode);
1938 EXT4_I(inode)->i_disksize = i_size;
1939
1940 if (i_size < inline_size) {
1941 /* Clear the content in the xattr space. */
1942 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1943 if (ext4_xattr_ibody_find(inode, &i, &is))
1944 goto out_error;
1945
1946 BUG_ON(is.s.not_found);
1947
1948 value_len = le32_to_cpu(is.s.here->e_value_size);
1949 value = kmalloc(value_len, GFP_NOFS);
1950 if (!value)
1951 goto out_error;
1952
1953 if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
1954 value, value_len))
1955 goto out_error;
1956
1957 i.value = value;
1958 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1959 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1960 if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
1961 goto out_error;
1962 }
1963
1964 /* Clear the content within i_blocks. */
Theodore Ts'o09c455a2014-01-07 12:58:19 -05001965 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1966 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1967 memset(p + i_size, 0,
1968 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1969 }
Tao Maaef1c852012-12-10 14:06:02 -05001970
1971 EXT4_I(inode)->i_inline_size = i_size <
1972 EXT4_MIN_INLINE_DATA_SIZE ?
1973 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1974 }
1975
1976out_error:
1977 up_write(&EXT4_I(inode)->i_data_sem);
1978out:
1979 brelse(is.iloc.bh);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001980 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05001981 kfree(value);
1982 if (inode->i_nlink)
1983 ext4_orphan_del(handle, inode);
1984
1985 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
1986 ext4_mark_inode_dirty(handle, inode);
1987 if (IS_SYNC(inode))
1988 ext4_handle_sync(handle);
1989
1990 ext4_journal_stop(handle);
1991 return;
1992}
Tao Ma0c8d4142012-12-10 14:06:03 -05001993
1994int ext4_convert_inline_data(struct inode *inode)
1995{
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001996 int error, needed_blocks, no_expand;
Tao Ma0c8d4142012-12-10 14:06:03 -05001997 handle_t *handle;
1998 struct ext4_iloc iloc;
1999
2000 if (!ext4_has_inline_data(inode)) {
2001 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2002 return 0;
2003 }
2004
2005 needed_blocks = ext4_writepage_trans_blocks(inode);
2006
2007 iloc.bh = NULL;
2008 error = ext4_get_inode_loc(inode, &iloc);
2009 if (error)
2010 return error;
2011
Theodore Ts'o9924a922013-02-08 21:59:22 -05002012 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Ma0c8d4142012-12-10 14:06:03 -05002013 if (IS_ERR(handle)) {
2014 error = PTR_ERR(handle);
2015 goto out_free;
2016 }
2017
Theodore Ts'oda1e4022017-01-11 21:50:46 -05002018 ext4_write_lock_xattr(inode, &no_expand);
2019 if (ext4_has_inline_data(inode))
2020 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2021 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma0c8d4142012-12-10 14:06:03 -05002022 ext4_journal_stop(handle);
2023out_free:
2024 brelse(iloc.bh);
2025 return error;
2026}