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