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