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