4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_lib.c
34 * Lustre Metadata Target (mdt) request unpacking helper.
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Fan Yong <fanyong@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/user_namespace.h>
48 #include <linux/uidgid.h>
50 #include "mdt_internal.h"
51 #include <uapi/linux/lnet/nidstr.h>
52 #include <lustre_nodemap.h>
54 typedef enum ucred_init_type {
60 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
62 return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
65 void mdt_exit_ucred(struct mdt_thread_info *info)
67 struct lu_ucred *uc = mdt_ucred(info);
68 struct mdt_device *mdt = info->mti_mdt;
71 if (uc->uc_valid != UCRED_INIT) {
72 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
74 put_group_info(uc->uc_ginfo);
77 if (uc->uc_identity) {
78 mdt_identity_put(mdt->mdt_identity_cache,
80 uc->uc_identity = NULL;
82 uc->uc_valid = UCRED_INIT;
86 static int match_nosquash_list(struct spinlock *rsi_lock,
87 struct list_head *nidlist,
93 rc = cfs_match_nid(peernid, nidlist);
94 spin_unlock(rsi_lock);
98 /* root_squash for inter-MDS operations */
99 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
101 struct lu_ucred *ucred = mdt_ucred(info);
102 struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
105 LASSERT(ucred != NULL);
106 if (!squash->rsi_uid || ucred->uc_fsuid)
109 if (match_nosquash_list(&squash->rsi_lock,
110 &squash->rsi_nosquash_nids,
112 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
113 libcfs_nid2str(peernid));
117 CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
118 libcfs_nid2str(peernid),
119 ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
120 squash->rsi_uid, squash->rsi_gid, 0);
122 ucred->uc_fsuid = squash->rsi_uid;
123 ucred->uc_fsgid = squash->rsi_gid;
125 ucred->uc_suppgids[0] = -1;
126 ucred->uc_suppgids[1] = -1;
131 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
133 struct ptlrpc_request *req = mdt_info_req(info);
134 const char *jobid = mdt_req_get_jobid(req);
136 /* set jobid if specified. */
138 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
140 uc->uc_jobid[0] = '\0';
143 static void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
145 if (info && info->mti_exp && info->mti_exp->exp_connection)
146 uc->uc_nid = info->mti_exp->exp_connection->c_peer.nid;
148 uc->uc_nid = LNET_NID_ANY;
151 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
154 struct lu_nodemap *nodemap = NULL;
157 if (info && info->mti_exp) {
158 nodemap = nodemap_get_from_exp(info->mti_exp);
159 if (nodemap && !IS_ERR(nodemap)) {
160 audit = nodemap->nmf_enable_audit;
161 nodemap_putref(nodemap);
165 uc->uc_enable_audit = audit;
168 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
169 void *buf, bool drop_fs_cap)
171 struct ptlrpc_request *req = mdt_info_req(info);
172 struct mdt_device *mdt = info->mti_mdt;
173 struct ptlrpc_user_desc *pud = req->rq_user_desc;
174 struct lu_ucred *ucred = mdt_ucred(info);
175 struct lu_nodemap *nodemap;
176 lnet_nid_t peernid = req->rq_peer.nid;
180 bool is_nm_gid_squashed = false;
185 LASSERT(req->rq_auth_gss);
186 LASSERT(!req->rq_auth_usr_mdt);
187 LASSERT(req->rq_user_desc);
188 LASSERT(ucred != NULL);
190 ucred->uc_valid = UCRED_INVALID;
192 nodemap = nodemap_get_from_exp(info->mti_exp);
194 RETURN(PTR_ERR(nodemap));
196 pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
197 NODEMAP_CLIENT_TO_FS, pud->pud_uid);
198 pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
199 NODEMAP_CLIENT_TO_FS, pud->pud_gid);
200 pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
201 NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
202 pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
203 NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
205 ucred->uc_o_uid = pud->pud_uid;
206 ucred->uc_o_gid = pud->pud_gid;
207 ucred->uc_o_fsuid = pud->pud_fsuid;
208 ucred->uc_o_fsgid = pud->pud_fsgid;
210 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
211 /* deny access before we get identity ref */
212 if (nodemap->nmf_deny_unknown) {
213 nodemap_putref(nodemap);
217 ucred->uc_fsuid = nodemap->nm_squash_uid;
218 ucred->uc_fsgid = nodemap->nm_squash_gid;
220 ucred->uc_suppgids[0] = -1;
221 ucred->uc_suppgids[1] = -1;
224 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
225 is_nm_gid_squashed = true;
227 nodemap_putref(nodemap);
229 if (type == BODY_INIT) {
230 struct mdt_body *body = (struct mdt_body *)buf;
232 ucred->uc_suppgids[0] = body->mbo_suppgid;
233 ucred->uc_suppgids[1] = -1;
236 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
237 req->rq_auth_uid != pud->pud_uid) {
238 CDEBUG(D_SEC, "local client %s: auth uid %u "
239 "while client claims %u:%u/%u:%u\n",
240 libcfs_nid2str(peernid), req->rq_auth_uid,
241 pud->pud_uid, pud->pud_gid,
242 pud->pud_fsuid, pud->pud_fsgid);
246 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
247 ucred->uc_identity = NULL;
248 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
250 struct md_identity *identity;
252 identity = mdt_identity_get(mdt->mdt_identity_cache,
254 if (IS_ERR(identity)) {
255 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
256 ucred->uc_identity = NULL;
257 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
261 "Deny access without identity: uid %u\n",
266 ucred->uc_identity = identity;
267 perm = mdt_identity_get_perm(ucred->uc_identity,
272 /* find out the setuid/setgid attempt */
273 setuid = (pud->pud_uid != pud->pud_fsuid);
274 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
275 (ucred->uc_identity &&
276 (pud->pud_gid != ucred->uc_identity->mi_gid)));
278 /* check permission of setuid */
279 if (setuid && !(perm & CFS_SETUID_PERM)) {
280 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
281 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
282 GOTO(out, rc = -EACCES);
285 /* check permission of setgid */
286 if (setgid && !(perm & CFS_SETGID_PERM)) {
287 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
288 "from %s\n", pud->pud_uid, pud->pud_gid,
289 pud->pud_fsuid, pud->pud_fsgid,
290 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
291 GOTO(out, rc = -EACCES);
294 if (perm & CFS_SETGRP_PERM) {
295 /* only set groups if GID is not squashed */
296 if (pud->pud_ngroups && !is_nm_gid_squashed) {
297 /* setgroups for local client */
298 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
299 if (!ucred->uc_ginfo) {
300 CERROR("failed to alloc %d groups\n",
302 GOTO(out, rc = -ENOMEM);
305 lustre_groups_from_list(ucred->uc_ginfo,
307 lustre_groups_sort(ucred->uc_ginfo);
309 ucred->uc_suppgids[0] = -1;
310 ucred->uc_suppgids[1] = -1;
311 ucred->uc_ginfo = NULL;
314 ucred->uc_suppgids[0] = -1;
315 ucred->uc_suppgids[1] = -1;
316 ucred->uc_ginfo = NULL;
319 ucred->uc_uid = pud->pud_uid;
320 ucred->uc_gid = pud->pud_gid;
321 ucred->uc_fsuid = pud->pud_fsuid;
322 ucred->uc_fsgid = pud->pud_fsgid;
324 /* process root_squash here. */
325 mdt_root_squash(info, peernid);
327 /* remove fs privilege for non-root user. */
328 if (ucred->uc_fsuid && drop_fs_cap)
329 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
331 ucred->uc_cap = pud->pud_cap;
332 ucred->uc_valid = UCRED_NEW;
333 ucred_set_jobid(info, ucred);
334 ucred_set_nid(info, ucred);
335 ucred_set_audit_enabled(info, ucred);
341 if (ucred->uc_ginfo) {
342 put_group_info(ucred->uc_ginfo);
343 ucred->uc_ginfo = NULL;
345 if (ucred->uc_identity) {
346 mdt_identity_put(mdt->mdt_identity_cache,
348 ucred->uc_identity = NULL;
356 * Check whether allow the client to set supplementary group IDs or not.
358 * \param[in] info pointer to the thread context
359 * \param[in] uc pointer to the RPC user descriptor
361 * \retval true if allow to set supplementary group IDs
362 * \retval false for other cases
364 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
368 /* 1. If identity_upcall is disabled,
369 * permit local client to do anything. */
370 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
373 /* 2. If fail to get related identities, then forbid any client to
374 * set supplementary group IDs. */
375 if (uc->uc_identity == NULL)
378 /* 3. Check the permission in the identities. */
379 perm = mdt_identity_get_perm(uc->uc_identity,
380 mdt_info_req(info)->rq_peer.nid);
381 if (perm & CFS_SETGRP_PERM)
387 int mdt_check_ucred(struct mdt_thread_info *info)
389 struct ptlrpc_request *req = mdt_info_req(info);
390 struct mdt_device *mdt = info->mti_mdt;
391 struct ptlrpc_user_desc *pud = req->rq_user_desc;
392 struct lu_ucred *ucred = mdt_ucred(info);
393 struct md_identity *identity = NULL;
394 lnet_nid_t peernid = req->rq_peer.nid;
402 LASSERT(ucred != NULL);
403 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
406 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
409 /* sanity check: if we use strong authentication, we expect the
410 * uid which client claimed is true */
411 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
412 req->rq_auth_uid != pud->pud_uid) {
413 CDEBUG(D_SEC, "local client %s: auth uid %u "
414 "while client claims %u:%u/%u:%u\n",
415 libcfs_nid2str(peernid), req->rq_auth_uid,
416 pud->pud_uid, pud->pud_gid,
417 pud->pud_fsuid, pud->pud_fsgid);
421 if (is_identity_get_disabled(mdt->mdt_identity_cache))
424 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
425 if (IS_ERR(identity)) {
426 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
429 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
435 perm = mdt_identity_get_perm(identity, peernid);
436 /* find out the setuid/setgid attempt */
437 setuid = (pud->pud_uid != pud->pud_fsuid);
438 setgid = (pud->pud_gid != pud->pud_fsgid ||
439 pud->pud_gid != identity->mi_gid);
441 /* check permission of setuid */
442 if (setuid && !(perm & CFS_SETUID_PERM)) {
443 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
444 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
445 GOTO(out, rc = -EACCES);
448 /* check permission of setgid */
449 if (setgid && !(perm & CFS_SETGID_PERM)) {
450 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
451 "from %s\n", pud->pud_uid, pud->pud_gid,
452 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
453 libcfs_nid2str(peernid));
454 GOTO(out, rc = -EACCES);
460 mdt_identity_put(mdt->mdt_identity_cache, identity);
464 static int old_init_ucred_common(struct mdt_thread_info *info,
465 struct lu_nodemap *nodemap,
468 struct lu_ucred *uc = mdt_ucred(info);
469 struct mdt_device *mdt = info->mti_mdt;
470 struct md_identity *identity = NULL;
472 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
473 /* deny access before we get identity ref */
474 if (nodemap->nmf_deny_unknown)
477 uc->uc_fsuid = nodemap->nm_squash_uid;
478 uc->uc_fsgid = nodemap->nm_squash_gid;
480 uc->uc_suppgids[0] = -1;
481 uc->uc_suppgids[1] = -1;
484 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
485 identity = mdt_identity_get(mdt->mdt_identity_cache,
487 if (IS_ERR(identity)) {
488 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
489 uc->uc_cap & CFS_CAP_FS_MASK)) {
492 CDEBUG(D_SEC, "Deny access without identity: "
493 "uid %u\n", uc->uc_fsuid);
498 uc->uc_identity = identity;
500 /* process root_squash here. */
501 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
503 /* remove fs privilege for non-root user. */
504 if (uc->uc_fsuid && drop_fs_cap)
505 uc->uc_cap &= ~CFS_CAP_FS_MASK;
506 uc->uc_valid = UCRED_OLD;
507 ucred_set_jobid(info, uc);
508 ucred_set_nid(info, uc);
509 ucred_set_audit_enabled(info, uc);
516 static int old_init_ucred(struct mdt_thread_info *info,
517 struct mdt_body *body, bool drop_fs_cap)
519 struct lu_ucred *uc = mdt_ucred(info);
520 struct lu_nodemap *nodemap;
524 nodemap = nodemap_get_from_exp(info->mti_exp);
526 RETURN(PTR_ERR(nodemap));
528 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
529 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
530 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
531 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
532 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
533 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
534 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
535 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
538 uc->uc_valid = UCRED_INVALID;
539 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
540 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
541 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
542 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
543 uc->uc_suppgids[0] = body->mbo_suppgid;
544 uc->uc_suppgids[1] = -1;
546 uc->uc_cap = body->mbo_capability;
548 rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
549 nodemap_putref(nodemap);
554 static int old_init_ucred_reint(struct mdt_thread_info *info)
556 struct lu_ucred *uc = mdt_ucred(info);
557 struct lu_nodemap *nodemap;
561 nodemap = nodemap_get_from_exp(info->mti_exp);
563 RETURN(PTR_ERR(nodemap));
567 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
568 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
569 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
570 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
572 uc->uc_valid = UCRED_INVALID;
573 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
574 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
577 rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
578 nodemap_putref(nodemap);
583 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
584 struct mdt_body *body,
587 struct ptlrpc_request *req = mdt_info_req(info);
588 struct lu_ucred *uc = mdt_ucred(info);
591 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
594 mdt_exit_ucred(info);
596 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
597 return old_init_ucred(info, body, drop_fs_cap);
599 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
602 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
604 return __mdt_init_ucred(info, body, true);
607 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
608 * doesn't set correct fsuid explicitely, but raise capability to allow
609 * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
610 * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
611 * will fail permission check. */
612 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
613 struct mdt_body *body)
615 return __mdt_init_ucred(info, body, false);
618 int mdt_init_ucred_reint(struct mdt_thread_info *info)
620 struct ptlrpc_request *req = mdt_info_req(info);
621 struct lu_ucred *uc = mdt_ucred(info);
622 struct md_attr *ma = &info->mti_attr;
625 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
628 /* LU-5564: for normal close request, skip permission check */
629 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
630 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
631 uc->uc_cap |= CFS_CAP_FS_MASK;
633 mdt_exit_ucred(info);
635 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
636 return old_init_ucred_reint(info);
638 return new_init_ucred(info, REC_INIT, NULL, true);
641 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
642 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
644 const struct lov_ost_data_v1 *lod;
645 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
649 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
652 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
653 POSTID(&lmm->lmm_oi), lmm_magic,
654 le32_to_cpu(lmm->lmm_pattern));
656 /* No support for compount layouts yet */
657 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
660 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
661 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
662 le32_to_cpu(lmm->lmm_stripe_size), count);
664 /* If it's a directory or a released file, then there are
665 * no actual objects to print, so bail out. */
666 if (valid & OBD_MD_FLDIREA ||
667 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
670 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
671 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
674 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
675 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
676 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
680 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
682 const struct lmv_mds_md_v1 *lmm1;
683 const struct lmv_foreign_md *lfm;
686 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
689 /* foreign LMV case */
690 lfm = &lmv->lmv_foreign_md;
691 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
693 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
694 le32_to_cpu(lfm->lfm_magic),
695 le32_to_cpu(lfm->lfm_length),
696 le32_to_cpu(lfm->lfm_type),
697 le32_to_cpu(lfm->lfm_flags),
698 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
702 lmm1 = &lmv->lmv_md_v1;
704 "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
705 le32_to_cpu(lmm1->lmv_magic),
706 le32_to_cpu(lmm1->lmv_master_mdt_index),
707 le32_to_cpu(lmm1->lmv_stripe_count),
708 le32_to_cpu(lmm1->lmv_hash_type));
710 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
713 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
716 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
717 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
721 /* Shrink and/or grow reply buffers */
722 int mdt_fix_reply(struct mdt_thread_info *info)
724 struct req_capsule *pill = info->mti_pill;
725 struct mdt_body *body;
726 int md_size, md_packed = 0;
731 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
732 LASSERT(body != NULL);
734 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
736 md_size = body->mbo_eadatasize;
740 acl_size = body->mbo_aclsize;
742 /* this replay - not send info to client */
743 if (info->mti_spec.no_create) {
748 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
753 &RMF_ACL, or &RMF_LOGCOOKIES
754 (optional) &RMF_CAPA1,
755 (optional) &RMF_CAPA2,
756 (optional) something else
759 /* MDT_MD buffer may be bigger than packed value, let's shrink all
760 * buffers before growing it */
761 if (info->mti_big_lmm_used) {
762 /* big_lmm buffer may be used even without packing the result
763 * into reply, just for internal server needs */
764 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
765 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
768 /* free big lmm if md_size is not needed */
769 if (md_size == 0 || md_packed == 0) {
770 info->mti_big_lmm_used = 0;
772 /* buffer must be allocated separately */
773 LASSERT(info->mti_attr.ma_lmm !=
774 req_capsule_server_get(pill, &RMF_MDT_MD));
775 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
777 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
778 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
781 if (info->mti_big_acl_used) {
783 info->mti_big_acl_used = 0;
785 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
786 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
787 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
788 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
789 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
792 /* Shrink optional SECCTX buffer if it is not used */
793 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
794 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
795 !(body->mbo_valid & OBD_MD_SECCTX))
796 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
799 * Some more field should be shrinked if needed.
800 * This should be done by those who added fields to reply message.
803 /* Grow MD buffer if needed finally */
804 if (info->mti_big_lmm_used) {
807 LASSERT(md_size > md_packed);
808 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
809 md_size - md_packed);
810 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
812 /* we can't answer with proper LOV EA, drop flags,
813 * the rc is also returned so this request is
814 * considered as failed */
815 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
816 /* don't return transno along with error */
817 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
819 /* now we need to pack right LOV/LMV EA */
820 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
821 if (info->mti_attr.ma_valid & MA_LOV) {
822 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
824 info->mti_attr.ma_lmm_size);
825 memcpy(lmm, info->mti_attr.ma_lmm,
826 info->mti_attr.ma_lmm_size);
827 } else if (info->mti_attr.ma_valid & MA_LMV) {
828 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
830 info->mti_attr.ma_lmv_size);
831 memcpy(lmm, info->mti_attr.ma_lmv,
832 info->mti_attr.ma_lmv_size);
836 /* update mdt_max_mdsize so clients will be aware about that */
837 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
838 info->mti_mdt->mdt_max_mdsize =
839 info->mti_attr.ma_lmm_size;
840 info->mti_big_lmm_used = 0;
843 if (info->mti_big_acl_used) {
844 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
847 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
849 body->mbo_valid &= ~OBD_MD_FLACL;
851 void *acl = req_capsule_server_get(pill, &RMF_ACL);
853 memcpy(acl, info->mti_big_acl, acl_size);
856 info->mti_big_acl_used = 0;
863 /* if object is dying, pack the lov/llog data,
864 * parameter info->mti_attr should be valid at this point!
865 * Also implements RAoLU policy */
866 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
869 struct mdt_body *repbody = NULL;
870 const struct lu_attr *la = &ma->ma_attr;
871 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
874 struct hsm_action_item hai = {
875 .hai_len = sizeof(hai),
876 .hai_action = HSMA_REMOVE,
877 .hai_extent.length = -1,
885 if (mdt_info_req(info) != NULL) {
886 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
887 LASSERT(repbody != NULL);
889 CDEBUG(D_INFO, "not running in a request/reply context\n");
892 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
893 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
895 if (ma->ma_valid & MA_LOV) {
896 CERROR("No need in LOV EA upon unlink\n");
900 repbody->mbo_eadatasize = 0;
902 /* Only check unlinked and archived if RAoLU and upon last close */
903 if (!cdt->cdt_remove_archive_on_last_unlink ||
904 atomic_read(&mo->mot_open_count) != 0)
907 /* mdt_attr_get_complex will clear ma_valid, so check here first */
908 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
911 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
914 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
916 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
917 * try setting them */
919 rc = mdt_attr_get_complex(info, mo, ma);
921 CERROR("%s: unable to fetch missing attributes of"
922 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
923 PFID(mdt_object_fid(mo)), rc);
927 if (need & MA_INODE) {
928 if (ma->ma_valid & MA_INODE) {
929 if (ma->ma_attr.la_nlink != 0)
937 if (ma->ma_valid & MA_HSM) {
938 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
946 /* RAoLU policy is active, last close on file has occured,
947 * file is unlinked, file is archived, so create remove request
949 * If CDT is not running, requests will be logged for later. */
950 if (ma->ma_hsm.mh_arch_id != 0)
951 archive_id = ma->ma_hsm.mh_arch_id;
953 archive_id = cdt->cdt_default_archive_id;
955 hai.hai_fid = *mdt_object_fid(mo);
957 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
960 CERROR("%s: unable to add HSM remove request for "DFID
961 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
962 PFID(mdt_object_fid(mo)), rc);
967 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
973 if (in & MDS_ATTR_MODE)
975 if (in & MDS_ATTR_UID)
977 if (in & MDS_ATTR_GID)
979 if (in & MDS_ATTR_SIZE)
981 if (in & MDS_ATTR_BLOCKS)
983 if (in & MDS_ATTR_ATIME_SET)
985 if (in & MDS_ATTR_CTIME_SET)
987 if (in & MDS_ATTR_MTIME_SET)
989 if (in & MDS_ATTR_ATTR_FLAG)
991 if (in & MDS_ATTR_KILL_SUID)
993 if (in & MDS_ATTR_KILL_SGID)
995 if (in & MDS_ATTR_PROJID)
997 if (in & MDS_ATTR_LSIZE)
999 if (in & MDS_ATTR_LBLOCKS)
1002 if (in & MDS_ATTR_FROM_OPEN)
1003 rr->rr_flags |= MRF_OPEN_TRUNC;
1004 if (in & MDS_ATTR_OVERRIDE)
1005 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1006 if (in & MDS_ATTR_FORCE)
1007 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1009 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1010 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1011 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1012 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1013 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1014 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1017 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", in);
1023 int mdt_name_unpack(struct req_capsule *pill,
1024 const struct req_msg_field *field,
1026 enum mdt_name_flags flags)
1028 ln->ln_name = req_capsule_client_get(pill, field);
1029 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1031 if (!lu_name_is_valid(ln)) {
1038 if ((flags & MNF_FIX_ANON) &&
1039 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1040 /* Newer (3.x) kernels use a name of "/" for the
1041 * "anonymous" disconnected dentries from NFS
1042 * filehandle conversion. See d_obtain_alias(). */
1050 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1051 const char **secctx_name,
1052 void **secctx, size_t *secctx_size)
1057 *secctx_name = NULL;
1061 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1062 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1065 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1070 if (name_size > XATTR_NAME_MAX + 1)
1073 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1074 if (strnlen(name, name_size) != name_size - 1)
1077 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1078 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1081 *secctx_name = name;
1082 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1083 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1088 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1090 struct lu_ucred *uc = mdt_ucred(info);
1091 struct md_attr *ma = &info->mti_attr;
1092 struct lu_attr *la = &ma->ma_attr;
1093 struct req_capsule *pill = info->mti_pill;
1094 struct mdt_reint_record *rr = &info->mti_rr;
1095 struct mdt_rec_setattr *rec;
1096 struct lu_nodemap *nodemap;
1100 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1101 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1105 /* This prior initialization is needed for old_init_ucred_reint() */
1106 uc->uc_fsuid = rec->sa_fsuid;
1107 uc->uc_fsgid = rec->sa_fsgid;
1108 uc->uc_cap = rec->sa_cap;
1109 uc->uc_suppgids[0] = rec->sa_suppgid;
1110 uc->uc_suppgids[1] = -1;
1112 rr->rr_fid1 = &rec->sa_fid;
1113 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1114 la->la_mode = rec->sa_mode;
1115 la->la_flags = rec->sa_attr_flags;
1117 nodemap = nodemap_get_from_exp(info->mti_exp);
1118 if (IS_ERR(nodemap))
1119 RETURN(PTR_ERR(nodemap));
1121 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1122 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1123 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1124 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1125 la->la_projid = rec->sa_projid;
1126 nodemap_putref(nodemap);
1128 la->la_size = rec->sa_size;
1129 la->la_blocks = rec->sa_blocks;
1130 la->la_ctime = rec->sa_ctime;
1131 la->la_atime = rec->sa_atime;
1132 la->la_mtime = rec->sa_mtime;
1133 ma->ma_valid = MA_INODE;
1135 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1136 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1141 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1143 struct req_capsule *pill = info->mti_pill;
1144 struct mdt_ioepoch *ioepoch;
1147 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1148 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1152 if (ioepoch == NULL)
1155 info->mti_open_handle = ioepoch->mio_open_handle;
1160 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1161 struct req_capsule *pill = info->mti_pill;
1163 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1164 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1165 if (info->mti_dlm_req == NULL)
1172 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1174 struct mdt_reint_record *rr = &info->mti_rr;
1175 struct md_attr *ma = &info->mti_attr;
1176 struct req_capsule *pill = info->mti_pill;
1180 rc = mdt_setattr_unpack_rec(info);
1184 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1185 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1186 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1189 if (rr->rr_eadatalen > 0) {
1190 const struct lmv_user_md *lum;
1192 lum = rr->rr_eadata;
1193 /* Sigh ma_valid(from req) does not indicate whether
1194 * it will set LOV/LMV EA, so we have to check magic */
1195 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1196 ma->ma_valid |= MA_LMV;
1197 ma->ma_lmv = (void *)rr->rr_eadata;
1198 ma->ma_lmv_size = rr->rr_eadatalen;
1200 ma->ma_valid |= MA_LOV;
1201 ma->ma_lmm = (void *)rr->rr_eadata;
1202 ma->ma_lmm_size = rr->rr_eadatalen;
1207 rc = mdt_dlmreq_unpack(info);
1211 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1213 struct md_attr *ma = &info->mti_attr;
1214 struct req_capsule *pill = info->mti_pill;
1217 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1220 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1222 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1223 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1229 int mdt_close_unpack(struct mdt_thread_info *info)
1234 rc = mdt_close_handle_unpack(info);
1238 rc = mdt_setattr_unpack_rec(info);
1242 rc = mdt_close_intent_unpack(info);
1246 RETURN(mdt_init_ucred_reint(info));
1249 static int mdt_create_unpack(struct mdt_thread_info *info)
1251 struct lu_ucred *uc = mdt_ucred(info);
1252 struct mdt_rec_create *rec;
1253 struct lu_attr *attr = &info->mti_attr.ma_attr;
1254 struct mdt_reint_record *rr = &info->mti_rr;
1255 struct req_capsule *pill = info->mti_pill;
1256 struct md_op_spec *sp = &info->mti_spec;
1261 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1262 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1266 /* This prior initialization is needed for old_init_ucred_reint() */
1267 uc->uc_fsuid = rec->cr_fsuid;
1268 uc->uc_fsgid = rec->cr_fsgid;
1269 uc->uc_cap = rec->cr_cap;
1270 uc->uc_suppgids[0] = rec->cr_suppgid1;
1271 uc->uc_suppgids[1] = -1;
1272 uc->uc_umask = rec->cr_umask;
1274 rr->rr_fid1 = &rec->cr_fid1;
1275 rr->rr_fid2 = &rec->cr_fid2;
1276 attr->la_mode = rec->cr_mode;
1277 attr->la_rdev = rec->cr_rdev;
1278 attr->la_uid = rec->cr_fsuid;
1279 attr->la_gid = rec->cr_fsgid;
1280 attr->la_ctime = rec->cr_time;
1281 attr->la_mtime = rec->cr_time;
1282 attr->la_atime = rec->cr_time;
1283 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1284 LA_CTIME | LA_MTIME | LA_ATIME;
1285 memset(&sp->u, 0, sizeof(sp->u));
1286 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1288 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1292 if (S_ISLNK(attr->la_mode)) {
1293 const char *tgt = NULL;
1295 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1296 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1297 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1298 sp->u.sp_symname = tgt;
1303 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1304 if (S_ISDIR(attr->la_mode) &&
1305 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1306 sp->u.sp_ea.eadata =
1307 req_capsule_client_get(pill, &RMF_EADATA);
1308 sp->u.sp_ea.eadatalen =
1309 req_capsule_get_size(pill, &RMF_EADATA,
1311 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1315 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1316 &sp->sp_cr_file_secctx,
1317 &sp->sp_cr_file_secctx_size);
1321 rc = req_check_sepol(pill);
1325 rc = mdt_dlmreq_unpack(info);
1329 static int mdt_link_unpack(struct mdt_thread_info *info)
1331 struct lu_ucred *uc = mdt_ucred(info);
1332 struct mdt_rec_link *rec;
1333 struct lu_attr *attr = &info->mti_attr.ma_attr;
1334 struct mdt_reint_record *rr = &info->mti_rr;
1335 struct req_capsule *pill = info->mti_pill;
1340 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1341 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1345 /* This prior initialization is needed for old_init_ucred_reint() */
1346 uc->uc_fsuid = rec->lk_fsuid;
1347 uc->uc_fsgid = rec->lk_fsgid;
1348 uc->uc_cap = rec->lk_cap;
1349 uc->uc_suppgids[0] = rec->lk_suppgid1;
1350 uc->uc_suppgids[1] = rec->lk_suppgid2;
1352 attr->la_uid = rec->lk_fsuid;
1353 attr->la_gid = rec->lk_fsgid;
1354 rr->rr_fid1 = &rec->lk_fid1;
1355 rr->rr_fid2 = &rec->lk_fid2;
1356 attr->la_ctime = rec->lk_time;
1357 attr->la_mtime = rec->lk_time;
1358 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1360 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1364 rc = req_check_sepol(pill);
1368 rc = mdt_dlmreq_unpack(info);
1373 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1375 struct lu_ucred *uc = mdt_ucred(info);
1376 struct mdt_rec_unlink *rec;
1377 struct lu_attr *attr = &info->mti_attr.ma_attr;
1378 struct mdt_reint_record *rr = &info->mti_rr;
1379 struct req_capsule *pill = info->mti_pill;
1384 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1385 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1389 /* This prior initialization is needed for old_init_ucred_reint() */
1390 uc->uc_fsuid = rec->ul_fsuid;
1391 uc->uc_fsgid = rec->ul_fsgid;
1392 uc->uc_cap = rec->ul_cap;
1393 uc->uc_suppgids[0] = rec->ul_suppgid1;
1394 uc->uc_suppgids[1] = -1;
1396 attr->la_uid = rec->ul_fsuid;
1397 attr->la_gid = rec->ul_fsgid;
1398 rr->rr_fid1 = &rec->ul_fid1;
1399 rr->rr_fid2 = &rec->ul_fid2;
1400 attr->la_ctime = rec->ul_time;
1401 attr->la_mtime = rec->ul_time;
1402 attr->la_mode = rec->ul_mode;
1403 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1405 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1409 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1411 rc = req_check_sepol(pill);
1415 rc = mdt_dlmreq_unpack(info);
1419 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1421 info->mti_spec.sp_rm_entry = 1;
1422 return mdt_unlink_unpack(info);
1425 static int mdt_rename_unpack(struct mdt_thread_info *info)
1427 struct lu_ucred *uc = mdt_ucred(info);
1428 struct mdt_rec_rename *rec;
1429 struct lu_attr *attr = &info->mti_attr.ma_attr;
1430 struct mdt_reint_record *rr = &info->mti_rr;
1431 struct req_capsule *pill = info->mti_pill;
1432 struct md_op_spec *spec = &info->mti_spec;
1437 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1438 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1442 /* This prior initialization is needed for old_init_ucred_reint() */
1443 uc->uc_fsuid = rec->rn_fsuid;
1444 uc->uc_fsgid = rec->rn_fsgid;
1445 uc->uc_cap = rec->rn_cap;
1446 uc->uc_suppgids[0] = rec->rn_suppgid1;
1447 uc->uc_suppgids[1] = rec->rn_suppgid2;
1449 attr->la_uid = rec->rn_fsuid;
1450 attr->la_gid = rec->rn_fsgid;
1451 rr->rr_fid1 = &rec->rn_fid1;
1452 rr->rr_fid2 = &rec->rn_fid2;
1453 attr->la_ctime = rec->rn_time;
1454 attr->la_mtime = rec->rn_time;
1455 /* rename_tgt contains the mode already */
1456 attr->la_mode = rec->rn_mode;
1457 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1459 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1463 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1467 spec->no_create = !!req_is_replay(mdt_info_req(info));
1469 rc = req_check_sepol(pill);
1473 rc = mdt_dlmreq_unpack(info);
1478 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1480 struct lu_ucred *uc = mdt_ucred(info);
1481 struct mdt_rec_rename *rec;
1482 struct lu_attr *attr = &info->mti_attr.ma_attr;
1483 struct mdt_reint_record *rr = &info->mti_rr;
1484 struct req_capsule *pill = info->mti_pill;
1485 struct md_op_spec *spec = &info->mti_spec;
1490 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1491 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1495 /* This prior initialization is needed for old_init_ucred_reint() */
1496 uc->uc_fsuid = rec->rn_fsuid;
1497 uc->uc_fsgid = rec->rn_fsgid;
1498 uc->uc_cap = rec->rn_cap;
1499 uc->uc_suppgids[0] = rec->rn_suppgid1;
1500 uc->uc_suppgids[1] = rec->rn_suppgid2;
1502 attr->la_uid = rec->rn_fsuid;
1503 attr->la_gid = rec->rn_fsgid;
1504 rr->rr_fid1 = &rec->rn_fid1;
1505 rr->rr_fid2 = &rec->rn_fid2;
1506 attr->la_ctime = rec->rn_time;
1507 attr->la_mtime = rec->rn_time;
1508 /* rename_tgt contains the mode already */
1509 attr->la_mode = rec->rn_mode;
1510 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1511 spec->sp_cr_flags = 0;
1513 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1517 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1518 rc = mdt_close_handle_unpack(info);
1522 spec->sp_migrate_close = 1;
1525 /* lustre version > 2.11 migration packs lum */
1526 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1527 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1528 rr->rr_eadatalen = req_capsule_get_size(pill,
1532 if (rr->rr_eadatalen > 0) {
1533 rr->rr_eadata = req_capsule_client_get(pill,
1535 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1536 spec->u.sp_ea.eadata = rr->rr_eadata;
1537 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1540 /* old client doesn't provide lum. */
1541 RETURN(-EOPNOTSUPP);
1545 spec->no_create = !!req_is_replay(mdt_info_req(info));
1547 rc = mdt_dlmreq_unpack(info);
1553 * please see comment above LOV_MAGIC_V1_DEFINED
1555 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1557 struct lov_user_md_v1 *v1 = eadata;
1561 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1562 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1563 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1564 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1565 __swab32(LOV_MAGIC_MAGIC))
1566 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1570 static int mdt_open_unpack(struct mdt_thread_info *info)
1572 struct lu_ucred *uc = mdt_ucred(info);
1573 struct mdt_rec_create *rec;
1574 struct lu_attr *attr = &info->mti_attr.ma_attr;
1575 struct req_capsule *pill = info->mti_pill;
1576 struct mdt_reint_record *rr = &info->mti_rr;
1577 struct ptlrpc_request *req = mdt_info_req(info);
1578 struct md_op_spec *sp = &info->mti_spec;
1582 BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1583 sizeof(struct mdt_rec_reint));
1584 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1588 /* This prior initialization is needed for old_init_ucred_reint() */
1589 uc->uc_fsuid = rec->cr_fsuid;
1590 uc->uc_fsgid = rec->cr_fsgid;
1591 uc->uc_cap = rec->cr_cap;
1592 uc->uc_suppgids[0] = rec->cr_suppgid1;
1593 uc->uc_suppgids[1] = rec->cr_suppgid2;
1594 uc->uc_umask = rec->cr_umask;
1596 rr->rr_fid1 = &rec->cr_fid1;
1597 rr->rr_fid2 = &rec->cr_fid2;
1598 rr->rr_open_handle = &rec->cr_open_handle_old;
1599 attr->la_mode = rec->cr_mode;
1600 attr->la_rdev = rec->cr_rdev;
1601 attr->la_uid = rec->cr_fsuid;
1602 attr->la_gid = rec->cr_fsgid;
1603 attr->la_ctime = rec->cr_time;
1604 attr->la_mtime = rec->cr_time;
1605 attr->la_atime = rec->cr_time;
1606 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1607 LA_CTIME | LA_MTIME | LA_ATIME;
1608 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1609 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1610 /* Do not trigger ASSERTION if client miss to set such flags. */
1611 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1614 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1616 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1618 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1619 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1622 if (rr->rr_eadatalen > 0) {
1623 rr->rr_eadata = req_capsule_client_get(pill,
1625 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1626 sp->u.sp_ea.eadata = rr->rr_eadata;
1627 sp->sp_archive_id = rec->cr_archive_id;
1628 sp->no_create = !!req_is_replay(req);
1629 mdt_fix_lov_magic(info, rr->rr_eadata);
1633 * Client default md_size may be 0 right after client start,
1634 * until all osc are connected, set here just some reasonable
1635 * value to prevent misbehavior.
1637 if (rr->rr_eadatalen == 0 &&
1638 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1639 rr->rr_eadatalen = MIN_MD_SIZE;
1642 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1643 &sp->sp_cr_file_secctx,
1644 &sp->sp_cr_file_secctx_size);
1648 rc = req_check_sepol(pill);
1655 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1657 struct mdt_reint_record *rr = &info->mti_rr;
1658 struct lu_ucred *uc = mdt_ucred(info);
1659 struct lu_attr *attr = &info->mti_attr.ma_attr;
1660 struct req_capsule *pill = info->mti_pill;
1661 struct mdt_rec_setxattr *rec;
1666 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1667 sizeof(struct mdt_rec_reint));
1668 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1672 /* This prior initialization is needed for old_init_ucred_reint() */
1673 uc->uc_fsuid = rec->sx_fsuid;
1674 uc->uc_fsgid = rec->sx_fsgid;
1675 uc->uc_cap = rec->sx_cap;
1676 uc->uc_suppgids[0] = rec->sx_suppgid1;
1677 uc->uc_suppgids[1] = -1;
1679 rr->rr_opcode = rec->sx_opcode;
1680 rr->rr_fid1 = &rec->sx_fid;
1681 attr->la_valid = rec->sx_valid;
1682 attr->la_ctime = rec->sx_time;
1683 attr->la_size = rec->sx_size;
1684 attr->la_flags = rec->sx_flags;
1686 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1690 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1691 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1694 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1697 if (rr->rr_eadatalen > 0) {
1698 rr->rr_eadata = req_capsule_client_get(pill,
1700 if (rr->rr_eadata == NULL)
1703 rr->rr_eadata = NULL;
1705 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1706 CDEBUG(D_INFO, "no xattr data supplied\n");
1710 rc = req_check_sepol(pill);
1714 if (mdt_dlmreq_unpack(info) < 0)
1720 static int mdt_resync_unpack(struct mdt_thread_info *info)
1722 struct req_capsule *pill = info->mti_pill;
1723 struct mdt_reint_record *rr = &info->mti_rr;
1724 struct lu_ucred *uc = mdt_ucred(info);
1725 struct mdt_rec_resync *rec;
1728 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1729 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1733 /* This prior initialization is needed for old_init_ucred_reint() */
1734 uc->uc_fsuid = rec->rs_fsuid;
1735 uc->uc_fsgid = rec->rs_fsgid;
1736 uc->uc_cap = rec->rs_cap;
1738 rr->rr_fid1 = &rec->rs_fid;
1739 rr->rr_mirror_id = rec->rs_mirror_id;
1741 /* cookie doesn't need to be swapped but it has been swapped
1742 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1744 if (ptlrpc_req_need_swab(mdt_info_req(info)))
1745 __swab64s(&rec->rs_lease_handle.cookie);
1746 rr->rr_lease_handle = &rec->rs_lease_handle;
1748 RETURN(mdt_dlmreq_unpack(info));
1751 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1753 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1754 [REINT_SETATTR] = mdt_setattr_unpack,
1755 [REINT_CREATE] = mdt_create_unpack,
1756 [REINT_LINK] = mdt_link_unpack,
1757 [REINT_UNLINK] = mdt_unlink_unpack,
1758 [REINT_RENAME] = mdt_rename_unpack,
1759 [REINT_OPEN] = mdt_open_unpack,
1760 [REINT_SETXATTR] = mdt_setxattr_unpack,
1761 [REINT_RMENTRY] = mdt_rmentry_unpack,
1762 [REINT_MIGRATE] = mdt_migrate_unpack,
1763 [REINT_RESYNC] = mdt_resync_unpack,
1766 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1771 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1772 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1773 info->mti_rr.rr_opcode = op;
1774 rc = mdt_reint_unpackers[op](info);
1776 CERROR("Unexpected opcode %d\n", op);
1782 void mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1783 struct mdt_object *child)
1786 struct lu_buf *buffer;
1787 struct mdt_body *repbody;
1788 struct req_capsule *pill = info->mti_pill;
1791 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1792 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1794 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1795 buffer = &info->mti_buf;
1797 /* fill reply buffer with security context now */
1798 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1800 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1802 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1803 buffer, secctx_name);
1806 "found security context of size %d for "DFID"\n",
1807 rc, PFID(mdt_object_fid(child)));
1809 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1810 repbody->mbo_valid |= OBD_MD_SECCTX;
1811 if (rc < buffer->lb_len)
1812 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1816 "security context not found for "DFID": rc = %d\n",
1817 PFID(mdt_object_fid(child)), rc);
1818 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,