xattr.c 81.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 * linux/fs/ext4/xattr.c
4
5
6
7
 *
 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
 *
 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
8
 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 * Extended attributes for symlinks and special files added per
 *  suggestion of Luka Renko <luka.renko@hermes.si>.
 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
 *  Red Hat Inc.
 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
 *  and Andreas Gruenbacher <agruen@suse.de>.
 */

/*
 * Extended attributes are stored directly in inodes (on file systems with
 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
 * field contains the block number if an inode uses an additional block. All
 * attributes must fit in the inode and one additional block. Blocks that
 * contain the identical set of attributes may be shared among several inodes.
 * Identical blocks are detected by keeping a cache of blocks that have
 * recently been accessed.
 *
 * The attributes in inodes and on blocks have a different header; the entries
 * are stored in the same format:
 *
 *   +------------------+
 *   | header           |
 *   | entry 1          | |
 *   | entry 2          | | growing downwards
 *   | entry 3          | v
 *   | four null bytes  |
 *   | . . .            |
 *   | value 1          | ^
 *   | value 3          | | growing upwards
 *   | value 2          | |
 *   +------------------+
 *
 * The header is followed by multiple entry descriptors. In disk blocks, the
 * entry descriptors are kept sorted. In inodes, they are unsorted. The
 * attribute values are aligned to the end of the block in no specific order.
 *
 * Locking strategy
 * ----------------
47
 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
48
49
50
51
52
53
54
55
56
 * EA blocks are only changed if they are exclusive to an inode, so
 * holding xattr_sem also means that nothing but the EA block's reference
 * count can change. Multiple writers to the same block are synchronized
 * by the buffer lock.
 */

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
Jan Kara's avatar
Jan Kara committed
57
#include <linux/mbcache.h>
58
#include <linux/quotaops.h>
59
#include <linux/iversion.h>
60
61
#include "ext4_jbd2.h"
#include "ext4.h"
62
63
64
#include "xattr.h"
#include "acl.h"

65
#ifdef EXT4_XATTR_DEBUG
66
67
68
69
70
71
# define ea_idebug(inode, fmt, ...)					\
	printk(KERN_DEBUG "inode %s:%lu: " fmt "\n",			\
	       inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
# define ea_bdebug(bh, fmt, ...)					\
	printk(KERN_DEBUG "block %pg:%lu: " fmt "\n",			\
	       bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
72
#else
73
74
# define ea_idebug(inode, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
# define ea_bdebug(bh, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
75
76
#endif

77
78
79
80
81
static void ext4_xattr_block_cache_insert(struct mb_cache *,
					  struct buffer_head *);
static struct buffer_head *
ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
			    struct mb_cache_entry **);
82
83
static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
				    size_t value_count);
84
static void ext4_xattr_rehash(struct ext4_xattr_header *);
85

86
static const struct xattr_handler * const ext4_xattr_handler_map[] = {
87
	[EXT4_XATTR_INDEX_USER]		     = &ext4_xattr_user_handler,
Theodore Ts'o's avatar
Theodore Ts'o committed
88
#ifdef CONFIG_EXT4_FS_POSIX_ACL
89
90
	[EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &posix_acl_access_xattr_handler,
	[EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
91
#endif
92
	[EXT4_XATTR_INDEX_TRUSTED]	     = &ext4_xattr_trusted_handler,
Theodore Ts'o's avatar
Theodore Ts'o committed
93
#ifdef CONFIG_EXT4_FS_SECURITY
94
	[EXT4_XATTR_INDEX_SECURITY]	     = &ext4_xattr_security_handler,
95
96
97
#endif
};

98
const struct xattr_handler *ext4_xattr_handlers[] = {
99
100
	&ext4_xattr_user_handler,
	&ext4_xattr_trusted_handler,
Theodore Ts'o's avatar
Theodore Ts'o committed
101
#ifdef CONFIG_EXT4_FS_POSIX_ACL
102
103
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
104
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
105
#ifdef CONFIG_EXT4_FS_SECURITY
106
	&ext4_xattr_security_handler,
107
108
109
110
#endif
	NULL
};

111
112
#define EA_BLOCK_CACHE(inode)	(((struct ext4_sb_info *) \
				inode->i_sb->s_fs_info)->s_ea_block_cache)
113

114
115
116
#define EA_INODE_CACHE(inode)	(((struct ext4_sb_info *) \
				inode->i_sb->s_fs_info)->s_ea_inode_cache)

117
118
119
120
static int
ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
			struct inode *inode);

121
122
123
124
125
126
127
#ifdef CONFIG_LOCKDEP
void ext4_xattr_inode_set_class(struct inode *ea_inode)
{
	lockdep_set_subclass(&ea_inode->i_rwsem, 1);
}
#endif

128
129
130
131
132
static __le32 ext4_xattr_block_csum(struct inode *inode,
				    sector_t block_nr,
				    struct ext4_xattr_header *hdr)
{
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
133
134
	__u32 csum;
	__le64 dsk_block_nr = cpu_to_le64(block_nr);
135
136
	__u32 dummy_csum = 0;
	int offset = offsetof(struct ext4_xattr_header, h_checksum);
137

138
139
	csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
			   sizeof(dsk_block_nr));
140
141
142
143
144
	csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
	csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
	offset += sizeof(dummy_csum);
	csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
			   EXT4_BLOCK_SIZE(inode->i_sb) - offset);
145

146
147
148
149
	return cpu_to_le32(csum);
}

static int ext4_xattr_block_csum_verify(struct inode *inode,
150
					struct buffer_head *bh)
151
{
152
153
	struct ext4_xattr_header *hdr = BHDR(bh);
	int ret = 1;
154

155
156
157
158
159
160
161
	if (ext4_has_metadata_csum(inode->i_sb)) {
		lock_buffer(bh);
		ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
							bh->b_blocknr, hdr));
		unlock_buffer(bh);
	}
	return ret;
162
163
}

164
165
static void ext4_xattr_block_csum_set(struct inode *inode,
				      struct buffer_head *bh)
166
{
167
168
169
	if (ext4_has_metadata_csum(inode->i_sb))
		BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
						bh->b_blocknr, BHDR(bh));
170
171
}

172
static inline const struct xattr_handler *
173
ext4_xattr_handler(int name_index)
174
{
175
	const struct xattr_handler *handler = NULL;
176

177
178
	if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
		handler = ext4_xattr_handler_map[name_index];
179
180
181
182
	return handler;
}

static int
183
184
ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
			 void *value_start)
185
{
186
187
	struct ext4_xattr_entry *e = entry;

188
	/* Find the end of the names list */
189
190
	while (!IS_LAST_ENTRY(e)) {
		struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
191
		if ((void *)next >= end)
192
			return -EFSCORRUPTED;
193
194
		if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
			return -EFSCORRUPTED;
195
		e = next;
196
	}
197

198
	/* Check the values */
199
	while (!IS_LAST_ENTRY(entry)) {
200
201
		u32 size = le32_to_cpu(entry->e_value_size);

202
		if (size > EXT4_XATTR_SIZE_MAX)
203
204
205
			return -EFSCORRUPTED;

		if (size != 0 && entry->e_value_inum == 0) {
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
			u16 offs = le16_to_cpu(entry->e_value_offs);
			void *value;

			/*
			 * The value cannot overlap the names, and the value
			 * with padding cannot extend beyond 'end'.  Check both
			 * the padded and unpadded sizes, since the size may
			 * overflow to 0 when adding padding.
			 */
			if (offs > end - value_start)
				return -EFSCORRUPTED;
			value = value_start + offs;
			if (value < (void *)e + sizeof(u32) ||
			    size > end - value ||
			    EXT4_XATTR_SIZE(size) > end - value)
				return -EFSCORRUPTED;
		}
223
224
225
		entry = EXT4_XATTR_NEXT(entry);
	}

226
227
228
229
	return 0;
}

static inline int
230
231
__ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
			 const char *function, unsigned int line)
232
{
233
	int error = -EFSCORRUPTED;
234

235
	if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
236
	    BHDR(bh)->h_blocks != cpu_to_le32(1))
237
		goto errout;
238
239
240
	if (buffer_verified(bh))
		return 0;

241
	error = -EFSBADCRC;
242
	if (!ext4_xattr_block_csum_verify(inode, bh))
243
		goto errout;
244
245
	error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
					 bh->b_data);
246
247
248
249
250
251
errout:
	if (error)
		__ext4_error_inode(inode, function, line, 0,
				   "corrupted xattr block %llu",
				   (unsigned long long) bh->b_blocknr);
	else
252
253
		set_buffer_verified(bh);
	return error;
254
255
}

256
257
258
259
#define ext4_xattr_check_block(inode, bh) \
	__ext4_xattr_check_block((inode), (bh),  __func__, __LINE__)


260
261
262
263
264
265
static int
__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
			 void *end, const char *function, unsigned int line)
{
	int error = -EFSCORRUPTED;

266
	if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
267
	    (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
268
		goto errout;
269
	error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
270
271
272
273
274
275
276
277
278
279
errout:
	if (error)
		__ext4_error_inode(inode, function, line, 0,
				   "corrupted in-inode xattr");
	return error;
}

#define xattr_check_inode(inode, header, end) \
	__xattr_check_inode((inode), (header), (end), __func__, __LINE__)

280
static int
281
282
xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
		 void *end, int name_index, const char *name, int sorted)
283
{
284
	struct ext4_xattr_entry *entry, *next;
285
286
287
288
289
290
	size_t name_len;
	int cmp = 1;

	if (name == NULL)
		return -EINVAL;
	name_len = strlen(name);
291
292
293
294
295
296
	for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
		next = EXT4_XATTR_NEXT(entry);
		if ((void *) next >= end) {
			EXT4_ERROR_INODE(inode, "corrupted xattr entries");
			return -EFSCORRUPTED;
		}
297
298
299
300
301
302
303
304
305
306
307
308
		cmp = name_index - entry->e_name_index;
		if (!cmp)
			cmp = name_len - entry->e_name_len;
		if (!cmp)
			cmp = memcmp(name, entry->e_name, name_len);
		if (cmp <= 0 && (sorted || cmp == 0))
			break;
	}
	*pentry = entry;
	return cmp ? -ENODATA : 0;
}

309
310
311
312
313
314
315
316
317
static u32
ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
{
	return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
}

static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
{
	return ((u64)ea_inode->i_ctime.tv_sec << 32) |
318
		(u32) inode_peek_iversion_raw(ea_inode);
319
320
321
322
323
}

static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
{
	ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
324
	inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
325
326
327
328
329
330
331
332
333
334
335
336
}

static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
{
	return (u32)ea_inode->i_atime.tv_sec;
}

static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
{
	ea_inode->i_atime.tv_sec = hash;
}

Andreas Dilger's avatar
Andreas Dilger committed
337
338
339
/*
 * Read the EA value from an inode.
 */
340
static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilger's avatar
Andreas Dilger committed
341
{
342
343
344
345
346
347
348
349
350
351
352
353
	int blocksize = 1 << ea_inode->i_blkbits;
	int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
	int tail_size = (size % blocksize) ?: blocksize;
	struct buffer_head *bhs_inline[8];
	struct buffer_head **bhs = bhs_inline;
	int i, ret;

	if (bh_count > ARRAY_SIZE(bhs_inline)) {
		bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
		if (!bhs)
			return -ENOMEM;
	}
354

355
356
357
358
	ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
			       true /* wait */, bhs);
	if (ret)
		goto free_bhs;
Andreas Dilger's avatar
Andreas Dilger committed
359

360
361
362
363
364
365
366
367
	for (i = 0; i < bh_count; i++) {
		/* There shouldn't be any holes in ea_inode. */
		if (!bhs[i]) {
			ret = -EFSCORRUPTED;
			goto put_bhs;
		}
		memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
		       i < bh_count - 1 ? blocksize : tail_size);
Andreas Dilger's avatar
Andreas Dilger committed
368
	}
369
370
371
372
373
374
375
376
	ret = 0;
put_bhs:
	for (i = 0; i < bh_count; i++)
		brelse(bhs[i]);
free_bhs:
	if (bhs != bhs_inline)
		kfree(bhs);
	return ret;
Andreas Dilger's avatar
Andreas Dilger committed
377
378
}

379
380
#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)

381
static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
382
				 u32 ea_inode_hash, struct inode **ea_inode)
Andreas Dilger's avatar
Andreas Dilger committed
383
{
384
385
	struct inode *inode;
	int err;
Andreas Dilger's avatar
Andreas Dilger committed
386

387
388
389
	inode = ext4_iget(parent->i_sb, ea_ino);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
390
391
392
		ext4_error(parent->i_sb,
			   "error while reading EA inode %lu err=%d", ea_ino,
			   err);
393
		return err;
Andreas Dilger's avatar
Andreas Dilger committed
394
395
	}

396
	if (is_bad_inode(inode)) {
397
398
399
		ext4_error(parent->i_sb,
			   "error while reading EA inode %lu is_bad_inode",
			   ea_ino);
400
401
402
403
404
		err = -EIO;
		goto error;
	}

	if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
405
406
407
		ext4_error(parent->i_sb,
			   "EA inode %lu does not have EXT4_EA_INODE_FL flag",
			    ea_ino);
408
		err = -EINVAL;
Andreas Dilger's avatar
Andreas Dilger committed
409
410
411
		goto error;
	}

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
	ext4_xattr_inode_set_class(inode);

	/*
	 * Check whether this is an old Lustre-style xattr inode. Lustre
	 * implementation does not have hash validation, rather it has a
	 * backpointer from ea_inode to the parent inode.
	 */
	if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
	    EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
	    inode->i_generation == parent->i_generation) {
		ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
		ext4_xattr_inode_set_ref(inode, 1);
	} else {
		inode_lock(inode);
		inode->i_flags |= S_NOQUOTA;
		inode_unlock(inode);
	}

430
431
	*ea_inode = inode;
	return 0;
Andreas Dilger's avatar
Andreas Dilger committed
432
error:
433
434
	iput(inode);
	return err;
Andreas Dilger's avatar
Andreas Dilger committed
435
436
}

437
static int
438
439
440
ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
			       struct ext4_xattr_entry *entry, void *buffer,
			       size_t size)
441
442
443
444
445
446
447
{
	u32 hash;

	/* Verify stored hash matches calculated hash. */
	hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
	if (hash != ext4_xattr_inode_get_hash(ea_inode))
		return -EFSCORRUPTED;
448
449
450
451
452
453
454
455
456
457
458

	if (entry) {
		__le32 e_hash, tmp_data;

		/* Verify entry hash. */
		tmp_data = cpu_to_le32(hash);
		e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
					       &tmp_data, 1);
		if (e_hash != entry->e_hash)
			return -EFSCORRUPTED;
	}
459
460
461
	return 0;
}

Andreas Dilger's avatar
Andreas Dilger committed
462
/*
463
 * Read xattr value from the EA inode.
Andreas Dilger's avatar
Andreas Dilger committed
464
465
 */
static int
466
467
ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
		     void *buffer, size_t size)
Andreas Dilger's avatar
Andreas Dilger committed
468
{
469
	struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
470
	struct inode *ea_inode;
471
	int err;
Andreas Dilger's avatar
Andreas Dilger committed
472

473
	err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
474
				    le32_to_cpu(entry->e_hash), &ea_inode);
475
476
477
478
	if (err) {
		ea_inode = NULL;
		goto out;
	}
Andreas Dilger's avatar
Andreas Dilger committed
479

480
481
482
483
484
485
486
	if (i_size_read(ea_inode) != size) {
		ext4_warning_inode(ea_inode,
				   "ea_inode file size=%llu entry size=%zu",
				   i_size_read(ea_inode), size);
		err = -EFSCORRUPTED;
		goto out;
	}
Andreas Dilger's avatar
Andreas Dilger committed
487

488
489
490
491
	err = ext4_xattr_inode_read(ea_inode, buffer, size);
	if (err)
		goto out;

492
493
494
495
	if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
		err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
						     size);
		if (err) {
496
497
498
499
500
			ext4_warning_inode(ea_inode,
					   "EA inode hash validation failed");
			goto out;
		}

501
502
503
504
505
		if (ea_inode_cache)
			mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
					ext4_xattr_inode_get_hash(ea_inode),
					ea_inode->i_ino, true /* reusable */);
	}
506
507
508
out:
	iput(ea_inode);
	return err;
Andreas Dilger's avatar
Andreas Dilger committed
509
510
}

511
static int
512
ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
513
514
515
		     void *buffer, size_t buffer_size)
{
	struct buffer_head *bh = NULL;
516
	struct ext4_xattr_entry *entry;
517
	size_t size;
518
	void *end;
519
	int error;
520
	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
521
522
523
524
525

	ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
		  name_index, name, buffer, (long)buffer_size);

	error = -ENODATA;
526
	if (!EXT4_I(inode)->i_file_acl)
527
		goto cleanup;
528
529
	ea_idebug(inode, "reading block %llu",
		  (unsigned long long)EXT4_I(inode)->i_file_acl);
530
	bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
531
532
533
534
	if (!bh)
		goto cleanup;
	ea_bdebug(bh, "b_count=%d, refcount=%d",
		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
535
536
	error = ext4_xattr_check_block(inode, bh);
	if (error)
537
		goto cleanup;
538
	ext4_xattr_block_cache_insert(ea_block_cache, bh);
539
	entry = BFIRST(bh);
540
541
	end = bh->b_data + bh->b_size;
	error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
542
543
544
	if (error)
		goto cleanup;
	size = le32_to_cpu(entry->e_value_size);
545
546
547
	error = -ERANGE;
	if (unlikely(size > EXT4_XATTR_SIZE_MAX))
		goto cleanup;
548
549
550
	if (buffer) {
		if (size > buffer_size)
			goto cleanup;
Andreas Dilger's avatar
Andreas Dilger committed
551
		if (entry->e_value_inum) {
552
553
			error = ext4_xattr_inode_get(inode, entry, buffer,
						     size);
Andreas Dilger's avatar
Andreas Dilger committed
554
555
556
			if (error)
				goto cleanup;
		} else {
557
558
559
560
561
562
			u16 offset = le16_to_cpu(entry->e_value_offs);
			void *p = bh->b_data + offset;

			if (unlikely(p + size > end))
				goto cleanup;
			memcpy(buffer, p, size);
Andreas Dilger's avatar
Andreas Dilger committed
563
		}
564
565
566
567
568
569
570
571
	}
	error = size;

cleanup:
	brelse(bh);
	return error;
}

Tao Ma's avatar
Tao Ma committed
572
int
573
ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
574
575
		     void *buffer, size_t buffer_size)
{
576
577
578
579
	struct ext4_xattr_ibody_header *header;
	struct ext4_xattr_entry *entry;
	struct ext4_inode *raw_inode;
	struct ext4_iloc iloc;
580
581
582
583
	size_t size;
	void *end;
	int error;

584
	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
585
		return -ENODATA;
586
	error = ext4_get_inode_loc(inode, &iloc);
587
588
	if (error)
		return error;
589
	raw_inode = ext4_raw_inode(&iloc);
590
	header = IHDR(inode, raw_inode);
591
	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
592
	error = xattr_check_inode(inode, header, end);
593
594
	if (error)
		goto cleanup;
595
	entry = IFIRST(header);
596
	error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
597
598
599
	if (error)
		goto cleanup;
	size = le32_to_cpu(entry->e_value_size);
600
601
602
	error = -ERANGE;
	if (unlikely(size > EXT4_XATTR_SIZE_MAX))
		goto cleanup;
603
604
605
	if (buffer) {
		if (size > buffer_size)
			goto cleanup;
Andreas Dilger's avatar
Andreas Dilger committed
606
		if (entry->e_value_inum) {
607
608
			error = ext4_xattr_inode_get(inode, entry, buffer,
						     size);
Andreas Dilger's avatar
Andreas Dilger committed
609
610
611
			if (error)
				goto cleanup;
		} else {
612
613
614
615
616
617
			u16 offset = le16_to_cpu(entry->e_value_offs);
			void *p = (void *)IFIRST(header) + offset;

			if (unlikely(p + size > end))
				goto cleanup;
			memcpy(buffer, p, size);
Andreas Dilger's avatar
Andreas Dilger committed
618
		}
619
620
621
622
623
624
625
626
627
	}
	error = size;

cleanup:
	brelse(iloc.bh);
	return error;
}

/*
628
 * ext4_xattr_get()
629
630
631
632
633
634
635
636
637
 *
 * Copy an extended attribute into the buffer
 * provided, or compute the buffer size required.
 * Buffer is NULL to compute the size of the buffer required.
 *
 * Returns a negative error number on failure, or the number of bytes
 * used / required on success.
 */
int
638
ext4_xattr_get(struct inode *inode, int name_index, const char *name,
639
640
641
642
	       void *buffer, size_t buffer_size)
{
	int error;

643
644
645
	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
		return -EIO;

646
647
648
	if (strlen(name) > 255)
		return -ERANGE;

649
650
	down_read(&EXT4_I(inode)->xattr_sem);
	error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
651
652
				     buffer_size);
	if (error == -ENODATA)
653
		error = ext4_xattr_block_get(inode, name_index, name, buffer,
654
					     buffer_size);
655
	up_read(&EXT4_I(inode)->xattr_sem);
656
657
658
659
	return error;
}

static int
660
ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
661
662
663
664
			char *buffer, size_t buffer_size)
{
	size_t rest = buffer_size;

665
	for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
666
		const struct xattr_handler *handler =
667
			ext4_xattr_handler(entry->e_name_index);
668

669
670
671
672
673
		if (handler && (!handler->list || handler->list(dentry))) {
			const char *prefix = handler->prefix ?: handler->name;
			size_t prefix_len = strlen(prefix);
			size_t size = prefix_len + entry->e_name_len + 1;

674
675
676
			if (buffer) {
				if (size > rest)
					return -ERANGE;
677
678
679
680
681
				memcpy(buffer, prefix, prefix_len);
				buffer += prefix_len;
				memcpy(buffer, entry->e_name, entry->e_name_len);
				buffer += entry->e_name_len;
				*buffer++ = 0;
682
683
684
685
			}
			rest -= size;
		}
	}
686
	return buffer_size - rest;  /* total size */
687
688
689
}

static int
690
ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
691
{
692
	struct inode *inode = d_inode(dentry);
693
694
695
696
697
698
699
	struct buffer_head *bh = NULL;
	int error;

	ea_idebug(inode, "buffer=%p, buffer_size=%ld",
		  buffer, (long)buffer_size);

	error = 0;
700
	if (!EXT4_I(inode)->i_file_acl)
701
		goto cleanup;
702
703
	ea_idebug(inode, "reading block %llu",
		  (unsigned long long)EXT4_I(inode)->i_file_acl);
704
	bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
705
706
707
708
709
	error = -EIO;
	if (!bh)
		goto cleanup;
	ea_bdebug(bh, "b_count=%d, refcount=%d",
		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
710
711
	error = ext4_xattr_check_block(inode, bh);
	if (error)
712
		goto cleanup;
713
	ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
714
	error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
715
716
717
718
719
720
721
722

cleanup:
	brelse(bh);

	return error;
}

static int
723
ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
724
{
725
	struct inode *inode = d_inode(dentry);
726
727
728
	struct ext4_xattr_ibody_header *header;
	struct ext4_inode *raw_inode;
	struct ext4_iloc iloc;
729
730
731
	void *end;
	int error;

732
	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
733
		return 0;
734
	error = ext4_get_inode_loc(inode, &iloc);
735
736
	if (error)
		return error;
737
	raw_inode = ext4_raw_inode(&iloc);
738
	header = IHDR(inode, raw_inode);
739
	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
740
	error = xattr_check_inode(inode, header, end);
741
742
	if (error)
		goto cleanup;
743
	error = ext4_xattr_list_entries(dentry, IFIRST(header),
744
745
746
747
748
749
750
751
					buffer, buffer_size);

cleanup:
	brelse(iloc.bh);
	return error;
}

/*
752
753
754
 * Inode operation listxattr()
 *
 * d_inode(dentry)->i_rwsem: don't care
755
756
757
758
759
760
761
762
 *
 * Copy a list of attribute names into the buffer
 * provided, or compute the buffer size required.
 * Buffer is NULL to compute the size of the buffer required.
 *
 * Returns a negative error number on failure, or the number of bytes
 * used / required on success.
 */
763
764
ssize_t
ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
765
{
766
	int ret, ret2;
767

768
	down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
769
770
771
772
773
774
	ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
	if (ret < 0)
		goto errout;
	if (buffer) {
		buffer += ret;
		buffer_size -= ret;
775
	}
776
777
778
779
780
	ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
	if (ret < 0)
		goto errout;
	ret += ret2;
errout:
781
	up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
782
	return ret;
783
784
785
}

/*
786
 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
787
788
 * not set, set it.
 */
789
static void ext4_xattr_update_super_block(handle_t *handle,
790
791
					  struct super_block *sb)
{
792
	if (ext4_has_feature_xattr(sb))
793
794
		return;

795
	BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
796
	if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
797
		ext4_set_feature_xattr(sb);
Theodore Ts'o's avatar
Theodore Ts'o committed
798
		ext4_handle_dirty_super(handle, sb);
799
800
801
	}
}

802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
{
	struct ext4_iloc iloc = { .bh = NULL };
	struct buffer_head *bh = NULL;
	struct ext4_inode *raw_inode;
	struct ext4_xattr_ibody_header *header;
	struct ext4_xattr_entry *entry;
	qsize_t ea_inode_refs = 0;
	void *end;
	int ret;

	lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);

	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
		ret = ext4_get_inode_loc(inode, &iloc);
		if (ret)
			goto out;
		raw_inode = ext4_raw_inode(&iloc);
		header = IHDR(inode, raw_inode);
		end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
		ret = xattr_check_inode(inode, header, end);
		if (ret)
			goto out;

		for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
		     entry = EXT4_XATTR_NEXT(entry))
			if (entry->e_value_inum)
				ea_inode_refs++;
	}

	if (EXT4_I(inode)->i_file_acl) {
		bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
		if (!bh) {
			ret = -EIO;
			goto out;
		}

839
840
		ret = ext4_xattr_check_block(inode, bh);
		if (ret)
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
			goto out;

		for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
		     entry = EXT4_XATTR_NEXT(entry))
			if (entry->e_value_inum)
				ea_inode_refs++;
	}
	*usage = ea_inode_refs + 1;
	ret = 0;
out:
	brelse(iloc.bh);
	brelse(bh);
	return ret;
}

856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
static inline size_t round_up_cluster(struct inode *inode, size_t length)
{
	struct super_block *sb = inode->i_sb;
	size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
				    inode->i_blkbits);
	size_t mask = ~(cluster_size - 1);

	return (length + cluster_size - 1) & mask;
}

static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
{
	int err;

	err = dquot_alloc_inode(inode);
	if (err)
		return err;
	err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
	if (err)
		dquot_free_inode(inode);
	return err;
}

879
880
881
static void ext4_xattr_inode_free_quota(struct inode *parent,
					struct inode *ea_inode,
					size_t len)
882
{
883
884
885
886
887
	if (ea_inode &&
	    ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
		return;
	dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
	dquot_free_inode(parent);
888
889
}

890
891
892
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
			     struct buffer_head *block_bh, size_t value_len,
			     bool is_create)
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
{
	int credits;
	int blocks;

	/*
	 * 1) Owner inode update
	 * 2) Ref count update on old xattr block
	 * 3) new xattr block
	 * 4) block bitmap update for new xattr block
	 * 5) group descriptor for new xattr block
	 * 6) block bitmap update for old xattr block
	 * 7) group descriptor for old block
	 *
	 * 6 & 7 can happen if we have two racing threads T_a and T_b
	 * which are each trying to set an xattr on inodes I_a and I_b
	 * which were both initially sharing an xattr block.
	 */
	credits = 7;

	/* Quota updates. */
	credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);

	/*
	 * In case of inline data, we may push out the data to a block,
	 * so we need to reserve credits for this eventuality
	 */
919
	if (inode && ext4_has_inline_data(inode))
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
		credits += ext4_writepage_trans_blocks(inode) + 1;

	/* We are done if ea_inode feature is not enabled. */
	if (!ext4_has_feature_ea_inode(sb))
		return credits;

	/* New ea_inode, inode map, block bitmap, group descriptor. */
	credits += 4;

	/* Data blocks. */
	blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;

	/* Indirection block or one level of extent tree. */
	blocks += 1;

	/* Block bitmap and group descriptor updates for each block. */
	credits += blocks * 2;

	/* Blocks themselves. */
	credits += blocks;

941
942
943
944
945
	if (!is_create) {
		/* Dereference ea_inode holding old xattr value.
		 * Old ea_inode, inode map, block bitmap, group descriptor.
		 */
		credits += 4;
946

947
948
		/* Data blocks for old ea_inode. */
		blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
949

950
951
952
953
		/* Indirection block or one level of extent tree for old
		 * ea_inode.
		 */
		blocks += 1;
954

955
956
957
		/* Block bitmap and group descriptor updates for each block. */
		credits += blocks * 2;
	}
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972

	/* We may need to clone the existing xattr block in which case we need
	 * to increment ref counts for existing ea_inodes referenced by it.
	 */
	if (block_bh) {
		struct ext4_xattr_entry *entry = BFIRST(block_bh);

		for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
			if (entry->e_value_inum)
				/* Ref count update on ea_inode. */
				credits += 1;
	}
	return credits;
}

973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
				     int credits, struct buffer_head *bh,
				     bool dirty, bool block_csum)
{
	int error;

	if (!ext4_handle_valid(handle))
		return 0;

	if (handle->h_buffer_credits >= credits)
		return 0;

	error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
	if (!error)
		return 0;
	if (error < 0) {
		ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
		return error;
	}

	if (bh && dirty) {
		if (block_csum)
			ext4_xattr_block_csum_set(inode, bh);
		error = ext4_handle_dirty_metadata(handle, NULL, bh);
		if (error) {
			ext4_warning(inode->i_sb, "Handle metadata (error %d)",
				     error);
			return error;
		}
	}

	error = ext4_journal_restart(handle, credits);
	if (error) {
		ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
		return error;
	}

	if (bh) {
		error = ext4_journal_get_write_access(handle, bh);
		if (error) {
			ext4_warning(inode->i_sb,
				     "Get write access failed (error %d)",
				     error);
			return error;
		}
	}
	return 0;
}

1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
				       int ref_change)
{
	struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
	struct ext4_iloc iloc;
	s64 ref_count;
	u32 hash;
	int ret;

	inode_lock(ea_inode);

	ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1034
	if (ret)
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
		goto out;

	ref_count = ext4_xattr_inode_get_ref(ea_inode);
	ref_count += ref_change;
	ext4_xattr_inode_set_ref(ea_inode, ref_count);

	if (ref_change > 0) {
		WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
			  ea_inode->i_ino, ref_count);

		if (ref_count == 1) {
			WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
				  ea_inode->i_ino, ea_inode->i_nlink);

			set_nlink(ea_inode, 1);
			ext4_orphan_del(handle, ea_inode);

1052
1053
1054
1055
1056
1057
1058
			if (ea_inode_cache) {
				hash = ext4_xattr_inode_get_hash(ea_inode);
				mb_cache_entry_create(ea_inode_cache,
						      GFP_NOFS, hash,
						      ea_inode->i_ino,
						      true /* reusable */);
			}
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
		}
	} else {
		WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
			  ea_inode->i_ino, ref_count);

		if (ref_count == 0) {
			WARN_ONCE(ea_inode->i_nlink != 1,
				  "EA inode %lu i_nlink=%u",
				  ea_inode->i_ino, ea_inode->i_nlink);

			clear_nlink(ea_inode);
			ext4_orphan_add(handle, ea_inode);

1072
1073
1074
1075
1076
			if (ea_inode_cache) {
				hash = ext4_xattr_inode_get_hash(ea_inode);
				mb_cache_entry_delete(ea_inode_cache, hash,
						      ea_inode->i_ino);
			}
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
		}
	}

	ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
	if (ret)
		ext4_warning_inode(ea_inode,
				   "ext4_mark_iloc_dirty() failed ret=%d", ret);
out:
	inode_unlock(ea_inode);
	return ret;
}

static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
{
	return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
}

static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
{
	return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
}

static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
					struct ext4_xattr_entry *first)
{
	struct inode *ea_inode;
	struct ext4_xattr_entry *entry;
	struct ext4_xattr_entry *failed_entry;
	unsigned int ea_ino;
	int err, saved_err;

	for (entry = first; !IS_LAST_ENTRY(entry);
	     entry = EXT4_XATTR_NEXT(entry)) {
		if (!entry->e_value_inum)
			continue;
		ea_ino = le32_to_cpu(entry->e_value_inum);
1113
1114
1115
		err = ext4_xattr_inode_iget(parent, ea_ino,
					    le32_to_cpu(entry->e_hash),
					    &ea_inode);
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
		if (err)
			goto cleanup;
		err = ext4_xattr_inode_inc_ref(handle, ea_inode);
		if (err) {
			ext4_warning_inode(ea_inode, "inc ref error %d", err);
			iput(ea_inode);
			goto cleanup;
		}
		iput(ea_inode);
	}
	return 0;

cleanup:
	saved_err = err;
	failed_entry = entry;

	for (entry = first; entry != failed_entry;
	     entry = EXT4_XATTR_NEXT(entry)) {
		if (!entry->e_value_inum)
			continue;
		ea_ino = le32_to_cpu(entry->e_value_inum);
1137
1138
1139
		err = ext4_xattr_inode_iget(parent, ea_ino,
					    le32_to_cpu(entry->e_hash),
					    &ea_inode);
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
		if (err) {
			ext4_warning(parent->i_sb,
				     "cleanup ea_ino %u iget error %d", ea_ino,
				     err);
			continue;
		}
		err = ext4_xattr_inode_dec_ref(handle, ea_inode);
		if (err)
			ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
					   err);
		iput(ea_inode);
	}
	return saved_err;
}

1155
static void
1156
1157
1158
1159
1160
ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
			     struct buffer_head *bh,
			     struct ext4_xattr_entry *first, bool block_csum,
			     struct ext4_xattr_inode_array **ea_inode_array,
			     int extra_credits, bool skip_quota)
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
{
	struct inode *ea_inode;
	struct ext4_xattr_entry *entry;
	bool dirty = false;
	unsigned int ea_ino;
	int err;
	int credits;

	/* One credit for dec ref on ea_inode, one for orphan list addition, */
	credits = 2 + extra_credits;

	for (entry = first; !IS_LAST_ENTRY(entry);
	     entry = EXT4_XATTR_NEXT(entry)) {
		if (!entry->e_value_inum)
			continue;
		ea_ino = le32_to_cpu(entry->e_value_inum);
1177
1178
1179
		err = ext4_xattr_inode_iget(parent, ea_ino,
					    le32_to_cpu(entry->e_hash),
					    &ea_inode);
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
		if (err)
			continue;

		err = ext4_expand_inode_array(ea_inode_array, ea_inode);
		if (err) {
			ext4_warning_inode(ea_inode,
					   "Expand inode array err=%d", err);
			iput(ea_inode);
			continue;
		}

		err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
						dirty, block_csum);
		if (err) {
			ext4_warning_inode(ea_inode, "Ensure credits err=%d",
					   err);
			continue;
		}

1199
1200
1201
1202
1203
1204
1205
1206
		err = ext4_xattr_inode_dec_ref(handle, ea_inode);
		if (err) {
			ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
					   err);
			continue;
		}

		if (!skip_quota)
1207
			ext4_xattr_inode_free_quota(parent, ea_inode,
1208
					      le32_to_cpu(entry->e_value_size));
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

		/*
		 * Forget about ea_inode within the same transaction that
		 * decrements the ref count. This avoids duplicate decrements in
		 * case the rest of the work spills over to subsequent
		 * transactions.
		 */
		entry->e_value_inum = 0;
		entry->e_value_size = 0;

		dirty = true;
	}

	if (dirty) {
		/*
		 * Note that we are deliberately skipping csum calculation for
		 * the final update because we do not expect any journal
		 * restarts until xattr block is freed.
		 */

		err = ext4_handle_dirty_metadata(handle, NULL, bh);
		if (err)
			ext4_warning_inode(parent,
					   "handle dirty metadata err=%d", err);
	}
}

1236
/*
1237
1238
 * Release the xattr block BH: If the reference count is > 1, decrement it;
 * otherwise free the block.
1239
1240
 */
static void
1241
ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1242
1243
1244
			 struct buffer_head *bh,
			 struct ext4_xattr_inode_array **ea_inode_array,
			 int extra_credits)
1245
{
1246
	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1247
	u32 hash, ref;
1248
	int error = 0;
1249

1250
	BUFFER_TRACE(bh, "get_write_access");
1251
1252
1253
1254
1255
	error = ext4_journal_get_write_access(handle, bh);
	if (error)
		goto out;

	lock_buffer(bh);
1256
1257
1258
	hash = le32_to_cpu(BHDR(bh)->h_hash);
	ref = le32_to_cpu(BHDR(bh)->h_refcount);
	if (ref == 1) {
1259
		ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara's avatar
Jan Kara committed
1260
1261
1262
1263
		/*
		 * This must happen under buffer lock for
		 * ext4_xattr_block_set() to reliably detect freed block
		 */
1264
1265
1266
		if (ea_block_cache)
			mb_cache_entry_delete(ea_block_cache, hash,
					      bh->b_blocknr);
1267
		get_bh(bh);
1268
		unlock_buffer(bh);
1269
1270
1271
1272
1273
1274
1275
1276

		if (ext4_has_feature_ea_inode(inode->i_sb))
			ext4_xattr_inode_dec_ref_all(handle, inode, bh,
						     BFIRST(bh),
						     true /* block_csum */,
						     ea_inode_array,
						     extra_credits,
						     true /* skip_quota */);
1277
1278
1279
		ext4_free_blocks(handle, inode, bh, 0, 1,
				 EXT4_FREE_BLOCKS_METADATA |
				 EXT4_FREE_BLOCKS_FORGET);
1280
	} else {
1281
1282
1283
1284
1285
		ref--;
		BHDR(bh)->h_refcount = cpu_to_le32(ref);
		if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
			struct mb_cache_entry *ce;

1286
1287
1288
1289
1290
1291
1292
			if (ea_block_cache) {
				ce = mb_cache_entry_get(ea_block_cache, hash,
							bh->b_blocknr);
				if (ce) {
					ce->e_reusable = 1;
					mb_cache_entry_put(ea_block_cache, ce);
				}
1293
1294
1295
			}
		}

1296
		ext4_xattr_block_csum_set(inode, bh);
1297
1298
1299
1300
1301
1302
		/*
		 * Beware of this ugliness: Releasing of xattr block references
		 * from different inodes can race and so we have to protect
		 * from a race where someone else frees the block (and releases
		 * its journal_head) before we are done dirtying the buffer. In
		 * nojournal mode this race is harmless and we actually cannot
1303
		 * call ext4_handle_dirty_metadata() with locked buffer as
1304
1305
1306
1307
		 * that function can call sync_dirty_buffer() so for that case
		 * we handle the dirtying after unlocking the buffer.
		 */
		if (ext4_handle_valid(handle))
1308
			error = ext4_handle_dirty_metadata(handle, inode, bh);
1309
		unlock_buffer(bh);
1310
		if (!ext4_handle_valid(handle))
1311
			error = ext4_handle_dirty_metadata(handle, inode, bh);
1312
		if (IS_SYNC(inode))
1313
			ext4_handle_sync(handle);