Whamcloud - gitweb
LU-16374 enc: align Base64 encoding with RFC 4648 base64url
[fs/lustre-release.git] / lustre / llite / xattr.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #include <linux/fs.h>
33 #include <linux/sched.h>
34 #include <linux/mm.h>
35 #include <linux/xattr.h>
36 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
37 #include <linux/selinux.h>
38 #endif
39
40 #define DEBUG_SUBSYSTEM S_LLITE
41
42 #include <obd_support.h>
43 #include <lustre_dlm.h>
44 #include <lustre_swab.h>
45
46 #include "llite_internal.h"
47
48 #ifndef HAVE_XATTR_HANDLER_NAME
49 static inline const char *xattr_prefix(const struct xattr_handler *handler)
50 {
51         return handler->prefix;
52 }
53 #endif
54
55 const struct xattr_handler *get_xattr_type(const char *name)
56 {
57         int i;
58
59         for (i = 0; ll_xattr_handlers[i]; i++) {
60                 const char *prefix = xattr_prefix(ll_xattr_handlers[i]);
61                 size_t prefix_len = strlen(prefix);
62
63                 if (!strncmp(prefix, name, prefix_len))
64                         return ll_xattr_handlers[i];
65         }
66
67         return NULL;
68 }
69
70 static int xattr_type_filter(struct ll_sb_info *sbi,
71                              const struct xattr_handler *handler)
72 {
73         /* No handler means XATTR_OTHER_T */
74         if (!handler)
75                 return -EOPNOTSUPP;
76
77         if ((handler->flags == XATTR_ACL_ACCESS_T ||
78              handler->flags == XATTR_ACL_DEFAULT_T) &&
79             !test_bit(LL_SBI_ACL, sbi->ll_flags))
80                 return -EOPNOTSUPP;
81
82         if (handler->flags == XATTR_USER_T &&
83             !test_bit(LL_SBI_USER_XATTR, sbi->ll_flags))
84                 return -EOPNOTSUPP;
85
86         if (handler->flags == XATTR_TRUSTED_T &&
87             !capable(CAP_SYS_ADMIN))
88                 return -EPERM;
89
90         return 0;
91 }
92
93 #ifndef HAVE_USER_NAMESPACE_ARG
94 #define ll_xattr_set_common(hd, ns, de, inode, name, value, size, flags) \
95         ll_xattr_set_common(hd, de, inode, name, value, size, flags)
96 #endif
97
98 static int ll_xattr_set_common(const struct xattr_handler *handler,
99                                struct user_namespace *mnt_userns,
100                                struct dentry *dentry, struct inode *inode,
101                                const char *name, const void *value, size_t size,
102                                int flags)
103 {
104         struct ll_sb_info *sbi = ll_i2sbi(inode);
105         struct ptlrpc_request *req = NULL;
106         const char *pv = value;
107         char *fullname;
108         ktime_t kstart = ktime_get();
109         u64 valid;
110         int rc;
111         ENTRY;
112
113         /* When setxattr() is called with a size of 0 the value is
114          * unconditionally replaced by "". When removexattr() is
115          * called we get a NULL value and XATTR_REPLACE for flags. */
116         if (!value && flags == XATTR_REPLACE)
117                 valid = OBD_MD_FLXATTRRM;
118         else
119                 valid = OBD_MD_FLXATTR;
120
121         /* FIXME: enable IMA when the conditions are ready */
122         if (handler->flags == XATTR_SECURITY_T &&
123             (!strcmp(name, "ima") || !strcmp(name, "evm")))
124                 RETURN(-EOPNOTSUPP);
125
126         rc = xattr_type_filter(sbi, handler);
127         if (rc)
128                 RETURN(rc);
129
130         if ((handler->flags == XATTR_ACL_ACCESS_T ||
131              handler->flags == XATTR_ACL_DEFAULT_T) &&
132             !inode_owner_or_capable(mnt_userns, inode))
133                 RETURN(-EPERM);
134
135         /* b10667: ignore lustre special xattr for now */
136         if (!strcmp(name, "hsm") ||
137             ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
138              (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
139                 RETURN(0);
140
141         rc = ll_security_secctx_name_filter(sbi, handler->flags, name);
142         if (rc)
143                 RETURN(rc);
144
145         /*
146          * In user.* namespace, only regular files and directories can have
147          * extended attributes.
148          */
149         if (handler->flags == XATTR_USER_T) {
150                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
151                         RETURN(-EPERM);
152         }
153
154         /* This check is required for compatibility with 2.14, in which
155          * encryption context is stored in security.c xattr.
156          * Setting the encryption context should only be possible by llcrypt
157          * when defining an encryption policy on a directory.
158          * When new files/dirs are created in an encrypted dir, the enc
159          * context is set directly in the create request.
160          */
161         if (handler->flags == XATTR_SECURITY_T && strcmp(name, "c") == 0)
162                 RETURN(-EPERM);
163
164         fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
165         if (!fullname)
166                 RETURN(-ENOMEM);
167
168         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
169                          pv, size, flags, ll_i2suppgid(inode), &req);
170         kfree(fullname);
171         if (rc) {
172                 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
173                         LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
174                         clear_bit(LL_SBI_USER_XATTR, sbi->ll_flags);
175                 }
176                 RETURN(rc);
177         }
178
179         ptlrpc_req_finished(req);
180
181         ll_stats_ops_tally(ll_i2sbi(inode), valid == OBD_MD_FLXATTRRM ?
182                                 LPROC_LL_REMOVEXATTR : LPROC_LL_SETXATTR,
183                            ktime_us_delta(ktime_get(), kstart));
184
185         RETURN(0);
186 }
187
188 static int get_hsm_state(struct inode *inode, u32 *hus_states)
189 {
190         struct md_op_data *op_data;
191         struct hsm_user_state *hus;
192         int rc;
193
194         OBD_ALLOC_PTR(hus);
195         if (!hus)
196                 return -ENOMEM;
197
198         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
199                                      LUSTRE_OPC_ANY, hus);
200         if (!IS_ERR(op_data)) {
201                 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
202                                    sizeof(*op_data), op_data, NULL);
203                 if (!rc)
204                         *hus_states = hus->hus_states;
205                 else
206                         CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
207                                rc);
208
209                 ll_finish_md_op_data(op_data);
210         } else {
211                 rc = PTR_ERR(op_data);
212                 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
213                        rc);
214         }
215         OBD_FREE_PTR(hus);
216         return rc;
217 }
218
219 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump, size_t size)
220 {
221         struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
222         struct lov_user_md *v1 = lump;
223         bool need_clear_release = false;
224         bool release_checked = false;
225         bool is_composite = false;
226         u16 entry_count = 1;
227         int rc = 0;
228         int i;
229
230         if (!lump)
231                 return 0;
232
233         if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
234                 if (size < sizeof(*comp_v1))
235                         return -ERANGE;
236
237                 entry_count = comp_v1->lcm_entry_count;
238                 if (size < offsetof(typeof(*comp_v1), lcm_entries[entry_count]))
239                         return -ERANGE;
240                 is_composite = true;
241         }
242
243         for (i = 0; i < entry_count; i++) {
244                 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
245                         void *ptr = comp_v1;
246
247                         if (comp_v1->lcm_entries[i].lcme_offset + sizeof(*v1) >
248                             size)
249                                 return -ERANGE;
250
251                         ptr += comp_v1->lcm_entries[i].lcme_offset;
252                         v1 = (struct lov_user_md *)ptr;
253                 }
254
255                 /*
256                  * Attributes that are saved via getxattr will always
257                  * have the stripe_offset as 0. Instead, the MDS
258                  * should be allowed to pick the starting OST index.
259                  * b=17846
260                  */
261                 if (!is_composite && v1->lmm_stripe_offset == 0)
262                         v1->lmm_stripe_offset = -1;
263
264                 /* Avoid anyone directly setting the RELEASED flag. */
265                 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
266                         if (!release_checked) {
267                                 u32 state = HS_NONE;
268
269                                 rc = get_hsm_state(inode, &state);
270                                 if (rc)
271                                         return rc;
272
273                                 if (!(state & HS_ARCHIVED))
274                                         need_clear_release = true;
275                                 release_checked = true;
276                         }
277                         if (need_clear_release)
278                                 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
279                 }
280         }
281
282         return rc;
283 }
284
285 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
286                            size_t size)
287 {
288         struct inode *inode = dentry->d_inode;
289         int rc = 0;
290
291         /*
292          * It is possible to set an xattr to a "" value of zero size.
293          * For this case we are going to treat it as a removal.
294          */
295         if (!size && lump)
296                 lump = NULL;
297
298         if (size && size < sizeof(*lump)) {
299                 /* ll_adjust_lum() or ll_lov_user_md_size() might access
300                  * before size - just give up now.
301                  */
302                 return -ERANGE;
303         }
304         rc = ll_adjust_lum(inode, lump, size);
305         if (rc)
306                 return rc;
307
308         if (lump && S_ISREG(inode->i_mode)) {
309                 u64 it_flags = FMODE_WRITE;
310                 ssize_t lum_size;
311
312                 lum_size = ll_lov_user_md_size(lump);
313                 if (lum_size < 0 || size < lum_size)
314                         return -ERANGE;
315
316                 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
317                                               lum_size);
318                 /**
319                  * b=10667: ignore -EEXIST.
320                  * Silently eat error on setting trusted.lov/lustre.lov
321                  * attribute for platforms that added the default option
322                  * to copy all attributes in 'cp' command. Both rsync and
323                  * tar --xattrs also will try to set LOVEA for existing
324                  * files.
325                  */
326                 if (rc == -EEXIST)
327                         rc = 0;
328         } else if (S_ISDIR(inode->i_mode)) {
329                 if (size != 0 && size < sizeof(struct lov_user_md))
330                         return -EINVAL;
331
332                 rc = ll_dir_setstripe(inode, lump, 0);
333         }
334
335         return rc;
336 }
337
338 #ifndef HAVE_USER_NAMESPACE_ARG
339 #define ll_xattr_set(hd, ns, de, inode, name, value, size, flags) \
340         ll_xattr_set(hd, de, inode, name, value, size, flags)
341 #endif
342
343 static int ll_xattr_set(const struct xattr_handler *handler,
344                         struct user_namespace *mnt_userns,
345                         struct dentry *dentry, struct inode *inode,
346                         const char *name, const void *value, size_t size,
347                         int flags)
348 {
349         ktime_t kstart = ktime_get();
350         int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
351                                                LPROC_LL_SETXATTR;
352         int rc;
353
354         LASSERT(inode);
355         LASSERT(name);
356
357         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
358                PFID(ll_inode2fid(inode)), inode, name);
359
360         /* lustre/trusted.lov.xxx would be passed through xattr API */
361         if (!strcmp(name, "lov")) {
362                 rc = ll_setstripe_ea(dentry, (struct lov_user_md *)value,
363                                        size);
364                 ll_stats_ops_tally(ll_i2sbi(inode), op_type,
365                                    ktime_us_delta(ktime_get(), kstart));
366                 return rc;
367         } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
368                 ll_stats_ops_tally(ll_i2sbi(inode), op_type,
369                                    ktime_us_delta(ktime_get(), kstart));
370                 return 0;
371         }
372
373         if (strncmp(name, "lov.", 4) == 0 &&
374             (__swab32(((struct lov_user_md *)value)->lmm_magic) &
375             le32_to_cpu(LOV_MAGIC_MASK)) == le32_to_cpu(LOV_MAGIC_MAGIC))
376                 lustre_swab_lov_user_md((struct lov_user_md *)value, 0);
377
378         return ll_xattr_set_common(handler, mnt_userns, dentry, inode, name,
379                                    value, size, flags);
380 }
381
382 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
383                   size_t size, u64 valid)
384 {
385         struct ll_inode_info *lli = ll_i2info(inode);
386         struct ll_sb_info *sbi = ll_i2sbi(inode);
387         struct ptlrpc_request *req = NULL;
388         void *xdata;
389         int rc;
390         ENTRY;
391
392         /* This check is required for compatibility with 2.14, in which
393          * encryption context is stored in security.c xattr. Accessing the
394          * encryption context should only be possible by llcrypt.
395          */
396         if (type == XATTR_SECURITY_T && strcmp(name, "security.c") == 0)
397                 GOTO(out_xattr, rc = -EPERM);
398
399         if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
400             (type != XATTR_SECURITY_T || !ll_xattr_is_seclabel(name)) &&
401             (type != XATTR_TRUSTED_T || strcmp(name, XATTR_NAME_SOM))) {
402                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
403                 if (rc == -EAGAIN)
404                         goto getxattr_nocache;
405                 if (rc < 0)
406                         GOTO(out_xattr, rc);
407
408                 /* Add "system.posix_acl_access" to the list */
409                 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
410                         if (size == 0) {
411                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
412                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
413                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
414                                        sizeof(XATTR_NAME_ACL_ACCESS));
415                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
416                         } else {
417                                 GOTO(out_xattr, rc = -ERANGE);
418                         }
419                 }
420         } else {
421 getxattr_nocache:
422                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
423                                  name, size, &req);
424                 if (rc < 0)
425                         GOTO(out_xattr, rc);
426
427                 /* only detect the xattr size */
428                 if (size == 0)
429                         GOTO(out, rc);
430
431                 if (size < rc)
432                         GOTO(out, rc = -ERANGE);
433
434                 /* do not need swab xattr data */
435                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
436                                                      rc);
437                 if (!xdata)
438                         GOTO(out, rc = -EPROTO);
439
440                 memcpy(buffer, xdata, rc);
441         }
442
443         EXIT;
444
445 out_xattr:
446         if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
447                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
448                               "it is not supported on the server: rc = %d\n",
449                               sbi->ll_fsname, rc);
450                 clear_bit(LL_SBI_USER_XATTR, sbi->ll_flags);
451         }
452 out:
453         ptlrpc_req_finished(req);
454         RETURN(rc);
455 }
456
457 static int ll_xattr_get_common(const struct xattr_handler *handler,
458                                struct dentry *dentry,
459                                struct inode *inode,
460                                const char *name, void *buffer, size_t size)
461 {
462         struct ll_sb_info *sbi = ll_i2sbi(inode);
463         ktime_t kstart = ktime_get();
464         char *fullname;
465         int rc;
466
467         ENTRY;
468
469         rc = xattr_type_filter(sbi, handler);
470         if (rc)
471                 RETURN(rc);
472
473         rc = ll_security_secctx_name_filter(sbi, handler->flags, name);
474         if (rc)
475                 RETURN(rc);
476
477 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
478         /* posix acl is under protection of LOOKUP lock. when calling to this,
479          * we just have path resolution to the target inode, so we have great
480          * chance that cached ACL is uptodate.
481          */
482         if (handler->flags == XATTR_ACL_ACCESS_T) {
483                 struct ll_inode_info *lli = ll_i2info(inode);
484                 struct posix_acl *acl;
485
486                 read_lock(&lli->lli_lock);
487                 acl = posix_acl_dup(lli->lli_posix_acl);
488                 read_unlock(&lli->lli_lock);
489
490                 if (!acl)
491                         RETURN(-ENODATA);
492
493                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
494                 posix_acl_release(acl);
495                 RETURN(rc);
496         }
497         if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
498                 RETURN(-ENODATA);
499 #endif
500
501         fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
502         if (!fullname)
503                 RETURN(-ENOMEM);
504
505         rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
506                            OBD_MD_FLXATTR);
507         kfree(fullname);
508         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR,
509                            ktime_us_delta(ktime_get(), kstart));
510
511         RETURN(rc);
512 }
513
514 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
515 {
516         ssize_t rc;
517
518         if (S_ISREG(inode->i_mode)) {
519                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
520                 struct cl_layout cl = {
521                         .cl_buf.lb_buf = buf,
522                         .cl_buf.lb_len = buf_size,
523                 };
524                 struct lu_env *env;
525                 u16 refcheck;
526
527                 if (!obj)
528                         RETURN(-ENODATA);
529
530                 env = cl_env_get(&refcheck);
531                 if (IS_ERR(env))
532                         RETURN(PTR_ERR(env));
533
534                 rc = cl_object_layout_get(env, obj, &cl);
535                 if (rc < 0)
536                         GOTO(out_env, rc);
537
538                 if (!cl.cl_size)
539                         GOTO(out_env, rc = -ENODATA);
540
541                 rc = cl.cl_size;
542
543                 if (!buf_size)
544                         GOTO(out_env, rc);
545
546                 LASSERT(buf && rc <= buf_size);
547
548                 /*
549                  * Do not return layout gen for getxattr() since
550                  * otherwise it would confuse tar --xattr by
551                  * recognizing layout gen as stripe offset when the
552                  * file is restored. See LU-2809.
553                  */
554                 if ((((struct lov_mds_md *)buf)->lmm_magic &
555                     __swab32(LOV_MAGIC_MAGIC)) == __swab32(LOV_MAGIC_MAGIC))
556                         lustre_swab_lov_user_md((struct lov_user_md *)buf,
557                                                 cl.cl_size);
558
559                 switch (((struct lov_mds_md *)buf)->lmm_magic) {
560                 case LOV_MAGIC_V1:
561                 case LOV_MAGIC_V3:
562                 case LOV_MAGIC_SPECIFIC:
563                         ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
564                         break;
565                 case LOV_MAGIC_COMP_V1:
566                 case LOV_MAGIC_FOREIGN:
567                         goto out_env;
568                 default:
569                         CERROR("Invalid LOV magic %08x\n",
570                                ((struct lov_mds_md *)buf)->lmm_magic);
571                         GOTO(out_env, rc = -EINVAL);
572                 }
573
574 out_env:
575                 cl_env_put(env, &refcheck);
576
577                 RETURN(rc);
578         } else if (S_ISDIR(inode->i_mode)) {
579                 struct ptlrpc_request *req = NULL;
580                 struct ptlrpc_request *root_req = NULL;
581                 struct lov_mds_md *lmm = NULL;
582                 int lmm_size = 0;
583
584                 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmm_size,
585                                               &req, &root_req, 0);
586                 if (rc < 0)
587                         GOTO(out_req, rc);
588
589                 if (!buf_size)
590                         GOTO(out_req, rc = lmm_size);
591
592                 if (buf_size < lmm_size)
593                         GOTO(out_req, rc = -ERANGE);
594
595                 memcpy(buf, lmm, lmm_size);
596                 GOTO(out_req, rc = lmm_size);
597 out_req:
598                 if (req)
599                         ptlrpc_req_finished(req);
600                 if (root_req)
601                         ptlrpc_req_finished(root_req);
602
603                 RETURN(rc);
604         } else {
605                 RETURN(-ENODATA);
606         }
607 }
608
609 static int ll_xattr_get(const struct xattr_handler *handler,
610                         struct dentry *dentry, struct inode *inode,
611                         const char *name, void *buffer, size_t size)
612 {
613         LASSERT(inode);
614         LASSERT(name);
615
616         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
617                PFID(ll_inode2fid(inode)), inode, name);
618
619         if (!strcmp(name, "lov")) {
620                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
621
622                 return ll_getxattr_lov(inode, buffer, size);
623         }
624
625         return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
626 }
627
628 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
629 {
630         struct inode *inode = dentry->d_inode;
631         struct ll_sb_info *sbi = ll_i2sbi(inode);
632         ktime_t kstart = ktime_get();
633         char *xattr_name;
634         ssize_t rc, rc2;
635         size_t len, rem;
636
637         LASSERT(inode);
638
639         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
640                PFID(ll_inode2fid(inode)), inode);
641
642         rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
643                            OBD_MD_FLXATTRLS);
644         if (rc < 0)
645                 RETURN(rc);
646
647         /*
648          * If we're being called to get the size of the xattr list
649          * (size == 0) then just assume that a lustre.lov xattr
650          * exists.
651          */
652         if (!size)
653                 goto out;
654
655         xattr_name = buffer;
656         rem = rc;
657
658         while (rem > 0) {
659                 const struct xattr_handler *xh = get_xattr_type(xattr_name);
660                 bool hide_xattr = false;
661
662                 /* Hide virtual project id xattr from the list when
663                  * parent has the inherit flag and the same project id,
664                  * so project id won't be messed up by copying the xattrs
665                  * when mv to a tree with different project id.
666                  */
667                 if (xh && xh->flags == XATTR_TRUSTED_T &&
668                     strcmp(xattr_name, XATTR_NAME_PROJID) == 0) {
669                         struct inode *dir = d_inode(dentry->d_parent);
670
671                         if ((ll_i2info(inode)->lli_projid ==
672                              ll_i2info(dir)->lli_projid) &&
673                             test_bit(LLIF_PROJECT_INHERIT,
674                                      &ll_i2info(dir)->lli_flags))
675                                 hide_xattr = true;
676                 } else if (xh && xh->flags == XATTR_SECURITY_T &&
677                            strcmp(xattr_name, "security.c") == 0) {
678                         /* Listing xattrs should not expose encryption
679                          * context. There is no handler defined for
680                          * XATTR_ENCRYPTION_PREFIX, so this test is just
681                          * needed for compatibility with 2.14, in which
682                          * encryption context is stored in security.c xattr.
683                          */
684                         hide_xattr = true;
685                 }
686
687                 len = strnlen(xattr_name, rem - 1) + 1;
688                 rem -= len;
689                 if (!xattr_type_filter(sbi, hide_xattr ? NULL : xh)) {
690                         /* Skip OK xattr type, leave it in buffer. */
691                         xattr_name += len;
692                         continue;
693                 }
694
695                 /*
696                  * Move up remaining xattrs in buffer
697                  * removing the xattr that is not OK.
698                  */
699                 memmove(xattr_name, xattr_name + len, rem);
700                 rc -= len;
701         }
702
703         rc2 = ll_getxattr_lov(inode, NULL, 0);
704         if (rc2 == -ENODATA)
705                 RETURN(rc);
706
707         if (rc2 < 0)
708                 RETURN(rc2);
709
710         if (size < rc + sizeof(XATTR_LUSTRE_LOV))
711                 RETURN(-ERANGE);
712
713         memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
714
715 out:
716         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR,
717                            ktime_us_delta(ktime_get(), kstart));
718
719         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
720 }
721
722 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
723 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
724                                    struct dentry *dentry, const char *name,
725                                    void *buffer, size_t size)
726 {
727         return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
728                                    buffer, size);
729 }
730
731 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
732                             struct dentry *dentry, const char *name,
733                             void *buffer, size_t size)
734 {
735         return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
736                             size);
737 }
738
739 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
740                                    struct dentry *dentry, const char *name,
741                                    const void *value, size_t size, int flags)
742 {
743         return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
744                                    value, size, flags);
745 }
746
747 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
748                             struct dentry *dentry, const char *name,
749                             const void *value, size_t size, int flags)
750 {
751         return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
752                             size, flags);
753 }
754
755 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
756 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
757 const struct xattr_handler *get_xattr_handler(int handler_flag)
758 {
759         int i = 0;
760
761         while (ll_xattr_handlers[i]) {
762                 if (ll_xattr_handlers[i]->flags == handler_flag)
763                         return ll_xattr_handlers[i];
764                 i++;
765         }
766         return NULL;
767 }
768
769 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
770                                    void *buffer, size_t size, int handler_flags)
771 {
772         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
773
774         if (!handler)
775                 return -ENXIO;
776
777         return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
778                                    buffer, size);
779 }
780
781 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
782                             void *buffer, size_t size, int handler_flags)
783 {
784         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
785
786         if (!handler)
787                 return -ENXIO;
788
789         return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
790                             size);
791 }
792
793 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
794                                    const void *value, size_t size, int flags,
795                                    int handler_flags)
796 {
797         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
798
799         if (!handler)
800                 return -ENXIO;
801
802         return ll_xattr_set_common(handler, NULL, dentry, dentry->d_inode, name,
803                                    value, size, flags);
804 }
805
806 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
807                             const void *value, size_t size, int flags,
808                             int handler_flags)
809 {
810         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
811
812         if (!handler)
813                 return -ENXIO;
814
815         return ll_xattr_set(handler, NULL, dentry, dentry->d_inode, name, value,
816                             size, flags);
817 }
818 #endif
819
820 static const struct xattr_handler ll_user_xattr_handler = {
821         .prefix = XATTR_USER_PREFIX,
822         .flags = XATTR_USER_T,
823 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
824         .get = ll_xattr_get_common_4_3,
825         .set = ll_xattr_set_common_4_3,
826 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
827 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
828         .get = ll_xattr_get_common_3_11,
829         .set = ll_xattr_set_common_3_11,
830 #else
831         .get = ll_xattr_get_common,
832         .set = ll_xattr_set_common,
833 #endif
834 };
835
836 static const struct xattr_handler ll_trusted_xattr_handler = {
837         .prefix = XATTR_TRUSTED_PREFIX,
838         .flags = XATTR_TRUSTED_T,
839 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
840         .get = ll_xattr_get_4_3,
841         .set = ll_xattr_set_4_3,
842 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
843 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
844         .get = ll_xattr_get_3_11,
845         .set = ll_xattr_set_3_11,
846 #else
847         .get = ll_xattr_get,
848         .set = ll_xattr_set,
849 #endif
850 };
851
852 static const struct xattr_handler ll_security_xattr_handler = {
853         .prefix = XATTR_SECURITY_PREFIX,
854         .flags = XATTR_SECURITY_T,
855 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
856         .get = ll_xattr_get_common_4_3,
857         .set = ll_xattr_set_common_4_3,
858 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
859 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
860         .get = ll_xattr_get_common_3_11,
861         .set = ll_xattr_set_common_3_11,
862 #else
863         .get = ll_xattr_get_common,
864         .set = ll_xattr_set_common,
865 #endif
866 };
867
868 static const struct xattr_handler ll_acl_access_xattr_handler = {
869 #ifdef HAVE_XATTR_HANDLER_NAME
870         .name = XATTR_NAME_POSIX_ACL_ACCESS,
871 #else
872         .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
873 #endif
874         .flags = XATTR_ACL_ACCESS_T,
875 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
876         .get = ll_xattr_get_common_4_3,
877         .set = ll_xattr_set_common_4_3,
878 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
879 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
880         .get = ll_xattr_get_common_3_11,
881         .set = ll_xattr_set_common_3_11,
882 #else
883         .get = ll_xattr_get_common,
884         .set = ll_xattr_set_common,
885 #endif
886 };
887
888 static const struct xattr_handler ll_acl_default_xattr_handler = {
889 #ifdef HAVE_XATTR_HANDLER_NAME
890         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
891 #else
892         .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
893 #endif
894         .flags = XATTR_ACL_DEFAULT_T,
895 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
896         .get = ll_xattr_get_common_4_3,
897         .set = ll_xattr_set_common_4_3,
898 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
899 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
900         .get = ll_xattr_get_common_3_11,
901         .set = ll_xattr_set_common_3_11,
902 #else
903         .get = ll_xattr_get_common,
904         .set = ll_xattr_set_common,
905 #endif
906 };
907
908 static const struct xattr_handler ll_lustre_xattr_handler = {
909         .prefix = XATTR_LUSTRE_PREFIX,
910         .flags = XATTR_LUSTRE_T,
911 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
912         .get = ll_xattr_get_4_3,
913         .set = ll_xattr_set_4_3,
914 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
915 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
916         .get = ll_xattr_get_3_11,
917         .set = ll_xattr_set_3_11,
918 #else
919         .get = ll_xattr_get,
920         .set = ll_xattr_set,
921 #endif
922 };
923
924 const struct xattr_handler *ll_xattr_handlers[] = {
925         &ll_user_xattr_handler,
926         &ll_trusted_xattr_handler,
927         &ll_security_xattr_handler,
928 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
929         &ll_acl_access_xattr_handler,
930         &ll_acl_default_xattr_handler,
931 #endif
932         &ll_lustre_xattr_handler,
933         NULL,
934 };