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