Whamcloud - gitweb
6cfad040770fd0b2517c16fce6ac4608cf9a092e
[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         rc = ll_adjust_lum(inode, lump);
287         if (rc)
288                 return rc;
289
290         if (lump && S_ISREG(inode->i_mode)) {
291                 u64 it_flags = FMODE_WRITE;
292                 ssize_t lum_size;
293
294                 lum_size = ll_lov_user_md_size(lump);
295                 if (lum_size < 0 || size < lum_size)
296                         return -ERANGE;
297
298                 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
299                                               lum_size);
300                 /**
301                  * b=10667: ignore -EEXIST.
302                  * Silently eat error on setting trusted.lov/lustre.lov
303                  * attribute for platforms that added the default option
304                  * to copy all attributes in 'cp' command. Both rsync and
305                  * tar --xattrs also will try to set LOVEA for existing
306                  * files.
307                  */
308                 if (rc == -EEXIST)
309                         rc = 0;
310         } else if (S_ISDIR(inode->i_mode)) {
311                 if (size != 0 && size < sizeof(struct lov_user_md))
312                         return -EINVAL;
313
314                 rc = ll_dir_setstripe(inode, lump, 0);
315         }
316
317         return rc;
318 }
319
320 static int ll_xattr_set(const struct xattr_handler *handler,
321                         struct dentry *dentry, struct inode *inode,
322                         const char *name, const void *value, size_t size,
323                         int flags)
324 {
325         ktime_t kstart = ktime_get();
326         int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
327                                                LPROC_LL_SETXATTR;
328         int rc;
329
330         LASSERT(inode);
331         LASSERT(name);
332
333         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
334                PFID(ll_inode2fid(inode)), inode, name);
335
336         /* lustre/trusted.lov.xxx would be passed through xattr API */
337         if (!strcmp(name, "lov")) {
338                 rc = ll_setstripe_ea(dentry, (struct lov_user_md *)value,
339                                        size);
340                 ll_stats_ops_tally(ll_i2sbi(inode), op_type,
341                                    ktime_us_delta(ktime_get(), kstart));
342                 return rc;
343         } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
344                 ll_stats_ops_tally(ll_i2sbi(inode), op_type,
345                                    ktime_us_delta(ktime_get(), kstart));
346                 return 0;
347         }
348
349         if (strncmp(name, "lov.", 4) == 0 &&
350             (__swab32(((struct lov_user_md *)value)->lmm_magic) &
351             le32_to_cpu(LOV_MAGIC_MASK)) == le32_to_cpu(LOV_MAGIC_MAGIC))
352                 lustre_swab_lov_user_md((struct lov_user_md *)value, 0);
353
354         return ll_xattr_set_common(handler, dentry, inode, name, value, size,
355                                    flags);
356 }
357
358 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
359                   size_t size, u64 valid)
360 {
361         struct ll_inode_info *lli = ll_i2info(inode);
362         struct ll_sb_info *sbi = ll_i2sbi(inode);
363         struct ptlrpc_request *req = NULL;
364         void *xdata;
365         int rc;
366         ENTRY;
367
368         if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
369             (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
370                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
371                 if (rc == -EAGAIN)
372                         goto getxattr_nocache;
373                 if (rc < 0)
374                         GOTO(out_xattr, rc);
375
376                 /* Add "system.posix_acl_access" to the list */
377                 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
378                         if (size == 0) {
379                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
380                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
381                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
382                                        sizeof(XATTR_NAME_ACL_ACCESS));
383                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
384                         } else {
385                                 GOTO(out_xattr, rc = -ERANGE);
386                         }
387                 }
388         } else {
389 getxattr_nocache:
390                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
391                                  name, size, &req);
392                 if (rc < 0)
393                         GOTO(out_xattr, rc);
394
395                 /* only detect the xattr size */
396                 if (size == 0)
397                         GOTO(out, rc);
398
399                 if (size < rc)
400                         GOTO(out, rc = -ERANGE);
401
402                 /* do not need swab xattr data */
403                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
404                                                      rc);
405                 if (!xdata)
406                         GOTO(out, rc = -EPROTO);
407
408                 memcpy(buffer, xdata, rc);
409         }
410
411         EXIT;
412
413 out_xattr:
414         if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
415                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
416                               "it is not supported on the server: rc = %d\n",
417                               sbi->ll_fsname, rc);
418                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
419         }
420 out:
421         ptlrpc_req_finished(req);
422         RETURN(rc);
423 }
424
425 static int ll_xattr_get_common(const struct xattr_handler *handler,
426                                struct dentry *dentry,
427                                struct inode *inode,
428                                const char *name, void *buffer, size_t size)
429 {
430         struct ll_sb_info *sbi = ll_i2sbi(inode);
431         ktime_t kstart = ktime_get();
432         char *fullname;
433         int rc;
434
435         ENTRY;
436
437         rc = xattr_type_filter(sbi, handler);
438         if (rc)
439                 RETURN(rc);
440
441         /* LU-549:  Disable security.selinux when selinux is disabled */
442         if (test_xattr_is_selinux_disabled(handler, name))
443                 RETURN(-EOPNOTSUPP);
444
445 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
446         /* posix acl is under protection of LOOKUP lock. when calling to this,
447          * we just have path resolution to the target inode, so we have great
448          * chance that cached ACL is uptodate.
449          */
450         if (handler->flags == XATTR_ACL_ACCESS_T) {
451                 struct ll_inode_info *lli = ll_i2info(inode);
452                 struct posix_acl *acl;
453
454                 spin_lock(&lli->lli_lock);
455                 acl = posix_acl_dup(lli->lli_posix_acl);
456                 spin_unlock(&lli->lli_lock);
457
458                 if (!acl)
459                         RETURN(-ENODATA);
460
461                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
462                 posix_acl_release(acl);
463                 RETURN(rc);
464         }
465         if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
466                 RETURN(-ENODATA);
467 #endif
468
469         fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
470         if (!fullname)
471                 RETURN(-ENOMEM);
472
473         rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
474                            OBD_MD_FLXATTR);
475         kfree(fullname);
476         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR,
477                            ktime_us_delta(ktime_get(), kstart));
478
479         RETURN(rc);
480 }
481
482 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
483 {
484         ssize_t rc;
485
486         if (S_ISREG(inode->i_mode)) {
487                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
488                 struct cl_layout cl = {
489                         .cl_buf.lb_buf = buf,
490                         .cl_buf.lb_len = buf_size,
491                 };
492                 struct lu_env *env;
493                 u16 refcheck;
494
495                 if (!obj)
496                         RETURN(-ENODATA);
497
498                 env = cl_env_get(&refcheck);
499                 if (IS_ERR(env))
500                         RETURN(PTR_ERR(env));
501
502                 rc = cl_object_layout_get(env, obj, &cl);
503                 if (rc < 0)
504                         GOTO(out_env, rc);
505
506                 if (!cl.cl_size)
507                         GOTO(out_env, rc = -ENODATA);
508
509                 rc = cl.cl_size;
510
511                 if (!buf_size)
512                         GOTO(out_env, rc);
513
514                 LASSERT(buf && rc <= buf_size);
515
516                 /*
517                  * Do not return layout gen for getxattr() since
518                  * otherwise it would confuse tar --xattr by
519                  * recognizing layout gen as stripe offset when the
520                  * file is restored. See LU-2809.
521                  */
522                 if ((((struct lov_mds_md *)buf)->lmm_magic &
523                     __swab32(LOV_MAGIC_MAGIC)) == __swab32(LOV_MAGIC_MAGIC))
524                         lustre_swab_lov_user_md((struct lov_user_md *)buf,
525                                                 cl.cl_size);
526
527                 switch (((struct lov_mds_md *)buf)->lmm_magic) {
528                 case LOV_MAGIC_V1:
529                 case LOV_MAGIC_V3:
530                 case LOV_MAGIC_SPECIFIC:
531                         ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
532                         break;
533                 case LOV_MAGIC_COMP_V1:
534                 case LOV_MAGIC_FOREIGN:
535                         goto out_env;
536                 default:
537                         CERROR("Invalid LOV magic %08x\n",
538                                ((struct lov_mds_md *)buf)->lmm_magic);
539                         GOTO(out_env, rc = -EINVAL);
540                 }
541
542 out_env:
543                 cl_env_put(env, &refcheck);
544
545                 RETURN(rc);
546         } else if (S_ISDIR(inode->i_mode)) {
547                 struct ptlrpc_request *req = NULL;
548                 struct lov_mds_md *lmm = NULL;
549                 int lmm_size = 0;
550
551                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
552                                       &req, 0);
553                 if (rc < 0)
554                         GOTO(out_req, rc);
555
556                 if (!buf_size)
557                         GOTO(out_req, rc = lmm_size);
558
559                 if (buf_size < lmm_size)
560                         GOTO(out_req, rc = -ERANGE);
561
562                 memcpy(buf, lmm, lmm_size);
563                 GOTO(out_req, rc = lmm_size);
564 out_req:
565                 if (req)
566                         ptlrpc_req_finished(req);
567
568                 RETURN(rc);
569         } else {
570                 RETURN(-ENODATA);
571         }
572 }
573
574 static int ll_xattr_get(const struct xattr_handler *handler,
575                         struct dentry *dentry, struct inode *inode,
576                         const char *name, void *buffer, size_t size)
577 {
578         LASSERT(inode);
579         LASSERT(name);
580
581         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
582                PFID(ll_inode2fid(inode)), inode, name);
583
584         if (!strcmp(name, "lov")) {
585                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
586
587                 return ll_getxattr_lov(inode, buffer, size);
588         }
589
590         return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
591 }
592
593 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
594 {
595         struct inode *inode = dentry->d_inode;
596         struct ll_sb_info *sbi = ll_i2sbi(inode);
597         ktime_t kstart = ktime_get();
598         char *xattr_name;
599         ssize_t rc, rc2;
600         size_t len, rem;
601
602         LASSERT(inode);
603
604         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
605                PFID(ll_inode2fid(inode)), inode);
606
607         rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
608                            OBD_MD_FLXATTRLS);
609         if (rc < 0)
610                 RETURN(rc);
611
612         /*
613          * If we're being called to get the size of the xattr list
614          * (size == 0) then just assume that a lustre.lov xattr
615          * exists.
616          */
617         if (!size)
618                 goto out;
619
620         xattr_name = buffer;
621         rem = rc;
622
623         while (rem > 0) {
624                 len = strnlen(xattr_name, rem - 1) + 1;
625                 rem -= len;
626                 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
627                         /* Skip OK xattr type, leave it in buffer. */
628                         xattr_name += len;
629                         continue;
630                 }
631
632                 /*
633                  * Move up remaining xattrs in buffer
634                  * removing the xattr that is not OK.
635                  */
636                 memmove(xattr_name, xattr_name + len, rem);
637                 rc -= len;
638         }
639
640         rc2 = ll_getxattr_lov(inode, NULL, 0);
641         if (rc2 == -ENODATA)
642                 RETURN(rc);
643
644         if (rc2 < 0)
645                 RETURN(rc2);
646
647         if (size < rc + sizeof(XATTR_LUSTRE_LOV))
648                 RETURN(-ERANGE);
649
650         memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
651
652 out:
653         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR,
654                            ktime_us_delta(ktime_get(), kstart));
655
656         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
657 }
658
659 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
660 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
661                                    struct dentry *dentry, const char *name,
662                                    void *buffer, size_t size)
663 {
664         return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
665                                    buffer, size);
666 }
667
668 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
669                             struct dentry *dentry, const char *name,
670                             void *buffer, size_t size)
671 {
672         return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
673                             size);
674 }
675
676 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
677                                    struct dentry *dentry, const char *name,
678                                    const void *value, size_t size, int flags)
679 {
680         return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
681                                    value, size, flags);
682 }
683
684 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
685                             struct dentry *dentry, const char *name,
686                             const void *value, size_t size, int flags)
687 {
688         return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
689                             size, flags);
690 }
691
692 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
693 const struct xattr_handler *get_xattr_handler(int handler_flag)
694 {
695         int i = 0;
696
697         while (ll_xattr_handlers[i]) {
698                 if (ll_xattr_handlers[i]->flags == handler_flag)
699                         return ll_xattr_handlers[i];
700                 i++;
701         }
702         return NULL;
703 }
704
705 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
706                                    void *buffer, size_t size, int handler_flags)
707 {
708         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
709
710         if (!handler)
711                 return -ENXIO;
712
713         return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
714                                    buffer, size);
715 }
716
717 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
718                             void *buffer, size_t size, int handler_flags)
719 {
720         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
721
722         if (!handler)
723                 return -ENXIO;
724
725         return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
726                             size);
727 }
728
729 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
730                                    const void *value, size_t size, int flags,
731                                    int handler_flags)
732 {
733         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
734
735         if (!handler)
736                 return -ENXIO;
737
738         return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
739                                    value, size, flags);
740 }
741
742 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
743                             const void *value, size_t size, int flags,
744                             int handler_flags)
745 {
746         const struct xattr_handler *handler = get_xattr_handler(handler_flags);
747
748         if (!handler)
749                 return -ENXIO;
750
751         return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
752                             size, flags);
753 }
754 #endif
755
756 static const struct xattr_handler ll_user_xattr_handler = {
757         .prefix = XATTR_USER_PREFIX,
758         .flags = XATTR_USER_T,
759 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
760         .get = ll_xattr_get_common_4_3,
761         .set = ll_xattr_set_common_4_3,
762 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
763         .get = ll_xattr_get_common_3_11,
764         .set = ll_xattr_set_common_3_11,
765 #else
766         .get = ll_xattr_get_common,
767         .set = ll_xattr_set_common,
768 #endif
769 };
770
771 static const struct xattr_handler ll_trusted_xattr_handler = {
772         .prefix = XATTR_TRUSTED_PREFIX,
773         .flags = XATTR_TRUSTED_T,
774 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
775         .get = ll_xattr_get_4_3,
776         .set = ll_xattr_set_4_3,
777 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
778         .get = ll_xattr_get_3_11,
779         .set = ll_xattr_set_3_11,
780 #else
781         .get = ll_xattr_get,
782         .set = ll_xattr_set,
783 #endif
784 };
785
786 static const struct xattr_handler ll_security_xattr_handler = {
787         .prefix = XATTR_SECURITY_PREFIX,
788         .flags = XATTR_SECURITY_T,
789 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
790         .get = ll_xattr_get_common_4_3,
791         .set = ll_xattr_set_common_4_3,
792 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
793         .get = ll_xattr_get_common_3_11,
794         .set = ll_xattr_set_common_3_11,
795 #else
796         .get = ll_xattr_get_common,
797         .set = ll_xattr_set_common,
798 #endif
799 };
800
801 static const struct xattr_handler ll_acl_access_xattr_handler = {
802 #ifdef HAVE_XATTR_HANDLER_NAME
803         .name = XATTR_NAME_POSIX_ACL_ACCESS,
804 #else
805         .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
806 #endif
807         .flags = XATTR_ACL_ACCESS_T,
808 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
809         .get = ll_xattr_get_common_4_3,
810         .set = ll_xattr_set_common_4_3,
811 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
812         .get = ll_xattr_get_common_3_11,
813         .set = ll_xattr_set_common_3_11,
814 #else
815         .get = ll_xattr_get_common,
816         .set = ll_xattr_set_common,
817 #endif
818 };
819
820 static const struct xattr_handler ll_acl_default_xattr_handler = {
821 #ifdef HAVE_XATTR_HANDLER_NAME
822         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
823 #else
824         .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
825 #endif
826         .flags = XATTR_ACL_DEFAULT_T,
827 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
828         .get = ll_xattr_get_common_4_3,
829         .set = ll_xattr_set_common_4_3,
830 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
831         .get = ll_xattr_get_common_3_11,
832         .set = ll_xattr_set_common_3_11,
833 #else
834         .get = ll_xattr_get_common,
835         .set = ll_xattr_set_common,
836 #endif
837 };
838
839 static const struct xattr_handler ll_lustre_xattr_handler = {
840         .prefix = XATTR_LUSTRE_PREFIX,
841         .flags = XATTR_LUSTRE_T,
842 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
843         .get = ll_xattr_get_4_3,
844         .set = ll_xattr_set_4_3,
845 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
846         .get = ll_xattr_get_3_11,
847         .set = ll_xattr_set_3_11,
848 #else
849         .get = ll_xattr_get,
850         .set = ll_xattr_set,
851 #endif
852 };
853
854 const struct xattr_handler *ll_xattr_handlers[] = {
855         &ll_user_xattr_handler,
856         &ll_trusted_xattr_handler,
857         &ll_security_xattr_handler,
858 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
859         &ll_acl_access_xattr_handler,
860         &ll_acl_default_xattr_handler,
861 #endif
862         &ll_lustre_xattr_handler,
863         NULL,
864 };