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