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