Whamcloud - gitweb
LU-14651 llite: extend inode methods with user namespace arg
[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                 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         /* LU-549:  Disable security.selinux when selinux is disabled */
474         if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
475             !strcmp(name, "selinux"))
476                 RETURN(-EOPNOTSUPP);
477
478 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
479         /* posix acl is under protection of LOOKUP lock. when calling to this,
480          * we just have path resolution to the target inode, so we have great
481          * chance that cached ACL is uptodate.
482          */
483         if (handler->flags == XATTR_ACL_ACCESS_T) {
484                 struct ll_inode_info *lli = ll_i2info(inode);
485                 struct posix_acl *acl;
486
487                 read_lock(&lli->lli_lock);
488                 acl = posix_acl_dup(lli->lli_posix_acl);
489                 read_unlock(&lli->lli_lock);
490
491                 if (!acl)
492                         RETURN(-ENODATA);
493
494                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
495                 posix_acl_release(acl);
496                 RETURN(rc);
497         }
498         if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
499                 RETURN(-ENODATA);
500 #endif
501
502         fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
503         if (!fullname)
504                 RETURN(-ENOMEM);
505
506         rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
507                            OBD_MD_FLXATTR);
508         kfree(fullname);
509         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR,
510                            ktime_us_delta(ktime_get(), kstart));
511
512         RETURN(rc);
513 }
514
515 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
516 {
517         ssize_t rc;
518
519         if (S_ISREG(inode->i_mode)) {
520                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
521                 struct cl_layout cl = {
522                         .cl_buf.lb_buf = buf,
523                         .cl_buf.lb_len = buf_size,
524                 };
525                 struct lu_env *env;
526                 u16 refcheck;
527
528                 if (!obj)
529                         RETURN(-ENODATA);
530
531                 env = cl_env_get(&refcheck);
532                 if (IS_ERR(env))
533                         RETURN(PTR_ERR(env));
534
535                 rc = cl_object_layout_get(env, obj, &cl);
536                 if (rc < 0)
537                         GOTO(out_env, rc);
538
539                 if (!cl.cl_size)
540                         GOTO(out_env, rc = -ENODATA);
541
542                 rc = cl.cl_size;
543
544                 if (!buf_size)
545                         GOTO(out_env, rc);
546
547                 LASSERT(buf && rc <= buf_size);
548
549                 /*
550                  * Do not return layout gen for getxattr() since
551                  * otherwise it would confuse tar --xattr by
552                  * recognizing layout gen as stripe offset when the
553                  * file is restored. See LU-2809.
554                  */
555                 if ((((struct lov_mds_md *)buf)->lmm_magic &
556                     __swab32(LOV_MAGIC_MAGIC)) == __swab32(LOV_MAGIC_MAGIC))
557                         lustre_swab_lov_user_md((struct lov_user_md *)buf,
558                                                 cl.cl_size);
559
560                 switch (((struct lov_mds_md *)buf)->lmm_magic) {
561                 case LOV_MAGIC_V1:
562                 case LOV_MAGIC_V3:
563                 case LOV_MAGIC_SPECIFIC:
564                         ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
565                         break;
566                 case LOV_MAGIC_COMP_V1:
567                 case LOV_MAGIC_FOREIGN:
568                         goto out_env;
569                 default:
570                         CERROR("Invalid LOV magic %08x\n",
571                                ((struct lov_mds_md *)buf)->lmm_magic);
572                         GOTO(out_env, rc = -EINVAL);
573                 }
574
575 out_env:
576                 cl_env_put(env, &refcheck);
577
578                 RETURN(rc);
579         } else if (S_ISDIR(inode->i_mode)) {
580                 struct ptlrpc_request *req = NULL;
581                 struct ptlrpc_request *root_req = NULL;
582                 struct lov_mds_md *lmm = NULL;
583                 int lmm_size = 0;
584
585                 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmm_size,
586                                               &req, &root_req, 0);
587                 if (rc < 0)
588                         GOTO(out_req, rc);
589
590                 if (!buf_size)
591                         GOTO(out_req, rc = lmm_size);
592
593                 if (buf_size < lmm_size)
594                         GOTO(out_req, rc = -ERANGE);
595
596                 memcpy(buf, lmm, lmm_size);
597                 GOTO(out_req, rc = lmm_size);
598 out_req:
599                 if (req)
600                         ptlrpc_req_finished(req);
601                 if (root_req)
602                         ptlrpc_req_finished(root_req);
603
604                 RETURN(rc);
605         } else {
606                 RETURN(-ENODATA);
607         }
608 }
609
610 static int ll_xattr_get(const struct xattr_handler *handler,
611                         struct dentry *dentry, struct inode *inode,
612                         const char *name, void *buffer, size_t size)
613 {
614         LASSERT(inode);
615         LASSERT(name);
616
617         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
618                PFID(ll_inode2fid(inode)), inode, name);
619
620         if (!strcmp(name, "lov")) {
621                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
622
623                 return ll_getxattr_lov(inode, buffer, size);
624         }
625
626         return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
627 }
628
629 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
630 {
631         struct inode *inode = dentry->d_inode;
632         struct ll_sb_info *sbi = ll_i2sbi(inode);
633         ktime_t kstart = ktime_get();
634         char *xattr_name;
635         ssize_t rc, rc2;
636         size_t len, rem;
637
638         LASSERT(inode);
639
640         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
641                PFID(ll_inode2fid(inode)), inode);
642
643         rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
644                            OBD_MD_FLXATTRLS);
645         if (rc < 0)
646                 RETURN(rc);
647
648         /*
649          * If we're being called to get the size of the xattr list
650          * (size == 0) then just assume that a lustre.lov xattr
651          * exists.
652          */
653         if (!size)
654                 goto out;
655
656         xattr_name = buffer;
657         rem = rc;
658
659         while (rem > 0) {
660                 const struct xattr_handler *xh = get_xattr_type(xattr_name);
661                 bool hide_xattr = false;
662
663                 /* Hide virtual project id xattr from the list when
664                  * parent has the inherit flag and the same project id,
665                  * so project id won't be messed up by copying the xattrs
666                  * when mv to a tree with different project id.
667                  */
668                 if (xh && xh->flags == XATTR_TRUSTED_T &&
669                     strcmp(xattr_name, XATTR_NAME_PROJID) == 0) {
670                         struct inode *dir = d_inode(dentry->d_parent);
671
672                         if ((ll_i2info(inode)->lli_projid ==
673                              ll_i2info(dir)->lli_projid) &&
674                             test_bit(LLIF_PROJECT_INHERIT,
675                                      &ll_i2info(dir)->lli_flags))
676                                 hide_xattr = true;
677                 } else if (xh && xh->flags == XATTR_SECURITY_T &&
678                            strcmp(xattr_name, "security.c") == 0) {
679                         /* Listing xattrs should not expose encryption
680                          * context. There is no handler defined for
681                          * XATTR_ENCRYPTION_PREFIX, so this test is just
682                          * needed for compatibility with 2.14, in which
683                          * encryption context is stored in security.c xattr.
684                          */
685                         hide_xattr = true;
686                 }
687
688                 len = strnlen(xattr_name, rem - 1) + 1;
689                 rem -= len;
690                 if (!xattr_type_filter(sbi, hide_xattr ? NULL : xh)) {
691                         /* Skip OK xattr type, leave it in buffer. */
692                         xattr_name += len;
693                         continue;
694                 }
695
696                 /*
697                  * Move up remaining xattrs in buffer
698                  * removing the xattr that is not OK.
699                  */
700                 memmove(xattr_name, xattr_name + len, rem);
701                 rc -= len;
702         }
703
704         rc2 = ll_getxattr_lov(inode, NULL, 0);
705         if (rc2 == -ENODATA)
706                 RETURN(rc);
707
708         if (rc2 < 0)
709                 RETURN(rc2);
710
711         if (size < rc + sizeof(XATTR_LUSTRE_LOV))
712                 RETURN(-ERANGE);
713
714         memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
715
716 out:
717         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR,
718                            ktime_us_delta(ktime_get(), kstart));
719
720         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
721 }
722
723 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
724 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
725                                    struct dentry *dentry, const char *name,
726                                    void *buffer, size_t size)
727 {
728         return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
729                                    buffer, size);
730 }
731
732 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
733                             struct dentry *dentry, const char *name,
734                             void *buffer, size_t size)
735 {
736         return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
737                             size);
738 }
739
740 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
741                                    struct dentry *dentry, const char *name,
742                                    const void *value, size_t size, int flags)
743 {
744         return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
745                                    value, size, flags);
746 }
747
748 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
749                             struct dentry *dentry, const char *name,
750                             const void *value, size_t size, int flags)
751 {
752         return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
753                             size, flags);
754 }
755
756 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
757 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
758 const struct xattr_handler *get_xattr_handler(int handler_flag)
759 {
760         int i = 0;
761
762         while (ll_xattr_handlers[i]) {
763                 if (ll_xattr_handlers[i]->flags == handler_flag)
764                         return ll_xattr_handlers[i];
765                 i++;
766         }
767         return NULL;
768 }
769
770 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
771                                    void *buffer, size_t size, int handler_flags)
772 {
773         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
774
775         if (!handler)
776                 return -ENXIO;
777
778         return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
779                                    buffer, size);
780 }
781
782 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
783                             void *buffer, size_t size, int handler_flags)
784 {
785         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
786
787         if (!handler)
788                 return -ENXIO;
789
790         return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
791                             size);
792 }
793
794 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
795                                    const void *value, size_t size, int flags,
796                                    int handler_flags)
797 {
798         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
799
800         if (!handler)
801                 return -ENXIO;
802
803         return ll_xattr_set_common(handler, NULL, dentry, dentry->d_inode, name,
804                                    value, size, flags);
805 }
806
807 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
808                             const void *value, size_t size, int flags,
809                             int handler_flags)
810 {
811         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
812
813         if (!handler)
814                 return -ENXIO;
815
816         return ll_xattr_set(handler, NULL, dentry, dentry->d_inode, name, value,
817                             size, flags);
818 }
819 #endif
820
821 static const struct xattr_handler ll_user_xattr_handler = {
822         .prefix = XATTR_USER_PREFIX,
823         .flags = XATTR_USER_T,
824 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
825         .get = ll_xattr_get_common_4_3,
826         .set = ll_xattr_set_common_4_3,
827 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
828 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
829         .get = ll_xattr_get_common_3_11,
830         .set = ll_xattr_set_common_3_11,
831 #else
832         .get = ll_xattr_get_common,
833         .set = ll_xattr_set_common,
834 #endif
835 };
836
837 static const struct xattr_handler ll_trusted_xattr_handler = {
838         .prefix = XATTR_TRUSTED_PREFIX,
839         .flags = XATTR_TRUSTED_T,
840 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
841         .get = ll_xattr_get_4_3,
842         .set = ll_xattr_set_4_3,
843 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
844 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
845         .get = ll_xattr_get_3_11,
846         .set = ll_xattr_set_3_11,
847 #else
848         .get = ll_xattr_get,
849         .set = ll_xattr_set,
850 #endif
851 };
852
853 static const struct xattr_handler ll_security_xattr_handler = {
854         .prefix = XATTR_SECURITY_PREFIX,
855         .flags = XATTR_SECURITY_T,
856 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
857         .get = ll_xattr_get_common_4_3,
858         .set = ll_xattr_set_common_4_3,
859 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
860 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
861         .get = ll_xattr_get_common_3_11,
862         .set = ll_xattr_set_common_3_11,
863 #else
864         .get = ll_xattr_get_common,
865         .set = ll_xattr_set_common,
866 #endif
867 };
868
869 static const struct xattr_handler ll_acl_access_xattr_handler = {
870 #ifdef HAVE_XATTR_HANDLER_NAME
871         .name = XATTR_NAME_POSIX_ACL_ACCESS,
872 #else
873         .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
874 #endif
875         .flags = XATTR_ACL_ACCESS_T,
876 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
877         .get = ll_xattr_get_common_4_3,
878         .set = ll_xattr_set_common_4_3,
879 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
880 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
881         .get = ll_xattr_get_common_3_11,
882         .set = ll_xattr_set_common_3_11,
883 #else
884         .get = ll_xattr_get_common,
885         .set = ll_xattr_set_common,
886 #endif
887 };
888
889 static const struct xattr_handler ll_acl_default_xattr_handler = {
890 #ifdef HAVE_XATTR_HANDLER_NAME
891         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
892 #else
893         .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
894 #endif
895         .flags = XATTR_ACL_DEFAULT_T,
896 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
897         .get = ll_xattr_get_common_4_3,
898         .set = ll_xattr_set_common_4_3,
899 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
900 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
901         .get = ll_xattr_get_common_3_11,
902         .set = ll_xattr_set_common_3_11,
903 #else
904         .get = ll_xattr_get_common,
905         .set = ll_xattr_set_common,
906 #endif
907 };
908
909 static const struct xattr_handler ll_lustre_xattr_handler = {
910         .prefix = XATTR_LUSTRE_PREFIX,
911         .flags = XATTR_LUSTRE_T,
912 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
913         .get = ll_xattr_get_4_3,
914         .set = ll_xattr_set_4_3,
915 #elif !defined(HAVE_USER_NAMESPACE_ARG) && \
916 !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
917         .get = ll_xattr_get_3_11,
918         .set = ll_xattr_set_3_11,
919 #else
920         .get = ll_xattr_get,
921         .set = ll_xattr_set,
922 #endif
923 };
924
925 const struct xattr_handler *ll_xattr_handlers[] = {
926         &ll_user_xattr_handler,
927         &ll_trusted_xattr_handler,
928         &ll_security_xattr_handler,
929 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
930         &ll_acl_access_xattr_handler,
931         &ll_acl_default_xattr_handler,
932 #endif
933         &ll_lustre_xattr_handler,
934         NULL,
935 };