Whamcloud - gitweb
LU-6528 mdt: keep FS capability for intent_getattr
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_lib.c
37  *
38  * Lustre Metadata Target (mdt) request unpacking helper.
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  * Author: Mike Shaver <shaver@clusterfs.com>
44  * Author: Nikita Danilov <nikita@clusterfs.com>
45  * Author: Huang Hua <huanghua@clusterfs.com>
46  * Author: Fan Yong <fanyong@clusterfs.com>
47  */
48
49 #define DEBUG_SUBSYSTEM S_MDS
50
51 #include <linux/user_namespace.h>
52 #ifdef HAVE_UIDGID_HEADER
53 # include <linux/uidgid.h>
54 #endif
55 #include "mdt_internal.h"
56 #include <lnet/nidstr.h>
57 #include <lustre_nodemap.h>
58
59 typedef enum ucred_init_type {
60         NONE_INIT       = 0,
61         BODY_INIT       = 1,
62         REC_INIT        = 2
63 } ucred_init_type_t;
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 rw_semaphore *sem,
87                                struct list_head *nidlist,
88                                lnet_nid_t peernid)
89 {
90         int rc;
91         ENTRY;
92         down_read(sem);
93         rc = cfs_match_nid(peernid, nidlist);
94         up_read(sem);
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_sem,
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 int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
144                           void *buf, bool drop_fs_cap)
145 {
146         struct ptlrpc_request   *req = mdt_info_req(info);
147         struct mdt_device       *mdt = info->mti_mdt;
148         struct ptlrpc_user_desc *pud = req->rq_user_desc;
149         struct lu_ucred         *ucred = mdt_ucred(info);
150         lnet_nid_t               peernid = req->rq_peer.nid;
151         __u32                    perm = 0;
152         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
153         int                      setuid;
154         int                      setgid;
155         int                      rc = 0;
156
157         ENTRY;
158
159         LASSERT(req->rq_auth_gss);
160         LASSERT(!req->rq_auth_usr_mdt);
161         LASSERT(req->rq_user_desc);
162         LASSERT(ucred != NULL);
163
164         ucred->uc_valid = UCRED_INVALID;
165
166         ucred->uc_o_uid   = pud->pud_uid;
167         ucred->uc_o_gid   = pud->pud_gid;
168         ucred->uc_o_fsuid = pud->pud_fsuid;
169         ucred->uc_o_fsgid = pud->pud_fsgid;
170
171         if (type == BODY_INIT) {
172                 struct mdt_body *body = (struct mdt_body *)buf;
173
174                 ucred->uc_suppgids[0] = body->mbo_suppgid;
175                 ucred->uc_suppgids[1] = -1;
176         }
177
178         /* sanity check: we expect the uid which client claimed is true */
179         if (remote) {
180                 if (!uid_valid(make_kuid(&init_user_ns, req->rq_auth_mapped_uid))) {
181                         CDEBUG(D_SEC, "remote user not mapped, deny access!\n");
182                         RETURN(-EACCES);
183                 }
184
185                 if (ptlrpc_user_desc_do_idmap(req, pud))
186                         RETURN(-EACCES);
187
188                 if (req->rq_auth_mapped_uid != pud->pud_uid) {
189                         CDEBUG(D_SEC, "remote client %s: auth/mapped uid %u/%u "
190                                "while client claims %u:%u/%u:%u\n",
191                                libcfs_nid2str(peernid), req->rq_auth_uid,
192                                req->rq_auth_mapped_uid,
193                                pud->pud_uid, pud->pud_gid,
194                                pud->pud_fsuid, pud->pud_fsgid);
195                         RETURN(-EACCES);
196                 }
197         } else {
198                 if (req->rq_auth_uid != pud->pud_uid) {
199                         CDEBUG(D_SEC, "local client %s: auth uid %u "
200                                "while client claims %u:%u/%u:%u\n",
201                                libcfs_nid2str(peernid), req->rq_auth_uid,
202                                pud->pud_uid, pud->pud_gid,
203                                pud->pud_fsuid, pud->pud_fsgid);
204                         RETURN(-EACCES);
205                 }
206         }
207
208         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
209                 if (remote) {
210                         CDEBUG(D_SEC, "remote client must run with identity_get "
211                                "enabled!\n");
212                         RETURN(-EACCES);
213                 } else {
214                         ucred->uc_identity = NULL;
215                         perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
216                                CFS_SETGRP_PERM;
217                 }
218         } else {
219                 struct md_identity *identity;
220
221                 identity = mdt_identity_get(mdt->mdt_identity_cache,
222                                             pud->pud_uid);
223                 if (IS_ERR(identity)) {
224                         if (unlikely(PTR_ERR(identity) == -EREMCHG &&
225                                      !remote)) {
226                                 ucred->uc_identity = NULL;
227                                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
228                                        CFS_SETGRP_PERM;
229                         } else {
230                                 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
231                                        pud->pud_uid);
232                                 RETURN(-EACCES);
233                         }
234                 } else {
235                         ucred->uc_identity = identity;
236                         perm = mdt_identity_get_perm(ucred->uc_identity,
237                                                      remote, peernid);
238                 }
239         }
240
241         /* find out the setuid/setgid attempt */
242         setuid = (pud->pud_uid != pud->pud_fsuid);
243         setgid = ((pud->pud_gid != pud->pud_fsgid) ||
244                   (ucred->uc_identity &&
245                    (pud->pud_gid != ucred->uc_identity->mi_gid)));
246
247         /* check permission of setuid */
248         if (setuid && !(perm & CFS_SETUID_PERM)) {
249                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
250                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
251                 GOTO(out, rc = -EACCES);
252         }
253
254         /* check permission of setgid */
255         if (setgid && !(perm & CFS_SETGID_PERM)) {
256                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
257                        "from %s\n", pud->pud_uid, pud->pud_gid,
258                        pud->pud_fsuid, pud->pud_fsgid,
259                        ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
260                 GOTO(out, rc = -EACCES);
261         }
262
263         /*
264          * NB: remote client not allowed to setgroups anyway.
265          */
266         if (!remote && perm & CFS_SETGRP_PERM) {
267                 if (pud->pud_ngroups) {
268                         /* setgroups for local client */
269                         ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
270                         if (!ucred->uc_ginfo) {
271                                 CERROR("failed to alloc %d groups\n",
272                                        pud->pud_ngroups);
273                                 GOTO(out, rc = -ENOMEM);
274                         }
275
276                         lustre_groups_from_list(ucred->uc_ginfo,
277                                                 pud->pud_groups);
278                         lustre_groups_sort(ucred->uc_ginfo);
279                 } else {
280                         ucred->uc_ginfo = NULL;
281                 }
282         } else {
283                 ucred->uc_suppgids[0] = -1;
284                 ucred->uc_suppgids[1] = -1;
285                 ucred->uc_ginfo = NULL;
286         }
287
288         ucred->uc_uid   = pud->pud_uid;
289         ucred->uc_gid   = pud->pud_gid;
290         ucred->uc_fsuid = pud->pud_fsuid;
291         ucred->uc_fsgid = pud->pud_fsgid;
292
293         /* process root_squash here. */
294         mdt_root_squash(info, peernid);
295
296         /* remove fs privilege for non-root user. */
297         if (ucred->uc_fsuid && drop_fs_cap)
298                 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
299         else
300                 ucred->uc_cap = pud->pud_cap;
301         if (remote && !(perm & CFS_RMTOWN_PERM))
302                 ucred->uc_cap &= ~(CFS_CAP_SYS_RESOURCE_MASK |
303                                    CFS_CAP_CHOWN_MASK);
304         ucred->uc_valid = UCRED_NEW;
305         ucred_set_jobid(info, ucred);
306
307         EXIT;
308
309 out:
310         if (rc) {
311                 if (ucred->uc_ginfo) {
312                         put_group_info(ucred->uc_ginfo);
313                         ucred->uc_ginfo = NULL;
314                 }
315                 if (ucred->uc_identity) {
316                         mdt_identity_put(mdt->mdt_identity_cache,
317                                          ucred->uc_identity);
318                         ucred->uc_identity = NULL;
319                 }
320         }
321
322         return rc;
323 }
324
325 /**
326  * Check whether allow the client to set supplementary group IDs or not.
327  *
328  * \param[in] info      pointer to the thread context
329  * \param[in] uc        pointer to the RPC user descriptor
330  *
331  * \retval              true if allow to set supplementary group IDs
332  * \retval              false for other cases
333  */
334 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
335 {
336         __u32 remote = exp_connect_rmtclient(info->mti_exp);
337         __u32 perm;
338
339         /* 1. If identity_upcall is disabled, then forbid remote client to set
340          *    supplementary group IDs, but permit local client to do that. */
341         if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache)) {
342                 if (remote)
343                         return false;
344
345                 return true;
346         }
347
348         /* 2. If fail to get related identities, then forbid any client to
349          *    set supplementary group IDs. */
350         if (uc->uc_identity == NULL)
351                 return false;
352
353         /* 3. Check the permission in the identities. */
354         perm = mdt_identity_get_perm(uc->uc_identity, remote,
355                                      mdt_info_req(info)->rq_peer.nid);
356         if (perm & CFS_SETGRP_PERM)
357                 return true;
358
359         return false;
360 }
361
362 int mdt_check_ucred(struct mdt_thread_info *info)
363 {
364         struct ptlrpc_request   *req = mdt_info_req(info);
365         struct mdt_device       *mdt = info->mti_mdt;
366         struct ptlrpc_user_desc *pud = req->rq_user_desc;
367         struct lu_ucred         *ucred = mdt_ucred(info);
368         struct md_identity      *identity = NULL;
369         lnet_nid_t               peernid = req->rq_peer.nid;
370         __u32                    perm = 0;
371         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
372         int                      setuid;
373         int                      setgid;
374         int                      rc = 0;
375
376         ENTRY;
377
378         LASSERT(ucred != NULL);
379         if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
380                 RETURN(0);
381
382         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
383                 RETURN(0);
384
385         /* sanity check: if we use strong authentication, we expect the
386          * uid which client claimed is true */
387         if (remote) {
388                 if (!uid_valid(make_kuid(&init_user_ns, req->rq_auth_mapped_uid))) {
389                         CDEBUG(D_SEC, "remote user not mapped, deny access!\n");
390                         RETURN(-EACCES);
391                 }
392
393                 if (ptlrpc_user_desc_do_idmap(req, pud))
394                         RETURN(-EACCES);
395
396                 if (req->rq_auth_mapped_uid != pud->pud_uid) {
397                         CDEBUG(D_SEC, "remote client %s: auth/mapped uid %u/%u "
398                                "while client claims %u:%u/%u:%u\n",
399                                libcfs_nid2str(peernid), req->rq_auth_uid,
400                                req->rq_auth_mapped_uid,
401                                pud->pud_uid, pud->pud_gid,
402                                pud->pud_fsuid, pud->pud_fsgid);
403                         RETURN(-EACCES);
404                 }
405         } else {
406                 if (req->rq_auth_uid != pud->pud_uid) {
407                         CDEBUG(D_SEC, "local client %s: auth uid %u "
408                                "while client claims %u:%u/%u:%u\n",
409                                libcfs_nid2str(peernid), req->rq_auth_uid,
410                                pud->pud_uid, pud->pud_gid,
411                                pud->pud_fsuid, pud->pud_fsgid);
412                         RETURN(-EACCES);
413                 }
414         }
415
416         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
417                 if (remote) {
418                         CDEBUG(D_SEC, "remote client must run with identity_get "
419                                "enabled!\n");
420                         RETURN(-EACCES);
421                 }
422                 RETURN(0);
423         }
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                              !remote)) {
429                         RETURN(0);
430                 } else {
431                         CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
432                                pud->pud_uid);
433                         RETURN(-EACCES);
434                }
435         }
436
437         perm = mdt_identity_get_perm(identity, remote, peernid);
438         /* find out the setuid/setgid attempt */
439         setuid = (pud->pud_uid != pud->pud_fsuid);
440         setgid = (pud->pud_gid != pud->pud_fsgid ||
441                   pud->pud_gid != identity->mi_gid);
442
443         /* check permission of setuid */
444         if (setuid && !(perm & CFS_SETUID_PERM)) {
445                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
446                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
447                 GOTO(out, rc = -EACCES);
448         }
449
450         /* check permission of setgid */
451         if (setgid && !(perm & CFS_SETGID_PERM)) {
452                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
453                        "from %s\n", pud->pud_uid, pud->pud_gid,
454                        pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
455                        libcfs_nid2str(peernid));
456                 GOTO(out, rc = -EACCES);
457         }
458
459         EXIT;
460
461 out:
462         mdt_identity_put(mdt->mdt_identity_cache, identity);
463         return rc;
464 }
465
466 static void mdt_squash_nodemap_id(struct lu_ucred *ucred,
467                                   struct lu_nodemap *nodemap)
468 {
469         if (ucred->uc_o_uid == nodemap->nm_squash_uid) {
470                 ucred->uc_fsuid = nodemap->nm_squash_uid;
471                 ucred->uc_fsgid = nodemap->nm_squash_gid;
472                 ucred->uc_cap = 0;
473                 ucred->uc_suppgids[0] = -1;
474                 ucred->uc_suppgids[1] = -1;
475         }
476 }
477
478
479 static int old_init_ucred(struct mdt_thread_info *info,
480                           struct mdt_body *body, bool drop_fs_cap)
481 {
482         struct lu_ucred         *uc = mdt_ucred(info);
483         struct mdt_device       *mdt = info->mti_mdt;
484         struct md_identity      *identity = NULL;
485         struct lu_nodemap       *nodemap =
486                 info->mti_exp->exp_target_data.ted_nodemap;
487         ENTRY;
488
489         body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
490                                        NODEMAP_CLIENT_TO_FS, body->mbo_uid);
491         body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
492                                        NODEMAP_CLIENT_TO_FS, body->mbo_gid);
493         body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
494                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
495         body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
496                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
497
498         LASSERT(uc != NULL);
499         uc->uc_valid = UCRED_INVALID;
500         uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
501         uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
502         uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
503         uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
504         uc->uc_suppgids[0] = body->mbo_suppgid;
505         uc->uc_suppgids[1] = -1;
506         uc->uc_ginfo = NULL;
507         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
508                 identity = mdt_identity_get(mdt->mdt_identity_cache,
509                                             uc->uc_fsuid);
510                 if (IS_ERR(identity)) {
511                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
512                                 identity = NULL;
513                         } else {
514                                 CDEBUG(D_SEC, "Deny access without identity: "
515                                        "uid %u\n", uc->uc_fsuid);
516                                 RETURN(-EACCES);
517                         }
518                 }
519         }
520         uc->uc_identity = identity;
521
522         mdt_squash_nodemap_id(uc, nodemap);
523
524         /* process root_squash here. */
525         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
526
527         /* remove fs privilege for non-root user. */
528         if (uc->uc_fsuid && drop_fs_cap)
529                 uc->uc_cap = body->mbo_capability & ~CFS_CAP_FS_MASK;
530         else
531                 uc->uc_cap = body->mbo_capability;
532         uc->uc_valid = UCRED_OLD;
533         ucred_set_jobid(info, uc);
534
535         RETURN(0);
536 }
537
538 static int old_init_ucred_reint(struct mdt_thread_info *info)
539 {
540         struct lu_ucred         *uc = mdt_ucred(info);
541         struct mdt_device       *mdt = info->mti_mdt;
542         struct md_identity      *identity = NULL;
543         struct lu_nodemap       *nodemap =
544                 info->mti_exp->exp_target_data.ted_nodemap;
545         ENTRY;
546
547         LASSERT(uc != NULL);
548
549         uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
550                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
551         uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
552                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
553
554         uc->uc_valid = UCRED_INVALID;
555         uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
556         uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
557         uc->uc_ginfo = NULL;
558
559         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
560                 identity = mdt_identity_get(mdt->mdt_identity_cache,
561                                             uc->uc_fsuid);
562                 if (IS_ERR(identity)) {
563                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
564                                 identity = NULL;
565                         } else {
566                                 CDEBUG(D_SEC, "Deny access without identity: "
567                                        "uid %u\n", uc->uc_fsuid);
568                                 RETURN(-EACCES);
569                         }
570                 }
571         }
572         uc->uc_identity = identity;
573
574         /* process root_squash here. */
575         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
576
577         /* remove fs privilege for non-root user. */
578         if (uc->uc_fsuid)
579                 uc->uc_cap &= ~CFS_CAP_FS_MASK;
580         uc->uc_valid = UCRED_OLD;
581         ucred_set_jobid(info, uc);
582
583         RETURN(0);
584 }
585
586 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
587                                    struct mdt_body *body,
588                                    bool drop_fs_cap)
589 {
590         struct ptlrpc_request   *req = mdt_info_req(info);
591         struct lu_ucred         *uc  = mdt_ucred(info);
592
593         LASSERT(uc != NULL);
594         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
595                 return 0;
596
597         mdt_exit_ucred(info);
598
599         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
600                 return old_init_ucred(info, body, drop_fs_cap);
601         else
602                 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
603 }
604
605 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
606 {
607         return __mdt_init_ucred(info, body, true);
608 }
609
610 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
611  * doesn't set correct fsuid explicitely, but raise capability to allow
612  * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
613  * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
614  * will fail permission check. */
615 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
616                                   struct mdt_body *body)
617 {
618         return __mdt_init_ucred(info, body, false);
619 }
620
621 int mdt_init_ucred_reint(struct mdt_thread_info *info)
622 {
623         struct ptlrpc_request *req = mdt_info_req(info);
624         struct lu_ucred       *uc  = mdt_ucred(info);
625
626         LASSERT(uc != NULL);
627         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
628                 return 0;
629
630         mdt_exit_ucred(info);
631
632         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
633                 return old_init_ucred_reint(info);
634         else
635                 return new_init_ucred(info, REC_INIT, NULL, true);
636 }
637
638 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
639 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
640 {
641         const struct lov_ost_data_v1    *lod;
642         int                              i;
643         __u16                            count;
644
645         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
646                 return;
647
648         count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
649
650         CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
651                POSTID(&lmm->lmm_oi), le32_to_cpu(lmm->lmm_magic),
652                le32_to_cpu(lmm->lmm_pattern));
653         CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
654                le32_to_cpu(lmm->lmm_stripe_size), count);
655
656         /* If it's a directory or a released file, then there are
657          * no actual objects to print, so bail out. */
658         if (valid & OBD_MD_FLDIREA ||
659             le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
660                 return;
661
662         LASSERT(count <= LOV_MAX_STRIPE_COUNT);
663         for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
664                 struct ost_id oi;
665
666                 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
667                 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
668                        i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
669         }
670 }
671
672 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
673 {
674         const struct lmv_mds_md_v1 *lmm1;
675         int                        i;
676
677         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
678                 return;
679
680         lmm1 = &lmv->lmv_md_v1;
681         CDEBUG(level, "magic 0x%08X, master %#X stripe_count %#x\n",
682                le32_to_cpu(lmm1->lmv_magic),
683                le32_to_cpu(lmm1->lmv_master_mdt_index),
684                le32_to_cpu(lmm1->lmv_stripe_count));
685
686         if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
687                 return;
688
689         for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
690                 struct lu_fid fid;
691
692                 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
693                 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
694         }
695 }
696
697 /* Shrink and/or grow reply buffers */
698 int mdt_fix_reply(struct mdt_thread_info *info)
699 {
700         struct req_capsule *pill = info->mti_pill;
701         struct mdt_body    *body;
702         int                md_size, md_packed = 0;
703         int                acl_size;
704         int                rc = 0;
705         ENTRY;
706
707         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
708         LASSERT(body != NULL);
709
710         if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
711                                OBD_MD_LINKNAME))
712                 md_size = body->mbo_eadatasize;
713         else
714                 md_size = 0;
715
716         acl_size = body->mbo_aclsize;
717
718         /* this replay - not send info to client */
719         if (info->mti_spec.no_create) {
720                 md_size = 0;
721                 acl_size = 0;
722         }
723
724         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
725                md_size, acl_size);
726 /*
727             &RMF_MDT_BODY,
728             &RMF_MDT_MD,
729             &RMF_ACL, or &RMF_LOGCOOKIES
730 (optional)  &RMF_CAPA1,
731 (optional)  &RMF_CAPA2,
732 (optional)  something else
733 */
734
735         /* MDT_MD buffer may be bigger than packed value, let's shrink all
736          * buffers before growing it */
737         if (info->mti_big_lmm_used) {
738                 /* big_lmm buffer may be used even without packing the result
739                  * into reply, just for internal server needs */
740                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
741                         md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
742                                                          RCL_SERVER);
743
744                 /* free big lmm if md_size is not needed */
745                 if (md_size == 0 || md_packed == 0) {
746                         info->mti_big_lmm_used = 0;
747                 } else {
748                         /* buffer must be allocated separately */
749                         LASSERT(info->mti_attr.ma_lmm !=
750                                 req_capsule_server_get(pill, &RMF_MDT_MD));
751                         req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
752                 }
753         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
754                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
755         }
756
757         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
758                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
759         else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
760                 req_capsule_shrink(pill, &RMF_LOGCOOKIES,
761                                    acl_size, RCL_SERVER);
762
763         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
764             !(body->mbo_valid & OBD_MD_FLMDSCAPA))
765                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
766
767         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
768             !(body->mbo_valid & OBD_MD_FLOSSCAPA))
769                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
770
771         /*
772          * Some more field should be shrinked if needed.
773          * This should be done by those who added fields to reply message.
774          */
775
776         /* Grow MD buffer if needed finally */
777         if (info->mti_big_lmm_used) {
778                 void *lmm;
779
780                 LASSERT(md_size > md_packed);
781                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
782                        md_size - md_packed);
783                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
784                 if (rc) {
785                         /* we can't answer with proper LOV EA, drop flags,
786                          * the rc is also returned so this request is
787                          * considered as failed */
788                         body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
789                         /* don't return transno along with error */
790                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
791                 } else {
792                         /* now we need to pack right LOV/LMV EA */
793                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
794                         if (info->mti_attr.ma_valid & MA_LOV) {
795                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
796                                                              RCL_SERVER) ==
797                                                 info->mti_attr.ma_lmm_size);
798                                 memcpy(lmm, info->mti_attr.ma_lmm,
799                                        info->mti_attr.ma_lmm_size);
800                         } else if (info->mti_attr.ma_valid & MA_LMV) {
801                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
802                                                              RCL_SERVER) ==
803                                                 info->mti_attr.ma_lmv_size);
804                                 memcpy(lmm, info->mti_attr.ma_lmv,
805                                        info->mti_attr.ma_lmv_size);
806                         }
807                 }
808                 /* update mdt_max_mdsize so clients will be aware about that */
809                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
810                         info->mti_mdt->mdt_max_mdsize =
811                                                     info->mti_attr.ma_lmm_size;
812                 info->mti_big_lmm_used = 0;
813         }
814         RETURN(rc);
815 }
816
817
818 /* if object is dying, pack the lov/llog data,
819  * parameter info->mti_attr should be valid at this point! */
820 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
821                            const struct md_attr *ma)
822 {
823         struct mdt_body       *repbody;
824         const struct lu_attr *la = &ma->ma_attr;
825         ENTRY;
826
827         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
828         LASSERT(repbody != NULL);
829
830         if (ma->ma_valid & MA_INODE)
831                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
832
833         if (ma->ma_valid & MA_LOV) {
834                 CERROR("No need in LOV EA upon unlink\n");
835                 dump_stack();
836         }
837         repbody->mbo_eadatasize = 0;
838
839         RETURN(0);
840 }
841
842 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
843                                   struct md_attr *ma)
844 {
845         __u64 out;
846
847         out = 0;
848         if (in & MDS_ATTR_MODE)
849                 out |= LA_MODE;
850         if (in & MDS_ATTR_UID)
851                 out |= LA_UID;
852         if (in & MDS_ATTR_GID)
853                 out |= LA_GID;
854         if (in & MDS_ATTR_SIZE)
855                 out |= LA_SIZE;
856         if (in & MDS_ATTR_BLOCKS)
857                 out |= LA_BLOCKS;
858         if (in & MDS_ATTR_ATIME_SET)
859                 out |= LA_ATIME;
860         if (in & MDS_ATTR_CTIME_SET)
861                 out |= LA_CTIME;
862         if (in & MDS_ATTR_MTIME_SET)
863                 out |= LA_MTIME;
864         if (in & MDS_ATTR_ATTR_FLAG)
865                 out |= LA_FLAGS;
866         if (in & MDS_ATTR_KILL_SUID)
867                 out |= LA_KILL_SUID;
868         if (in & MDS_ATTR_KILL_SGID)
869                 out |= LA_KILL_SGID;
870
871         if (in & MDS_ATTR_FROM_OPEN)
872                 rr->rr_flags |= MRF_OPEN_TRUNC;
873         if (in & MDS_OPEN_OWNEROVERRIDE)
874                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
875         if (in & MDS_ATTR_FORCE)
876                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
877
878         in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID |
879                 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
880                 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
881                 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
882                 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
883                 MDS_ATTR_FROM_OPEN | MDS_OPEN_OWNEROVERRIDE);
884         if (in != 0)
885                 CERROR("Unknown attr bits: "LPX64"\n", in);
886         return out;
887 }
888
889 /* unpacking */
890
891 int mdt_name_unpack(struct req_capsule *pill,
892                     const struct req_msg_field *field,
893                     struct lu_name *ln,
894                     enum mdt_name_flags flags)
895 {
896         ln->ln_name = req_capsule_client_get(pill, field);
897         ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
898
899         if (!lu_name_is_valid(ln)) {
900                 ln->ln_name = NULL;
901                 ln->ln_namelen = 0;
902
903                 return -EPROTO;
904         }
905
906         if ((flags & MNF_FIX_ANON) &&
907             ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
908                 /* Newer (3.x) kernels use a name of "/" for the
909                  * "anonymous" disconnected dentries from NFS
910                  * filehandle conversion. See d_obtain_alias(). */
911                 ln->ln_name = NULL;
912                 ln->ln_namelen = 0;
913         }
914
915         return 0;
916 }
917
918 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
919 {
920         struct lu_ucred         *uc = mdt_ucred(info);
921         struct md_attr          *ma = &info->mti_attr;
922         struct lu_attr          *la = &ma->ma_attr;
923         struct req_capsule      *pill = info->mti_pill;
924         struct mdt_reint_record *rr = &info->mti_rr;
925         struct mdt_rec_setattr  *rec;
926         struct lu_nodemap       *nodemap =
927                 info->mti_exp->exp_target_data.ted_nodemap;
928         ENTRY;
929
930         CLASSERT(sizeof(struct mdt_rec_setattr)== sizeof(struct mdt_rec_reint));
931         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
932         if (rec == NULL)
933                 RETURN(-EFAULT);
934
935         /* This prior initialization is needed for old_init_ucred_reint() */
936         uc->uc_fsuid = rec->sa_fsuid;
937         uc->uc_fsgid = rec->sa_fsgid;
938         uc->uc_cap   = rec->sa_cap;
939         uc->uc_suppgids[0] = rec->sa_suppgid;
940         uc->uc_suppgids[1] = -1;
941
942         rr->rr_fid1 = &rec->sa_fid;
943         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
944         la->la_mode  = rec->sa_mode;
945         la->la_flags = rec->sa_attr_flags;
946         la->la_uid   = nodemap_map_id(nodemap, NODEMAP_UID,
947                                       NODEMAP_CLIENT_TO_FS, rec->sa_uid);
948         la->la_gid   = nodemap_map_id(nodemap, NODEMAP_GID,
949                                       NODEMAP_CLIENT_TO_FS, rec->sa_gid);
950         la->la_size  = rec->sa_size;
951         la->la_blocks = rec->sa_blocks;
952         la->la_ctime = rec->sa_ctime;
953         la->la_atime = rec->sa_atime;
954         la->la_mtime = rec->sa_mtime;
955         ma->ma_valid = MA_INODE;
956
957         if (rec->sa_bias & MDS_DATA_MODIFIED)
958                 ma->ma_attr_flags |= MDS_DATA_MODIFIED;
959         else
960                 ma->ma_attr_flags &= ~MDS_DATA_MODIFIED;
961
962         if (rec->sa_bias & MDS_HSM_RELEASE)
963                 ma->ma_attr_flags |= MDS_HSM_RELEASE;
964         else
965                 ma->ma_attr_flags &= ~MDS_HSM_RELEASE;
966
967         if (rec->sa_bias & MDS_CLOSE_LAYOUT_SWAP)
968                 ma->ma_attr_flags |= MDS_CLOSE_LAYOUT_SWAP;
969         else
970                 ma->ma_attr_flags &= ~MDS_CLOSE_LAYOUT_SWAP;
971
972         RETURN(0);
973 }
974
975 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
976 {
977         struct req_capsule *pill = info->mti_pill;
978         struct mdt_ioepoch *ioepoch;
979         ENTRY;
980
981         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
982                 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
983         else
984                 ioepoch = NULL;
985
986         if (ioepoch == NULL)
987                 RETURN(-EPROTO);
988
989         info->mti_close_handle = ioepoch->mio_handle;
990
991         RETURN(0);
992 }
993
994 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
995         struct req_capsule      *pill = info->mti_pill;
996
997         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
998                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
999                 if (info->mti_dlm_req == NULL)
1000                         RETURN(-EFAULT);
1001         }
1002
1003         RETURN(0);
1004 }
1005
1006 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1007 {
1008         struct mdt_reint_record *rr = &info->mti_rr;
1009         struct md_attr          *ma = &info->mti_attr;
1010         struct req_capsule      *pill = info->mti_pill;
1011         int rc;
1012         ENTRY;
1013
1014         rc = mdt_setattr_unpack_rec(info);
1015         if (rc)
1016                 RETURN(rc);
1017
1018         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1019                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1020                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1021                                                         RCL_CLIENT);
1022                 if (rr->rr_eadatalen > 0) {
1023                         const struct lmv_user_md        *lum;
1024
1025                         lum = rr->rr_eadata;
1026                         /* Sigh ma_valid(from req) does not indicate whether
1027                          * it will set LOV/LMV EA, so we have to check magic */
1028                         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1029                                 ma->ma_valid |= MA_LMV;
1030                                 ma->ma_lmv = (void *)rr->rr_eadata;
1031                                 ma->ma_lmv_size = rr->rr_eadatalen;
1032                         } else {
1033                                 ma->ma_valid |= MA_LOV;
1034                                 ma->ma_lmm = (void *)rr->rr_eadata;
1035                                 ma->ma_lmm_size = rr->rr_eadatalen;
1036                         }
1037                 }
1038         }
1039
1040         rc = mdt_dlmreq_unpack(info);
1041         RETURN(rc);
1042 }
1043
1044 static int mdt_intent_close_unpack(struct mdt_thread_info *info)
1045 {
1046         struct md_attr          *ma = &info->mti_attr;
1047         struct req_capsule      *pill = info->mti_pill;
1048         ENTRY;
1049
1050         if (!(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
1051                 RETURN(0);
1052
1053         req_capsule_extend(pill, &RQF_MDS_INTENT_CLOSE);
1054
1055         if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1056             req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1057                 RETURN(-EFAULT);
1058
1059         RETURN(0);
1060 }
1061
1062 int mdt_close_unpack(struct mdt_thread_info *info)
1063 {
1064         int rc;
1065         ENTRY;
1066
1067         rc = mdt_close_handle_unpack(info);
1068         if (rc)
1069                 RETURN(rc);
1070
1071         rc = mdt_setattr_unpack_rec(info);
1072         if (rc)
1073                 RETURN(rc);
1074
1075         rc = mdt_intent_close_unpack(info);
1076         if (rc)
1077                 RETURN(rc);
1078
1079         RETURN(mdt_init_ucred_reint(info));
1080 }
1081
1082 static int mdt_create_unpack(struct mdt_thread_info *info)
1083 {
1084         struct lu_ucred         *uc  = mdt_ucred(info);
1085         struct mdt_rec_create   *rec;
1086         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1087         struct mdt_reint_record *rr = &info->mti_rr;
1088         struct req_capsule      *pill = info->mti_pill;
1089         struct md_op_spec       *sp = &info->mti_spec;
1090         int rc;
1091         ENTRY;
1092
1093         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1094         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1095         if (rec == NULL)
1096                 RETURN(-EFAULT);
1097
1098         /* This prior initialization is needed for old_init_ucred_reint() */
1099         uc->uc_fsuid = rec->cr_fsuid;
1100         uc->uc_fsgid = rec->cr_fsgid;
1101         uc->uc_cap   = rec->cr_cap;
1102         uc->uc_suppgids[0] = rec->cr_suppgid1;
1103         uc->uc_suppgids[1] = -1;
1104         uc->uc_umask = rec->cr_umask;
1105
1106         rr->rr_fid1 = &rec->cr_fid1;
1107         rr->rr_fid2 = &rec->cr_fid2;
1108         attr->la_mode = rec->cr_mode;
1109         attr->la_rdev  = rec->cr_rdev;
1110         attr->la_uid   = rec->cr_fsuid;
1111         attr->la_gid   = rec->cr_fsgid;
1112         attr->la_ctime = rec->cr_time;
1113         attr->la_mtime = rec->cr_time;
1114         attr->la_atime = rec->cr_time;
1115         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1116                          LA_CTIME | LA_MTIME | LA_ATIME;
1117         memset(&sp->u, 0, sizeof(sp->u));
1118         sp->sp_cr_flags = get_mrc_cr_flags(rec);
1119
1120         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1121         if (rc < 0)
1122                 RETURN(rc);
1123
1124         if (S_ISLNK(attr->la_mode)) {
1125                 const char *tgt = NULL;
1126
1127                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1128                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1129                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1130                         sp->u.sp_symname = tgt;
1131                 }
1132                 if (tgt == NULL)
1133                         RETURN(-EFAULT);
1134         } else {
1135                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
1136                 if (S_ISDIR(attr->la_mode) &&
1137                     req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1138                         sp->u.sp_ea.eadata =
1139                                 req_capsule_client_get(pill, &RMF_EADATA);
1140                         sp->u.sp_ea.eadatalen =
1141                                 req_capsule_get_size(pill, &RMF_EADATA,
1142                                                      RCL_CLIENT);
1143                         sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1144                 }
1145         }
1146
1147         rc = mdt_dlmreq_unpack(info);
1148         RETURN(rc);
1149 }
1150
1151 static int mdt_link_unpack(struct mdt_thread_info *info)
1152 {
1153         struct lu_ucred         *uc  = mdt_ucred(info);
1154         struct mdt_rec_link     *rec;
1155         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1156         struct mdt_reint_record *rr = &info->mti_rr;
1157         struct req_capsule      *pill = info->mti_pill;
1158         int rc;
1159         ENTRY;
1160
1161         CLASSERT(sizeof(struct mdt_rec_link) == sizeof(struct mdt_rec_reint));
1162         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1163         if (rec == NULL)
1164                 RETURN(-EFAULT);
1165
1166         /* This prior initialization is needed for old_init_ucred_reint() */
1167         uc->uc_fsuid = rec->lk_fsuid;
1168         uc->uc_fsgid = rec->lk_fsgid;
1169         uc->uc_cap   = rec->lk_cap;
1170         uc->uc_suppgids[0] = rec->lk_suppgid1;
1171         uc->uc_suppgids[1] = rec->lk_suppgid2;
1172
1173         attr->la_uid = rec->lk_fsuid;
1174         attr->la_gid = rec->lk_fsgid;
1175         rr->rr_fid1 = &rec->lk_fid1;
1176         rr->rr_fid2 = &rec->lk_fid2;
1177         attr->la_ctime = rec->lk_time;
1178         attr->la_mtime = rec->lk_time;
1179         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1180
1181         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1182         if (rc < 0)
1183                 RETURN(rc);
1184
1185         rc = mdt_dlmreq_unpack(info);
1186
1187         RETURN(rc);
1188 }
1189
1190 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1191 {
1192         struct lu_ucred         *uc  = mdt_ucred(info);
1193         struct mdt_rec_unlink   *rec;
1194         struct md_attr          *ma = &info->mti_attr;
1195         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1196         struct mdt_reint_record *rr = &info->mti_rr;
1197         struct req_capsule      *pill = info->mti_pill;
1198         int rc;
1199         ENTRY;
1200
1201         CLASSERT(sizeof(struct mdt_rec_unlink) == sizeof(struct mdt_rec_reint));
1202         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1203         if (rec == NULL)
1204                 RETURN(-EFAULT);
1205
1206         /* This prior initialization is needed for old_init_ucred_reint() */
1207         uc->uc_fsuid = rec->ul_fsuid;
1208         uc->uc_fsgid = rec->ul_fsgid;
1209         uc->uc_cap   = rec->ul_cap;
1210         uc->uc_suppgids[0] = rec->ul_suppgid1;
1211         uc->uc_suppgids[1] = -1;
1212
1213         attr->la_uid = rec->ul_fsuid;
1214         attr->la_gid = rec->ul_fsgid;
1215         rr->rr_fid1 = &rec->ul_fid1;
1216         rr->rr_fid2 = &rec->ul_fid2;
1217         attr->la_ctime = rec->ul_time;
1218         attr->la_mtime = rec->ul_time;
1219         attr->la_mode  = rec->ul_mode;
1220         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1221
1222         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1223         if (rc < 0)
1224                 RETURN(rc);
1225
1226         if (rec->ul_bias & MDS_VTX_BYPASS)
1227                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1228         else
1229                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1230
1231         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1232
1233         rc = mdt_dlmreq_unpack(info);
1234         RETURN(rc);
1235 }
1236
1237 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1238 {
1239         info->mti_spec.sp_rm_entry = 1;
1240         return mdt_unlink_unpack(info);
1241 }
1242
1243 static int mdt_rename_unpack(struct mdt_thread_info *info)
1244 {
1245         struct lu_ucred         *uc = mdt_ucred(info);
1246         struct mdt_rec_rename   *rec;
1247         struct md_attr          *ma = &info->mti_attr;
1248         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1249         struct mdt_reint_record *rr = &info->mti_rr;
1250         struct req_capsule      *pill = info->mti_pill;
1251         int rc;
1252         ENTRY;
1253
1254         CLASSERT(sizeof(struct mdt_rec_rename) == sizeof(struct mdt_rec_reint));
1255         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1256         if (rec == NULL)
1257                 RETURN(-EFAULT);
1258
1259         /* This prior initialization is needed for old_init_ucred_reint() */
1260         uc->uc_fsuid = rec->rn_fsuid;
1261         uc->uc_fsgid = rec->rn_fsgid;
1262         uc->uc_cap   = rec->rn_cap;
1263         uc->uc_suppgids[0] = rec->rn_suppgid1;
1264         uc->uc_suppgids[1] = rec->rn_suppgid2;
1265
1266         attr->la_uid = rec->rn_fsuid;
1267         attr->la_gid = rec->rn_fsgid;
1268         rr->rr_fid1 = &rec->rn_fid1;
1269         rr->rr_fid2 = &rec->rn_fid2;
1270         attr->la_ctime = rec->rn_time;
1271         attr->la_mtime = rec->rn_time;
1272         /* rename_tgt contains the mode already */
1273         attr->la_mode = rec->rn_mode;
1274         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1275
1276         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1277         if (rc < 0)
1278                 RETURN(rc);
1279
1280         rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1281         if (rc < 0)
1282                 RETURN(rc);
1283
1284         if (rec->rn_bias & MDS_VTX_BYPASS)
1285                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1286         else
1287                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1288
1289         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1290
1291
1292         rc = mdt_dlmreq_unpack(info);
1293
1294         RETURN(rc);
1295 }
1296
1297 /*
1298  * please see comment above LOV_MAGIC_V1_DEF
1299  */
1300 static void mdt_fix_lov_magic(struct mdt_thread_info *info)
1301 {
1302         struct mdt_reint_record *rr = &info->mti_rr;
1303         struct lov_user_md_v1   *v1;
1304
1305         v1 = (void *)rr->rr_eadata;
1306         LASSERT(v1);
1307
1308         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1309                 if (v1->lmm_magic == LOV_USER_MAGIC_V1) {
1310                         v1->lmm_magic = LOV_MAGIC_V1_DEF;
1311                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
1312                         v1->lmm_magic = __swab32(LOV_MAGIC_V1_DEF);
1313                 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
1314                         v1->lmm_magic = LOV_MAGIC_V3_DEF;
1315                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
1316                         v1->lmm_magic = __swab32(LOV_MAGIC_V3_DEF);
1317                 }
1318         }
1319 }
1320
1321 static int mdt_open_unpack(struct mdt_thread_info *info)
1322 {
1323         struct lu_ucred         *uc = mdt_ucred(info);
1324         struct mdt_rec_create   *rec;
1325         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1326         struct req_capsule      *pill = info->mti_pill;
1327         struct mdt_reint_record *rr   = &info->mti_rr;
1328         struct ptlrpc_request   *req  = mdt_info_req(info);
1329         struct md_op_spec       *sp   = &info->mti_spec;
1330         ENTRY;
1331
1332         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1333         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1334         if (rec == NULL)
1335                 RETURN(-EFAULT);
1336
1337         /* This prior initialization is needed for old_init_ucred_reint() */
1338         uc->uc_fsuid = rec->cr_fsuid;
1339         uc->uc_fsgid = rec->cr_fsgid;
1340         uc->uc_cap   = rec->cr_cap;
1341         uc->uc_suppgids[0] = rec->cr_suppgid1;
1342         uc->uc_suppgids[1] = rec->cr_suppgid2;
1343         uc->uc_umask = rec->cr_umask;
1344
1345         rr->rr_fid1   = &rec->cr_fid1;
1346         rr->rr_fid2   = &rec->cr_fid2;
1347         rr->rr_handle = &rec->cr_old_handle;
1348         attr->la_mode = rec->cr_mode;
1349         attr->la_rdev  = rec->cr_rdev;
1350         attr->la_uid   = rec->cr_fsuid;
1351         attr->la_gid   = rec->cr_fsgid;
1352         attr->la_ctime = rec->cr_time;
1353         attr->la_mtime = rec->cr_time;
1354         attr->la_atime = rec->cr_time;
1355         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1356                          LA_CTIME | LA_MTIME | LA_ATIME;
1357         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1358         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1359         /* Do not trigger ASSERTION if client miss to set such flags. */
1360         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1361                 RETURN(-EPROTO);
1362
1363         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1364
1365         mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1366
1367         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1368                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1369                                                         RCL_CLIENT);
1370                 if (rr->rr_eadatalen > 0) {
1371                         rr->rr_eadata = req_capsule_client_get(pill,
1372                                                                &RMF_EADATA);
1373                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1374                         sp->u.sp_ea.eadata = rr->rr_eadata;
1375                         sp->no_create = !!req_is_replay(req);
1376                         mdt_fix_lov_magic(info);
1377                 }
1378
1379                 /*
1380                  * Client default md_size may be 0 right after client start,
1381                  * until all osc are connected, set here just some reasonable
1382                  * value to prevent misbehavior.
1383                  */
1384                 if (rr->rr_eadatalen == 0 &&
1385                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1386                         rr->rr_eadatalen = MIN_MD_SIZE;
1387         }
1388
1389         RETURN(0);
1390 }
1391
1392 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1393 {
1394         struct mdt_reint_record *rr     = &info->mti_rr;
1395         struct lu_ucred         *uc     = mdt_ucred(info);
1396         struct lu_attr          *attr   = &info->mti_attr.ma_attr;
1397         struct req_capsule      *pill   = info->mti_pill;
1398         struct mdt_rec_setxattr *rec;
1399         int                      rc;
1400         ENTRY;
1401
1402
1403         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1404                          sizeof(struct mdt_rec_reint));
1405
1406         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1407         if (rec == NULL)
1408                 RETURN(-EFAULT);
1409
1410         /* This prior initialization is needed for old_init_ucred_reint() */
1411         uc->uc_fsuid  = rec->sx_fsuid;
1412         uc->uc_fsgid  = rec->sx_fsgid;
1413         uc->uc_cap    = rec->sx_cap;
1414         uc->uc_suppgids[0] = rec->sx_suppgid1;
1415         uc->uc_suppgids[1] = -1;
1416
1417         rr->rr_opcode = rec->sx_opcode;
1418         rr->rr_fid1   = &rec->sx_fid;
1419         attr->la_valid = rec->sx_valid;
1420         attr->la_ctime = rec->sx_time;
1421         attr->la_size = rec->sx_size;
1422         attr->la_flags = rec->sx_flags;
1423
1424         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1425         if (rc < 0)
1426                 RETURN(rc);
1427
1428         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1429                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1430                                                         RCL_CLIENT);
1431                 if (rr->rr_eadatalen > 0) {
1432                         rr->rr_eadata = req_capsule_client_get(pill,
1433                                                                &RMF_EADATA);
1434                         if (rr->rr_eadata == NULL)
1435                                 RETURN(-EFAULT);
1436                 } else {
1437                         rr->rr_eadata = NULL;
1438                 }
1439         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1440                 CDEBUG(D_INFO, "no xattr data supplied\n");
1441                 RETURN(-EFAULT);
1442         }
1443
1444         if (mdt_dlmreq_unpack(info) < 0)
1445                 RETURN(-EPROTO);
1446
1447         RETURN(0);
1448 }
1449
1450
1451 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1452
1453 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1454         [REINT_SETATTR]  = mdt_setattr_unpack,
1455         [REINT_CREATE]   = mdt_create_unpack,
1456         [REINT_LINK]     = mdt_link_unpack,
1457         [REINT_UNLINK]   = mdt_unlink_unpack,
1458         [REINT_RENAME]   = mdt_rename_unpack,
1459         [REINT_OPEN]     = mdt_open_unpack,
1460         [REINT_SETXATTR] = mdt_setxattr_unpack,
1461         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1462         [REINT_MIGRATE]  = mdt_rename_unpack,
1463 };
1464
1465 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1466 {
1467         int rc;
1468         ENTRY;
1469
1470         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1471         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1472                 info->mti_rr.rr_opcode = op;
1473                 rc = mdt_reint_unpackers[op](info);
1474         } else {
1475                 CERROR("Unexpected opcode %d\n", op);
1476                 rc = -EFAULT;
1477         }
1478         RETURN(rc);
1479 }