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