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