fs/ntfs3: Implement super_operations::shutdown
[linux-block.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_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 (buffer) {
223                         /* Check if we can use field ea->name */
224                         if (off + ea_size > size)
225                                 break;
226
227                         if (ret + ea->name_len + 1 > bytes_per_buffer) {
228                                 err = -ERANGE;
229                                 goto out;
230                         }
231
232                         memcpy(buffer + ret, ea->name, ea->name_len);
233                         buffer[ret + ea->name_len] = 0;
234                 }
235
236                 ret += ea->name_len + 1;
237         }
238
239 out:
240         kfree(ea_all);
241         return err ? err : ret;
242 }
243
244 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
245                        void *buffer, size_t size, size_t *required)
246 {
247         struct ntfs_inode *ni = ntfs_i(inode);
248         const struct EA_INFO *info;
249         struct EA_FULL *ea_all = NULL;
250         const struct EA_FULL *ea;
251         u32 off, len;
252         int err;
253
254         if (!(ni->ni_flags & NI_FLAG_EA))
255                 return -ENODATA;
256
257         if (!required)
258                 ni_lock(ni);
259
260         len = 0;
261
262         if (name_len > 255) {
263                 err = -ENAMETOOLONG;
264                 goto out;
265         }
266
267         err = ntfs_read_ea(ni, &ea_all, 0, &info);
268         if (err)
269                 goto out;
270
271         if (!info)
272                 goto out;
273
274         /* Enumerate all xattrs. */
275         if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off,
276                      NULL)) {
277                 err = -ENODATA;
278                 goto out;
279         }
280         ea = Add2Ptr(ea_all, off);
281
282         len = le16_to_cpu(ea->elength);
283         if (!buffer) {
284                 err = 0;
285                 goto out;
286         }
287
288         if (len > size) {
289                 err = -ERANGE;
290                 if (required)
291                         *required = len;
292                 goto out;
293         }
294
295         memcpy(buffer, ea->name + ea->name_len + 1, len);
296         err = 0;
297
298 out:
299         kfree(ea_all);
300         if (!required)
301                 ni_unlock(ni);
302
303         return err ? err : len;
304 }
305
306 static noinline int ntfs_set_ea(struct inode *inode, const char *name,
307                                 size_t name_len, const void *value,
308                                 size_t val_size, int flags, bool locked,
309                                 __le16 *ea_size)
310 {
311         struct ntfs_inode *ni = ntfs_i(inode);
312         struct ntfs_sb_info *sbi = ni->mi.sbi;
313         int err;
314         struct EA_INFO ea_info;
315         const struct EA_INFO *info;
316         struct EA_FULL *new_ea;
317         struct EA_FULL *ea_all = NULL;
318         size_t add, new_pack;
319         u32 off, size, ea_sz;
320         __le16 size_pack;
321         struct ATTRIB *attr;
322         struct ATTR_LIST_ENTRY *le;
323         struct mft_inode *mi;
324         struct runs_tree ea_run;
325         u64 new_sz;
326         void *p;
327
328         if (!locked)
329                 ni_lock(ni);
330
331         run_init(&ea_run);
332
333         if (name_len > 255) {
334                 err = -ENAMETOOLONG;
335                 goto out;
336         }
337
338         add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
339
340         err = ntfs_read_ea(ni, &ea_all, add, &info);
341         if (err)
342                 goto out;
343
344         if (!info) {
345                 memset(&ea_info, 0, sizeof(ea_info));
346                 size = 0;
347                 size_pack = 0;
348         } else {
349                 memcpy(&ea_info, info, sizeof(ea_info));
350                 size = le32_to_cpu(ea_info.size);
351                 size_pack = ea_info.size_pack;
352         }
353
354         if (info && find_ea(ea_all, size, name, name_len, &off, &ea_sz)) {
355                 struct EA_FULL *ea;
356
357                 if (flags & XATTR_CREATE) {
358                         err = -EEXIST;
359                         goto out;
360                 }
361
362                 ea = Add2Ptr(ea_all, off);
363
364                 /*
365                  * Check simple case when we try to insert xattr with the same value
366                  * e.g. ntfs_save_wsl_perm
367                  */
368                 if (val_size && le16_to_cpu(ea->elength) == val_size &&
369                     !memcmp(ea->name + ea->name_len + 1, value, val_size)) {
370                         /* xattr already contains the required value. */
371                         goto out;
372                 }
373
374                 /* Remove current xattr. */
375                 if (ea->flags & FILE_NEED_EA)
376                         le16_add_cpu(&ea_info.count, -1);
377
378                 le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea));
379
380                 memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz);
381
382                 size -= ea_sz;
383                 memset(Add2Ptr(ea_all, size), 0, ea_sz);
384
385                 ea_info.size = cpu_to_le32(size);
386
387                 if ((flags & XATTR_REPLACE) && !val_size) {
388                         /* Remove xattr. */
389                         goto update_ea;
390                 }
391         } else {
392                 if (flags & XATTR_REPLACE) {
393                         err = -ENODATA;
394                         goto out;
395                 }
396
397                 if (!ea_all) {
398                         ea_all = kzalloc(add, GFP_NOFS);
399                         if (!ea_all) {
400                                 err = -ENOMEM;
401                                 goto out;
402                         }
403                 }
404         }
405
406         /* Append new xattr. */
407         new_ea = Add2Ptr(ea_all, size);
408         new_ea->size = cpu_to_le32(add);
409         new_ea->flags = 0;
410         new_ea->name_len = name_len;
411         new_ea->elength = cpu_to_le16(val_size);
412         memcpy(new_ea->name, name, name_len);
413         new_ea->name[name_len] = 0;
414         memcpy(new_ea->name + name_len + 1, value, val_size);
415         new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea);
416         ea_info.size_pack = cpu_to_le16(new_pack);
417         /* New size of ATTR_EA. */
418         size += add;
419         ea_info.size = cpu_to_le32(size);
420
421         /*
422          * 1. Check ea_info.size_pack for overflow.
423          * 2. New attribute size must fit value from $AttrDef
424          */
425         if (new_pack > 0xffff || size > sbi->ea_max_size) {
426                 ntfs_inode_warn(
427                         inode,
428                         "The size of extended attributes must not exceed 64KiB");
429                 err = -EFBIG; // -EINVAL?
430                 goto out;
431         }
432
433 update_ea:
434
435         if (!info) {
436                 /* Create xattr. */
437                 if (!size) {
438                         err = 0;
439                         goto out;
440                 }
441
442                 err = ni_insert_resident(ni, sizeof(struct EA_INFO),
443                                          ATTR_EA_INFO, NULL, 0, NULL, NULL,
444                                          NULL);
445                 if (err)
446                         goto out;
447
448                 err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL,
449                                          NULL);
450                 if (err)
451                         goto out;
452         }
453
454         new_sz = size;
455         err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz,
456                             false, NULL);
457         if (err)
458                 goto out;
459
460         le = NULL;
461         attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi);
462         if (!attr) {
463                 err = -EINVAL;
464                 goto out;
465         }
466
467         if (!size) {
468                 /* Delete xattr, ATTR_EA_INFO */
469                 ni_remove_attr_le(ni, attr, mi, le);
470         } else {
471                 p = resident_data_ex(attr, sizeof(struct EA_INFO));
472                 if (!p) {
473                         err = -EINVAL;
474                         goto out;
475                 }
476                 memcpy(p, &ea_info, sizeof(struct EA_INFO));
477                 mi->dirty = true;
478         }
479
480         le = NULL;
481         attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi);
482         if (!attr) {
483                 err = -EINVAL;
484                 goto out;
485         }
486
487         if (!size) {
488                 /* Delete xattr, ATTR_EA */
489                 ni_remove_attr_le(ni, attr, mi, le);
490         } else if (attr->non_res) {
491                 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &ea_run, 0,
492                                            size);
493                 if (err)
494                         goto out;
495
496                 err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0);
497                 if (err)
498                         goto out;
499         } else {
500                 p = resident_data_ex(attr, size);
501                 if (!p) {
502                         err = -EINVAL;
503                         goto out;
504                 }
505                 memcpy(p, ea_all, size);
506                 mi->dirty = true;
507         }
508
509         /* Check if we delete the last xattr. */
510         if (size)
511                 ni->ni_flags |= NI_FLAG_EA;
512         else
513                 ni->ni_flags &= ~NI_FLAG_EA;
514
515         if (ea_info.size_pack != size_pack)
516                 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
517         if (ea_size)
518                 *ea_size = ea_info.size_pack;
519         mark_inode_dirty(&ni->vfs_inode);
520
521 out:
522         if (!locked)
523                 ni_unlock(ni);
524
525         run_close(&ea_run);
526         kfree(ea_all);
527
528         return err;
529 }
530
531 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
532
533 /*
534  * ntfs_get_acl - inode_operations::get_acl
535  */
536 struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
537                                int type)
538 {
539         struct inode *inode = d_inode(dentry);
540         struct ntfs_inode *ni = ntfs_i(inode);
541         const char *name;
542         size_t name_len;
543         struct posix_acl *acl;
544         size_t req;
545         int err;
546         void *buf;
547
548         /* Allocate PATH_MAX bytes. */
549         buf = __getname();
550         if (!buf)
551                 return ERR_PTR(-ENOMEM);
552
553         /* Possible values of 'type' was already checked above. */
554         if (type == ACL_TYPE_ACCESS) {
555                 name = XATTR_NAME_POSIX_ACL_ACCESS;
556                 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
557         } else {
558                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
559                 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
560         }
561
562         ni_lock(ni);
563
564         err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req);
565
566         ni_unlock(ni);
567
568         /* Translate extended attribute to acl. */
569         if (err >= 0) {
570                 acl = posix_acl_from_xattr(&init_user_ns, buf, err);
571         } else if (err == -ENODATA) {
572                 acl = NULL;
573         } else {
574                 acl = ERR_PTR(err);
575         }
576
577         if (!IS_ERR(acl))
578                 set_cached_acl(inode, type, acl);
579
580         __putname(buf);
581
582         return acl;
583 }
584
585 static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap,
586                                     struct inode *inode, struct posix_acl *acl,
587                                     int type, bool init_acl)
588 {
589         const char *name;
590         size_t size, name_len;
591         void *value;
592         int err;
593         int flags;
594         umode_t mode;
595
596         if (S_ISLNK(inode->i_mode))
597                 return -EOPNOTSUPP;
598
599         mode = inode->i_mode;
600         switch (type) {
601         case ACL_TYPE_ACCESS:
602                 /* Do not change i_mode if we are in init_acl */
603                 if (acl && !init_acl) {
604                         err = posix_acl_update_mode(idmap, inode, &mode, &acl);
605                         if (err)
606                                 return err;
607                 }
608                 name = XATTR_NAME_POSIX_ACL_ACCESS;
609                 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
610                 break;
611
612         case ACL_TYPE_DEFAULT:
613                 if (!S_ISDIR(inode->i_mode))
614                         return acl ? -EACCES : 0;
615                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
616                 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
617                 break;
618
619         default:
620                 return -EINVAL;
621         }
622
623         if (!acl) {
624                 /* Remove xattr if it can be presented via mode. */
625                 size = 0;
626                 value = NULL;
627                 flags = XATTR_REPLACE;
628         } else {
629                 size = posix_acl_xattr_size(acl->a_count);
630                 value = kmalloc(size, GFP_NOFS);
631                 if (!value)
632                         return -ENOMEM;
633                 err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
634                 if (err < 0)
635                         goto out;
636                 flags = 0;
637         }
638
639         err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0, NULL);
640         if (err == -ENODATA && !size)
641                 err = 0; /* Removing non existed xattr. */
642         if (!err) {
643                 set_cached_acl(inode, type, acl);
644                 inode->i_mode = mode;
645                 inode_set_ctime_current(inode);
646                 mark_inode_dirty(inode);
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 mnt_idmap *idmap, struct dentry *dentry,
659                  struct posix_acl *acl, int type)
660 {
661         return ntfs_set_acl_ex(idmap, d_inode(dentry), 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 mnt_idmap *idmap, 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(idmap, 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(idmap, 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 mnt_idmap *idmap, struct dentry *dentry)
704 {
705         struct inode *inode = d_inode(dentry);
706         struct super_block *sb = inode->i_sb;
707
708         if (!(sb->s_flags & SB_POSIXACL))
709                 return 0;
710
711         if (S_ISLNK(inode->i_mode))
712                 return -EOPNOTSUPP;
713
714         return posix_acl_chmod(idmap, dentry, inode->i_mode);
715 }
716
717 /*
718  * ntfs_listxattr - inode_operations::listxattr
719  */
720 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
721 {
722         struct inode *inode = d_inode(dentry);
723         struct ntfs_inode *ni = ntfs_i(inode);
724         ssize_t ret;
725
726         if (!(ni->ni_flags & NI_FLAG_EA)) {
727                 /* no xattr in file */
728                 return 0;
729         }
730
731         ni_lock(ni);
732
733         ret = ntfs_list_ea(ni, buffer, size);
734
735         ni_unlock(ni);
736
737         return ret;
738 }
739
740 static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
741                          struct inode *inode, const char *name, void *buffer,
742                          size_t size)
743 {
744         int err;
745         struct ntfs_inode *ni = ntfs_i(inode);
746
747         if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
748                 return -EIO;
749
750         /* Dispatch request. */
751         if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
752                 /* system.dos_attrib */
753                 if (!buffer) {
754                         err = sizeof(u8);
755                 } else if (size < sizeof(u8)) {
756                         err = -ENODATA;
757                 } else {
758                         err = sizeof(u8);
759                         *(u8 *)buffer = le32_to_cpu(ni->std_fa);
760                 }
761                 goto out;
762         }
763
764         if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
765             !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
766                 /* system.ntfs_attrib */
767                 if (!buffer) {
768                         err = sizeof(u32);
769                 } else if (size < sizeof(u32)) {
770                         err = -ENODATA;
771                 } else {
772                         err = sizeof(u32);
773                         *(u32 *)buffer = le32_to_cpu(ni->std_fa);
774                         if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
775                                 *(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer);
776                 }
777                 goto out;
778         }
779
780         if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
781                 /* system.ntfs_security*/
782                 struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
783                 size_t sd_size = 0;
784
785                 if (!is_ntfs3(ni->mi.sbi)) {
786                         /* We should get nt4 security. */
787                         err = -EINVAL;
788                         goto out;
789                 } else if (le32_to_cpu(ni->std_security_id) <
790                            SECURITY_ID_FIRST) {
791                         err = -ENOENT;
792                         goto out;
793                 }
794
795                 err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
796                                               &sd, &sd_size);
797                 if (err)
798                         goto out;
799
800                 if (!is_sd_valid(sd, sd_size)) {
801                         ntfs_inode_warn(
802                                 inode,
803                                 "looks like you get incorrect security descriptor id=%u",
804                                 ni->std_security_id);
805                 }
806
807                 if (!buffer) {
808                         err = sd_size;
809                 } else if (size < sd_size) {
810                         err = -ENODATA;
811                 } else {
812                         err = sd_size;
813                         memcpy(buffer, sd, sd_size);
814                 }
815                 kfree(sd);
816                 goto out;
817         }
818
819         /* Deal with NTFS extended attribute. */
820         err = ntfs_get_ea(inode, name, strlen(name), buffer, size, NULL);
821
822 out:
823         return err;
824 }
825
826 /*
827  * ntfs_setxattr - inode_operations::setxattr
828  */
829 static noinline int ntfs_setxattr(const struct xattr_handler *handler,
830                                   struct mnt_idmap *idmap, struct dentry *de,
831                                   struct inode *inode, const char *name,
832                                   const void *value, size_t size, int flags)
833 {
834         int err = -EINVAL;
835         struct ntfs_inode *ni = ntfs_i(inode);
836         enum FILE_ATTRIBUTE new_fa;
837
838         /* Dispatch request. */
839         if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
840                 if (sizeof(u8) != size)
841                         goto out;
842                 new_fa = cpu_to_le32(*(u8 *)value);
843                 goto set_new_fa;
844         }
845
846         if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
847             !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
848                 if (size != sizeof(u32))
849                         goto out;
850                 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
851                         new_fa = cpu_to_le32(be32_to_cpu(*(__be32 *)value));
852                 else
853                         new_fa = cpu_to_le32(*(u32 *)value);
854
855                 if (S_ISREG(inode->i_mode)) {
856                         /* Process compressed/sparsed in special way. */
857                         ni_lock(ni);
858                         err = ni_new_attr_flags(ni, new_fa);
859                         ni_unlock(ni);
860                         if (err)
861                                 goto out;
862                 }
863 set_new_fa:
864                 /*
865                  * Thanks Mark Harmstone:
866                  * Keep directory bit consistency.
867                  */
868                 if (S_ISDIR(inode->i_mode))
869                         new_fa |= FILE_ATTRIBUTE_DIRECTORY;
870                 else
871                         new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
872
873                 if (ni->std_fa != new_fa) {
874                         ni->std_fa = new_fa;
875                         if (new_fa & FILE_ATTRIBUTE_READONLY)
876                                 inode->i_mode &= ~0222;
877                         else
878                                 inode->i_mode |= 0222;
879                         /* Std attribute always in primary record. */
880                         ni->mi.dirty = true;
881                         mark_inode_dirty(inode);
882                 }
883                 err = 0;
884
885                 goto out;
886         }
887
888         if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
889                 /* system.ntfs_security*/
890                 __le32 security_id;
891                 bool inserted;
892                 struct ATTR_STD_INFO5 *std;
893
894                 if (!is_ntfs3(ni->mi.sbi)) {
895                         /*
896                          * We should replace ATTR_SECURE.
897                          * Skip this way cause it is nt4 feature.
898                          */
899                         err = -EINVAL;
900                         goto out;
901                 }
902
903                 if (!is_sd_valid(value, size)) {
904                         err = -EINVAL;
905                         ntfs_inode_warn(
906                                 inode,
907                                 "you try to set invalid security descriptor");
908                         goto out;
909                 }
910
911                 err = ntfs_insert_security(ni->mi.sbi, value, size,
912                                            &security_id, &inserted);
913                 if (err)
914                         goto out;
915
916                 ni_lock(ni);
917                 std = ni_std5(ni);
918                 if (!std) {
919                         err = -EINVAL;
920                 } else if (std->security_id != security_id) {
921                         std->security_id = ni->std_security_id = security_id;
922                         /* Std attribute always in primary record. */
923                         ni->mi.dirty = true;
924                         mark_inode_dirty(&ni->vfs_inode);
925                 }
926                 ni_unlock(ni);
927                 goto out;
928         }
929
930         /* Deal with NTFS extended attribute. */
931         err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0,
932                           NULL);
933
934 out:
935         inode_set_ctime_current(inode);
936         mark_inode_dirty(inode);
937
938         return err;
939 }
940
941 /*
942  * ntfs_save_wsl_perm
943  *
944  * save uid/gid/mode in xattr
945  */
946 int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size)
947 {
948         int err;
949         __le32 value;
950         struct ntfs_inode *ni = ntfs_i(inode);
951
952         ni_lock(ni);
953         value = cpu_to_le32(i_uid_read(inode));
954         err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
955                           sizeof(value), 0, true, ea_size);
956         if (err)
957                 goto out;
958
959         value = cpu_to_le32(i_gid_read(inode));
960         err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
961                           sizeof(value), 0, true, ea_size);
962         if (err)
963                 goto out;
964
965         value = cpu_to_le32(inode->i_mode);
966         err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
967                           sizeof(value), 0, true, ea_size);
968         if (err)
969                 goto out;
970
971         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
972                 value = cpu_to_le32(inode->i_rdev);
973                 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
974                                   sizeof(value), 0, true, ea_size);
975                 if (err)
976                         goto out;
977         }
978
979 out:
980         ni_unlock(ni);
981         /* In case of error should we delete all WSL xattr? */
982         return err;
983 }
984
985 /*
986  * ntfs_get_wsl_perm
987  *
988  * get uid/gid/mode from xattr
989  * it is called from ntfs_iget5->ntfs_read_mft
990  */
991 void ntfs_get_wsl_perm(struct inode *inode)
992 {
993         size_t sz;
994         __le32 value[3];
995
996         if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
997                         sizeof(value[0]), &sz) == sizeof(value[0]) &&
998             ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
999                         sizeof(value[1]), &sz) == sizeof(value[1]) &&
1000             ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
1001                         sizeof(value[2]), &sz) == sizeof(value[2])) {
1002                 i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
1003                 i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
1004                 inode->i_mode = le32_to_cpu(value[2]);
1005
1006                 if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
1007                                 &value[0], sizeof(value),
1008                                 &sz) == sizeof(value[0])) {
1009                         inode->i_rdev = le32_to_cpu(value[0]);
1010                 }
1011         }
1012 }
1013
1014 static bool ntfs_xattr_user_list(struct dentry *dentry)
1015 {
1016         return true;
1017 }
1018
1019 // clang-format off
1020 static const struct xattr_handler ntfs_other_xattr_handler = {
1021         .prefix = "",
1022         .get    = ntfs_getxattr,
1023         .set    = ntfs_setxattr,
1024         .list   = ntfs_xattr_user_list,
1025 };
1026
1027 const struct xattr_handler * const ntfs_xattr_handlers[] = {
1028         &ntfs_other_xattr_handler,
1029         NULL,
1030 };
1031 // clang-format on