Whamcloud - gitweb
LU-13826 utils: fix compatibility for LL_IOC_MDC_GETINFO
[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 const struct xattr_handler *get_xattr_type(const char *name)
58 {
59         int i;
60
61         for (i = 0; ll_xattr_handlers[i]; i++) {
62                 const char *prefix = xattr_prefix(ll_xattr_handlers[i]);
63                 size_t prefix_len = strlen(prefix);
64
65                 if (!strncmp(prefix, name, prefix_len))
66                         return ll_xattr_handlers[i];
67         }
68
69         return NULL;
70 }
71
72 static int xattr_type_filter(struct ll_sb_info *sbi,
73                              const struct xattr_handler *handler)
74 {
75         /* No handler means XATTR_OTHER_T */
76         if (!handler)
77                 return -EOPNOTSUPP;
78
79         if ((handler->flags == XATTR_ACL_ACCESS_T ||
80              handler->flags == XATTR_ACL_DEFAULT_T) &&
81             !(sbi->ll_flags & LL_SBI_ACL))
82                 return -EOPNOTSUPP;
83
84         if (handler->flags == XATTR_USER_T &&
85             !(sbi->ll_flags & LL_SBI_USER_XATTR))
86                 return -EOPNOTSUPP;
87
88         if (handler->flags == XATTR_TRUSTED_T &&
89             !capable(CFS_CAP_SYS_ADMIN))
90                 return -EPERM;
91
92         return 0;
93 }
94
95 static int ll_xattr_set_common(const struct xattr_handler *handler,
96                                struct dentry *dentry, struct inode *inode,
97                                const char *name, const void *value, size_t size,
98                                int flags)
99 {
100         struct ll_sb_info *sbi = ll_i2sbi(inode);
101         struct ptlrpc_request *req = NULL;
102         const char *pv = value;
103         char *fullname;
104         ktime_t kstart = ktime_get();
105         u64 valid;
106         int rc;
107         ENTRY;
108
109         /* When setxattr() is called with a size of 0 the value is
110          * unconditionally replaced by "". When removexattr() is
111          * called we get a NULL value and XATTR_REPLACE for flags. */
112         if (!value && flags == XATTR_REPLACE)
113                 valid = OBD_MD_FLXATTRRM;
114         else
115                 valid = OBD_MD_FLXATTR;
116
117         /* FIXME: enable IMA when the conditions are ready */
118         if (handler->flags == XATTR_SECURITY_T &&
119             (!strcmp(name, "ima") || !strcmp(name, "evm")))
120                 RETURN(-EOPNOTSUPP);
121
122         rc = xattr_type_filter(sbi, handler);
123         if (rc)
124                 RETURN(rc);
125
126         if ((handler->flags == XATTR_ACL_ACCESS_T ||
127              handler->flags == XATTR_ACL_DEFAULT_T) &&
128             !inode_owner_or_capable(inode))
129                 RETURN(-EPERM);
130
131         /* b10667: ignore lustre special xattr for now */
132         if (!strcmp(name, "hsm") ||
133             ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
134              (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
135                 RETURN(0);
136
137         /* LU-549:  Disable security.selinux when selinux is disabled */
138         if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
139             strcmp(name, "selinux") == 0)
140                 RETURN(-EOPNOTSUPP);
141
142         /*
143          * In user.* namespace, only regular files and directories can have
144          * extended attributes.
145          */
146         if (handler->flags == XATTR_USER_T) {
147                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
148                         RETURN(-EPERM);
149         }
150
151         fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
152         if (!fullname)
153                 RETURN(-ENOMEM);
154
155         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
156                          pv, size, flags, ll_i2suppgid(inode), &req);
157         kfree(fullname);
158         if (rc) {
159                 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
160                         LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
161                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
162                 }
163                 RETURN(rc);
164         }
165
166         ptlrpc_req_finished(req);
167
168         ll_stats_ops_tally(ll_i2sbi(inode), valid == OBD_MD_FLXATTRRM ?
169                                 LPROC_LL_REMOVEXATTR : LPROC_LL_SETXATTR,
170                            ktime_us_delta(ktime_get(), kstart));
171
172         RETURN(0);
173 }
174
175 static int get_hsm_state(struct inode *inode, u32 *hus_states)
176 {
177         struct md_op_data *op_data;
178         struct hsm_user_state *hus;
179         int rc;
180
181         OBD_ALLOC_PTR(hus);
182         if (!hus)
183                 return -ENOMEM;
184
185         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
186                                      LUSTRE_OPC_ANY, hus);
187         if (!IS_ERR(op_data)) {
188                 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
189                                    sizeof(*op_data), op_data, NULL);
190                 if (!rc)
191                         *hus_states = hus->hus_states;
192                 else
193                         CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
194                                rc);
195
196                 ll_finish_md_op_data(op_data);
197         } else {
198                 rc = PTR_ERR(op_data);
199                 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
200                        rc);
201         }
202         OBD_FREE_PTR(hus);
203         return rc;
204 }
205
206 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump, size_t size)
207 {
208         struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
209         struct lov_user_md *v1 = lump;
210         bool need_clear_release = false;
211         bool release_checked = false;
212         bool is_composite = false;
213         u16 entry_count = 1;
214         int rc = 0;
215         int i;
216
217         if (!lump)
218                 return 0;
219
220         if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
221                 if (size < sizeof(*comp_v1))
222                         return -ERANGE;
223
224                 entry_count = comp_v1->lcm_entry_count;
225                 if (size < offsetof(typeof(*comp_v1), lcm_entries[entry_count]))
226                         return -ERANGE;
227                 is_composite = true;
228         }
229
230         for (i = 0; i < entry_count; i++) {
231                 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
232                         void *ptr = comp_v1;
233
234                         if (comp_v1->lcm_entries[i].lcme_offset + sizeof(*v1) >
235                             size)
236                                 return -ERANGE;
237
238                         ptr += comp_v1->lcm_entries[i].lcme_offset;
239                         v1 = (struct lov_user_md *)ptr;
240                 }
241
242                 /*
243                  * Attributes that are saved via getxattr will always
244                  * have the stripe_offset as 0. Instead, the MDS
245                  * should be allowed to pick the starting OST index.
246                  * b=17846
247                  */
248                 if (!is_composite && v1->lmm_stripe_offset == 0)
249                         v1->lmm_stripe_offset = -1;
250
251                 /* Avoid anyone directly setting the RELEASED flag. */
252                 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
253                         if (!release_checked) {
254                                 u32 state = HS_NONE;
255
256                                 rc = get_hsm_state(inode, &state);
257                                 if (rc)
258                                         return rc;
259
260                                 if (!(state & HS_ARCHIVED))
261                                         need_clear_release = true;
262                                 release_checked = true;
263                         }
264                         if (need_clear_release)
265                                 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
266                 }
267         }
268
269         return rc;
270 }
271
272 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
273                            size_t size)
274 {
275         struct inode *inode = dentry->d_inode;
276         int rc = 0;
277
278         /*
279          * It is possible to set an xattr to a "" value of zero size.
280          * For this case we are going to treat it as a removal.
281          */
282         if (!size && lump)
283                 lump = NULL;
284
285         if (size && size < sizeof(*lump)) {
286                 /* ll_adjust_lum() or ll_lov_user_md_size() might access
287                  * before size - just give up now.
288                  */
289                 return -ERANGE;
290         }
291         rc = ll_adjust_lum(inode, lump, size);
292         if (rc)
293                 return rc;
294
295         if (lump && S_ISREG(inode->i_mode)) {
296                 u64 it_flags = FMODE_WRITE;
297                 ssize_t lum_size;
298
299                 lum_size = ll_lov_user_md_size(lump);
300                 if (lum_size < 0 || size < lum_size)
301                         return -ERANGE;
302
303                 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
304                                               lum_size);
305                 /**
306                  * b=10667: ignore -EEXIST.
307                  * Silently eat error on setting trusted.lov/lustre.lov
308                  * attribute for platforms that added the default option
309                  * to copy all attributes in 'cp' command. Both rsync and
310                  * tar --xattrs also will try to set LOVEA for existing
311                  * files.
312                  */
313                 if (rc == -EEXIST)
314                         rc = 0;
315         } else if (S_ISDIR(inode->i_mode)) {
316                 if (size != 0 && size < sizeof(struct lov_user_md))
317                         return -EINVAL;
318
319                 rc = ll_dir_setstripe(inode, lump, 0);
320         }
321
322         return rc;
323 }
324
325 static int ll_xattr_set(const struct xattr_handler *handler,
326                         struct dentry *dentry, struct inode *inode,
327                         const char *name, const void *value, size_t size,
328                         int flags)
329 {
330         ktime_t kstart = ktime_get();
331         int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
332                                                LPROC_LL_SETXATTR;
333         int rc;
334
335         LASSERT(inode);
336         LASSERT(name);
337
338         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
339                PFID(ll_inode2fid(inode)), inode, name);
340
341         /* lustre/trusted.lov.xxx would be passed through xattr API */
342         if (!strcmp(name, "lov")) {
343                 rc = ll_setstripe_ea(dentry, (struct lov_user_md *)value,
344                                        size);
345                 ll_stats_ops_tally(ll_i2sbi(inode), op_type,
346                                    ktime_us_delta(ktime_get(), kstart));
347                 return rc;
348         } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
349                 ll_stats_ops_tally(ll_i2sbi(inode), op_type,
350                                    ktime_us_delta(ktime_get(), kstart));
351                 return 0;
352         }
353
354         if (strncmp(name, "lov.", 4) == 0 &&
355             (__swab32(((struct lov_user_md *)value)->lmm_magic) &
356             le32_to_cpu(LOV_MAGIC_MASK)) == le32_to_cpu(LOV_MAGIC_MAGIC))
357                 lustre_swab_lov_user_md((struct lov_user_md *)value, 0);
358
359         return ll_xattr_set_common(handler, dentry, inode, name, value, size,
360                                    flags);
361 }
362
363 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
364                   size_t size, u64 valid)
365 {
366         struct ll_inode_info *lli = ll_i2info(inode);
367         struct ll_sb_info *sbi = ll_i2sbi(inode);
368         struct ptlrpc_request *req = NULL;
369         void *xdata;
370         int rc;
371         ENTRY;
372
373         if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
374             (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
375                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
376                 if (rc == -EAGAIN)
377                         goto getxattr_nocache;
378                 if (rc < 0)
379                         GOTO(out_xattr, rc);
380
381                 /* Add "system.posix_acl_access" to the list */
382                 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
383                         if (size == 0) {
384                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
385                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
386                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
387                                        sizeof(XATTR_NAME_ACL_ACCESS));
388                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
389                         } else {
390                                 GOTO(out_xattr, rc = -ERANGE);
391                         }
392                 }
393         } else {
394 getxattr_nocache:
395                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
396                                  name, size, &req);
397                 if (rc < 0)
398                         GOTO(out_xattr, rc);
399
400                 /* only detect the xattr size */
401                 if (size == 0)
402                         GOTO(out, rc);
403
404                 if (size < rc)
405                         GOTO(out, rc = -ERANGE);
406
407                 /* do not need swab xattr data */
408                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
409                                                      rc);
410                 if (!xdata)
411                         GOTO(out, rc = -EPROTO);
412
413                 memcpy(buffer, xdata, rc);
414         }
415
416         EXIT;
417
418 out_xattr:
419         if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
420                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
421                               "it is not supported on the server: rc = %d\n",
422                               sbi->ll_fsname, rc);
423                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
424         }
425 out:
426         ptlrpc_req_finished(req);
427         RETURN(rc);
428 }
429
430 static int ll_xattr_get_common(const struct xattr_handler *handler,
431                                struct dentry *dentry,
432                                struct inode *inode,
433                                const char *name, void *buffer, size_t size)
434 {
435         struct ll_sb_info *sbi = ll_i2sbi(inode);
436         ktime_t kstart = ktime_get();
437         char *fullname;
438         int rc;
439
440         ENTRY;
441
442         rc = xattr_type_filter(sbi, handler);
443         if (rc)
444                 RETURN(rc);
445
446         /* LU-549:  Disable security.selinux when selinux is disabled */
447         if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
448             !strcmp(name, "selinux"))
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 };