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,
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_suppgids[0] = -1;
218 ucred->uc_suppgids[1] = -1;
221 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
222 is_nm_gid_squashed = true;
224 nodemap_putref(nodemap);
226 if (type == BODY_INIT) {
227 struct mdt_body *body = (struct mdt_body *)buf;
229 ucred->uc_suppgids[0] = body->mbo_suppgid;
230 ucred->uc_suppgids[1] = -1;
233 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
234 req->rq_auth_uid != pud->pud_uid) {
235 CDEBUG(D_SEC, "local client %s: auth uid %u "
236 "while client claims %u:%u/%u:%u\n",
237 libcfs_nid2str(peernid), req->rq_auth_uid,
238 pud->pud_uid, pud->pud_gid,
239 pud->pud_fsuid, pud->pud_fsgid);
243 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
244 ucred->uc_identity = NULL;
245 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
247 struct md_identity *identity;
249 identity = mdt_identity_get(mdt->mdt_identity_cache,
251 if (IS_ERR(identity)) {
252 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
253 ucred->uc_identity = NULL;
254 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
258 "Deny access without identity: uid %u\n",
263 ucred->uc_identity = identity;
264 perm = mdt_identity_get_perm(ucred->uc_identity,
269 /* find out the setuid/setgid attempt */
270 setuid = (pud->pud_uid != pud->pud_fsuid);
271 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
272 (ucred->uc_identity &&
273 (pud->pud_gid != ucred->uc_identity->mi_gid)));
275 /* check permission of setuid */
276 if (setuid && !(perm & CFS_SETUID_PERM)) {
277 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
278 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
279 GOTO(out, rc = -EACCES);
282 /* check permission of setgid */
283 if (setgid && !(perm & CFS_SETGID_PERM)) {
284 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
285 "from %s\n", pud->pud_uid, pud->pud_gid,
286 pud->pud_fsuid, pud->pud_fsgid,
287 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
288 GOTO(out, rc = -EACCES);
291 if (perm & CFS_SETGRP_PERM) {
292 /* only set groups if GID is not squashed */
293 if (pud->pud_ngroups && !is_nm_gid_squashed) {
294 /* setgroups for local client */
295 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
296 if (!ucred->uc_ginfo) {
297 CERROR("failed to alloc %d groups\n",
299 GOTO(out, rc = -ENOMEM);
302 lustre_groups_from_list(ucred->uc_ginfo,
304 lustre_groups_sort(ucred->uc_ginfo);
306 ucred->uc_suppgids[0] = -1;
307 ucred->uc_suppgids[1] = -1;
308 ucred->uc_ginfo = NULL;
311 ucred->uc_suppgids[0] = -1;
312 ucred->uc_suppgids[1] = -1;
313 ucred->uc_ginfo = NULL;
316 ucred->uc_uid = pud->pud_uid;
317 ucred->uc_gid = pud->pud_gid;
319 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
322 ucred->uc_cap = pud->pud_cap;
324 ucred->uc_fsuid = pud->pud_fsuid;
325 ucred->uc_fsgid = pud->pud_fsgid;
327 /* process root_squash here. */
328 mdt_root_squash(info, peernid);
330 ucred->uc_valid = UCRED_NEW;
331 ucred_set_jobid(info, ucred);
332 ucred_set_nid(info, ucred);
333 ucred_set_audit_enabled(info, ucred);
339 if (ucred->uc_ginfo) {
340 put_group_info(ucred->uc_ginfo);
341 ucred->uc_ginfo = NULL;
343 if (ucred->uc_identity) {
344 mdt_identity_put(mdt->mdt_identity_cache,
346 ucred->uc_identity = NULL;
354 * Check whether allow the client to set supplementary group IDs or not.
356 * \param[in] info pointer to the thread context
357 * \param[in] uc pointer to the RPC user descriptor
359 * \retval true if allow to set supplementary group IDs
360 * \retval false for other cases
362 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
366 /* 1. If identity_upcall is disabled,
367 * permit local client to do anything. */
368 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
371 /* 2. If fail to get related identities, then forbid any client to
372 * set supplementary group IDs. */
373 if (uc->uc_identity == NULL)
376 /* 3. Check the permission in the identities. */
377 perm = mdt_identity_get_perm(uc->uc_identity,
378 mdt_info_req(info)->rq_peer.nid);
379 if (perm & CFS_SETGRP_PERM)
385 int mdt_check_ucred(struct mdt_thread_info *info)
387 struct ptlrpc_request *req = mdt_info_req(info);
388 struct mdt_device *mdt = info->mti_mdt;
389 struct ptlrpc_user_desc *pud = req->rq_user_desc;
390 struct lu_ucred *ucred = mdt_ucred(info);
391 struct md_identity *identity = NULL;
392 lnet_nid_t peernid = req->rq_peer.nid;
400 LASSERT(ucred != NULL);
401 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
404 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
407 /* sanity check: if we use strong authentication, we expect the
408 * uid which client claimed is true */
409 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
410 req->rq_auth_uid != pud->pud_uid) {
411 CDEBUG(D_SEC, "local client %s: auth uid %u "
412 "while client claims %u:%u/%u:%u\n",
413 libcfs_nid2str(peernid), req->rq_auth_uid,
414 pud->pud_uid, pud->pud_gid,
415 pud->pud_fsuid, pud->pud_fsgid);
419 if (is_identity_get_disabled(mdt->mdt_identity_cache))
422 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
423 if (IS_ERR(identity)) {
424 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
427 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
433 perm = mdt_identity_get_perm(identity, peernid);
434 /* find out the setuid/setgid attempt */
435 setuid = (pud->pud_uid != pud->pud_fsuid);
436 setgid = (pud->pud_gid != pud->pud_fsgid ||
437 pud->pud_gid != identity->mi_gid);
439 /* check permission of setuid */
440 if (setuid && !(perm & CFS_SETUID_PERM)) {
441 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
442 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
443 GOTO(out, rc = -EACCES);
446 /* check permission of setgid */
447 if (setgid && !(perm & CFS_SETGID_PERM)) {
448 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
449 "from %s\n", pud->pud_uid, pud->pud_gid,
450 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
451 libcfs_nid2str(peernid));
452 GOTO(out, rc = -EACCES);
458 mdt_identity_put(mdt->mdt_identity_cache, identity);
462 static int old_init_ucred_common(struct mdt_thread_info *info,
463 struct lu_nodemap *nodemap)
465 struct lu_ucred *uc = mdt_ucred(info);
466 struct mdt_device *mdt = info->mti_mdt;
467 struct md_identity *identity = NULL;
469 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
470 /* deny access before we get identity ref */
471 if (nodemap->nmf_deny_unknown)
475 uc->uc_suppgids[0] = -1;
476 uc->uc_suppgids[1] = -1;
479 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
480 identity = mdt_identity_get(mdt->mdt_identity_cache,
482 if (IS_ERR(identity)) {
483 kernel_cap_t kcap = cap_combine(CAP_FS_SET,
485 u32 cap_mask = kcap.cap[0];
487 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
488 uc->uc_cap & cap_mask)) {
491 CDEBUG(D_SEC, "Deny access without identity: "
492 "uid %u\n", uc->uc_fsuid);
497 uc->uc_identity = identity;
499 /* process root_squash here. */
500 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
502 uc->uc_valid = UCRED_OLD;
503 ucred_set_jobid(info, uc);
504 ucred_set_nid(info, uc);
505 ucred_set_audit_enabled(info, uc);
512 static int old_init_ucred(struct mdt_thread_info *info,
513 struct mdt_body *body)
515 struct lu_ucred *uc = mdt_ucred(info);
516 struct lu_nodemap *nodemap;
520 nodemap = nodemap_get_from_exp(info->mti_exp);
522 RETURN(PTR_ERR(nodemap));
524 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
525 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
526 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
527 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
528 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
529 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
530 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
531 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
534 uc->uc_valid = UCRED_INVALID;
535 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
536 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
537 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
538 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
539 uc->uc_suppgids[0] = body->mbo_suppgid;
540 uc->uc_suppgids[1] = -1;
542 uc->uc_cap = body->mbo_capability;
544 rc = old_init_ucred_common(info, nodemap);
545 nodemap_putref(nodemap);
550 static int old_init_ucred_reint(struct mdt_thread_info *info)
552 struct lu_ucred *uc = mdt_ucred(info);
553 struct lu_nodemap *nodemap;
557 nodemap = nodemap_get_from_exp(info->mti_exp);
559 RETURN(PTR_ERR(nodemap));
563 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
564 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
565 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
566 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
568 uc->uc_valid = UCRED_INVALID;
569 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
570 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
573 rc = old_init_ucred_common(info, nodemap);
574 nodemap_putref(nodemap);
579 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
580 struct mdt_body *body)
582 struct ptlrpc_request *req = mdt_info_req(info);
583 struct lu_ucred *uc = mdt_ucred(info);
586 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
589 mdt_exit_ucred(info);
591 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
592 return old_init_ucred(info, body);
594 return new_init_ucred(info, BODY_INIT, body);
597 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
599 return __mdt_init_ucred(info, body);
602 int mdt_init_ucred_reint(struct mdt_thread_info *info)
604 struct ptlrpc_request *req = mdt_info_req(info);
605 struct lu_ucred *uc = mdt_ucred(info);
606 struct md_attr *ma = &info->mti_attr;
609 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
612 /* LU-5564: for normal close request, skip permission check */
613 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
614 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP))) {
615 kernel_cap_t kcap = { { uc->uc_cap, } };
617 kcap = cap_raise_nfsd_set(kcap, CAP_FULL_SET);
618 kcap = cap_raise_fs_set(kcap, CAP_FULL_SET);
619 uc->uc_cap = kcap.cap[0];
622 mdt_exit_ucred(info);
624 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
625 return old_init_ucred_reint(info);
627 return new_init_ucred(info, REC_INIT, NULL);
630 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
631 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
633 const struct lov_ost_data_v1 *lod;
634 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
638 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
641 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
642 POSTID(&lmm->lmm_oi), lmm_magic,
643 le32_to_cpu(lmm->lmm_pattern));
645 /* No support for compount layouts yet */
646 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
649 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
650 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
651 le32_to_cpu(lmm->lmm_stripe_size), count);
653 /* If it's a directory or a released file, then there are
654 * no actual objects to print, so bail out. */
655 if (valid & OBD_MD_FLDIREA ||
656 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
659 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
660 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
663 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
664 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
665 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
669 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
671 const struct lmv_mds_md_v1 *lmm1;
672 const struct lmv_foreign_md *lfm;
675 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
678 /* foreign LMV case */
679 lfm = &lmv->lmv_foreign_md;
680 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
682 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
683 le32_to_cpu(lfm->lfm_magic),
684 le32_to_cpu(lfm->lfm_length),
685 le32_to_cpu(lfm->lfm_type),
686 le32_to_cpu(lfm->lfm_flags),
687 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
691 lmm1 = &lmv->lmv_md_v1;
693 "magic 0x%08X, master %#X stripe_count %d hash_type %#x\n",
694 le32_to_cpu(lmm1->lmv_magic),
695 le32_to_cpu(lmm1->lmv_master_mdt_index),
696 le32_to_cpu(lmm1->lmv_stripe_count),
697 le32_to_cpu(lmm1->lmv_hash_type));
699 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
702 if (le32_to_cpu(lmm1->lmv_stripe_count) > LMV_MAX_STRIPE_COUNT)
705 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
708 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
709 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
713 /* Shrink and/or grow reply buffers */
714 int mdt_fix_reply(struct mdt_thread_info *info)
716 struct req_capsule *pill = info->mti_pill;
717 struct mdt_body *body;
718 int md_size, md_packed = 0;
723 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
724 LASSERT(body != NULL);
726 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
728 md_size = body->mbo_eadatasize;
732 acl_size = body->mbo_aclsize;
734 /* this replay - not send info to client */
735 if (info->mti_spec.no_create) {
740 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
745 &RMF_ACL, or &RMF_LOGCOOKIES
746 (optional) &RMF_CAPA1,
747 (optional) &RMF_CAPA2,
748 (optional) something else
751 /* MDT_MD buffer may be bigger than packed value, let's shrink all
752 * buffers before growing it */
753 if (info->mti_big_lmm_used) {
754 /* big_lmm buffer may be used even without packing the result
755 * into reply, just for internal server needs */
756 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
757 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
760 /* free big lmm if md_size is not needed */
761 if (md_size == 0 || md_packed == 0) {
762 info->mti_big_lmm_used = 0;
764 /* buffer must be allocated separately */
765 LASSERT(info->mti_attr.ma_lmm !=
766 req_capsule_server_get(pill, &RMF_MDT_MD));
767 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
769 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
770 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
773 if (info->mti_big_acl_used) {
775 info->mti_big_acl_used = 0;
777 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
778 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
779 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
780 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
781 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
784 /* Shrink optional SECCTX buffer if it is not used */
785 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
786 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
787 !(body->mbo_valid & OBD_MD_SECCTX))
788 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
790 /* Shrink optional ENCCTX buffer if it is not used */
791 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
792 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0 &&
793 !(body->mbo_valid & OBD_MD_ENCCTX))
794 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0, RCL_SERVER);
797 * Some more field should be shrinked if needed.
798 * This should be done by those who added fields to reply message.
801 /* Grow MD buffer if needed finally */
802 if (info->mti_big_lmm_used) {
805 LASSERT(md_size > md_packed);
806 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
807 md_size - md_packed);
808 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
810 /* we can't answer with proper LOV EA, drop flags,
811 * the rc is also returned so this request is
812 * considered as failed */
813 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
814 /* don't return transno along with error */
815 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
817 /* now we need to pack right LOV/LMV EA */
818 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
819 if (info->mti_attr.ma_valid & MA_LOV) {
820 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
822 info->mti_attr.ma_lmm_size);
823 memcpy(lmm, info->mti_attr.ma_lmm,
824 info->mti_attr.ma_lmm_size);
825 } else if (info->mti_attr.ma_valid & MA_LMV) {
826 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
828 info->mti_attr.ma_lmv_size);
829 memcpy(lmm, info->mti_attr.ma_lmv,
830 info->mti_attr.ma_lmv_size);
834 /* update mdt_max_mdsize so clients will be aware about that */
835 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
836 info->mti_mdt->mdt_max_mdsize =
837 info->mti_attr.ma_lmm_size;
838 info->mti_big_lmm_used = 0;
841 if (info->mti_big_acl_used) {
842 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
845 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
847 body->mbo_valid &= ~OBD_MD_FLACL;
849 void *acl = req_capsule_server_get(pill, &RMF_ACL);
851 memcpy(acl, info->mti_big_acl, acl_size);
854 info->mti_big_acl_used = 0;
861 /* if object is dying, pack the lov/llog data,
862 * parameter info->mti_attr should be valid at this point!
863 * Also implements RAoLU policy */
864 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
867 struct mdt_body *repbody = NULL;
868 const struct lu_attr *la = &ma->ma_attr;
869 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
872 struct hsm_action_item hai = {
873 .hai_len = sizeof(hai),
874 .hai_action = HSMA_REMOVE,
875 .hai_extent.length = -1,
883 if (mdt_info_req(info) != NULL) {
884 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
885 LASSERT(repbody != NULL);
887 CDEBUG(D_INFO, "not running in a request/reply context\n");
890 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
891 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
893 if (ma->ma_valid & MA_LOV) {
894 CERROR("No need in LOV EA upon unlink\n");
898 repbody->mbo_eadatasize = 0;
900 /* Only check unlinked and archived if RAoLU and upon last close */
901 if (!cdt->cdt_remove_archive_on_last_unlink ||
902 atomic_read(&mo->mot_open_count) != 0)
905 /* mdt_attr_get_complex will clear ma_valid, so check here first */
906 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
909 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
912 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
914 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
915 * try setting them */
917 rc = mdt_attr_get_complex(info, mo, ma);
919 CERROR("%s: unable to fetch missing attributes of"
920 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
921 PFID(mdt_object_fid(mo)), rc);
925 if (need & MA_INODE) {
926 if (ma->ma_valid & MA_INODE) {
927 if (ma->ma_attr.la_nlink != 0)
935 if (ma->ma_valid & MA_HSM) {
936 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
944 /* RAoLU policy is active, last close on file has occured,
945 * file is unlinked, file is archived, so create remove request
947 * If CDT is not running, requests will be logged for later. */
948 if (ma->ma_hsm.mh_arch_id != 0)
949 archive_id = ma->ma_hsm.mh_arch_id;
951 archive_id = cdt->cdt_default_archive_id;
953 hai.hai_fid = *mdt_object_fid(mo);
955 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
958 CERROR("%s: unable to add HSM remove request for "DFID
959 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
960 PFID(mdt_object_fid(mo)), rc);
965 static __u64 mdt_attr_valid_xlate(enum mds_attr_flags in,
966 struct mdt_reint_record *rr,
972 if (in & MDS_ATTR_MODE)
974 if (in & MDS_ATTR_UID)
976 if (in & MDS_ATTR_GID)
978 if (in & MDS_ATTR_SIZE)
980 if (in & MDS_ATTR_BLOCKS)
982 if (in & MDS_ATTR_ATIME_SET)
984 if (in & MDS_ATTR_CTIME_SET)
986 if (in & MDS_ATTR_MTIME_SET)
988 if (in & MDS_ATTR_ATTR_FLAG)
990 if (in & MDS_ATTR_KILL_SUID)
992 if (in & MDS_ATTR_KILL_SGID)
994 if (in & MDS_ATTR_PROJID)
996 if (in & MDS_ATTR_LSIZE)
998 if (in & MDS_ATTR_LBLOCKS)
1001 if (in & MDS_ATTR_FROM_OPEN)
1002 rr->rr_flags |= MRF_OPEN_TRUNC;
1003 if (in & MDS_ATTR_OVERRIDE)
1004 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1005 if (in & MDS_ATTR_FORCE)
1006 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1008 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1009 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1010 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1011 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1012 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1013 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1016 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", (u64)in);
1023 int mdt_name_unpack(struct req_capsule *pill,
1024 const struct req_msg_field *field,
1026 enum mdt_name_flags flags)
1028 ln->ln_name = req_capsule_client_get(pill, field);
1029 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1031 if (!lu_name_is_valid(ln)) {
1038 if ((flags & MNF_FIX_ANON) &&
1039 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1040 /* Newer (3.x) kernels use a name of "/" for the
1041 * "anonymous" disconnected dentries from NFS
1042 * filehandle conversion. See d_obtain_alias(). */
1050 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1051 const char **secctx_name,
1052 void **secctx, size_t *secctx_size)
1057 *secctx_name = NULL;
1061 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1062 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1065 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1070 if (name_size > XATTR_NAME_MAX + 1)
1073 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1074 if (strnlen(name, name_size) != name_size - 1)
1077 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1078 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1081 *secctx_name = name;
1082 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1083 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1088 static int mdt_file_encctx_unpack(struct req_capsule *pill,
1089 void **encctx, size_t *encctx_size)
1094 if (!exp_connect_encrypt(pill->rc_req->rq_export))
1097 if (!req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_CLIENT) ||
1098 !req_capsule_field_present(pill, &RMF_FILE_ENCCTX, RCL_CLIENT))
1101 *encctx_size = req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_CLIENT);
1102 if (*encctx_size == 0)
1105 *encctx = req_capsule_client_get(pill, &RMF_FILE_ENCCTX);
1110 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1112 struct lu_ucred *uc = mdt_ucred(info);
1113 struct md_attr *ma = &info->mti_attr;
1114 struct lu_attr *la = &ma->ma_attr;
1115 struct req_capsule *pill = info->mti_pill;
1116 struct mdt_reint_record *rr = &info->mti_rr;
1117 struct mdt_rec_setattr *rec;
1118 struct lu_nodemap *nodemap;
1122 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1123 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1127 /* This prior initialization is needed for old_init_ucred_reint() */
1128 uc->uc_fsuid = rec->sa_fsuid;
1129 uc->uc_fsgid = rec->sa_fsgid;
1130 uc->uc_cap = rec->sa_cap;
1131 uc->uc_suppgids[0] = rec->sa_suppgid;
1132 uc->uc_suppgids[1] = -1;
1134 rr->rr_fid1 = &rec->sa_fid;
1135 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1136 la->la_mode = rec->sa_mode;
1137 la->la_flags = rec->sa_attr_flags;
1139 nodemap = nodemap_get_from_exp(info->mti_exp);
1140 if (IS_ERR(nodemap))
1141 RETURN(PTR_ERR(nodemap));
1143 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1144 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1145 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1146 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1147 la->la_projid = rec->sa_projid;
1148 nodemap_putref(nodemap);
1150 la->la_size = rec->sa_size;
1151 la->la_blocks = rec->sa_blocks;
1152 la->la_ctime = rec->sa_ctime;
1153 la->la_atime = rec->sa_atime;
1154 la->la_mtime = rec->sa_mtime;
1155 ma->ma_valid = MA_INODE;
1157 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1158 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1163 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1165 struct req_capsule *pill = info->mti_pill;
1166 struct mdt_ioepoch *ioepoch;
1169 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1170 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1174 if (ioepoch == NULL)
1177 info->mti_open_handle = ioepoch->mio_open_handle;
1182 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1183 struct req_capsule *pill = info->mti_pill;
1185 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1186 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1187 if (info->mti_dlm_req == NULL)
1194 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1196 struct mdt_reint_record *rr = &info->mti_rr;
1197 struct md_attr *ma = &info->mti_attr;
1198 struct req_capsule *pill = info->mti_pill;
1202 rc = mdt_setattr_unpack_rec(info);
1206 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1207 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1208 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1211 if (rr->rr_eadatalen > 0) {
1212 const struct lmv_user_md *lum;
1214 lum = rr->rr_eadata;
1215 /* Sigh ma_valid(from req) does not indicate whether
1216 * it will set LOV/LMV EA, so we have to check magic */
1217 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1218 ma->ma_valid |= MA_LMV;
1219 ma->ma_lmv = (void *)rr->rr_eadata;
1220 ma->ma_lmv_size = rr->rr_eadatalen;
1222 ma->ma_valid |= MA_LOV;
1223 ma->ma_lmm = (void *)rr->rr_eadata;
1224 ma->ma_lmm_size = rr->rr_eadatalen;
1229 rc = mdt_dlmreq_unpack(info);
1233 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1235 struct md_attr *ma = &info->mti_attr;
1236 struct req_capsule *pill = info->mti_pill;
1239 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1242 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1244 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1245 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1251 int mdt_close_unpack(struct mdt_thread_info *info)
1256 rc = mdt_close_handle_unpack(info);
1260 rc = mdt_setattr_unpack_rec(info);
1264 rc = mdt_close_intent_unpack(info);
1268 RETURN(mdt_init_ucred_reint(info));
1271 static int mdt_create_unpack(struct mdt_thread_info *info)
1273 struct lu_ucred *uc = mdt_ucred(info);
1274 struct mdt_rec_create *rec;
1275 struct lu_attr *attr = &info->mti_attr.ma_attr;
1276 struct mdt_reint_record *rr = &info->mti_rr;
1277 struct req_capsule *pill = info->mti_pill;
1278 struct md_op_spec *sp = &info->mti_spec;
1283 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1284 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1288 /* This prior initialization is needed for old_init_ucred_reint() */
1289 uc->uc_fsuid = rec->cr_fsuid;
1290 uc->uc_fsgid = rec->cr_fsgid;
1291 uc->uc_cap = rec->cr_cap;
1292 uc->uc_suppgids[0] = rec->cr_suppgid1;
1293 uc->uc_suppgids[1] = -1;
1294 uc->uc_umask = rec->cr_umask;
1296 rr->rr_fid1 = &rec->cr_fid1;
1297 rr->rr_fid2 = &rec->cr_fid2;
1298 attr->la_mode = rec->cr_mode;
1299 attr->la_rdev = rec->cr_rdev;
1300 attr->la_uid = rec->cr_fsuid;
1301 attr->la_gid = rec->cr_fsgid;
1302 attr->la_ctime = rec->cr_time;
1303 attr->la_mtime = rec->cr_time;
1304 attr->la_atime = rec->cr_time;
1305 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1306 LA_CTIME | LA_MTIME | LA_ATIME;
1307 memset(&sp->u, 0, sizeof(sp->u));
1308 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1310 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1314 if (S_ISLNK(attr->la_mode)) {
1315 const char *tgt = NULL;
1317 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1318 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1319 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1320 sp->u.sp_symname = tgt;
1325 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1326 if (S_ISDIR(attr->la_mode) &&
1327 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1328 sp->u.sp_ea.eadata =
1329 req_capsule_client_get(pill, &RMF_EADATA);
1330 sp->u.sp_ea.eadatalen =
1331 req_capsule_get_size(pill, &RMF_EADATA,
1333 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1337 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1338 &sp->sp_cr_file_secctx,
1339 &sp->sp_cr_file_secctx_size);
1343 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1344 &sp->sp_cr_file_encctx_size);
1348 rc = req_check_sepol(pill);
1352 rc = mdt_dlmreq_unpack(info);
1356 static int mdt_link_unpack(struct mdt_thread_info *info)
1358 struct lu_ucred *uc = mdt_ucred(info);
1359 struct mdt_rec_link *rec;
1360 struct lu_attr *attr = &info->mti_attr.ma_attr;
1361 struct mdt_reint_record *rr = &info->mti_rr;
1362 struct req_capsule *pill = info->mti_pill;
1367 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1368 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1372 /* This prior initialization is needed for old_init_ucred_reint() */
1373 uc->uc_fsuid = rec->lk_fsuid;
1374 uc->uc_fsgid = rec->lk_fsgid;
1375 uc->uc_cap = rec->lk_cap;
1376 uc->uc_suppgids[0] = rec->lk_suppgid1;
1377 uc->uc_suppgids[1] = rec->lk_suppgid2;
1379 attr->la_uid = rec->lk_fsuid;
1380 attr->la_gid = rec->lk_fsgid;
1381 rr->rr_fid1 = &rec->lk_fid1;
1382 rr->rr_fid2 = &rec->lk_fid2;
1383 attr->la_ctime = rec->lk_time;
1384 attr->la_mtime = rec->lk_time;
1385 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1387 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1391 rc = req_check_sepol(pill);
1395 rc = mdt_dlmreq_unpack(info);
1400 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1402 struct lu_ucred *uc = mdt_ucred(info);
1403 struct mdt_rec_unlink *rec;
1404 struct lu_attr *attr = &info->mti_attr.ma_attr;
1405 struct mdt_reint_record *rr = &info->mti_rr;
1406 struct req_capsule *pill = info->mti_pill;
1411 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1412 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1416 /* This prior initialization is needed for old_init_ucred_reint() */
1417 uc->uc_fsuid = rec->ul_fsuid;
1418 uc->uc_fsgid = rec->ul_fsgid;
1419 uc->uc_cap = rec->ul_cap;
1420 uc->uc_suppgids[0] = rec->ul_suppgid1;
1421 uc->uc_suppgids[1] = -1;
1423 attr->la_uid = rec->ul_fsuid;
1424 attr->la_gid = rec->ul_fsgid;
1425 rr->rr_fid1 = &rec->ul_fid1;
1426 rr->rr_fid2 = &rec->ul_fid2;
1427 attr->la_ctime = rec->ul_time;
1428 attr->la_mtime = rec->ul_time;
1429 attr->la_mode = rec->ul_mode;
1430 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1432 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1436 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1438 rc = req_check_sepol(pill);
1442 rc = mdt_dlmreq_unpack(info);
1446 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1448 info->mti_spec.sp_rm_entry = 1;
1449 return mdt_unlink_unpack(info);
1452 static int mdt_rename_unpack(struct mdt_thread_info *info)
1454 struct lu_ucred *uc = mdt_ucred(info);
1455 struct mdt_rec_rename *rec;
1456 struct lu_attr *attr = &info->mti_attr.ma_attr;
1457 struct mdt_reint_record *rr = &info->mti_rr;
1458 struct req_capsule *pill = info->mti_pill;
1459 struct md_op_spec *spec = &info->mti_spec;
1464 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1465 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1469 /* This prior initialization is needed for old_init_ucred_reint() */
1470 uc->uc_fsuid = rec->rn_fsuid;
1471 uc->uc_fsgid = rec->rn_fsgid;
1472 uc->uc_cap = rec->rn_cap;
1473 uc->uc_suppgids[0] = rec->rn_suppgid1;
1474 uc->uc_suppgids[1] = rec->rn_suppgid2;
1476 attr->la_uid = rec->rn_fsuid;
1477 attr->la_gid = rec->rn_fsgid;
1478 rr->rr_fid1 = &rec->rn_fid1;
1479 rr->rr_fid2 = &rec->rn_fid2;
1480 attr->la_ctime = rec->rn_time;
1481 attr->la_mtime = rec->rn_time;
1482 /* rename_tgt contains the mode already */
1483 attr->la_mode = rec->rn_mode;
1484 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1486 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1490 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1494 spec->no_create = !!req_is_replay(mdt_info_req(info));
1496 rc = req_check_sepol(pill);
1500 rc = mdt_dlmreq_unpack(info);
1505 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1507 struct lu_ucred *uc = mdt_ucred(info);
1508 struct mdt_rec_rename *rec;
1509 struct lu_attr *attr = &info->mti_attr.ma_attr;
1510 struct mdt_reint_record *rr = &info->mti_rr;
1511 struct req_capsule *pill = info->mti_pill;
1512 struct md_op_spec *spec = &info->mti_spec;
1517 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1518 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1522 /* This prior initialization is needed for old_init_ucred_reint() */
1523 uc->uc_fsuid = rec->rn_fsuid;
1524 uc->uc_fsgid = rec->rn_fsgid;
1525 uc->uc_cap = rec->rn_cap;
1526 uc->uc_suppgids[0] = rec->rn_suppgid1;
1527 uc->uc_suppgids[1] = rec->rn_suppgid2;
1529 attr->la_uid = rec->rn_fsuid;
1530 attr->la_gid = rec->rn_fsgid;
1531 rr->rr_fid1 = &rec->rn_fid1;
1532 rr->rr_fid2 = &rec->rn_fid2;
1533 attr->la_ctime = rec->rn_time;
1534 attr->la_mtime = rec->rn_time;
1535 /* rename_tgt contains the mode already */
1536 attr->la_mode = rec->rn_mode;
1537 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1538 spec->sp_cr_flags = 0;
1540 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1544 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1545 rc = mdt_close_handle_unpack(info);
1549 spec->sp_migrate_close = 1;
1551 spec->sp_migrate_close = 0;
1553 spec->sp_migrate_nsonly = 0;
1555 /* lustre version > 2.11 migration packs lum */
1556 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1557 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1558 rr->rr_eadatalen = req_capsule_get_size(pill,
1562 if (rr->rr_eadatalen > 0) {
1563 rr->rr_eadata = req_capsule_client_get(pill,
1565 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1566 spec->u.sp_ea.eadata = rr->rr_eadata;
1567 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1570 /* old client doesn't provide lum. */
1571 RETURN(-EOPNOTSUPP);
1575 spec->no_create = !!req_is_replay(mdt_info_req(info));
1577 rc = mdt_dlmreq_unpack(info);
1583 * please see comment above LOV_MAGIC_V1_DEFINED
1585 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1587 struct lov_user_md_v1 *v1 = eadata;
1591 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1592 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1593 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1594 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1595 __swab32(LOV_MAGIC_MAGIC))
1596 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1600 static int mdt_open_unpack(struct mdt_thread_info *info)
1602 struct lu_ucred *uc = mdt_ucred(info);
1603 struct mdt_rec_create *rec;
1604 struct lu_attr *attr = &info->mti_attr.ma_attr;
1605 struct req_capsule *pill = info->mti_pill;
1606 struct mdt_reint_record *rr = &info->mti_rr;
1607 struct ptlrpc_request *req = mdt_info_req(info);
1608 struct md_op_spec *sp = &info->mti_spec;
1612 BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1613 sizeof(struct mdt_rec_reint));
1614 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1618 /* This prior initialization is needed for old_init_ucred_reint() */
1619 uc->uc_fsuid = rec->cr_fsuid;
1620 uc->uc_fsgid = rec->cr_fsgid;
1621 uc->uc_cap = rec->cr_cap;
1622 uc->uc_suppgids[0] = rec->cr_suppgid1;
1623 uc->uc_suppgids[1] = rec->cr_suppgid2;
1624 uc->uc_umask = rec->cr_umask;
1626 rr->rr_fid1 = &rec->cr_fid1;
1627 rr->rr_fid2 = &rec->cr_fid2;
1628 rr->rr_open_handle = &rec->cr_open_handle_old;
1629 attr->la_mode = rec->cr_mode;
1630 attr->la_rdev = rec->cr_rdev;
1631 attr->la_uid = rec->cr_fsuid;
1632 attr->la_gid = rec->cr_fsgid;
1633 attr->la_ctime = rec->cr_time;
1634 attr->la_mtime = rec->cr_time;
1635 attr->la_atime = rec->cr_time;
1636 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1637 LA_CTIME | LA_MTIME | LA_ATIME;
1638 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1639 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1640 /* Do not trigger ASSERTION if client miss to set such flags. */
1641 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1644 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1646 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1648 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1649 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1652 if (rr->rr_eadatalen > 0) {
1653 rr->rr_eadata = req_capsule_client_get(pill,
1655 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1656 sp->u.sp_ea.eadata = rr->rr_eadata;
1657 sp->sp_archive_id = rec->cr_archive_id;
1658 sp->no_create = !!req_is_replay(req);
1659 mdt_fix_lov_magic(info, rr->rr_eadata);
1663 * Client default md_size may be 0 right after client start,
1664 * until all osc are connected, set here just some reasonable
1665 * value to prevent misbehavior.
1667 if (rr->rr_eadatalen == 0 &&
1668 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1669 rr->rr_eadatalen = MIN_MD_SIZE;
1672 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1673 &sp->sp_cr_file_secctx,
1674 &sp->sp_cr_file_secctx_size);
1678 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1679 &sp->sp_cr_file_encctx_size);
1683 rc = req_check_sepol(pill);
1690 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1692 struct mdt_reint_record *rr = &info->mti_rr;
1693 struct lu_ucred *uc = mdt_ucred(info);
1694 struct lu_attr *attr = &info->mti_attr.ma_attr;
1695 struct req_capsule *pill = info->mti_pill;
1696 struct mdt_rec_setxattr *rec;
1701 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1702 sizeof(struct mdt_rec_reint));
1703 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1707 /* This prior initialization is needed for old_init_ucred_reint() */
1708 uc->uc_fsuid = rec->sx_fsuid;
1709 uc->uc_fsgid = rec->sx_fsgid;
1710 uc->uc_cap = rec->sx_cap;
1711 uc->uc_suppgids[0] = rec->sx_suppgid1;
1712 uc->uc_suppgids[1] = -1;
1714 rr->rr_opcode = rec->sx_opcode;
1715 rr->rr_fid1 = &rec->sx_fid;
1716 attr->la_valid = rec->sx_valid;
1717 attr->la_ctime = rec->sx_time;
1718 attr->la_size = rec->sx_size;
1719 attr->la_flags = rec->sx_flags;
1721 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1725 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1726 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1729 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1732 if (rr->rr_eadatalen > 0) {
1733 rr->rr_eadata = req_capsule_client_get(pill,
1735 if (rr->rr_eadata == NULL)
1738 rr->rr_eadata = NULL;
1740 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1741 CDEBUG(D_INFO, "no xattr data supplied\n");
1745 rc = req_check_sepol(pill);
1749 if (mdt_dlmreq_unpack(info) < 0)
1755 static int mdt_resync_unpack(struct mdt_thread_info *info)
1757 struct req_capsule *pill = info->mti_pill;
1758 struct mdt_reint_record *rr = &info->mti_rr;
1759 struct lu_ucred *uc = mdt_ucred(info);
1760 struct mdt_rec_resync *rec;
1763 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1764 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1768 /* This prior initialization is needed for old_init_ucred_reint() */
1769 uc->uc_fsuid = rec->rs_fsuid;
1770 uc->uc_fsgid = rec->rs_fsgid;
1771 uc->uc_cap = rec->rs_cap;
1773 rr->rr_fid1 = &rec->rs_fid;
1774 rr->rr_mirror_id = rec->rs_mirror_id;
1776 /* cookie doesn't need to be swapped but it has been swapped
1777 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1779 if (ptlrpc_req_need_swab(mdt_info_req(info)))
1780 __swab64s(&rec->rs_lease_handle.cookie);
1781 rr->rr_lease_handle = &rec->rs_lease_handle;
1783 RETURN(mdt_dlmreq_unpack(info));
1786 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1788 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1789 [REINT_SETATTR] = mdt_setattr_unpack,
1790 [REINT_CREATE] = mdt_create_unpack,
1791 [REINT_LINK] = mdt_link_unpack,
1792 [REINT_UNLINK] = mdt_unlink_unpack,
1793 [REINT_RENAME] = mdt_rename_unpack,
1794 [REINT_OPEN] = mdt_open_unpack,
1795 [REINT_SETXATTR] = mdt_setxattr_unpack,
1796 [REINT_RMENTRY] = mdt_rmentry_unpack,
1797 [REINT_MIGRATE] = mdt_migrate_unpack,
1798 [REINT_RESYNC] = mdt_resync_unpack,
1801 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1806 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1807 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1808 info->mti_rr.rr_opcode = op;
1809 rc = mdt_reint_unpackers[op](info);
1811 CERROR("Unexpected opcode %d\n", op);
1817 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1818 struct mdt_object *child)
1821 struct lu_buf *buffer;
1822 struct mdt_body *repbody;
1823 struct req_capsule *pill = info->mti_pill;
1826 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1827 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1829 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1830 buffer = &info->mti_buf;
1832 /* fill reply buffer with security context now */
1833 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1835 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1837 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1838 buffer, secctx_name);
1841 "found security context of size %d for "DFID"\n",
1842 rc, PFID(mdt_object_fid(child)));
1844 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1845 repbody->mbo_valid |= OBD_MD_SECCTX;
1846 if (rc < buffer->lb_len)
1847 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1852 "security context not found for "DFID": rc = %d\n",
1853 PFID(mdt_object_fid(child)), rc);
1854 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1856 /* handling -ENOENT is important because it may change
1857 * object state in DNE env dropping LOHA_EXISTS flag,
1858 * it is important to return that to the caller.
1859 * Check LU-13115 for details.
1868 /* check whether two FIDs belong to different MDT. */
1869 static int mdt_fids_different_target(struct mdt_thread_info *info,
1870 const struct lu_fid *fid1,
1871 const struct lu_fid *fid2)
1873 const struct lu_env *env = info->mti_env;
1874 struct mdt_device *mdt = info->mti_mdt;
1875 struct lu_seq_range *range = &info->mti_range;
1876 struct seq_server_site *ss;
1877 __u32 index1, index2;
1880 if (fid_seq(fid1) == fid_seq(fid2))
1883 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1885 range->lsr_flags = LU_SEQ_RANGE_MDT;
1886 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1890 index1 = range->lsr_index;
1892 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1896 index2 = range->lsr_index;
1898 return index1 != index2;
1902 * Check whether \a child is remote object on \a parent.
1904 * \param[in] info thread environment
1905 * \param[in] parent parent object, it's the same as child object in
1907 * \param[in] child child object
1909 * \retval 1 is remote object.
1910 * \retval 0 isn't remote object.
1911 * \retval < 1 error code
1913 int mdt_is_remote_object(struct mdt_thread_info *info,
1914 struct mdt_object *parent,
1915 struct mdt_object *child)
1917 struct lu_buf *buf = &info->mti_big_buf;
1918 struct linkea_data ldata = { NULL };
1919 struct link_ea_header *leh;
1920 struct link_ea_entry *lee;
1921 struct lu_name name;
1929 if (fid_is_root(mdt_object_fid(child)))
1932 if (likely(parent != child)) {
1933 if (mdt_object_remote(parent) ^ mdt_object_remote(child))
1936 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1939 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1940 mdt_object_fid(child));
1944 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1945 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1950 rc = mdt_links_read(info, child, &ldata);
1951 /* can't read linkea, just assume it's remote object */
1952 if (rc == -ENOENT || rc == -ENODATA)
1958 lee = (struct link_ea_entry *)(leh + 1);
1959 for (i = 0; i < leh->leh_reccount; i++) {
1960 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1961 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1962 if (mdt_fids_different_target(info, &pfid,
1963 mdt_object_fid(child)))
1970 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
1971 struct mdt_object *child)
1973 struct lu_buf *buffer;
1974 struct mdt_body *repbody;
1975 struct req_capsule *pill = info->mti_pill;
1976 struct obd_export *exp = mdt_info_req(info)->rq_export;
1979 if (!exp_connect_encrypt(exp))
1982 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
1983 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0) {
1984 struct lu_attr la = { 0 };
1985 struct dt_object *dt = mdt_obj2dt(child);
1987 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
1988 dt_attr_get(info->mti_env, mdt_obj2dt(child), &la);
1990 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL) {
1991 buffer = &info->mti_buf;
1993 /* fill reply buffer with encryption context now */
1995 req_capsule_get_size(pill, &RMF_FILE_ENCCTX,
1998 req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
1999 rc = mo_xattr_get(info->mti_env,
2000 mdt_object_child(child),
2002 LL_XATTR_NAME_ENCRYPTION_CONTEXT);
2005 "found encryption ctx of size %d for "DFID"\n",
2006 rc, PFID(mdt_object_fid(child)));
2008 repbody = req_capsule_server_get(pill,
2010 repbody->mbo_valid |= OBD_MD_ENCCTX;
2011 if (rc < buffer->lb_len)
2012 req_capsule_shrink(pill,
2013 &RMF_FILE_ENCCTX, rc,
2018 "encryption ctx not found for "DFID": rc = %d\n",
2019 PFID(mdt_object_fid(child)), rc);
2020 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2022 /* handling -ENOENT is important because it may
2023 * change object state in DNE env dropping
2024 * LOHA_EXISTS flag, it is important to return
2025 * that to the caller.
2026 * Check LU-13115 for details.
2032 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,