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