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