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) {
321 ucred->uc_fsuid = nodemap->nm_squash_uid;
322 ucred->uc_fsgid = nodemap->nm_squash_gid;
325 ucred->uc_fsuid = pud->pud_fsuid;
326 ucred->uc_fsgid = pud->pud_fsgid;
327 ucred->uc_cap = pud->pud_cap;
330 /* process root_squash here. */
331 mdt_root_squash(info, peernid);
333 /* remove fs privilege for non-root user. */
334 if (ucred->uc_fsuid && drop_fs_cap)
335 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
337 ucred->uc_cap = pud->pud_cap;
338 ucred->uc_valid = UCRED_NEW;
339 ucred_set_jobid(info, ucred);
340 ucred_set_nid(info, ucred);
341 ucred_set_audit_enabled(info, ucred);
347 if (ucred->uc_ginfo) {
348 put_group_info(ucred->uc_ginfo);
349 ucred->uc_ginfo = NULL;
351 if (ucred->uc_identity) {
352 mdt_identity_put(mdt->mdt_identity_cache,
354 ucred->uc_identity = NULL;
362 * Check whether allow the client to set supplementary group IDs or not.
364 * \param[in] info pointer to the thread context
365 * \param[in] uc pointer to the RPC user descriptor
367 * \retval true if allow to set supplementary group IDs
368 * \retval false for other cases
370 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
374 /* 1. If identity_upcall is disabled,
375 * permit local client to do anything. */
376 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
379 /* 2. If fail to get related identities, then forbid any client to
380 * set supplementary group IDs. */
381 if (uc->uc_identity == NULL)
384 /* 3. Check the permission in the identities. */
385 perm = mdt_identity_get_perm(uc->uc_identity,
386 mdt_info_req(info)->rq_peer.nid);
387 if (perm & CFS_SETGRP_PERM)
393 int mdt_check_ucred(struct mdt_thread_info *info)
395 struct ptlrpc_request *req = mdt_info_req(info);
396 struct mdt_device *mdt = info->mti_mdt;
397 struct ptlrpc_user_desc *pud = req->rq_user_desc;
398 struct lu_ucred *ucred = mdt_ucred(info);
399 struct md_identity *identity = NULL;
400 lnet_nid_t peernid = req->rq_peer.nid;
408 LASSERT(ucred != NULL);
409 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
412 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
415 /* sanity check: if we use strong authentication, we expect the
416 * uid which client claimed is true */
417 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
418 req->rq_auth_uid != pud->pud_uid) {
419 CDEBUG(D_SEC, "local client %s: auth uid %u "
420 "while client claims %u:%u/%u:%u\n",
421 libcfs_nid2str(peernid), req->rq_auth_uid,
422 pud->pud_uid, pud->pud_gid,
423 pud->pud_fsuid, pud->pud_fsgid);
427 if (is_identity_get_disabled(mdt->mdt_identity_cache))
430 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
431 if (IS_ERR(identity)) {
432 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
435 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
441 perm = mdt_identity_get_perm(identity, peernid);
442 /* find out the setuid/setgid attempt */
443 setuid = (pud->pud_uid != pud->pud_fsuid);
444 setgid = (pud->pud_gid != pud->pud_fsgid ||
445 pud->pud_gid != identity->mi_gid);
447 /* check permission of setuid */
448 if (setuid && !(perm & CFS_SETUID_PERM)) {
449 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
450 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
451 GOTO(out, rc = -EACCES);
454 /* check permission of setgid */
455 if (setgid && !(perm & CFS_SETGID_PERM)) {
456 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
457 "from %s\n", pud->pud_uid, pud->pud_gid,
458 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
459 libcfs_nid2str(peernid));
460 GOTO(out, rc = -EACCES);
466 mdt_identity_put(mdt->mdt_identity_cache, identity);
470 static int old_init_ucred_common(struct mdt_thread_info *info,
471 struct lu_nodemap *nodemap,
474 struct lu_ucred *uc = mdt_ucred(info);
475 struct mdt_device *mdt = info->mti_mdt;
476 struct md_identity *identity = NULL;
478 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
479 /* deny access before we get identity ref */
480 if (nodemap->nmf_deny_unknown)
483 uc->uc_fsuid = nodemap->nm_squash_uid;
484 uc->uc_fsgid = nodemap->nm_squash_gid;
486 uc->uc_suppgids[0] = -1;
487 uc->uc_suppgids[1] = -1;
490 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
491 identity = mdt_identity_get(mdt->mdt_identity_cache,
493 if (IS_ERR(identity)) {
494 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
495 uc->uc_cap & CFS_CAP_FS_MASK)) {
498 CDEBUG(D_SEC, "Deny access without identity: "
499 "uid %u\n", uc->uc_fsuid);
504 uc->uc_identity = identity;
506 /* process root_squash here. */
507 mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
509 /* remove fs privilege for non-root user. */
510 if (uc->uc_fsuid && drop_fs_cap)
511 uc->uc_cap &= ~CFS_CAP_FS_MASK;
512 uc->uc_valid = UCRED_OLD;
513 ucred_set_jobid(info, uc);
514 ucred_set_nid(info, uc);
515 ucred_set_audit_enabled(info, uc);
522 static int old_init_ucred(struct mdt_thread_info *info,
523 struct mdt_body *body, bool drop_fs_cap)
525 struct lu_ucred *uc = mdt_ucred(info);
526 struct lu_nodemap *nodemap;
530 nodemap = nodemap_get_from_exp(info->mti_exp);
532 RETURN(PTR_ERR(nodemap));
534 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
535 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
536 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
537 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
538 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
539 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
540 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
541 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
544 uc->uc_valid = UCRED_INVALID;
545 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
546 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
547 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
548 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
549 uc->uc_suppgids[0] = body->mbo_suppgid;
550 uc->uc_suppgids[1] = -1;
552 uc->uc_cap = body->mbo_capability;
554 rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
555 nodemap_putref(nodemap);
560 static int old_init_ucred_reint(struct mdt_thread_info *info)
562 struct lu_ucred *uc = mdt_ucred(info);
563 struct lu_nodemap *nodemap;
567 nodemap = nodemap_get_from_exp(info->mti_exp);
569 RETURN(PTR_ERR(nodemap));
573 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
574 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
575 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
576 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
578 uc->uc_valid = UCRED_INVALID;
579 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
580 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
583 rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
584 nodemap_putref(nodemap);
589 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
590 struct mdt_body *body,
593 struct ptlrpc_request *req = mdt_info_req(info);
594 struct lu_ucred *uc = mdt_ucred(info);
597 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
600 mdt_exit_ucred(info);
602 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
603 return old_init_ucred(info, body, drop_fs_cap);
605 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
608 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
610 return __mdt_init_ucred(info, body, true);
613 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
614 * doesn't set correct fsuid explicitely, but raise capability to allow
615 * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
616 * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
617 * will fail permission check. */
618 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
619 struct mdt_body *body)
621 return __mdt_init_ucred(info, body, false);
624 int mdt_init_ucred_reint(struct mdt_thread_info *info)
626 struct ptlrpc_request *req = mdt_info_req(info);
627 struct lu_ucred *uc = mdt_ucred(info);
628 struct md_attr *ma = &info->mti_attr;
631 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
634 /* LU-5564: for normal close request, skip permission check */
635 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
636 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
637 uc->uc_cap |= CFS_CAP_FS_MASK;
639 mdt_exit_ucred(info);
641 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
642 return old_init_ucred_reint(info);
644 return new_init_ucred(info, REC_INIT, NULL, true);
647 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
648 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
650 const struct lov_ost_data_v1 *lod;
651 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
655 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
658 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
659 POSTID(&lmm->lmm_oi), lmm_magic,
660 le32_to_cpu(lmm->lmm_pattern));
662 /* No support for compount layouts yet */
663 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
666 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
667 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
668 le32_to_cpu(lmm->lmm_stripe_size), count);
670 /* If it's a directory or a released file, then there are
671 * no actual objects to print, so bail out. */
672 if (valid & OBD_MD_FLDIREA ||
673 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
676 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
677 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
680 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
681 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
682 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
686 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
688 const struct lmv_mds_md_v1 *lmm1;
691 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
694 lmm1 = &lmv->lmv_md_v1;
696 "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
697 le32_to_cpu(lmm1->lmv_magic),
698 le32_to_cpu(lmm1->lmv_master_mdt_index),
699 le32_to_cpu(lmm1->lmv_stripe_count),
700 le32_to_cpu(lmm1->lmv_hash_type));
702 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
705 for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
708 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
709 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
713 /* Shrink and/or grow reply buffers */
714 int mdt_fix_reply(struct mdt_thread_info *info)
716 struct req_capsule *pill = info->mti_pill;
717 struct mdt_body *body;
718 int md_size, md_packed = 0;
723 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
724 LASSERT(body != NULL);
726 if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
728 md_size = body->mbo_eadatasize;
732 acl_size = body->mbo_aclsize;
734 /* this replay - not send info to client */
735 if (info->mti_spec.no_create) {
740 CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
745 &RMF_ACL, or &RMF_LOGCOOKIES
746 (optional) &RMF_CAPA1,
747 (optional) &RMF_CAPA2,
748 (optional) something else
751 /* MDT_MD buffer may be bigger than packed value, let's shrink all
752 * buffers before growing it */
753 if (info->mti_big_lmm_used) {
754 /* big_lmm buffer may be used even without packing the result
755 * into reply, just for internal server needs */
756 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
757 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
760 /* free big lmm if md_size is not needed */
761 if (md_size == 0 || md_packed == 0) {
762 info->mti_big_lmm_used = 0;
764 /* buffer must be allocated separately */
765 LASSERT(info->mti_attr.ma_lmm !=
766 req_capsule_server_get(pill, &RMF_MDT_MD));
767 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
769 } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
770 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
773 if (info->mti_big_acl_used) {
775 info->mti_big_acl_used = 0;
777 req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
778 } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
779 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
780 } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
781 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
785 * Some more field should be shrinked if needed.
786 * This should be done by those who added fields to reply message.
789 /* Grow MD buffer if needed finally */
790 if (info->mti_big_lmm_used) {
793 LASSERT(md_size > md_packed);
794 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
795 md_size - md_packed);
796 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
798 /* we can't answer with proper LOV EA, drop flags,
799 * the rc is also returned so this request is
800 * considered as failed */
801 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
802 /* don't return transno along with error */
803 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
805 /* now we need to pack right LOV/LMV EA */
806 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
807 if (info->mti_attr.ma_valid & MA_LOV) {
808 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
810 info->mti_attr.ma_lmm_size);
811 memcpy(lmm, info->mti_attr.ma_lmm,
812 info->mti_attr.ma_lmm_size);
813 } else if (info->mti_attr.ma_valid & MA_LMV) {
814 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
816 info->mti_attr.ma_lmv_size);
817 memcpy(lmm, info->mti_attr.ma_lmv,
818 info->mti_attr.ma_lmv_size);
822 /* update mdt_max_mdsize so clients will be aware about that */
823 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
824 info->mti_mdt->mdt_max_mdsize =
825 info->mti_attr.ma_lmm_size;
826 info->mti_big_lmm_used = 0;
829 if (info->mti_big_acl_used) {
830 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
833 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
835 body->mbo_valid &= ~OBD_MD_FLACL;
837 void *acl = req_capsule_server_get(pill, &RMF_ACL);
839 memcpy(acl, info->mti_big_acl, acl_size);
842 info->mti_big_acl_used = 0;
849 /* if object is dying, pack the lov/llog data,
850 * parameter info->mti_attr should be valid at this point!
851 * Also implements RAoLU policy */
852 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
855 struct mdt_body *repbody = NULL;
856 const struct lu_attr *la = &ma->ma_attr;
857 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
860 struct hsm_action_item hai = {
861 .hai_len = sizeof(hai),
862 .hai_action = HSMA_REMOVE,
863 .hai_extent.length = -1,
871 if (mdt_info_req(info) != NULL) {
872 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
873 LASSERT(repbody != NULL);
875 CDEBUG(D_INFO, "not running in a request/reply context\n");
878 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
879 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
881 if (ma->ma_valid & MA_LOV) {
882 CERROR("No need in LOV EA upon unlink\n");
886 repbody->mbo_eadatasize = 0;
888 /* Only check unlinked and archived if RAoLU and upon last close */
889 if (!cdt->cdt_remove_archive_on_last_unlink ||
890 atomic_read(&mo->mot_open_count) != 0)
893 /* mdt_attr_get_complex will clear ma_valid, so check here first */
894 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
897 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
900 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
902 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
903 * try setting them */
905 rc = mdt_attr_get_complex(info, mo, ma);
907 CERROR("%s: unable to fetch missing attributes of"
908 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
909 PFID(mdt_object_fid(mo)), rc);
913 if (need & MA_INODE) {
914 if (ma->ma_valid & MA_INODE) {
915 if (ma->ma_attr.la_nlink != 0)
923 if (ma->ma_valid & MA_HSM) {
924 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
932 /* RAoLU policy is active, last close on file has occured,
933 * file is unlinked, file is archived, so create remove request
935 * If CDT is not running, requests will be logged for later. */
936 if (ma->ma_hsm.mh_arch_id != 0)
937 archive_id = ma->ma_hsm.mh_arch_id;
939 archive_id = cdt->cdt_default_archive_id;
941 hai.hai_fid = *mdt_object_fid(mo);
943 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
946 CERROR("%s: unable to add HSM remove request for "DFID
947 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
948 PFID(mdt_object_fid(mo)), rc);
953 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
959 if (in & MDS_ATTR_MODE)
961 if (in & MDS_ATTR_UID)
963 if (in & MDS_ATTR_GID)
965 if (in & MDS_ATTR_SIZE)
967 if (in & MDS_ATTR_BLOCKS)
969 if (in & MDS_ATTR_ATIME_SET)
971 if (in & MDS_ATTR_CTIME_SET)
973 if (in & MDS_ATTR_MTIME_SET)
975 if (in & MDS_ATTR_ATTR_FLAG)
977 if (in & MDS_ATTR_KILL_SUID)
979 if (in & MDS_ATTR_KILL_SGID)
981 if (in & MDS_ATTR_PROJID)
983 if (in & MDS_ATTR_LSIZE)
985 if (in & MDS_ATTR_LBLOCKS)
988 if (in & MDS_ATTR_FROM_OPEN)
989 rr->rr_flags |= MRF_OPEN_TRUNC;
990 if (in & MDS_ATTR_OVERRIDE)
991 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
992 if (in & MDS_ATTR_FORCE)
993 ma->ma_attr_flags |= MDS_PERM_BYPASS;
995 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
996 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
997 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
998 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
999 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1000 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1003 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", in);
1009 int mdt_name_unpack(struct req_capsule *pill,
1010 const struct req_msg_field *field,
1012 enum mdt_name_flags flags)
1014 ln->ln_name = req_capsule_client_get(pill, field);
1015 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1017 if (!lu_name_is_valid(ln)) {
1024 if ((flags & MNF_FIX_ANON) &&
1025 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1026 /* Newer (3.x) kernels use a name of "/" for the
1027 * "anonymous" disconnected dentries from NFS
1028 * filehandle conversion. See d_obtain_alias(). */
1036 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1037 const char **secctx_name,
1038 void **secctx, size_t *secctx_size)
1043 *secctx_name = NULL;
1047 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1048 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1051 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1056 if (name_size > XATTR_NAME_MAX + 1)
1059 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1060 if (strnlen(name, name_size) != name_size - 1)
1063 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1064 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1067 *secctx_name = name;
1068 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1069 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1074 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1076 struct lu_ucred *uc = mdt_ucred(info);
1077 struct md_attr *ma = &info->mti_attr;
1078 struct lu_attr *la = &ma->ma_attr;
1079 struct req_capsule *pill = info->mti_pill;
1080 struct mdt_reint_record *rr = &info->mti_rr;
1081 struct mdt_rec_setattr *rec;
1082 struct lu_nodemap *nodemap;
1086 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1087 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1091 /* This prior initialization is needed for old_init_ucred_reint() */
1092 uc->uc_fsuid = rec->sa_fsuid;
1093 uc->uc_fsgid = rec->sa_fsgid;
1094 uc->uc_cap = rec->sa_cap;
1095 uc->uc_suppgids[0] = rec->sa_suppgid;
1096 uc->uc_suppgids[1] = -1;
1098 rr->rr_fid1 = &rec->sa_fid;
1099 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1100 la->la_mode = rec->sa_mode;
1101 la->la_flags = rec->sa_attr_flags;
1103 nodemap = nodemap_get_from_exp(info->mti_exp);
1104 if (IS_ERR(nodemap))
1105 RETURN(PTR_ERR(nodemap));
1107 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1108 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1109 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1110 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1111 la->la_projid = rec->sa_projid;
1112 nodemap_putref(nodemap);
1114 la->la_size = rec->sa_size;
1115 la->la_blocks = rec->sa_blocks;
1116 la->la_ctime = rec->sa_ctime;
1117 la->la_atime = rec->sa_atime;
1118 la->la_mtime = rec->sa_mtime;
1119 ma->ma_valid = MA_INODE;
1121 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1122 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE);
1126 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1128 struct req_capsule *pill = info->mti_pill;
1129 struct mdt_ioepoch *ioepoch;
1132 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1133 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1137 if (ioepoch == NULL)
1140 info->mti_open_handle = ioepoch->mio_open_handle;
1145 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1146 struct req_capsule *pill = info->mti_pill;
1148 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1149 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1150 if (info->mti_dlm_req == NULL)
1157 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1159 struct mdt_reint_record *rr = &info->mti_rr;
1160 struct md_attr *ma = &info->mti_attr;
1161 struct req_capsule *pill = info->mti_pill;
1165 rc = mdt_setattr_unpack_rec(info);
1169 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1170 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1171 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1173 if (rr->rr_eadatalen > 0) {
1174 const struct lmv_user_md *lum;
1176 lum = rr->rr_eadata;
1177 /* Sigh ma_valid(from req) does not indicate whether
1178 * it will set LOV/LMV EA, so we have to check magic */
1179 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1180 ma->ma_valid |= MA_LMV;
1181 ma->ma_lmv = (void *)rr->rr_eadata;
1182 ma->ma_lmv_size = rr->rr_eadatalen;
1184 ma->ma_valid |= MA_LOV;
1185 ma->ma_lmm = (void *)rr->rr_eadata;
1186 ma->ma_lmm_size = rr->rr_eadatalen;
1191 rc = mdt_dlmreq_unpack(info);
1195 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1197 struct md_attr *ma = &info->mti_attr;
1198 struct req_capsule *pill = info->mti_pill;
1201 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1204 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1206 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1207 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1213 int mdt_close_unpack(struct mdt_thread_info *info)
1218 rc = mdt_close_handle_unpack(info);
1222 rc = mdt_setattr_unpack_rec(info);
1226 rc = mdt_close_intent_unpack(info);
1230 RETURN(mdt_init_ucred_reint(info));
1233 static int mdt_create_unpack(struct mdt_thread_info *info)
1235 struct lu_ucred *uc = mdt_ucred(info);
1236 struct mdt_rec_create *rec;
1237 struct lu_attr *attr = &info->mti_attr.ma_attr;
1238 struct mdt_reint_record *rr = &info->mti_rr;
1239 struct req_capsule *pill = info->mti_pill;
1240 struct md_op_spec *sp = &info->mti_spec;
1245 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1246 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1250 /* This prior initialization is needed for old_init_ucred_reint() */
1251 uc->uc_fsuid = rec->cr_fsuid;
1252 uc->uc_fsgid = rec->cr_fsgid;
1253 uc->uc_cap = rec->cr_cap;
1254 uc->uc_suppgids[0] = rec->cr_suppgid1;
1255 uc->uc_suppgids[1] = -1;
1256 uc->uc_umask = rec->cr_umask;
1258 rr->rr_fid1 = &rec->cr_fid1;
1259 rr->rr_fid2 = &rec->cr_fid2;
1260 attr->la_mode = rec->cr_mode;
1261 attr->la_rdev = rec->cr_rdev;
1262 attr->la_uid = rec->cr_fsuid;
1263 attr->la_gid = rec->cr_fsgid;
1264 attr->la_ctime = rec->cr_time;
1265 attr->la_mtime = rec->cr_time;
1266 attr->la_atime = rec->cr_time;
1267 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1268 LA_CTIME | LA_MTIME | LA_ATIME;
1269 memset(&sp->u, 0, sizeof(sp->u));
1270 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1272 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1276 if (S_ISLNK(attr->la_mode)) {
1277 const char *tgt = NULL;
1279 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1280 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1281 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1282 sp->u.sp_symname = tgt;
1287 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1288 if (S_ISDIR(attr->la_mode) &&
1289 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1290 sp->u.sp_ea.eadata =
1291 req_capsule_client_get(pill, &RMF_EADATA);
1292 sp->u.sp_ea.eadatalen =
1293 req_capsule_get_size(pill, &RMF_EADATA,
1295 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1299 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1300 &sp->sp_cr_file_secctx,
1301 &sp->sp_cr_file_secctx_size);
1305 rc = req_check_sepol(pill);
1309 rc = mdt_dlmreq_unpack(info);
1313 static int mdt_link_unpack(struct mdt_thread_info *info)
1315 struct lu_ucred *uc = mdt_ucred(info);
1316 struct mdt_rec_link *rec;
1317 struct lu_attr *attr = &info->mti_attr.ma_attr;
1318 struct mdt_reint_record *rr = &info->mti_rr;
1319 struct req_capsule *pill = info->mti_pill;
1324 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1325 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1329 /* This prior initialization is needed for old_init_ucred_reint() */
1330 uc->uc_fsuid = rec->lk_fsuid;
1331 uc->uc_fsgid = rec->lk_fsgid;
1332 uc->uc_cap = rec->lk_cap;
1333 uc->uc_suppgids[0] = rec->lk_suppgid1;
1334 uc->uc_suppgids[1] = rec->lk_suppgid2;
1336 attr->la_uid = rec->lk_fsuid;
1337 attr->la_gid = rec->lk_fsgid;
1338 rr->rr_fid1 = &rec->lk_fid1;
1339 rr->rr_fid2 = &rec->lk_fid2;
1340 attr->la_ctime = rec->lk_time;
1341 attr->la_mtime = rec->lk_time;
1342 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1344 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1348 rc = req_check_sepol(pill);
1352 rc = mdt_dlmreq_unpack(info);
1357 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1359 struct lu_ucred *uc = mdt_ucred(info);
1360 struct mdt_rec_unlink *rec;
1361 struct lu_attr *attr = &info->mti_attr.ma_attr;
1362 struct mdt_reint_record *rr = &info->mti_rr;
1363 struct req_capsule *pill = info->mti_pill;
1368 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1369 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1373 /* This prior initialization is needed for old_init_ucred_reint() */
1374 uc->uc_fsuid = rec->ul_fsuid;
1375 uc->uc_fsgid = rec->ul_fsgid;
1376 uc->uc_cap = rec->ul_cap;
1377 uc->uc_suppgids[0] = rec->ul_suppgid1;
1378 uc->uc_suppgids[1] = -1;
1380 attr->la_uid = rec->ul_fsuid;
1381 attr->la_gid = rec->ul_fsgid;
1382 rr->rr_fid1 = &rec->ul_fid1;
1383 rr->rr_fid2 = &rec->ul_fid2;
1384 attr->la_ctime = rec->ul_time;
1385 attr->la_mtime = rec->ul_time;
1386 attr->la_mode = rec->ul_mode;
1387 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1389 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1393 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1395 rc = req_check_sepol(pill);
1399 rc = mdt_dlmreq_unpack(info);
1403 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1405 info->mti_spec.sp_rm_entry = 1;
1406 return mdt_unlink_unpack(info);
1409 static int mdt_rename_unpack(struct mdt_thread_info *info)
1411 struct lu_ucred *uc = mdt_ucred(info);
1412 struct mdt_rec_rename *rec;
1413 struct lu_attr *attr = &info->mti_attr.ma_attr;
1414 struct mdt_reint_record *rr = &info->mti_rr;
1415 struct req_capsule *pill = info->mti_pill;
1416 struct md_op_spec *spec = &info->mti_spec;
1421 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1422 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1426 /* This prior initialization is needed for old_init_ucred_reint() */
1427 uc->uc_fsuid = rec->rn_fsuid;
1428 uc->uc_fsgid = rec->rn_fsgid;
1429 uc->uc_cap = rec->rn_cap;
1430 uc->uc_suppgids[0] = rec->rn_suppgid1;
1431 uc->uc_suppgids[1] = rec->rn_suppgid2;
1433 attr->la_uid = rec->rn_fsuid;
1434 attr->la_gid = rec->rn_fsgid;
1435 rr->rr_fid1 = &rec->rn_fid1;
1436 rr->rr_fid2 = &rec->rn_fid2;
1437 attr->la_ctime = rec->rn_time;
1438 attr->la_mtime = rec->rn_time;
1439 /* rename_tgt contains the mode already */
1440 attr->la_mode = rec->rn_mode;
1441 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1443 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1447 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1451 spec->no_create = !!req_is_replay(mdt_info_req(info));
1453 rc = req_check_sepol(pill);
1457 rc = mdt_dlmreq_unpack(info);
1462 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1464 struct lu_ucred *uc = mdt_ucred(info);
1465 struct mdt_rec_rename *rec;
1466 struct lu_attr *attr = &info->mti_attr.ma_attr;
1467 struct mdt_reint_record *rr = &info->mti_rr;
1468 struct req_capsule *pill = info->mti_pill;
1469 struct md_op_spec *spec = &info->mti_spec;
1474 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1475 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1479 /* This prior initialization is needed for old_init_ucred_reint() */
1480 uc->uc_fsuid = rec->rn_fsuid;
1481 uc->uc_fsgid = rec->rn_fsgid;
1482 uc->uc_cap = rec->rn_cap;
1483 uc->uc_suppgids[0] = rec->rn_suppgid1;
1484 uc->uc_suppgids[1] = rec->rn_suppgid2;
1486 attr->la_uid = rec->rn_fsuid;
1487 attr->la_gid = rec->rn_fsgid;
1488 rr->rr_fid1 = &rec->rn_fid1;
1489 rr->rr_fid2 = &rec->rn_fid2;
1490 attr->la_ctime = rec->rn_time;
1491 attr->la_mtime = rec->rn_time;
1492 /* rename_tgt contains the mode already */
1493 attr->la_mode = rec->rn_mode;
1494 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1495 spec->sp_cr_flags = 0;
1497 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1501 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1502 rc = mdt_close_handle_unpack(info);
1506 spec->sp_migrate_close = 1;
1509 /* lustre version > 2.11 migration packs lum */
1510 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1511 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1512 rr->rr_eadatalen = req_capsule_get_size(pill,
1515 if (rr->rr_eadatalen > 0) {
1516 rr->rr_eadata = req_capsule_client_get(pill,
1518 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1519 spec->u.sp_ea.eadata = rr->rr_eadata;
1520 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1523 /* old client doesn't provide lum. */
1524 RETURN(-EOPNOTSUPP);
1528 spec->no_create = !!req_is_replay(mdt_info_req(info));
1530 rc = mdt_dlmreq_unpack(info);
1536 * please see comment above LOV_MAGIC_V1_DEFINED
1538 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1540 struct lov_user_md_v1 *v1 = eadata;
1544 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1545 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1546 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1547 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1548 __swab32(LOV_MAGIC_MAGIC))
1549 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1553 static int mdt_open_unpack(struct mdt_thread_info *info)
1555 struct lu_ucred *uc = mdt_ucred(info);
1556 struct mdt_rec_create *rec;
1557 struct lu_attr *attr = &info->mti_attr.ma_attr;
1558 struct req_capsule *pill = info->mti_pill;
1559 struct mdt_reint_record *rr = &info->mti_rr;
1560 struct ptlrpc_request *req = mdt_info_req(info);
1561 struct md_op_spec *sp = &info->mti_spec;
1565 CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1566 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1570 /* This prior initialization is needed for old_init_ucred_reint() */
1571 uc->uc_fsuid = rec->cr_fsuid;
1572 uc->uc_fsgid = rec->cr_fsgid;
1573 uc->uc_cap = rec->cr_cap;
1574 uc->uc_suppgids[0] = rec->cr_suppgid1;
1575 uc->uc_suppgids[1] = rec->cr_suppgid2;
1576 uc->uc_umask = rec->cr_umask;
1578 rr->rr_fid1 = &rec->cr_fid1;
1579 rr->rr_fid2 = &rec->cr_fid2;
1580 rr->rr_open_handle = &rec->cr_open_handle_old;
1581 attr->la_mode = rec->cr_mode;
1582 attr->la_rdev = rec->cr_rdev;
1583 attr->la_uid = rec->cr_fsuid;
1584 attr->la_gid = rec->cr_fsgid;
1585 attr->la_ctime = rec->cr_time;
1586 attr->la_mtime = rec->cr_time;
1587 attr->la_atime = rec->cr_time;
1588 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1589 LA_CTIME | LA_MTIME | LA_ATIME;
1590 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1591 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1592 /* Do not trigger ASSERTION if client miss to set such flags. */
1593 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1596 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1598 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1600 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1601 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1603 if (rr->rr_eadatalen > 0) {
1604 rr->rr_eadata = req_capsule_client_get(pill,
1606 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1607 sp->u.sp_ea.eadata = rr->rr_eadata;
1608 sp->no_create = !!req_is_replay(req);
1609 mdt_fix_lov_magic(info, rr->rr_eadata);
1613 * Client default md_size may be 0 right after client start,
1614 * until all osc are connected, set here just some reasonable
1615 * value to prevent misbehavior.
1617 if (rr->rr_eadatalen == 0 &&
1618 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1619 rr->rr_eadatalen = MIN_MD_SIZE;
1622 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1623 &sp->sp_cr_file_secctx,
1624 &sp->sp_cr_file_secctx_size);
1628 rc = req_check_sepol(pill);
1635 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1637 struct mdt_reint_record *rr = &info->mti_rr;
1638 struct lu_ucred *uc = mdt_ucred(info);
1639 struct lu_attr *attr = &info->mti_attr.ma_attr;
1640 struct req_capsule *pill = info->mti_pill;
1641 struct mdt_rec_setxattr *rec;
1646 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1647 sizeof(struct mdt_rec_reint));
1649 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1653 /* This prior initialization is needed for old_init_ucred_reint() */
1654 uc->uc_fsuid = rec->sx_fsuid;
1655 uc->uc_fsgid = rec->sx_fsgid;
1656 uc->uc_cap = rec->sx_cap;
1657 uc->uc_suppgids[0] = rec->sx_suppgid1;
1658 uc->uc_suppgids[1] = -1;
1660 rr->rr_opcode = rec->sx_opcode;
1661 rr->rr_fid1 = &rec->sx_fid;
1662 attr->la_valid = rec->sx_valid;
1663 attr->la_ctime = rec->sx_time;
1664 attr->la_size = rec->sx_size;
1665 attr->la_flags = rec->sx_flags;
1667 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1671 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1672 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1674 if (rr->rr_eadatalen > 0) {
1675 rr->rr_eadata = req_capsule_client_get(pill,
1677 if (rr->rr_eadata == NULL)
1680 rr->rr_eadata = NULL;
1682 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1683 CDEBUG(D_INFO, "no xattr data supplied\n");
1687 rc = req_check_sepol(pill);
1691 if (mdt_dlmreq_unpack(info) < 0)
1697 static int mdt_resync_unpack(struct mdt_thread_info *info)
1699 struct req_capsule *pill = info->mti_pill;
1700 struct mdt_reint_record *rr = &info->mti_rr;
1701 struct lu_ucred *uc = mdt_ucred(info);
1702 struct mdt_rec_resync *rec;
1705 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1706 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1710 /* This prior initialization is needed for old_init_ucred_reint() */
1711 uc->uc_fsuid = rec->rs_fsuid;
1712 uc->uc_fsgid = rec->rs_fsgid;
1713 uc->uc_cap = rec->rs_cap;
1715 rr->rr_fid1 = &rec->rs_fid;
1716 rr->rr_mirror_id = rec->rs_mirror_id;
1718 /* cookie doesn't need to be swapped but it has been swapped
1719 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1721 if (ptlrpc_req_need_swab(mdt_info_req(info)))
1722 __swab64s(&rec->rs_lease_handle.cookie);
1723 rr->rr_lease_handle = &rec->rs_lease_handle;
1725 RETURN(mdt_dlmreq_unpack(info));
1728 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1730 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1731 [REINT_SETATTR] = mdt_setattr_unpack,
1732 [REINT_CREATE] = mdt_create_unpack,
1733 [REINT_LINK] = mdt_link_unpack,
1734 [REINT_UNLINK] = mdt_unlink_unpack,
1735 [REINT_RENAME] = mdt_rename_unpack,
1736 [REINT_OPEN] = mdt_open_unpack,
1737 [REINT_SETXATTR] = mdt_setxattr_unpack,
1738 [REINT_RMENTRY] = mdt_rmentry_unpack,
1739 [REINT_MIGRATE] = mdt_migrate_unpack,
1740 [REINT_RESYNC] = mdt_resync_unpack,
1743 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1748 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1749 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1750 info->mti_rr.rr_opcode = op;
1751 rc = mdt_reint_unpackers[op](info);
1753 CERROR("Unexpected opcode %d\n", op);
1759 /* check whether two FIDs belong to different MDT. */
1760 static int mdt_fids_different_target(struct mdt_thread_info *info,
1761 const struct lu_fid *fid1,
1762 const struct lu_fid *fid2)
1764 const struct lu_env *env = info->mti_env;
1765 struct mdt_device *mdt = info->mti_mdt;
1766 struct lu_seq_range *range = &info->mti_range;
1767 struct seq_server_site *ss;
1768 __u32 index1, index2;
1771 if (fid_seq(fid1) == fid_seq(fid2))
1774 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1776 range->lsr_flags = LU_SEQ_RANGE_MDT;
1777 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1781 index1 = range->lsr_index;
1783 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1787 index2 = range->lsr_index;
1789 return index1 != index2;
1793 * Check whether \a child is remote object on \a parent.
1795 * \param[in] info thread environment
1796 * \param[in] parent parent object, it's the same as child object in
1798 * \param[in] child child object
1800 * \retval 1 is remote object.
1801 * \retval 0 isn't remote object.
1802 * \retval < 1 error code
1804 int mdt_is_remote_object(struct mdt_thread_info *info,
1805 struct mdt_object *parent,
1806 struct mdt_object *child)
1808 struct lu_buf *buf = &info->mti_big_buf;
1809 struct linkea_data ldata = { NULL };
1810 struct link_ea_header *leh;
1811 struct link_ea_entry *lee;
1812 struct lu_name name;
1820 if (fid_is_root(mdt_object_fid(child)))
1823 if (likely(parent != child)) {
1824 if (mdt_object_remote(parent) ^ mdt_object_remote(child))
1827 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1830 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1831 mdt_object_fid(child));
1835 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1836 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1841 rc = mdt_links_read(info, child, &ldata);
1842 /* can't read linkea, just assume it's remote object */
1843 if (rc == -ENOENT || rc == -ENODATA)
1849 lee = (struct link_ea_entry *)(leh + 1);
1850 for (i = 0; i < leh->leh_reccount; i++) {
1851 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1852 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1853 if (mdt_fids_different_target(info, &pfid,
1854 mdt_object_fid(child)))