blob: f0bbc8cb65555169e85abb23587ddcb7006547bf [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
Andreas Gruenbacher7046ae32017-10-01 17:57:54 -040015#include <linux/iomap.h>
Michael Halcrow4bdfc872015-04-12 00:56:28 -040016#include <linux/fiemap.h>
17
Tao Ma67cf5b02012-12-10 14:04:46 -050018#include "ext4_jbd2.h"
19#include "ext4.h"
20#include "xattr.h"
Tao Maf19d5872012-12-10 14:05:51 -050021#include "truncate.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)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -040065 if (!entry->e_value_inum && entry->e_value_size) {
Tao Ma67cf5b02012-12-10 14:04:46 -050066 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
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500219 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
220 return;
221
Tao Ma67cf5b02012-12-10 14:04:46 -0500222 BUG_ON(!EXT4_I(inode)->i_inline_off);
223 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
224
225 raw_inode = ext4_raw_inode(iloc);
226 buffer += pos;
227
228 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
229 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
230 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
231 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
232
233 len -= cp_len;
234 buffer += cp_len;
235 pos += cp_len;
236 }
237
238 if (!len)
239 return;
240
241 pos -= EXT4_MIN_INLINE_DATA_SIZE;
242 header = IHDR(inode, raw_inode);
243 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
244 EXT4_I(inode)->i_inline_off);
245
246 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
247 buffer, len);
248}
249
250static int ext4_create_inline_data(handle_t *handle,
251 struct inode *inode, unsigned len)
252{
253 int error;
254 void *value = NULL;
255 struct ext4_xattr_ibody_find is = {
256 .s = { .not_found = -ENODATA, },
257 };
258 struct ext4_xattr_info i = {
259 .name_index = EXT4_XATTR_INDEX_SYSTEM,
260 .name = EXT4_XATTR_SYSTEM_DATA,
261 };
262
263 error = ext4_get_inode_loc(inode, &is.iloc);
264 if (error)
265 return error;
266
liang xie5d601252014-05-12 22:06:43 -0400267 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500268 error = ext4_journal_get_write_access(handle, is.iloc.bh);
269 if (error)
270 goto out;
271
272 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500273 value = EXT4_ZERO_XATTR_VALUE;
Tao Ma67cf5b02012-12-10 14:04:46 -0500274 len -= EXT4_MIN_INLINE_DATA_SIZE;
275 } else {
276 value = "";
277 len = 0;
278 }
279
280 /* Insert the the xttr entry. */
281 i.value = value;
282 i.value_len = len;
283
284 error = ext4_xattr_ibody_find(inode, &i, &is);
285 if (error)
286 goto out;
287
288 BUG_ON(!is.s.not_found);
289
Tao Ma0d812f72012-12-10 14:06:02 -0500290 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500291 if (error) {
292 if (error == -ENOSPC)
293 ext4_clear_inode_state(inode,
294 EXT4_STATE_MAY_INLINE_DATA);
295 goto out;
296 }
297
298 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
299 0, EXT4_MIN_INLINE_DATA_SIZE);
300
301 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
302 (void *)ext4_raw_inode(&is.iloc));
303 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
304 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
305 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
Jan Karaa3caa242016-11-20 17:32:59 -0500306 /*
307 * Propagate changes to inode->i_flags as well - e.g. S_DAX may
308 * get cleared
309 */
310 ext4_set_inode_flags(inode);
Tao Ma67cf5b02012-12-10 14:04:46 -0500311 get_bh(is.iloc.bh);
312 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
313
314out:
315 brelse(is.iloc.bh);
316 return error;
317}
318
319static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
320 unsigned int len)
321{
322 int error;
323 void *value = NULL;
324 struct ext4_xattr_ibody_find is = {
325 .s = { .not_found = -ENODATA, },
326 };
327 struct ext4_xattr_info i = {
328 .name_index = EXT4_XATTR_INDEX_SYSTEM,
329 .name = EXT4_XATTR_SYSTEM_DATA,
330 };
331
332 /* If the old space is ok, write the data directly. */
333 if (len <= EXT4_I(inode)->i_inline_size)
334 return 0;
335
336 error = ext4_get_inode_loc(inode, &is.iloc);
337 if (error)
338 return error;
339
340 error = ext4_xattr_ibody_find(inode, &i, &is);
341 if (error)
342 goto out;
343
344 BUG_ON(is.s.not_found);
345
346 len -= EXT4_MIN_INLINE_DATA_SIZE;
347 value = kzalloc(len, GFP_NOFS);
Dan Carpenter578620f2016-12-10 09:56:01 -0500348 if (!value) {
349 error = -ENOMEM;
Tao Ma67cf5b02012-12-10 14:04:46 -0500350 goto out;
Dan Carpenter578620f2016-12-10 09:56:01 -0500351 }
Tao Ma67cf5b02012-12-10 14:04:46 -0500352
353 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
354 value, len);
355 if (error == -ENODATA)
356 goto out;
357
liang xie5d601252014-05-12 22:06:43 -0400358 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500359 error = ext4_journal_get_write_access(handle, is.iloc.bh);
360 if (error)
361 goto out;
362
363 /* Update the xttr entry. */
364 i.value = value;
365 i.value_len = len;
366
Tao Ma0d812f72012-12-10 14:06:02 -0500367 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500368 if (error)
369 goto out;
370
371 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
372 (void *)ext4_raw_inode(&is.iloc));
373 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
374 le32_to_cpu(is.s.here->e_value_size);
375 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
376 get_bh(is.iloc.bh);
377 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
378
379out:
380 kfree(value);
381 brelse(is.iloc.bh);
382 return error;
383}
384
Stephen Hemmingerc1978552014-05-12 10:50:23 -0400385static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
386 unsigned int len)
Tao Ma67cf5b02012-12-10 14:04:46 -0500387{
Theodore Ts'oc755e252017-01-11 21:50:46 -0500388 int ret, size, no_expand;
Tao Ma67cf5b02012-12-10 14:04:46 -0500389 struct ext4_inode_info *ei = EXT4_I(inode);
390
391 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
392 return -ENOSPC;
393
394 size = ext4_get_max_inline_size(inode);
395 if (size < len)
396 return -ENOSPC;
397
Theodore Ts'oc755e252017-01-11 21:50:46 -0500398 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -0500399
400 if (ei->i_inline_off)
401 ret = ext4_update_inline_data(handle, inode, len);
402 else
403 ret = ext4_create_inline_data(handle, inode, len);
404
Theodore Ts'oc755e252017-01-11 21:50:46 -0500405 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -0500406 return ret;
407}
408
409static int ext4_destroy_inline_data_nolock(handle_t *handle,
410 struct inode *inode)
411{
412 struct ext4_inode_info *ei = EXT4_I(inode);
413 struct ext4_xattr_ibody_find is = {
414 .s = { .not_found = 0, },
415 };
416 struct ext4_xattr_info i = {
417 .name_index = EXT4_XATTR_INDEX_SYSTEM,
418 .name = EXT4_XATTR_SYSTEM_DATA,
419 .value = NULL,
420 .value_len = 0,
421 };
422 int error;
423
424 if (!ei->i_inline_off)
425 return 0;
426
427 error = ext4_get_inode_loc(inode, &is.iloc);
428 if (error)
429 return error;
430
431 error = ext4_xattr_ibody_find(inode, &i, &is);
432 if (error)
433 goto out;
434
liang xie5d601252014-05-12 22:06:43 -0400435 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500436 error = ext4_journal_get_write_access(handle, is.iloc.bh);
437 if (error)
438 goto out;
439
Tao Ma0d812f72012-12-10 14:06:02 -0500440 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500441 if (error)
442 goto out;
443
444 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
445 0, EXT4_MIN_INLINE_DATA_SIZE);
446
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400447 if (ext4_has_feature_extents(inode->i_sb)) {
Tao Ma67cf5b02012-12-10 14:04:46 -0500448 if (S_ISDIR(inode->i_mode) ||
449 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
450 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
451 ext4_ext_tree_init(handle, inode);
452 }
453 }
454 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
Jan Karaa3caa242016-11-20 17:32:59 -0500455 /*
456 * Propagate changes to inode->i_flags as well - e.g. S_DAX may
457 * get set.
458 */
459 ext4_set_inode_flags(inode);
Tao Ma67cf5b02012-12-10 14:04:46 -0500460
461 get_bh(is.iloc.bh);
462 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
463
464 EXT4_I(inode)->i_inline_off = 0;
465 EXT4_I(inode)->i_inline_size = 0;
466 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
467out:
468 brelse(is.iloc.bh);
469 if (error == -ENODATA)
470 error = 0;
471 return error;
472}
473
Tao Ma46c7f252012-12-10 14:04:52 -0500474static int ext4_read_inline_page(struct inode *inode, struct page *page)
475{
476 void *kaddr;
477 int ret = 0;
478 size_t len;
479 struct ext4_iloc iloc;
480
481 BUG_ON(!PageLocked(page));
482 BUG_ON(!ext4_has_inline_data(inode));
483 BUG_ON(page->index);
484
485 if (!EXT4_I(inode)->i_inline_off) {
486 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
487 inode->i_ino);
488 goto out;
489 }
490
491 ret = ext4_get_inode_loc(inode, &iloc);
492 if (ret)
493 goto out;
494
495 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
496 kaddr = kmap_atomic(page);
497 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
498 flush_dcache_page(page);
499 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300500 zero_user_segment(page, len, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500501 SetPageUptodate(page);
502 brelse(iloc.bh);
503
504out:
505 return ret;
506}
507
508int ext4_readpage_inline(struct inode *inode, struct page *page)
509{
510 int ret = 0;
511
512 down_read(&EXT4_I(inode)->xattr_sem);
513 if (!ext4_has_inline_data(inode)) {
514 up_read(&EXT4_I(inode)->xattr_sem);
515 return -EAGAIN;
516 }
517
518 /*
519 * Current inline data can only exist in the 1st page,
520 * So for all the other pages, just set them uptodate.
521 */
522 if (!page->index)
523 ret = ext4_read_inline_page(inode, page);
524 else if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300525 zero_user_segment(page, 0, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500526 SetPageUptodate(page);
527 }
528
529 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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oc755e252017-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'oeb5efbc2017-02-04 23:04:00 -0500950 int ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500951
Theodore Ts'oeb5efbc2017-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;
Eric Biggers1bc0af62017-04-29 23:27:26 -04001038 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 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001053 dir->i_mtime = dir->i_ctime = current_time(dir);
Tao Ma3c47d542012-12-10 14:05:59 -05001054 ext4_update_dx_flag(dir);
1055 dir->i_version++;
Tao Ma3c47d542012-12-10 14:05:59 -05001056 return 1;
1057}
1058
1059static void *ext4_get_inline_xattr_pos(struct inode *inode,
1060 struct ext4_iloc *iloc)
1061{
1062 struct ext4_xattr_entry *entry;
1063 struct ext4_xattr_ibody_header *header;
1064
1065 BUG_ON(!EXT4_I(inode)->i_inline_off);
1066
1067 header = IHDR(inode, ext4_raw_inode(iloc));
1068 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1069 EXT4_I(inode)->i_inline_off);
1070
1071 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1072}
1073
1074/* Set the final de to cover the whole block. */
1075static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1076{
1077 struct ext4_dir_entry_2 *de, *prev_de;
1078 void *limit;
1079 int de_len;
1080
1081 de = (struct ext4_dir_entry_2 *)de_buf;
1082 if (old_size) {
1083 limit = de_buf + old_size;
1084 do {
1085 prev_de = de;
1086 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1087 de_buf += de_len;
1088 de = (struct ext4_dir_entry_2 *)de_buf;
1089 } while (de_buf < limit);
1090
1091 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1092 old_size, new_size);
1093 } else {
1094 /* this is just created, so create an empty entry. */
1095 de->inode = 0;
1096 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1097 }
1098}
1099
1100static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1101 struct ext4_iloc *iloc)
1102{
1103 int ret;
1104 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1105 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1106
1107 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1108 return -ENOSPC;
1109
1110 ret = ext4_update_inline_data(handle, dir,
1111 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1112 if (ret)
1113 return ret;
1114
1115 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1116 EXT4_I(dir)->i_inline_size -
1117 EXT4_MIN_INLINE_DATA_SIZE);
1118 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1119 return 0;
1120}
1121
1122static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1123 struct ext4_iloc *iloc,
1124 void *buf, int inline_size)
1125{
1126 ext4_create_inline_data(handle, inode, inline_size);
1127 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1128 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1129}
1130
1131static int ext4_finish_convert_inline_dir(handle_t *handle,
1132 struct inode *inode,
1133 struct buffer_head *dir_block,
1134 void *buf,
1135 int inline_size)
1136{
1137 int err, csum_size = 0, header_size = 0;
1138 struct ext4_dir_entry_2 *de;
1139 struct ext4_dir_entry_tail *t;
1140 void *target = dir_block->b_data;
1141
1142 /*
1143 * First create "." and ".." and then copy the dir information
1144 * back to the block.
1145 */
1146 de = (struct ext4_dir_entry_2 *)target;
1147 de = ext4_init_dot_dotdot(inode, de,
1148 inode->i_sb->s_blocksize, csum_size,
1149 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1150 header_size = (void *)de - target;
1151
1152 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1153 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1154
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001155 if (ext4_has_metadata_csum(inode->i_sb))
Tao Ma3c47d542012-12-10 14:05:59 -05001156 csum_size = sizeof(struct ext4_dir_entry_tail);
1157
1158 inode->i_size = inode->i_sb->s_blocksize;
1159 i_size_write(inode, inode->i_sb->s_blocksize);
1160 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1161 ext4_update_final_de(dir_block->b_data,
1162 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1163 inode->i_sb->s_blocksize - csum_size);
1164
1165 if (csum_size) {
1166 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1167 inode->i_sb->s_blocksize);
1168 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1169 }
1170 set_buffer_uptodate(dir_block);
1171 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1172 if (err)
Eric Biggersb9cf6252017-03-15 14:52:02 -04001173 return err;
Tao Ma3c47d542012-12-10 14:05:59 -05001174 set_buffer_verified(dir_block);
Eric Biggersb9cf6252017-03-15 14:52:02 -04001175 return ext4_mark_inode_dirty(handle, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05001176}
1177
1178static int ext4_convert_inline_data_nolock(handle_t *handle,
1179 struct inode *inode,
1180 struct ext4_iloc *iloc)
1181{
1182 int error;
1183 void *buf = NULL;
1184 struct buffer_head *data_bh = NULL;
1185 struct ext4_map_blocks map;
1186 int inline_size;
1187
1188 inline_size = ext4_get_inline_size(inode);
1189 buf = kmalloc(inline_size, GFP_NOFS);
1190 if (!buf) {
1191 error = -ENOMEM;
1192 goto out;
1193 }
1194
1195 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1196 if (error < 0)
1197 goto out;
1198
Darrick J. Wong40b163f2014-07-28 13:06:26 -04001199 /*
1200 * Make sure the inline directory entries pass checks before we try to
1201 * convert them, so that we avoid touching stuff that needs fsck.
1202 */
1203 if (S_ISDIR(inode->i_mode)) {
1204 error = ext4_check_all_de(inode, iloc->bh,
1205 buf + EXT4_INLINE_DOTDOT_SIZE,
1206 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1207 if (error)
1208 goto out;
1209 }
1210
Tao Ma3c47d542012-12-10 14:05:59 -05001211 error = ext4_destroy_inline_data_nolock(handle, inode);
1212 if (error)
1213 goto out;
1214
1215 map.m_lblk = 0;
1216 map.m_len = 1;
1217 map.m_flags = 0;
1218 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1219 if (error < 0)
1220 goto out_restore;
1221 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1222 error = -EIO;
1223 goto out_restore;
1224 }
1225
1226 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1227 if (!data_bh) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001228 error = -ENOMEM;
Tao Ma3c47d542012-12-10 14:05:59 -05001229 goto out_restore;
1230 }
1231
1232 lock_buffer(data_bh);
1233 error = ext4_journal_get_create_access(handle, data_bh);
1234 if (error) {
1235 unlock_buffer(data_bh);
1236 error = -EIO;
1237 goto out_restore;
1238 }
1239 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1240
1241 if (!S_ISDIR(inode->i_mode)) {
1242 memcpy(data_bh->b_data, buf, inline_size);
1243 set_buffer_uptodate(data_bh);
1244 error = ext4_handle_dirty_metadata(handle,
1245 inode, data_bh);
1246 } else {
1247 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1248 buf, inline_size);
1249 }
1250
1251 unlock_buffer(data_bh);
1252out_restore:
1253 if (error)
1254 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1255
1256out:
1257 brelse(data_bh);
1258 kfree(buf);
1259 return error;
1260}
1261
1262/*
1263 * Try to add the new entry to the inline data.
1264 * If succeeds, return 0. If not, extended the inline dir and copied data to
1265 * the new created block.
1266 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001267int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001268 struct inode *dir, struct inode *inode)
Tao Ma3c47d542012-12-10 14:05:59 -05001269{
Theodore Ts'oc755e252017-01-11 21:50:46 -05001270 int ret, inline_size, no_expand;
Tao Ma3c47d542012-12-10 14:05:59 -05001271 void *inline_start;
1272 struct ext4_iloc iloc;
Tao Ma3c47d542012-12-10 14:05:59 -05001273
1274 ret = ext4_get_inode_loc(dir, &iloc);
1275 if (ret)
1276 return ret;
1277
Theodore Ts'oc755e252017-01-11 21:50:46 -05001278 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma3c47d542012-12-10 14:05:59 -05001279 if (!ext4_has_inline_data(dir))
1280 goto out;
1281
1282 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1283 EXT4_INLINE_DOTDOT_SIZE;
1284 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1285
Theodore Ts'o56a04912016-01-08 16:00:31 -05001286 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
Tao Ma3c47d542012-12-10 14:05:59 -05001287 inline_start, inline_size);
1288 if (ret != -ENOSPC)
1289 goto out;
1290
1291 /* check whether it can be inserted to inline xattr space. */
1292 inline_size = EXT4_I(dir)->i_inline_size -
1293 EXT4_MIN_INLINE_DATA_SIZE;
1294 if (!inline_size) {
1295 /* Try to use the xattr space.*/
1296 ret = ext4_update_inline_dir(handle, dir, &iloc);
1297 if (ret && ret != -ENOSPC)
1298 goto out;
1299
1300 inline_size = EXT4_I(dir)->i_inline_size -
1301 EXT4_MIN_INLINE_DATA_SIZE;
1302 }
1303
1304 if (inline_size) {
1305 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1306
Theodore Ts'o56a04912016-01-08 16:00:31 -05001307 ret = ext4_add_dirent_to_inline(handle, fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001308 inode, &iloc, inline_start,
1309 inline_size);
Tao Ma3c47d542012-12-10 14:05:59 -05001310
1311 if (ret != -ENOSPC)
1312 goto out;
1313 }
1314
1315 /*
1316 * The inline space is filled up, so create a new block for it.
1317 * As the extent tree will be created, we have to save the inline
1318 * dir first.
1319 */
1320 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1321
1322out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001323 ext4_write_unlock_xattr(dir, &no_expand);
Theodore Ts'ob907f2d2017-01-11 22:14:49 -05001324 ext4_mark_inode_dirty(handle, dir);
Tao Ma3c47d542012-12-10 14:05:59 -05001325 brelse(iloc.bh);
1326 return ret;
1327}
1328
Tao Ma8af0f082013-04-19 17:53:09 -04001329/*
1330 * This function fills a red-black tree with information from an
1331 * inlined dir. It returns the number directory entries loaded
1332 * into the tree. If there is an error it is returned in err.
1333 */
1334int htree_inlinedir_to_tree(struct file *dir_file,
1335 struct inode *dir, ext4_lblk_t block,
1336 struct dx_hash_info *hinfo,
1337 __u32 start_hash, __u32 start_minor_hash,
1338 int *has_inline_data)
1339{
1340 int err = 0, count = 0;
1341 unsigned int parent_ino;
1342 int pos;
1343 struct ext4_dir_entry_2 *de;
1344 struct inode *inode = file_inode(dir_file);
1345 int ret, inline_size = 0;
1346 struct ext4_iloc iloc;
1347 void *dir_buf = NULL;
1348 struct ext4_dir_entry_2 fake;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001349 struct fscrypt_str tmp_str;
Tao Ma8af0f082013-04-19 17:53:09 -04001350
1351 ret = ext4_get_inode_loc(inode, &iloc);
1352 if (ret)
1353 return ret;
1354
1355 down_read(&EXT4_I(inode)->xattr_sem);
1356 if (!ext4_has_inline_data(inode)) {
1357 up_read(&EXT4_I(inode)->xattr_sem);
1358 *has_inline_data = 0;
1359 goto out;
1360 }
1361
1362 inline_size = ext4_get_inline_size(inode);
1363 dir_buf = kmalloc(inline_size, GFP_NOFS);
1364 if (!dir_buf) {
1365 ret = -ENOMEM;
1366 up_read(&EXT4_I(inode)->xattr_sem);
1367 goto out;
1368 }
1369
1370 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1371 up_read(&EXT4_I(inode)->xattr_sem);
1372 if (ret < 0)
1373 goto out;
1374
1375 pos = 0;
1376 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1377 while (pos < inline_size) {
1378 /*
1379 * As inlined dir doesn't store any information about '.' and
1380 * only the inode number of '..' is stored, we have to handle
1381 * them differently.
1382 */
1383 if (pos == 0) {
1384 fake.inode = cpu_to_le32(inode->i_ino);
1385 fake.name_len = 1;
1386 strcpy(fake.name, ".");
1387 fake.rec_len = ext4_rec_len_to_disk(
1388 EXT4_DIR_REC_LEN(fake.name_len),
1389 inline_size);
1390 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1391 de = &fake;
1392 pos = EXT4_INLINE_DOTDOT_OFFSET;
1393 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1394 fake.inode = cpu_to_le32(parent_ino);
1395 fake.name_len = 2;
1396 strcpy(fake.name, "..");
1397 fake.rec_len = ext4_rec_len_to_disk(
1398 EXT4_DIR_REC_LEN(fake.name_len),
1399 inline_size);
1400 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1401 de = &fake;
1402 pos = EXT4_INLINE_DOTDOT_SIZE;
1403 } else {
1404 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1405 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1406 if (ext4_check_dir_entry(inode, dir_file, de,
1407 iloc.bh, dir_buf,
1408 inline_size, pos)) {
1409 ret = count;
1410 goto out;
1411 }
1412 }
1413
1414 ext4fs_dirhash(de->name, de->name_len, hinfo);
1415 if ((hinfo->hash < start_hash) ||
1416 ((hinfo->hash == start_hash) &&
1417 (hinfo->minor_hash < start_minor_hash)))
1418 continue;
1419 if (de->inode == 0)
1420 continue;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001421 tmp_str.name = de->name;
1422 tmp_str.len = de->name_len;
1423 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1424 hinfo->minor_hash, de, &tmp_str);
Tao Ma8af0f082013-04-19 17:53:09 -04001425 if (err) {
1426 count = err;
1427 goto out;
1428 }
1429 count++;
1430 }
1431 ret = count;
1432out:
1433 kfree(dir_buf);
1434 brelse(iloc.bh);
1435 return ret;
1436}
1437
Tao Mac4d8b022013-04-19 17:55:33 -04001438/*
1439 * So this function is called when the volume is mkfsed with
1440 * dir_index disabled. In order to keep f_pos persistent
1441 * after we convert from an inlined dir to a blocked based,
1442 * we just pretend that we are a normal dir and return the
1443 * offset as if '.' and '..' really take place.
1444 *
1445 */
Al Viro725bebb2013-05-17 16:08:53 -04001446int ext4_read_inline_dir(struct file *file,
1447 struct dir_context *ctx,
Tao Ma65d165d2012-12-10 14:05:59 -05001448 int *has_inline_data)
1449{
Tao Ma65d165d2012-12-10 14:05:59 -05001450 unsigned int offset, parent_ino;
Al Viro725bebb2013-05-17 16:08:53 -04001451 int i;
Tao Ma65d165d2012-12-10 14:05:59 -05001452 struct ext4_dir_entry_2 *de;
1453 struct super_block *sb;
Al Viro725bebb2013-05-17 16:08:53 -04001454 struct inode *inode = file_inode(file);
Tao Ma65d165d2012-12-10 14:05:59 -05001455 int ret, inline_size = 0;
1456 struct ext4_iloc iloc;
1457 void *dir_buf = NULL;
Tao Mac4d8b022013-04-19 17:55:33 -04001458 int dotdot_offset, dotdot_size, extra_offset, extra_size;
Tao Ma65d165d2012-12-10 14:05:59 -05001459
1460 ret = ext4_get_inode_loc(inode, &iloc);
1461 if (ret)
1462 return ret;
1463
1464 down_read(&EXT4_I(inode)->xattr_sem);
1465 if (!ext4_has_inline_data(inode)) {
1466 up_read(&EXT4_I(inode)->xattr_sem);
1467 *has_inline_data = 0;
1468 goto out;
1469 }
1470
1471 inline_size = ext4_get_inline_size(inode);
1472 dir_buf = kmalloc(inline_size, GFP_NOFS);
1473 if (!dir_buf) {
1474 ret = -ENOMEM;
1475 up_read(&EXT4_I(inode)->xattr_sem);
1476 goto out;
1477 }
1478
1479 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1480 up_read(&EXT4_I(inode)->xattr_sem);
1481 if (ret < 0)
1482 goto out;
1483
BoxiLiu48ffdab2013-10-30 08:07:20 -04001484 ret = 0;
Tao Ma65d165d2012-12-10 14:05:59 -05001485 sb = inode->i_sb;
Tao Ma65d165d2012-12-10 14:05:59 -05001486 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
Al Viro725bebb2013-05-17 16:08:53 -04001487 offset = ctx->pos;
Tao Ma65d165d2012-12-10 14:05:59 -05001488
Tao Mac4d8b022013-04-19 17:55:33 -04001489 /*
1490 * dotdot_offset and dotdot_size is the real offset and
1491 * size for ".." and "." if the dir is block based while
1492 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1493 * So we will use extra_offset and extra_size to indicate them
1494 * during the inline dir iteration.
1495 */
1496 dotdot_offset = EXT4_DIR_REC_LEN(1);
1497 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1498 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1499 extra_size = extra_offset + inline_size;
1500
Al Viro725bebb2013-05-17 16:08:53 -04001501 /*
1502 * If the version has changed since the last call to
1503 * readdir(2), then we might be pointing to an invalid
1504 * dirent right now. Scan from the start of the inline
1505 * dir to make sure.
1506 */
1507 if (file->f_version != inode->i_version) {
1508 for (i = 0; i < extra_size && i < offset;) {
1509 /*
1510 * "." is with offset 0 and
1511 * ".." is dotdot_offset.
1512 */
1513 if (!i) {
1514 i = dotdot_offset;
1515 continue;
1516 } else if (i == dotdot_offset) {
1517 i = dotdot_size;
Tao Mac4d8b022013-04-19 17:55:33 -04001518 continue;
1519 }
Al Viro725bebb2013-05-17 16:08:53 -04001520 /* for other entry, the real offset in
1521 * the buf has to be tuned accordingly.
1522 */
Tao Mac4d8b022013-04-19 17:55:33 -04001523 de = (struct ext4_dir_entry_2 *)
Al Viro725bebb2013-05-17 16:08:53 -04001524 (dir_buf + i - extra_offset);
1525 /* It's too expensive to do a full
1526 * dirent test each time round this
1527 * loop, but we do have to test at
1528 * least that it is non-zero. A
1529 * failure will be detected in the
1530 * dirent test below. */
1531 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1532 < EXT4_DIR_REC_LEN(1))
1533 break;
1534 i += ext4_rec_len_from_disk(de->rec_len,
1535 extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001536 }
Al Viro725bebb2013-05-17 16:08:53 -04001537 offset = i;
1538 ctx->pos = offset;
1539 file->f_version = inode->i_version;
1540 }
1541
1542 while (ctx->pos < extra_size) {
1543 if (ctx->pos == 0) {
1544 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1545 goto out;
1546 ctx->pos = dotdot_offset;
1547 continue;
1548 }
1549
1550 if (ctx->pos == dotdot_offset) {
1551 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1552 goto out;
1553 ctx->pos = dotdot_size;
1554 continue;
1555 }
1556
1557 de = (struct ext4_dir_entry_2 *)
1558 (dir_buf + ctx->pos - extra_offset);
1559 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1560 extra_size, ctx->pos))
1561 goto out;
1562 if (le32_to_cpu(de->inode)) {
1563 if (!dir_emit(ctx, de->name, de->name_len,
1564 le32_to_cpu(de->inode),
1565 get_dtype(sb, de->file_type)))
1566 goto out;
1567 }
1568 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001569 }
1570out:
1571 kfree(dir_buf);
1572 brelse(iloc.bh);
1573 return ret;
1574}
1575
Tao Ma32f7f222012-12-10 14:06:01 -05001576struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1577 struct ext4_dir_entry_2 **parent_de,
1578 int *retval)
1579{
1580 struct ext4_iloc iloc;
1581
1582 *retval = ext4_get_inode_loc(inode, &iloc);
1583 if (*retval)
1584 return NULL;
1585
1586 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1587
1588 return iloc.bh;
1589}
1590
Tao Ma3c47d542012-12-10 14:05:59 -05001591/*
1592 * Try to create the inline data for the new dir.
1593 * If it succeeds, return 0, otherwise return the error.
1594 * In case of ENOSPC, the caller should create the normal disk layout dir.
1595 */
1596int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1597 struct inode *inode)
1598{
1599 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1600 struct ext4_iloc iloc;
1601 struct ext4_dir_entry_2 *de;
1602
1603 ret = ext4_get_inode_loc(inode, &iloc);
1604 if (ret)
1605 return ret;
1606
1607 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1608 if (ret)
1609 goto out;
1610
1611 /*
1612 * For inline dir, we only save the inode information for the ".."
1613 * and create a fake dentry to cover the left space.
1614 */
1615 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1616 de->inode = cpu_to_le32(parent->i_ino);
1617 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1618 de->inode = 0;
1619 de->rec_len = ext4_rec_len_to_disk(
1620 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1621 inline_size);
1622 set_nlink(inode, 2);
1623 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1624out:
1625 brelse(iloc.bh);
1626 return ret;
1627}
1628
Tao Mae8e948e2012-12-10 14:06:00 -05001629struct buffer_head *ext4_find_inline_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001630 struct ext4_filename *fname,
Tao Mae8e948e2012-12-10 14:06:00 -05001631 struct ext4_dir_entry_2 **res_dir,
1632 int *has_inline_data)
1633{
1634 int ret;
1635 struct ext4_iloc iloc;
1636 void *inline_start;
1637 int inline_size;
1638
1639 if (ext4_get_inode_loc(dir, &iloc))
1640 return NULL;
1641
1642 down_read(&EXT4_I(dir)->xattr_sem);
1643 if (!ext4_has_inline_data(dir)) {
1644 *has_inline_data = 0;
1645 goto out;
1646 }
1647
1648 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1649 EXT4_INLINE_DOTDOT_SIZE;
1650 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001651 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
Eric Biggersd6b97552017-05-24 18:10:49 -04001652 dir, fname, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001653 if (ret == 1)
1654 goto out_find;
1655 if (ret < 0)
1656 goto out;
1657
1658 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1659 goto out;
1660
1661 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1662 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1663
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001664 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
Eric Biggersd6b97552017-05-24 18:10:49 -04001665 dir, fname, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001666 if (ret == 1)
1667 goto out_find;
1668
1669out:
1670 brelse(iloc.bh);
1671 iloc.bh = NULL;
1672out_find:
1673 up_read(&EXT4_I(dir)->xattr_sem);
1674 return iloc.bh;
1675}
1676
Tao Ma9f40fe52012-12-10 14:06:00 -05001677int ext4_delete_inline_entry(handle_t *handle,
1678 struct inode *dir,
1679 struct ext4_dir_entry_2 *de_del,
1680 struct buffer_head *bh,
1681 int *has_inline_data)
1682{
Theodore Ts'oc755e252017-01-11 21:50:46 -05001683 int err, inline_size, no_expand;
Tao Ma9f40fe52012-12-10 14:06:00 -05001684 struct ext4_iloc iloc;
1685 void *inline_start;
1686
1687 err = ext4_get_inode_loc(dir, &iloc);
1688 if (err)
1689 return err;
1690
Theodore Ts'oc755e252017-01-11 21:50:46 -05001691 ext4_write_lock_xattr(dir, &no_expand);
Tao Ma9f40fe52012-12-10 14:06:00 -05001692 if (!ext4_has_inline_data(dir)) {
1693 *has_inline_data = 0;
1694 goto out;
1695 }
1696
1697 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1698 EXT4_MIN_INLINE_DATA_SIZE) {
1699 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1700 EXT4_INLINE_DOTDOT_SIZE;
1701 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1702 EXT4_INLINE_DOTDOT_SIZE;
1703 } else {
1704 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1705 inline_size = ext4_get_inline_size(dir) -
1706 EXT4_MIN_INLINE_DATA_SIZE;
1707 }
1708
liang xie5d601252014-05-12 22:06:43 -04001709 BUFFER_TRACE(bh, "get_write_access");
Tao Ma9f40fe52012-12-10 14:06:00 -05001710 err = ext4_journal_get_write_access(handle, bh);
1711 if (err)
1712 goto out;
1713
1714 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1715 inline_start, inline_size, 0);
1716 if (err)
1717 goto out;
1718
Tao Ma9f40fe52012-12-10 14:06:00 -05001719 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1720out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001721 ext4_write_unlock_xattr(dir, &no_expand);
Theodore Ts'ob907f2d2017-01-11 22:14:49 -05001722 if (likely(err == 0))
1723 err = ext4_mark_inode_dirty(handle, dir);
Tao Ma9f40fe52012-12-10 14:06:00 -05001724 brelse(iloc.bh);
1725 if (err != -ENOENT)
1726 ext4_std_error(dir->i_sb, err);
1727 return err;
1728}
1729
Tao Ma61f86632012-12-10 14:06:01 -05001730/*
1731 * Get the inline dentry at offset.
1732 */
1733static inline struct ext4_dir_entry_2 *
1734ext4_get_inline_entry(struct inode *inode,
1735 struct ext4_iloc *iloc,
1736 unsigned int offset,
1737 void **inline_start,
1738 int *inline_size)
1739{
1740 void *inline_pos;
1741
1742 BUG_ON(offset > ext4_get_inline_size(inode));
1743
1744 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1745 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1746 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1747 } else {
1748 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1749 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1750 *inline_size = ext4_get_inline_size(inode) -
1751 EXT4_MIN_INLINE_DATA_SIZE;
1752 }
1753
1754 if (inline_start)
1755 *inline_start = inline_pos;
1756 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1757}
1758
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001759bool empty_inline_dir(struct inode *dir, int *has_inline_data)
Tao Ma61f86632012-12-10 14:06:01 -05001760{
1761 int err, inline_size;
1762 struct ext4_iloc iloc;
1763 void *inline_pos;
1764 unsigned int offset;
1765 struct ext4_dir_entry_2 *de;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001766 bool ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001767
1768 err = ext4_get_inode_loc(dir, &iloc);
1769 if (err) {
1770 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1771 err, dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001772 return true;
Tao Ma61f86632012-12-10 14:06:01 -05001773 }
1774
1775 down_read(&EXT4_I(dir)->xattr_sem);
1776 if (!ext4_has_inline_data(dir)) {
1777 *has_inline_data = 0;
1778 goto out;
1779 }
1780
1781 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1782 if (!le32_to_cpu(de->inode)) {
1783 ext4_warning(dir->i_sb,
1784 "bad inline directory (dir #%lu) - no `..'",
1785 dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001786 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001787 goto out;
1788 }
1789
1790 offset = EXT4_INLINE_DOTDOT_SIZE;
1791 while (offset < dir->i_size) {
1792 de = ext4_get_inline_entry(dir, &iloc, offset,
1793 &inline_pos, &inline_size);
1794 if (ext4_check_dir_entry(dir, NULL, de,
1795 iloc.bh, inline_pos,
1796 inline_size, offset)) {
1797 ext4_warning(dir->i_sb,
1798 "bad inline directory (dir #%lu) - "
1799 "inode %u, rec_len %u, name_len %d"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001800 "inline size %d",
Tao Ma61f86632012-12-10 14:06:01 -05001801 dir->i_ino, le32_to_cpu(de->inode),
1802 le16_to_cpu(de->rec_len), de->name_len,
1803 inline_size);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001804 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001805 goto out;
1806 }
1807 if (le32_to_cpu(de->inode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001808 ret = false;
Tao Ma61f86632012-12-10 14:06:01 -05001809 goto out;
1810 }
1811 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1812 }
1813
1814out:
1815 up_read(&EXT4_I(dir)->xattr_sem);
1816 brelse(iloc.bh);
1817 return ret;
1818}
1819
Tao Ma67cf5b02012-12-10 14:04:46 -05001820int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1821{
Theodore Ts'oc755e252017-01-11 21:50:46 -05001822 int ret, no_expand;
Tao Ma67cf5b02012-12-10 14:04:46 -05001823
Theodore Ts'oc755e252017-01-11 21:50:46 -05001824 ext4_write_lock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001825 ret = ext4_destroy_inline_data_nolock(handle, inode);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001826 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma67cf5b02012-12-10 14:04:46 -05001827
1828 return ret;
1829}
Tao Ma94191982012-12-10 14:06:02 -05001830
Andreas Gruenbacher7046ae32017-10-01 17:57:54 -04001831int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1832{
1833 __u64 addr;
1834 int error = -EAGAIN;
1835 struct ext4_iloc iloc;
1836
1837 down_read(&EXT4_I(inode)->xattr_sem);
1838 if (!ext4_has_inline_data(inode))
1839 goto out;
1840
1841 error = ext4_get_inode_loc(inode, &iloc);
1842 if (error)
1843 goto out;
1844
1845 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1846 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1847 addr += offsetof(struct ext4_inode, i_block);
1848
1849 brelse(iloc.bh);
1850
1851 iomap->addr = addr;
1852 iomap->offset = 0;
1853 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1854 i_size_read(inode));
1855 iomap->type = 0;
1856 iomap->flags = IOMAP_F_DATA_INLINE;
1857
1858out:
1859 up_read(&EXT4_I(inode)->xattr_sem);
1860 return error;
1861}
1862
Tao Ma94191982012-12-10 14:06:02 -05001863int ext4_inline_data_fiemap(struct inode *inode,
1864 struct fiemap_extent_info *fieinfo,
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001865 int *has_inline, __u64 start, __u64 len)
Tao Ma94191982012-12-10 14:06:02 -05001866{
1867 __u64 physical = 0;
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001868 __u64 inline_len;
1869 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1870 FIEMAP_EXTENT_LAST;
Tao Ma94191982012-12-10 14:06:02 -05001871 int error = 0;
1872 struct ext4_iloc iloc;
1873
1874 down_read(&EXT4_I(inode)->xattr_sem);
1875 if (!ext4_has_inline_data(inode)) {
1876 *has_inline = 0;
1877 goto out;
1878 }
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001879 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1880 i_size_read(inode));
1881 if (start >= inline_len)
1882 goto out;
1883 if (start + len < inline_len)
1884 inline_len = start + len;
1885 inline_len -= start;
Tao Ma94191982012-12-10 14:06:02 -05001886
1887 error = ext4_get_inode_loc(inode, &iloc);
1888 if (error)
1889 goto out;
1890
Jan Karaeaf37932013-05-31 19:33:42 -04001891 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
Tao Ma94191982012-12-10 14:06:02 -05001892 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1893 physical += offsetof(struct ext4_inode, i_block);
Tao Ma94191982012-12-10 14:06:02 -05001894
1895 if (physical)
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001896 error = fiemap_fill_next_extent(fieinfo, start, physical,
1897 inline_len, flags);
Tao Ma94191982012-12-10 14:06:02 -05001898 brelse(iloc.bh);
1899out:
1900 up_read(&EXT4_I(inode)->xattr_sem);
1901 return (error < 0 ? error : 0);
1902}
Tao Ma0d812f72012-12-10 14:06:02 -05001903
1904/*
1905 * Called during xattr set, and if we can sparse space 'needed',
1906 * just create the extent tree evict the data to the outer block.
1907 *
1908 * We use jbd2 instead of page cache to move data to the 1st block
1909 * so that the whole transaction can be committed as a whole and
1910 * the data isn't lost because of the delayed page cache write.
1911 */
1912int ext4_try_to_evict_inline_data(handle_t *handle,
1913 struct inode *inode,
1914 int needed)
1915{
1916 int error;
1917 struct ext4_xattr_entry *entry;
Tao Ma0d812f72012-12-10 14:06:02 -05001918 struct ext4_inode *raw_inode;
1919 struct ext4_iloc iloc;
1920
1921 error = ext4_get_inode_loc(inode, &iloc);
1922 if (error)
1923 return error;
1924
1925 raw_inode = ext4_raw_inode(&iloc);
Tao Ma0d812f72012-12-10 14:06:02 -05001926 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
1927 EXT4_I(inode)->i_inline_off);
1928 if (EXT4_XATTR_LEN(entry->e_name_len) +
1929 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) {
1930 error = -ENOSPC;
1931 goto out;
1932 }
1933
1934 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
1935out:
1936 brelse(iloc.bh);
1937 return error;
1938}
Tao Maaef1c852012-12-10 14:06:02 -05001939
Theodore Ts'o01daf942017-01-22 19:35:49 -05001940int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
Tao Maaef1c852012-12-10 14:06:02 -05001941{
1942 handle_t *handle;
Theodore Ts'o01daf942017-01-22 19:35:49 -05001943 int inline_size, value_len, needed_blocks, no_expand, err = 0;
Tao Maaef1c852012-12-10 14:06:02 -05001944 size_t i_size;
1945 void *value = NULL;
1946 struct ext4_xattr_ibody_find is = {
1947 .s = { .not_found = -ENODATA, },
1948 };
1949 struct ext4_xattr_info i = {
1950 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1951 .name = EXT4_XATTR_SYSTEM_DATA,
1952 };
1953
1954
1955 needed_blocks = ext4_writepage_trans_blocks(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -05001956 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
Tao Maaef1c852012-12-10 14:06:02 -05001957 if (IS_ERR(handle))
Theodore Ts'o01daf942017-01-22 19:35:49 -05001958 return PTR_ERR(handle);
Tao Maaef1c852012-12-10 14:06:02 -05001959
Theodore Ts'oc755e252017-01-11 21:50:46 -05001960 ext4_write_lock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05001961 if (!ext4_has_inline_data(inode)) {
1962 *has_inline = 0;
1963 ext4_journal_stop(handle);
Theodore Ts'o01daf942017-01-22 19:35:49 -05001964 return 0;
Tao Maaef1c852012-12-10 14:06:02 -05001965 }
1966
Theodore Ts'o01daf942017-01-22 19:35:49 -05001967 if ((err = ext4_orphan_add(handle, inode)) != 0)
Tao Maaef1c852012-12-10 14:06:02 -05001968 goto out;
1969
Theodore Ts'o01daf942017-01-22 19:35:49 -05001970 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
Tao Maaef1c852012-12-10 14:06:02 -05001971 goto out;
1972
1973 down_write(&EXT4_I(inode)->i_data_sem);
1974 i_size = inode->i_size;
1975 inline_size = ext4_get_inline_size(inode);
1976 EXT4_I(inode)->i_disksize = i_size;
1977
1978 if (i_size < inline_size) {
1979 /* Clear the content in the xattr space. */
1980 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
Theodore Ts'o01daf942017-01-22 19:35:49 -05001981 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
Tao Maaef1c852012-12-10 14:06:02 -05001982 goto out_error;
1983
1984 BUG_ON(is.s.not_found);
1985
1986 value_len = le32_to_cpu(is.s.here->e_value_size);
1987 value = kmalloc(value_len, GFP_NOFS);
Theodore Ts'o01daf942017-01-22 19:35:49 -05001988 if (!value) {
1989 err = -ENOMEM;
Tao Maaef1c852012-12-10 14:06:02 -05001990 goto out_error;
Theodore Ts'o01daf942017-01-22 19:35:49 -05001991 }
Tao Maaef1c852012-12-10 14:06:02 -05001992
Theodore Ts'o01daf942017-01-22 19:35:49 -05001993 err = ext4_xattr_ibody_get(inode, i.name_index,
1994 i.name, value, value_len);
1995 if (err <= 0)
Tao Maaef1c852012-12-10 14:06:02 -05001996 goto out_error;
1997
1998 i.value = value;
1999 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
2000 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
Theodore Ts'o01daf942017-01-22 19:35:49 -05002001 err = ext4_xattr_ibody_inline_set(handle, inode,
2002 &i, &is);
2003 if (err)
Tao Maaef1c852012-12-10 14:06:02 -05002004 goto out_error;
2005 }
2006
2007 /* Clear the content within i_blocks. */
Theodore Ts'o09c455a2014-01-07 12:58:19 -05002008 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
2009 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
2010 memset(p + i_size, 0,
2011 EXT4_MIN_INLINE_DATA_SIZE - i_size);
2012 }
Tao Maaef1c852012-12-10 14:06:02 -05002013
2014 EXT4_I(inode)->i_inline_size = i_size <
2015 EXT4_MIN_INLINE_DATA_SIZE ?
2016 EXT4_MIN_INLINE_DATA_SIZE : i_size;
2017 }
2018
2019out_error:
2020 up_write(&EXT4_I(inode)->i_data_sem);
2021out:
2022 brelse(is.iloc.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002023 ext4_write_unlock_xattr(inode, &no_expand);
Tao Maaef1c852012-12-10 14:06:02 -05002024 kfree(value);
2025 if (inode->i_nlink)
2026 ext4_orphan_del(handle, inode);
2027
Theodore Ts'o01daf942017-01-22 19:35:49 -05002028 if (err == 0) {
2029 inode->i_mtime = inode->i_ctime = current_time(inode);
2030 err = ext4_mark_inode_dirty(handle, inode);
2031 if (IS_SYNC(inode))
2032 ext4_handle_sync(handle);
2033 }
Tao Maaef1c852012-12-10 14:06:02 -05002034 ext4_journal_stop(handle);
Theodore Ts'o01daf942017-01-22 19:35:49 -05002035 return err;
Tao Maaef1c852012-12-10 14:06:02 -05002036}
Tao Ma0c8d4142012-12-10 14:06:03 -05002037
2038int ext4_convert_inline_data(struct inode *inode)
2039{
Theodore Ts'oc755e252017-01-11 21:50:46 -05002040 int error, needed_blocks, no_expand;
Tao Ma0c8d4142012-12-10 14:06:03 -05002041 handle_t *handle;
2042 struct ext4_iloc iloc;
2043
2044 if (!ext4_has_inline_data(inode)) {
2045 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2046 return 0;
2047 }
2048
2049 needed_blocks = ext4_writepage_trans_blocks(inode);
2050
2051 iloc.bh = NULL;
2052 error = ext4_get_inode_loc(inode, &iloc);
2053 if (error)
2054 return error;
2055
Theodore Ts'o9924a922013-02-08 21:59:22 -05002056 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Ma0c8d4142012-12-10 14:06:03 -05002057 if (IS_ERR(handle)) {
2058 error = PTR_ERR(handle);
2059 goto out_free;
2060 }
2061
Theodore Ts'oc755e252017-01-11 21:50:46 -05002062 ext4_write_lock_xattr(inode, &no_expand);
2063 if (ext4_has_inline_data(inode))
2064 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2065 ext4_write_unlock_xattr(inode, &no_expand);
Tao Ma0c8d4142012-12-10 14:06:03 -05002066 ext4_journal_stop(handle);
2067out_free:
2068 brelse(iloc.bh);
2069 return error;
2070}