Whamcloud - gitweb
LU-2682 fid: cleanup direct _id and _seq access
[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, 2012, 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 "mdt_internal.h"
52 #include <lnet/lib-lnet.h>
53
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 void mdt_exit_ucred(struct mdt_thread_info *info)
62 {
63         struct lu_ucred   *uc  = mdt_ucred(info);
64         struct mdt_device *mdt = info->mti_mdt;
65
66         LASSERT(uc != NULL);
67         if (uc->uc_valid != UCRED_INIT) {
68                 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
69                 if (uc->uc_ginfo) {
70                         cfs_put_group_info(uc->uc_ginfo);
71                         uc->uc_ginfo = NULL;
72                 }
73                 if (uc->uc_identity) {
74                         mdt_identity_put(mdt->mdt_identity_cache,
75                                          uc->uc_identity);
76                         uc->uc_identity = NULL;
77                 }
78                 uc->uc_valid = UCRED_INIT;
79         }
80 }
81
82 static int match_nosquash_list(struct rw_semaphore *sem,
83                                cfs_list_t *nidlist,
84                                lnet_nid_t peernid)
85 {
86         int rc;
87         ENTRY;
88         down_read(sem);
89         rc = cfs_match_nid(peernid, nidlist);
90         up_read(sem);
91         RETURN(rc);
92 }
93
94 /* root_squash for inter-MDS operations */
95 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
96 {
97         struct lu_ucred *ucred = mdt_ucred(info);
98         ENTRY;
99
100         LASSERT(ucred != NULL);
101         if (!info->mti_mdt->mdt_squash_uid || ucred->uc_fsuid)
102                 RETURN(0);
103
104         if (match_nosquash_list(&info->mti_mdt->mdt_squash_sem,
105                                 &info->mti_mdt->mdt_nosquash_nids,
106                                 peernid)) {
107                 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
108                        libcfs_nid2str(peernid));
109                 RETURN(0);
110         }
111
112         CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
113                libcfs_nid2str(peernid),
114                ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
115                info->mti_mdt->mdt_squash_uid, info->mti_mdt->mdt_squash_gid,
116                0);
117
118         ucred->uc_fsuid = info->mti_mdt->mdt_squash_uid;
119         ucred->uc_fsgid = info->mti_mdt->mdt_squash_gid;
120         ucred->uc_cap = 0;
121         ucred->uc_suppgids[0] = -1;
122         ucred->uc_suppgids[1] = -1;
123
124         RETURN(0);
125 }
126
127 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
128                           void *buf)
129 {
130         struct ptlrpc_request   *req = mdt_info_req(info);
131         struct mdt_device       *mdt = info->mti_mdt;
132         struct ptlrpc_user_desc *pud = req->rq_user_desc;
133         struct lu_ucred         *ucred = mdt_ucred(info);
134         lnet_nid_t               peernid = req->rq_peer.nid;
135         __u32                    perm = 0;
136         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
137         int                      setuid;
138         int                      setgid;
139         int                      rc = 0;
140
141         ENTRY;
142
143         LASSERT(req->rq_auth_gss);
144         LASSERT(!req->rq_auth_usr_mdt);
145         LASSERT(req->rq_user_desc);
146         LASSERT(ucred != NULL);
147
148         ucred->uc_valid = UCRED_INVALID;
149
150         ucred->uc_o_uid   = pud->pud_uid;
151         ucred->uc_o_gid   = pud->pud_gid;
152         ucred->uc_o_fsuid = pud->pud_fsuid;
153         ucred->uc_o_fsgid = pud->pud_fsgid;
154
155         if (type == BODY_INIT) {
156                 struct mdt_body *body = (struct mdt_body *)buf;
157
158                 ucred->uc_suppgids[0] = body->suppgid;
159                 ucred->uc_suppgids[1] = -1;
160         }
161
162         /* sanity check: we expect the uid which client claimed is true */
163         if (remote) {
164                 if (req->rq_auth_mapped_uid == INVALID_UID) {
165                         CDEBUG(D_SEC, "remote user not mapped, deny access!\n");
166                         RETURN(-EACCES);
167                 }
168
169                 if (ptlrpc_user_desc_do_idmap(req, pud))
170                         RETURN(-EACCES);
171
172                 if (req->rq_auth_mapped_uid != pud->pud_uid) {
173                         CDEBUG(D_SEC, "remote client %s: auth/mapped uid %u/%u "
174                                "while client claims %u:%u/%u:%u\n",
175                                libcfs_nid2str(peernid), req->rq_auth_uid,
176                                req->rq_auth_mapped_uid,
177                                pud->pud_uid, pud->pud_gid,
178                                pud->pud_fsuid, pud->pud_fsgid);
179                         RETURN(-EACCES);
180                 }
181         } else {
182                 if (req->rq_auth_uid != pud->pud_uid) {
183                         CDEBUG(D_SEC, "local client %s: auth uid %u "
184                                "while client claims %u:%u/%u:%u\n",
185                                libcfs_nid2str(peernid), req->rq_auth_uid,
186                                pud->pud_uid, pud->pud_gid,
187                                pud->pud_fsuid, pud->pud_fsgid);
188                         RETURN(-EACCES);
189                 }
190         }
191
192         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
193                 if (remote) {
194                         CDEBUG(D_SEC, "remote client must run with identity_get "
195                                "enabled!\n");
196                         RETURN(-EACCES);
197                 } else {
198                         ucred->uc_identity = NULL;
199                         perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
200                                CFS_SETGRP_PERM;
201                 }
202         } else {
203                 struct md_identity *identity;
204
205                 identity = mdt_identity_get(mdt->mdt_identity_cache,
206                                             pud->pud_uid);
207                 if (IS_ERR(identity)) {
208                         if (unlikely(PTR_ERR(identity) == -EREMCHG &&
209                                      !remote)) {
210                                 ucred->uc_identity = NULL;
211                                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
212                                        CFS_SETGRP_PERM;
213                         } else {
214                                 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
215                                        pud->pud_uid);
216                                 RETURN(-EACCES);
217                         }
218                 } else {
219                         ucred->uc_identity = identity;
220                         perm = mdt_identity_get_perm(ucred->uc_identity,
221                                                      remote, peernid);
222                 }
223         }
224
225         /* find out the setuid/setgid attempt */
226         setuid = (pud->pud_uid != pud->pud_fsuid);
227         setgid = ((pud->pud_gid != pud->pud_fsgid) ||
228                   (ucred->uc_identity &&
229                    (pud->pud_gid != ucred->uc_identity->mi_gid)));
230
231         /* check permission of setuid */
232         if (setuid && !(perm & CFS_SETUID_PERM)) {
233                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
234                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
235                 GOTO(out, rc = -EACCES);
236         }
237
238         /* check permission of setgid */
239         if (setgid && !(perm & CFS_SETGID_PERM)) {
240                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
241                        "from %s\n", pud->pud_uid, pud->pud_gid,
242                        pud->pud_fsuid, pud->pud_fsgid,
243                        ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
244                 GOTO(out, rc = -EACCES);
245         }
246
247         /*
248          * NB: remote client not allowed to setgroups anyway.
249          */
250         if (!remote && perm & CFS_SETGRP_PERM) {
251                 if (pud->pud_ngroups) {
252                         /* setgroups for local client */
253                         ucred->uc_ginfo = cfs_groups_alloc(pud->pud_ngroups);
254                         if (!ucred->uc_ginfo) {
255                                 CERROR("failed to alloc %d groups\n",
256                                        pud->pud_ngroups);
257                                 GOTO(out, rc = -ENOMEM);
258                         }
259
260                         lustre_groups_from_list(ucred->uc_ginfo,
261                                                 pud->pud_groups);
262                         lustre_groups_sort(ucred->uc_ginfo);
263                 } else {
264                         ucred->uc_ginfo = NULL;
265                 }
266         } else {
267                 ucred->uc_suppgids[0] = -1;
268                 ucred->uc_suppgids[1] = -1;
269                 ucred->uc_ginfo = NULL;
270         }
271
272         ucred->uc_uid   = pud->pud_uid;
273         ucred->uc_gid   = pud->pud_gid;
274         ucred->uc_fsuid = pud->pud_fsuid;
275         ucred->uc_fsgid = pud->pud_fsgid;
276
277         /* process root_squash here. */
278         mdt_root_squash(info, peernid);
279
280         /* remove fs privilege for non-root user. */
281         if (ucred->uc_fsuid)
282                 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
283         else
284                 ucred->uc_cap = pud->pud_cap;
285         if (remote && !(perm & CFS_RMTOWN_PERM))
286                 ucred->uc_cap &= ~(CFS_CAP_SYS_RESOURCE_MASK |
287                                    CFS_CAP_CHOWN_MASK);
288         ucred->uc_valid = UCRED_NEW;
289
290         EXIT;
291
292 out:
293         if (rc) {
294                 if (ucred->uc_ginfo) {
295                         cfs_put_group_info(ucred->uc_ginfo);
296                         ucred->uc_ginfo = NULL;
297                 }
298                 if (ucred->uc_identity) {
299                         mdt_identity_put(mdt->mdt_identity_cache,
300                                          ucred->uc_identity);
301                         ucred->uc_identity = NULL;
302                 }
303         }
304
305         return rc;
306 }
307
308 int mdt_check_ucred(struct mdt_thread_info *info)
309 {
310         struct ptlrpc_request   *req = mdt_info_req(info);
311         struct mdt_device       *mdt = info->mti_mdt;
312         struct ptlrpc_user_desc *pud = req->rq_user_desc;
313         struct lu_ucred         *ucred = mdt_ucred(info);
314         struct md_identity      *identity = NULL;
315         lnet_nid_t               peernid = req->rq_peer.nid;
316         __u32                    perm = 0;
317         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
318         int                      setuid;
319         int                      setgid;
320         int                      rc = 0;
321
322         ENTRY;
323
324         LASSERT(ucred != NULL);
325         if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
326                 RETURN(0);
327
328         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
329                 RETURN(0);
330
331         /* sanity check: if we use strong authentication, we expect the
332          * uid which client claimed is true */
333         if (remote) {
334                 if (req->rq_auth_mapped_uid == INVALID_UID) {
335                         CDEBUG(D_SEC, "remote user not mapped, deny access!\n");
336                         RETURN(-EACCES);
337                 }
338
339                 if (ptlrpc_user_desc_do_idmap(req, pud))
340                         RETURN(-EACCES);
341
342                 if (req->rq_auth_mapped_uid != pud->pud_uid) {
343                         CDEBUG(D_SEC, "remote client %s: auth/mapped uid %u/%u "
344                                "while client claims %u:%u/%u:%u\n",
345                                libcfs_nid2str(peernid), req->rq_auth_uid,
346                                req->rq_auth_mapped_uid,
347                                pud->pud_uid, pud->pud_gid,
348                                pud->pud_fsuid, pud->pud_fsgid);
349                         RETURN(-EACCES);
350                 }
351         } else {
352                 if (req->rq_auth_uid != pud->pud_uid) {
353                         CDEBUG(D_SEC, "local client %s: auth uid %u "
354                                "while client claims %u:%u/%u:%u\n",
355                                libcfs_nid2str(peernid), req->rq_auth_uid,
356                                pud->pud_uid, pud->pud_gid,
357                                pud->pud_fsuid, pud->pud_fsgid);
358                         RETURN(-EACCES);
359                 }
360         }
361
362         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
363                 if (remote) {
364                         CDEBUG(D_SEC, "remote client must run with identity_get "
365                                "enabled!\n");
366                         RETURN(-EACCES);
367                 }
368                 RETURN(0);
369         }
370
371         identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
372         if (IS_ERR(identity)) {
373                 if (unlikely(PTR_ERR(identity) == -EREMCHG &&
374                              !remote)) {
375                         RETURN(0);
376                 } else {
377                         CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
378                                pud->pud_uid);
379                         RETURN(-EACCES);
380                }
381         }
382
383         perm = mdt_identity_get_perm(identity, remote, peernid);
384         /* find out the setuid/setgid attempt */
385         setuid = (pud->pud_uid != pud->pud_fsuid);
386         setgid = (pud->pud_gid != pud->pud_fsgid ||
387                   pud->pud_gid != identity->mi_gid);
388
389         /* check permission of setuid */
390         if (setuid && !(perm & CFS_SETUID_PERM)) {
391                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
392                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
393                 GOTO(out, rc = -EACCES);
394         }
395
396         /* check permission of setgid */
397         if (setgid && !(perm & CFS_SETGID_PERM)) {
398                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
399                        "from %s\n", pud->pud_uid, pud->pud_gid,
400                        pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
401                        libcfs_nid2str(peernid));
402                 GOTO(out, rc = -EACCES);
403         }
404
405         EXIT;
406
407 out:
408         mdt_identity_put(mdt->mdt_identity_cache, identity);
409         return rc;
410 }
411
412 static int old_init_ucred(struct mdt_thread_info *info,
413                           struct mdt_body *body)
414 {
415         struct lu_ucred *uc = mdt_ucred(info);
416         struct mdt_device  *mdt = info->mti_mdt;
417         struct md_identity *identity = NULL;
418
419         ENTRY;
420
421         LASSERT(uc != NULL);
422         uc->uc_valid = UCRED_INVALID;
423         uc->uc_o_uid = uc->uc_uid = body->uid;
424         uc->uc_o_gid = uc->uc_gid = body->gid;
425         uc->uc_o_fsuid = uc->uc_fsuid = body->fsuid;
426         uc->uc_o_fsgid = uc->uc_fsgid = body->fsgid;
427         uc->uc_suppgids[0] = body->suppgid;
428         uc->uc_suppgids[1] = -1;
429         uc->uc_ginfo = NULL;
430         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
431                 identity = mdt_identity_get(mdt->mdt_identity_cache,
432                                             uc->uc_fsuid);
433                 if (IS_ERR(identity)) {
434                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
435                                 identity = NULL;
436                         } else {
437                                 CDEBUG(D_SEC, "Deny access without identity: "
438                                        "uid %u\n", uc->uc_fsuid);
439                                 RETURN(-EACCES);
440                         }
441                 }
442         }
443         uc->uc_identity = identity;
444
445         /* process root_squash here. */
446         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
447
448         /* remove fs privilege for non-root user. */
449         if (uc->uc_fsuid)
450                 uc->uc_cap = body->capability & ~CFS_CAP_FS_MASK;
451         else
452                 uc->uc_cap = body->capability;
453         uc->uc_valid = UCRED_OLD;
454
455         RETURN(0);
456 }
457
458 static int old_init_ucred_reint(struct mdt_thread_info *info)
459 {
460         struct lu_ucred *uc = mdt_ucred(info);
461         struct mdt_device  *mdt = info->mti_mdt;
462         struct md_identity *identity = NULL;
463
464         ENTRY;
465
466         LASSERT(uc != NULL);
467         uc->uc_valid = UCRED_INVALID;
468         uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
469         uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
470         uc->uc_ginfo = NULL;
471         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
472                 identity = mdt_identity_get(mdt->mdt_identity_cache,
473                                             uc->uc_fsuid);
474                 if (IS_ERR(identity)) {
475                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
476                                 identity = NULL;
477                         } else {
478                                 CDEBUG(D_SEC, "Deny access without identity: "
479                                        "uid %u\n", uc->uc_fsuid);
480                                 RETURN(-EACCES);
481                         }
482                 }
483         }
484         uc->uc_identity = identity;
485
486         /* process root_squash here. */
487         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
488
489         /* remove fs privilege for non-root user. */
490         if (uc->uc_fsuid)
491                 uc->uc_cap &= ~CFS_CAP_FS_MASK;
492         uc->uc_valid = UCRED_OLD;
493
494         RETURN(0);
495 }
496
497 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
498 {
499         struct ptlrpc_request *req = mdt_info_req(info);
500         struct lu_ucred       *uc  = mdt_ucred(info);
501
502         LASSERT(uc != NULL);
503         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
504                 return 0;
505
506         mdt_exit_ucred(info);
507
508         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
509                 return old_init_ucred(info, body);
510         else
511                 return new_init_ucred(info, BODY_INIT, body);
512 }
513
514 int mdt_init_ucred_reint(struct mdt_thread_info *info)
515 {
516         struct ptlrpc_request *req = mdt_info_req(info);
517         struct lu_ucred       *uc  = mdt_ucred(info);
518
519         LASSERT(uc != NULL);
520         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
521                 return 0;
522
523         mdt_exit_ucred(info);
524
525         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
526                 return old_init_ucred_reint(info);
527         else
528                 return new_init_ucred(info, REC_INIT, NULL);
529 }
530
531 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
532 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm)
533 {
534         const struct lov_ost_data_v1 *lod;
535         int                           i;
536         __u16                         count;
537
538         count = le16_to_cpu(((struct lov_user_md*)lmm)->lmm_stripe_count);
539
540         CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
541                POSTID(&lmm->lmm_oi), le32_to_cpu(lmm->lmm_magic),
542                le32_to_cpu(lmm->lmm_pattern));
543         CDEBUG(level,"stripe_size=0x%x, stripe_count=0x%x\n",
544                le32_to_cpu(lmm->lmm_stripe_size), count);
545         if (count == LOV_ALL_STRIPES)
546                 return;
547
548         LASSERT(count <= LOV_MAX_STRIPE_COUNT);
549         for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
550                 struct ost_id   oi;
551                 ostid_le_to_cpu((struct ost_id *)&lod->l_ost_oi, &oi);
552                 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
553                        i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
554         }
555 }
556
557 /* Shrink and/or grow reply buffers */
558 int mdt_fix_reply(struct mdt_thread_info *info)
559 {
560         struct req_capsule *pill = info->mti_pill;
561         struct mdt_body    *body;
562         int                md_size, md_packed = 0;
563         int                acl_size;
564         int                rc = 0;
565         ENTRY;
566
567         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
568         LASSERT(body != NULL);
569
570         if (body->valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE | OBD_MD_LINKNAME))
571                 md_size = body->eadatasize;
572         else
573                 md_size = 0;
574
575         acl_size = body->aclsize;
576
577         /* this replay - not send info to client */
578         if (info->mti_spec.no_create) {
579                 md_size = 0;
580                 acl_size = 0;
581         }
582
583         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d"
584                         " MDSCAPA = %llx, OSSCAPA = %llx\n",
585                         md_size, acl_size,
586                         (unsigned long long)(body->valid & OBD_MD_FLMDSCAPA),
587                         (unsigned long long)(body->valid & OBD_MD_FLOSSCAPA));
588 /*
589             &RMF_MDT_BODY,
590             &RMF_MDT_MD,
591             &RMF_ACL, or &RMF_LOGCOOKIES
592 (optional)  &RMF_CAPA1,
593 (optional)  &RMF_CAPA2,
594 (optional)  something else
595 */
596
597         /* MDT_MD buffer may be bigger than packed value, let's shrink all
598          * buffers before growing it */
599         if (info->mti_big_lmm_used) {
600                 LASSERT(req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER));
601                 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
602                                                  RCL_SERVER);
603                 LASSERT(md_packed > 0);
604                 /* buffer must be allocated separately */
605                 LASSERT(info->mti_attr.ma_lmm !=
606                         req_capsule_server_get(pill, &RMF_MDT_MD));
607                 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
608                 /* free big lmm if md_size is not needed */
609                 if (md_size == 0)
610                         info->mti_big_lmm_used = 0;
611         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
612                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
613         }
614
615         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
616                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
617         else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
618                 req_capsule_shrink(pill, &RMF_LOGCOOKIES,
619                                    acl_size, RCL_SERVER);
620
621         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
622             !(body->valid & OBD_MD_FLMDSCAPA))
623                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
624
625         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
626             !(body->valid & OBD_MD_FLOSSCAPA))
627                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
628
629         /*
630          * Some more field should be shrinked if needed.
631          * This should be done by those who added fields to reply message.
632          */
633
634         /* Grow MD buffer if needed finally */
635         if (info->mti_big_lmm_used) {
636                 void *lmm;
637
638                 LASSERT(md_size > md_packed);
639                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
640                        md_size - md_packed);
641                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
642                 if (rc) {
643                         /* we can't answer with proper LOV EA, drop flags,
644                          * the rc is also returned so this request is
645                          * considered as failed */
646                         body->valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
647                         /* don't return transno along with error */
648                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
649                 } else {
650                         /* now we need to pack right LOV EA */
651                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
652                         LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
653                                                      RCL_SERVER) ==
654                                 info->mti_attr.ma_lmm_size);
655                         memcpy(lmm, info->mti_attr.ma_lmm,
656                                info->mti_attr.ma_lmm_size);
657                 }
658                 /* update mdt_max_mdsize so clients will be aware about that */
659                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
660                         info->mti_mdt->mdt_max_mdsize =
661                                                     info->mti_attr.ma_lmm_size;
662                 info->mti_big_lmm_used = 0;
663         }
664         RETURN(rc);
665 }
666
667
668 /* if object is dying, pack the lov/llog data,
669  * parameter info->mti_attr should be valid at this point! */
670 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
671                            const struct md_attr *ma)
672 {
673         struct mdt_body       *repbody;
674         const struct lu_attr *la = &ma->ma_attr;
675         int rc;
676         ENTRY;
677
678         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
679         LASSERT(repbody != NULL);
680
681         if (ma->ma_valid & MA_INODE)
682                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
683
684         if (ma->ma_valid & MA_LOV) {
685                 CERROR("No need in LOV EA upon unlink\n");
686                 dump_stack();
687         }
688         repbody->eadatasize = 0;
689
690         if (ma->ma_cookie_size && (ma->ma_valid & MA_COOKIE)) {
691                 repbody->aclsize = ma->ma_cookie_size;
692                 repbody->valid |= OBD_MD_FLCOOKIE;
693         }
694
695         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
696             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
697             repbody->valid & OBD_MD_FLEASIZE) {
698                 struct lustre_capa *capa;
699
700                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
701                 LASSERT(capa);
702                 capa->lc_opc = CAPA_OPC_OSS_DESTROY;
703                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
704                 if (rc)
705                         RETURN(rc);
706
707                 repbody->valid |= OBD_MD_FLOSSCAPA;
708         }
709
710         RETURN(0);
711 }
712
713 static inline unsigned int attr_unpack(__u64 sa_valid) {
714         unsigned int ia_valid = 0;
715
716         if (sa_valid & MDS_ATTR_MODE)
717                 ia_valid |= ATTR_MODE;
718         if (sa_valid & MDS_ATTR_UID)
719                 ia_valid |= ATTR_UID;
720         if (sa_valid & MDS_ATTR_GID)
721                 ia_valid |= ATTR_GID;
722         if (sa_valid & MDS_ATTR_SIZE)
723                 ia_valid |= ATTR_SIZE;
724         if (sa_valid & MDS_ATTR_ATIME)
725                 ia_valid |= ATTR_ATIME;
726         if (sa_valid & MDS_ATTR_MTIME)
727                 ia_valid |= ATTR_MTIME;
728         if (sa_valid & MDS_ATTR_CTIME)
729                 ia_valid |= ATTR_CTIME;
730         if (sa_valid & MDS_ATTR_ATIME_SET)
731                 ia_valid |= ATTR_ATIME_SET;
732         if (sa_valid & MDS_ATTR_MTIME_SET)
733                 ia_valid |= ATTR_MTIME_SET;
734         if (sa_valid & MDS_ATTR_FORCE)
735                 ia_valid |= ATTR_FORCE;
736         if (sa_valid & MDS_ATTR_ATTR_FLAG)
737                 ia_valid |= ATTR_ATTR_FLAG;
738         if (sa_valid & MDS_ATTR_KILL_SUID)
739                 ia_valid |=  ATTR_KILL_SUID;
740         if (sa_valid & MDS_ATTR_KILL_SGID)
741                 ia_valid |= ATTR_KILL_SGID;
742         if (sa_valid & MDS_ATTR_CTIME_SET)
743                 ia_valid |= ATTR_CTIME_SET;
744         if (sa_valid & MDS_ATTR_FROM_OPEN)
745                 ia_valid |= ATTR_FROM_OPEN;
746         if (sa_valid & MDS_ATTR_BLOCKS)
747                 ia_valid |= ATTR_BLOCKS;
748         if (sa_valid & MDS_OPEN_OWNEROVERRIDE)
749                 ia_valid |= MDS_OPEN_OWNEROVERRIDE;
750         return ia_valid;
751 }
752
753 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
754                                   struct md_attr *ma)
755 {
756         __u64 out;
757
758         out = 0;
759         if (in & ATTR_MODE)
760                 out |= LA_MODE;
761         if (in & ATTR_UID)
762                 out |= LA_UID;
763         if (in & ATTR_GID)
764                 out |= LA_GID;
765         if (in & ATTR_SIZE)
766                 out |= LA_SIZE;
767         if (in & ATTR_BLOCKS)
768                 out |= LA_BLOCKS;
769
770         if (in & ATTR_FROM_OPEN)
771                 rr->rr_flags |= MRF_OPEN_TRUNC;
772
773         if (in & ATTR_ATIME_SET)
774                 out |= LA_ATIME;
775
776         if (in & ATTR_CTIME_SET)
777                 out |= LA_CTIME;
778
779         if (in & ATTR_MTIME_SET)
780                 out |= LA_MTIME;
781
782         if (in & ATTR_ATTR_FLAG)
783                 out |= LA_FLAGS;
784
785         if (in & ATTR_KILL_SUID)
786                 out |= LA_KILL_SUID;
787
788         if (in & ATTR_KILL_SGID)
789                 out |= LA_KILL_SGID;
790
791         if (in & MDS_OPEN_OWNEROVERRIDE)
792                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
793
794         if (in & ATTR_FORCE)
795                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
796
797         /*XXX need ATTR_RAW?*/
798         in &= ~(ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_BLOCKS|
799                 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME|ATTR_FROM_OPEN|
800                 ATTR_ATIME_SET|ATTR_CTIME_SET|ATTR_MTIME_SET|
801                 ATTR_ATTR_FLAG|ATTR_RAW|MDS_OPEN_OWNEROVERRIDE|
802                 ATTR_FORCE|ATTR_KILL_SUID|ATTR_KILL_SGID);
803         if (in != 0)
804                 CERROR("Unknown attr bits: "LPX64"\n", in);
805         return out;
806 }
807 /* unpacking */
808
809 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
810 {
811         struct lu_ucred         *uc  = mdt_ucred(info);
812         struct md_attr          *ma = &info->mti_attr;
813         struct lu_attr          *la = &ma->ma_attr;
814         struct req_capsule      *pill = info->mti_pill;
815         struct mdt_reint_record *rr = &info->mti_rr;
816         struct mdt_rec_setattr  *rec;
817         ENTRY;
818
819         CLASSERT(sizeof(struct mdt_rec_setattr)== sizeof(struct mdt_rec_reint));
820         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
821         if (rec == NULL)
822                 RETURN(-EFAULT);
823
824         /* This prior initialization is needed for old_init_ucred_reint() */
825         uc->uc_fsuid = rec->sa_fsuid;
826         uc->uc_fsgid = rec->sa_fsgid;
827         uc->uc_cap   = rec->sa_cap;
828         uc->uc_suppgids[0] = rec->sa_suppgid;
829         uc->uc_suppgids[1] = -1;
830
831         rr->rr_fid1 = &rec->sa_fid;
832         la->la_valid = mdt_attr_valid_xlate(attr_unpack(rec->sa_valid), rr, ma);
833         la->la_mode  = rec->sa_mode;
834         la->la_flags = rec->sa_attr_flags;
835         la->la_uid   = rec->sa_uid;
836         la->la_gid   = rec->sa_gid;
837         la->la_size  = rec->sa_size;
838         la->la_blocks = rec->sa_blocks;
839         la->la_ctime = rec->sa_ctime;
840         la->la_atime = rec->sa_atime;
841         la->la_mtime = rec->sa_mtime;
842         ma->ma_valid = MA_INODE;
843
844         if (rec->sa_bias & MDS_DATA_MODIFIED)
845                 ma->ma_attr_flags |= MDS_DATA_MODIFIED;
846         else
847                 ma->ma_attr_flags &= ~MDS_DATA_MODIFIED;
848
849         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
850                 mdt_set_capainfo(info, 0, rr->rr_fid1,
851                                  req_capsule_client_get(pill, &RMF_CAPA1));
852
853         RETURN(0);
854 }
855
856 static int mdt_ioepoch_unpack(struct mdt_thread_info *info)
857 {
858         struct req_capsule *pill = info->mti_pill;
859         ENTRY;
860
861         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
862                 info->mti_ioepoch =
863                         req_capsule_client_get(pill, &RMF_MDT_EPOCH);
864         else
865                 info->mti_ioepoch = NULL;
866         RETURN(info->mti_ioepoch == NULL ? -EFAULT : 0);
867 }
868
869 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
870         struct req_capsule      *pill = info->mti_pill;
871
872         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
873                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
874                 if (info->mti_dlm_req == NULL)
875                         RETURN(-EFAULT);
876         }
877
878         RETURN(0);
879 }
880
881 static int mdt_setattr_unpack(struct mdt_thread_info *info)
882 {
883         struct mdt_reint_record *rr = &info->mti_rr;
884         struct md_attr          *ma = &info->mti_attr;
885         struct req_capsule      *pill = info->mti_pill;
886         int rc;
887         ENTRY;
888
889         rc = mdt_setattr_unpack_rec(info);
890         if (rc)
891                 RETURN(rc);
892
893         /* Epoch may be absent */
894         mdt_ioepoch_unpack(info);
895
896         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
897                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
898                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
899                                                         RCL_CLIENT);
900                 ma->ma_lmm_size = rr->rr_eadatalen;
901                 if (ma->ma_lmm_size > 0) {
902                         ma->ma_lmm = (void *)rr->rr_eadata;
903                         ma->ma_valid |= MA_LOV;
904                 }
905         }
906
907         rc = mdt_dlmreq_unpack(info);
908         RETURN(rc);
909 }
910
911 int mdt_close_unpack(struct mdt_thread_info *info)
912 {
913         int rc;
914         ENTRY;
915
916         rc = mdt_ioepoch_unpack(info);
917         if (rc)
918                 RETURN(rc);
919
920         rc = mdt_setattr_unpack_rec(info);
921         if (rc)
922                 RETURN(rc);
923         RETURN(mdt_init_ucred_reint(info));
924 }
925
926 static int mdt_create_unpack(struct mdt_thread_info *info)
927 {
928         struct lu_ucred         *uc  = mdt_ucred(info);
929         struct mdt_rec_create   *rec;
930         struct lu_attr          *attr = &info->mti_attr.ma_attr;
931         struct mdt_reint_record *rr = &info->mti_rr;
932         struct req_capsule      *pill = info->mti_pill;
933         struct md_op_spec       *sp = &info->mti_spec;
934         int rc;
935         ENTRY;
936
937         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
938         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
939         if (rec == NULL)
940                 RETURN(-EFAULT);
941
942         /* This prior initialization is needed for old_init_ucred_reint() */
943         uc->uc_fsuid = rec->cr_fsuid;
944         uc->uc_fsgid = rec->cr_fsgid;
945         uc->uc_cap   = rec->cr_cap;
946         uc->uc_suppgids[0] = rec->cr_suppgid1;
947         uc->uc_suppgids[1] = -1;
948         uc->uc_umask = rec->cr_umask;
949
950         rr->rr_fid1 = &rec->cr_fid1;
951         rr->rr_fid2 = &rec->cr_fid2;
952         attr->la_mode = rec->cr_mode;
953         attr->la_rdev  = rec->cr_rdev;
954         attr->la_uid   = rec->cr_fsuid;
955         attr->la_gid   = rec->cr_fsgid;
956         attr->la_ctime = rec->cr_time;
957         attr->la_mtime = rec->cr_time;
958         attr->la_atime = rec->cr_time;
959         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
960                          LA_CTIME | LA_MTIME | LA_ATIME;
961         memset(&sp->u, 0, sizeof(sp->u));
962         sp->sp_cr_flags = get_mrc_cr_flags(rec);
963
964         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
965                 mdt_set_capainfo(info, 0, rr->rr_fid1,
966                                  req_capsule_client_get(pill, &RMF_CAPA1));
967         mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
968
969         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
970         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME,
971                                               RCL_CLIENT) - 1;
972         LASSERT(rr->rr_name && rr->rr_namelen > 0);
973
974         if (S_ISLNK(attr->la_mode)) {
975                 const char *tgt = NULL;
976
977                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
978                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
979                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
980                         sp->u.sp_symname = tgt;
981                 }
982                 if (tgt == NULL)
983                         RETURN(-EFAULT);
984         } else {
985                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
986         }
987
988         rc = mdt_dlmreq_unpack(info);
989         RETURN(rc);
990 }
991
992 static int mdt_link_unpack(struct mdt_thread_info *info)
993 {
994         struct lu_ucred         *uc  = mdt_ucred(info);
995         struct mdt_rec_link     *rec;
996         struct lu_attr          *attr = &info->mti_attr.ma_attr;
997         struct mdt_reint_record *rr = &info->mti_rr;
998         struct req_capsule      *pill = info->mti_pill;
999         int rc;
1000         ENTRY;
1001
1002         CLASSERT(sizeof(struct mdt_rec_link) == sizeof(struct mdt_rec_reint));
1003         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1004         if (rec == NULL)
1005                 RETURN(-EFAULT);
1006
1007         /* This prior initialization is needed for old_init_ucred_reint() */
1008         uc->uc_fsuid = rec->lk_fsuid;
1009         uc->uc_fsgid = rec->lk_fsgid;
1010         uc->uc_cap   = rec->lk_cap;
1011         uc->uc_suppgids[0] = rec->lk_suppgid1;
1012         uc->uc_suppgids[1] = rec->lk_suppgid2;
1013
1014         attr->la_uid = rec->lk_fsuid;
1015         attr->la_gid = rec->lk_fsgid;
1016         rr->rr_fid1 = &rec->lk_fid1;
1017         rr->rr_fid2 = &rec->lk_fid2;
1018         attr->la_ctime = rec->lk_time;
1019         attr->la_mtime = rec->lk_time;
1020         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1021
1022         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1023                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1024                                  req_capsule_client_get(pill, &RMF_CAPA1));
1025         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1026                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1027                                  req_capsule_client_get(pill, &RMF_CAPA2));
1028
1029         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1030         if (rr->rr_name == NULL)
1031                 RETURN(-EFAULT);
1032         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1033
1034         LASSERT(rr->rr_namelen > 0);
1035
1036         rc = mdt_dlmreq_unpack(info);
1037         RETURN(rc);
1038 }
1039
1040 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1041 {
1042         struct lu_ucred         *uc  = mdt_ucred(info);
1043         struct mdt_rec_unlink   *rec;
1044         struct md_attr          *ma = &info->mti_attr;
1045         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1046         struct mdt_reint_record *rr = &info->mti_rr;
1047         struct req_capsule      *pill = info->mti_pill;
1048         int rc;
1049         ENTRY;
1050
1051         CLASSERT(sizeof(struct mdt_rec_unlink) == sizeof(struct mdt_rec_reint));
1052         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1053         if (rec == NULL)
1054                 RETURN(-EFAULT);
1055
1056         /* This prior initialization is needed for old_init_ucred_reint() */
1057         uc->uc_fsuid = rec->ul_fsuid;
1058         uc->uc_fsgid = rec->ul_fsgid;
1059         uc->uc_cap   = rec->ul_cap;
1060         uc->uc_suppgids[0] = rec->ul_suppgid1;
1061         uc->uc_suppgids[1] = -1;
1062
1063         attr->la_uid = rec->ul_fsuid;
1064         attr->la_gid = rec->ul_fsgid;
1065         rr->rr_fid1 = &rec->ul_fid1;
1066         rr->rr_fid2 = &rec->ul_fid2;
1067         attr->la_ctime = rec->ul_time;
1068         attr->la_mtime = rec->ul_time;
1069         attr->la_mode  = rec->ul_mode;
1070         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1071
1072         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1073                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1074                                  req_capsule_client_get(pill, &RMF_CAPA1));
1075
1076         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1077         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1078         if (rr->rr_name == NULL || rr->rr_namelen == 0)
1079                 RETURN(-EFAULT);
1080
1081         if (rec->ul_bias & MDS_VTX_BYPASS)
1082                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1083         else
1084                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1085
1086         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1087
1088         rc = mdt_dlmreq_unpack(info);
1089         RETURN(rc);
1090 }
1091
1092 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1093 {
1094         info->mti_spec.sp_rm_entry = 1;
1095         return mdt_unlink_unpack(info);
1096 }
1097
1098 static int mdt_rename_unpack(struct mdt_thread_info *info)
1099 {
1100         struct lu_ucred         *uc = mdt_ucred(info);
1101         struct mdt_rec_rename   *rec;
1102         struct md_attr          *ma = &info->mti_attr;
1103         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1104         struct mdt_reint_record *rr = &info->mti_rr;
1105         struct req_capsule      *pill = info->mti_pill;
1106         int rc;
1107         ENTRY;
1108
1109         CLASSERT(sizeof(struct mdt_rec_rename) == sizeof(struct mdt_rec_reint));
1110         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1111         if (rec == NULL)
1112                 RETURN(-EFAULT);
1113
1114         /* This prior initialization is needed for old_init_ucred_reint() */
1115         uc->uc_fsuid = rec->rn_fsuid;
1116         uc->uc_fsgid = rec->rn_fsgid;
1117         uc->uc_cap   = rec->rn_cap;
1118         uc->uc_suppgids[0] = rec->rn_suppgid1;
1119         uc->uc_suppgids[1] = rec->rn_suppgid2;
1120
1121         attr->la_uid = rec->rn_fsuid;
1122         attr->la_gid = rec->rn_fsgid;
1123         rr->rr_fid1 = &rec->rn_fid1;
1124         rr->rr_fid2 = &rec->rn_fid2;
1125         attr->la_ctime = rec->rn_time;
1126         attr->la_mtime = rec->rn_time;
1127         /* rename_tgt contains the mode already */
1128         attr->la_mode = rec->rn_mode;
1129         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1130
1131         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1132                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1133                                  req_capsule_client_get(pill, &RMF_CAPA1));
1134         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1135                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1136                                  req_capsule_client_get(pill, &RMF_CAPA2));
1137
1138         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1139         rr->rr_tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1140         if (rr->rr_name == NULL || rr->rr_tgt == NULL)
1141                 RETURN(-EFAULT);
1142         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1143         rr->rr_tgtlen = req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT) - 1;
1144         LASSERT(rr->rr_namelen > 0 && rr->rr_tgtlen > 0);
1145
1146         if (rec->rn_bias & MDS_VTX_BYPASS)
1147                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1148         else
1149                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1150
1151         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1152
1153         rc = mdt_dlmreq_unpack(info);
1154         RETURN(rc);
1155 }
1156
1157 /*
1158  * please see comment above LOV_MAGIC_V1_DEF
1159  */
1160 static void mdt_fix_lov_magic(struct mdt_thread_info *info)
1161 {
1162         struct mdt_reint_record *rr = &info->mti_rr;
1163         struct lov_user_md_v1   *v1;
1164
1165         v1 = (void *)rr->rr_eadata;
1166         LASSERT(v1);
1167
1168         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1169                 if (v1->lmm_magic == LOV_USER_MAGIC_V1) {
1170                         v1->lmm_magic = LOV_MAGIC_V1_DEF;
1171                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
1172                         v1->lmm_magic = __swab32(LOV_MAGIC_V1_DEF);
1173                 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
1174                         v1->lmm_magic = LOV_MAGIC_V3_DEF;
1175                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
1176                         v1->lmm_magic = __swab32(LOV_MAGIC_V3_DEF);
1177                 }
1178         }
1179 }
1180
1181 static int mdt_open_unpack(struct mdt_thread_info *info)
1182 {
1183         struct lu_ucred         *uc = mdt_ucred(info);
1184         struct mdt_rec_create   *rec;
1185         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1186         struct req_capsule      *pill = info->mti_pill;
1187         struct mdt_reint_record *rr   = &info->mti_rr;
1188         struct ptlrpc_request   *req  = mdt_info_req(info);
1189         struct md_op_spec       *sp   = &info->mti_spec;
1190         ENTRY;
1191
1192         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1193         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1194         if (rec == NULL)
1195                 RETURN(-EFAULT);
1196
1197         /* This prior initialization is needed for old_init_ucred_reint() */
1198         uc->uc_fsuid = rec->cr_fsuid;
1199         uc->uc_fsgid = rec->cr_fsgid;
1200         uc->uc_cap   = rec->cr_cap;
1201         uc->uc_suppgids[0] = rec->cr_suppgid1;
1202         uc->uc_suppgids[1] = rec->cr_suppgid2;
1203         uc->uc_umask = rec->cr_umask;
1204
1205         rr->rr_fid1   = &rec->cr_fid1;
1206         rr->rr_fid2   = &rec->cr_fid2;
1207         rr->rr_handle = &rec->cr_old_handle;
1208         attr->la_mode = rec->cr_mode;
1209         attr->la_rdev  = rec->cr_rdev;
1210         attr->la_uid   = rec->cr_fsuid;
1211         attr->la_gid   = rec->cr_fsgid;
1212         attr->la_ctime = rec->cr_time;
1213         attr->la_mtime = rec->cr_time;
1214         attr->la_atime = rec->cr_time;
1215         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1216                          LA_CTIME | LA_MTIME | LA_ATIME;
1217         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1218         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1219         /* Do not trigger ASSERTION if client miss to set such flags. */
1220         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1221                 RETURN(-EPROTO);
1222         info->mti_replayepoch = rec->cr_ioepoch;
1223
1224         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1225
1226         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1227                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1228                                  req_capsule_client_get(pill, &RMF_CAPA1));
1229         if (req_is_replay(req) &&
1230             req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT)) {
1231 #if 0
1232                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1233                                  req_capsule_client_get(pill, &RMF_CAPA2));
1234 #else
1235                 /*
1236                  * FIXME: capa in replay open request might have expired,
1237                  * bypass capa check. Security hole?
1238                  */
1239                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1240                 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1241 #endif
1242         }
1243
1244         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1245         if (rr->rr_name == NULL)
1246                 RETURN(-EFAULT);
1247         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1248
1249         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1250                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1251                                                         RCL_CLIENT);
1252                 if (rr->rr_eadatalen > 0) {
1253                         rr->rr_eadata = req_capsule_client_get(pill,
1254                                                                &RMF_EADATA);
1255                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1256                         sp->u.sp_ea.eadata = rr->rr_eadata;
1257                         sp->no_create = !!req_is_replay(req);
1258                         mdt_fix_lov_magic(info);
1259                 }
1260
1261                 /*
1262                  * Client default md_size may be 0 right after client start,
1263                  * until all osc are connected, set here just some reasonable
1264                  * value to prevent misbehavior.
1265                  */
1266                 if (rr->rr_eadatalen == 0 &&
1267                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1268                         rr->rr_eadatalen = MIN_MD_SIZE;
1269         }
1270
1271         RETURN(0);
1272 }
1273
1274 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1275 {
1276         struct mdt_reint_record   *rr   = &info->mti_rr;
1277         struct lu_ucred           *uc   = mdt_ucred(info);
1278         struct lu_attr            *attr = &info->mti_attr.ma_attr;
1279         struct req_capsule        *pill = info->mti_pill;
1280         struct mdt_rec_setxattr   *rec;
1281         ENTRY;
1282
1283
1284         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1285                          sizeof(struct mdt_rec_reint));
1286
1287         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1288         if (rec == NULL)
1289                 RETURN(-EFAULT);
1290
1291         /* This prior initialization is needed for old_init_ucred_reint() */
1292         uc->uc_fsuid  = rec->sx_fsuid;
1293         uc->uc_fsgid  = rec->sx_fsgid;
1294         uc->uc_cap    = rec->sx_cap;
1295         uc->uc_suppgids[0] = rec->sx_suppgid1;
1296         uc->uc_suppgids[1] = -1;
1297
1298         rr->rr_opcode = rec->sx_opcode;
1299         rr->rr_fid1   = &rec->sx_fid;
1300         attr->la_valid = rec->sx_valid;
1301         attr->la_ctime = rec->sx_time;
1302         attr->la_size = rec->sx_size;
1303         attr->la_flags = rec->sx_flags;
1304
1305         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1306                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1307                                  req_capsule_client_get(pill, &RMF_CAPA1));
1308         else
1309                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1310
1311         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1312         if (rr->rr_name == NULL)
1313                 RETURN(-EFAULT);
1314         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1315         LASSERT(rr->rr_namelen > 0);
1316
1317         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1318                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1319                                                         RCL_CLIENT);
1320                 if (rr->rr_eadatalen > 0) {
1321                         rr->rr_eadata = req_capsule_client_get(pill,
1322                                                                &RMF_EADATA);
1323                         if (rr->rr_eadata == NULL)
1324                                 RETURN(-EFAULT);
1325                 } else {
1326                         rr->rr_eadata = NULL;
1327                 }
1328         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1329                 CDEBUG(D_INFO, "no xattr data supplied\n");
1330                 RETURN(-EFAULT);
1331         }
1332
1333         RETURN(0);
1334 }
1335
1336
1337 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1338
1339 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1340         [REINT_SETATTR]  = mdt_setattr_unpack,
1341         [REINT_CREATE]   = mdt_create_unpack,
1342         [REINT_LINK]     = mdt_link_unpack,
1343         [REINT_UNLINK]   = mdt_unlink_unpack,
1344         [REINT_RENAME]   = mdt_rename_unpack,
1345         [REINT_OPEN]     = mdt_open_unpack,
1346         [REINT_SETXATTR] = mdt_setxattr_unpack,
1347         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1348 };
1349
1350 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1351 {
1352         int rc;
1353         ENTRY;
1354
1355         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1356         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1357                 info->mti_rr.rr_opcode = op;
1358                 rc = mdt_reint_unpackers[op](info);
1359         } else {
1360                 CERROR("Unexpected opcode %d\n", op);
1361                 rc = -EFAULT;
1362         }
1363         RETURN(rc);
1364 }