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_lib.c
34 * Lustre Metadata Target (mdt) request unpacking helper.
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Fan Yong <fanyong@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/user_namespace.h>
48 #include <linux/uidgid.h>
50 #include "mdt_internal.h"
51 #include <uapi/linux/lnet/nidstr.h>
52 #include <lustre_nodemap.h>
54 typedef enum ucred_init_type {
60 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
62 return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
65 void mdt_exit_ucred(struct mdt_thread_info *info)
67 struct lu_ucred *uc = mdt_ucred(info);
68 struct mdt_device *mdt = info->mti_mdt;
71 if (uc->uc_valid != UCRED_INIT) {
72 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
74 put_group_info(uc->uc_ginfo);
77 if (uc->uc_identity) {
78 mdt_identity_put(mdt->mdt_identity_cache,
80 uc->uc_identity = NULL;
82 uc->uc_valid = UCRED_INIT;
86 static int match_nosquash_list(struct spinlock *rsi_lock,
87 struct list_head *nidlist,
93 rc = cfs_match_nid(peernid, nidlist);
94 spin_unlock(rsi_lock);
98 /* root_squash for inter-MDS operations */
99 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
101 struct lu_ucred *ucred = mdt_ucred(info);
102 struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
105 LASSERT(ucred != NULL);
106 if (!squash->rsi_uid || ucred->uc_fsuid)
109 if (match_nosquash_list(&squash->rsi_lock,
110 &squash->rsi_nosquash_nids,
112 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
113 libcfs_nid2str(peernid));
117 CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
118 libcfs_nid2str(peernid),
119 ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
120 squash->rsi_uid, squash->rsi_gid, 0);
122 ucred->uc_fsuid = squash->rsi_uid;
123 ucred->uc_fsgid = squash->rsi_gid;
125 ucred->uc_suppgids[0] = -1;
126 ucred->uc_suppgids[1] = -1;
131 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
133 struct ptlrpc_request *req = mdt_info_req(info);
134 const char *jobid = mdt_req_get_jobid(req);
136 /* set jobid if specified. */
138 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
140 uc->uc_jobid[0] = '\0';
143 static void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
145 if (info && info->mti_exp && info->mti_exp->exp_connection)
146 uc->uc_nid = info->mti_exp->exp_connection->c_peer.nid;
148 uc->uc_nid = LNET_NID_ANY;
151 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
154 struct lu_nodemap *nodemap = NULL;
157 if (info && info->mti_exp) {
158 nodemap = nodemap_get_from_exp(info->mti_exp);
159 if (nodemap && !IS_ERR(nodemap)) {
160 audit = nodemap->nmf_enable_audit;
161 nodemap_putref(nodemap);
165 uc->uc_enable_audit = audit;
168 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
169 void *buf, bool drop_fs_cap)
171 struct ptlrpc_request *req = mdt_info_req(info);
172 struct mdt_device *mdt = info->mti_mdt;
173 struct ptlrpc_user_desc *pud = req->rq_user_desc;
174 struct lu_ucred *ucred = mdt_ucred(info);
175 struct lu_nodemap *nodemap;
176 lnet_nid_t peernid = req->rq_peer.nid;
180 bool is_nm_gid_squashed = false;
185 LASSERT(req->rq_auth_gss);
186 LASSERT(!req->rq_auth_usr_mdt);
187 LASSERT(req->rq_user_desc);
188 LASSERT(ucred != NULL);
190 ucred->uc_valid = UCRED_INVALID;
192 nodemap = nodemap_get_from_exp(info->mti_exp);
194 RETURN(PTR_ERR(nodemap));
196 pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
197 NODEMAP_CLIENT_TO_FS, pud->pud_uid);
198 pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
199 NODEMAP_CLIENT_TO_FS, pud->pud_gid);
200 pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
201 NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
202 pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
203 NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
205 ucred->uc_o_uid = pud->pud_uid;
206 ucred->uc_o_gid = pud->pud_gid;
207 ucred->uc_o_fsuid = pud->pud_fsuid;
208 ucred->uc_o_fsgid = pud->pud_fsgid;
210 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
211 /* deny access before we get identity ref */
212 if (nodemap->nmf_deny_unknown) {
213 nodemap_putref(nodemap);
217 ucred->uc_fsuid = nodemap->nm_squash_uid;
218 ucred->uc_fsgid = nodemap->nm_squash_gid;
220 ucred->uc_suppgids[0] = -1;
221 ucred->uc_suppgids[1] = -1;
224 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
225 is_nm_gid_squashed = true;
227 nodemap_putref(nodemap);
229 if (type == BODY_INIT) {
230 struct mdt_body *body = (struct mdt_body *)buf;
232 ucred->uc_suppgids[0] = body->mbo_suppgid;
233 ucred->uc_suppgids[1] = -1;
236 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
237 req->rq_auth_uid != pud->pud_uid) {
238 CDEBUG(D_SEC, "local client %s: auth uid %u "
239 "while client claims %u:%u/%u:%u\n",
240 libcfs_nid2str(peernid), req->rq_auth_uid,
241 pud->pud_uid, pud->pud_gid,
242 pud->pud_fsuid, pud->pud_fsgid);
246 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
247 ucred->uc_identity = NULL;
248 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
250 struct md_identity *identity;
252 identity = mdt_identity_get(mdt->mdt_identity_cache,
254 if (IS_ERR(identity)) {
255 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
256 ucred->uc_identity = NULL;
257 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
261 "Deny access without identity: uid %u\n",
266 ucred->uc_identity = identity;
267 perm = mdt_identity_get_perm(ucred->uc_identity,
272 /* find out the setuid/setgid attempt */
273 setuid = (pud->pud_uid != pud->pud_fsuid);
274 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
275 (ucred->uc_identity &&
276 (pud->pud_gid != ucred->uc_identity->mi_gid)));
278 /* check permission of setuid */
279 if (setuid && !(perm & CFS_SETUID_PERM)) {
280 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
281 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
282 GOTO(out, rc = -EACCES);
285 /* check permission of setgid */
286 if (setgid && !(perm & CFS_SETGID_PERM)) {
287 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
288 "from %s\n", pud->pud_uid, pud->pud_gid,
289 pud->pud_fsuid, pud->pud_fsgid,
290 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
291 GOTO(out, rc = -EACCES);
294 if (perm & CFS_SETGRP_PERM) {
295 /* only set groups if GID is not squashed */
296 if (pud->pud_ngroups && !is_nm_gid_squashed) {
297 /* setgroups for local client */
298 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
299 if (!ucred->uc_ginfo) {
300 CERROR("failed to alloc %d groups\n",
302 GOTO(out, rc = -ENOMEM);
305 lustre_groups_from_list(ucred->uc_ginfo,
307 lustre_groups_sort(ucred->uc_ginfo);
309 ucred->uc_suppgids[0] = -1;
310 ucred->uc_suppgids[1] = -1;
311 ucred->uc_ginfo = NULL;
314 ucred->uc_suppgids[0] = -1;
315 ucred->uc_suppgids[1] = -1;
316 ucred->uc_ginfo = NULL;
319 ucred->uc_uid = pud->pud_uid;
320 ucred->uc_gid = pud->pud_gid;
321 ucred->uc_fsuid = pud->pud_fsuid;
322 ucred->uc_fsgid = pud->pud_fsgid;
324 /* process root_squash here. */
325 mdt_root_squash(info, peernid);
327 /* remove fs privilege for non-root user. */
328 if (ucred->uc_fsuid && drop_fs_cap)
329 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
331 ucred->uc_cap = pud->pud_cap;
332 ucred->uc_valid = UCRED_NEW;
333 ucred_set_jobid(info, ucred);
334 ucred_set_nid(info, ucred);
335 ucred_set_audit_enabled(info, ucred);
341 if (ucred->uc_ginfo) {
342 put_group_info(ucred->uc_ginfo);
343 ucred->uc_ginfo = NULL;
345 if (ucred->uc_identity) {
346 mdt_identity_put(mdt->mdt_identity_cache,
348 ucred->uc_identity = NULL;
356 * Check whether allow the client to set supplementary group IDs or not.
358 * \param[in] info pointer to the thread context
359 * \param[in] uc pointer to the RPC user descriptor
361 * \retval true if allow to set supplementary group IDs
362 * \retval false for other cases
364 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
368 /* 1. If identity_upcall is disabled,
369 * permit local client to do anything. */
370 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
373 /* 2. If fail to get related identities, then forbid any client to
374 * set supplementary group IDs. */
375 if (uc->uc_identity == NULL)
378 /* 3. Check the permission in the identities. */
379 perm = mdt_identity_get_perm(uc->uc_identity,
380 mdt_info_req(info)->rq_peer.nid);
381 if (perm & CFS_SETGRP_PERM)
387 int mdt_check_ucred(struct mdt_thread_info *info)
389 struct ptlrpc_request *req = mdt_info_req(info);
390 struct mdt_device *mdt = info->mti_mdt;
391 struct ptlrpc_user_desc *pud = req->rq_user_desc;
392 struct lu_ucred *ucred = mdt_ucred(info);
393 struct md_identity *identity = NULL;
394 lnet_nid_t peernid = req->rq_peer.nid;
402 LASSERT(ucred != NULL);
403 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
406 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
409 /* sanity check: if we use strong authentication, we expect the
410 * uid which client claimed is true */
411 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
412 req->rq_auth_uid != pud->pud_uid) {
413 CDEBUG(D_SEC, "local client %s: auth uid %u "
414 "while client claims %u:%u/%u:%u\n",
415 libcfs_nid2str(peernid), req->rq_auth_uid,
416 pud->pud_uid, pud->pud_gid,
417 pud->pud_fsuid, pud->pud_fsgid);
421 if (is_identity_get_disabled(mdt->mdt_identity_cache))
424 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
425 if (IS_ERR(identity)) {
426 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
429 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
435 perm = mdt_identity_get_perm(identity, peernid);
436 /* find out the setuid/setgid attempt */
437 setuid = (pud->pud_uid != pud->pud_fsuid);
438 setgid = (pud->pud_gid != pud->pud_fsgid ||
439 pud->pud_gid != identity->mi_gid);
441 /* check permission of setuid */
442 if (setuid && !(perm & CFS_SETUID_PERM)) {
443 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
444 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
445 GOTO(out, rc = -EACCES);
448 /* check permission of setgid */
449 if (setgid && !(perm & CFS_SETGID_PERM)) {
450 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
451 "from %s\n", pud->pud_uid, pud->pud_gid,
452 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
453 libcfs_nid2str(peernid));
454 GOTO(out, rc = -EACCES);
460 mdt_identity_put(mdt->mdt_identity_cache, identity);
464 static int old_init_ucred_common(struct mdt_thread_info *info,
465 struct lu_nodemap *nodemap,
468 struct lu_ucred *uc = mdt_ucred(info);
469 struct mdt_device *mdt = info->mti_mdt;
470 struct md_identity *identity = NULL;
472 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
473 /* deny access before we get identity ref */
474 if (nodemap->nmf_deny_unknown)
477 uc->uc_fsuid = nodemap->nm_squash_uid;
478 uc->uc_fsgid = nodemap->nm_squash_gid;
480 uc->uc_suppgids[0] = -1;
481 uc->uc_suppgids[1] = -1;
484 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
485 identity = mdt_identity_get(mdt->mdt_identity_cache,
487 if (IS_ERR(identity)) {
488 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
489 uc->uc_cap & CFS_CAP_FS_MASK)) {
492 CDEBUG(D_SEC, "Deny access without identity: "
493 "uid %u\n", uc->uc_fsuid);
498 uc->uc_identity = identity;
500 /* process root_squash here. */
501 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
503 /* remove fs privilege for non-root user. */
504 if (uc->uc_fsuid && drop_fs_cap)
505 uc->uc_cap &= ~CFS_CAP_FS_MASK;
506 uc->uc_valid = UCRED_OLD;
507 ucred_set_jobid(info, uc);
508 ucred_set_nid(info, uc);
509 ucred_set_audit_enabled(info, uc);
516 static int old_init_ucred(struct mdt_thread_info *info,
517 struct mdt_body *body, bool drop_fs_cap)
519 struct lu_ucred *uc = mdt_ucred(info);
520 struct lu_nodemap *nodemap;
524 nodemap = nodemap_get_from_exp(info->mti_exp);
526 RETURN(PTR_ERR(nodemap));
528 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
529 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
530 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
531 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
532 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
533 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
534 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
535 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
538 uc->uc_valid = UCRED_INVALID;
539 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
540 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
541 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
542 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
543 uc->uc_suppgids[0] = body->mbo_suppgid;
544 uc->uc_suppgids[1] = -1;
546 uc->uc_cap = body->mbo_capability;
548 rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
549 nodemap_putref(nodemap);
554 static int old_init_ucred_reint(struct mdt_thread_info *info)
556 struct lu_ucred *uc = mdt_ucred(info);
557 struct lu_nodemap *nodemap;
561 nodemap = nodemap_get_from_exp(info->mti_exp);
563 RETURN(PTR_ERR(nodemap));
567 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
568 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
569 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
570 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
572 uc->uc_valid = UCRED_INVALID;
573 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
574 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
577 rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
578 nodemap_putref(nodemap);
583 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
584 struct mdt_body *body,
587 struct ptlrpc_request *req = mdt_info_req(info);
588 struct lu_ucred *uc = mdt_ucred(info);
591 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
594 mdt_exit_ucred(info);
596 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
597 return old_init_ucred(info, body, drop_fs_cap);
599 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
602 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
604 return __mdt_init_ucred(info, body, true);
607 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
608 * doesn't set correct fsuid explicitely, but raise capability to allow
609 * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
610 * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
611 * will fail permission check. */
612 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
613 struct mdt_body *body)
615 return __mdt_init_ucred(info, body, false);
618 int mdt_init_ucred_reint(struct mdt_thread_info *info)
620 struct ptlrpc_request *req = mdt_info_req(info);
621 struct lu_ucred *uc = mdt_ucred(info);
622 struct md_attr *ma = &info->mti_attr;
625 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
628 /* LU-5564: for normal close request, skip permission check */
629 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
630 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
631 uc->uc_cap |= CFS_CAP_FS_MASK;
633 mdt_exit_ucred(info);
635 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
636 return old_init_ucred_reint(info);
638 return new_init_ucred(info, REC_INIT, NULL, true);
641 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
642 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
644 const struct lov_ost_data_v1 *lod;
645 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
649 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
652 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
653 POSTID(&lmm->lmm_oi), lmm_magic,
654 le32_to_cpu(lmm->lmm_pattern));
656 /* No support for compount layouts yet */
657 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
660 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
661 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
662 le32_to_cpu(lmm->lmm_stripe_size), count);
664 /* If it's a directory or a released file, then there are
665 * no actual objects to print, so bail out. */
666 if (valid & OBD_MD_FLDIREA ||
667 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
670 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
671 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
674 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
675 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
676 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
680 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
682 const struct lmv_mds_md_v1 *lmm1;
683 const struct lmv_foreign_md *lfm;
686 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
689 /* foreign LMV case */
690 lfm = &lmv->lmv_foreign_md;
691 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
693 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
694 le32_to_cpu(lfm->lfm_magic),
695 le32_to_cpu(lfm->lfm_length),
696 le32_to_cpu(lfm->lfm_type),
697 le32_to_cpu(lfm->lfm_flags),
698 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
702 lmm1 = &lmv->lmv_md_v1;
704 "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
705 le32_to_cpu(lmm1->lmv_magic),
706 le32_to_cpu(lmm1->lmv_master_mdt_index),
707 le32_to_cpu(lmm1->lmv_stripe_count),
708 le32_to_cpu(lmm1->lmv_hash_type));
710 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
713 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
716 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
717 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
721 /* Shrink and/or grow reply buffers */
722 int mdt_fix_reply(struct mdt_thread_info *info)
724 struct req_capsule *pill = info->mti_pill;
725 struct mdt_body *body;
726 int md_size, md_packed = 0;
731 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
732 LASSERT(body != NULL);
734 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
736 md_size = body->mbo_eadatasize;
740 acl_size = body->mbo_aclsize;
742 /* this replay - not send info to client */
743 if (info->mti_spec.no_create) {
748 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
753 &RMF_ACL, or &RMF_LOGCOOKIES
754 (optional) &RMF_CAPA1,
755 (optional) &RMF_CAPA2,
756 (optional) something else
759 /* MDT_MD buffer may be bigger than packed value, let's shrink all
760 * buffers before growing it */
761 if (info->mti_big_lmm_used) {
762 /* big_lmm buffer may be used even without packing the result
763 * into reply, just for internal server needs */
764 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
765 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
768 /* free big lmm if md_size is not needed */
769 if (md_size == 0 || md_packed == 0) {
770 info->mti_big_lmm_used = 0;
772 /* buffer must be allocated separately */
773 LASSERT(info->mti_attr.ma_lmm !=
774 req_capsule_server_get(pill, &RMF_MDT_MD));
775 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
777 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
778 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
781 if (info->mti_big_acl_used) {
783 info->mti_big_acl_used = 0;
785 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
786 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
787 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
788 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
789 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
792 /* Shrink optional SECCTX buffer if it is not used */
793 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
794 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
795 !(body->mbo_valid & OBD_MD_SECCTX))
796 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
798 /* Shrink optional ENCCTX buffer if it is not used */
799 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
800 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0 &&
801 !(body->mbo_valid & OBD_MD_ENCCTX))
802 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0, RCL_SERVER);
805 * Some more field should be shrinked if needed.
806 * This should be done by those who added fields to reply message.
809 /* Grow MD buffer if needed finally */
810 if (info->mti_big_lmm_used) {
813 LASSERT(md_size > md_packed);
814 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
815 md_size - md_packed);
816 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
818 /* we can't answer with proper LOV EA, drop flags,
819 * the rc is also returned so this request is
820 * considered as failed */
821 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
822 /* don't return transno along with error */
823 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
825 /* now we need to pack right LOV/LMV EA */
826 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
827 if (info->mti_attr.ma_valid & MA_LOV) {
828 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
830 info->mti_attr.ma_lmm_size);
831 memcpy(lmm, info->mti_attr.ma_lmm,
832 info->mti_attr.ma_lmm_size);
833 } else if (info->mti_attr.ma_valid & MA_LMV) {
834 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
836 info->mti_attr.ma_lmv_size);
837 memcpy(lmm, info->mti_attr.ma_lmv,
838 info->mti_attr.ma_lmv_size);
842 /* update mdt_max_mdsize so clients will be aware about that */
843 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
844 info->mti_mdt->mdt_max_mdsize =
845 info->mti_attr.ma_lmm_size;
846 info->mti_big_lmm_used = 0;
849 if (info->mti_big_acl_used) {
850 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
853 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
855 body->mbo_valid &= ~OBD_MD_FLACL;
857 void *acl = req_capsule_server_get(pill, &RMF_ACL);
859 memcpy(acl, info->mti_big_acl, acl_size);
862 info->mti_big_acl_used = 0;
869 /* if object is dying, pack the lov/llog data,
870 * parameter info->mti_attr should be valid at this point!
871 * Also implements RAoLU policy */
872 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
875 struct mdt_body *repbody = NULL;
876 const struct lu_attr *la = &ma->ma_attr;
877 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
880 struct hsm_action_item hai = {
881 .hai_len = sizeof(hai),
882 .hai_action = HSMA_REMOVE,
883 .hai_extent.length = -1,
891 if (mdt_info_req(info) != NULL) {
892 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
893 LASSERT(repbody != NULL);
895 CDEBUG(D_INFO, "not running in a request/reply context\n");
898 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
899 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
901 if (ma->ma_valid & MA_LOV) {
902 CERROR("No need in LOV EA upon unlink\n");
906 repbody->mbo_eadatasize = 0;
908 /* Only check unlinked and archived if RAoLU and upon last close */
909 if (!cdt->cdt_remove_archive_on_last_unlink ||
910 atomic_read(&mo->mot_open_count) != 0)
913 /* mdt_attr_get_complex will clear ma_valid, so check here first */
914 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
917 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
920 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
922 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
923 * try setting them */
925 rc = mdt_attr_get_complex(info, mo, ma);
927 CERROR("%s: unable to fetch missing attributes of"
928 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
929 PFID(mdt_object_fid(mo)), rc);
933 if (need & MA_INODE) {
934 if (ma->ma_valid & MA_INODE) {
935 if (ma->ma_attr.la_nlink != 0)
943 if (ma->ma_valid & MA_HSM) {
944 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
952 /* RAoLU policy is active, last close on file has occured,
953 * file is unlinked, file is archived, so create remove request
955 * If CDT is not running, requests will be logged for later. */
956 if (ma->ma_hsm.mh_arch_id != 0)
957 archive_id = ma->ma_hsm.mh_arch_id;
959 archive_id = cdt->cdt_default_archive_id;
961 hai.hai_fid = *mdt_object_fid(mo);
963 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
966 CERROR("%s: unable to add HSM remove request for "DFID
967 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
968 PFID(mdt_object_fid(mo)), rc);
973 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
979 if (in & MDS_ATTR_MODE)
981 if (in & MDS_ATTR_UID)
983 if (in & MDS_ATTR_GID)
985 if (in & MDS_ATTR_SIZE)
987 if (in & MDS_ATTR_BLOCKS)
989 if (in & MDS_ATTR_ATIME_SET)
991 if (in & MDS_ATTR_CTIME_SET)
993 if (in & MDS_ATTR_MTIME_SET)
995 if (in & MDS_ATTR_ATTR_FLAG)
997 if (in & MDS_ATTR_KILL_SUID)
999 if (in & MDS_ATTR_KILL_SGID)
1000 out |= LA_KILL_SGID;
1001 if (in & MDS_ATTR_PROJID)
1003 if (in & MDS_ATTR_LSIZE)
1005 if (in & MDS_ATTR_LBLOCKS)
1008 if (in & MDS_ATTR_FROM_OPEN)
1009 rr->rr_flags |= MRF_OPEN_TRUNC;
1010 if (in & MDS_ATTR_OVERRIDE)
1011 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1012 if (in & MDS_ATTR_FORCE)
1013 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1015 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1016 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1017 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1018 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1019 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1020 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1023 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", in);
1029 int mdt_name_unpack(struct req_capsule *pill,
1030 const struct req_msg_field *field,
1032 enum mdt_name_flags flags)
1034 ln->ln_name = req_capsule_client_get(pill, field);
1035 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1037 if (!lu_name_is_valid(ln)) {
1044 if ((flags & MNF_FIX_ANON) &&
1045 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1046 /* Newer (3.x) kernels use a name of "/" for the
1047 * "anonymous" disconnected dentries from NFS
1048 * filehandle conversion. See d_obtain_alias(). */
1056 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1057 const char **secctx_name,
1058 void **secctx, size_t *secctx_size)
1063 *secctx_name = NULL;
1067 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1068 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1071 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1076 if (name_size > XATTR_NAME_MAX + 1)
1079 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1080 if (strnlen(name, name_size) != name_size - 1)
1083 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1084 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1087 *secctx_name = name;
1088 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1089 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1094 static int mdt_file_encctx_unpack(struct req_capsule *pill,
1095 void **encctx, size_t *encctx_size)
1100 if (!exp_connect_encrypt(pill->rc_req->rq_export))
1103 if (!req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_CLIENT) ||
1104 !req_capsule_field_present(pill, &RMF_FILE_ENCCTX, RCL_CLIENT))
1107 *encctx_size = req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_CLIENT);
1108 if (*encctx_size == 0)
1111 *encctx = req_capsule_client_get(pill, &RMF_FILE_ENCCTX);
1116 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1118 struct lu_ucred *uc = mdt_ucred(info);
1119 struct md_attr *ma = &info->mti_attr;
1120 struct lu_attr *la = &ma->ma_attr;
1121 struct req_capsule *pill = info->mti_pill;
1122 struct mdt_reint_record *rr = &info->mti_rr;
1123 struct mdt_rec_setattr *rec;
1124 struct lu_nodemap *nodemap;
1128 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1129 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1133 /* This prior initialization is needed for old_init_ucred_reint() */
1134 uc->uc_fsuid = rec->sa_fsuid;
1135 uc->uc_fsgid = rec->sa_fsgid;
1136 uc->uc_cap = rec->sa_cap;
1137 uc->uc_suppgids[0] = rec->sa_suppgid;
1138 uc->uc_suppgids[1] = -1;
1140 rr->rr_fid1 = &rec->sa_fid;
1141 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1142 la->la_mode = rec->sa_mode;
1143 la->la_flags = rec->sa_attr_flags;
1145 nodemap = nodemap_get_from_exp(info->mti_exp);
1146 if (IS_ERR(nodemap))
1147 RETURN(PTR_ERR(nodemap));
1149 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1150 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1151 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1152 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1153 la->la_projid = rec->sa_projid;
1154 nodemap_putref(nodemap);
1156 la->la_size = rec->sa_size;
1157 la->la_blocks = rec->sa_blocks;
1158 la->la_ctime = rec->sa_ctime;
1159 la->la_atime = rec->sa_atime;
1160 la->la_mtime = rec->sa_mtime;
1161 ma->ma_valid = MA_INODE;
1163 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1164 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1169 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1171 struct req_capsule *pill = info->mti_pill;
1172 struct mdt_ioepoch *ioepoch;
1175 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1176 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1180 if (ioepoch == NULL)
1183 info->mti_open_handle = ioepoch->mio_open_handle;
1188 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1189 struct req_capsule *pill = info->mti_pill;
1191 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1192 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1193 if (info->mti_dlm_req == NULL)
1200 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1202 struct mdt_reint_record *rr = &info->mti_rr;
1203 struct md_attr *ma = &info->mti_attr;
1204 struct req_capsule *pill = info->mti_pill;
1208 rc = mdt_setattr_unpack_rec(info);
1212 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1213 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1214 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1217 if (rr->rr_eadatalen > 0) {
1218 const struct lmv_user_md *lum;
1220 lum = rr->rr_eadata;
1221 /* Sigh ma_valid(from req) does not indicate whether
1222 * it will set LOV/LMV EA, so we have to check magic */
1223 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1224 ma->ma_valid |= MA_LMV;
1225 ma->ma_lmv = (void *)rr->rr_eadata;
1226 ma->ma_lmv_size = rr->rr_eadatalen;
1228 ma->ma_valid |= MA_LOV;
1229 ma->ma_lmm = (void *)rr->rr_eadata;
1230 ma->ma_lmm_size = rr->rr_eadatalen;
1235 rc = mdt_dlmreq_unpack(info);
1239 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1241 struct md_attr *ma = &info->mti_attr;
1242 struct req_capsule *pill = info->mti_pill;
1245 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1248 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1250 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1251 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1257 int mdt_close_unpack(struct mdt_thread_info *info)
1262 rc = mdt_close_handle_unpack(info);
1266 rc = mdt_setattr_unpack_rec(info);
1270 rc = mdt_close_intent_unpack(info);
1274 RETURN(mdt_init_ucred_reint(info));
1277 static int mdt_create_unpack(struct mdt_thread_info *info)
1279 struct lu_ucred *uc = mdt_ucred(info);
1280 struct mdt_rec_create *rec;
1281 struct lu_attr *attr = &info->mti_attr.ma_attr;
1282 struct mdt_reint_record *rr = &info->mti_rr;
1283 struct req_capsule *pill = info->mti_pill;
1284 struct md_op_spec *sp = &info->mti_spec;
1289 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1290 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1294 /* This prior initialization is needed for old_init_ucred_reint() */
1295 uc->uc_fsuid = rec->cr_fsuid;
1296 uc->uc_fsgid = rec->cr_fsgid;
1297 uc->uc_cap = rec->cr_cap;
1298 uc->uc_suppgids[0] = rec->cr_suppgid1;
1299 uc->uc_suppgids[1] = -1;
1300 uc->uc_umask = rec->cr_umask;
1302 rr->rr_fid1 = &rec->cr_fid1;
1303 rr->rr_fid2 = &rec->cr_fid2;
1304 attr->la_mode = rec->cr_mode;
1305 attr->la_rdev = rec->cr_rdev;
1306 attr->la_uid = rec->cr_fsuid;
1307 attr->la_gid = rec->cr_fsgid;
1308 attr->la_ctime = rec->cr_time;
1309 attr->la_mtime = rec->cr_time;
1310 attr->la_atime = rec->cr_time;
1311 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1312 LA_CTIME | LA_MTIME | LA_ATIME;
1313 memset(&sp->u, 0, sizeof(sp->u));
1314 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1316 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1320 if (S_ISLNK(attr->la_mode)) {
1321 const char *tgt = NULL;
1323 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1324 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1325 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1326 sp->u.sp_symname = tgt;
1331 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1332 if (S_ISDIR(attr->la_mode) &&
1333 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1334 sp->u.sp_ea.eadata =
1335 req_capsule_client_get(pill, &RMF_EADATA);
1336 sp->u.sp_ea.eadatalen =
1337 req_capsule_get_size(pill, &RMF_EADATA,
1339 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1343 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1344 &sp->sp_cr_file_secctx,
1345 &sp->sp_cr_file_secctx_size);
1349 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1350 &sp->sp_cr_file_encctx_size);
1354 rc = req_check_sepol(pill);
1358 rc = mdt_dlmreq_unpack(info);
1362 static int mdt_link_unpack(struct mdt_thread_info *info)
1364 struct lu_ucred *uc = mdt_ucred(info);
1365 struct mdt_rec_link *rec;
1366 struct lu_attr *attr = &info->mti_attr.ma_attr;
1367 struct mdt_reint_record *rr = &info->mti_rr;
1368 struct req_capsule *pill = info->mti_pill;
1373 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1374 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1378 /* This prior initialization is needed for old_init_ucred_reint() */
1379 uc->uc_fsuid = rec->lk_fsuid;
1380 uc->uc_fsgid = rec->lk_fsgid;
1381 uc->uc_cap = rec->lk_cap;
1382 uc->uc_suppgids[0] = rec->lk_suppgid1;
1383 uc->uc_suppgids[1] = rec->lk_suppgid2;
1385 attr->la_uid = rec->lk_fsuid;
1386 attr->la_gid = rec->lk_fsgid;
1387 rr->rr_fid1 = &rec->lk_fid1;
1388 rr->rr_fid2 = &rec->lk_fid2;
1389 attr->la_ctime = rec->lk_time;
1390 attr->la_mtime = rec->lk_time;
1391 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1393 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1397 rc = req_check_sepol(pill);
1401 rc = mdt_dlmreq_unpack(info);
1406 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1408 struct lu_ucred *uc = mdt_ucred(info);
1409 struct mdt_rec_unlink *rec;
1410 struct lu_attr *attr = &info->mti_attr.ma_attr;
1411 struct mdt_reint_record *rr = &info->mti_rr;
1412 struct req_capsule *pill = info->mti_pill;
1417 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1418 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1422 /* This prior initialization is needed for old_init_ucred_reint() */
1423 uc->uc_fsuid = rec->ul_fsuid;
1424 uc->uc_fsgid = rec->ul_fsgid;
1425 uc->uc_cap = rec->ul_cap;
1426 uc->uc_suppgids[0] = rec->ul_suppgid1;
1427 uc->uc_suppgids[1] = -1;
1429 attr->la_uid = rec->ul_fsuid;
1430 attr->la_gid = rec->ul_fsgid;
1431 rr->rr_fid1 = &rec->ul_fid1;
1432 rr->rr_fid2 = &rec->ul_fid2;
1433 attr->la_ctime = rec->ul_time;
1434 attr->la_mtime = rec->ul_time;
1435 attr->la_mode = rec->ul_mode;
1436 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1438 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1442 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1444 rc = req_check_sepol(pill);
1448 rc = mdt_dlmreq_unpack(info);
1452 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1454 info->mti_spec.sp_rm_entry = 1;
1455 return mdt_unlink_unpack(info);
1458 static int mdt_rename_unpack(struct mdt_thread_info *info)
1460 struct lu_ucred *uc = mdt_ucred(info);
1461 struct mdt_rec_rename *rec;
1462 struct lu_attr *attr = &info->mti_attr.ma_attr;
1463 struct mdt_reint_record *rr = &info->mti_rr;
1464 struct req_capsule *pill = info->mti_pill;
1465 struct md_op_spec *spec = &info->mti_spec;
1470 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1471 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1475 /* This prior initialization is needed for old_init_ucred_reint() */
1476 uc->uc_fsuid = rec->rn_fsuid;
1477 uc->uc_fsgid = rec->rn_fsgid;
1478 uc->uc_cap = rec->rn_cap;
1479 uc->uc_suppgids[0] = rec->rn_suppgid1;
1480 uc->uc_suppgids[1] = rec->rn_suppgid2;
1482 attr->la_uid = rec->rn_fsuid;
1483 attr->la_gid = rec->rn_fsgid;
1484 rr->rr_fid1 = &rec->rn_fid1;
1485 rr->rr_fid2 = &rec->rn_fid2;
1486 attr->la_ctime = rec->rn_time;
1487 attr->la_mtime = rec->rn_time;
1488 /* rename_tgt contains the mode already */
1489 attr->la_mode = rec->rn_mode;
1490 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1492 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1496 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1500 spec->no_create = !!req_is_replay(mdt_info_req(info));
1502 rc = req_check_sepol(pill);
1506 rc = mdt_dlmreq_unpack(info);
1511 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1513 struct lu_ucred *uc = mdt_ucred(info);
1514 struct mdt_rec_rename *rec;
1515 struct lu_attr *attr = &info->mti_attr.ma_attr;
1516 struct mdt_reint_record *rr = &info->mti_rr;
1517 struct req_capsule *pill = info->mti_pill;
1518 struct md_op_spec *spec = &info->mti_spec;
1523 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1524 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1528 /* This prior initialization is needed for old_init_ucred_reint() */
1529 uc->uc_fsuid = rec->rn_fsuid;
1530 uc->uc_fsgid = rec->rn_fsgid;
1531 uc->uc_cap = rec->rn_cap;
1532 uc->uc_suppgids[0] = rec->rn_suppgid1;
1533 uc->uc_suppgids[1] = rec->rn_suppgid2;
1535 attr->la_uid = rec->rn_fsuid;
1536 attr->la_gid = rec->rn_fsgid;
1537 rr->rr_fid1 = &rec->rn_fid1;
1538 rr->rr_fid2 = &rec->rn_fid2;
1539 attr->la_ctime = rec->rn_time;
1540 attr->la_mtime = rec->rn_time;
1541 /* rename_tgt contains the mode already */
1542 attr->la_mode = rec->rn_mode;
1543 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1544 spec->sp_cr_flags = 0;
1546 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1550 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1551 rc = mdt_close_handle_unpack(info);
1555 spec->sp_migrate_close = 1;
1557 spec->sp_migrate_close = 0;
1559 spec->sp_migrate_nsonly = 0;
1561 /* lustre version > 2.11 migration packs lum */
1562 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1563 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1564 rr->rr_eadatalen = req_capsule_get_size(pill,
1568 if (rr->rr_eadatalen > 0) {
1569 rr->rr_eadata = req_capsule_client_get(pill,
1571 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1572 spec->u.sp_ea.eadata = rr->rr_eadata;
1573 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1576 /* old client doesn't provide lum. */
1577 RETURN(-EOPNOTSUPP);
1581 spec->no_create = !!req_is_replay(mdt_info_req(info));
1583 rc = mdt_dlmreq_unpack(info);
1589 * please see comment above LOV_MAGIC_V1_DEFINED
1591 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1593 struct lov_user_md_v1 *v1 = eadata;
1597 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1598 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1599 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1600 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1601 __swab32(LOV_MAGIC_MAGIC))
1602 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1606 static int mdt_open_unpack(struct mdt_thread_info *info)
1608 struct lu_ucred *uc = mdt_ucred(info);
1609 struct mdt_rec_create *rec;
1610 struct lu_attr *attr = &info->mti_attr.ma_attr;
1611 struct req_capsule *pill = info->mti_pill;
1612 struct mdt_reint_record *rr = &info->mti_rr;
1613 struct ptlrpc_request *req = mdt_info_req(info);
1614 struct md_op_spec *sp = &info->mti_spec;
1618 BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1619 sizeof(struct mdt_rec_reint));
1620 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1624 /* This prior initialization is needed for old_init_ucred_reint() */
1625 uc->uc_fsuid = rec->cr_fsuid;
1626 uc->uc_fsgid = rec->cr_fsgid;
1627 uc->uc_cap = rec->cr_cap;
1628 uc->uc_suppgids[0] = rec->cr_suppgid1;
1629 uc->uc_suppgids[1] = rec->cr_suppgid2;
1630 uc->uc_umask = rec->cr_umask;
1632 rr->rr_fid1 = &rec->cr_fid1;
1633 rr->rr_fid2 = &rec->cr_fid2;
1634 rr->rr_open_handle = &rec->cr_open_handle_old;
1635 attr->la_mode = rec->cr_mode;
1636 attr->la_rdev = rec->cr_rdev;
1637 attr->la_uid = rec->cr_fsuid;
1638 attr->la_gid = rec->cr_fsgid;
1639 attr->la_ctime = rec->cr_time;
1640 attr->la_mtime = rec->cr_time;
1641 attr->la_atime = rec->cr_time;
1642 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1643 LA_CTIME | LA_MTIME | LA_ATIME;
1644 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1645 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1646 /* Do not trigger ASSERTION if client miss to set such flags. */
1647 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1650 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1652 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1654 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1655 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1658 if (rr->rr_eadatalen > 0) {
1659 rr->rr_eadata = req_capsule_client_get(pill,
1661 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1662 sp->u.sp_ea.eadata = rr->rr_eadata;
1663 sp->sp_archive_id = rec->cr_archive_id;
1664 sp->no_create = !!req_is_replay(req);
1665 mdt_fix_lov_magic(info, rr->rr_eadata);
1669 * Client default md_size may be 0 right after client start,
1670 * until all osc are connected, set here just some reasonable
1671 * value to prevent misbehavior.
1673 if (rr->rr_eadatalen == 0 &&
1674 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1675 rr->rr_eadatalen = MIN_MD_SIZE;
1678 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1679 &sp->sp_cr_file_secctx,
1680 &sp->sp_cr_file_secctx_size);
1684 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1685 &sp->sp_cr_file_encctx_size);
1689 rc = req_check_sepol(pill);
1696 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1698 struct mdt_reint_record *rr = &info->mti_rr;
1699 struct lu_ucred *uc = mdt_ucred(info);
1700 struct lu_attr *attr = &info->mti_attr.ma_attr;
1701 struct req_capsule *pill = info->mti_pill;
1702 struct mdt_rec_setxattr *rec;
1707 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1708 sizeof(struct mdt_rec_reint));
1709 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1713 /* This prior initialization is needed for old_init_ucred_reint() */
1714 uc->uc_fsuid = rec->sx_fsuid;
1715 uc->uc_fsgid = rec->sx_fsgid;
1716 uc->uc_cap = rec->sx_cap;
1717 uc->uc_suppgids[0] = rec->sx_suppgid1;
1718 uc->uc_suppgids[1] = -1;
1720 rr->rr_opcode = rec->sx_opcode;
1721 rr->rr_fid1 = &rec->sx_fid;
1722 attr->la_valid = rec->sx_valid;
1723 attr->la_ctime = rec->sx_time;
1724 attr->la_size = rec->sx_size;
1725 attr->la_flags = rec->sx_flags;
1727 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1731 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1732 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1735 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1738 if (rr->rr_eadatalen > 0) {
1739 rr->rr_eadata = req_capsule_client_get(pill,
1741 if (rr->rr_eadata == NULL)
1744 rr->rr_eadata = NULL;
1746 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1747 CDEBUG(D_INFO, "no xattr data supplied\n");
1751 rc = req_check_sepol(pill);
1755 if (mdt_dlmreq_unpack(info) < 0)
1761 static int mdt_resync_unpack(struct mdt_thread_info *info)
1763 struct req_capsule *pill = info->mti_pill;
1764 struct mdt_reint_record *rr = &info->mti_rr;
1765 struct lu_ucred *uc = mdt_ucred(info);
1766 struct mdt_rec_resync *rec;
1769 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1770 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1774 /* This prior initialization is needed for old_init_ucred_reint() */
1775 uc->uc_fsuid = rec->rs_fsuid;
1776 uc->uc_fsgid = rec->rs_fsgid;
1777 uc->uc_cap = rec->rs_cap;
1779 rr->rr_fid1 = &rec->rs_fid;
1780 rr->rr_mirror_id = rec->rs_mirror_id;
1782 /* cookie doesn't need to be swapped but it has been swapped
1783 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1785 if (ptlrpc_req_need_swab(mdt_info_req(info)))
1786 __swab64s(&rec->rs_lease_handle.cookie);
1787 rr->rr_lease_handle = &rec->rs_lease_handle;
1789 RETURN(mdt_dlmreq_unpack(info));
1792 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1794 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1795 [REINT_SETATTR] = mdt_setattr_unpack,
1796 [REINT_CREATE] = mdt_create_unpack,
1797 [REINT_LINK] = mdt_link_unpack,
1798 [REINT_UNLINK] = mdt_unlink_unpack,
1799 [REINT_RENAME] = mdt_rename_unpack,
1800 [REINT_OPEN] = mdt_open_unpack,
1801 [REINT_SETXATTR] = mdt_setxattr_unpack,
1802 [REINT_RMENTRY] = mdt_rmentry_unpack,
1803 [REINT_MIGRATE] = mdt_migrate_unpack,
1804 [REINT_RESYNC] = mdt_resync_unpack,
1807 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1812 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1813 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1814 info->mti_rr.rr_opcode = op;
1815 rc = mdt_reint_unpackers[op](info);
1817 CERROR("Unexpected opcode %d\n", op);
1823 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1824 struct mdt_object *child)
1827 struct lu_buf *buffer;
1828 struct mdt_body *repbody;
1829 struct req_capsule *pill = info->mti_pill;
1832 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1833 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1835 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1836 buffer = &info->mti_buf;
1838 /* fill reply buffer with security context now */
1839 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1841 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1843 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1844 buffer, secctx_name);
1847 "found security context of size %d for "DFID"\n",
1848 rc, PFID(mdt_object_fid(child)));
1850 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1851 repbody->mbo_valid |= OBD_MD_SECCTX;
1852 if (rc < buffer->lb_len)
1853 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1858 "security context not found for "DFID": rc = %d\n",
1859 PFID(mdt_object_fid(child)), rc);
1860 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1862 /* handling -ENOENT is important because it may change
1863 * object state in DNE env dropping LOHA_EXISTS flag,
1864 * it is important to return that to the caller.
1865 * Check LU-13115 for details.
1874 /* check whether two FIDs belong to different MDT. */
1875 static int mdt_fids_different_target(struct mdt_thread_info *info,
1876 const struct lu_fid *fid1,
1877 const struct lu_fid *fid2)
1879 const struct lu_env *env = info->mti_env;
1880 struct mdt_device *mdt = info->mti_mdt;
1881 struct lu_seq_range *range = &info->mti_range;
1882 struct seq_server_site *ss;
1883 __u32 index1, index2;
1886 if (fid_seq(fid1) == fid_seq(fid2))
1889 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1891 range->lsr_flags = LU_SEQ_RANGE_MDT;
1892 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1896 index1 = range->lsr_index;
1898 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1902 index2 = range->lsr_index;
1904 return index1 != index2;
1907 static bool mdt_object_is_shard(struct mdt_thread_info *info,
1908 struct mdt_object *obj)
1910 struct lmv_mds_md_v1 *lmv = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
1914 if (!mdt_object_exists(obj))
1917 if (!S_ISDIR(lu_object_attr(&obj->mot_obj)))
1921 buf.lb_len = sizeof(*lmv);
1922 rc = mo_xattr_get(info->mti_env, mdt_object_child(obj), &buf,
1927 return lmv->lmv_magic == cpu_to_le32(LMV_MAGIC_STRIPE);
1931 * Check whether \a child is remote object on \a parent.
1933 * \param[in] info thread environment
1934 * \param[in] parent parent object, it's the same as child object in
1936 * \param[in] child child object
1938 * \retval 1 is remote object.
1939 * \retval 0 isn't remote object.
1940 * \retval < 1 error code
1942 int mdt_is_remote_object(struct mdt_thread_info *info,
1943 struct mdt_object *parent,
1944 struct mdt_object *child)
1946 struct lu_buf *buf = &info->mti_big_buf;
1947 struct linkea_data ldata = { NULL };
1948 struct link_ea_header *leh;
1949 struct link_ea_entry *lee;
1950 struct lu_name name;
1958 if (fid_is_root(mdt_object_fid(child)))
1961 if (likely(parent != child)) {
1962 if (mdt_object_remote(parent) ^ mdt_object_remote(child)) {
1963 /* don't treat shard as remote object, otherwise client
1964 * need to revalidate shards all the time.
1966 if (mdt_object_is_shard(info, child))
1971 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1974 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1975 mdt_object_fid(child));
1979 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1980 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1985 rc = mdt_links_read(info, child, &ldata);
1986 /* can't read linkea, just assume it's remote object */
1987 if (rc == -ENOENT || rc == -ENODATA)
1993 lee = (struct link_ea_entry *)(leh + 1);
1994 for (i = 0; i < leh->leh_reccount; i++) {
1995 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1996 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1997 if (mdt_fids_different_target(info, &pfid,
1998 mdt_object_fid(child)))
2005 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
2006 struct mdt_object *child)
2008 struct lu_buf *buffer;
2009 struct mdt_body *repbody;
2010 struct req_capsule *pill = info->mti_pill;
2011 struct obd_export *exp = mdt_info_req(info)->rq_export;
2014 if (!exp_connect_encrypt(exp))
2017 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
2018 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0) {
2019 struct lu_attr la = { 0 };
2020 struct dt_object *dt = mdt_obj2dt(child);
2022 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
2023 dt_attr_get(info->mti_env, mdt_obj2dt(child), &la);
2025 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL) {
2026 buffer = &info->mti_buf;
2028 /* fill reply buffer with encryption context now */
2030 req_capsule_get_size(pill, &RMF_FILE_ENCCTX,
2033 req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
2034 rc = mo_xattr_get(info->mti_env,
2035 mdt_object_child(child),
2037 LL_XATTR_NAME_ENCRYPTION_CONTEXT);
2040 "found encryption ctx of size %d for "DFID"\n",
2041 rc, PFID(mdt_object_fid(child)));
2043 repbody = req_capsule_server_get(pill,
2045 repbody->mbo_valid |= OBD_MD_ENCCTX;
2046 if (rc < buffer->lb_len)
2047 req_capsule_shrink(pill,
2048 &RMF_FILE_ENCCTX, rc,
2053 "encryption ctx not found for "DFID": rc = %d\n",
2054 PFID(mdt_object_fid(child)), rc);
2055 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2057 /* handling -ENOENT is important because it may
2058 * change object state in DNE env dropping
2059 * LOHA_EXISTS flag, it is important to return
2060 * that to the caller.
2061 * Check LU-13115 for details.
2067 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,