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