Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / mdt / mdt_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_lib.c
33  *
34  * Lustre Metadata Target (mdt) request unpacking helper.
35  *
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>
43  */
44
45 #define DEBUG_SUBSYSTEM S_MDS
46
47 #include <linux/user_namespace.h>
48 #include <linux/uidgid.h>
49
50 #include "mdt_internal.h"
51 #include <uapi/linux/lnet/nidstr.h>
52 #include <lustre_nodemap.h>
53
54 typedef enum ucred_init_type {
55         NONE_INIT       = 0,
56         BODY_INIT       = 1,
57         REC_INIT        = 2
58 } ucred_init_type_t;
59
60 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
61 {
62         return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
63 }
64
65 void mdt_exit_ucred(struct mdt_thread_info *info)
66 {
67         struct lu_ucred   *uc  = mdt_ucred(info);
68         struct mdt_device *mdt = info->mti_mdt;
69
70         LASSERT(uc != NULL);
71         if (uc->uc_valid != UCRED_INIT) {
72                 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
73                 if (uc->uc_ginfo) {
74                         put_group_info(uc->uc_ginfo);
75                         uc->uc_ginfo = NULL;
76                 }
77                 if (uc->uc_identity) {
78                         mdt_identity_put(mdt->mdt_identity_cache,
79                                          uc->uc_identity);
80                         uc->uc_identity = NULL;
81                 }
82                 uc->uc_valid = UCRED_INIT;
83         }
84 }
85
86 static int match_nosquash_list(struct spinlock *rsi_lock,
87                                struct list_head *nidlist,
88                                lnet_nid_t peernid)
89 {
90         int rc;
91         ENTRY;
92         spin_lock(rsi_lock);
93         rc = cfs_match_nid(peernid, nidlist);
94         spin_unlock(rsi_lock);
95         RETURN(rc);
96 }
97
98 /* root_squash for inter-MDS operations */
99 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
100 {
101         struct lu_ucred *ucred = mdt_ucred(info);
102         struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
103         ENTRY;
104
105         LASSERT(ucred != NULL);
106         if (!squash->rsi_uid || ucred->uc_fsuid)
107                 RETURN(0);
108
109         if (match_nosquash_list(&squash->rsi_lock,
110                                 &squash->rsi_nosquash_nids,
111                                 peernid)) {
112                 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
113                        libcfs_nid2str(peernid));
114                 RETURN(0);
115         }
116
117         CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
118                libcfs_nid2str(peernid),
119                ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
120                squash->rsi_uid, squash->rsi_gid, 0);
121
122         ucred->uc_fsuid = squash->rsi_uid;
123         ucred->uc_fsgid = squash->rsi_gid;
124         ucred->uc_cap = 0;
125         ucred->uc_suppgids[0] = -1;
126         ucred->uc_suppgids[1] = -1;
127
128         RETURN(0);
129 }
130
131 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
132 {
133         struct ptlrpc_request   *req = mdt_info_req(info);
134         const char              *jobid = mdt_req_get_jobid(req);
135
136         /* set jobid if specified. */
137         if (jobid)
138                 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
139         else
140                 uc->uc_jobid[0] = '\0';
141 }
142
143 static void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
144 {
145         if (info && info->mti_exp && info->mti_exp->exp_connection)
146                 uc->uc_nid = info->mti_exp->exp_connection->c_peer.nid;
147         else
148                 uc->uc_nid = LNET_NID_ANY;
149 }
150
151 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
152                                     struct lu_ucred *uc)
153 {
154         struct lu_nodemap *nodemap = NULL;
155         bool audit = true;
156
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);
162                 }
163         }
164
165         uc->uc_enable_audit = audit;
166 }
167
168 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
169                           void *buf, bool drop_fs_cap)
170 {
171         struct ptlrpc_request *req = mdt_info_req(info);
172         struct mdt_device *mdt = info->mti_mdt;
173         struct ptlrpc_user_desc *pud = req->rq_user_desc;
174         struct lu_ucred *ucred = mdt_ucred(info);
175         struct lu_nodemap *nodemap;
176         lnet_nid_t peernid = req->rq_peer.nid;
177         __u32 perm = 0;
178         int setuid;
179         int setgid;
180         bool is_nm_gid_squashed = false;
181         int rc = 0;
182
183         ENTRY;
184
185         LASSERT(req->rq_auth_gss);
186         LASSERT(!req->rq_auth_usr_mdt);
187         LASSERT(req->rq_user_desc);
188         LASSERT(ucred != NULL);
189
190         ucred->uc_valid = UCRED_INVALID;
191
192         nodemap = nodemap_get_from_exp(info->mti_exp);
193         if (IS_ERR(nodemap))
194                 RETURN(PTR_ERR(nodemap));
195
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);
204
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;
209
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);
214                         RETURN(-EACCES);
215                 }
216
217                 ucred->uc_fsuid = nodemap->nm_squash_uid;
218                 ucred->uc_fsgid = nodemap->nm_squash_gid;
219                 ucred->uc_cap = 0;
220                 ucred->uc_suppgids[0] = -1;
221                 ucred->uc_suppgids[1] = -1;
222         }
223
224         if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
225                 is_nm_gid_squashed = true;
226
227         nodemap_putref(nodemap);
228
229         if (type == BODY_INIT) {
230                 struct mdt_body *body = (struct mdt_body *)buf;
231
232                 ucred->uc_suppgids[0] = body->mbo_suppgid;
233                 ucred->uc_suppgids[1] = -1;
234         }
235
236         if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
237             req->rq_auth_uid != pud->pud_uid) {
238                 CDEBUG(D_SEC, "local client %s: auth uid %u "
239                        "while client claims %u:%u/%u:%u\n",
240                        libcfs_nid2str(peernid), req->rq_auth_uid,
241                        pud->pud_uid, pud->pud_gid,
242                        pud->pud_fsuid, pud->pud_fsgid);
243                 RETURN(-EACCES);
244         }
245
246         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
247                 ucred->uc_identity = NULL;
248                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
249         } else {
250                 struct md_identity *identity;
251
252                 identity = mdt_identity_get(mdt->mdt_identity_cache,
253                                             pud->pud_uid);
254                 if (IS_ERR(identity)) {
255                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
256                                 ucred->uc_identity = NULL;
257                                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
258                                        CFS_SETGRP_PERM;
259                         } else {
260                                 CDEBUG(D_SEC,
261                                        "Deny access without identity: uid %u\n",
262                                        pud->pud_uid);
263                                 RETURN(-EACCES);
264                         }
265                 } else {
266                         ucred->uc_identity = identity;
267                         perm = mdt_identity_get_perm(ucred->uc_identity,
268                                                      peernid);
269                 }
270         }
271
272         /* find out the setuid/setgid attempt */
273         setuid = (pud->pud_uid != pud->pud_fsuid);
274         setgid = ((pud->pud_gid != pud->pud_fsgid) ||
275                   (ucred->uc_identity &&
276                    (pud->pud_gid != ucred->uc_identity->mi_gid)));
277
278         /* check permission of setuid */
279         if (setuid && !(perm & CFS_SETUID_PERM)) {
280                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
281                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
282                 GOTO(out, rc = -EACCES);
283         }
284
285         /* check permission of setgid */
286         if (setgid && !(perm & CFS_SETGID_PERM)) {
287                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
288                        "from %s\n", pud->pud_uid, pud->pud_gid,
289                        pud->pud_fsuid, pud->pud_fsgid,
290                        ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
291                 GOTO(out, rc = -EACCES);
292         }
293
294         if (perm & CFS_SETGRP_PERM) {
295                 /* only set groups if GID is not squashed */
296                 if (pud->pud_ngroups && !is_nm_gid_squashed) {
297                         /* setgroups for local client */
298                         ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
299                         if (!ucred->uc_ginfo) {
300                                 CERROR("failed to alloc %d groups\n",
301                                        pud->pud_ngroups);
302                                 GOTO(out, rc = -ENOMEM);
303                         }
304
305                         lustre_groups_from_list(ucred->uc_ginfo,
306                                                 pud->pud_groups);
307                         lustre_groups_sort(ucred->uc_ginfo);
308                 } else {
309                         ucred->uc_suppgids[0] = -1;
310                         ucred->uc_suppgids[1] = -1;
311                         ucred->uc_ginfo = NULL;
312                 }
313         } else {
314                 ucred->uc_suppgids[0] = -1;
315                 ucred->uc_suppgids[1] = -1;
316                 ucred->uc_ginfo = NULL;
317         }
318
319         ucred->uc_uid = pud->pud_uid;
320         ucred->uc_gid = pud->pud_gid;
321         ucred->uc_fsuid = pud->pud_fsuid;
322         ucred->uc_fsgid = pud->pud_fsgid;
323
324         /* process root_squash here. */
325         mdt_root_squash(info, peernid);
326
327         /* remove fs privilege for non-root user. */
328         if (ucred->uc_fsuid && drop_fs_cap)
329                 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
330         else
331                 ucred->uc_cap = pud->pud_cap;
332         ucred->uc_valid = UCRED_NEW;
333         ucred_set_jobid(info, ucred);
334         ucred_set_nid(info, ucred);
335         ucred_set_audit_enabled(info, ucred);
336
337         EXIT;
338
339 out:
340         if (rc) {
341                 if (ucred->uc_ginfo) {
342                         put_group_info(ucred->uc_ginfo);
343                         ucred->uc_ginfo = NULL;
344                 }
345                 if (ucred->uc_identity) {
346                         mdt_identity_put(mdt->mdt_identity_cache,
347                                          ucred->uc_identity);
348                         ucred->uc_identity = NULL;
349                 }
350         }
351
352         return rc;
353 }
354
355 /**
356  * Check whether allow the client to set supplementary group IDs or not.
357  *
358  * \param[in] info      pointer to the thread context
359  * \param[in] uc        pointer to the RPC user descriptor
360  *
361  * \retval              true if allow to set supplementary group IDs
362  * \retval              false for other cases
363  */
364 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
365 {
366         __u32 perm;
367
368         /* 1. If identity_upcall is disabled,
369          *    permit local client to do anything. */
370         if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
371                 return true;
372
373         /* 2. If fail to get related identities, then forbid any client to
374          *    set supplementary group IDs. */
375         if (uc->uc_identity == NULL)
376                 return false;
377
378         /* 3. Check the permission in the identities. */
379         perm = mdt_identity_get_perm(uc->uc_identity,
380                                      mdt_info_req(info)->rq_peer.nid);
381         if (perm & CFS_SETGRP_PERM)
382                 return true;
383
384         return false;
385 }
386
387 int mdt_check_ucred(struct mdt_thread_info *info)
388 {
389         struct ptlrpc_request   *req = mdt_info_req(info);
390         struct mdt_device       *mdt = info->mti_mdt;
391         struct ptlrpc_user_desc *pud = req->rq_user_desc;
392         struct lu_ucred         *ucred = mdt_ucred(info);
393         struct md_identity      *identity = NULL;
394         lnet_nid_t               peernid = req->rq_peer.nid;
395         __u32                    perm = 0;
396         int                      setuid;
397         int                      setgid;
398         int                      rc = 0;
399
400         ENTRY;
401
402         LASSERT(ucred != NULL);
403         if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
404                 RETURN(0);
405
406         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
407                 RETURN(0);
408
409         /* sanity check: if we use strong authentication, we expect the
410          * uid which client claimed is true */
411         if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
412             req->rq_auth_uid != pud->pud_uid) {
413                 CDEBUG(D_SEC, "local client %s: auth uid %u "
414                        "while client claims %u:%u/%u:%u\n",
415                        libcfs_nid2str(peernid), req->rq_auth_uid,
416                        pud->pud_uid, pud->pud_gid,
417                        pud->pud_fsuid, pud->pud_fsgid);
418                 RETURN(-EACCES);
419         }
420
421         if (is_identity_get_disabled(mdt->mdt_identity_cache))
422                 RETURN(0);
423
424         identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
425         if (IS_ERR(identity)) {
426                 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
427                         RETURN(0);
428                 } else {
429                         CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
430                                pud->pud_uid);
431                         RETURN(-EACCES);
432                 }
433         }
434
435         perm = mdt_identity_get_perm(identity, peernid);
436         /* find out the setuid/setgid attempt */
437         setuid = (pud->pud_uid != pud->pud_fsuid);
438         setgid = (pud->pud_gid != pud->pud_fsgid ||
439                   pud->pud_gid != identity->mi_gid);
440
441         /* check permission of setuid */
442         if (setuid && !(perm & CFS_SETUID_PERM)) {
443                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
444                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
445                 GOTO(out, rc = -EACCES);
446         }
447
448         /* check permission of setgid */
449         if (setgid && !(perm & CFS_SETGID_PERM)) {
450                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
451                        "from %s\n", pud->pud_uid, pud->pud_gid,
452                        pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
453                        libcfs_nid2str(peernid));
454                 GOTO(out, rc = -EACCES);
455         }
456
457         EXIT;
458
459 out:
460         mdt_identity_put(mdt->mdt_identity_cache, identity);
461         return rc;
462 }
463
464 static int old_init_ucred_common(struct mdt_thread_info *info,
465                                  struct lu_nodemap *nodemap,
466                                  bool drop_fs_cap)
467 {
468         struct lu_ucred         *uc = mdt_ucred(info);
469         struct mdt_device       *mdt = info->mti_mdt;
470         struct md_identity      *identity = NULL;
471
472         if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
473                 /* deny access before we get identity ref */
474                 if (nodemap->nmf_deny_unknown)
475                         RETURN(-EACCES);
476
477                 uc->uc_fsuid = nodemap->nm_squash_uid;
478                 uc->uc_fsgid = nodemap->nm_squash_gid;
479                 uc->uc_cap = 0;
480                 uc->uc_suppgids[0] = -1;
481                 uc->uc_suppgids[1] = -1;
482         }
483
484         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
485                 identity = mdt_identity_get(mdt->mdt_identity_cache,
486                                             uc->uc_fsuid);
487                 if (IS_ERR(identity)) {
488                         if (unlikely(PTR_ERR(identity) == -EREMCHG ||
489                                      uc->uc_cap & CFS_CAP_FS_MASK)) {
490                                 identity = NULL;
491                         } else {
492                                 CDEBUG(D_SEC, "Deny access without identity: "
493                                        "uid %u\n", uc->uc_fsuid);
494                                 RETURN(-EACCES);
495                         }
496                 }
497         }
498         uc->uc_identity = identity;
499
500         /* process root_squash here. */
501         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
502
503         /* remove fs privilege for non-root user. */
504         if (uc->uc_fsuid && drop_fs_cap)
505                 uc->uc_cap &= ~CFS_CAP_FS_MASK;
506         uc->uc_valid = UCRED_OLD;
507         ucred_set_jobid(info, uc);
508         ucred_set_nid(info, uc);
509         ucred_set_audit_enabled(info, uc);
510
511         EXIT;
512
513         return 0;
514 }
515
516 static int old_init_ucred(struct mdt_thread_info *info,
517                           struct mdt_body *body, bool drop_fs_cap)
518 {
519         struct lu_ucred *uc = mdt_ucred(info);
520         struct lu_nodemap *nodemap;
521         int rc;
522         ENTRY;
523
524         nodemap = nodemap_get_from_exp(info->mti_exp);
525         if (IS_ERR(nodemap))
526                 RETURN(PTR_ERR(nodemap));
527
528         body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
529                                        NODEMAP_CLIENT_TO_FS, body->mbo_uid);
530         body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
531                                        NODEMAP_CLIENT_TO_FS, body->mbo_gid);
532         body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
533                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
534         body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
535                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
536
537         LASSERT(uc != NULL);
538         uc->uc_valid = UCRED_INVALID;
539         uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
540         uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
541         uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
542         uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
543         uc->uc_suppgids[0] = body->mbo_suppgid;
544         uc->uc_suppgids[1] = -1;
545         uc->uc_ginfo = NULL;
546         uc->uc_cap = body->mbo_capability;
547
548         rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
549         nodemap_putref(nodemap);
550
551         RETURN(rc);
552 }
553
554 static int old_init_ucred_reint(struct mdt_thread_info *info)
555 {
556         struct lu_ucred *uc = mdt_ucred(info);
557         struct lu_nodemap *nodemap;
558         int rc;
559         ENTRY;
560
561         nodemap = nodemap_get_from_exp(info->mti_exp);
562         if (IS_ERR(nodemap))
563                 RETURN(PTR_ERR(nodemap));
564
565         LASSERT(uc != NULL);
566
567         uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
568                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
569         uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
570                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
571
572         uc->uc_valid = UCRED_INVALID;
573         uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
574         uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
575         uc->uc_ginfo = NULL;
576
577         rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
578         nodemap_putref(nodemap);
579
580         RETURN(rc);
581 }
582
583 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
584                                    struct mdt_body *body,
585                                    bool drop_fs_cap)
586 {
587         struct ptlrpc_request   *req = mdt_info_req(info);
588         struct lu_ucred         *uc  = mdt_ucred(info);
589
590         LASSERT(uc != NULL);
591         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
592                 return 0;
593
594         mdt_exit_ucred(info);
595
596         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
597                 return old_init_ucred(info, body, drop_fs_cap);
598         else
599                 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
600 }
601
602 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
603 {
604         return __mdt_init_ucred(info, body, true);
605 }
606
607 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
608  * doesn't set correct fsuid explicitely, but raise capability to allow
609  * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
610  * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
611  * will fail permission check. */
612 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
613                                   struct mdt_body *body)
614 {
615         return __mdt_init_ucred(info, body, false);
616 }
617
618 int mdt_init_ucred_reint(struct mdt_thread_info *info)
619 {
620         struct ptlrpc_request *req = mdt_info_req(info);
621         struct lu_ucred       *uc  = mdt_ucred(info);
622         struct md_attr        *ma  = &info->mti_attr;
623
624         LASSERT(uc != NULL);
625         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
626                 return 0;
627
628         /* LU-5564: for normal close request, skip permission check */
629         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
630             !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
631                 uc->uc_cap |= CFS_CAP_FS_MASK;
632
633         mdt_exit_ucred(info);
634
635         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
636                 return old_init_ucred_reint(info);
637         else
638                 return new_init_ucred(info, REC_INIT, NULL, true);
639 }
640
641 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
642 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
643 {
644         const struct lov_ost_data_v1 *lod;
645         __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
646         __u16 count;
647         int i;
648
649         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
650                 return;
651
652         CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
653                POSTID(&lmm->lmm_oi), lmm_magic,
654                le32_to_cpu(lmm->lmm_pattern));
655
656         /* No support for compount layouts yet */
657         if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
658                 return;
659
660         count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
661         CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
662                le32_to_cpu(lmm->lmm_stripe_size), count);
663
664         /* If it's a directory or a released file, then there are
665          * no actual objects to print, so bail out. */
666         if (valid & OBD_MD_FLDIREA ||
667             le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
668                 return;
669
670         LASSERT(count <= LOV_MAX_STRIPE_COUNT);
671         for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
672                 struct ost_id oi;
673
674                 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
675                 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
676                        i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
677         }
678 }
679
680 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
681 {
682         const struct lmv_mds_md_v1 *lmm1;
683         const struct lmv_foreign_md *lfm;
684         int                        i;
685
686         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
687                 return;
688
689         /* foreign LMV case */
690         lfm = &lmv->lmv_foreign_md;
691         if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
692                 CDEBUG_LIMIT(level,
693                              "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
694                              le32_to_cpu(lfm->lfm_magic),
695                              le32_to_cpu(lfm->lfm_length),
696                              le32_to_cpu(lfm->lfm_type),
697                              le32_to_cpu(lfm->lfm_flags),
698                              le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
699                 return;
700         }
701
702         lmm1 = &lmv->lmv_md_v1;
703         CDEBUG(level,
704                "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
705                le32_to_cpu(lmm1->lmv_magic),
706                le32_to_cpu(lmm1->lmv_master_mdt_index),
707                le32_to_cpu(lmm1->lmv_stripe_count),
708                le32_to_cpu(lmm1->lmv_hash_type));
709
710         if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
711                 return;
712
713         for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
714                 struct lu_fid fid;
715
716                 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
717                 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
718         }
719 }
720
721 /* Shrink and/or grow reply buffers */
722 int mdt_fix_reply(struct mdt_thread_info *info)
723 {
724         struct req_capsule *pill = info->mti_pill;
725         struct mdt_body    *body;
726         int                md_size, md_packed = 0;
727         int                acl_size;
728         int                rc = 0;
729         ENTRY;
730
731         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
732         LASSERT(body != NULL);
733
734         if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
735                                OBD_MD_LINKNAME))
736                 md_size = body->mbo_eadatasize;
737         else
738                 md_size = 0;
739
740         acl_size = body->mbo_aclsize;
741
742         /* this replay - not send info to client */
743         if (info->mti_spec.no_create) {
744                 md_size = 0;
745                 acl_size = 0;
746         }
747
748         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
749                md_size, acl_size);
750 /*
751             &RMF_MDT_BODY,
752             &RMF_MDT_MD,
753             &RMF_ACL, or &RMF_LOGCOOKIES
754 (optional)  &RMF_CAPA1,
755 (optional)  &RMF_CAPA2,
756 (optional)  something else
757 */
758
759         /* MDT_MD buffer may be bigger than packed value, let's shrink all
760          * buffers before growing it */
761         if (info->mti_big_lmm_used) {
762                 /* big_lmm buffer may be used even without packing the result
763                  * into reply, just for internal server needs */
764                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
765                         md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
766                                                          RCL_SERVER);
767
768                 /* free big lmm if md_size is not needed */
769                 if (md_size == 0 || md_packed == 0) {
770                         info->mti_big_lmm_used = 0;
771                 } else {
772                         /* buffer must be allocated separately */
773                         LASSERT(info->mti_attr.ma_lmm !=
774                                 req_capsule_server_get(pill, &RMF_MDT_MD));
775                         req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
776                 }
777         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
778                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
779         }
780
781         if (info->mti_big_acl_used) {
782                 if (acl_size == 0)
783                         info->mti_big_acl_used = 0;
784                 else
785                         req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
786         } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
787                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
788         } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
789                 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
790         }
791
792         /* Shrink optional SECCTX buffer if it is not used */
793         if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
794             req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
795             !(body->mbo_valid & OBD_MD_SECCTX))
796                 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
797
798         /*
799          * Some more field should be shrinked if needed.
800          * This should be done by those who added fields to reply message.
801          */
802
803         /* Grow MD buffer if needed finally */
804         if (info->mti_big_lmm_used) {
805                 void *lmm;
806
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);
811                 if (rc) {
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);
818                 } else {
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,
823                                                              RCL_SERVER) ==
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,
829                                                              RCL_SERVER) ==
830                                                 info->mti_attr.ma_lmv_size);
831                                 memcpy(lmm, info->mti_attr.ma_lmv,
832                                        info->mti_attr.ma_lmv_size);
833                         }
834                 }
835
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;
841         }
842
843         if (info->mti_big_acl_used) {
844                 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
845                        acl_size);
846
847                 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
848                 if (rc) {
849                         body->mbo_valid &= ~OBD_MD_FLACL;
850                 } else {
851                         void *acl = req_capsule_server_get(pill, &RMF_ACL);
852
853                         memcpy(acl, info->mti_big_acl, acl_size);
854                 }
855
856                 info->mti_big_acl_used = 0;
857         }
858
859         RETURN(rc);
860 }
861
862
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,
867                            struct md_attr *ma)
868 {
869         struct mdt_body *repbody = NULL;
870         const struct lu_attr *la = &ma->ma_attr;
871         struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
872         int rc;
873         __u64 need = 0;
874         struct hsm_action_item hai = {
875                 .hai_len = sizeof(hai),
876                 .hai_action = HSMA_REMOVE,
877                 .hai_extent.length = -1,
878                 .hai_cookie = 0,
879                 .hai_gid = 0,
880         };
881         int archive_id;
882
883         ENTRY;
884
885         if (mdt_info_req(info) != NULL) {
886                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
887                 LASSERT(repbody != NULL);
888         } else {
889                 CDEBUG(D_INFO, "not running in a request/reply context\n");
890         }
891
892         if ((ma->ma_valid & MA_INODE) && repbody != NULL)
893                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
894
895         if (ma->ma_valid & MA_LOV) {
896                 CERROR("No need in LOV EA upon unlink\n");
897                 dump_stack();
898         }
899         if (repbody != NULL)
900                 repbody->mbo_eadatasize = 0;
901
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)
905                 RETURN(0);
906
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))
909                 RETURN(0);
910
911         if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
912                 RETURN(0);
913
914         need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
915         if (need != 0) {
916                 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
917                  * try setting them */
918                 ma->ma_need |= need;
919                 rc = mdt_attr_get_complex(info, mo, ma);
920                 if (rc) {
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);
924                         RETURN(0);
925                 }
926
927                 if (need & MA_INODE) {
928                         if (ma->ma_valid & MA_INODE) {
929                                 if (ma->ma_attr.la_nlink != 0)
930                                         RETURN(0);
931                         } else {
932                                 RETURN(0);
933                         }
934                 }
935
936                 if (need & MA_HSM) {
937                         if (ma->ma_valid & MA_HSM) {
938                                 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
939                                         RETURN(0);
940                         } else {
941                                 RETURN(0);
942                         }
943                 }
944         }
945
946         /* RAoLU policy is active, last close on file has occured,
947          * file is unlinked, file is archived, so create remove request
948          * for copytool!
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;
952         else
953                 archive_id = cdt->cdt_default_archive_id;
954
955         hai.hai_fid = *mdt_object_fid(mo);
956
957         rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
958                                   &hai);
959         if (rc)
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);
963
964         RETURN(0);
965 }
966
967 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
968                                   struct md_attr *ma)
969 {
970         __u64 out;
971
972         out = 0;
973         if (in & MDS_ATTR_MODE)
974                 out |= LA_MODE;
975         if (in & MDS_ATTR_UID)
976                 out |= LA_UID;
977         if (in & MDS_ATTR_GID)
978                 out |= LA_GID;
979         if (in & MDS_ATTR_SIZE)
980                 out |= LA_SIZE;
981         if (in & MDS_ATTR_BLOCKS)
982                 out |= LA_BLOCKS;
983         if (in & MDS_ATTR_ATIME_SET)
984                 out |= LA_ATIME;
985         if (in & MDS_ATTR_CTIME_SET)
986                 out |= LA_CTIME;
987         if (in & MDS_ATTR_MTIME_SET)
988                 out |= LA_MTIME;
989         if (in & MDS_ATTR_ATTR_FLAG)
990                 out |= LA_FLAGS;
991         if (in & MDS_ATTR_KILL_SUID)
992                 out |= LA_KILL_SUID;
993         if (in & MDS_ATTR_KILL_SGID)
994                 out |= LA_KILL_SGID;
995         if (in & MDS_ATTR_PROJID)
996                 out |= LA_PROJID;
997         if (in & MDS_ATTR_LSIZE)
998                 out |= LA_LSIZE;
999         if (in & MDS_ATTR_LBLOCKS)
1000                 out |= LA_LBLOCKS;
1001
1002         if (in & MDS_ATTR_FROM_OPEN)
1003                 rr->rr_flags |= MRF_OPEN_TRUNC;
1004         if (in & MDS_ATTR_OVERRIDE)
1005                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1006         if (in & MDS_ATTR_FORCE)
1007                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1008
1009         in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1010                 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1011                 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1012                 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1013                 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1014                 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1015                 MDS_ATTR_OVERRIDE);
1016         if (in != 0)
1017                 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", in);
1018         return out;
1019 }
1020
1021 /* unpacking */
1022
1023 int mdt_name_unpack(struct req_capsule *pill,
1024                     const struct req_msg_field *field,
1025                     struct lu_name *ln,
1026                     enum mdt_name_flags flags)
1027 {
1028         ln->ln_name = req_capsule_client_get(pill, field);
1029         ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1030
1031         if (!lu_name_is_valid(ln)) {
1032                 ln->ln_name = NULL;
1033                 ln->ln_namelen = 0;
1034
1035                 return -EPROTO;
1036         }
1037
1038         if ((flags & MNF_FIX_ANON) &&
1039             ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1040                 /* Newer (3.x) kernels use a name of "/" for the
1041                  * "anonymous" disconnected dentries from NFS
1042                  * filehandle conversion. See d_obtain_alias(). */
1043                 ln->ln_name = NULL;
1044                 ln->ln_namelen = 0;
1045         }
1046
1047         return 0;
1048 }
1049
1050 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1051                                   const char **secctx_name,
1052                                   void **secctx, size_t *secctx_size)
1053 {
1054         const char *name;
1055         size_t name_size;
1056
1057         *secctx_name = NULL;
1058         *secctx = NULL;
1059         *secctx_size = 0;
1060
1061         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1062             !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1063                 return 0;
1064
1065         name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1066                                          RCL_CLIENT);
1067         if (name_size == 0)
1068                 return 0;
1069
1070         if (name_size > XATTR_NAME_MAX + 1)
1071                 return -EPROTO;
1072
1073         name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1074         if (strnlen(name, name_size) != name_size - 1)
1075                 return -EPROTO;
1076
1077         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1078             !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1079                 return -EPROTO;
1080
1081         *secctx_name = name;
1082         *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1083         *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1084
1085         return 0;
1086 }
1087
1088 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1089 {
1090         struct lu_ucred *uc = mdt_ucred(info);
1091         struct md_attr *ma = &info->mti_attr;
1092         struct lu_attr *la = &ma->ma_attr;
1093         struct req_capsule *pill = info->mti_pill;
1094         struct mdt_reint_record *rr = &info->mti_rr;
1095         struct mdt_rec_setattr *rec;
1096         struct lu_nodemap *nodemap;
1097
1098         ENTRY;
1099
1100         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1101         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1102         if (rec == NULL)
1103                 RETURN(-EFAULT);
1104
1105         /* This prior initialization is needed for old_init_ucred_reint() */
1106         uc->uc_fsuid = rec->sa_fsuid;
1107         uc->uc_fsgid = rec->sa_fsgid;
1108         uc->uc_cap   = rec->sa_cap;
1109         uc->uc_suppgids[0] = rec->sa_suppgid;
1110         uc->uc_suppgids[1] = -1;
1111
1112         rr->rr_fid1 = &rec->sa_fid;
1113         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1114         la->la_mode  = rec->sa_mode;
1115         la->la_flags = rec->sa_attr_flags;
1116
1117         nodemap = nodemap_get_from_exp(info->mti_exp);
1118         if (IS_ERR(nodemap))
1119                 RETURN(PTR_ERR(nodemap));
1120
1121         la->la_uid   = nodemap_map_id(nodemap, NODEMAP_UID,
1122                                       NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1123         la->la_gid   = nodemap_map_id(nodemap, NODEMAP_GID,
1124                                       NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1125         la->la_projid = rec->sa_projid;
1126         nodemap_putref(nodemap);
1127
1128         la->la_size  = rec->sa_size;
1129         la->la_blocks = rec->sa_blocks;
1130         la->la_ctime = rec->sa_ctime;
1131         la->la_atime = rec->sa_atime;
1132         la->la_mtime = rec->sa_mtime;
1133         ma->ma_valid = MA_INODE;
1134
1135         ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1136                                 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1137                                 MDS_PCC_ATTACH);
1138         RETURN(0);
1139 }
1140
1141 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1142 {
1143         struct req_capsule *pill = info->mti_pill;
1144         struct mdt_ioepoch *ioepoch;
1145         ENTRY;
1146
1147         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1148                 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1149         else
1150                 ioepoch = NULL;
1151
1152         if (ioepoch == NULL)
1153                 RETURN(-EPROTO);
1154
1155         info->mti_open_handle = ioepoch->mio_open_handle;
1156
1157         RETURN(0);
1158 }
1159
1160 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1161         struct req_capsule      *pill = info->mti_pill;
1162
1163         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1164                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1165                 if (info->mti_dlm_req == NULL)
1166                         RETURN(-EFAULT);
1167         }
1168
1169         RETURN(0);
1170 }
1171
1172 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1173 {
1174         struct mdt_reint_record *rr = &info->mti_rr;
1175         struct md_attr          *ma = &info->mti_attr;
1176         struct req_capsule      *pill = info->mti_pill;
1177         int rc;
1178         ENTRY;
1179
1180         rc = mdt_setattr_unpack_rec(info);
1181         if (rc)
1182                 RETURN(rc);
1183
1184         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1185                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1186                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1187                                                         RCL_CLIENT);
1188
1189                 if (rr->rr_eadatalen > 0) {
1190                         const struct lmv_user_md        *lum;
1191
1192                         lum = rr->rr_eadata;
1193                         /* Sigh ma_valid(from req) does not indicate whether
1194                          * it will set LOV/LMV EA, so we have to check magic */
1195                         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1196                                 ma->ma_valid |= MA_LMV;
1197                                 ma->ma_lmv = (void *)rr->rr_eadata;
1198                                 ma->ma_lmv_size = rr->rr_eadatalen;
1199                         } else {
1200                                 ma->ma_valid |= MA_LOV;
1201                                 ma->ma_lmm = (void *)rr->rr_eadata;
1202                                 ma->ma_lmm_size = rr->rr_eadatalen;
1203                         }
1204                 }
1205         }
1206
1207         rc = mdt_dlmreq_unpack(info);
1208         RETURN(rc);
1209 }
1210
1211 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1212 {
1213         struct md_attr          *ma = &info->mti_attr;
1214         struct req_capsule      *pill = info->mti_pill;
1215         ENTRY;
1216
1217         if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1218                 RETURN(0);
1219
1220         req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1221
1222         if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1223             req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1224                 RETURN(-EFAULT);
1225
1226         RETURN(0);
1227 }
1228
1229 int mdt_close_unpack(struct mdt_thread_info *info)
1230 {
1231         int rc;
1232         ENTRY;
1233
1234         rc = mdt_close_handle_unpack(info);
1235         if (rc)
1236                 RETURN(rc);
1237
1238         rc = mdt_setattr_unpack_rec(info);
1239         if (rc)
1240                 RETURN(rc);
1241
1242         rc = mdt_close_intent_unpack(info);
1243         if (rc)
1244                 RETURN(rc);
1245
1246         RETURN(mdt_init_ucred_reint(info));
1247 }
1248
1249 static int mdt_create_unpack(struct mdt_thread_info *info)
1250 {
1251         struct lu_ucred *uc  = mdt_ucred(info);
1252         struct mdt_rec_create *rec;
1253         struct lu_attr *attr = &info->mti_attr.ma_attr;
1254         struct mdt_reint_record *rr = &info->mti_rr;
1255         struct req_capsule      *pill = info->mti_pill;
1256         struct md_op_spec       *sp = &info->mti_spec;
1257         int rc;
1258
1259         ENTRY;
1260
1261         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1262         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1263         if (rec == NULL)
1264                 RETURN(-EFAULT);
1265
1266         /* This prior initialization is needed for old_init_ucred_reint() */
1267         uc->uc_fsuid = rec->cr_fsuid;
1268         uc->uc_fsgid = rec->cr_fsgid;
1269         uc->uc_cap   = rec->cr_cap;
1270         uc->uc_suppgids[0] = rec->cr_suppgid1;
1271         uc->uc_suppgids[1] = -1;
1272         uc->uc_umask = rec->cr_umask;
1273
1274         rr->rr_fid1 = &rec->cr_fid1;
1275         rr->rr_fid2 = &rec->cr_fid2;
1276         attr->la_mode = rec->cr_mode;
1277         attr->la_rdev  = rec->cr_rdev;
1278         attr->la_uid   = rec->cr_fsuid;
1279         attr->la_gid   = rec->cr_fsgid;
1280         attr->la_ctime = rec->cr_time;
1281         attr->la_mtime = rec->cr_time;
1282         attr->la_atime = rec->cr_time;
1283         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1284                          LA_CTIME | LA_MTIME | LA_ATIME;
1285         memset(&sp->u, 0, sizeof(sp->u));
1286         sp->sp_cr_flags = get_mrc_cr_flags(rec);
1287
1288         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1289         if (rc < 0)
1290                 RETURN(rc);
1291
1292         if (S_ISLNK(attr->la_mode)) {
1293                 const char *tgt = NULL;
1294
1295                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1296                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1297                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1298                         sp->u.sp_symname = tgt;
1299                 }
1300                 if (tgt == NULL)
1301                         RETURN(-EFAULT);
1302         } else {
1303                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1304                 if (S_ISDIR(attr->la_mode) &&
1305                     req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1306                         sp->u.sp_ea.eadata =
1307                                 req_capsule_client_get(pill, &RMF_EADATA);
1308                         sp->u.sp_ea.eadatalen =
1309                                 req_capsule_get_size(pill, &RMF_EADATA,
1310                                                      RCL_CLIENT);
1311                         sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1312                 }
1313         }
1314
1315         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1316                                     &sp->sp_cr_file_secctx,
1317                                     &sp->sp_cr_file_secctx_size);
1318         if (rc < 0)
1319                 RETURN(rc);
1320
1321         rc = req_check_sepol(pill);
1322         if (rc)
1323                 RETURN(rc);
1324
1325         rc = mdt_dlmreq_unpack(info);
1326         RETURN(rc);
1327 }
1328
1329 static int mdt_link_unpack(struct mdt_thread_info *info)
1330 {
1331         struct lu_ucred *uc  = mdt_ucred(info);
1332         struct mdt_rec_link *rec;
1333         struct lu_attr *attr = &info->mti_attr.ma_attr;
1334         struct mdt_reint_record *rr = &info->mti_rr;
1335         struct req_capsule *pill = info->mti_pill;
1336         int rc;
1337
1338         ENTRY;
1339
1340         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1341         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1342         if (rec == NULL)
1343                 RETURN(-EFAULT);
1344
1345         /* This prior initialization is needed for old_init_ucred_reint() */
1346         uc->uc_fsuid = rec->lk_fsuid;
1347         uc->uc_fsgid = rec->lk_fsgid;
1348         uc->uc_cap   = rec->lk_cap;
1349         uc->uc_suppgids[0] = rec->lk_suppgid1;
1350         uc->uc_suppgids[1] = rec->lk_suppgid2;
1351
1352         attr->la_uid = rec->lk_fsuid;
1353         attr->la_gid = rec->lk_fsgid;
1354         rr->rr_fid1 = &rec->lk_fid1;
1355         rr->rr_fid2 = &rec->lk_fid2;
1356         attr->la_ctime = rec->lk_time;
1357         attr->la_mtime = rec->lk_time;
1358         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1359
1360         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1361         if (rc < 0)
1362                 RETURN(rc);
1363
1364         rc = req_check_sepol(pill);
1365         if (rc)
1366                 RETURN(rc);
1367
1368         rc = mdt_dlmreq_unpack(info);
1369
1370         RETURN(rc);
1371 }
1372
1373 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1374 {
1375         struct lu_ucred *uc  = mdt_ucred(info);
1376         struct mdt_rec_unlink *rec;
1377         struct lu_attr *attr = &info->mti_attr.ma_attr;
1378         struct mdt_reint_record *rr = &info->mti_rr;
1379         struct req_capsule      *pill = info->mti_pill;
1380         int rc;
1381
1382         ENTRY;
1383
1384         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1385         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1386         if (rec == NULL)
1387                 RETURN(-EFAULT);
1388
1389         /* This prior initialization is needed for old_init_ucred_reint() */
1390         uc->uc_fsuid = rec->ul_fsuid;
1391         uc->uc_fsgid = rec->ul_fsgid;
1392         uc->uc_cap   = rec->ul_cap;
1393         uc->uc_suppgids[0] = rec->ul_suppgid1;
1394         uc->uc_suppgids[1] = -1;
1395
1396         attr->la_uid = rec->ul_fsuid;
1397         attr->la_gid = rec->ul_fsgid;
1398         rr->rr_fid1 = &rec->ul_fid1;
1399         rr->rr_fid2 = &rec->ul_fid2;
1400         attr->la_ctime = rec->ul_time;
1401         attr->la_mtime = rec->ul_time;
1402         attr->la_mode  = rec->ul_mode;
1403         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1404
1405         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1406         if (rc < 0)
1407                 RETURN(rc);
1408
1409         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1410
1411         rc = req_check_sepol(pill);
1412         if (rc)
1413                 RETURN(rc);
1414
1415         rc = mdt_dlmreq_unpack(info);
1416         RETURN(rc);
1417 }
1418
1419 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1420 {
1421         info->mti_spec.sp_rm_entry = 1;
1422         return mdt_unlink_unpack(info);
1423 }
1424
1425 static int mdt_rename_unpack(struct mdt_thread_info *info)
1426 {
1427         struct lu_ucred *uc = mdt_ucred(info);
1428         struct mdt_rec_rename *rec;
1429         struct lu_attr *attr = &info->mti_attr.ma_attr;
1430         struct mdt_reint_record *rr = &info->mti_rr;
1431         struct req_capsule *pill = info->mti_pill;
1432         struct md_op_spec *spec = &info->mti_spec;
1433         int rc;
1434
1435         ENTRY;
1436
1437         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1438         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1439         if (rec == NULL)
1440                 RETURN(-EFAULT);
1441
1442         /* This prior initialization is needed for old_init_ucred_reint() */
1443         uc->uc_fsuid = rec->rn_fsuid;
1444         uc->uc_fsgid = rec->rn_fsgid;
1445         uc->uc_cap   = rec->rn_cap;
1446         uc->uc_suppgids[0] = rec->rn_suppgid1;
1447         uc->uc_suppgids[1] = rec->rn_suppgid2;
1448
1449         attr->la_uid = rec->rn_fsuid;
1450         attr->la_gid = rec->rn_fsgid;
1451         rr->rr_fid1 = &rec->rn_fid1;
1452         rr->rr_fid2 = &rec->rn_fid2;
1453         attr->la_ctime = rec->rn_time;
1454         attr->la_mtime = rec->rn_time;
1455         /* rename_tgt contains the mode already */
1456         attr->la_mode = rec->rn_mode;
1457         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1458
1459         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1460         if (rc < 0)
1461                 RETURN(rc);
1462
1463         rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1464         if (rc < 0)
1465                 RETURN(rc);
1466
1467         spec->no_create = !!req_is_replay(mdt_info_req(info));
1468
1469         rc = req_check_sepol(pill);
1470         if (rc)
1471                 RETURN(rc);
1472
1473         rc = mdt_dlmreq_unpack(info);
1474
1475         RETURN(rc);
1476 }
1477
1478 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1479 {
1480         struct lu_ucred *uc = mdt_ucred(info);
1481         struct mdt_rec_rename *rec;
1482         struct lu_attr *attr = &info->mti_attr.ma_attr;
1483         struct mdt_reint_record *rr = &info->mti_rr;
1484         struct req_capsule *pill = info->mti_pill;
1485         struct md_op_spec *spec = &info->mti_spec;
1486         int rc;
1487
1488         ENTRY;
1489
1490         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1491         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1492         if (rec == NULL)
1493                 RETURN(-EFAULT);
1494
1495         /* This prior initialization is needed for old_init_ucred_reint() */
1496         uc->uc_fsuid = rec->rn_fsuid;
1497         uc->uc_fsgid = rec->rn_fsgid;
1498         uc->uc_cap   = rec->rn_cap;
1499         uc->uc_suppgids[0] = rec->rn_suppgid1;
1500         uc->uc_suppgids[1] = rec->rn_suppgid2;
1501
1502         attr->la_uid = rec->rn_fsuid;
1503         attr->la_gid = rec->rn_fsgid;
1504         rr->rr_fid1 = &rec->rn_fid1;
1505         rr->rr_fid2 = &rec->rn_fid2;
1506         attr->la_ctime = rec->rn_time;
1507         attr->la_mtime = rec->rn_time;
1508         /* rename_tgt contains the mode already */
1509         attr->la_mode = rec->rn_mode;
1510         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1511         spec->sp_cr_flags = 0;
1512
1513         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1514         if (rc < 0)
1515                 RETURN(rc);
1516
1517         if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1518                 rc = mdt_close_handle_unpack(info);
1519                 if (rc)
1520                         RETURN(rc);
1521
1522                 spec->sp_migrate_close = 1;
1523         } else {
1524                 spec->sp_migrate_close = 0;
1525         }
1526
1527         /* lustre version > 2.11 migration packs lum */
1528         if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1529                 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1530                         rr->rr_eadatalen = req_capsule_get_size(pill,
1531                                                                 &RMF_EADATA,
1532                                                                 RCL_CLIENT);
1533
1534                         if (rr->rr_eadatalen > 0) {
1535                                 rr->rr_eadata = req_capsule_client_get(pill,
1536                                                                 &RMF_EADATA);
1537                                 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1538                                 spec->u.sp_ea.eadata = rr->rr_eadata;
1539                                 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1540                         }
1541                 } else {
1542                         /* old client doesn't provide lum. */
1543                         RETURN(-EOPNOTSUPP);
1544                 }
1545         }
1546
1547         spec->no_create = !!req_is_replay(mdt_info_req(info));
1548
1549         rc = mdt_dlmreq_unpack(info);
1550
1551         RETURN(rc);
1552 }
1553
1554 /*
1555  * please see comment above LOV_MAGIC_V1_DEFINED
1556  */
1557 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1558 {
1559         struct lov_user_md_v1   *v1 = eadata;
1560
1561         LASSERT(v1);
1562
1563         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1564                 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1565                         v1->lmm_magic |= LOV_MAGIC_DEFINED;
1566                 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1567                          __swab32(LOV_MAGIC_MAGIC))
1568                         v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1569         }
1570 }
1571
1572 static int mdt_open_unpack(struct mdt_thread_info *info)
1573 {
1574         struct lu_ucred *uc = mdt_ucred(info);
1575         struct mdt_rec_create *rec;
1576         struct lu_attr *attr = &info->mti_attr.ma_attr;
1577         struct req_capsule *pill = info->mti_pill;
1578         struct mdt_reint_record *rr = &info->mti_rr;
1579         struct ptlrpc_request *req = mdt_info_req(info);
1580         struct md_op_spec *sp = &info->mti_spec;
1581         int rc;
1582         ENTRY;
1583
1584         BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1585                      sizeof(struct mdt_rec_reint));
1586         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1587         if (rec == NULL)
1588                 RETURN(-EFAULT);
1589
1590         /* This prior initialization is needed for old_init_ucred_reint() */
1591         uc->uc_fsuid = rec->cr_fsuid;
1592         uc->uc_fsgid = rec->cr_fsgid;
1593         uc->uc_cap   = rec->cr_cap;
1594         uc->uc_suppgids[0] = rec->cr_suppgid1;
1595         uc->uc_suppgids[1] = rec->cr_suppgid2;
1596         uc->uc_umask = rec->cr_umask;
1597
1598         rr->rr_fid1   = &rec->cr_fid1;
1599         rr->rr_fid2   = &rec->cr_fid2;
1600         rr->rr_open_handle = &rec->cr_open_handle_old;
1601         attr->la_mode = rec->cr_mode;
1602         attr->la_rdev  = rec->cr_rdev;
1603         attr->la_uid   = rec->cr_fsuid;
1604         attr->la_gid   = rec->cr_fsgid;
1605         attr->la_ctime = rec->cr_time;
1606         attr->la_mtime = rec->cr_time;
1607         attr->la_atime = rec->cr_time;
1608         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1609                          LA_CTIME | LA_MTIME | LA_ATIME;
1610         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1611         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1612         /* Do not trigger ASSERTION if client miss to set such flags. */
1613         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1614                 RETURN(-EPROTO);
1615
1616         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1617
1618         mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1619
1620         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1621                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1622                                                         RCL_CLIENT);
1623
1624                 if (rr->rr_eadatalen > 0) {
1625                         rr->rr_eadata = req_capsule_client_get(pill,
1626                                                                &RMF_EADATA);
1627                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1628                         sp->u.sp_ea.eadata = rr->rr_eadata;
1629                         sp->sp_archive_id = rec->cr_archive_id;
1630                         sp->no_create = !!req_is_replay(req);
1631                         mdt_fix_lov_magic(info, rr->rr_eadata);
1632                 }
1633
1634                 /*
1635                  * Client default md_size may be 0 right after client start,
1636                  * until all osc are connected, set here just some reasonable
1637                  * value to prevent misbehavior.
1638                  */
1639                 if (rr->rr_eadatalen == 0 &&
1640                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1641                         rr->rr_eadatalen = MIN_MD_SIZE;
1642         }
1643
1644         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1645                                     &sp->sp_cr_file_secctx,
1646                                     &sp->sp_cr_file_secctx_size);
1647         if (rc < 0)
1648                 RETURN(rc);
1649
1650         rc = req_check_sepol(pill);
1651         if (rc)
1652                 RETURN(rc);
1653
1654         RETURN(rc);
1655 }
1656
1657 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1658 {
1659         struct mdt_reint_record *rr = &info->mti_rr;
1660         struct lu_ucred *uc = mdt_ucred(info);
1661         struct lu_attr *attr = &info->mti_attr.ma_attr;
1662         struct req_capsule *pill = info->mti_pill;
1663         struct mdt_rec_setxattr *rec;
1664         int rc;
1665         ENTRY;
1666
1667
1668         BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1669                      sizeof(struct mdt_rec_reint));
1670         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1671         if (rec == NULL)
1672                 RETURN(-EFAULT);
1673
1674         /* This prior initialization is needed for old_init_ucred_reint() */
1675         uc->uc_fsuid  = rec->sx_fsuid;
1676         uc->uc_fsgid  = rec->sx_fsgid;
1677         uc->uc_cap    = rec->sx_cap;
1678         uc->uc_suppgids[0] = rec->sx_suppgid1;
1679         uc->uc_suppgids[1] = -1;
1680
1681         rr->rr_opcode = rec->sx_opcode;
1682         rr->rr_fid1   = &rec->sx_fid;
1683         attr->la_valid = rec->sx_valid;
1684         attr->la_ctime = rec->sx_time;
1685         attr->la_size = rec->sx_size;
1686         attr->la_flags = rec->sx_flags;
1687
1688         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1689         if (rc < 0)
1690                 RETURN(rc);
1691
1692         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1693                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1694                                                         RCL_CLIENT);
1695
1696                 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1697                         RETURN(-E2BIG);
1698
1699                 if (rr->rr_eadatalen > 0) {
1700                         rr->rr_eadata = req_capsule_client_get(pill,
1701                                                                &RMF_EADATA);
1702                         if (rr->rr_eadata == NULL)
1703                                 RETURN(-EFAULT);
1704                 } else {
1705                         rr->rr_eadata = NULL;
1706                 }
1707         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1708                 CDEBUG(D_INFO, "no xattr data supplied\n");
1709                 RETURN(-EFAULT);
1710         }
1711
1712         rc = req_check_sepol(pill);
1713         if (rc)
1714                 RETURN(rc);
1715
1716         if (mdt_dlmreq_unpack(info) < 0)
1717                 RETURN(-EPROTO);
1718
1719         RETURN(0);
1720 }
1721
1722 static int mdt_resync_unpack(struct mdt_thread_info *info)
1723 {
1724         struct req_capsule      *pill = info->mti_pill;
1725         struct mdt_reint_record *rr   = &info->mti_rr;
1726         struct lu_ucred         *uc     = mdt_ucred(info);
1727         struct mdt_rec_resync   *rec;
1728         ENTRY;
1729
1730         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1731         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1732         if (rec == NULL)
1733                 RETURN(-EFAULT);
1734
1735         /* This prior initialization is needed for old_init_ucred_reint() */
1736         uc->uc_fsuid = rec->rs_fsuid;
1737         uc->uc_fsgid = rec->rs_fsgid;
1738         uc->uc_cap   = rec->rs_cap;
1739
1740         rr->rr_fid1   = &rec->rs_fid;
1741         rr->rr_mirror_id = rec->rs_mirror_id;
1742
1743         /* cookie doesn't need to be swapped but it has been swapped
1744          * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1745          * restoring. */
1746         if (ptlrpc_req_need_swab(mdt_info_req(info)))
1747                 __swab64s(&rec->rs_lease_handle.cookie);
1748         rr->rr_lease_handle = &rec->rs_lease_handle;
1749
1750         RETURN(mdt_dlmreq_unpack(info));
1751 }
1752
1753 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1754
1755 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1756         [REINT_SETATTR]  = mdt_setattr_unpack,
1757         [REINT_CREATE]   = mdt_create_unpack,
1758         [REINT_LINK]     = mdt_link_unpack,
1759         [REINT_UNLINK]   = mdt_unlink_unpack,
1760         [REINT_RENAME]   = mdt_rename_unpack,
1761         [REINT_OPEN]     = mdt_open_unpack,
1762         [REINT_SETXATTR] = mdt_setxattr_unpack,
1763         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1764         [REINT_MIGRATE]  = mdt_migrate_unpack,
1765         [REINT_RESYNC]   = mdt_resync_unpack,
1766 };
1767
1768 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1769 {
1770         int rc;
1771         ENTRY;
1772
1773         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1774         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1775                 info->mti_rr.rr_opcode = op;
1776                 rc = mdt_reint_unpackers[op](info);
1777         } else {
1778                 CERROR("Unexpected opcode %d\n", op);
1779                 rc = -EFAULT;
1780         }
1781         RETURN(rc);
1782 }
1783
1784 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1785                              struct mdt_object *child)
1786 {
1787         char *secctx_name;
1788         struct lu_buf *buffer;
1789         struct mdt_body *repbody;
1790         struct req_capsule *pill = info->mti_pill;
1791         int rc = 0;
1792
1793         if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1794             req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1795                 secctx_name =
1796                         req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1797                 buffer = &info->mti_buf;
1798
1799                 /* fill reply buffer with security context now */
1800                 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1801                                                       RCL_SERVER);
1802                 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1803                                                         &RMF_FILE_SECCTX);
1804                 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1805                                   buffer, secctx_name);
1806                 if (rc >= 0) {
1807                         CDEBUG(D_SEC,
1808                                "found security context of size %d for "DFID"\n",
1809                                rc, PFID(mdt_object_fid(child)));
1810
1811                         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1812                         repbody->mbo_valid |= OBD_MD_SECCTX;
1813                         if (rc < buffer->lb_len)
1814                                 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1815                                                    RCL_SERVER);
1816                         rc = 0;
1817                 } else {
1818                         CDEBUG(D_SEC,
1819                              "security context not found for "DFID": rc = %d\n",
1820                              PFID(mdt_object_fid(child)), rc);
1821                         req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1822                                            RCL_SERVER);
1823                         /* handling -ENOENT is important because it may change
1824                          * object state in DNE env dropping LOHA_EXISTS flag,
1825                          * it is important to return that to the caller.
1826                          * Check LU-13115 for details.
1827                          */
1828                         if (rc != -ENOENT)
1829                                 rc = 0;
1830                 }
1831         }
1832         return rc;
1833 }