xref: /openbmc/linux/fs/ntfs3/xattr.c (revision d81e06be921f90d5f1bada59d4549ca6f1bedc61)
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"
20be71b5cbSKonstantin Komarov #define SYSTEM_NTFS_SECURITY "system.ntfs_security"
21be71b5cbSKonstantin Komarov // clang-format on
22be71b5cbSKonstantin Komarov 
23be71b5cbSKonstantin Komarov static inline size_t unpacked_ea_size(const struct EA_FULL *ea)
24be71b5cbSKonstantin Komarov {
25be71b5cbSKonstantin Komarov 	return ea->size ? le32_to_cpu(ea->size)
26d3624466SKonstantin Komarov 			: ALIGN(struct_size(ea, name,
27d3624466SKonstantin Komarov 					    1 + ea->name_len +
28d3624466SKonstantin Komarov 						    le16_to_cpu(ea->elength)),
29d3624466SKonstantin Komarov 				4);
30be71b5cbSKonstantin Komarov }
31be71b5cbSKonstantin Komarov 
32be71b5cbSKonstantin Komarov static inline size_t packed_ea_size(const struct EA_FULL *ea)
33be71b5cbSKonstantin Komarov {
34be71b5cbSKonstantin Komarov 	return struct_size(ea, name,
35be71b5cbSKonstantin Komarov 			   1 + ea->name_len + le16_to_cpu(ea->elength)) -
36be71b5cbSKonstantin Komarov 	       offsetof(struct EA_FULL, flags);
37be71b5cbSKonstantin Komarov }
38be71b5cbSKonstantin Komarov 
39be71b5cbSKonstantin Komarov /*
40be71b5cbSKonstantin Komarov  * find_ea
41be71b5cbSKonstantin Komarov  *
42e8b8e97fSKari Argillander  * Assume there is at least one xattr in the list.
43be71b5cbSKonstantin Komarov  */
44be71b5cbSKonstantin Komarov static inline bool find_ea(const struct EA_FULL *ea_all, u32 bytes,
45be71b5cbSKonstantin Komarov 			   const char *name, u8 name_len, u32 *off)
46be71b5cbSKonstantin Komarov {
47be71b5cbSKonstantin Komarov 	*off = 0;
48be71b5cbSKonstantin Komarov 
49be71b5cbSKonstantin Komarov 	if (!ea_all || !bytes)
50be71b5cbSKonstantin Komarov 		return false;
51be71b5cbSKonstantin Komarov 
52be71b5cbSKonstantin Komarov 	for (;;) {
53be71b5cbSKonstantin Komarov 		const struct EA_FULL *ea = Add2Ptr(ea_all, *off);
54be71b5cbSKonstantin Komarov 		u32 next_off = *off + unpacked_ea_size(ea);
55be71b5cbSKonstantin Komarov 
56be71b5cbSKonstantin Komarov 		if (next_off > bytes)
57be71b5cbSKonstantin Komarov 			return false;
58be71b5cbSKonstantin Komarov 
59be71b5cbSKonstantin Komarov 		if (ea->name_len == name_len &&
60be71b5cbSKonstantin Komarov 		    !memcmp(ea->name, name, name_len))
61be71b5cbSKonstantin Komarov 			return true;
62be71b5cbSKonstantin Komarov 
63be71b5cbSKonstantin Komarov 		*off = next_off;
64be71b5cbSKonstantin Komarov 		if (next_off >= bytes)
65be71b5cbSKonstantin Komarov 			return false;
66be71b5cbSKonstantin Komarov 	}
67be71b5cbSKonstantin Komarov }
68be71b5cbSKonstantin Komarov 
69be71b5cbSKonstantin Komarov /*
70e8b8e97fSKari Argillander  * ntfs_read_ea - Read all extended attributes.
71e8b8e97fSKari Argillander  * @ea:		New allocated memory.
72e8b8e97fSKari Argillander  * @info:	Pointer into resident data.
73be71b5cbSKonstantin Komarov  */
74be71b5cbSKonstantin Komarov static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea,
75be71b5cbSKonstantin Komarov 			size_t add_bytes, const struct EA_INFO **info)
76be71b5cbSKonstantin Komarov {
77be71b5cbSKonstantin Komarov 	int err;
78be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le = NULL;
79be71b5cbSKonstantin Komarov 	struct ATTRIB *attr_info, *attr_ea;
80be71b5cbSKonstantin Komarov 	void *ea_p;
81be71b5cbSKonstantin Komarov 	u32 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)
98be71b5cbSKonstantin Komarov 		return -EINVAL;
99be71b5cbSKonstantin Komarov 
100e8b8e97fSKari Argillander 	/* Check Ea limit. */
101be71b5cbSKonstantin Komarov 	size = le32_to_cpu((*info)->size);
102be71b5cbSKonstantin Komarov 	if (size > ni->mi.sbi->ea_max_size)
103be71b5cbSKonstantin Komarov 		return -EFBIG;
104be71b5cbSKonstantin Komarov 
105be71b5cbSKonstantin Komarov 	if (attr_size(attr_ea) > ni->mi.sbi->ea_max_size)
106be71b5cbSKonstantin Komarov 		return -EFBIG;
107be71b5cbSKonstantin Komarov 
108e8b8e97fSKari Argillander 	/* Allocate memory for packed Ea. */
109195c52bdSKari Argillander 	ea_p = kmalloc(size + add_bytes, GFP_NOFS);
110be71b5cbSKonstantin Komarov 	if (!ea_p)
111be71b5cbSKonstantin Komarov 		return -ENOMEM;
112be71b5cbSKonstantin Komarov 
113be71b5cbSKonstantin Komarov 	if (attr_ea->non_res) {
114be71b5cbSKonstantin Komarov 		struct runs_tree run;
115be71b5cbSKonstantin Komarov 
116be71b5cbSKonstantin Komarov 		run_init(&run);
117be71b5cbSKonstantin Komarov 
118be71b5cbSKonstantin Komarov 		err = attr_load_runs(attr_ea, ni, &run, NULL);
119be71b5cbSKonstantin Komarov 		if (!err)
120be71b5cbSKonstantin Komarov 			err = ntfs_read_run_nb(ni->mi.sbi, &run, 0, ea_p, size,
121be71b5cbSKonstantin Komarov 					       NULL);
122be71b5cbSKonstantin Komarov 		run_close(&run);
123be71b5cbSKonstantin Komarov 
124be71b5cbSKonstantin Komarov 		if (err)
125be71b5cbSKonstantin Komarov 			goto out;
126be71b5cbSKonstantin Komarov 	} else {
127be71b5cbSKonstantin Komarov 		void *p = resident_data_ex(attr_ea, size);
128be71b5cbSKonstantin Komarov 
129be71b5cbSKonstantin Komarov 		if (!p) {
130be71b5cbSKonstantin Komarov 			err = -EINVAL;
131be71b5cbSKonstantin Komarov 			goto out;
132be71b5cbSKonstantin Komarov 		}
133be71b5cbSKonstantin Komarov 		memcpy(ea_p, p, size);
134be71b5cbSKonstantin Komarov 	}
135be71b5cbSKonstantin Komarov 
136be71b5cbSKonstantin Komarov 	memset(Add2Ptr(ea_p, size), 0, add_bytes);
137be71b5cbSKonstantin Komarov 	*ea = ea_p;
138be71b5cbSKonstantin Komarov 	return 0;
139be71b5cbSKonstantin Komarov 
140be71b5cbSKonstantin Komarov out:
141195c52bdSKari Argillander 	kfree(ea_p);
142be71b5cbSKonstantin Komarov 	*ea = NULL;
143be71b5cbSKonstantin Komarov 	return err;
144be71b5cbSKonstantin Komarov }
145be71b5cbSKonstantin Komarov 
146be71b5cbSKonstantin Komarov /*
147be71b5cbSKonstantin Komarov  * ntfs_list_ea
148be71b5cbSKonstantin Komarov  *
149e8b8e97fSKari Argillander  * Copy a list of xattrs names into the buffer
150e8b8e97fSKari Argillander  * provided, or compute the buffer size required.
151be71b5cbSKonstantin Komarov  *
152e8b8e97fSKari Argillander  * Return:
153e8b8e97fSKari Argillander  * * Number of bytes used / required on
154e8b8e97fSKari Argillander  * * -ERRNO - on failure
155be71b5cbSKonstantin Komarov  */
156be71b5cbSKonstantin Komarov static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer,
157be71b5cbSKonstantin Komarov 			    size_t bytes_per_buffer)
158be71b5cbSKonstantin Komarov {
159be71b5cbSKonstantin Komarov 	const struct EA_INFO *info;
160be71b5cbSKonstantin Komarov 	struct EA_FULL *ea_all = NULL;
161be71b5cbSKonstantin Komarov 	const struct EA_FULL *ea;
162be71b5cbSKonstantin Komarov 	u32 off, size;
163be71b5cbSKonstantin Komarov 	int err;
164be71b5cbSKonstantin Komarov 	size_t ret;
165be71b5cbSKonstantin Komarov 
166be71b5cbSKonstantin Komarov 	err = ntfs_read_ea(ni, &ea_all, 0, &info);
167be71b5cbSKonstantin Komarov 	if (err)
168be71b5cbSKonstantin Komarov 		return err;
169be71b5cbSKonstantin Komarov 
170be71b5cbSKonstantin Komarov 	if (!info || !ea_all)
171be71b5cbSKonstantin Komarov 		return 0;
172be71b5cbSKonstantin Komarov 
173be71b5cbSKonstantin Komarov 	size = le32_to_cpu(info->size);
174be71b5cbSKonstantin Komarov 
175e8b8e97fSKari Argillander 	/* Enumerate all xattrs. */
176be71b5cbSKonstantin Komarov 	for (ret = 0, off = 0; off < size; off += unpacked_ea_size(ea)) {
177be71b5cbSKonstantin Komarov 		ea = Add2Ptr(ea_all, off);
178be71b5cbSKonstantin Komarov 
179be71b5cbSKonstantin Komarov 		if (buffer) {
180be71b5cbSKonstantin Komarov 			if (ret + ea->name_len + 1 > bytes_per_buffer) {
181be71b5cbSKonstantin Komarov 				err = -ERANGE;
182be71b5cbSKonstantin Komarov 				goto out;
183be71b5cbSKonstantin Komarov 			}
184be71b5cbSKonstantin Komarov 
185be71b5cbSKonstantin Komarov 			memcpy(buffer + ret, ea->name, ea->name_len);
186be71b5cbSKonstantin Komarov 			buffer[ret + ea->name_len] = 0;
187be71b5cbSKonstantin Komarov 		}
188be71b5cbSKonstantin Komarov 
189be71b5cbSKonstantin Komarov 		ret += ea->name_len + 1;
190be71b5cbSKonstantin Komarov 	}
191be71b5cbSKonstantin Komarov 
192be71b5cbSKonstantin Komarov out:
193195c52bdSKari Argillander 	kfree(ea_all);
194be71b5cbSKonstantin Komarov 	return err ? err : ret;
195be71b5cbSKonstantin Komarov }
196be71b5cbSKonstantin Komarov 
197be71b5cbSKonstantin Komarov static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
198be71b5cbSKonstantin Komarov 		       void *buffer, size_t size, size_t *required)
199be71b5cbSKonstantin Komarov {
200be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
201be71b5cbSKonstantin Komarov 	const struct EA_INFO *info;
202be71b5cbSKonstantin Komarov 	struct EA_FULL *ea_all = NULL;
203be71b5cbSKonstantin Komarov 	const struct EA_FULL *ea;
204be71b5cbSKonstantin Komarov 	u32 off, len;
205be71b5cbSKonstantin Komarov 	int err;
206be71b5cbSKonstantin Komarov 
207be71b5cbSKonstantin Komarov 	if (!(ni->ni_flags & NI_FLAG_EA))
208be71b5cbSKonstantin Komarov 		return -ENODATA;
209be71b5cbSKonstantin Komarov 
210be71b5cbSKonstantin Komarov 	if (!required)
211be71b5cbSKonstantin Komarov 		ni_lock(ni);
212be71b5cbSKonstantin Komarov 
213be71b5cbSKonstantin Komarov 	len = 0;
214be71b5cbSKonstantin Komarov 
215be71b5cbSKonstantin Komarov 	if (name_len > 255) {
216be71b5cbSKonstantin Komarov 		err = -ENAMETOOLONG;
217be71b5cbSKonstantin Komarov 		goto out;
218be71b5cbSKonstantin Komarov 	}
219be71b5cbSKonstantin Komarov 
220be71b5cbSKonstantin Komarov 	err = ntfs_read_ea(ni, &ea_all, 0, &info);
221be71b5cbSKonstantin Komarov 	if (err)
222be71b5cbSKonstantin Komarov 		goto out;
223be71b5cbSKonstantin Komarov 
224be71b5cbSKonstantin Komarov 	if (!info)
225be71b5cbSKonstantin Komarov 		goto out;
226be71b5cbSKonstantin Komarov 
227e8b8e97fSKari Argillander 	/* Enumerate all xattrs. */
228be71b5cbSKonstantin Komarov 	if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off)) {
229be71b5cbSKonstantin Komarov 		err = -ENODATA;
230be71b5cbSKonstantin Komarov 		goto out;
231be71b5cbSKonstantin Komarov 	}
232be71b5cbSKonstantin Komarov 	ea = Add2Ptr(ea_all, off);
233be71b5cbSKonstantin Komarov 
234be71b5cbSKonstantin Komarov 	len = le16_to_cpu(ea->elength);
235be71b5cbSKonstantin Komarov 	if (!buffer) {
236be71b5cbSKonstantin Komarov 		err = 0;
237be71b5cbSKonstantin Komarov 		goto out;
238be71b5cbSKonstantin Komarov 	}
239be71b5cbSKonstantin Komarov 
240be71b5cbSKonstantin Komarov 	if (len > size) {
241be71b5cbSKonstantin Komarov 		err = -ERANGE;
242be71b5cbSKonstantin Komarov 		if (required)
243be71b5cbSKonstantin Komarov 			*required = len;
244be71b5cbSKonstantin Komarov 		goto out;
245be71b5cbSKonstantin Komarov 	}
246be71b5cbSKonstantin Komarov 
247be71b5cbSKonstantin Komarov 	memcpy(buffer, ea->name + ea->name_len + 1, len);
248be71b5cbSKonstantin Komarov 	err = 0;
249be71b5cbSKonstantin Komarov 
250be71b5cbSKonstantin Komarov out:
251195c52bdSKari Argillander 	kfree(ea_all);
252be71b5cbSKonstantin Komarov 	if (!required)
253be71b5cbSKonstantin Komarov 		ni_unlock(ni);
254be71b5cbSKonstantin Komarov 
255be71b5cbSKonstantin Komarov 	return err ? err : len;
256be71b5cbSKonstantin Komarov }
257be71b5cbSKonstantin Komarov 
258be71b5cbSKonstantin Komarov static noinline int ntfs_set_ea(struct inode *inode, const char *name,
259be71b5cbSKonstantin Komarov 				size_t name_len, const void *value,
260*d81e06beSKonstantin Komarov 				size_t val_size, int flags)
261be71b5cbSKonstantin Komarov {
262be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
263be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
264be71b5cbSKonstantin Komarov 	int err;
265be71b5cbSKonstantin Komarov 	struct EA_INFO ea_info;
266be71b5cbSKonstantin Komarov 	const struct EA_INFO *info;
267be71b5cbSKonstantin Komarov 	struct EA_FULL *new_ea;
268be71b5cbSKonstantin Komarov 	struct EA_FULL *ea_all = NULL;
269be71b5cbSKonstantin Komarov 	size_t add, new_pack;
270be71b5cbSKonstantin Komarov 	u32 off, size;
271be71b5cbSKonstantin Komarov 	__le16 size_pack;
272be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
273be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le;
274be71b5cbSKonstantin Komarov 	struct mft_inode *mi;
275be71b5cbSKonstantin Komarov 	struct runs_tree ea_run;
276be71b5cbSKonstantin Komarov 	u64 new_sz;
277be71b5cbSKonstantin Komarov 	void *p;
278be71b5cbSKonstantin Komarov 
279be71b5cbSKonstantin Komarov 	ni_lock(ni);
280be71b5cbSKonstantin Komarov 
281be71b5cbSKonstantin Komarov 	run_init(&ea_run);
282be71b5cbSKonstantin Komarov 
283be71b5cbSKonstantin Komarov 	if (name_len > 255) {
284be71b5cbSKonstantin Komarov 		err = -ENAMETOOLONG;
285be71b5cbSKonstantin Komarov 		goto out;
286be71b5cbSKonstantin Komarov 	}
287be71b5cbSKonstantin Komarov 
288fa3cacf5SKari Argillander 	add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
289be71b5cbSKonstantin Komarov 
290be71b5cbSKonstantin Komarov 	err = ntfs_read_ea(ni, &ea_all, add, &info);
291be71b5cbSKonstantin Komarov 	if (err)
292be71b5cbSKonstantin Komarov 		goto out;
293be71b5cbSKonstantin Komarov 
294be71b5cbSKonstantin Komarov 	if (!info) {
295be71b5cbSKonstantin Komarov 		memset(&ea_info, 0, sizeof(ea_info));
296be71b5cbSKonstantin Komarov 		size = 0;
297be71b5cbSKonstantin Komarov 		size_pack = 0;
298be71b5cbSKonstantin Komarov 	} else {
299be71b5cbSKonstantin Komarov 		memcpy(&ea_info, info, sizeof(ea_info));
300be71b5cbSKonstantin Komarov 		size = le32_to_cpu(ea_info.size);
301be71b5cbSKonstantin Komarov 		size_pack = ea_info.size_pack;
302be71b5cbSKonstantin Komarov 	}
303be71b5cbSKonstantin Komarov 
304be71b5cbSKonstantin Komarov 	if (info && find_ea(ea_all, size, name, name_len, &off)) {
305be71b5cbSKonstantin Komarov 		struct EA_FULL *ea;
306be71b5cbSKonstantin Komarov 		size_t ea_sz;
307be71b5cbSKonstantin Komarov 
308be71b5cbSKonstantin Komarov 		if (flags & XATTR_CREATE) {
309be71b5cbSKonstantin Komarov 			err = -EEXIST;
310be71b5cbSKonstantin Komarov 			goto out;
311be71b5cbSKonstantin Komarov 		}
312be71b5cbSKonstantin Komarov 
313be71b5cbSKonstantin Komarov 		ea = Add2Ptr(ea_all, off);
314be71b5cbSKonstantin Komarov 
315be71b5cbSKonstantin Komarov 		/*
316be71b5cbSKonstantin Komarov 		 * Check simple case when we try to insert xattr with the same value
317be71b5cbSKonstantin Komarov 		 * e.g. ntfs_save_wsl_perm
318be71b5cbSKonstantin Komarov 		 */
319be71b5cbSKonstantin Komarov 		if (val_size && le16_to_cpu(ea->elength) == val_size &&
320be71b5cbSKonstantin Komarov 		    !memcmp(ea->name + ea->name_len + 1, value, val_size)) {
321e8b8e97fSKari Argillander 			/* xattr already contains the required value. */
322be71b5cbSKonstantin Komarov 			goto out;
323be71b5cbSKonstantin Komarov 		}
324be71b5cbSKonstantin Komarov 
325e8b8e97fSKari Argillander 		/* Remove current xattr. */
326be71b5cbSKonstantin Komarov 		if (ea->flags & FILE_NEED_EA)
327be71b5cbSKonstantin Komarov 			le16_add_cpu(&ea_info.count, -1);
328be71b5cbSKonstantin Komarov 
329be71b5cbSKonstantin Komarov 		ea_sz = unpacked_ea_size(ea);
330be71b5cbSKonstantin Komarov 
331be71b5cbSKonstantin Komarov 		le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea));
332be71b5cbSKonstantin Komarov 
333be71b5cbSKonstantin Komarov 		memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz);
334be71b5cbSKonstantin Komarov 
335be71b5cbSKonstantin Komarov 		size -= ea_sz;
336be71b5cbSKonstantin Komarov 		memset(Add2Ptr(ea_all, size), 0, ea_sz);
337be71b5cbSKonstantin Komarov 
338be71b5cbSKonstantin Komarov 		ea_info.size = cpu_to_le32(size);
339be71b5cbSKonstantin Komarov 
340be71b5cbSKonstantin Komarov 		if ((flags & XATTR_REPLACE) && !val_size) {
341e8b8e97fSKari Argillander 			/* Remove xattr. */
342be71b5cbSKonstantin Komarov 			goto update_ea;
343be71b5cbSKonstantin Komarov 		}
344be71b5cbSKonstantin Komarov 	} else {
345be71b5cbSKonstantin Komarov 		if (flags & XATTR_REPLACE) {
346be71b5cbSKonstantin Komarov 			err = -ENODATA;
347be71b5cbSKonstantin Komarov 			goto out;
348be71b5cbSKonstantin Komarov 		}
349be71b5cbSKonstantin Komarov 
350be71b5cbSKonstantin Komarov 		if (!ea_all) {
351195c52bdSKari Argillander 			ea_all = kzalloc(add, GFP_NOFS);
352be71b5cbSKonstantin Komarov 			if (!ea_all) {
353be71b5cbSKonstantin Komarov 				err = -ENOMEM;
354be71b5cbSKonstantin Komarov 				goto out;
355be71b5cbSKonstantin Komarov 			}
356be71b5cbSKonstantin Komarov 		}
357be71b5cbSKonstantin Komarov 	}
358be71b5cbSKonstantin Komarov 
359e8b8e97fSKari Argillander 	/* Append new xattr. */
360be71b5cbSKonstantin Komarov 	new_ea = Add2Ptr(ea_all, size);
361be71b5cbSKonstantin Komarov 	new_ea->size = cpu_to_le32(add);
362be71b5cbSKonstantin Komarov 	new_ea->flags = 0;
363be71b5cbSKonstantin Komarov 	new_ea->name_len = name_len;
364be71b5cbSKonstantin Komarov 	new_ea->elength = cpu_to_le16(val_size);
365be71b5cbSKonstantin Komarov 	memcpy(new_ea->name, name, name_len);
366be71b5cbSKonstantin Komarov 	new_ea->name[name_len] = 0;
367be71b5cbSKonstantin Komarov 	memcpy(new_ea->name + name_len + 1, value, val_size);
368be71b5cbSKonstantin Komarov 	new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea);
369be71b5cbSKonstantin Komarov 
370e8b8e97fSKari Argillander 	/* Should fit into 16 bits. */
371be71b5cbSKonstantin Komarov 	if (new_pack > 0xffff) {
372be71b5cbSKonstantin Komarov 		err = -EFBIG; // -EINVAL?
373be71b5cbSKonstantin Komarov 		goto out;
374be71b5cbSKonstantin Komarov 	}
375be71b5cbSKonstantin Komarov 	ea_info.size_pack = cpu_to_le16(new_pack);
376be71b5cbSKonstantin Komarov 
377e8b8e97fSKari Argillander 	/* New size of ATTR_EA. */
378be71b5cbSKonstantin Komarov 	size += add;
379be71b5cbSKonstantin Komarov 	if (size > sbi->ea_max_size) {
380be71b5cbSKonstantin Komarov 		err = -EFBIG; // -EINVAL?
381be71b5cbSKonstantin Komarov 		goto out;
382be71b5cbSKonstantin Komarov 	}
383be71b5cbSKonstantin Komarov 	ea_info.size = cpu_to_le32(size);
384be71b5cbSKonstantin Komarov 
385be71b5cbSKonstantin Komarov update_ea:
386be71b5cbSKonstantin Komarov 
387be71b5cbSKonstantin Komarov 	if (!info) {
388e8b8e97fSKari Argillander 		/* Create xattr. */
389be71b5cbSKonstantin Komarov 		if (!size) {
390be71b5cbSKonstantin Komarov 			err = 0;
391be71b5cbSKonstantin Komarov 			goto out;
392be71b5cbSKonstantin Komarov 		}
393be71b5cbSKonstantin Komarov 
394be71b5cbSKonstantin Komarov 		err = ni_insert_resident(ni, sizeof(struct EA_INFO),
39578ab59feSKonstantin Komarov 					 ATTR_EA_INFO, NULL, 0, NULL, NULL,
39678ab59feSKonstantin Komarov 					 NULL);
397be71b5cbSKonstantin Komarov 		if (err)
398be71b5cbSKonstantin Komarov 			goto out;
399be71b5cbSKonstantin Komarov 
40078ab59feSKonstantin Komarov 		err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL,
40178ab59feSKonstantin Komarov 					 NULL);
402be71b5cbSKonstantin Komarov 		if (err)
403be71b5cbSKonstantin Komarov 			goto out;
404be71b5cbSKonstantin Komarov 	}
405be71b5cbSKonstantin Komarov 
406be71b5cbSKonstantin Komarov 	new_sz = size;
407be71b5cbSKonstantin Komarov 	err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz,
408be71b5cbSKonstantin Komarov 			    false, NULL);
409be71b5cbSKonstantin Komarov 	if (err)
410be71b5cbSKonstantin Komarov 		goto out;
411be71b5cbSKonstantin Komarov 
412be71b5cbSKonstantin Komarov 	le = NULL;
413be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi);
414be71b5cbSKonstantin Komarov 	if (!attr) {
415be71b5cbSKonstantin Komarov 		err = -EINVAL;
416be71b5cbSKonstantin Komarov 		goto out;
417be71b5cbSKonstantin Komarov 	}
418be71b5cbSKonstantin Komarov 
419be71b5cbSKonstantin Komarov 	if (!size) {
420e8b8e97fSKari Argillander 		/* Delete xattr, ATTR_EA_INFO */
42178ab59feSKonstantin Komarov 		ni_remove_attr_le(ni, attr, mi, le);
422be71b5cbSKonstantin Komarov 	} else {
423be71b5cbSKonstantin Komarov 		p = resident_data_ex(attr, sizeof(struct EA_INFO));
424be71b5cbSKonstantin Komarov 		if (!p) {
425be71b5cbSKonstantin Komarov 			err = -EINVAL;
426be71b5cbSKonstantin Komarov 			goto out;
427be71b5cbSKonstantin Komarov 		}
428be71b5cbSKonstantin Komarov 		memcpy(p, &ea_info, sizeof(struct EA_INFO));
429be71b5cbSKonstantin Komarov 		mi->dirty = true;
430be71b5cbSKonstantin Komarov 	}
431be71b5cbSKonstantin Komarov 
432be71b5cbSKonstantin Komarov 	le = NULL;
433be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi);
434be71b5cbSKonstantin Komarov 	if (!attr) {
435be71b5cbSKonstantin Komarov 		err = -EINVAL;
436be71b5cbSKonstantin Komarov 		goto out;
437be71b5cbSKonstantin Komarov 	}
438be71b5cbSKonstantin Komarov 
439be71b5cbSKonstantin Komarov 	if (!size) {
440e8b8e97fSKari Argillander 		/* Delete xattr, ATTR_EA */
44178ab59feSKonstantin Komarov 		ni_remove_attr_le(ni, attr, mi, le);
442be71b5cbSKonstantin Komarov 	} else if (attr->non_res) {
44363544672SKonstantin Komarov 		err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0);
444be71b5cbSKonstantin Komarov 		if (err)
445be71b5cbSKonstantin Komarov 			goto out;
446be71b5cbSKonstantin Komarov 	} else {
447be71b5cbSKonstantin Komarov 		p = resident_data_ex(attr, size);
448be71b5cbSKonstantin Komarov 		if (!p) {
449be71b5cbSKonstantin Komarov 			err = -EINVAL;
450be71b5cbSKonstantin Komarov 			goto out;
451be71b5cbSKonstantin Komarov 		}
452be71b5cbSKonstantin Komarov 		memcpy(p, ea_all, size);
453be71b5cbSKonstantin Komarov 		mi->dirty = true;
454be71b5cbSKonstantin Komarov 	}
455be71b5cbSKonstantin Komarov 
456e8b8e97fSKari Argillander 	/* Check if we delete the last xattr. */
457be71b5cbSKonstantin Komarov 	if (size)
458be71b5cbSKonstantin Komarov 		ni->ni_flags |= NI_FLAG_EA;
459be71b5cbSKonstantin Komarov 	else
460be71b5cbSKonstantin Komarov 		ni->ni_flags &= ~NI_FLAG_EA;
461be71b5cbSKonstantin Komarov 
462be71b5cbSKonstantin Komarov 	if (ea_info.size_pack != size_pack)
463be71b5cbSKonstantin Komarov 		ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
464be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
465be71b5cbSKonstantin Komarov 
466be71b5cbSKonstantin Komarov out:
467be71b5cbSKonstantin Komarov 	ni_unlock(ni);
468be71b5cbSKonstantin Komarov 
469be71b5cbSKonstantin Komarov 	run_close(&ea_run);
470195c52bdSKari Argillander 	kfree(ea_all);
471be71b5cbSKonstantin Komarov 
472be71b5cbSKonstantin Komarov 	return err;
473be71b5cbSKonstantin Komarov }
474be71b5cbSKonstantin Komarov 
475be71b5cbSKonstantin Komarov #ifdef CONFIG_NTFS3_FS_POSIX_ACL
476be71b5cbSKonstantin Komarov static struct posix_acl *ntfs_get_acl_ex(struct user_namespace *mnt_userns,
477be71b5cbSKonstantin Komarov 					 struct inode *inode, int type,
478be71b5cbSKonstantin Komarov 					 int locked)
479be71b5cbSKonstantin Komarov {
480be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
481be71b5cbSKonstantin Komarov 	const char *name;
482be71b5cbSKonstantin Komarov 	size_t name_len;
483be71b5cbSKonstantin Komarov 	struct posix_acl *acl;
484be71b5cbSKonstantin Komarov 	size_t req;
485be71b5cbSKonstantin Komarov 	int err;
486be71b5cbSKonstantin Komarov 	void *buf;
487be71b5cbSKonstantin Komarov 
488e8b8e97fSKari Argillander 	/* Allocate PATH_MAX bytes. */
489be71b5cbSKonstantin Komarov 	buf = __getname();
490be71b5cbSKonstantin Komarov 	if (!buf)
491be71b5cbSKonstantin Komarov 		return ERR_PTR(-ENOMEM);
492be71b5cbSKonstantin Komarov 
493e8b8e97fSKari Argillander 	/* Possible values of 'type' was already checked above. */
494be71b5cbSKonstantin Komarov 	if (type == ACL_TYPE_ACCESS) {
495be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_ACCESS;
496be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
497be71b5cbSKonstantin Komarov 	} else {
498be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_DEFAULT;
499be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
500be71b5cbSKonstantin Komarov 	}
501be71b5cbSKonstantin Komarov 
502be71b5cbSKonstantin Komarov 	if (!locked)
503be71b5cbSKonstantin Komarov 		ni_lock(ni);
504be71b5cbSKonstantin Komarov 
505be71b5cbSKonstantin Komarov 	err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req);
506be71b5cbSKonstantin Komarov 
507be71b5cbSKonstantin Komarov 	if (!locked)
508be71b5cbSKonstantin Komarov 		ni_unlock(ni);
509be71b5cbSKonstantin Komarov 
510e8b8e97fSKari Argillander 	/* Translate extended attribute to acl. */
5112926e429SDan Carpenter 	if (err >= 0) {
512be71b5cbSKonstantin Komarov 		acl = posix_acl_from_xattr(mnt_userns, buf, err);
5130bd5fdb8SKonstantin Komarov 	} else if (err == -ENODATA) {
5140bd5fdb8SKonstantin Komarov 		acl = NULL;
5150bd5fdb8SKonstantin Komarov 	} else {
5160bd5fdb8SKonstantin Komarov 		acl = ERR_PTR(err);
5170bd5fdb8SKonstantin Komarov 	}
5180bd5fdb8SKonstantin Komarov 
519be71b5cbSKonstantin Komarov 	if (!IS_ERR(acl))
520be71b5cbSKonstantin Komarov 		set_cached_acl(inode, type, acl);
521be71b5cbSKonstantin Komarov 
522be71b5cbSKonstantin Komarov 	__putname(buf);
523be71b5cbSKonstantin Komarov 
524be71b5cbSKonstantin Komarov 	return acl;
525be71b5cbSKonstantin Komarov }
526be71b5cbSKonstantin Komarov 
527be71b5cbSKonstantin Komarov /*
528e8b8e97fSKari Argillander  * ntfs_get_acl - inode_operations::get_acl
529be71b5cbSKonstantin Komarov  */
530be71b5cbSKonstantin Komarov struct posix_acl *ntfs_get_acl(struct inode *inode, int type)
531be71b5cbSKonstantin Komarov {
532be71b5cbSKonstantin Komarov 	/* TODO: init_user_ns? */
533be71b5cbSKonstantin Komarov 	return ntfs_get_acl_ex(&init_user_ns, inode, type, 0);
534be71b5cbSKonstantin Komarov }
535be71b5cbSKonstantin Komarov 
536be71b5cbSKonstantin Komarov static noinline int ntfs_set_acl_ex(struct user_namespace *mnt_userns,
537be71b5cbSKonstantin Komarov 				    struct inode *inode, struct posix_acl *acl,
538*d81e06beSKonstantin Komarov 				    int type)
539be71b5cbSKonstantin Komarov {
540be71b5cbSKonstantin Komarov 	const char *name;
541be71b5cbSKonstantin Komarov 	size_t size, name_len;
542be71b5cbSKonstantin Komarov 	void *value = NULL;
543be71b5cbSKonstantin Komarov 	int err = 0;
544398c35f4SKonstantin Komarov 	int flags;
545be71b5cbSKonstantin Komarov 
546be71b5cbSKonstantin Komarov 	if (S_ISLNK(inode->i_mode))
547be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
548be71b5cbSKonstantin Komarov 
549be71b5cbSKonstantin Komarov 	switch (type) {
550be71b5cbSKonstantin Komarov 	case ACL_TYPE_ACCESS:
551be71b5cbSKonstantin Komarov 		if (acl) {
552be71b5cbSKonstantin Komarov 			umode_t mode = inode->i_mode;
553be71b5cbSKonstantin Komarov 
554ba77237eSKonstantin Komarov 			err = posix_acl_update_mode(mnt_userns, inode, &mode,
555ba77237eSKonstantin Komarov 						    &acl);
556ba77237eSKonstantin Komarov 			if (err)
557ba77237eSKonstantin Komarov 				goto out;
558be71b5cbSKonstantin Komarov 
559be71b5cbSKonstantin Komarov 			if (inode->i_mode != mode) {
560be71b5cbSKonstantin Komarov 				inode->i_mode = mode;
561be71b5cbSKonstantin Komarov 				mark_inode_dirty(inode);
562be71b5cbSKonstantin Komarov 			}
563be71b5cbSKonstantin Komarov 		}
564be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_ACCESS;
565be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
566be71b5cbSKonstantin Komarov 		break;
567be71b5cbSKonstantin Komarov 
568be71b5cbSKonstantin Komarov 	case ACL_TYPE_DEFAULT:
569be71b5cbSKonstantin Komarov 		if (!S_ISDIR(inode->i_mode))
570be71b5cbSKonstantin Komarov 			return acl ? -EACCES : 0;
571be71b5cbSKonstantin Komarov 		name = XATTR_NAME_POSIX_ACL_DEFAULT;
572be71b5cbSKonstantin Komarov 		name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
573be71b5cbSKonstantin Komarov 		break;
574be71b5cbSKonstantin Komarov 
575be71b5cbSKonstantin Komarov 	default:
576be71b5cbSKonstantin Komarov 		return -EINVAL;
577be71b5cbSKonstantin Komarov 	}
578be71b5cbSKonstantin Komarov 
579be71b5cbSKonstantin Komarov 	if (!acl) {
580398c35f4SKonstantin Komarov 		/* Remove xattr if it can be presented via mode. */
581be71b5cbSKonstantin Komarov 		size = 0;
582be71b5cbSKonstantin Komarov 		value = NULL;
583398c35f4SKonstantin Komarov 		flags = XATTR_REPLACE;
584be71b5cbSKonstantin Komarov 	} else {
585be71b5cbSKonstantin Komarov 		size = posix_acl_xattr_size(acl->a_count);
586195c52bdSKari Argillander 		value = kmalloc(size, GFP_NOFS);
587be71b5cbSKonstantin Komarov 		if (!value)
588be71b5cbSKonstantin Komarov 			return -ENOMEM;
589be71b5cbSKonstantin Komarov 		err = posix_acl_to_xattr(mnt_userns, acl, value, size);
590be71b5cbSKonstantin Komarov 		if (err < 0)
591be71b5cbSKonstantin Komarov 			goto out;
592398c35f4SKonstantin Komarov 		flags = 0;
593be71b5cbSKonstantin Komarov 	}
594be71b5cbSKonstantin Komarov 
595*d81e06beSKonstantin Komarov 	err = ntfs_set_ea(inode, name, name_len, value, size, flags);
596398c35f4SKonstantin Komarov 	if (err == -ENODATA && !size)
597398c35f4SKonstantin Komarov 		err = 0; /* Removing non existed xattr. */
598be71b5cbSKonstantin Komarov 	if (!err)
599be71b5cbSKonstantin Komarov 		set_cached_acl(inode, type, acl);
600be71b5cbSKonstantin Komarov 
601be71b5cbSKonstantin Komarov out:
602195c52bdSKari Argillander 	kfree(value);
603be71b5cbSKonstantin Komarov 
604be71b5cbSKonstantin Komarov 	return err;
605be71b5cbSKonstantin Komarov }
606be71b5cbSKonstantin Komarov 
607be71b5cbSKonstantin Komarov /*
608e8b8e97fSKari Argillander  * ntfs_set_acl - inode_operations::set_acl
609be71b5cbSKonstantin Komarov  */
610be71b5cbSKonstantin Komarov int ntfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
611be71b5cbSKonstantin Komarov 		 struct posix_acl *acl, int type)
612be71b5cbSKonstantin Komarov {
613*d81e06beSKonstantin Komarov 	return ntfs_set_acl_ex(mnt_userns, inode, acl, type);
614be71b5cbSKonstantin Komarov }
615be71b5cbSKonstantin Komarov 
616be71b5cbSKonstantin Komarov static int ntfs_xattr_get_acl(struct user_namespace *mnt_userns,
617be71b5cbSKonstantin Komarov 			      struct inode *inode, int type, void *buffer,
618be71b5cbSKonstantin Komarov 			      size_t size)
619be71b5cbSKonstantin Komarov {
620be71b5cbSKonstantin Komarov 	struct posix_acl *acl;
621be71b5cbSKonstantin Komarov 	int err;
622be71b5cbSKonstantin Komarov 
62378ab59feSKonstantin Komarov 	if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
62478ab59feSKonstantin Komarov 		ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
625be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
62678ab59feSKonstantin Komarov 	}
627be71b5cbSKonstantin Komarov 
628be71b5cbSKonstantin Komarov 	acl = ntfs_get_acl(inode, type);
629be71b5cbSKonstantin Komarov 	if (IS_ERR(acl))
630be71b5cbSKonstantin Komarov 		return PTR_ERR(acl);
631be71b5cbSKonstantin Komarov 
632be71b5cbSKonstantin Komarov 	if (!acl)
633be71b5cbSKonstantin Komarov 		return -ENODATA;
634be71b5cbSKonstantin Komarov 
635be71b5cbSKonstantin Komarov 	err = posix_acl_to_xattr(mnt_userns, acl, buffer, size);
636b1e0c55aSKonstantin Komarov 	posix_acl_release(acl);
637be71b5cbSKonstantin Komarov 
638be71b5cbSKonstantin Komarov 	return err;
639be71b5cbSKonstantin Komarov }
640be71b5cbSKonstantin Komarov 
641be71b5cbSKonstantin Komarov static int ntfs_xattr_set_acl(struct user_namespace *mnt_userns,
642be71b5cbSKonstantin Komarov 			      struct inode *inode, int type, const void *value,
643be71b5cbSKonstantin Komarov 			      size_t size)
644be71b5cbSKonstantin Komarov {
645be71b5cbSKonstantin Komarov 	struct posix_acl *acl;
646be71b5cbSKonstantin Komarov 	int err;
647be71b5cbSKonstantin Komarov 
64878ab59feSKonstantin Komarov 	if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
64978ab59feSKonstantin Komarov 		ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
650be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
65178ab59feSKonstantin Komarov 	}
652be71b5cbSKonstantin Komarov 
653be71b5cbSKonstantin Komarov 	if (!inode_owner_or_capable(mnt_userns, inode))
654be71b5cbSKonstantin Komarov 		return -EPERM;
655be71b5cbSKonstantin Komarov 
656be71b5cbSKonstantin Komarov 	if (!value) {
657be71b5cbSKonstantin Komarov 		acl = NULL;
658be71b5cbSKonstantin Komarov 	} else {
659be71b5cbSKonstantin Komarov 		acl = posix_acl_from_xattr(mnt_userns, value, size);
660be71b5cbSKonstantin Komarov 		if (IS_ERR(acl))
661be71b5cbSKonstantin Komarov 			return PTR_ERR(acl);
662be71b5cbSKonstantin Komarov 
663be71b5cbSKonstantin Komarov 		if (acl) {
664be71b5cbSKonstantin Komarov 			err = posix_acl_valid(mnt_userns, acl);
665be71b5cbSKonstantin Komarov 			if (err)
666be71b5cbSKonstantin Komarov 				goto release_and_out;
667be71b5cbSKonstantin Komarov 		}
668be71b5cbSKonstantin Komarov 	}
669be71b5cbSKonstantin Komarov 
670be71b5cbSKonstantin Komarov 	err = ntfs_set_acl(mnt_userns, inode, acl, type);
671be71b5cbSKonstantin Komarov 
672be71b5cbSKonstantin Komarov release_and_out:
673b1e0c55aSKonstantin Komarov 	posix_acl_release(acl);
674be71b5cbSKonstantin Komarov 	return err;
675be71b5cbSKonstantin Komarov }
676be71b5cbSKonstantin Komarov 
677be71b5cbSKonstantin Komarov /*
678e8b8e97fSKari Argillander  * ntfs_init_acl - Initialize the ACLs of a new inode.
679e8b8e97fSKari Argillander  *
680e8b8e97fSKari Argillander  * Called from ntfs_create_inode().
681be71b5cbSKonstantin Komarov  */
682be71b5cbSKonstantin Komarov int ntfs_init_acl(struct user_namespace *mnt_userns, struct inode *inode,
683be71b5cbSKonstantin Komarov 		  struct inode *dir)
684be71b5cbSKonstantin Komarov {
685be71b5cbSKonstantin Komarov 	struct posix_acl *default_acl, *acl;
686be71b5cbSKonstantin Komarov 	int err;
687be71b5cbSKonstantin Komarov 
68866019837SKonstantin Komarov 	err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
68966019837SKonstantin Komarov 	if (err)
69066019837SKonstantin Komarov 		return err;
691be71b5cbSKonstantin Komarov 
69266019837SKonstantin Komarov 	if (default_acl) {
693be71b5cbSKonstantin Komarov 		err = ntfs_set_acl_ex(mnt_userns, inode, default_acl,
694*d81e06beSKonstantin Komarov 				      ACL_TYPE_DEFAULT);
69566019837SKonstantin Komarov 		posix_acl_release(default_acl);
69666019837SKonstantin Komarov 	} else {
69766019837SKonstantin Komarov 		inode->i_default_acl = NULL;
69866019837SKonstantin Komarov 	}
699be71b5cbSKonstantin Komarov 
700be71b5cbSKonstantin Komarov 	if (!acl)
701be71b5cbSKonstantin Komarov 		inode->i_acl = NULL;
70266019837SKonstantin Komarov 	else {
70366019837SKonstantin Komarov 		if (!err)
70466019837SKonstantin Komarov 			err = ntfs_set_acl_ex(mnt_userns, inode, acl,
705*d81e06beSKonstantin Komarov 					      ACL_TYPE_ACCESS);
706be71b5cbSKonstantin Komarov 		posix_acl_release(acl);
70766019837SKonstantin Komarov 	}
708be71b5cbSKonstantin Komarov 
709be71b5cbSKonstantin Komarov 	return err;
710be71b5cbSKonstantin Komarov }
711be71b5cbSKonstantin Komarov #endif
712be71b5cbSKonstantin Komarov 
713be71b5cbSKonstantin Komarov /*
714e8b8e97fSKari Argillander  * ntfs_acl_chmod - Helper for ntfs3_setattr().
715be71b5cbSKonstantin Komarov  */
716be71b5cbSKonstantin Komarov int ntfs_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode)
717be71b5cbSKonstantin Komarov {
718be71b5cbSKonstantin Komarov 	struct super_block *sb = inode->i_sb;
719be71b5cbSKonstantin Komarov 
720be71b5cbSKonstantin Komarov 	if (!(sb->s_flags & SB_POSIXACL))
721be71b5cbSKonstantin Komarov 		return 0;
722be71b5cbSKonstantin Komarov 
723be71b5cbSKonstantin Komarov 	if (S_ISLNK(inode->i_mode))
724be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
725be71b5cbSKonstantin Komarov 
726be71b5cbSKonstantin Komarov 	return posix_acl_chmod(mnt_userns, inode, inode->i_mode);
727be71b5cbSKonstantin Komarov }
728be71b5cbSKonstantin Komarov 
729be71b5cbSKonstantin Komarov /*
730e8b8e97fSKari Argillander  * ntfs_permission - inode_operations::permission
731be71b5cbSKonstantin Komarov  */
732be71b5cbSKonstantin Komarov int ntfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
733be71b5cbSKonstantin Komarov 		    int mask)
734be71b5cbSKonstantin Komarov {
73528a941ffSKari Argillander 	if (ntfs_sb(inode->i_sb)->options->noacsrules) {
736e8b8e97fSKari Argillander 		/* "No access rules" mode - Allow all changes. */
737be71b5cbSKonstantin Komarov 		return 0;
738be71b5cbSKonstantin Komarov 	}
739be71b5cbSKonstantin Komarov 
740be71b5cbSKonstantin Komarov 	return generic_permission(mnt_userns, inode, mask);
741be71b5cbSKonstantin Komarov }
742be71b5cbSKonstantin Komarov 
743be71b5cbSKonstantin Komarov /*
744e8b8e97fSKari Argillander  * ntfs_listxattr - inode_operations::listxattr
745be71b5cbSKonstantin Komarov  */
746be71b5cbSKonstantin Komarov ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
747be71b5cbSKonstantin Komarov {
748be71b5cbSKonstantin Komarov 	struct inode *inode = d_inode(dentry);
749be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
750be71b5cbSKonstantin Komarov 	ssize_t ret;
751be71b5cbSKonstantin Komarov 
752be71b5cbSKonstantin Komarov 	if (!(ni->ni_flags & NI_FLAG_EA)) {
753be71b5cbSKonstantin Komarov 		/* no xattr in file */
754be71b5cbSKonstantin Komarov 		return 0;
755be71b5cbSKonstantin Komarov 	}
756be71b5cbSKonstantin Komarov 
757be71b5cbSKonstantin Komarov 	ni_lock(ni);
758be71b5cbSKonstantin Komarov 
759be71b5cbSKonstantin Komarov 	ret = ntfs_list_ea(ni, buffer, size);
760be71b5cbSKonstantin Komarov 
761be71b5cbSKonstantin Komarov 	ni_unlock(ni);
762be71b5cbSKonstantin Komarov 
763be71b5cbSKonstantin Komarov 	return ret;
764be71b5cbSKonstantin Komarov }
765be71b5cbSKonstantin Komarov 
766be71b5cbSKonstantin Komarov static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
767be71b5cbSKonstantin Komarov 			 struct inode *inode, const char *name, void *buffer,
768be71b5cbSKonstantin Komarov 			 size_t size)
769be71b5cbSKonstantin Komarov {
770be71b5cbSKonstantin Komarov 	int err;
771be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
772be71b5cbSKonstantin Komarov 	size_t name_len = strlen(name);
773be71b5cbSKonstantin Komarov 
774e8b8e97fSKari Argillander 	/* Dispatch request. */
775be71b5cbSKonstantin Komarov 	if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
776be71b5cbSKonstantin Komarov 	    !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
777be71b5cbSKonstantin Komarov 		/* system.dos_attrib */
778be71b5cbSKonstantin Komarov 		if (!buffer) {
779be71b5cbSKonstantin Komarov 			err = sizeof(u8);
780be71b5cbSKonstantin Komarov 		} else if (size < sizeof(u8)) {
781be71b5cbSKonstantin Komarov 			err = -ENODATA;
782be71b5cbSKonstantin Komarov 		} else {
783be71b5cbSKonstantin Komarov 			err = sizeof(u8);
784be71b5cbSKonstantin Komarov 			*(u8 *)buffer = le32_to_cpu(ni->std_fa);
785be71b5cbSKonstantin Komarov 		}
786be71b5cbSKonstantin Komarov 		goto out;
787be71b5cbSKonstantin Komarov 	}
788be71b5cbSKonstantin Komarov 
789be71b5cbSKonstantin Komarov 	if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
790be71b5cbSKonstantin Komarov 	    !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
791be71b5cbSKonstantin Komarov 		/* system.ntfs_attrib */
792be71b5cbSKonstantin Komarov 		if (!buffer) {
793be71b5cbSKonstantin Komarov 			err = sizeof(u32);
794be71b5cbSKonstantin Komarov 		} else if (size < sizeof(u32)) {
795be71b5cbSKonstantin Komarov 			err = -ENODATA;
796be71b5cbSKonstantin Komarov 		} else {
797be71b5cbSKonstantin Komarov 			err = sizeof(u32);
798be71b5cbSKonstantin Komarov 			*(u32 *)buffer = le32_to_cpu(ni->std_fa);
799be71b5cbSKonstantin Komarov 		}
800be71b5cbSKonstantin Komarov 		goto out;
801be71b5cbSKonstantin Komarov 	}
802be71b5cbSKonstantin Komarov 
803be71b5cbSKonstantin Komarov 	if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
804be71b5cbSKonstantin Komarov 	    !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
805be71b5cbSKonstantin Komarov 		/* system.ntfs_security*/
806be71b5cbSKonstantin Komarov 		struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
807be71b5cbSKonstantin Komarov 		size_t sd_size = 0;
808be71b5cbSKonstantin Komarov 
809be71b5cbSKonstantin Komarov 		if (!is_ntfs3(ni->mi.sbi)) {
810e8b8e97fSKari Argillander 			/* We should get nt4 security. */
811be71b5cbSKonstantin Komarov 			err = -EINVAL;
812be71b5cbSKonstantin Komarov 			goto out;
813be71b5cbSKonstantin Komarov 		} else if (le32_to_cpu(ni->std_security_id) <
814be71b5cbSKonstantin Komarov 			   SECURITY_ID_FIRST) {
815be71b5cbSKonstantin Komarov 			err = -ENOENT;
816be71b5cbSKonstantin Komarov 			goto out;
817be71b5cbSKonstantin Komarov 		}
818be71b5cbSKonstantin Komarov 
819be71b5cbSKonstantin Komarov 		err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
820be71b5cbSKonstantin Komarov 					      &sd, &sd_size);
821be71b5cbSKonstantin Komarov 		if (err)
822be71b5cbSKonstantin Komarov 			goto out;
823be71b5cbSKonstantin Komarov 
824be71b5cbSKonstantin Komarov 		if (!is_sd_valid(sd, sd_size)) {
825be71b5cbSKonstantin Komarov 			ntfs_inode_warn(
826be71b5cbSKonstantin Komarov 				inode,
827be71b5cbSKonstantin Komarov 				"looks like you get incorrect security descriptor id=%u",
828be71b5cbSKonstantin Komarov 				ni->std_security_id);
829be71b5cbSKonstantin Komarov 		}
830be71b5cbSKonstantin Komarov 
831be71b5cbSKonstantin Komarov 		if (!buffer) {
832be71b5cbSKonstantin Komarov 			err = sd_size;
833be71b5cbSKonstantin Komarov 		} else if (size < sd_size) {
834be71b5cbSKonstantin Komarov 			err = -ENODATA;
835be71b5cbSKonstantin Komarov 		} else {
836be71b5cbSKonstantin Komarov 			err = sd_size;
837be71b5cbSKonstantin Komarov 			memcpy(buffer, sd, sd_size);
838be71b5cbSKonstantin Komarov 		}
839195c52bdSKari Argillander 		kfree(sd);
840be71b5cbSKonstantin Komarov 		goto out;
841be71b5cbSKonstantin Komarov 	}
842be71b5cbSKonstantin Komarov 
843be71b5cbSKonstantin Komarov #ifdef CONFIG_NTFS3_FS_POSIX_ACL
844be71b5cbSKonstantin Komarov 	if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
845be71b5cbSKonstantin Komarov 	     !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
846be71b5cbSKonstantin Komarov 		     sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
847be71b5cbSKonstantin Komarov 	    (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
848be71b5cbSKonstantin Komarov 	     !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
849be71b5cbSKonstantin Komarov 		     sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
850be71b5cbSKonstantin Komarov 		/* TODO: init_user_ns? */
851be71b5cbSKonstantin Komarov 		err = ntfs_xattr_get_acl(
852be71b5cbSKonstantin Komarov 			&init_user_ns, inode,
853be71b5cbSKonstantin Komarov 			name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
854be71b5cbSKonstantin Komarov 				? ACL_TYPE_ACCESS
855be71b5cbSKonstantin Komarov 				: ACL_TYPE_DEFAULT,
856be71b5cbSKonstantin Komarov 			buffer, size);
857be71b5cbSKonstantin Komarov 		goto out;
858be71b5cbSKonstantin Komarov 	}
859be71b5cbSKonstantin Komarov #endif
860e8b8e97fSKari Argillander 	/* Deal with NTFS extended attribute. */
861be71b5cbSKonstantin Komarov 	err = ntfs_get_ea(inode, name, name_len, buffer, size, NULL);
862be71b5cbSKonstantin Komarov 
863be71b5cbSKonstantin Komarov out:
864be71b5cbSKonstantin Komarov 	return err;
865be71b5cbSKonstantin Komarov }
866be71b5cbSKonstantin Komarov 
867be71b5cbSKonstantin Komarov /*
868e8b8e97fSKari Argillander  * ntfs_setxattr - inode_operations::setxattr
869be71b5cbSKonstantin Komarov  */
870be71b5cbSKonstantin Komarov static noinline int ntfs_setxattr(const struct xattr_handler *handler,
871be71b5cbSKonstantin Komarov 				  struct user_namespace *mnt_userns,
872be71b5cbSKonstantin Komarov 				  struct dentry *de, struct inode *inode,
873be71b5cbSKonstantin Komarov 				  const char *name, const void *value,
874be71b5cbSKonstantin Komarov 				  size_t size, int flags)
875be71b5cbSKonstantin Komarov {
876be71b5cbSKonstantin Komarov 	int err = -EINVAL;
877be71b5cbSKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
878be71b5cbSKonstantin Komarov 	size_t name_len = strlen(name);
879be71b5cbSKonstantin Komarov 	enum FILE_ATTRIBUTE new_fa;
880be71b5cbSKonstantin Komarov 
881e8b8e97fSKari Argillander 	/* Dispatch request. */
882be71b5cbSKonstantin Komarov 	if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
883be71b5cbSKonstantin Komarov 	    !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
884be71b5cbSKonstantin Komarov 		if (sizeof(u8) != size)
885be71b5cbSKonstantin Komarov 			goto out;
886be71b5cbSKonstantin Komarov 		new_fa = cpu_to_le32(*(u8 *)value);
887be71b5cbSKonstantin Komarov 		goto set_new_fa;
888be71b5cbSKonstantin Komarov 	}
889be71b5cbSKonstantin Komarov 
890be71b5cbSKonstantin Komarov 	if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
891be71b5cbSKonstantin Komarov 	    !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
892be71b5cbSKonstantin Komarov 		if (size != sizeof(u32))
893be71b5cbSKonstantin Komarov 			goto out;
894be71b5cbSKonstantin Komarov 		new_fa = cpu_to_le32(*(u32 *)value);
895be71b5cbSKonstantin Komarov 
896be71b5cbSKonstantin Komarov 		if (S_ISREG(inode->i_mode)) {
897e8b8e97fSKari Argillander 			/* Process compressed/sparsed in special way. */
898be71b5cbSKonstantin Komarov 			ni_lock(ni);
899be71b5cbSKonstantin Komarov 			err = ni_new_attr_flags(ni, new_fa);
900be71b5cbSKonstantin Komarov 			ni_unlock(ni);
901be71b5cbSKonstantin Komarov 			if (err)
902be71b5cbSKonstantin Komarov 				goto out;
903be71b5cbSKonstantin Komarov 		}
904be71b5cbSKonstantin Komarov set_new_fa:
905be71b5cbSKonstantin Komarov 		/*
906be71b5cbSKonstantin Komarov 		 * Thanks Mark Harmstone:
907e8b8e97fSKari Argillander 		 * Keep directory bit consistency.
908be71b5cbSKonstantin Komarov 		 */
909be71b5cbSKonstantin Komarov 		if (S_ISDIR(inode->i_mode))
910be71b5cbSKonstantin Komarov 			new_fa |= FILE_ATTRIBUTE_DIRECTORY;
911be71b5cbSKonstantin Komarov 		else
912be71b5cbSKonstantin Komarov 			new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
913be71b5cbSKonstantin Komarov 
914be71b5cbSKonstantin Komarov 		if (ni->std_fa != new_fa) {
915be71b5cbSKonstantin Komarov 			ni->std_fa = new_fa;
916be71b5cbSKonstantin Komarov 			if (new_fa & FILE_ATTRIBUTE_READONLY)
917be71b5cbSKonstantin Komarov 				inode->i_mode &= ~0222;
918be71b5cbSKonstantin Komarov 			else
919be71b5cbSKonstantin Komarov 				inode->i_mode |= 0222;
920e8b8e97fSKari Argillander 			/* Std attribute always in primary record. */
921be71b5cbSKonstantin Komarov 			ni->mi.dirty = true;
922be71b5cbSKonstantin Komarov 			mark_inode_dirty(inode);
923be71b5cbSKonstantin Komarov 		}
924be71b5cbSKonstantin Komarov 		err = 0;
925be71b5cbSKonstantin Komarov 
926be71b5cbSKonstantin Komarov 		goto out;
927be71b5cbSKonstantin Komarov 	}
928be71b5cbSKonstantin Komarov 
929be71b5cbSKonstantin Komarov 	if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
930be71b5cbSKonstantin Komarov 	    !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
931be71b5cbSKonstantin Komarov 		/* system.ntfs_security*/
932be71b5cbSKonstantin Komarov 		__le32 security_id;
933be71b5cbSKonstantin Komarov 		bool inserted;
934be71b5cbSKonstantin Komarov 		struct ATTR_STD_INFO5 *std;
935be71b5cbSKonstantin Komarov 
936be71b5cbSKonstantin Komarov 		if (!is_ntfs3(ni->mi.sbi)) {
937be71b5cbSKonstantin Komarov 			/*
938e8b8e97fSKari Argillander 			 * We should replace ATTR_SECURE.
939e8b8e97fSKari Argillander 			 * Skip this way cause it is nt4 feature.
940be71b5cbSKonstantin Komarov 			 */
941be71b5cbSKonstantin Komarov 			err = -EINVAL;
942be71b5cbSKonstantin Komarov 			goto out;
943be71b5cbSKonstantin Komarov 		}
944be71b5cbSKonstantin Komarov 
945be71b5cbSKonstantin Komarov 		if (!is_sd_valid(value, size)) {
946be71b5cbSKonstantin Komarov 			err = -EINVAL;
947be71b5cbSKonstantin Komarov 			ntfs_inode_warn(
948be71b5cbSKonstantin Komarov 				inode,
949be71b5cbSKonstantin Komarov 				"you try to set invalid security descriptor");
950be71b5cbSKonstantin Komarov 			goto out;
951be71b5cbSKonstantin Komarov 		}
952be71b5cbSKonstantin Komarov 
953be71b5cbSKonstantin Komarov 		err = ntfs_insert_security(ni->mi.sbi, value, size,
954be71b5cbSKonstantin Komarov 					   &security_id, &inserted);
955be71b5cbSKonstantin Komarov 		if (err)
956be71b5cbSKonstantin Komarov 			goto out;
957be71b5cbSKonstantin Komarov 
958be71b5cbSKonstantin Komarov 		ni_lock(ni);
959be71b5cbSKonstantin Komarov 		std = ni_std5(ni);
960be71b5cbSKonstantin Komarov 		if (!std) {
961be71b5cbSKonstantin Komarov 			err = -EINVAL;
962be71b5cbSKonstantin Komarov 		} else if (std->security_id != security_id) {
963be71b5cbSKonstantin Komarov 			std->security_id = ni->std_security_id = security_id;
964e8b8e97fSKari Argillander 			/* Std attribute always in primary record. */
965be71b5cbSKonstantin Komarov 			ni->mi.dirty = true;
966be71b5cbSKonstantin Komarov 			mark_inode_dirty(&ni->vfs_inode);
967be71b5cbSKonstantin Komarov 		}
968be71b5cbSKonstantin Komarov 		ni_unlock(ni);
969be71b5cbSKonstantin Komarov 		goto out;
970be71b5cbSKonstantin Komarov 	}
971be71b5cbSKonstantin Komarov 
972be71b5cbSKonstantin Komarov #ifdef CONFIG_NTFS3_FS_POSIX_ACL
973be71b5cbSKonstantin Komarov 	if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
974be71b5cbSKonstantin Komarov 	     !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
975be71b5cbSKonstantin Komarov 		     sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
976be71b5cbSKonstantin Komarov 	    (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
977be71b5cbSKonstantin Komarov 	     !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
978be71b5cbSKonstantin Komarov 		     sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
979be71b5cbSKonstantin Komarov 		err = ntfs_xattr_set_acl(
980be71b5cbSKonstantin Komarov 			mnt_userns, inode,
981be71b5cbSKonstantin Komarov 			name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
982be71b5cbSKonstantin Komarov 				? ACL_TYPE_ACCESS
983be71b5cbSKonstantin Komarov 				: ACL_TYPE_DEFAULT,
984be71b5cbSKonstantin Komarov 			value, size);
985be71b5cbSKonstantin Komarov 		goto out;
986be71b5cbSKonstantin Komarov 	}
987be71b5cbSKonstantin Komarov #endif
988e8b8e97fSKari Argillander 	/* Deal with NTFS extended attribute. */
989*d81e06beSKonstantin Komarov 	err = ntfs_set_ea(inode, name, name_len, value, size, flags);
990be71b5cbSKonstantin Komarov 
991be71b5cbSKonstantin Komarov out:
992be71b5cbSKonstantin Komarov 	return err;
993be71b5cbSKonstantin Komarov }
994be71b5cbSKonstantin Komarov 
995be71b5cbSKonstantin Komarov /*
996be71b5cbSKonstantin Komarov  * ntfs_save_wsl_perm
997be71b5cbSKonstantin Komarov  *
998be71b5cbSKonstantin Komarov  * save uid/gid/mode in xattr
999be71b5cbSKonstantin Komarov  */
1000be71b5cbSKonstantin Komarov int ntfs_save_wsl_perm(struct inode *inode)
1001be71b5cbSKonstantin Komarov {
1002be71b5cbSKonstantin Komarov 	int err;
1003be71b5cbSKonstantin Komarov 	__le32 value;
1004be71b5cbSKonstantin Komarov 
1005*d81e06beSKonstantin Komarov 	/* TODO: refactor this, so we don't lock 4 times in ntfs_set_ea */
1006be71b5cbSKonstantin Komarov 	value = cpu_to_le32(i_uid_read(inode));
1007be71b5cbSKonstantin Komarov 	err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
1008*d81e06beSKonstantin Komarov 			  sizeof(value), 0);
1009be71b5cbSKonstantin Komarov 	if (err)
1010be71b5cbSKonstantin Komarov 		goto out;
1011be71b5cbSKonstantin Komarov 
1012be71b5cbSKonstantin Komarov 	value = cpu_to_le32(i_gid_read(inode));
1013be71b5cbSKonstantin Komarov 	err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
1014*d81e06beSKonstantin Komarov 			  sizeof(value), 0);
1015be71b5cbSKonstantin Komarov 	if (err)
1016be71b5cbSKonstantin Komarov 		goto out;
1017be71b5cbSKonstantin Komarov 
1018be71b5cbSKonstantin Komarov 	value = cpu_to_le32(inode->i_mode);
1019be71b5cbSKonstantin Komarov 	err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
1020*d81e06beSKonstantin Komarov 			  sizeof(value), 0);
1021be71b5cbSKonstantin Komarov 	if (err)
1022be71b5cbSKonstantin Komarov 		goto out;
1023be71b5cbSKonstantin Komarov 
1024be71b5cbSKonstantin Komarov 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1025be71b5cbSKonstantin Komarov 		value = cpu_to_le32(inode->i_rdev);
1026be71b5cbSKonstantin Komarov 		err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
1027*d81e06beSKonstantin Komarov 				  sizeof(value), 0);
1028be71b5cbSKonstantin Komarov 		if (err)
1029be71b5cbSKonstantin Komarov 			goto out;
1030be71b5cbSKonstantin Komarov 	}
1031be71b5cbSKonstantin Komarov 
1032be71b5cbSKonstantin Komarov out:
1033be71b5cbSKonstantin Komarov 	/* In case of error should we delete all WSL xattr? */
1034be71b5cbSKonstantin Komarov 	return err;
1035be71b5cbSKonstantin Komarov }
1036be71b5cbSKonstantin Komarov 
1037be71b5cbSKonstantin Komarov /*
1038be71b5cbSKonstantin Komarov  * ntfs_get_wsl_perm
1039be71b5cbSKonstantin Komarov  *
1040be71b5cbSKonstantin Komarov  * get uid/gid/mode from xattr
1041be71b5cbSKonstantin Komarov  * it is called from ntfs_iget5->ntfs_read_mft
1042be71b5cbSKonstantin Komarov  */
1043be71b5cbSKonstantin Komarov void ntfs_get_wsl_perm(struct inode *inode)
1044be71b5cbSKonstantin Komarov {
1045be71b5cbSKonstantin Komarov 	size_t sz;
1046be71b5cbSKonstantin Komarov 	__le32 value[3];
1047be71b5cbSKonstantin Komarov 
1048be71b5cbSKonstantin Komarov 	if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
1049be71b5cbSKonstantin Komarov 			sizeof(value[0]), &sz) == sizeof(value[0]) &&
1050be71b5cbSKonstantin Komarov 	    ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
1051be71b5cbSKonstantin Komarov 			sizeof(value[1]), &sz) == sizeof(value[1]) &&
1052be71b5cbSKonstantin Komarov 	    ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
1053be71b5cbSKonstantin Komarov 			sizeof(value[2]), &sz) == sizeof(value[2])) {
1054be71b5cbSKonstantin Komarov 		i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
1055be71b5cbSKonstantin Komarov 		i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
1056be71b5cbSKonstantin Komarov 		inode->i_mode = le32_to_cpu(value[2]);
1057be71b5cbSKonstantin Komarov 
1058be71b5cbSKonstantin Komarov 		if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
1059be71b5cbSKonstantin Komarov 				&value[0], sizeof(value),
1060be71b5cbSKonstantin Komarov 				&sz) == sizeof(value[0])) {
1061be71b5cbSKonstantin Komarov 			inode->i_rdev = le32_to_cpu(value[0]);
1062be71b5cbSKonstantin Komarov 		}
1063be71b5cbSKonstantin Komarov 	}
1064be71b5cbSKonstantin Komarov }
1065be71b5cbSKonstantin Komarov 
1066be71b5cbSKonstantin Komarov static bool ntfs_xattr_user_list(struct dentry *dentry)
1067be71b5cbSKonstantin Komarov {
1068be71b5cbSKonstantin Komarov 	return true;
1069be71b5cbSKonstantin Komarov }
1070be71b5cbSKonstantin Komarov 
1071be71b5cbSKonstantin Komarov // clang-format off
1072be71b5cbSKonstantin Komarov static const struct xattr_handler ntfs_xattr_handler = {
1073be71b5cbSKonstantin Komarov 	.prefix	= "",
1074be71b5cbSKonstantin Komarov 	.get	= ntfs_getxattr,
1075be71b5cbSKonstantin Komarov 	.set	= ntfs_setxattr,
1076be71b5cbSKonstantin Komarov 	.list	= ntfs_xattr_user_list,
1077be71b5cbSKonstantin Komarov };
1078be71b5cbSKonstantin Komarov 
1079be71b5cbSKonstantin Komarov const struct xattr_handler *ntfs_xattr_handlers[] = {
1080be71b5cbSKonstantin Komarov 	&ntfs_xattr_handler,
1081be71b5cbSKonstantin Komarov 	NULL,
1082be71b5cbSKonstantin Komarov };
1083be71b5cbSKonstantin Komarov // clang-format on
1084