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