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/
31 * lustre/mdt/mdt_lib.c
33 * Lustre Metadata Target (mdt) request unpacking helper.
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Phil Schwan <phil@clusterfs.com>
38 * Author: Mike Shaver <shaver@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Fan Yong <fanyong@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/user_namespace.h>
47 #include <linux/uidgid.h>
49 #include "mdt_internal.h"
50 #include <uapi/linux/lnet/nidstr.h>
51 #include <lustre_nodemap.h>
53 typedef enum ucred_init_type {
59 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
61 return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
64 void mdt_exit_ucred(struct mdt_thread_info *info)
66 struct lu_ucred *uc = mdt_ucred(info);
67 struct mdt_device *mdt = info->mti_mdt;
70 if (uc->uc_valid != UCRED_INIT) {
71 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
73 put_group_info(uc->uc_ginfo);
76 if (uc->uc_identity) {
77 mdt_identity_put(mdt->mdt_identity_cache,
79 uc->uc_identity = NULL;
81 uc->uc_valid = UCRED_INIT;
85 static int match_nosquash_list(struct spinlock *rsi_lock,
86 struct list_head *nidlist,
92 rc = cfs_match_nid(peernid, nidlist);
93 spin_unlock(rsi_lock);
97 /* root_squash for inter-MDS operations */
98 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
100 struct lu_ucred *ucred = mdt_ucred(info);
101 struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
104 LASSERT(ucred != NULL);
105 if (!squash->rsi_uid || ucred->uc_fsuid)
108 if (match_nosquash_list(&squash->rsi_lock,
109 &squash->rsi_nosquash_nids,
111 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
112 libcfs_nid2str(peernid));
116 CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
117 libcfs_nid2str(peernid),
118 ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
119 squash->rsi_uid, squash->rsi_gid, 0);
121 ucred->uc_fsuid = squash->rsi_uid;
122 ucred->uc_fsgid = squash->rsi_gid;
124 ucred->uc_suppgids[0] = -1;
125 ucred->uc_suppgids[1] = -1;
130 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
132 struct ptlrpc_request *req = mdt_info_req(info);
133 const char *jobid = mdt_req_get_jobid(req);
135 /* set jobid if specified. */
137 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
139 uc->uc_jobid[0] = '\0';
142 static void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
144 if (info && info->mti_exp && info->mti_exp->exp_connection)
145 uc->uc_nid = info->mti_exp->exp_connection->c_peer.nid;
147 uc->uc_nid = LNET_NID_ANY;
150 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
153 struct lu_nodemap *nodemap = NULL;
156 if (info && info->mti_exp) {
157 nodemap = nodemap_get_from_exp(info->mti_exp);
158 if (nodemap && !IS_ERR(nodemap)) {
159 audit = nodemap->nmf_enable_audit;
160 nodemap_putref(nodemap);
164 uc->uc_enable_audit = audit;
167 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
170 struct ptlrpc_request *req = mdt_info_req(info);
171 struct mdt_device *mdt = info->mti_mdt;
172 struct ptlrpc_user_desc *pud = req->rq_user_desc;
173 struct lu_ucred *ucred = mdt_ucred(info);
174 struct lu_nodemap *nodemap;
175 lnet_nid_t peernid = req->rq_peer.nid;
179 bool is_nm_gid_squashed = false;
184 LASSERT(req->rq_auth_gss);
185 LASSERT(!req->rq_auth_usr_mdt);
186 LASSERT(req->rq_user_desc);
187 LASSERT(ucred != NULL);
189 ucred->uc_valid = UCRED_INVALID;
191 nodemap = nodemap_get_from_exp(info->mti_exp);
193 RETURN(PTR_ERR(nodemap));
195 pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
196 NODEMAP_CLIENT_TO_FS, pud->pud_uid);
197 pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
198 NODEMAP_CLIENT_TO_FS, pud->pud_gid);
199 pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
200 NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
201 pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
202 NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
204 ucred->uc_o_uid = pud->pud_uid;
205 ucred->uc_o_gid = pud->pud_gid;
206 ucred->uc_o_fsuid = pud->pud_fsuid;
207 ucred->uc_o_fsgid = pud->pud_fsgid;
209 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
210 /* deny access before we get identity ref */
211 if (nodemap->nmf_deny_unknown) {
212 nodemap_putref(nodemap);
216 ucred->uc_suppgids[0] = -1;
217 ucred->uc_suppgids[1] = -1;
220 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
221 is_nm_gid_squashed = true;
223 nodemap_putref(nodemap);
225 if (type == BODY_INIT) {
226 struct mdt_body *body = (struct mdt_body *)buf;
228 ucred->uc_suppgids[0] = body->mbo_suppgid;
229 ucred->uc_suppgids[1] = -1;
232 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
233 req->rq_auth_uid != pud->pud_uid) {
234 CDEBUG(D_SEC, "local client %s: auth uid %u "
235 "while client claims %u:%u/%u:%u\n",
236 libcfs_nid2str(peernid), req->rq_auth_uid,
237 pud->pud_uid, pud->pud_gid,
238 pud->pud_fsuid, pud->pud_fsgid);
242 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
243 ucred->uc_identity = NULL;
244 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
246 struct md_identity *identity;
248 identity = mdt_identity_get(mdt->mdt_identity_cache,
250 if (IS_ERR(identity)) {
251 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
252 ucred->uc_identity = NULL;
253 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
257 "Deny access without identity: uid %u\n",
262 ucred->uc_identity = identity;
263 perm = mdt_identity_get_perm(ucred->uc_identity,
268 /* find out the setuid/setgid attempt */
269 setuid = (pud->pud_uid != pud->pud_fsuid);
270 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
271 (ucred->uc_identity &&
272 (pud->pud_gid != ucred->uc_identity->mi_gid)));
274 /* check permission of setuid */
275 if (setuid && !(perm & CFS_SETUID_PERM)) {
276 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
277 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
278 GOTO(out, rc = -EACCES);
281 /* check permission of setgid */
282 if (setgid && !(perm & CFS_SETGID_PERM)) {
283 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
284 "from %s\n", pud->pud_uid, pud->pud_gid,
285 pud->pud_fsuid, pud->pud_fsgid,
286 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
287 GOTO(out, rc = -EACCES);
290 if (perm & CFS_SETGRP_PERM) {
291 /* only set groups if GID is not squashed */
292 if (pud->pud_ngroups && !is_nm_gid_squashed) {
293 /* setgroups for local client */
294 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
295 if (!ucred->uc_ginfo) {
296 CERROR("failed to alloc %d groups\n",
298 GOTO(out, rc = -ENOMEM);
301 lustre_groups_from_list(ucred->uc_ginfo,
303 lustre_groups_sort(ucred->uc_ginfo);
305 ucred->uc_suppgids[0] = -1;
306 ucred->uc_suppgids[1] = -1;
307 ucred->uc_ginfo = NULL;
310 ucred->uc_suppgids[0] = -1;
311 ucred->uc_suppgids[1] = -1;
312 ucred->uc_ginfo = NULL;
315 ucred->uc_uid = pud->pud_uid;
316 ucred->uc_gid = pud->pud_gid;
318 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
321 ucred->uc_cap = pud->pud_cap;
323 ucred->uc_fsuid = pud->pud_fsuid;
324 ucred->uc_fsgid = pud->pud_fsgid;
326 /* process root_squash here. */
327 mdt_root_squash(info, peernid);
329 ucred->uc_valid = UCRED_NEW;
330 ucred_set_jobid(info, ucred);
331 ucred_set_nid(info, ucred);
332 ucred_set_audit_enabled(info, ucred);
338 if (ucred->uc_ginfo) {
339 put_group_info(ucred->uc_ginfo);
340 ucred->uc_ginfo = NULL;
342 if (ucred->uc_identity) {
343 mdt_identity_put(mdt->mdt_identity_cache,
345 ucred->uc_identity = NULL;
353 * Check whether allow the client to set supplementary group IDs or not.
355 * \param[in] info pointer to the thread context
356 * \param[in] uc pointer to the RPC user descriptor
358 * \retval true if allow to set supplementary group IDs
359 * \retval false for other cases
361 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
365 /* 1. If identity_upcall is disabled,
366 * permit local client to do anything. */
367 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
370 /* 2. If fail to get related identities, then forbid any client to
371 * set supplementary group IDs. */
372 if (uc->uc_identity == NULL)
375 /* 3. Check the permission in the identities. */
376 perm = mdt_identity_get_perm(uc->uc_identity,
377 mdt_info_req(info)->rq_peer.nid);
378 if (perm & CFS_SETGRP_PERM)
384 int mdt_check_ucred(struct mdt_thread_info *info)
386 struct ptlrpc_request *req = mdt_info_req(info);
387 struct mdt_device *mdt = info->mti_mdt;
388 struct ptlrpc_user_desc *pud = req->rq_user_desc;
389 struct lu_ucred *ucred = mdt_ucred(info);
390 struct md_identity *identity = NULL;
391 lnet_nid_t peernid = req->rq_peer.nid;
399 LASSERT(ucred != NULL);
400 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
403 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
406 /* sanity check: if we use strong authentication, we expect the
407 * uid which client claimed is true */
408 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
409 req->rq_auth_uid != pud->pud_uid) {
410 CDEBUG(D_SEC, "local client %s: auth uid %u "
411 "while client claims %u:%u/%u:%u\n",
412 libcfs_nid2str(peernid), req->rq_auth_uid,
413 pud->pud_uid, pud->pud_gid,
414 pud->pud_fsuid, pud->pud_fsgid);
418 if (is_identity_get_disabled(mdt->mdt_identity_cache))
421 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
422 if (IS_ERR(identity)) {
423 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
426 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
432 perm = mdt_identity_get_perm(identity, peernid);
433 /* find out the setuid/setgid attempt */
434 setuid = (pud->pud_uid != pud->pud_fsuid);
435 setgid = (pud->pud_gid != pud->pud_fsgid ||
436 pud->pud_gid != identity->mi_gid);
438 /* check permission of setuid */
439 if (setuid && !(perm & CFS_SETUID_PERM)) {
440 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
441 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
442 GOTO(out, rc = -EACCES);
445 /* check permission of setgid */
446 if (setgid && !(perm & CFS_SETGID_PERM)) {
447 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
448 "from %s\n", pud->pud_uid, pud->pud_gid,
449 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
450 libcfs_nid2str(peernid));
451 GOTO(out, rc = -EACCES);
457 mdt_identity_put(mdt->mdt_identity_cache, identity);
461 static int old_init_ucred_common(struct mdt_thread_info *info,
462 struct lu_nodemap *nodemap)
464 struct lu_ucred *uc = mdt_ucred(info);
465 struct mdt_device *mdt = info->mti_mdt;
466 struct md_identity *identity = NULL;
468 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
469 /* deny access before we get identity ref */
470 if (nodemap->nmf_deny_unknown)
474 uc->uc_suppgids[0] = -1;
475 uc->uc_suppgids[1] = -1;
478 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
479 identity = mdt_identity_get(mdt->mdt_identity_cache,
481 if (IS_ERR(identity)) {
482 kernel_cap_t kcap = cap_combine(CAP_FS_SET,
484 u32 cap_mask = kcap.cap[0];
486 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
487 uc->uc_cap & cap_mask)) {
490 CDEBUG(D_SEC, "Deny access without identity: "
491 "uid %u\n", uc->uc_fsuid);
496 uc->uc_identity = identity;
498 /* process root_squash here. */
499 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
501 uc->uc_valid = UCRED_OLD;
502 ucred_set_jobid(info, uc);
503 ucred_set_nid(info, uc);
504 ucred_set_audit_enabled(info, uc);
511 static int old_init_ucred(struct mdt_thread_info *info,
512 struct mdt_body *body)
514 struct lu_ucred *uc = mdt_ucred(info);
515 struct lu_nodemap *nodemap;
519 nodemap = nodemap_get_from_exp(info->mti_exp);
521 RETURN(PTR_ERR(nodemap));
523 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
524 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
525 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
526 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
527 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
528 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
529 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
530 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
533 uc->uc_valid = UCRED_INVALID;
534 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
535 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
536 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
537 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
538 uc->uc_suppgids[0] = body->mbo_suppgid;
539 uc->uc_suppgids[1] = -1;
541 uc->uc_cap = body->mbo_capability;
543 rc = old_init_ucred_common(info, nodemap);
544 nodemap_putref(nodemap);
549 static int old_init_ucred_reint(struct mdt_thread_info *info)
551 struct lu_ucred *uc = mdt_ucred(info);
552 struct lu_nodemap *nodemap;
556 nodemap = nodemap_get_from_exp(info->mti_exp);
558 RETURN(PTR_ERR(nodemap));
562 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
563 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
564 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
565 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
567 uc->uc_valid = UCRED_INVALID;
568 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
569 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
572 rc = old_init_ucred_common(info, nodemap);
573 nodemap_putref(nodemap);
578 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
579 struct mdt_body *body)
581 struct ptlrpc_request *req = mdt_info_req(info);
582 struct lu_ucred *uc = mdt_ucred(info);
585 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
588 mdt_exit_ucred(info);
590 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
591 return old_init_ucred(info, body);
593 return new_init_ucred(info, BODY_INIT, body);
596 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
598 return __mdt_init_ucred(info, body);
601 int mdt_init_ucred_reint(struct mdt_thread_info *info)
603 struct ptlrpc_request *req = mdt_info_req(info);
604 struct lu_ucred *uc = mdt_ucred(info);
605 struct md_attr *ma = &info->mti_attr;
608 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
611 /* LU-5564: for normal close request, skip permission check */
612 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
613 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP))) {
614 kernel_cap_t kcap = { { uc->uc_cap, } };
616 kcap = cap_raise_nfsd_set(kcap, CAP_FULL_SET);
617 kcap = cap_raise_fs_set(kcap, CAP_FULL_SET);
618 uc->uc_cap = kcap.cap[0];
621 mdt_exit_ucred(info);
623 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
624 return old_init_ucred_reint(info);
626 return new_init_ucred(info, REC_INIT, NULL);
629 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
630 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
632 const struct lov_ost_data_v1 *lod;
633 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
637 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
640 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
641 POSTID(&lmm->lmm_oi), lmm_magic,
642 le32_to_cpu(lmm->lmm_pattern));
644 /* No support for compount layouts yet */
645 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
648 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
649 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
650 le32_to_cpu(lmm->lmm_stripe_size), count);
652 /* If it's a directory or a released file, then there are
653 * no actual objects to print, so bail out. */
654 if (valid & OBD_MD_FLDIREA ||
655 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
658 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
659 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
662 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
663 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
664 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
668 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
670 const struct lmv_mds_md_v1 *lmm1;
671 const struct lmv_foreign_md *lfm;
674 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
677 /* foreign LMV case */
678 lfm = &lmv->lmv_foreign_md;
679 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
681 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
682 le32_to_cpu(lfm->lfm_magic),
683 le32_to_cpu(lfm->lfm_length),
684 le32_to_cpu(lfm->lfm_type),
685 le32_to_cpu(lfm->lfm_flags),
686 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
690 lmm1 = &lmv->lmv_md_v1;
692 "magic 0x%08X, master %#X stripe_count %d hash_type %#x\n",
693 le32_to_cpu(lmm1->lmv_magic),
694 le32_to_cpu(lmm1->lmv_master_mdt_index),
695 le32_to_cpu(lmm1->lmv_stripe_count),
696 le32_to_cpu(lmm1->lmv_hash_type));
698 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
701 if (le32_to_cpu(lmm1->lmv_stripe_count) > LMV_MAX_STRIPE_COUNT)
704 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
707 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
708 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
712 /* Shrink and/or grow reply buffers */
713 int mdt_fix_reply(struct mdt_thread_info *info)
715 struct req_capsule *pill = info->mti_pill;
716 struct mdt_body *body;
717 int md_size, md_packed = 0;
722 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
723 LASSERT(body != NULL);
725 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
727 md_size = body->mbo_eadatasize;
731 acl_size = body->mbo_aclsize;
733 /* this replay - not send info to client */
734 if (info->mti_spec.no_create) {
739 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
744 &RMF_ACL, or &RMF_LOGCOOKIES
745 (optional) &RMF_CAPA1,
746 (optional) &RMF_CAPA2,
747 (optional) something else
750 /* MDT_MD buffer may be bigger than packed value, let's shrink all
751 * buffers before growing it */
752 if (info->mti_big_lmm_used) {
753 /* big_lmm buffer may be used even without packing the result
754 * into reply, just for internal server needs */
755 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
756 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
759 /* free big lmm if md_size is not needed */
760 if (md_size == 0 || md_packed == 0) {
761 info->mti_big_lmm_used = 0;
763 /* buffer must be allocated separately */
764 LASSERT(info->mti_attr.ma_lmm !=
765 req_capsule_server_get(pill, &RMF_MDT_MD));
766 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
768 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
769 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
772 if (info->mti_big_acl_used) {
774 info->mti_big_acl_used = 0;
776 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
777 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
778 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
779 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
780 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
783 /* Shrink optional SECCTX buffer if it is not used */
784 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
785 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
786 !(body->mbo_valid & OBD_MD_SECCTX))
787 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
789 /* Shrink optional ENCCTX buffer if it is not used */
790 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
791 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0 &&
792 !(body->mbo_valid & OBD_MD_ENCCTX))
793 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0, RCL_SERVER);
796 * Some more field should be shrinked if needed.
797 * This should be done by those who added fields to reply message.
800 /* Grow MD buffer if needed finally */
801 if (info->mti_big_lmm_used) {
804 LASSERT(md_size > md_packed);
805 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
806 md_size - md_packed);
807 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
809 /* we can't answer with proper LOV EA, drop flags,
810 * the rc is also returned so this request is
811 * considered as failed */
812 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
813 /* don't return transno along with error */
814 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
816 /* now we need to pack right LOV/LMV EA */
817 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
818 if (info->mti_attr.ma_valid & MA_LOV) {
819 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
821 info->mti_attr.ma_lmm_size);
822 memcpy(lmm, info->mti_attr.ma_lmm,
823 info->mti_attr.ma_lmm_size);
824 } else if (info->mti_attr.ma_valid & MA_LMV) {
825 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
827 info->mti_attr.ma_lmv_size);
828 memcpy(lmm, info->mti_attr.ma_lmv,
829 info->mti_attr.ma_lmv_size);
833 /* update mdt_max_mdsize so clients will be aware about that */
834 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
835 info->mti_mdt->mdt_max_mdsize =
836 info->mti_attr.ma_lmm_size;
837 info->mti_big_lmm_used = 0;
840 if (info->mti_big_acl_used) {
841 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
844 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
846 body->mbo_valid &= ~OBD_MD_FLACL;
848 void *acl = req_capsule_server_get(pill, &RMF_ACL);
850 memcpy(acl, info->mti_big_acl, acl_size);
853 info->mti_big_acl_used = 0;
860 /* if object is dying, pack the lov/llog data,
861 * parameter info->mti_attr should be valid at this point!
862 * Also implements RAoLU policy */
863 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
866 struct mdt_body *repbody = NULL;
867 const struct lu_attr *la = &ma->ma_attr;
868 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
871 struct hsm_action_item hai = {
872 .hai_len = sizeof(hai),
873 .hai_action = HSMA_REMOVE,
874 .hai_extent.length = -1,
882 if (mdt_info_req(info) != NULL) {
883 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
884 LASSERT(repbody != NULL);
886 CDEBUG(D_INFO, "not running in a request/reply context\n");
889 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
890 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
892 if (ma->ma_valid & MA_LOV) {
893 CERROR("No need in LOV EA upon unlink\n");
897 repbody->mbo_eadatasize = 0;
899 /* Only check unlinked and archived if RAoLU and upon last close */
900 if (!cdt->cdt_remove_archive_on_last_unlink ||
901 atomic_read(&mo->mot_open_count) != 0)
904 /* mdt_attr_get_complex will clear ma_valid, so check here first */
905 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
908 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
911 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
913 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
914 * try setting them */
916 rc = mdt_attr_get_complex(info, mo, ma);
918 CERROR("%s: unable to fetch missing attributes of"
919 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
920 PFID(mdt_object_fid(mo)), rc);
924 if (need & MA_INODE) {
925 if (ma->ma_valid & MA_INODE) {
926 if (ma->ma_attr.la_nlink != 0)
934 if (ma->ma_valid & MA_HSM) {
935 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
943 /* RAoLU policy is active, last close on file has occured,
944 * file is unlinked, file is archived, so create remove request
946 * If CDT is not running, requests will be logged for later. */
947 if (ma->ma_hsm.mh_arch_id != 0)
948 archive_id = ma->ma_hsm.mh_arch_id;
950 archive_id = cdt->cdt_default_archive_id;
952 hai.hai_fid = *mdt_object_fid(mo);
954 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
957 CERROR("%s: unable to add HSM remove request for "DFID
958 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
959 PFID(mdt_object_fid(mo)), rc);
964 static __u64 mdt_attr_valid_xlate(enum mds_attr_flags in,
965 struct mdt_reint_record *rr,
971 if (in & MDS_ATTR_MODE)
973 if (in & MDS_ATTR_UID)
975 if (in & MDS_ATTR_GID)
977 if (in & MDS_ATTR_SIZE)
979 if (in & MDS_ATTR_BLOCKS)
981 if (in & MDS_ATTR_ATIME_SET)
983 if (in & MDS_ATTR_CTIME_SET)
985 if (in & MDS_ATTR_MTIME_SET)
987 if (in & MDS_ATTR_ATTR_FLAG)
989 if (in & MDS_ATTR_KILL_SUID)
991 if (in & MDS_ATTR_KILL_SGID)
993 if (in & MDS_ATTR_PROJID)
995 if (in & MDS_ATTR_LSIZE)
997 if (in & MDS_ATTR_LBLOCKS)
1000 if (in & MDS_ATTR_FROM_OPEN)
1001 rr->rr_flags |= MRF_OPEN_TRUNC;
1002 if (in & MDS_ATTR_OVERRIDE)
1003 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1004 if (in & MDS_ATTR_FORCE)
1005 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1007 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1008 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1009 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1010 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1011 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1012 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1015 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", (u64)in);
1022 int mdt_name_unpack(struct req_capsule *pill,
1023 const struct req_msg_field *field,
1025 enum mdt_name_flags flags)
1027 ln->ln_name = req_capsule_client_get(pill, field);
1028 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1030 if (!lu_name_is_valid(ln)) {
1037 if ((flags & MNF_FIX_ANON) &&
1038 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1039 /* Newer (3.x) kernels use a name of "/" for the
1040 * "anonymous" disconnected dentries from NFS
1041 * filehandle conversion. See d_obtain_alias(). */
1049 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1050 const char **secctx_name,
1051 void **secctx, size_t *secctx_size)
1056 *secctx_name = NULL;
1060 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1061 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1064 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1069 if (name_size > XATTR_NAME_MAX + 1)
1072 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1073 if (strnlen(name, name_size) != name_size - 1)
1076 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1077 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1080 *secctx_name = name;
1081 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1082 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1087 static int mdt_file_encctx_unpack(struct req_capsule *pill,
1088 void **encctx, size_t *encctx_size)
1093 if (!exp_connect_encrypt(pill->rc_req->rq_export))
1096 if (!req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_CLIENT) ||
1097 !req_capsule_field_present(pill, &RMF_FILE_ENCCTX, RCL_CLIENT))
1100 *encctx_size = req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_CLIENT);
1101 if (*encctx_size == 0)
1104 *encctx = req_capsule_client_get(pill, &RMF_FILE_ENCCTX);
1109 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1111 struct lu_ucred *uc = mdt_ucred(info);
1112 struct md_attr *ma = &info->mti_attr;
1113 struct lu_attr *la = &ma->ma_attr;
1114 struct req_capsule *pill = info->mti_pill;
1115 struct mdt_reint_record *rr = &info->mti_rr;
1116 struct mdt_rec_setattr *rec;
1117 struct lu_nodemap *nodemap;
1121 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1122 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1126 /* This prior initialization is needed for old_init_ucred_reint() */
1127 uc->uc_fsuid = rec->sa_fsuid;
1128 uc->uc_fsgid = rec->sa_fsgid;
1129 uc->uc_cap = rec->sa_cap;
1130 uc->uc_suppgids[0] = rec->sa_suppgid;
1131 uc->uc_suppgids[1] = -1;
1133 rr->rr_fid1 = &rec->sa_fid;
1134 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1135 la->la_mode = rec->sa_mode;
1136 la->la_flags = rec->sa_attr_flags;
1138 nodemap = nodemap_get_from_exp(info->mti_exp);
1139 if (IS_ERR(nodemap))
1140 RETURN(PTR_ERR(nodemap));
1142 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1143 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1144 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1145 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1146 la->la_projid = rec->sa_projid;
1147 nodemap_putref(nodemap);
1149 la->la_size = rec->sa_size;
1150 la->la_blocks = rec->sa_blocks;
1151 la->la_ctime = rec->sa_ctime;
1152 la->la_atime = rec->sa_atime;
1153 la->la_mtime = rec->sa_mtime;
1154 ma->ma_valid = MA_INODE;
1156 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1157 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1162 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1164 struct req_capsule *pill = info->mti_pill;
1165 struct mdt_ioepoch *ioepoch;
1168 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1169 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1173 if (ioepoch == NULL)
1176 info->mti_open_handle = ioepoch->mio_open_handle;
1181 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1182 struct req_capsule *pill = info->mti_pill;
1184 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1185 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1186 if (info->mti_dlm_req == NULL)
1193 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1195 struct mdt_reint_record *rr = &info->mti_rr;
1196 struct md_attr *ma = &info->mti_attr;
1197 struct req_capsule *pill = info->mti_pill;
1201 rc = mdt_setattr_unpack_rec(info);
1205 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1206 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1207 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1210 if (rr->rr_eadatalen > 0) {
1211 const struct lmv_user_md *lum;
1213 lum = rr->rr_eadata;
1214 /* Sigh ma_valid(from req) does not indicate whether
1215 * it will set LOV/LMV EA, so we have to check magic */
1216 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1217 ma->ma_valid |= MA_LMV;
1218 ma->ma_lmv = (void *)rr->rr_eadata;
1219 ma->ma_lmv_size = rr->rr_eadatalen;
1221 ma->ma_valid |= MA_LOV;
1222 ma->ma_lmm = (void *)rr->rr_eadata;
1223 ma->ma_lmm_size = rr->rr_eadatalen;
1228 rc = mdt_dlmreq_unpack(info);
1232 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1234 struct md_attr *ma = &info->mti_attr;
1235 struct req_capsule *pill = info->mti_pill;
1238 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1241 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1243 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1244 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1250 int mdt_close_unpack(struct mdt_thread_info *info)
1255 rc = mdt_close_handle_unpack(info);
1259 rc = mdt_setattr_unpack_rec(info);
1263 rc = mdt_close_intent_unpack(info);
1267 RETURN(mdt_init_ucred_reint(info));
1270 static int mdt_create_unpack(struct mdt_thread_info *info)
1272 struct lu_ucred *uc = mdt_ucred(info);
1273 struct mdt_rec_create *rec;
1274 struct lu_attr *attr = &info->mti_attr.ma_attr;
1275 struct mdt_reint_record *rr = &info->mti_rr;
1276 struct req_capsule *pill = info->mti_pill;
1277 struct md_op_spec *sp = &info->mti_spec;
1282 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1283 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1287 /* This prior initialization is needed for old_init_ucred_reint() */
1288 uc->uc_fsuid = rec->cr_fsuid;
1289 uc->uc_fsgid = rec->cr_fsgid;
1290 uc->uc_cap = rec->cr_cap;
1291 uc->uc_suppgids[0] = rec->cr_suppgid1;
1292 uc->uc_suppgids[1] = -1;
1293 uc->uc_umask = rec->cr_umask;
1295 rr->rr_fid1 = &rec->cr_fid1;
1296 rr->rr_fid2 = &rec->cr_fid2;
1297 attr->la_mode = rec->cr_mode;
1298 attr->la_rdev = rec->cr_rdev;
1299 attr->la_uid = rec->cr_fsuid;
1300 attr->la_gid = rec->cr_fsgid;
1301 attr->la_ctime = rec->cr_time;
1302 attr->la_mtime = rec->cr_time;
1303 attr->la_atime = rec->cr_time;
1304 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1305 LA_CTIME | LA_MTIME | LA_ATIME;
1306 memset(&sp->u, 0, sizeof(sp->u));
1307 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1309 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1313 if (S_ISLNK(attr->la_mode)) {
1314 const char *tgt = NULL;
1316 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1317 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1318 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1319 sp->u.sp_symname = tgt;
1324 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1325 if (S_ISDIR(attr->la_mode) &&
1326 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1327 sp->u.sp_ea.eadata =
1328 req_capsule_client_get(pill, &RMF_EADATA);
1329 sp->u.sp_ea.eadatalen =
1330 req_capsule_get_size(pill, &RMF_EADATA,
1332 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1336 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1337 &sp->sp_cr_file_secctx,
1338 &sp->sp_cr_file_secctx_size);
1342 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1343 &sp->sp_cr_file_encctx_size);
1347 rc = req_check_sepol(pill);
1351 rc = mdt_dlmreq_unpack(info);
1355 static int mdt_link_unpack(struct mdt_thread_info *info)
1357 struct lu_ucred *uc = mdt_ucred(info);
1358 struct mdt_rec_link *rec;
1359 struct lu_attr *attr = &info->mti_attr.ma_attr;
1360 struct mdt_reint_record *rr = &info->mti_rr;
1361 struct req_capsule *pill = info->mti_pill;
1366 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1367 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1371 /* This prior initialization is needed for old_init_ucred_reint() */
1372 uc->uc_fsuid = rec->lk_fsuid;
1373 uc->uc_fsgid = rec->lk_fsgid;
1374 uc->uc_cap = rec->lk_cap;
1375 uc->uc_suppgids[0] = rec->lk_suppgid1;
1376 uc->uc_suppgids[1] = rec->lk_suppgid2;
1378 attr->la_uid = rec->lk_fsuid;
1379 attr->la_gid = rec->lk_fsgid;
1380 rr->rr_fid1 = &rec->lk_fid1;
1381 rr->rr_fid2 = &rec->lk_fid2;
1382 attr->la_ctime = rec->lk_time;
1383 attr->la_mtime = rec->lk_time;
1384 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1386 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1390 rc = req_check_sepol(pill);
1394 rc = mdt_dlmreq_unpack(info);
1399 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1401 struct lu_ucred *uc = mdt_ucred(info);
1402 struct mdt_rec_unlink *rec;
1403 struct lu_attr *attr = &info->mti_attr.ma_attr;
1404 struct mdt_reint_record *rr = &info->mti_rr;
1405 struct req_capsule *pill = info->mti_pill;
1410 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1411 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1415 /* This prior initialization is needed for old_init_ucred_reint() */
1416 uc->uc_fsuid = rec->ul_fsuid;
1417 uc->uc_fsgid = rec->ul_fsgid;
1418 uc->uc_cap = rec->ul_cap;
1419 uc->uc_suppgids[0] = rec->ul_suppgid1;
1420 uc->uc_suppgids[1] = -1;
1422 attr->la_uid = rec->ul_fsuid;
1423 attr->la_gid = rec->ul_fsgid;
1424 rr->rr_fid1 = &rec->ul_fid1;
1425 rr->rr_fid2 = &rec->ul_fid2;
1426 attr->la_ctime = rec->ul_time;
1427 attr->la_mtime = rec->ul_time;
1428 attr->la_mode = rec->ul_mode;
1429 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1431 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1435 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1437 rc = req_check_sepol(pill);
1441 rc = mdt_dlmreq_unpack(info);
1445 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1447 info->mti_spec.sp_rm_entry = 1;
1448 return mdt_unlink_unpack(info);
1451 static int mdt_rename_unpack(struct mdt_thread_info *info)
1453 struct lu_ucred *uc = mdt_ucred(info);
1454 struct mdt_rec_rename *rec;
1455 struct lu_attr *attr = &info->mti_attr.ma_attr;
1456 struct mdt_reint_record *rr = &info->mti_rr;
1457 struct req_capsule *pill = info->mti_pill;
1458 struct md_op_spec *spec = &info->mti_spec;
1463 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1464 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1468 /* This prior initialization is needed for old_init_ucred_reint() */
1469 uc->uc_fsuid = rec->rn_fsuid;
1470 uc->uc_fsgid = rec->rn_fsgid;
1471 uc->uc_cap = rec->rn_cap;
1472 uc->uc_suppgids[0] = rec->rn_suppgid1;
1473 uc->uc_suppgids[1] = rec->rn_suppgid2;
1475 attr->la_uid = rec->rn_fsuid;
1476 attr->la_gid = rec->rn_fsgid;
1477 rr->rr_fid1 = &rec->rn_fid1;
1478 rr->rr_fid2 = &rec->rn_fid2;
1479 attr->la_ctime = rec->rn_time;
1480 attr->la_mtime = rec->rn_time;
1481 /* rename_tgt contains the mode already */
1482 attr->la_mode = rec->rn_mode;
1483 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1485 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1489 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1493 spec->no_create = !!req_is_replay(mdt_info_req(info));
1495 rc = req_check_sepol(pill);
1499 rc = mdt_dlmreq_unpack(info);
1504 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1506 struct lu_ucred *uc = mdt_ucred(info);
1507 struct mdt_rec_rename *rec;
1508 struct lu_attr *attr = &info->mti_attr.ma_attr;
1509 struct mdt_reint_record *rr = &info->mti_rr;
1510 struct req_capsule *pill = info->mti_pill;
1511 struct md_op_spec *spec = &info->mti_spec;
1516 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1517 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1521 /* This prior initialization is needed for old_init_ucred_reint() */
1522 uc->uc_fsuid = rec->rn_fsuid;
1523 uc->uc_fsgid = rec->rn_fsgid;
1524 uc->uc_cap = rec->rn_cap;
1525 uc->uc_suppgids[0] = rec->rn_suppgid1;
1526 uc->uc_suppgids[1] = rec->rn_suppgid2;
1528 attr->la_uid = rec->rn_fsuid;
1529 attr->la_gid = rec->rn_fsgid;
1530 rr->rr_fid1 = &rec->rn_fid1;
1531 rr->rr_fid2 = &rec->rn_fid2;
1532 attr->la_ctime = rec->rn_time;
1533 attr->la_mtime = rec->rn_time;
1534 /* rename_tgt contains the mode already */
1535 attr->la_mode = rec->rn_mode;
1536 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1537 spec->sp_cr_flags = 0;
1539 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1543 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1544 rc = mdt_close_handle_unpack(info);
1548 spec->sp_migrate_close = 1;
1550 spec->sp_migrate_close = 0;
1552 spec->sp_migrate_nsonly = 0;
1554 /* lustre version > 2.11 migration packs lum */
1555 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1556 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1557 rr->rr_eadatalen = req_capsule_get_size(pill,
1561 if (rr->rr_eadatalen > 0) {
1562 struct lmv_user_md_v1 *lmu;
1564 lmu = req_capsule_client_get(pill, &RMF_EADATA);
1565 lmu->lum_hash_type |=
1566 cpu_to_le32(LMV_HASH_FLAG_FIXED);
1567 rr->rr_eadata = lmu;
1568 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1569 spec->u.sp_ea.eadata = rr->rr_eadata;
1570 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1573 /* old client doesn't provide lum. */
1574 RETURN(-EOPNOTSUPP);
1578 spec->no_create = !!req_is_replay(mdt_info_req(info));
1580 rc = mdt_dlmreq_unpack(info);
1586 * please see comment above LOV_MAGIC_V1_DEFINED
1588 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1590 struct lov_user_md_v1 *v1 = eadata;
1594 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1595 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1596 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1597 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1598 __swab32(LOV_MAGIC_MAGIC))
1599 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1603 static int mdt_open_unpack(struct mdt_thread_info *info)
1605 struct lu_ucred *uc = mdt_ucred(info);
1606 struct mdt_rec_create *rec;
1607 struct lu_attr *attr = &info->mti_attr.ma_attr;
1608 struct req_capsule *pill = info->mti_pill;
1609 struct mdt_reint_record *rr = &info->mti_rr;
1610 struct ptlrpc_request *req = mdt_info_req(info);
1611 struct md_op_spec *sp = &info->mti_spec;
1615 BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1616 sizeof(struct mdt_rec_reint));
1617 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1621 /* This prior initialization is needed for old_init_ucred_reint() */
1622 uc->uc_fsuid = rec->cr_fsuid;
1623 uc->uc_fsgid = rec->cr_fsgid;
1624 uc->uc_cap = rec->cr_cap;
1625 uc->uc_suppgids[0] = rec->cr_suppgid1;
1626 uc->uc_suppgids[1] = rec->cr_suppgid2;
1627 uc->uc_umask = rec->cr_umask;
1629 rr->rr_fid1 = &rec->cr_fid1;
1630 rr->rr_fid2 = &rec->cr_fid2;
1631 rr->rr_open_handle = &rec->cr_open_handle_old;
1632 attr->la_mode = rec->cr_mode;
1633 attr->la_rdev = rec->cr_rdev;
1634 attr->la_uid = rec->cr_fsuid;
1635 attr->la_gid = rec->cr_fsgid;
1636 attr->la_ctime = rec->cr_time;
1637 attr->la_mtime = rec->cr_time;
1638 attr->la_atime = rec->cr_time;
1639 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1640 LA_CTIME | LA_MTIME | LA_ATIME;
1641 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1642 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1643 /* Do not trigger ASSERTION if client miss to set such flags. */
1644 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1647 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1649 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1651 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1652 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1655 if (rr->rr_eadatalen > 0) {
1656 rr->rr_eadata = req_capsule_client_get(pill,
1658 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1659 sp->u.sp_ea.eadata = rr->rr_eadata;
1660 sp->sp_archive_id = rec->cr_archive_id;
1661 sp->no_create = !!req_is_replay(req);
1662 mdt_fix_lov_magic(info, rr->rr_eadata);
1666 * Client default md_size may be 0 right after client start,
1667 * until all osc are connected, set here just some reasonable
1668 * value to prevent misbehavior.
1670 if (rr->rr_eadatalen == 0 &&
1671 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1672 rr->rr_eadatalen = MIN_MD_SIZE;
1675 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1676 &sp->sp_cr_file_secctx,
1677 &sp->sp_cr_file_secctx_size);
1681 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1682 &sp->sp_cr_file_encctx_size);
1686 rc = req_check_sepol(pill);
1693 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1695 struct mdt_reint_record *rr = &info->mti_rr;
1696 struct lu_ucred *uc = mdt_ucred(info);
1697 struct lu_attr *attr = &info->mti_attr.ma_attr;
1698 struct req_capsule *pill = info->mti_pill;
1699 struct mdt_rec_setxattr *rec;
1704 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1705 sizeof(struct mdt_rec_reint));
1706 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1710 /* This prior initialization is needed for old_init_ucred_reint() */
1711 uc->uc_fsuid = rec->sx_fsuid;
1712 uc->uc_fsgid = rec->sx_fsgid;
1713 uc->uc_cap = rec->sx_cap;
1714 uc->uc_suppgids[0] = rec->sx_suppgid1;
1715 uc->uc_suppgids[1] = -1;
1717 rr->rr_opcode = rec->sx_opcode;
1718 rr->rr_fid1 = &rec->sx_fid;
1719 attr->la_valid = rec->sx_valid;
1720 attr->la_ctime = rec->sx_time;
1721 attr->la_size = rec->sx_size;
1722 attr->la_flags = rec->sx_flags;
1724 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1728 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1729 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1732 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1735 if (rr->rr_eadatalen > 0) {
1736 rr->rr_eadata = req_capsule_client_get(pill,
1738 if (rr->rr_eadata == NULL)
1741 rr->rr_eadata = NULL;
1743 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1744 CDEBUG(D_INFO, "no xattr data supplied\n");
1748 rc = req_check_sepol(pill);
1752 if (mdt_dlmreq_unpack(info) < 0)
1758 static int mdt_resync_unpack(struct mdt_thread_info *info)
1760 struct req_capsule *pill = info->mti_pill;
1761 struct mdt_reint_record *rr = &info->mti_rr;
1762 struct lu_ucred *uc = mdt_ucred(info);
1763 struct mdt_rec_resync *rec;
1766 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1767 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1771 /* This prior initialization is needed for old_init_ucred_reint() */
1772 uc->uc_fsuid = rec->rs_fsuid;
1773 uc->uc_fsgid = rec->rs_fsgid;
1774 uc->uc_cap = rec->rs_cap;
1776 rr->rr_fid1 = &rec->rs_fid;
1777 rr->rr_mirror_id = rec->rs_mirror_id;
1779 /* cookie doesn't need to be swapped but it has been swapped
1780 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1782 if (req_capsule_req_need_swab(pill))
1783 __swab64s(&rec->rs_lease_handle.cookie);
1784 rr->rr_lease_handle = &rec->rs_lease_handle;
1786 RETURN(mdt_dlmreq_unpack(info));
1789 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1791 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1792 [REINT_SETATTR] = mdt_setattr_unpack,
1793 [REINT_CREATE] = mdt_create_unpack,
1794 [REINT_LINK] = mdt_link_unpack,
1795 [REINT_UNLINK] = mdt_unlink_unpack,
1796 [REINT_RENAME] = mdt_rename_unpack,
1797 [REINT_OPEN] = mdt_open_unpack,
1798 [REINT_SETXATTR] = mdt_setxattr_unpack,
1799 [REINT_RMENTRY] = mdt_rmentry_unpack,
1800 [REINT_MIGRATE] = mdt_migrate_unpack,
1801 [REINT_RESYNC] = mdt_resync_unpack,
1804 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1809 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1810 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1811 info->mti_rr.rr_opcode = op;
1812 rc = mdt_reint_unpackers[op](info);
1814 CERROR("Unexpected opcode %d\n", op);
1820 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1821 struct mdt_object *child)
1824 struct lu_buf *buffer;
1825 struct mdt_body *repbody;
1826 struct req_capsule *pill = info->mti_pill;
1829 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1830 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1832 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1833 buffer = &info->mti_buf;
1835 /* fill reply buffer with security context now */
1836 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1838 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1840 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1841 buffer, secctx_name);
1844 "found security context of size %d for "DFID"\n",
1845 rc, PFID(mdt_object_fid(child)));
1847 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1848 repbody->mbo_valid |= OBD_MD_SECCTX;
1849 if (rc < buffer->lb_len)
1850 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1855 "security context not found for "DFID": rc = %d\n",
1856 PFID(mdt_object_fid(child)), rc);
1857 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1859 /* handling -ENOENT is important because it may change
1860 * object state in DNE env dropping LOHA_EXISTS flag,
1861 * it is important to return that to the caller.
1862 * Check LU-13115 for details.
1871 /* check whether two FIDs belong to different MDT. */
1872 static int mdt_fids_different_target(struct mdt_thread_info *info,
1873 const struct lu_fid *fid1,
1874 const struct lu_fid *fid2)
1876 const struct lu_env *env = info->mti_env;
1877 struct mdt_device *mdt = info->mti_mdt;
1878 struct lu_seq_range *range = &info->mti_range;
1879 struct seq_server_site *ss;
1880 __u32 index1, index2;
1883 if (fid_seq(fid1) == fid_seq(fid2))
1886 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1888 range->lsr_flags = LU_SEQ_RANGE_MDT;
1889 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1893 index1 = range->lsr_index;
1895 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1899 index2 = range->lsr_index;
1901 return index1 != index2;
1905 * Check whether \a child is remote object on \a parent.
1907 * \param[in] info thread environment
1908 * \param[in] parent parent object, it's the same as child object in
1910 * \param[in] child child object
1912 * \retval 1 is remote object.
1913 * \retval 0 isn't remote object.
1914 * \retval < 1 error code
1916 int mdt_is_remote_object(struct mdt_thread_info *info,
1917 struct mdt_object *parent,
1918 struct mdt_object *child)
1920 struct lu_buf *buf = &info->mti_big_buf;
1921 struct linkea_data ldata = { NULL };
1922 struct link_ea_header *leh;
1923 struct link_ea_entry *lee;
1924 struct lu_name name;
1932 if (fid_is_root(mdt_object_fid(child)))
1935 if (likely(parent != child)) {
1936 if (mdt_object_remote(parent) ^ mdt_object_remote(child))
1939 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1942 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1943 mdt_object_fid(child));
1947 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1948 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1953 rc = mdt_links_read(info, child, &ldata);
1954 /* can't read linkea, just assume it's remote object */
1955 if (rc == -ENOENT || rc == -ENODATA)
1961 lee = (struct link_ea_entry *)(leh + 1);
1962 for (i = 0; i < leh->leh_reccount; i++) {
1963 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1964 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1965 if (mdt_fids_different_target(info, &pfid,
1966 mdt_object_fid(child)))
1973 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
1974 struct mdt_object *child)
1976 struct lu_buf *buffer;
1977 struct mdt_body *repbody;
1978 struct req_capsule *pill = info->mti_pill;
1979 struct obd_export *exp = mdt_info_req(info)->rq_export;
1982 if (!exp_connect_encrypt(exp))
1985 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
1986 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0) {
1987 struct lu_attr la = { 0 };
1988 struct dt_object *dt = mdt_obj2dt(child);
1990 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
1991 dt_attr_get(info->mti_env, mdt_obj2dt(child), &la);
1993 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL) {
1994 buffer = &info->mti_buf;
1996 /* fill reply buffer with encryption context now */
1998 req_capsule_get_size(pill, &RMF_FILE_ENCCTX,
2001 req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
2002 rc = mo_xattr_get(info->mti_env,
2003 mdt_object_child(child),
2005 LL_XATTR_NAME_ENCRYPTION_CONTEXT);
2008 "found encryption ctx of size %d for "DFID"\n",
2009 rc, PFID(mdt_object_fid(child)));
2011 repbody = req_capsule_server_get(pill,
2013 repbody->mbo_valid |= OBD_MD_ENCCTX;
2014 if (rc < buffer->lb_len)
2015 req_capsule_shrink(pill,
2016 &RMF_FILE_ENCCTX, rc,
2021 "encryption ctx not found for "DFID": rc = %d\n",
2022 PFID(mdt_object_fid(child)), rc);
2023 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2025 /* handling -ENOENT is important because it may
2026 * change object state in DNE env dropping
2027 * LOHA_EXISTS flag, it is important to return
2028 * that to the caller.
2029 * Check LU-13115 for details.
2035 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,