Blame view

fs/ext4/inline.c 48.9 KB
67cf5b09a   Tao Ma   ext4: add the bas...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * Copyright (c) 2012 Taobao.
   * Written by Tao Ma <boyu.mt@taobao.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of version 2.1 of the GNU Lesser General Public License
   * as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   */
4bdfc873b   Michael Halcrow   ext4 crypto: inse...
14
15
  
  #include <linux/fiemap.h>
67cf5b09a   Tao Ma   ext4: add the bas...
16
17
18
  #include "ext4_jbd2.h"
  #include "ext4.h"
  #include "xattr.h"
f19d5870c   Tao Ma   ext4: add normal ...
19
  #include "truncate.h"
67cf5b09a   Tao Ma   ext4: add the bas...
20
21
22
  
  #define EXT4_XATTR_SYSTEM_DATA	"data"
  #define EXT4_MIN_INLINE_DATA_SIZE	((sizeof(__le32) * EXT4_N_BLOCKS))
8af0f0822   Tao Ma   ext4: fix readdir...
23
24
  #define EXT4_INLINE_DOTDOT_OFFSET	2
  #define EXT4_INLINE_DOTDOT_SIZE		4
67cf5b09a   Tao Ma   ext4: add the bas...
25

c197855ea   Stephen Hemminger   ext4: make local ...
26
  static int ext4_get_inline_size(struct inode *inode)
67cf5b09a   Tao Ma   ext4: add the bas...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  {
  	if (EXT4_I(inode)->i_inline_off)
  		return EXT4_I(inode)->i_inline_size;
  
  	return 0;
  }
  
  static int get_max_inline_xattr_value_size(struct inode *inode,
  					   struct ext4_iloc *iloc)
  {
  	struct ext4_xattr_ibody_header *header;
  	struct ext4_xattr_entry *entry;
  	struct ext4_inode *raw_inode;
  	int free, min_offs;
  
  	min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
  			EXT4_GOOD_OLD_INODE_SIZE -
  			EXT4_I(inode)->i_extra_isize -
  			sizeof(struct ext4_xattr_ibody_header);
  
  	/*
  	 * We need to subtract another sizeof(__u32) since an in-inode xattr
  	 * needs an empty 4 bytes to indicate the gap between the xattr entry
  	 * and the name/value pair.
  	 */
  	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  		return EXT4_XATTR_SIZE(min_offs -
  			EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
  			EXT4_XATTR_ROUND - sizeof(__u32));
  
  	raw_inode = ext4_raw_inode(iloc);
  	header = IHDR(inode, raw_inode);
  	entry = IFIRST(header);
  
  	/* Compute min_offs. */
  	for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
e50e5129f   Andreas Dilger   ext4: xattr-in-in...
63
  		if (!entry->e_value_inum && entry->e_value_size) {
67cf5b09a   Tao Ma   ext4: add the bas...
64
65
66
67
68
69
70
71
72
73
74
  			size_t offs = le16_to_cpu(entry->e_value_offs);
  			if (offs < min_offs)
  				min_offs = offs;
  		}
  	}
  	free = min_offs -
  		((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
  
  	if (EXT4_I(inode)->i_inline_off) {
  		entry = (struct ext4_xattr_entry *)
  			((void *)raw_inode + EXT4_I(inode)->i_inline_off);
c4932dbe6   boxi liu   ext4: improve fre...
75
  		free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
67cf5b09a   Tao Ma   ext4: add the bas...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  		goto out;
  	}
  
  	free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
  
  	if (free > EXT4_XATTR_ROUND)
  		free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
  	else
  		free = 0;
  
  out:
  	return free;
  }
  
  /*
   * Get the maximum size we now can store in an inode.
   * If we can't find the space for a xattr entry, don't use the space
   * of the extents since we have no space to indicate the inline data.
   */
  int ext4_get_max_inline_size(struct inode *inode)
  {
  	int error, max_inline_size;
  	struct ext4_iloc iloc;
  
  	if (EXT4_I(inode)->i_extra_isize == 0)
  		return 0;
  
  	error = ext4_get_inode_loc(inode, &iloc);
  	if (error) {
  		ext4_error_inode(inode, __func__, __LINE__, 0,
  				 "can't get inode location %lu",
  				 inode->i_ino);
  		return 0;
  	}
  
  	down_read(&EXT4_I(inode)->xattr_sem);
  	max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
  	up_read(&EXT4_I(inode)->xattr_sem);
  
  	brelse(iloc.bh);
  
  	if (!max_inline_size)
  		return 0;
  
  	return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
  }
67cf5b09a   Tao Ma   ext4: add the bas...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  /*
   * this function does not take xattr_sem, which is OK because it is
   * currently only used in a code path coming form ext4_iget, before
   * the new inode has been unlocked
   */
  int ext4_find_inline_data_nolock(struct inode *inode)
  {
  	struct ext4_xattr_ibody_find is = {
  		.s = { .not_found = -ENODATA, },
  	};
  	struct ext4_xattr_info i = {
  		.name_index = EXT4_XATTR_INDEX_SYSTEM,
  		.name = EXT4_XATTR_SYSTEM_DATA,
  	};
  	int error;
  
  	if (EXT4_I(inode)->i_extra_isize == 0)
  		return 0;
  
  	error = ext4_get_inode_loc(inode, &is.iloc);
  	if (error)
  		return error;
  
  	error = ext4_xattr_ibody_find(inode, &i, &is);
  	if (error)
  		goto out;
  
  	if (!is.s.not_found) {
e81d371da   Theodore Ts'o   ext4: do not allo...
150
151
152
153
154
155
  		if (is.s.here->e_value_inum) {
  			EXT4_ERROR_INODE(inode, "inline data xattr refers "
  					 "to an external xattr inode");
  			error = -EFSCORRUPTED;
  			goto out;
  		}
67cf5b09a   Tao Ma   ext4: add the bas...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  		EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  					(void *)ext4_raw_inode(&is.iloc));
  		EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
  				le32_to_cpu(is.s.here->e_value_size);
  		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  	}
  out:
  	brelse(is.iloc.bh);
  	return error;
  }
  
  static int ext4_read_inline_data(struct inode *inode, void *buffer,
  				 unsigned int len,
  				 struct ext4_iloc *iloc)
  {
  	struct ext4_xattr_entry *entry;
  	struct ext4_xattr_ibody_header *header;
  	int cp_len = 0;
  	struct ext4_inode *raw_inode;
  
  	if (!len)
  		return 0;
  
  	BUG_ON(len > EXT4_I(inode)->i_inline_size);
  
  	cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
  			len : EXT4_MIN_INLINE_DATA_SIZE;
  
  	raw_inode = ext4_raw_inode(iloc);
  	memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
  
  	len -= cp_len;
  	buffer += cp_len;
  
  	if (!len)
  		goto out;
  
  	header = IHDR(inode, raw_inode);
  	entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  					    EXT4_I(inode)->i_inline_off);
  	len = min_t(unsigned int, len,
  		    (unsigned int)le32_to_cpu(entry->e_value_size));
  
  	memcpy(buffer,
  	       (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
  	cp_len += len;
  
  out:
  	return cp_len;
  }
  
  /*
   * write the buffer to the inline inode.
   * If 'create' is set, we don't need to do the extra copy in the xattr
0d812f77b   Tao Ma   ext4: evict inlin...
210
211
   * value since it is already handled by ext4_xattr_ibody_inline_set.
   * That saves us one memcpy.
67cf5b09a   Tao Ma   ext4: add the bas...
212
   */
c197855ea   Stephen Hemminger   ext4: make local ...
213
214
  static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
  				   void *buffer, loff_t pos, unsigned int len)
67cf5b09a   Tao Ma   ext4: add the bas...
215
216
217
218
219
  {
  	struct ext4_xattr_entry *entry;
  	struct ext4_xattr_ibody_header *header;
  	struct ext4_inode *raw_inode;
  	int cp_len = 0;
0db1ff222   Theodore Ts'o   ext4: add shutdow...
220
221
  	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  		return;
67cf5b09a   Tao Ma   ext4: add the bas...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  	BUG_ON(!EXT4_I(inode)->i_inline_off);
  	BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
  
  	raw_inode = ext4_raw_inode(iloc);
  	buffer += pos;
  
  	if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
  		cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
  			 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
  		memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
  
  		len -= cp_len;
  		buffer += cp_len;
  		pos += cp_len;
  	}
  
  	if (!len)
  		return;
  
  	pos -= EXT4_MIN_INLINE_DATA_SIZE;
  	header = IHDR(inode, raw_inode);
  	entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  					    EXT4_I(inode)->i_inline_off);
  
  	memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
  	       buffer, len);
  }
  
  static int ext4_create_inline_data(handle_t *handle,
  				   struct inode *inode, unsigned len)
  {
  	int error;
  	void *value = NULL;
  	struct ext4_xattr_ibody_find is = {
  		.s = { .not_found = -ENODATA, },
  	};
  	struct ext4_xattr_info i = {
  		.name_index = EXT4_XATTR_INDEX_SYSTEM,
  		.name = EXT4_XATTR_SYSTEM_DATA,
  	};
  
  	error = ext4_get_inode_loc(inode, &is.iloc);
  	if (error)
  		return error;
5d6012553   liang xie   ext4: add missing...
266
  	BUFFER_TRACE(is.iloc.bh, "get_write_access");
67cf5b09a   Tao Ma   ext4: add the bas...
267
268
269
270
271
  	error = ext4_journal_get_write_access(handle, is.iloc.bh);
  	if (error)
  		goto out;
  
  	if (len > EXT4_MIN_INLINE_DATA_SIZE) {
bd9926e80   Theodore Ts'o   ext4: zero out in...
272
  		value = EXT4_ZERO_XATTR_VALUE;
67cf5b09a   Tao Ma   ext4: add the bas...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  		len -= EXT4_MIN_INLINE_DATA_SIZE;
  	} else {
  		value = "";
  		len = 0;
  	}
  
  	/* Insert the the xttr entry. */
  	i.value = value;
  	i.value_len = len;
  
  	error = ext4_xattr_ibody_find(inode, &i, &is);
  	if (error)
  		goto out;
  
  	BUG_ON(!is.s.not_found);
0d812f77b   Tao Ma   ext4: evict inlin...
288
  	error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
67cf5b09a   Tao Ma   ext4: add the bas...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  	if (error) {
  		if (error == -ENOSPC)
  			ext4_clear_inode_state(inode,
  					       EXT4_STATE_MAY_INLINE_DATA);
  		goto out;
  	}
  
  	memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  		0, EXT4_MIN_INLINE_DATA_SIZE);
  
  	EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  				      (void *)ext4_raw_inode(&is.iloc));
  	EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
  	ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  	ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
  	get_bh(is.iloc.bh);
  	error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  
  out:
  	brelse(is.iloc.bh);
  	return error;
  }
  
  static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
  				   unsigned int len)
  {
  	int error;
  	void *value = NULL;
  	struct ext4_xattr_ibody_find is = {
  		.s = { .not_found = -ENODATA, },
  	};
  	struct ext4_xattr_info i = {
  		.name_index = EXT4_XATTR_INDEX_SYSTEM,
  		.name = EXT4_XATTR_SYSTEM_DATA,
  	};
  
  	/* If the old space is ok, write the data directly. */
  	if (len <= EXT4_I(inode)->i_inline_size)
  		return 0;
  
  	error = ext4_get_inode_loc(inode, &is.iloc);
  	if (error)
  		return error;
  
  	error = ext4_xattr_ibody_find(inode, &i, &is);
  	if (error)
  		goto out;
  
  	BUG_ON(is.s.not_found);
  
  	len -= EXT4_MIN_INLINE_DATA_SIZE;
  	value = kzalloc(len, GFP_NOFS);
578620f45   Dan Carpenter   ext4: return -ENO...
341
342
  	if (!value) {
  		error = -ENOMEM;
67cf5b09a   Tao Ma   ext4: add the bas...
343
  		goto out;
578620f45   Dan Carpenter   ext4: return -ENO...
344
  	}
67cf5b09a   Tao Ma   ext4: add the bas...
345
346
347
348
349
  
  	error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
  				     value, len);
  	if (error == -ENODATA)
  		goto out;
5d6012553   liang xie   ext4: add missing...
350
  	BUFFER_TRACE(is.iloc.bh, "get_write_access");
67cf5b09a   Tao Ma   ext4: add the bas...
351
352
353
354
355
356
357
  	error = ext4_journal_get_write_access(handle, is.iloc.bh);
  	if (error)
  		goto out;
  
  	/* Update the xttr entry. */
  	i.value = value;
  	i.value_len = len;
0d812f77b   Tao Ma   ext4: evict inlin...
358
  	error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
67cf5b09a   Tao Ma   ext4: add the bas...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  	if (error)
  		goto out;
  
  	EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  				      (void *)ext4_raw_inode(&is.iloc));
  	EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
  				le32_to_cpu(is.s.here->e_value_size);
  	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  	get_bh(is.iloc.bh);
  	error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  
  out:
  	kfree(value);
  	brelse(is.iloc.bh);
  	return error;
  }
c197855ea   Stephen Hemminger   ext4: make local ...
375
376
  static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
  				    unsigned int len)
67cf5b09a   Tao Ma   ext4: add the bas...
377
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
378
  	int ret, size, no_expand;
67cf5b09a   Tao Ma   ext4: add the bas...
379
380
381
382
383
384
385
386
  	struct ext4_inode_info *ei = EXT4_I(inode);
  
  	if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
  		return -ENOSPC;
  
  	size = ext4_get_max_inline_size(inode);
  	if (size < len)
  		return -ENOSPC;
c755e2513   Theodore Ts'o   ext4: fix deadloc...
387
  	ext4_write_lock_xattr(inode, &no_expand);
67cf5b09a   Tao Ma   ext4: add the bas...
388
389
390
391
392
  
  	if (ei->i_inline_off)
  		ret = ext4_update_inline_data(handle, inode, len);
  	else
  		ret = ext4_create_inline_data(handle, inode, len);
c755e2513   Theodore Ts'o   ext4: fix deadloc...
393
  	ext4_write_unlock_xattr(inode, &no_expand);
67cf5b09a   Tao Ma   ext4: add the bas...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
  	return ret;
  }
  
  static int ext4_destroy_inline_data_nolock(handle_t *handle,
  					   struct inode *inode)
  {
  	struct ext4_inode_info *ei = EXT4_I(inode);
  	struct ext4_xattr_ibody_find is = {
  		.s = { .not_found = 0, },
  	};
  	struct ext4_xattr_info i = {
  		.name_index = EXT4_XATTR_INDEX_SYSTEM,
  		.name = EXT4_XATTR_SYSTEM_DATA,
  		.value = NULL,
  		.value_len = 0,
  	};
  	int error;
  
  	if (!ei->i_inline_off)
  		return 0;
  
  	error = ext4_get_inode_loc(inode, &is.iloc);
  	if (error)
  		return error;
  
  	error = ext4_xattr_ibody_find(inode, &i, &is);
  	if (error)
  		goto out;
5d6012553   liang xie   ext4: add missing...
422
  	BUFFER_TRACE(is.iloc.bh, "get_write_access");
67cf5b09a   Tao Ma   ext4: add the bas...
423
424
425
  	error = ext4_journal_get_write_access(handle, is.iloc.bh);
  	if (error)
  		goto out;
0d812f77b   Tao Ma   ext4: evict inlin...
426
  	error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
67cf5b09a   Tao Ma   ext4: add the bas...
427
428
429
430
431
  	if (error)
  		goto out;
  
  	memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  		0, EXT4_MIN_INLINE_DATA_SIZE);
deb465ec7   Theodore Ts'o   ext4: clear i_dat...
432
  	memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
67cf5b09a   Tao Ma   ext4: add the bas...
433

e2b911c53   Darrick J. Wong   ext4: clean up fe...
434
  	if (ext4_has_feature_extents(inode->i_sb)) {
67cf5b09a   Tao Ma   ext4: add the bas...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  		if (S_ISDIR(inode->i_mode) ||
  		    S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  			ext4_ext_tree_init(handle, inode);
  		}
  	}
  	ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
  
  	get_bh(is.iloc.bh);
  	error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  
  	EXT4_I(inode)->i_inline_off = 0;
  	EXT4_I(inode)->i_inline_size = 0;
  	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  out:
  	brelse(is.iloc.bh);
  	if (error == -ENODATA)
  		error = 0;
  	return error;
  }
46c7f2545   Tao Ma   ext4: add read su...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  static int ext4_read_inline_page(struct inode *inode, struct page *page)
  {
  	void *kaddr;
  	int ret = 0;
  	size_t len;
  	struct ext4_iloc iloc;
  
  	BUG_ON(!PageLocked(page));
  	BUG_ON(!ext4_has_inline_data(inode));
  	BUG_ON(page->index);
  
  	if (!EXT4_I(inode)->i_inline_off) {
  		ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
  			     inode->i_ino);
  		goto out;
  	}
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret)
  		goto out;
  
  	len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
  	kaddr = kmap_atomic(page);
  	ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
  	flush_dcache_page(page);
  	kunmap_atomic(kaddr);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
481
  	zero_user_segment(page, len, PAGE_SIZE);
46c7f2545   Tao Ma   ext4: add read su...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
  	SetPageUptodate(page);
  	brelse(iloc.bh);
  
  out:
  	return ret;
  }
  
  int ext4_readpage_inline(struct inode *inode, struct page *page)
  {
  	int ret = 0;
  
  	down_read(&EXT4_I(inode)->xattr_sem);
  	if (!ext4_has_inline_data(inode)) {
  		up_read(&EXT4_I(inode)->xattr_sem);
  		return -EAGAIN;
  	}
  
  	/*
  	 * Current inline data can only exist in the 1st page,
  	 * So for all the other pages, just set them uptodate.
  	 */
  	if (!page->index)
  		ret = ext4_read_inline_page(inode, page);
  	else if (!PageUptodate(page)) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
506
  		zero_user_segment(page, 0, PAGE_SIZE);
46c7f2545   Tao Ma   ext4: add read su...
507
508
509
510
511
512
513
514
  		SetPageUptodate(page);
  	}
  
  	up_read(&EXT4_I(inode)->xattr_sem);
  
  	unlock_page(page);
  	return ret >= 0 ? 0 : ret;
  }
f19d5870c   Tao Ma   ext4: add normal ...
515
516
517
518
  static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
  					      struct inode *inode,
  					      unsigned flags)
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
519
  	int ret, needed_blocks, no_expand;
f19d5870c   Tao Ma   ext4: add normal ...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
  	handle_t *handle = NULL;
  	int retries = 0, sem_held = 0;
  	struct page *page = NULL;
  	unsigned from, to;
  	struct ext4_iloc iloc;
  
  	if (!ext4_has_inline_data(inode)) {
  		/*
  		 * clear the flag so that no new write
  		 * will trap here again.
  		 */
  		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  		return 0;
  	}
  
  	needed_blocks = ext4_writepage_trans_blocks(inode);
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret)
  		return ret;
  
  retry:
9924a92a8   Theodore Ts'o   ext4: pass contex...
542
  	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
f19d5870c   Tao Ma   ext4: add normal ...
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
  		handle = NULL;
  		goto out;
  	}
  
  	/* We cannot recurse into the filesystem as the transaction is already
  	 * started */
  	flags |= AOP_FLAG_NOFS;
  
  	page = grab_cache_page_write_begin(mapping, 0, flags);
  	if (!page) {
  		ret = -ENOMEM;
  		goto out;
  	}
c755e2513   Theodore Ts'o   ext4: fix deadloc...
558
  	ext4_write_lock_xattr(inode, &no_expand);
f19d5870c   Tao Ma   ext4: add normal ...
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  	sem_held = 1;
  	/* If some one has already done this for us, just exit. */
  	if (!ext4_has_inline_data(inode)) {
  		ret = 0;
  		goto out;
  	}
  
  	from = 0;
  	to = ext4_get_inline_size(inode);
  	if (!PageUptodate(page)) {
  		ret = ext4_read_inline_page(inode, page);
  		if (ret < 0)
  			goto out;
  	}
  
  	ret = ext4_destroy_inline_data_nolock(handle, inode);
  	if (ret)
  		goto out;
705965bd6   Jan Kara   ext4: rename and ...
577
578
579
580
  	if (ext4_should_dioread_nolock(inode)) {
  		ret = __block_write_begin(page, from, to,
  					  ext4_get_block_unwritten);
  	} else
f19d5870c   Tao Ma   ext4: add normal ...
581
582
583
584
585
586
587
588
589
590
  		ret = __block_write_begin(page, from, to, ext4_get_block);
  
  	if (!ret && ext4_should_journal_data(inode)) {
  		ret = ext4_walk_page_buffers(handle, page_buffers(page),
  					     from, to, NULL,
  					     do_journal_get_write_access);
  	}
  
  	if (ret) {
  		unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
591
  		put_page(page);
684de5748   Darrick J. Wong   ext4: don't keep ...
592
  		page = NULL;
f19d5870c   Tao Ma   ext4: add normal ...
593
  		ext4_orphan_add(handle, inode);
c755e2513   Theodore Ts'o   ext4: fix deadloc...
594
  		ext4_write_unlock_xattr(inode, &no_expand);
f19d5870c   Tao Ma   ext4: add normal ...
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  		sem_held = 0;
  		ext4_journal_stop(handle);
  		handle = NULL;
  		ext4_truncate_failed_write(inode);
  		/*
  		 * If truncate failed early the inode might
  		 * still be on the orphan list; we need to
  		 * make sure the inode is removed from the
  		 * orphan list in that case.
  		 */
  		if (inode->i_nlink)
  			ext4_orphan_del(NULL, inode);
  	}
  
  	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  		goto retry;
684de5748   Darrick J. Wong   ext4: don't keep ...
611
612
  	if (page)
  		block_commit_write(page, from, to);
f19d5870c   Tao Ma   ext4: add normal ...
613
614
615
  out:
  	if (page) {
  		unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
616
  		put_page(page);
f19d5870c   Tao Ma   ext4: add normal ...
617
618
  	}
  	if (sem_held)
c755e2513   Theodore Ts'o   ext4: fix deadloc...
619
  		ext4_write_unlock_xattr(inode, &no_expand);
f19d5870c   Tao Ma   ext4: add normal ...
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
  	if (handle)
  		ext4_journal_stop(handle);
  	brelse(iloc.bh);
  	return ret;
  }
  
  /*
   * Try to write data in the inode.
   * If the inode has inline data, check whether the new write can be
   * in the inode also. If not, create the page the handle, move the data
   * to the page make it update and let the later codes create extent for it.
   */
  int ext4_try_to_write_inline_data(struct address_space *mapping,
  				  struct inode *inode,
  				  loff_t pos, unsigned len,
  				  unsigned flags,
  				  struct page **pagep)
  {
  	int ret;
  	handle_t *handle;
  	struct page *page;
  	struct ext4_iloc iloc;
  
  	if (pos + len > ext4_get_max_inline_size(inode))
  		goto convert;
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret)
  		return ret;
  
  	/*
  	 * The possible write could happen in the inode,
  	 * so try to reserve the space in inode first.
  	 */
9924a92a8   Theodore Ts'o   ext4: pass contex...
654
  	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
f19d5870c   Tao Ma   ext4: add normal ...
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
  		handle = NULL;
  		goto out;
  	}
  
  	ret = ext4_prepare_inline_data(handle, inode, pos + len);
  	if (ret && ret != -ENOSPC)
  		goto out;
  
  	/* We don't have space in inline inode, so convert it to extent. */
  	if (ret == -ENOSPC) {
  		ext4_journal_stop(handle);
  		brelse(iloc.bh);
  		goto convert;
  	}
cdcbe750a   Theodore Ts'o   ext4: fix inline ...
671
672
673
  	ret = ext4_journal_get_write_access(handle, iloc.bh);
  	if (ret)
  		goto out;
f19d5870c   Tao Ma   ext4: add normal ...
674
675
676
677
678
679
680
681
682
683
684
685
686
  	flags |= AOP_FLAG_NOFS;
  
  	page = grab_cache_page_write_begin(mapping, 0, flags);
  	if (!page) {
  		ret = -ENOMEM;
  		goto out;
  	}
  
  	*pagep = page;
  	down_read(&EXT4_I(inode)->xattr_sem);
  	if (!ext4_has_inline_data(inode)) {
  		ret = 0;
  		unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
687
  		put_page(page);
f19d5870c   Tao Ma   ext4: add normal ...
688
689
690
691
692
  		goto out_up_read;
  	}
  
  	if (!PageUptodate(page)) {
  		ret = ext4_read_inline_page(inode, page);
92bb9b067   Maurizio Lombardi   ext4: missing unl...
693
694
695
  		if (ret < 0) {
  			unlock_page(page);
  			put_page(page);
f19d5870c   Tao Ma   ext4: add normal ...
696
  			goto out_up_read;
92bb9b067   Maurizio Lombardi   ext4: missing unl...
697
  		}
f19d5870c   Tao Ma   ext4: add normal ...
698
699
700
701
702
703
704
  	}
  
  	ret = 1;
  	handle = NULL;
  out_up_read:
  	up_read(&EXT4_I(inode)->xattr_sem);
  out:
cdcbe750a   Theodore Ts'o   ext4: fix inline ...
705
  	if (handle && (ret != 1))
f19d5870c   Tao Ma   ext4: add normal ...
706
707
708
709
710
711
712
713
714
715
716
  		ext4_journal_stop(handle);
  	brelse(iloc.bh);
  	return ret;
  convert:
  	return ext4_convert_inline_data_to_extent(mapping,
  						  inode, flags);
  }
  
  int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
  			       unsigned copied, struct page *page)
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
717
  	int ret, no_expand;
f19d5870c   Tao Ma   ext4: add normal ...
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  	void *kaddr;
  	struct ext4_iloc iloc;
  
  	if (unlikely(copied < len)) {
  		if (!PageUptodate(page)) {
  			copied = 0;
  			goto out;
  		}
  	}
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret) {
  		ext4_std_error(inode->i_sb, ret);
  		copied = 0;
  		goto out;
  	}
c755e2513   Theodore Ts'o   ext4: fix deadloc...
734
  	ext4_write_lock_xattr(inode, &no_expand);
f19d5870c   Tao Ma   ext4: add normal ...
735
736
737
738
739
740
741
742
  	BUG_ON(!ext4_has_inline_data(inode));
  
  	kaddr = kmap_atomic(page);
  	ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
  	kunmap_atomic(kaddr);
  	SetPageUptodate(page);
  	/* clear page dirty so that writepages wouldn't work for us. */
  	ClearPageDirty(page);
c755e2513   Theodore Ts'o   ext4: fix deadloc...
743
  	ext4_write_unlock_xattr(inode, &no_expand);
f19d5870c   Tao Ma   ext4: add normal ...
744
  	brelse(iloc.bh);
cdcbe750a   Theodore Ts'o   ext4: fix inline ...
745
  	mark_inode_dirty(inode);
f19d5870c   Tao Ma   ext4: add normal ...
746
747
748
  out:
  	return copied;
  }
3fdcfb668   Tao Ma   ext4: add journal...
749
750
751
752
753
  struct buffer_head *
  ext4_journalled_write_inline_data(struct inode *inode,
  				  unsigned len,
  				  struct page *page)
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
754
  	int ret, no_expand;
3fdcfb668   Tao Ma   ext4: add journal...
755
756
757
758
759
760
761
762
  	void *kaddr;
  	struct ext4_iloc iloc;
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret) {
  		ext4_std_error(inode->i_sb, ret);
  		return NULL;
  	}
c755e2513   Theodore Ts'o   ext4: fix deadloc...
763
  	ext4_write_lock_xattr(inode, &no_expand);
3fdcfb668   Tao Ma   ext4: add journal...
764
765
766
  	kaddr = kmap_atomic(page);
  	ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
  	kunmap_atomic(kaddr);
c755e2513   Theodore Ts'o   ext4: fix deadloc...
767
  	ext4_write_unlock_xattr(inode, &no_expand);
3fdcfb668   Tao Ma   ext4: add journal...
768
769
770
  
  	return iloc.bh;
  }
9c3569b50   Tao Ma   ext4: add delallo...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
  /*
   * Try to make the page cache and handle ready for the inline data case.
   * We can call this function in 2 cases:
   * 1. The inode is created and the first write exceeds inline size. We can
   *    clear the inode state safely.
   * 2. The inode has inline data, then we need to read the data, make it
   *    update and dirty so that ext4_da_writepages can handle it. We don't
   *    need to start the journal since the file's metatdata isn't changed now.
   */
  static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
  						 struct inode *inode,
  						 unsigned flags,
  						 void **fsdata)
  {
  	int ret = 0, inline_size;
  	struct page *page;
  
  	page = grab_cache_page_write_begin(mapping, 0, flags);
  	if (!page)
  		return -ENOMEM;
  
  	down_read(&EXT4_I(inode)->xattr_sem);
  	if (!ext4_has_inline_data(inode)) {
  		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  		goto out;
  	}
  
  	inline_size = ext4_get_inline_size(inode);
  
  	if (!PageUptodate(page)) {
  		ret = ext4_read_inline_page(inode, page);
  		if (ret < 0)
  			goto out;
  	}
  
  	ret = __block_write_begin(page, 0, inline_size,
  				  ext4_da_get_block_prep);
  	if (ret) {
50db71abc   Dmitry Monakhov   ext4: ext4_da_con...
809
810
  		up_read(&EXT4_I(inode)->xattr_sem);
  		unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
811
  		put_page(page);
9c3569b50   Tao Ma   ext4: add delallo...
812
  		ext4_truncate_failed_write(inode);
50db71abc   Dmitry Monakhov   ext4: ext4_da_con...
813
  		return ret;
9c3569b50   Tao Ma   ext4: add delallo...
814
815
816
817
818
819
820
821
822
823
824
  	}
  
  	SetPageDirty(page);
  	SetPageUptodate(page);
  	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  	*fsdata = (void *)CONVERT_INLINE_DATA;
  
  out:
  	up_read(&EXT4_I(inode)->xattr_sem);
  	if (page) {
  		unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
825
  		put_page(page);
9c3569b50   Tao Ma   ext4: add delallo...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
  	}
  	return ret;
  }
  
  /*
   * Prepare the write for the inline data.
   * If the the data can be written into the inode, we just read
   * the page and make it uptodate, and start the journal.
   * Otherwise read the page, makes it dirty so that it can be
   * handle in writepages(the i_disksize update is left to the
   * normal ext4_da_write_end).
   */
  int ext4_da_write_inline_data_begin(struct address_space *mapping,
  				    struct inode *inode,
  				    loff_t pos, unsigned len,
  				    unsigned flags,
  				    struct page **pagep,
  				    void **fsdata)
  {
  	int ret, inline_size;
  	handle_t *handle;
  	struct page *page;
  	struct ext4_iloc iloc;
13b63ba40   Lukas Czerner   ext4: initialize ...
849
  	int retries = 0;
9c3569b50   Tao Ma   ext4: add delallo...
850
851
852
853
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret)
  		return ret;
bc0ca9df3   Jan Kara   ext4: retry alloc...
854
  retry_journal:
9924a92a8   Theodore Ts'o   ext4: pass contex...
855
  	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
9c3569b50   Tao Ma   ext4: add delallo...
856
857
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
9c3569b50   Tao Ma   ext4: add delallo...
858
859
860
861
862
863
864
865
866
  		goto out;
  	}
  
  	inline_size = ext4_get_max_inline_size(inode);
  
  	ret = -ENOSPC;
  	if (inline_size >= pos + len) {
  		ret = ext4_prepare_inline_data(handle, inode, pos + len);
  		if (ret && ret != -ENOSPC)
52e447775   Jan Kara   ext4: standardize...
867
  			goto out_journal;
9c3569b50   Tao Ma   ext4: add delallo...
868
  	}
5cc28a9ea   Dmitry Monakhov   ext4: prevent fsr...
869
870
871
872
873
  	/*
  	 * We cannot recurse into the filesystem as the transaction
  	 * is already started.
  	 */
  	flags |= AOP_FLAG_NOFS;
9c3569b50   Tao Ma   ext4: add delallo...
874
  	if (ret == -ENOSPC) {
02945e49d   Theodore Ts'o   ext4: avoid runni...
875
  		ext4_journal_stop(handle);
9c3569b50   Tao Ma   ext4: add delallo...
876
877
878
879
  		ret = ext4_da_convert_inline_data_to_extent(mapping,
  							    inode,
  							    flags,
  							    fsdata);
bc0ca9df3   Jan Kara   ext4: retry alloc...
880
881
882
  		if (ret == -ENOSPC &&
  		    ext4_should_retry_alloc(inode->i_sb, &retries))
  			goto retry_journal;
9c3569b50   Tao Ma   ext4: add delallo...
883
884
  		goto out;
  	}
9c3569b50   Tao Ma   ext4: add delallo...
885
886
887
  	page = grab_cache_page_write_begin(mapping, 0, flags);
  	if (!page) {
  		ret = -ENOMEM;
52e447775   Jan Kara   ext4: standardize...
888
  		goto out_journal;
9c3569b50   Tao Ma   ext4: add delallo...
889
890
891
892
893
894
895
896
897
898
899
900
901
  	}
  
  	down_read(&EXT4_I(inode)->xattr_sem);
  	if (!ext4_has_inline_data(inode)) {
  		ret = 0;
  		goto out_release_page;
  	}
  
  	if (!PageUptodate(page)) {
  		ret = ext4_read_inline_page(inode, page);
  		if (ret < 0)
  			goto out_release_page;
  	}
cdcbe750a   Theodore Ts'o   ext4: fix inline ...
902
903
904
  	ret = ext4_journal_get_write_access(handle, iloc.bh);
  	if (ret)
  		goto out_release_page;
9c3569b50   Tao Ma   ext4: add delallo...
905
906
907
  
  	up_read(&EXT4_I(inode)->xattr_sem);
  	*pagep = page;
9c3569b50   Tao Ma   ext4: add delallo...
908
909
910
911
912
  	brelse(iloc.bh);
  	return 1;
  out_release_page:
  	up_read(&EXT4_I(inode)->xattr_sem);
  	unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
913
  	put_page(page);
52e447775   Jan Kara   ext4: standardize...
914
915
  out_journal:
  	ext4_journal_stop(handle);
9c3569b50   Tao Ma   ext4: add delallo...
916
  out:
9c3569b50   Tao Ma   ext4: add delallo...
917
918
919
920
921
922
923
924
  	brelse(iloc.bh);
  	return ret;
  }
  
  int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
  				  unsigned len, unsigned copied,
  				  struct page *page)
  {
eb5efbcb7   Theodore Ts'o   ext4: fix inline ...
925
  	int ret;
9c3569b50   Tao Ma   ext4: add delallo...
926

eb5efbcb7   Theodore Ts'o   ext4: fix inline ...
927
928
929
930
931
932
933
  	ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
  	if (ret < 0) {
  		unlock_page(page);
  		put_page(page);
  		return ret;
  	}
  	copied = ret;
9c3569b50   Tao Ma   ext4: add delallo...
934
935
936
937
938
939
940
941
  
  	/*
  	 * No need to use i_size_read() here, the i_size
  	 * cannot change under us because we hold i_mutex.
  	 *
  	 * But it's important to update i_size while still holding page lock:
  	 * page writeout could otherwise come in and zero beyond i_size.
  	 */
cdcbe750a   Theodore Ts'o   ext4: fix inline ...
942
  	if (pos+copied > inode->i_size)
9c3569b50   Tao Ma   ext4: add delallo...
943
  		i_size_write(inode, pos+copied);
9c3569b50   Tao Ma   ext4: add delallo...
944
  	unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
945
  	put_page(page);
9c3569b50   Tao Ma   ext4: add delallo...
946
947
948
949
950
951
952
  
  	/*
  	 * Don't mark the inode dirty under page lock. First, it unnecessarily
  	 * makes the holding time of page lock longer. Second, it forces lock
  	 * ordering of page lock and transaction start for journaling
  	 * filesystems.
  	 */
cdcbe750a   Theodore Ts'o   ext4: fix inline ...
953
  	mark_inode_dirty(inode);
9c3569b50   Tao Ma   ext4: add delallo...
954
955
956
  
  	return copied;
  }
f19d5870c   Tao Ma   ext4: add normal ...
957

3c47d5417   Tao Ma   ext4: let add_dir...
958
959
960
961
962
963
964
965
966
967
968
969
970
971
  #ifdef INLINE_DIR_DEBUG
  void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
  			  void *inline_start, int inline_size)
  {
  	int offset;
  	unsigned short de_len;
  	struct ext4_dir_entry_2 *de = inline_start;
  	void *dlimit = inline_start + inline_size;
  
  	trace_printk("inode %lu
  ", dir->i_ino);
  	offset = 0;
  	while ((void *)de < dlimit) {
  		de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
80cfb71e2   Rasmus Villemoes   ext4: fix transpo...
972
973
  		trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u
  ",
3c47d5417   Tao Ma   ext4: let add_dir...
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
  			     offset, de_len, de->name_len, de->name,
  			     de->name_len, le32_to_cpu(de->inode));
  		if (ext4_check_dir_entry(dir, NULL, de, bh,
  					 inline_start, inline_size, offset))
  			BUG();
  
  		offset += de_len;
  		de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  	}
  }
  #else
  #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
  #endif
  
  /*
   * Add a new entry into a inline dir.
   * It will return -ENOSPC if no space is available, and -EIO
   * and -EEXIST if directory entry already exists.
   */
  static int ext4_add_dirent_to_inline(handle_t *handle,
5b643f9ce   Theodore Ts'o   ext4 crypto: opti...
994
  				     struct ext4_filename *fname,
56a04915d   Theodore Ts'o   ext4 crypto: simp...
995
  				     struct inode *dir,
3c47d5417   Tao Ma   ext4: let add_dir...
996
997
998
999
  				     struct inode *inode,
  				     struct ext4_iloc *iloc,
  				     void *inline_start, int inline_size)
  {
3c47d5417   Tao Ma   ext4: let add_dir...
1000
1001
  	int		err;
  	struct ext4_dir_entry_2 *de;
5b643f9ce   Theodore Ts'o   ext4 crypto: opti...
1002
1003
  	err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
  				inline_size, fname, &de);
3c47d5417   Tao Ma   ext4: let add_dir...
1004
1005
  	if (err)
  		return err;
5d6012553   liang xie   ext4: add missing...
1006
  	BUFFER_TRACE(iloc->bh, "get_write_access");
3c47d5417   Tao Ma   ext4: let add_dir...
1007
1008
1009
  	err = ext4_journal_get_write_access(handle, iloc->bh);
  	if (err)
  		return err;
1bc0af600   Eric Biggers   ext4: trim return...
1010
  	ext4_insert_dentry(inode, de, inline_size, fname);
3c47d5417   Tao Ma   ext4: let add_dir...
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
  
  	ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
  
  	/*
  	 * XXX shouldn't update any times until successful
  	 * completion of syscall, but too many callers depend
  	 * on this.
  	 *
  	 * XXX similarly, too many callers depend on
  	 * ext4_new_inode() setting the times, but error
  	 * recovery deletes the inode, so the worst that can
  	 * happen is that the times are slightly out of date
  	 * and/or different from the directory change time.
  	 */
eeca7ea1b   Deepa Dinamani   ext4: use current...
1025
  	dir->i_mtime = dir->i_ctime = current_time(dir);
3c47d5417   Tao Ma   ext4: let add_dir...
1026
1027
  	ext4_update_dx_flag(dir);
  	dir->i_version++;
3c47d5417   Tao Ma   ext4: let add_dir...
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
  	return 1;
  }
  
  static void *ext4_get_inline_xattr_pos(struct inode *inode,
  				       struct ext4_iloc *iloc)
  {
  	struct ext4_xattr_entry *entry;
  	struct ext4_xattr_ibody_header *header;
  
  	BUG_ON(!EXT4_I(inode)->i_inline_off);
  
  	header = IHDR(inode, ext4_raw_inode(iloc));
  	entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
  					    EXT4_I(inode)->i_inline_off);
  
  	return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
  }
  
  /* Set the final de to cover the whole block. */
  static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
  {
  	struct ext4_dir_entry_2 *de, *prev_de;
  	void *limit;
  	int de_len;
  
  	de = (struct ext4_dir_entry_2 *)de_buf;
  	if (old_size) {
  		limit = de_buf + old_size;
  		do {
  			prev_de = de;
  			de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
  			de_buf += de_len;
  			de = (struct ext4_dir_entry_2 *)de_buf;
  		} while (de_buf < limit);
  
  		prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
  							old_size, new_size);
  	} else {
  		/* this is just created, so create an empty entry. */
  		de->inode = 0;
  		de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
  	}
  }
  
  static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
  				  struct ext4_iloc *iloc)
  {
  	int ret;
  	int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
  	int new_size = get_max_inline_xattr_value_size(dir, iloc);
  
  	if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
  		return -ENOSPC;
  
  	ret = ext4_update_inline_data(handle, dir,
  				      new_size + EXT4_MIN_INLINE_DATA_SIZE);
  	if (ret)
  		return ret;
  
  	ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
  			     EXT4_I(dir)->i_inline_size -
  						EXT4_MIN_INLINE_DATA_SIZE);
  	dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
  	return 0;
  }
  
  static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
  				     struct ext4_iloc *iloc,
  				     void *buf, int inline_size)
  {
  	ext4_create_inline_data(handle, inode, inline_size);
  	ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
  	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  }
  
  static int ext4_finish_convert_inline_dir(handle_t *handle,
  					  struct inode *inode,
  					  struct buffer_head *dir_block,
  					  void *buf,
  					  int inline_size)
  {
  	int err, csum_size = 0, header_size = 0;
  	struct ext4_dir_entry_2 *de;
  	struct ext4_dir_entry_tail *t;
  	void *target = dir_block->b_data;
  
  	/*
  	 * First create "." and ".." and then copy the dir information
  	 * back to the block.
  	 */
  	de = (struct ext4_dir_entry_2 *)target;
  	de = ext4_init_dot_dotdot(inode, de,
  		inode->i_sb->s_blocksize, csum_size,
  		le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
  	header_size = (void *)de - target;
  
  	memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
  		inline_size - EXT4_INLINE_DOTDOT_SIZE);
9aa5d32ba   Dmitry Monakhov   ext4: Replace ope...
1126
  	if (ext4_has_metadata_csum(inode->i_sb))
3c47d5417   Tao Ma   ext4: let add_dir...
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
  		csum_size = sizeof(struct ext4_dir_entry_tail);
  
  	inode->i_size = inode->i_sb->s_blocksize;
  	i_size_write(inode, inode->i_sb->s_blocksize);
  	EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  	ext4_update_final_de(dir_block->b_data,
  			inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
  			inode->i_sb->s_blocksize - csum_size);
  
  	if (csum_size) {
  		t = EXT4_DIRENT_TAIL(dir_block->b_data,
  				     inode->i_sb->s_blocksize);
  		initialize_dirent_tail(t, inode->i_sb->s_blocksize);
  	}
  	set_buffer_uptodate(dir_block);
  	err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  	if (err)
b9cf625d6   Eric Biggers   ext4: mark inode ...
1144
  		return err;
3c47d5417   Tao Ma   ext4: let add_dir...
1145
  	set_buffer_verified(dir_block);
b9cf625d6   Eric Biggers   ext4: mark inode ...
1146
  	return ext4_mark_inode_dirty(handle, inode);
3c47d5417   Tao Ma   ext4: let add_dir...
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
  }
  
  static int ext4_convert_inline_data_nolock(handle_t *handle,
  					   struct inode *inode,
  					   struct ext4_iloc *iloc)
  {
  	int error;
  	void *buf = NULL;
  	struct buffer_head *data_bh = NULL;
  	struct ext4_map_blocks map;
  	int inline_size;
  
  	inline_size = ext4_get_inline_size(inode);
  	buf = kmalloc(inline_size, GFP_NOFS);
  	if (!buf) {
  		error = -ENOMEM;
  		goto out;
  	}
  
  	error = ext4_read_inline_data(inode, buf, inline_size, iloc);
  	if (error < 0)
  		goto out;
40b163f1c   Darrick J. Wong   ext4: check inlin...
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
  	/*
  	 * Make sure the inline directory entries pass checks before we try to
  	 * convert them, so that we avoid touching stuff that needs fsck.
  	 */
  	if (S_ISDIR(inode->i_mode)) {
  		error = ext4_check_all_de(inode, iloc->bh,
  					buf + EXT4_INLINE_DOTDOT_SIZE,
  					inline_size - EXT4_INLINE_DOTDOT_SIZE);
  		if (error)
  			goto out;
  	}
3c47d5417   Tao Ma   ext4: let add_dir...
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
  	error = ext4_destroy_inline_data_nolock(handle, inode);
  	if (error)
  		goto out;
  
  	map.m_lblk = 0;
  	map.m_len = 1;
  	map.m_flags = 0;
  	error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
  	if (error < 0)
  		goto out_restore;
  	if (!(map.m_flags & EXT4_MAP_MAPPED)) {
  		error = -EIO;
  		goto out_restore;
  	}
  
  	data_bh = sb_getblk(inode->i_sb, map.m_pblk);
  	if (!data_bh) {
860d21e2c   Theodore Ts'o   ext4: return ENOM...
1197
  		error = -ENOMEM;
3c47d5417   Tao Ma   ext4: let add_dir...
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
  		goto out_restore;
  	}
  
  	lock_buffer(data_bh);
  	error = ext4_journal_get_create_access(handle, data_bh);
  	if (error) {
  		unlock_buffer(data_bh);
  		error = -EIO;
  		goto out_restore;
  	}
  	memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
  
  	if (!S_ISDIR(inode->i_mode)) {
  		memcpy(data_bh->b_data, buf, inline_size);
  		set_buffer_uptodate(data_bh);
  		error = ext4_handle_dirty_metadata(handle,
  						   inode, data_bh);
  	} else {
  		error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
  						       buf, inline_size);
  	}
  
  	unlock_buffer(data_bh);
  out_restore:
  	if (error)
  		ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
  
  out:
  	brelse(data_bh);
  	kfree(buf);
  	return error;
  }
  
  /*
   * Try to add the new entry to the inline data.
   * If succeeds, return 0. If not, extended the inline dir and copied data to
   * the new created block.
   */
5b643f9ce   Theodore Ts'o   ext4 crypto: opti...
1236
  int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
56a04915d   Theodore Ts'o   ext4 crypto: simp...
1237
  			      struct inode *dir, struct inode *inode)
3c47d5417   Tao Ma   ext4: let add_dir...
1238
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1239
  	int ret, inline_size, no_expand;
3c47d5417   Tao Ma   ext4: let add_dir...
1240
1241
  	void *inline_start;
  	struct ext4_iloc iloc;
3c47d5417   Tao Ma   ext4: let add_dir...
1242
1243
1244
1245
  
  	ret = ext4_get_inode_loc(dir, &iloc);
  	if (ret)
  		return ret;
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1246
  	ext4_write_lock_xattr(dir, &no_expand);
3c47d5417   Tao Ma   ext4: let add_dir...
1247
1248
1249
1250
1251
1252
  	if (!ext4_has_inline_data(dir))
  		goto out;
  
  	inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  						 EXT4_INLINE_DOTDOT_SIZE;
  	inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
56a04915d   Theodore Ts'o   ext4 crypto: simp...
1253
  	ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
3c47d5417   Tao Ma   ext4: let add_dir...
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
  					inline_start, inline_size);
  	if (ret != -ENOSPC)
  		goto out;
  
  	/* check whether it can be inserted to inline xattr space. */
  	inline_size = EXT4_I(dir)->i_inline_size -
  			EXT4_MIN_INLINE_DATA_SIZE;
  	if (!inline_size) {
  		/* Try to use the xattr space.*/
  		ret = ext4_update_inline_dir(handle, dir, &iloc);
  		if (ret && ret != -ENOSPC)
  			goto out;
  
  		inline_size = EXT4_I(dir)->i_inline_size -
  				EXT4_MIN_INLINE_DATA_SIZE;
  	}
  
  	if (inline_size) {
  		inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
56a04915d   Theodore Ts'o   ext4 crypto: simp...
1273
  		ret = ext4_add_dirent_to_inline(handle, fname, dir,
5b643f9ce   Theodore Ts'o   ext4 crypto: opti...
1274
1275
  						inode, &iloc, inline_start,
  						inline_size);
3c47d5417   Tao Ma   ext4: let add_dir...
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  
  		if (ret != -ENOSPC)
  			goto out;
  	}
  
  	/*
  	 * The inline space is filled up, so create a new block for it.
  	 * As the extent tree will be created, we have to save the inline
  	 * dir first.
  	 */
  	ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
  
  out:
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1289
  	ext4_write_unlock_xattr(dir, &no_expand);
b907f2d51   Theodore Ts'o   ext4: avoid calli...
1290
  	ext4_mark_inode_dirty(handle, dir);
3c47d5417   Tao Ma   ext4: let add_dir...
1291
1292
1293
  	brelse(iloc.bh);
  	return ret;
  }
8af0f0822   Tao Ma   ext4: fix readdir...
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
  /*
   * This function fills a red-black tree with information from an
   * inlined dir.  It returns the number directory entries loaded
   * into the tree.  If there is an error it is returned in err.
   */
  int htree_inlinedir_to_tree(struct file *dir_file,
  			    struct inode *dir, ext4_lblk_t block,
  			    struct dx_hash_info *hinfo,
  			    __u32 start_hash, __u32 start_minor_hash,
  			    int *has_inline_data)
  {
  	int err = 0, count = 0;
  	unsigned int parent_ino;
  	int pos;
  	struct ext4_dir_entry_2 *de;
  	struct inode *inode = file_inode(dir_file);
  	int ret, inline_size = 0;
  	struct ext4_iloc iloc;
  	void *dir_buf = NULL;
  	struct ext4_dir_entry_2 fake;
a7550b30a   Jaegeuk Kim   ext4 crypto: migr...
1314
  	struct fscrypt_str tmp_str;
8af0f0822   Tao Ma   ext4: fix readdir...
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret)
  		return ret;
  
  	down_read(&EXT4_I(inode)->xattr_sem);
  	if (!ext4_has_inline_data(inode)) {
  		up_read(&EXT4_I(inode)->xattr_sem);
  		*has_inline_data = 0;
  		goto out;
  	}
  
  	inline_size = ext4_get_inline_size(inode);
  	dir_buf = kmalloc(inline_size, GFP_NOFS);
  	if (!dir_buf) {
  		ret = -ENOMEM;
  		up_read(&EXT4_I(inode)->xattr_sem);
  		goto out;
  	}
  
  	ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  	up_read(&EXT4_I(inode)->xattr_sem);
  	if (ret < 0)
  		goto out;
  
  	pos = 0;
  	parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  	while (pos < inline_size) {
  		/*
  		 * As inlined dir doesn't store any information about '.' and
  		 * only the inode number of '..' is stored, we have to handle
  		 * them differently.
  		 */
  		if (pos == 0) {
  			fake.inode = cpu_to_le32(inode->i_ino);
  			fake.name_len = 1;
  			strcpy(fake.name, ".");
  			fake.rec_len = ext4_rec_len_to_disk(
  						EXT4_DIR_REC_LEN(fake.name_len),
  						inline_size);
  			ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  			de = &fake;
  			pos = EXT4_INLINE_DOTDOT_OFFSET;
  		} else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
  			fake.inode = cpu_to_le32(parent_ino);
  			fake.name_len = 2;
  			strcpy(fake.name, "..");
  			fake.rec_len = ext4_rec_len_to_disk(
  						EXT4_DIR_REC_LEN(fake.name_len),
  						inline_size);
  			ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  			de = &fake;
  			pos = EXT4_INLINE_DOTDOT_SIZE;
  		} else {
  			de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
  			pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
  			if (ext4_check_dir_entry(inode, dir_file, de,
  					 iloc.bh, dir_buf,
  					 inline_size, pos)) {
  				ret = count;
  				goto out;
  			}
  		}
  
  		ext4fs_dirhash(de->name, de->name_len, hinfo);
  		if ((hinfo->hash < start_hash) ||
  		    ((hinfo->hash == start_hash) &&
  		     (hinfo->minor_hash < start_minor_hash)))
  			continue;
  		if (de->inode == 0)
  			continue;
2f61830ae   Theodore Ts'o   ext4 crypto: teac...
1386
1387
1388
1389
  		tmp_str.name = de->name;
  		tmp_str.len = de->name_len;
  		err = ext4_htree_store_dirent(dir_file, hinfo->hash,
  					      hinfo->minor_hash, de, &tmp_str);
8af0f0822   Tao Ma   ext4: fix readdir...
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
  		if (err) {
  			count = err;
  			goto out;
  		}
  		count++;
  	}
  	ret = count;
  out:
  	kfree(dir_buf);
  	brelse(iloc.bh);
  	return ret;
  }
c4d8b0235   Tao Ma   ext4: fix readdir...
1402
1403
1404
1405
1406
1407
1408
1409
  /*
   * So this function is called when the volume is mkfsed with
   * dir_index disabled. In order to keep f_pos persistent
   * after we convert from an inlined dir to a blocked based,
   * we just pretend that we are a normal dir and return the
   * offset as if '.' and '..' really take place.
   *
   */
725bebb27   Al Viro   [readdir] convert...
1410
1411
  int ext4_read_inline_dir(struct file *file,
  			 struct dir_context *ctx,
65d165d93   Tao Ma   ext4: let ext4_re...
1412
1413
  			 int *has_inline_data)
  {
65d165d93   Tao Ma   ext4: let ext4_re...
1414
  	unsigned int offset, parent_ino;
725bebb27   Al Viro   [readdir] convert...
1415
  	int i;
65d165d93   Tao Ma   ext4: let ext4_re...
1416
1417
  	struct ext4_dir_entry_2 *de;
  	struct super_block *sb;
725bebb27   Al Viro   [readdir] convert...
1418
  	struct inode *inode = file_inode(file);
65d165d93   Tao Ma   ext4: let ext4_re...
1419
1420
1421
  	int ret, inline_size = 0;
  	struct ext4_iloc iloc;
  	void *dir_buf = NULL;
c4d8b0235   Tao Ma   ext4: fix readdir...
1422
  	int dotdot_offset, dotdot_size, extra_offset, extra_size;
65d165d93   Tao Ma   ext4: let ext4_re...
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret)
  		return ret;
  
  	down_read(&EXT4_I(inode)->xattr_sem);
  	if (!ext4_has_inline_data(inode)) {
  		up_read(&EXT4_I(inode)->xattr_sem);
  		*has_inline_data = 0;
  		goto out;
  	}
  
  	inline_size = ext4_get_inline_size(inode);
  	dir_buf = kmalloc(inline_size, GFP_NOFS);
  	if (!dir_buf) {
  		ret = -ENOMEM;
  		up_read(&EXT4_I(inode)->xattr_sem);
  		goto out;
  	}
  
  	ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  	up_read(&EXT4_I(inode)->xattr_sem);
  	if (ret < 0)
  		goto out;
48ffdab1c   BoxiLiu   ext4: change ext4...
1447
  	ret = 0;
65d165d93   Tao Ma   ext4: let ext4_re...
1448
  	sb = inode->i_sb;
65d165d93   Tao Ma   ext4: let ext4_re...
1449
  	parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
725bebb27   Al Viro   [readdir] convert...
1450
  	offset = ctx->pos;
c4d8b0235   Tao Ma   ext4: fix readdir...
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
  
  	/*
  	 * dotdot_offset and dotdot_size is the real offset and
  	 * size for ".." and "." if the dir is block based while
  	 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
  	 * So we will use extra_offset and extra_size to indicate them
  	 * during the inline dir iteration.
  	 */
  	dotdot_offset = EXT4_DIR_REC_LEN(1);
  	dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
  	extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
  	extra_size = extra_offset + inline_size;
65d165d93   Tao Ma   ext4: let ext4_re...
1463

725bebb27   Al Viro   [readdir] convert...
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
  	/*
  	 * If the version has changed since the last call to
  	 * readdir(2), then we might be pointing to an invalid
  	 * dirent right now.  Scan from the start of the inline
  	 * dir to make sure.
  	 */
  	if (file->f_version != inode->i_version) {
  		for (i = 0; i < extra_size && i < offset;) {
  			/*
  			 * "." is with offset 0 and
  			 * ".." is dotdot_offset.
  			 */
  			if (!i) {
  				i = dotdot_offset;
  				continue;
  			} else if (i == dotdot_offset) {
  				i = dotdot_size;
c4d8b0235   Tao Ma   ext4: fix readdir...
1481
1482
  				continue;
  			}
725bebb27   Al Viro   [readdir] convert...
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
  			/* for other entry, the real offset in
  			 * the buf has to be tuned accordingly.
  			 */
  			de = (struct ext4_dir_entry_2 *)
  				(dir_buf + i - extra_offset);
  			/* It's too expensive to do a full
  			 * dirent test each time round this
  			 * loop, but we do have to test at
  			 * least that it is non-zero.  A
  			 * failure will be detected in the
  			 * dirent test below. */
  			if (ext4_rec_len_from_disk(de->rec_len, extra_size)
  				< EXT4_DIR_REC_LEN(1))
  				break;
  			i += ext4_rec_len_from_disk(de->rec_len,
  						    extra_size);
  		}
  		offset = i;
  		ctx->pos = offset;
  		file->f_version = inode->i_version;
  	}
65d165d93   Tao Ma   ext4: let ext4_re...
1504

725bebb27   Al Viro   [readdir] convert...
1505
1506
1507
1508
1509
1510
1511
  	while (ctx->pos < extra_size) {
  		if (ctx->pos == 0) {
  			if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
  				goto out;
  			ctx->pos = dotdot_offset;
  			continue;
  		}
65d165d93   Tao Ma   ext4: let ext4_re...
1512

725bebb27   Al Viro   [readdir] convert...
1513
1514
1515
1516
1517
1518
  		if (ctx->pos == dotdot_offset) {
  			if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
  				goto out;
  			ctx->pos = dotdot_size;
  			continue;
  		}
65d165d93   Tao Ma   ext4: let ext4_re...
1519

725bebb27   Al Viro   [readdir] convert...
1520
1521
1522
1523
1524
1525
1526
1527
1528
  		de = (struct ext4_dir_entry_2 *)
  			(dir_buf + ctx->pos - extra_offset);
  		if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
  					 extra_size, ctx->pos))
  			goto out;
  		if (le32_to_cpu(de->inode)) {
  			if (!dir_emit(ctx, de->name, de->name_len,
  				      le32_to_cpu(de->inode),
  				      get_dtype(sb, de->file_type)))
65d165d93   Tao Ma   ext4: let ext4_re...
1529
  				goto out;
65d165d93   Tao Ma   ext4: let ext4_re...
1530
  		}
725bebb27   Al Viro   [readdir] convert...
1531
  		ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
65d165d93   Tao Ma   ext4: let ext4_re...
1532
1533
1534
1535
1536
1537
  	}
  out:
  	kfree(dir_buf);
  	brelse(iloc.bh);
  	return ret;
  }
32f7f22c0   Tao Ma   ext4: let ext4_re...
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
  struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
  					struct ext4_dir_entry_2 **parent_de,
  					int *retval)
  {
  	struct ext4_iloc iloc;
  
  	*retval = ext4_get_inode_loc(inode, &iloc);
  	if (*retval)
  		return NULL;
  
  	*parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  
  	return iloc.bh;
  }
3c47d5417   Tao Ma   ext4: let add_dir...
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
  /*
   * Try to create the inline data for the new dir.
   * If it succeeds, return 0, otherwise return the error.
   * In case of ENOSPC, the caller should create the normal disk layout dir.
   */
  int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
  			       struct inode *inode)
  {
  	int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  	struct ext4_iloc iloc;
  	struct ext4_dir_entry_2 *de;
  
  	ret = ext4_get_inode_loc(inode, &iloc);
  	if (ret)
  		return ret;
  
  	ret = ext4_prepare_inline_data(handle, inode, inline_size);
  	if (ret)
  		goto out;
  
  	/*
  	 * For inline dir, we only save the inode information for the ".."
  	 * and create a fake dentry to cover the left space.
  	 */
  	de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  	de->inode = cpu_to_le32(parent->i_ino);
  	de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
  	de->inode = 0;
  	de->rec_len = ext4_rec_len_to_disk(
  				inline_size - EXT4_INLINE_DOTDOT_SIZE,
  				inline_size);
  	set_nlink(inode, 2);
  	inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
  out:
  	brelse(iloc.bh);
  	return ret;
  }
e8e948e78   Tao Ma   ext4: let ext4_fi...
1589
  struct buffer_head *ext4_find_inline_entry(struct inode *dir,
5b643f9ce   Theodore Ts'o   ext4 crypto: opti...
1590
  					struct ext4_filename *fname,
e8e948e78   Tao Ma   ext4: let ext4_fi...
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
  					struct ext4_dir_entry_2 **res_dir,
  					int *has_inline_data)
  {
  	int ret;
  	struct ext4_iloc iloc;
  	void *inline_start;
  	int inline_size;
  
  	if (ext4_get_inode_loc(dir, &iloc))
  		return NULL;
  
  	down_read(&EXT4_I(dir)->xattr_sem);
  	if (!ext4_has_inline_data(dir)) {
  		*has_inline_data = 0;
  		goto out;
  	}
  
  	inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  						EXT4_INLINE_DOTDOT_SIZE;
  	inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
5b643f9ce   Theodore Ts'o   ext4 crypto: opti...
1611
  	ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
d6b975504   Eric Biggers   ext4: remove unus...
1612
  			      dir, fname, 0, res_dir);
e8e948e78   Tao Ma   ext4: let ext4_fi...
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
  	if (ret == 1)
  		goto out_find;
  	if (ret < 0)
  		goto out;
  
  	if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
  		goto out;
  
  	inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  	inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
5b643f9ce   Theodore Ts'o   ext4 crypto: opti...
1623
  	ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
d6b975504   Eric Biggers   ext4: remove unus...
1624
  			      dir, fname, 0, res_dir);
e8e948e78   Tao Ma   ext4: let ext4_fi...
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
  	if (ret == 1)
  		goto out_find;
  
  out:
  	brelse(iloc.bh);
  	iloc.bh = NULL;
  out_find:
  	up_read(&EXT4_I(dir)->xattr_sem);
  	return iloc.bh;
  }
9f40fe546   Tao Ma   ext4: let ext4_de...
1635
1636
1637
1638
1639
1640
  int ext4_delete_inline_entry(handle_t *handle,
  			     struct inode *dir,
  			     struct ext4_dir_entry_2 *de_del,
  			     struct buffer_head *bh,
  			     int *has_inline_data)
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1641
  	int err, inline_size, no_expand;
9f40fe546   Tao Ma   ext4: let ext4_de...
1642
1643
1644
1645
1646
1647
  	struct ext4_iloc iloc;
  	void *inline_start;
  
  	err = ext4_get_inode_loc(dir, &iloc);
  	if (err)
  		return err;
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1648
  	ext4_write_lock_xattr(dir, &no_expand);
9f40fe546   Tao Ma   ext4: let ext4_de...
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
  	if (!ext4_has_inline_data(dir)) {
  		*has_inline_data = 0;
  		goto out;
  	}
  
  	if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
  		EXT4_MIN_INLINE_DATA_SIZE) {
  		inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  					EXT4_INLINE_DOTDOT_SIZE;
  		inline_size = EXT4_MIN_INLINE_DATA_SIZE -
  				EXT4_INLINE_DOTDOT_SIZE;
  	} else {
  		inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  		inline_size = ext4_get_inline_size(dir) -
  				EXT4_MIN_INLINE_DATA_SIZE;
  	}
5d6012553   liang xie   ext4: add missing...
1665
  	BUFFER_TRACE(bh, "get_write_access");
9f40fe546   Tao Ma   ext4: let ext4_de...
1666
1667
1668
1669
1670
1671
1672
1673
  	err = ext4_journal_get_write_access(handle, bh);
  	if (err)
  		goto out;
  
  	err = ext4_generic_delete_entry(handle, dir, de_del, bh,
  					inline_start, inline_size, 0);
  	if (err)
  		goto out;
9f40fe546   Tao Ma   ext4: let ext4_de...
1674
1675
  	ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
  out:
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1676
  	ext4_write_unlock_xattr(dir, &no_expand);
b907f2d51   Theodore Ts'o   ext4: avoid calli...
1677
1678
  	if (likely(err == 0))
  		err = ext4_mark_inode_dirty(handle, dir);
9f40fe546   Tao Ma   ext4: let ext4_de...
1679
1680
1681
1682
1683
  	brelse(iloc.bh);
  	if (err != -ENOENT)
  		ext4_std_error(dir->i_sb, err);
  	return err;
  }
61f86638d   Tao Ma   ext4: let empty_d...
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
  /*
   * Get the inline dentry at offset.
   */
  static inline struct ext4_dir_entry_2 *
  ext4_get_inline_entry(struct inode *inode,
  		      struct ext4_iloc *iloc,
  		      unsigned int offset,
  		      void **inline_start,
  		      int *inline_size)
  {
  	void *inline_pos;
  
  	BUG_ON(offset > ext4_get_inline_size(inode));
  
  	if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
  		inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
  		*inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  	} else {
  		inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
  		offset -= EXT4_MIN_INLINE_DATA_SIZE;
  		*inline_size = ext4_get_inline_size(inode) -
  				EXT4_MIN_INLINE_DATA_SIZE;
  	}
  
  	if (inline_start)
  		*inline_start = inline_pos;
  	return (struct ext4_dir_entry_2 *)(inline_pos + offset);
  }
a7550b30a   Jaegeuk Kim   ext4 crypto: migr...
1712
  bool empty_inline_dir(struct inode *dir, int *has_inline_data)
61f86638d   Tao Ma   ext4: let empty_d...
1713
1714
1715
  {
  	int err, inline_size;
  	struct ext4_iloc iloc;
3f9eafe87   Theodore Ts'o   ext4: avoid divid...
1716
  	size_t inline_len;
61f86638d   Tao Ma   ext4: let empty_d...
1717
1718
1719
  	void *inline_pos;
  	unsigned int offset;
  	struct ext4_dir_entry_2 *de;
a7550b30a   Jaegeuk Kim   ext4 crypto: migr...
1720
  	bool ret = true;
61f86638d   Tao Ma   ext4: let empty_d...
1721
1722
1723
1724
1725
  
  	err = ext4_get_inode_loc(dir, &iloc);
  	if (err) {
  		EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
  				 err, dir->i_ino);
a7550b30a   Jaegeuk Kim   ext4 crypto: migr...
1726
  		return true;
61f86638d   Tao Ma   ext4: let empty_d...
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
  	}
  
  	down_read(&EXT4_I(dir)->xattr_sem);
  	if (!ext4_has_inline_data(dir)) {
  		*has_inline_data = 0;
  		goto out;
  	}
  
  	de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  	if (!le32_to_cpu(de->inode)) {
  		ext4_warning(dir->i_sb,
  			     "bad inline directory (dir #%lu) - no `..'",
  			     dir->i_ino);
a7550b30a   Jaegeuk Kim   ext4 crypto: migr...
1740
  		ret = true;
61f86638d   Tao Ma   ext4: let empty_d...
1741
1742
  		goto out;
  	}
3f9eafe87   Theodore Ts'o   ext4: avoid divid...
1743
  	inline_len = ext4_get_inline_size(dir);
61f86638d   Tao Ma   ext4: let empty_d...
1744
  	offset = EXT4_INLINE_DOTDOT_SIZE;
3f9eafe87   Theodore Ts'o   ext4: avoid divid...
1745
  	while (offset < inline_len) {
61f86638d   Tao Ma   ext4: let empty_d...
1746
1747
1748
1749
1750
1751
1752
1753
  		de = ext4_get_inline_entry(dir, &iloc, offset,
  					   &inline_pos, &inline_size);
  		if (ext4_check_dir_entry(dir, NULL, de,
  					 iloc.bh, inline_pos,
  					 inline_size, offset)) {
  			ext4_warning(dir->i_sb,
  				     "bad inline directory (dir #%lu) - "
  				     "inode %u, rec_len %u, name_len %d"
8d2ae1cbe   Jakub Wilk   ext4: remove trai...
1754
  				     "inline size %d",
61f86638d   Tao Ma   ext4: let empty_d...
1755
1756
1757
  				     dir->i_ino, le32_to_cpu(de->inode),
  				     le16_to_cpu(de->rec_len), de->name_len,
  				     inline_size);
a7550b30a   Jaegeuk Kim   ext4 crypto: migr...
1758
  			ret = true;
61f86638d   Tao Ma   ext4: let empty_d...
1759
1760
1761
  			goto out;
  		}
  		if (le32_to_cpu(de->inode)) {
a7550b30a   Jaegeuk Kim   ext4 crypto: migr...
1762
  			ret = false;
61f86638d   Tao Ma   ext4: let empty_d...
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
  			goto out;
  		}
  		offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
  	}
  
  out:
  	up_read(&EXT4_I(dir)->xattr_sem);
  	brelse(iloc.bh);
  	return ret;
  }
67cf5b09a   Tao Ma   ext4: add the bas...
1773
1774
  int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1775
  	int ret, no_expand;
67cf5b09a   Tao Ma   ext4: add the bas...
1776

c755e2513   Theodore Ts'o   ext4: fix deadloc...
1777
  	ext4_write_lock_xattr(inode, &no_expand);
67cf5b09a   Tao Ma   ext4: add the bas...
1778
  	ret = ext4_destroy_inline_data_nolock(handle, inode);
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1779
  	ext4_write_unlock_xattr(inode, &no_expand);
67cf5b09a   Tao Ma   ext4: add the bas...
1780
1781
1782
  
  	return ret;
  }
941919856   Tao Ma   ext4: let fiemap ...
1783
1784
1785
  
  int ext4_inline_data_fiemap(struct inode *inode,
  			    struct fiemap_extent_info *fieinfo,
d952d69e2   Dmitry Monakhov   ext4: ext4_inline...
1786
  			    int *has_inline, __u64 start, __u64 len)
941919856   Tao Ma   ext4: let fiemap ...
1787
1788
  {
  	__u64 physical = 0;
d952d69e2   Dmitry Monakhov   ext4: ext4_inline...
1789
1790
1791
  	__u64 inline_len;
  	__u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
  		FIEMAP_EXTENT_LAST;
941919856   Tao Ma   ext4: let fiemap ...
1792
1793
1794
1795
1796
1797
1798
1799
  	int error = 0;
  	struct ext4_iloc iloc;
  
  	down_read(&EXT4_I(inode)->xattr_sem);
  	if (!ext4_has_inline_data(inode)) {
  		*has_inline = 0;
  		goto out;
  	}
d952d69e2   Dmitry Monakhov   ext4: ext4_inline...
1800
1801
1802
1803
1804
1805
1806
  	inline_len = min_t(size_t, ext4_get_inline_size(inode),
  			   i_size_read(inode));
  	if (start >= inline_len)
  		goto out;
  	if (start + len < inline_len)
  		inline_len = start + len;
  	inline_len -= start;
941919856   Tao Ma   ext4: let fiemap ...
1807
1808
1809
1810
  
  	error = ext4_get_inode_loc(inode, &iloc);
  	if (error)
  		goto out;
eaf379372   Jan Kara   ext4: fix data of...
1811
  	physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
941919856   Tao Ma   ext4: let fiemap ...
1812
1813
  	physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
  	physical += offsetof(struct ext4_inode, i_block);
941919856   Tao Ma   ext4: let fiemap ...
1814

941919856   Tao Ma   ext4: let fiemap ...
1815
1816
1817
  	brelse(iloc.bh);
  out:
  	up_read(&EXT4_I(inode)->xattr_sem);
b4727299f   Theodore Ts'o   ext4: fix a poten...
1818
1819
1820
  	if (physical)
  		error = fiemap_fill_next_extent(fieinfo, start, physical,
  						inline_len, flags);
941919856   Tao Ma   ext4: let fiemap ...
1821
1822
  	return (error < 0 ? error : 0);
  }
0d812f77b   Tao Ma   ext4: evict inlin...
1823

01daf9452   Theodore Ts'o   ext4: propagate e...
1824
  int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
aef1c8513   Tao Ma   ext4: let ext4_tr...
1825
1826
  {
  	handle_t *handle;
01daf9452   Theodore Ts'o   ext4: propagate e...
1827
  	int inline_size, value_len, needed_blocks, no_expand, err = 0;
aef1c8513   Tao Ma   ext4: let ext4_tr...
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
  	size_t i_size;
  	void *value = NULL;
  	struct ext4_xattr_ibody_find is = {
  		.s = { .not_found = -ENODATA, },
  	};
  	struct ext4_xattr_info i = {
  		.name_index = EXT4_XATTR_INDEX_SYSTEM,
  		.name = EXT4_XATTR_SYSTEM_DATA,
  	};
  
  
  	needed_blocks = ext4_writepage_trans_blocks(inode);
9924a92a8   Theodore Ts'o   ext4: pass contex...
1840
  	handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
aef1c8513   Tao Ma   ext4: let ext4_tr...
1841
  	if (IS_ERR(handle))
01daf9452   Theodore Ts'o   ext4: propagate e...
1842
  		return PTR_ERR(handle);
aef1c8513   Tao Ma   ext4: let ext4_tr...
1843

c755e2513   Theodore Ts'o   ext4: fix deadloc...
1844
  	ext4_write_lock_xattr(inode, &no_expand);
aef1c8513   Tao Ma   ext4: let ext4_tr...
1845
1846
1847
  	if (!ext4_has_inline_data(inode)) {
  		*has_inline = 0;
  		ext4_journal_stop(handle);
01daf9452   Theodore Ts'o   ext4: propagate e...
1848
  		return 0;
aef1c8513   Tao Ma   ext4: let ext4_tr...
1849
  	}
01daf9452   Theodore Ts'o   ext4: propagate e...
1850
  	if ((err = ext4_orphan_add(handle, inode)) != 0)
aef1c8513   Tao Ma   ext4: let ext4_tr...
1851
  		goto out;
01daf9452   Theodore Ts'o   ext4: propagate e...
1852
  	if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
aef1c8513   Tao Ma   ext4: let ext4_tr...
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
  		goto out;
  
  	down_write(&EXT4_I(inode)->i_data_sem);
  	i_size = inode->i_size;
  	inline_size = ext4_get_inline_size(inode);
  	EXT4_I(inode)->i_disksize = i_size;
  
  	if (i_size < inline_size) {
  		/* Clear the content in the xattr space. */
  		if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
01daf9452   Theodore Ts'o   ext4: propagate e...
1863
  			if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
aef1c8513   Tao Ma   ext4: let ext4_tr...
1864
1865
1866
1867
1868
1869
  				goto out_error;
  
  			BUG_ON(is.s.not_found);
  
  			value_len = le32_to_cpu(is.s.here->e_value_size);
  			value = kmalloc(value_len, GFP_NOFS);
01daf9452   Theodore Ts'o   ext4: propagate e...
1870
1871
  			if (!value) {
  				err = -ENOMEM;
aef1c8513   Tao Ma   ext4: let ext4_tr...
1872
  				goto out_error;
01daf9452   Theodore Ts'o   ext4: propagate e...
1873
  			}
aef1c8513   Tao Ma   ext4: let ext4_tr...
1874

01daf9452   Theodore Ts'o   ext4: propagate e...
1875
1876
1877
  			err = ext4_xattr_ibody_get(inode, i.name_index,
  						   i.name, value, value_len);
  			if (err <= 0)
aef1c8513   Tao Ma   ext4: let ext4_tr...
1878
1879
1880
1881
1882
  				goto out_error;
  
  			i.value = value;
  			i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
  					i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
01daf9452   Theodore Ts'o   ext4: propagate e...
1883
1884
1885
  			err = ext4_xattr_ibody_inline_set(handle, inode,
  							  &i, &is);
  			if (err)
aef1c8513   Tao Ma   ext4: let ext4_tr...
1886
1887
1888
1889
  				goto out_error;
  		}
  
  		/* Clear the content within i_blocks. */
09c455aaa   Theodore Ts'o   ext4: avoid clear...
1890
1891
1892
1893
1894
  		if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
  			void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
  			memset(p + i_size, 0,
  			       EXT4_MIN_INLINE_DATA_SIZE - i_size);
  		}
aef1c8513   Tao Ma   ext4: let ext4_tr...
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
  
  		EXT4_I(inode)->i_inline_size = i_size <
  					EXT4_MIN_INLINE_DATA_SIZE ?
  					EXT4_MIN_INLINE_DATA_SIZE : i_size;
  	}
  
  out_error:
  	up_write(&EXT4_I(inode)->i_data_sem);
  out:
  	brelse(is.iloc.bh);
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1905
  	ext4_write_unlock_xattr(inode, &no_expand);
aef1c8513   Tao Ma   ext4: let ext4_tr...
1906
1907
1908
  	kfree(value);
  	if (inode->i_nlink)
  		ext4_orphan_del(handle, inode);
01daf9452   Theodore Ts'o   ext4: propagate e...
1909
1910
1911
1912
1913
1914
  	if (err == 0) {
  		inode->i_mtime = inode->i_ctime = current_time(inode);
  		err = ext4_mark_inode_dirty(handle, inode);
  		if (IS_SYNC(inode))
  			ext4_handle_sync(handle);
  	}
aef1c8513   Tao Ma   ext4: let ext4_tr...
1915
  	ext4_journal_stop(handle);
01daf9452   Theodore Ts'o   ext4: propagate e...
1916
  	return err;
aef1c8513   Tao Ma   ext4: let ext4_tr...
1917
  }
0c8d414f1   Tao Ma   ext4: let falloca...
1918
1919
1920
  
  int ext4_convert_inline_data(struct inode *inode)
  {
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1921
  	int error, needed_blocks, no_expand;
0c8d414f1   Tao Ma   ext4: let falloca...
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
  	handle_t *handle;
  	struct ext4_iloc iloc;
  
  	if (!ext4_has_inline_data(inode)) {
  		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  		return 0;
  	}
  
  	needed_blocks = ext4_writepage_trans_blocks(inode);
  
  	iloc.bh = NULL;
  	error = ext4_get_inode_loc(inode, &iloc);
  	if (error)
  		return error;
9924a92a8   Theodore Ts'o   ext4: pass contex...
1936
  	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
0c8d414f1   Tao Ma   ext4: let falloca...
1937
1938
1939
1940
  	if (IS_ERR(handle)) {
  		error = PTR_ERR(handle);
  		goto out_free;
  	}
c755e2513   Theodore Ts'o   ext4: fix deadloc...
1941
1942
1943
1944
  	ext4_write_lock_xattr(inode, &no_expand);
  	if (ext4_has_inline_data(inode))
  		error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
  	ext4_write_unlock_xattr(inode, &no_expand);
0c8d414f1   Tao Ma   ext4: let falloca...
1945
1946
1947
1948
1949
  	ext4_journal_stop(handle);
  out_free:
  	brelse(iloc.bh);
  	return error;
  }