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