Whamcloud - gitweb
LU-7530 mdt: Do not leak identity when no nodemap is present
[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, 2015, 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 int old_init_ucred_common(struct mdt_thread_info *info,
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         struct lu_nodemap       *nodemap =
473                 info->mti_exp->exp_target_data.ted_nodemap;
474
475         if (nodemap == NULL) {
476                 CDEBUG(D_SEC, "%s: cli %s/%p nodemap not set.\n",
477                        mdt2obd_dev(mdt)->obd_name,
478                        info->mti_exp->exp_client_uuid.uuid, info->mti_exp);
479                 RETURN(-EACCES);
480         }
481
482         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
483                 identity = mdt_identity_get(mdt->mdt_identity_cache,
484                                             uc->uc_fsuid);
485                 if (IS_ERR(identity)) {
486                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
487                                 identity = NULL;
488                         } else {
489                                 CDEBUG(D_SEC, "Deny access without identity: "
490                                        "uid %u\n", uc->uc_fsuid);
491                                 RETURN(-EACCES);
492                         }
493                 }
494         }
495         uc->uc_identity = identity;
496
497         if (uc->uc_o_uid == nodemap->nm_squash_uid) {
498                 uc->uc_fsuid = nodemap->nm_squash_uid;
499                 uc->uc_fsgid = nodemap->nm_squash_gid;
500                 uc->uc_cap = 0;
501                 uc->uc_suppgids[0] = -1;
502                 uc->uc_suppgids[1] = -1;
503         }
504
505         /* process root_squash here. */
506         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
507
508         /* remove fs privilege for non-root user. */
509         if (uc->uc_fsuid)
510                 uc->uc_cap &= ~CFS_CAP_FS_MASK;
511         uc->uc_valid = UCRED_OLD;
512         ucred_set_jobid(info, uc);
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                 info->mti_exp->exp_target_data.ted_nodemap;
523         int                      rc;
524         ENTRY;
525
526         body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
527                                        NODEMAP_CLIENT_TO_FS, body->mbo_uid);
528         body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
529                                        NODEMAP_CLIENT_TO_FS, body->mbo_gid);
530         body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
531                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
532         body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
533                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
534
535         LASSERT(uc != NULL);
536         uc->uc_valid = UCRED_INVALID;
537         uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
538         uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
539         uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
540         uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
541         uc->uc_suppgids[0] = body->mbo_suppgid;
542         uc->uc_suppgids[1] = -1;
543         uc->uc_ginfo = NULL;
544         uc->uc_cap = body->mbo_capability;
545
546         rc = old_init_ucred_common(info, drop_fs_cap);
547
548         RETURN(rc);
549 }
550
551 static int old_init_ucred_reint(struct mdt_thread_info *info)
552 {
553         struct lu_ucred         *uc = mdt_ucred(info);
554         struct lu_nodemap       *nodemap =
555                 info->mti_exp->exp_target_data.ted_nodemap;
556         int                      rc;
557         ENTRY;
558
559         LASSERT(uc != NULL);
560
561         uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
562                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
563         uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
564                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
565
566         uc->uc_valid = UCRED_INVALID;
567         uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
568         uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
569         uc->uc_ginfo = NULL;
570
571         rc = old_init_ucred_common(info, true); /* drop_fs_cap = true */
572
573         RETURN(rc);
574 }
575
576 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
577                                    struct mdt_body *body,
578                                    bool drop_fs_cap)
579 {
580         struct ptlrpc_request   *req = mdt_info_req(info);
581         struct lu_ucred         *uc  = mdt_ucred(info);
582
583         LASSERT(uc != NULL);
584         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
585                 return 0;
586
587         mdt_exit_ucred(info);
588
589         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
590                 return old_init_ucred(info, body, drop_fs_cap);
591         else
592                 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
593 }
594
595 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
596 {
597         return __mdt_init_ucred(info, body, true);
598 }
599
600 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
601  * doesn't set correct fsuid explicitely, but raise capability to allow
602  * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
603  * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
604  * will fail permission check. */
605 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
606                                   struct mdt_body *body)
607 {
608         return __mdt_init_ucred(info, body, false);
609 }
610
611 int mdt_init_ucred_reint(struct mdt_thread_info *info)
612 {
613         struct ptlrpc_request *req = mdt_info_req(info);
614         struct lu_ucred       *uc  = mdt_ucred(info);
615
616         LASSERT(uc != NULL);
617         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
618                 return 0;
619
620         mdt_exit_ucred(info);
621
622         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
623                 return old_init_ucred_reint(info);
624         else
625                 return new_init_ucred(info, REC_INIT, NULL, true);
626 }
627
628 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
629 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
630 {
631         const struct lov_ost_data_v1    *lod;
632         int                              i;
633         __u16                            count;
634
635         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
636                 return;
637
638         count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
639
640         CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
641                POSTID(&lmm->lmm_oi), le32_to_cpu(lmm->lmm_magic),
642                le32_to_cpu(lmm->lmm_pattern));
643         CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
644                le32_to_cpu(lmm->lmm_stripe_size), count);
645
646         /* If it's a directory or a released file, then there are
647          * no actual objects to print, so bail out. */
648         if (valid & OBD_MD_FLDIREA ||
649             le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
650                 return;
651
652         LASSERT(count <= LOV_MAX_STRIPE_COUNT);
653         for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
654                 struct ost_id oi;
655
656                 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
657                 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
658                        i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
659         }
660 }
661
662 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
663 {
664         const struct lmv_mds_md_v1 *lmm1;
665         int                        i;
666
667         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
668                 return;
669
670         lmm1 = &lmv->lmv_md_v1;
671         CDEBUG(level, "magic 0x%08X, master %#X stripe_count %#x\n",
672                le32_to_cpu(lmm1->lmv_magic),
673                le32_to_cpu(lmm1->lmv_master_mdt_index),
674                le32_to_cpu(lmm1->lmv_stripe_count));
675
676         if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
677                 return;
678
679         for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
680                 struct lu_fid fid;
681
682                 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
683                 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
684         }
685 }
686
687 /* Shrink and/or grow reply buffers */
688 int mdt_fix_reply(struct mdt_thread_info *info)
689 {
690         struct req_capsule *pill = info->mti_pill;
691         struct mdt_body    *body;
692         int                md_size, md_packed = 0;
693         int                acl_size;
694         int                rc = 0;
695         ENTRY;
696
697         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
698         LASSERT(body != NULL);
699
700         if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
701                                OBD_MD_LINKNAME))
702                 md_size = body->mbo_eadatasize;
703         else
704                 md_size = 0;
705
706         acl_size = body->mbo_aclsize;
707
708         /* this replay - not send info to client */
709         if (info->mti_spec.no_create) {
710                 md_size = 0;
711                 acl_size = 0;
712         }
713
714         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
715                md_size, acl_size);
716 /*
717             &RMF_MDT_BODY,
718             &RMF_MDT_MD,
719             &RMF_ACL, or &RMF_LOGCOOKIES
720 (optional)  &RMF_CAPA1,
721 (optional)  &RMF_CAPA2,
722 (optional)  something else
723 */
724
725         /* MDT_MD buffer may be bigger than packed value, let's shrink all
726          * buffers before growing it */
727         if (info->mti_big_lmm_used) {
728                 /* big_lmm buffer may be used even without packing the result
729                  * into reply, just for internal server needs */
730                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
731                         md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
732                                                          RCL_SERVER);
733
734                 /* free big lmm if md_size is not needed */
735                 if (md_size == 0 || md_packed == 0) {
736                         info->mti_big_lmm_used = 0;
737                 } else {
738                         /* buffer must be allocated separately */
739                         LASSERT(info->mti_attr.ma_lmm !=
740                                 req_capsule_server_get(pill, &RMF_MDT_MD));
741                         req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
742                 }
743         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
744                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
745         }
746
747         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
748                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
749         else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
750                 req_capsule_shrink(pill, &RMF_LOGCOOKIES,
751                                    acl_size, RCL_SERVER);
752
753         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
754             !(body->mbo_valid & OBD_MD_FLMDSCAPA))
755                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
756
757         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
758             !(body->mbo_valid & OBD_MD_FLOSSCAPA))
759                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
760
761         /*
762          * Some more field should be shrinked if needed.
763          * This should be done by those who added fields to reply message.
764          */
765
766         /* Grow MD buffer if needed finally */
767         if (info->mti_big_lmm_used) {
768                 void *lmm;
769
770                 LASSERT(md_size > md_packed);
771                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
772                        md_size - md_packed);
773                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
774                 if (rc) {
775                         /* we can't answer with proper LOV EA, drop flags,
776                          * the rc is also returned so this request is
777                          * considered as failed */
778                         body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
779                         /* don't return transno along with error */
780                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
781                 } else {
782                         /* now we need to pack right LOV/LMV EA */
783                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
784                         if (info->mti_attr.ma_valid & MA_LOV) {
785                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
786                                                              RCL_SERVER) ==
787                                                 info->mti_attr.ma_lmm_size);
788                                 memcpy(lmm, info->mti_attr.ma_lmm,
789                                        info->mti_attr.ma_lmm_size);
790                         } else if (info->mti_attr.ma_valid & MA_LMV) {
791                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
792                                                              RCL_SERVER) ==
793                                                 info->mti_attr.ma_lmv_size);
794                                 memcpy(lmm, info->mti_attr.ma_lmv,
795                                        info->mti_attr.ma_lmv_size);
796                         }
797                 }
798                 /* update mdt_max_mdsize so clients will be aware about that */
799                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
800                         info->mti_mdt->mdt_max_mdsize =
801                                                     info->mti_attr.ma_lmm_size;
802                 info->mti_big_lmm_used = 0;
803         }
804         RETURN(rc);
805 }
806
807
808 /* if object is dying, pack the lov/llog data,
809  * parameter info->mti_attr should be valid at this point! */
810 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
811                            const struct md_attr *ma)
812 {
813         struct mdt_body       *repbody;
814         const struct lu_attr *la = &ma->ma_attr;
815         ENTRY;
816
817         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
818         LASSERT(repbody != NULL);
819
820         if (ma->ma_valid & MA_INODE)
821                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
822
823         if (ma->ma_valid & MA_LOV) {
824                 CERROR("No need in LOV EA upon unlink\n");
825                 dump_stack();
826         }
827         repbody->mbo_eadatasize = 0;
828
829         RETURN(0);
830 }
831
832 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
833                                   struct md_attr *ma)
834 {
835         __u64 out;
836
837         out = 0;
838         if (in & MDS_ATTR_MODE)
839                 out |= LA_MODE;
840         if (in & MDS_ATTR_UID)
841                 out |= LA_UID;
842         if (in & MDS_ATTR_GID)
843                 out |= LA_GID;
844         if (in & MDS_ATTR_SIZE)
845                 out |= LA_SIZE;
846         if (in & MDS_ATTR_BLOCKS)
847                 out |= LA_BLOCKS;
848         if (in & MDS_ATTR_ATIME_SET)
849                 out |= LA_ATIME;
850         if (in & MDS_ATTR_CTIME_SET)
851                 out |= LA_CTIME;
852         if (in & MDS_ATTR_MTIME_SET)
853                 out |= LA_MTIME;
854         if (in & MDS_ATTR_ATTR_FLAG)
855                 out |= LA_FLAGS;
856         if (in & MDS_ATTR_KILL_SUID)
857                 out |= LA_KILL_SUID;
858         if (in & MDS_ATTR_KILL_SGID)
859                 out |= LA_KILL_SGID;
860
861         if (in & MDS_ATTR_FROM_OPEN)
862                 rr->rr_flags |= MRF_OPEN_TRUNC;
863         if (in & MDS_OPEN_OWNEROVERRIDE)
864                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
865         if (in & MDS_ATTR_FORCE)
866                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
867
868         in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID |
869                 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
870                 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
871                 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
872                 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
873                 MDS_ATTR_FROM_OPEN | MDS_OPEN_OWNEROVERRIDE);
874         if (in != 0)
875                 CERROR("Unknown attr bits: "LPX64"\n", in);
876         return out;
877 }
878
879 /* unpacking */
880
881 int mdt_name_unpack(struct req_capsule *pill,
882                     const struct req_msg_field *field,
883                     struct lu_name *ln,
884                     enum mdt_name_flags flags)
885 {
886         ln->ln_name = req_capsule_client_get(pill, field);
887         ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
888
889         if (!lu_name_is_valid(ln)) {
890                 ln->ln_name = NULL;
891                 ln->ln_namelen = 0;
892
893                 return -EPROTO;
894         }
895
896         if ((flags & MNF_FIX_ANON) &&
897             ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
898                 /* Newer (3.x) kernels use a name of "/" for the
899                  * "anonymous" disconnected dentries from NFS
900                  * filehandle conversion. See d_obtain_alias(). */
901                 ln->ln_name = NULL;
902                 ln->ln_namelen = 0;
903         }
904
905         return 0;
906 }
907
908 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
909 {
910         struct lu_ucred         *uc = mdt_ucred(info);
911         struct md_attr          *ma = &info->mti_attr;
912         struct lu_attr          *la = &ma->ma_attr;
913         struct req_capsule      *pill = info->mti_pill;
914         struct mdt_reint_record *rr = &info->mti_rr;
915         struct mdt_rec_setattr  *rec;
916         struct lu_nodemap       *nodemap =
917                 info->mti_exp->exp_target_data.ted_nodemap;
918         ENTRY;
919
920         CLASSERT(sizeof(struct mdt_rec_setattr)== sizeof(struct mdt_rec_reint));
921         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
922         if (rec == NULL)
923                 RETURN(-EFAULT);
924
925         /* This prior initialization is needed for old_init_ucred_reint() */
926         uc->uc_fsuid = rec->sa_fsuid;
927         uc->uc_fsgid = rec->sa_fsgid;
928         uc->uc_cap   = rec->sa_cap;
929         uc->uc_suppgids[0] = rec->sa_suppgid;
930         uc->uc_suppgids[1] = -1;
931
932         rr->rr_fid1 = &rec->sa_fid;
933         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
934         la->la_mode  = rec->sa_mode;
935         la->la_flags = rec->sa_attr_flags;
936         la->la_uid   = nodemap_map_id(nodemap, NODEMAP_UID,
937                                       NODEMAP_CLIENT_TO_FS, rec->sa_uid);
938         la->la_gid   = nodemap_map_id(nodemap, NODEMAP_GID,
939                                       NODEMAP_CLIENT_TO_FS, rec->sa_gid);
940         la->la_size  = rec->sa_size;
941         la->la_blocks = rec->sa_blocks;
942         la->la_ctime = rec->sa_ctime;
943         la->la_atime = rec->sa_atime;
944         la->la_mtime = rec->sa_mtime;
945         ma->ma_valid = MA_INODE;
946
947         if (rec->sa_bias & MDS_DATA_MODIFIED)
948                 ma->ma_attr_flags |= MDS_DATA_MODIFIED;
949         else
950                 ma->ma_attr_flags &= ~MDS_DATA_MODIFIED;
951
952         if (rec->sa_bias & MDS_HSM_RELEASE)
953                 ma->ma_attr_flags |= MDS_HSM_RELEASE;
954         else
955                 ma->ma_attr_flags &= ~MDS_HSM_RELEASE;
956
957         if (rec->sa_bias & MDS_CLOSE_LAYOUT_SWAP)
958                 ma->ma_attr_flags |= MDS_CLOSE_LAYOUT_SWAP;
959         else
960                 ma->ma_attr_flags &= ~MDS_CLOSE_LAYOUT_SWAP;
961
962         RETURN(0);
963 }
964
965 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
966 {
967         struct req_capsule *pill = info->mti_pill;
968         struct mdt_ioepoch *ioepoch;
969         ENTRY;
970
971         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
972                 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
973         else
974                 ioepoch = NULL;
975
976         if (ioepoch == NULL)
977                 RETURN(-EPROTO);
978
979         info->mti_close_handle = ioepoch->mio_handle;
980
981         RETURN(0);
982 }
983
984 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
985         struct req_capsule      *pill = info->mti_pill;
986
987         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
988                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
989                 if (info->mti_dlm_req == NULL)
990                         RETURN(-EFAULT);
991         }
992
993         RETURN(0);
994 }
995
996 static int mdt_setattr_unpack(struct mdt_thread_info *info)
997 {
998         struct mdt_reint_record *rr = &info->mti_rr;
999         struct md_attr          *ma = &info->mti_attr;
1000         struct req_capsule      *pill = info->mti_pill;
1001         int rc;
1002         ENTRY;
1003
1004         rc = mdt_setattr_unpack_rec(info);
1005         if (rc)
1006                 RETURN(rc);
1007
1008         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1009                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1010                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1011                                                         RCL_CLIENT);
1012                 if (rr->rr_eadatalen > 0) {
1013                         const struct lmv_user_md        *lum;
1014
1015                         lum = rr->rr_eadata;
1016                         /* Sigh ma_valid(from req) does not indicate whether
1017                          * it will set LOV/LMV EA, so we have to check magic */
1018                         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1019                                 ma->ma_valid |= MA_LMV;
1020                                 ma->ma_lmv = (void *)rr->rr_eadata;
1021                                 ma->ma_lmv_size = rr->rr_eadatalen;
1022                         } else {
1023                                 ma->ma_valid |= MA_LOV;
1024                                 ma->ma_lmm = (void *)rr->rr_eadata;
1025                                 ma->ma_lmm_size = rr->rr_eadatalen;
1026                         }
1027                 }
1028         }
1029
1030         rc = mdt_dlmreq_unpack(info);
1031         RETURN(rc);
1032 }
1033
1034 static int mdt_intent_close_unpack(struct mdt_thread_info *info)
1035 {
1036         struct md_attr          *ma = &info->mti_attr;
1037         struct req_capsule      *pill = info->mti_pill;
1038         ENTRY;
1039
1040         if (!(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
1041                 RETURN(0);
1042
1043         req_capsule_extend(pill, &RQF_MDS_INTENT_CLOSE);
1044
1045         if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1046             req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1047                 RETURN(-EFAULT);
1048
1049         RETURN(0);
1050 }
1051
1052 int mdt_close_unpack(struct mdt_thread_info *info)
1053 {
1054         int rc;
1055         ENTRY;
1056
1057         rc = mdt_close_handle_unpack(info);
1058         if (rc)
1059                 RETURN(rc);
1060
1061         rc = mdt_setattr_unpack_rec(info);
1062         if (rc)
1063                 RETURN(rc);
1064
1065         rc = mdt_intent_close_unpack(info);
1066         if (rc)
1067                 RETURN(rc);
1068
1069         RETURN(mdt_init_ucred_reint(info));
1070 }
1071
1072 static int mdt_create_unpack(struct mdt_thread_info *info)
1073 {
1074         struct lu_ucred         *uc  = mdt_ucred(info);
1075         struct mdt_rec_create   *rec;
1076         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1077         struct mdt_reint_record *rr = &info->mti_rr;
1078         struct req_capsule      *pill = info->mti_pill;
1079         struct md_op_spec       *sp = &info->mti_spec;
1080         int rc;
1081         ENTRY;
1082
1083         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1084         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1085         if (rec == NULL)
1086                 RETURN(-EFAULT);
1087
1088         /* This prior initialization is needed for old_init_ucred_reint() */
1089         uc->uc_fsuid = rec->cr_fsuid;
1090         uc->uc_fsgid = rec->cr_fsgid;
1091         uc->uc_cap   = rec->cr_cap;
1092         uc->uc_suppgids[0] = rec->cr_suppgid1;
1093         uc->uc_suppgids[1] = -1;
1094         uc->uc_umask = rec->cr_umask;
1095
1096         rr->rr_fid1 = &rec->cr_fid1;
1097         rr->rr_fid2 = &rec->cr_fid2;
1098         attr->la_mode = rec->cr_mode;
1099         attr->la_rdev  = rec->cr_rdev;
1100         attr->la_uid   = rec->cr_fsuid;
1101         attr->la_gid   = rec->cr_fsgid;
1102         attr->la_ctime = rec->cr_time;
1103         attr->la_mtime = rec->cr_time;
1104         attr->la_atime = rec->cr_time;
1105         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1106                          LA_CTIME | LA_MTIME | LA_ATIME;
1107         memset(&sp->u, 0, sizeof(sp->u));
1108         sp->sp_cr_flags = get_mrc_cr_flags(rec);
1109
1110         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1111         if (rc < 0)
1112                 RETURN(rc);
1113
1114         if (S_ISLNK(attr->la_mode)) {
1115                 const char *tgt = NULL;
1116
1117                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1118                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1119                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1120                         sp->u.sp_symname = tgt;
1121                 }
1122                 if (tgt == NULL)
1123                         RETURN(-EFAULT);
1124         } else {
1125                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
1126                 if (S_ISDIR(attr->la_mode) &&
1127                     req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1128                         sp->u.sp_ea.eadata =
1129                                 req_capsule_client_get(pill, &RMF_EADATA);
1130                         sp->u.sp_ea.eadatalen =
1131                                 req_capsule_get_size(pill, &RMF_EADATA,
1132                                                      RCL_CLIENT);
1133                         sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1134                 }
1135         }
1136
1137         rc = mdt_dlmreq_unpack(info);
1138         RETURN(rc);
1139 }
1140
1141 static int mdt_link_unpack(struct mdt_thread_info *info)
1142 {
1143         struct lu_ucred         *uc  = mdt_ucred(info);
1144         struct mdt_rec_link     *rec;
1145         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1146         struct mdt_reint_record *rr = &info->mti_rr;
1147         struct req_capsule      *pill = info->mti_pill;
1148         int rc;
1149         ENTRY;
1150
1151         CLASSERT(sizeof(struct mdt_rec_link) == sizeof(struct mdt_rec_reint));
1152         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1153         if (rec == NULL)
1154                 RETURN(-EFAULT);
1155
1156         /* This prior initialization is needed for old_init_ucred_reint() */
1157         uc->uc_fsuid = rec->lk_fsuid;
1158         uc->uc_fsgid = rec->lk_fsgid;
1159         uc->uc_cap   = rec->lk_cap;
1160         uc->uc_suppgids[0] = rec->lk_suppgid1;
1161         uc->uc_suppgids[1] = rec->lk_suppgid2;
1162
1163         attr->la_uid = rec->lk_fsuid;
1164         attr->la_gid = rec->lk_fsgid;
1165         rr->rr_fid1 = &rec->lk_fid1;
1166         rr->rr_fid2 = &rec->lk_fid2;
1167         attr->la_ctime = rec->lk_time;
1168         attr->la_mtime = rec->lk_time;
1169         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1170
1171         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1172         if (rc < 0)
1173                 RETURN(rc);
1174
1175         rc = mdt_dlmreq_unpack(info);
1176
1177         RETURN(rc);
1178 }
1179
1180 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1181 {
1182         struct lu_ucred         *uc  = mdt_ucred(info);
1183         struct mdt_rec_unlink   *rec;
1184         struct md_attr          *ma = &info->mti_attr;
1185         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1186         struct mdt_reint_record *rr = &info->mti_rr;
1187         struct req_capsule      *pill = info->mti_pill;
1188         int rc;
1189         ENTRY;
1190
1191         CLASSERT(sizeof(struct mdt_rec_unlink) == sizeof(struct mdt_rec_reint));
1192         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1193         if (rec == NULL)
1194                 RETURN(-EFAULT);
1195
1196         /* This prior initialization is needed for old_init_ucred_reint() */
1197         uc->uc_fsuid = rec->ul_fsuid;
1198         uc->uc_fsgid = rec->ul_fsgid;
1199         uc->uc_cap   = rec->ul_cap;
1200         uc->uc_suppgids[0] = rec->ul_suppgid1;
1201         uc->uc_suppgids[1] = -1;
1202
1203         attr->la_uid = rec->ul_fsuid;
1204         attr->la_gid = rec->ul_fsgid;
1205         rr->rr_fid1 = &rec->ul_fid1;
1206         rr->rr_fid2 = &rec->ul_fid2;
1207         attr->la_ctime = rec->ul_time;
1208         attr->la_mtime = rec->ul_time;
1209         attr->la_mode  = rec->ul_mode;
1210         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1211
1212         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1213         if (rc < 0)
1214                 RETURN(rc);
1215
1216         if (rec->ul_bias & MDS_VTX_BYPASS)
1217                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1218         else
1219                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1220
1221         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1222
1223         rc = mdt_dlmreq_unpack(info);
1224         RETURN(rc);
1225 }
1226
1227 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1228 {
1229         info->mti_spec.sp_rm_entry = 1;
1230         return mdt_unlink_unpack(info);
1231 }
1232
1233 static int mdt_rename_unpack(struct mdt_thread_info *info)
1234 {
1235         struct lu_ucred         *uc = mdt_ucred(info);
1236         struct mdt_rec_rename   *rec;
1237         struct md_attr          *ma = &info->mti_attr;
1238         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1239         struct mdt_reint_record *rr = &info->mti_rr;
1240         struct req_capsule      *pill = info->mti_pill;
1241         int rc;
1242         ENTRY;
1243
1244         CLASSERT(sizeof(struct mdt_rec_rename) == sizeof(struct mdt_rec_reint));
1245         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1246         if (rec == NULL)
1247                 RETURN(-EFAULT);
1248
1249         /* This prior initialization is needed for old_init_ucred_reint() */
1250         uc->uc_fsuid = rec->rn_fsuid;
1251         uc->uc_fsgid = rec->rn_fsgid;
1252         uc->uc_cap   = rec->rn_cap;
1253         uc->uc_suppgids[0] = rec->rn_suppgid1;
1254         uc->uc_suppgids[1] = rec->rn_suppgid2;
1255
1256         attr->la_uid = rec->rn_fsuid;
1257         attr->la_gid = rec->rn_fsgid;
1258         rr->rr_fid1 = &rec->rn_fid1;
1259         rr->rr_fid2 = &rec->rn_fid2;
1260         attr->la_ctime = rec->rn_time;
1261         attr->la_mtime = rec->rn_time;
1262         /* rename_tgt contains the mode already */
1263         attr->la_mode = rec->rn_mode;
1264         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1265
1266         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1267         if (rc < 0)
1268                 RETURN(rc);
1269
1270         rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1271         if (rc < 0)
1272                 RETURN(rc);
1273
1274         if (rec->rn_bias & MDS_VTX_BYPASS)
1275                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1276         else
1277                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1278
1279         if (rec->rn_bias & MDS_RENAME_MIGRATE) {
1280                 req_capsule_extend(info->mti_pill, &RQF_MDS_REINT_MIGRATE);
1281                 rc = mdt_close_handle_unpack(info);
1282                 if (rc < 0)
1283                         RETURN(rc);
1284                 info->mti_spec.sp_migrate_close = 1;
1285         }
1286
1287         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1288
1289
1290         rc = mdt_dlmreq_unpack(info);
1291
1292         RETURN(rc);
1293 }
1294
1295 /*
1296  * please see comment above LOV_MAGIC_V1_DEF
1297  */
1298 static void mdt_fix_lov_magic(struct mdt_thread_info *info)
1299 {
1300         struct mdt_reint_record *rr = &info->mti_rr;
1301         struct lov_user_md_v1   *v1;
1302
1303         v1 = (void *)rr->rr_eadata;
1304         LASSERT(v1);
1305
1306         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1307                 if (v1->lmm_magic == LOV_USER_MAGIC_V1) {
1308                         v1->lmm_magic = LOV_MAGIC_V1_DEF;
1309                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
1310                         v1->lmm_magic = __swab32(LOV_MAGIC_V1_DEF);
1311                 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
1312                         v1->lmm_magic = LOV_MAGIC_V3_DEF;
1313                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
1314                         v1->lmm_magic = __swab32(LOV_MAGIC_V3_DEF);
1315                 }
1316         }
1317 }
1318
1319 static int mdt_open_unpack(struct mdt_thread_info *info)
1320 {
1321         struct lu_ucred         *uc = mdt_ucred(info);
1322         struct mdt_rec_create   *rec;
1323         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1324         struct req_capsule      *pill = info->mti_pill;
1325         struct mdt_reint_record *rr   = &info->mti_rr;
1326         struct ptlrpc_request   *req  = mdt_info_req(info);
1327         struct md_op_spec       *sp   = &info->mti_spec;
1328         ENTRY;
1329
1330         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1331         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1332         if (rec == NULL)
1333                 RETURN(-EFAULT);
1334
1335         /* This prior initialization is needed for old_init_ucred_reint() */
1336         uc->uc_fsuid = rec->cr_fsuid;
1337         uc->uc_fsgid = rec->cr_fsgid;
1338         uc->uc_cap   = rec->cr_cap;
1339         uc->uc_suppgids[0] = rec->cr_suppgid1;
1340         uc->uc_suppgids[1] = rec->cr_suppgid2;
1341         uc->uc_umask = rec->cr_umask;
1342
1343         rr->rr_fid1   = &rec->cr_fid1;
1344         rr->rr_fid2   = &rec->cr_fid2;
1345         rr->rr_handle = &rec->cr_old_handle;
1346         attr->la_mode = rec->cr_mode;
1347         attr->la_rdev  = rec->cr_rdev;
1348         attr->la_uid   = rec->cr_fsuid;
1349         attr->la_gid   = rec->cr_fsgid;
1350         attr->la_ctime = rec->cr_time;
1351         attr->la_mtime = rec->cr_time;
1352         attr->la_atime = rec->cr_time;
1353         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1354                          LA_CTIME | LA_MTIME | LA_ATIME;
1355         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1356         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1357         /* Do not trigger ASSERTION if client miss to set such flags. */
1358         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1359                 RETURN(-EPROTO);
1360
1361         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1362
1363         mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1364
1365         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1366                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1367                                                         RCL_CLIENT);
1368                 if (rr->rr_eadatalen > 0) {
1369                         rr->rr_eadata = req_capsule_client_get(pill,
1370                                                                &RMF_EADATA);
1371                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1372                         sp->u.sp_ea.eadata = rr->rr_eadata;
1373                         sp->no_create = !!req_is_replay(req);
1374                         mdt_fix_lov_magic(info);
1375                 }
1376
1377                 /*
1378                  * Client default md_size may be 0 right after client start,
1379                  * until all osc are connected, set here just some reasonable
1380                  * value to prevent misbehavior.
1381                  */
1382                 if (rr->rr_eadatalen == 0 &&
1383                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1384                         rr->rr_eadatalen = MIN_MD_SIZE;
1385         }
1386
1387         RETURN(0);
1388 }
1389
1390 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1391 {
1392         struct mdt_reint_record *rr     = &info->mti_rr;
1393         struct lu_ucred         *uc     = mdt_ucred(info);
1394         struct lu_attr          *attr   = &info->mti_attr.ma_attr;
1395         struct req_capsule      *pill   = info->mti_pill;
1396         struct mdt_rec_setxattr *rec;
1397         int                      rc;
1398         ENTRY;
1399
1400
1401         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1402                          sizeof(struct mdt_rec_reint));
1403
1404         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1405         if (rec == NULL)
1406                 RETURN(-EFAULT);
1407
1408         /* This prior initialization is needed for old_init_ucred_reint() */
1409         uc->uc_fsuid  = rec->sx_fsuid;
1410         uc->uc_fsgid  = rec->sx_fsgid;
1411         uc->uc_cap    = rec->sx_cap;
1412         uc->uc_suppgids[0] = rec->sx_suppgid1;
1413         uc->uc_suppgids[1] = -1;
1414
1415         rr->rr_opcode = rec->sx_opcode;
1416         rr->rr_fid1   = &rec->sx_fid;
1417         attr->la_valid = rec->sx_valid;
1418         attr->la_ctime = rec->sx_time;
1419         attr->la_size = rec->sx_size;
1420         attr->la_flags = rec->sx_flags;
1421
1422         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1423         if (rc < 0)
1424                 RETURN(rc);
1425
1426         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1427                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1428                                                         RCL_CLIENT);
1429                 if (rr->rr_eadatalen > 0) {
1430                         rr->rr_eadata = req_capsule_client_get(pill,
1431                                                                &RMF_EADATA);
1432                         if (rr->rr_eadata == NULL)
1433                                 RETURN(-EFAULT);
1434                 } else {
1435                         rr->rr_eadata = NULL;
1436                 }
1437         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1438                 CDEBUG(D_INFO, "no xattr data supplied\n");
1439                 RETURN(-EFAULT);
1440         }
1441
1442         if (mdt_dlmreq_unpack(info) < 0)
1443                 RETURN(-EPROTO);
1444
1445         RETURN(0);
1446 }
1447
1448
1449 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1450
1451 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1452         [REINT_SETATTR]  = mdt_setattr_unpack,
1453         [REINT_CREATE]   = mdt_create_unpack,
1454         [REINT_LINK]     = mdt_link_unpack,
1455         [REINT_UNLINK]   = mdt_unlink_unpack,
1456         [REINT_RENAME]   = mdt_rename_unpack,
1457         [REINT_OPEN]     = mdt_open_unpack,
1458         [REINT_SETXATTR] = mdt_setxattr_unpack,
1459         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1460         [REINT_MIGRATE]  = mdt_rename_unpack,
1461 };
1462
1463 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1464 {
1465         int rc;
1466         ENTRY;
1467
1468         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1469         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1470                 info->mti_rr.rr_opcode = op;
1471                 rc = mdt_reint_unpackers[op](info);
1472         } else {
1473                 CERROR("Unexpected opcode %d\n", op);
1474                 rc = -EFAULT;
1475         }
1476         RETURN(rc);
1477 }