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