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