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