Whamcloud - gitweb
9bd7526595158985c60d313b84a8be6e687a8bbb
[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 mdt_reint_record *rr = &info->mti_rr;
314         struct lmv_user_md *lmu = rr->rr_eadata;
315         __u32 lum_stripe_count = lmu->lum_stripe_count;
316         struct lu_buf *buf = &info->mti_buf;
317         struct lmv_mds_md_v1 *lmv;
318         struct md_attr *ma = &info->mti_attr;
319         struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
320         struct mdt_object *pobj = NULL;
321         struct mdt_object *obj;
322         struct mdt_lock_handle *lhp = NULL;
323         struct mdt_lock_handle *lhc;
324         int rc;
325
326         ENTRY;
327
328         rc = mdt_remote_dir_permission(info);
329         if (rc)
330                 RETURN(rc);
331
332         /* mti_big_lmm is used to save LMV, but it may be uninitialized. */
333         if (unlikely(!info->mti_big_lmm)) {
334                 info->mti_big_lmmsize = lmv_mds_md_size(64, LMV_MAGIC);
335                 OBD_ALLOC(info->mti_big_lmm, info->mti_big_lmmsize);
336                 if (!info->mti_big_lmm)
337                         RETURN(-ENOMEM);
338         }
339
340         obj = mdt_object_find(env, mdt, rr->rr_fid1);
341         if (IS_ERR(obj))
342                 RETURN(PTR_ERR(obj));
343
344 relock:
345         /* lock object */
346         lhc = &info->mti_lh[MDT_LH_CHILD];
347         mdt_lock_reg_init(lhc, LCK_EX);
348         rc = mdt_reint_striped_lock(info, obj, lhc, MDS_INODELOCK_FULL, einfo,
349                                     true);
350         if (rc)
351                 GOTO(put_obj, rc);
352
353         ma->ma_lmv = info->mti_big_lmm;
354         ma->ma_lmv_size = info->mti_big_lmmsize;
355         ma->ma_valid = 0;
356         rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
357         if (rc)
358                 GOTO(unlock_obj, rc);
359
360         /* user may run 'lfs migrate' multiple times, so it's shrunk already */
361         if (!(ma->ma_valid & MA_LMV))
362                 GOTO(unlock_obj, rc = -EALREADY);
363
364         lmv = &ma->ma_lmv->lmv_md_v1;
365
366         /* ditto */
367         if (!(le32_to_cpu(lmv->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION))
368                 GOTO(unlock_obj, rc = -EALREADY);
369
370         lum_stripe_count = lmu->lum_stripe_count;
371         if (!lum_stripe_count)
372                 lum_stripe_count = cpu_to_le32(1);
373
374         if (lmv->lmv_migrate_offset != lum_stripe_count) {
375                 CERROR("%s: "DFID" migrate mdt count mismatch %u != %u\n",
376                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
377                         lmv->lmv_migrate_offset, lmu->lum_stripe_count);
378                 GOTO(unlock_obj, rc = -EINVAL);
379         }
380
381         if (lmv->lmv_master_mdt_index != lmu->lum_stripe_offset) {
382                 CERROR("%s: "DFID" migrate mdt index mismatch %u != %u\n",
383                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
384                         lmv->lmv_master_mdt_index, lmu->lum_stripe_offset);
385                 GOTO(unlock_obj, rc = -EINVAL);
386         }
387
388         if (lum_stripe_count > 1 &&
389             (lmv->lmv_hash_type & cpu_to_le32(LMV_HASH_TYPE_MASK)) !=
390             lmu->lum_hash_type) {
391                 CERROR("%s: "DFID" migrate mdt hash mismatch %u != %u\n",
392                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
393                         lmv->lmv_hash_type, lmu->lum_hash_type);
394                 GOTO(unlock_obj, rc = -EINVAL);
395         }
396
397         if (le32_to_cpu(lmu->lum_stripe_count) < 2 && !pobj) {
398                 /*
399                  * lock parent because dir will be shrunk to be 1 stripe, which
400                  * should be converted to normal directory, but that will
401                  * change dir fid and update namespace of parent.
402                  */
403                 lhp = &info->mti_lh[MDT_LH_PARENT];
404                 mdt_lock_reg_init(lhp, LCK_PW);
405
406                 /* get parent from PFID */
407                 ma->ma_need |= MA_PFID;
408                 ma->ma_valid = 0;
409                 rc = mdt_attr_get_complex(info, obj, ma);
410                 if (rc)
411                         GOTO(unlock_obj, rc);
412
413                 if (!(ma->ma_valid & MA_PFID))
414                         GOTO(unlock_obj, rc = -ENOTSUPP);
415
416                 pobj = mdt_object_find(env, mdt, &ma->ma_pfid);
417                 if (IS_ERR(pobj)) {
418                         rc = PTR_ERR(pobj);
419                         pobj = NULL;
420                         GOTO(unlock_obj, rc);
421                 }
422
423                 mdt_reint_striped_unlock(info, obj, lhc, einfo, 1);
424
425                 if (mdt_object_remote(pobj)) {
426                         rc = mdt_remote_object_lock(info, pobj, rr->rr_fid1,
427                                                     &lhp->mlh_rreg_lh, LCK_EX,
428                                                     MDS_INODELOCK_LOOKUP,
429                                                     false);
430                         if (rc != ELDLM_OK) {
431                                 mdt_object_put(env, pobj);
432                                 GOTO(put_obj, rc);
433                         }
434                         mdt_object_unlock(info, NULL, lhp, 1);
435                 }
436
437                 rc = mdt_reint_object_lock(info, pobj, lhp,
438                                            MDS_INODELOCK_UPDATE, true);
439                 if (rc) {
440                         mdt_object_put(env, pobj);
441                         GOTO(put_obj, rc);
442                 }
443
444                 goto relock;
445         }
446
447         buf->lb_buf = rr->rr_eadata;
448         buf->lb_len = rr->rr_eadatalen;
449         rc = mo_xattr_set(env, mdt_object_child(obj), buf, XATTR_NAME_LMV, 0);
450         GOTO(unlock_obj, rc);
451
452 unlock_obj:
453         mdt_reint_striped_unlock(info, obj, lhc, einfo, rc);
454         if (pobj)
455                 mdt_object_unlock_put(info, pobj, lhp, rc);
456 put_obj:
457         mdt_object_put(env, obj);
458
459         return rc;
460 }
461
462 int mdt_reint_setxattr(struct mdt_thread_info *info,
463                        struct mdt_lock_handle *unused)
464 {
465         struct ptlrpc_request   *req = mdt_info_req(info);
466         struct mdt_lock_handle  *lh;
467         const struct lu_env     *env  = info->mti_env;
468         struct lu_buf           *buf  = &info->mti_buf;
469         struct mdt_reint_record *rr   = &info->mti_rr;
470         struct md_attr          *ma = &info->mti_attr;
471         struct lu_attr          *attr = &info->mti_attr.ma_attr;
472         struct mdt_object       *obj;
473         struct md_object        *child;
474         __u64                    valid = attr->la_valid;
475         const char              *xattr_name = rr->rr_name.ln_name;
476         int                      xattr_len = rr->rr_eadatalen;
477         __u64                    lockpart = MDS_INODELOCK_UPDATE;
478         int                      rc;
479         ENTRY;
480
481         CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
482                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
483
484         if (info->mti_dlm_req)
485                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
486
487         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
488                 RETURN(err_serious(-ENOMEM));
489
490         rc = mdt_init_ucred_reint(info);
491         if (rc != 0)
492                 RETURN(rc);
493
494         if (strncmp(xattr_name, XATTR_USER_PREFIX,
495                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
496                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
497                         GOTO(out, rc = -EOPNOTSUPP);
498         } else if (strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
499                     sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0) {
500
501                 /* setxattr(LMV) with lum is used to shrink dir layout */
502                 if (strcmp(xattr_name, XATTR_NAME_LMV) == 0) {
503                         __u32 *magic = rr->rr_eadata;
504
505                         /* we don't let to remove LMV? */
506                         if (!rr->rr_eadata)
507                                 GOTO(out, rc = 0);
508
509                         if (le32_to_cpu(*magic) == LMV_USER_MAGIC ||
510                             le32_to_cpu(*magic) == LMV_USER_MAGIC_SPECIFIC) {
511                                 rc = mdt_dir_layout_shrink(info);
512                                 GOTO(out, rc);
513                         }
514                 }
515
516                 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
517                         GOTO(out, rc = -EPERM);
518
519                 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0 ||
520                     strcmp(xattr_name, XATTR_NAME_LMA) == 0 ||
521                     strcmp(xattr_name, XATTR_NAME_LMV) == 0 ||
522                     strcmp(xattr_name, XATTR_NAME_LINK) == 0 ||
523                     strcmp(xattr_name, XATTR_NAME_FID) == 0 ||
524                     strcmp(xattr_name, XATTR_NAME_VERSION) == 0 ||
525                     strcmp(xattr_name, XATTR_NAME_SOM) == 0 ||
526                     strcmp(xattr_name, XATTR_NAME_HSM) == 0 ||
527                     strcmp(xattr_name, XATTR_NAME_LFSCK_NAMESPACE) == 0)
528                         GOTO(out, rc = 0);
529         } else if ((valid & OBD_MD_FLXATTR) &&
530                    (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
531                     strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0)) {
532                 rc = mdt_nodemap_map_acl(info, rr->rr_eadata, xattr_len,
533                                          xattr_name, NODEMAP_CLIENT_TO_FS);
534                 if (rc < 0)
535                         GOTO(out, rc);
536                 /* ACLs were mapped out, return an error so the user knows */
537                 if (rc != xattr_len)
538                         GOTO(out, rc = -EPERM);
539         } else if ((strlen(xattr_name) > strlen(XATTR_LUSTRE_LOV) + 1) &&
540                    strncmp(xattr_name, XATTR_LUSTRE_LOV,
541                            strlen(XATTR_LUSTRE_LOV)) == 0) {
542
543                 if (strncmp(xattr_name, XATTR_LUSTRE_LOV".add",
544                             strlen(XATTR_LUSTRE_LOV".add")) &&
545                     strncmp(xattr_name, XATTR_LUSTRE_LOV".set",
546                             strlen(XATTR_LUSTRE_LOV".set")) &&
547                     strncmp(xattr_name, XATTR_LUSTRE_LOV".del",
548                             strlen(XATTR_LUSTRE_LOV".del"))) {
549                         CERROR("%s: invalid xattr name: %s\n",
550                                mdt_obd_name(info->mti_mdt), xattr_name);
551                         GOTO(out, rc = -EINVAL);
552                 }
553
554                 lockpart |= MDS_INODELOCK_LAYOUT;
555         }
556
557         /* Revoke all clients' lookup lock, since the access
558          * permissions for this inode is changed when ACL_ACCESS is
559          * set. This isn't needed for ACL_DEFAULT, since that does
560          * not change the access permissions of this inode, nor any
561          * other existing inodes. It is setting the ACLs inherited
562          * by new directories/files at create time. */
563         /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
564         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
565                 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
566         /* We need to take the lock on behalf of old clients so that newer
567          * clients flush their xattr caches */
568         else
569                 lockpart |= MDS_INODELOCK_XATTR;
570
571         lh = &info->mti_lh[MDT_LH_PARENT];
572         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
573          * to cancel them. */
574         mdt_lock_reg_init(lh, LCK_EX);
575         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
576         if (IS_ERR(obj))
577                 GOTO(out, rc = PTR_ERR(obj));
578
579         tgt_vbr_obj_set(env, mdt_obj2dt(obj));
580         rc = mdt_version_get_check_save(info, obj, 0);
581         if (rc)
582                 GOTO(out_unlock, rc);
583
584         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
585                 /* This isn't strictly an error, but all current clients
586                  * should set OBD_MD_FLCTIME when setting attributes. */
587                 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
588                       "setxattr %s: [object "DFID"] [valid %llu]\n",
589                       mdt_obd_name(info->mti_mdt), xattr_name,
590                       PFID(rr->rr_fid1), valid);
591                 attr->la_ctime = ktime_get_real_seconds();
592         }
593         attr->la_valid = LA_CTIME;
594         child = mdt_object_child(obj);
595         if (valid & OBD_MD_FLXATTR) {
596                 int     flags = 0;
597
598                 if (attr->la_flags & XATTR_REPLACE)
599                         flags |= LU_XATTR_REPLACE;
600
601                 if (attr->la_flags & XATTR_CREATE)
602                         flags |= LU_XATTR_CREATE;
603
604                 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
605                                OBD_FAIL_MDS_SETXATTR_WRITE);
606
607                 buf->lb_buf = rr->rr_eadata;
608                 buf->lb_len = xattr_len;
609                 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
610                 /* update ctime after xattr changed */
611                 if (rc == 0) {
612                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
613                         mo_attr_set(env, child, ma);
614                 }
615         } else if (valid & OBD_MD_FLXATTRRM) {
616                 rc = mo_xattr_del(env, child, xattr_name);
617                 /* update ctime after xattr changed */
618                 if (rc == 0) {
619                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
620                         mo_attr_set(env, child, ma);
621                 }
622         } else {
623                 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
624                 rc = -EINVAL;
625         }
626
627         if (rc == 0)
628                 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
629
630         EXIT;
631 out_unlock:
632         mdt_object_unlock_put(info, obj, lh, rc);
633 out:
634         mdt_exit_ucred(info);
635         return rc;
636 }