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