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