4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_xattr.c
34 * Lustre Metadata Target (mdt) extended attributes management.
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>
42 #define DEBUG_SUBSYSTEM S_MDS
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"
51 /* return EADATA length to the caller. negative value means error */
52 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
54 struct req_capsule *pill = info->mti_pill;
55 struct ptlrpc_request *req = mdt_info_req(info);
56 const char *xattr_name;
58 static const char user_string[] = "user.";
64 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
66 /* Determine how many bytes we need */
67 if (valid == OBD_MD_FLXATTR) {
68 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
72 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
73 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
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)
89 } else if (valid == OBD_MD_FLXATTRLS) {
91 size = mo_xattr_list(info->mti_env,
92 mdt_object_child(info->mti_object),
94 } else if (valid == OBD_MD_FLXATTRALL) {
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);
103 CDEBUG(D_INFO, "Valid bits: %#llx\n",
104 info->mti_body->mbo_valid);
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);
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);
119 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
120 info->mti_body->mbo_eadatasize == 0 ? 0 : size);
122 rc2 = req_capsule_server_pack(pill);
126 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
129 RETURN(rc < 0 ? rc : size);
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)
136 struct lu_nodemap *nodemap;
137 struct obd_export *exp = info->mti_exp;
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);
149 nodemap = nodemap_get_from_exp(exp);
151 GOTO(out, rc = PTR_ERR(nodemap));
153 rc = nodemap_map_acl(nodemap, buf, size, tree_type);
154 nodemap_putref(nodemap);
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)
166 const struct lu_env *env = info->mti_env;
167 char *v, *b, *eadatahead, *eadatatail;
169 int eadatasize, eavallen = 0, eavallens = 0, rc;
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
180 eadatahead = buf->lb_buf;
182 /* Fill out EADATA first */
183 rc = mo_xattr_list(env, next, buf);
185 GOTO(out_shrink, rc);
188 eadatatail = eadatahead + eadatasize;
190 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
191 sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
193 /* Fill out EAVALS and EAVALS_LENS */
194 for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
196 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
197 rc = mo_xattr_get(env, next, buf, b);
199 GOTO(out_shrink, rc);
200 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, b,
201 NODEMAP_FS_TO_CLIENT);
203 GOTO(out_shrink, rc);
204 sizes[eavallens] = rc;
215 repbody->mbo_aclsize = eavallen;
216 repbody->mbo_max_mdsize = eavallens;
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);
228 int mdt_getxattr(struct mdt_thread_info *info)
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;
239 LASSERT(info->mti_object != NULL);
240 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
242 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
244 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
246 RETURN(err_serious(-EFAULT));
248 rc = mdt_init_ucred(info, reqbody);
250 RETURN(err_serious(rc));
252 next = mdt_object_child(info->mti_object);
253 easize = mdt_getxattr_pack_reply(info);
254 if (easize == -ENODATA)
255 GOTO(out, rc = easize);
257 GOTO(out, rc = err_serious(easize));
259 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
260 LASSERT(repbody != NULL);
262 /* No need further getxattr. */
263 if (easize == 0 || reqbody->mbo_eadatasize == 0)
264 GOTO(out, rc = easize);
266 buf = &info->mti_buf;
267 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
268 buf->lb_len = easize;
270 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
272 if (valid == OBD_MD_FLXATTR) {
273 const char *xattr_name = req_capsule_client_get(info->mti_pill,
275 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
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");
284 rc = mo_xattr_list(info->mti_env, next, buf);
286 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
287 } else if (valid == OBD_MD_FLXATTRALL) {
288 rc = mdt_getxattr_all(info, reqbody, repbody,
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;
304 mdt_exit_ucred(info);
308 /* shrink dir layout after migration */
309 static int mdt_dir_layout_shrink(struct mdt_thread_info *info)
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;
329 if (!mdt->mdt_enable_dir_migration)
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)
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)
345 obj = mdt_object_find(env, mdt, rr->rr_fid1);
347 RETURN(PTR_ERR(obj));
349 /* get parent from PFID */
350 rc = mdt_attr_get_pfid(info, obj, &ma->ma_pfid);
354 pobj = mdt_object_find(env, mdt, &ma->ma_pfid);
356 GOTO(put_obj, rc = PTR_ERR(pobj));
358 /* revoke object remote LOOKUP lock */
359 if (mdt_object_remote(pobj)) {
360 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
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.
370 lhp = &info->mti_lh[MDT_LH_PARENT];
371 mdt_lock_reg_init(lhp, LCK_PW);
373 if (le32_to_cpu(lmu->lum_stripe_count) < 2) {
374 rc = mdt_reint_object_lock(info, pobj, lhp,
375 MDS_INODELOCK_UPDATE, true);
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,
386 GOTO(unlock_pobj, rc);
388 ma->ma_lmv = info->mti_big_lmm;
389 ma->ma_lmv_size = info->mti_big_lmmsize;
391 rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
393 GOTO(unlock_obj, rc);
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);
399 lmv = &ma->ma_lmv->lmv_md_v1;
402 if (!(le32_to_cpu(lmv->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION))
403 GOTO(unlock_obj, rc = -EALREADY);
405 lum_stripe_count = lmu->lum_stripe_count;
406 if (!lum_stripe_count)
407 lum_stripe_count = cpu_to_le32(1);
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);
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);
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);
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);
438 mdt_reint_striped_unlock(info, obj, lhc, einfo, rc);
440 mdt_object_unlock(info, pobj, lhp, rc);
442 mdt_object_put(env, pobj);
444 mdt_object_put(env, obj);
449 int mdt_reint_setxattr(struct mdt_thread_info *info,
450 struct mdt_lock_handle *unused)
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;
468 CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
469 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
471 if (info->mti_dlm_req)
472 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
474 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
475 RETURN(err_serious(-ENOMEM));
477 rc = mdt_init_ucred_reint(info);
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) {
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;
492 /* we don't let to remove LMV? */
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);
503 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
504 GOTO(out, rc = -EPERM);
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)
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);
523 /* ACLs were mapped out, return an error so the user knows */
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) {
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);
541 lockpart |= MDS_INODELOCK_LAYOUT;
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 */
556 lockpart |= MDS_INODELOCK_XATTR;
558 lh = &info->mti_lh[MDT_LH_PARENT];
559 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
561 mdt_lock_reg_init(lh, LCK_EX);
562 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
564 GOTO(out, rc = PTR_ERR(obj));
566 tgt_vbr_obj_set(env, mdt_obj2dt(obj));
567 rc = mdt_version_get_check_save(info, obj, 0);
569 GOTO(out_unlock, rc);
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();
580 attr->la_valid = LA_CTIME;
581 child = mdt_object_child(obj);
582 if (valid & OBD_MD_FLXATTR) {
585 if (attr->la_flags & XATTR_REPLACE)
586 flags |= LU_XATTR_REPLACE;
588 if (attr->la_flags & XATTR_CREATE)
589 flags |= LU_XATTR_CREATE;
591 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
592 OBD_FAIL_MDS_SETXATTR_WRITE);
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 */
599 ma->ma_attr_flags |= MDS_PERM_BYPASS;
600 mo_attr_set(env, child, ma);
602 } else if (valid & OBD_MD_FLXATTRRM) {
603 rc = mo_xattr_del(env, child, xattr_name);
604 /* update ctime after xattr changed */
606 ma->ma_attr_flags |= MDS_PERM_BYPASS;
607 mo_attr_set(env, child, ma);
610 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
615 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
619 mdt_object_unlock_put(info, obj, lh, rc);
621 mdt_exit_ucred(info);