xref: /openbmc/linux/fs/ext4/xattr.c (revision 19f5fb7a)
1ac27a0ecSDave Kleikamp /*
2617ba13bSMingming Cao  * linux/fs/ext4/xattr.c
3ac27a0ecSDave Kleikamp  *
4ac27a0ecSDave Kleikamp  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5ac27a0ecSDave Kleikamp  *
6ac27a0ecSDave Kleikamp  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7617ba13bSMingming Cao  * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
8ac27a0ecSDave Kleikamp  * Extended attributes for symlinks and special files added per
9ac27a0ecSDave Kleikamp  *  suggestion of Luka Renko <luka.renko@hermes.si>.
10ac27a0ecSDave Kleikamp  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
11ac27a0ecSDave Kleikamp  *  Red Hat Inc.
12ac27a0ecSDave Kleikamp  * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13ac27a0ecSDave Kleikamp  *  and Andreas Gruenbacher <agruen@suse.de>.
14ac27a0ecSDave Kleikamp  */
15ac27a0ecSDave Kleikamp 
16ac27a0ecSDave Kleikamp /*
17ac27a0ecSDave Kleikamp  * Extended attributes are stored directly in inodes (on file systems with
18ac27a0ecSDave Kleikamp  * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19ac27a0ecSDave Kleikamp  * field contains the block number if an inode uses an additional block. All
20ac27a0ecSDave Kleikamp  * attributes must fit in the inode and one additional block. Blocks that
21ac27a0ecSDave Kleikamp  * contain the identical set of attributes may be shared among several inodes.
22ac27a0ecSDave Kleikamp  * Identical blocks are detected by keeping a cache of blocks that have
23ac27a0ecSDave Kleikamp  * recently been accessed.
24ac27a0ecSDave Kleikamp  *
25ac27a0ecSDave Kleikamp  * The attributes in inodes and on blocks have a different header; the entries
26ac27a0ecSDave Kleikamp  * are stored in the same format:
27ac27a0ecSDave Kleikamp  *
28ac27a0ecSDave Kleikamp  *   +------------------+
29ac27a0ecSDave Kleikamp  *   | header           |
30ac27a0ecSDave Kleikamp  *   | entry 1          | |
31ac27a0ecSDave Kleikamp  *   | entry 2          | | growing downwards
32ac27a0ecSDave Kleikamp  *   | entry 3          | v
33ac27a0ecSDave Kleikamp  *   | four null bytes  |
34ac27a0ecSDave Kleikamp  *   | . . .            |
35ac27a0ecSDave Kleikamp  *   | value 1          | ^
36ac27a0ecSDave Kleikamp  *   | value 3          | | growing upwards
37ac27a0ecSDave Kleikamp  *   | value 2          | |
38ac27a0ecSDave Kleikamp  *   +------------------+
39ac27a0ecSDave Kleikamp  *
40ac27a0ecSDave Kleikamp  * The header is followed by multiple entry descriptors. In disk blocks, the
41ac27a0ecSDave Kleikamp  * entry descriptors are kept sorted. In inodes, they are unsorted. The
42ac27a0ecSDave Kleikamp  * attribute values are aligned to the end of the block in no specific order.
43ac27a0ecSDave Kleikamp  *
44ac27a0ecSDave Kleikamp  * Locking strategy
45ac27a0ecSDave Kleikamp  * ----------------
46617ba13bSMingming Cao  * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
47ac27a0ecSDave Kleikamp  * EA blocks are only changed if they are exclusive to an inode, so
48ac27a0ecSDave Kleikamp  * holding xattr_sem also means that nothing but the EA block's reference
49ac27a0ecSDave Kleikamp  * count can change. Multiple writers to the same block are synchronized
50ac27a0ecSDave Kleikamp  * by the buffer lock.
51ac27a0ecSDave Kleikamp  */
52ac27a0ecSDave Kleikamp 
53ac27a0ecSDave Kleikamp #include <linux/init.h>
54ac27a0ecSDave Kleikamp #include <linux/fs.h>
55ac27a0ecSDave Kleikamp #include <linux/slab.h>
56ac27a0ecSDave Kleikamp #include <linux/mbcache.h>
57ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
58ac27a0ecSDave Kleikamp #include <linux/rwsem.h>
593dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
603dcf5451SChristoph Hellwig #include "ext4.h"
61ac27a0ecSDave Kleikamp #include "xattr.h"
62ac27a0ecSDave Kleikamp #include "acl.h"
63ac27a0ecSDave Kleikamp 
64617ba13bSMingming Cao #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
65617ba13bSMingming Cao #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
66ac27a0ecSDave Kleikamp #define BFIRST(bh) ENTRY(BHDR(bh)+1)
67ac27a0ecSDave Kleikamp #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
68ac27a0ecSDave Kleikamp 
69617ba13bSMingming Cao #ifdef EXT4_XATTR_DEBUG
70ac27a0ecSDave Kleikamp # define ea_idebug(inode, f...) do { \
71ac27a0ecSDave Kleikamp 		printk(KERN_DEBUG "inode %s:%lu: ", \
72ac27a0ecSDave Kleikamp 			inode->i_sb->s_id, inode->i_ino); \
73ac27a0ecSDave Kleikamp 		printk(f); \
74ac27a0ecSDave Kleikamp 		printk("\n"); \
75ac27a0ecSDave Kleikamp 	} while (0)
76ac27a0ecSDave Kleikamp # define ea_bdebug(bh, f...) do { \
77ac27a0ecSDave Kleikamp 		char b[BDEVNAME_SIZE]; \
78ac27a0ecSDave Kleikamp 		printk(KERN_DEBUG "block %s:%lu: ", \
79ac27a0ecSDave Kleikamp 			bdevname(bh->b_bdev, b), \
80ac27a0ecSDave Kleikamp 			(unsigned long) bh->b_blocknr); \
81ac27a0ecSDave Kleikamp 		printk(f); \
82ac27a0ecSDave Kleikamp 		printk("\n"); \
83ac27a0ecSDave Kleikamp 	} while (0)
84ac27a0ecSDave Kleikamp #else
85ac27a0ecSDave Kleikamp # define ea_idebug(f...)
86ac27a0ecSDave Kleikamp # define ea_bdebug(f...)
87ac27a0ecSDave Kleikamp #endif
88ac27a0ecSDave Kleikamp 
89617ba13bSMingming Cao static void ext4_xattr_cache_insert(struct buffer_head *);
90617ba13bSMingming Cao static struct buffer_head *ext4_xattr_cache_find(struct inode *,
91617ba13bSMingming Cao 						 struct ext4_xattr_header *,
92ac27a0ecSDave Kleikamp 						 struct mb_cache_entry **);
93617ba13bSMingming Cao static void ext4_xattr_rehash(struct ext4_xattr_header *,
94617ba13bSMingming Cao 			      struct ext4_xattr_entry *);
95431547b3SChristoph Hellwig static int ext4_xattr_list(struct dentry *dentry, char *buffer,
96d3a95d47SMingming Cao 			   size_t buffer_size);
97ac27a0ecSDave Kleikamp 
98617ba13bSMingming Cao static struct mb_cache *ext4_xattr_cache;
99ac27a0ecSDave Kleikamp 
100617ba13bSMingming Cao static struct xattr_handler *ext4_xattr_handler_map[] = {
101617ba13bSMingming Cao 	[EXT4_XATTR_INDEX_USER]		     = &ext4_xattr_user_handler,
10203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
103617ba13bSMingming Cao 	[EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext4_xattr_acl_access_handler,
104617ba13bSMingming Cao 	[EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
105ac27a0ecSDave Kleikamp #endif
106617ba13bSMingming Cao 	[EXT4_XATTR_INDEX_TRUSTED]	     = &ext4_xattr_trusted_handler,
10703010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_SECURITY
108617ba13bSMingming Cao 	[EXT4_XATTR_INDEX_SECURITY]	     = &ext4_xattr_security_handler,
109ac27a0ecSDave Kleikamp #endif
110ac27a0ecSDave Kleikamp };
111ac27a0ecSDave Kleikamp 
112617ba13bSMingming Cao struct xattr_handler *ext4_xattr_handlers[] = {
113617ba13bSMingming Cao 	&ext4_xattr_user_handler,
114617ba13bSMingming Cao 	&ext4_xattr_trusted_handler,
11503010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
116617ba13bSMingming Cao 	&ext4_xattr_acl_access_handler,
117617ba13bSMingming Cao 	&ext4_xattr_acl_default_handler,
118ac27a0ecSDave Kleikamp #endif
11903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_SECURITY
120617ba13bSMingming Cao 	&ext4_xattr_security_handler,
121ac27a0ecSDave Kleikamp #endif
122ac27a0ecSDave Kleikamp 	NULL
123ac27a0ecSDave Kleikamp };
124ac27a0ecSDave Kleikamp 
125ac27a0ecSDave Kleikamp static inline struct xattr_handler *
126617ba13bSMingming Cao ext4_xattr_handler(int name_index)
127ac27a0ecSDave Kleikamp {
128ac27a0ecSDave Kleikamp 	struct xattr_handler *handler = NULL;
129ac27a0ecSDave Kleikamp 
130617ba13bSMingming Cao 	if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
131617ba13bSMingming Cao 		handler = ext4_xattr_handler_map[name_index];
132ac27a0ecSDave Kleikamp 	return handler;
133ac27a0ecSDave Kleikamp }
134ac27a0ecSDave Kleikamp 
135ac27a0ecSDave Kleikamp /*
136ac27a0ecSDave Kleikamp  * Inode operation listxattr()
137ac27a0ecSDave Kleikamp  *
138ac27a0ecSDave Kleikamp  * dentry->d_inode->i_mutex: don't care
139ac27a0ecSDave Kleikamp  */
140ac27a0ecSDave Kleikamp ssize_t
141617ba13bSMingming Cao ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
142ac27a0ecSDave Kleikamp {
143431547b3SChristoph Hellwig 	return ext4_xattr_list(dentry, buffer, size);
144ac27a0ecSDave Kleikamp }
145ac27a0ecSDave Kleikamp 
146ac27a0ecSDave Kleikamp static int
147617ba13bSMingming Cao ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
148ac27a0ecSDave Kleikamp {
149ac27a0ecSDave Kleikamp 	while (!IS_LAST_ENTRY(entry)) {
150617ba13bSMingming Cao 		struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
151ac27a0ecSDave Kleikamp 		if ((void *)next >= end)
152ac27a0ecSDave Kleikamp 			return -EIO;
153ac27a0ecSDave Kleikamp 		entry = next;
154ac27a0ecSDave Kleikamp 	}
155ac27a0ecSDave Kleikamp 	return 0;
156ac27a0ecSDave Kleikamp }
157ac27a0ecSDave Kleikamp 
158ac27a0ecSDave Kleikamp static inline int
159617ba13bSMingming Cao ext4_xattr_check_block(struct buffer_head *bh)
160ac27a0ecSDave Kleikamp {
161ac27a0ecSDave Kleikamp 	int error;
162ac27a0ecSDave Kleikamp 
163617ba13bSMingming Cao 	if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
164ac27a0ecSDave Kleikamp 	    BHDR(bh)->h_blocks != cpu_to_le32(1))
165ac27a0ecSDave Kleikamp 		return -EIO;
166617ba13bSMingming Cao 	error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
167ac27a0ecSDave Kleikamp 	return error;
168ac27a0ecSDave Kleikamp }
169ac27a0ecSDave Kleikamp 
170ac27a0ecSDave Kleikamp static inline int
171617ba13bSMingming Cao ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
172ac27a0ecSDave Kleikamp {
173ac27a0ecSDave Kleikamp 	size_t value_size = le32_to_cpu(entry->e_value_size);
174ac27a0ecSDave Kleikamp 
175ac27a0ecSDave Kleikamp 	if (entry->e_value_block != 0 || value_size > size ||
176ac27a0ecSDave Kleikamp 	    le16_to_cpu(entry->e_value_offs) + value_size > size)
177ac27a0ecSDave Kleikamp 		return -EIO;
178ac27a0ecSDave Kleikamp 	return 0;
179ac27a0ecSDave Kleikamp }
180ac27a0ecSDave Kleikamp 
181ac27a0ecSDave Kleikamp static int
182617ba13bSMingming Cao ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
183ac27a0ecSDave Kleikamp 		      const char *name, size_t size, int sorted)
184ac27a0ecSDave Kleikamp {
185617ba13bSMingming Cao 	struct ext4_xattr_entry *entry;
186ac27a0ecSDave Kleikamp 	size_t name_len;
187ac27a0ecSDave Kleikamp 	int cmp = 1;
188ac27a0ecSDave Kleikamp 
189ac27a0ecSDave Kleikamp 	if (name == NULL)
190ac27a0ecSDave Kleikamp 		return -EINVAL;
191ac27a0ecSDave Kleikamp 	name_len = strlen(name);
192ac27a0ecSDave Kleikamp 	entry = *pentry;
193617ba13bSMingming Cao 	for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
194ac27a0ecSDave Kleikamp 		cmp = name_index - entry->e_name_index;
195ac27a0ecSDave Kleikamp 		if (!cmp)
196ac27a0ecSDave Kleikamp 			cmp = name_len - entry->e_name_len;
197ac27a0ecSDave Kleikamp 		if (!cmp)
198ac27a0ecSDave Kleikamp 			cmp = memcmp(name, entry->e_name, name_len);
199ac27a0ecSDave Kleikamp 		if (cmp <= 0 && (sorted || cmp == 0))
200ac27a0ecSDave Kleikamp 			break;
201ac27a0ecSDave Kleikamp 	}
202ac27a0ecSDave Kleikamp 	*pentry = entry;
203617ba13bSMingming Cao 	if (!cmp && ext4_xattr_check_entry(entry, size))
204ac27a0ecSDave Kleikamp 			return -EIO;
205ac27a0ecSDave Kleikamp 	return cmp ? -ENODATA : 0;
206ac27a0ecSDave Kleikamp }
207ac27a0ecSDave Kleikamp 
208ac27a0ecSDave Kleikamp static int
209617ba13bSMingming Cao ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
210ac27a0ecSDave Kleikamp 		     void *buffer, size_t buffer_size)
211ac27a0ecSDave Kleikamp {
212ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
213617ba13bSMingming Cao 	struct ext4_xattr_entry *entry;
214ac27a0ecSDave Kleikamp 	size_t size;
215ac27a0ecSDave Kleikamp 	int error;
216ac27a0ecSDave Kleikamp 
217ac27a0ecSDave Kleikamp 	ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
218ac27a0ecSDave Kleikamp 		  name_index, name, buffer, (long)buffer_size);
219ac27a0ecSDave Kleikamp 
220ac27a0ecSDave Kleikamp 	error = -ENODATA;
221617ba13bSMingming Cao 	if (!EXT4_I(inode)->i_file_acl)
222ac27a0ecSDave Kleikamp 		goto cleanup;
223617ba13bSMingming Cao 	ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
224617ba13bSMingming Cao 	bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
225ac27a0ecSDave Kleikamp 	if (!bh)
226ac27a0ecSDave Kleikamp 		goto cleanup;
227ac27a0ecSDave Kleikamp 	ea_bdebug(bh, "b_count=%d, refcount=%d",
228ac27a0ecSDave Kleikamp 		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
229617ba13bSMingming Cao 	if (ext4_xattr_check_block(bh)) {
23046e665e9SHarvey Harrison bad_block:	ext4_error(inode->i_sb, __func__,
2312ae02107SMingming Cao 			   "inode %lu: bad block %llu", inode->i_ino,
232617ba13bSMingming Cao 			   EXT4_I(inode)->i_file_acl);
233ac27a0ecSDave Kleikamp 		error = -EIO;
234ac27a0ecSDave Kleikamp 		goto cleanup;
235ac27a0ecSDave Kleikamp 	}
236617ba13bSMingming Cao 	ext4_xattr_cache_insert(bh);
237ac27a0ecSDave Kleikamp 	entry = BFIRST(bh);
238617ba13bSMingming Cao 	error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
239ac27a0ecSDave Kleikamp 	if (error == -EIO)
240ac27a0ecSDave Kleikamp 		goto bad_block;
241ac27a0ecSDave Kleikamp 	if (error)
242ac27a0ecSDave Kleikamp 		goto cleanup;
243ac27a0ecSDave Kleikamp 	size = le32_to_cpu(entry->e_value_size);
244ac27a0ecSDave Kleikamp 	if (buffer) {
245ac27a0ecSDave Kleikamp 		error = -ERANGE;
246ac27a0ecSDave Kleikamp 		if (size > buffer_size)
247ac27a0ecSDave Kleikamp 			goto cleanup;
248ac27a0ecSDave Kleikamp 		memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
249ac27a0ecSDave Kleikamp 		       size);
250ac27a0ecSDave Kleikamp 	}
251ac27a0ecSDave Kleikamp 	error = size;
252ac27a0ecSDave Kleikamp 
253ac27a0ecSDave Kleikamp cleanup:
254ac27a0ecSDave Kleikamp 	brelse(bh);
255ac27a0ecSDave Kleikamp 	return error;
256ac27a0ecSDave Kleikamp }
257ac27a0ecSDave Kleikamp 
258ac27a0ecSDave Kleikamp static int
259617ba13bSMingming Cao ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
260ac27a0ecSDave Kleikamp 		     void *buffer, size_t buffer_size)
261ac27a0ecSDave Kleikamp {
262617ba13bSMingming Cao 	struct ext4_xattr_ibody_header *header;
263617ba13bSMingming Cao 	struct ext4_xattr_entry *entry;
264617ba13bSMingming Cao 	struct ext4_inode *raw_inode;
265617ba13bSMingming Cao 	struct ext4_iloc iloc;
266ac27a0ecSDave Kleikamp 	size_t size;
267ac27a0ecSDave Kleikamp 	void *end;
268ac27a0ecSDave Kleikamp 	int error;
269ac27a0ecSDave Kleikamp 
270*19f5fb7aSTheodore Ts'o 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
271ac27a0ecSDave Kleikamp 		return -ENODATA;
272617ba13bSMingming Cao 	error = ext4_get_inode_loc(inode, &iloc);
273ac27a0ecSDave Kleikamp 	if (error)
274ac27a0ecSDave Kleikamp 		return error;
275617ba13bSMingming Cao 	raw_inode = ext4_raw_inode(&iloc);
276ac27a0ecSDave Kleikamp 	header = IHDR(inode, raw_inode);
277ac27a0ecSDave Kleikamp 	entry = IFIRST(header);
278617ba13bSMingming Cao 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
279617ba13bSMingming Cao 	error = ext4_xattr_check_names(entry, end);
280ac27a0ecSDave Kleikamp 	if (error)
281ac27a0ecSDave Kleikamp 		goto cleanup;
282617ba13bSMingming Cao 	error = ext4_xattr_find_entry(&entry, name_index, name,
283ac27a0ecSDave Kleikamp 				      end - (void *)entry, 0);
284ac27a0ecSDave Kleikamp 	if (error)
285ac27a0ecSDave Kleikamp 		goto cleanup;
286ac27a0ecSDave Kleikamp 	size = le32_to_cpu(entry->e_value_size);
287ac27a0ecSDave Kleikamp 	if (buffer) {
288ac27a0ecSDave Kleikamp 		error = -ERANGE;
289ac27a0ecSDave Kleikamp 		if (size > buffer_size)
290ac27a0ecSDave Kleikamp 			goto cleanup;
291ac27a0ecSDave Kleikamp 		memcpy(buffer, (void *)IFIRST(header) +
292ac27a0ecSDave Kleikamp 		       le16_to_cpu(entry->e_value_offs), size);
293ac27a0ecSDave Kleikamp 	}
294ac27a0ecSDave Kleikamp 	error = size;
295ac27a0ecSDave Kleikamp 
296ac27a0ecSDave Kleikamp cleanup:
297ac27a0ecSDave Kleikamp 	brelse(iloc.bh);
298ac27a0ecSDave Kleikamp 	return error;
299ac27a0ecSDave Kleikamp }
300ac27a0ecSDave Kleikamp 
301ac27a0ecSDave Kleikamp /*
302617ba13bSMingming Cao  * ext4_xattr_get()
303ac27a0ecSDave Kleikamp  *
304ac27a0ecSDave Kleikamp  * Copy an extended attribute into the buffer
305ac27a0ecSDave Kleikamp  * provided, or compute the buffer size required.
306ac27a0ecSDave Kleikamp  * Buffer is NULL to compute the size of the buffer required.
307ac27a0ecSDave Kleikamp  *
308ac27a0ecSDave Kleikamp  * Returns a negative error number on failure, or the number of bytes
309ac27a0ecSDave Kleikamp  * used / required on success.
310ac27a0ecSDave Kleikamp  */
311ac27a0ecSDave Kleikamp int
312617ba13bSMingming Cao ext4_xattr_get(struct inode *inode, int name_index, const char *name,
313ac27a0ecSDave Kleikamp 	       void *buffer, size_t buffer_size)
314ac27a0ecSDave Kleikamp {
315ac27a0ecSDave Kleikamp 	int error;
316ac27a0ecSDave Kleikamp 
317617ba13bSMingming Cao 	down_read(&EXT4_I(inode)->xattr_sem);
318617ba13bSMingming Cao 	error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
319ac27a0ecSDave Kleikamp 				     buffer_size);
320ac27a0ecSDave Kleikamp 	if (error == -ENODATA)
321617ba13bSMingming Cao 		error = ext4_xattr_block_get(inode, name_index, name, buffer,
322ac27a0ecSDave Kleikamp 					     buffer_size);
323617ba13bSMingming Cao 	up_read(&EXT4_I(inode)->xattr_sem);
324ac27a0ecSDave Kleikamp 	return error;
325ac27a0ecSDave Kleikamp }
326ac27a0ecSDave Kleikamp 
327ac27a0ecSDave Kleikamp static int
328431547b3SChristoph Hellwig ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
329ac27a0ecSDave Kleikamp 			char *buffer, size_t buffer_size)
330ac27a0ecSDave Kleikamp {
331ac27a0ecSDave Kleikamp 	size_t rest = buffer_size;
332ac27a0ecSDave Kleikamp 
333617ba13bSMingming Cao 	for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
334ac27a0ecSDave Kleikamp 		struct xattr_handler *handler =
335617ba13bSMingming Cao 			ext4_xattr_handler(entry->e_name_index);
336ac27a0ecSDave Kleikamp 
337ac27a0ecSDave Kleikamp 		if (handler) {
338431547b3SChristoph Hellwig 			size_t size = handler->list(dentry, buffer, rest,
339ac27a0ecSDave Kleikamp 						    entry->e_name,
340431547b3SChristoph Hellwig 						    entry->e_name_len,
341431547b3SChristoph Hellwig 						    handler->flags);
342ac27a0ecSDave Kleikamp 			if (buffer) {
343ac27a0ecSDave Kleikamp 				if (size > rest)
344ac27a0ecSDave Kleikamp 					return -ERANGE;
345ac27a0ecSDave Kleikamp 				buffer += size;
346ac27a0ecSDave Kleikamp 			}
347ac27a0ecSDave Kleikamp 			rest -= size;
348ac27a0ecSDave Kleikamp 		}
349ac27a0ecSDave Kleikamp 	}
350ac27a0ecSDave Kleikamp 	return buffer_size - rest;
351ac27a0ecSDave Kleikamp }
352ac27a0ecSDave Kleikamp 
353ac27a0ecSDave Kleikamp static int
354431547b3SChristoph Hellwig ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
355ac27a0ecSDave Kleikamp {
356431547b3SChristoph Hellwig 	struct inode *inode = dentry->d_inode;
357ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
358ac27a0ecSDave Kleikamp 	int error;
359ac27a0ecSDave Kleikamp 
360ac27a0ecSDave Kleikamp 	ea_idebug(inode, "buffer=%p, buffer_size=%ld",
361ac27a0ecSDave Kleikamp 		  buffer, (long)buffer_size);
362ac27a0ecSDave Kleikamp 
363ac27a0ecSDave Kleikamp 	error = 0;
364617ba13bSMingming Cao 	if (!EXT4_I(inode)->i_file_acl)
365ac27a0ecSDave Kleikamp 		goto cleanup;
366617ba13bSMingming Cao 	ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
367617ba13bSMingming Cao 	bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
368ac27a0ecSDave Kleikamp 	error = -EIO;
369ac27a0ecSDave Kleikamp 	if (!bh)
370ac27a0ecSDave Kleikamp 		goto cleanup;
371ac27a0ecSDave Kleikamp 	ea_bdebug(bh, "b_count=%d, refcount=%d",
372ac27a0ecSDave Kleikamp 		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
373617ba13bSMingming Cao 	if (ext4_xattr_check_block(bh)) {
37446e665e9SHarvey Harrison 		ext4_error(inode->i_sb, __func__,
3752ae02107SMingming Cao 			   "inode %lu: bad block %llu", inode->i_ino,
376617ba13bSMingming Cao 			   EXT4_I(inode)->i_file_acl);
377ac27a0ecSDave Kleikamp 		error = -EIO;
378ac27a0ecSDave Kleikamp 		goto cleanup;
379ac27a0ecSDave Kleikamp 	}
380617ba13bSMingming Cao 	ext4_xattr_cache_insert(bh);
381431547b3SChristoph Hellwig 	error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
382ac27a0ecSDave Kleikamp 
383ac27a0ecSDave Kleikamp cleanup:
384ac27a0ecSDave Kleikamp 	brelse(bh);
385ac27a0ecSDave Kleikamp 
386ac27a0ecSDave Kleikamp 	return error;
387ac27a0ecSDave Kleikamp }
388ac27a0ecSDave Kleikamp 
389ac27a0ecSDave Kleikamp static int
390431547b3SChristoph Hellwig ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
391ac27a0ecSDave Kleikamp {
392431547b3SChristoph Hellwig 	struct inode *inode = dentry->d_inode;
393617ba13bSMingming Cao 	struct ext4_xattr_ibody_header *header;
394617ba13bSMingming Cao 	struct ext4_inode *raw_inode;
395617ba13bSMingming Cao 	struct ext4_iloc iloc;
396ac27a0ecSDave Kleikamp 	void *end;
397ac27a0ecSDave Kleikamp 	int error;
398ac27a0ecSDave Kleikamp 
399*19f5fb7aSTheodore Ts'o 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
400ac27a0ecSDave Kleikamp 		return 0;
401617ba13bSMingming Cao 	error = ext4_get_inode_loc(inode, &iloc);
402ac27a0ecSDave Kleikamp 	if (error)
403ac27a0ecSDave Kleikamp 		return error;
404617ba13bSMingming Cao 	raw_inode = ext4_raw_inode(&iloc);
405ac27a0ecSDave Kleikamp 	header = IHDR(inode, raw_inode);
406617ba13bSMingming Cao 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
407617ba13bSMingming Cao 	error = ext4_xattr_check_names(IFIRST(header), end);
408ac27a0ecSDave Kleikamp 	if (error)
409ac27a0ecSDave Kleikamp 		goto cleanup;
410431547b3SChristoph Hellwig 	error = ext4_xattr_list_entries(dentry, IFIRST(header),
411ac27a0ecSDave Kleikamp 					buffer, buffer_size);
412ac27a0ecSDave Kleikamp 
413ac27a0ecSDave Kleikamp cleanup:
414ac27a0ecSDave Kleikamp 	brelse(iloc.bh);
415ac27a0ecSDave Kleikamp 	return error;
416ac27a0ecSDave Kleikamp }
417ac27a0ecSDave Kleikamp 
418ac27a0ecSDave Kleikamp /*
419617ba13bSMingming Cao  * ext4_xattr_list()
420ac27a0ecSDave Kleikamp  *
421ac27a0ecSDave Kleikamp  * Copy a list of attribute names into the buffer
422ac27a0ecSDave Kleikamp  * provided, or compute the buffer size required.
423ac27a0ecSDave Kleikamp  * Buffer is NULL to compute the size of the buffer required.
424ac27a0ecSDave Kleikamp  *
425ac27a0ecSDave Kleikamp  * Returns a negative error number on failure, or the number of bytes
426ac27a0ecSDave Kleikamp  * used / required on success.
427ac27a0ecSDave Kleikamp  */
428d3a95d47SMingming Cao static int
429431547b3SChristoph Hellwig ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
430ac27a0ecSDave Kleikamp {
431ac27a0ecSDave Kleikamp 	int i_error, b_error;
432ac27a0ecSDave Kleikamp 
433431547b3SChristoph Hellwig 	down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
434431547b3SChristoph Hellwig 	i_error = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
435ac27a0ecSDave Kleikamp 	if (i_error < 0) {
436ac27a0ecSDave Kleikamp 		b_error = 0;
437ac27a0ecSDave Kleikamp 	} else {
438ac27a0ecSDave Kleikamp 		if (buffer) {
439ac27a0ecSDave Kleikamp 			buffer += i_error;
440ac27a0ecSDave Kleikamp 			buffer_size -= i_error;
441ac27a0ecSDave Kleikamp 		}
442431547b3SChristoph Hellwig 		b_error = ext4_xattr_block_list(dentry, buffer, buffer_size);
443ac27a0ecSDave Kleikamp 		if (b_error < 0)
444ac27a0ecSDave Kleikamp 			i_error = 0;
445ac27a0ecSDave Kleikamp 	}
446431547b3SChristoph Hellwig 	up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
447ac27a0ecSDave Kleikamp 	return i_error + b_error;
448ac27a0ecSDave Kleikamp }
449ac27a0ecSDave Kleikamp 
450ac27a0ecSDave Kleikamp /*
451617ba13bSMingming Cao  * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
452ac27a0ecSDave Kleikamp  * not set, set it.
453ac27a0ecSDave Kleikamp  */
454617ba13bSMingming Cao static void ext4_xattr_update_super_block(handle_t *handle,
455ac27a0ecSDave Kleikamp 					  struct super_block *sb)
456ac27a0ecSDave Kleikamp {
457617ba13bSMingming Cao 	if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
458ac27a0ecSDave Kleikamp 		return;
459ac27a0ecSDave Kleikamp 
460617ba13bSMingming Cao 	if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
461ed2908f3SAndreas Gruenbacher 		EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
462ac27a0ecSDave Kleikamp 		sb->s_dirt = 1;
4630390131bSFrank Mayhar 		ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
464ac27a0ecSDave Kleikamp 	}
465ac27a0ecSDave Kleikamp }
466ac27a0ecSDave Kleikamp 
467ac27a0ecSDave Kleikamp /*
468ac27a0ecSDave Kleikamp  * Release the xattr block BH: If the reference count is > 1, decrement
469ac27a0ecSDave Kleikamp  * it; otherwise free the block.
470ac27a0ecSDave Kleikamp  */
471ac27a0ecSDave Kleikamp static void
472617ba13bSMingming Cao ext4_xattr_release_block(handle_t *handle, struct inode *inode,
473ac27a0ecSDave Kleikamp 			 struct buffer_head *bh)
474ac27a0ecSDave Kleikamp {
475ac27a0ecSDave Kleikamp 	struct mb_cache_entry *ce = NULL;
4768a2bfdcbSMingming Cao 	int error = 0;
477ac27a0ecSDave Kleikamp 
478617ba13bSMingming Cao 	ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
4798a2bfdcbSMingming Cao 	error = ext4_journal_get_write_access(handle, bh);
4808a2bfdcbSMingming Cao 	if (error)
4818a2bfdcbSMingming Cao 		goto out;
4828a2bfdcbSMingming Cao 
4838a2bfdcbSMingming Cao 	lock_buffer(bh);
484ac27a0ecSDave Kleikamp 	if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
485ac27a0ecSDave Kleikamp 		ea_bdebug(bh, "refcount now=0; freeing");
486ac27a0ecSDave Kleikamp 		if (ce)
487ac27a0ecSDave Kleikamp 			mb_cache_entry_free(ce);
488ac27a0ecSDave Kleikamp 		get_bh(bh);
489e6362609STheodore Ts'o 		ext4_free_blocks(handle, inode, bh, 0, 1,
490e6362609STheodore Ts'o 				 EXT4_FREE_BLOCKS_METADATA |
491e6362609STheodore Ts'o 				 EXT4_FREE_BLOCKS_FORGET);
492ac27a0ecSDave Kleikamp 	} else {
493e8546d06SMarcin Slusarz 		le32_add_cpu(&BHDR(bh)->h_refcount, -1);
4940390131bSFrank Mayhar 		error = ext4_handle_dirty_metadata(handle, inode, bh);
495ac27a0ecSDave Kleikamp 		if (IS_SYNC(inode))
4960390131bSFrank Mayhar 			ext4_handle_sync(handle);
497a269eb18SJan Kara 		vfs_dq_free_block(inode, 1);
498ac27a0ecSDave Kleikamp 		ea_bdebug(bh, "refcount now=%d; releasing",
499ac27a0ecSDave Kleikamp 			  le32_to_cpu(BHDR(bh)->h_refcount));
500ac27a0ecSDave Kleikamp 		if (ce)
501ac27a0ecSDave Kleikamp 			mb_cache_entry_release(ce);
502ac27a0ecSDave Kleikamp 	}
5038a2bfdcbSMingming Cao 	unlock_buffer(bh);
5048a2bfdcbSMingming Cao out:
5058a2bfdcbSMingming Cao 	ext4_std_error(inode->i_sb, error);
5068a2bfdcbSMingming Cao 	return;
507ac27a0ecSDave Kleikamp }
508ac27a0ecSDave Kleikamp 
5096dd4ee7cSKalpak Shah /*
5106dd4ee7cSKalpak Shah  * Find the available free space for EAs. This also returns the total number of
5116dd4ee7cSKalpak Shah  * bytes used by EA entries.
5126dd4ee7cSKalpak Shah  */
5136dd4ee7cSKalpak Shah static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
5146dd4ee7cSKalpak Shah 				    size_t *min_offs, void *base, int *total)
5156dd4ee7cSKalpak Shah {
5166dd4ee7cSKalpak Shah 	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
5176dd4ee7cSKalpak Shah 		*total += EXT4_XATTR_LEN(last->e_name_len);
5186dd4ee7cSKalpak Shah 		if (!last->e_value_block && last->e_value_size) {
5196dd4ee7cSKalpak Shah 			size_t offs = le16_to_cpu(last->e_value_offs);
5206dd4ee7cSKalpak Shah 			if (offs < *min_offs)
5216dd4ee7cSKalpak Shah 				*min_offs = offs;
5226dd4ee7cSKalpak Shah 		}
5236dd4ee7cSKalpak Shah 	}
5246dd4ee7cSKalpak Shah 	return (*min_offs - ((void *)last - base) - sizeof(__u32));
5256dd4ee7cSKalpak Shah }
5266dd4ee7cSKalpak Shah 
527617ba13bSMingming Cao struct ext4_xattr_info {
528ac27a0ecSDave Kleikamp 	int name_index;
529ac27a0ecSDave Kleikamp 	const char *name;
530ac27a0ecSDave Kleikamp 	const void *value;
531ac27a0ecSDave Kleikamp 	size_t value_len;
532ac27a0ecSDave Kleikamp };
533ac27a0ecSDave Kleikamp 
534617ba13bSMingming Cao struct ext4_xattr_search {
535617ba13bSMingming Cao 	struct ext4_xattr_entry *first;
536ac27a0ecSDave Kleikamp 	void *base;
537ac27a0ecSDave Kleikamp 	void *end;
538617ba13bSMingming Cao 	struct ext4_xattr_entry *here;
539ac27a0ecSDave Kleikamp 	int not_found;
540ac27a0ecSDave Kleikamp };
541ac27a0ecSDave Kleikamp 
542ac27a0ecSDave Kleikamp static int
543617ba13bSMingming Cao ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
544ac27a0ecSDave Kleikamp {
545617ba13bSMingming Cao 	struct ext4_xattr_entry *last;
546ac27a0ecSDave Kleikamp 	size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
547ac27a0ecSDave Kleikamp 
548ac27a0ecSDave Kleikamp 	/* Compute min_offs and last. */
549ac27a0ecSDave Kleikamp 	last = s->first;
550617ba13bSMingming Cao 	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
551ac27a0ecSDave Kleikamp 		if (!last->e_value_block && last->e_value_size) {
552ac27a0ecSDave Kleikamp 			size_t offs = le16_to_cpu(last->e_value_offs);
553ac27a0ecSDave Kleikamp 			if (offs < min_offs)
554ac27a0ecSDave Kleikamp 				min_offs = offs;
555ac27a0ecSDave Kleikamp 		}
556ac27a0ecSDave Kleikamp 	}
557ac27a0ecSDave Kleikamp 	free = min_offs - ((void *)last - s->base) - sizeof(__u32);
558ac27a0ecSDave Kleikamp 	if (!s->not_found) {
559ac27a0ecSDave Kleikamp 		if (!s->here->e_value_block && s->here->e_value_size) {
560ac27a0ecSDave Kleikamp 			size_t size = le32_to_cpu(s->here->e_value_size);
561617ba13bSMingming Cao 			free += EXT4_XATTR_SIZE(size);
562ac27a0ecSDave Kleikamp 		}
563617ba13bSMingming Cao 		free += EXT4_XATTR_LEN(name_len);
564ac27a0ecSDave Kleikamp 	}
565ac27a0ecSDave Kleikamp 	if (i->value) {
566617ba13bSMingming Cao 		if (free < EXT4_XATTR_SIZE(i->value_len) ||
567617ba13bSMingming Cao 		    free < EXT4_XATTR_LEN(name_len) +
568617ba13bSMingming Cao 			   EXT4_XATTR_SIZE(i->value_len))
569ac27a0ecSDave Kleikamp 			return -ENOSPC;
570ac27a0ecSDave Kleikamp 	}
571ac27a0ecSDave Kleikamp 
572ac27a0ecSDave Kleikamp 	if (i->value && s->not_found) {
573ac27a0ecSDave Kleikamp 		/* Insert the new name. */
574617ba13bSMingming Cao 		size_t size = EXT4_XATTR_LEN(name_len);
575ac27a0ecSDave Kleikamp 		size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
576ac27a0ecSDave Kleikamp 		memmove((void *)s->here + size, s->here, rest);
577ac27a0ecSDave Kleikamp 		memset(s->here, 0, size);
578ac27a0ecSDave Kleikamp 		s->here->e_name_index = i->name_index;
579ac27a0ecSDave Kleikamp 		s->here->e_name_len = name_len;
580ac27a0ecSDave Kleikamp 		memcpy(s->here->e_name, i->name, name_len);
581ac27a0ecSDave Kleikamp 	} else {
582ac27a0ecSDave Kleikamp 		if (!s->here->e_value_block && s->here->e_value_size) {
583ac27a0ecSDave Kleikamp 			void *first_val = s->base + min_offs;
584ac27a0ecSDave Kleikamp 			size_t offs = le16_to_cpu(s->here->e_value_offs);
585ac27a0ecSDave Kleikamp 			void *val = s->base + offs;
586617ba13bSMingming Cao 			size_t size = EXT4_XATTR_SIZE(
587ac27a0ecSDave Kleikamp 				le32_to_cpu(s->here->e_value_size));
588ac27a0ecSDave Kleikamp 
589617ba13bSMingming Cao 			if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
590ac27a0ecSDave Kleikamp 				/* The old and the new value have the same
591ac27a0ecSDave Kleikamp 				   size. Just replace. */
592ac27a0ecSDave Kleikamp 				s->here->e_value_size =
593ac27a0ecSDave Kleikamp 					cpu_to_le32(i->value_len);
594617ba13bSMingming Cao 				memset(val + size - EXT4_XATTR_PAD, 0,
595617ba13bSMingming Cao 				       EXT4_XATTR_PAD); /* Clear pad bytes. */
596ac27a0ecSDave Kleikamp 				memcpy(val, i->value, i->value_len);
597ac27a0ecSDave Kleikamp 				return 0;
598ac27a0ecSDave Kleikamp 			}
599ac27a0ecSDave Kleikamp 
600ac27a0ecSDave Kleikamp 			/* Remove the old value. */
601ac27a0ecSDave Kleikamp 			memmove(first_val + size, first_val, val - first_val);
602ac27a0ecSDave Kleikamp 			memset(first_val, 0, size);
603ac27a0ecSDave Kleikamp 			s->here->e_value_size = 0;
604ac27a0ecSDave Kleikamp 			s->here->e_value_offs = 0;
605ac27a0ecSDave Kleikamp 			min_offs += size;
606ac27a0ecSDave Kleikamp 
607ac27a0ecSDave Kleikamp 			/* Adjust all value offsets. */
608ac27a0ecSDave Kleikamp 			last = s->first;
609ac27a0ecSDave Kleikamp 			while (!IS_LAST_ENTRY(last)) {
610ac27a0ecSDave Kleikamp 				size_t o = le16_to_cpu(last->e_value_offs);
611ac27a0ecSDave Kleikamp 				if (!last->e_value_block &&
612ac27a0ecSDave Kleikamp 				    last->e_value_size && o < offs)
613ac27a0ecSDave Kleikamp 					last->e_value_offs =
614ac27a0ecSDave Kleikamp 						cpu_to_le16(o + size);
615617ba13bSMingming Cao 				last = EXT4_XATTR_NEXT(last);
616ac27a0ecSDave Kleikamp 			}
617ac27a0ecSDave Kleikamp 		}
618ac27a0ecSDave Kleikamp 		if (!i->value) {
619ac27a0ecSDave Kleikamp 			/* Remove the old name. */
620617ba13bSMingming Cao 			size_t size = EXT4_XATTR_LEN(name_len);
621ac27a0ecSDave Kleikamp 			last = ENTRY((void *)last - size);
622ac27a0ecSDave Kleikamp 			memmove(s->here, (void *)s->here + size,
623ac27a0ecSDave Kleikamp 				(void *)last - (void *)s->here + sizeof(__u32));
624ac27a0ecSDave Kleikamp 			memset(last, 0, size);
625ac27a0ecSDave Kleikamp 		}
626ac27a0ecSDave Kleikamp 	}
627ac27a0ecSDave Kleikamp 
628ac27a0ecSDave Kleikamp 	if (i->value) {
629ac27a0ecSDave Kleikamp 		/* Insert the new value. */
630ac27a0ecSDave Kleikamp 		s->here->e_value_size = cpu_to_le32(i->value_len);
631ac27a0ecSDave Kleikamp 		if (i->value_len) {
632617ba13bSMingming Cao 			size_t size = EXT4_XATTR_SIZE(i->value_len);
633ac27a0ecSDave Kleikamp 			void *val = s->base + min_offs - size;
634ac27a0ecSDave Kleikamp 			s->here->e_value_offs = cpu_to_le16(min_offs - size);
635617ba13bSMingming Cao 			memset(val + size - EXT4_XATTR_PAD, 0,
636617ba13bSMingming Cao 			       EXT4_XATTR_PAD); /* Clear the pad bytes. */
637ac27a0ecSDave Kleikamp 			memcpy(val, i->value, i->value_len);
638ac27a0ecSDave Kleikamp 		}
639ac27a0ecSDave Kleikamp 	}
640ac27a0ecSDave Kleikamp 	return 0;
641ac27a0ecSDave Kleikamp }
642ac27a0ecSDave Kleikamp 
643617ba13bSMingming Cao struct ext4_xattr_block_find {
644617ba13bSMingming Cao 	struct ext4_xattr_search s;
645ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
646ac27a0ecSDave Kleikamp };
647ac27a0ecSDave Kleikamp 
648ac27a0ecSDave Kleikamp static int
649617ba13bSMingming Cao ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
650617ba13bSMingming Cao 		      struct ext4_xattr_block_find *bs)
651ac27a0ecSDave Kleikamp {
652ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
653ac27a0ecSDave Kleikamp 	int error;
654ac27a0ecSDave Kleikamp 
655ac27a0ecSDave Kleikamp 	ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
656ac27a0ecSDave Kleikamp 		  i->name_index, i->name, i->value, (long)i->value_len);
657ac27a0ecSDave Kleikamp 
658617ba13bSMingming Cao 	if (EXT4_I(inode)->i_file_acl) {
659ac27a0ecSDave Kleikamp 		/* The inode already has an extended attribute block. */
660617ba13bSMingming Cao 		bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
661ac27a0ecSDave Kleikamp 		error = -EIO;
662ac27a0ecSDave Kleikamp 		if (!bs->bh)
663ac27a0ecSDave Kleikamp 			goto cleanup;
664ac27a0ecSDave Kleikamp 		ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
665ac27a0ecSDave Kleikamp 			atomic_read(&(bs->bh->b_count)),
666ac27a0ecSDave Kleikamp 			le32_to_cpu(BHDR(bs->bh)->h_refcount));
667617ba13bSMingming Cao 		if (ext4_xattr_check_block(bs->bh)) {
66846e665e9SHarvey Harrison 			ext4_error(sb, __func__,
6692ae02107SMingming Cao 				"inode %lu: bad block %llu", inode->i_ino,
670617ba13bSMingming Cao 				EXT4_I(inode)->i_file_acl);
671ac27a0ecSDave Kleikamp 			error = -EIO;
672ac27a0ecSDave Kleikamp 			goto cleanup;
673ac27a0ecSDave Kleikamp 		}
674ac27a0ecSDave Kleikamp 		/* Find the named attribute. */
675ac27a0ecSDave Kleikamp 		bs->s.base = BHDR(bs->bh);
676ac27a0ecSDave Kleikamp 		bs->s.first = BFIRST(bs->bh);
677ac27a0ecSDave Kleikamp 		bs->s.end = bs->bh->b_data + bs->bh->b_size;
678ac27a0ecSDave Kleikamp 		bs->s.here = bs->s.first;
679617ba13bSMingming Cao 		error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
680ac27a0ecSDave Kleikamp 					      i->name, bs->bh->b_size, 1);
681ac27a0ecSDave Kleikamp 		if (error && error != -ENODATA)
682ac27a0ecSDave Kleikamp 			goto cleanup;
683ac27a0ecSDave Kleikamp 		bs->s.not_found = error;
684ac27a0ecSDave Kleikamp 	}
685ac27a0ecSDave Kleikamp 	error = 0;
686ac27a0ecSDave Kleikamp 
687ac27a0ecSDave Kleikamp cleanup:
688ac27a0ecSDave Kleikamp 	return error;
689ac27a0ecSDave Kleikamp }
690ac27a0ecSDave Kleikamp 
691ac27a0ecSDave Kleikamp static int
692617ba13bSMingming Cao ext4_xattr_block_set(handle_t *handle, struct inode *inode,
693617ba13bSMingming Cao 		     struct ext4_xattr_info *i,
694617ba13bSMingming Cao 		     struct ext4_xattr_block_find *bs)
695ac27a0ecSDave Kleikamp {
696ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
697ac27a0ecSDave Kleikamp 	struct buffer_head *new_bh = NULL;
698617ba13bSMingming Cao 	struct ext4_xattr_search *s = &bs->s;
699ac27a0ecSDave Kleikamp 	struct mb_cache_entry *ce = NULL;
7008a2bfdcbSMingming Cao 	int error = 0;
701ac27a0ecSDave Kleikamp 
702617ba13bSMingming Cao #define header(x) ((struct ext4_xattr_header *)(x))
703ac27a0ecSDave Kleikamp 
704ac27a0ecSDave Kleikamp 	if (i->value && i->value_len > sb->s_blocksize)
705ac27a0ecSDave Kleikamp 		return -ENOSPC;
706ac27a0ecSDave Kleikamp 	if (s->base) {
707617ba13bSMingming Cao 		ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
708ac27a0ecSDave Kleikamp 					bs->bh->b_blocknr);
7098a2bfdcbSMingming Cao 		error = ext4_journal_get_write_access(handle, bs->bh);
7108a2bfdcbSMingming Cao 		if (error)
7118a2bfdcbSMingming Cao 			goto cleanup;
7128a2bfdcbSMingming Cao 		lock_buffer(bs->bh);
7138a2bfdcbSMingming Cao 
714ac27a0ecSDave Kleikamp 		if (header(s->base)->h_refcount == cpu_to_le32(1)) {
715ac27a0ecSDave Kleikamp 			if (ce) {
716ac27a0ecSDave Kleikamp 				mb_cache_entry_free(ce);
717ac27a0ecSDave Kleikamp 				ce = NULL;
718ac27a0ecSDave Kleikamp 			}
719ac27a0ecSDave Kleikamp 			ea_bdebug(bs->bh, "modifying in-place");
720617ba13bSMingming Cao 			error = ext4_xattr_set_entry(i, s);
721ac27a0ecSDave Kleikamp 			if (!error) {
722ac27a0ecSDave Kleikamp 				if (!IS_LAST_ENTRY(s->first))
723617ba13bSMingming Cao 					ext4_xattr_rehash(header(s->base),
724ac27a0ecSDave Kleikamp 							  s->here);
725617ba13bSMingming Cao 				ext4_xattr_cache_insert(bs->bh);
726ac27a0ecSDave Kleikamp 			}
727ac27a0ecSDave Kleikamp 			unlock_buffer(bs->bh);
728ac27a0ecSDave Kleikamp 			if (error == -EIO)
729ac27a0ecSDave Kleikamp 				goto bad_block;
730ac27a0ecSDave Kleikamp 			if (!error)
7310390131bSFrank Mayhar 				error = ext4_handle_dirty_metadata(handle,
7320390131bSFrank Mayhar 								   inode,
733ac27a0ecSDave Kleikamp 								   bs->bh);
734ac27a0ecSDave Kleikamp 			if (error)
735ac27a0ecSDave Kleikamp 				goto cleanup;
736ac27a0ecSDave Kleikamp 			goto inserted;
737ac27a0ecSDave Kleikamp 		} else {
738ac27a0ecSDave Kleikamp 			int offset = (char *)s->here - bs->bh->b_data;
739ac27a0ecSDave Kleikamp 
7408a2bfdcbSMingming Cao 			unlock_buffer(bs->bh);
7418a2bfdcbSMingming Cao 			jbd2_journal_release_buffer(handle, bs->bh);
742ac27a0ecSDave Kleikamp 			if (ce) {
743ac27a0ecSDave Kleikamp 				mb_cache_entry_release(ce);
744ac27a0ecSDave Kleikamp 				ce = NULL;
745ac27a0ecSDave Kleikamp 			}
746ac27a0ecSDave Kleikamp 			ea_bdebug(bs->bh, "cloning");
747216553c4SJosef Bacik 			s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
748ac27a0ecSDave Kleikamp 			error = -ENOMEM;
749ac27a0ecSDave Kleikamp 			if (s->base == NULL)
750ac27a0ecSDave Kleikamp 				goto cleanup;
751ac27a0ecSDave Kleikamp 			memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
752ac27a0ecSDave Kleikamp 			s->first = ENTRY(header(s->base)+1);
753ac27a0ecSDave Kleikamp 			header(s->base)->h_refcount = cpu_to_le32(1);
754ac27a0ecSDave Kleikamp 			s->here = ENTRY(s->base + offset);
755ac27a0ecSDave Kleikamp 			s->end = s->base + bs->bh->b_size;
756ac27a0ecSDave Kleikamp 		}
757ac27a0ecSDave Kleikamp 	} else {
758ac27a0ecSDave Kleikamp 		/* Allocate a buffer where we construct the new block. */
759216553c4SJosef Bacik 		s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
760ac27a0ecSDave Kleikamp 		/* assert(header == s->base) */
761ac27a0ecSDave Kleikamp 		error = -ENOMEM;
762ac27a0ecSDave Kleikamp 		if (s->base == NULL)
763ac27a0ecSDave Kleikamp 			goto cleanup;
764617ba13bSMingming Cao 		header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
765ac27a0ecSDave Kleikamp 		header(s->base)->h_blocks = cpu_to_le32(1);
766ac27a0ecSDave Kleikamp 		header(s->base)->h_refcount = cpu_to_le32(1);
767ac27a0ecSDave Kleikamp 		s->first = ENTRY(header(s->base)+1);
768ac27a0ecSDave Kleikamp 		s->here = ENTRY(header(s->base)+1);
769ac27a0ecSDave Kleikamp 		s->end = s->base + sb->s_blocksize;
770ac27a0ecSDave Kleikamp 	}
771ac27a0ecSDave Kleikamp 
772617ba13bSMingming Cao 	error = ext4_xattr_set_entry(i, s);
773ac27a0ecSDave Kleikamp 	if (error == -EIO)
774ac27a0ecSDave Kleikamp 		goto bad_block;
775ac27a0ecSDave Kleikamp 	if (error)
776ac27a0ecSDave Kleikamp 		goto cleanup;
777ac27a0ecSDave Kleikamp 	if (!IS_LAST_ENTRY(s->first))
778617ba13bSMingming Cao 		ext4_xattr_rehash(header(s->base), s->here);
779ac27a0ecSDave Kleikamp 
780ac27a0ecSDave Kleikamp inserted:
781ac27a0ecSDave Kleikamp 	if (!IS_LAST_ENTRY(s->first)) {
782617ba13bSMingming Cao 		new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
783ac27a0ecSDave Kleikamp 		if (new_bh) {
784ac27a0ecSDave Kleikamp 			/* We found an identical block in the cache. */
785ac27a0ecSDave Kleikamp 			if (new_bh == bs->bh)
786ac27a0ecSDave Kleikamp 				ea_bdebug(new_bh, "keeping");
787ac27a0ecSDave Kleikamp 			else {
788ac27a0ecSDave Kleikamp 				/* The old block is released after updating
789ac27a0ecSDave Kleikamp 				   the inode. */
790ac27a0ecSDave Kleikamp 				error = -EDQUOT;
791a269eb18SJan Kara 				if (vfs_dq_alloc_block(inode, 1))
792ac27a0ecSDave Kleikamp 					goto cleanup;
793617ba13bSMingming Cao 				error = ext4_journal_get_write_access(handle,
794ac27a0ecSDave Kleikamp 								      new_bh);
795ac27a0ecSDave Kleikamp 				if (error)
796ac27a0ecSDave Kleikamp 					goto cleanup_dquot;
797ac27a0ecSDave Kleikamp 				lock_buffer(new_bh);
798e8546d06SMarcin Slusarz 				le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
799ac27a0ecSDave Kleikamp 				ea_bdebug(new_bh, "reusing; refcount now=%d",
800ac27a0ecSDave Kleikamp 					le32_to_cpu(BHDR(new_bh)->h_refcount));
801ac27a0ecSDave Kleikamp 				unlock_buffer(new_bh);
8020390131bSFrank Mayhar 				error = ext4_handle_dirty_metadata(handle,
8030390131bSFrank Mayhar 								   inode,
804ac27a0ecSDave Kleikamp 								   new_bh);
805ac27a0ecSDave Kleikamp 				if (error)
806ac27a0ecSDave Kleikamp 					goto cleanup_dquot;
807ac27a0ecSDave Kleikamp 			}
808ac27a0ecSDave Kleikamp 			mb_cache_entry_release(ce);
809ac27a0ecSDave Kleikamp 			ce = NULL;
810ac27a0ecSDave Kleikamp 		} else if (bs->bh && s->base == bs->bh->b_data) {
811ac27a0ecSDave Kleikamp 			/* We were modifying this block in-place. */
812ac27a0ecSDave Kleikamp 			ea_bdebug(bs->bh, "keeping this block");
813ac27a0ecSDave Kleikamp 			new_bh = bs->bh;
814ac27a0ecSDave Kleikamp 			get_bh(new_bh);
815ac27a0ecSDave Kleikamp 		} else {
816ac27a0ecSDave Kleikamp 			/* We need to allocate a new block */
817fb0a387dSEric Sandeen 			ext4_fsblk_t goal, block;
818fb0a387dSEric Sandeen 
819fb0a387dSEric Sandeen 			goal = ext4_group_first_block_no(sb,
820d00a6d7bSAkinobu Mita 						EXT4_I(inode)->i_block_group);
821fb0a387dSEric Sandeen 
822fb0a387dSEric Sandeen 			/* non-extent files can't have physical blocks past 2^32 */
823fb0a387dSEric Sandeen 			if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
824fb0a387dSEric Sandeen 				goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
825fb0a387dSEric Sandeen 
826fb0a387dSEric Sandeen 			block = ext4_new_meta_blocks(handle, inode,
82797df5d15STheodore Ts'o 						  goal, NULL, &error);
828ac27a0ecSDave Kleikamp 			if (error)
829ac27a0ecSDave Kleikamp 				goto cleanup;
830fb0a387dSEric Sandeen 
831fb0a387dSEric Sandeen 			if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
832fb0a387dSEric Sandeen 				BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
833fb0a387dSEric Sandeen 
834ac27a0ecSDave Kleikamp 			ea_idebug(inode, "creating block %d", block);
835ac27a0ecSDave Kleikamp 
836ac27a0ecSDave Kleikamp 			new_bh = sb_getblk(sb, block);
837ac27a0ecSDave Kleikamp 			if (!new_bh) {
838ac27a0ecSDave Kleikamp getblk_failed:
839e6362609STheodore Ts'o 				ext4_free_blocks(handle, inode, 0, block, 1,
840e6362609STheodore Ts'o 						 EXT4_FREE_BLOCKS_METADATA);
841ac27a0ecSDave Kleikamp 				error = -EIO;
842ac27a0ecSDave Kleikamp 				goto cleanup;
843ac27a0ecSDave Kleikamp 			}
844ac27a0ecSDave Kleikamp 			lock_buffer(new_bh);
845617ba13bSMingming Cao 			error = ext4_journal_get_create_access(handle, new_bh);
846ac27a0ecSDave Kleikamp 			if (error) {
847ac27a0ecSDave Kleikamp 				unlock_buffer(new_bh);
848ac27a0ecSDave Kleikamp 				goto getblk_failed;
849ac27a0ecSDave Kleikamp 			}
850ac27a0ecSDave Kleikamp 			memcpy(new_bh->b_data, s->base, new_bh->b_size);
851ac27a0ecSDave Kleikamp 			set_buffer_uptodate(new_bh);
852ac27a0ecSDave Kleikamp 			unlock_buffer(new_bh);
853617ba13bSMingming Cao 			ext4_xattr_cache_insert(new_bh);
8540390131bSFrank Mayhar 			error = ext4_handle_dirty_metadata(handle,
8550390131bSFrank Mayhar 							   inode, new_bh);
856ac27a0ecSDave Kleikamp 			if (error)
857ac27a0ecSDave Kleikamp 				goto cleanup;
858ac27a0ecSDave Kleikamp 		}
859ac27a0ecSDave Kleikamp 	}
860ac27a0ecSDave Kleikamp 
861ac27a0ecSDave Kleikamp 	/* Update the inode. */
862617ba13bSMingming Cao 	EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
863ac27a0ecSDave Kleikamp 
864ac27a0ecSDave Kleikamp 	/* Drop the previous xattr block. */
865ac27a0ecSDave Kleikamp 	if (bs->bh && bs->bh != new_bh)
866617ba13bSMingming Cao 		ext4_xattr_release_block(handle, inode, bs->bh);
867ac27a0ecSDave Kleikamp 	error = 0;
868ac27a0ecSDave Kleikamp 
869ac27a0ecSDave Kleikamp cleanup:
870ac27a0ecSDave Kleikamp 	if (ce)
871ac27a0ecSDave Kleikamp 		mb_cache_entry_release(ce);
872ac27a0ecSDave Kleikamp 	brelse(new_bh);
873ac27a0ecSDave Kleikamp 	if (!(bs->bh && s->base == bs->bh->b_data))
874ac27a0ecSDave Kleikamp 		kfree(s->base);
875ac27a0ecSDave Kleikamp 
876ac27a0ecSDave Kleikamp 	return error;
877ac27a0ecSDave Kleikamp 
878ac27a0ecSDave Kleikamp cleanup_dquot:
879a269eb18SJan Kara 	vfs_dq_free_block(inode, 1);
880ac27a0ecSDave Kleikamp 	goto cleanup;
881ac27a0ecSDave Kleikamp 
882ac27a0ecSDave Kleikamp bad_block:
88346e665e9SHarvey Harrison 	ext4_error(inode->i_sb, __func__,
8842ae02107SMingming Cao 		   "inode %lu: bad block %llu", inode->i_ino,
885617ba13bSMingming Cao 		   EXT4_I(inode)->i_file_acl);
886ac27a0ecSDave Kleikamp 	goto cleanup;
887ac27a0ecSDave Kleikamp 
888ac27a0ecSDave Kleikamp #undef header
889ac27a0ecSDave Kleikamp }
890ac27a0ecSDave Kleikamp 
891617ba13bSMingming Cao struct ext4_xattr_ibody_find {
892617ba13bSMingming Cao 	struct ext4_xattr_search s;
893617ba13bSMingming Cao 	struct ext4_iloc iloc;
894ac27a0ecSDave Kleikamp };
895ac27a0ecSDave Kleikamp 
896ac27a0ecSDave Kleikamp static int
897617ba13bSMingming Cao ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
898617ba13bSMingming Cao 		      struct ext4_xattr_ibody_find *is)
899ac27a0ecSDave Kleikamp {
900617ba13bSMingming Cao 	struct ext4_xattr_ibody_header *header;
901617ba13bSMingming Cao 	struct ext4_inode *raw_inode;
902ac27a0ecSDave Kleikamp 	int error;
903ac27a0ecSDave Kleikamp 
904617ba13bSMingming Cao 	if (EXT4_I(inode)->i_extra_isize == 0)
905ac27a0ecSDave Kleikamp 		return 0;
906617ba13bSMingming Cao 	raw_inode = ext4_raw_inode(&is->iloc);
907ac27a0ecSDave Kleikamp 	header = IHDR(inode, raw_inode);
908ac27a0ecSDave Kleikamp 	is->s.base = is->s.first = IFIRST(header);
909ac27a0ecSDave Kleikamp 	is->s.here = is->s.first;
910617ba13bSMingming Cao 	is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
911*19f5fb7aSTheodore Ts'o 	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
912617ba13bSMingming Cao 		error = ext4_xattr_check_names(IFIRST(header), is->s.end);
913ac27a0ecSDave Kleikamp 		if (error)
914ac27a0ecSDave Kleikamp 			return error;
915ac27a0ecSDave Kleikamp 		/* Find the named attribute. */
916617ba13bSMingming Cao 		error = ext4_xattr_find_entry(&is->s.here, i->name_index,
917ac27a0ecSDave Kleikamp 					      i->name, is->s.end -
918ac27a0ecSDave Kleikamp 					      (void *)is->s.base, 0);
919ac27a0ecSDave Kleikamp 		if (error && error != -ENODATA)
920ac27a0ecSDave Kleikamp 			return error;
921ac27a0ecSDave Kleikamp 		is->s.not_found = error;
922ac27a0ecSDave Kleikamp 	}
923ac27a0ecSDave Kleikamp 	return 0;
924ac27a0ecSDave Kleikamp }
925ac27a0ecSDave Kleikamp 
926ac27a0ecSDave Kleikamp static int
927617ba13bSMingming Cao ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
928617ba13bSMingming Cao 		     struct ext4_xattr_info *i,
929617ba13bSMingming Cao 		     struct ext4_xattr_ibody_find *is)
930ac27a0ecSDave Kleikamp {
931617ba13bSMingming Cao 	struct ext4_xattr_ibody_header *header;
932617ba13bSMingming Cao 	struct ext4_xattr_search *s = &is->s;
933ac27a0ecSDave Kleikamp 	int error;
934ac27a0ecSDave Kleikamp 
935617ba13bSMingming Cao 	if (EXT4_I(inode)->i_extra_isize == 0)
936ac27a0ecSDave Kleikamp 		return -ENOSPC;
937617ba13bSMingming Cao 	error = ext4_xattr_set_entry(i, s);
938ac27a0ecSDave Kleikamp 	if (error)
939ac27a0ecSDave Kleikamp 		return error;
940617ba13bSMingming Cao 	header = IHDR(inode, ext4_raw_inode(&is->iloc));
941ac27a0ecSDave Kleikamp 	if (!IS_LAST_ENTRY(s->first)) {
942617ba13bSMingming Cao 		header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
943*19f5fb7aSTheodore Ts'o 		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
944ac27a0ecSDave Kleikamp 	} else {
945ac27a0ecSDave Kleikamp 		header->h_magic = cpu_to_le32(0);
946*19f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
947ac27a0ecSDave Kleikamp 	}
948ac27a0ecSDave Kleikamp 	return 0;
949ac27a0ecSDave Kleikamp }
950ac27a0ecSDave Kleikamp 
951ac27a0ecSDave Kleikamp /*
952617ba13bSMingming Cao  * ext4_xattr_set_handle()
953ac27a0ecSDave Kleikamp  *
954ac27a0ecSDave Kleikamp  * Create, replace or remove an extended attribute for this inode. Buffer
955ac27a0ecSDave Kleikamp  * is NULL to remove an existing extended attribute, and non-NULL to
956ac27a0ecSDave Kleikamp  * either replace an existing extended attribute, or create a new extended
957ac27a0ecSDave Kleikamp  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
958ac27a0ecSDave Kleikamp  * specify that an extended attribute must exist and must not exist
959ac27a0ecSDave Kleikamp  * previous to the call, respectively.
960ac27a0ecSDave Kleikamp  *
961ac27a0ecSDave Kleikamp  * Returns 0, or a negative error number on failure.
962ac27a0ecSDave Kleikamp  */
963ac27a0ecSDave Kleikamp int
964617ba13bSMingming Cao ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
965ac27a0ecSDave Kleikamp 		      const char *name, const void *value, size_t value_len,
966ac27a0ecSDave Kleikamp 		      int flags)
967ac27a0ecSDave Kleikamp {
968617ba13bSMingming Cao 	struct ext4_xattr_info i = {
969ac27a0ecSDave Kleikamp 		.name_index = name_index,
970ac27a0ecSDave Kleikamp 		.name = name,
971ac27a0ecSDave Kleikamp 		.value = value,
972ac27a0ecSDave Kleikamp 		.value_len = value_len,
973ac27a0ecSDave Kleikamp 
974ac27a0ecSDave Kleikamp 	};
975617ba13bSMingming Cao 	struct ext4_xattr_ibody_find is = {
976ac27a0ecSDave Kleikamp 		.s = { .not_found = -ENODATA, },
977ac27a0ecSDave Kleikamp 	};
978617ba13bSMingming Cao 	struct ext4_xattr_block_find bs = {
979ac27a0ecSDave Kleikamp 		.s = { .not_found = -ENODATA, },
980ac27a0ecSDave Kleikamp 	};
9814d20c685SKalpak Shah 	unsigned long no_expand;
982ac27a0ecSDave Kleikamp 	int error;
983ac27a0ecSDave Kleikamp 
984ac27a0ecSDave Kleikamp 	if (!name)
985ac27a0ecSDave Kleikamp 		return -EINVAL;
986ac27a0ecSDave Kleikamp 	if (strlen(name) > 255)
987ac27a0ecSDave Kleikamp 		return -ERANGE;
988617ba13bSMingming Cao 	down_write(&EXT4_I(inode)->xattr_sem);
989*19f5fb7aSTheodore Ts'o 	no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
990*19f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
9914d20c685SKalpak Shah 
992617ba13bSMingming Cao 	error = ext4_get_inode_loc(inode, &is.iloc);
993ac27a0ecSDave Kleikamp 	if (error)
994ac27a0ecSDave Kleikamp 		goto cleanup;
995ac27a0ecSDave Kleikamp 
99686ebfd08SEric Sandeen 	error = ext4_journal_get_write_access(handle, is.iloc.bh);
99786ebfd08SEric Sandeen 	if (error)
99886ebfd08SEric Sandeen 		goto cleanup;
99986ebfd08SEric Sandeen 
1000*19f5fb7aSTheodore Ts'o 	if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1001617ba13bSMingming Cao 		struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1002617ba13bSMingming Cao 		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1003*19f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1004ac27a0ecSDave Kleikamp 	}
1005ac27a0ecSDave Kleikamp 
1006617ba13bSMingming Cao 	error = ext4_xattr_ibody_find(inode, &i, &is);
1007ac27a0ecSDave Kleikamp 	if (error)
1008ac27a0ecSDave Kleikamp 		goto cleanup;
1009ac27a0ecSDave Kleikamp 	if (is.s.not_found)
1010617ba13bSMingming Cao 		error = ext4_xattr_block_find(inode, &i, &bs);
1011ac27a0ecSDave Kleikamp 	if (error)
1012ac27a0ecSDave Kleikamp 		goto cleanup;
1013ac27a0ecSDave Kleikamp 	if (is.s.not_found && bs.s.not_found) {
1014ac27a0ecSDave Kleikamp 		error = -ENODATA;
1015ac27a0ecSDave Kleikamp 		if (flags & XATTR_REPLACE)
1016ac27a0ecSDave Kleikamp 			goto cleanup;
1017ac27a0ecSDave Kleikamp 		error = 0;
1018ac27a0ecSDave Kleikamp 		if (!value)
1019ac27a0ecSDave Kleikamp 			goto cleanup;
1020ac27a0ecSDave Kleikamp 	} else {
1021ac27a0ecSDave Kleikamp 		error = -EEXIST;
1022ac27a0ecSDave Kleikamp 		if (flags & XATTR_CREATE)
1023ac27a0ecSDave Kleikamp 			goto cleanup;
1024ac27a0ecSDave Kleikamp 	}
1025ac27a0ecSDave Kleikamp 	if (!value) {
1026ac27a0ecSDave Kleikamp 		if (!is.s.not_found)
1027617ba13bSMingming Cao 			error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1028ac27a0ecSDave Kleikamp 		else if (!bs.s.not_found)
1029617ba13bSMingming Cao 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
1030ac27a0ecSDave Kleikamp 	} else {
1031617ba13bSMingming Cao 		error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1032ac27a0ecSDave Kleikamp 		if (!error && !bs.s.not_found) {
1033ac27a0ecSDave Kleikamp 			i.value = NULL;
1034617ba13bSMingming Cao 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
1035ac27a0ecSDave Kleikamp 		} else if (error == -ENOSPC) {
10367e01c8e5STiger Yang 			if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
10377e01c8e5STiger Yang 				error = ext4_xattr_block_find(inode, &i, &bs);
10387e01c8e5STiger Yang 				if (error)
10397e01c8e5STiger Yang 					goto cleanup;
10407e01c8e5STiger Yang 			}
1041617ba13bSMingming Cao 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
1042ac27a0ecSDave Kleikamp 			if (error)
1043ac27a0ecSDave Kleikamp 				goto cleanup;
1044ac27a0ecSDave Kleikamp 			if (!is.s.not_found) {
1045ac27a0ecSDave Kleikamp 				i.value = NULL;
1046617ba13bSMingming Cao 				error = ext4_xattr_ibody_set(handle, inode, &i,
1047ac27a0ecSDave Kleikamp 							     &is);
1048ac27a0ecSDave Kleikamp 			}
1049ac27a0ecSDave Kleikamp 		}
1050ac27a0ecSDave Kleikamp 	}
1051ac27a0ecSDave Kleikamp 	if (!error) {
1052617ba13bSMingming Cao 		ext4_xattr_update_super_block(handle, inode->i_sb);
1053ef7f3835SKalpak Shah 		inode->i_ctime = ext4_current_time(inode);
10546dd4ee7cSKalpak Shah 		if (!value)
1055*19f5fb7aSTheodore Ts'o 			ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1056617ba13bSMingming Cao 		error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1057ac27a0ecSDave Kleikamp 		/*
1058617ba13bSMingming Cao 		 * The bh is consumed by ext4_mark_iloc_dirty, even with
1059ac27a0ecSDave Kleikamp 		 * error != 0.
1060ac27a0ecSDave Kleikamp 		 */
1061ac27a0ecSDave Kleikamp 		is.iloc.bh = NULL;
1062ac27a0ecSDave Kleikamp 		if (IS_SYNC(inode))
10630390131bSFrank Mayhar 			ext4_handle_sync(handle);
1064ac27a0ecSDave Kleikamp 	}
1065ac27a0ecSDave Kleikamp 
1066ac27a0ecSDave Kleikamp cleanup:
1067ac27a0ecSDave Kleikamp 	brelse(is.iloc.bh);
1068ac27a0ecSDave Kleikamp 	brelse(bs.bh);
10694d20c685SKalpak Shah 	if (no_expand == 0)
1070*19f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1071617ba13bSMingming Cao 	up_write(&EXT4_I(inode)->xattr_sem);
1072ac27a0ecSDave Kleikamp 	return error;
1073ac27a0ecSDave Kleikamp }
1074ac27a0ecSDave Kleikamp 
1075ac27a0ecSDave Kleikamp /*
1076617ba13bSMingming Cao  * ext4_xattr_set()
1077ac27a0ecSDave Kleikamp  *
1078617ba13bSMingming Cao  * Like ext4_xattr_set_handle, but start from an inode. This extended
1079ac27a0ecSDave Kleikamp  * attribute modification is a filesystem transaction by itself.
1080ac27a0ecSDave Kleikamp  *
1081ac27a0ecSDave Kleikamp  * Returns 0, or a negative error number on failure.
1082ac27a0ecSDave Kleikamp  */
1083ac27a0ecSDave Kleikamp int
1084617ba13bSMingming Cao ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1085ac27a0ecSDave Kleikamp 	       const void *value, size_t value_len, int flags)
1086ac27a0ecSDave Kleikamp {
1087ac27a0ecSDave Kleikamp 	handle_t *handle;
1088ac27a0ecSDave Kleikamp 	int error, retries = 0;
1089ac27a0ecSDave Kleikamp 
1090ac27a0ecSDave Kleikamp retry:
1091617ba13bSMingming Cao 	handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
1092ac27a0ecSDave Kleikamp 	if (IS_ERR(handle)) {
1093ac27a0ecSDave Kleikamp 		error = PTR_ERR(handle);
1094ac27a0ecSDave Kleikamp 	} else {
1095ac27a0ecSDave Kleikamp 		int error2;
1096ac27a0ecSDave Kleikamp 
1097617ba13bSMingming Cao 		error = ext4_xattr_set_handle(handle, inode, name_index, name,
1098ac27a0ecSDave Kleikamp 					      value, value_len, flags);
1099617ba13bSMingming Cao 		error2 = ext4_journal_stop(handle);
1100ac27a0ecSDave Kleikamp 		if (error == -ENOSPC &&
1101617ba13bSMingming Cao 		    ext4_should_retry_alloc(inode->i_sb, &retries))
1102ac27a0ecSDave Kleikamp 			goto retry;
1103ac27a0ecSDave Kleikamp 		if (error == 0)
1104ac27a0ecSDave Kleikamp 			error = error2;
1105ac27a0ecSDave Kleikamp 	}
1106ac27a0ecSDave Kleikamp 
1107ac27a0ecSDave Kleikamp 	return error;
1108ac27a0ecSDave Kleikamp }
1109ac27a0ecSDave Kleikamp 
1110ac27a0ecSDave Kleikamp /*
11116dd4ee7cSKalpak Shah  * Shift the EA entries in the inode to create space for the increased
11126dd4ee7cSKalpak Shah  * i_extra_isize.
11136dd4ee7cSKalpak Shah  */
11146dd4ee7cSKalpak Shah static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
11156dd4ee7cSKalpak Shah 				     int value_offs_shift, void *to,
11166dd4ee7cSKalpak Shah 				     void *from, size_t n, int blocksize)
11176dd4ee7cSKalpak Shah {
11186dd4ee7cSKalpak Shah 	struct ext4_xattr_entry *last = entry;
11196dd4ee7cSKalpak Shah 	int new_offs;
11206dd4ee7cSKalpak Shah 
11216dd4ee7cSKalpak Shah 	/* Adjust the value offsets of the entries */
11226dd4ee7cSKalpak Shah 	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
11236dd4ee7cSKalpak Shah 		if (!last->e_value_block && last->e_value_size) {
11246dd4ee7cSKalpak Shah 			new_offs = le16_to_cpu(last->e_value_offs) +
11256dd4ee7cSKalpak Shah 							value_offs_shift;
11266dd4ee7cSKalpak Shah 			BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
11276dd4ee7cSKalpak Shah 				 > blocksize);
11286dd4ee7cSKalpak Shah 			last->e_value_offs = cpu_to_le16(new_offs);
11296dd4ee7cSKalpak Shah 		}
11306dd4ee7cSKalpak Shah 	}
11316dd4ee7cSKalpak Shah 	/* Shift the entries by n bytes */
11326dd4ee7cSKalpak Shah 	memmove(to, from, n);
11336dd4ee7cSKalpak Shah }
11346dd4ee7cSKalpak Shah 
11356dd4ee7cSKalpak Shah /*
11366dd4ee7cSKalpak Shah  * Expand an inode by new_extra_isize bytes when EAs are present.
11376dd4ee7cSKalpak Shah  * Returns 0 on success or negative error number on failure.
11386dd4ee7cSKalpak Shah  */
11396dd4ee7cSKalpak Shah int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
11406dd4ee7cSKalpak Shah 			       struct ext4_inode *raw_inode, handle_t *handle)
11416dd4ee7cSKalpak Shah {
11426dd4ee7cSKalpak Shah 	struct ext4_xattr_ibody_header *header;
11436dd4ee7cSKalpak Shah 	struct ext4_xattr_entry *entry, *last, *first;
11446dd4ee7cSKalpak Shah 	struct buffer_head *bh = NULL;
11456dd4ee7cSKalpak Shah 	struct ext4_xattr_ibody_find *is = NULL;
11466dd4ee7cSKalpak Shah 	struct ext4_xattr_block_find *bs = NULL;
11476dd4ee7cSKalpak Shah 	char *buffer = NULL, *b_entry_name = NULL;
11486dd4ee7cSKalpak Shah 	size_t min_offs, free;
11496dd4ee7cSKalpak Shah 	int total_ino, total_blk;
11506dd4ee7cSKalpak Shah 	void *base, *start, *end;
11516dd4ee7cSKalpak Shah 	int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1152ac39849dSAneesh Kumar K.V 	int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
11536dd4ee7cSKalpak Shah 
11546dd4ee7cSKalpak Shah 	down_write(&EXT4_I(inode)->xattr_sem);
11556dd4ee7cSKalpak Shah retry:
11566dd4ee7cSKalpak Shah 	if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
11576dd4ee7cSKalpak Shah 		up_write(&EXT4_I(inode)->xattr_sem);
11586dd4ee7cSKalpak Shah 		return 0;
11596dd4ee7cSKalpak Shah 	}
11606dd4ee7cSKalpak Shah 
11616dd4ee7cSKalpak Shah 	header = IHDR(inode, raw_inode);
11626dd4ee7cSKalpak Shah 	entry = IFIRST(header);
11636dd4ee7cSKalpak Shah 
11646dd4ee7cSKalpak Shah 	/*
11656dd4ee7cSKalpak Shah 	 * Check if enough free space is available in the inode to shift the
11666dd4ee7cSKalpak Shah 	 * entries ahead by new_extra_isize.
11676dd4ee7cSKalpak Shah 	 */
11686dd4ee7cSKalpak Shah 
11696dd4ee7cSKalpak Shah 	base = start = entry;
11706dd4ee7cSKalpak Shah 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
11716dd4ee7cSKalpak Shah 	min_offs = end - base;
11726dd4ee7cSKalpak Shah 	last = entry;
11736dd4ee7cSKalpak Shah 	total_ino = sizeof(struct ext4_xattr_ibody_header);
11746dd4ee7cSKalpak Shah 
11756dd4ee7cSKalpak Shah 	free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
11766dd4ee7cSKalpak Shah 	if (free >= new_extra_isize) {
11776dd4ee7cSKalpak Shah 		entry = IFIRST(header);
11786dd4ee7cSKalpak Shah 		ext4_xattr_shift_entries(entry,	EXT4_I(inode)->i_extra_isize
11796dd4ee7cSKalpak Shah 				- new_extra_isize, (void *)raw_inode +
11806dd4ee7cSKalpak Shah 				EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
11816dd4ee7cSKalpak Shah 				(void *)header, total_ino,
11826dd4ee7cSKalpak Shah 				inode->i_sb->s_blocksize);
11836dd4ee7cSKalpak Shah 		EXT4_I(inode)->i_extra_isize = new_extra_isize;
11846dd4ee7cSKalpak Shah 		error = 0;
11856dd4ee7cSKalpak Shah 		goto cleanup;
11866dd4ee7cSKalpak Shah 	}
11876dd4ee7cSKalpak Shah 
11886dd4ee7cSKalpak Shah 	/*
11896dd4ee7cSKalpak Shah 	 * Enough free space isn't available in the inode, check if
11906dd4ee7cSKalpak Shah 	 * EA block can hold new_extra_isize bytes.
11916dd4ee7cSKalpak Shah 	 */
11926dd4ee7cSKalpak Shah 	if (EXT4_I(inode)->i_file_acl) {
11936dd4ee7cSKalpak Shah 		bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
11946dd4ee7cSKalpak Shah 		error = -EIO;
11956dd4ee7cSKalpak Shah 		if (!bh)
11966dd4ee7cSKalpak Shah 			goto cleanup;
11976dd4ee7cSKalpak Shah 		if (ext4_xattr_check_block(bh)) {
119846e665e9SHarvey Harrison 			ext4_error(inode->i_sb, __func__,
11996dd4ee7cSKalpak Shah 				"inode %lu: bad block %llu", inode->i_ino,
12006dd4ee7cSKalpak Shah 				EXT4_I(inode)->i_file_acl);
12016dd4ee7cSKalpak Shah 			error = -EIO;
12026dd4ee7cSKalpak Shah 			goto cleanup;
12036dd4ee7cSKalpak Shah 		}
12046dd4ee7cSKalpak Shah 		base = BHDR(bh);
12056dd4ee7cSKalpak Shah 		first = BFIRST(bh);
12066dd4ee7cSKalpak Shah 		end = bh->b_data + bh->b_size;
12076dd4ee7cSKalpak Shah 		min_offs = end - base;
12086dd4ee7cSKalpak Shah 		free = ext4_xattr_free_space(first, &min_offs, base,
12096dd4ee7cSKalpak Shah 					     &total_blk);
12106dd4ee7cSKalpak Shah 		if (free < new_extra_isize) {
12116dd4ee7cSKalpak Shah 			if (!tried_min_extra_isize && s_min_extra_isize) {
12126dd4ee7cSKalpak Shah 				tried_min_extra_isize++;
12136dd4ee7cSKalpak Shah 				new_extra_isize = s_min_extra_isize;
12146dd4ee7cSKalpak Shah 				brelse(bh);
12156dd4ee7cSKalpak Shah 				goto retry;
12166dd4ee7cSKalpak Shah 			}
12176dd4ee7cSKalpak Shah 			error = -1;
12186dd4ee7cSKalpak Shah 			goto cleanup;
12196dd4ee7cSKalpak Shah 		}
12206dd4ee7cSKalpak Shah 	} else {
12216dd4ee7cSKalpak Shah 		free = inode->i_sb->s_blocksize;
12226dd4ee7cSKalpak Shah 	}
12236dd4ee7cSKalpak Shah 
12246dd4ee7cSKalpak Shah 	while (new_extra_isize > 0) {
12256dd4ee7cSKalpak Shah 		size_t offs, size, entry_size;
12266dd4ee7cSKalpak Shah 		struct ext4_xattr_entry *small_entry = NULL;
12276dd4ee7cSKalpak Shah 		struct ext4_xattr_info i = {
12286dd4ee7cSKalpak Shah 			.value = NULL,
12296dd4ee7cSKalpak Shah 			.value_len = 0,
12306dd4ee7cSKalpak Shah 		};
12316dd4ee7cSKalpak Shah 		unsigned int total_size;  /* EA entry size + value size */
12326dd4ee7cSKalpak Shah 		unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
12336dd4ee7cSKalpak Shah 		unsigned int min_total_size = ~0U;
12346dd4ee7cSKalpak Shah 
12356dd4ee7cSKalpak Shah 		is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
12366dd4ee7cSKalpak Shah 		bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
12376dd4ee7cSKalpak Shah 		if (!is || !bs) {
12386dd4ee7cSKalpak Shah 			error = -ENOMEM;
12396dd4ee7cSKalpak Shah 			goto cleanup;
12406dd4ee7cSKalpak Shah 		}
12416dd4ee7cSKalpak Shah 
12426dd4ee7cSKalpak Shah 		is->s.not_found = -ENODATA;
12436dd4ee7cSKalpak Shah 		bs->s.not_found = -ENODATA;
12446dd4ee7cSKalpak Shah 		is->iloc.bh = NULL;
12456dd4ee7cSKalpak Shah 		bs->bh = NULL;
12466dd4ee7cSKalpak Shah 
12476dd4ee7cSKalpak Shah 		last = IFIRST(header);
12486dd4ee7cSKalpak Shah 		/* Find the entry best suited to be pushed into EA block */
12496dd4ee7cSKalpak Shah 		entry = NULL;
12506dd4ee7cSKalpak Shah 		for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
12516dd4ee7cSKalpak Shah 			total_size =
12526dd4ee7cSKalpak Shah 			EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
12536dd4ee7cSKalpak Shah 					EXT4_XATTR_LEN(last->e_name_len);
12546dd4ee7cSKalpak Shah 			if (total_size <= free && total_size < min_total_size) {
12556dd4ee7cSKalpak Shah 				if (total_size < new_extra_isize) {
12566dd4ee7cSKalpak Shah 					small_entry = last;
12576dd4ee7cSKalpak Shah 				} else {
12586dd4ee7cSKalpak Shah 					entry = last;
12596dd4ee7cSKalpak Shah 					min_total_size = total_size;
12606dd4ee7cSKalpak Shah 				}
12616dd4ee7cSKalpak Shah 			}
12626dd4ee7cSKalpak Shah 		}
12636dd4ee7cSKalpak Shah 
12646dd4ee7cSKalpak Shah 		if (entry == NULL) {
12656dd4ee7cSKalpak Shah 			if (small_entry) {
12666dd4ee7cSKalpak Shah 				entry = small_entry;
12676dd4ee7cSKalpak Shah 			} else {
12686dd4ee7cSKalpak Shah 				if (!tried_min_extra_isize &&
12696dd4ee7cSKalpak Shah 				    s_min_extra_isize) {
12706dd4ee7cSKalpak Shah 					tried_min_extra_isize++;
12716dd4ee7cSKalpak Shah 					new_extra_isize = s_min_extra_isize;
12726dd4ee7cSKalpak Shah 					goto retry;
12736dd4ee7cSKalpak Shah 				}
12746dd4ee7cSKalpak Shah 				error = -1;
12756dd4ee7cSKalpak Shah 				goto cleanup;
12766dd4ee7cSKalpak Shah 			}
12776dd4ee7cSKalpak Shah 		}
12786dd4ee7cSKalpak Shah 		offs = le16_to_cpu(entry->e_value_offs);
12796dd4ee7cSKalpak Shah 		size = le32_to_cpu(entry->e_value_size);
12806dd4ee7cSKalpak Shah 		entry_size = EXT4_XATTR_LEN(entry->e_name_len);
12816dd4ee7cSKalpak Shah 		i.name_index = entry->e_name_index,
12826dd4ee7cSKalpak Shah 		buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
12836dd4ee7cSKalpak Shah 		b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
12846dd4ee7cSKalpak Shah 		if (!buffer || !b_entry_name) {
12856dd4ee7cSKalpak Shah 			error = -ENOMEM;
12866dd4ee7cSKalpak Shah 			goto cleanup;
12876dd4ee7cSKalpak Shah 		}
12886dd4ee7cSKalpak Shah 		/* Save the entry name and the entry value */
12896dd4ee7cSKalpak Shah 		memcpy(buffer, (void *)IFIRST(header) + offs,
12906dd4ee7cSKalpak Shah 		       EXT4_XATTR_SIZE(size));
12916dd4ee7cSKalpak Shah 		memcpy(b_entry_name, entry->e_name, entry->e_name_len);
12926dd4ee7cSKalpak Shah 		b_entry_name[entry->e_name_len] = '\0';
12936dd4ee7cSKalpak Shah 		i.name = b_entry_name;
12946dd4ee7cSKalpak Shah 
12956dd4ee7cSKalpak Shah 		error = ext4_get_inode_loc(inode, &is->iloc);
12966dd4ee7cSKalpak Shah 		if (error)
12976dd4ee7cSKalpak Shah 			goto cleanup;
12986dd4ee7cSKalpak Shah 
12996dd4ee7cSKalpak Shah 		error = ext4_xattr_ibody_find(inode, &i, is);
13006dd4ee7cSKalpak Shah 		if (error)
13016dd4ee7cSKalpak Shah 			goto cleanup;
13026dd4ee7cSKalpak Shah 
13036dd4ee7cSKalpak Shah 		/* Remove the chosen entry from the inode */
13046dd4ee7cSKalpak Shah 		error = ext4_xattr_ibody_set(handle, inode, &i, is);
13056dd4ee7cSKalpak Shah 
13066dd4ee7cSKalpak Shah 		entry = IFIRST(header);
13076dd4ee7cSKalpak Shah 		if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
13086dd4ee7cSKalpak Shah 			shift_bytes = new_extra_isize;
13096dd4ee7cSKalpak Shah 		else
13106dd4ee7cSKalpak Shah 			shift_bytes = entry_size + size;
13116dd4ee7cSKalpak Shah 		/* Adjust the offsets and shift the remaining entries ahead */
13126dd4ee7cSKalpak Shah 		ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
13136dd4ee7cSKalpak Shah 			shift_bytes, (void *)raw_inode +
13146dd4ee7cSKalpak Shah 			EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
13156dd4ee7cSKalpak Shah 			(void *)header, total_ino - entry_size,
13166dd4ee7cSKalpak Shah 			inode->i_sb->s_blocksize);
13176dd4ee7cSKalpak Shah 
13186dd4ee7cSKalpak Shah 		extra_isize += shift_bytes;
13196dd4ee7cSKalpak Shah 		new_extra_isize -= shift_bytes;
13206dd4ee7cSKalpak Shah 		EXT4_I(inode)->i_extra_isize = extra_isize;
13216dd4ee7cSKalpak Shah 
13226dd4ee7cSKalpak Shah 		i.name = b_entry_name;
13236dd4ee7cSKalpak Shah 		i.value = buffer;
1324ac39849dSAneesh Kumar K.V 		i.value_len = size;
13256dd4ee7cSKalpak Shah 		error = ext4_xattr_block_find(inode, &i, bs);
13266dd4ee7cSKalpak Shah 		if (error)
13276dd4ee7cSKalpak Shah 			goto cleanup;
13286dd4ee7cSKalpak Shah 
13296dd4ee7cSKalpak Shah 		/* Add entry which was removed from the inode into the block */
13306dd4ee7cSKalpak Shah 		error = ext4_xattr_block_set(handle, inode, &i, bs);
13316dd4ee7cSKalpak Shah 		if (error)
13326dd4ee7cSKalpak Shah 			goto cleanup;
13336dd4ee7cSKalpak Shah 		kfree(b_entry_name);
13346dd4ee7cSKalpak Shah 		kfree(buffer);
1335d3533d72SJulia Lawall 		b_entry_name = NULL;
1336d3533d72SJulia Lawall 		buffer = NULL;
13376dd4ee7cSKalpak Shah 		brelse(is->iloc.bh);
13386dd4ee7cSKalpak Shah 		kfree(is);
13396dd4ee7cSKalpak Shah 		kfree(bs);
13406dd4ee7cSKalpak Shah 	}
13416dd4ee7cSKalpak Shah 	brelse(bh);
13426dd4ee7cSKalpak Shah 	up_write(&EXT4_I(inode)->xattr_sem);
13436dd4ee7cSKalpak Shah 	return 0;
13446dd4ee7cSKalpak Shah 
13456dd4ee7cSKalpak Shah cleanup:
13466dd4ee7cSKalpak Shah 	kfree(b_entry_name);
13476dd4ee7cSKalpak Shah 	kfree(buffer);
13486dd4ee7cSKalpak Shah 	if (is)
13496dd4ee7cSKalpak Shah 		brelse(is->iloc.bh);
13506dd4ee7cSKalpak Shah 	kfree(is);
13516dd4ee7cSKalpak Shah 	kfree(bs);
13526dd4ee7cSKalpak Shah 	brelse(bh);
13536dd4ee7cSKalpak Shah 	up_write(&EXT4_I(inode)->xattr_sem);
13546dd4ee7cSKalpak Shah 	return error;
13556dd4ee7cSKalpak Shah }
13566dd4ee7cSKalpak Shah 
13576dd4ee7cSKalpak Shah 
13586dd4ee7cSKalpak Shah 
13596dd4ee7cSKalpak Shah /*
1360617ba13bSMingming Cao  * ext4_xattr_delete_inode()
1361ac27a0ecSDave Kleikamp  *
1362ac27a0ecSDave Kleikamp  * Free extended attribute resources associated with this inode. This
1363ac27a0ecSDave Kleikamp  * is called immediately before an inode is freed. We have exclusive
1364ac27a0ecSDave Kleikamp  * access to the inode.
1365ac27a0ecSDave Kleikamp  */
1366ac27a0ecSDave Kleikamp void
1367617ba13bSMingming Cao ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1368ac27a0ecSDave Kleikamp {
1369ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
1370ac27a0ecSDave Kleikamp 
1371617ba13bSMingming Cao 	if (!EXT4_I(inode)->i_file_acl)
1372ac27a0ecSDave Kleikamp 		goto cleanup;
1373617ba13bSMingming Cao 	bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1374ac27a0ecSDave Kleikamp 	if (!bh) {
137546e665e9SHarvey Harrison 		ext4_error(inode->i_sb, __func__,
13762ae02107SMingming Cao 			"inode %lu: block %llu read error", inode->i_ino,
1377617ba13bSMingming Cao 			EXT4_I(inode)->i_file_acl);
1378ac27a0ecSDave Kleikamp 		goto cleanup;
1379ac27a0ecSDave Kleikamp 	}
1380617ba13bSMingming Cao 	if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1381ac27a0ecSDave Kleikamp 	    BHDR(bh)->h_blocks != cpu_to_le32(1)) {
138246e665e9SHarvey Harrison 		ext4_error(inode->i_sb, __func__,
13832ae02107SMingming Cao 			"inode %lu: bad block %llu", inode->i_ino,
1384617ba13bSMingming Cao 			EXT4_I(inode)->i_file_acl);
1385ac27a0ecSDave Kleikamp 		goto cleanup;
1386ac27a0ecSDave Kleikamp 	}
1387617ba13bSMingming Cao 	ext4_xattr_release_block(handle, inode, bh);
1388617ba13bSMingming Cao 	EXT4_I(inode)->i_file_acl = 0;
1389ac27a0ecSDave Kleikamp 
1390ac27a0ecSDave Kleikamp cleanup:
1391ac27a0ecSDave Kleikamp 	brelse(bh);
1392ac27a0ecSDave Kleikamp }
1393ac27a0ecSDave Kleikamp 
1394ac27a0ecSDave Kleikamp /*
1395617ba13bSMingming Cao  * ext4_xattr_put_super()
1396ac27a0ecSDave Kleikamp  *
1397ac27a0ecSDave Kleikamp  * This is called when a file system is unmounted.
1398ac27a0ecSDave Kleikamp  */
1399ac27a0ecSDave Kleikamp void
1400617ba13bSMingming Cao ext4_xattr_put_super(struct super_block *sb)
1401ac27a0ecSDave Kleikamp {
1402ac27a0ecSDave Kleikamp 	mb_cache_shrink(sb->s_bdev);
1403ac27a0ecSDave Kleikamp }
1404ac27a0ecSDave Kleikamp 
1405ac27a0ecSDave Kleikamp /*
1406617ba13bSMingming Cao  * ext4_xattr_cache_insert()
1407ac27a0ecSDave Kleikamp  *
1408ac27a0ecSDave Kleikamp  * Create a new entry in the extended attribute cache, and insert
1409ac27a0ecSDave Kleikamp  * it unless such an entry is already in the cache.
1410ac27a0ecSDave Kleikamp  *
1411ac27a0ecSDave Kleikamp  * Returns 0, or a negative error number on failure.
1412ac27a0ecSDave Kleikamp  */
1413ac27a0ecSDave Kleikamp static void
1414617ba13bSMingming Cao ext4_xattr_cache_insert(struct buffer_head *bh)
1415ac27a0ecSDave Kleikamp {
1416ac27a0ecSDave Kleikamp 	__u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1417ac27a0ecSDave Kleikamp 	struct mb_cache_entry *ce;
1418ac27a0ecSDave Kleikamp 	int error;
1419ac27a0ecSDave Kleikamp 
1420335e92e8SJan Kara 	ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
1421ac27a0ecSDave Kleikamp 	if (!ce) {
1422ac27a0ecSDave Kleikamp 		ea_bdebug(bh, "out of memory");
1423ac27a0ecSDave Kleikamp 		return;
1424ac27a0ecSDave Kleikamp 	}
1425ac27a0ecSDave Kleikamp 	error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
1426ac27a0ecSDave Kleikamp 	if (error) {
1427ac27a0ecSDave Kleikamp 		mb_cache_entry_free(ce);
1428ac27a0ecSDave Kleikamp 		if (error == -EBUSY) {
1429ac27a0ecSDave Kleikamp 			ea_bdebug(bh, "already in cache");
1430ac27a0ecSDave Kleikamp 			error = 0;
1431ac27a0ecSDave Kleikamp 		}
1432ac27a0ecSDave Kleikamp 	} else {
1433ac27a0ecSDave Kleikamp 		ea_bdebug(bh, "inserting [%x]", (int)hash);
1434ac27a0ecSDave Kleikamp 		mb_cache_entry_release(ce);
1435ac27a0ecSDave Kleikamp 	}
1436ac27a0ecSDave Kleikamp }
1437ac27a0ecSDave Kleikamp 
1438ac27a0ecSDave Kleikamp /*
1439617ba13bSMingming Cao  * ext4_xattr_cmp()
1440ac27a0ecSDave Kleikamp  *
1441ac27a0ecSDave Kleikamp  * Compare two extended attribute blocks for equality.
1442ac27a0ecSDave Kleikamp  *
1443ac27a0ecSDave Kleikamp  * Returns 0 if the blocks are equal, 1 if they differ, and
1444ac27a0ecSDave Kleikamp  * a negative error number on errors.
1445ac27a0ecSDave Kleikamp  */
1446ac27a0ecSDave Kleikamp static int
1447617ba13bSMingming Cao ext4_xattr_cmp(struct ext4_xattr_header *header1,
1448617ba13bSMingming Cao 	       struct ext4_xattr_header *header2)
1449ac27a0ecSDave Kleikamp {
1450617ba13bSMingming Cao 	struct ext4_xattr_entry *entry1, *entry2;
1451ac27a0ecSDave Kleikamp 
1452ac27a0ecSDave Kleikamp 	entry1 = ENTRY(header1+1);
1453ac27a0ecSDave Kleikamp 	entry2 = ENTRY(header2+1);
1454ac27a0ecSDave Kleikamp 	while (!IS_LAST_ENTRY(entry1)) {
1455ac27a0ecSDave Kleikamp 		if (IS_LAST_ENTRY(entry2))
1456ac27a0ecSDave Kleikamp 			return 1;
1457ac27a0ecSDave Kleikamp 		if (entry1->e_hash != entry2->e_hash ||
1458ac27a0ecSDave Kleikamp 		    entry1->e_name_index != entry2->e_name_index ||
1459ac27a0ecSDave Kleikamp 		    entry1->e_name_len != entry2->e_name_len ||
1460ac27a0ecSDave Kleikamp 		    entry1->e_value_size != entry2->e_value_size ||
1461ac27a0ecSDave Kleikamp 		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1462ac27a0ecSDave Kleikamp 			return 1;
1463ac27a0ecSDave Kleikamp 		if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1464ac27a0ecSDave Kleikamp 			return -EIO;
1465ac27a0ecSDave Kleikamp 		if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1466ac27a0ecSDave Kleikamp 			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1467ac27a0ecSDave Kleikamp 			   le32_to_cpu(entry1->e_value_size)))
1468ac27a0ecSDave Kleikamp 			return 1;
1469ac27a0ecSDave Kleikamp 
1470617ba13bSMingming Cao 		entry1 = EXT4_XATTR_NEXT(entry1);
1471617ba13bSMingming Cao 		entry2 = EXT4_XATTR_NEXT(entry2);
1472ac27a0ecSDave Kleikamp 	}
1473ac27a0ecSDave Kleikamp 	if (!IS_LAST_ENTRY(entry2))
1474ac27a0ecSDave Kleikamp 		return 1;
1475ac27a0ecSDave Kleikamp 	return 0;
1476ac27a0ecSDave Kleikamp }
1477ac27a0ecSDave Kleikamp 
1478ac27a0ecSDave Kleikamp /*
1479617ba13bSMingming Cao  * ext4_xattr_cache_find()
1480ac27a0ecSDave Kleikamp  *
1481ac27a0ecSDave Kleikamp  * Find an identical extended attribute block.
1482ac27a0ecSDave Kleikamp  *
1483ac27a0ecSDave Kleikamp  * Returns a pointer to the block found, or NULL if such a block was
1484ac27a0ecSDave Kleikamp  * not found or an error occurred.
1485ac27a0ecSDave Kleikamp  */
1486ac27a0ecSDave Kleikamp static struct buffer_head *
1487617ba13bSMingming Cao ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1488ac27a0ecSDave Kleikamp 		      struct mb_cache_entry **pce)
1489ac27a0ecSDave Kleikamp {
1490ac27a0ecSDave Kleikamp 	__u32 hash = le32_to_cpu(header->h_hash);
1491ac27a0ecSDave Kleikamp 	struct mb_cache_entry *ce;
1492ac27a0ecSDave Kleikamp 
1493ac27a0ecSDave Kleikamp 	if (!header->h_hash)
1494ac27a0ecSDave Kleikamp 		return NULL;  /* never share */
1495ac27a0ecSDave Kleikamp 	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1496ac27a0ecSDave Kleikamp again:
1497617ba13bSMingming Cao 	ce = mb_cache_entry_find_first(ext4_xattr_cache, 0,
1498ac27a0ecSDave Kleikamp 				       inode->i_sb->s_bdev, hash);
1499ac27a0ecSDave Kleikamp 	while (ce) {
1500ac27a0ecSDave Kleikamp 		struct buffer_head *bh;
1501ac27a0ecSDave Kleikamp 
1502ac27a0ecSDave Kleikamp 		if (IS_ERR(ce)) {
1503ac27a0ecSDave Kleikamp 			if (PTR_ERR(ce) == -EAGAIN)
1504ac27a0ecSDave Kleikamp 				goto again;
1505ac27a0ecSDave Kleikamp 			break;
1506ac27a0ecSDave Kleikamp 		}
1507ac27a0ecSDave Kleikamp 		bh = sb_bread(inode->i_sb, ce->e_block);
1508ac27a0ecSDave Kleikamp 		if (!bh) {
150946e665e9SHarvey Harrison 			ext4_error(inode->i_sb, __func__,
1510ac27a0ecSDave Kleikamp 				"inode %lu: block %lu read error",
1511ac27a0ecSDave Kleikamp 				inode->i_ino, (unsigned long) ce->e_block);
1512ac27a0ecSDave Kleikamp 		} else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1513617ba13bSMingming Cao 				EXT4_XATTR_REFCOUNT_MAX) {
1514ac27a0ecSDave Kleikamp 			ea_idebug(inode, "block %lu refcount %d>=%d",
1515ac27a0ecSDave Kleikamp 				  (unsigned long) ce->e_block,
1516ac27a0ecSDave Kleikamp 				  le32_to_cpu(BHDR(bh)->h_refcount),
1517617ba13bSMingming Cao 					  EXT4_XATTR_REFCOUNT_MAX);
1518617ba13bSMingming Cao 		} else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1519ac27a0ecSDave Kleikamp 			*pce = ce;
1520ac27a0ecSDave Kleikamp 			return bh;
1521ac27a0ecSDave Kleikamp 		}
1522ac27a0ecSDave Kleikamp 		brelse(bh);
1523ac27a0ecSDave Kleikamp 		ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1524ac27a0ecSDave Kleikamp 	}
1525ac27a0ecSDave Kleikamp 	return NULL;
1526ac27a0ecSDave Kleikamp }
1527ac27a0ecSDave Kleikamp 
1528ac27a0ecSDave Kleikamp #define NAME_HASH_SHIFT 5
1529ac27a0ecSDave Kleikamp #define VALUE_HASH_SHIFT 16
1530ac27a0ecSDave Kleikamp 
1531ac27a0ecSDave Kleikamp /*
1532617ba13bSMingming Cao  * ext4_xattr_hash_entry()
1533ac27a0ecSDave Kleikamp  *
1534ac27a0ecSDave Kleikamp  * Compute the hash of an extended attribute.
1535ac27a0ecSDave Kleikamp  */
1536617ba13bSMingming Cao static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1537617ba13bSMingming Cao 					 struct ext4_xattr_entry *entry)
1538ac27a0ecSDave Kleikamp {
1539ac27a0ecSDave Kleikamp 	__u32 hash = 0;
1540ac27a0ecSDave Kleikamp 	char *name = entry->e_name;
1541ac27a0ecSDave Kleikamp 	int n;
1542ac27a0ecSDave Kleikamp 
1543ac27a0ecSDave Kleikamp 	for (n = 0; n < entry->e_name_len; n++) {
1544ac27a0ecSDave Kleikamp 		hash = (hash << NAME_HASH_SHIFT) ^
1545ac27a0ecSDave Kleikamp 		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1546ac27a0ecSDave Kleikamp 		       *name++;
1547ac27a0ecSDave Kleikamp 	}
1548ac27a0ecSDave Kleikamp 
1549ac27a0ecSDave Kleikamp 	if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1550ac27a0ecSDave Kleikamp 		__le32 *value = (__le32 *)((char *)header +
1551ac27a0ecSDave Kleikamp 			le16_to_cpu(entry->e_value_offs));
1552ac27a0ecSDave Kleikamp 		for (n = (le32_to_cpu(entry->e_value_size) +
1553617ba13bSMingming Cao 		     EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1554ac27a0ecSDave Kleikamp 			hash = (hash << VALUE_HASH_SHIFT) ^
1555ac27a0ecSDave Kleikamp 			       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1556ac27a0ecSDave Kleikamp 			       le32_to_cpu(*value++);
1557ac27a0ecSDave Kleikamp 		}
1558ac27a0ecSDave Kleikamp 	}
1559ac27a0ecSDave Kleikamp 	entry->e_hash = cpu_to_le32(hash);
1560ac27a0ecSDave Kleikamp }
1561ac27a0ecSDave Kleikamp 
1562ac27a0ecSDave Kleikamp #undef NAME_HASH_SHIFT
1563ac27a0ecSDave Kleikamp #undef VALUE_HASH_SHIFT
1564ac27a0ecSDave Kleikamp 
1565ac27a0ecSDave Kleikamp #define BLOCK_HASH_SHIFT 16
1566ac27a0ecSDave Kleikamp 
1567ac27a0ecSDave Kleikamp /*
1568617ba13bSMingming Cao  * ext4_xattr_rehash()
1569ac27a0ecSDave Kleikamp  *
1570ac27a0ecSDave Kleikamp  * Re-compute the extended attribute hash value after an entry has changed.
1571ac27a0ecSDave Kleikamp  */
1572617ba13bSMingming Cao static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1573617ba13bSMingming Cao 			      struct ext4_xattr_entry *entry)
1574ac27a0ecSDave Kleikamp {
1575617ba13bSMingming Cao 	struct ext4_xattr_entry *here;
1576ac27a0ecSDave Kleikamp 	__u32 hash = 0;
1577ac27a0ecSDave Kleikamp 
1578617ba13bSMingming Cao 	ext4_xattr_hash_entry(header, entry);
1579ac27a0ecSDave Kleikamp 	here = ENTRY(header+1);
1580ac27a0ecSDave Kleikamp 	while (!IS_LAST_ENTRY(here)) {
1581ac27a0ecSDave Kleikamp 		if (!here->e_hash) {
1582ac27a0ecSDave Kleikamp 			/* Block is not shared if an entry's hash value == 0 */
1583ac27a0ecSDave Kleikamp 			hash = 0;
1584ac27a0ecSDave Kleikamp 			break;
1585ac27a0ecSDave Kleikamp 		}
1586ac27a0ecSDave Kleikamp 		hash = (hash << BLOCK_HASH_SHIFT) ^
1587ac27a0ecSDave Kleikamp 		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1588ac27a0ecSDave Kleikamp 		       le32_to_cpu(here->e_hash);
1589617ba13bSMingming Cao 		here = EXT4_XATTR_NEXT(here);
1590ac27a0ecSDave Kleikamp 	}
1591ac27a0ecSDave Kleikamp 	header->h_hash = cpu_to_le32(hash);
1592ac27a0ecSDave Kleikamp }
1593ac27a0ecSDave Kleikamp 
1594ac27a0ecSDave Kleikamp #undef BLOCK_HASH_SHIFT
1595ac27a0ecSDave Kleikamp 
1596ac27a0ecSDave Kleikamp int __init
1597617ba13bSMingming Cao init_ext4_xattr(void)
1598ac27a0ecSDave Kleikamp {
1599617ba13bSMingming Cao 	ext4_xattr_cache = mb_cache_create("ext4_xattr", NULL,
1600ac27a0ecSDave Kleikamp 		sizeof(struct mb_cache_entry) +
1601ac27a0ecSDave Kleikamp 		sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
1602617ba13bSMingming Cao 	if (!ext4_xattr_cache)
1603ac27a0ecSDave Kleikamp 		return -ENOMEM;
1604ac27a0ecSDave Kleikamp 	return 0;
1605ac27a0ecSDave Kleikamp }
1606ac27a0ecSDave Kleikamp 
1607ac27a0ecSDave Kleikamp void
1608617ba13bSMingming Cao exit_ext4_xattr(void)
1609ac27a0ecSDave Kleikamp {
1610617ba13bSMingming Cao 	if (ext4_xattr_cache)
1611617ba13bSMingming Cao 		mb_cache_destroy(ext4_xattr_cache);
1612617ba13bSMingming Cao 	ext4_xattr_cache = NULL;
1613ac27a0ecSDave Kleikamp }
1614