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 = lnet_nid_to_nid4(
146 &info->mti_exp->exp_connection->c_peer.nid);
148 uc->uc_nid = LNET_NID_ANY;
151 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
154 struct lu_nodemap *nodemap = NULL;
157 if (info && info->mti_exp) {
158 nodemap = nodemap_get_from_exp(info->mti_exp);
159 if (nodemap && !IS_ERR(nodemap)) {
160 audit = nodemap->nmf_enable_audit;
161 nodemap_putref(nodemap);
165 uc->uc_enable_audit = audit;
168 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
171 struct ptlrpc_request *req = mdt_info_req(info);
172 struct mdt_device *mdt = info->mti_mdt;
173 struct ptlrpc_user_desc *pud = req->rq_user_desc;
174 struct lu_ucred *ucred = mdt_ucred(info);
175 struct lu_nodemap *nodemap;
176 lnet_nid_t peernid = req->rq_peer.nid;
180 bool is_nm_gid_squashed = false;
185 LASSERT(req->rq_auth_gss);
186 LASSERT(!req->rq_auth_usr_mdt);
187 LASSERT(req->rq_user_desc);
188 LASSERT(ucred != NULL);
190 ucred->uc_valid = UCRED_INVALID;
192 nodemap = nodemap_get_from_exp(info->mti_exp);
194 RETURN(PTR_ERR(nodemap));
196 pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
197 NODEMAP_CLIENT_TO_FS, pud->pud_uid);
198 pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
199 NODEMAP_CLIENT_TO_FS, pud->pud_gid);
200 pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
201 NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
202 pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
203 NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
205 ucred->uc_o_uid = pud->pud_uid;
206 ucred->uc_o_gid = pud->pud_gid;
207 ucred->uc_o_fsuid = pud->pud_fsuid;
208 ucred->uc_o_fsgid = pud->pud_fsgid;
210 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
211 /* deny access before we get identity ref */
212 if (nodemap->nmf_deny_unknown) {
213 nodemap_putref(nodemap);
217 ucred->uc_suppgids[0] = -1;
218 ucred->uc_suppgids[1] = -1;
221 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
222 is_nm_gid_squashed = true;
224 nodemap_putref(nodemap);
226 if (type == BODY_INIT) {
227 struct mdt_body *body = (struct mdt_body *)buf;
229 ucred->uc_suppgids[0] = body->mbo_suppgid;
230 ucred->uc_suppgids[1] = -1;
233 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
234 req->rq_auth_uid != pud->pud_uid) {
235 CDEBUG(D_SEC, "local client %s: auth uid %u "
236 "while client claims %u:%u/%u:%u\n",
237 libcfs_nid2str(peernid), req->rq_auth_uid,
238 pud->pud_uid, pud->pud_gid,
239 pud->pud_fsuid, pud->pud_fsgid);
243 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
244 ucred->uc_identity = NULL;
245 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
247 struct md_identity *identity;
249 identity = mdt_identity_get(mdt->mdt_identity_cache,
251 if (IS_ERR(identity)) {
252 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
253 ucred->uc_identity = NULL;
254 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
258 "Deny access without identity: uid %u\n",
263 ucred->uc_identity = identity;
264 perm = mdt_identity_get_perm(ucred->uc_identity,
269 /* find out the setuid/setgid attempt */
270 setuid = (pud->pud_uid != pud->pud_fsuid);
271 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
272 (ucred->uc_identity &&
273 (pud->pud_gid != ucred->uc_identity->mi_gid)));
275 /* check permission of setuid */
276 if (setuid && !(perm & CFS_SETUID_PERM)) {
277 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
278 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
279 GOTO(out, rc = -EACCES);
282 /* check permission of setgid */
283 if (setgid && !(perm & CFS_SETGID_PERM)) {
284 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
285 "from %s\n", pud->pud_uid, pud->pud_gid,
286 pud->pud_fsuid, pud->pud_fsgid,
287 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
288 GOTO(out, rc = -EACCES);
291 if (perm & CFS_SETGRP_PERM) {
292 /* only set groups if GID is not squashed */
293 if (pud->pud_ngroups && !is_nm_gid_squashed) {
294 /* setgroups for local client */
295 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
296 if (!ucred->uc_ginfo) {
297 CERROR("failed to alloc %d groups\n",
299 GOTO(out, rc = -ENOMEM);
302 lustre_groups_from_list(ucred->uc_ginfo,
304 lustre_groups_sort(ucred->uc_ginfo);
306 ucred->uc_suppgids[0] = -1;
307 ucred->uc_suppgids[1] = -1;
308 ucred->uc_ginfo = NULL;
311 ucred->uc_suppgids[0] = -1;
312 ucred->uc_suppgids[1] = -1;
313 ucred->uc_ginfo = NULL;
316 ucred->uc_uid = pud->pud_uid;
317 ucred->uc_gid = pud->pud_gid;
319 ucred->uc_cap = CAP_EMPTY_SET;
320 if (!nodemap || ucred->uc_o_uid != nodemap->nm_squash_uid)
321 ucred->uc_cap.cap[0] = 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)
473 uc->uc_cap = CAP_EMPTY_SET;
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 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
483 cap_raised(uc->uc_cap,
484 CAP_DAC_READ_SEARCH))) {
487 CDEBUG(D_SEC, "Deny access without identity: "
488 "uid %u\n", uc->uc_fsuid);
493 uc->uc_identity = identity;
495 /* process root_squash here. */
496 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
498 uc->uc_valid = UCRED_OLD;
499 ucred_set_jobid(info, uc);
500 ucred_set_nid(info, uc);
501 ucred_set_audit_enabled(info, uc);
508 static int old_init_ucred(struct mdt_thread_info *info,
509 struct mdt_body *body)
511 struct lu_ucred *uc = mdt_ucred(info);
512 struct lu_nodemap *nodemap;
516 nodemap = nodemap_get_from_exp(info->mti_exp);
518 RETURN(PTR_ERR(nodemap));
520 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
521 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
522 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
523 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
524 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
525 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
526 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
527 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
530 uc->uc_valid = UCRED_INVALID;
531 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
532 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
533 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
534 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
535 uc->uc_suppgids[0] = body->mbo_suppgid;
536 uc->uc_suppgids[1] = -1;
538 uc->uc_cap = CAP_EMPTY_SET;
539 uc->uc_cap.cap[0] = body->mbo_capability;
541 rc = old_init_ucred_common(info, nodemap);
542 nodemap_putref(nodemap);
547 static int old_init_ucred_reint(struct mdt_thread_info *info)
549 struct lu_ucred *uc = mdt_ucred(info);
550 struct lu_nodemap *nodemap;
554 nodemap = nodemap_get_from_exp(info->mti_exp);
556 RETURN(PTR_ERR(nodemap));
560 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
561 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
562 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
563 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
565 uc->uc_valid = UCRED_INVALID;
566 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
567 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
570 rc = old_init_ucred_common(info, nodemap);
571 nodemap_putref(nodemap);
576 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
577 struct mdt_body *body)
579 struct ptlrpc_request *req = mdt_info_req(info);
580 struct lu_ucred *uc = mdt_ucred(info);
583 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
586 mdt_exit_ucred(info);
588 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
589 return old_init_ucred(info, body);
591 return new_init_ucred(info, BODY_INIT, body);
594 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
596 return __mdt_init_ucred(info, body);
599 int mdt_init_ucred_reint(struct mdt_thread_info *info)
601 struct ptlrpc_request *req = mdt_info_req(info);
602 struct lu_ucred *uc = mdt_ucred(info);
603 struct md_attr *ma = &info->mti_attr;
606 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
609 /* LU-5564: for normal close request, skip permission check */
610 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
611 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP))) {
612 cap_raise_nfsd_set(uc->uc_cap, CAP_FULL_SET);
613 cap_raise_fs_set(uc->uc_cap, CAP_FULL_SET);
616 mdt_exit_ucred(info);
618 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
619 return old_init_ucred_reint(info);
621 return new_init_ucred(info, REC_INIT, NULL);
624 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
625 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
627 const struct lov_ost_data_v1 *lod;
628 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
632 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
635 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
636 POSTID(&lmm->lmm_oi), lmm_magic,
637 le32_to_cpu(lmm->lmm_pattern));
639 /* No support for compount layouts yet */
640 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
643 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
644 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
645 le32_to_cpu(lmm->lmm_stripe_size), count);
647 /* If it's a directory or a released file, then there are
648 * no actual objects to print, so bail out. */
649 if (valid & OBD_MD_FLDIREA ||
650 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
653 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
654 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
657 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
658 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
659 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
663 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
665 const struct lmv_mds_md_v1 *lmm1;
666 const struct lmv_foreign_md *lfm;
669 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
672 /* foreign LMV case */
673 lfm = &lmv->lmv_foreign_md;
674 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
676 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
677 le32_to_cpu(lfm->lfm_magic),
678 le32_to_cpu(lfm->lfm_length),
679 le32_to_cpu(lfm->lfm_type),
680 le32_to_cpu(lfm->lfm_flags),
681 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
685 lmm1 = &lmv->lmv_md_v1;
687 "magic 0x%08X, master %#X stripe_count %d hash_type %#x\n",
688 le32_to_cpu(lmm1->lmv_magic),
689 le32_to_cpu(lmm1->lmv_master_mdt_index),
690 le32_to_cpu(lmm1->lmv_stripe_count),
691 le32_to_cpu(lmm1->lmv_hash_type));
693 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
696 if (le32_to_cpu(lmm1->lmv_stripe_count) > LMV_MAX_STRIPE_COUNT)
699 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
702 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
703 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
707 /* Shrink and/or grow reply buffers */
708 int mdt_fix_reply(struct mdt_thread_info *info)
710 struct req_capsule *pill = info->mti_pill;
711 struct mdt_body *body;
712 int md_size, md_packed = 0;
717 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
718 LASSERT(body != NULL);
720 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
722 md_size = body->mbo_eadatasize;
726 acl_size = body->mbo_aclsize;
728 /* this replay - not send info to client */
729 if (info->mti_spec.no_create) {
734 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
739 &RMF_ACL, or &RMF_LOGCOOKIES
740 (optional) &RMF_CAPA1,
741 (optional) &RMF_CAPA2,
742 (optional) something else
745 /* MDT_MD buffer may be bigger than packed value, let's shrink all
746 * buffers before growing it */
747 if (info->mti_big_lmm_used) {
748 /* big_lmm buffer may be used even without packing the result
749 * into reply, just for internal server needs */
750 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
751 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
754 /* free big lmm if md_size is not needed */
755 if (md_size == 0 || md_packed == 0) {
756 info->mti_big_lmm_used = 0;
758 /* buffer must be allocated separately */
759 LASSERT(info->mti_attr.ma_lmm !=
760 req_capsule_server_get(pill, &RMF_MDT_MD));
761 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
763 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
764 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
767 if (info->mti_big_acl_used) {
769 info->mti_big_acl_used = 0;
771 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
772 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
773 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
774 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
775 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
778 /* Shrink optional SECCTX buffer if it is not used */
779 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
780 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
781 !(body->mbo_valid & OBD_MD_SECCTX))
782 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
784 /* Shrink optional ENCCTX buffer if it is not used */
785 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
786 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0 &&
787 !(body->mbo_valid & OBD_MD_ENCCTX))
788 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0, RCL_SERVER);
791 * Some more field should be shrinked if needed.
792 * This should be done by those who added fields to reply message.
795 /* Grow MD buffer if needed finally */
796 if (info->mti_big_lmm_used) {
799 LASSERT(md_size > md_packed);
800 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
801 md_size - md_packed);
802 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
804 /* we can't answer with proper LOV EA, drop flags,
805 * the rc is also returned so this request is
806 * considered as failed */
807 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
808 /* don't return transno along with error */
809 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
811 /* now we need to pack right LOV/LMV EA */
812 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
813 if (info->mti_attr.ma_valid & MA_LOV) {
814 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
816 info->mti_attr.ma_lmm_size);
817 memcpy(lmm, info->mti_attr.ma_lmm,
818 info->mti_attr.ma_lmm_size);
819 } else if (info->mti_attr.ma_valid & MA_LMV) {
820 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
822 info->mti_attr.ma_lmv_size);
823 memcpy(lmm, info->mti_attr.ma_lmv,
824 info->mti_attr.ma_lmv_size);
828 /* update mdt_max_mdsize so clients will be aware about that */
829 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
830 info->mti_mdt->mdt_max_mdsize =
831 info->mti_attr.ma_lmm_size;
832 info->mti_big_lmm_used = 0;
835 if (info->mti_big_acl_used) {
836 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
839 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
841 body->mbo_valid &= ~OBD_MD_FLACL;
843 void *acl = req_capsule_server_get(pill, &RMF_ACL);
845 memcpy(acl, info->mti_big_acl, acl_size);
848 info->mti_big_acl_used = 0;
855 /* if object is dying, pack the lov/llog data,
856 * parameter info->mti_attr should be valid at this point!
857 * Also implements RAoLU policy */
858 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
861 struct mdt_body *repbody = NULL;
862 const struct lu_attr *la = &ma->ma_attr;
863 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
866 struct hsm_action_item hai = {
867 .hai_len = sizeof(hai),
868 .hai_action = HSMA_REMOVE,
869 .hai_extent.length = -1,
877 if (mdt_info_req(info) != NULL) {
878 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
879 LASSERT(repbody != NULL);
881 CDEBUG(D_INFO, "not running in a request/reply context\n");
884 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
885 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
887 if (ma->ma_valid & MA_LOV) {
888 CERROR("No need in LOV EA upon unlink\n");
892 repbody->mbo_eadatasize = 0;
894 /* Only check unlinked and archived if RAoLU and upon last close */
895 if (!cdt->cdt_remove_archive_on_last_unlink ||
896 atomic_read(&mo->mot_open_count) != 0)
899 /* mdt_attr_get_complex will clear ma_valid, so check here first */
900 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
903 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
906 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
908 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
909 * try setting them */
911 rc = mdt_attr_get_complex(info, mo, ma);
913 CERROR("%s: unable to fetch missing attributes of"
914 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
915 PFID(mdt_object_fid(mo)), rc);
919 if (need & MA_INODE) {
920 if (ma->ma_valid & MA_INODE) {
921 if (ma->ma_attr.la_nlink != 0)
929 if (ma->ma_valid & MA_HSM) {
930 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
938 /* RAoLU policy is active, last close on file has occured,
939 * file is unlinked, file is archived, so create remove request
941 * If CDT is not running, requests will be logged for later. */
942 if (ma->ma_hsm.mh_arch_id != 0)
943 archive_id = ma->ma_hsm.mh_arch_id;
945 archive_id = cdt->cdt_default_archive_id;
947 hai.hai_fid = *mdt_object_fid(mo);
949 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
952 CERROR("%s: unable to add HSM remove request for "DFID
953 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
954 PFID(mdt_object_fid(mo)), rc);
959 static __u64 mdt_attr_valid_xlate(enum mds_attr_flags in,
960 struct mdt_reint_record *rr,
966 if (in & MDS_ATTR_MODE)
968 if (in & MDS_ATTR_UID)
970 if (in & MDS_ATTR_GID)
972 if (in & MDS_ATTR_SIZE)
974 if (in & MDS_ATTR_BLOCKS)
976 if (in & MDS_ATTR_ATIME_SET)
978 if (in & MDS_ATTR_CTIME_SET)
980 if (in & MDS_ATTR_MTIME_SET)
982 if (in & MDS_ATTR_ATTR_FLAG)
984 if (in & MDS_ATTR_KILL_SUID)
986 if (in & MDS_ATTR_KILL_SGID)
988 if (in & MDS_ATTR_PROJID)
990 if (in & MDS_ATTR_LSIZE)
992 if (in & MDS_ATTR_LBLOCKS)
995 if (in & MDS_ATTR_FROM_OPEN)
996 rr->rr_flags |= MRF_OPEN_TRUNC;
997 if (in & MDS_ATTR_OVERRIDE)
998 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
999 if (in & MDS_ATTR_FORCE)
1000 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1002 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1003 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1004 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1005 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1006 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1007 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1010 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", (u64)in);
1017 int mdt_name_unpack(struct req_capsule *pill,
1018 const struct req_msg_field *field,
1020 enum mdt_name_flags flags)
1022 ln->ln_name = req_capsule_client_get(pill, field);
1023 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1025 if (!lu_name_is_valid(ln)) {
1032 if ((flags & MNF_FIX_ANON) &&
1033 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1034 /* Newer (3.x) kernels use a name of "/" for the
1035 * "anonymous" disconnected dentries from NFS
1036 * filehandle conversion. See d_obtain_alias(). */
1044 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1045 const char **secctx_name,
1046 void **secctx, size_t *secctx_size)
1051 *secctx_name = NULL;
1055 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1056 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1059 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1064 if (name_size > XATTR_NAME_MAX + 1)
1067 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1068 if (strnlen(name, name_size) != name_size - 1)
1071 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1072 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1075 *secctx_name = name;
1076 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1077 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1082 static int mdt_file_encctx_unpack(struct req_capsule *pill,
1083 void **encctx, size_t *encctx_size)
1088 if (!exp_connect_encrypt(pill->rc_req->rq_export))
1091 if (!req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_CLIENT) ||
1092 !req_capsule_field_present(pill, &RMF_FILE_ENCCTX, RCL_CLIENT))
1095 *encctx_size = req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_CLIENT);
1096 if (*encctx_size == 0)
1099 *encctx = req_capsule_client_get(pill, &RMF_FILE_ENCCTX);
1104 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1106 struct lu_ucred *uc = mdt_ucred(info);
1107 struct md_attr *ma = &info->mti_attr;
1108 struct lu_attr *la = &ma->ma_attr;
1109 struct req_capsule *pill = info->mti_pill;
1110 struct mdt_reint_record *rr = &info->mti_rr;
1111 struct mdt_rec_setattr *rec;
1112 struct lu_nodemap *nodemap;
1116 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1117 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1121 /* This prior initialization is needed for old_init_ucred_reint() */
1122 uc->uc_fsuid = rec->sa_fsuid;
1123 uc->uc_fsgid = rec->sa_fsgid;
1124 uc->uc_cap = CAP_EMPTY_SET;
1125 uc->uc_cap.cap[0] = rec->sa_cap;
1126 uc->uc_suppgids[0] = rec->sa_suppgid;
1127 uc->uc_suppgids[1] = -1;
1129 rr->rr_fid1 = &rec->sa_fid;
1130 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1131 la->la_mode = rec->sa_mode;
1132 la->la_flags = rec->sa_attr_flags;
1134 nodemap = nodemap_get_from_exp(info->mti_exp);
1135 if (IS_ERR(nodemap))
1136 RETURN(PTR_ERR(nodemap));
1138 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1139 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1140 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1141 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1142 la->la_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
1143 NODEMAP_CLIENT_TO_FS, rec->sa_projid);
1144 nodemap_putref(nodemap);
1146 la->la_size = rec->sa_size;
1147 la->la_blocks = rec->sa_blocks;
1148 la->la_ctime = rec->sa_ctime;
1149 la->la_atime = rec->sa_atime;
1150 la->la_mtime = rec->sa_mtime;
1151 ma->ma_valid = MA_INODE;
1153 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1154 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1159 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1161 struct req_capsule *pill = info->mti_pill;
1162 struct mdt_ioepoch *ioepoch;
1165 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1166 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1170 if (ioepoch == NULL)
1173 info->mti_open_handle = ioepoch->mio_open_handle;
1178 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1179 struct req_capsule *pill = info->mti_pill;
1181 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1182 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1183 if (info->mti_dlm_req == NULL)
1190 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1192 struct mdt_reint_record *rr = &info->mti_rr;
1193 struct md_attr *ma = &info->mti_attr;
1194 struct req_capsule *pill = info->mti_pill;
1198 rc = mdt_setattr_unpack_rec(info);
1202 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1203 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1204 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1207 if (rr->rr_eadatalen > 0) {
1208 const struct lmv_user_md *lum;
1210 lum = rr->rr_eadata;
1211 /* Sigh ma_valid(from req) does not indicate whether
1212 * it will set LOV/LMV EA, so we have to check magic */
1213 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1214 ma->ma_valid |= MA_LMV;
1215 ma->ma_lmv = (void *)rr->rr_eadata;
1216 ma->ma_lmv_size = rr->rr_eadatalen;
1218 ma->ma_valid |= MA_LOV;
1219 ma->ma_lmm = (void *)rr->rr_eadata;
1220 ma->ma_lmm_size = rr->rr_eadatalen;
1225 rc = mdt_dlmreq_unpack(info);
1229 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1231 struct md_attr *ma = &info->mti_attr;
1232 struct req_capsule *pill = info->mti_pill;
1235 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1238 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1240 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1241 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1247 int mdt_close_unpack(struct mdt_thread_info *info)
1252 rc = mdt_close_handle_unpack(info);
1256 rc = mdt_setattr_unpack_rec(info);
1260 rc = mdt_close_intent_unpack(info);
1264 RETURN(mdt_init_ucred_reint(info));
1267 static int mdt_create_unpack(struct mdt_thread_info *info)
1269 struct lu_ucred *uc = mdt_ucred(info);
1270 struct mdt_rec_create *rec;
1271 struct lu_attr *attr = &info->mti_attr.ma_attr;
1272 struct mdt_reint_record *rr = &info->mti_rr;
1273 struct req_capsule *pill = info->mti_pill;
1274 struct md_op_spec *sp = &info->mti_spec;
1279 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1280 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1284 /* This prior initialization is needed for old_init_ucred_reint() */
1285 uc->uc_fsuid = rec->cr_fsuid;
1286 uc->uc_fsgid = rec->cr_fsgid;
1287 uc->uc_cap = CAP_EMPTY_SET;
1288 uc->uc_cap.cap[0] = rec->cr_cap;
1289 uc->uc_suppgids[0] = rec->cr_suppgid1;
1290 uc->uc_suppgids[1] = -1;
1291 uc->uc_umask = rec->cr_umask;
1293 rr->rr_fid1 = &rec->cr_fid1;
1294 rr->rr_fid2 = &rec->cr_fid2;
1295 attr->la_mode = rec->cr_mode;
1296 attr->la_rdev = rec->cr_rdev;
1297 attr->la_uid = rec->cr_fsuid;
1298 attr->la_gid = rec->cr_fsgid;
1299 attr->la_ctime = rec->cr_time;
1300 attr->la_mtime = rec->cr_time;
1301 attr->la_atime = rec->cr_time;
1302 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1303 LA_CTIME | LA_MTIME | LA_ATIME;
1304 memset(&sp->u, 0, sizeof(sp->u));
1305 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1307 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1311 if (S_ISLNK(attr->la_mode)) {
1312 const char *tgt = NULL;
1315 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1316 sz = req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT);
1318 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1319 sp->u.sp_symname.ln_name = tgt;
1320 sp->u.sp_symname.ln_namelen = sz - 1; /* skip NUL */
1325 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1326 if (S_ISDIR(attr->la_mode) &&
1327 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1328 sp->u.sp_ea.eadata =
1329 req_capsule_client_get(pill, &RMF_EADATA);
1330 sp->u.sp_ea.eadatalen =
1331 req_capsule_get_size(pill, &RMF_EADATA,
1333 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1337 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1338 &sp->sp_cr_file_secctx,
1339 &sp->sp_cr_file_secctx_size);
1343 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1344 &sp->sp_cr_file_encctx_size);
1348 rc = req_check_sepol(pill);
1352 rc = mdt_dlmreq_unpack(info);
1356 static int mdt_link_unpack(struct mdt_thread_info *info)
1358 struct lu_ucred *uc = mdt_ucred(info);
1359 struct mdt_rec_link *rec;
1360 struct lu_attr *attr = &info->mti_attr.ma_attr;
1361 struct mdt_reint_record *rr = &info->mti_rr;
1362 struct req_capsule *pill = info->mti_pill;
1367 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1368 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1372 /* This prior initialization is needed for old_init_ucred_reint() */
1373 uc->uc_fsuid = rec->lk_fsuid;
1374 uc->uc_fsgid = rec->lk_fsgid;
1375 uc->uc_cap = CAP_EMPTY_SET;
1376 uc->uc_cap.cap[0] = rec->lk_cap;
1377 uc->uc_suppgids[0] = rec->lk_suppgid1;
1378 uc->uc_suppgids[1] = rec->lk_suppgid2;
1380 attr->la_uid = rec->lk_fsuid;
1381 attr->la_gid = rec->lk_fsgid;
1382 rr->rr_fid1 = &rec->lk_fid1;
1383 rr->rr_fid2 = &rec->lk_fid2;
1384 attr->la_ctime = rec->lk_time;
1385 attr->la_mtime = rec->lk_time;
1386 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1388 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1392 rc = req_check_sepol(pill);
1396 rc = mdt_dlmreq_unpack(info);
1401 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1403 struct lu_ucred *uc = mdt_ucred(info);
1404 struct mdt_rec_unlink *rec;
1405 struct lu_attr *attr = &info->mti_attr.ma_attr;
1406 struct mdt_reint_record *rr = &info->mti_rr;
1407 struct req_capsule *pill = info->mti_pill;
1412 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1413 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1417 /* This prior initialization is needed for old_init_ucred_reint() */
1418 uc->uc_fsuid = rec->ul_fsuid;
1419 uc->uc_fsgid = rec->ul_fsgid;
1420 uc->uc_cap = CAP_EMPTY_SET;
1421 uc->uc_cap.cap[0] = rec->ul_cap;
1422 uc->uc_suppgids[0] = rec->ul_suppgid1;
1423 uc->uc_suppgids[1] = -1;
1425 attr->la_uid = rec->ul_fsuid;
1426 attr->la_gid = rec->ul_fsgid;
1427 rr->rr_fid1 = &rec->ul_fid1;
1428 rr->rr_fid2 = &rec->ul_fid2;
1429 attr->la_ctime = rec->ul_time;
1430 attr->la_mtime = rec->ul_time;
1431 attr->la_mode = rec->ul_mode;
1432 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1433 if (rec->ul_bias & MDS_FID_OP)
1434 info->mti_spec.sp_cr_flags |= MDS_OP_WITH_FID;
1436 info->mti_spec.sp_cr_flags &= ~MDS_OP_WITH_FID;
1438 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1442 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1444 rc = req_check_sepol(pill);
1448 rc = mdt_dlmreq_unpack(info);
1452 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1454 info->mti_spec.sp_rm_entry = 1;
1455 return mdt_unlink_unpack(info);
1458 static int mdt_rename_unpack(struct mdt_thread_info *info)
1460 struct lu_ucred *uc = mdt_ucred(info);
1461 struct mdt_rec_rename *rec;
1462 struct lu_attr *attr = &info->mti_attr.ma_attr;
1463 struct mdt_reint_record *rr = &info->mti_rr;
1464 struct req_capsule *pill = info->mti_pill;
1465 struct md_op_spec *spec = &info->mti_spec;
1470 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1471 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1475 /* This prior initialization is needed for old_init_ucred_reint() */
1476 uc->uc_fsuid = rec->rn_fsuid;
1477 uc->uc_fsgid = rec->rn_fsgid;
1478 uc->uc_cap = CAP_EMPTY_SET;
1479 uc->uc_cap.cap[0] = rec->rn_cap;
1480 uc->uc_suppgids[0] = rec->rn_suppgid1;
1481 uc->uc_suppgids[1] = rec->rn_suppgid2;
1483 attr->la_uid = rec->rn_fsuid;
1484 attr->la_gid = rec->rn_fsgid;
1485 rr->rr_fid1 = &rec->rn_fid1;
1486 rr->rr_fid2 = &rec->rn_fid2;
1487 attr->la_ctime = rec->rn_time;
1488 attr->la_mtime = rec->rn_time;
1489 /* rename_tgt contains the mode already */
1490 attr->la_mode = rec->rn_mode;
1491 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1493 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1497 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1501 spec->no_create = !!req_is_replay(mdt_info_req(info));
1503 rc = req_check_sepol(pill);
1507 rc = mdt_dlmreq_unpack(info);
1512 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1514 struct lu_ucred *uc = mdt_ucred(info);
1515 struct mdt_rec_rename *rec;
1516 struct lu_attr *attr = &info->mti_attr.ma_attr;
1517 struct mdt_reint_record *rr = &info->mti_rr;
1518 struct req_capsule *pill = info->mti_pill;
1519 struct md_op_spec *spec = &info->mti_spec;
1524 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1525 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1529 /* This prior initialization is needed for old_init_ucred_reint() */
1530 uc->uc_fsuid = rec->rn_fsuid;
1531 uc->uc_fsgid = rec->rn_fsgid;
1532 uc->uc_cap = CAP_EMPTY_SET;
1533 uc->uc_cap.cap[0] = rec->rn_cap;
1534 uc->uc_suppgids[0] = rec->rn_suppgid1;
1535 uc->uc_suppgids[1] = rec->rn_suppgid2;
1537 attr->la_uid = rec->rn_fsuid;
1538 attr->la_gid = rec->rn_fsgid;
1539 rr->rr_fid1 = &rec->rn_fid1;
1540 rr->rr_fid2 = &rec->rn_fid2;
1541 attr->la_ctime = rec->rn_time;
1542 attr->la_mtime = rec->rn_time;
1543 /* rename_tgt contains the mode already */
1544 attr->la_mode = rec->rn_mode;
1545 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1546 spec->sp_cr_flags = 0;
1548 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1552 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1553 rc = mdt_close_handle_unpack(info);
1557 spec->sp_migrate_close = 1;
1559 spec->sp_migrate_close = 0;
1562 spec->sp_migrate_nsonly = !!(rec->rn_bias & MDS_MIGRATE_NSONLY);
1564 /* lustre version > 2.11 migration packs lum */
1565 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1566 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1567 rr->rr_eadatalen = req_capsule_get_size(pill,
1571 if (rr->rr_eadatalen > 0) {
1572 struct lmv_user_md_v1 *lmu;
1574 lmu = req_capsule_client_get(pill, &RMF_EADATA);
1575 lmu->lum_hash_type |=
1576 cpu_to_le32(LMV_HASH_FLAG_FIXED);
1577 rr->rr_eadata = lmu;
1578 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1579 spec->u.sp_ea.eadata = rr->rr_eadata;
1580 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1583 /* old client doesn't provide lum. */
1584 RETURN(-EOPNOTSUPP);
1588 spec->no_create = !!req_is_replay(mdt_info_req(info));
1590 rc = mdt_dlmreq_unpack(info);
1596 * please see comment above LOV_MAGIC_V1_DEFINED
1598 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1600 struct lov_user_md_v1 *v1 = eadata;
1604 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1605 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1606 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1607 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1608 __swab32(LOV_MAGIC_MAGIC))
1609 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1613 static int mdt_open_unpack(struct mdt_thread_info *info)
1615 struct lu_ucred *uc = mdt_ucred(info);
1616 struct mdt_rec_create *rec;
1617 struct lu_attr *attr = &info->mti_attr.ma_attr;
1618 struct req_capsule *pill = info->mti_pill;
1619 struct mdt_reint_record *rr = &info->mti_rr;
1620 struct ptlrpc_request *req = mdt_info_req(info);
1621 struct md_op_spec *sp = &info->mti_spec;
1625 BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1626 sizeof(struct mdt_rec_reint));
1627 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1631 /* This prior initialization is needed for old_init_ucred_reint() */
1632 uc->uc_fsuid = rec->cr_fsuid;
1633 uc->uc_fsgid = rec->cr_fsgid;
1634 uc->uc_cap = CAP_EMPTY_SET;
1635 uc->uc_cap.cap[0] = rec->cr_cap;
1636 uc->uc_suppgids[0] = rec->cr_suppgid1;
1637 uc->uc_suppgids[1] = rec->cr_suppgid2;
1638 uc->uc_umask = rec->cr_umask;
1640 rr->rr_fid1 = &rec->cr_fid1;
1641 rr->rr_fid2 = &rec->cr_fid2;
1642 rr->rr_open_handle = &rec->cr_open_handle_old;
1643 attr->la_mode = rec->cr_mode;
1644 attr->la_rdev = rec->cr_rdev;
1645 attr->la_uid = rec->cr_fsuid;
1646 attr->la_gid = rec->cr_fsgid;
1647 attr->la_ctime = rec->cr_time;
1648 attr->la_mtime = rec->cr_time;
1649 attr->la_atime = rec->cr_time;
1650 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1651 LA_CTIME | LA_MTIME | LA_ATIME;
1652 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1653 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1654 /* Do not trigger ASSERTION if client miss to set such flags. */
1655 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1658 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1660 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1662 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1663 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1666 if (rr->rr_eadatalen > 0) {
1667 rr->rr_eadata = req_capsule_client_get(pill,
1669 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1670 sp->u.sp_ea.eadata = rr->rr_eadata;
1671 sp->sp_archive_id = rec->cr_archive_id;
1672 sp->no_create = !!req_is_replay(req);
1673 mdt_fix_lov_magic(info, rr->rr_eadata);
1677 * Client default md_size may be 0 right after client start,
1678 * until all osc are connected, set here just some reasonable
1679 * value to prevent misbehavior.
1681 if (rr->rr_eadatalen == 0 &&
1682 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1683 rr->rr_eadatalen = MIN_MD_SIZE;
1686 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1687 &sp->sp_cr_file_secctx,
1688 &sp->sp_cr_file_secctx_size);
1692 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1693 &sp->sp_cr_file_encctx_size);
1697 rc = req_check_sepol(pill);
1704 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1706 struct mdt_reint_record *rr = &info->mti_rr;
1707 struct lu_ucred *uc = mdt_ucred(info);
1708 struct lu_attr *attr = &info->mti_attr.ma_attr;
1709 struct req_capsule *pill = info->mti_pill;
1710 struct mdt_rec_setxattr *rec;
1715 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1716 sizeof(struct mdt_rec_reint));
1717 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1721 /* This prior initialization is needed for old_init_ucred_reint() */
1722 uc->uc_fsuid = rec->sx_fsuid;
1723 uc->uc_fsgid = rec->sx_fsgid;
1724 uc->uc_cap = CAP_EMPTY_SET;
1725 uc->uc_cap.cap[0] = rec->sx_cap;
1726 uc->uc_suppgids[0] = rec->sx_suppgid1;
1727 uc->uc_suppgids[1] = -1;
1729 rr->rr_opcode = rec->sx_opcode;
1730 rr->rr_fid1 = &rec->sx_fid;
1731 attr->la_valid = rec->sx_valid;
1732 attr->la_ctime = rec->sx_time;
1733 attr->la_size = rec->sx_size;
1734 attr->la_flags = rec->sx_flags;
1736 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1740 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1741 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1744 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1747 if (rr->rr_eadatalen > 0) {
1748 rr->rr_eadata = req_capsule_client_get(pill,
1750 if (rr->rr_eadata == NULL)
1753 rr->rr_eadata = NULL;
1755 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1756 CDEBUG(D_INFO, "no xattr data supplied\n");
1760 rc = req_check_sepol(pill);
1764 if (mdt_dlmreq_unpack(info) < 0)
1770 static int mdt_resync_unpack(struct mdt_thread_info *info)
1772 struct req_capsule *pill = info->mti_pill;
1773 struct mdt_reint_record *rr = &info->mti_rr;
1774 struct lu_ucred *uc = mdt_ucred(info);
1775 struct mdt_rec_resync *rec;
1778 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1779 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1783 /* This prior initialization is needed for old_init_ucred_reint() */
1784 uc->uc_fsuid = rec->rs_fsuid;
1785 uc->uc_fsgid = rec->rs_fsgid;
1786 uc->uc_cap = CAP_EMPTY_SET;
1787 uc->uc_cap.cap[0] = rec->rs_cap;
1789 rr->rr_fid1 = &rec->rs_fid;
1790 rr->rr_mirror_id = rec->rs_mirror_id;
1792 /* cookie doesn't need to be swapped but it has been swapped
1793 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1795 if (req_capsule_req_need_swab(pill))
1796 __swab64s(&rec->rs_lease_handle.cookie);
1797 rr->rr_lease_handle = &rec->rs_lease_handle;
1799 RETURN(mdt_dlmreq_unpack(info));
1802 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1804 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1805 [REINT_SETATTR] = mdt_setattr_unpack,
1806 [REINT_CREATE] = mdt_create_unpack,
1807 [REINT_LINK] = mdt_link_unpack,
1808 [REINT_UNLINK] = mdt_unlink_unpack,
1809 [REINT_RENAME] = mdt_rename_unpack,
1810 [REINT_OPEN] = mdt_open_unpack,
1811 [REINT_SETXATTR] = mdt_setxattr_unpack,
1812 [REINT_RMENTRY] = mdt_rmentry_unpack,
1813 [REINT_MIGRATE] = mdt_migrate_unpack,
1814 [REINT_RESYNC] = mdt_resync_unpack,
1817 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1822 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1823 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1824 info->mti_rr.rr_opcode = op;
1825 rc = mdt_reint_unpackers[op](info);
1827 CERROR("Unexpected opcode %d\n", op);
1833 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1834 struct mdt_object *child)
1837 struct lu_buf *buffer;
1838 struct mdt_body *repbody;
1839 struct req_capsule *pill = info->mti_pill;
1842 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1843 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1845 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1846 buffer = &info->mti_buf;
1848 /* fill reply buffer with security context now */
1849 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1851 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1853 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1854 buffer, secctx_name);
1857 "found security context of size %d for "DFID"\n",
1858 rc, PFID(mdt_object_fid(child)));
1860 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1861 repbody->mbo_valid |= OBD_MD_SECCTX;
1862 if (rc < buffer->lb_len)
1863 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1868 "security context not found for "DFID": rc = %d\n",
1869 PFID(mdt_object_fid(child)), rc);
1870 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1872 /* handling -ENOENT is important because it may change
1873 * object state in DNE env dropping LOHA_EXISTS flag,
1874 * it is important to return that to the caller.
1875 * Check LU-13115 for details.
1884 /* check whether two FIDs belong to different MDT. */
1885 static int mdt_fids_different_target(struct mdt_thread_info *info,
1886 const struct lu_fid *fid1,
1887 const struct lu_fid *fid2)
1889 const struct lu_env *env = info->mti_env;
1890 struct mdt_device *mdt = info->mti_mdt;
1891 struct lu_seq_range *range = &info->mti_range;
1892 struct seq_server_site *ss;
1893 __u32 index1, index2;
1896 if (fid_seq(fid1) == fid_seq(fid2))
1899 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1901 range->lsr_flags = LU_SEQ_RANGE_MDT;
1902 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1906 index1 = range->lsr_index;
1908 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1912 index2 = range->lsr_index;
1914 return index1 != index2;
1918 * Check whether \a child is remote object on \a parent.
1920 * \param[in] info thread environment
1921 * \param[in] parent parent object, it's the same as child object in
1923 * \param[in] child child object
1925 * \retval 1 is remote object.
1926 * \retval 0 isn't remote object.
1927 * \retval < 1 error code
1929 int mdt_is_remote_object(struct mdt_thread_info *info,
1930 struct mdt_object *parent,
1931 struct mdt_object *child)
1933 struct lu_buf *buf = &info->mti_big_buf;
1934 struct linkea_data ldata = { NULL };
1935 struct link_ea_header *leh;
1936 struct link_ea_entry *lee;
1937 struct lu_name name;
1945 if (fid_is_root(mdt_object_fid(child)))
1948 if (likely(parent != child)) {
1949 if (mdt_object_remote(parent) ^ mdt_object_remote(child))
1952 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1955 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1956 mdt_object_fid(child));
1960 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1961 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1966 rc = mdt_links_read(info, child, &ldata);
1967 /* can't read linkea, just assume it's remote object */
1968 if (rc == -ENOENT || rc == -ENODATA)
1974 lee = (struct link_ea_entry *)(leh + 1);
1975 for (i = 0; i < leh->leh_reccount; i++) {
1976 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1977 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1978 if (mdt_fids_different_target(info, &pfid,
1979 mdt_object_fid(child)))
1986 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
1987 struct mdt_object *child)
1989 struct lu_buf *buffer;
1990 struct mdt_body *repbody;
1991 struct req_capsule *pill = info->mti_pill;
1992 struct obd_export *exp = mdt_info_req(info)->rq_export;
1995 if (!exp_connect_encrypt(exp))
1998 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
1999 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0) {
2000 struct lu_attr la = { 0 };
2001 struct dt_object *dt = mdt_obj2dt(child);
2003 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
2004 dt_attr_get(info->mti_env, mdt_obj2dt(child), &la);
2006 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL) {
2007 buffer = &info->mti_buf;
2009 /* fill reply buffer with encryption context now */
2011 req_capsule_get_size(pill, &RMF_FILE_ENCCTX,
2014 req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
2015 rc = mo_xattr_get(info->mti_env,
2016 mdt_object_child(child),
2018 LL_XATTR_NAME_ENCRYPTION_CONTEXT);
2019 if (unlikely(rc == -ENODATA))
2020 /* For compatibility with 2.14 */
2021 rc = mo_xattr_get(info->mti_env,
2022 mdt_object_child(child),
2024 LL_XATTR_NAME_ENCRYPTION_CONTEXT_OLD);
2027 "found encryption ctx of size %d for "DFID"\n",
2028 rc, PFID(mdt_object_fid(child)));
2030 repbody = req_capsule_server_get(pill,
2032 repbody->mbo_valid |= OBD_MD_ENCCTX;
2033 if (rc < buffer->lb_len)
2034 req_capsule_shrink(pill,
2035 &RMF_FILE_ENCCTX, rc,
2040 "encryption ctx not found for "DFID": rc = %d\n",
2041 PFID(mdt_object_fid(child)), rc);
2042 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2044 /* handling -ENOENT is important because it may
2045 * change object state in DNE env dropping
2046 * LOHA_EXISTS flag, it is important to return
2047 * that to the caller.
2048 * Check LU-13115 for details.
2054 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,