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