xref: /openbmc/linux/fs/ntfs3/xattr.c (revision 75c5e0c9e5cad34deeec7c131c2b1368df5a8c75)
1be71b5cbSKonstantin Komarov // SPDX-License-Identifier: GPL-2.0
2be71b5cbSKonstantin Komarov /*
3be71b5cbSKonstantin Komarov  *
4be71b5cbSKonstantin Komarov  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5be71b5cbSKonstantin Komarov  *
6be71b5cbSKonstantin Komarov  */
7be71b5cbSKonstantin Komarov 
8be71b5cbSKonstantin Komarov #include <linux/fs.h>
9be71b5cbSKonstantin Komarov #include <linux/posix_acl.h>
10be71b5cbSKonstantin Komarov #include <linux/posix_acl_xattr.h>
11be71b5cbSKonstantin Komarov #include <linux/xattr.h>
12be71b5cbSKonstantin Komarov 
13be71b5cbSKonstantin Komarov #include "debug.h"
14be71b5cbSKonstantin Komarov #include "ntfs.h"
15be71b5cbSKonstantin Komarov #include "ntfs_fs.h"
16be71b5cbSKonstantin Komarov 
17be71b5cbSKonstantin Komarov // clang-format off
18be71b5cbSKonstantin Komarov #define SYSTEM_DOS_ATTRIB     "system.dos_attrib"
19be71b5cbSKonstantin Komarov #define SYSTEM_NTFS_ATTRIB    "system.ntfs_attrib"
200d19f3d7SDaniel Pinto #define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be"
21be71b5cbSKonstantin Komarov #define SYSTEM_NTFS_SECURITY  "system.ntfs_security"
22be71b5cbSKonstantin Komarov // clang-format on
23be71b5cbSKonstantin Komarov 
24be71b5cbSKonstantin Komarov static inline size_t unpacked_ea_size(const struct EA_FULL *ea)
25be71b5cbSKonstantin Komarov {
26be71b5cbSKonstantin Komarov 	return ea->size ? le32_to_cpu(ea->size)
27d3624466SKonstantin Komarov 			: ALIGN(struct_size(ea, name,
28d3624466SKonstantin Komarov 					    1 + ea->name_len +
29d3624466SKonstantin Komarov 						    le16_to_cpu(ea->elength)),
30d3624466SKonstantin Komarov 				4);
31be71b5cbSKonstantin Komarov }
32be71b5cbSKonstantin Komarov 
33be71b5cbSKonstantin Komarov static inline size_t packed_ea_size(const struct EA_FULL *ea)
34be71b5cbSKonstantin Komarov {
35be71b5cbSKonstantin Komarov 	return struct_size(ea, name,
36be71b5cbSKonstantin Komarov 			   1 + ea->name_len + le16_to_cpu(ea->elength)) -
37be71b5cbSKonstantin Komarov 	       offsetof(struct EA_FULL, flags);
38be71b5cbSKonstantin Komarov }
39be71b5cbSKonstantin Komarov 
40be71b5cbSKonstantin Komarov /*
41be71b5cbSKonstantin Komarov  * find_ea
42be71b5cbSKonstantin Komarov  *
43e8b8e97fSKari Argillander  * Assume there is at least one xattr in the list.
44be71b5cbSKonstantin Komarov  */
45be71b5cbSKonstantin Komarov static inline bool find_ea(const struct EA_FULL *ea_all, u32 bytes,
460e8235d2SKonstantin Komarov 			   const char *name, u8 name_len, u32 *off, u32 *ea_sz)
47be71b5cbSKonstantin Komarov {
480e8235d2SKonstantin Komarov 	u32 ea_size;
490e8235d2SKonstantin Komarov 
50be71b5cbSKonstantin Komarov 	*off = 0;
510e8235d2SKonstantin Komarov 	if (!ea_all)
52be71b5cbSKonstantin Komarov 		return false;
53be71b5cbSKonstantin Komarov 
540e8235d2SKonstantin Komarov 	for (; *off < bytes; *off += ea_size) {
55be71b5cbSKonstantin Komarov 		const struct EA_FULL *ea = Add2Ptr(ea_all, *off);
560e8235d2SKonstantin Komarov 		ea_size = unpacked_ea_size(ea);
57be71b5cbSKonstantin Komarov 		if (ea->name_len == name_len &&
580e8235d2SKonstantin Komarov 		    !memcmp(ea->name, name, name_len)) {
590e8235d2SKonstantin Komarov 			if (ea_sz)
600e8235d2SKonstantin Komarov 				*ea_sz = ea_size;
61be71b5cbSKonstantin Komarov 			return true;
62be71b5cbSKonstantin Komarov 		}
63be71b5cbSKonstantin Komarov 	}
64be71b5cbSKonstantin Komarov 
650e8235d2SKonstantin Komarov 	return false;
660e8235d2SKonstantin Komarov }
670e8235d2SKonstantin Komarov 
68be71b5cbSKonstantin Komarov /*
69e8b8e97fSKari Argillander  * ntfs_read_ea - Read all extended attributes.
70e8b8e97fSKari Argillander  * @ea:		New allocated memory.
71e8b8e97fSKari Argillander  * @info:	Pointer into resident data.
72be71b5cbSKonstantin Komarov  */
73be71b5cbSKonstantin Komarov static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea,
74be71b5cbSKonstantin Komarov 			size_t add_bytes, const struct EA_INFO **info)
75be71b5cbSKonstantin Komarov {
760e8235d2SKonstantin Komarov 	int err = -EINVAL;
77cff32466SKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
78be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le = NULL;
79be71b5cbSKonstantin Komarov 	struct ATTRIB *attr_info, *attr_ea;
80be71b5cbSKonstantin Komarov 	void *ea_p;
810e8235d2SKonstantin Komarov 	u32 size, off, ea_size;
82be71b5cbSKonstantin Komarov 
83be71b5cbSKonstantin Komarov 	static_assert(le32_to_cpu(ATTR_EA_INFO) < le32_to_cpu(ATTR_EA));
84be71b5cbSKonstantin Komarov 
85be71b5cbSKonstantin Komarov 	*ea = NULL;
86be71b5cbSKonstantin Komarov 	*info = NULL;
87be71b5cbSKonstantin Komarov 
88be71b5cbSKonstantin Komarov 	attr_info =
89be71b5cbSKonstantin Komarov 		ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, NULL);
90be71b5cbSKonstantin Komarov 	attr_ea =
91be71b5cbSKonstantin Komarov 		ni_find_attr(ni, attr_info, &le, ATTR_EA, NULL, 0, NULL, NULL);
92be71b5cbSKonstantin Komarov 
93be71b5cbSKonstantin Komarov 	if (!attr_ea || !attr_info)
94be71b5cbSKonstantin Komarov 		return 0;
95be71b5cbSKonstantin Komarov 
96be71b5cbSKonstantin Komarov 	*info = resident_data_ex(attr_info, sizeof(struct EA_INFO));
97be71b5cbSKonstantin Komarov 	if (!*info)
980e8235d2SKonstantin Komarov 		goto out;
99be71b5cbSKonstantin Komarov 
100e8b8e97fSKari Argillander 	/* Check Ea limit. */
101be71b5cbSKonstantin Komarov 	size = le32_to_cpu((*info)->size);
1020e8235d2SKonstantin Komarov 	if (size > sbi->ea_max_size) {
1030e8235d2SKonstantin Komarov 		err = -EFBIG;
1040e8235d2SKonstantin Komarov 		goto out;
1050e8235d2SKonstantin Komarov 	}
106be71b5cbSKonstantin Komarov 
1070e8235d2SKonstantin Komarov 	if (attr_size(attr_ea) > sbi->ea_max_size) {
1080e8235d2SKonstantin Komarov 		err = -EFBIG;
1090e8235d2SKonstantin Komarov 		goto out;
1100e8235d2SKonstantin Komarov 	}
111be71b5cbSKonstantin Komarov 
112cff32466SKonstantin Komarov 	if (!size) {
11387e21c99SKonstantin Komarov 		/* EA info persists, but xattr is empty. Looks like EA problem. */
1140e8235d2SKonstantin Komarov 		goto out;
1150e8235d2SKonstantin Komarov 	}
116be71b5cbSKonstantin Komarov 
117be71b5cbSKonstantin Komarov 	/* Allocate memory for packed Ea. */
118e001e608SDan Carpenter 	ea_p = kmalloc(size_add(size, add_bytes), GFP_NOFS);
119be71b5cbSKonstantin Komarov 	if (!ea_p)
120be71b5cbSKonstantin Komarov 		return -ENOMEM;
121be71b5cbSKonstantin Komarov 
1220e8235d2SKonstantin Komarov 	if (attr_ea->non_res) {
123be71b5cbSKonstantin Komarov 		struct runs_tree run;
124be71b5cbSKonstantin Komarov 
125be71b5cbSKonstantin Komarov 		run_init(&run);
126be71b5cbSKonstantin Komarov 
12742f86b12SKonstantin Komarov 		err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &run, 0, size);
128be71b5cbSKonstantin Komarov 		if (!err)
129cff32466SKonstantin Komarov 			err = ntfs_read_run_nb(sbi, &run, 0, ea_p, size, NULL);
130be71b5cbSKonstantin Komarov 		run_close(&run);
131be71b5cbSKonstantin Komarov 
132be71b5cbSKonstantin Komarov 		if (err)
1330e8235d2SKonstantin Komarov 			goto out1;
134be71b5cbSKonstantin Komarov 	} else {
135be71b5cbSKonstantin Komarov 		void *p = resident_data_ex(attr_ea, size);
136be71b5cbSKonstantin Komarov 
1370e8235d2SKonstantin Komarov 		if (!p)
1380e8235d2SKonstantin Komarov 			goto out1;
139be71b5cbSKonstantin Komarov 		memcpy(ea_p, p, size);
140be71b5cbSKonstantin Komarov 	}
141be71b5cbSKonstantin Komarov 
142be71b5cbSKonstantin Komarov 	memset(Add2Ptr(ea_p, size), 0, add_bytes);
1430e8235d2SKonstantin Komarov 
1440e8235d2SKonstantin Komarov 	/* Check all attributes for consistency. */
1450e8235d2SKonstantin Komarov 	for (off = 0; off < size; off += ea_size) {
1460e8235d2SKonstantin Komarov 		const struct EA_FULL *ef = Add2Ptr(ea_p, off);
1470e8235d2SKonstantin Komarov 		u32 bytes = size - off;
1480e8235d2SKonstantin Komarov 
1490e8235d2SKonstantin Komarov 		/* Check if we can use field ea->size. */
1500e8235d2SKonstantin Komarov 		if (bytes < sizeof(ef->size))
1510e8235d2SKonstantin Komarov 			goto out1;
1520e8235d2SKonstantin Komarov 
1530e8235d2SKonstantin Komarov 		if (ef->size) {
1540e8235d2SKonstantin Komarov 			ea_size = le32_to_cpu(ef->size);
1550e8235d2SKonstantin Komarov 			if (ea_size > bytes)
1560e8235d2SKonstantin Komarov 				goto out1;
1570e8235d2SKonstantin Komarov 			continue;
1580e8235d2SKonstantin Komarov 		}
1590e8235d2SKonstantin Komarov 
1600e8235d2SKonstantin Komarov 		/* Check if we can use fields ef->name_len and ef->elength. */
1610e8235d2SKonstantin Komarov 		if (bytes < offsetof(struct EA_FULL, name))
1620e8235d2SKonstantin Komarov 			goto out1;
1630e8235d2SKonstantin Komarov 
1640e8235d2SKonstantin Komarov 		ea_size = ALIGN(struct_size(ef, name,
1650e8235d2SKonstantin Komarov 					    1 + ef->name_len +
1660e8235d2SKonstantin Komarov 						    le16_to_cpu(ef->elength)),
1670e8235d2SKonstantin Komarov 				4);
1680e8235d2SKonstantin Komarov 		if (ea_size > bytes)
1690e8235d2SKonstantin Komarov 			goto out1;
1700e8235d2SKonstantin Komarov 	}
1710e8235d2SKonstantin Komarov 
172be71b5cbSKonstantin Komarov 	*ea = ea_p;
173be71b5cbSKonstantin Komarov 	return 0;
174be71b5cbSKonstantin Komarov 
1750e8235d2SKonstantin Komarov out1:
176195c52bdSKari Argillander 	kfree(ea_p);
1770e8235d2SKonstantin Komarov out:
1780e8235d2SKonstantin Komarov 	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
179be71b5cbSKonstantin Komarov 	return err;
180be71b5cbSKonstantin Komarov }
181be71b5cbSKonstantin Komarov 
182be71b5cbSKonstantin Komarov /*
183be71b5cbSKonstantin Komarov  * ntfs_list_ea
184be71b5cbSKonstantin Komarov  *
185e8b8e97fSKari Argillander  * Copy a list of xattrs names into the buffer
186e8b8e97fSKari Argillander  * provided, or compute the buffer size required.
187be71b5cbSKonstantin Komarov  *
188e8b8e97fSKari Argillander  * Return:
189e8b8e97fSKari Argillander  * * Number of bytes used / required on
190e8b8e97fSKari Argillander  * * -ERRNO - on failure
191be71b5cbSKonstantin Komarov  */
192be71b5cbSKonstantin Komarov static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer,
193be71b5cbSKonstantin Komarov 			    size_t bytes_per_buffer)
194be71b5cbSKonstantin Komarov {
195be71b5cbSKonstantin Komarov 	const struct EA_INFO *info;
196be71b5cbSKonstantin Komarov 	struct EA_FULL *ea_all = NULL;
197be71b5cbSKonstantin Komarov 	const struct EA_FULL *ea;
198be71b5cbSKonstantin Komarov 	u32 off, size;
199be71b5cbSKonstantin Komarov 	int err;
2000e8235d2SKonstantin Komarov 	int ea_size;
201be71b5cbSKonstantin Komarov 	size_t ret;
202be71b5cbSKonstantin Komarov 
203be71b5cbSKonstantin Komarov 	err = ntfs_read_ea(ni, &ea_all, 0, &info);
204be71b5cbSKonstantin Komarov 	if (err)
205be71b5cbSKonstantin Komarov 		return err;
206be71b5cbSKonstantin Komarov 
207be71b5cbSKonstantin Komarov 	if (!info || !ea_all)
208be71b5cbSKonstantin Komarov 		return 0;
209be71b5cbSKonstantin Komarov 
210be71b5cbSKonstantin Komarov 	size = le32_to_cpu(info->size);
211be71b5cbSKonstantin Komarov 
212e8b8e97fSKari Argillander 	/* Enumerate all xattrs. */
2130e8235d2SKonstantin Komarov 	for (ret = 0, off = 0; off < size; off += ea_size) {
214be71b5cbSKonstantin Komarov 		ea = Add2Ptr(ea_all, off);
2150e8235d2SKonstantin Komarov 		ea_size = unpacked_ea_size(ea);
216be71b5cbSKonstantin Komarov 
217be71b5cbSKonstantin Komarov 		if (buffer) {
218be71b5cbSKonstantin Komarov 			if (ret + ea->name_len + 1 > bytes_per_buffer) {
219be71b5cbSKonstantin Komarov 				err = -ERANGE;
220be71b5cbSKonstantin Komarov 				goto out;
221be71b5cbSKonstantin Komarov 			}
222be71b5cbSKonstantin Komarov 
223be71b5cbSKonstantin Komarov 			memcpy(buffer + ret, ea->name, ea->name_len);
224be71b5cbSKonstantin Komarov 			buffer[ret + ea->name_len] = 0;
225be71b5cbSKonstantin Komarov 		}
226be71b5cbSKonstantin Komarov 
227be71b5cbSKonstantin Komarov 		ret += ea->name_len + 1;
228be71b5cbSKonstantin Komarov 	}
229be71b5cbSKonstantin Komarov 
230be71b5cbSKonstantin Komarov out:
231195c52bdSKari Argillander 	kfree(ea_all);
232be71b5cbSKonstantin Komarov 	return err ? err : ret;
233be71b5cbSKonstantin Komarov }
234be71b5cbSKonstantin Komarov 
235be71b5cbSKonstantin Komarov static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
236be71b5cbSKonstantin Komarov 		       void *buffer, size_t size, size_t *required)
237be71b5cbSKonstantin Komarov {
238be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
239be71b5cbSKonstantin Komarov 	const struct EA_INFO *info;
240be71b5cbSKonstantin Komarov 	struct EA_FULL *ea_all = NULL;
241be71b5cbSKonstantin Komarov 	const struct EA_FULL *ea;
242be71b5cbSKonstantin Komarov 	u32 off, len;
243be71b5cbSKonstantin Komarov 	int err;
244be71b5cbSKonstantin Komarov 
245be71b5cbSKonstantin Komarov 	if (!(ni->ni_flags & NI_FLAG_EA))
246be71b5cbSKonstantin Komarov 		return -ENODATA;
247be71b5cbSKonstantin Komarov 
248be71b5cbSKonstantin Komarov 	if (!required)
249be71b5cbSKonstantin Komarov 		ni_lock(ni);
250be71b5cbSKonstantin Komarov 
251be71b5cbSKonstantin Komarov 	len = 0;
252be71b5cbSKonstantin Komarov 
253be71b5cbSKonstantin Komarov 	if (name_len > 255) {
254be71b5cbSKonstantin Komarov 		err = -ENAMETOOLONG;
255be71b5cbSKonstantin Komarov 		goto out;
256be71b5cbSKonstantin Komarov 	}
257be71b5cbSKonstantin Komarov 
258be71b5cbSKonstantin Komarov 	err = ntfs_read_ea(ni, &ea_all, 0, &info);
259be71b5cbSKonstantin Komarov 	if (err)
260be71b5cbSKonstantin Komarov 		goto out;
261be71b5cbSKonstantin Komarov 
262be71b5cbSKonstantin Komarov 	if (!info)
263be71b5cbSKonstantin Komarov 		goto out;
264be71b5cbSKonstantin Komarov 
265e8b8e97fSKari Argillander 	/* Enumerate all xattrs. */
2660e8235d2SKonstantin Komarov 	if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off,
2670e8235d2SKonstantin Komarov 		     NULL)) {
268be71b5cbSKonstantin Komarov 		err = -ENODATA;
269be71b5cbSKonstantin Komarov 		goto out;
270be71b5cbSKonstantin Komarov 	}
271be71b5cbSKonstantin Komarov 	ea = Add2Ptr(ea_all, off);
272be71b5cbSKonstantin Komarov 
273be71b5cbSKonstantin Komarov 	len = le16_to_cpu(ea->elength);
274be71b5cbSKonstantin Komarov 	if (!buffer) {
275be71b5cbSKonstantin Komarov 		err = 0;
276be71b5cbSKonstantin Komarov 		goto out;
277be71b5cbSKonstantin Komarov 	}
278be71b5cbSKonstantin Komarov 
279be71b5cbSKonstantin Komarov 	if (len > size) {
280be71b5cbSKonstantin Komarov 		err = -ERANGE;
281be71b5cbSKonstantin Komarov 		if (required)
282be71b5cbSKonstantin Komarov 			*required = len;
283be71b5cbSKonstantin Komarov 		goto out;
284be71b5cbSKonstantin Komarov 	}
285be71b5cbSKonstantin Komarov 
286be71b5cbSKonstantin Komarov 	memcpy(buffer, ea->name + ea->name_len + 1, len);
287be71b5cbSKonstantin Komarov 	err = 0;
288be71b5cbSKonstantin Komarov 
289be71b5cbSKonstantin Komarov out:
290195c52bdSKari Argillander 	kfree(ea_all);
291be71b5cbSKonstantin Komarov 	if (!required)
292be71b5cbSKonstantin Komarov 		ni_unlock(ni);
293be71b5cbSKonstantin Komarov 
294be71b5cbSKonstantin Komarov 	return err ? err : len;
295be71b5cbSKonstantin Komarov }
296be71b5cbSKonstantin Komarov 
297be71b5cbSKonstantin Komarov static noinline int ntfs_set_ea(struct inode *inode, const char *name,
298be71b5cbSKonstantin Komarov 				size_t name_len, const void *value,
2991842fbc8SKonstantin Komarov 				size_t val_size, int flags, bool locked,
3001842fbc8SKonstantin Komarov 				__le16 *ea_size)
301be71b5cbSKonstantin Komarov {
302be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
303be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
304be71b5cbSKonstantin Komarov 	int err;
305be71b5cbSKonstantin Komarov 	struct EA_INFO ea_info;
306be71b5cbSKonstantin Komarov 	const struct EA_INFO *info;
307be71b5cbSKonstantin Komarov 	struct EA_FULL *new_ea;
308be71b5cbSKonstantin Komarov 	struct EA_FULL *ea_all = NULL;
309be71b5cbSKonstantin Komarov 	size_t add, new_pack;
3100e8235d2SKonstantin Komarov 	u32 off, size, ea_sz;
311be71b5cbSKonstantin Komarov 	__le16 size_pack;
312be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
313be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le;
314be71b5cbSKonstantin Komarov 	struct mft_inode *mi;
315be71b5cbSKonstantin Komarov 	struct runs_tree ea_run;
316be71b5cbSKonstantin Komarov 	u64 new_sz;
317be71b5cbSKonstantin Komarov 	void *p;
318be71b5cbSKonstantin Komarov 
3193a2154b2SKonstantin Komarov 	if (!locked)
320be71b5cbSKonstantin Komarov 		ni_lock(ni);
321be71b5cbSKonstantin Komarov 
322be71b5cbSKonstantin Komarov 	run_init(&ea_run);
323be71b5cbSKonstantin Komarov 
324be71b5cbSKonstantin Komarov 	if (name_len > 255) {
325be71b5cbSKonstantin Komarov 		err = -ENAMETOOLONG;
326be71b5cbSKonstantin Komarov 		goto out;
327be71b5cbSKonstantin Komarov 	}
328be71b5cbSKonstantin Komarov 
329fa3cacf5SKari Argillander 	add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
330be71b5cbSKonstantin Komarov 
331be71b5cbSKonstantin Komarov 	err = ntfs_read_ea(ni, &ea_all, add, &info);
332be71b5cbSKonstantin Komarov 	if (err)
333be71b5cbSKonstantin Komarov 		goto out;
334be71b5cbSKonstantin Komarov 
335be71b5cbSKonstantin Komarov 	if (!info) {
336be71b5cbSKonstantin Komarov 		memset(&ea_info, 0, sizeof(ea_info));
337be71b5cbSKonstantin Komarov 		size = 0;
338be71b5cbSKonstantin Komarov 		size_pack = 0;
339be71b5cbSKonstantin Komarov 	} else {
340be71b5cbSKonstantin Komarov 		memcpy(&ea_info, info, sizeof(ea_info));
341be71b5cbSKonstantin Komarov 		size = le32_to_cpu(ea_info.size);
342be71b5cbSKonstantin Komarov 		size_pack = ea_info.size_pack;
343be71b5cbSKonstantin Komarov 	}
344be71b5cbSKonstantin Komarov 
3450e8235d2SKonstantin Komarov 	if (info && find_ea(ea_all, size, name, name_len, &off, &ea_sz)) {
346be71b5cbSKonstantin Komarov 		struct EA_FULL *ea;
347be71b5cbSKonstantin Komarov 
348be71b5cbSKonstantin Komarov 		if (flags & XATTR_CREATE) {
349be71b5cbSKonstantin Komarov 			err = -EEXIST;
350be71b5cbSKonstantin Komarov 			goto out;
351be71b5cbSKonstantin Komarov 		}
352be71b5cbSKonstantin Komarov 
353be71b5cbSKonstantin Komarov 		ea = Add2Ptr(ea_all, off);
354be71b5cbSKonstantin Komarov 
355be71b5cbSKonstantin Komarov 		/*
356be71b5cbSKonstantin Komarov 		 * Check simple case when we try to insert xattr with the same value
357be71b5cbSKonstantin Komarov 		 * e.g. ntfs_save_wsl_perm
358be71b5cbSKonstantin Komarov 		 */
359be71b5cbSKonstantin Komarov 		if (val_size && le16_to_cpu(ea->elength) == val_size &&
360be71b5cbSKonstantin Komarov 		    !memcmp(ea->name + ea->name_len + 1, value, val_size)) {
361e8b8e97fSKari Argillander 			/* xattr already contains the required value. */
362be71b5cbSKonstantin Komarov 			goto out;
363be71b5cbSKonstantin Komarov 		}
364be71b5cbSKonstantin Komarov 
365e8b8e97fSKari Argillander 		/* Remove current xattr. */
366be71b5cbSKonstantin Komarov 		if (ea->flags & FILE_NEED_EA)
367be71b5cbSKonstantin Komarov 			le16_add_cpu(&ea_info.count, -1);
368be71b5cbSKonstantin Komarov 
369be71b5cbSKonstantin Komarov 		le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea));
370be71b5cbSKonstantin Komarov 
371be71b5cbSKonstantin Komarov 		memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz);
372be71b5cbSKonstantin Komarov 
373be71b5cbSKonstantin Komarov 		size -= ea_sz;
374be71b5cbSKonstantin Komarov 		memset(Add2Ptr(ea_all, size), 0, ea_sz);
375be71b5cbSKonstantin Komarov 
376be71b5cbSKonstantin Komarov 		ea_info.size = cpu_to_le32(size);
377be71b5cbSKonstantin Komarov 
378be71b5cbSKonstantin Komarov 		if ((flags & XATTR_REPLACE) && !val_size) {
379e8b8e97fSKari Argillander 			/* Remove xattr. */
380be71b5cbSKonstantin Komarov 			goto update_ea;
381be71b5cbSKonstantin Komarov 		}
382be71b5cbSKonstantin Komarov 	} else {
383be71b5cbSKonstantin Komarov 		if (flags & XATTR_REPLACE) {
384be71b5cbSKonstantin Komarov 			err = -ENODATA;
385be71b5cbSKonstantin Komarov 			goto out;
386be71b5cbSKonstantin Komarov 		}
387be71b5cbSKonstantin Komarov 
388be71b5cbSKonstantin Komarov 		if (!ea_all) {
389195c52bdSKari Argillander 			ea_all = kzalloc(add, GFP_NOFS);
390be71b5cbSKonstantin Komarov 			if (!ea_all) {
391be71b5cbSKonstantin Komarov 				err = -ENOMEM;
392be71b5cbSKonstantin Komarov 				goto out;
393be71b5cbSKonstantin Komarov 			}
394be71b5cbSKonstantin Komarov 		}
395be71b5cbSKonstantin Komarov 	}
396be71b5cbSKonstantin Komarov 
397e8b8e97fSKari Argillander 	/* Append new xattr. */
398be71b5cbSKonstantin Komarov 	new_ea = Add2Ptr(ea_all, size);
399be71b5cbSKonstantin Komarov 	new_ea->size = cpu_to_le32(add);
400be71b5cbSKonstantin Komarov 	new_ea->flags = 0;
401be71b5cbSKonstantin Komarov 	new_ea->name_len = name_len;
402be71b5cbSKonstantin Komarov 	new_ea->elength = cpu_to_le16(val_size);
403be71b5cbSKonstantin Komarov 	memcpy(new_ea->name, name, name_len);
404be71b5cbSKonstantin Komarov 	new_ea->name[name_len] = 0;
405be71b5cbSKonstantin Komarov 	memcpy(new_ea->name + name_len + 1, value, val_size);
406be71b5cbSKonstantin Komarov 	new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea);
407be71b5cbSKonstantin Komarov 	ea_info.size_pack = cpu_to_le16(new_pack);
408e8b8e97fSKari Argillander 	/* New size of ATTR_EA. */
409be71b5cbSKonstantin Komarov 	size += add;
410cff32466SKonstantin Komarov 	ea_info.size = cpu_to_le32(size);
411cff32466SKonstantin Komarov 
412cff32466SKonstantin Komarov 	/*
413cff32466SKonstantin Komarov 	 * 1. Check ea_info.size_pack for overflow.
414e479f0a6SYu Zhe 	 * 2. New attribute size must fit value from $AttrDef
415cff32466SKonstantin Komarov 	 */
416cff32466SKonstantin Komarov 	if (new_pack > 0xffff || size > sbi->ea_max_size) {
417cff32466SKonstantin Komarov 		ntfs_inode_warn(
418cff32466SKonstantin Komarov 			inode,
419cff32466SKonstantin Komarov 			"The size of extended attributes must not exceed 64KiB");
420be71b5cbSKonstantin Komarov 		err = -EFBIG; // -EINVAL?
421be71b5cbSKonstantin Komarov 		goto out;
422be71b5cbSKonstantin Komarov 	}
423be71b5cbSKonstantin Komarov 
424be71b5cbSKonstantin Komarov update_ea:
425be71b5cbSKonstantin Komarov 
426be71b5cbSKonstantin Komarov 	if (!info) {
427e8b8e97fSKari Argillander 		/* Create xattr. */
428be71b5cbSKonstantin Komarov 		if (!size) {
429be71b5cbSKonstantin Komarov 			err = 0;
430be71b5cbSKonstantin Komarov 			goto out;
431be71b5cbSKonstantin Komarov 		}
432be71b5cbSKonstantin Komarov 
433be71b5cbSKonstantin Komarov 		err = ni_insert_resident(ni, sizeof(struct EA_INFO),
43478ab59feSKonstantin Komarov 					 ATTR_EA_INFO, NULL, 0, NULL, NULL,
43578ab59feSKonstantin Komarov 					 NULL);
436be71b5cbSKonstantin Komarov 		if (err)
437be71b5cbSKonstantin Komarov 			goto out;
438be71b5cbSKonstantin Komarov 
43978ab59feSKonstantin Komarov 		err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL,
44078ab59feSKonstantin Komarov 					 NULL);
441be71b5cbSKonstantin Komarov 		if (err)
442be71b5cbSKonstantin Komarov 			goto out;
443be71b5cbSKonstantin Komarov 	}
444be71b5cbSKonstantin Komarov 
445be71b5cbSKonstantin Komarov 	new_sz = size;
446be71b5cbSKonstantin Komarov 	err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz,
447be71b5cbSKonstantin Komarov 			    false, NULL);
448be71b5cbSKonstantin Komarov 	if (err)
449be71b5cbSKonstantin Komarov 		goto out;
450be71b5cbSKonstantin Komarov 
451be71b5cbSKonstantin Komarov 	le = NULL;
452be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi);
453be71b5cbSKonstantin Komarov 	if (!attr) {
454be71b5cbSKonstantin Komarov 		err = -EINVAL;
455be71b5cbSKonstantin Komarov 		goto out;
456be71b5cbSKonstantin Komarov 	}
457be71b5cbSKonstantin Komarov 
458be71b5cbSKonstantin Komarov 	if (!size) {
459e8b8e97fSKari Argillander 		/* Delete xattr, ATTR_EA_INFO */
46078ab59feSKonstantin Komarov 		ni_remove_attr_le(ni, attr, mi, le);
461be71b5cbSKonstantin Komarov 	} else {
462be71b5cbSKonstantin Komarov 		p = resident_data_ex(attr, sizeof(struct EA_INFO));
463be71b5cbSKonstantin Komarov 		if (!p) {
464be71b5cbSKonstantin Komarov 			err = -EINVAL;
465be71b5cbSKonstantin Komarov 			goto out;
466be71b5cbSKonstantin Komarov 		}
467be71b5cbSKonstantin Komarov 		memcpy(p, &ea_info, sizeof(struct EA_INFO));
468be71b5cbSKonstantin Komarov 		mi->dirty = true;
469be71b5cbSKonstantin Komarov 	}
470be71b5cbSKonstantin Komarov 
471be71b5cbSKonstantin Komarov 	le = NULL;
472be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi);
473be71b5cbSKonstantin Komarov 	if (!attr) {
474be71b5cbSKonstantin Komarov 		err = -EINVAL;
475be71b5cbSKonstantin Komarov 		goto out;
476be71b5cbSKonstantin Komarov 	}
477be71b5cbSKonstantin Komarov 
478be71b5cbSKonstantin Komarov 	if (!size) {
479e8b8e97fSKari Argillander 		/* Delete xattr, ATTR_EA */
48078ab59feSKonstantin Komarov 		ni_remove_attr_le(ni, attr, mi, le);
481be71b5cbSKonstantin Komarov 	} else if (attr->non_res) {
48242f86b12SKonstantin Komarov 		err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &ea_run, 0,
48342f86b12SKonstantin Komarov 					   size);
48442f86b12SKonstantin Komarov 		if (err)
48542f86b12SKonstantin Komarov 			goto out;
48642f86b12SKonstantin Komarov 
48763544672SKonstantin Komarov 		err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0);
488be71b5cbSKonstantin Komarov 		if (err)
489be71b5cbSKonstantin Komarov 			goto out;
490be71b5cbSKonstantin Komarov 	} else {
491be71b5cbSKonstantin Komarov 		p = resident_data_ex(attr, size);
492be71b5cbSKonstantin Komarov 		if (!p) {
493be71b5cbSKonstantin Komarov 			err = -EINVAL;
494be71b5cbSKonstantin Komarov 			goto out;
495be71b5cbSKonstantin Komarov 		}
496be71b5cbSKonstantin Komarov 		memcpy(p, ea_all, size);
497be71b5cbSKonstantin Komarov 		mi->dirty = true;
498be71b5cbSKonstantin Komarov 	}
499be71b5cbSKonstantin Komarov 
500e8b8e97fSKari Argillander 	/* Check if we delete the last xattr. */
501be71b5cbSKonstantin Komarov 	if (size)
502be71b5cbSKonstantin Komarov 		ni->ni_flags |= NI_FLAG_EA;
503be71b5cbSKonstantin Komarov 	else
504be71b5cbSKonstantin Komarov 		ni->ni_flags &= ~NI_FLAG_EA;
505be71b5cbSKonstantin Komarov 
506be71b5cbSKonstantin Komarov 	if (ea_info.size_pack != size_pack)
507be71b5cbSKonstantin Komarov 		ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
5081842fbc8SKonstantin Komarov 	if (ea_size)
5091842fbc8SKonstantin Komarov 		*ea_size = ea_info.size_pack;
510be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
511be71b5cbSKonstantin Komarov 
512be71b5cbSKonstantin Komarov out:
5133a2154b2SKonstantin Komarov 	if (!locked)
514be71b5cbSKonstantin Komarov 		ni_unlock(ni);
515be71b5cbSKonstantin Komarov 
516be71b5cbSKonstantin Komarov 	run_close(&ea_run);
517195c52bdSKari Argillander 	kfree(ea_all);
518be71b5cbSKonstantin Komarov 
519be71b5cbSKonstantin Komarov 	return err;
520be71b5cbSKonstantin Komarov }
521be71b5cbSKonstantin Komarov 
522be71b5cbSKonstantin Komarov #ifdef CONFIG_NTFS3_FS_POSIX_ACL
523*75c5e0c9SKonstantin Komarov 
524*75c5e0c9SKonstantin Komarov /*
525*75c5e0c9SKonstantin Komarov  * ntfs_get_acl - inode_operations::get_acl
526*75c5e0c9SKonstantin Komarov  */
527*75c5e0c9SKonstantin Komarov struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap,
528*75c5e0c9SKonstantin Komarov 			       struct dentry *dentry, int type)
529be71b5cbSKonstantin Komarov {
530*75c5e0c9SKonstantin Komarov 	struct inode *inode = d_inode(dentry);
531be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
532be71b5cbSKonstantin Komarov 	const char *name;
533be71b5cbSKonstantin Komarov 	size_t name_len;
534be71b5cbSKonstantin Komarov 	struct posix_acl *acl;
535be71b5cbSKonstantin Komarov 	size_t req;
536be71b5cbSKonstantin Komarov 	int err;
537be71b5cbSKonstantin Komarov 	void *buf;
538be71b5cbSKonstantin Komarov 
539e8b8e97fSKari Argillander 	/* Allocate PATH_MAX bytes. */
540be71b5cbSKonstantin Komarov 	buf = __getname();
541be71b5cbSKonstantin Komarov 	if (!buf)
542be71b5cbSKonstantin Komarov 		return ERR_PTR(-ENOMEM);
543be71b5cbSKonstantin Komarov 
544e8b8e97fSKari Argillander 	/* Possible values of 'type' was already checked above. */
545be71b5cbSKonstantin Komarov 	if (type == ACL_TYPE_ACCESS) {
546be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_ACCESS;
547be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
548be71b5cbSKonstantin Komarov 	} else {
549be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_DEFAULT;
550be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
551be71b5cbSKonstantin Komarov 	}
552be71b5cbSKonstantin Komarov 
553be71b5cbSKonstantin Komarov 	ni_lock(ni);
554be71b5cbSKonstantin Komarov 
555be71b5cbSKonstantin Komarov 	err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req);
556be71b5cbSKonstantin Komarov 
557be71b5cbSKonstantin Komarov 	ni_unlock(ni);
558be71b5cbSKonstantin Komarov 
559e8b8e97fSKari Argillander 	/* Translate extended attribute to acl. */
5602926e429SDan Carpenter 	if (err >= 0) {
5610c3bc789SChristian Brauner 		acl = posix_acl_from_xattr(&init_user_ns, buf, err);
5620bd5fdb8SKonstantin Komarov 	} else if (err == -ENODATA) {
5630bd5fdb8SKonstantin Komarov 		acl = NULL;
5640bd5fdb8SKonstantin Komarov 	} else {
5650bd5fdb8SKonstantin Komarov 		acl = ERR_PTR(err);
5660bd5fdb8SKonstantin Komarov 	}
5670bd5fdb8SKonstantin Komarov 
568be71b5cbSKonstantin Komarov 	if (!IS_ERR(acl))
569be71b5cbSKonstantin Komarov 		set_cached_acl(inode, type, acl);
570be71b5cbSKonstantin Komarov 
571be71b5cbSKonstantin Komarov 	__putname(buf);
572be71b5cbSKonstantin Komarov 
573be71b5cbSKonstantin Komarov 	return acl;
574be71b5cbSKonstantin Komarov }
575be71b5cbSKonstantin Komarov 
576700b7940SChristian Brauner static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap,
577be71b5cbSKonstantin Komarov 				    struct inode *inode, struct posix_acl *acl,
5789186d472SKonstantin Komarov 				    int type, bool init_acl)
579be71b5cbSKonstantin Komarov {
580be71b5cbSKonstantin Komarov 	const char *name;
581be71b5cbSKonstantin Komarov 	size_t size, name_len;
582460bbf29SKonstantin Komarov 	void *value;
583460bbf29SKonstantin Komarov 	int err;
584398c35f4SKonstantin Komarov 	int flags;
585460bbf29SKonstantin Komarov 	umode_t mode;
586be71b5cbSKonstantin Komarov 
587be71b5cbSKonstantin Komarov 	if (S_ISLNK(inode->i_mode))
588be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
589be71b5cbSKonstantin Komarov 
590460bbf29SKonstantin Komarov 	mode = inode->i_mode;
591be71b5cbSKonstantin Komarov 	switch (type) {
592be71b5cbSKonstantin Komarov 	case ACL_TYPE_ACCESS:
5939186d472SKonstantin Komarov 		/* Do not change i_mode if we are in init_acl */
5949186d472SKonstantin Komarov 		if (acl && !init_acl) {
595700b7940SChristian Brauner 			err = posix_acl_update_mode(idmap, inode, &mode,
596ba77237eSKonstantin Komarov 						    &acl);
597ba77237eSKonstantin Komarov 			if (err)
598d4073595SDan Carpenter 				return err;
599be71b5cbSKonstantin Komarov 		}
600be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_ACCESS;
601be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
602be71b5cbSKonstantin Komarov 		break;
603be71b5cbSKonstantin Komarov 
604be71b5cbSKonstantin Komarov 	case ACL_TYPE_DEFAULT:
605be71b5cbSKonstantin Komarov 		if (!S_ISDIR(inode->i_mode))
606be71b5cbSKonstantin Komarov 			return acl ? -EACCES : 0;
607be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_DEFAULT;
608be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
609be71b5cbSKonstantin Komarov 		break;
610be71b5cbSKonstantin Komarov 
611be71b5cbSKonstantin Komarov 	default:
612be71b5cbSKonstantin Komarov 		return -EINVAL;
613be71b5cbSKonstantin Komarov 	}
614be71b5cbSKonstantin Komarov 
615be71b5cbSKonstantin Komarov 	if (!acl) {
616398c35f4SKonstantin Komarov 		/* Remove xattr if it can be presented via mode. */
617be71b5cbSKonstantin Komarov 		size = 0;
618be71b5cbSKonstantin Komarov 		value = NULL;
619398c35f4SKonstantin Komarov 		flags = XATTR_REPLACE;
620be71b5cbSKonstantin Komarov 	} else {
621be71b5cbSKonstantin Komarov 		size = posix_acl_xattr_size(acl->a_count);
622195c52bdSKari Argillander 		value = kmalloc(size, GFP_NOFS);
623be71b5cbSKonstantin Komarov 		if (!value)
624be71b5cbSKonstantin Komarov 			return -ENOMEM;
6250c3bc789SChristian Brauner 		err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
626be71b5cbSKonstantin Komarov 		if (err < 0)
627be71b5cbSKonstantin Komarov 			goto out;
628398c35f4SKonstantin Komarov 		flags = 0;
629be71b5cbSKonstantin Komarov 	}
630be71b5cbSKonstantin Komarov 
6311842fbc8SKonstantin Komarov 	err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0, NULL);
632398c35f4SKonstantin Komarov 	if (err == -ENODATA && !size)
633398c35f4SKonstantin Komarov 		err = 0; /* Removing non existed xattr. */
634460bbf29SKonstantin Komarov 	if (!err) {
635be71b5cbSKonstantin Komarov 		set_cached_acl(inode, type, acl);
636460bbf29SKonstantin Komarov 		inode->i_mode = mode;
637e31195a3SKonstantin Komarov 		inode->i_ctime = current_time(inode);
638460bbf29SKonstantin Komarov 		mark_inode_dirty(inode);
639460bbf29SKonstantin Komarov 	}
640be71b5cbSKonstantin Komarov 
641be71b5cbSKonstantin Komarov out:
642195c52bdSKari Argillander 	kfree(value);
643be71b5cbSKonstantin Komarov 
644be71b5cbSKonstantin Komarov 	return err;
645be71b5cbSKonstantin Komarov }
646be71b5cbSKonstantin Komarov 
647be71b5cbSKonstantin Komarov /*
648e8b8e97fSKari Argillander  * ntfs_set_acl - inode_operations::set_acl
649be71b5cbSKonstantin Komarov  */
65013e83a49SChristian Brauner int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
651be71b5cbSKonstantin Komarov 		 struct posix_acl *acl, int type)
652be71b5cbSKonstantin Komarov {
653700b7940SChristian Brauner 	return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false);
654be71b5cbSKonstantin Komarov }
655be71b5cbSKonstantin Komarov 
656be71b5cbSKonstantin Komarov /*
657e8b8e97fSKari Argillander  * ntfs_init_acl - Initialize the ACLs of a new inode.
658e8b8e97fSKari Argillander  *
659e8b8e97fSKari Argillander  * Called from ntfs_create_inode().
660be71b5cbSKonstantin Komarov  */
661700b7940SChristian Brauner int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode,
662be71b5cbSKonstantin Komarov 		  struct inode *dir)
663be71b5cbSKonstantin Komarov {
664be71b5cbSKonstantin Komarov 	struct posix_acl *default_acl, *acl;
665be71b5cbSKonstantin Komarov 	int err;
666be71b5cbSKonstantin Komarov 
66766019837SKonstantin Komarov 	err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
66866019837SKonstantin Komarov 	if (err)
66966019837SKonstantin Komarov 		return err;
670be71b5cbSKonstantin Komarov 
67166019837SKonstantin Komarov 	if (default_acl) {
672700b7940SChristian Brauner 		err = ntfs_set_acl_ex(idmap, inode, default_acl,
6739186d472SKonstantin Komarov 				      ACL_TYPE_DEFAULT, true);
67466019837SKonstantin Komarov 		posix_acl_release(default_acl);
67566019837SKonstantin Komarov 	} else {
67666019837SKonstantin Komarov 		inode->i_default_acl = NULL;
67766019837SKonstantin Komarov 	}
678be71b5cbSKonstantin Komarov 
67919e890ffSYang Xu 	if (acl) {
68066019837SKonstantin Komarov 		if (!err)
681700b7940SChristian Brauner 			err = ntfs_set_acl_ex(idmap, inode, acl,
6829186d472SKonstantin Komarov 					      ACL_TYPE_ACCESS, true);
683be71b5cbSKonstantin Komarov 		posix_acl_release(acl);
68419e890ffSYang Xu 	} else {
68519e890ffSYang Xu 		inode->i_acl = NULL;
68666019837SKonstantin Komarov 	}
687be71b5cbSKonstantin Komarov 
688be71b5cbSKonstantin Komarov 	return err;
689be71b5cbSKonstantin Komarov }
690be71b5cbSKonstantin Komarov #endif
691be71b5cbSKonstantin Komarov 
692be71b5cbSKonstantin Komarov /*
693e8b8e97fSKari Argillander  * ntfs_acl_chmod - Helper for ntfs3_setattr().
694be71b5cbSKonstantin Komarov  */
69513e83a49SChristian Brauner int ntfs_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry)
696be71b5cbSKonstantin Komarov {
697138060baSChristian Brauner 	struct inode *inode = d_inode(dentry);
698be71b5cbSKonstantin Komarov 	struct super_block *sb = inode->i_sb;
699be71b5cbSKonstantin Komarov 
700be71b5cbSKonstantin Komarov 	if (!(sb->s_flags & SB_POSIXACL))
701be71b5cbSKonstantin Komarov 		return 0;
702be71b5cbSKonstantin Komarov 
703be71b5cbSKonstantin Komarov 	if (S_ISLNK(inode->i_mode))
704be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
705be71b5cbSKonstantin Komarov 
70613e83a49SChristian Brauner 	return posix_acl_chmod(idmap, dentry, inode->i_mode);
707be71b5cbSKonstantin Komarov }
708be71b5cbSKonstantin Komarov 
709be71b5cbSKonstantin Komarov /*
710e8b8e97fSKari Argillander  * ntfs_listxattr - inode_operations::listxattr
711be71b5cbSKonstantin Komarov  */
712be71b5cbSKonstantin Komarov ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
713be71b5cbSKonstantin Komarov {
714be71b5cbSKonstantin Komarov 	struct inode *inode = d_inode(dentry);
715be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
716be71b5cbSKonstantin Komarov 	ssize_t ret;
717be71b5cbSKonstantin Komarov 
718be71b5cbSKonstantin Komarov 	if (!(ni->ni_flags & NI_FLAG_EA)) {
719be71b5cbSKonstantin Komarov 		/* no xattr in file */
720be71b5cbSKonstantin Komarov 		return 0;
721be71b5cbSKonstantin Komarov 	}
722be71b5cbSKonstantin Komarov 
723be71b5cbSKonstantin Komarov 	ni_lock(ni);
724be71b5cbSKonstantin Komarov 
725be71b5cbSKonstantin Komarov 	ret = ntfs_list_ea(ni, buffer, size);
726be71b5cbSKonstantin Komarov 
727be71b5cbSKonstantin Komarov 	ni_unlock(ni);
728be71b5cbSKonstantin Komarov 
729be71b5cbSKonstantin Komarov 	return ret;
730be71b5cbSKonstantin Komarov }
731be71b5cbSKonstantin Komarov 
732be71b5cbSKonstantin Komarov static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
733be71b5cbSKonstantin Komarov 			 struct inode *inode, const char *name, void *buffer,
734be71b5cbSKonstantin Komarov 			 size_t size)
735be71b5cbSKonstantin Komarov {
736be71b5cbSKonstantin Komarov 	int err;
737be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
738be71b5cbSKonstantin Komarov 
739e8b8e97fSKari Argillander 	/* Dispatch request. */
740d45da67cSYuan Can 	if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
741be71b5cbSKonstantin Komarov 		/* system.dos_attrib */
742be71b5cbSKonstantin Komarov 		if (!buffer) {
743be71b5cbSKonstantin Komarov 			err = sizeof(u8);
744be71b5cbSKonstantin Komarov 		} else if (size < sizeof(u8)) {
745be71b5cbSKonstantin Komarov 			err = -ENODATA;
746be71b5cbSKonstantin Komarov 		} else {
747be71b5cbSKonstantin Komarov 			err = sizeof(u8);
748be71b5cbSKonstantin Komarov 			*(u8 *)buffer = le32_to_cpu(ni->std_fa);
749be71b5cbSKonstantin Komarov 		}
750be71b5cbSKonstantin Komarov 		goto out;
751be71b5cbSKonstantin Komarov 	}
752be71b5cbSKonstantin Komarov 
7530d19f3d7SDaniel Pinto 	if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
7540d19f3d7SDaniel Pinto 	    !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
755be71b5cbSKonstantin Komarov 		/* system.ntfs_attrib */
756be71b5cbSKonstantin Komarov 		if (!buffer) {
757be71b5cbSKonstantin Komarov 			err = sizeof(u32);
758be71b5cbSKonstantin Komarov 		} else if (size < sizeof(u32)) {
759be71b5cbSKonstantin Komarov 			err = -ENODATA;
760be71b5cbSKonstantin Komarov 		} else {
761be71b5cbSKonstantin Komarov 			err = sizeof(u32);
762be71b5cbSKonstantin Komarov 			*(u32 *)buffer = le32_to_cpu(ni->std_fa);
7630d19f3d7SDaniel Pinto 			if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
7640203471dSDaniel Pinto 				*(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer);
765be71b5cbSKonstantin Komarov 		}
766be71b5cbSKonstantin Komarov 		goto out;
767be71b5cbSKonstantin Komarov 	}
768be71b5cbSKonstantin Komarov 
769d45da67cSYuan Can 	if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
770be71b5cbSKonstantin Komarov 		/* system.ntfs_security*/
771be71b5cbSKonstantin Komarov 		struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
772be71b5cbSKonstantin Komarov 		size_t sd_size = 0;
773be71b5cbSKonstantin Komarov 
774be71b5cbSKonstantin Komarov 		if (!is_ntfs3(ni->mi.sbi)) {
775e8b8e97fSKari Argillander 			/* We should get nt4 security. */
776be71b5cbSKonstantin Komarov 			err = -EINVAL;
777be71b5cbSKonstantin Komarov 			goto out;
778be71b5cbSKonstantin Komarov 		} else if (le32_to_cpu(ni->std_security_id) <
779be71b5cbSKonstantin Komarov 			   SECURITY_ID_FIRST) {
780be71b5cbSKonstantin Komarov 			err = -ENOENT;
781be71b5cbSKonstantin Komarov 			goto out;
782be71b5cbSKonstantin Komarov 		}
783be71b5cbSKonstantin Komarov 
784be71b5cbSKonstantin Komarov 		err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
785be71b5cbSKonstantin Komarov 					      &sd, &sd_size);
786be71b5cbSKonstantin Komarov 		if (err)
787be71b5cbSKonstantin Komarov 			goto out;
788be71b5cbSKonstantin Komarov 
789be71b5cbSKonstantin Komarov 		if (!is_sd_valid(sd, sd_size)) {
790be71b5cbSKonstantin Komarov 			ntfs_inode_warn(
791be71b5cbSKonstantin Komarov 				inode,
792be71b5cbSKonstantin Komarov 				"looks like you get incorrect security descriptor id=%u",
793be71b5cbSKonstantin Komarov 				ni->std_security_id);
794be71b5cbSKonstantin Komarov 		}
795be71b5cbSKonstantin Komarov 
796be71b5cbSKonstantin Komarov 		if (!buffer) {
797be71b5cbSKonstantin Komarov 			err = sd_size;
798be71b5cbSKonstantin Komarov 		} else if (size < sd_size) {
799be71b5cbSKonstantin Komarov 			err = -ENODATA;
800be71b5cbSKonstantin Komarov 		} else {
801be71b5cbSKonstantin Komarov 			err = sd_size;
802be71b5cbSKonstantin Komarov 			memcpy(buffer, sd, sd_size);
803be71b5cbSKonstantin Komarov 		}
804195c52bdSKari Argillander 		kfree(sd);
805be71b5cbSKonstantin Komarov 		goto out;
806be71b5cbSKonstantin Komarov 	}
807be71b5cbSKonstantin Komarov 
808e8b8e97fSKari Argillander 	/* Deal with NTFS extended attribute. */
809d45da67cSYuan Can 	err = ntfs_get_ea(inode, name, strlen(name), buffer, size, NULL);
810be71b5cbSKonstantin Komarov 
811be71b5cbSKonstantin Komarov out:
812be71b5cbSKonstantin Komarov 	return err;
813be71b5cbSKonstantin Komarov }
814be71b5cbSKonstantin Komarov 
815be71b5cbSKonstantin Komarov /*
816e8b8e97fSKari Argillander  * ntfs_setxattr - inode_operations::setxattr
817be71b5cbSKonstantin Komarov  */
818be71b5cbSKonstantin Komarov static noinline int ntfs_setxattr(const struct xattr_handler *handler,
81939f60c1cSChristian Brauner 				  struct mnt_idmap *idmap,
820be71b5cbSKonstantin Komarov 				  struct dentry *de, struct inode *inode,
821be71b5cbSKonstantin Komarov 				  const char *name, const void *value,
822be71b5cbSKonstantin Komarov 				  size_t size, int flags)
823be71b5cbSKonstantin Komarov {
824be71b5cbSKonstantin Komarov 	int err = -EINVAL;
825be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
826be71b5cbSKonstantin Komarov 	enum FILE_ATTRIBUTE new_fa;
827be71b5cbSKonstantin Komarov 
828e8b8e97fSKari Argillander 	/* Dispatch request. */
829d45da67cSYuan Can 	if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
830be71b5cbSKonstantin Komarov 		if (sizeof(u8) != size)
831be71b5cbSKonstantin Komarov 			goto out;
832be71b5cbSKonstantin Komarov 		new_fa = cpu_to_le32(*(u8 *)value);
833be71b5cbSKonstantin Komarov 		goto set_new_fa;
834be71b5cbSKonstantin Komarov 	}
835be71b5cbSKonstantin Komarov 
8360d19f3d7SDaniel Pinto 	if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
8370d19f3d7SDaniel Pinto 	    !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
838be71b5cbSKonstantin Komarov 		if (size != sizeof(u32))
839be71b5cbSKonstantin Komarov 			goto out;
8400d19f3d7SDaniel Pinto 		if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
8410203471dSDaniel Pinto 			new_fa = cpu_to_le32(be32_to_cpu(*(__be32 *)value));
8420d19f3d7SDaniel Pinto 		else
843be71b5cbSKonstantin Komarov 			new_fa = cpu_to_le32(*(u32 *)value);
844be71b5cbSKonstantin Komarov 
845be71b5cbSKonstantin Komarov 		if (S_ISREG(inode->i_mode)) {
846e8b8e97fSKari Argillander 			/* Process compressed/sparsed in special way. */
847be71b5cbSKonstantin Komarov 			ni_lock(ni);
848be71b5cbSKonstantin Komarov 			err = ni_new_attr_flags(ni, new_fa);
849be71b5cbSKonstantin Komarov 			ni_unlock(ni);
850be71b5cbSKonstantin Komarov 			if (err)
851be71b5cbSKonstantin Komarov 				goto out;
852be71b5cbSKonstantin Komarov 		}
853be71b5cbSKonstantin Komarov set_new_fa:
854be71b5cbSKonstantin Komarov 		/*
855be71b5cbSKonstantin Komarov 		 * Thanks Mark Harmstone:
856e8b8e97fSKari Argillander 		 * Keep directory bit consistency.
857be71b5cbSKonstantin Komarov 		 */
858be71b5cbSKonstantin Komarov 		if (S_ISDIR(inode->i_mode))
859be71b5cbSKonstantin Komarov 			new_fa |= FILE_ATTRIBUTE_DIRECTORY;
860be71b5cbSKonstantin Komarov 		else
861be71b5cbSKonstantin Komarov 			new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
862be71b5cbSKonstantin Komarov 
863be71b5cbSKonstantin Komarov 		if (ni->std_fa != new_fa) {
864be71b5cbSKonstantin Komarov 			ni->std_fa = new_fa;
865be71b5cbSKonstantin Komarov 			if (new_fa & FILE_ATTRIBUTE_READONLY)
866be71b5cbSKonstantin Komarov 				inode->i_mode &= ~0222;
867be71b5cbSKonstantin Komarov 			else
868be71b5cbSKonstantin Komarov 				inode->i_mode |= 0222;
869e8b8e97fSKari Argillander 			/* Std attribute always in primary record. */
870be71b5cbSKonstantin Komarov 			ni->mi.dirty = true;
871be71b5cbSKonstantin Komarov 			mark_inode_dirty(inode);
872be71b5cbSKonstantin Komarov 		}
873be71b5cbSKonstantin Komarov 		err = 0;
874be71b5cbSKonstantin Komarov 
875be71b5cbSKonstantin Komarov 		goto out;
876be71b5cbSKonstantin Komarov 	}
877be71b5cbSKonstantin Komarov 
878d45da67cSYuan Can 	if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
879be71b5cbSKonstantin Komarov 		/* system.ntfs_security*/
880be71b5cbSKonstantin Komarov 		__le32 security_id;
881be71b5cbSKonstantin Komarov 		bool inserted;
882be71b5cbSKonstantin Komarov 		struct ATTR_STD_INFO5 *std;
883be71b5cbSKonstantin Komarov 
884be71b5cbSKonstantin Komarov 		if (!is_ntfs3(ni->mi.sbi)) {
885be71b5cbSKonstantin Komarov 			/*
886e8b8e97fSKari Argillander 			 * We should replace ATTR_SECURE.
887e8b8e97fSKari Argillander 			 * Skip this way cause it is nt4 feature.
888be71b5cbSKonstantin Komarov 			 */
889be71b5cbSKonstantin Komarov 			err = -EINVAL;
890be71b5cbSKonstantin Komarov 			goto out;
891be71b5cbSKonstantin Komarov 		}
892be71b5cbSKonstantin Komarov 
893be71b5cbSKonstantin Komarov 		if (!is_sd_valid(value, size)) {
894be71b5cbSKonstantin Komarov 			err = -EINVAL;
895be71b5cbSKonstantin Komarov 			ntfs_inode_warn(
896be71b5cbSKonstantin Komarov 				inode,
897be71b5cbSKonstantin Komarov 				"you try to set invalid security descriptor");
898be71b5cbSKonstantin Komarov 			goto out;
899be71b5cbSKonstantin Komarov 		}
900be71b5cbSKonstantin Komarov 
901be71b5cbSKonstantin Komarov 		err = ntfs_insert_security(ni->mi.sbi, value, size,
902be71b5cbSKonstantin Komarov 					   &security_id, &inserted);
903be71b5cbSKonstantin Komarov 		if (err)
904be71b5cbSKonstantin Komarov 			goto out;
905be71b5cbSKonstantin Komarov 
906be71b5cbSKonstantin Komarov 		ni_lock(ni);
907be71b5cbSKonstantin Komarov 		std = ni_std5(ni);
908be71b5cbSKonstantin Komarov 		if (!std) {
909be71b5cbSKonstantin Komarov 			err = -EINVAL;
910be71b5cbSKonstantin Komarov 		} else if (std->security_id != security_id) {
911be71b5cbSKonstantin Komarov 			std->security_id = ni->std_security_id = security_id;
912e8b8e97fSKari Argillander 			/* Std attribute always in primary record. */
913be71b5cbSKonstantin Komarov 			ni->mi.dirty = true;
914be71b5cbSKonstantin Komarov 			mark_inode_dirty(&ni->vfs_inode);
915be71b5cbSKonstantin Komarov 		}
916be71b5cbSKonstantin Komarov 		ni_unlock(ni);
917be71b5cbSKonstantin Komarov 		goto out;
918be71b5cbSKonstantin Komarov 	}
919be71b5cbSKonstantin Komarov 
920e8b8e97fSKari Argillander 	/* Deal with NTFS extended attribute. */
9211842fbc8SKonstantin Komarov 	err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0,
9221842fbc8SKonstantin Komarov 			  NULL);
923be71b5cbSKonstantin Komarov 
924be71b5cbSKonstantin Komarov out:
9252d44667cSKonstantin Komarov 	inode->i_ctime = current_time(inode);
9262d44667cSKonstantin Komarov 	mark_inode_dirty(inode);
9272d44667cSKonstantin Komarov 
928be71b5cbSKonstantin Komarov 	return err;
929be71b5cbSKonstantin Komarov }
930be71b5cbSKonstantin Komarov 
931be71b5cbSKonstantin Komarov /*
932be71b5cbSKonstantin Komarov  * ntfs_save_wsl_perm
933be71b5cbSKonstantin Komarov  *
934be71b5cbSKonstantin Komarov  * save uid/gid/mode in xattr
935be71b5cbSKonstantin Komarov  */
9361842fbc8SKonstantin Komarov int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size)
937be71b5cbSKonstantin Komarov {
938be71b5cbSKonstantin Komarov 	int err;
939be71b5cbSKonstantin Komarov 	__le32 value;
9403a2154b2SKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
941be71b5cbSKonstantin Komarov 
9423a2154b2SKonstantin Komarov 	ni_lock(ni);
943be71b5cbSKonstantin Komarov 	value = cpu_to_le32(i_uid_read(inode));
944be71b5cbSKonstantin Komarov 	err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
9451842fbc8SKonstantin Komarov 			  sizeof(value), 0, true, ea_size);
946be71b5cbSKonstantin Komarov 	if (err)
947be71b5cbSKonstantin Komarov 		goto out;
948be71b5cbSKonstantin Komarov 
949be71b5cbSKonstantin Komarov 	value = cpu_to_le32(i_gid_read(inode));
950be71b5cbSKonstantin Komarov 	err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
9511842fbc8SKonstantin Komarov 			  sizeof(value), 0, true, ea_size);
952be71b5cbSKonstantin Komarov 	if (err)
953be71b5cbSKonstantin Komarov 		goto out;
954be71b5cbSKonstantin Komarov 
955be71b5cbSKonstantin Komarov 	value = cpu_to_le32(inode->i_mode);
956be71b5cbSKonstantin Komarov 	err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
9571842fbc8SKonstantin Komarov 			  sizeof(value), 0, true, ea_size);
958be71b5cbSKonstantin Komarov 	if (err)
959be71b5cbSKonstantin Komarov 		goto out;
960be71b5cbSKonstantin Komarov 
961be71b5cbSKonstantin Komarov 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
962be71b5cbSKonstantin Komarov 		value = cpu_to_le32(inode->i_rdev);
963be71b5cbSKonstantin Komarov 		err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
9641842fbc8SKonstantin Komarov 				  sizeof(value), 0, true, ea_size);
965be71b5cbSKonstantin Komarov 		if (err)
966be71b5cbSKonstantin Komarov 			goto out;
967be71b5cbSKonstantin Komarov 	}
968be71b5cbSKonstantin Komarov 
969be71b5cbSKonstantin Komarov out:
9703a2154b2SKonstantin Komarov 	ni_unlock(ni);
971be71b5cbSKonstantin Komarov 	/* In case of error should we delete all WSL xattr? */
972be71b5cbSKonstantin Komarov 	return err;
973be71b5cbSKonstantin Komarov }
974be71b5cbSKonstantin Komarov 
975be71b5cbSKonstantin Komarov /*
976be71b5cbSKonstantin Komarov  * ntfs_get_wsl_perm
977be71b5cbSKonstantin Komarov  *
978be71b5cbSKonstantin Komarov  * get uid/gid/mode from xattr
979be71b5cbSKonstantin Komarov  * it is called from ntfs_iget5->ntfs_read_mft
980be71b5cbSKonstantin Komarov  */
981be71b5cbSKonstantin Komarov void ntfs_get_wsl_perm(struct inode *inode)
982be71b5cbSKonstantin Komarov {
983be71b5cbSKonstantin Komarov 	size_t sz;
984be71b5cbSKonstantin Komarov 	__le32 value[3];
985be71b5cbSKonstantin Komarov 
986be71b5cbSKonstantin Komarov 	if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
987be71b5cbSKonstantin Komarov 			sizeof(value[0]), &sz) == sizeof(value[0]) &&
988be71b5cbSKonstantin Komarov 	    ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
989be71b5cbSKonstantin Komarov 			sizeof(value[1]), &sz) == sizeof(value[1]) &&
990be71b5cbSKonstantin Komarov 	    ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
991be71b5cbSKonstantin Komarov 			sizeof(value[2]), &sz) == sizeof(value[2])) {
992be71b5cbSKonstantin Komarov 		i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
993be71b5cbSKonstantin Komarov 		i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
994be71b5cbSKonstantin Komarov 		inode->i_mode = le32_to_cpu(value[2]);
995be71b5cbSKonstantin Komarov 
996be71b5cbSKonstantin Komarov 		if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
997be71b5cbSKonstantin Komarov 				&value[0], sizeof(value),
998be71b5cbSKonstantin Komarov 				&sz) == sizeof(value[0])) {
999be71b5cbSKonstantin Komarov 			inode->i_rdev = le32_to_cpu(value[0]);
1000be71b5cbSKonstantin Komarov 		}
1001be71b5cbSKonstantin Komarov 	}
1002be71b5cbSKonstantin Komarov }
1003be71b5cbSKonstantin Komarov 
1004be71b5cbSKonstantin Komarov static bool ntfs_xattr_user_list(struct dentry *dentry)
1005be71b5cbSKonstantin Komarov {
1006be71b5cbSKonstantin Komarov 	return true;
1007be71b5cbSKonstantin Komarov }
1008be71b5cbSKonstantin Komarov 
1009be71b5cbSKonstantin Komarov // clang-format off
1010a26aa123SChristian Brauner static const struct xattr_handler ntfs_other_xattr_handler = {
1011be71b5cbSKonstantin Komarov 	.prefix	= "",
1012be71b5cbSKonstantin Komarov 	.get	= ntfs_getxattr,
1013be71b5cbSKonstantin Komarov 	.set	= ntfs_setxattr,
1014be71b5cbSKonstantin Komarov 	.list	= ntfs_xattr_user_list,
1015be71b5cbSKonstantin Komarov };
1016be71b5cbSKonstantin Komarov 
1017be71b5cbSKonstantin Komarov const struct xattr_handler *ntfs_xattr_handlers[] = {
1018a26aa123SChristian Brauner #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1019a26aa123SChristian Brauner 	&posix_acl_access_xattr_handler,
1020a26aa123SChristian Brauner 	&posix_acl_default_xattr_handler,
1021a26aa123SChristian Brauner #endif
1022a26aa123SChristian Brauner 	&ntfs_other_xattr_handler,
1023be71b5cbSKonstantin Komarov 	NULL,
1024be71b5cbSKonstantin Komarov };
1025be71b5cbSKonstantin Komarov // clang-format on
1026