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