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.cap[0],
119 squash->rsi_uid, squash->rsi_gid, 0);
121 ucred->uc_fsuid = squash->rsi_uid;
122 ucred->uc_fsgid = squash->rsi_gid;
123 ucred->uc_cap = CAP_EMPTY_SET;
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 ucred->uc_cap = CAP_EMPTY_SET;
319 if (!nodemap || ucred->uc_o_uid != nodemap->nm_squash_uid)
320 ucred->uc_cap.cap[0] = pud->pud_cap;
322 ucred->uc_fsuid = pud->pud_fsuid;
323 ucred->uc_fsgid = pud->pud_fsgid;
325 /* process root_squash here. */
326 mdt_root_squash(info, peernid);
328 ucred->uc_valid = UCRED_NEW;
329 ucred_set_jobid(info, ucred);
330 ucred_set_nid(info, ucred);
331 ucred_set_audit_enabled(info, ucred);
337 if (ucred->uc_ginfo) {
338 put_group_info(ucred->uc_ginfo);
339 ucred->uc_ginfo = NULL;
341 if (ucred->uc_identity) {
342 mdt_identity_put(mdt->mdt_identity_cache,
344 ucred->uc_identity = NULL;
352 * Check whether allow the client to set supplementary group IDs or not.
354 * \param[in] info pointer to the thread context
355 * \param[in] uc pointer to the RPC user descriptor
357 * \retval true if allow to set supplementary group IDs
358 * \retval false for other cases
360 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
364 /* 1. If identity_upcall is disabled,
365 * permit local client to do anything. */
366 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
369 /* 2. If fail to get related identities, then forbid any client to
370 * set supplementary group IDs. */
371 if (uc->uc_identity == NULL)
374 /* 3. Check the permission in the identities. */
375 perm = mdt_identity_get_perm(uc->uc_identity,
376 mdt_info_req(info)->rq_peer.nid);
377 if (perm & CFS_SETGRP_PERM)
383 int mdt_check_ucred(struct mdt_thread_info *info)
385 struct ptlrpc_request *req = mdt_info_req(info);
386 struct mdt_device *mdt = info->mti_mdt;
387 struct ptlrpc_user_desc *pud = req->rq_user_desc;
388 struct lu_ucred *ucred = mdt_ucred(info);
389 struct md_identity *identity = NULL;
390 lnet_nid_t peernid = req->rq_peer.nid;
398 LASSERT(ucred != NULL);
399 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
402 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
405 /* sanity check: if we use strong authentication, we expect the
406 * uid which client claimed is true */
407 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
408 req->rq_auth_uid != pud->pud_uid) {
409 CDEBUG(D_SEC, "local client %s: auth uid %u "
410 "while client claims %u:%u/%u:%u\n",
411 libcfs_nid2str(peernid), req->rq_auth_uid,
412 pud->pud_uid, pud->pud_gid,
413 pud->pud_fsuid, pud->pud_fsgid);
417 if (is_identity_get_disabled(mdt->mdt_identity_cache))
420 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
421 if (IS_ERR(identity)) {
422 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
425 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
431 perm = mdt_identity_get_perm(identity, peernid);
432 /* find out the setuid/setgid attempt */
433 setuid = (pud->pud_uid != pud->pud_fsuid);
434 setgid = (pud->pud_gid != pud->pud_fsgid ||
435 pud->pud_gid != identity->mi_gid);
437 /* check permission of setuid */
438 if (setuid && !(perm & CFS_SETUID_PERM)) {
439 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
440 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
441 GOTO(out, rc = -EACCES);
444 /* check permission of setgid */
445 if (setgid && !(perm & CFS_SETGID_PERM)) {
446 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
447 "from %s\n", pud->pud_uid, pud->pud_gid,
448 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
449 libcfs_nid2str(peernid));
450 GOTO(out, rc = -EACCES);
456 mdt_identity_put(mdt->mdt_identity_cache, identity);
460 static int old_init_ucred_common(struct mdt_thread_info *info,
461 struct lu_nodemap *nodemap)
463 struct lu_ucred *uc = mdt_ucred(info);
464 struct mdt_device *mdt = info->mti_mdt;
465 struct md_identity *identity = NULL;
467 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
468 /* deny access before we get identity ref */
469 if (nodemap->nmf_deny_unknown)
472 uc->uc_cap = CAP_EMPTY_SET;
473 uc->uc_suppgids[0] = -1;
474 uc->uc_suppgids[1] = -1;
477 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
478 identity = mdt_identity_get(mdt->mdt_identity_cache,
480 if (IS_ERR(identity)) {
481 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
482 cap_raised(uc->uc_cap,
483 CAP_DAC_READ_SEARCH))) {
486 CDEBUG(D_SEC, "Deny access without identity: "
487 "uid %u\n", uc->uc_fsuid);
492 uc->uc_identity = identity;
494 /* process root_squash here. */
495 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
497 uc->uc_valid = UCRED_OLD;
498 ucred_set_jobid(info, uc);
499 ucred_set_nid(info, uc);
500 ucred_set_audit_enabled(info, uc);
507 static int old_init_ucred(struct mdt_thread_info *info,
508 struct mdt_body *body)
510 struct lu_ucred *uc = mdt_ucred(info);
511 struct lu_nodemap *nodemap;
515 nodemap = nodemap_get_from_exp(info->mti_exp);
517 RETURN(PTR_ERR(nodemap));
519 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
520 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
521 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
522 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
523 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
524 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
525 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
526 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
529 uc->uc_valid = UCRED_INVALID;
530 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
531 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
532 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
533 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
534 uc->uc_suppgids[0] = body->mbo_suppgid;
535 uc->uc_suppgids[1] = -1;
537 uc->uc_cap = CAP_EMPTY_SET;
538 uc->uc_cap.cap[0] = body->mbo_capability;
540 rc = old_init_ucred_common(info, nodemap);
541 nodemap_putref(nodemap);
546 static int old_init_ucred_reint(struct mdt_thread_info *info)
548 struct lu_ucred *uc = mdt_ucred(info);
549 struct lu_nodemap *nodemap;
553 nodemap = nodemap_get_from_exp(info->mti_exp);
555 RETURN(PTR_ERR(nodemap));
559 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
560 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
561 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
562 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
564 uc->uc_valid = UCRED_INVALID;
565 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
566 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
569 rc = old_init_ucred_common(info, nodemap);
570 nodemap_putref(nodemap);
575 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
576 struct mdt_body *body)
578 struct ptlrpc_request *req = mdt_info_req(info);
579 struct lu_ucred *uc = mdt_ucred(info);
582 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
585 mdt_exit_ucred(info);
587 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
588 return old_init_ucred(info, body);
590 return new_init_ucred(info, BODY_INIT, body);
593 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
595 return __mdt_init_ucred(info, body);
598 int mdt_init_ucred_reint(struct mdt_thread_info *info)
600 struct ptlrpc_request *req = mdt_info_req(info);
601 struct lu_ucred *uc = mdt_ucred(info);
602 struct md_attr *ma = &info->mti_attr;
605 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
608 /* LU-5564: for normal close request, skip permission check */
609 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
610 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP))) {
611 cap_raise_nfsd_set(uc->uc_cap, CAP_FULL_SET);
612 cap_raise_fs_set(uc->uc_cap, CAP_FULL_SET);
615 mdt_exit_ucred(info);
617 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
618 return old_init_ucred_reint(info);
620 return new_init_ucred(info, REC_INIT, NULL);
623 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
624 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
626 const struct lov_ost_data_v1 *lod;
627 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
631 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
634 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
635 POSTID(&lmm->lmm_oi), lmm_magic,
636 le32_to_cpu(lmm->lmm_pattern));
638 /* No support for compount layouts yet */
639 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
642 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
643 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
644 le32_to_cpu(lmm->lmm_stripe_size), count);
646 /* If it's a directory or a released file, then there are
647 * no actual objects to print, so bail out. */
648 if (valid & OBD_MD_FLDIREA ||
649 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
652 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
653 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
656 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
657 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
658 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
662 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
664 const struct lmv_mds_md_v1 *lmm1;
665 const struct lmv_foreign_md *lfm;
668 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
671 /* foreign LMV case */
672 lfm = &lmv->lmv_foreign_md;
673 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
675 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
676 le32_to_cpu(lfm->lfm_magic),
677 le32_to_cpu(lfm->lfm_length),
678 le32_to_cpu(lfm->lfm_type),
679 le32_to_cpu(lfm->lfm_flags),
680 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
684 lmm1 = &lmv->lmv_md_v1;
686 "magic 0x%08X, master %#X stripe_count %d hash_type %#x\n",
687 le32_to_cpu(lmm1->lmv_magic),
688 le32_to_cpu(lmm1->lmv_master_mdt_index),
689 le32_to_cpu(lmm1->lmv_stripe_count),
690 le32_to_cpu(lmm1->lmv_hash_type));
692 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
695 if (le32_to_cpu(lmm1->lmv_stripe_count) > LMV_MAX_STRIPE_COUNT)
698 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
701 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
702 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
706 /* Shrink and/or grow reply buffers */
707 int mdt_fix_reply(struct mdt_thread_info *info)
709 struct req_capsule *pill = info->mti_pill;
710 struct mdt_body *body;
711 int md_size, md_packed = 0;
716 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
717 LASSERT(body != NULL);
719 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
721 md_size = body->mbo_eadatasize;
725 acl_size = body->mbo_aclsize;
727 /* this replay - not send info to client */
728 if (info->mti_spec.no_create) {
733 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
738 &RMF_ACL, or &RMF_LOGCOOKIES
739 (optional) &RMF_CAPA1,
740 (optional) &RMF_CAPA2,
741 (optional) something else
744 /* MDT_MD buffer may be bigger than packed value, let's shrink all
745 * buffers before growing it */
746 if (info->mti_big_lmm_used) {
747 /* big_lmm buffer may be used even without packing the result
748 * into reply, just for internal server needs */
749 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
750 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
753 /* free big lmm if md_size is not needed */
754 if (md_size == 0 || md_packed == 0) {
755 info->mti_big_lmm_used = 0;
757 /* buffer must be allocated separately */
758 LASSERT(info->mti_attr.ma_lmm !=
759 req_capsule_server_get(pill, &RMF_MDT_MD));
760 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
762 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
763 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
766 if (info->mti_big_acl_used) {
768 info->mti_big_acl_used = 0;
770 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
771 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
772 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
773 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
774 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
777 /* Shrink optional SECCTX buffer if it is not used */
778 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
779 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
780 !(body->mbo_valid & OBD_MD_SECCTX))
781 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
783 /* Shrink optional ENCCTX buffer if it is not used */
784 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
785 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0 &&
786 !(body->mbo_valid & OBD_MD_ENCCTX))
787 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0, RCL_SERVER);
790 * Some more field should be shrinked if needed.
791 * This should be done by those who added fields to reply message.
794 /* Grow MD buffer if needed finally */
795 if (info->mti_big_lmm_used) {
798 LASSERT(md_size > md_packed);
799 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
800 md_size - md_packed);
801 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
803 /* we can't answer with proper LOV EA, drop flags,
804 * the rc is also returned so this request is
805 * considered as failed */
806 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
807 /* don't return transno along with error */
808 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
810 /* now we need to pack right LOV/LMV EA */
811 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
812 if (info->mti_attr.ma_valid & MA_LOV) {
813 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
815 info->mti_attr.ma_lmm_size);
816 memcpy(lmm, info->mti_attr.ma_lmm,
817 info->mti_attr.ma_lmm_size);
818 } else if (info->mti_attr.ma_valid & MA_LMV) {
819 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
821 info->mti_attr.ma_lmv_size);
822 memcpy(lmm, info->mti_attr.ma_lmv,
823 info->mti_attr.ma_lmv_size);
827 /* update mdt_max_mdsize so clients will be aware about that */
828 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
829 info->mti_mdt->mdt_max_mdsize =
830 info->mti_attr.ma_lmm_size;
831 info->mti_big_lmm_used = 0;
834 if (info->mti_big_acl_used) {
835 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
838 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
840 body->mbo_valid &= ~OBD_MD_FLACL;
842 void *acl = req_capsule_server_get(pill, &RMF_ACL);
844 memcpy(acl, info->mti_big_acl, acl_size);
847 info->mti_big_acl_used = 0;
854 /* if object is dying, pack the lov/llog data,
855 * parameter info->mti_attr should be valid at this point!
856 * Also implements RAoLU policy */
857 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
860 struct mdt_body *repbody = NULL;
861 const struct lu_attr *la = &ma->ma_attr;
862 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
865 struct hsm_action_item hai = {
866 .hai_len = sizeof(hai),
867 .hai_action = HSMA_REMOVE,
868 .hai_extent.length = -1,
876 if (mdt_info_req(info) != NULL) {
877 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
878 LASSERT(repbody != NULL);
880 CDEBUG(D_INFO, "not running in a request/reply context\n");
883 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
884 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
886 if (ma->ma_valid & MA_LOV) {
887 CERROR("No need in LOV EA upon unlink\n");
891 repbody->mbo_eadatasize = 0;
893 /* Only check unlinked and archived if RAoLU and upon last close */
894 if (!cdt->cdt_remove_archive_on_last_unlink ||
895 atomic_read(&mo->mot_open_count) != 0)
898 /* mdt_attr_get_complex will clear ma_valid, so check here first */
899 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
902 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
905 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
907 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
908 * try setting them */
910 rc = mdt_attr_get_complex(info, mo, ma);
912 CERROR("%s: unable to fetch missing attributes of"
913 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
914 PFID(mdt_object_fid(mo)), rc);
918 if (need & MA_INODE) {
919 if (ma->ma_valid & MA_INODE) {
920 if (ma->ma_attr.la_nlink != 0)
928 if (ma->ma_valid & MA_HSM) {
929 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
937 /* RAoLU policy is active, last close on file has occured,
938 * file is unlinked, file is archived, so create remove request
940 * If CDT is not running, requests will be logged for later. */
941 if (ma->ma_hsm.mh_arch_id != 0)
942 archive_id = ma->ma_hsm.mh_arch_id;
944 archive_id = cdt->cdt_default_archive_id;
946 hai.hai_fid = *mdt_object_fid(mo);
948 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
951 CERROR("%s: unable to add HSM remove request for "DFID
952 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
953 PFID(mdt_object_fid(mo)), rc);
958 static __u64 mdt_attr_valid_xlate(enum mds_attr_flags in,
959 struct mdt_reint_record *rr,
965 if (in & MDS_ATTR_MODE)
967 if (in & MDS_ATTR_UID)
969 if (in & MDS_ATTR_GID)
971 if (in & MDS_ATTR_SIZE)
973 if (in & MDS_ATTR_BLOCKS)
975 if (in & MDS_ATTR_ATIME_SET)
977 if (in & MDS_ATTR_CTIME_SET)
979 if (in & MDS_ATTR_MTIME_SET)
981 if (in & MDS_ATTR_ATTR_FLAG)
983 if (in & MDS_ATTR_KILL_SUID)
985 if (in & MDS_ATTR_KILL_SGID)
987 if (in & MDS_ATTR_PROJID)
989 if (in & MDS_ATTR_LSIZE)
991 if (in & MDS_ATTR_LBLOCKS)
994 if (in & MDS_ATTR_FROM_OPEN)
995 rr->rr_flags |= MRF_OPEN_TRUNC;
996 if (in & MDS_ATTR_OVERRIDE)
997 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
998 if (in & MDS_ATTR_FORCE)
999 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1001 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1002 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1003 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1004 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1005 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1006 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1009 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", (u64)in);
1016 int mdt_name_unpack(struct req_capsule *pill,
1017 const struct req_msg_field *field,
1019 enum mdt_name_flags flags)
1021 ln->ln_name = req_capsule_client_get(pill, field);
1022 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1024 if (!lu_name_is_valid(ln)) {
1031 if ((flags & MNF_FIX_ANON) &&
1032 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1033 /* Newer (3.x) kernels use a name of "/" for the
1034 * "anonymous" disconnected dentries from NFS
1035 * filehandle conversion. See d_obtain_alias(). */
1043 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1044 const char **secctx_name,
1045 void **secctx, size_t *secctx_size)
1050 *secctx_name = NULL;
1054 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1055 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1058 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1063 if (name_size > XATTR_NAME_MAX + 1)
1066 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1067 if (strnlen(name, name_size) != name_size - 1)
1070 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1071 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1074 *secctx_name = name;
1075 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1076 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1081 static int mdt_file_encctx_unpack(struct req_capsule *pill,
1082 void **encctx, size_t *encctx_size)
1087 if (!exp_connect_encrypt(pill->rc_req->rq_export))
1090 if (!req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_CLIENT) ||
1091 !req_capsule_field_present(pill, &RMF_FILE_ENCCTX, RCL_CLIENT))
1094 *encctx_size = req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_CLIENT);
1095 if (*encctx_size == 0)
1098 *encctx = req_capsule_client_get(pill, &RMF_FILE_ENCCTX);
1103 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1105 struct lu_ucred *uc = mdt_ucred(info);
1106 struct md_attr *ma = &info->mti_attr;
1107 struct lu_attr *la = &ma->ma_attr;
1108 struct req_capsule *pill = info->mti_pill;
1109 struct mdt_reint_record *rr = &info->mti_rr;
1110 struct mdt_rec_setattr *rec;
1111 struct lu_nodemap *nodemap;
1115 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1116 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1120 /* This prior initialization is needed for old_init_ucred_reint() */
1121 uc->uc_fsuid = rec->sa_fsuid;
1122 uc->uc_fsgid = rec->sa_fsgid;
1123 uc->uc_cap = CAP_EMPTY_SET;
1124 uc->uc_cap.cap[0] = rec->sa_cap;
1125 uc->uc_suppgids[0] = rec->sa_suppgid;
1126 uc->uc_suppgids[1] = -1;
1128 rr->rr_fid1 = &rec->sa_fid;
1129 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1130 la->la_mode = rec->sa_mode;
1131 la->la_flags = rec->sa_attr_flags;
1133 nodemap = nodemap_get_from_exp(info->mti_exp);
1134 if (IS_ERR(nodemap))
1135 RETURN(PTR_ERR(nodemap));
1137 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1138 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1139 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1140 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1141 la->la_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
1142 NODEMAP_CLIENT_TO_FS, rec->sa_projid);
1143 nodemap_putref(nodemap);
1145 la->la_size = rec->sa_size;
1146 la->la_blocks = rec->sa_blocks;
1147 la->la_ctime = rec->sa_ctime;
1148 la->la_atime = rec->sa_atime;
1149 la->la_mtime = rec->sa_mtime;
1150 ma->ma_valid = MA_INODE;
1152 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1153 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1158 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1160 struct req_capsule *pill = info->mti_pill;
1161 struct mdt_ioepoch *ioepoch;
1164 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1165 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1169 if (ioepoch == NULL)
1172 info->mti_open_handle = ioepoch->mio_open_handle;
1177 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1178 struct req_capsule *pill = info->mti_pill;
1180 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1181 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1182 if (info->mti_dlm_req == NULL)
1189 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1191 struct mdt_reint_record *rr = &info->mti_rr;
1192 struct md_attr *ma = &info->mti_attr;
1193 struct req_capsule *pill = info->mti_pill;
1197 rc = mdt_setattr_unpack_rec(info);
1201 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1202 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1203 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1206 if (rr->rr_eadatalen > 0) {
1207 const struct lmv_user_md *lum;
1209 lum = rr->rr_eadata;
1210 /* Sigh ma_valid(from req) does not indicate whether
1211 * it will set LOV/LMV EA, so we have to check magic */
1212 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1213 ma->ma_valid |= MA_LMV;
1214 ma->ma_lmv = (void *)rr->rr_eadata;
1215 ma->ma_lmv_size = rr->rr_eadatalen;
1217 ma->ma_valid |= MA_LOV;
1218 ma->ma_lmm = (void *)rr->rr_eadata;
1219 ma->ma_lmm_size = rr->rr_eadatalen;
1224 rc = mdt_dlmreq_unpack(info);
1228 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1230 struct md_attr *ma = &info->mti_attr;
1231 struct req_capsule *pill = info->mti_pill;
1234 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1237 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1239 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1240 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1246 int mdt_close_unpack(struct mdt_thread_info *info)
1251 rc = mdt_close_handle_unpack(info);
1255 rc = mdt_setattr_unpack_rec(info);
1259 rc = mdt_close_intent_unpack(info);
1263 RETURN(mdt_init_ucred_reint(info));
1266 static int mdt_create_unpack(struct mdt_thread_info *info)
1268 struct lu_ucred *uc = mdt_ucred(info);
1269 struct mdt_rec_create *rec;
1270 struct lu_attr *attr = &info->mti_attr.ma_attr;
1271 struct mdt_reint_record *rr = &info->mti_rr;
1272 struct req_capsule *pill = info->mti_pill;
1273 struct md_op_spec *sp = &info->mti_spec;
1278 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1279 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1283 /* This prior initialization is needed for old_init_ucred_reint() */
1284 uc->uc_fsuid = rec->cr_fsuid;
1285 uc->uc_fsgid = rec->cr_fsgid;
1286 uc->uc_cap = CAP_EMPTY_SET;
1287 uc->uc_cap.cap[0] = rec->cr_cap;
1288 uc->uc_suppgids[0] = rec->cr_suppgid1;
1289 uc->uc_suppgids[1] = -1;
1290 uc->uc_umask = rec->cr_umask;
1292 rr->rr_fid1 = &rec->cr_fid1;
1293 rr->rr_fid2 = &rec->cr_fid2;
1294 attr->la_mode = rec->cr_mode;
1295 attr->la_rdev = rec->cr_rdev;
1296 attr->la_uid = rec->cr_fsuid;
1297 attr->la_gid = rec->cr_fsgid;
1298 attr->la_ctime = rec->cr_time;
1299 attr->la_mtime = rec->cr_time;
1300 attr->la_atime = rec->cr_time;
1301 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1302 LA_CTIME | LA_MTIME | LA_ATIME;
1303 memset(&sp->u, 0, sizeof(sp->u));
1304 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1306 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1310 if (S_ISLNK(attr->la_mode)) {
1311 const char *tgt = NULL;
1314 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1315 sz = req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT);
1317 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1318 sp->u.sp_symname.ln_name = tgt;
1319 sp->u.sp_symname.ln_namelen = sz - 1; /* skip NUL */
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 = CAP_EMPTY_SET;
1375 uc->uc_cap.cap[0] = 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 = CAP_EMPTY_SET;
1420 uc->uc_cap.cap[0] = rec->ul_cap;
1421 uc->uc_suppgids[0] = rec->ul_suppgid1;
1422 uc->uc_suppgids[1] = -1;
1424 attr->la_uid = rec->ul_fsuid;
1425 attr->la_gid = rec->ul_fsgid;
1426 rr->rr_fid1 = &rec->ul_fid1;
1427 rr->rr_fid2 = &rec->ul_fid2;
1428 attr->la_ctime = rec->ul_time;
1429 attr->la_mtime = rec->ul_time;
1430 attr->la_mode = rec->ul_mode;
1431 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1432 if (rec->ul_bias & MDS_FID_OP)
1433 info->mti_spec.sp_cr_flags |= MDS_OP_WITH_FID;
1435 info->mti_spec.sp_cr_flags &= ~MDS_OP_WITH_FID;
1437 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1441 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1443 rc = req_check_sepol(pill);
1447 rc = mdt_dlmreq_unpack(info);
1451 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1453 info->mti_spec.sp_rm_entry = 1;
1454 return mdt_unlink_unpack(info);
1457 static int mdt_rename_unpack(struct mdt_thread_info *info)
1459 struct lu_ucred *uc = mdt_ucred(info);
1460 struct mdt_rec_rename *rec;
1461 struct lu_attr *attr = &info->mti_attr.ma_attr;
1462 struct mdt_reint_record *rr = &info->mti_rr;
1463 struct req_capsule *pill = info->mti_pill;
1464 struct md_op_spec *spec = &info->mti_spec;
1469 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1470 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1474 /* This prior initialization is needed for old_init_ucred_reint() */
1475 uc->uc_fsuid = rec->rn_fsuid;
1476 uc->uc_fsgid = rec->rn_fsgid;
1477 uc->uc_cap = CAP_EMPTY_SET;
1478 uc->uc_cap.cap[0] = rec->rn_cap;
1479 uc->uc_suppgids[0] = rec->rn_suppgid1;
1480 uc->uc_suppgids[1] = rec->rn_suppgid2;
1482 attr->la_uid = rec->rn_fsuid;
1483 attr->la_gid = rec->rn_fsgid;
1484 rr->rr_fid1 = &rec->rn_fid1;
1485 rr->rr_fid2 = &rec->rn_fid2;
1486 attr->la_ctime = rec->rn_time;
1487 attr->la_mtime = rec->rn_time;
1488 /* rename_tgt contains the mode already */
1489 attr->la_mode = rec->rn_mode;
1490 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1492 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1496 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1500 spec->no_create = !!req_is_replay(mdt_info_req(info));
1502 rc = req_check_sepol(pill);
1506 rc = mdt_dlmreq_unpack(info);
1511 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1513 struct lu_ucred *uc = mdt_ucred(info);
1514 struct mdt_rec_rename *rec;
1515 struct lu_attr *attr = &info->mti_attr.ma_attr;
1516 struct mdt_reint_record *rr = &info->mti_rr;
1517 struct req_capsule *pill = info->mti_pill;
1518 struct md_op_spec *spec = &info->mti_spec;
1523 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1524 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1528 /* This prior initialization is needed for old_init_ucred_reint() */
1529 uc->uc_fsuid = rec->rn_fsuid;
1530 uc->uc_fsgid = rec->rn_fsgid;
1531 uc->uc_cap = CAP_EMPTY_SET;
1532 uc->uc_cap.cap[0] = rec->rn_cap;
1533 uc->uc_suppgids[0] = rec->rn_suppgid1;
1534 uc->uc_suppgids[1] = rec->rn_suppgid2;
1536 attr->la_uid = rec->rn_fsuid;
1537 attr->la_gid = rec->rn_fsgid;
1538 rr->rr_fid1 = &rec->rn_fid1;
1539 rr->rr_fid2 = &rec->rn_fid2;
1540 attr->la_ctime = rec->rn_time;
1541 attr->la_mtime = rec->rn_time;
1542 /* rename_tgt contains the mode already */
1543 attr->la_mode = rec->rn_mode;
1544 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1545 spec->sp_cr_flags = 0;
1547 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1551 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1552 rc = mdt_close_handle_unpack(info);
1556 spec->sp_migrate_close = 1;
1558 spec->sp_migrate_close = 0;
1560 spec->sp_migrate_nsonly = 0;
1562 /* lustre version > 2.11 migration packs lum */
1563 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1564 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1565 rr->rr_eadatalen = req_capsule_get_size(pill,
1569 if (rr->rr_eadatalen > 0) {
1570 struct lmv_user_md_v1 *lmu;
1572 lmu = req_capsule_client_get(pill, &RMF_EADATA);
1573 lmu->lum_hash_type |=
1574 cpu_to_le32(LMV_HASH_FLAG_FIXED);
1575 rr->rr_eadata = lmu;
1576 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1577 spec->u.sp_ea.eadata = rr->rr_eadata;
1578 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1581 /* old client doesn't provide lum. */
1582 RETURN(-EOPNOTSUPP);
1586 spec->no_create = !!req_is_replay(mdt_info_req(info));
1588 rc = mdt_dlmreq_unpack(info);
1594 * please see comment above LOV_MAGIC_V1_DEFINED
1596 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1598 struct lov_user_md_v1 *v1 = eadata;
1602 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1603 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1604 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1605 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1606 __swab32(LOV_MAGIC_MAGIC))
1607 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1611 static int mdt_open_unpack(struct mdt_thread_info *info)
1613 struct lu_ucred *uc = mdt_ucred(info);
1614 struct mdt_rec_create *rec;
1615 struct lu_attr *attr = &info->mti_attr.ma_attr;
1616 struct req_capsule *pill = info->mti_pill;
1617 struct mdt_reint_record *rr = &info->mti_rr;
1618 struct ptlrpc_request *req = mdt_info_req(info);
1619 struct md_op_spec *sp = &info->mti_spec;
1623 BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1624 sizeof(struct mdt_rec_reint));
1625 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1629 /* This prior initialization is needed for old_init_ucred_reint() */
1630 uc->uc_fsuid = rec->cr_fsuid;
1631 uc->uc_fsgid = rec->cr_fsgid;
1632 uc->uc_cap = CAP_EMPTY_SET;
1633 uc->uc_cap.cap[0] = rec->cr_cap;
1634 uc->uc_suppgids[0] = rec->cr_suppgid1;
1635 uc->uc_suppgids[1] = rec->cr_suppgid2;
1636 uc->uc_umask = rec->cr_umask;
1638 rr->rr_fid1 = &rec->cr_fid1;
1639 rr->rr_fid2 = &rec->cr_fid2;
1640 rr->rr_open_handle = &rec->cr_open_handle_old;
1641 attr->la_mode = rec->cr_mode;
1642 attr->la_rdev = rec->cr_rdev;
1643 attr->la_uid = rec->cr_fsuid;
1644 attr->la_gid = rec->cr_fsgid;
1645 attr->la_ctime = rec->cr_time;
1646 attr->la_mtime = rec->cr_time;
1647 attr->la_atime = rec->cr_time;
1648 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1649 LA_CTIME | LA_MTIME | LA_ATIME;
1650 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1651 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1652 /* Do not trigger ASSERTION if client miss to set such flags. */
1653 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1656 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1658 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1660 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1661 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1664 if (rr->rr_eadatalen > 0) {
1665 rr->rr_eadata = req_capsule_client_get(pill,
1667 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1668 sp->u.sp_ea.eadata = rr->rr_eadata;
1669 sp->sp_archive_id = rec->cr_archive_id;
1670 sp->no_create = !!req_is_replay(req);
1671 mdt_fix_lov_magic(info, rr->rr_eadata);
1675 * Client default md_size may be 0 right after client start,
1676 * until all osc are connected, set here just some reasonable
1677 * value to prevent misbehavior.
1679 if (rr->rr_eadatalen == 0 &&
1680 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1681 rr->rr_eadatalen = MIN_MD_SIZE;
1684 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1685 &sp->sp_cr_file_secctx,
1686 &sp->sp_cr_file_secctx_size);
1690 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1691 &sp->sp_cr_file_encctx_size);
1695 rc = req_check_sepol(pill);
1702 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1704 struct mdt_reint_record *rr = &info->mti_rr;
1705 struct lu_ucred *uc = mdt_ucred(info);
1706 struct lu_attr *attr = &info->mti_attr.ma_attr;
1707 struct req_capsule *pill = info->mti_pill;
1708 struct mdt_rec_setxattr *rec;
1713 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1714 sizeof(struct mdt_rec_reint));
1715 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1719 /* This prior initialization is needed for old_init_ucred_reint() */
1720 uc->uc_fsuid = rec->sx_fsuid;
1721 uc->uc_fsgid = rec->sx_fsgid;
1722 uc->uc_cap = CAP_EMPTY_SET;
1723 uc->uc_cap.cap[0] = rec->sx_cap;
1724 uc->uc_suppgids[0] = rec->sx_suppgid1;
1725 uc->uc_suppgids[1] = -1;
1727 rr->rr_opcode = rec->sx_opcode;
1728 rr->rr_fid1 = &rec->sx_fid;
1729 attr->la_valid = rec->sx_valid;
1730 attr->la_ctime = rec->sx_time;
1731 attr->la_size = rec->sx_size;
1732 attr->la_flags = rec->sx_flags;
1734 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1738 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1739 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1742 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1745 if (rr->rr_eadatalen > 0) {
1746 rr->rr_eadata = req_capsule_client_get(pill,
1748 if (rr->rr_eadata == NULL)
1751 rr->rr_eadata = NULL;
1753 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1754 CDEBUG(D_INFO, "no xattr data supplied\n");
1758 rc = req_check_sepol(pill);
1762 if (mdt_dlmreq_unpack(info) < 0)
1768 static int mdt_resync_unpack(struct mdt_thread_info *info)
1770 struct req_capsule *pill = info->mti_pill;
1771 struct mdt_reint_record *rr = &info->mti_rr;
1772 struct lu_ucred *uc = mdt_ucred(info);
1773 struct mdt_rec_resync *rec;
1776 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1777 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1781 /* This prior initialization is needed for old_init_ucred_reint() */
1782 uc->uc_fsuid = rec->rs_fsuid;
1783 uc->uc_fsgid = rec->rs_fsgid;
1784 uc->uc_cap = CAP_EMPTY_SET;
1785 uc->uc_cap.cap[0] = rec->rs_cap;
1787 rr->rr_fid1 = &rec->rs_fid;
1788 rr->rr_mirror_id = rec->rs_mirror_id;
1790 /* cookie doesn't need to be swapped but it has been swapped
1791 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1793 if (req_capsule_req_need_swab(pill))
1794 __swab64s(&rec->rs_lease_handle.cookie);
1795 rr->rr_lease_handle = &rec->rs_lease_handle;
1797 RETURN(mdt_dlmreq_unpack(info));
1800 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1802 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1803 [REINT_SETATTR] = mdt_setattr_unpack,
1804 [REINT_CREATE] = mdt_create_unpack,
1805 [REINT_LINK] = mdt_link_unpack,
1806 [REINT_UNLINK] = mdt_unlink_unpack,
1807 [REINT_RENAME] = mdt_rename_unpack,
1808 [REINT_OPEN] = mdt_open_unpack,
1809 [REINT_SETXATTR] = mdt_setxattr_unpack,
1810 [REINT_RMENTRY] = mdt_rmentry_unpack,
1811 [REINT_MIGRATE] = mdt_migrate_unpack,
1812 [REINT_RESYNC] = mdt_resync_unpack,
1815 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1820 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1821 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1822 info->mti_rr.rr_opcode = op;
1823 rc = mdt_reint_unpackers[op](info);
1825 CERROR("Unexpected opcode %d\n", op);
1831 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1832 struct mdt_object *child)
1835 struct lu_buf *buffer;
1836 struct mdt_body *repbody;
1837 struct req_capsule *pill = info->mti_pill;
1840 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1841 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1843 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1844 buffer = &info->mti_buf;
1846 /* fill reply buffer with security context now */
1847 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1849 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1851 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1852 buffer, secctx_name);
1855 "found security context of size %d for "DFID"\n",
1856 rc, PFID(mdt_object_fid(child)));
1858 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1859 repbody->mbo_valid |= OBD_MD_SECCTX;
1860 if (rc < buffer->lb_len)
1861 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1866 "security context not found for "DFID": rc = %d\n",
1867 PFID(mdt_object_fid(child)), rc);
1868 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1870 /* handling -ENOENT is important because it may change
1871 * object state in DNE env dropping LOHA_EXISTS flag,
1872 * it is important to return that to the caller.
1873 * Check LU-13115 for details.
1882 /* check whether two FIDs belong to different MDT. */
1883 static int mdt_fids_different_target(struct mdt_thread_info *info,
1884 const struct lu_fid *fid1,
1885 const struct lu_fid *fid2)
1887 const struct lu_env *env = info->mti_env;
1888 struct mdt_device *mdt = info->mti_mdt;
1889 struct lu_seq_range *range = &info->mti_range;
1890 struct seq_server_site *ss;
1891 __u32 index1, index2;
1894 if (fid_seq(fid1) == fid_seq(fid2))
1897 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1899 range->lsr_flags = LU_SEQ_RANGE_MDT;
1900 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1904 index1 = range->lsr_index;
1906 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1910 index2 = range->lsr_index;
1912 return index1 != index2;
1916 * Check whether \a child is remote object on \a parent.
1918 * \param[in] info thread environment
1919 * \param[in] parent parent object, it's the same as child object in
1921 * \param[in] child child object
1923 * \retval 1 is remote object.
1924 * \retval 0 isn't remote object.
1925 * \retval < 1 error code
1927 int mdt_is_remote_object(struct mdt_thread_info *info,
1928 struct mdt_object *parent,
1929 struct mdt_object *child)
1931 struct lu_buf *buf = &info->mti_big_buf;
1932 struct linkea_data ldata = { NULL };
1933 struct link_ea_header *leh;
1934 struct link_ea_entry *lee;
1935 struct lu_name name;
1943 if (fid_is_root(mdt_object_fid(child)))
1946 if (likely(parent != child)) {
1947 if (mdt_object_remote(parent) ^ mdt_object_remote(child))
1950 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1953 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1954 mdt_object_fid(child));
1958 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1959 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1964 rc = mdt_links_read(info, child, &ldata);
1965 /* can't read linkea, just assume it's remote object */
1966 if (rc == -ENOENT || rc == -ENODATA)
1972 lee = (struct link_ea_entry *)(leh + 1);
1973 for (i = 0; i < leh->leh_reccount; i++) {
1974 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1975 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1976 if (mdt_fids_different_target(info, &pfid,
1977 mdt_object_fid(child)))
1984 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
1985 struct mdt_object *child)
1987 struct lu_buf *buffer;
1988 struct mdt_body *repbody;
1989 struct req_capsule *pill = info->mti_pill;
1990 struct obd_export *exp = mdt_info_req(info)->rq_export;
1993 if (!exp_connect_encrypt(exp))
1996 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
1997 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0) {
1998 struct lu_attr la = { 0 };
1999 struct dt_object *dt = mdt_obj2dt(child);
2001 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
2002 dt_attr_get(info->mti_env, mdt_obj2dt(child), &la);
2004 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL) {
2005 buffer = &info->mti_buf;
2007 /* fill reply buffer with encryption context now */
2009 req_capsule_get_size(pill, &RMF_FILE_ENCCTX,
2012 req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
2013 rc = mo_xattr_get(info->mti_env,
2014 mdt_object_child(child),
2016 LL_XATTR_NAME_ENCRYPTION_CONTEXT);
2019 "found encryption ctx of size %d for "DFID"\n",
2020 rc, PFID(mdt_object_fid(child)));
2022 repbody = req_capsule_server_get(pill,
2024 repbody->mbo_valid |= OBD_MD_ENCCTX;
2025 if (rc < buffer->lb_len)
2026 req_capsule_shrink(pill,
2027 &RMF_FILE_ENCCTX, rc,
2032 "encryption ctx not found for "DFID": rc = %d\n",
2033 PFID(mdt_object_fid(child)), rc);
2034 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2036 /* handling -ENOENT is important because it may
2037 * change object state in DNE env dropping
2038 * LOHA_EXISTS flag, it is important to return
2039 * that to the caller.
2040 * Check LU-13115 for details.
2046 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,