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