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_suppgids[0] = -1;
219 ucred->uc_suppgids[1] = -1;
222 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
223 is_nm_gid_squashed = true;
225 nodemap_putref(nodemap);
227 if (type == BODY_INIT) {
228 struct mdt_body *body = (struct mdt_body *)buf;
230 ucred->uc_suppgids[0] = body->mbo_suppgid;
231 ucred->uc_suppgids[1] = -1;
234 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
235 req->rq_auth_uid != pud->pud_uid) {
236 CDEBUG(D_SEC, "local client %s: auth uid %u "
237 "while client claims %u:%u/%u:%u\n",
238 libcfs_nid2str(peernid), req->rq_auth_uid,
239 pud->pud_uid, pud->pud_gid,
240 pud->pud_fsuid, pud->pud_fsgid);
244 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
245 ucred->uc_identity = NULL;
246 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
248 struct md_identity *identity;
250 identity = mdt_identity_get(mdt->mdt_identity_cache,
252 if (IS_ERR(identity)) {
253 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
254 ucred->uc_identity = NULL;
255 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
259 "Deny access without identity: uid %u\n",
264 ucred->uc_identity = identity;
265 perm = mdt_identity_get_perm(ucred->uc_identity,
270 /* find out the setuid/setgid attempt */
271 setuid = (pud->pud_uid != pud->pud_fsuid);
272 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
273 (ucred->uc_identity &&
274 (pud->pud_gid != ucred->uc_identity->mi_gid)));
276 /* check permission of setuid */
277 if (setuid && !(perm & CFS_SETUID_PERM)) {
278 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
279 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
280 GOTO(out, rc = -EACCES);
283 /* check permission of setgid */
284 if (setgid && !(perm & CFS_SETGID_PERM)) {
285 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
286 "from %s\n", pud->pud_uid, pud->pud_gid,
287 pud->pud_fsuid, pud->pud_fsgid,
288 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
289 GOTO(out, rc = -EACCES);
292 if (perm & CFS_SETGRP_PERM) {
293 /* only set groups if GID is not squashed */
294 if (pud->pud_ngroups && !is_nm_gid_squashed) {
295 /* setgroups for local client */
296 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
297 if (!ucred->uc_ginfo) {
298 CERROR("failed to alloc %d groups\n",
300 GOTO(out, rc = -ENOMEM);
303 lustre_groups_from_list(ucred->uc_ginfo,
305 lustre_groups_sort(ucred->uc_ginfo);
307 ucred->uc_suppgids[0] = -1;
308 ucred->uc_suppgids[1] = -1;
309 ucred->uc_ginfo = NULL;
312 ucred->uc_suppgids[0] = -1;
313 ucred->uc_suppgids[1] = -1;
314 ucred->uc_ginfo = NULL;
317 ucred->uc_uid = pud->pud_uid;
318 ucred->uc_gid = pud->pud_gid;
320 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
323 ucred->uc_cap = pud->pud_cap;
325 ucred->uc_fsuid = pud->pud_fsuid;
326 ucred->uc_fsgid = pud->pud_fsgid;
328 /* process root_squash here. */
329 mdt_root_squash(info, peernid);
331 /* remove fs privilege for non-root user. */
332 if (ucred->uc_fsuid && drop_fs_cap)
333 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
335 ucred->uc_cap = pud->pud_cap;
336 ucred->uc_valid = UCRED_NEW;
337 ucred_set_jobid(info, ucred);
338 ucred_set_nid(info, ucred);
339 ucred_set_audit_enabled(info, ucred);
345 if (ucred->uc_ginfo) {
346 put_group_info(ucred->uc_ginfo);
347 ucred->uc_ginfo = NULL;
349 if (ucred->uc_identity) {
350 mdt_identity_put(mdt->mdt_identity_cache,
352 ucred->uc_identity = NULL;
360 * Check whether allow the client to set supplementary group IDs or not.
362 * \param[in] info pointer to the thread context
363 * \param[in] uc pointer to the RPC user descriptor
365 * \retval true if allow to set supplementary group IDs
366 * \retval false for other cases
368 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
372 /* 1. If identity_upcall is disabled,
373 * permit local client to do anything. */
374 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
377 /* 2. If fail to get related identities, then forbid any client to
378 * set supplementary group IDs. */
379 if (uc->uc_identity == NULL)
382 /* 3. Check the permission in the identities. */
383 perm = mdt_identity_get_perm(uc->uc_identity,
384 mdt_info_req(info)->rq_peer.nid);
385 if (perm & CFS_SETGRP_PERM)
391 int mdt_check_ucred(struct mdt_thread_info *info)
393 struct ptlrpc_request *req = mdt_info_req(info);
394 struct mdt_device *mdt = info->mti_mdt;
395 struct ptlrpc_user_desc *pud = req->rq_user_desc;
396 struct lu_ucred *ucred = mdt_ucred(info);
397 struct md_identity *identity = NULL;
398 lnet_nid_t peernid = req->rq_peer.nid;
406 LASSERT(ucred != NULL);
407 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
410 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
413 /* sanity check: if we use strong authentication, we expect the
414 * uid which client claimed is true */
415 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
416 req->rq_auth_uid != pud->pud_uid) {
417 CDEBUG(D_SEC, "local client %s: auth uid %u "
418 "while client claims %u:%u/%u:%u\n",
419 libcfs_nid2str(peernid), req->rq_auth_uid,
420 pud->pud_uid, pud->pud_gid,
421 pud->pud_fsuid, pud->pud_fsgid);
425 if (is_identity_get_disabled(mdt->mdt_identity_cache))
428 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
429 if (IS_ERR(identity)) {
430 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
433 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
439 perm = mdt_identity_get_perm(identity, peernid);
440 /* find out the setuid/setgid attempt */
441 setuid = (pud->pud_uid != pud->pud_fsuid);
442 setgid = (pud->pud_gid != pud->pud_fsgid ||
443 pud->pud_gid != identity->mi_gid);
445 /* check permission of setuid */
446 if (setuid && !(perm & CFS_SETUID_PERM)) {
447 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
448 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
449 GOTO(out, rc = -EACCES);
452 /* check permission of setgid */
453 if (setgid && !(perm & CFS_SETGID_PERM)) {
454 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
455 "from %s\n", pud->pud_uid, pud->pud_gid,
456 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
457 libcfs_nid2str(peernid));
458 GOTO(out, rc = -EACCES);
464 mdt_identity_put(mdt->mdt_identity_cache, identity);
468 static int old_init_ucred_common(struct mdt_thread_info *info,
469 struct lu_nodemap *nodemap,
472 struct lu_ucred *uc = mdt_ucred(info);
473 struct mdt_device *mdt = info->mti_mdt;
474 struct md_identity *identity = NULL;
476 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
477 /* deny access before we get identity ref */
478 if (nodemap->nmf_deny_unknown)
482 uc->uc_suppgids[0] = -1;
483 uc->uc_suppgids[1] = -1;
486 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
487 identity = mdt_identity_get(mdt->mdt_identity_cache,
489 if (IS_ERR(identity)) {
490 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
491 uc->uc_cap & CFS_CAP_FS_MASK)) {
494 CDEBUG(D_SEC, "Deny access without identity: "
495 "uid %u\n", uc->uc_fsuid);
500 uc->uc_identity = identity;
502 /* process root_squash here. */
503 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
505 /* remove fs privilege for non-root user. */
506 if (uc->uc_fsuid && drop_fs_cap)
507 uc->uc_cap &= ~CFS_CAP_FS_MASK;
508 uc->uc_valid = UCRED_OLD;
509 ucred_set_jobid(info, uc);
510 ucred_set_nid(info, uc);
511 ucred_set_audit_enabled(info, uc);
518 static int old_init_ucred(struct mdt_thread_info *info,
519 struct mdt_body *body, bool drop_fs_cap)
521 struct lu_ucred *uc = mdt_ucred(info);
522 struct lu_nodemap *nodemap;
526 nodemap = nodemap_get_from_exp(info->mti_exp);
528 RETURN(PTR_ERR(nodemap));
530 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
531 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
532 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
533 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
534 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
535 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
536 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
537 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
540 uc->uc_valid = UCRED_INVALID;
541 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
542 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
543 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
544 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
545 uc->uc_suppgids[0] = body->mbo_suppgid;
546 uc->uc_suppgids[1] = -1;
548 uc->uc_cap = body->mbo_capability;
550 rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
551 nodemap_putref(nodemap);
556 static int old_init_ucred_reint(struct mdt_thread_info *info)
558 struct lu_ucred *uc = mdt_ucred(info);
559 struct lu_nodemap *nodemap;
563 nodemap = nodemap_get_from_exp(info->mti_exp);
565 RETURN(PTR_ERR(nodemap));
569 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
570 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
571 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
572 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
574 uc->uc_valid = UCRED_INVALID;
575 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
576 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
579 rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
580 nodemap_putref(nodemap);
585 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
586 struct mdt_body *body,
589 struct ptlrpc_request *req = mdt_info_req(info);
590 struct lu_ucred *uc = mdt_ucred(info);
593 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
596 mdt_exit_ucred(info);
598 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
599 return old_init_ucred(info, body, drop_fs_cap);
601 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
604 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
606 return __mdt_init_ucred(info, body, true);
609 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
610 * doesn't set correct fsuid explicitely, but raise capability to allow
611 * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
612 * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
613 * will fail permission check. */
614 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
615 struct mdt_body *body)
617 return __mdt_init_ucred(info, body, false);
620 int mdt_init_ucred_reint(struct mdt_thread_info *info)
622 struct ptlrpc_request *req = mdt_info_req(info);
623 struct lu_ucred *uc = mdt_ucred(info);
624 struct md_attr *ma = &info->mti_attr;
627 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
630 /* LU-5564: for normal close request, skip permission check */
631 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
632 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
633 uc->uc_cap |= CFS_CAP_FS_MASK;
635 mdt_exit_ucred(info);
637 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
638 return old_init_ucred_reint(info);
640 return new_init_ucred(info, REC_INIT, NULL, true);
643 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
644 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
646 const struct lov_ost_data_v1 *lod;
647 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
651 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
654 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
655 POSTID(&lmm->lmm_oi), lmm_magic,
656 le32_to_cpu(lmm->lmm_pattern));
658 /* No support for compount layouts yet */
659 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
662 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
663 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
664 le32_to_cpu(lmm->lmm_stripe_size), count);
666 /* If it's a directory or a released file, then there are
667 * no actual objects to print, so bail out. */
668 if (valid & OBD_MD_FLDIREA ||
669 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
672 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
673 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
676 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
677 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
678 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
682 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
684 const struct lmv_mds_md_v1 *lmm1;
687 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
690 lmm1 = &lmv->lmv_md_v1;
692 "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
693 le32_to_cpu(lmm1->lmv_magic),
694 le32_to_cpu(lmm1->lmv_master_mdt_index),
695 le32_to_cpu(lmm1->lmv_stripe_count),
696 le32_to_cpu(lmm1->lmv_hash_type));
698 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
701 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
704 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
705 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
709 /* Shrink and/or grow reply buffers */
710 int mdt_fix_reply(struct mdt_thread_info *info)
712 struct req_capsule *pill = info->mti_pill;
713 struct mdt_body *body;
714 int md_size, md_packed = 0;
719 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
720 LASSERT(body != NULL);
722 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
724 md_size = body->mbo_eadatasize;
728 acl_size = body->mbo_aclsize;
730 /* this replay - not send info to client */
731 if (info->mti_spec.no_create) {
736 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
741 &RMF_ACL, or &RMF_LOGCOOKIES
742 (optional) &RMF_CAPA1,
743 (optional) &RMF_CAPA2,
744 (optional) something else
747 /* MDT_MD buffer may be bigger than packed value, let's shrink all
748 * buffers before growing it */
749 if (info->mti_big_lmm_used) {
750 /* big_lmm buffer may be used even without packing the result
751 * into reply, just for internal server needs */
752 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
753 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
756 /* free big lmm if md_size is not needed */
757 if (md_size == 0 || md_packed == 0) {
758 info->mti_big_lmm_used = 0;
760 /* buffer must be allocated separately */
761 LASSERT(info->mti_attr.ma_lmm !=
762 req_capsule_server_get(pill, &RMF_MDT_MD));
763 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
765 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
766 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
769 if (info->mti_big_acl_used) {
771 info->mti_big_acl_used = 0;
773 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
774 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
775 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
776 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
777 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
780 /* Shrink optional SECCTX buffer if it is not used */
781 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
782 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
783 !(body->mbo_valid & OBD_MD_SECCTX))
784 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
787 * Some more field should be shrinked if needed.
788 * This should be done by those who added fields to reply message.
791 /* Grow MD buffer if needed finally */
792 if (info->mti_big_lmm_used) {
795 LASSERT(md_size > md_packed);
796 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
797 md_size - md_packed);
798 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
800 /* we can't answer with proper LOV EA, drop flags,
801 * the rc is also returned so this request is
802 * considered as failed */
803 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
804 /* don't return transno along with error */
805 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
807 /* now we need to pack right LOV/LMV EA */
808 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
809 if (info->mti_attr.ma_valid & MA_LOV) {
810 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
812 info->mti_attr.ma_lmm_size);
813 memcpy(lmm, info->mti_attr.ma_lmm,
814 info->mti_attr.ma_lmm_size);
815 } else if (info->mti_attr.ma_valid & MA_LMV) {
816 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
818 info->mti_attr.ma_lmv_size);
819 memcpy(lmm, info->mti_attr.ma_lmv,
820 info->mti_attr.ma_lmv_size);
824 /* update mdt_max_mdsize so clients will be aware about that */
825 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
826 info->mti_mdt->mdt_max_mdsize =
827 info->mti_attr.ma_lmm_size;
828 info->mti_big_lmm_used = 0;
831 if (info->mti_big_acl_used) {
832 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
835 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
837 body->mbo_valid &= ~OBD_MD_FLACL;
839 void *acl = req_capsule_server_get(pill, &RMF_ACL);
841 memcpy(acl, info->mti_big_acl, acl_size);
844 info->mti_big_acl_used = 0;
851 /* if object is dying, pack the lov/llog data,
852 * parameter info->mti_attr should be valid at this point!
853 * Also implements RAoLU policy */
854 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
857 struct mdt_body *repbody = NULL;
858 const struct lu_attr *la = &ma->ma_attr;
859 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
862 struct hsm_action_item hai = {
863 .hai_len = sizeof(hai),
864 .hai_action = HSMA_REMOVE,
865 .hai_extent.length = -1,
873 if (mdt_info_req(info) != NULL) {
874 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
875 LASSERT(repbody != NULL);
877 CDEBUG(D_INFO, "not running in a request/reply context\n");
880 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
881 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
883 if (ma->ma_valid & MA_LOV) {
884 CERROR("No need in LOV EA upon unlink\n");
888 repbody->mbo_eadatasize = 0;
890 /* Only check unlinked and archived if RAoLU and upon last close */
891 if (!cdt->cdt_remove_archive_on_last_unlink ||
892 atomic_read(&mo->mot_open_count) != 0)
895 /* mdt_attr_get_complex will clear ma_valid, so check here first */
896 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
899 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
902 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
904 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
905 * try setting them */
907 rc = mdt_attr_get_complex(info, mo, ma);
909 CERROR("%s: unable to fetch missing attributes of"
910 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
911 PFID(mdt_object_fid(mo)), rc);
915 if (need & MA_INODE) {
916 if (ma->ma_valid & MA_INODE) {
917 if (ma->ma_attr.la_nlink != 0)
925 if (ma->ma_valid & MA_HSM) {
926 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
934 /* RAoLU policy is active, last close on file has occured,
935 * file is unlinked, file is archived, so create remove request
937 * If CDT is not running, requests will be logged for later. */
938 if (ma->ma_hsm.mh_arch_id != 0)
939 archive_id = ma->ma_hsm.mh_arch_id;
941 archive_id = cdt->cdt_default_archive_id;
943 hai.hai_fid = *mdt_object_fid(mo);
945 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
948 CERROR("%s: unable to add HSM remove request for "DFID
949 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
950 PFID(mdt_object_fid(mo)), rc);
955 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
961 if (in & MDS_ATTR_MODE)
963 if (in & MDS_ATTR_UID)
965 if (in & MDS_ATTR_GID)
967 if (in & MDS_ATTR_SIZE)
969 if (in & MDS_ATTR_BLOCKS)
971 if (in & MDS_ATTR_ATIME_SET)
973 if (in & MDS_ATTR_CTIME_SET)
975 if (in & MDS_ATTR_MTIME_SET)
977 if (in & MDS_ATTR_ATTR_FLAG)
979 if (in & MDS_ATTR_KILL_SUID)
981 if (in & MDS_ATTR_KILL_SGID)
983 if (in & MDS_ATTR_PROJID)
985 if (in & MDS_ATTR_LSIZE)
987 if (in & MDS_ATTR_LBLOCKS)
990 if (in & MDS_ATTR_FROM_OPEN)
991 rr->rr_flags |= MRF_OPEN_TRUNC;
992 if (in & MDS_ATTR_OVERRIDE)
993 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
994 if (in & MDS_ATTR_FORCE)
995 ma->ma_attr_flags |= MDS_PERM_BYPASS;
997 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
998 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
999 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1000 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1001 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1002 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1005 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", in);
1011 int mdt_name_unpack(struct req_capsule *pill,
1012 const struct req_msg_field *field,
1014 enum mdt_name_flags flags)
1016 ln->ln_name = req_capsule_client_get(pill, field);
1017 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1019 if (!lu_name_is_valid(ln)) {
1026 if ((flags & MNF_FIX_ANON) &&
1027 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1028 /* Newer (3.x) kernels use a name of "/" for the
1029 * "anonymous" disconnected dentries from NFS
1030 * filehandle conversion. See d_obtain_alias(). */
1038 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1039 const char **secctx_name,
1040 void **secctx, size_t *secctx_size)
1045 *secctx_name = NULL;
1049 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1050 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1053 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1058 if (name_size > XATTR_NAME_MAX + 1)
1061 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1062 if (strnlen(name, name_size) != name_size - 1)
1065 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1066 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1069 *secctx_name = name;
1070 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1071 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1076 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1078 struct lu_ucred *uc = mdt_ucred(info);
1079 struct md_attr *ma = &info->mti_attr;
1080 struct lu_attr *la = &ma->ma_attr;
1081 struct req_capsule *pill = info->mti_pill;
1082 struct mdt_reint_record *rr = &info->mti_rr;
1083 struct mdt_rec_setattr *rec;
1084 struct lu_nodemap *nodemap;
1088 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1089 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1093 /* This prior initialization is needed for old_init_ucred_reint() */
1094 uc->uc_fsuid = rec->sa_fsuid;
1095 uc->uc_fsgid = rec->sa_fsgid;
1096 uc->uc_cap = rec->sa_cap;
1097 uc->uc_suppgids[0] = rec->sa_suppgid;
1098 uc->uc_suppgids[1] = -1;
1100 rr->rr_fid1 = &rec->sa_fid;
1101 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1102 la->la_mode = rec->sa_mode;
1103 la->la_flags = rec->sa_attr_flags;
1105 nodemap = nodemap_get_from_exp(info->mti_exp);
1106 if (IS_ERR(nodemap))
1107 RETURN(PTR_ERR(nodemap));
1109 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1110 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1111 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1112 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1113 la->la_projid = rec->sa_projid;
1114 nodemap_putref(nodemap);
1116 la->la_size = rec->sa_size;
1117 la->la_blocks = rec->sa_blocks;
1118 la->la_ctime = rec->sa_ctime;
1119 la->la_atime = rec->sa_atime;
1120 la->la_mtime = rec->sa_mtime;
1121 ma->ma_valid = MA_INODE;
1123 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1124 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE);
1128 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1130 struct req_capsule *pill = info->mti_pill;
1131 struct mdt_ioepoch *ioepoch;
1134 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1135 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1139 if (ioepoch == NULL)
1142 info->mti_open_handle = ioepoch->mio_open_handle;
1147 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1148 struct req_capsule *pill = info->mti_pill;
1150 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1151 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1152 if (info->mti_dlm_req == NULL)
1159 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1161 struct mdt_reint_record *rr = &info->mti_rr;
1162 struct md_attr *ma = &info->mti_attr;
1163 struct req_capsule *pill = info->mti_pill;
1167 rc = mdt_setattr_unpack_rec(info);
1171 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1172 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1173 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1175 if (rr->rr_eadatalen > 0) {
1176 const struct lmv_user_md *lum;
1178 lum = rr->rr_eadata;
1179 /* Sigh ma_valid(from req) does not indicate whether
1180 * it will set LOV/LMV EA, so we have to check magic */
1181 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1182 ma->ma_valid |= MA_LMV;
1183 ma->ma_lmv = (void *)rr->rr_eadata;
1184 ma->ma_lmv_size = rr->rr_eadatalen;
1186 ma->ma_valid |= MA_LOV;
1187 ma->ma_lmm = (void *)rr->rr_eadata;
1188 ma->ma_lmm_size = rr->rr_eadatalen;
1193 rc = mdt_dlmreq_unpack(info);
1197 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1199 struct md_attr *ma = &info->mti_attr;
1200 struct req_capsule *pill = info->mti_pill;
1203 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1206 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1208 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1209 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1215 int mdt_close_unpack(struct mdt_thread_info *info)
1220 rc = mdt_close_handle_unpack(info);
1224 rc = mdt_setattr_unpack_rec(info);
1228 rc = mdt_close_intent_unpack(info);
1232 RETURN(mdt_init_ucred_reint(info));
1235 static int mdt_create_unpack(struct mdt_thread_info *info)
1237 struct lu_ucred *uc = mdt_ucred(info);
1238 struct mdt_rec_create *rec;
1239 struct lu_attr *attr = &info->mti_attr.ma_attr;
1240 struct mdt_reint_record *rr = &info->mti_rr;
1241 struct req_capsule *pill = info->mti_pill;
1242 struct md_op_spec *sp = &info->mti_spec;
1247 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1248 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1252 /* This prior initialization is needed for old_init_ucred_reint() */
1253 uc->uc_fsuid = rec->cr_fsuid;
1254 uc->uc_fsgid = rec->cr_fsgid;
1255 uc->uc_cap = rec->cr_cap;
1256 uc->uc_suppgids[0] = rec->cr_suppgid1;
1257 uc->uc_suppgids[1] = -1;
1258 uc->uc_umask = rec->cr_umask;
1260 rr->rr_fid1 = &rec->cr_fid1;
1261 rr->rr_fid2 = &rec->cr_fid2;
1262 attr->la_mode = rec->cr_mode;
1263 attr->la_rdev = rec->cr_rdev;
1264 attr->la_uid = rec->cr_fsuid;
1265 attr->la_gid = rec->cr_fsgid;
1266 attr->la_ctime = rec->cr_time;
1267 attr->la_mtime = rec->cr_time;
1268 attr->la_atime = rec->cr_time;
1269 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1270 LA_CTIME | LA_MTIME | LA_ATIME;
1271 memset(&sp->u, 0, sizeof(sp->u));
1272 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1274 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1278 if (S_ISLNK(attr->la_mode)) {
1279 const char *tgt = NULL;
1281 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1282 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1283 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1284 sp->u.sp_symname = tgt;
1289 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1290 if (S_ISDIR(attr->la_mode) &&
1291 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1292 sp->u.sp_ea.eadata =
1293 req_capsule_client_get(pill, &RMF_EADATA);
1294 sp->u.sp_ea.eadatalen =
1295 req_capsule_get_size(pill, &RMF_EADATA,
1297 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1301 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1302 &sp->sp_cr_file_secctx,
1303 &sp->sp_cr_file_secctx_size);
1307 rc = req_check_sepol(pill);
1311 rc = mdt_dlmreq_unpack(info);
1315 static int mdt_link_unpack(struct mdt_thread_info *info)
1317 struct lu_ucred *uc = mdt_ucred(info);
1318 struct mdt_rec_link *rec;
1319 struct lu_attr *attr = &info->mti_attr.ma_attr;
1320 struct mdt_reint_record *rr = &info->mti_rr;
1321 struct req_capsule *pill = info->mti_pill;
1326 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1327 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1331 /* This prior initialization is needed for old_init_ucred_reint() */
1332 uc->uc_fsuid = rec->lk_fsuid;
1333 uc->uc_fsgid = rec->lk_fsgid;
1334 uc->uc_cap = rec->lk_cap;
1335 uc->uc_suppgids[0] = rec->lk_suppgid1;
1336 uc->uc_suppgids[1] = rec->lk_suppgid2;
1338 attr->la_uid = rec->lk_fsuid;
1339 attr->la_gid = rec->lk_fsgid;
1340 rr->rr_fid1 = &rec->lk_fid1;
1341 rr->rr_fid2 = &rec->lk_fid2;
1342 attr->la_ctime = rec->lk_time;
1343 attr->la_mtime = rec->lk_time;
1344 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1346 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1350 rc = req_check_sepol(pill);
1354 rc = mdt_dlmreq_unpack(info);
1359 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1361 struct lu_ucred *uc = mdt_ucred(info);
1362 struct mdt_rec_unlink *rec;
1363 struct lu_attr *attr = &info->mti_attr.ma_attr;
1364 struct mdt_reint_record *rr = &info->mti_rr;
1365 struct req_capsule *pill = info->mti_pill;
1370 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1371 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1375 /* This prior initialization is needed for old_init_ucred_reint() */
1376 uc->uc_fsuid = rec->ul_fsuid;
1377 uc->uc_fsgid = rec->ul_fsgid;
1378 uc->uc_cap = rec->ul_cap;
1379 uc->uc_suppgids[0] = rec->ul_suppgid1;
1380 uc->uc_suppgids[1] = -1;
1382 attr->la_uid = rec->ul_fsuid;
1383 attr->la_gid = rec->ul_fsgid;
1384 rr->rr_fid1 = &rec->ul_fid1;
1385 rr->rr_fid2 = &rec->ul_fid2;
1386 attr->la_ctime = rec->ul_time;
1387 attr->la_mtime = rec->ul_time;
1388 attr->la_mode = rec->ul_mode;
1389 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1391 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1395 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1397 rc = req_check_sepol(pill);
1401 rc = mdt_dlmreq_unpack(info);
1405 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1407 info->mti_spec.sp_rm_entry = 1;
1408 return mdt_unlink_unpack(info);
1411 static int mdt_rename_unpack(struct mdt_thread_info *info)
1413 struct lu_ucred *uc = mdt_ucred(info);
1414 struct mdt_rec_rename *rec;
1415 struct lu_attr *attr = &info->mti_attr.ma_attr;
1416 struct mdt_reint_record *rr = &info->mti_rr;
1417 struct req_capsule *pill = info->mti_pill;
1418 struct md_op_spec *spec = &info->mti_spec;
1423 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1424 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1428 /* This prior initialization is needed for old_init_ucred_reint() */
1429 uc->uc_fsuid = rec->rn_fsuid;
1430 uc->uc_fsgid = rec->rn_fsgid;
1431 uc->uc_cap = rec->rn_cap;
1432 uc->uc_suppgids[0] = rec->rn_suppgid1;
1433 uc->uc_suppgids[1] = rec->rn_suppgid2;
1435 attr->la_uid = rec->rn_fsuid;
1436 attr->la_gid = rec->rn_fsgid;
1437 rr->rr_fid1 = &rec->rn_fid1;
1438 rr->rr_fid2 = &rec->rn_fid2;
1439 attr->la_ctime = rec->rn_time;
1440 attr->la_mtime = rec->rn_time;
1441 /* rename_tgt contains the mode already */
1442 attr->la_mode = rec->rn_mode;
1443 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1445 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1449 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1453 spec->no_create = !!req_is_replay(mdt_info_req(info));
1455 rc = req_check_sepol(pill);
1459 rc = mdt_dlmreq_unpack(info);
1464 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1466 struct lu_ucred *uc = mdt_ucred(info);
1467 struct mdt_rec_rename *rec;
1468 struct lu_attr *attr = &info->mti_attr.ma_attr;
1469 struct mdt_reint_record *rr = &info->mti_rr;
1470 struct req_capsule *pill = info->mti_pill;
1471 struct md_op_spec *spec = &info->mti_spec;
1476 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1477 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1481 /* This prior initialization is needed for old_init_ucred_reint() */
1482 uc->uc_fsuid = rec->rn_fsuid;
1483 uc->uc_fsgid = rec->rn_fsgid;
1484 uc->uc_cap = rec->rn_cap;
1485 uc->uc_suppgids[0] = rec->rn_suppgid1;
1486 uc->uc_suppgids[1] = rec->rn_suppgid2;
1488 attr->la_uid = rec->rn_fsuid;
1489 attr->la_gid = rec->rn_fsgid;
1490 rr->rr_fid1 = &rec->rn_fid1;
1491 rr->rr_fid2 = &rec->rn_fid2;
1492 attr->la_ctime = rec->rn_time;
1493 attr->la_mtime = rec->rn_time;
1494 /* rename_tgt contains the mode already */
1495 attr->la_mode = rec->rn_mode;
1496 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1497 spec->sp_cr_flags = 0;
1499 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1503 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1504 rc = mdt_close_handle_unpack(info);
1508 spec->sp_migrate_close = 1;
1511 /* lustre version > 2.11 migration packs lum */
1512 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1513 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1514 rr->rr_eadatalen = req_capsule_get_size(pill,
1517 if (rr->rr_eadatalen > 0) {
1518 rr->rr_eadata = req_capsule_client_get(pill,
1520 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1521 spec->u.sp_ea.eadata = rr->rr_eadata;
1522 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1525 /* old client doesn't provide lum. */
1526 RETURN(-EOPNOTSUPP);
1530 spec->no_create = !!req_is_replay(mdt_info_req(info));
1532 rc = mdt_dlmreq_unpack(info);
1538 * please see comment above LOV_MAGIC_V1_DEFINED
1540 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1542 struct lov_user_md_v1 *v1 = eadata;
1546 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1547 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1548 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1549 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1550 __swab32(LOV_MAGIC_MAGIC))
1551 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1555 static int mdt_open_unpack(struct mdt_thread_info *info)
1557 struct lu_ucred *uc = mdt_ucred(info);
1558 struct mdt_rec_create *rec;
1559 struct lu_attr *attr = &info->mti_attr.ma_attr;
1560 struct req_capsule *pill = info->mti_pill;
1561 struct mdt_reint_record *rr = &info->mti_rr;
1562 struct ptlrpc_request *req = mdt_info_req(info);
1563 struct md_op_spec *sp = &info->mti_spec;
1567 CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1568 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1572 /* This prior initialization is needed for old_init_ucred_reint() */
1573 uc->uc_fsuid = rec->cr_fsuid;
1574 uc->uc_fsgid = rec->cr_fsgid;
1575 uc->uc_cap = rec->cr_cap;
1576 uc->uc_suppgids[0] = rec->cr_suppgid1;
1577 uc->uc_suppgids[1] = rec->cr_suppgid2;
1578 uc->uc_umask = rec->cr_umask;
1580 rr->rr_fid1 = &rec->cr_fid1;
1581 rr->rr_fid2 = &rec->cr_fid2;
1582 rr->rr_open_handle = &rec->cr_open_handle_old;
1583 attr->la_mode = rec->cr_mode;
1584 attr->la_rdev = rec->cr_rdev;
1585 attr->la_uid = rec->cr_fsuid;
1586 attr->la_gid = rec->cr_fsgid;
1587 attr->la_ctime = rec->cr_time;
1588 attr->la_mtime = rec->cr_time;
1589 attr->la_atime = rec->cr_time;
1590 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1591 LA_CTIME | LA_MTIME | LA_ATIME;
1592 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1593 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1594 /* Do not trigger ASSERTION if client miss to set such flags. */
1595 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1598 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1600 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1602 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1603 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1605 if (rr->rr_eadatalen > 0) {
1606 rr->rr_eadata = req_capsule_client_get(pill,
1608 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1609 sp->u.sp_ea.eadata = rr->rr_eadata;
1610 sp->no_create = !!req_is_replay(req);
1611 mdt_fix_lov_magic(info, rr->rr_eadata);
1615 * Client default md_size may be 0 right after client start,
1616 * until all osc are connected, set here just some reasonable
1617 * value to prevent misbehavior.
1619 if (rr->rr_eadatalen == 0 &&
1620 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1621 rr->rr_eadatalen = MIN_MD_SIZE;
1624 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1625 &sp->sp_cr_file_secctx,
1626 &sp->sp_cr_file_secctx_size);
1630 rc = req_check_sepol(pill);
1637 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1639 struct mdt_reint_record *rr = &info->mti_rr;
1640 struct lu_ucred *uc = mdt_ucred(info);
1641 struct lu_attr *attr = &info->mti_attr.ma_attr;
1642 struct req_capsule *pill = info->mti_pill;
1643 struct mdt_rec_setxattr *rec;
1648 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1649 sizeof(struct mdt_rec_reint));
1651 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1655 /* This prior initialization is needed for old_init_ucred_reint() */
1656 uc->uc_fsuid = rec->sx_fsuid;
1657 uc->uc_fsgid = rec->sx_fsgid;
1658 uc->uc_cap = rec->sx_cap;
1659 uc->uc_suppgids[0] = rec->sx_suppgid1;
1660 uc->uc_suppgids[1] = -1;
1662 rr->rr_opcode = rec->sx_opcode;
1663 rr->rr_fid1 = &rec->sx_fid;
1664 attr->la_valid = rec->sx_valid;
1665 attr->la_ctime = rec->sx_time;
1666 attr->la_size = rec->sx_size;
1667 attr->la_flags = rec->sx_flags;
1669 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1673 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1674 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1676 if (rr->rr_eadatalen > 0) {
1677 rr->rr_eadata = req_capsule_client_get(pill,
1679 if (rr->rr_eadata == NULL)
1682 rr->rr_eadata = NULL;
1684 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1685 CDEBUG(D_INFO, "no xattr data supplied\n");
1689 rc = req_check_sepol(pill);
1693 if (mdt_dlmreq_unpack(info) < 0)
1699 static int mdt_resync_unpack(struct mdt_thread_info *info)
1701 struct req_capsule *pill = info->mti_pill;
1702 struct mdt_reint_record *rr = &info->mti_rr;
1703 struct lu_ucred *uc = mdt_ucred(info);
1704 struct mdt_rec_resync *rec;
1707 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1708 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1712 /* This prior initialization is needed for old_init_ucred_reint() */
1713 uc->uc_fsuid = rec->rs_fsuid;
1714 uc->uc_fsgid = rec->rs_fsgid;
1715 uc->uc_cap = rec->rs_cap;
1717 rr->rr_fid1 = &rec->rs_fid;
1718 rr->rr_mirror_id = rec->rs_mirror_id;
1720 /* cookie doesn't need to be swapped but it has been swapped
1721 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1723 if (ptlrpc_req_need_swab(mdt_info_req(info)))
1724 __swab64s(&rec->rs_lease_handle.cookie);
1725 rr->rr_lease_handle = &rec->rs_lease_handle;
1727 RETURN(mdt_dlmreq_unpack(info));
1730 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1732 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1733 [REINT_SETATTR] = mdt_setattr_unpack,
1734 [REINT_CREATE] = mdt_create_unpack,
1735 [REINT_LINK] = mdt_link_unpack,
1736 [REINT_UNLINK] = mdt_unlink_unpack,
1737 [REINT_RENAME] = mdt_rename_unpack,
1738 [REINT_OPEN] = mdt_open_unpack,
1739 [REINT_SETXATTR] = mdt_setxattr_unpack,
1740 [REINT_RMENTRY] = mdt_rmentry_unpack,
1741 [REINT_MIGRATE] = mdt_migrate_unpack,
1742 [REINT_RESYNC] = mdt_resync_unpack,
1745 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1750 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1751 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1752 info->mti_rr.rr_opcode = op;
1753 rc = mdt_reint_unpackers[op](info);
1755 CERROR("Unexpected opcode %d\n", op);
1761 /* check whether two FIDs belong to different MDT. */
1762 static int mdt_fids_different_target(struct mdt_thread_info *info,
1763 const struct lu_fid *fid1,
1764 const struct lu_fid *fid2)
1766 const struct lu_env *env = info->mti_env;
1767 struct mdt_device *mdt = info->mti_mdt;
1768 struct lu_seq_range *range = &info->mti_range;
1769 struct seq_server_site *ss;
1770 __u32 index1, index2;
1773 if (fid_seq(fid1) == fid_seq(fid2))
1776 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1778 range->lsr_flags = LU_SEQ_RANGE_MDT;
1779 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1783 index1 = range->lsr_index;
1785 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1789 index2 = range->lsr_index;
1791 return index1 != index2;
1795 * Check whether \a child is remote object on \a parent.
1797 * \param[in] info thread environment
1798 * \param[in] parent parent object, it's the same as child object in
1800 * \param[in] child child object
1802 * \retval 1 is remote object.
1803 * \retval 0 isn't remote object.
1804 * \retval < 1 error code
1806 int mdt_is_remote_object(struct mdt_thread_info *info,
1807 struct mdt_object *parent,
1808 struct mdt_object *child)
1810 struct lu_buf *buf = &info->mti_big_buf;
1811 struct linkea_data ldata = { NULL };
1812 struct link_ea_header *leh;
1813 struct link_ea_entry *lee;
1814 struct lu_name name;
1822 if (fid_is_root(mdt_object_fid(child)))
1825 if (likely(parent != child)) {
1826 if (mdt_object_remote(parent) ^ mdt_object_remote(child))
1829 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1832 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1833 mdt_object_fid(child));
1837 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1838 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1843 rc = mdt_links_read(info, child, &ldata);
1844 /* can't read linkea, just assume it's remote object */
1845 if (rc == -ENOENT || rc == -ENODATA)
1851 lee = (struct link_ea_entry *)(leh + 1);
1852 for (i = 0; i < leh->leh_reccount; i++) {
1853 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1854 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1855 if (mdt_fids_different_target(info, &pfid,
1856 mdt_object_fid(child)))
1863 void mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1864 struct mdt_object *child)
1867 struct lu_buf *buffer;
1868 struct mdt_body *repbody;
1869 struct req_capsule *pill = info->mti_pill;
1872 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1873 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1875 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1876 buffer = &info->mti_buf;
1878 /* fill reply buffer with security context now */
1879 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1881 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1883 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1884 buffer, secctx_name);
1887 "found security context of size %d for "DFID"\n",
1888 rc, PFID(mdt_object_fid(child)));
1890 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1891 repbody->mbo_valid |= OBD_MD_SECCTX;
1892 if (rc < buffer->lb_len)
1893 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1897 "security context not found for "DFID": rc = %d\n",
1898 PFID(mdt_object_fid(child)), rc);
1899 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,