Whamcloud - gitweb
LU-16634 obdclass: improve iocontrol error messages
[fs/lustre-release.git] / lustre / mdt / mdt_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  * lustre/mdt/mdt_xattr.c
32  *
33  * Lustre Metadata Target (mdt) extended attributes management.
34  *
35  * Author: Peter Braam <braam@clusterfs.com>
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  * Author: Phil Schwan <phil@clusterfs.com>
38  * Author: Huang Hua <huanghua@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_MDS
42
43 #include <linux/xattr.h>
44 #include <obd_class.h>
45 #include <lustre_nodemap.h>
46 #include <lustre_acl.h>
47 #include <lustre_lmv.h>
48 #include "mdt_internal.h"
49
50
51 /* return EADATA length to the caller. negative value means error */
52 static int mdt_getxattr_pack_reply(struct mdt_thread_info *info)
53 {
54         struct req_capsule *pill = info->mti_pill;
55         struct ptlrpc_request *req = mdt_info_req(info);
56         const char *xattr_name;
57         u64 valid;
58         static const char user_string[] = "user.";
59         int size;
60         int rc = 0;
61         int rc2;
62         ENTRY;
63
64         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
65
66         /* Determine how many bytes we need */
67         if (valid == OBD_MD_FLXATTR) {
68                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
69                 if (!xattr_name)
70                         RETURN(-EFAULT);
71
72                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
73                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
74                         RETURN(-EOPNOTSUPP);
75
76                 size = mo_xattr_get(info->mti_env,
77                                     mdt_object_child(info->mti_object),
78                                     &LU_BUF_NULL, xattr_name);
79                 if (size == -ENODATA) {
80                         /* XXX: Some client code will not handle -ENODATA
81                          * for XATTR_NAME_LOV (trusted.lov) properly.
82                          */
83                         if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
84                                 rc = 0;
85                         else
86                                 rc = -ENODATA;
87
88                         size = 0;
89                 }
90         } else if (valid == OBD_MD_FLXATTRLS) {
91                 xattr_name = "list";
92                 size = mo_xattr_list(info->mti_env,
93                                      mdt_object_child(info->mti_object),
94                                      &LU_BUF_NULL);
95         } else if (valid == OBD_MD_FLXATTRALL) {
96                 xattr_name = "all";
97                 /* N.B. eadatasize = 0 is not valid for FLXATTRALL */
98                 /* We could calculate accurate sizes, but this would
99                  * introduce a lot of overhead, let's do it later...
100                  */
101                 size = info->mti_body->mbo_eadatasize;
102                 if (size <= 0 || size > info->mti_mdt->mdt_max_ea_size ||
103                     size & (sizeof(__u32) - 1)) {
104                         DEBUG_REQ(D_ERROR, req,
105                                   "%s: invalid EA size(%d) for FLXATTRALL\n",
106                                   mdt_obd_name(info->mti_mdt), size);
107                         RETURN(-EINVAL);
108                 }
109                 req_capsule_set_size(pill, &RMF_EAVALS, RCL_SERVER, size);
110                 req_capsule_set_size(pill, &RMF_EAVALS_LENS, RCL_SERVER, size);
111         } else {
112                 CDEBUG(D_INFO, "Valid bits: %#llx\n",
113                        info->mti_body->mbo_valid);
114                 RETURN(-EINVAL);
115         }
116
117         if (size < 0) {
118                 if (size != -EOPNOTSUPP && size != -ENOENT)
119                         CERROR("%s: error geting EA size for '%s': rc = %d\n",
120                                mdt_obd_name(info->mti_mdt), xattr_name, size);
121                 RETURN(size);
122         }
123
124         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
125                 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
126                                      LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
127
128         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
129                              info->mti_body->mbo_eadatasize == 0 ? 0 : size);
130
131         rc2 = req_capsule_server_pack(pill);
132         if (rc2 < 0)
133                 RETURN(rc2);
134
135         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
136                 RETURN(-ENOMEM);
137
138         RETURN(rc < 0 ? rc : size);
139 }
140
141 static int mdt_nodemap_map_acl(struct mdt_thread_info *info, void *buf,
142                                size_t size, const char *name,
143                                enum nodemap_tree_type tree_type)
144 {
145         struct lu_nodemap      *nodemap;
146         struct obd_export      *exp = info->mti_exp;
147         int                     rc = size;
148
149         ENTRY;
150
151         if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
152             strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
153                 if (size > info->mti_mdt->mdt_max_ea_size ||
154                      (!exp_connect_large_acl(exp) &&
155                       size > LUSTRE_POSIX_ACL_MAX_SIZE_OLD))
156                         GOTO(out, rc = -ERANGE);
157
158                 nodemap = nodemap_get_from_exp(exp);
159                 if (IS_ERR(nodemap))
160                         GOTO(out, rc = PTR_ERR(nodemap));
161
162                 rc = nodemap_map_acl(nodemap, buf, size, tree_type);
163                 nodemap_putref(nodemap);
164                 if (rc < 0)
165                         GOTO(out, rc);
166         }
167 out:
168         RETURN(rc);
169 }
170
171 static int mdt_getxattr_all(struct mdt_thread_info *info,
172                             struct mdt_body *reqbody, struct mdt_body *repbody,
173                             struct lu_buf *buf, struct md_object *next)
174 {
175         const struct lu_env *env = info->mti_env;
176         char *v, *b, *eadatahead, *eadatatail;
177         __u32 *sizes;
178         int eadatasize, eavallen = 0, eavallens = 0, rc;
179
180         ENTRY;
181
182         /*
183          * The format of the pill is the following:
184          * EADATA:      attr1\0attr2\0...attrn\0
185          * EAVALS:      val1val2...valn
186          * EAVALS_LENS: 4,4,...4
187          */
188
189         eadatahead = buf->lb_buf;
190
191         /* Fill out EADATA first */
192         rc = mo_xattr_list(env, next, buf);
193         if (rc < 0)
194                 GOTO(out_shrink, rc);
195
196         eadatasize = rc;
197         eadatatail = eadatahead + eadatasize;
198
199         v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
200         sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
201
202         /* Fill out EAVALS and EAVALS_LENS */
203         for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
204                 buf->lb_buf = v;
205                 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
206                 rc = mo_xattr_get(env, next, buf, b);
207                 if (rc < 0)
208                         GOTO(out_shrink, rc);
209                 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, b,
210                                          NODEMAP_FS_TO_CLIENT);
211                 if (rc < 0)
212                         GOTO(out_shrink, rc);
213                 sizes[eavallens] = rc;
214                 eavallens++;
215                 eavallen += rc;
216         }
217
218 out_shrink:
219         if (rc < 0) {
220                 eadatasize = 0;
221                 eavallens = 0;
222                 eavallen = 0;
223         }
224         repbody->mbo_aclsize = eavallen;
225         repbody->mbo_max_mdsize = eavallens;
226
227         req_capsule_shrink(info->mti_pill, &RMF_EAVALS, eavallen, RCL_SERVER);
228         req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
229                            eavallens * sizeof(__u32), RCL_SERVER);
230         req_capsule_shrink(info->mti_pill, &RMF_EADATA, eadatasize, RCL_SERVER);
231
232         if (rc >= 0)
233                 RETURN(eadatasize);
234         return rc;
235 }
236
237 int mdt_getxattr(struct mdt_thread_info *info)
238 {
239         struct ptlrpc_request  *req = mdt_info_req(info);
240         struct mdt_body        *reqbody;
241         struct mdt_body        *repbody = NULL;
242         struct md_object       *next;
243         struct lu_buf          *buf;
244         int                     easize, rc;
245         u64                     valid;
246         ktime_t                 kstart = ktime_get();
247         ENTRY;
248
249         LASSERT(info->mti_object != NULL);
250         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
251
252         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
253
254         rc = req_check_sepol(info->mti_pill);
255         if (rc)
256                 RETURN(err_serious(rc));
257
258         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
259         if (reqbody == NULL)
260                 RETURN(err_serious(-EFAULT));
261
262         rc = mdt_init_ucred(info, reqbody);
263         if (rc)
264                 RETURN(err_serious(rc));
265
266         next = mdt_object_child(info->mti_object);
267         easize = mdt_getxattr_pack_reply(info);
268         if (easize == -ENODATA)
269                 GOTO(out, rc = easize);
270         else if (easize < 0)
271                 GOTO(out, rc = err_serious(easize));
272
273         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
274         LASSERT(repbody != NULL);
275
276         /* No need further getxattr. */
277         if (easize == 0 || reqbody->mbo_eadatasize == 0)
278                 GOTO(out, rc = easize);
279
280         buf = &info->mti_buf;
281         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
282         buf->lb_len = easize;
283
284         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
285
286         if (valid == OBD_MD_FLXATTR) {
287                 const char *xattr_name = req_capsule_client_get(info->mti_pill,
288                                                                 &RMF_NAME);
289                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
290                 if (rc < 0)
291                         GOTO(out, rc);
292
293                 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, xattr_name,
294                                          NODEMAP_FS_TO_CLIENT);
295         } else if (valid == OBD_MD_FLXATTRLS) {
296                 CDEBUG(D_INODE, "listxattr\n");
297
298                 rc = mo_xattr_list(info->mti_env, next, buf);
299                 if (rc < 0)
300                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
301         } else if (valid == OBD_MD_FLXATTRALL) {
302                 rc = mdt_getxattr_all(info, reqbody, repbody,
303                                       buf, next);
304         } else
305                 LBUG();
306
307         EXIT;
308 out:
309         if (rc >= 0) {
310                 mdt_counter_incr(req, LPROC_MDT_GETXATTR,
311                                  ktime_us_delta(ktime_get(), kstart));
312                 /* LU-11109: Set OBD_MD_FLXATTR on success so that
313                  * newer clients can distinguish between nonexistent
314                  * xattrs and zero length values.
315                  */
316                 repbody->mbo_valid |= OBD_MD_FLXATTR;
317                 repbody->mbo_eadatasize = rc;
318                 rc = 0;
319         }
320         mdt_exit_ucred(info);
321         return rc;
322 }
323
324 /* update dir layout after migration/restripe */
325 int mdt_dir_layout_update(struct mdt_thread_info *info)
326 {
327         const struct lu_env *env = info->mti_env;
328         struct mdt_device *mdt = info->mti_mdt;
329         struct lu_ucred *uc = mdt_ucred(info);
330         struct mdt_reint_record *rr = &info->mti_rr;
331         struct lmv_user_md *lmu = rr->rr_eadata;
332         __u32 lum_stripe_count = lmu->lum_stripe_count;
333         struct md_layout_change *mlc = &info->mti_mlc;
334         struct lmv_mds_md_v1 *lmv;
335         struct md_attr *ma = &info->mti_attr;
336         struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
337         struct mdt_object *pobj = NULL;
338         struct mdt_object *obj;
339         struct mdt_lock_handle *lhp = NULL;
340         struct mdt_lock_handle *lhc;
341         bool shrink = false;
342         int rc;
343
344         ENTRY;
345
346         if (!mdt->mdt_enable_dir_migration)
347                 RETURN(-EPERM);
348
349         /* we want rbac roles to have precedence over any other
350          * permission or capability checks
351          */
352         if (!uc->uc_rbac_dne_ops ||
353             (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
354              uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
355              mdt->mdt_enable_remote_dir_gid != -1))
356                 RETURN(-EPERM);
357
358         obj = mdt_object_find(env, mdt, rr->rr_fid1);
359         if (IS_ERR(obj))
360                 RETURN(PTR_ERR(obj));
361
362         /* get parent from PFID */
363         rc = mdt_attr_get_pfid(info, obj, &ma->ma_pfid);
364         if (rc)
365                 GOTO(put_obj, rc);
366
367         pobj = mdt_object_find(env, mdt, &ma->ma_pfid);
368         if (IS_ERR(pobj))
369                 GOTO(put_obj, rc = PTR_ERR(pobj));
370
371         /* revoke object remote LOOKUP lock */
372         if (mdt_object_remote(pobj)) {
373                 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
374                 if (rc)
375                         GOTO(put_pobj, rc);
376         }
377
378         /*
379          * lock parent if dir will be shrunk to 1 stripe, because dir will be
380          * converted to normal directory, as will change dir FID and update
381          * namespace of parent.
382          */
383         lhp = &info->mti_lh[MDT_LH_PARENT];
384         mdt_lock_reg_init(lhp, LCK_PW);
385
386         if (le32_to_cpu(lmu->lum_stripe_count) < 2) {
387                 rc = mdt_reint_object_lock(info, pobj, lhp,
388                                            MDS_INODELOCK_UPDATE, true);
389                 if (rc)
390                         GOTO(put_pobj, rc);
391         }
392
393         /* lock object */
394         lhc = &info->mti_lh[MDT_LH_CHILD];
395         mdt_lock_reg_init(lhc, LCK_EX);
396         rc = mdt_reint_striped_lock(info, obj, lhc, MDS_INODELOCK_FULL, einfo,
397                                     true);
398         if (rc)
399                 GOTO(unlock_pobj, rc);
400
401         rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
402         if (rc)
403                 GOTO(unlock_obj, rc);
404
405         /* user may run 'lfs migrate' multiple times, so it's shrunk already */
406         if (!(ma->ma_valid & MA_LMV))
407                 GOTO(unlock_obj, rc = -EALREADY);
408
409         lmv = &ma->ma_lmv->lmv_md_v1;
410         if (!lmv_is_sane(lmv))
411                 GOTO(unlock_obj, rc = -EBADF);
412
413         /* ditto */
414         if (!lmv_is_layout_changing(lmv))
415                 GOTO(unlock_obj, rc = -EALREADY);
416
417         lum_stripe_count = lmu->lum_stripe_count;
418         if (!lum_stripe_count)
419                 lum_stripe_count = cpu_to_le32(1);
420
421         if (lmv_is_migrating(lmv)) {
422                 if (lmv->lmv_migrate_offset != lum_stripe_count) {
423                         CERROR("%s: "DFID" migrate mdt count mismatch %u != %u\n",
424                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
425                                 lmv->lmv_migrate_offset, lmu->lum_stripe_count);
426                         GOTO(unlock_obj, rc = -EINVAL);
427                 }
428
429                 /* lum_stripe_offset is not checked, because subdir is migrated
430                  * to where its parent is located to avoid unnecessary remote
431                  * directory.
432                  */
433
434                 if (lum_stripe_count > 1 && lmu->lum_hash_type &&
435                     lmu->lum_hash_type !=
436                     (lmv->lmv_hash_type & cpu_to_le32(LMV_HASH_TYPE_MASK))) {
437                         CERROR("%s: "DFID" migrate mdt hash mismatch %u != %u\n",
438                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
439                                 lmv->lmv_hash_type, lmu->lum_hash_type);
440                         GOTO(unlock_obj, rc = -EINVAL);
441                 }
442
443                 shrink = true;
444         } else if (lmv_is_splitting(lmv)) {
445                 if (lmv->lmv_stripe_count != lum_stripe_count) {
446                         CERROR("%s: "DFID" stripe count mismatch %u != %u\n",
447                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
448                                 lmv->lmv_stripe_count, lmu->lum_stripe_count);
449                         GOTO(unlock_obj, rc = -EINVAL);
450                 }
451
452                 if (lmu->lum_stripe_offset != LMV_OFFSET_DEFAULT) {
453                         CERROR("%s: "DFID" dir split offset %u != -1\n",
454                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
455                                 lmu->lum_stripe_offset);
456                         GOTO(unlock_obj, rc = -EINVAL);
457                 }
458
459                 if (lmu->lum_hash_type &&
460                     lmu->lum_hash_type !=
461                     (lmv->lmv_hash_type & cpu_to_le32(LMV_HASH_TYPE_MASK))) {
462                         CERROR("%s: "DFID" split hash mismatch %u != %u\n",
463                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
464                                 lmv->lmv_hash_type, lmu->lum_hash_type);
465                         GOTO(unlock_obj, rc = -EINVAL);
466                 }
467         } else if (lmv_is_merging(lmv)) {
468                 if (lmv->lmv_merge_offset != lum_stripe_count) {
469                         CERROR("%s: "DFID" stripe count mismatch %u != %u\n",
470                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
471                                 lmv->lmv_merge_offset, lmu->lum_stripe_count);
472                         GOTO(unlock_obj, rc = -EINVAL);
473                 }
474
475                 if (lmu->lum_stripe_offset != LMV_OFFSET_DEFAULT) {
476                         CERROR("%s: "DFID" dir merge offset %u != -1\n",
477                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
478                                 lmu->lum_stripe_offset);
479                         GOTO(unlock_obj, rc = -EINVAL);
480                 }
481
482                 if (lmu->lum_hash_type &&
483                     (lmu->lum_hash_type & cpu_to_le32(LMV_HASH_TYPE_MASK)) !=
484                     (lmv->lmv_merge_hash & cpu_to_le32(LMV_HASH_TYPE_MASK))) {
485                         CERROR("%s: "DFID" merge hash mismatch %u != %u\n",
486                                 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
487                                 lmv->lmv_merge_hash, lmu->lum_hash_type);
488                         GOTO(unlock_obj, rc = -EINVAL);
489                 }
490
491                 if (lum_stripe_count < lmv->lmv_stripe_count)
492                         shrink = true;
493         }
494
495         if (shrink) {
496                 mlc->mlc_opc = MD_LAYOUT_SHRINK;
497                 mlc->mlc_buf.lb_buf = rr->rr_eadata;
498                 mlc->mlc_buf.lb_len = rr->rr_eadatalen;
499                 rc = mo_layout_change(env, mdt_object_child(obj), mlc);
500         } else {
501                 struct lu_buf *buf = &info->mti_buf;
502                 u32 version = le32_to_cpu(lmv->lmv_layout_version);
503
504                 lmv->lmv_hash_type &= ~LMV_HASH_FLAG_LAYOUT_CHANGE;
505                 lmv->lmv_layout_version = cpu_to_le32(++version);
506                 lmv->lmv_migrate_offset = 0;
507                 lmv->lmv_migrate_hash = 0;
508                 buf->lb_buf = lmv;
509                 buf->lb_len = sizeof(*lmv);
510                 rc = mo_xattr_set(env, mdt_object_child(obj), buf,
511                                   XATTR_NAME_LMV, LU_XATTR_REPLACE);
512         }
513         GOTO(unlock_obj, rc);
514
515 unlock_obj:
516         mdt_reint_striped_unlock(info, obj, lhc, einfo, rc);
517 unlock_pobj:
518         mdt_object_unlock(info, pobj, lhp, rc);
519 put_pobj:
520         mdt_object_put(env, pobj);
521 put_obj:
522         mdt_object_put(env, obj);
523
524         return rc;
525 }
526
527 int mdt_reint_setxattr(struct mdt_thread_info *info,
528                        struct mdt_lock_handle *unused)
529 {
530         struct ptlrpc_request   *req = mdt_info_req(info);
531         struct mdt_lock_handle  *lh;
532         const struct lu_env     *env  = info->mti_env;
533         struct lu_buf           *buf  = &info->mti_buf;
534         struct mdt_reint_record *rr   = &info->mti_rr;
535         struct md_attr          *ma = &info->mti_attr;
536         struct lu_attr          *attr = &info->mti_attr.ma_attr;
537         struct mdt_object       *obj;
538         struct md_object        *child;
539         __u64                    valid = attr->la_valid;
540         const char              *xattr_name = rr->rr_name.ln_name;
541         int                      xattr_len = rr->rr_eadatalen;
542         __u64                    lockpart = MDS_INODELOCK_UPDATE;
543         ktime_t                  kstart = ktime_get();
544         int                      rc;
545         ENTRY;
546
547         CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
548                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
549
550         if (info->mti_dlm_req)
551                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
552
553         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
554                 RETURN(err_serious(-ENOMEM));
555
556         rc = mdt_init_ucred_reint(info);
557         if (rc != 0)
558                 RETURN(rc);
559
560         if (strncmp(xattr_name, XATTR_USER_PREFIX,
561                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
562                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
563                         GOTO(out, rc = -EOPNOTSUPP);
564         } else if (strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
565                     sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0) {
566
567                 /* setxattr(LMV) with lum is used to shrink dir layout */
568                 if (strcmp(xattr_name, XATTR_NAME_LMV) == 0) {
569                         __u32 *magic = rr->rr_eadata;
570
571                         /* we don't let to remove LMV? */
572                         if (!rr->rr_eadata)
573                                 GOTO(out, rc = 0);
574
575                         if (le32_to_cpu(*magic) == LMV_USER_MAGIC ||
576                             le32_to_cpu(*magic) == LMV_USER_MAGIC_SPECIFIC) {
577                                 rc = mdt_dir_layout_update(info);
578                                 GOTO(out, rc);
579                         }
580                 }
581
582                 if (!cap_raised(mdt_ucred(info)->uc_cap, CAP_SYS_ADMIN))
583                         GOTO(out, rc = -EPERM);
584
585                 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0 ||
586                     strcmp(xattr_name, XATTR_NAME_LMA) == 0 ||
587                     strcmp(xattr_name, XATTR_NAME_LMV) == 0 ||
588                     strcmp(xattr_name, XATTR_NAME_LINK) == 0 ||
589                     strcmp(xattr_name, XATTR_NAME_FID) == 0 ||
590                     strcmp(xattr_name, XATTR_NAME_VERSION) == 0 ||
591                     strcmp(xattr_name, XATTR_NAME_SOM) == 0 ||
592                     strcmp(xattr_name, XATTR_NAME_HSM) == 0 ||
593                     strcmp(xattr_name, XATTR_NAME_LFSCK_NAMESPACE) == 0)
594                         GOTO(out, rc = 0);
595         } else if ((valid & OBD_MD_FLXATTR) &&
596                    (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
597                     strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0)) {
598                 rc = mdt_nodemap_map_acl(info, rr->rr_eadata, xattr_len,
599                                          xattr_name, NODEMAP_CLIENT_TO_FS);
600                 if (rc < 0)
601                         GOTO(out, rc);
602                 /* ACLs were mapped out, return an error so the user knows */
603                 if (rc != xattr_len)
604                         GOTO(out, rc = -EPERM);
605         } else if ((strlen(xattr_name) > sizeof(XATTR_LUSTRE_LOV)) &&
606                    strncmp(xattr_name, XATTR_LUSTRE_LOV,
607                            strlen(XATTR_LUSTRE_LOV)) == 0) {
608
609                 if (!allowed_lustre_lov(xattr_name)) {
610                         CERROR("%s: invalid xattr name: %s\n",
611                                mdt_obd_name(info->mti_mdt), xattr_name);
612                         GOTO(out, rc = -EINVAL);
613                 }
614
615                 lockpart |= MDS_INODELOCK_LAYOUT;
616         }
617
618         /* Revoke all clients' lookup lock, since the access
619          * permissions for this inode is changed when ACL_ACCESS is
620          * set. This isn't needed for ACL_DEFAULT, since that does
621          * not change the access permissions of this inode, nor any
622          * other existing inodes. It is setting the ACLs inherited
623          * by new directories/files at create time.
624          */
625         /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
626         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
627                 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
628         /* We need to take the lock on behalf of old clients so that newer
629          * clients flush their xattr caches
630          */
631         else
632                 lockpart |= MDS_INODELOCK_XATTR;
633
634         lh = &info->mti_lh[MDT_LH_PARENT];
635         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
636          * to cancel them.
637          */
638         mdt_lock_reg_init(lh, LCK_EX);
639         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
640         if (IS_ERR(obj))
641                 GOTO(out, rc = PTR_ERR(obj));
642
643         tgt_vbr_obj_set(env, mdt_obj2dt(obj));
644         rc = mdt_version_get_check_save(info, obj, 0);
645         if (rc)
646                 GOTO(out_unlock, rc);
647
648         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
649                 /* This isn't strictly an error, but all current clients
650                  * should set OBD_MD_FLCTIME when setting attributes.
651                  */
652                 CWARN("%s: client miss to set OBD_MD_FLCTIME when setxattr %s: [object "DFID"] [valid %llu]\n",
653                       mdt_obd_name(info->mti_mdt), xattr_name,
654                       PFID(rr->rr_fid1), valid);
655                 attr->la_ctime = ktime_get_real_seconds();
656         }
657         attr->la_valid = LA_CTIME;
658         child = mdt_object_child(obj);
659         if (valid & OBD_MD_FLXATTR) {
660                 int     flags = 0;
661
662                 if (attr->la_flags & XATTR_REPLACE)
663                         flags |= LU_XATTR_REPLACE;
664
665                 if (attr->la_flags & XATTR_CREATE)
666                         flags |= LU_XATTR_CREATE;
667
668                 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
669                                OBD_FAIL_MDS_SETXATTR_WRITE);
670
671                 buf->lb_buf = rr->rr_eadata;
672                 buf->lb_len = xattr_len;
673                 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
674                 /* update ctime after xattr changed */
675                 if (rc == 0) {
676                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
677                         mo_attr_set(env, child, ma);
678                 }
679         } else if (valid & OBD_MD_FLXATTRRM) {
680                 rc = mo_xattr_del(env, child, xattr_name);
681                 /* update ctime after xattr changed */
682                 if (rc == 0) {
683                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
684                         mo_attr_set(env, child, ma);
685                 }
686         } else {
687                 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
688                 rc = -EINVAL;
689         }
690
691         if (rc == 0)
692                 mdt_counter_incr(req, LPROC_MDT_SETXATTR,
693                                  ktime_us_delta(ktime_get(), kstart));
694
695         EXIT;
696 out_unlock:
697         mdt_object_unlock_put(info, obj, lh, rc);
698 out:
699         mdt_exit_ucred(info);
700         return rc;
701 }