blob: 354c55c3f70c74e9734f692ece1f6d1e54742cc6 [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
Tahsin Erdogan47387402017-06-22 11:28:55 -040075static void ext4_xattr_block_cache_insert(struct mb_cache *,
76 struct buffer_head *);
77static struct buffer_head *
78ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
79 struct mb_cache_entry **);
Tahsin Erdogandaf83282017-06-22 11:52:03 -040080static void ext4_xattr_hash_entry(struct ext4_xattr_entry *entry,
81 void *value_base);
82static void ext4_xattr_rehash(struct ext4_xattr_header *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083
Eric Biggersd6006182017-04-29 23:47:50 -040084static const struct xattr_handler * const 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
Tahsin Erdogan47387402017-06-22 11:28:55 -0400109#define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_ea_block_cache)
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400111
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400112#define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
113 inode->i_sb->s_fs_info)->s_ea_inode_cache)
114
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400115static int
116ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
117 struct inode *inode);
118
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400119#ifdef CONFIG_LOCKDEP
120void ext4_xattr_inode_set_class(struct inode *ea_inode)
121{
122 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
123}
124#endif
125
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400126static __le32 ext4_xattr_block_csum(struct inode *inode,
127 sector_t block_nr,
128 struct ext4_xattr_header *hdr)
129{
130 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400131 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400132 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400133 __u32 dummy_csum = 0;
134 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135
Theodore Ts'od6a77102013-04-09 23:59:55 -0400136 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
137 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400138 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
139 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
140 offset += sizeof(dummy_csum);
141 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
142 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400143
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400144 return cpu_to_le32(csum);
145}
146
147static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400148 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400149{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400150 struct ext4_xattr_header *hdr = BHDR(bh);
151 int ret = 1;
152
153 if (ext4_has_metadata_csum(inode->i_sb)) {
154 lock_buffer(bh);
155 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
156 bh->b_blocknr, hdr));
157 unlock_buffer(bh);
158 }
159 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400160}
161
162static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400163 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400164{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400165 if (ext4_has_metadata_csum(inode->i_sb))
166 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
167 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400168}
169
Stephen Hemminger11e27522010-05-13 17:53:18 -0700170static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700171ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700173 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174
Mingming Cao617ba132006-10-11 01:20:53 -0700175 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
176 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 return handler;
178}
179
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400181ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
182 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400184 struct ext4_xattr_entry *e = entry;
185
Eric Biggersd7614cc2016-12-01 14:57:29 -0500186 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400187 while (!IS_LAST_ENTRY(e)) {
188 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400190 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400191 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400193
Eric Biggersd7614cc2016-12-01 14:57:29 -0500194 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400195 while (!IS_LAST_ENTRY(entry)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400196 if (entry->e_value_size != 0 &&
197 entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500198 u16 offs = le16_to_cpu(entry->e_value_offs);
199 u32 size = le32_to_cpu(entry->e_value_size);
200 void *value;
201
202 /*
203 * The value cannot overlap the names, and the value
204 * with padding cannot extend beyond 'end'. Check both
205 * the padded and unpadded sizes, since the size may
206 * overflow to 0 when adding padding.
207 */
208 if (offs > end - value_start)
209 return -EFSCORRUPTED;
210 value = value_start + offs;
211 if (value < (void *)e + sizeof(u32) ||
212 size > end - value ||
213 EXT4_XATTR_SIZE(size) > end - value)
214 return -EFSCORRUPTED;
215 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400216 entry = EXT4_XATTR_NEXT(entry);
217 }
218
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 return 0;
220}
221
222static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400223ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400225 int error;
226
227 if (buffer_verified(bh))
228 return 0;
229
Mingming Cao617ba132006-10-11 01:20:53 -0700230 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400232 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400233 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400234 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400235 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
236 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400237 if (!error)
238 set_buffer_verified(bh);
239 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240}
241
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400242static int
243__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
244 void *end, const char *function, unsigned int line)
245{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400246 int error = -EFSCORRUPTED;
247
Eric Biggers290ab232016-12-01 14:51:58 -0500248 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400249 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400250 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400251 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400252errout:
253 if (error)
254 __ext4_error_inode(inode, function, line, 0,
255 "corrupted in-inode xattr");
256 return error;
257}
258
259#define xattr_check_inode(inode, header, end) \
260 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
261
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262static int
Mingming Cao617ba132006-10-11 01:20:53 -0700263ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400264 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265{
Mingming Cao617ba132006-10-11 01:20:53 -0700266 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 size_t name_len;
268 int cmp = 1;
269
270 if (name == NULL)
271 return -EINVAL;
272 name_len = strlen(name);
273 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700274 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 cmp = name_index - entry->e_name_index;
276 if (!cmp)
277 cmp = name_len - entry->e_name_len;
278 if (!cmp)
279 cmp = memcmp(name, entry->e_name, name_len);
280 if (cmp <= 0 && (sorted || cmp == 0))
281 break;
282 }
283 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 return cmp ? -ENODATA : 0;
285}
286
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400287static u32
288ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
289{
290 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
291}
292
293static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
294{
295 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
296 ((u32)ea_inode->i_version);
297}
298
299static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
300{
301 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
302 ea_inode->i_version = (u32)ref_count;
303}
304
305static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
306{
307 return (u32)ea_inode->i_atime.tv_sec;
308}
309
310static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
311{
312 ea_inode->i_atime.tv_sec = hash;
313}
314
Andreas Dilgere50e5122017-06-21 21:10:32 -0400315/*
316 * Read the EA value from an inode.
317 */
Tahsin Erdogan909666932017-06-21 21:57:36 -0400318static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400319{
320 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400321 struct buffer_head *bh;
Tahsin Erdogan909666932017-06-21 21:57:36 -0400322 int blocksize = ea_inode->i_sb->s_blocksize;
323 size_t csize, copied = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400324 void *copy_pos = buf;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400325
Tahsin Erdogan909666932017-06-21 21:57:36 -0400326 while (copied < size) {
327 csize = (size - copied) > blocksize ? blocksize : size - copied;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400328 bh = ext4_bread(NULL, ea_inode, block, 0);
Tahsin Erdogan909666932017-06-21 21:57:36 -0400329 if (IS_ERR(bh))
Andreas Dilgere50e5122017-06-21 21:10:32 -0400330 return PTR_ERR(bh);
Tahsin Erdogan909666932017-06-21 21:57:36 -0400331 if (!bh)
332 return -EFSCORRUPTED;
333
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400334 memcpy(copy_pos, bh->b_data, csize);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400335 brelse(bh);
336
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400337 copy_pos += csize;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400338 block += 1;
Tahsin Erdogan909666932017-06-21 21:57:36 -0400339 copied += csize;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400340 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400341 return 0;
342}
343
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400344static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
345 struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400346{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400347 struct inode *inode;
348 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400349
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400350 inode = ext4_iget(parent->i_sb, ea_ino);
351 if (IS_ERR(inode)) {
352 err = PTR_ERR(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400353 ext4_error(parent->i_sb,
354 "error while reading EA inode %lu err=%d", ea_ino,
355 err);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400356 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400357 }
358
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400359 if (is_bad_inode(inode)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400360 ext4_error(parent->i_sb,
361 "error while reading EA inode %lu is_bad_inode",
362 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400363 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400364 goto error;
365 }
366
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400367 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400368 ext4_error(parent->i_sb,
369 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
370 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400371 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400372 goto error;
373 }
374
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400375 *ea_inode = inode;
376 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400377error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400378 iput(inode);
379 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400380}
381
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400382static int
383ext4_xattr_inode_verify_hash(struct inode *ea_inode, void *buffer, size_t size)
384{
385 u32 hash;
386
387 /* Verify stored hash matches calculated hash. */
388 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
389 if (hash != ext4_xattr_inode_get_hash(ea_inode))
390 return -EFSCORRUPTED;
391 return 0;
392}
393
394#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
395
Andreas Dilgere50e5122017-06-21 21:10:32 -0400396/*
397 * Read the value from the EA inode.
398 */
399static int
400ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
Tahsin Erdogan909666932017-06-21 21:57:36 -0400401 size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400402{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400403 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400404 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400405 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400406
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400407 err = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
408 if (err) {
409 ea_inode = NULL;
410 goto out;
411 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400412
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400413 if (i_size_read(ea_inode) != size) {
414 ext4_warning_inode(ea_inode,
415 "ea_inode file size=%llu entry size=%zu",
416 i_size_read(ea_inode), size);
417 err = -EFSCORRUPTED;
418 goto out;
419 }
420
421 err = ext4_xattr_inode_read(ea_inode, buffer, size);
422 if (err)
423 goto out;
424
425 err = ext4_xattr_inode_verify_hash(ea_inode, buffer, size);
426 /*
427 * Compatibility check for old Lustre ea_inode implementation. Old
428 * version does not have hash validation, but it has a backpointer
429 * from ea_inode to the parent inode.
430 */
431 if (err == -EFSCORRUPTED) {
432 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != inode->i_ino ||
433 ea_inode->i_generation != inode->i_generation) {
434 ext4_warning_inode(ea_inode,
435 "EA inode hash validation failed");
436 goto out;
437 }
438 /* Do not add ea_inode to the cache. */
439 ea_inode_cache = NULL;
440 } else if (err)
441 goto out;
442
443 if (ea_inode_cache)
444 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
445 ext4_xattr_inode_get_hash(ea_inode),
446 ea_inode->i_ino, true /* reusable */);
447out:
Andreas Dilgere50e5122017-06-21 21:10:32 -0400448 iput(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400449 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400450}
451
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700452static int
Mingming Cao617ba132006-10-11 01:20:53 -0700453ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700454 void *buffer, size_t buffer_size)
455{
456 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700457 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 size_t size;
459 int error;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400460 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461
462 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
463 name_index, name, buffer, (long)buffer_size);
464
465 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700466 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700467 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400468 ea_idebug(inode, "reading block %llu",
469 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700470 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 if (!bh)
472 goto cleanup;
473 ea_bdebug(bh, "b_count=%d, refcount=%d",
474 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400475 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400476 EXT4_ERROR_INODE(inode, "bad block %llu",
477 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400478 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479 goto cleanup;
480 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400481 ext4_xattr_block_cache_insert(ea_block_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400483 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484 if (error)
485 goto cleanup;
486 size = le32_to_cpu(entry->e_value_size);
487 if (buffer) {
488 error = -ERANGE;
489 if (size > buffer_size)
490 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400491 if (entry->e_value_inum) {
492 error = ext4_xattr_inode_get(inode,
493 le32_to_cpu(entry->e_value_inum),
Tahsin Erdogan909666932017-06-21 21:57:36 -0400494 buffer, size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400495 if (error)
496 goto cleanup;
497 } else {
498 memcpy(buffer, bh->b_data +
499 le16_to_cpu(entry->e_value_offs), size);
500 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501 }
502 error = size;
503
504cleanup:
505 brelse(bh);
506 return error;
507}
508
Tao Ma879b3822012-12-05 10:28:46 -0500509int
Mingming Cao617ba132006-10-11 01:20:53 -0700510ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 void *buffer, size_t buffer_size)
512{
Mingming Cao617ba132006-10-11 01:20:53 -0700513 struct ext4_xattr_ibody_header *header;
514 struct ext4_xattr_entry *entry;
515 struct ext4_inode *raw_inode;
516 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 size_t size;
518 void *end;
519 int error;
520
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500521 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700523 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 if (error)
525 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700526 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700528 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400529 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530 if (error)
531 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400532 entry = IFIRST(header);
533 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534 if (error)
535 goto cleanup;
536 size = le32_to_cpu(entry->e_value_size);
537 if (buffer) {
538 error = -ERANGE;
539 if (size > buffer_size)
540 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400541 if (entry->e_value_inum) {
542 error = ext4_xattr_inode_get(inode,
543 le32_to_cpu(entry->e_value_inum),
Tahsin Erdogan909666932017-06-21 21:57:36 -0400544 buffer, size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400545 if (error)
546 goto cleanup;
547 } else {
548 memcpy(buffer, (void *)IFIRST(header) +
549 le16_to_cpu(entry->e_value_offs), size);
550 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 }
552 error = size;
553
554cleanup:
555 brelse(iloc.bh);
556 return error;
557}
558
559/*
Mingming Cao617ba132006-10-11 01:20:53 -0700560 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 *
562 * Copy an extended attribute into the buffer
563 * provided, or compute the buffer size required.
564 * Buffer is NULL to compute the size of the buffer required.
565 *
566 * Returns a negative error number on failure, or the number of bytes
567 * used / required on success.
568 */
569int
Mingming Cao617ba132006-10-11 01:20:53 -0700570ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700571 void *buffer, size_t buffer_size)
572{
573 int error;
574
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500575 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
576 return -EIO;
577
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400578 if (strlen(name) > 255)
579 return -ERANGE;
580
Mingming Cao617ba132006-10-11 01:20:53 -0700581 down_read(&EXT4_I(inode)->xattr_sem);
582 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583 buffer_size);
584 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700585 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700587 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 return error;
589}
590
591static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000592ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 char *buffer, size_t buffer_size)
594{
595 size_t rest = buffer_size;
596
Mingming Cao617ba132006-10-11 01:20:53 -0700597 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700598 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700599 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100601 if (handler && (!handler->list || handler->list(dentry))) {
602 const char *prefix = handler->prefix ?: handler->name;
603 size_t prefix_len = strlen(prefix);
604 size_t size = prefix_len + entry->e_name_len + 1;
605
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 if (buffer) {
607 if (size > rest)
608 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100609 memcpy(buffer, prefix, prefix_len);
610 buffer += prefix_len;
611 memcpy(buffer, entry->e_name, entry->e_name_len);
612 buffer += entry->e_name_len;
613 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700614 }
615 rest -= size;
616 }
617 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100618 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619}
620
621static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000622ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623{
David Howells2b0143b2015-03-17 22:25:59 +0000624 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 struct buffer_head *bh = NULL;
626 int error;
627
628 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
629 buffer, (long)buffer_size);
630
631 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700632 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700633 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400634 ea_idebug(inode, "reading block %llu",
635 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700636 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700637 error = -EIO;
638 if (!bh)
639 goto cleanup;
640 ea_bdebug(bh, "b_count=%d, refcount=%d",
641 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400642 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400643 EXT4_ERROR_INODE(inode, "bad block %llu",
644 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400645 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 goto cleanup;
647 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400648 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000649 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650
651cleanup:
652 brelse(bh);
653
654 return error;
655}
656
657static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000658ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659{
David Howells2b0143b2015-03-17 22:25:59 +0000660 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700661 struct ext4_xattr_ibody_header *header;
662 struct ext4_inode *raw_inode;
663 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664 void *end;
665 int error;
666
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500667 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700669 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 if (error)
671 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700672 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700674 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400675 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 if (error)
677 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000678 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679 buffer, buffer_size);
680
681cleanup:
682 brelse(iloc.bh);
683 return error;
684}
685
686/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400687 * Inode operation listxattr()
688 *
689 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690 *
691 * Copy a list of attribute names into the buffer
692 * provided, or compute the buffer size required.
693 * Buffer is NULL to compute the size of the buffer required.
694 *
695 * Returns a negative error number on failure, or the number of bytes
696 * used / required on success.
697 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400698ssize_t
699ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500701 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700702
David Howells2b0143b2015-03-17 22:25:59 +0000703 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500704 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
705 if (ret < 0)
706 goto errout;
707 if (buffer) {
708 buffer += ret;
709 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500711 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
712 if (ret < 0)
713 goto errout;
714 ret += ret2;
715errout:
David Howells2b0143b2015-03-17 22:25:59 +0000716 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500717 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718}
719
720/*
Mingming Cao617ba132006-10-11 01:20:53 -0700721 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 * not set, set it.
723 */
Mingming Cao617ba132006-10-11 01:20:53 -0700724static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 struct super_block *sb)
726{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400727 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 return;
729
liang xie5d601252014-05-12 22:06:43 -0400730 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700731 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400732 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400733 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735}
736
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400737int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
738{
739 struct ext4_iloc iloc = { .bh = NULL };
740 struct buffer_head *bh = NULL;
741 struct ext4_inode *raw_inode;
742 struct ext4_xattr_ibody_header *header;
743 struct ext4_xattr_entry *entry;
744 qsize_t ea_inode_refs = 0;
745 void *end;
746 int ret;
747
748 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
749
750 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
751 ret = ext4_get_inode_loc(inode, &iloc);
752 if (ret)
753 goto out;
754 raw_inode = ext4_raw_inode(&iloc);
755 header = IHDR(inode, raw_inode);
756 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
757 ret = xattr_check_inode(inode, header, end);
758 if (ret)
759 goto out;
760
761 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
762 entry = EXT4_XATTR_NEXT(entry))
763 if (entry->e_value_inum)
764 ea_inode_refs++;
765 }
766
767 if (EXT4_I(inode)->i_file_acl) {
768 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
769 if (!bh) {
770 ret = -EIO;
771 goto out;
772 }
773
774 if (ext4_xattr_check_block(inode, bh)) {
775 ret = -EFSCORRUPTED;
776 goto out;
777 }
778
779 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
780 entry = EXT4_XATTR_NEXT(entry))
781 if (entry->e_value_inum)
782 ea_inode_refs++;
783 }
784 *usage = ea_inode_refs + 1;
785 ret = 0;
786out:
787 brelse(iloc.bh);
788 brelse(bh);
789 return ret;
790}
791
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400792static inline size_t round_up_cluster(struct inode *inode, size_t length)
793{
794 struct super_block *sb = inode->i_sb;
795 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
796 inode->i_blkbits);
797 size_t mask = ~(cluster_size - 1);
798
799 return (length + cluster_size - 1) & mask;
800}
801
802static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
803{
804 int err;
805
806 err = dquot_alloc_inode(inode);
807 if (err)
808 return err;
809 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
810 if (err)
811 dquot_free_inode(inode);
812 return err;
813}
814
815static void ext4_xattr_inode_free_quota(struct inode *inode, size_t len)
816{
817 dquot_free_space_nodirty(inode, round_up_cluster(inode, len));
818 dquot_free_inode(inode);
819}
820
821static int __ext4_xattr_set_credits(struct inode *inode,
822 struct buffer_head *block_bh,
823 size_t value_len)
824{
825 struct super_block *sb = inode->i_sb;
826 int credits;
827 int blocks;
828
829 /*
830 * 1) Owner inode update
831 * 2) Ref count update on old xattr block
832 * 3) new xattr block
833 * 4) block bitmap update for new xattr block
834 * 5) group descriptor for new xattr block
835 * 6) block bitmap update for old xattr block
836 * 7) group descriptor for old block
837 *
838 * 6 & 7 can happen if we have two racing threads T_a and T_b
839 * which are each trying to set an xattr on inodes I_a and I_b
840 * which were both initially sharing an xattr block.
841 */
842 credits = 7;
843
844 /* Quota updates. */
845 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
846
847 /*
848 * In case of inline data, we may push out the data to a block,
849 * so we need to reserve credits for this eventuality
850 */
851 if (ext4_has_inline_data(inode))
852 credits += ext4_writepage_trans_blocks(inode) + 1;
853
854 /* We are done if ea_inode feature is not enabled. */
855 if (!ext4_has_feature_ea_inode(sb))
856 return credits;
857
858 /* New ea_inode, inode map, block bitmap, group descriptor. */
859 credits += 4;
860
861 /* Data blocks. */
862 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
863
864 /* Indirection block or one level of extent tree. */
865 blocks += 1;
866
867 /* Block bitmap and group descriptor updates for each block. */
868 credits += blocks * 2;
869
870 /* Blocks themselves. */
871 credits += blocks;
872
873 /* Dereference ea_inode holding old xattr value.
874 * Old ea_inode, inode map, block bitmap, group descriptor.
875 */
876 credits += 4;
877
878 /* Data blocks for old ea_inode. */
879 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
880
881 /* Indirection block or one level of extent tree for old ea_inode. */
882 blocks += 1;
883
884 /* Block bitmap and group descriptor updates for each block. */
885 credits += blocks * 2;
886
887 /* We may need to clone the existing xattr block in which case we need
888 * to increment ref counts for existing ea_inodes referenced by it.
889 */
890 if (block_bh) {
891 struct ext4_xattr_entry *entry = BFIRST(block_bh);
892
893 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
894 if (entry->e_value_inum)
895 /* Ref count update on ea_inode. */
896 credits += 1;
897 }
898 return credits;
899}
900
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400901static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
902 int credits, struct buffer_head *bh,
903 bool dirty, bool block_csum)
904{
905 int error;
906
907 if (!ext4_handle_valid(handle))
908 return 0;
909
910 if (handle->h_buffer_credits >= credits)
911 return 0;
912
913 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
914 if (!error)
915 return 0;
916 if (error < 0) {
917 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
918 return error;
919 }
920
921 if (bh && dirty) {
922 if (block_csum)
923 ext4_xattr_block_csum_set(inode, bh);
924 error = ext4_handle_dirty_metadata(handle, NULL, bh);
925 if (error) {
926 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
927 error);
928 return error;
929 }
930 }
931
932 error = ext4_journal_restart(handle, credits);
933 if (error) {
934 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
935 return error;
936 }
937
938 if (bh) {
939 error = ext4_journal_get_write_access(handle, bh);
940 if (error) {
941 ext4_warning(inode->i_sb,
942 "Get write access failed (error %d)",
943 error);
944 return error;
945 }
946 }
947 return 0;
948}
949
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400950static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
951 int ref_change)
952{
953 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
954 struct ext4_iloc iloc;
955 s64 ref_count;
956 u32 hash;
957 int ret;
958
959 inode_lock(ea_inode);
960
961 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
962 if (ret) {
963 iloc.bh = NULL;
964 goto out;
965 }
966
967 ref_count = ext4_xattr_inode_get_ref(ea_inode);
968 ref_count += ref_change;
969 ext4_xattr_inode_set_ref(ea_inode, ref_count);
970
971 if (ref_change > 0) {
972 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
973 ea_inode->i_ino, ref_count);
974
975 if (ref_count == 1) {
976 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
977 ea_inode->i_ino, ea_inode->i_nlink);
978
979 set_nlink(ea_inode, 1);
980 ext4_orphan_del(handle, ea_inode);
981
982 hash = ext4_xattr_inode_get_hash(ea_inode);
983 mb_cache_entry_create(ea_inode_cache, GFP_NOFS, hash,
984 ea_inode->i_ino,
985 true /* reusable */);
986 }
987 } else {
988 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
989 ea_inode->i_ino, ref_count);
990
991 if (ref_count == 0) {
992 WARN_ONCE(ea_inode->i_nlink != 1,
993 "EA inode %lu i_nlink=%u",
994 ea_inode->i_ino, ea_inode->i_nlink);
995
996 clear_nlink(ea_inode);
997 ext4_orphan_add(handle, ea_inode);
998
999 hash = ext4_xattr_inode_get_hash(ea_inode);
1000 mb_cache_entry_delete(ea_inode_cache, hash,
1001 ea_inode->i_ino);
1002 }
1003 }
1004
1005 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1006 iloc.bh = NULL;
1007 if (ret)
1008 ext4_warning_inode(ea_inode,
1009 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1010out:
1011 brelse(iloc.bh);
1012 inode_unlock(ea_inode);
1013 return ret;
1014}
1015
1016static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1017{
1018 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1019}
1020
1021static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1022{
1023 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1024}
1025
1026static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1027 struct ext4_xattr_entry *first)
1028{
1029 struct inode *ea_inode;
1030 struct ext4_xattr_entry *entry;
1031 struct ext4_xattr_entry *failed_entry;
1032 unsigned int ea_ino;
1033 int err, saved_err;
1034
1035 for (entry = first; !IS_LAST_ENTRY(entry);
1036 entry = EXT4_XATTR_NEXT(entry)) {
1037 if (!entry->e_value_inum)
1038 continue;
1039 ea_ino = le32_to_cpu(entry->e_value_inum);
1040 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1041 if (err)
1042 goto cleanup;
1043 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1044 if (err) {
1045 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1046 iput(ea_inode);
1047 goto cleanup;
1048 }
1049 iput(ea_inode);
1050 }
1051 return 0;
1052
1053cleanup:
1054 saved_err = err;
1055 failed_entry = entry;
1056
1057 for (entry = first; entry != failed_entry;
1058 entry = EXT4_XATTR_NEXT(entry)) {
1059 if (!entry->e_value_inum)
1060 continue;
1061 ea_ino = le32_to_cpu(entry->e_value_inum);
1062 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1063 if (err) {
1064 ext4_warning(parent->i_sb,
1065 "cleanup ea_ino %u iget error %d", ea_ino,
1066 err);
1067 continue;
1068 }
1069 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1070 if (err)
1071 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1072 err);
1073 iput(ea_inode);
1074 }
1075 return saved_err;
1076}
1077
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001078static void
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001079ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1080 struct buffer_head *bh,
1081 struct ext4_xattr_entry *first, bool block_csum,
1082 struct ext4_xattr_inode_array **ea_inode_array,
1083 int extra_credits, bool skip_quota)
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001084{
1085 struct inode *ea_inode;
1086 struct ext4_xattr_entry *entry;
1087 bool dirty = false;
1088 unsigned int ea_ino;
1089 int err;
1090 int credits;
1091
1092 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1093 credits = 2 + extra_credits;
1094
1095 for (entry = first; !IS_LAST_ENTRY(entry);
1096 entry = EXT4_XATTR_NEXT(entry)) {
1097 if (!entry->e_value_inum)
1098 continue;
1099 ea_ino = le32_to_cpu(entry->e_value_inum);
1100 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1101 if (err)
1102 continue;
1103
1104 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1105 if (err) {
1106 ext4_warning_inode(ea_inode,
1107 "Expand inode array err=%d", err);
1108 iput(ea_inode);
1109 continue;
1110 }
1111
1112 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1113 dirty, block_csum);
1114 if (err) {
1115 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1116 err);
1117 continue;
1118 }
1119
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001120 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1121 if (err) {
1122 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1123 err);
1124 continue;
1125 }
1126
1127 if (!skip_quota)
1128 ext4_xattr_inode_free_quota(parent,
1129 le32_to_cpu(entry->e_value_size));
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001130
1131 /*
1132 * Forget about ea_inode within the same transaction that
1133 * decrements the ref count. This avoids duplicate decrements in
1134 * case the rest of the work spills over to subsequent
1135 * transactions.
1136 */
1137 entry->e_value_inum = 0;
1138 entry->e_value_size = 0;
1139
1140 dirty = true;
1141 }
1142
1143 if (dirty) {
1144 /*
1145 * Note that we are deliberately skipping csum calculation for
1146 * the final update because we do not expect any journal
1147 * restarts until xattr block is freed.
1148 */
1149
1150 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1151 if (err)
1152 ext4_warning_inode(parent,
1153 "handle dirty metadata err=%d", err);
1154 }
1155}
1156
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001157/*
Jan Karaec4cb1a2014-04-07 10:54:21 -04001158 * Release the xattr block BH: If the reference count is > 1, decrement it;
1159 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 */
1161static void
Mingming Cao617ba132006-10-11 01:20:53 -07001162ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001163 struct buffer_head *bh,
1164 struct ext4_xattr_inode_array **ea_inode_array,
1165 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166{
Tahsin Erdogan47387402017-06-22 11:28:55 -04001167 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001168 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001169 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170
liang xie5d601252014-05-12 22:06:43 -04001171 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001172 error = ext4_journal_get_write_access(handle, bh);
1173 if (error)
1174 goto out;
1175
1176 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001177 hash = le32_to_cpu(BHDR(bh)->h_hash);
1178 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1179 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -05001181 /*
1182 * This must happen under buffer lock for
1183 * ext4_xattr_block_set() to reliably detect freed block
1184 */
Tahsin Erdogan47387402017-06-22 11:28:55 -04001185 mb_cache_entry_delete(ea_block_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001187 unlock_buffer(bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001188
1189 if (ext4_has_feature_ea_inode(inode->i_sb))
1190 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1191 BFIRST(bh),
1192 true /* block_csum */,
1193 ea_inode_array,
1194 extra_credits,
1195 true /* skip_quota */);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001196 ext4_free_blocks(handle, inode, bh, 0, 1,
1197 EXT4_FREE_BLOCKS_METADATA |
1198 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001200 ref--;
1201 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1202 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1203 struct mb_cache_entry *ce;
1204
Tahsin Erdogan47387402017-06-22 11:28:55 -04001205 ce = mb_cache_entry_get(ea_block_cache, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001206 bh->b_blocknr);
1207 if (ce) {
1208 ce->e_reusable = 1;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001209 mb_cache_entry_put(ea_block_cache, ce);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001210 }
1211 }
1212
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001213 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001214 /*
1215 * Beware of this ugliness: Releasing of xattr block references
1216 * from different inodes can race and so we have to protect
1217 * from a race where someone else frees the block (and releases
1218 * its journal_head) before we are done dirtying the buffer. In
1219 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001220 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -04001221 * that function can call sync_dirty_buffer() so for that case
1222 * we handle the dirtying after unlocking the buffer.
1223 */
1224 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001225 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -05001226 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001227 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001228 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001229 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001230 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001231 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001232 ea_bdebug(bh, "refcount now=%d; releasing",
1233 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001235out:
1236 ext4_std_error(inode->i_sb, error);
1237 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238}
1239
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001240/*
1241 * Find the available free space for EAs. This also returns the total number of
1242 * bytes used by EA entries.
1243 */
1244static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1245 size_t *min_offs, void *base, int *total)
1246{
1247 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001248 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001249 size_t offs = le16_to_cpu(last->e_value_offs);
1250 if (offs < *min_offs)
1251 *min_offs = offs;
1252 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001253 if (total)
1254 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001255 }
1256 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1257}
1258
Andreas Dilgere50e5122017-06-21 21:10:32 -04001259/*
1260 * Write the value of the EA in an inode.
1261 */
1262static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1263 const void *buf, int bufsize)
1264{
1265 struct buffer_head *bh = NULL;
1266 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001267 int blocksize = ea_inode->i_sb->s_blocksize;
1268 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001269 int csize, wsize = 0;
1270 int ret = 0;
1271 int retries = 0;
1272
1273retry:
1274 while (ret >= 0 && ret < max_blocks) {
1275 struct ext4_map_blocks map;
1276 map.m_lblk = block += ret;
1277 map.m_len = max_blocks -= ret;
1278
1279 ret = ext4_map_blocks(handle, ea_inode, &map,
1280 EXT4_GET_BLOCKS_CREATE);
1281 if (ret <= 0) {
1282 ext4_mark_inode_dirty(handle, ea_inode);
1283 if (ret == -ENOSPC &&
1284 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1285 ret = 0;
1286 goto retry;
1287 }
1288 break;
1289 }
1290 }
1291
1292 if (ret < 0)
1293 return ret;
1294
1295 block = 0;
1296 while (wsize < bufsize) {
1297 if (bh != NULL)
1298 brelse(bh);
1299 csize = (bufsize - wsize) > blocksize ? blocksize :
1300 bufsize - wsize;
1301 bh = ext4_getblk(handle, ea_inode, block, 0);
1302 if (IS_ERR(bh))
1303 return PTR_ERR(bh);
1304 ret = ext4_journal_get_write_access(handle, bh);
1305 if (ret)
1306 goto out;
1307
1308 memcpy(bh->b_data, buf, csize);
1309 set_buffer_uptodate(bh);
1310 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1311
1312 buf += csize;
1313 wsize += csize;
1314 block += 1;
1315 }
1316
1317 inode_lock(ea_inode);
1318 i_size_write(ea_inode, wsize);
1319 ext4_update_i_disksize(ea_inode, wsize);
1320 inode_unlock(ea_inode);
1321
1322 ext4_mark_inode_dirty(handle, ea_inode);
1323
1324out:
1325 brelse(bh);
1326
1327 return ret;
1328}
1329
1330/*
1331 * Create an inode to store the value of a large EA.
1332 */
1333static struct inode *ext4_xattr_inode_create(handle_t *handle,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001334 struct inode *inode, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001335{
1336 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001337 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001338 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001339
1340 /*
1341 * Let the next inode be the goal, so we try and allocate the EA inode
1342 * in the same group, or nearby one.
1343 */
1344 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001345 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -04001346 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001347 if (!IS_ERR(ea_inode)) {
1348 ea_inode->i_op = &ext4_file_inode_operations;
1349 ea_inode->i_fop = &ext4_file_operations;
1350 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -04001351 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001352 unlock_new_inode(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001353 ext4_xattr_inode_set_ref(ea_inode, 1);
1354 ext4_xattr_inode_set_hash(ea_inode, hash);
1355 err = ext4_mark_inode_dirty(handle, ea_inode);
1356 if (!err)
1357 err = ext4_inode_attach_jinode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001358 if (err) {
1359 iput(ea_inode);
1360 return ERR_PTR(err);
1361 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001362
1363 /*
1364 * Xattr inodes are shared therefore quota charging is performed
1365 * at a higher level.
1366 */
1367 dquot_free_inode(ea_inode);
1368 dquot_drop(ea_inode);
1369 inode_lock(ea_inode);
1370 ea_inode->i_flags |= S_NOQUOTA;
1371 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001372 }
1373
1374 return ea_inode;
1375}
1376
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001377static struct inode *
1378ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1379 size_t value_len, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001380{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001381 struct inode *ea_inode;
1382 struct mb_cache_entry *ce;
1383 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1384 void *ea_data;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001385
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001386 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1387 if (!ce)
1388 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001389
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001390 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1391 if (!ea_data) {
1392 mb_cache_entry_put(ea_inode_cache, ce);
1393 return NULL;
1394 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001395
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001396 while (ce) {
1397 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1398 if (!IS_ERR(ea_inode) &&
1399 !is_bad_inode(ea_inode) &&
1400 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1401 i_size_read(ea_inode) == value_len &&
1402 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1403 !ext4_xattr_inode_verify_hash(ea_inode, ea_data,
1404 value_len) &&
1405 !memcmp(value, ea_data, value_len)) {
1406 mb_cache_entry_touch(ea_inode_cache, ce);
1407 mb_cache_entry_put(ea_inode_cache, ce);
1408 kvfree(ea_data);
1409 return ea_inode;
1410 }
1411
1412 if (!IS_ERR(ea_inode))
1413 iput(ea_inode);
1414 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1415 }
1416 kvfree(ea_data);
1417 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001418}
1419
1420/*
1421 * Add value of the EA in an inode.
1422 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001423static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1424 const void *value, size_t value_len,
1425 struct inode **ret_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001426{
1427 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001428 u32 hash;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001429 int err;
1430
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001431 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1432 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1433 if (ea_inode) {
1434 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1435 if (err) {
1436 iput(ea_inode);
1437 return err;
1438 }
1439
1440 *ret_inode = ea_inode;
1441 return 0;
1442 }
1443
Andreas Dilgere50e5122017-06-21 21:10:32 -04001444 /* Create an inode for the EA value */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001445 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001446 if (IS_ERR(ea_inode))
1447 return PTR_ERR(ea_inode);
1448
1449 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001450 if (err) {
1451 ext4_xattr_inode_dec_ref(handle, ea_inode);
1452 iput(ea_inode);
1453 return err;
1454 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001455
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001456 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1457 ea_inode->i_ino, true /* reusable */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001458
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001459 *ret_inode = ea_inode;
1460 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001461}
1462
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001463/*
1464 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1465 * feature is enabled.
1466 */
1467#define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1468
Andreas Dilgere50e5122017-06-21 21:10:32 -04001469static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1470 struct ext4_xattr_search *s,
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001471 handle_t *handle, struct inode *inode,
1472 bool is_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473{
Mingming Cao617ba132006-10-11 01:20:53 -07001474 struct ext4_xattr_entry *last;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001475 struct ext4_xattr_entry *here = s->here;
1476 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001477 int in_inode = i->in_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001478 struct inode *old_ea_inode = NULL;
1479 struct inode *new_ea_inode = NULL;
1480 size_t old_size, new_size;
1481 int ret;
1482
1483 /* Space used by old and new values. */
1484 old_size = (!s->not_found && !here->e_value_inum) ?
1485 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1486 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1487
1488 /*
1489 * Optimization for the simple case when old and new values have the
1490 * same padded sizes. Not applicable if external inodes are involved.
1491 */
1492 if (new_size && new_size == old_size) {
1493 size_t offs = le16_to_cpu(here->e_value_offs);
1494 void *val = s->base + offs;
1495
1496 here->e_value_size = cpu_to_le32(i->value_len);
1497 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1498 memset(val, 0, new_size);
1499 } else {
1500 memcpy(val, i->value, i->value_len);
1501 /* Clear padding bytes. */
1502 memset(val + i->value_len, 0, new_size - i->value_len);
1503 }
1504 return 0;
1505 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001506
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 /* Compute min_offs and last. */
1508 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -07001509 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001510 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 size_t offs = le16_to_cpu(last->e_value_offs);
1512 if (offs < min_offs)
1513 min_offs = offs;
1514 }
1515 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001516
1517 /* Check whether we have enough space. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001519 size_t free;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001520
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001521 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1522 if (!s->not_found)
1523 free += EXT4_XATTR_LEN(name_len) + old_size;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001524
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001525 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1526 ret = -ENOSPC;
1527 goto out;
1528 }
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001529
1530 /*
1531 * If storing the value in an external inode is an option,
1532 * reserve space for xattr entries/names in the external
1533 * attribute block so that a long value does not occupy the
1534 * whole space and prevent futher entries being added.
1535 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001536 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1537 new_size && is_block &&
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001538 (min_offs + old_size - new_size) <
1539 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1540 ret = -ENOSPC;
1541 goto out;
1542 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543 }
1544
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001545 /*
1546 * Getting access to old and new ea inodes is subject to failures.
1547 * Finish that work before doing any modifications to the xattr data.
1548 */
1549 if (!s->not_found && here->e_value_inum) {
1550 ret = ext4_xattr_inode_iget(inode,
1551 le32_to_cpu(here->e_value_inum),
1552 &old_ea_inode);
1553 if (ret) {
1554 old_ea_inode = NULL;
1555 goto out;
1556 }
1557 }
1558 if (i->value && in_inode) {
1559 WARN_ON_ONCE(!i->value_len);
1560
1561 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1562 if (ret)
1563 goto out;
1564
1565 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1566 i->value_len,
1567 &new_ea_inode);
1568 if (ret) {
1569 new_ea_inode = NULL;
1570 ext4_xattr_inode_free_quota(inode, i->value_len);
1571 goto out;
1572 }
1573 }
1574
1575 if (old_ea_inode) {
1576 /* We are ready to release ref count on the old_ea_inode. */
1577 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1578 if (ret) {
1579 /* Release newly required ref count on new_ea_inode. */
1580 if (new_ea_inode) {
1581 int err;
1582
1583 err = ext4_xattr_inode_dec_ref(handle,
1584 new_ea_inode);
1585 if (err)
1586 ext4_warning_inode(new_ea_inode,
1587 "dec ref new_ea_inode err=%d",
1588 err);
1589 ext4_xattr_inode_free_quota(inode,
1590 i->value_len);
1591 }
1592 goto out;
1593 }
1594
1595 ext4_xattr_inode_free_quota(inode,
1596 le32_to_cpu(here->e_value_size));
1597 }
1598
1599 /* No failures allowed past this point. */
1600
1601 if (!s->not_found && here->e_value_offs) {
1602 /* Remove the old value. */
1603 void *first_val = s->base + min_offs;
1604 size_t offs = le16_to_cpu(here->e_value_offs);
1605 void *val = s->base + offs;
1606
1607 memmove(first_val + old_size, first_val, val - first_val);
1608 memset(first_val, 0, old_size);
1609 min_offs += old_size;
1610
1611 /* Adjust all value offsets. */
1612 last = s->first;
1613 while (!IS_LAST_ENTRY(last)) {
1614 size_t o = le16_to_cpu(last->e_value_offs);
1615
1616 if (!last->e_value_inum &&
1617 last->e_value_size && o < offs)
1618 last->e_value_offs = cpu_to_le16(o + old_size);
1619 last = EXT4_XATTR_NEXT(last);
1620 }
1621 }
1622
1623 if (!i->value) {
1624 /* Remove old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001625 size_t size = EXT4_XATTR_LEN(name_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001626
1627 last = ENTRY((void *)last - size);
1628 memmove(here, (void *)here + size,
1629 (void *)last - (void *)here + sizeof(__u32));
1630 memset(last, 0, size);
1631 } else if (s->not_found) {
1632 /* Insert new name. */
1633 size_t size = EXT4_XATTR_LEN(name_len);
1634 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1635
1636 memmove((void *)here + size, here, rest);
1637 memset(here, 0, size);
1638 here->e_name_index = i->name_index;
1639 here->e_name_len = name_len;
1640 memcpy(here->e_name, i->name, name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641 } else {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001642 /* This is an update, reset value info. */
1643 here->e_value_inum = 0;
1644 here->e_value_offs = 0;
1645 here->e_value_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001646 }
1647
1648 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001649 /* Insert new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001650 if (in_inode) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001651 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001652 } else if (i->value_len) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001653 void *val = s->base + min_offs - new_size;
1654
1655 here->e_value_offs = cpu_to_le16(min_offs - new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001656 if (i->value == EXT4_ZERO_XATTR_VALUE) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001657 memset(val, 0, new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001658 } else {
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001659 memcpy(val, i->value, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001660 /* Clear padding bytes. */
1661 memset(val + i->value_len, 0,
1662 new_size - i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001663 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001665 here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666 }
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001667
1668 if (is_block) {
1669 if (i->value)
1670 ext4_xattr_hash_entry(here, s->base);
1671 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1672 }
1673
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001674 ret = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001675out:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001676 iput(old_ea_inode);
1677 iput(new_ea_inode);
1678 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001679}
1680
Mingming Cao617ba132006-10-11 01:20:53 -07001681struct ext4_xattr_block_find {
1682 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683 struct buffer_head *bh;
1684};
1685
1686static int
Mingming Cao617ba132006-10-11 01:20:53 -07001687ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1688 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689{
1690 struct super_block *sb = inode->i_sb;
1691 int error;
1692
1693 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1694 i->name_index, i->name, i->value, (long)i->value_len);
1695
Mingming Cao617ba132006-10-11 01:20:53 -07001696 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001698 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001699 error = -EIO;
1700 if (!bs->bh)
1701 goto cleanup;
1702 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1703 atomic_read(&(bs->bh->b_count)),
1704 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001705 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001706 EXT4_ERROR_INODE(inode, "bad block %llu",
1707 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001708 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 goto cleanup;
1710 }
1711 /* Find the named attribute. */
1712 bs->s.base = BHDR(bs->bh);
1713 bs->s.first = BFIRST(bs->bh);
1714 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1715 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001716 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001717 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001718 if (error && error != -ENODATA)
1719 goto cleanup;
1720 bs->s.not_found = error;
1721 }
1722 error = 0;
1723
1724cleanup:
1725 return error;
1726}
1727
1728static int
Mingming Cao617ba132006-10-11 01:20:53 -07001729ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1730 struct ext4_xattr_info *i,
1731 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001732{
1733 struct super_block *sb = inode->i_sb;
1734 struct buffer_head *new_bh = NULL;
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04001735 struct ext4_xattr_search s_copy = bs->s;
1736 struct ext4_xattr_search *s = &s_copy;
Jan Kara7a2508e2016-02-22 22:35:22 -05001737 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001738 int error = 0;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001739 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001740 struct inode *ea_inode = NULL;
1741 size_t old_ea_inode_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001742
Mingming Cao617ba132006-10-11 01:20:53 -07001743#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001746 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001747 error = ext4_journal_get_write_access(handle, bs->bh);
1748 if (error)
1749 goto cleanup;
1750 lock_buffer(bs->bh);
1751
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001752 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001753 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1754
1755 /*
1756 * This must happen under buffer lock for
1757 * ext4_xattr_block_set() to reliably detect modified
1758 * block
1759 */
Tahsin Erdogan47387402017-06-22 11:28:55 -04001760 mb_cache_entry_delete(ea_block_cache, hash,
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04001761 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762 ea_bdebug(bs->bh, "modifying in-place");
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001763 error = ext4_xattr_set_entry(i, s, handle, inode,
1764 true /* is_block */);
1765 if (!error)
Tahsin Erdogan47387402017-06-22 11:28:55 -04001766 ext4_xattr_block_cache_insert(ea_block_cache,
1767 bs->bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001768 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001770 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 goto bad_block;
1772 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001773 error = ext4_handle_dirty_metadata(handle,
1774 inode,
1775 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001776 if (error)
1777 goto cleanup;
1778 goto inserted;
1779 } else {
1780 int offset = (char *)s->here - bs->bh->b_data;
1781
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001782 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001784 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001785 error = -ENOMEM;
1786 if (s->base == NULL)
1787 goto cleanup;
1788 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1789 s->first = ENTRY(header(s->base)+1);
1790 header(s->base)->h_refcount = cpu_to_le32(1);
1791 s->here = ENTRY(s->base + offset);
1792 s->end = s->base + bs->bh->b_size;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001793
1794 /*
1795 * If existing entry points to an xattr inode, we need
1796 * to prevent ext4_xattr_set_entry() from decrementing
1797 * ref count on it because the reference belongs to the
1798 * original block. In this case, make the entry look
1799 * like it has an empty value.
1800 */
1801 if (!s->not_found && s->here->e_value_inum) {
1802 /*
1803 * Defer quota free call for previous inode
1804 * until success is guaranteed.
1805 */
1806 old_ea_inode_size = le32_to_cpu(
1807 s->here->e_value_size);
1808 s->here->e_value_inum = 0;
1809 s->here->e_value_size = 0;
1810 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001811 }
1812 } else {
1813 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001814 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001815 /* assert(header == s->base) */
1816 error = -ENOMEM;
1817 if (s->base == NULL)
1818 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001819 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001820 header(s->base)->h_blocks = cpu_to_le32(1);
1821 header(s->base)->h_refcount = cpu_to_le32(1);
1822 s->first = ENTRY(header(s->base)+1);
1823 s->here = ENTRY(header(s->base)+1);
1824 s->end = s->base + sb->s_blocksize;
1825 }
1826
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001827 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001828 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001829 goto bad_block;
1830 if (error)
1831 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001832
1833 if (i->value && s->here->e_value_inum) {
1834 unsigned int ea_ino;
1835
1836 /*
1837 * A ref count on ea_inode has been taken as part of the call to
1838 * ext4_xattr_set_entry() above. We would like to drop this
1839 * extra ref but we have to wait until the xattr block is
1840 * initialized and has its own ref count on the ea_inode.
1841 */
1842 ea_ino = le32_to_cpu(s->here->e_value_inum);
1843 error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
1844 if (error) {
1845 ea_inode = NULL;
1846 goto cleanup;
1847 }
1848 }
1849
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001850inserted:
1851 if (!IS_LAST_ENTRY(s->first)) {
Tahsin Erdogan47387402017-06-22 11:28:55 -04001852 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1853 &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 if (new_bh) {
1855 /* We found an identical block in the cache. */
1856 if (new_bh == bs->bh)
1857 ea_bdebug(new_bh, "keeping");
1858 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001859 u32 ref;
1860
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001861 WARN_ON_ONCE(dquot_initialize_needed(inode));
1862
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863 /* The old block is released after updating
1864 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001865 error = dquot_alloc_block(inode,
1866 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001867 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001868 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001869 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001870 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001871 new_bh);
1872 if (error)
1873 goto cleanup_dquot;
1874 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001875 /*
1876 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001877 * freeing, rehashing or adding references to
1878 * xattr block. Once we hold buffer lock xattr
1879 * block's state is stable so we can check
1880 * whether the block got freed / rehashed or
1881 * not. Since we unhash mbcache entry under
1882 * buffer lock when freeing / rehashing xattr
1883 * block, checking whether entry is still
1884 * hashed is reliable. Same rules hold for
1885 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001886 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001887 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1888 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001889 /*
1890 * Undo everything and check mbcache
1891 * again.
1892 */
1893 unlock_buffer(new_bh);
1894 dquot_free_block(inode,
1895 EXT4_C2B(EXT4_SB(sb),
1896 1));
1897 brelse(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04001898 mb_cache_entry_put(ea_block_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05001899 ce = NULL;
1900 new_bh = NULL;
1901 goto inserted;
1902 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001903 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1904 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1905 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1906 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001907 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001908 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001909 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001910 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001911 error = ext4_handle_dirty_metadata(handle,
1912 inode,
1913 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001914 if (error)
1915 goto cleanup_dquot;
1916 }
Tahsin Erdogan47387402017-06-22 11:28:55 -04001917 mb_cache_entry_touch(ea_block_cache, ce);
1918 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001919 ce = NULL;
1920 } else if (bs->bh && s->base == bs->bh->b_data) {
1921 /* We were modifying this block in-place. */
1922 ea_bdebug(bs->bh, "keeping this block");
1923 new_bh = bs->bh;
1924 get_bh(new_bh);
1925 } else {
1926 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001927 ext4_fsblk_t goal, block;
1928
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001929 WARN_ON_ONCE(dquot_initialize_needed(inode));
1930
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001931 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04001932 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001933
1934 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001935 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001936 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1937
Allison Henderson55f020d2011-05-25 07:41:26 -04001938 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1939 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001940 if (error)
1941 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001942
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001943 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001944 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1945
Joe Perchesace36ad2012-03-19 23:11:43 -04001946 ea_idebug(inode, "creating block %llu",
1947 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001948
1949 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001950 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001951 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001953 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001954 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001955 goto cleanup;
1956 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001957 error = ext4_xattr_inode_inc_ref_all(handle, inode,
1958 ENTRY(header(s->base)+1));
1959 if (error)
1960 goto getblk_failed;
1961 if (ea_inode) {
1962 /* Drop the extra ref on ea_inode. */
1963 error = ext4_xattr_inode_dec_ref(handle,
1964 ea_inode);
1965 if (error)
1966 ext4_warning_inode(ea_inode,
1967 "dec ref error=%d",
1968 error);
1969 iput(ea_inode);
1970 ea_inode = NULL;
1971 }
1972
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001973 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001974 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001975 if (error) {
1976 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001977 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001978 goto getblk_failed;
1979 }
1980 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001981 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001982 set_buffer_uptodate(new_bh);
1983 unlock_buffer(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04001984 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001985 error = ext4_handle_dirty_metadata(handle, inode,
1986 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001987 if (error)
1988 goto cleanup;
1989 }
1990 }
1991
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001992 if (old_ea_inode_size)
1993 ext4_xattr_inode_free_quota(inode, old_ea_inode_size);
1994
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001995 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001996 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001997
1998 /* Drop the previous xattr block. */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001999 if (bs->bh && bs->bh != new_bh) {
2000 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2001
2002 ext4_xattr_release_block(handle, inode, bs->bh,
2003 &ea_inode_array,
2004 0 /* extra_credits */);
2005 ext4_xattr_inode_array_free(ea_inode_array);
2006 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007 error = 0;
2008
2009cleanup:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002010 if (ea_inode) {
2011 int error2;
2012
2013 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2014 if (error2)
2015 ext4_warning_inode(ea_inode, "dec ref error=%d",
2016 error2);
2017
2018 /* If there was an error, revert the quota charge. */
2019 if (error)
2020 ext4_xattr_inode_free_quota(inode,
2021 i_size_read(ea_inode));
2022 iput(ea_inode);
2023 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002024 if (ce)
Tahsin Erdogan47387402017-06-22 11:28:55 -04002025 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002026 brelse(new_bh);
2027 if (!(bs->bh && s->base == bs->bh->b_data))
2028 kfree(s->base);
2029
2030 return error;
2031
2032cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05002033 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 goto cleanup;
2035
2036bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04002037 EXT4_ERROR_INODE(inode, "bad block %llu",
2038 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002039 goto cleanup;
2040
2041#undef header
2042}
2043
Tao Ma879b3822012-12-05 10:28:46 -05002044int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2045 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002046{
Mingming Cao617ba132006-10-11 01:20:53 -07002047 struct ext4_xattr_ibody_header *header;
2048 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002049 int error;
2050
Mingming Cao617ba132006-10-11 01:20:53 -07002051 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002053 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054 header = IHDR(inode, raw_inode);
2055 is->s.base = is->s.first = IFIRST(header);
2056 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07002057 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002058 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002059 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002060 if (error)
2061 return error;
2062 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07002063 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04002064 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002065 if (error && error != -ENODATA)
2066 return error;
2067 is->s.not_found = error;
2068 }
2069 return 0;
2070}
2071
Tao Ma0d812f72012-12-10 14:06:02 -05002072int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2073 struct ext4_xattr_info *i,
2074 struct ext4_xattr_ibody_find *is)
2075{
2076 struct ext4_xattr_ibody_header *header;
2077 struct ext4_xattr_search *s = &is->s;
2078 int error;
2079
2080 if (EXT4_I(inode)->i_extra_isize == 0)
2081 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002082 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002083 if (error) {
2084 if (error == -ENOSPC &&
2085 ext4_has_inline_data(inode)) {
2086 error = ext4_try_to_evict_inline_data(handle, inode,
2087 EXT4_XATTR_LEN(strlen(i->name) +
2088 EXT4_XATTR_SIZE(i->value_len)));
2089 if (error)
2090 return error;
2091 error = ext4_xattr_ibody_find(inode, i, is);
2092 if (error)
2093 return error;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002094 error = ext4_xattr_set_entry(i, s, handle, inode,
2095 false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002096 }
2097 if (error)
2098 return error;
2099 }
2100 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2101 if (!IS_LAST_ENTRY(s->first)) {
2102 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2103 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2104 } else {
2105 header->h_magic = cpu_to_le32(0);
2106 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2107 }
2108 return 0;
2109}
2110
Andreas Dilgere50e5122017-06-21 21:10:32 -04002111static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05002112 struct ext4_xattr_info *i,
2113 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002114{
Mingming Cao617ba132006-10-11 01:20:53 -07002115 struct ext4_xattr_ibody_header *header;
2116 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002117 int error;
2118
Mingming Cao617ba132006-10-11 01:20:53 -07002119 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002120 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002121 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122 if (error)
2123 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07002124 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002126 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002127 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002128 } else {
2129 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002130 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002131 }
2132 return 0;
2133}
2134
Jan Kara3fd16462016-02-22 22:43:04 -05002135static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2136 struct ext4_xattr_info *i)
2137{
2138 void *value;
2139
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04002140 /* When e_value_inum is set the value is stored externally. */
2141 if (s->here->e_value_inum)
2142 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05002143 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2144 return 0;
2145 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2146 return !memcmp(value, i->value, i->value_len);
2147}
2148
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002149static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2150{
2151 struct buffer_head *bh;
2152 int error;
2153
2154 if (!EXT4_I(inode)->i_file_acl)
2155 return NULL;
2156 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2157 if (!bh)
2158 return ERR_PTR(-EIO);
2159 error = ext4_xattr_check_block(inode, bh);
2160 if (error)
2161 return ERR_PTR(error);
2162 return bh;
2163}
2164
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002165/*
Mingming Cao617ba132006-10-11 01:20:53 -07002166 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002167 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05002168 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 * is NULL to remove an existing extended attribute, and non-NULL to
2170 * either replace an existing extended attribute, or create a new extended
2171 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2172 * specify that an extended attribute must exist and must not exist
2173 * previous to the call, respectively.
2174 *
2175 * Returns 0, or a negative error number on failure.
2176 */
2177int
Mingming Cao617ba132006-10-11 01:20:53 -07002178ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002179 const char *name, const void *value, size_t value_len,
2180 int flags)
2181{
Mingming Cao617ba132006-10-11 01:20:53 -07002182 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002183 .name_index = name_index,
2184 .name = name,
2185 .value = value,
2186 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04002187 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002188 };
Mingming Cao617ba132006-10-11 01:20:53 -07002189 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002190 .s = { .not_found = -ENODATA, },
2191 };
Mingming Cao617ba132006-10-11 01:20:53 -07002192 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 .s = { .not_found = -ENODATA, },
2194 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05002195 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002196 int error;
2197
2198 if (!name)
2199 return -EINVAL;
2200 if (strlen(name) > 255)
2201 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002202
Theodore Ts'oc755e252017-01-11 21:50:46 -05002203 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04002204
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002205 /* Check journal credits under write lock. */
2206 if (ext4_handle_valid(handle)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002207 struct buffer_head *bh;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002208 int credits;
2209
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002210 bh = ext4_xattr_get_block(inode);
2211 if (IS_ERR(bh)) {
2212 error = PTR_ERR(bh);
2213 goto cleanup;
2214 }
2215
2216 credits = __ext4_xattr_set_credits(inode, bh, value_len);
2217 brelse(bh);
2218
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002219 if (!ext4_handle_has_enough_credits(handle, credits)) {
2220 error = -ENOSPC;
2221 goto cleanup;
2222 }
2223 }
2224
Eric Sandeen66543612011-10-26 03:32:07 -04002225 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05002226 if (error)
2227 goto cleanup;
2228
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002229 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002230 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2231 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002232 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233 }
2234
Mingming Cao617ba132006-10-11 01:20:53 -07002235 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002236 if (error)
2237 goto cleanup;
2238 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002239 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002240 if (error)
2241 goto cleanup;
2242 if (is.s.not_found && bs.s.not_found) {
2243 error = -ENODATA;
2244 if (flags & XATTR_REPLACE)
2245 goto cleanup;
2246 error = 0;
2247 if (!value)
2248 goto cleanup;
2249 } else {
2250 error = -EEXIST;
2251 if (flags & XATTR_CREATE)
2252 goto cleanup;
2253 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002254
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002255 if (!value) {
2256 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002257 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002259 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05002261 error = 0;
2262 /* Xattr value did not change? Save us some work and bail out */
2263 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2264 goto cleanup;
2265 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2266 goto cleanup;
2267
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002268 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2269 (EXT4_XATTR_SIZE(i.value_len) >
2270 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2271 i.in_inode = 1;
2272retry_inode:
Andreas Dilgere50e5122017-06-21 21:10:32 -04002273 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002274 if (!error && !bs.s.not_found) {
2275 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002276 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002277 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07002278 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2279 error = ext4_xattr_block_find(inode, &i, &bs);
2280 if (error)
2281 goto cleanup;
2282 }
Mingming Cao617ba132006-10-11 01:20:53 -07002283 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002284 if (!error && !is.s.not_found) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002286 error = ext4_xattr_ibody_set(handle, inode, &i,
2287 &is);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002288 } else if (error == -ENOSPC) {
2289 /*
2290 * Xattr does not fit in the block, store at
2291 * external inode if possible.
2292 */
2293 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2294 !i.in_inode) {
2295 i.in_inode = 1;
2296 goto retry_inode;
2297 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298 }
2299 }
2300 }
2301 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07002302 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002303 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002304 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05002305 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002306 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002307 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002308 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 * error != 0.
2310 */
2311 is.iloc.bh = NULL;
2312 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002313 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002314 }
2315
2316cleanup:
2317 brelse(is.iloc.bh);
2318 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002319 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002320 return error;
2321}
2322
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002323int ext4_xattr_set_credits(struct inode *inode, size_t value_len, int *credits)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002324{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002325 struct buffer_head *bh;
2326 int err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002327
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002328 *credits = 0;
2329
2330 if (!EXT4_SB(inode->i_sb)->s_journal)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002331 return 0;
2332
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002333 down_read(&EXT4_I(inode)->xattr_sem);
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002334
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002335 bh = ext4_xattr_get_block(inode);
2336 if (IS_ERR(bh)) {
2337 err = PTR_ERR(bh);
2338 } else {
2339 *credits = __ext4_xattr_set_credits(inode, bh, value_len);
2340 brelse(bh);
2341 err = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002342 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002343
2344 up_read(&EXT4_I(inode)->xattr_sem);
2345 return err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002346}
2347
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002348/*
Mingming Cao617ba132006-10-11 01:20:53 -07002349 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002350 *
Mingming Cao617ba132006-10-11 01:20:53 -07002351 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002352 * attribute modification is a filesystem transaction by itself.
2353 *
2354 * Returns 0, or a negative error number on failure.
2355 */
2356int
Mingming Cao617ba132006-10-11 01:20:53 -07002357ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002358 const void *value, size_t value_len, int flags)
2359{
2360 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002361 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002362 int error, retries = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002363 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002365 error = dquot_initialize(inode);
2366 if (error)
2367 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002368
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002369retry:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002370 error = ext4_xattr_set_credits(inode, value_len, &credits);
2371 if (error)
2372 return error;
2373
Theodore Ts'o9924a922013-02-08 21:59:22 -05002374 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002375 if (IS_ERR(handle)) {
2376 error = PTR_ERR(handle);
2377 } else {
2378 int error2;
2379
Mingming Cao617ba132006-10-11 01:20:53 -07002380 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002381 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07002382 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04002384 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002385 goto retry;
2386 if (error == 0)
2387 error = error2;
2388 }
2389
2390 return error;
2391}
2392
2393/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002394 * Shift the EA entries in the inode to create space for the increased
2395 * i_extra_isize.
2396 */
2397static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2398 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04002399 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002400{
2401 struct ext4_xattr_entry *last = entry;
2402 int new_offs;
2403
Jan Kara94405712016-08-29 15:41:11 -04002404 /* We always shift xattr headers further thus offsets get lower */
2405 BUG_ON(value_offs_shift > 0);
2406
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002407 /* Adjust the value offsets of the entries */
2408 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002409 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002410 new_offs = le16_to_cpu(last->e_value_offs) +
2411 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002412 last->e_value_offs = cpu_to_le16(new_offs);
2413 }
2414 }
2415 /* Shift the entries by n bytes */
2416 memmove(to, from, n);
2417}
2418
2419/*
Jan Kara3f2571c2016-08-29 15:42:11 -04002420 * Move xattr pointed to by 'entry' from inode into external xattr block
2421 */
2422static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2423 struct ext4_inode *raw_inode,
2424 struct ext4_xattr_entry *entry)
2425{
2426 struct ext4_xattr_ibody_find *is = NULL;
2427 struct ext4_xattr_block_find *bs = NULL;
2428 char *buffer = NULL, *b_entry_name = NULL;
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002429 size_t value_size = le32_to_cpu(entry->e_value_size);
Jan Kara3f2571c2016-08-29 15:42:11 -04002430 struct ext4_xattr_info i = {
2431 .value = NULL,
2432 .value_len = 0,
2433 .name_index = entry->e_name_index,
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002434 .in_inode = !!entry->e_value_inum,
Jan Kara3f2571c2016-08-29 15:42:11 -04002435 };
2436 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2437 int error;
2438
Jan Kara3f2571c2016-08-29 15:42:11 -04002439 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2440 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2441 buffer = kmalloc(value_size, GFP_NOFS);
2442 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2443 if (!is || !bs || !buffer || !b_entry_name) {
2444 error = -ENOMEM;
2445 goto out;
2446 }
2447
2448 is->s.not_found = -ENODATA;
2449 bs->s.not_found = -ENODATA;
2450 is->iloc.bh = NULL;
2451 bs->bh = NULL;
2452
2453 /* Save the entry name and the entry value */
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002454 if (entry->e_value_inum) {
2455 error = ext4_xattr_inode_get(inode,
2456 le32_to_cpu(entry->e_value_inum),
2457 buffer, value_size);
2458 if (error)
2459 goto out;
2460 } else {
2461 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2462 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2463 }
2464
Jan Kara3f2571c2016-08-29 15:42:11 -04002465 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2466 b_entry_name[entry->e_name_len] = '\0';
2467 i.name = b_entry_name;
2468
2469 error = ext4_get_inode_loc(inode, &is->iloc);
2470 if (error)
2471 goto out;
2472
2473 error = ext4_xattr_ibody_find(inode, &i, is);
2474 if (error)
2475 goto out;
2476
2477 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04002478 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04002479 if (error)
2480 goto out;
2481
Jan Kara3f2571c2016-08-29 15:42:11 -04002482 i.value = buffer;
2483 i.value_len = value_size;
2484 error = ext4_xattr_block_find(inode, &i, bs);
2485 if (error)
2486 goto out;
2487
2488 /* Add entry which was removed from the inode into the block */
2489 error = ext4_xattr_block_set(handle, inode, &i, bs);
2490 if (error)
2491 goto out;
2492 error = 0;
2493out:
2494 kfree(b_entry_name);
2495 kfree(buffer);
2496 if (is)
2497 brelse(is->iloc.bh);
2498 kfree(is);
2499 kfree(bs);
2500
2501 return error;
2502}
2503
Jan Karadfa20642016-08-29 15:44:11 -04002504static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2505 struct ext4_inode *raw_inode,
2506 int isize_diff, size_t ifree,
2507 size_t bfree, int *total_ino)
2508{
2509 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2510 struct ext4_xattr_entry *small_entry;
2511 struct ext4_xattr_entry *entry;
2512 struct ext4_xattr_entry *last;
2513 unsigned int entry_size; /* EA entry size */
2514 unsigned int total_size; /* EA entry size + value size */
2515 unsigned int min_total_size;
2516 int error;
2517
2518 while (isize_diff > ifree) {
2519 entry = NULL;
2520 small_entry = NULL;
2521 min_total_size = ~0U;
2522 last = IFIRST(header);
2523 /* Find the entry best suited to be pushed into EA block */
2524 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002525 total_size = EXT4_XATTR_LEN(last->e_name_len);
2526 if (!last->e_value_inum)
2527 total_size += EXT4_XATTR_SIZE(
2528 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002529 if (total_size <= bfree &&
2530 total_size < min_total_size) {
2531 if (total_size + ifree < isize_diff) {
2532 small_entry = last;
2533 } else {
2534 entry = last;
2535 min_total_size = total_size;
2536 }
2537 }
2538 }
2539
2540 if (entry == NULL) {
2541 if (small_entry == NULL)
2542 return -ENOSPC;
2543 entry = small_entry;
2544 }
2545
2546 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002547 total_size = entry_size;
2548 if (!entry->e_value_inum)
2549 total_size += EXT4_XATTR_SIZE(
2550 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002551 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2552 entry);
2553 if (error)
2554 return error;
2555
2556 *total_ino -= entry_size;
2557 ifree += total_size;
2558 bfree -= total_size;
2559 }
2560
2561 return 0;
2562}
2563
Jan Kara3f2571c2016-08-29 15:42:11 -04002564/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002565 * Expand an inode by new_extra_isize bytes when EAs are present.
2566 * Returns 0 on success or negative error number on failure.
2567 */
2568int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2569 struct ext4_inode *raw_inode, handle_t *handle)
2570{
2571 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002572 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04002573 size_t min_offs;
2574 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05002575 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04002576 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04002577 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04002578 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 -04002579 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002580 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002581
Theodore Ts'oc755e252017-01-11 21:50:46 -05002582 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
2583 return 0;
2584
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002585retry:
Jan Karad0141192016-08-11 11:50:30 -04002586 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04002587 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2588 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002589
2590 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002591
2592 /*
2593 * Check if enough free space is available in the inode to shift the
2594 * entries ahead by new_extra_isize.
2595 */
2596
Jan Kara6e0cd082016-08-29 15:43:11 -04002597 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002598 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2599 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002600 total_ino = sizeof(struct ext4_xattr_ibody_header);
2601
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002602 error = xattr_check_inode(inode, header, end);
2603 if (error)
2604 goto cleanup;
2605
Jan Kara6e0cd082016-08-29 15:43:11 -04002606 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002607 if (ifree >= isize_diff)
2608 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002609
2610 /*
2611 * Enough free space isn't available in the inode, check if
2612 * EA block can hold new_extra_isize bytes.
2613 */
2614 if (EXT4_I(inode)->i_file_acl) {
2615 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2616 error = -EIO;
2617 if (!bh)
2618 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04002619 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002620 EXT4_ERROR_INODE(inode, "bad block %llu",
2621 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002622 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002623 goto cleanup;
2624 }
2625 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002626 end = bh->b_data + bh->b_size;
2627 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04002628 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2629 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04002630 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002631 if (!tried_min_extra_isize && s_min_extra_isize) {
2632 tried_min_extra_isize++;
2633 new_extra_isize = s_min_extra_isize;
2634 brelse(bh);
2635 goto retry;
2636 }
Jan Karadfa20642016-08-29 15:44:11 -04002637 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002638 goto cleanup;
2639 }
2640 } else {
Jan Karae3014d12016-08-29 15:38:11 -04002641 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002642 }
2643
Jan Karadfa20642016-08-29 15:44:11 -04002644 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2645 isize_diff, ifree, bfree,
2646 &total_ino);
2647 if (error) {
2648 if (error == -ENOSPC && !tried_min_extra_isize &&
2649 s_min_extra_isize) {
2650 tried_min_extra_isize++;
2651 new_extra_isize = s_min_extra_isize;
2652 brelse(bh);
2653 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002654 }
Jan Karadfa20642016-08-29 15:44:11 -04002655 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002656 }
Jan Karae3014d12016-08-29 15:38:11 -04002657shift:
2658 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04002659 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04002660 - new_extra_isize, (void *)raw_inode +
2661 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04002662 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002663 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002664 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04002665out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05002666 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002667 return 0;
2668
2669cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002670 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04002671 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05002672 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04002673 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002674 no_expand = 1;
2675 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002676 return error;
2677}
2678
Andreas Dilgere50e5122017-06-21 21:10:32 -04002679#define EIA_INCR 16 /* must be 2^n */
2680#define EIA_MASK (EIA_INCR - 1)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002681
2682/* Add the large xattr @inode into @ea_inode_array for deferred iput().
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002683 * If @ea_inode_array is new or full it will be grown and the old
Andreas Dilgere50e5122017-06-21 21:10:32 -04002684 * contents copied over.
2685 */
2686static int
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002687ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2688 struct inode *inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002689{
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002690 if (*ea_inode_array == NULL) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002691 /*
2692 * Start with 15 inodes, so it fits into a power-of-two size.
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002693 * If *ea_inode_array is NULL, this is essentially offsetof()
Andreas Dilgere50e5122017-06-21 21:10:32 -04002694 */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002695 (*ea_inode_array) =
2696 kmalloc(offsetof(struct ext4_xattr_inode_array,
2697 inodes[EIA_MASK]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002698 GFP_NOFS);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002699 if (*ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002700 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002701 (*ea_inode_array)->count = 0;
2702 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002703 /* expand the array once all 15 + n * 16 slots are full */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002704 struct ext4_xattr_inode_array *new_array = NULL;
2705 int count = (*ea_inode_array)->count;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002706
2707 /* if new_array is NULL, this is essentially offsetof() */
2708 new_array = kmalloc(
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002709 offsetof(struct ext4_xattr_inode_array,
2710 inodes[count + EIA_INCR]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002711 GFP_NOFS);
2712 if (new_array == NULL)
2713 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002714 memcpy(new_array, *ea_inode_array,
2715 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2716 kfree(*ea_inode_array);
2717 *ea_inode_array = new_array;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002718 }
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002719 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002720 return 0;
2721}
2722
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002723/*
Mingming Cao617ba132006-10-11 01:20:53 -07002724 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002725 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04002726 * Free extended attribute resources associated with this inode. Traverse
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002727 * all entries and decrement reference on any xattr inodes associated with this
2728 * inode. This is called immediately before an inode is freed. We have exclusive
2729 * access to the inode. If an orphan inode is deleted it will also release its
2730 * references on xattr block and xattr inodes.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002731 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002732int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2733 struct ext4_xattr_inode_array **ea_inode_array,
2734 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002735{
2736 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002737 struct ext4_xattr_ibody_header *header;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002738 struct ext4_iloc iloc = { .bh = NULL };
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002739 struct ext4_xattr_entry *entry;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002740 int error;
2741
2742 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2743 NULL /* bh */,
2744 false /* dirty */,
2745 false /* block_csum */);
2746 if (error) {
2747 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2748 goto cleanup;
2749 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002750
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002751 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2752 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002753
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002754 error = ext4_get_inode_loc(inode, &iloc);
Tahsin Erdogan65d30002017-06-21 22:24:38 -04002755 if (error) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002756 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002757 goto cleanup;
2758 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002759
2760 error = ext4_journal_get_write_access(handle, iloc.bh);
2761 if (error) {
2762 EXT4_ERROR_INODE(inode, "write access (error %d)",
2763 error);
2764 goto cleanup;
2765 }
2766
2767 header = IHDR(inode, ext4_raw_inode(&iloc));
2768 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2769 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2770 IFIRST(header),
2771 false /* block_csum */,
2772 ea_inode_array,
2773 extra_credits,
2774 false /* skip_quota */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002775 }
2776
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002777 if (EXT4_I(inode)->i_file_acl) {
2778 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2779 if (!bh) {
2780 EXT4_ERROR_INODE(inode, "block %llu read error",
2781 EXT4_I(inode)->i_file_acl);
2782 error = -EIO;
2783 goto cleanup;
2784 }
2785 error = ext4_xattr_check_block(inode, bh);
2786 if (error) {
2787 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2788 EXT4_I(inode)->i_file_acl, error);
2789 goto cleanup;
2790 }
2791
2792 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2793 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2794 entry = EXT4_XATTR_NEXT(entry))
2795 if (entry->e_value_inum)
2796 ext4_xattr_inode_free_quota(inode,
2797 le32_to_cpu(entry->e_value_size));
2798
2799 }
2800
2801 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2802 extra_credits);
2803 /*
2804 * Update i_file_acl value in the same transaction that releases
2805 * block.
2806 */
2807 EXT4_I(inode)->i_file_acl = 0;
2808 error = ext4_mark_inode_dirty(handle, inode);
2809 if (error) {
2810 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2811 error);
2812 goto cleanup;
2813 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002814 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002815 error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002816cleanup:
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002817 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002818 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002819 return error;
2820}
2821
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002822void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002823{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002824 int idx;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002825
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002826 if (ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002827 return;
2828
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002829 for (idx = 0; idx < ea_inode_array->count; ++idx)
2830 iput(ea_inode_array->inodes[idx]);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002831 kfree(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002832}
2833
2834/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002835 * ext4_xattr_block_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002836 *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002837 * Create a new entry in the extended attribute block cache, and insert
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002838 * it unless such an entry is already in the cache.
2839 *
2840 * Returns 0, or a negative error number on failure.
2841 */
2842static void
Tahsin Erdogan47387402017-06-22 11:28:55 -04002843ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2844 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002845{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002846 struct ext4_xattr_header *header = BHDR(bh);
2847 __u32 hash = le32_to_cpu(header->h_hash);
2848 int reusable = le32_to_cpu(header->h_refcount) <
2849 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850 int error;
2851
Tahsin Erdogan47387402017-06-22 11:28:55 -04002852 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002853 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002854 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002855 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002856 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002857 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002858 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002859}
2860
2861/*
Mingming Cao617ba132006-10-11 01:20:53 -07002862 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002863 *
2864 * Compare two extended attribute blocks for equality.
2865 *
2866 * Returns 0 if the blocks are equal, 1 if they differ, and
2867 * a negative error number on errors.
2868 */
2869static int
Mingming Cao617ba132006-10-11 01:20:53 -07002870ext4_xattr_cmp(struct ext4_xattr_header *header1,
2871 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002872{
Mingming Cao617ba132006-10-11 01:20:53 -07002873 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002874
2875 entry1 = ENTRY(header1+1);
2876 entry2 = ENTRY(header2+1);
2877 while (!IS_LAST_ENTRY(entry1)) {
2878 if (IS_LAST_ENTRY(entry2))
2879 return 1;
2880 if (entry1->e_hash != entry2->e_hash ||
2881 entry1->e_name_index != entry2->e_name_index ||
2882 entry1->e_name_len != entry2->e_name_len ||
2883 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002884 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002885 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2886 return 1;
Tahsin Erdogan7cec1912017-06-21 22:14:30 -04002887 if (!entry1->e_value_inum &&
2888 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002889 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2890 le32_to_cpu(entry1->e_value_size)))
2891 return 1;
2892
Mingming Cao617ba132006-10-11 01:20:53 -07002893 entry1 = EXT4_XATTR_NEXT(entry1);
2894 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002895 }
2896 if (!IS_LAST_ENTRY(entry2))
2897 return 1;
2898 return 0;
2899}
2900
2901/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002902 * ext4_xattr_block_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002903 *
2904 * Find an identical extended attribute block.
2905 *
2906 * Returns a pointer to the block found, or NULL if such a block was
2907 * not found or an error occurred.
2908 */
2909static struct buffer_head *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002910ext4_xattr_block_cache_find(struct inode *inode,
2911 struct ext4_xattr_header *header,
2912 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002913{
2914 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002915 struct mb_cache_entry *ce;
Tahsin Erdogan47387402017-06-22 11:28:55 -04002916 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002917
2918 if (!header->h_hash)
2919 return NULL; /* never share */
2920 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002921 ce = mb_cache_entry_find_first(ea_block_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002922 while (ce) {
2923 struct buffer_head *bh;
2924
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04002925 bh = sb_bread(inode->i_sb, ce->e_value);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002926 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002927 EXT4_ERROR_INODE(inode, "block %lu read error",
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04002928 (unsigned long)ce->e_value);
Mingming Cao617ba132006-10-11 01:20:53 -07002929 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002930 *pce = ce;
2931 return bh;
2932 }
2933 brelse(bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002934 ce = mb_cache_entry_find_next(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002935 }
2936 return NULL;
2937}
2938
2939#define NAME_HASH_SHIFT 5
2940#define VALUE_HASH_SHIFT 16
2941
2942/*
Mingming Cao617ba132006-10-11 01:20:53 -07002943 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944 *
2945 * Compute the hash of an extended attribute.
2946 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002947static void ext4_xattr_hash_entry(struct ext4_xattr_entry *entry,
2948 void *value_base)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949{
2950 __u32 hash = 0;
2951 char *name = entry->e_name;
2952 int n;
2953
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002954 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002955 hash = (hash << NAME_HASH_SHIFT) ^
2956 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2957 *name++;
2958 }
2959
Andreas Dilgere50e5122017-06-21 21:10:32 -04002960 if (!entry->e_value_inum && entry->e_value_size) {
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002961 __le32 *value = (__le32 *)((char *)value_base +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002962 le16_to_cpu(entry->e_value_offs));
2963 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07002964 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002965 hash = (hash << VALUE_HASH_SHIFT) ^
2966 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2967 le32_to_cpu(*value++);
2968 }
2969 }
2970 entry->e_hash = cpu_to_le32(hash);
2971}
2972
2973#undef NAME_HASH_SHIFT
2974#undef VALUE_HASH_SHIFT
2975
2976#define BLOCK_HASH_SHIFT 16
2977
2978/*
Mingming Cao617ba132006-10-11 01:20:53 -07002979 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002980 *
2981 * Re-compute the extended attribute hash value after an entry has changed.
2982 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002983static void ext4_xattr_rehash(struct ext4_xattr_header *header)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984{
Mingming Cao617ba132006-10-11 01:20:53 -07002985 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002986 __u32 hash = 0;
2987
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002988 here = ENTRY(header+1);
2989 while (!IS_LAST_ENTRY(here)) {
2990 if (!here->e_hash) {
2991 /* Block is not shared if an entry's hash value == 0 */
2992 hash = 0;
2993 break;
2994 }
2995 hash = (hash << BLOCK_HASH_SHIFT) ^
2996 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2997 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07002998 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002999 }
3000 header->h_hash = cpu_to_le32(hash);
3001}
3002
3003#undef BLOCK_HASH_SHIFT
3004
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003005#define HASH_BUCKET_BITS 10
3006
Jan Kara7a2508e2016-02-22 22:35:22 -05003007struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05003008ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003009{
Jan Kara7a2508e2016-02-22 22:35:22 -05003010 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003011}
3012
Jan Kara7a2508e2016-02-22 22:35:22 -05003013void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003014{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003015 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05003016 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003018