blob: d8ca4b9f9dd634ad5ae6b3f77ce8730a2be08916 [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{
379 int ret, size;
380 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
389 down_write(&EXT4_I(inode)->xattr_sem);
390
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
396 up_write(&EXT4_I(inode)->xattr_sem);
397
398 return ret;
399}
400
401static int ext4_destroy_inline_data_nolock(handle_t *handle,
402 struct inode *inode)
403{
404 struct ext4_inode_info *ei = EXT4_I(inode);
405 struct ext4_xattr_ibody_find is = {
406 .s = { .not_found = 0, },
407 };
408 struct ext4_xattr_info i = {
409 .name_index = EXT4_XATTR_INDEX_SYSTEM,
410 .name = EXT4_XATTR_SYSTEM_DATA,
411 .value = NULL,
412 .value_len = 0,
413 };
414 int error;
415
416 if (!ei->i_inline_off)
417 return 0;
418
419 error = ext4_get_inode_loc(inode, &is.iloc);
420 if (error)
421 return error;
422
423 error = ext4_xattr_ibody_find(inode, &i, &is);
424 if (error)
425 goto out;
426
liang xie5d601252014-05-12 22:06:43 -0400427 BUFFER_TRACE(is.iloc.bh, "get_write_access");
Tao Ma67cf5b02012-12-10 14:04:46 -0500428 error = ext4_journal_get_write_access(handle, is.iloc.bh);
429 if (error)
430 goto out;
431
Tao Ma0d812f72012-12-10 14:06:02 -0500432 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
Tao Ma67cf5b02012-12-10 14:04:46 -0500433 if (error)
434 goto out;
435
436 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
437 0, EXT4_MIN_INLINE_DATA_SIZE);
438
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400439 if (ext4_has_feature_extents(inode->i_sb)) {
Tao Ma67cf5b02012-12-10 14:04:46 -0500440 if (S_ISDIR(inode->i_mode) ||
441 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
442 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
443 ext4_ext_tree_init(handle, inode);
444 }
445 }
446 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
447
448 get_bh(is.iloc.bh);
449 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
450
451 EXT4_I(inode)->i_inline_off = 0;
452 EXT4_I(inode)->i_inline_size = 0;
453 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
454out:
455 brelse(is.iloc.bh);
456 if (error == -ENODATA)
457 error = 0;
458 return error;
459}
460
Tao Ma46c7f252012-12-10 14:04:52 -0500461static int ext4_read_inline_page(struct inode *inode, struct page *page)
462{
463 void *kaddr;
464 int ret = 0;
465 size_t len;
466 struct ext4_iloc iloc;
467
468 BUG_ON(!PageLocked(page));
469 BUG_ON(!ext4_has_inline_data(inode));
470 BUG_ON(page->index);
471
472 if (!EXT4_I(inode)->i_inline_off) {
473 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
474 inode->i_ino);
475 goto out;
476 }
477
478 ret = ext4_get_inode_loc(inode, &iloc);
479 if (ret)
480 goto out;
481
482 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
483 kaddr = kmap_atomic(page);
484 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
485 flush_dcache_page(page);
486 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300487 zero_user_segment(page, len, PAGE_SIZE);
Tao Ma46c7f252012-12-10 14:04:52 -0500488 SetPageUptodate(page);
489 brelse(iloc.bh);
490
491out:
492 return ret;
493}
494
495int ext4_readpage_inline(struct inode *inode, struct page *page)
496{
497 int ret = 0;
498
499 down_read(&EXT4_I(inode)->xattr_sem);
500 if (!ext4_has_inline_data(inode)) {
501 up_read(&EXT4_I(inode)->xattr_sem);
502 return -EAGAIN;
503 }
504
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{
526 int ret, needed_blocks;
527 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
566 down_write(&EXT4_I(inode)->xattr_sem);
567 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);
603 up_write(&EXT4_I(inode)->xattr_sem);
604 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)
629 up_write(&EXT4_I(inode)->xattr_sem);
630 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
682 flags |= AOP_FLAG_NOFS;
683
684 page = grab_cache_page_write_begin(mapping, 0, flags);
685 if (!page) {
686 ret = -ENOMEM;
687 goto out;
688 }
689
690 *pagep = page;
691 down_read(&EXT4_I(inode)->xattr_sem);
692 if (!ext4_has_inline_data(inode)) {
693 ret = 0;
694 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300695 put_page(page);
Tao Maf19d5872012-12-10 14:05:51 -0500696 goto out_up_read;
697 }
698
699 if (!PageUptodate(page)) {
700 ret = ext4_read_inline_page(inode, page);
701 if (ret < 0)
702 goto out_up_read;
703 }
704
705 ret = 1;
706 handle = NULL;
707out_up_read:
708 up_read(&EXT4_I(inode)->xattr_sem);
709out:
710 if (handle)
711 ext4_journal_stop(handle);
712 brelse(iloc.bh);
713 return ret;
714convert:
715 return ext4_convert_inline_data_to_extent(mapping,
716 inode, flags);
717}
718
719int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
720 unsigned copied, struct page *page)
721{
722 int ret;
723 void *kaddr;
724 struct ext4_iloc iloc;
725
726 if (unlikely(copied < len)) {
727 if (!PageUptodate(page)) {
728 copied = 0;
729 goto out;
730 }
731 }
732
733 ret = ext4_get_inode_loc(inode, &iloc);
734 if (ret) {
735 ext4_std_error(inode->i_sb, ret);
736 copied = 0;
737 goto out;
738 }
739
740 down_write(&EXT4_I(inode)->xattr_sem);
741 BUG_ON(!ext4_has_inline_data(inode));
742
743 kaddr = kmap_atomic(page);
744 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
745 kunmap_atomic(kaddr);
746 SetPageUptodate(page);
747 /* clear page dirty so that writepages wouldn't work for us. */
748 ClearPageDirty(page);
749
750 up_write(&EXT4_I(inode)->xattr_sem);
751 brelse(iloc.bh);
752out:
753 return copied;
754}
755
Tao Ma3fdcfb62012-12-10 14:05:57 -0500756struct buffer_head *
757ext4_journalled_write_inline_data(struct inode *inode,
758 unsigned len,
759 struct page *page)
760{
761 int ret;
762 void *kaddr;
763 struct ext4_iloc iloc;
764
765 ret = ext4_get_inode_loc(inode, &iloc);
766 if (ret) {
767 ext4_std_error(inode->i_sb, ret);
768 return NULL;
769 }
770
771 down_write(&EXT4_I(inode)->xattr_sem);
772 kaddr = kmap_atomic(page);
773 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
774 kunmap_atomic(kaddr);
775 up_write(&EXT4_I(inode)->xattr_sem);
776
777 return iloc.bh;
778}
779
Tao Ma9c3569b2012-12-10 14:05:57 -0500780/*
781 * Try to make the page cache and handle ready for the inline data case.
782 * We can call this function in 2 cases:
783 * 1. The inode is created and the first write exceeds inline size. We can
784 * clear the inode state safely.
785 * 2. The inode has inline data, then we need to read the data, make it
786 * update and dirty so that ext4_da_writepages can handle it. We don't
787 * need to start the journal since the file's metatdata isn't changed now.
788 */
789static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
790 struct inode *inode,
791 unsigned flags,
792 void **fsdata)
793{
794 int ret = 0, inline_size;
795 struct page *page;
796
797 page = grab_cache_page_write_begin(mapping, 0, flags);
798 if (!page)
799 return -ENOMEM;
800
801 down_read(&EXT4_I(inode)->xattr_sem);
802 if (!ext4_has_inline_data(inode)) {
803 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
804 goto out;
805 }
806
807 inline_size = ext4_get_inline_size(inode);
808
809 if (!PageUptodate(page)) {
810 ret = ext4_read_inline_page(inode, page);
811 if (ret < 0)
812 goto out;
813 }
814
815 ret = __block_write_begin(page, 0, inline_size,
816 ext4_da_get_block_prep);
817 if (ret) {
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500818 up_read(&EXT4_I(inode)->xattr_sem);
819 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300820 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500821 ext4_truncate_failed_write(inode);
Dmitry Monakhov50db71a2014-12-05 21:37:15 -0500822 return ret;
Tao Ma9c3569b2012-12-10 14:05:57 -0500823 }
824
825 SetPageDirty(page);
826 SetPageUptodate(page);
827 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
828 *fsdata = (void *)CONVERT_INLINE_DATA;
829
830out:
831 up_read(&EXT4_I(inode)->xattr_sem);
832 if (page) {
833 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300834 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500835 }
836 return ret;
837}
838
839/*
840 * Prepare the write for the inline data.
841 * If the the data can be written into the inode, we just read
842 * the page and make it uptodate, and start the journal.
843 * Otherwise read the page, makes it dirty so that it can be
844 * handle in writepages(the i_disksize update is left to the
845 * normal ext4_da_write_end).
846 */
847int ext4_da_write_inline_data_begin(struct address_space *mapping,
848 struct inode *inode,
849 loff_t pos, unsigned len,
850 unsigned flags,
851 struct page **pagep,
852 void **fsdata)
853{
854 int ret, inline_size;
855 handle_t *handle;
856 struct page *page;
857 struct ext4_iloc iloc;
Jan Karabc0ca9df2014-01-06 14:02:23 -0500858 int retries;
Tao Ma9c3569b2012-12-10 14:05:57 -0500859
860 ret = ext4_get_inode_loc(inode, &iloc);
861 if (ret)
862 return ret;
863
Jan Karabc0ca9df2014-01-06 14:02:23 -0500864retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500865 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Tao Ma9c3569b2012-12-10 14:05:57 -0500866 if (IS_ERR(handle)) {
867 ret = PTR_ERR(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500868 goto out;
869 }
870
871 inline_size = ext4_get_max_inline_size(inode);
872
873 ret = -ENOSPC;
874 if (inline_size >= pos + len) {
875 ret = ext4_prepare_inline_data(handle, inode, pos + len);
876 if (ret && ret != -ENOSPC)
Jan Kara52e44772014-01-06 14:03:23 -0500877 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500878 }
879
Dmitry Monakhov5cc28a92014-12-02 16:09:50 -0500880 /*
881 * We cannot recurse into the filesystem as the transaction
882 * is already started.
883 */
884 flags |= AOP_FLAG_NOFS;
885
Tao Ma9c3569b2012-12-10 14:05:57 -0500886 if (ret == -ENOSPC) {
887 ret = ext4_da_convert_inline_data_to_extent(mapping,
888 inode,
889 flags,
890 fsdata);
Jan Karabc0ca9df2014-01-06 14:02:23 -0500891 ext4_journal_stop(handle);
Jan Karabc0ca9df2014-01-06 14:02:23 -0500892 if (ret == -ENOSPC &&
893 ext4_should_retry_alloc(inode->i_sb, &retries))
894 goto retry_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500895 goto out;
896 }
897
Tao Ma9c3569b2012-12-10 14:05:57 -0500898
899 page = grab_cache_page_write_begin(mapping, 0, flags);
900 if (!page) {
901 ret = -ENOMEM;
Jan Kara52e44772014-01-06 14:03:23 -0500902 goto out_journal;
Tao Ma9c3569b2012-12-10 14:05:57 -0500903 }
904
905 down_read(&EXT4_I(inode)->xattr_sem);
906 if (!ext4_has_inline_data(inode)) {
907 ret = 0;
908 goto out_release_page;
909 }
910
911 if (!PageUptodate(page)) {
912 ret = ext4_read_inline_page(inode, page);
913 if (ret < 0)
914 goto out_release_page;
915 }
916
917 up_read(&EXT4_I(inode)->xattr_sem);
918 *pagep = page;
Tao Ma9c3569b2012-12-10 14:05:57 -0500919 brelse(iloc.bh);
920 return 1;
921out_release_page:
922 up_read(&EXT4_I(inode)->xattr_sem);
923 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300924 put_page(page);
Jan Kara52e44772014-01-06 14:03:23 -0500925out_journal:
926 ext4_journal_stop(handle);
Tao Ma9c3569b2012-12-10 14:05:57 -0500927out:
Tao Ma9c3569b2012-12-10 14:05:57 -0500928 brelse(iloc.bh);
929 return ret;
930}
931
932int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
933 unsigned len, unsigned copied,
934 struct page *page)
935{
936 int i_size_changed = 0;
937
938 copied = ext4_write_inline_data_end(inode, pos, len, copied, page);
939
940 /*
941 * No need to use i_size_read() here, the i_size
942 * cannot change under us because we hold i_mutex.
943 *
944 * But it's important to update i_size while still holding page lock:
945 * page writeout could otherwise come in and zero beyond i_size.
946 */
947 if (pos+copied > inode->i_size) {
948 i_size_write(inode, pos+copied);
949 i_size_changed = 1;
950 }
951 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300952 put_page(page);
Tao Ma9c3569b2012-12-10 14:05:57 -0500953
954 /*
955 * Don't mark the inode dirty under page lock. First, it unnecessarily
956 * makes the holding time of page lock longer. Second, it forces lock
957 * ordering of page lock and transaction start for journaling
958 * filesystems.
959 */
960 if (i_size_changed)
961 mark_inode_dirty(inode);
962
963 return copied;
964}
Tao Maf19d5872012-12-10 14:05:51 -0500965
Tao Ma3c47d542012-12-10 14:05:59 -0500966#ifdef INLINE_DIR_DEBUG
967void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
968 void *inline_start, int inline_size)
969{
970 int offset;
971 unsigned short de_len;
972 struct ext4_dir_entry_2 *de = inline_start;
973 void *dlimit = inline_start + inline_size;
974
975 trace_printk("inode %lu\n", dir->i_ino);
976 offset = 0;
977 while ((void *)de < dlimit) {
978 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
Rasmus Villemoes80cfb712015-04-02 16:42:43 -0400979 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
Tao Ma3c47d542012-12-10 14:05:59 -0500980 offset, de_len, de->name_len, de->name,
981 de->name_len, le32_to_cpu(de->inode));
982 if (ext4_check_dir_entry(dir, NULL, de, bh,
983 inline_start, inline_size, offset))
984 BUG();
985
986 offset += de_len;
987 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
988 }
989}
990#else
991#define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
992#endif
993
994/*
995 * Add a new entry into a inline dir.
996 * It will return -ENOSPC if no space is available, and -EIO
997 * and -EEXIST if directory entry already exists.
998 */
999static int ext4_add_dirent_to_inline(handle_t *handle,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001000 struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001001 struct inode *dir,
Tao Ma3c47d542012-12-10 14:05:59 -05001002 struct inode *inode,
1003 struct ext4_iloc *iloc,
1004 void *inline_start, int inline_size)
1005{
Tao Ma3c47d542012-12-10 14:05:59 -05001006 int err;
1007 struct ext4_dir_entry_2 *de;
1008
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001009 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1010 inline_size, fname, &de);
Tao Ma3c47d542012-12-10 14:05:59 -05001011 if (err)
1012 return err;
1013
liang xie5d601252014-05-12 22:06:43 -04001014 BUFFER_TRACE(iloc->bh, "get_write_access");
Tao Ma3c47d542012-12-10 14:05:59 -05001015 err = ext4_journal_get_write_access(handle, iloc->bh);
1016 if (err)
1017 return err;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001018 ext4_insert_dentry(dir, inode, de, inline_size, fname);
Tao Ma3c47d542012-12-10 14:05:59 -05001019
1020 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1021
1022 /*
1023 * XXX shouldn't update any times until successful
1024 * completion of syscall, but too many callers depend
1025 * on this.
1026 *
1027 * XXX similarly, too many callers depend on
1028 * ext4_new_inode() setting the times, but error
1029 * recovery deletes the inode, so the worst that can
1030 * happen is that the times are slightly out of date
1031 * and/or different from the directory change time.
1032 */
1033 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1034 ext4_update_dx_flag(dir);
1035 dir->i_version++;
1036 ext4_mark_inode_dirty(handle, dir);
1037 return 1;
1038}
1039
1040static void *ext4_get_inline_xattr_pos(struct inode *inode,
1041 struct ext4_iloc *iloc)
1042{
1043 struct ext4_xattr_entry *entry;
1044 struct ext4_xattr_ibody_header *header;
1045
1046 BUG_ON(!EXT4_I(inode)->i_inline_off);
1047
1048 header = IHDR(inode, ext4_raw_inode(iloc));
1049 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1050 EXT4_I(inode)->i_inline_off);
1051
1052 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1053}
1054
1055/* Set the final de to cover the whole block. */
1056static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1057{
1058 struct ext4_dir_entry_2 *de, *prev_de;
1059 void *limit;
1060 int de_len;
1061
1062 de = (struct ext4_dir_entry_2 *)de_buf;
1063 if (old_size) {
1064 limit = de_buf + old_size;
1065 do {
1066 prev_de = de;
1067 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1068 de_buf += de_len;
1069 de = (struct ext4_dir_entry_2 *)de_buf;
1070 } while (de_buf < limit);
1071
1072 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1073 old_size, new_size);
1074 } else {
1075 /* this is just created, so create an empty entry. */
1076 de->inode = 0;
1077 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1078 }
1079}
1080
1081static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1082 struct ext4_iloc *iloc)
1083{
1084 int ret;
1085 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1086 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1087
1088 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1089 return -ENOSPC;
1090
1091 ret = ext4_update_inline_data(handle, dir,
1092 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1093 if (ret)
1094 return ret;
1095
1096 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1097 EXT4_I(dir)->i_inline_size -
1098 EXT4_MIN_INLINE_DATA_SIZE);
1099 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1100 return 0;
1101}
1102
1103static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1104 struct ext4_iloc *iloc,
1105 void *buf, int inline_size)
1106{
1107 ext4_create_inline_data(handle, inode, inline_size);
1108 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1109 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1110}
1111
1112static int ext4_finish_convert_inline_dir(handle_t *handle,
1113 struct inode *inode,
1114 struct buffer_head *dir_block,
1115 void *buf,
1116 int inline_size)
1117{
1118 int err, csum_size = 0, header_size = 0;
1119 struct ext4_dir_entry_2 *de;
1120 struct ext4_dir_entry_tail *t;
1121 void *target = dir_block->b_data;
1122
1123 /*
1124 * First create "." and ".." and then copy the dir information
1125 * back to the block.
1126 */
1127 de = (struct ext4_dir_entry_2 *)target;
1128 de = ext4_init_dot_dotdot(inode, de,
1129 inode->i_sb->s_blocksize, csum_size,
1130 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1131 header_size = (void *)de - target;
1132
1133 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1134 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1135
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001136 if (ext4_has_metadata_csum(inode->i_sb))
Tao Ma3c47d542012-12-10 14:05:59 -05001137 csum_size = sizeof(struct ext4_dir_entry_tail);
1138
1139 inode->i_size = inode->i_sb->s_blocksize;
1140 i_size_write(inode, inode->i_sb->s_blocksize);
1141 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1142 ext4_update_final_de(dir_block->b_data,
1143 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1144 inode->i_sb->s_blocksize - csum_size);
1145
1146 if (csum_size) {
1147 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1148 inode->i_sb->s_blocksize);
1149 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1150 }
1151 set_buffer_uptodate(dir_block);
1152 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1153 if (err)
1154 goto out;
1155 set_buffer_verified(dir_block);
1156out:
1157 return err;
1158}
1159
1160static int ext4_convert_inline_data_nolock(handle_t *handle,
1161 struct inode *inode,
1162 struct ext4_iloc *iloc)
1163{
1164 int error;
1165 void *buf = NULL;
1166 struct buffer_head *data_bh = NULL;
1167 struct ext4_map_blocks map;
1168 int inline_size;
1169
1170 inline_size = ext4_get_inline_size(inode);
1171 buf = kmalloc(inline_size, GFP_NOFS);
1172 if (!buf) {
1173 error = -ENOMEM;
1174 goto out;
1175 }
1176
1177 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1178 if (error < 0)
1179 goto out;
1180
Darrick J. Wong40b163f2014-07-28 13:06:26 -04001181 /*
1182 * Make sure the inline directory entries pass checks before we try to
1183 * convert them, so that we avoid touching stuff that needs fsck.
1184 */
1185 if (S_ISDIR(inode->i_mode)) {
1186 error = ext4_check_all_de(inode, iloc->bh,
1187 buf + EXT4_INLINE_DOTDOT_SIZE,
1188 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1189 if (error)
1190 goto out;
1191 }
1192
Tao Ma3c47d542012-12-10 14:05:59 -05001193 error = ext4_destroy_inline_data_nolock(handle, inode);
1194 if (error)
1195 goto out;
1196
1197 map.m_lblk = 0;
1198 map.m_len = 1;
1199 map.m_flags = 0;
1200 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1201 if (error < 0)
1202 goto out_restore;
1203 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1204 error = -EIO;
1205 goto out_restore;
1206 }
1207
1208 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1209 if (!data_bh) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001210 error = -ENOMEM;
Tao Ma3c47d542012-12-10 14:05:59 -05001211 goto out_restore;
1212 }
1213
1214 lock_buffer(data_bh);
1215 error = ext4_journal_get_create_access(handle, data_bh);
1216 if (error) {
1217 unlock_buffer(data_bh);
1218 error = -EIO;
1219 goto out_restore;
1220 }
1221 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1222
1223 if (!S_ISDIR(inode->i_mode)) {
1224 memcpy(data_bh->b_data, buf, inline_size);
1225 set_buffer_uptodate(data_bh);
1226 error = ext4_handle_dirty_metadata(handle,
1227 inode, data_bh);
1228 } else {
1229 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1230 buf, inline_size);
1231 }
1232
1233 unlock_buffer(data_bh);
1234out_restore:
1235 if (error)
1236 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1237
1238out:
1239 brelse(data_bh);
1240 kfree(buf);
1241 return error;
1242}
1243
1244/*
1245 * Try to add the new entry to the inline data.
1246 * If succeeds, return 0. If not, extended the inline dir and copied data to
1247 * the new created block.
1248 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001249int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001250 struct inode *dir, struct inode *inode)
Tao Ma3c47d542012-12-10 14:05:59 -05001251{
1252 int ret, inline_size;
1253 void *inline_start;
1254 struct ext4_iloc iloc;
Tao Ma3c47d542012-12-10 14:05:59 -05001255
1256 ret = ext4_get_inode_loc(dir, &iloc);
1257 if (ret)
1258 return ret;
1259
1260 down_write(&EXT4_I(dir)->xattr_sem);
1261 if (!ext4_has_inline_data(dir))
1262 goto out;
1263
1264 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1265 EXT4_INLINE_DOTDOT_SIZE;
1266 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1267
Theodore Ts'o56a04912016-01-08 16:00:31 -05001268 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
Tao Ma3c47d542012-12-10 14:05:59 -05001269 inline_start, inline_size);
1270 if (ret != -ENOSPC)
1271 goto out;
1272
1273 /* check whether it can be inserted to inline xattr space. */
1274 inline_size = EXT4_I(dir)->i_inline_size -
1275 EXT4_MIN_INLINE_DATA_SIZE;
1276 if (!inline_size) {
1277 /* Try to use the xattr space.*/
1278 ret = ext4_update_inline_dir(handle, dir, &iloc);
1279 if (ret && ret != -ENOSPC)
1280 goto out;
1281
1282 inline_size = EXT4_I(dir)->i_inline_size -
1283 EXT4_MIN_INLINE_DATA_SIZE;
1284 }
1285
1286 if (inline_size) {
1287 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1288
Theodore Ts'o56a04912016-01-08 16:00:31 -05001289 ret = ext4_add_dirent_to_inline(handle, fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001290 inode, &iloc, inline_start,
1291 inline_size);
Tao Ma3c47d542012-12-10 14:05:59 -05001292
1293 if (ret != -ENOSPC)
1294 goto out;
1295 }
1296
1297 /*
1298 * The inline space is filled up, so create a new block for it.
1299 * As the extent tree will be created, we have to save the inline
1300 * dir first.
1301 */
1302 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1303
1304out:
1305 ext4_mark_inode_dirty(handle, dir);
1306 up_write(&EXT4_I(dir)->xattr_sem);
1307 brelse(iloc.bh);
1308 return ret;
1309}
1310
Tao Ma8af0f082013-04-19 17:53:09 -04001311/*
1312 * This function fills a red-black tree with information from an
1313 * inlined dir. It returns the number directory entries loaded
1314 * into the tree. If there is an error it is returned in err.
1315 */
1316int htree_inlinedir_to_tree(struct file *dir_file,
1317 struct inode *dir, ext4_lblk_t block,
1318 struct dx_hash_info *hinfo,
1319 __u32 start_hash, __u32 start_minor_hash,
1320 int *has_inline_data)
1321{
1322 int err = 0, count = 0;
1323 unsigned int parent_ino;
1324 int pos;
1325 struct ext4_dir_entry_2 *de;
1326 struct inode *inode = file_inode(dir_file);
1327 int ret, inline_size = 0;
1328 struct ext4_iloc iloc;
1329 void *dir_buf = NULL;
1330 struct ext4_dir_entry_2 fake;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001331 struct fscrypt_str tmp_str;
Tao Ma8af0f082013-04-19 17:53:09 -04001332
1333 ret = ext4_get_inode_loc(inode, &iloc);
1334 if (ret)
1335 return ret;
1336
1337 down_read(&EXT4_I(inode)->xattr_sem);
1338 if (!ext4_has_inline_data(inode)) {
1339 up_read(&EXT4_I(inode)->xattr_sem);
1340 *has_inline_data = 0;
1341 goto out;
1342 }
1343
1344 inline_size = ext4_get_inline_size(inode);
1345 dir_buf = kmalloc(inline_size, GFP_NOFS);
1346 if (!dir_buf) {
1347 ret = -ENOMEM;
1348 up_read(&EXT4_I(inode)->xattr_sem);
1349 goto out;
1350 }
1351
1352 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1353 up_read(&EXT4_I(inode)->xattr_sem);
1354 if (ret < 0)
1355 goto out;
1356
1357 pos = 0;
1358 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1359 while (pos < inline_size) {
1360 /*
1361 * As inlined dir doesn't store any information about '.' and
1362 * only the inode number of '..' is stored, we have to handle
1363 * them differently.
1364 */
1365 if (pos == 0) {
1366 fake.inode = cpu_to_le32(inode->i_ino);
1367 fake.name_len = 1;
1368 strcpy(fake.name, ".");
1369 fake.rec_len = ext4_rec_len_to_disk(
1370 EXT4_DIR_REC_LEN(fake.name_len),
1371 inline_size);
1372 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1373 de = &fake;
1374 pos = EXT4_INLINE_DOTDOT_OFFSET;
1375 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1376 fake.inode = cpu_to_le32(parent_ino);
1377 fake.name_len = 2;
1378 strcpy(fake.name, "..");
1379 fake.rec_len = ext4_rec_len_to_disk(
1380 EXT4_DIR_REC_LEN(fake.name_len),
1381 inline_size);
1382 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1383 de = &fake;
1384 pos = EXT4_INLINE_DOTDOT_SIZE;
1385 } else {
1386 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1387 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1388 if (ext4_check_dir_entry(inode, dir_file, de,
1389 iloc.bh, dir_buf,
1390 inline_size, pos)) {
1391 ret = count;
1392 goto out;
1393 }
1394 }
1395
1396 ext4fs_dirhash(de->name, de->name_len, hinfo);
1397 if ((hinfo->hash < start_hash) ||
1398 ((hinfo->hash == start_hash) &&
1399 (hinfo->minor_hash < start_minor_hash)))
1400 continue;
1401 if (de->inode == 0)
1402 continue;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001403 tmp_str.name = de->name;
1404 tmp_str.len = de->name_len;
1405 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1406 hinfo->minor_hash, de, &tmp_str);
Tao Ma8af0f082013-04-19 17:53:09 -04001407 if (err) {
1408 count = err;
1409 goto out;
1410 }
1411 count++;
1412 }
1413 ret = count;
1414out:
1415 kfree(dir_buf);
1416 brelse(iloc.bh);
1417 return ret;
1418}
1419
Tao Mac4d8b022013-04-19 17:55:33 -04001420/*
1421 * So this function is called when the volume is mkfsed with
1422 * dir_index disabled. In order to keep f_pos persistent
1423 * after we convert from an inlined dir to a blocked based,
1424 * we just pretend that we are a normal dir and return the
1425 * offset as if '.' and '..' really take place.
1426 *
1427 */
Al Viro725bebb2013-05-17 16:08:53 -04001428int ext4_read_inline_dir(struct file *file,
1429 struct dir_context *ctx,
Tao Ma65d165d2012-12-10 14:05:59 -05001430 int *has_inline_data)
1431{
Tao Ma65d165d2012-12-10 14:05:59 -05001432 unsigned int offset, parent_ino;
Al Viro725bebb2013-05-17 16:08:53 -04001433 int i;
Tao Ma65d165d2012-12-10 14:05:59 -05001434 struct ext4_dir_entry_2 *de;
1435 struct super_block *sb;
Al Viro725bebb2013-05-17 16:08:53 -04001436 struct inode *inode = file_inode(file);
Tao Ma65d165d2012-12-10 14:05:59 -05001437 int ret, inline_size = 0;
1438 struct ext4_iloc iloc;
1439 void *dir_buf = NULL;
Tao Mac4d8b022013-04-19 17:55:33 -04001440 int dotdot_offset, dotdot_size, extra_offset, extra_size;
Tao Ma65d165d2012-12-10 14:05:59 -05001441
1442 ret = ext4_get_inode_loc(inode, &iloc);
1443 if (ret)
1444 return ret;
1445
1446 down_read(&EXT4_I(inode)->xattr_sem);
1447 if (!ext4_has_inline_data(inode)) {
1448 up_read(&EXT4_I(inode)->xattr_sem);
1449 *has_inline_data = 0;
1450 goto out;
1451 }
1452
1453 inline_size = ext4_get_inline_size(inode);
1454 dir_buf = kmalloc(inline_size, GFP_NOFS);
1455 if (!dir_buf) {
1456 ret = -ENOMEM;
1457 up_read(&EXT4_I(inode)->xattr_sem);
1458 goto out;
1459 }
1460
1461 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1462 up_read(&EXT4_I(inode)->xattr_sem);
1463 if (ret < 0)
1464 goto out;
1465
BoxiLiu48ffdab2013-10-30 08:07:20 -04001466 ret = 0;
Tao Ma65d165d2012-12-10 14:05:59 -05001467 sb = inode->i_sb;
Tao Ma65d165d2012-12-10 14:05:59 -05001468 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
Al Viro725bebb2013-05-17 16:08:53 -04001469 offset = ctx->pos;
Tao Ma65d165d2012-12-10 14:05:59 -05001470
Tao Mac4d8b022013-04-19 17:55:33 -04001471 /*
1472 * dotdot_offset and dotdot_size is the real offset and
1473 * size for ".." and "." if the dir is block based while
1474 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1475 * So we will use extra_offset and extra_size to indicate them
1476 * during the inline dir iteration.
1477 */
1478 dotdot_offset = EXT4_DIR_REC_LEN(1);
1479 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1480 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1481 extra_size = extra_offset + inline_size;
1482
Al Viro725bebb2013-05-17 16:08:53 -04001483 /*
1484 * If the version has changed since the last call to
1485 * readdir(2), then we might be pointing to an invalid
1486 * dirent right now. Scan from the start of the inline
1487 * dir to make sure.
1488 */
1489 if (file->f_version != inode->i_version) {
1490 for (i = 0; i < extra_size && i < offset;) {
1491 /*
1492 * "." is with offset 0 and
1493 * ".." is dotdot_offset.
1494 */
1495 if (!i) {
1496 i = dotdot_offset;
1497 continue;
1498 } else if (i == dotdot_offset) {
1499 i = dotdot_size;
Tao Mac4d8b022013-04-19 17:55:33 -04001500 continue;
1501 }
Al Viro725bebb2013-05-17 16:08:53 -04001502 /* for other entry, the real offset in
1503 * the buf has to be tuned accordingly.
1504 */
Tao Mac4d8b022013-04-19 17:55:33 -04001505 de = (struct ext4_dir_entry_2 *)
Al Viro725bebb2013-05-17 16:08:53 -04001506 (dir_buf + i - extra_offset);
1507 /* It's too expensive to do a full
1508 * dirent test each time round this
1509 * loop, but we do have to test at
1510 * least that it is non-zero. A
1511 * failure will be detected in the
1512 * dirent test below. */
1513 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1514 < EXT4_DIR_REC_LEN(1))
1515 break;
1516 i += ext4_rec_len_from_disk(de->rec_len,
1517 extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001518 }
Al Viro725bebb2013-05-17 16:08:53 -04001519 offset = i;
1520 ctx->pos = offset;
1521 file->f_version = inode->i_version;
1522 }
1523
1524 while (ctx->pos < extra_size) {
1525 if (ctx->pos == 0) {
1526 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1527 goto out;
1528 ctx->pos = dotdot_offset;
1529 continue;
1530 }
1531
1532 if (ctx->pos == dotdot_offset) {
1533 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1534 goto out;
1535 ctx->pos = dotdot_size;
1536 continue;
1537 }
1538
1539 de = (struct ext4_dir_entry_2 *)
1540 (dir_buf + ctx->pos - extra_offset);
1541 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1542 extra_size, ctx->pos))
1543 goto out;
1544 if (le32_to_cpu(de->inode)) {
1545 if (!dir_emit(ctx, de->name, de->name_len,
1546 le32_to_cpu(de->inode),
1547 get_dtype(sb, de->file_type)))
1548 goto out;
1549 }
1550 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
Tao Ma65d165d2012-12-10 14:05:59 -05001551 }
1552out:
1553 kfree(dir_buf);
1554 brelse(iloc.bh);
1555 return ret;
1556}
1557
Tao Ma32f7f222012-12-10 14:06:01 -05001558struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1559 struct ext4_dir_entry_2 **parent_de,
1560 int *retval)
1561{
1562 struct ext4_iloc iloc;
1563
1564 *retval = ext4_get_inode_loc(inode, &iloc);
1565 if (*retval)
1566 return NULL;
1567
1568 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1569
1570 return iloc.bh;
1571}
1572
Tao Ma3c47d542012-12-10 14:05:59 -05001573/*
1574 * Try to create the inline data for the new dir.
1575 * If it succeeds, return 0, otherwise return the error.
1576 * In case of ENOSPC, the caller should create the normal disk layout dir.
1577 */
1578int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1579 struct inode *inode)
1580{
1581 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1582 struct ext4_iloc iloc;
1583 struct ext4_dir_entry_2 *de;
1584
1585 ret = ext4_get_inode_loc(inode, &iloc);
1586 if (ret)
1587 return ret;
1588
1589 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1590 if (ret)
1591 goto out;
1592
1593 /*
1594 * For inline dir, we only save the inode information for the ".."
1595 * and create a fake dentry to cover the left space.
1596 */
1597 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1598 de->inode = cpu_to_le32(parent->i_ino);
1599 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1600 de->inode = 0;
1601 de->rec_len = ext4_rec_len_to_disk(
1602 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1603 inline_size);
1604 set_nlink(inode, 2);
1605 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1606out:
1607 brelse(iloc.bh);
1608 return ret;
1609}
1610
Tao Mae8e948e2012-12-10 14:06:00 -05001611struct buffer_head *ext4_find_inline_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001612 struct ext4_filename *fname,
Tao Mae8e948e2012-12-10 14:06:00 -05001613 const struct qstr *d_name,
1614 struct ext4_dir_entry_2 **res_dir,
1615 int *has_inline_data)
1616{
1617 int ret;
1618 struct ext4_iloc iloc;
1619 void *inline_start;
1620 int inline_size;
1621
1622 if (ext4_get_inode_loc(dir, &iloc))
1623 return NULL;
1624
1625 down_read(&EXT4_I(dir)->xattr_sem);
1626 if (!ext4_has_inline_data(dir)) {
1627 *has_inline_data = 0;
1628 goto out;
1629 }
1630
1631 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1632 EXT4_INLINE_DOTDOT_SIZE;
1633 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001634 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1635 dir, fname, d_name, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001636 if (ret == 1)
1637 goto out_find;
1638 if (ret < 0)
1639 goto out;
1640
1641 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1642 goto out;
1643
1644 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1645 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1646
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001647 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1648 dir, fname, d_name, 0, res_dir);
Tao Mae8e948e2012-12-10 14:06:00 -05001649 if (ret == 1)
1650 goto out_find;
1651
1652out:
1653 brelse(iloc.bh);
1654 iloc.bh = NULL;
1655out_find:
1656 up_read(&EXT4_I(dir)->xattr_sem);
1657 return iloc.bh;
1658}
1659
Tao Ma9f40fe52012-12-10 14:06:00 -05001660int ext4_delete_inline_entry(handle_t *handle,
1661 struct inode *dir,
1662 struct ext4_dir_entry_2 *de_del,
1663 struct buffer_head *bh,
1664 int *has_inline_data)
1665{
1666 int err, inline_size;
1667 struct ext4_iloc iloc;
1668 void *inline_start;
1669
1670 err = ext4_get_inode_loc(dir, &iloc);
1671 if (err)
1672 return err;
1673
1674 down_write(&EXT4_I(dir)->xattr_sem);
1675 if (!ext4_has_inline_data(dir)) {
1676 *has_inline_data = 0;
1677 goto out;
1678 }
1679
1680 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1681 EXT4_MIN_INLINE_DATA_SIZE) {
1682 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1683 EXT4_INLINE_DOTDOT_SIZE;
1684 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1685 EXT4_INLINE_DOTDOT_SIZE;
1686 } else {
1687 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1688 inline_size = ext4_get_inline_size(dir) -
1689 EXT4_MIN_INLINE_DATA_SIZE;
1690 }
1691
liang xie5d601252014-05-12 22:06:43 -04001692 BUFFER_TRACE(bh, "get_write_access");
Tao Ma9f40fe52012-12-10 14:06:00 -05001693 err = ext4_journal_get_write_access(handle, bh);
1694 if (err)
1695 goto out;
1696
1697 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1698 inline_start, inline_size, 0);
1699 if (err)
1700 goto out;
1701
Tao Ma9f40fe52012-12-10 14:06:00 -05001702 err = ext4_mark_inode_dirty(handle, dir);
1703 if (unlikely(err))
1704 goto out;
1705
1706 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1707out:
1708 up_write(&EXT4_I(dir)->xattr_sem);
1709 brelse(iloc.bh);
1710 if (err != -ENOENT)
1711 ext4_std_error(dir->i_sb, err);
1712 return err;
1713}
1714
Tao Ma61f86632012-12-10 14:06:01 -05001715/*
1716 * Get the inline dentry at offset.
1717 */
1718static inline struct ext4_dir_entry_2 *
1719ext4_get_inline_entry(struct inode *inode,
1720 struct ext4_iloc *iloc,
1721 unsigned int offset,
1722 void **inline_start,
1723 int *inline_size)
1724{
1725 void *inline_pos;
1726
1727 BUG_ON(offset > ext4_get_inline_size(inode));
1728
1729 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1730 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1731 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1732 } else {
1733 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1734 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1735 *inline_size = ext4_get_inline_size(inode) -
1736 EXT4_MIN_INLINE_DATA_SIZE;
1737 }
1738
1739 if (inline_start)
1740 *inline_start = inline_pos;
1741 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1742}
1743
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001744bool empty_inline_dir(struct inode *dir, int *has_inline_data)
Tao Ma61f86632012-12-10 14:06:01 -05001745{
1746 int err, inline_size;
1747 struct ext4_iloc iloc;
1748 void *inline_pos;
1749 unsigned int offset;
1750 struct ext4_dir_entry_2 *de;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001751 bool ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001752
1753 err = ext4_get_inode_loc(dir, &iloc);
1754 if (err) {
1755 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1756 err, dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001757 return true;
Tao Ma61f86632012-12-10 14:06:01 -05001758 }
1759
1760 down_read(&EXT4_I(dir)->xattr_sem);
1761 if (!ext4_has_inline_data(dir)) {
1762 *has_inline_data = 0;
1763 goto out;
1764 }
1765
1766 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1767 if (!le32_to_cpu(de->inode)) {
1768 ext4_warning(dir->i_sb,
1769 "bad inline directory (dir #%lu) - no `..'",
1770 dir->i_ino);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001771 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001772 goto out;
1773 }
1774
1775 offset = EXT4_INLINE_DOTDOT_SIZE;
1776 while (offset < dir->i_size) {
1777 de = ext4_get_inline_entry(dir, &iloc, offset,
1778 &inline_pos, &inline_size);
1779 if (ext4_check_dir_entry(dir, NULL, de,
1780 iloc.bh, inline_pos,
1781 inline_size, offset)) {
1782 ext4_warning(dir->i_sb,
1783 "bad inline directory (dir #%lu) - "
1784 "inode %u, rec_len %u, name_len %d"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001785 "inline size %d",
Tao Ma61f86632012-12-10 14:06:01 -05001786 dir->i_ino, le32_to_cpu(de->inode),
1787 le16_to_cpu(de->rec_len), de->name_len,
1788 inline_size);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001789 ret = true;
Tao Ma61f86632012-12-10 14:06:01 -05001790 goto out;
1791 }
1792 if (le32_to_cpu(de->inode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001793 ret = false;
Tao Ma61f86632012-12-10 14:06:01 -05001794 goto out;
1795 }
1796 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1797 }
1798
1799out:
1800 up_read(&EXT4_I(dir)->xattr_sem);
1801 brelse(iloc.bh);
1802 return ret;
1803}
1804
Tao Ma67cf5b02012-12-10 14:04:46 -05001805int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1806{
1807 int ret;
1808
1809 down_write(&EXT4_I(inode)->xattr_sem);
1810 ret = ext4_destroy_inline_data_nolock(handle, inode);
1811 up_write(&EXT4_I(inode)->xattr_sem);
1812
1813 return ret;
1814}
Tao Ma94191982012-12-10 14:06:02 -05001815
1816int ext4_inline_data_fiemap(struct inode *inode,
1817 struct fiemap_extent_info *fieinfo,
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001818 int *has_inline, __u64 start, __u64 len)
Tao Ma94191982012-12-10 14:06:02 -05001819{
1820 __u64 physical = 0;
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001821 __u64 inline_len;
1822 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1823 FIEMAP_EXTENT_LAST;
Tao Ma94191982012-12-10 14:06:02 -05001824 int error = 0;
1825 struct ext4_iloc iloc;
1826
1827 down_read(&EXT4_I(inode)->xattr_sem);
1828 if (!ext4_has_inline_data(inode)) {
1829 *has_inline = 0;
1830 goto out;
1831 }
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001832 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1833 i_size_read(inode));
1834 if (start >= inline_len)
1835 goto out;
1836 if (start + len < inline_len)
1837 inline_len = start + len;
1838 inline_len -= start;
Tao Ma94191982012-12-10 14:06:02 -05001839
1840 error = ext4_get_inode_loc(inode, &iloc);
1841 if (error)
1842 goto out;
1843
Jan Karaeaf37932013-05-31 19:33:42 -04001844 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
Tao Ma94191982012-12-10 14:06:02 -05001845 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1846 physical += offsetof(struct ext4_inode, i_block);
Tao Ma94191982012-12-10 14:06:02 -05001847
1848 if (physical)
Dmitry Monakhovd952d692014-12-02 16:11:20 -05001849 error = fiemap_fill_next_extent(fieinfo, start, physical,
1850 inline_len, flags);
Tao Ma94191982012-12-10 14:06:02 -05001851 brelse(iloc.bh);
1852out:
1853 up_read(&EXT4_I(inode)->xattr_sem);
1854 return (error < 0 ? error : 0);
1855}
Tao Ma0d812f72012-12-10 14:06:02 -05001856
1857/*
1858 * Called during xattr set, and if we can sparse space 'needed',
1859 * just create the extent tree evict the data to the outer block.
1860 *
1861 * We use jbd2 instead of page cache to move data to the 1st block
1862 * so that the whole transaction can be committed as a whole and
1863 * the data isn't lost because of the delayed page cache write.
1864 */
1865int ext4_try_to_evict_inline_data(handle_t *handle,
1866 struct inode *inode,
1867 int needed)
1868{
1869 int error;
1870 struct ext4_xattr_entry *entry;
Tao Ma0d812f72012-12-10 14:06:02 -05001871 struct ext4_inode *raw_inode;
1872 struct ext4_iloc iloc;
1873
1874 error = ext4_get_inode_loc(inode, &iloc);
1875 if (error)
1876 return error;
1877
1878 raw_inode = ext4_raw_inode(&iloc);
Tao Ma0d812f72012-12-10 14:06:02 -05001879 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
1880 EXT4_I(inode)->i_inline_off);
1881 if (EXT4_XATTR_LEN(entry->e_name_len) +
1882 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) {
1883 error = -ENOSPC;
1884 goto out;
1885 }
1886
1887 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
1888out:
1889 brelse(iloc.bh);
1890 return error;
1891}
Tao Maaef1c852012-12-10 14:06:02 -05001892
1893void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1894{
1895 handle_t *handle;
1896 int inline_size, value_len, needed_blocks;
1897 size_t i_size;
1898 void *value = NULL;
1899 struct ext4_xattr_ibody_find is = {
1900 .s = { .not_found = -ENODATA, },
1901 };
1902 struct ext4_xattr_info i = {
1903 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1904 .name = EXT4_XATTR_SYSTEM_DATA,
1905 };
1906
1907
1908 needed_blocks = ext4_writepage_trans_blocks(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -05001909 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
Tao Maaef1c852012-12-10 14:06:02 -05001910 if (IS_ERR(handle))
1911 return;
1912
1913 down_write(&EXT4_I(inode)->xattr_sem);
1914 if (!ext4_has_inline_data(inode)) {
1915 *has_inline = 0;
1916 ext4_journal_stop(handle);
1917 return;
1918 }
1919
1920 if (ext4_orphan_add(handle, inode))
1921 goto out;
1922
1923 if (ext4_get_inode_loc(inode, &is.iloc))
1924 goto out;
1925
1926 down_write(&EXT4_I(inode)->i_data_sem);
1927 i_size = inode->i_size;
1928 inline_size = ext4_get_inline_size(inode);
1929 EXT4_I(inode)->i_disksize = i_size;
1930
1931 if (i_size < inline_size) {
1932 /* Clear the content in the xattr space. */
1933 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1934 if (ext4_xattr_ibody_find(inode, &i, &is))
1935 goto out_error;
1936
1937 BUG_ON(is.s.not_found);
1938
1939 value_len = le32_to_cpu(is.s.here->e_value_size);
1940 value = kmalloc(value_len, GFP_NOFS);
1941 if (!value)
1942 goto out_error;
1943
1944 if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
1945 value, value_len))
1946 goto out_error;
1947
1948 i.value = value;
1949 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1950 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1951 if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
1952 goto out_error;
1953 }
1954
1955 /* Clear the content within i_blocks. */
Theodore Ts'o09c455a2014-01-07 12:58:19 -05001956 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1957 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1958 memset(p + i_size, 0,
1959 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1960 }
Tao Maaef1c852012-12-10 14:06:02 -05001961
1962 EXT4_I(inode)->i_inline_size = i_size <
1963 EXT4_MIN_INLINE_DATA_SIZE ?
1964 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1965 }
1966
1967out_error:
1968 up_write(&EXT4_I(inode)->i_data_sem);
1969out:
1970 brelse(is.iloc.bh);
1971 up_write(&EXT4_I(inode)->xattr_sem);
1972 kfree(value);
1973 if (inode->i_nlink)
1974 ext4_orphan_del(handle, inode);
1975
1976 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
1977 ext4_mark_inode_dirty(handle, inode);
1978 if (IS_SYNC(inode))
1979 ext4_handle_sync(handle);
1980
1981 ext4_journal_stop(handle);
1982 return;
1983}
Tao Ma0c8d4142012-12-10 14:06:03 -05001984
1985int ext4_convert_inline_data(struct inode *inode)
1986{
1987 int error, needed_blocks;
1988 handle_t *handle;
1989 struct ext4_iloc iloc;
1990
1991 if (!ext4_has_inline_data(inode)) {
1992 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1993 return 0;
1994 }
1995
1996 needed_blocks = ext4_writepage_trans_blocks(inode);
1997
1998 iloc.bh = NULL;
1999 error = ext4_get_inode_loc(inode, &iloc);
2000 if (error)
2001 return error;
2002
Theodore Ts'o9924a922013-02-08 21:59:22 -05002003 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Tao Ma0c8d4142012-12-10 14:06:03 -05002004 if (IS_ERR(handle)) {
2005 error = PTR_ERR(handle);
2006 goto out_free;
2007 }
2008
2009 down_write(&EXT4_I(inode)->xattr_sem);
2010 if (!ext4_has_inline_data(inode)) {
2011 up_write(&EXT4_I(inode)->xattr_sem);
2012 goto out;
2013 }
2014
2015 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2016 up_write(&EXT4_I(inode)->xattr_sem);
2017out:
2018 ext4_journal_stop(handle);
2019out_free:
2020 brelse(iloc.bh);
2021 return error;
2022}