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