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