fs/ntfs3: Change how module init/info messages are displayed
[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)
539 {
540         /* TODO: init_user_ns? */
541         return ntfs_get_acl_ex(&init_user_ns, inode, type, 0);
542 }
543
544 static noinline int ntfs_set_acl_ex(struct user_namespace *mnt_userns,
545                                     struct inode *inode, struct posix_acl *acl,
546                                     int type, int locked)
547 {
548         const char *name;
549         size_t size, name_len;
550         void *value = NULL;
551         int err = 0;
552
553         if (S_ISLNK(inode->i_mode))
554                 return -EOPNOTSUPP;
555
556         switch (type) {
557         case ACL_TYPE_ACCESS:
558                 if (acl) {
559                         umode_t mode = inode->i_mode;
560
561                         err = posix_acl_equiv_mode(acl, &mode);
562                         if (err < 0)
563                                 return err;
564
565                         if (inode->i_mode != mode) {
566                                 inode->i_mode = mode;
567                                 mark_inode_dirty(inode);
568                         }
569
570                         if (!err) {
571                                 /*
572                                  * ACL can be exactly represented in the
573                                  * traditional file mode permission bits.
574                                  */
575                                 acl = NULL;
576                         }
577                 }
578                 name = XATTR_NAME_POSIX_ACL_ACCESS;
579                 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
580                 break;
581
582         case ACL_TYPE_DEFAULT:
583                 if (!S_ISDIR(inode->i_mode))
584                         return acl ? -EACCES : 0;
585                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
586                 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
587                 break;
588
589         default:
590                 return -EINVAL;
591         }
592
593         if (!acl) {
594                 size = 0;
595                 value = NULL;
596         } else {
597                 size = posix_acl_xattr_size(acl->a_count);
598                 value = kmalloc(size, GFP_NOFS);
599                 if (!value)
600                         return -ENOMEM;
601
602                 err = posix_acl_to_xattr(mnt_userns, acl, value, size);
603                 if (err < 0)
604                         goto out;
605         }
606
607         err = ntfs_set_ea(inode, name, name_len, value, size, 0, locked);
608         if (!err)
609                 set_cached_acl(inode, type, acl);
610
611 out:
612         kfree(value);
613
614         return err;
615 }
616
617 /*
618  * ntfs_set_acl - inode_operations::set_acl
619  */
620 int ntfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
621                  struct posix_acl *acl, int type)
622 {
623         return ntfs_set_acl_ex(mnt_userns, inode, acl, type, 0);
624 }
625
626 static int ntfs_xattr_get_acl(struct user_namespace *mnt_userns,
627                               struct inode *inode, int type, void *buffer,
628                               size_t size)
629 {
630         struct posix_acl *acl;
631         int err;
632
633         if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
634                 ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
635                 return -EOPNOTSUPP;
636         }
637
638         acl = ntfs_get_acl(inode, type);
639         if (IS_ERR(acl))
640                 return PTR_ERR(acl);
641
642         if (!acl)
643                 return -ENODATA;
644
645         err = posix_acl_to_xattr(mnt_userns, acl, buffer, size);
646         ntfs_posix_acl_release(acl);
647
648         return err;
649 }
650
651 static int ntfs_xattr_set_acl(struct user_namespace *mnt_userns,
652                               struct inode *inode, int type, const void *value,
653                               size_t size)
654 {
655         struct posix_acl *acl;
656         int err;
657
658         if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
659                 ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
660                 return -EOPNOTSUPP;
661         }
662
663         if (!inode_owner_or_capable(mnt_userns, inode))
664                 return -EPERM;
665
666         if (!value) {
667                 acl = NULL;
668         } else {
669                 acl = posix_acl_from_xattr(mnt_userns, value, size);
670                 if (IS_ERR(acl))
671                         return PTR_ERR(acl);
672
673                 if (acl) {
674                         err = posix_acl_valid(mnt_userns, acl);
675                         if (err)
676                                 goto release_and_out;
677                 }
678         }
679
680         err = ntfs_set_acl(mnt_userns, inode, acl, type);
681
682 release_and_out:
683         ntfs_posix_acl_release(acl);
684         return err;
685 }
686
687 /*
688  * ntfs_init_acl - Initialize the ACLs of a new inode.
689  *
690  * Called from ntfs_create_inode().
691  */
692 int ntfs_init_acl(struct user_namespace *mnt_userns, struct inode *inode,
693                   struct inode *dir)
694 {
695         struct posix_acl *default_acl, *acl;
696         int err;
697
698         /*
699          * TODO: Refactoring lock.
700          * ni_lock(dir) ... -> posix_acl_create(dir,...) -> ntfs_get_acl -> ni_lock(dir)
701          */
702         inode->i_default_acl = NULL;
703
704         default_acl = ntfs_get_acl_ex(mnt_userns, dir, ACL_TYPE_DEFAULT, 1);
705
706         if (!default_acl || default_acl == ERR_PTR(-EOPNOTSUPP)) {
707                 inode->i_mode &= ~current_umask();
708                 err = 0;
709                 goto out;
710         }
711
712         if (IS_ERR(default_acl)) {
713                 err = PTR_ERR(default_acl);
714                 goto out;
715         }
716
717         acl = default_acl;
718         err = __posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
719         if (err < 0)
720                 goto out1;
721         if (!err) {
722                 posix_acl_release(acl);
723                 acl = NULL;
724         }
725
726         if (!S_ISDIR(inode->i_mode)) {
727                 posix_acl_release(default_acl);
728                 default_acl = NULL;
729         }
730
731         if (default_acl)
732                 err = ntfs_set_acl_ex(mnt_userns, inode, default_acl,
733                                       ACL_TYPE_DEFAULT, 1);
734
735         if (!acl)
736                 inode->i_acl = NULL;
737         else if (!err)
738                 err = ntfs_set_acl_ex(mnt_userns, inode, acl, ACL_TYPE_ACCESS,
739                                       1);
740
741         posix_acl_release(acl);
742 out1:
743         posix_acl_release(default_acl);
744
745 out:
746         return err;
747 }
748 #endif
749
750 /*
751  * ntfs_acl_chmod - Helper for ntfs3_setattr().
752  */
753 int ntfs_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode)
754 {
755         struct super_block *sb = inode->i_sb;
756
757         if (!(sb->s_flags & SB_POSIXACL))
758                 return 0;
759
760         if (S_ISLNK(inode->i_mode))
761                 return -EOPNOTSUPP;
762
763         return posix_acl_chmod(mnt_userns, inode, inode->i_mode);
764 }
765
766 /*
767  * ntfs_permission - inode_operations::permission
768  */
769 int ntfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
770                     int mask)
771 {
772         if (ntfs_sb(inode->i_sb)->options.no_acs_rules) {
773                 /* "No access rules" mode - Allow all changes. */
774                 return 0;
775         }
776
777         return generic_permission(mnt_userns, inode, mask);
778 }
779
780 /*
781  * ntfs_listxattr - inode_operations::listxattr
782  */
783 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
784 {
785         struct inode *inode = d_inode(dentry);
786         struct ntfs_inode *ni = ntfs_i(inode);
787         ssize_t ret;
788
789         if (!(ni->ni_flags & NI_FLAG_EA)) {
790                 /* no xattr in file */
791                 return 0;
792         }
793
794         ni_lock(ni);
795
796         ret = ntfs_list_ea(ni, buffer, size);
797
798         ni_unlock(ni);
799
800         return ret;
801 }
802
803 static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
804                          struct inode *inode, const char *name, void *buffer,
805                          size_t size)
806 {
807         int err;
808         struct ntfs_inode *ni = ntfs_i(inode);
809         size_t name_len = strlen(name);
810
811         /* Dispatch request. */
812         if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
813             !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
814                 /* system.dos_attrib */
815                 if (!buffer) {
816                         err = sizeof(u8);
817                 } else if (size < sizeof(u8)) {
818                         err = -ENODATA;
819                 } else {
820                         err = sizeof(u8);
821                         *(u8 *)buffer = le32_to_cpu(ni->std_fa);
822                 }
823                 goto out;
824         }
825
826         if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
827             !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
828                 /* system.ntfs_attrib */
829                 if (!buffer) {
830                         err = sizeof(u32);
831                 } else if (size < sizeof(u32)) {
832                         err = -ENODATA;
833                 } else {
834                         err = sizeof(u32);
835                         *(u32 *)buffer = le32_to_cpu(ni->std_fa);
836                 }
837                 goto out;
838         }
839
840         if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
841             !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
842                 /* system.ntfs_security*/
843                 struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
844                 size_t sd_size = 0;
845
846                 if (!is_ntfs3(ni->mi.sbi)) {
847                         /* We should get nt4 security. */
848                         err = -EINVAL;
849                         goto out;
850                 } else if (le32_to_cpu(ni->std_security_id) <
851                            SECURITY_ID_FIRST) {
852                         err = -ENOENT;
853                         goto out;
854                 }
855
856                 err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
857                                               &sd, &sd_size);
858                 if (err)
859                         goto out;
860
861                 if (!is_sd_valid(sd, sd_size)) {
862                         ntfs_inode_warn(
863                                 inode,
864                                 "looks like you get incorrect security descriptor id=%u",
865                                 ni->std_security_id);
866                 }
867
868                 if (!buffer) {
869                         err = sd_size;
870                 } else if (size < sd_size) {
871                         err = -ENODATA;
872                 } else {
873                         err = sd_size;
874                         memcpy(buffer, sd, sd_size);
875                 }
876                 kfree(sd);
877                 goto out;
878         }
879
880 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
881         if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
882              !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
883                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
884             (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
885              !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
886                      sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
887                 /* TODO: init_user_ns? */
888                 err = ntfs_xattr_get_acl(
889                         &init_user_ns, inode,
890                         name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
891                                 ? ACL_TYPE_ACCESS
892                                 : ACL_TYPE_DEFAULT,
893                         buffer, size);
894                 goto out;
895         }
896 #endif
897         /* Deal with NTFS extended attribute. */
898         err = ntfs_get_ea(inode, name, name_len, buffer, size, NULL);
899
900 out:
901         return err;
902 }
903
904 /*
905  * ntfs_setxattr - inode_operations::setxattr
906  */
907 static noinline int ntfs_setxattr(const struct xattr_handler *handler,
908                                   struct user_namespace *mnt_userns,
909                                   struct dentry *de, struct inode *inode,
910                                   const char *name, const void *value,
911                                   size_t size, int flags)
912 {
913         int err = -EINVAL;
914         struct ntfs_inode *ni = ntfs_i(inode);
915         size_t name_len = strlen(name);
916         enum FILE_ATTRIBUTE new_fa;
917
918         /* Dispatch request. */
919         if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
920             !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
921                 if (sizeof(u8) != size)
922                         goto out;
923                 new_fa = cpu_to_le32(*(u8 *)value);
924                 goto set_new_fa;
925         }
926
927         if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
928             !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
929                 if (size != sizeof(u32))
930                         goto out;
931                 new_fa = cpu_to_le32(*(u32 *)value);
932
933                 if (S_ISREG(inode->i_mode)) {
934                         /* Process compressed/sparsed in special way. */
935                         ni_lock(ni);
936                         err = ni_new_attr_flags(ni, new_fa);
937                         ni_unlock(ni);
938                         if (err)
939                                 goto out;
940                 }
941 set_new_fa:
942                 /*
943                  * Thanks Mark Harmstone:
944                  * Keep directory bit consistency.
945                  */
946                 if (S_ISDIR(inode->i_mode))
947                         new_fa |= FILE_ATTRIBUTE_DIRECTORY;
948                 else
949                         new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
950
951                 if (ni->std_fa != new_fa) {
952                         ni->std_fa = new_fa;
953                         if (new_fa & FILE_ATTRIBUTE_READONLY)
954                                 inode->i_mode &= ~0222;
955                         else
956                                 inode->i_mode |= 0222;
957                         /* Std attribute always in primary record. */
958                         ni->mi.dirty = true;
959                         mark_inode_dirty(inode);
960                 }
961                 err = 0;
962
963                 goto out;
964         }
965
966         if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
967             !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
968                 /* system.ntfs_security*/
969                 __le32 security_id;
970                 bool inserted;
971                 struct ATTR_STD_INFO5 *std;
972
973                 if (!is_ntfs3(ni->mi.sbi)) {
974                         /*
975                          * We should replace ATTR_SECURE.
976                          * Skip this way cause it is nt4 feature.
977                          */
978                         err = -EINVAL;
979                         goto out;
980                 }
981
982                 if (!is_sd_valid(value, size)) {
983                         err = -EINVAL;
984                         ntfs_inode_warn(
985                                 inode,
986                                 "you try to set invalid security descriptor");
987                         goto out;
988                 }
989
990                 err = ntfs_insert_security(ni->mi.sbi, value, size,
991                                            &security_id, &inserted);
992                 if (err)
993                         goto out;
994
995                 ni_lock(ni);
996                 std = ni_std5(ni);
997                 if (!std) {
998                         err = -EINVAL;
999                 } else if (std->security_id != security_id) {
1000                         std->security_id = ni->std_security_id = security_id;
1001                         /* Std attribute always in primary record. */
1002                         ni->mi.dirty = true;
1003                         mark_inode_dirty(&ni->vfs_inode);
1004                 }
1005                 ni_unlock(ni);
1006                 goto out;
1007         }
1008
1009 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1010         if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
1011              !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1012                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
1013             (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
1014              !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1015                      sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
1016                 err = ntfs_xattr_set_acl(
1017                         mnt_userns, inode,
1018                         name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
1019                                 ? ACL_TYPE_ACCESS
1020                                 : ACL_TYPE_DEFAULT,
1021                         value, size);
1022                 goto out;
1023         }
1024 #endif
1025         /* Deal with NTFS extended attribute. */
1026         err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0);
1027
1028 out:
1029         return err;
1030 }
1031
1032 /*
1033  * ntfs_save_wsl_perm
1034  *
1035  * save uid/gid/mode in xattr
1036  */
1037 int ntfs_save_wsl_perm(struct inode *inode)
1038 {
1039         int err;
1040         __le32 value;
1041
1042         value = cpu_to_le32(i_uid_read(inode));
1043         err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
1044                           sizeof(value), 0, 0);
1045         if (err)
1046                 goto out;
1047
1048         value = cpu_to_le32(i_gid_read(inode));
1049         err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
1050                           sizeof(value), 0, 0);
1051         if (err)
1052                 goto out;
1053
1054         value = cpu_to_le32(inode->i_mode);
1055         err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
1056                           sizeof(value), 0, 0);
1057         if (err)
1058                 goto out;
1059
1060         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1061                 value = cpu_to_le32(inode->i_rdev);
1062                 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
1063                                   sizeof(value), 0, 0);
1064                 if (err)
1065                         goto out;
1066         }
1067
1068 out:
1069         /* In case of error should we delete all WSL xattr? */
1070         return err;
1071 }
1072
1073 /*
1074  * ntfs_get_wsl_perm
1075  *
1076  * get uid/gid/mode from xattr
1077  * it is called from ntfs_iget5->ntfs_read_mft
1078  */
1079 void ntfs_get_wsl_perm(struct inode *inode)
1080 {
1081         size_t sz;
1082         __le32 value[3];
1083
1084         if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
1085                         sizeof(value[0]), &sz) == sizeof(value[0]) &&
1086             ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
1087                         sizeof(value[1]), &sz) == sizeof(value[1]) &&
1088             ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
1089                         sizeof(value[2]), &sz) == sizeof(value[2])) {
1090                 i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
1091                 i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
1092                 inode->i_mode = le32_to_cpu(value[2]);
1093
1094                 if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
1095                                 &value[0], sizeof(value),
1096                                 &sz) == sizeof(value[0])) {
1097                         inode->i_rdev = le32_to_cpu(value[0]);
1098                 }
1099         }
1100 }
1101
1102 static bool ntfs_xattr_user_list(struct dentry *dentry)
1103 {
1104         return true;
1105 }
1106
1107 // clang-format off
1108 static const struct xattr_handler ntfs_xattr_handler = {
1109         .prefix = "",
1110         .get    = ntfs_getxattr,
1111         .set    = ntfs_setxattr,
1112         .list   = ntfs_xattr_user_list,
1113 };
1114
1115 const struct xattr_handler *ntfs_xattr_handlers[] = {
1116         &ntfs_xattr_handler,
1117         NULL,
1118 };
1119 // clang-format on