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