blob: c40bd55b6400307d8d4be60c3dd850c6163ef142 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/xattr.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
Mingming Cao617ba132006-10-11 01:20:53 -07007 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07008 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
11 * Red Hat Inc.
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
14 */
15
16/*
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
24 *
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
27 *
28 * +------------------+
29 * | header |
30 * | entry 1 | |
31 * | entry 2 | | growing downwards
32 * | entry 3 | v
33 * | four null bytes |
34 * | . . . |
35 * | value 1 | ^
36 * | value 3 | | growing upwards
37 * | value 2 | |
38 * +------------------+
39 *
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
43 *
44 * Locking strategy
45 * ----------------
Mingming Cao617ba132006-10-11 01:20:53 -070046 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
50 * by the buffer lock.
51 */
52
53#include <linux/init.h>
54#include <linux/fs.h>
55#include <linux/slab.h>
Jan Kara7a2508e2016-02-22 22:35:22 -050056#include <linux/mbcache.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057#include <linux/quotaops.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040058#include "ext4_jbd2.h"
59#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070060#include "xattr.h"
61#include "acl.h"
62
Mingming Cao617ba132006-10-11 01:20:53 -070063#ifdef EXT4_XATTR_DEBUG
Joe Perchesd74f3d22016-10-15 09:57:31 -040064# define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67# define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070#else
Joe Perchesace36ad2012-03-19 23:11:43 -040071# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073#endif
74
Jan Kara7a2508e2016-02-22 22:35:22 -050075static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070076static struct buffer_head *ext4_xattr_cache_find(struct inode *,
77 struct ext4_xattr_header *,
Jan Kara7a2508e2016-02-22 22:35:22 -050078 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070079static void ext4_xattr_rehash(struct ext4_xattr_header *,
80 struct ext4_xattr_entry *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000081static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040082 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083
Stephen Hemminger11e27522010-05-13 17:53:18 -070084static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070085 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040086#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080087 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
88 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089#endif
Mingming Cao617ba132006-10-11 01:20:53 -070090 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040091#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070092 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070093#endif
94};
95
Stephen Hemminger11e27522010-05-13 17:53:18 -070096const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070097 &ext4_xattr_user_handler,
98 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040099#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800100 &posix_acl_access_xattr_handler,
101 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400103#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700104 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105#endif
106 NULL
107};
108
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400109#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_mb_cache)
111
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400112static __le32 ext4_xattr_block_csum(struct inode *inode,
113 sector_t block_nr,
114 struct ext4_xattr_header *hdr)
115{
116 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400117 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400118 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400119 __u32 dummy_csum = 0;
120 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400121
Theodore Ts'od6a77102013-04-09 23:59:55 -0400122 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
123 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400124 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
125 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
126 offset += sizeof(dummy_csum);
127 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
128 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400129
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400130 return cpu_to_le32(csum);
131}
132
133static int ext4_xattr_block_csum_verify(struct inode *inode,
134 sector_t block_nr,
135 struct ext4_xattr_header *hdr)
136{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400137 if (ext4_has_metadata_csum(inode->i_sb) &&
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400138 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
139 return 0;
140 return 1;
141}
142
143static void ext4_xattr_block_csum_set(struct inode *inode,
144 sector_t block_nr,
145 struct ext4_xattr_header *hdr)
146{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400147 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400148 return;
149
150 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
151}
152
153static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
154 struct inode *inode,
155 struct buffer_head *bh)
156{
157 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
158 return ext4_handle_dirty_metadata(handle, inode, bh);
159}
160
Stephen Hemminger11e27522010-05-13 17:53:18 -0700161static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700162ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700164 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165
Mingming Cao617ba132006-10-11 01:20:53 -0700166 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
167 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 return handler;
169}
170
171/*
172 * Inode operation listxattr()
173 *
David Howells2b0143b2015-03-17 22:25:59 +0000174 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175 */
176ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700177ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000179 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180}
181
182static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400183ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
184 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400186 struct ext4_xattr_entry *e = entry;
187
Eric Biggersd7614cc2016-12-01 14:57:29 -0500188 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400189 while (!IS_LAST_ENTRY(e)) {
190 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400192 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400193 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400195
Eric Biggersd7614cc2016-12-01 14:57:29 -0500196 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400197 while (!IS_LAST_ENTRY(entry)) {
Jan Kara2de58f12016-08-29 15:39:11 -0400198 if (entry->e_value_block != 0)
199 return -EFSCORRUPTED;
Eric Biggersd7614cc2016-12-01 14:57:29 -0500200 if (entry->e_value_size != 0) {
201 u16 offs = le16_to_cpu(entry->e_value_offs);
202 u32 size = le32_to_cpu(entry->e_value_size);
203 void *value;
204
205 /*
206 * The value cannot overlap the names, and the value
207 * with padding cannot extend beyond 'end'. Check both
208 * the padded and unpadded sizes, since the size may
209 * overflow to 0 when adding padding.
210 */
211 if (offs > end - value_start)
212 return -EFSCORRUPTED;
213 value = value_start + offs;
214 if (value < (void *)e + sizeof(u32) ||
215 size > end - value ||
216 EXT4_XATTR_SIZE(size) > end - value)
217 return -EFSCORRUPTED;
218 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400219 entry = EXT4_XATTR_NEXT(entry);
220 }
221
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 return 0;
223}
224
225static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400226ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400228 int error;
229
230 if (buffer_verified(bh))
231 return 0;
232
Mingming Cao617ba132006-10-11 01:20:53 -0700233 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700234 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400235 return -EFSCORRUPTED;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400236 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400237 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400238 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
239 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400240 if (!error)
241 set_buffer_verified(bh);
242 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243}
244
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400245static int
246__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
247 void *end, const char *function, unsigned int line)
248{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400249 int error = -EFSCORRUPTED;
250
Eric Biggers290ab232016-12-01 14:51:58 -0500251 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400252 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400253 goto errout;
Eric Biggers290ab232016-12-01 14:51:58 -0500254 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400255errout:
256 if (error)
257 __ext4_error_inode(inode, function, line, 0,
258 "corrupted in-inode xattr");
259 return error;
260}
261
262#define xattr_check_inode(inode, header, end) \
263 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
264
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700266ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267{
268 size_t value_size = le32_to_cpu(entry->e_value_size);
269
270 if (entry->e_value_block != 0 || value_size > size ||
271 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400272 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 return 0;
274}
275
276static int
Mingming Cao617ba132006-10-11 01:20:53 -0700277ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 const char *name, size_t size, int sorted)
279{
Mingming Cao617ba132006-10-11 01:20:53 -0700280 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 size_t name_len;
282 int cmp = 1;
283
284 if (name == NULL)
285 return -EINVAL;
286 name_len = strlen(name);
287 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700288 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 cmp = name_index - entry->e_name_index;
290 if (!cmp)
291 cmp = name_len - entry->e_name_len;
292 if (!cmp)
293 cmp = memcmp(name, entry->e_name, name_len);
294 if (cmp <= 0 && (sorted || cmp == 0))
295 break;
296 }
297 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700298 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400299 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 return cmp ? -ENODATA : 0;
301}
302
303static int
Mingming Cao617ba132006-10-11 01:20:53 -0700304ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 void *buffer, size_t buffer_size)
306{
307 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700308 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 size_t size;
310 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500311 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312
313 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
314 name_index, name, buffer, (long)buffer_size);
315
316 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700317 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400319 ea_idebug(inode, "reading block %llu",
320 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700321 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700322 if (!bh)
323 goto cleanup;
324 ea_bdebug(bh, "b_count=%d, refcount=%d",
325 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400326 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500327bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400328 EXT4_ERROR_INODE(inode, "bad block %llu",
329 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400330 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 goto cleanup;
332 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400333 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700334 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700335 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400336 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700337 goto bad_block;
338 if (error)
339 goto cleanup;
340 size = le32_to_cpu(entry->e_value_size);
341 if (buffer) {
342 error = -ERANGE;
343 if (size > buffer_size)
344 goto cleanup;
345 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
346 size);
347 }
348 error = size;
349
350cleanup:
351 brelse(bh);
352 return error;
353}
354
Tao Ma879b3822012-12-05 10:28:46 -0500355int
Mingming Cao617ba132006-10-11 01:20:53 -0700356ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700357 void *buffer, size_t buffer_size)
358{
Mingming Cao617ba132006-10-11 01:20:53 -0700359 struct ext4_xattr_ibody_header *header;
360 struct ext4_xattr_entry *entry;
361 struct ext4_inode *raw_inode;
362 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 size_t size;
364 void *end;
365 int error;
366
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500367 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700369 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 if (error)
371 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700372 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700373 header = IHDR(inode, raw_inode);
374 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700375 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400376 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700377 if (error)
378 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700379 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 end - (void *)entry, 0);
381 if (error)
382 goto cleanup;
383 size = le32_to_cpu(entry->e_value_size);
384 if (buffer) {
385 error = -ERANGE;
386 if (size > buffer_size)
387 goto cleanup;
388 memcpy(buffer, (void *)IFIRST(header) +
389 le16_to_cpu(entry->e_value_offs), size);
390 }
391 error = size;
392
393cleanup:
394 brelse(iloc.bh);
395 return error;
396}
397
398/*
Mingming Cao617ba132006-10-11 01:20:53 -0700399 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 *
401 * Copy an extended attribute into the buffer
402 * provided, or compute the buffer size required.
403 * Buffer is NULL to compute the size of the buffer required.
404 *
405 * Returns a negative error number on failure, or the number of bytes
406 * used / required on success.
407 */
408int
Mingming Cao617ba132006-10-11 01:20:53 -0700409ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 void *buffer, size_t buffer_size)
411{
412 int error;
413
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400414 if (strlen(name) > 255)
415 return -ERANGE;
416
Mingming Cao617ba132006-10-11 01:20:53 -0700417 down_read(&EXT4_I(inode)->xattr_sem);
418 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700419 buffer_size);
420 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700421 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700423 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700424 return error;
425}
426
427static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000428ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429 char *buffer, size_t buffer_size)
430{
431 size_t rest = buffer_size;
432
Mingming Cao617ba132006-10-11 01:20:53 -0700433 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700434 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700435 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100437 if (handler && (!handler->list || handler->list(dentry))) {
438 const char *prefix = handler->prefix ?: handler->name;
439 size_t prefix_len = strlen(prefix);
440 size_t size = prefix_len + entry->e_name_len + 1;
441
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442 if (buffer) {
443 if (size > rest)
444 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100445 memcpy(buffer, prefix, prefix_len);
446 buffer += prefix_len;
447 memcpy(buffer, entry->e_name, entry->e_name_len);
448 buffer += entry->e_name_len;
449 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450 }
451 rest -= size;
452 }
453 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100454 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455}
456
457static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000458ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459{
David Howells2b0143b2015-03-17 22:25:59 +0000460 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461 struct buffer_head *bh = NULL;
462 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500463 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464
465 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
466 buffer, (long)buffer_size);
467
468 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700469 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400471 ea_idebug(inode, "reading block %llu",
472 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700473 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474 error = -EIO;
475 if (!bh)
476 goto cleanup;
477 ea_bdebug(bh, "b_count=%d, refcount=%d",
478 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400479 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400480 EXT4_ERROR_INODE(inode, "bad block %llu",
481 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400482 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 goto cleanup;
484 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400485 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000486 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700487
488cleanup:
489 brelse(bh);
490
491 return error;
492}
493
494static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000495ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496{
David Howells2b0143b2015-03-17 22:25:59 +0000497 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700498 struct ext4_xattr_ibody_header *header;
499 struct ext4_inode *raw_inode;
500 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501 void *end;
502 int error;
503
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500504 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700506 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507 if (error)
508 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700509 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700511 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400512 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513 if (error)
514 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000515 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 buffer, buffer_size);
517
518cleanup:
519 brelse(iloc.bh);
520 return error;
521}
522
523/*
Mingming Cao617ba132006-10-11 01:20:53 -0700524 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 *
526 * Copy a list of attribute names into the buffer
527 * provided, or compute the buffer size required.
528 * Buffer is NULL to compute the size of the buffer required.
529 *
530 * Returns a negative error number on failure, or the number of bytes
531 * used / required on success.
532 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400533static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000534ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500536 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537
David Howells2b0143b2015-03-17 22:25:59 +0000538 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500539 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
540 if (ret < 0)
541 goto errout;
542 if (buffer) {
543 buffer += ret;
544 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500546 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
547 if (ret < 0)
548 goto errout;
549 ret += ret2;
550errout:
David Howells2b0143b2015-03-17 22:25:59 +0000551 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500552 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553}
554
555/*
Mingming Cao617ba132006-10-11 01:20:53 -0700556 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557 * not set, set it.
558 */
Mingming Cao617ba132006-10-11 01:20:53 -0700559static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 struct super_block *sb)
561{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400562 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 return;
564
liang xie5d601252014-05-12 22:06:43 -0400565 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700566 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400567 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400568 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570}
571
572/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400573 * Release the xattr block BH: If the reference count is > 1, decrement it;
574 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575 */
576static void
Mingming Cao617ba132006-10-11 01:20:53 -0700577ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 struct buffer_head *bh)
579{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500580 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
581 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800582 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583
liang xie5d601252014-05-12 22:06:43 -0400584 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800585 error = ext4_journal_get_write_access(handle, bh);
586 if (error)
587 goto out;
588
589 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500590 hash = le32_to_cpu(BHDR(bh)->h_hash);
591 ref = le32_to_cpu(BHDR(bh)->h_refcount);
592 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500594 /*
595 * This must happen under buffer lock for
596 * ext4_xattr_block_set() to reliably detect freed block
597 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500598 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400600 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500601 ext4_free_blocks(handle, inode, bh, 0, 1,
602 EXT4_FREE_BLOCKS_METADATA |
603 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700604 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500605 ref--;
606 BHDR(bh)->h_refcount = cpu_to_le32(ref);
607 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
608 struct mb_cache_entry *ce;
609
610 ce = mb_cache_entry_get(ext4_mb_cache, hash,
611 bh->b_blocknr);
612 if (ce) {
613 ce->e_reusable = 1;
614 mb_cache_entry_put(ext4_mb_cache, ce);
615 }
616 }
617
Jan Karaec4cb1a2014-04-07 10:54:21 -0400618 /*
619 * Beware of this ugliness: Releasing of xattr block references
620 * from different inodes can race and so we have to protect
621 * from a race where someone else frees the block (and releases
622 * its journal_head) before we are done dirtying the buffer. In
623 * nojournal mode this race is harmless and we actually cannot
624 * call ext4_handle_dirty_xattr_block() with locked buffer as
625 * that function can call sync_dirty_buffer() so for that case
626 * we handle the dirtying after unlocking the buffer.
627 */
628 if (ext4_handle_valid(handle))
629 error = ext4_handle_dirty_xattr_block(handle, inode,
630 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500631 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400632 if (!ext4_handle_valid(handle))
633 error = ext4_handle_dirty_xattr_block(handle, inode,
634 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800635 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500636 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500637 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800638 ea_bdebug(bh, "refcount now=%d; releasing",
639 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800641out:
642 ext4_std_error(inode->i_sb, error);
643 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700644}
645
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400646/*
647 * Find the available free space for EAs. This also returns the total number of
648 * bytes used by EA entries.
649 */
650static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
651 size_t *min_offs, void *base, int *total)
652{
653 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400654 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400655 size_t offs = le16_to_cpu(last->e_value_offs);
656 if (offs < *min_offs)
657 *min_offs = offs;
658 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500659 if (total)
660 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400661 }
662 return (*min_offs - ((void *)last - base) - sizeof(__u32));
663}
664
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665static int
Mingming Cao617ba132006-10-11 01:20:53 -0700666ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667{
Mingming Cao617ba132006-10-11 01:20:53 -0700668 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
670
671 /* Compute min_offs and last. */
672 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700673 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400674 if (last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 size_t offs = le16_to_cpu(last->e_value_offs);
676 if (offs < min_offs)
677 min_offs = offs;
678 }
679 }
680 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
681 if (!s->not_found) {
Jan Kara1cba4232016-08-29 15:40:11 -0400682 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700684 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 }
Mingming Cao617ba132006-10-11 01:20:53 -0700686 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 }
688 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400689 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700690 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 return -ENOSPC;
692 }
693
694 if (i->value && s->not_found) {
695 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700696 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
698 memmove((void *)s->here + size, s->here, rest);
699 memset(s->here, 0, size);
700 s->here->e_name_index = i->name_index;
701 s->here->e_name_len = name_len;
702 memcpy(s->here->e_name, i->name, name_len);
703 } else {
Jan Kara1cba4232016-08-29 15:40:11 -0400704 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 void *first_val = s->base + min_offs;
706 size_t offs = le16_to_cpu(s->here->e_value_offs);
707 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700708 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 le32_to_cpu(s->here->e_value_size));
710
Mingming Cao617ba132006-10-11 01:20:53 -0700711 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712 /* The old and the new value have the same
713 size. Just replace. */
714 s->here->e_value_size =
715 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500716 if (i->value == EXT4_ZERO_XATTR_VALUE) {
717 memset(val, 0, size);
718 } else {
719 /* Clear pad bytes first. */
720 memset(val + size - EXT4_XATTR_PAD, 0,
721 EXT4_XATTR_PAD);
722 memcpy(val, i->value, i->value_len);
723 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 return 0;
725 }
726
727 /* Remove the old value. */
728 memmove(first_val + size, first_val, val - first_val);
729 memset(first_val, 0, size);
730 s->here->e_value_size = 0;
731 s->here->e_value_offs = 0;
732 min_offs += size;
733
734 /* Adjust all value offsets. */
735 last = s->first;
736 while (!IS_LAST_ENTRY(last)) {
737 size_t o = le16_to_cpu(last->e_value_offs);
Jan Kara1cba4232016-08-29 15:40:11 -0400738 if (last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 last->e_value_offs =
740 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700741 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 }
743 }
744 if (!i->value) {
745 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700746 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 last = ENTRY((void *)last - size);
748 memmove(s->here, (void *)s->here + size,
749 (void *)last - (void *)s->here + sizeof(__u32));
750 memset(last, 0, size);
751 }
752 }
753
754 if (i->value) {
755 /* Insert the new value. */
756 s->here->e_value_size = cpu_to_le32(i->value_len);
757 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700758 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 void *val = s->base + min_offs - size;
760 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500761 if (i->value == EXT4_ZERO_XATTR_VALUE) {
762 memset(val, 0, size);
763 } else {
764 /* Clear the pad bytes first. */
765 memset(val + size - EXT4_XATTR_PAD, 0,
766 EXT4_XATTR_PAD);
767 memcpy(val, i->value, i->value_len);
768 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769 }
770 }
771 return 0;
772}
773
Mingming Cao617ba132006-10-11 01:20:53 -0700774struct ext4_xattr_block_find {
775 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 struct buffer_head *bh;
777};
778
779static int
Mingming Cao617ba132006-10-11 01:20:53 -0700780ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
781 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782{
783 struct super_block *sb = inode->i_sb;
784 int error;
785
786 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
787 i->name_index, i->name, i->value, (long)i->value_len);
788
Mingming Cao617ba132006-10-11 01:20:53 -0700789 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700791 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 error = -EIO;
793 if (!bs->bh)
794 goto cleanup;
795 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
796 atomic_read(&(bs->bh->b_count)),
797 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400798 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400799 EXT4_ERROR_INODE(inode, "bad block %llu",
800 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400801 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802 goto cleanup;
803 }
804 /* Find the named attribute. */
805 bs->s.base = BHDR(bs->bh);
806 bs->s.first = BFIRST(bs->bh);
807 bs->s.end = bs->bh->b_data + bs->bh->b_size;
808 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700809 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810 i->name, bs->bh->b_size, 1);
811 if (error && error != -ENODATA)
812 goto cleanup;
813 bs->s.not_found = error;
814 }
815 error = 0;
816
817cleanup:
818 return error;
819}
820
821static int
Mingming Cao617ba132006-10-11 01:20:53 -0700822ext4_xattr_block_set(handle_t *handle, struct inode *inode,
823 struct ext4_xattr_info *i,
824 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825{
826 struct super_block *sb = inode->i_sb;
827 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700828 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500829 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800830 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500831 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832
Mingming Cao617ba132006-10-11 01:20:53 -0700833#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834
835 if (i->value && i->value_len > sb->s_blocksize)
836 return -ENOSPC;
837 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400838 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800839 error = ext4_journal_get_write_access(handle, bs->bh);
840 if (error)
841 goto cleanup;
842 lock_buffer(bs->bh);
843
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500845 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
846
847 /*
848 * This must happen under buffer lock for
849 * ext4_xattr_block_set() to reliably detect modified
850 * block
851 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500852 mb_cache_entry_delete_block(ext4_mb_cache, hash,
853 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700855 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 if (!error) {
857 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700858 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700859 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400860 ext4_xattr_cache_insert(ext4_mb_cache,
861 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862 }
863 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400864 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 goto bad_block;
866 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400867 error = ext4_handle_dirty_xattr_block(handle,
868 inode,
869 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870 if (error)
871 goto cleanup;
872 goto inserted;
873 } else {
874 int offset = (char *)s->here - bs->bh->b_data;
875
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800876 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400878 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 error = -ENOMEM;
880 if (s->base == NULL)
881 goto cleanup;
882 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
883 s->first = ENTRY(header(s->base)+1);
884 header(s->base)->h_refcount = cpu_to_le32(1);
885 s->here = ENTRY(s->base + offset);
886 s->end = s->base + bs->bh->b_size;
887 }
888 } else {
889 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400890 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 /* assert(header == s->base) */
892 error = -ENOMEM;
893 if (s->base == NULL)
894 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700895 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896 header(s->base)->h_blocks = cpu_to_le32(1);
897 header(s->base)->h_refcount = cpu_to_le32(1);
898 s->first = ENTRY(header(s->base)+1);
899 s->here = ENTRY(header(s->base)+1);
900 s->end = s->base + sb->s_blocksize;
901 }
902
Mingming Cao617ba132006-10-11 01:20:53 -0700903 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400904 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 goto bad_block;
906 if (error)
907 goto cleanup;
908 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700909 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910
911inserted:
912 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700913 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914 if (new_bh) {
915 /* We found an identical block in the cache. */
916 if (new_bh == bs->bh)
917 ea_bdebug(new_bh, "keeping");
918 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500919 u32 ref;
920
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921 /* The old block is released after updating
922 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500923 error = dquot_alloc_block(inode,
924 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500925 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400927 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700928 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700929 new_bh);
930 if (error)
931 goto cleanup_dquot;
932 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500933 /*
934 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500935 * freeing, rehashing or adding references to
936 * xattr block. Once we hold buffer lock xattr
937 * block's state is stable so we can check
938 * whether the block got freed / rehashed or
939 * not. Since we unhash mbcache entry under
940 * buffer lock when freeing / rehashing xattr
941 * block, checking whether entry is still
942 * hashed is reliable. Same rules hold for
943 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500944 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500945 if (hlist_bl_unhashed(&ce->e_hash_list) ||
946 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500947 /*
948 * Undo everything and check mbcache
949 * again.
950 */
951 unlock_buffer(new_bh);
952 dquot_free_block(inode,
953 EXT4_C2B(EXT4_SB(sb),
954 1));
955 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500956 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500957 ce = NULL;
958 new_bh = NULL;
959 goto inserted;
960 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500961 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
962 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
963 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
964 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500966 ref);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400968 error = ext4_handle_dirty_xattr_block(handle,
969 inode,
970 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 if (error)
972 goto cleanup_dquot;
973 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500974 mb_cache_entry_touch(ext4_mb_cache, ce);
975 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 ce = NULL;
977 } else if (bs->bh && s->base == bs->bh->b_data) {
978 /* We were modifying this block in-place. */
979 ea_bdebug(bs->bh, "keeping this block");
980 new_bh = bs->bh;
981 get_bh(new_bh);
982 } else {
983 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400984 ext4_fsblk_t goal, block;
985
986 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400987 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400988
989 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400990 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400991 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
992
Allison Henderson55f020d2011-05-25 07:41:26 -0400993 block = ext4_new_meta_blocks(handle, inode, goal, 0,
994 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995 if (error)
996 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400997
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400998 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400999 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1000
Joe Perchesace36ad2012-03-19 23:11:43 -04001001 ea_idebug(inode, "creating block %llu",
1002 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003
1004 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001005 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001006 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001008 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001009 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 goto cleanup;
1011 }
1012 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001013 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 if (error) {
1015 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001016 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001017 goto getblk_failed;
1018 }
1019 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1020 set_buffer_uptodate(new_bh);
1021 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001022 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001023 error = ext4_handle_dirty_xattr_block(handle,
1024 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 if (error)
1026 goto cleanup;
1027 }
1028 }
1029
1030 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001031 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032
1033 /* Drop the previous xattr block. */
1034 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001035 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001036 error = 0;
1037
1038cleanup:
1039 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001040 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041 brelse(new_bh);
1042 if (!(bs->bh && s->base == bs->bh->b_data))
1043 kfree(s->base);
1044
1045 return error;
1046
1047cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001048 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001049 goto cleanup;
1050
1051bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001052 EXT4_ERROR_INODE(inode, "bad block %llu",
1053 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 goto cleanup;
1055
1056#undef header
1057}
1058
Tao Ma879b3822012-12-05 10:28:46 -05001059int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1060 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061{
Mingming Cao617ba132006-10-11 01:20:53 -07001062 struct ext4_xattr_ibody_header *header;
1063 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 int error;
1065
Mingming Cao617ba132006-10-11 01:20:53 -07001066 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001068 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001069 header = IHDR(inode, raw_inode);
1070 is->s.base = is->s.first = IFIRST(header);
1071 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001072 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001073 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001074 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 if (error)
1076 return error;
1077 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001078 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 i->name, is->s.end -
1080 (void *)is->s.base, 0);
1081 if (error && error != -ENODATA)
1082 return error;
1083 is->s.not_found = error;
1084 }
1085 return 0;
1086}
1087
Tao Ma0d812f72012-12-10 14:06:02 -05001088int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1089 struct ext4_xattr_info *i,
1090 struct ext4_xattr_ibody_find *is)
1091{
1092 struct ext4_xattr_ibody_header *header;
1093 struct ext4_xattr_search *s = &is->s;
1094 int error;
1095
1096 if (EXT4_I(inode)->i_extra_isize == 0)
1097 return -ENOSPC;
1098 error = ext4_xattr_set_entry(i, s);
1099 if (error) {
1100 if (error == -ENOSPC &&
1101 ext4_has_inline_data(inode)) {
1102 error = ext4_try_to_evict_inline_data(handle, inode,
1103 EXT4_XATTR_LEN(strlen(i->name) +
1104 EXT4_XATTR_SIZE(i->value_len)));
1105 if (error)
1106 return error;
1107 error = ext4_xattr_ibody_find(inode, i, is);
1108 if (error)
1109 return error;
1110 error = ext4_xattr_set_entry(i, s);
1111 }
1112 if (error)
1113 return error;
1114 }
1115 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1116 if (!IS_LAST_ENTRY(s->first)) {
1117 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1118 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1119 } else {
1120 header->h_magic = cpu_to_le32(0);
1121 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1122 }
1123 return 0;
1124}
1125
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001126static int ext4_xattr_ibody_set(struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001127 struct ext4_xattr_info *i,
1128 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129{
Mingming Cao617ba132006-10-11 01:20:53 -07001130 struct ext4_xattr_ibody_header *header;
1131 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 int error;
1133
Mingming Cao617ba132006-10-11 01:20:53 -07001134 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001136 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001137 if (error)
1138 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001139 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001141 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001142 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143 } else {
1144 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001145 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 }
1147 return 0;
1148}
1149
Jan Kara3fd16462016-02-22 22:43:04 -05001150static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1151 struct ext4_xattr_info *i)
1152{
1153 void *value;
1154
1155 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1156 return 0;
1157 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1158 return !memcmp(value, i->value, i->value_len);
1159}
1160
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161/*
Mingming Cao617ba132006-10-11 01:20:53 -07001162 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001164 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 * is NULL to remove an existing extended attribute, and non-NULL to
1166 * either replace an existing extended attribute, or create a new extended
1167 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1168 * specify that an extended attribute must exist and must not exist
1169 * previous to the call, respectively.
1170 *
1171 * Returns 0, or a negative error number on failure.
1172 */
1173int
Mingming Cao617ba132006-10-11 01:20:53 -07001174ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 const char *name, const void *value, size_t value_len,
1176 int flags)
1177{
Mingming Cao617ba132006-10-11 01:20:53 -07001178 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 .name_index = name_index,
1180 .name = name,
1181 .value = value,
1182 .value_len = value_len,
1183
1184 };
Mingming Cao617ba132006-10-11 01:20:53 -07001185 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186 .s = { .not_found = -ENODATA, },
1187 };
Mingming Cao617ba132006-10-11 01:20:53 -07001188 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001189 .s = { .not_found = -ENODATA, },
1190 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05001191 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 int error;
1193
1194 if (!name)
1195 return -EINVAL;
1196 if (strlen(name) > 255)
1197 return -ERANGE;
Theodore Ts'oc755e252017-01-11 21:50:46 -05001198 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001199
Eric Sandeen66543612011-10-26 03:32:07 -04001200 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001201 if (error)
1202 goto cleanup;
1203
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001204 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001205 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1206 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001207 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 }
1209
Mingming Cao617ba132006-10-11 01:20:53 -07001210 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001211 if (error)
1212 goto cleanup;
1213 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001214 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 if (error)
1216 goto cleanup;
1217 if (is.s.not_found && bs.s.not_found) {
1218 error = -ENODATA;
1219 if (flags & XATTR_REPLACE)
1220 goto cleanup;
1221 error = 0;
1222 if (!value)
1223 goto cleanup;
1224 } else {
1225 error = -EEXIST;
1226 if (flags & XATTR_CREATE)
1227 goto cleanup;
1228 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 if (!value) {
1230 if (!is.s.not_found)
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001231 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001233 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001235 error = 0;
1236 /* Xattr value did not change? Save us some work and bail out */
1237 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1238 goto cleanup;
1239 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1240 goto cleanup;
1241
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001242 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243 if (!error && !bs.s.not_found) {
1244 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001245 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001247 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1248 error = ext4_xattr_block_find(inode, &i, &bs);
1249 if (error)
1250 goto cleanup;
1251 }
Mingming Cao617ba132006-10-11 01:20:53 -07001252 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253 if (error)
1254 goto cleanup;
1255 if (!is.s.not_found) {
1256 i.value = NULL;
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001257 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 }
1259 }
1260 }
1261 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001262 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001263 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001264 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05001265 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001266 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001267 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001268 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 * error != 0.
1270 */
1271 is.iloc.bh = NULL;
1272 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001273 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274 }
1275
1276cleanup:
1277 brelse(is.iloc.bh);
1278 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001279 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280 return error;
1281}
1282
1283/*
Mingming Cao617ba132006-10-11 01:20:53 -07001284 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001285 *
Mingming Cao617ba132006-10-11 01:20:53 -07001286 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 * attribute modification is a filesystem transaction by itself.
1288 *
1289 * Returns 0, or a negative error number on failure.
1290 */
1291int
Mingming Cao617ba132006-10-11 01:20:53 -07001292ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 const void *value, size_t value_len, int flags)
1294{
1295 handle_t *handle;
1296 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001297 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298
1299retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001300 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301 if (IS_ERR(handle)) {
1302 error = PTR_ERR(handle);
1303 } else {
1304 int error2;
1305
Mingming Cao617ba132006-10-11 01:20:53 -07001306 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001308 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001310 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001311 goto retry;
1312 if (error == 0)
1313 error = error2;
1314 }
1315
1316 return error;
1317}
1318
1319/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001320 * Shift the EA entries in the inode to create space for the increased
1321 * i_extra_isize.
1322 */
1323static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1324 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001325 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001326{
1327 struct ext4_xattr_entry *last = entry;
1328 int new_offs;
1329
Jan Kara94405712016-08-29 15:41:11 -04001330 /* We always shift xattr headers further thus offsets get lower */
1331 BUG_ON(value_offs_shift > 0);
1332
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001333 /* Adjust the value offsets of the entries */
1334 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -04001335 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001336 new_offs = le16_to_cpu(last->e_value_offs) +
1337 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001338 last->e_value_offs = cpu_to_le16(new_offs);
1339 }
1340 }
1341 /* Shift the entries by n bytes */
1342 memmove(to, from, n);
1343}
1344
1345/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001346 * Move xattr pointed to by 'entry' from inode into external xattr block
1347 */
1348static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1349 struct ext4_inode *raw_inode,
1350 struct ext4_xattr_entry *entry)
1351{
1352 struct ext4_xattr_ibody_find *is = NULL;
1353 struct ext4_xattr_block_find *bs = NULL;
1354 char *buffer = NULL, *b_entry_name = NULL;
1355 size_t value_offs, value_size;
1356 struct ext4_xattr_info i = {
1357 .value = NULL,
1358 .value_len = 0,
1359 .name_index = entry->e_name_index,
1360 };
1361 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1362 int error;
1363
1364 value_offs = le16_to_cpu(entry->e_value_offs);
1365 value_size = le32_to_cpu(entry->e_value_size);
1366
1367 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1368 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1369 buffer = kmalloc(value_size, GFP_NOFS);
1370 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1371 if (!is || !bs || !buffer || !b_entry_name) {
1372 error = -ENOMEM;
1373 goto out;
1374 }
1375
1376 is->s.not_found = -ENODATA;
1377 bs->s.not_found = -ENODATA;
1378 is->iloc.bh = NULL;
1379 bs->bh = NULL;
1380
1381 /* Save the entry name and the entry value */
1382 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1383 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1384 b_entry_name[entry->e_name_len] = '\0';
1385 i.name = b_entry_name;
1386
1387 error = ext4_get_inode_loc(inode, &is->iloc);
1388 if (error)
1389 goto out;
1390
1391 error = ext4_xattr_ibody_find(inode, &i, is);
1392 if (error)
1393 goto out;
1394
1395 /* Remove the chosen entry from the inode */
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001396 error = ext4_xattr_ibody_set(inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001397 if (error)
1398 goto out;
1399
1400 i.name = b_entry_name;
1401 i.value = buffer;
1402 i.value_len = value_size;
1403 error = ext4_xattr_block_find(inode, &i, bs);
1404 if (error)
1405 goto out;
1406
1407 /* Add entry which was removed from the inode into the block */
1408 error = ext4_xattr_block_set(handle, inode, &i, bs);
1409 if (error)
1410 goto out;
1411 error = 0;
1412out:
1413 kfree(b_entry_name);
1414 kfree(buffer);
1415 if (is)
1416 brelse(is->iloc.bh);
1417 kfree(is);
1418 kfree(bs);
1419
1420 return error;
1421}
1422
Jan Karadfa20642016-08-29 15:44:11 -04001423static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1424 struct ext4_inode *raw_inode,
1425 int isize_diff, size_t ifree,
1426 size_t bfree, int *total_ino)
1427{
1428 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1429 struct ext4_xattr_entry *small_entry;
1430 struct ext4_xattr_entry *entry;
1431 struct ext4_xattr_entry *last;
1432 unsigned int entry_size; /* EA entry size */
1433 unsigned int total_size; /* EA entry size + value size */
1434 unsigned int min_total_size;
1435 int error;
1436
1437 while (isize_diff > ifree) {
1438 entry = NULL;
1439 small_entry = NULL;
1440 min_total_size = ~0U;
1441 last = IFIRST(header);
1442 /* Find the entry best suited to be pushed into EA block */
1443 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1444 total_size =
1445 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1446 EXT4_XATTR_LEN(last->e_name_len);
1447 if (total_size <= bfree &&
1448 total_size < min_total_size) {
1449 if (total_size + ifree < isize_diff) {
1450 small_entry = last;
1451 } else {
1452 entry = last;
1453 min_total_size = total_size;
1454 }
1455 }
1456 }
1457
1458 if (entry == NULL) {
1459 if (small_entry == NULL)
1460 return -ENOSPC;
1461 entry = small_entry;
1462 }
1463
1464 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1465 total_size = entry_size +
1466 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1467 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1468 entry);
1469 if (error)
1470 return error;
1471
1472 *total_ino -= entry_size;
1473 ifree += total_size;
1474 bfree -= total_size;
1475 }
1476
1477 return 0;
1478}
1479
Jan Kara3f2571c2016-08-29 15:42:11 -04001480/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001481 * Expand an inode by new_extra_isize bytes when EAs are present.
1482 * Returns 0 on success or negative error number on failure.
1483 */
1484int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1485 struct ext4_inode *raw_inode, handle_t *handle)
1486{
1487 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001488 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001489 size_t min_offs;
1490 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001491 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001492 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001493 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001494 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04001495 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001496 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001497
Theodore Ts'oc755e252017-01-11 21:50:46 -05001498 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1499 return 0;
1500
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001501retry:
Jan Karad0141192016-08-11 11:50:30 -04001502 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001503 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1504 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001505
1506 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001507
1508 /*
1509 * Check if enough free space is available in the inode to shift the
1510 * entries ahead by new_extra_isize.
1511 */
1512
Jan Kara6e0cd082016-08-29 15:43:11 -04001513 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001514 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1515 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001516 total_ino = sizeof(struct ext4_xattr_ibody_header);
1517
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001518 error = xattr_check_inode(inode, header, end);
1519 if (error)
1520 goto cleanup;
1521
Jan Kara6e0cd082016-08-29 15:43:11 -04001522 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001523 if (ifree >= isize_diff)
1524 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001525
1526 /*
1527 * Enough free space isn't available in the inode, check if
1528 * EA block can hold new_extra_isize bytes.
1529 */
1530 if (EXT4_I(inode)->i_file_acl) {
1531 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1532 error = -EIO;
1533 if (!bh)
1534 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001535 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001536 EXT4_ERROR_INODE(inode, "bad block %llu",
1537 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001538 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001539 goto cleanup;
1540 }
1541 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001542 end = bh->b_data + bh->b_size;
1543 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001544 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1545 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001546 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001547 if (!tried_min_extra_isize && s_min_extra_isize) {
1548 tried_min_extra_isize++;
1549 new_extra_isize = s_min_extra_isize;
1550 brelse(bh);
1551 goto retry;
1552 }
Jan Karadfa20642016-08-29 15:44:11 -04001553 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001554 goto cleanup;
1555 }
1556 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001557 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001558 }
1559
Jan Karadfa20642016-08-29 15:44:11 -04001560 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1561 isize_diff, ifree, bfree,
1562 &total_ino);
1563 if (error) {
1564 if (error == -ENOSPC && !tried_min_extra_isize &&
1565 s_min_extra_isize) {
1566 tried_min_extra_isize++;
1567 new_extra_isize = s_min_extra_isize;
1568 brelse(bh);
1569 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001570 }
Jan Karadfa20642016-08-29 15:44:11 -04001571 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001572 }
Jan Karae3014d12016-08-29 15:38:11 -04001573shift:
1574 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001575 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001576 - new_extra_isize, (void *)raw_inode +
1577 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001578 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001579 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001580 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001581out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001582 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001583 return 0;
1584
1585cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001586 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001587 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05001588 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001589 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001590 no_expand = 1;
1591 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001592 return error;
1593}
1594
1595
1596
1597/*
Mingming Cao617ba132006-10-11 01:20:53 -07001598 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599 *
1600 * Free extended attribute resources associated with this inode. This
1601 * is called immediately before an inode is freed. We have exclusive
1602 * access to the inode.
1603 */
1604void
Mingming Cao617ba132006-10-11 01:20:53 -07001605ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606{
1607 struct buffer_head *bh = NULL;
1608
Mingming Cao617ba132006-10-11 01:20:53 -07001609 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001611 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001613 EXT4_ERROR_INODE(inode, "block %llu read error",
1614 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 goto cleanup;
1616 }
Mingming Cao617ba132006-10-11 01:20:53 -07001617 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001619 EXT4_ERROR_INODE(inode, "bad block %llu",
1620 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621 goto cleanup;
1622 }
Mingming Cao617ba132006-10-11 01:20:53 -07001623 ext4_xattr_release_block(handle, inode, bh);
1624 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625
1626cleanup:
1627 brelse(bh);
1628}
1629
1630/*
Mingming Cao617ba132006-10-11 01:20:53 -07001631 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 *
1633 * Create a new entry in the extended attribute cache, and insert
1634 * it unless such an entry is already in the cache.
1635 *
1636 * Returns 0, or a negative error number on failure.
1637 */
1638static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001639ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001641 struct ext4_xattr_header *header = BHDR(bh);
1642 __u32 hash = le32_to_cpu(header->h_hash);
1643 int reusable = le32_to_cpu(header->h_refcount) <
1644 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001645 int error;
1646
Jan Kara7a2508e2016-02-22 22:35:22 -05001647 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001648 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001650 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001652 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654}
1655
1656/*
Mingming Cao617ba132006-10-11 01:20:53 -07001657 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001658 *
1659 * Compare two extended attribute blocks for equality.
1660 *
1661 * Returns 0 if the blocks are equal, 1 if they differ, and
1662 * a negative error number on errors.
1663 */
1664static int
Mingming Cao617ba132006-10-11 01:20:53 -07001665ext4_xattr_cmp(struct ext4_xattr_header *header1,
1666 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667{
Mingming Cao617ba132006-10-11 01:20:53 -07001668 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669
1670 entry1 = ENTRY(header1+1);
1671 entry2 = ENTRY(header2+1);
1672 while (!IS_LAST_ENTRY(entry1)) {
1673 if (IS_LAST_ENTRY(entry2))
1674 return 1;
1675 if (entry1->e_hash != entry2->e_hash ||
1676 entry1->e_name_index != entry2->e_name_index ||
1677 entry1->e_name_len != entry2->e_name_len ||
1678 entry1->e_value_size != entry2->e_value_size ||
1679 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1680 return 1;
1681 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001682 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1684 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1685 le32_to_cpu(entry1->e_value_size)))
1686 return 1;
1687
Mingming Cao617ba132006-10-11 01:20:53 -07001688 entry1 = EXT4_XATTR_NEXT(entry1);
1689 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690 }
1691 if (!IS_LAST_ENTRY(entry2))
1692 return 1;
1693 return 0;
1694}
1695
1696/*
Mingming Cao617ba132006-10-11 01:20:53 -07001697 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 *
1699 * Find an identical extended attribute block.
1700 *
1701 * Returns a pointer to the block found, or NULL if such a block was
1702 * not found or an error occurred.
1703 */
1704static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001705ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001706 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707{
1708 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001709 struct mb_cache_entry *ce;
1710 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711
1712 if (!header->h_hash)
1713 return NULL; /* never share */
1714 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001715 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001716 while (ce) {
1717 struct buffer_head *bh;
1718
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719 bh = sb_bread(inode->i_sb, ce->e_block);
1720 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001721 EXT4_ERROR_INODE(inode, "block %lu read error",
1722 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001723 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 *pce = ce;
1725 return bh;
1726 }
1727 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001728 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729 }
1730 return NULL;
1731}
1732
1733#define NAME_HASH_SHIFT 5
1734#define VALUE_HASH_SHIFT 16
1735
1736/*
Mingming Cao617ba132006-10-11 01:20:53 -07001737 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001738 *
1739 * Compute the hash of an extended attribute.
1740 */
Mingming Cao617ba132006-10-11 01:20:53 -07001741static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1742 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743{
1744 __u32 hash = 0;
1745 char *name = entry->e_name;
1746 int n;
1747
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001748 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001749 hash = (hash << NAME_HASH_SHIFT) ^
1750 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1751 *name++;
1752 }
1753
Jan Kara1cba4232016-08-29 15:40:11 -04001754 if (entry->e_value_size != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 __le32 *value = (__le32 *)((char *)header +
1756 le16_to_cpu(entry->e_value_offs));
1757 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001758 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759 hash = (hash << VALUE_HASH_SHIFT) ^
1760 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1761 le32_to_cpu(*value++);
1762 }
1763 }
1764 entry->e_hash = cpu_to_le32(hash);
1765}
1766
1767#undef NAME_HASH_SHIFT
1768#undef VALUE_HASH_SHIFT
1769
1770#define BLOCK_HASH_SHIFT 16
1771
1772/*
Mingming Cao617ba132006-10-11 01:20:53 -07001773 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 *
1775 * Re-compute the extended attribute hash value after an entry has changed.
1776 */
Mingming Cao617ba132006-10-11 01:20:53 -07001777static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1778 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779{
Mingming Cao617ba132006-10-11 01:20:53 -07001780 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781 __u32 hash = 0;
1782
Mingming Cao617ba132006-10-11 01:20:53 -07001783 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001784 here = ENTRY(header+1);
1785 while (!IS_LAST_ENTRY(here)) {
1786 if (!here->e_hash) {
1787 /* Block is not shared if an entry's hash value == 0 */
1788 hash = 0;
1789 break;
1790 }
1791 hash = (hash << BLOCK_HASH_SHIFT) ^
1792 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1793 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001794 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795 }
1796 header->h_hash = cpu_to_le32(hash);
1797}
1798
1799#undef BLOCK_HASH_SHIFT
1800
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001801#define HASH_BUCKET_BITS 10
1802
Jan Kara7a2508e2016-02-22 22:35:22 -05001803struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001804ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001805{
Jan Kara7a2508e2016-02-22 22:35:22 -05001806 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001807}
1808
Jan Kara7a2508e2016-02-22 22:35:22 -05001809void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001810{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001811 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001812 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001814