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