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 #ifdef HAVE_UIDGID_HEADER
49 # include <linux/uidgid.h>
51 #include "mdt_internal.h"
52 #include <uapi/linux/lnet/nidstr.h>
53 #include <lustre_nodemap.h>
55 typedef enum ucred_init_type {
61 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
63 return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
66 void mdt_exit_ucred(struct mdt_thread_info *info)
68 struct lu_ucred *uc = mdt_ucred(info);
69 struct mdt_device *mdt = info->mti_mdt;
72 if (uc->uc_valid != UCRED_INIT) {
73 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
75 put_group_info(uc->uc_ginfo);
78 if (uc->uc_identity) {
79 mdt_identity_put(mdt->mdt_identity_cache,
81 uc->uc_identity = NULL;
83 uc->uc_valid = UCRED_INIT;
87 static int match_nosquash_list(struct rw_semaphore *sem,
88 struct list_head *nidlist,
94 rc = cfs_match_nid(peernid, nidlist);
99 /* root_squash for inter-MDS operations */
100 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
102 struct lu_ucred *ucred = mdt_ucred(info);
103 struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
106 LASSERT(ucred != NULL);
107 if (!squash->rsi_uid || ucred->uc_fsuid)
110 if (match_nosquash_list(&squash->rsi_sem,
111 &squash->rsi_nosquash_nids,
113 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
114 libcfs_nid2str(peernid));
118 CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
119 libcfs_nid2str(peernid),
120 ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
121 squash->rsi_uid, squash->rsi_gid, 0);
123 ucred->uc_fsuid = squash->rsi_uid;
124 ucred->uc_fsgid = squash->rsi_gid;
126 ucred->uc_suppgids[0] = -1;
127 ucred->uc_suppgids[1] = -1;
132 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
134 struct ptlrpc_request *req = mdt_info_req(info);
135 const char *jobid = mdt_req_get_jobid(req);
137 /* set jobid if specified. */
139 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
141 uc->uc_jobid[0] = '\0';
144 static void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
146 if (info && info->mti_exp && info->mti_exp->exp_connection)
147 uc->uc_nid = info->mti_exp->exp_connection->c_peer.nid;
149 uc->uc_nid = LNET_NID_ANY;
152 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
155 struct lu_nodemap *nodemap = NULL;
158 if (info && info->mti_exp) {
159 nodemap = nodemap_get_from_exp(info->mti_exp);
160 if (nodemap && !IS_ERR(nodemap)) {
161 audit = nodemap->nmf_enable_audit;
162 nodemap_putref(nodemap);
166 uc->uc_enable_audit = audit;
169 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
170 void *buf, bool drop_fs_cap)
172 struct ptlrpc_request *req = mdt_info_req(info);
173 struct mdt_device *mdt = info->mti_mdt;
174 struct ptlrpc_user_desc *pud = req->rq_user_desc;
175 struct lu_ucred *ucred = mdt_ucred(info);
176 struct lu_nodemap *nodemap;
177 lnet_nid_t peernid = req->rq_peer.nid;
181 bool is_nm_gid_squashed = false;
186 LASSERT(req->rq_auth_gss);
187 LASSERT(!req->rq_auth_usr_mdt);
188 LASSERT(req->rq_user_desc);
189 LASSERT(ucred != NULL);
191 ucred->uc_valid = UCRED_INVALID;
193 nodemap = nodemap_get_from_exp(info->mti_exp);
195 RETURN(PTR_ERR(nodemap));
197 pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
198 NODEMAP_CLIENT_TO_FS, pud->pud_uid);
199 pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
200 NODEMAP_CLIENT_TO_FS, pud->pud_gid);
201 pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
202 NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
203 pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
204 NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
206 ucred->uc_o_uid = pud->pud_uid;
207 ucred->uc_o_gid = pud->pud_gid;
208 ucred->uc_o_fsuid = pud->pud_fsuid;
209 ucred->uc_o_fsgid = pud->pud_fsgid;
211 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
212 /* deny access before we get identity ref */
213 if (nodemap->nmf_deny_unknown) {
214 nodemap_putref(nodemap);
218 ucred->uc_fsuid = nodemap->nm_squash_uid;
219 ucred->uc_fsgid = nodemap->nm_squash_gid;
221 ucred->uc_suppgids[0] = -1;
222 ucred->uc_suppgids[1] = -1;
225 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
226 is_nm_gid_squashed = true;
228 nodemap_putref(nodemap);
230 if (type == BODY_INIT) {
231 struct mdt_body *body = (struct mdt_body *)buf;
233 ucred->uc_suppgids[0] = body->mbo_suppgid;
234 ucred->uc_suppgids[1] = -1;
237 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
238 req->rq_auth_uid != pud->pud_uid) {
239 CDEBUG(D_SEC, "local client %s: auth uid %u "
240 "while client claims %u:%u/%u:%u\n",
241 libcfs_nid2str(peernid), req->rq_auth_uid,
242 pud->pud_uid, pud->pud_gid,
243 pud->pud_fsuid, pud->pud_fsgid);
247 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
248 ucred->uc_identity = NULL;
249 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
251 struct md_identity *identity;
253 identity = mdt_identity_get(mdt->mdt_identity_cache,
255 if (IS_ERR(identity)) {
256 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
257 ucred->uc_identity = NULL;
258 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
262 "Deny access without identity: uid %u\n",
267 ucred->uc_identity = identity;
268 perm = mdt_identity_get_perm(ucred->uc_identity,
273 /* find out the setuid/setgid attempt */
274 setuid = (pud->pud_uid != pud->pud_fsuid);
275 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
276 (ucred->uc_identity &&
277 (pud->pud_gid != ucred->uc_identity->mi_gid)));
279 /* check permission of setuid */
280 if (setuid && !(perm & CFS_SETUID_PERM)) {
281 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
282 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
283 GOTO(out, rc = -EACCES);
286 /* check permission of setgid */
287 if (setgid && !(perm & CFS_SETGID_PERM)) {
288 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
289 "from %s\n", pud->pud_uid, pud->pud_gid,
290 pud->pud_fsuid, pud->pud_fsgid,
291 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
292 GOTO(out, rc = -EACCES);
295 if (perm & CFS_SETGRP_PERM) {
296 /* only set groups if GID is not squashed */
297 if (pud->pud_ngroups && !is_nm_gid_squashed) {
298 /* setgroups for local client */
299 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
300 if (!ucred->uc_ginfo) {
301 CERROR("failed to alloc %d groups\n",
303 GOTO(out, rc = -ENOMEM);
306 lustre_groups_from_list(ucred->uc_ginfo,
308 lustre_groups_sort(ucred->uc_ginfo);
310 ucred->uc_suppgids[0] = -1;
311 ucred->uc_suppgids[1] = -1;
312 ucred->uc_ginfo = NULL;
315 ucred->uc_suppgids[0] = -1;
316 ucred->uc_suppgids[1] = -1;
317 ucred->uc_ginfo = NULL;
320 ucred->uc_uid = pud->pud_uid;
321 ucred->uc_gid = pud->pud_gid;
322 ucred->uc_fsuid = pud->pud_fsuid;
323 ucred->uc_fsgid = pud->pud_fsgid;
325 /* process root_squash here. */
326 mdt_root_squash(info, peernid);
328 /* remove fs privilege for non-root user. */
329 if (ucred->uc_fsuid && drop_fs_cap)
330 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
332 ucred->uc_cap = pud->pud_cap;
333 ucred->uc_valid = UCRED_NEW;
334 ucred_set_jobid(info, ucred);
335 ucred_set_nid(info, ucred);
336 ucred_set_audit_enabled(info, ucred);
342 if (ucred->uc_ginfo) {
343 put_group_info(ucred->uc_ginfo);
344 ucred->uc_ginfo = NULL;
346 if (ucred->uc_identity) {
347 mdt_identity_put(mdt->mdt_identity_cache,
349 ucred->uc_identity = NULL;
357 * Check whether allow the client to set supplementary group IDs or not.
359 * \param[in] info pointer to the thread context
360 * \param[in] uc pointer to the RPC user descriptor
362 * \retval true if allow to set supplementary group IDs
363 * \retval false for other cases
365 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
369 /* 1. If identity_upcall is disabled,
370 * permit local client to do anything. */
371 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
374 /* 2. If fail to get related identities, then forbid any client to
375 * set supplementary group IDs. */
376 if (uc->uc_identity == NULL)
379 /* 3. Check the permission in the identities. */
380 perm = mdt_identity_get_perm(uc->uc_identity,
381 mdt_info_req(info)->rq_peer.nid);
382 if (perm & CFS_SETGRP_PERM)
388 int mdt_check_ucred(struct mdt_thread_info *info)
390 struct ptlrpc_request *req = mdt_info_req(info);
391 struct mdt_device *mdt = info->mti_mdt;
392 struct ptlrpc_user_desc *pud = req->rq_user_desc;
393 struct lu_ucred *ucred = mdt_ucred(info);
394 struct md_identity *identity = NULL;
395 lnet_nid_t peernid = req->rq_peer.nid;
403 LASSERT(ucred != NULL);
404 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
407 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
410 /* sanity check: if we use strong authentication, we expect the
411 * uid which client claimed is true */
412 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
413 req->rq_auth_uid != pud->pud_uid) {
414 CDEBUG(D_SEC, "local client %s: auth uid %u "
415 "while client claims %u:%u/%u:%u\n",
416 libcfs_nid2str(peernid), req->rq_auth_uid,
417 pud->pud_uid, pud->pud_gid,
418 pud->pud_fsuid, pud->pud_fsgid);
422 if (is_identity_get_disabled(mdt->mdt_identity_cache))
425 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
426 if (IS_ERR(identity)) {
427 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
430 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
436 perm = mdt_identity_get_perm(identity, peernid);
437 /* find out the setuid/setgid attempt */
438 setuid = (pud->pud_uid != pud->pud_fsuid);
439 setgid = (pud->pud_gid != pud->pud_fsgid ||
440 pud->pud_gid != identity->mi_gid);
442 /* check permission of setuid */
443 if (setuid && !(perm & CFS_SETUID_PERM)) {
444 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
445 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
446 GOTO(out, rc = -EACCES);
449 /* check permission of setgid */
450 if (setgid && !(perm & CFS_SETGID_PERM)) {
451 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
452 "from %s\n", pud->pud_uid, pud->pud_gid,
453 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
454 libcfs_nid2str(peernid));
455 GOTO(out, rc = -EACCES);
461 mdt_identity_put(mdt->mdt_identity_cache, identity);
465 static int old_init_ucred_common(struct mdt_thread_info *info,
466 struct lu_nodemap *nodemap,
469 struct lu_ucred *uc = mdt_ucred(info);
470 struct mdt_device *mdt = info->mti_mdt;
471 struct md_identity *identity = NULL;
473 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
474 /* deny access before we get identity ref */
475 if (nodemap->nmf_deny_unknown)
478 uc->uc_fsuid = nodemap->nm_squash_uid;
479 uc->uc_fsgid = nodemap->nm_squash_gid;
481 uc->uc_suppgids[0] = -1;
482 uc->uc_suppgids[1] = -1;
485 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
486 identity = mdt_identity_get(mdt->mdt_identity_cache,
488 if (IS_ERR(identity)) {
489 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
490 uc->uc_cap & CFS_CAP_FS_MASK)) {
493 CDEBUG(D_SEC, "Deny access without identity: "
494 "uid %u\n", uc->uc_fsuid);
499 uc->uc_identity = identity;
501 /* process root_squash here. */
502 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
504 /* remove fs privilege for non-root user. */
505 if (uc->uc_fsuid && drop_fs_cap)
506 uc->uc_cap &= ~CFS_CAP_FS_MASK;
507 uc->uc_valid = UCRED_OLD;
508 ucred_set_jobid(info, uc);
509 ucred_set_nid(info, uc);
510 ucred_set_audit_enabled(info, uc);
517 static int old_init_ucred(struct mdt_thread_info *info,
518 struct mdt_body *body, bool drop_fs_cap)
520 struct lu_ucred *uc = mdt_ucred(info);
521 struct lu_nodemap *nodemap;
525 nodemap = nodemap_get_from_exp(info->mti_exp);
527 RETURN(PTR_ERR(nodemap));
529 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
530 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
531 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
532 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
533 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
534 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
535 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
536 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
539 uc->uc_valid = UCRED_INVALID;
540 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
541 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
542 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
543 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
544 uc->uc_suppgids[0] = body->mbo_suppgid;
545 uc->uc_suppgids[1] = -1;
547 uc->uc_cap = body->mbo_capability;
549 rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
550 nodemap_putref(nodemap);
555 static int old_init_ucred_reint(struct mdt_thread_info *info)
557 struct lu_ucred *uc = mdt_ucred(info);
558 struct lu_nodemap *nodemap;
562 nodemap = nodemap_get_from_exp(info->mti_exp);
564 RETURN(PTR_ERR(nodemap));
568 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
569 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
570 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
571 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
573 uc->uc_valid = UCRED_INVALID;
574 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
575 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
578 rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
579 nodemap_putref(nodemap);
584 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
585 struct mdt_body *body,
588 struct ptlrpc_request *req = mdt_info_req(info);
589 struct lu_ucred *uc = mdt_ucred(info);
592 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
595 mdt_exit_ucred(info);
597 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
598 return old_init_ucred(info, body, drop_fs_cap);
600 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
603 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
605 return __mdt_init_ucred(info, body, true);
608 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
609 * doesn't set correct fsuid explicitely, but raise capability to allow
610 * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
611 * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
612 * will fail permission check. */
613 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
614 struct mdt_body *body)
616 return __mdt_init_ucred(info, body, false);
619 int mdt_init_ucred_reint(struct mdt_thread_info *info)
621 struct ptlrpc_request *req = mdt_info_req(info);
622 struct lu_ucred *uc = mdt_ucred(info);
623 struct md_attr *ma = &info->mti_attr;
626 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
629 /* LU-5564: for normal close request, skip permission check */
630 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
631 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
632 uc->uc_cap |= CFS_CAP_FS_MASK;
634 mdt_exit_ucred(info);
636 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
637 return old_init_ucred_reint(info);
639 return new_init_ucred(info, REC_INIT, NULL, true);
642 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
643 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
645 const struct lov_ost_data_v1 *lod;
646 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
650 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
653 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
654 POSTID(&lmm->lmm_oi), lmm_magic,
655 le32_to_cpu(lmm->lmm_pattern));
657 /* No support for compount layouts yet */
658 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
661 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
662 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
663 le32_to_cpu(lmm->lmm_stripe_size), count);
665 /* If it's a directory or a released file, then there are
666 * no actual objects to print, so bail out. */
667 if (valid & OBD_MD_FLDIREA ||
668 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
671 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
672 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
675 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
676 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
677 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
681 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
683 const struct lmv_mds_md_v1 *lmm1;
684 const struct lmv_foreign_md *lfm;
687 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
690 /* foreign LMV case */
691 lfm = &lmv->lmv_foreign_md;
692 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
694 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
695 le32_to_cpu(lfm->lfm_magic),
696 le32_to_cpu(lfm->lfm_length),
697 le32_to_cpu(lfm->lfm_type),
698 le32_to_cpu(lfm->lfm_flags),
699 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
703 lmm1 = &lmv->lmv_md_v1;
705 "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
706 le32_to_cpu(lmm1->lmv_magic),
707 le32_to_cpu(lmm1->lmv_master_mdt_index),
708 le32_to_cpu(lmm1->lmv_stripe_count),
709 le32_to_cpu(lmm1->lmv_hash_type));
711 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
714 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
717 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
718 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
722 /* Shrink and/or grow reply buffers */
723 int mdt_fix_reply(struct mdt_thread_info *info)
725 struct req_capsule *pill = info->mti_pill;
726 struct mdt_body *body;
727 int md_size, md_packed = 0;
732 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
733 LASSERT(body != NULL);
735 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
737 md_size = body->mbo_eadatasize;
741 acl_size = body->mbo_aclsize;
743 /* this replay - not send info to client */
744 if (info->mti_spec.no_create) {
749 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
754 &RMF_ACL, or &RMF_LOGCOOKIES
755 (optional) &RMF_CAPA1,
756 (optional) &RMF_CAPA2,
757 (optional) something else
760 /* MDT_MD buffer may be bigger than packed value, let's shrink all
761 * buffers before growing it */
762 if (info->mti_big_lmm_used) {
763 /* big_lmm buffer may be used even without packing the result
764 * into reply, just for internal server needs */
765 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
766 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
769 /* free big lmm if md_size is not needed */
770 if (md_size == 0 || md_packed == 0) {
771 info->mti_big_lmm_used = 0;
773 /* buffer must be allocated separately */
774 LASSERT(info->mti_attr.ma_lmm !=
775 req_capsule_server_get(pill, &RMF_MDT_MD));
776 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
778 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
779 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
782 if (info->mti_big_acl_used) {
784 info->mti_big_acl_used = 0;
786 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
787 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
788 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
789 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
790 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
794 * Some more field should be shrinked if needed.
795 * This should be done by those who added fields to reply message.
798 /* Grow MD buffer if needed finally */
799 if (info->mti_big_lmm_used) {
802 LASSERT(md_size > md_packed);
803 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
804 md_size - md_packed);
805 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
807 /* we can't answer with proper LOV EA, drop flags,
808 * the rc is also returned so this request is
809 * considered as failed */
810 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
811 /* don't return transno along with error */
812 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
814 /* now we need to pack right LOV/LMV EA */
815 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
816 if (info->mti_attr.ma_valid & MA_LOV) {
817 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
819 info->mti_attr.ma_lmm_size);
820 memcpy(lmm, info->mti_attr.ma_lmm,
821 info->mti_attr.ma_lmm_size);
822 } else if (info->mti_attr.ma_valid & MA_LMV) {
823 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
825 info->mti_attr.ma_lmv_size);
826 memcpy(lmm, info->mti_attr.ma_lmv,
827 info->mti_attr.ma_lmv_size);
831 /* update mdt_max_mdsize so clients will be aware about that */
832 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
833 info->mti_mdt->mdt_max_mdsize =
834 info->mti_attr.ma_lmm_size;
835 info->mti_big_lmm_used = 0;
838 if (info->mti_big_acl_used) {
839 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
842 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
844 body->mbo_valid &= ~OBD_MD_FLACL;
846 void *acl = req_capsule_server_get(pill, &RMF_ACL);
848 memcpy(acl, info->mti_big_acl, acl_size);
851 info->mti_big_acl_used = 0;
858 /* if object is dying, pack the lov/llog data,
859 * parameter info->mti_attr should be valid at this point!
860 * Also implements RAoLU policy */
861 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
864 struct mdt_body *repbody = NULL;
865 const struct lu_attr *la = &ma->ma_attr;
866 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
869 struct hsm_action_item hai = {
870 .hai_len = sizeof(hai),
871 .hai_action = HSMA_REMOVE,
872 .hai_extent.length = -1,
880 if (mdt_info_req(info) != NULL) {
881 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
882 LASSERT(repbody != NULL);
884 CDEBUG(D_INFO, "not running in a request/reply context\n");
887 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
888 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
890 if (ma->ma_valid & MA_LOV) {
891 CERROR("No need in LOV EA upon unlink\n");
895 repbody->mbo_eadatasize = 0;
897 /* Only check unlinked and archived if RAoLU and upon last close */
898 if (!cdt->cdt_remove_archive_on_last_unlink ||
899 atomic_read(&mo->mot_open_count) != 0)
902 /* mdt_attr_get_complex will clear ma_valid, so check here first */
903 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
906 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
909 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
911 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
912 * try setting them */
914 rc = mdt_attr_get_complex(info, mo, ma);
916 CERROR("%s: unable to fetch missing attributes of"
917 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
918 PFID(mdt_object_fid(mo)), rc);
922 if (need & MA_INODE) {
923 if (ma->ma_valid & MA_INODE) {
924 if (ma->ma_attr.la_nlink != 0)
932 if (ma->ma_valid & MA_HSM) {
933 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
941 /* RAoLU policy is active, last close on file has occured,
942 * file is unlinked, file is archived, so create remove request
944 * If CDT is not running, requests will be logged for later. */
945 if (ma->ma_hsm.mh_arch_id != 0)
946 archive_id = ma->ma_hsm.mh_arch_id;
948 archive_id = cdt->cdt_default_archive_id;
950 hai.hai_fid = *mdt_object_fid(mo);
952 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
955 CERROR("%s: unable to add HSM remove request for "DFID
956 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
957 PFID(mdt_object_fid(mo)), rc);
962 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
968 if (in & MDS_ATTR_MODE)
970 if (in & MDS_ATTR_UID)
972 if (in & MDS_ATTR_GID)
974 if (in & MDS_ATTR_SIZE)
976 if (in & MDS_ATTR_BLOCKS)
978 if (in & MDS_ATTR_ATIME_SET)
980 if (in & MDS_ATTR_CTIME_SET)
982 if (in & MDS_ATTR_MTIME_SET)
984 if (in & MDS_ATTR_ATTR_FLAG)
986 if (in & MDS_ATTR_KILL_SUID)
988 if (in & MDS_ATTR_KILL_SGID)
990 if (in & MDS_ATTR_PROJID)
992 if (in & MDS_ATTR_LSIZE)
994 if (in & MDS_ATTR_LBLOCKS)
997 if (in & MDS_ATTR_FROM_OPEN)
998 rr->rr_flags |= MRF_OPEN_TRUNC;
999 if (in & MDS_ATTR_OVERRIDE)
1000 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1001 if (in & MDS_ATTR_FORCE)
1002 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1004 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1005 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1006 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1007 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1008 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1009 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1012 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", in);
1018 int mdt_name_unpack(struct req_capsule *pill,
1019 const struct req_msg_field *field,
1021 enum mdt_name_flags flags)
1023 ln->ln_name = req_capsule_client_get(pill, field);
1024 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1026 if (!lu_name_is_valid(ln)) {
1033 if ((flags & MNF_FIX_ANON) &&
1034 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1035 /* Newer (3.x) kernels use a name of "/" for the
1036 * "anonymous" disconnected dentries from NFS
1037 * filehandle conversion. See d_obtain_alias(). */
1045 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1046 const char **secctx_name,
1047 void **secctx, size_t *secctx_size)
1052 *secctx_name = NULL;
1056 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1057 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1060 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1065 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1066 if (strnlen(name, name_size) != name_size - 1)
1069 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1070 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1073 *secctx_name = name;
1074 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1075 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1080 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1082 struct lu_ucred *uc = mdt_ucred(info);
1083 struct md_attr *ma = &info->mti_attr;
1084 struct lu_attr *la = &ma->ma_attr;
1085 struct req_capsule *pill = info->mti_pill;
1086 struct mdt_reint_record *rr = &info->mti_rr;
1087 struct mdt_rec_setattr *rec;
1088 struct lu_nodemap *nodemap;
1092 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1093 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1097 /* This prior initialization is needed for old_init_ucred_reint() */
1098 uc->uc_fsuid = rec->sa_fsuid;
1099 uc->uc_fsgid = rec->sa_fsgid;
1100 uc->uc_cap = rec->sa_cap;
1101 uc->uc_suppgids[0] = rec->sa_suppgid;
1102 uc->uc_suppgids[1] = -1;
1104 rr->rr_fid1 = &rec->sa_fid;
1105 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1106 la->la_mode = rec->sa_mode;
1107 la->la_flags = rec->sa_attr_flags;
1109 nodemap = nodemap_get_from_exp(info->mti_exp);
1110 if (IS_ERR(nodemap))
1111 RETURN(PTR_ERR(nodemap));
1113 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1114 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1115 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1116 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1117 la->la_projid = rec->sa_projid;
1118 nodemap_putref(nodemap);
1120 la->la_size = rec->sa_size;
1121 la->la_blocks = rec->sa_blocks;
1122 la->la_ctime = rec->sa_ctime;
1123 la->la_atime = rec->sa_atime;
1124 la->la_mtime = rec->sa_mtime;
1125 ma->ma_valid = MA_INODE;
1127 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1128 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE);
1132 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1134 struct req_capsule *pill = info->mti_pill;
1135 struct mdt_ioepoch *ioepoch;
1138 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1139 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1143 if (ioepoch == NULL)
1146 info->mti_open_handle = ioepoch->mio_open_handle;
1151 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1152 struct req_capsule *pill = info->mti_pill;
1154 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1155 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1156 if (info->mti_dlm_req == NULL)
1163 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1165 struct mdt_reint_record *rr = &info->mti_rr;
1166 struct md_attr *ma = &info->mti_attr;
1167 struct req_capsule *pill = info->mti_pill;
1171 rc = mdt_setattr_unpack_rec(info);
1175 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1176 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1177 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1180 if (rr->rr_eadatalen > 0) {
1181 const struct lmv_user_md *lum;
1183 lum = rr->rr_eadata;
1184 /* Sigh ma_valid(from req) does not indicate whether
1185 * it will set LOV/LMV EA, so we have to check magic */
1186 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1187 ma->ma_valid |= MA_LMV;
1188 ma->ma_lmv = (void *)rr->rr_eadata;
1189 ma->ma_lmv_size = rr->rr_eadatalen;
1191 ma->ma_valid |= MA_LOV;
1192 ma->ma_lmm = (void *)rr->rr_eadata;
1193 ma->ma_lmm_size = rr->rr_eadatalen;
1198 rc = mdt_dlmreq_unpack(info);
1202 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1204 struct md_attr *ma = &info->mti_attr;
1205 struct req_capsule *pill = info->mti_pill;
1208 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1211 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1213 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1214 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1220 int mdt_close_unpack(struct mdt_thread_info *info)
1225 rc = mdt_close_handle_unpack(info);
1229 rc = mdt_setattr_unpack_rec(info);
1233 rc = mdt_close_intent_unpack(info);
1237 RETURN(mdt_init_ucred_reint(info));
1240 static int mdt_create_unpack(struct mdt_thread_info *info)
1242 struct lu_ucred *uc = mdt_ucred(info);
1243 struct mdt_rec_create *rec;
1244 struct lu_attr *attr = &info->mti_attr.ma_attr;
1245 struct mdt_reint_record *rr = &info->mti_rr;
1246 struct req_capsule *pill = info->mti_pill;
1247 struct md_op_spec *sp = &info->mti_spec;
1252 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1253 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1257 /* This prior initialization is needed for old_init_ucred_reint() */
1258 uc->uc_fsuid = rec->cr_fsuid;
1259 uc->uc_fsgid = rec->cr_fsgid;
1260 uc->uc_cap = rec->cr_cap;
1261 uc->uc_suppgids[0] = rec->cr_suppgid1;
1262 uc->uc_suppgids[1] = -1;
1263 uc->uc_umask = rec->cr_umask;
1265 rr->rr_fid1 = &rec->cr_fid1;
1266 rr->rr_fid2 = &rec->cr_fid2;
1267 attr->la_mode = rec->cr_mode;
1268 attr->la_rdev = rec->cr_rdev;
1269 attr->la_uid = rec->cr_fsuid;
1270 attr->la_gid = rec->cr_fsgid;
1271 attr->la_ctime = rec->cr_time;
1272 attr->la_mtime = rec->cr_time;
1273 attr->la_atime = rec->cr_time;
1274 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1275 LA_CTIME | LA_MTIME | LA_ATIME;
1276 memset(&sp->u, 0, sizeof(sp->u));
1277 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1279 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1283 if (S_ISLNK(attr->la_mode)) {
1284 const char *tgt = NULL;
1286 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1287 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1288 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1289 sp->u.sp_symname = tgt;
1294 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1295 if (S_ISDIR(attr->la_mode) &&
1296 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1297 sp->u.sp_ea.eadata =
1298 req_capsule_client_get(pill, &RMF_EADATA);
1299 sp->u.sp_ea.eadatalen =
1300 req_capsule_get_size(pill, &RMF_EADATA,
1302 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1306 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1307 &sp->sp_cr_file_secctx,
1308 &sp->sp_cr_file_secctx_size);
1312 rc = req_check_sepol(pill);
1316 rc = mdt_dlmreq_unpack(info);
1320 static int mdt_link_unpack(struct mdt_thread_info *info)
1322 struct lu_ucred *uc = mdt_ucred(info);
1323 struct mdt_rec_link *rec;
1324 struct lu_attr *attr = &info->mti_attr.ma_attr;
1325 struct mdt_reint_record *rr = &info->mti_rr;
1326 struct req_capsule *pill = info->mti_pill;
1331 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1332 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1336 /* This prior initialization is needed for old_init_ucred_reint() */
1337 uc->uc_fsuid = rec->lk_fsuid;
1338 uc->uc_fsgid = rec->lk_fsgid;
1339 uc->uc_cap = rec->lk_cap;
1340 uc->uc_suppgids[0] = rec->lk_suppgid1;
1341 uc->uc_suppgids[1] = rec->lk_suppgid2;
1343 attr->la_uid = rec->lk_fsuid;
1344 attr->la_gid = rec->lk_fsgid;
1345 rr->rr_fid1 = &rec->lk_fid1;
1346 rr->rr_fid2 = &rec->lk_fid2;
1347 attr->la_ctime = rec->lk_time;
1348 attr->la_mtime = rec->lk_time;
1349 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1351 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1355 rc = req_check_sepol(pill);
1359 rc = mdt_dlmreq_unpack(info);
1364 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1366 struct lu_ucred *uc = mdt_ucred(info);
1367 struct mdt_rec_unlink *rec;
1368 struct lu_attr *attr = &info->mti_attr.ma_attr;
1369 struct mdt_reint_record *rr = &info->mti_rr;
1370 struct req_capsule *pill = info->mti_pill;
1375 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1376 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1380 /* This prior initialization is needed for old_init_ucred_reint() */
1381 uc->uc_fsuid = rec->ul_fsuid;
1382 uc->uc_fsgid = rec->ul_fsgid;
1383 uc->uc_cap = rec->ul_cap;
1384 uc->uc_suppgids[0] = rec->ul_suppgid1;
1385 uc->uc_suppgids[1] = -1;
1387 attr->la_uid = rec->ul_fsuid;
1388 attr->la_gid = rec->ul_fsgid;
1389 rr->rr_fid1 = &rec->ul_fid1;
1390 rr->rr_fid2 = &rec->ul_fid2;
1391 attr->la_ctime = rec->ul_time;
1392 attr->la_mtime = rec->ul_time;
1393 attr->la_mode = rec->ul_mode;
1394 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1396 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1400 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1402 rc = req_check_sepol(pill);
1406 rc = mdt_dlmreq_unpack(info);
1410 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1412 info->mti_spec.sp_rm_entry = 1;
1413 return mdt_unlink_unpack(info);
1416 static int mdt_rename_unpack(struct mdt_thread_info *info)
1418 struct lu_ucred *uc = mdt_ucred(info);
1419 struct mdt_rec_rename *rec;
1420 struct lu_attr *attr = &info->mti_attr.ma_attr;
1421 struct mdt_reint_record *rr = &info->mti_rr;
1422 struct req_capsule *pill = info->mti_pill;
1423 struct md_op_spec *spec = &info->mti_spec;
1428 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1429 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1433 /* This prior initialization is needed for old_init_ucred_reint() */
1434 uc->uc_fsuid = rec->rn_fsuid;
1435 uc->uc_fsgid = rec->rn_fsgid;
1436 uc->uc_cap = rec->rn_cap;
1437 uc->uc_suppgids[0] = rec->rn_suppgid1;
1438 uc->uc_suppgids[1] = rec->rn_suppgid2;
1440 attr->la_uid = rec->rn_fsuid;
1441 attr->la_gid = rec->rn_fsgid;
1442 rr->rr_fid1 = &rec->rn_fid1;
1443 rr->rr_fid2 = &rec->rn_fid2;
1444 attr->la_ctime = rec->rn_time;
1445 attr->la_mtime = rec->rn_time;
1446 /* rename_tgt contains the mode already */
1447 attr->la_mode = rec->rn_mode;
1448 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1450 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1454 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1458 spec->no_create = !!req_is_replay(mdt_info_req(info));
1460 rc = req_check_sepol(pill);
1464 rc = mdt_dlmreq_unpack(info);
1469 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1471 struct lu_ucred *uc = mdt_ucred(info);
1472 struct mdt_rec_rename *rec;
1473 struct lu_attr *attr = &info->mti_attr.ma_attr;
1474 struct mdt_reint_record *rr = &info->mti_rr;
1475 struct req_capsule *pill = info->mti_pill;
1476 struct md_op_spec *spec = &info->mti_spec;
1481 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1482 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1486 /* This prior initialization is needed for old_init_ucred_reint() */
1487 uc->uc_fsuid = rec->rn_fsuid;
1488 uc->uc_fsgid = rec->rn_fsgid;
1489 uc->uc_cap = rec->rn_cap;
1490 uc->uc_suppgids[0] = rec->rn_suppgid1;
1491 uc->uc_suppgids[1] = rec->rn_suppgid2;
1493 attr->la_uid = rec->rn_fsuid;
1494 attr->la_gid = rec->rn_fsgid;
1495 rr->rr_fid1 = &rec->rn_fid1;
1496 rr->rr_fid2 = &rec->rn_fid2;
1497 attr->la_ctime = rec->rn_time;
1498 attr->la_mtime = rec->rn_time;
1499 /* rename_tgt contains the mode already */
1500 attr->la_mode = rec->rn_mode;
1501 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1503 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1507 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1508 rc = mdt_close_handle_unpack(info);
1512 spec->sp_migrate_close = 1;
1515 /* lustre version > 2.11 migration packs lum */
1516 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1517 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1518 rr->rr_eadatalen = req_capsule_get_size(pill,
1522 if (rr->rr_eadatalen > 0) {
1523 rr->rr_eadata = req_capsule_client_get(pill,
1525 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1526 spec->u.sp_ea.eadata = rr->rr_eadata;
1527 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1530 /* old client doesn't provide lum. */
1531 RETURN(-EOPNOTSUPP);
1535 spec->no_create = !!req_is_replay(mdt_info_req(info));
1537 rc = mdt_dlmreq_unpack(info);
1543 * please see comment above LOV_MAGIC_V1_DEFINED
1545 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1547 struct lov_user_md_v1 *v1 = eadata;
1551 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1552 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1553 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1554 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1555 __swab32(LOV_MAGIC_MAGIC))
1556 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1560 static int mdt_open_unpack(struct mdt_thread_info *info)
1562 struct lu_ucred *uc = mdt_ucred(info);
1563 struct mdt_rec_create *rec;
1564 struct lu_attr *attr = &info->mti_attr.ma_attr;
1565 struct req_capsule *pill = info->mti_pill;
1566 struct mdt_reint_record *rr = &info->mti_rr;
1567 struct ptlrpc_request *req = mdt_info_req(info);
1568 struct md_op_spec *sp = &info->mti_spec;
1572 CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1573 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1577 /* This prior initialization is needed for old_init_ucred_reint() */
1578 uc->uc_fsuid = rec->cr_fsuid;
1579 uc->uc_fsgid = rec->cr_fsgid;
1580 uc->uc_cap = rec->cr_cap;
1581 uc->uc_suppgids[0] = rec->cr_suppgid1;
1582 uc->uc_suppgids[1] = rec->cr_suppgid2;
1583 uc->uc_umask = rec->cr_umask;
1585 rr->rr_fid1 = &rec->cr_fid1;
1586 rr->rr_fid2 = &rec->cr_fid2;
1587 rr->rr_open_handle = &rec->cr_open_handle_old;
1588 attr->la_mode = rec->cr_mode;
1589 attr->la_rdev = rec->cr_rdev;
1590 attr->la_uid = rec->cr_fsuid;
1591 attr->la_gid = rec->cr_fsgid;
1592 attr->la_ctime = rec->cr_time;
1593 attr->la_mtime = rec->cr_time;
1594 attr->la_atime = rec->cr_time;
1595 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1596 LA_CTIME | LA_MTIME | LA_ATIME;
1597 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1598 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1599 /* Do not trigger ASSERTION if client miss to set such flags. */
1600 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1603 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1605 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1607 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1608 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1611 if (rr->rr_eadatalen > 0) {
1612 rr->rr_eadata = req_capsule_client_get(pill,
1614 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1615 sp->u.sp_ea.eadata = rr->rr_eadata;
1616 sp->no_create = !!req_is_replay(req);
1617 mdt_fix_lov_magic(info, rr->rr_eadata);
1621 * Client default md_size may be 0 right after client start,
1622 * until all osc are connected, set here just some reasonable
1623 * value to prevent misbehavior.
1625 if (rr->rr_eadatalen == 0 &&
1626 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1627 rr->rr_eadatalen = MIN_MD_SIZE;
1630 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1631 &sp->sp_cr_file_secctx,
1632 &sp->sp_cr_file_secctx_size);
1636 rc = req_check_sepol(pill);
1643 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1645 struct mdt_reint_record *rr = &info->mti_rr;
1646 struct lu_ucred *uc = mdt_ucred(info);
1647 struct lu_attr *attr = &info->mti_attr.ma_attr;
1648 struct req_capsule *pill = info->mti_pill;
1649 struct mdt_rec_setxattr *rec;
1654 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1655 sizeof(struct mdt_rec_reint));
1657 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1661 /* This prior initialization is needed for old_init_ucred_reint() */
1662 uc->uc_fsuid = rec->sx_fsuid;
1663 uc->uc_fsgid = rec->sx_fsgid;
1664 uc->uc_cap = rec->sx_cap;
1665 uc->uc_suppgids[0] = rec->sx_suppgid1;
1666 uc->uc_suppgids[1] = -1;
1668 rr->rr_opcode = rec->sx_opcode;
1669 rr->rr_fid1 = &rec->sx_fid;
1670 attr->la_valid = rec->sx_valid;
1671 attr->la_ctime = rec->sx_time;
1672 attr->la_size = rec->sx_size;
1673 attr->la_flags = rec->sx_flags;
1675 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1679 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1680 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1683 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1686 if (rr->rr_eadatalen > 0) {
1687 rr->rr_eadata = req_capsule_client_get(pill,
1689 if (rr->rr_eadata == NULL)
1692 rr->rr_eadata = NULL;
1694 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1695 CDEBUG(D_INFO, "no xattr data supplied\n");
1699 rc = req_check_sepol(pill);
1703 if (mdt_dlmreq_unpack(info) < 0)
1709 static int mdt_resync_unpack(struct mdt_thread_info *info)
1711 struct req_capsule *pill = info->mti_pill;
1712 struct mdt_reint_record *rr = &info->mti_rr;
1713 struct lu_ucred *uc = mdt_ucred(info);
1714 struct mdt_rec_resync *rec;
1717 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1718 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1722 /* This prior initialization is needed for old_init_ucred_reint() */
1723 uc->uc_fsuid = rec->rs_fsuid;
1724 uc->uc_fsgid = rec->rs_fsgid;
1725 uc->uc_cap = rec->rs_cap;
1727 rr->rr_fid1 = &rec->rs_fid;
1728 rr->rr_mirror_id = rec->rs_mirror_id;
1730 /* cookie doesn't need to be swapped but it has been swapped
1731 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1733 if (ptlrpc_req_need_swab(mdt_info_req(info)))
1734 __swab64s(&rec->rs_lease_handle.cookie);
1735 rr->rr_lease_handle = &rec->rs_lease_handle;
1737 RETURN(mdt_dlmreq_unpack(info));
1740 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1742 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1743 [REINT_SETATTR] = mdt_setattr_unpack,
1744 [REINT_CREATE] = mdt_create_unpack,
1745 [REINT_LINK] = mdt_link_unpack,
1746 [REINT_UNLINK] = mdt_unlink_unpack,
1747 [REINT_RENAME] = mdt_rename_unpack,
1748 [REINT_OPEN] = mdt_open_unpack,
1749 [REINT_SETXATTR] = mdt_setxattr_unpack,
1750 [REINT_RMENTRY] = mdt_rmentry_unpack,
1751 [REINT_MIGRATE] = mdt_migrate_unpack,
1752 [REINT_RESYNC] = mdt_resync_unpack,
1755 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1760 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1761 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1762 info->mti_rr.rr_opcode = op;
1763 rc = mdt_reint_unpackers[op](info);
1765 CERROR("Unexpected opcode %d\n", op);
1771 void mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1772 struct mdt_object *child)
1775 struct lu_buf *buffer;
1776 struct mdt_body *repbody;
1777 struct req_capsule *pill = info->mti_pill;
1780 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1781 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1783 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1784 buffer = &info->mti_buf;
1786 /* fill reply buffer with security context now */
1787 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1789 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1791 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1792 buffer, secctx_name);
1795 "found security context of size %d for "DFID"\n",
1796 rc, PFID(mdt_object_fid(child)));
1798 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1799 repbody->mbo_valid |= OBD_MD_SECCTX;
1800 if (rc < buffer->lb_len)
1801 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1805 "security context not found for "DFID": rc = %d\n",
1806 PFID(mdt_object_fid(child)), rc);
1807 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,