4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_lib.c
33 * Lustre Metadata Target (mdt) request unpacking helper.
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Phil Schwan <phil@clusterfs.com>
38 * Author: Mike Shaver <shaver@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Fan Yong <fanyong@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/user_namespace.h>
47 #include <linux/uidgid.h>
49 #include "mdt_internal.h"
50 #include <uapi/linux/lnet/nidstr.h>
51 #include <lustre_nodemap.h>
53 typedef enum ucred_init_type {
59 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
61 return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
64 void mdt_exit_ucred(struct mdt_thread_info *info)
66 struct lu_ucred *uc = mdt_ucred(info);
67 struct mdt_device *mdt = info->mti_mdt;
70 if (uc->uc_valid != UCRED_INIT) {
71 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
73 put_group_info(uc->uc_ginfo);
76 if (uc->uc_identity) {
77 mdt_identity_put(mdt->mdt_identity_cache,
79 uc->uc_identity = NULL;
81 uc->uc_valid = UCRED_INIT;
85 static int match_nosquash_list(struct spinlock *rsi_lock,
86 struct list_head *nidlist,
92 rc = cfs_match_nid(peernid, nidlist);
93 spin_unlock(rsi_lock);
97 /* root_squash for inter-MDS operations */
98 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
100 struct lu_ucred *ucred = mdt_ucred(info);
101 struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
104 LASSERT(ucred != NULL);
105 if (!squash->rsi_uid || ucred->uc_fsuid)
108 if (match_nosquash_list(&squash->rsi_lock,
109 &squash->rsi_nosquash_nids,
111 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
112 libcfs_nid2str(peernid));
116 CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
117 libcfs_nid2str(peernid),
118 ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap.cap[0],
119 squash->rsi_uid, squash->rsi_gid, 0);
121 ucred->uc_fsuid = squash->rsi_uid;
122 ucred->uc_fsgid = squash->rsi_gid;
123 ucred->uc_cap = CAP_EMPTY_SET;
124 ucred->uc_suppgids[0] = -1;
125 ucred->uc_suppgids[1] = -1;
130 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
132 struct ptlrpc_request *req = mdt_info_req(info);
133 const char *jobid = mdt_req_get_jobid(req);
135 /* set jobid if specified. */
137 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
139 uc->uc_jobid[0] = '\0';
142 static void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
144 if (info && info->mti_exp && info->mti_exp->exp_connection)
145 uc->uc_nid = lnet_nid_to_nid4(
146 &info->mti_exp->exp_connection->c_peer.nid);
148 uc->uc_nid = LNET_NID_ANY;
151 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
154 struct lu_nodemap *nodemap = NULL;
157 if (info && info->mti_exp) {
158 nodemap = nodemap_get_from_exp(info->mti_exp);
159 if (nodemap && !IS_ERR(nodemap)) {
160 audit = nodemap->nmf_enable_audit;
161 nodemap_putref(nodemap);
165 uc->uc_enable_audit = audit;
168 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
171 struct ptlrpc_request *req = mdt_info_req(info);
172 struct mdt_device *mdt = info->mti_mdt;
173 struct ptlrpc_user_desc *pud = req->rq_user_desc;
174 struct lu_ucred *ucred = mdt_ucred(info);
175 struct lu_nodemap *nodemap;
176 lnet_nid_t peernid = lnet_nid_to_nid4(&req->rq_peer.nid);
180 bool is_nm_gid_squashed = false;
185 LASSERT(req->rq_auth_gss);
186 LASSERT(!req->rq_auth_usr_mdt);
187 LASSERT(req->rq_user_desc);
188 LASSERT(ucred != NULL);
190 ucred->uc_valid = UCRED_INVALID;
192 nodemap = nodemap_get_from_exp(info->mti_exp);
194 RETURN(PTR_ERR(nodemap));
196 pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
197 NODEMAP_CLIENT_TO_FS, pud->pud_uid);
198 pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
199 NODEMAP_CLIENT_TO_FS, pud->pud_gid);
200 pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
201 NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
202 pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
203 NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
205 ucred->uc_o_uid = pud->pud_uid;
206 ucred->uc_o_gid = pud->pud_gid;
207 ucred->uc_o_fsuid = pud->pud_fsuid;
208 ucred->uc_o_fsgid = pud->pud_fsgid;
210 if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
211 /* deny access before we get identity ref */
212 if (nodemap->nmf_deny_unknown) {
213 nodemap_putref(nodemap);
217 ucred->uc_suppgids[0] = -1;
218 ucred->uc_suppgids[1] = -1;
221 if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
222 is_nm_gid_squashed = true;
224 nodemap_putref(nodemap);
226 if (type == BODY_INIT) {
227 struct mdt_body *body = (struct mdt_body *)buf;
229 ucred->uc_suppgids[0] = body->mbo_suppgid;
230 ucred->uc_suppgids[1] = -1;
233 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
234 req->rq_auth_uid != pud->pud_uid) {
235 CDEBUG(D_SEC, "local client %s: auth uid %u "
236 "while client claims %u:%u/%u:%u\n",
237 libcfs_nid2str(peernid), req->rq_auth_uid,
238 pud->pud_uid, pud->pud_gid,
239 pud->pud_fsuid, pud->pud_fsgid);
243 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
244 ucred->uc_identity = NULL;
245 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
247 struct md_identity *identity;
249 identity = mdt_identity_get(mdt->mdt_identity_cache,
251 if (IS_ERR(identity)) {
252 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
253 ucred->uc_identity = NULL;
254 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
258 "Deny access without identity: uid %u\n",
263 ucred->uc_identity = identity;
264 perm = mdt_identity_get_perm(ucred->uc_identity,
269 /* find out the setuid/setgid attempt */
270 setuid = (pud->pud_uid != pud->pud_fsuid);
271 setgid = ((pud->pud_gid != pud->pud_fsgid) ||
272 (ucred->uc_identity &&
273 (pud->pud_gid != ucred->uc_identity->mi_gid)));
275 /* check permission of setuid */
276 if (setuid && !(perm & CFS_SETUID_PERM)) {
277 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
278 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
279 GOTO(out, rc = -EACCES);
282 /* check permission of setgid */
283 if (setgid && !(perm & CFS_SETGID_PERM)) {
284 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
285 "from %s\n", pud->pud_uid, pud->pud_gid,
286 pud->pud_fsuid, pud->pud_fsgid,
287 ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
288 GOTO(out, rc = -EACCES);
291 if (perm & CFS_SETGRP_PERM) {
292 /* only set groups if GID is not squashed */
293 if (pud->pud_ngroups && !is_nm_gid_squashed) {
294 /* setgroups for local client */
295 ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
296 if (!ucred->uc_ginfo) {
297 CERROR("failed to alloc %d groups\n",
299 GOTO(out, rc = -ENOMEM);
302 lustre_groups_from_list(ucred->uc_ginfo,
304 lustre_groups_sort(ucred->uc_ginfo);
306 ucred->uc_suppgids[0] = -1;
307 ucred->uc_suppgids[1] = -1;
308 ucred->uc_ginfo = NULL;
311 ucred->uc_suppgids[0] = -1;
312 ucred->uc_suppgids[1] = -1;
313 ucred->uc_ginfo = NULL;
316 ucred->uc_uid = pud->pud_uid;
317 ucred->uc_gid = pud->pud_gid;
319 ucred->uc_cap = CAP_EMPTY_SET;
320 if (!nodemap || ucred->uc_o_uid != nodemap->nm_squash_uid)
321 ucred->uc_cap.cap[0] = pud->pud_cap;
323 ucred->uc_fsuid = pud->pud_fsuid;
324 ucred->uc_fsgid = pud->pud_fsgid;
326 /* process root_squash here. */
327 mdt_root_squash(info, peernid);
329 ucred->uc_valid = UCRED_NEW;
330 ucred_set_jobid(info, ucred);
331 ucred_set_nid(info, ucred);
332 ucred_set_audit_enabled(info, ucred);
338 if (ucred->uc_ginfo) {
339 put_group_info(ucred->uc_ginfo);
340 ucred->uc_ginfo = NULL;
342 if (ucred->uc_identity) {
343 mdt_identity_put(mdt->mdt_identity_cache,
345 ucred->uc_identity = NULL;
353 * Check whether allow the client to set supplementary group IDs or not.
355 * \param[in] info pointer to the thread context
356 * \param[in] uc pointer to the RPC user descriptor
358 * \retval true if allow to set supplementary group IDs
359 * \retval false for other cases
361 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
365 /* 1. If identity_upcall is disabled,
366 * permit local client to do anything. */
367 if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
370 /* 2. If fail to get related identities, then forbid any client to
371 * set supplementary group IDs. */
372 if (uc->uc_identity == NULL)
375 /* 3. Check the permission in the identities. */
376 perm = mdt_identity_get_perm(
378 lnet_nid_to_nid4(&mdt_info_req(info)->rq_peer.nid));
379 if (perm & CFS_SETGRP_PERM)
385 int mdt_check_ucred(struct mdt_thread_info *info)
387 struct ptlrpc_request *req = mdt_info_req(info);
388 struct mdt_device *mdt = info->mti_mdt;
389 struct ptlrpc_user_desc *pud = req->rq_user_desc;
390 struct lu_ucred *ucred = mdt_ucred(info);
391 struct md_identity *identity = NULL;
392 lnet_nid_t peernid = lnet_nid_to_nid4(&req->rq_peer.nid);
400 LASSERT(ucred != NULL);
401 if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
404 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
407 /* sanity check: if we use strong authentication, we expect the
408 * uid which client claimed is true */
409 if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
410 req->rq_auth_uid != pud->pud_uid) {
411 CDEBUG(D_SEC, "local client %s: auth uid %u "
412 "while client claims %u:%u/%u:%u\n",
413 libcfs_nid2str(peernid), req->rq_auth_uid,
414 pud->pud_uid, pud->pud_gid,
415 pud->pud_fsuid, pud->pud_fsgid);
419 if (is_identity_get_disabled(mdt->mdt_identity_cache))
422 identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
423 if (IS_ERR(identity)) {
424 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
427 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
433 perm = mdt_identity_get_perm(identity, peernid);
434 /* find out the setuid/setgid attempt */
435 setuid = (pud->pud_uid != pud->pud_fsuid);
436 setgid = (pud->pud_gid != pud->pud_fsgid ||
437 pud->pud_gid != identity->mi_gid);
439 /* check permission of setuid */
440 if (setuid && !(perm & CFS_SETUID_PERM)) {
441 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
442 pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
443 GOTO(out, rc = -EACCES);
446 /* check permission of setgid */
447 if (setgid && !(perm & CFS_SETGID_PERM)) {
448 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
449 "from %s\n", pud->pud_uid, pud->pud_gid,
450 pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
451 libcfs_nid2str(peernid));
452 GOTO(out, rc = -EACCES);
458 mdt_identity_put(mdt->mdt_identity_cache, identity);
462 static int old_init_ucred_common(struct mdt_thread_info *info,
463 struct lu_nodemap *nodemap)
465 struct lu_ucred *uc = mdt_ucred(info);
466 struct mdt_device *mdt = info->mti_mdt;
467 struct md_identity *identity = NULL;
469 if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
470 /* deny access before we get identity ref */
471 if (nodemap->nmf_deny_unknown)
474 uc->uc_cap = CAP_EMPTY_SET;
475 uc->uc_suppgids[0] = -1;
476 uc->uc_suppgids[1] = -1;
479 if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
480 identity = mdt_identity_get(mdt->mdt_identity_cache,
482 if (IS_ERR(identity)) {
483 if (unlikely(PTR_ERR(identity) == -EREMCHG ||
484 cap_raised(uc->uc_cap,
485 CAP_DAC_READ_SEARCH))) {
488 CDEBUG(D_SEC, "Deny access without identity: "
489 "uid %u\n", uc->uc_fsuid);
494 uc->uc_identity = identity;
496 /* process root_squash here. */
497 mdt_root_squash(info,
498 lnet_nid_to_nid4(&mdt_info_req(info)->rq_peer.nid));
500 uc->uc_valid = UCRED_OLD;
501 ucred_set_jobid(info, uc);
502 ucred_set_nid(info, uc);
503 ucred_set_audit_enabled(info, uc);
510 static int old_init_ucred(struct mdt_thread_info *info,
511 struct mdt_body *body)
513 struct lu_ucred *uc = mdt_ucred(info);
514 struct lu_nodemap *nodemap;
518 nodemap = nodemap_get_from_exp(info->mti_exp);
520 RETURN(PTR_ERR(nodemap));
522 body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
523 NODEMAP_CLIENT_TO_FS, body->mbo_uid);
524 body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
525 NODEMAP_CLIENT_TO_FS, body->mbo_gid);
526 body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
527 NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
528 body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
529 NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
532 uc->uc_valid = UCRED_INVALID;
533 uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
534 uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
535 uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
536 uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
537 uc->uc_suppgids[0] = body->mbo_suppgid;
538 uc->uc_suppgids[1] = -1;
540 uc->uc_cap = CAP_EMPTY_SET;
541 uc->uc_cap.cap[0] = body->mbo_capability;
543 rc = old_init_ucred_common(info, nodemap);
544 nodemap_putref(nodemap);
549 static int old_init_ucred_reint(struct mdt_thread_info *info)
551 struct lu_ucred *uc = mdt_ucred(info);
552 struct lu_nodemap *nodemap;
556 nodemap = nodemap_get_from_exp(info->mti_exp);
558 RETURN(PTR_ERR(nodemap));
562 uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
563 NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
564 uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
565 NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
567 uc->uc_valid = UCRED_INVALID;
568 uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
569 uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
572 rc = old_init_ucred_common(info, nodemap);
573 nodemap_putref(nodemap);
578 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
579 struct mdt_body *body)
581 struct ptlrpc_request *req = mdt_info_req(info);
582 struct lu_ucred *uc = mdt_ucred(info);
585 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
588 mdt_exit_ucred(info);
590 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
591 return old_init_ucred(info, body);
593 return new_init_ucred(info, BODY_INIT, body);
596 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
598 return __mdt_init_ucred(info, body);
601 int mdt_init_ucred_reint(struct mdt_thread_info *info)
603 struct ptlrpc_request *req = mdt_info_req(info);
604 struct lu_ucred *uc = mdt_ucred(info);
605 struct md_attr *ma = &info->mti_attr;
608 if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
611 /* LU-5564: for normal close request, skip permission check */
612 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
613 !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP))) {
614 cap_raise_nfsd_set(uc->uc_cap, CAP_FULL_SET);
615 cap_raise_fs_set(uc->uc_cap, CAP_FULL_SET);
618 mdt_exit_ucred(info);
620 if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
621 return old_init_ucred_reint(info);
623 return new_init_ucred(info, REC_INIT, NULL);
626 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
627 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
629 const struct lov_ost_data_v1 *lod;
630 __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
634 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
637 CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
638 POSTID(&lmm->lmm_oi), lmm_magic,
639 le32_to_cpu(lmm->lmm_pattern));
641 /* No support for compount layouts yet */
642 if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
645 count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
646 CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
647 le32_to_cpu(lmm->lmm_stripe_size), count);
649 /* If it's a directory or a released file, then there are
650 * no actual objects to print, so bail out. */
651 if (valid & OBD_MD_FLDIREA ||
652 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
655 LASSERT(count <= LOV_MAX_STRIPE_COUNT);
656 for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
659 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
660 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
661 i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
665 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
667 const struct lmv_mds_md_v1 *lmm1;
668 const struct lmv_foreign_md *lfm;
671 if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
674 /* foreign LMV case */
675 lfm = &lmv->lmv_foreign_md;
676 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
678 "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
679 le32_to_cpu(lfm->lfm_magic),
680 le32_to_cpu(lfm->lfm_length),
681 le32_to_cpu(lfm->lfm_type),
682 le32_to_cpu(lfm->lfm_flags),
683 le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
687 lmm1 = &lmv->lmv_md_v1;
689 "magic 0x%08X, master %#X stripe_count %d hash_type %#x\n",
690 le32_to_cpu(lmm1->lmv_magic),
691 le32_to_cpu(lmm1->lmv_master_mdt_index),
692 le32_to_cpu(lmm1->lmv_stripe_count),
693 le32_to_cpu(lmm1->lmv_hash_type));
695 if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
698 if (le32_to_cpu(lmm1->lmv_stripe_count) > LMV_MAX_STRIPE_COUNT)
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);
786 /* Shrink optional ENCCTX buffer if it is not used */
787 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
788 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0 &&
789 !(body->mbo_valid & OBD_MD_ENCCTX))
790 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0, RCL_SERVER);
792 /* Shrink optional default LMV buffer if it is not used */
793 if (req_capsule_has_field(pill, &RMF_DEFAULT_MDT_MD, RCL_SERVER) &&
794 req_capsule_get_size(pill, &RMF_DEFAULT_MDT_MD, RCL_SERVER) != 0 &&
795 !(body->mbo_valid & OBD_MD_DEFAULT_MEA))
796 req_capsule_shrink(pill, &RMF_DEFAULT_MDT_MD, 0, RCL_SERVER);
799 * Some more field should be shrinked if needed.
800 * This should be done by those who added fields to reply message.
803 /* Grow MD buffer if needed finally */
804 if (info->mti_big_lmm_used) {
807 LASSERT(md_size > md_packed);
808 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
809 md_size - md_packed);
810 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
812 /* we can't answer with proper LOV EA, drop flags,
813 * the rc is also returned so this request is
814 * considered as failed */
815 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
816 /* don't return transno along with error */
817 lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
819 /* now we need to pack right LOV/LMV EA */
820 lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
821 if (info->mti_attr.ma_valid & MA_LOV) {
822 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
824 info->mti_attr.ma_lmm_size);
825 memcpy(lmm, info->mti_attr.ma_lmm,
826 info->mti_attr.ma_lmm_size);
827 } else if (info->mti_attr.ma_valid & MA_LMV) {
828 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
830 info->mti_attr.ma_lmv_size);
831 memcpy(lmm, info->mti_attr.ma_lmv,
832 info->mti_attr.ma_lmv_size);
836 /* update mdt_max_mdsize so clients will be aware about that */
837 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
838 info->mti_mdt->mdt_max_mdsize =
839 info->mti_attr.ma_lmm_size;
840 info->mti_big_lmm_used = 0;
843 if (info->mti_big_acl_used) {
844 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
847 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
849 body->mbo_valid &= ~OBD_MD_FLACL;
851 void *acl = req_capsule_server_get(pill, &RMF_ACL);
853 memcpy(acl, info->mti_big_acl, acl_size);
856 info->mti_big_acl_used = 0;
863 /* if object is dying, pack the lov/llog data,
864 * parameter info->mti_attr should be valid at this point!
865 * Also implements RAoLU policy */
866 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
869 struct mdt_body *repbody = NULL;
870 const struct lu_attr *la = &ma->ma_attr;
871 struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
874 struct hsm_action_item hai = {
875 .hai_len = sizeof(hai),
876 .hai_action = HSMA_REMOVE,
877 .hai_extent.length = -1,
885 if (mdt_info_req(info) != NULL) {
886 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
887 LASSERT(repbody != NULL);
889 CDEBUG(D_INFO, "not running in a request/reply context\n");
892 if ((ma->ma_valid & MA_INODE) && repbody != NULL)
893 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
895 if (ma->ma_valid & MA_LOV) {
896 CERROR("No need in LOV EA upon unlink\n");
900 repbody->mbo_eadatasize = 0;
902 /* Only check unlinked and archived if RAoLU and upon last close */
903 if (!cdt->cdt_remove_archive_on_last_unlink ||
904 atomic_read(&mo->mot_open_count) != 0)
907 /* mdt_attr_get_complex will clear ma_valid, so check here first */
908 if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
911 if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
914 need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
916 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
917 * try setting them */
919 rc = mdt_attr_get_complex(info, mo, ma);
921 CERROR("%s: unable to fetch missing attributes of"
922 DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
923 PFID(mdt_object_fid(mo)), rc);
927 if (need & MA_INODE) {
928 if (ma->ma_valid & MA_INODE) {
929 if (ma->ma_attr.la_nlink != 0)
937 if (ma->ma_valid & MA_HSM) {
938 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
946 /* RAoLU policy is active, last close on file has occured,
947 * file is unlinked, file is archived, so create remove request
949 * If CDT is not running, requests will be logged for later. */
950 if (ma->ma_hsm.mh_arch_id != 0)
951 archive_id = ma->ma_hsm.mh_arch_id;
953 archive_id = cdt->cdt_default_archive_id;
955 hai.hai_fid = *mdt_object_fid(mo);
957 rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
960 CERROR("%s: unable to add HSM remove request for "DFID
961 ": rc=%d\n", mdt_obd_name(info->mti_mdt),
962 PFID(mdt_object_fid(mo)), rc);
967 static __u64 mdt_attr_valid_xlate(enum mds_attr_flags in,
968 struct mdt_reint_record *rr,
974 if (in & MDS_ATTR_MODE)
976 if (in & MDS_ATTR_UID)
978 if (in & MDS_ATTR_GID)
980 if (in & MDS_ATTR_SIZE)
982 if (in & MDS_ATTR_BLOCKS)
984 if (in & MDS_ATTR_ATIME_SET)
986 if (in & MDS_ATTR_CTIME_SET)
988 if (in & MDS_ATTR_MTIME_SET)
990 if (in & MDS_ATTR_ATTR_FLAG)
992 if (in & MDS_ATTR_KILL_SUID)
994 if (in & MDS_ATTR_KILL_SGID)
996 if (in & MDS_ATTR_PROJID)
998 if (in & MDS_ATTR_LSIZE)
1000 if (in & MDS_ATTR_LBLOCKS)
1003 if (in & MDS_ATTR_FROM_OPEN)
1004 rr->rr_flags |= MRF_OPEN_TRUNC;
1005 if (in & MDS_ATTR_OVERRIDE)
1006 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1007 if (in & MDS_ATTR_FORCE)
1008 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1010 in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1011 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1012 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1013 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1014 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1015 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1018 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", (u64)in);
1025 int mdt_name_unpack(struct req_capsule *pill,
1026 const struct req_msg_field *field,
1028 enum mdt_name_flags flags)
1030 ln->ln_name = req_capsule_client_get(pill, field);
1031 ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1033 if (!lu_name_is_valid(ln)) {
1040 if ((flags & MNF_FIX_ANON) &&
1041 ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1042 /* Newer (3.x) kernels use a name of "/" for the
1043 * "anonymous" disconnected dentries from NFS
1044 * filehandle conversion. See d_obtain_alias(). */
1052 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1053 const char **secctx_name,
1054 void **secctx, size_t *secctx_size)
1059 *secctx_name = NULL;
1063 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1064 !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1067 name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1072 if (name_size > XATTR_NAME_MAX + 1)
1075 name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1076 if (strnlen(name, name_size) != name_size - 1)
1079 if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1080 !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1083 *secctx_name = name;
1084 *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1085 *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1090 static int mdt_file_encctx_unpack(struct req_capsule *pill,
1091 void **encctx, size_t *encctx_size)
1096 if (!exp_connect_encrypt(pill->rc_req->rq_export))
1099 if (!req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_CLIENT) ||
1100 !req_capsule_field_present(pill, &RMF_FILE_ENCCTX, RCL_CLIENT))
1103 *encctx_size = req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_CLIENT);
1104 if (*encctx_size == 0)
1107 *encctx = req_capsule_client_get(pill, &RMF_FILE_ENCCTX);
1112 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1114 struct lu_ucred *uc = mdt_ucred(info);
1115 struct md_attr *ma = &info->mti_attr;
1116 struct lu_attr *la = &ma->ma_attr;
1117 struct req_capsule *pill = info->mti_pill;
1118 struct mdt_reint_record *rr = &info->mti_rr;
1119 struct mdt_rec_setattr *rec;
1120 struct lu_nodemap *nodemap;
1124 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1125 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1129 /* This prior initialization is needed for old_init_ucred_reint() */
1130 uc->uc_fsuid = rec->sa_fsuid;
1131 uc->uc_fsgid = rec->sa_fsgid;
1132 uc->uc_cap = CAP_EMPTY_SET;
1133 uc->uc_cap.cap[0] = rec->sa_cap;
1134 uc->uc_suppgids[0] = rec->sa_suppgid;
1135 uc->uc_suppgids[1] = -1;
1137 rr->rr_fid1 = &rec->sa_fid;
1138 la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1139 la->la_mode = rec->sa_mode;
1140 la->la_flags = rec->sa_attr_flags;
1142 nodemap = nodemap_get_from_exp(info->mti_exp);
1143 if (IS_ERR(nodemap))
1144 RETURN(PTR_ERR(nodemap));
1146 la->la_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1147 NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1148 la->la_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1149 NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1150 la->la_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
1151 NODEMAP_CLIENT_TO_FS, rec->sa_projid);
1152 nodemap_putref(nodemap);
1154 la->la_size = rec->sa_size;
1155 la->la_blocks = rec->sa_blocks;
1156 la->la_ctime = rec->sa_ctime;
1157 la->la_atime = rec->sa_atime;
1158 la->la_mtime = rec->sa_mtime;
1159 ma->ma_valid = MA_INODE;
1161 ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1162 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1167 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1169 struct req_capsule *pill = info->mti_pill;
1170 struct mdt_ioepoch *ioepoch;
1173 if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1174 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1178 if (ioepoch == NULL)
1181 info->mti_open_handle = ioepoch->mio_open_handle;
1186 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1187 struct req_capsule *pill = info->mti_pill;
1189 if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1190 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1191 if (info->mti_dlm_req == NULL)
1198 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1200 struct mdt_reint_record *rr = &info->mti_rr;
1201 struct md_attr *ma = &info->mti_attr;
1202 struct req_capsule *pill = info->mti_pill;
1206 rc = mdt_setattr_unpack_rec(info);
1210 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1211 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1212 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1215 if (rr->rr_eadatalen > 0) {
1216 const struct lmv_user_md *lum;
1218 lum = rr->rr_eadata;
1219 /* Sigh ma_valid(from req) does not indicate whether
1220 * it will set LOV/LMV EA, so we have to check magic */
1221 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1222 ma->ma_valid |= MA_LMV;
1223 ma->ma_lmv = (void *)rr->rr_eadata;
1224 ma->ma_lmv_size = rr->rr_eadatalen;
1226 ma->ma_valid |= MA_LOV;
1227 ma->ma_lmm = (void *)rr->rr_eadata;
1228 ma->ma_lmm_size = rr->rr_eadatalen;
1233 rc = mdt_dlmreq_unpack(info);
1237 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1239 struct md_attr *ma = &info->mti_attr;
1240 struct req_capsule *pill = info->mti_pill;
1243 if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1246 req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1248 if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1249 req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1255 int mdt_close_unpack(struct mdt_thread_info *info)
1260 rc = mdt_close_handle_unpack(info);
1264 rc = mdt_setattr_unpack_rec(info);
1268 rc = mdt_close_intent_unpack(info);
1272 RETURN(mdt_init_ucred_reint(info));
1275 static int mdt_create_unpack(struct mdt_thread_info *info)
1277 struct lu_ucred *uc = mdt_ucred(info);
1278 struct mdt_rec_create *rec;
1279 struct lu_attr *attr = &info->mti_attr.ma_attr;
1280 struct mdt_reint_record *rr = &info->mti_rr;
1281 struct req_capsule *pill = info->mti_pill;
1282 struct md_op_spec *sp = &info->mti_spec;
1287 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1288 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1292 /* This prior initialization is needed for old_init_ucred_reint() */
1293 uc->uc_fsuid = rec->cr_fsuid;
1294 uc->uc_fsgid = rec->cr_fsgid;
1295 uc->uc_cap = CAP_EMPTY_SET;
1296 uc->uc_cap.cap[0] = rec->cr_cap;
1297 uc->uc_suppgids[0] = rec->cr_suppgid1;
1298 uc->uc_suppgids[1] = -1;
1299 uc->uc_umask = rec->cr_umask;
1301 rr->rr_fid1 = &rec->cr_fid1;
1302 rr->rr_fid2 = &rec->cr_fid2;
1303 attr->la_mode = rec->cr_mode;
1304 attr->la_rdev = rec->cr_rdev;
1305 attr->la_uid = rec->cr_fsuid;
1306 attr->la_gid = rec->cr_fsgid;
1307 attr->la_ctime = rec->cr_time;
1308 attr->la_mtime = rec->cr_time;
1309 attr->la_atime = rec->cr_time;
1310 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1311 LA_CTIME | LA_MTIME | LA_ATIME;
1312 memset(&sp->u, 0, sizeof(sp->u));
1313 sp->sp_cr_flags = get_mrc_cr_flags(rec);
1315 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1319 if (S_ISLNK(attr->la_mode)) {
1320 const char *tgt = NULL;
1323 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1324 sz = req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT);
1326 tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1327 sp->u.sp_symname.ln_name = tgt;
1328 sp->u.sp_symname.ln_namelen = sz - 1; /* skip NUL */
1333 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1334 if (S_ISDIR(attr->la_mode) &&
1335 req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1336 sp->u.sp_ea.eadata =
1337 req_capsule_client_get(pill, &RMF_EADATA);
1338 sp->u.sp_ea.eadatalen =
1339 req_capsule_get_size(pill, &RMF_EADATA,
1341 sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1345 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1346 &sp->sp_cr_file_secctx,
1347 &sp->sp_cr_file_secctx_size);
1351 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1352 &sp->sp_cr_file_encctx_size);
1356 rc = req_check_sepol(pill);
1360 rc = mdt_dlmreq_unpack(info);
1364 static int mdt_link_unpack(struct mdt_thread_info *info)
1366 struct lu_ucred *uc = mdt_ucred(info);
1367 struct mdt_rec_link *rec;
1368 struct lu_attr *attr = &info->mti_attr.ma_attr;
1369 struct mdt_reint_record *rr = &info->mti_rr;
1370 struct req_capsule *pill = info->mti_pill;
1375 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1376 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1380 /* This prior initialization is needed for old_init_ucred_reint() */
1381 uc->uc_fsuid = rec->lk_fsuid;
1382 uc->uc_fsgid = rec->lk_fsgid;
1383 uc->uc_cap = CAP_EMPTY_SET;
1384 uc->uc_cap.cap[0] = rec->lk_cap;
1385 uc->uc_suppgids[0] = rec->lk_suppgid1;
1386 uc->uc_suppgids[1] = rec->lk_suppgid2;
1388 attr->la_uid = rec->lk_fsuid;
1389 attr->la_gid = rec->lk_fsgid;
1390 rr->rr_fid1 = &rec->lk_fid1;
1391 rr->rr_fid2 = &rec->lk_fid2;
1392 attr->la_ctime = rec->lk_time;
1393 attr->la_mtime = rec->lk_time;
1394 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1396 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1400 rc = req_check_sepol(pill);
1404 rc = mdt_dlmreq_unpack(info);
1409 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1411 struct lu_ucred *uc = mdt_ucred(info);
1412 struct mdt_rec_unlink *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;
1420 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1421 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1425 /* This prior initialization is needed for old_init_ucred_reint() */
1426 uc->uc_fsuid = rec->ul_fsuid;
1427 uc->uc_fsgid = rec->ul_fsgid;
1428 uc->uc_cap = CAP_EMPTY_SET;
1429 uc->uc_cap.cap[0] = rec->ul_cap;
1430 uc->uc_suppgids[0] = rec->ul_suppgid1;
1431 uc->uc_suppgids[1] = -1;
1433 attr->la_uid = rec->ul_fsuid;
1434 attr->la_gid = rec->ul_fsgid;
1435 rr->rr_fid1 = &rec->ul_fid1;
1436 rr->rr_fid2 = &rec->ul_fid2;
1437 attr->la_ctime = rec->ul_time;
1438 attr->la_mtime = rec->ul_time;
1439 attr->la_mode = rec->ul_mode;
1440 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1441 if (rec->ul_bias & MDS_FID_OP)
1442 info->mti_spec.sp_cr_flags |= MDS_OP_WITH_FID;
1444 info->mti_spec.sp_cr_flags &= ~MDS_OP_WITH_FID;
1446 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1450 info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1452 rc = req_check_sepol(pill);
1456 rc = mdt_dlmreq_unpack(info);
1460 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1462 info->mti_spec.sp_rm_entry = 1;
1463 return mdt_unlink_unpack(info);
1466 static int mdt_rename_unpack(struct mdt_thread_info *info)
1468 struct lu_ucred *uc = mdt_ucred(info);
1469 struct mdt_rec_rename *rec;
1470 struct lu_attr *attr = &info->mti_attr.ma_attr;
1471 struct mdt_reint_record *rr = &info->mti_rr;
1472 struct req_capsule *pill = info->mti_pill;
1473 struct md_op_spec *spec = &info->mti_spec;
1478 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1479 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1483 /* This prior initialization is needed for old_init_ucred_reint() */
1484 uc->uc_fsuid = rec->rn_fsuid;
1485 uc->uc_fsgid = rec->rn_fsgid;
1486 uc->uc_cap = CAP_EMPTY_SET;
1487 uc->uc_cap.cap[0] = rec->rn_cap;
1488 uc->uc_suppgids[0] = rec->rn_suppgid1;
1489 uc->uc_suppgids[1] = rec->rn_suppgid2;
1491 attr->la_uid = rec->rn_fsuid;
1492 attr->la_gid = rec->rn_fsgid;
1493 rr->rr_fid1 = &rec->rn_fid1;
1494 rr->rr_fid2 = &rec->rn_fid2;
1495 attr->la_ctime = rec->rn_time;
1496 attr->la_mtime = rec->rn_time;
1497 /* rename_tgt contains the mode already */
1498 attr->la_mode = rec->rn_mode;
1499 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1501 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1505 rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1509 spec->no_create = !!req_is_replay(mdt_info_req(info));
1511 rc = req_check_sepol(pill);
1515 rc = mdt_dlmreq_unpack(info);
1520 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1522 struct lu_ucred *uc = mdt_ucred(info);
1523 struct mdt_rec_rename *rec;
1524 struct lu_attr *attr = &info->mti_attr.ma_attr;
1525 struct mdt_reint_record *rr = &info->mti_rr;
1526 struct req_capsule *pill = info->mti_pill;
1527 struct md_op_spec *spec = &info->mti_spec;
1532 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1533 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1537 /* This prior initialization is needed for old_init_ucred_reint() */
1538 uc->uc_fsuid = rec->rn_fsuid;
1539 uc->uc_fsgid = rec->rn_fsgid;
1540 uc->uc_cap = CAP_EMPTY_SET;
1541 uc->uc_cap.cap[0] = rec->rn_cap;
1542 uc->uc_suppgids[0] = rec->rn_suppgid1;
1543 uc->uc_suppgids[1] = rec->rn_suppgid2;
1545 attr->la_uid = rec->rn_fsuid;
1546 attr->la_gid = rec->rn_fsgid;
1547 rr->rr_fid1 = &rec->rn_fid1;
1548 rr->rr_fid2 = &rec->rn_fid2;
1549 attr->la_ctime = rec->rn_time;
1550 attr->la_mtime = rec->rn_time;
1551 /* rename_tgt contains the mode already */
1552 attr->la_mode = rec->rn_mode;
1553 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1554 spec->sp_cr_flags = 0;
1556 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1560 if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1561 rc = mdt_close_handle_unpack(info);
1565 spec->sp_migrate_close = 1;
1567 spec->sp_migrate_close = 0;
1570 spec->sp_migrate_nsonly = !!(rec->rn_bias & MDS_MIGRATE_NSONLY);
1572 /* lustre version > 2.11 migration packs lum */
1573 if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1574 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1575 rr->rr_eadatalen = req_capsule_get_size(pill,
1579 if (rr->rr_eadatalen > 0) {
1580 struct lmv_user_md_v1 *lmu;
1582 lmu = req_capsule_client_get(pill, &RMF_EADATA);
1583 lmu->lum_hash_type |=
1584 cpu_to_le32(LMV_HASH_FLAG_FIXED);
1585 rr->rr_eadata = lmu;
1586 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1587 spec->u.sp_ea.eadata = rr->rr_eadata;
1588 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1591 /* old client doesn't provide lum. */
1592 RETURN(-EOPNOTSUPP);
1596 spec->no_create = !!req_is_replay(mdt_info_req(info));
1598 rc = mdt_dlmreq_unpack(info);
1604 * please see comment above LOV_MAGIC_V1_DEFINED
1606 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1608 struct lov_user_md_v1 *v1 = eadata;
1612 if (unlikely(req_is_replay(mdt_info_req(info)))) {
1613 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1614 v1->lmm_magic |= LOV_MAGIC_DEFINED;
1615 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1616 __swab32(LOV_MAGIC_MAGIC))
1617 v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1621 static int mdt_open_unpack(struct mdt_thread_info *info)
1623 struct lu_ucred *uc = mdt_ucred(info);
1624 struct mdt_rec_create *rec;
1625 struct lu_attr *attr = &info->mti_attr.ma_attr;
1626 struct req_capsule *pill = info->mti_pill;
1627 struct mdt_reint_record *rr = &info->mti_rr;
1628 struct ptlrpc_request *req = mdt_info_req(info);
1629 struct md_op_spec *sp = &info->mti_spec;
1633 BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1634 sizeof(struct mdt_rec_reint));
1635 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1639 /* This prior initialization is needed for old_init_ucred_reint() */
1640 uc->uc_fsuid = rec->cr_fsuid;
1641 uc->uc_fsgid = rec->cr_fsgid;
1642 uc->uc_cap = CAP_EMPTY_SET;
1643 uc->uc_cap.cap[0] = rec->cr_cap;
1644 uc->uc_suppgids[0] = rec->cr_suppgid1;
1645 uc->uc_suppgids[1] = rec->cr_suppgid2;
1646 uc->uc_umask = rec->cr_umask;
1648 rr->rr_fid1 = &rec->cr_fid1;
1649 rr->rr_fid2 = &rec->cr_fid2;
1650 rr->rr_open_handle = &rec->cr_open_handle_old;
1651 attr->la_mode = rec->cr_mode;
1652 attr->la_rdev = rec->cr_rdev;
1653 attr->la_uid = rec->cr_fsuid;
1654 attr->la_gid = rec->cr_fsgid;
1655 attr->la_ctime = rec->cr_time;
1656 attr->la_mtime = rec->cr_time;
1657 attr->la_atime = rec->cr_time;
1658 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
1659 LA_CTIME | LA_MTIME | LA_ATIME;
1660 memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1661 info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1662 /* Do not trigger ASSERTION if client miss to set such flags. */
1663 if (unlikely(info->mti_spec.sp_cr_flags == 0))
1666 info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1668 mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1670 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1671 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 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1678 sp->u.sp_ea.eadata = rr->rr_eadata;
1679 sp->sp_archive_id = rec->cr_archive_id;
1680 sp->no_create = !!req_is_replay(req);
1681 mdt_fix_lov_magic(info, rr->rr_eadata);
1685 * Client default md_size may be 0 right after client start,
1686 * until all osc are connected, set here just some reasonable
1687 * value to prevent misbehavior.
1689 if (rr->rr_eadatalen == 0 &&
1690 !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1691 rr->rr_eadatalen = MIN_MD_SIZE;
1694 rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1695 &sp->sp_cr_file_secctx,
1696 &sp->sp_cr_file_secctx_size);
1700 rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1701 &sp->sp_cr_file_encctx_size);
1705 rc = req_check_sepol(pill);
1712 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1714 struct mdt_reint_record *rr = &info->mti_rr;
1715 struct lu_ucred *uc = mdt_ucred(info);
1716 struct lu_attr *attr = &info->mti_attr.ma_attr;
1717 struct req_capsule *pill = info->mti_pill;
1718 struct mdt_rec_setxattr *rec;
1723 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1724 sizeof(struct mdt_rec_reint));
1725 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1729 /* This prior initialization is needed for old_init_ucred_reint() */
1730 uc->uc_fsuid = rec->sx_fsuid;
1731 uc->uc_fsgid = rec->sx_fsgid;
1732 uc->uc_cap = CAP_EMPTY_SET;
1733 uc->uc_cap.cap[0] = rec->sx_cap;
1734 uc->uc_suppgids[0] = rec->sx_suppgid1;
1735 uc->uc_suppgids[1] = -1;
1737 rr->rr_opcode = rec->sx_opcode;
1738 rr->rr_fid1 = &rec->sx_fid;
1739 attr->la_valid = rec->sx_valid;
1740 attr->la_ctime = rec->sx_time;
1741 attr->la_size = rec->sx_size;
1742 attr->la_flags = rec->sx_flags;
1744 rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1748 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1749 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1752 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1755 if (rr->rr_eadatalen > 0) {
1756 rr->rr_eadata = req_capsule_client_get(pill,
1758 if (rr->rr_eadata == NULL)
1761 rr->rr_eadata = NULL;
1763 } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1764 CDEBUG(D_INFO, "no xattr data supplied\n");
1768 rc = req_check_sepol(pill);
1772 if (mdt_dlmreq_unpack(info) < 0)
1778 static int mdt_resync_unpack(struct mdt_thread_info *info)
1780 struct req_capsule *pill = info->mti_pill;
1781 struct mdt_reint_record *rr = &info->mti_rr;
1782 struct lu_ucred *uc = mdt_ucred(info);
1783 struct mdt_rec_resync *rec;
1786 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1787 rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1791 /* This prior initialization is needed for old_init_ucred_reint() */
1792 uc->uc_fsuid = rec->rs_fsuid;
1793 uc->uc_fsgid = rec->rs_fsgid;
1794 uc->uc_cap = CAP_EMPTY_SET;
1795 uc->uc_cap.cap[0] = rec->rs_cap;
1797 rr->rr_fid1 = &rec->rs_fid;
1798 rr->rr_mirror_id = rec->rs_mirror_id;
1800 /* cookie doesn't need to be swapped but it has been swapped
1801 * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1803 if (req_capsule_req_need_swab(pill))
1804 __swab64s(&rec->rs_lease_handle.cookie);
1805 rr->rr_lease_handle = &rec->rs_lease_handle;
1807 RETURN(mdt_dlmreq_unpack(info));
1810 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1812 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1813 [REINT_SETATTR] = mdt_setattr_unpack,
1814 [REINT_CREATE] = mdt_create_unpack,
1815 [REINT_LINK] = mdt_link_unpack,
1816 [REINT_UNLINK] = mdt_unlink_unpack,
1817 [REINT_RENAME] = mdt_rename_unpack,
1818 [REINT_OPEN] = mdt_open_unpack,
1819 [REINT_SETXATTR] = mdt_setxattr_unpack,
1820 [REINT_RMENTRY] = mdt_rmentry_unpack,
1821 [REINT_MIGRATE] = mdt_migrate_unpack,
1822 [REINT_RESYNC] = mdt_resync_unpack,
1825 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1830 memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1831 if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1832 info->mti_rr.rr_opcode = op;
1833 rc = mdt_reint_unpackers[op](info);
1835 CERROR("Unexpected opcode %d\n", op);
1841 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1842 struct mdt_object *child)
1845 struct lu_buf *buffer;
1846 struct mdt_body *repbody;
1847 struct req_capsule *pill = info->mti_pill;
1850 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1851 req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1853 req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1854 buffer = &info->mti_buf;
1856 /* fill reply buffer with security context now */
1857 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1859 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1861 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1862 buffer, secctx_name);
1865 "found security context of size %d for "DFID"\n",
1866 rc, PFID(mdt_object_fid(child)));
1868 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1869 repbody->mbo_valid |= OBD_MD_SECCTX;
1870 if (rc < buffer->lb_len)
1871 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1876 "security context not found for "DFID": rc = %d\n",
1877 PFID(mdt_object_fid(child)), rc);
1878 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1880 /* handling -ENOENT is important because it may change
1881 * object state in DNE env dropping LOHA_EXISTS flag,
1882 * it is important to return that to the caller.
1883 * Check LU-13115 for details.
1892 /* check whether two FIDs belong to different MDT. */
1893 static int mdt_fids_different_target(struct mdt_thread_info *info,
1894 const struct lu_fid *fid1,
1895 const struct lu_fid *fid2)
1897 const struct lu_env *env = info->mti_env;
1898 struct mdt_device *mdt = info->mti_mdt;
1899 struct lu_seq_range *range = &info->mti_range;
1900 struct seq_server_site *ss;
1901 __u32 index1, index2;
1904 if (fid_seq(fid1) == fid_seq(fid2))
1907 ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1909 range->lsr_flags = LU_SEQ_RANGE_MDT;
1910 rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1914 index1 = range->lsr_index;
1916 rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1920 index2 = range->lsr_index;
1922 return index1 != index2;
1926 * Check whether \a child is remote object on \a parent.
1928 * \param[in] info thread environment
1929 * \param[in] parent parent object, it's the same as child object in
1931 * \param[in] child child object
1933 * \retval 1 is remote object.
1934 * \retval 0 isn't remote object.
1935 * \retval < 1 error code
1937 int mdt_is_remote_object(struct mdt_thread_info *info,
1938 struct mdt_object *parent,
1939 struct mdt_object *child)
1941 struct lu_buf *buf = &info->mti_big_buf;
1942 struct linkea_data ldata = { NULL };
1943 struct link_ea_header *leh;
1944 struct link_ea_entry *lee;
1945 struct lu_name name;
1953 if (fid_is_root(mdt_object_fid(child)))
1956 if (likely(parent != child)) {
1957 if (mdt_object_remote(parent) ^ mdt_object_remote(child))
1960 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1963 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1964 mdt_object_fid(child));
1968 /* client < 2.13.52 getattr_by_fid parent and child are the same */
1969 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1974 rc = mdt_links_read(info, child, &ldata);
1975 /* can't read linkea, just assume it's remote object */
1976 if (rc == -ENOENT || rc == -ENODATA)
1982 lee = (struct link_ea_entry *)(leh + 1);
1983 for (i = 0; i < leh->leh_reccount; i++) {
1984 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1985 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1986 if (mdt_fids_different_target(info, &pfid,
1987 mdt_object_fid(child)))
1994 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
1995 struct mdt_object *child)
1997 struct lu_buf *buffer;
1998 struct mdt_body *repbody;
1999 struct req_capsule *pill = info->mti_pill;
2000 struct obd_export *exp = mdt_info_req(info)->rq_export;
2003 if (!exp_connect_encrypt(exp))
2006 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
2007 req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0) {
2008 struct lu_attr la = { 0 };
2009 struct dt_object *dt = mdt_obj2dt(child);
2011 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
2012 dt_attr_get(info->mti_env, mdt_obj2dt(child), &la);
2014 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL) {
2015 buffer = &info->mti_buf;
2017 /* fill reply buffer with encryption context now */
2019 req_capsule_get_size(pill, &RMF_FILE_ENCCTX,
2022 req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
2023 rc = mo_xattr_get(info->mti_env,
2024 mdt_object_child(child),
2026 LL_XATTR_NAME_ENCRYPTION_CONTEXT);
2027 if (unlikely(rc == -ENODATA))
2028 /* For compatibility with 2.14 */
2029 rc = mo_xattr_get(info->mti_env,
2030 mdt_object_child(child),
2032 LL_XATTR_NAME_ENCRYPTION_CONTEXT_OLD);
2035 "found encryption ctx of size %d for "DFID"\n",
2036 rc, PFID(mdt_object_fid(child)));
2038 repbody = req_capsule_server_get(pill,
2040 repbody->mbo_valid |= OBD_MD_ENCCTX;
2041 if (rc < buffer->lb_len)
2042 req_capsule_shrink(pill,
2043 &RMF_FILE_ENCCTX, rc,
2048 "encryption ctx not found for "DFID": rc = %d\n",
2049 PFID(mdt_object_fid(child)), rc);
2050 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2052 /* handling -ENOENT is important because it may
2053 * change object state in DNE env dropping
2054 * LOHA_EXISTS flag, it is important to return
2055 * that to the caller.
2056 * Check LU-13115 for details.
2062 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,