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