Whamcloud - gitweb
0d3307a8fbe2cca3f71428c610cc7d59d6d030ae
[fs/lustre-release.git] / lustre / mdt / mdt_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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
50 #ifndef EXPORT_SYMTAB
51 # define EXPORT_SYMTAB
52 #endif
53 #define DEBUG_SUBSYSTEM S_MDS
54
55 #include "mdt_internal.h"
56 #include <lnet/lib-lnet.h>
57
58
59 typedef enum ucred_init_type {
60         NONE_INIT       = 0,
61         BODY_INIT       = 1,
62         REC_INIT        = 2
63 } ucred_init_type_t;
64
65 void mdt_exit_ucred(struct mdt_thread_info *info)
66 {
67         struct md_ucred   *uc  = mdt_ucred(info);
68         struct mdt_device *mdt = info->mti_mdt;
69
70         if (uc->mu_valid != UCRED_INIT) {
71                 uc->mu_suppgids[0] = uc->mu_suppgids[1] = -1;
72                 if (uc->mu_ginfo) {
73                         cfs_put_group_info(uc->mu_ginfo);
74                         uc->mu_ginfo = NULL;
75                 }
76                 if (uc->mu_identity) {
77                         mdt_identity_put(mdt->mdt_identity_cache,
78                                          uc->mu_identity);
79                         uc->mu_identity = NULL;
80                 }
81                 uc->mu_valid = UCRED_INIT;
82         }
83 }
84
85 static int match_nosquash_list(cfs_rw_semaphore_t *sem,
86                                cfs_list_t *nidlist,
87                                lnet_nid_t peernid)
88 {
89         int rc;
90         ENTRY;
91         cfs_down_read(sem);
92         rc = cfs_match_nid(peernid, nidlist);
93         cfs_up_read(sem);
94         RETURN(rc);
95 }
96
97 /* root_squash for inter-MDS operations */
98 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
99 {
100         struct md_ucred *ucred = mdt_ucred(info);
101         ENTRY;
102
103         if (!info->mti_mdt->mdt_squash_uid || ucred->mu_fsuid)
104                 RETURN(0);
105
106         if (match_nosquash_list(&info->mti_mdt->mdt_squash_sem,
107                                 &info->mti_mdt->mdt_nosquash_nids,
108                                 peernid)) {
109                 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
110                        libcfs_nid2str(peernid));
111                 RETURN(0);
112         }
113
114         CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
115                libcfs_nid2str(peernid),
116                ucred->mu_fsuid, ucred->mu_fsgid, ucred->mu_cap,
117                info->mti_mdt->mdt_squash_uid, info->mti_mdt->mdt_squash_gid,
118                0);
119
120         ucred->mu_fsuid = info->mti_mdt->mdt_squash_uid;
121         ucred->mu_fsgid = info->mti_mdt->mdt_squash_gid;
122         ucred->mu_cap = 0;
123         ucred->mu_suppgids[0] = -1;
124         ucred->mu_suppgids[1] = -1;
125
126         RETURN(0);
127 }
128
129 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
130                           void *buf)
131 {
132         struct ptlrpc_request   *req = mdt_info_req(info);
133         struct mdt_device       *mdt = info->mti_mdt;
134         struct ptlrpc_user_desc *pud = req->rq_user_desc;
135         struct md_ucred         *ucred = mdt_ucred(info);
136         lnet_nid_t               peernid = req->rq_peer.nid;
137         __u32                    perm = 0;
138         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
139         int                      setuid;
140         int                      setgid;
141         int                      rc = 0;
142
143         ENTRY;
144
145         LASSERT(req->rq_auth_gss);
146         LASSERT(!req->rq_auth_usr_mdt);
147         LASSERT(req->rq_user_desc);
148
149         ucred->mu_valid = UCRED_INVALID;
150
151         ucred->mu_o_uid   = pud->pud_uid;
152         ucred->mu_o_gid   = pud->pud_gid;
153         ucred->mu_o_fsuid = pud->pud_fsuid;
154         ucred->mu_o_fsgid = pud->pud_fsgid;
155
156         if (type == BODY_INIT) {
157                 struct mdt_body *body = (struct mdt_body *)buf;
158
159                 ucred->mu_suppgids[0] = body->suppgid;
160                 ucred->mu_suppgids[1] = -1;
161         }
162
163         /* sanity check: we expect the uid which client claimed is true */
164         if (remote) {
165                 if (req->rq_auth_mapped_uid == INVALID_UID) {
166                         CDEBUG(D_SEC, "remote user not mapped, deny access!\n");
167                         RETURN(-EACCES);
168                 }
169
170                 if (ptlrpc_user_desc_do_idmap(req, pud))
171                         RETURN(-EACCES);
172
173                 if (req->rq_auth_mapped_uid != pud->pud_uid) {
174                         CDEBUG(D_SEC, "remote client %s: auth/mapped uid %u/%u "
175                                "while client claims %u:%u/%u:%u\n",
176                                libcfs_nid2str(peernid), req->rq_auth_uid,
177                                req->rq_auth_mapped_uid,
178                                pud->pud_uid, pud->pud_gid,
179                                pud->pud_fsuid, pud->pud_fsgid);
180                         RETURN(-EACCES);
181                 }
182         } else {
183                 if (req->rq_auth_uid != pud->pud_uid) {
184                         CDEBUG(D_SEC, "local client %s: auth uid %u "
185                                "while client claims %u:%u/%u:%u\n",
186                                libcfs_nid2str(peernid), req->rq_auth_uid,
187                                pud->pud_uid, pud->pud_gid,
188                                pud->pud_fsuid, pud->pud_fsgid);
189                         RETURN(-EACCES);
190                 }
191         }
192
193         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
194                 if (remote) {
195                         CDEBUG(D_SEC, "remote client must run with identity_get "
196                                "enabled!\n");
197                         RETURN(-EACCES);
198                 } else {
199                         ucred->mu_identity = NULL;
200                         perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
201                                CFS_SETGRP_PERM;
202                 }
203         } else {
204                 struct md_identity *identity;
205
206                 identity = mdt_identity_get(mdt->mdt_identity_cache,
207                                             pud->pud_uid);
208                 if (IS_ERR(identity)) {
209                         if (unlikely(PTR_ERR(identity) == -EREMCHG &&
210                                      !remote)) {
211                                 ucred->mu_identity = NULL;
212                                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
213                                        CFS_SETGRP_PERM;
214                         } else {
215                                 CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
216                                        pud->pud_uid);
217                                 RETURN(-EACCES);
218                         }
219                 } else {
220                         ucred->mu_identity = identity;
221                         perm = mdt_identity_get_perm(ucred->mu_identity,
222                                                      remote, peernid);
223                 }
224         }
225
226         /* find out the setuid/setgid attempt */
227         setuid = (pud->pud_uid != pud->pud_fsuid);
228         setgid = ((pud->pud_gid != pud->pud_fsgid) ||
229                   (ucred->mu_identity &&
230                   (pud->pud_gid != ucred->mu_identity->mi_gid)));
231
232         /* check permission of setuid */
233         if (setuid && !(perm & CFS_SETUID_PERM)) {
234                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
235                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
236                 GOTO(out, rc = -EACCES);
237         }
238
239         /* check permission of setgid */
240         if (setgid && !(perm & CFS_SETGID_PERM)) {
241                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
242                        "from %s\n", pud->pud_uid, pud->pud_gid,
243                        pud->pud_fsuid, pud->pud_fsgid,
244                        ucred->mu_identity->mi_gid, libcfs_nid2str(peernid));
245                 GOTO(out, rc = -EACCES);
246         }
247
248         /*
249          * NB: remote client not allowed to setgroups anyway.
250          */
251         if (!remote && perm & CFS_SETGRP_PERM) {
252                 if (pud->pud_ngroups) {
253                         /* setgroups for local client */
254                         ucred->mu_ginfo = cfs_groups_alloc(pud->pud_ngroups);
255                         if (!ucred->mu_ginfo) {
256                                 CERROR("failed to alloc %d groups\n",
257                                        pud->pud_ngroups);
258                                 GOTO(out, rc = -ENOMEM);
259                         }
260
261                         lustre_groups_from_list(ucred->mu_ginfo,
262                                                 pud->pud_groups);
263                         lustre_groups_sort(ucred->mu_ginfo);
264                 } else {
265                         ucred->mu_ginfo = NULL;
266                 }
267         } else {
268                 ucred->mu_suppgids[0] = -1;
269                 ucred->mu_suppgids[1] = -1;
270                 ucred->mu_ginfo = NULL;
271         }
272
273         ucred->mu_uid   = pud->pud_uid;
274         ucred->mu_gid   = pud->pud_gid;
275         ucred->mu_fsuid = pud->pud_fsuid;
276         ucred->mu_fsgid = pud->pud_fsgid;
277
278         /* process root_squash here. */
279         mdt_root_squash(info, peernid);
280
281         /* remove fs privilege for non-root user. */
282         if (ucred->mu_fsuid)
283                 ucred->mu_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
284         else
285                 ucred->mu_cap = pud->pud_cap;
286         if (remote && !(perm & CFS_RMTOWN_PERM))
287                 ucred->mu_cap &= ~(CFS_CAP_SYS_RESOURCE_MASK |
288                                    CFS_CAP_CHOWN_MASK);
289         ucred->mu_valid = UCRED_NEW;
290
291         EXIT;
292
293 out:
294         if (rc) {
295                 if (ucred->mu_ginfo) {
296                         cfs_put_group_info(ucred->mu_ginfo);
297                         ucred->mu_ginfo = NULL;
298                 }
299                 if (ucred->mu_identity) {
300                         mdt_identity_put(mdt->mdt_identity_cache,
301                                          ucred->mu_identity);
302                         ucred->mu_identity = NULL;
303                 }
304         }
305
306         return rc;
307 }
308
309 int mdt_check_ucred(struct mdt_thread_info *info)
310 {
311         struct ptlrpc_request   *req = mdt_info_req(info);
312         struct mdt_device       *mdt = info->mti_mdt;
313         struct ptlrpc_user_desc *pud = req->rq_user_desc;
314         struct md_ucred         *ucred = mdt_ucred(info);
315         struct md_identity      *identity = NULL;
316         lnet_nid_t               peernid = req->rq_peer.nid;
317         __u32                    perm = 0;
318         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
319         int                      setuid;
320         int                      setgid;
321         int                      rc = 0;
322
323         ENTRY;
324
325         if ((ucred->mu_valid == UCRED_OLD) || (ucred->mu_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 md_ucred *uc = mdt_ucred(info);
416         struct mdt_device  *mdt = info->mti_mdt;
417         struct md_identity *identity = NULL;
418
419         ENTRY;
420
421         uc->mu_valid = UCRED_INVALID;
422         uc->mu_o_uid = uc->mu_uid = body->uid;
423         uc->mu_o_gid = uc->mu_gid = body->gid;
424         uc->mu_o_fsuid = uc->mu_fsuid = body->fsuid;
425         uc->mu_o_fsgid = uc->mu_fsgid = body->fsgid;
426         uc->mu_suppgids[0] = body->suppgid;
427         uc->mu_suppgids[1] = -1;
428         uc->mu_ginfo = NULL;
429         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
430                 identity = mdt_identity_get(mdt->mdt_identity_cache,
431                                             uc->mu_fsuid);
432                 if (IS_ERR(identity)) {
433                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
434                                 identity = NULL;
435                         } else {
436                                 CDEBUG(D_SEC, "Deny access without identity: "
437                                        "uid %u\n", uc->mu_fsuid);
438                                 RETURN(-EACCES);
439                         }
440                 }
441         }
442         uc->mu_identity = identity;
443
444         /* process root_squash here. */
445         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
446
447         /* remove fs privilege for non-root user. */
448         if (uc->mu_fsuid)
449                 uc->mu_cap = body->capability & ~CFS_CAP_FS_MASK;
450         else
451                 uc->mu_cap = body->capability;
452         uc->mu_valid = UCRED_OLD;
453
454         RETURN(0);
455 }
456
457 static int old_init_ucred_reint(struct mdt_thread_info *info)
458 {
459         struct md_ucred *uc = mdt_ucred(info);
460         struct mdt_device  *mdt = info->mti_mdt;
461         struct md_identity *identity = NULL;
462
463         ENTRY;
464
465         uc->mu_valid = UCRED_INVALID;
466         uc->mu_o_uid = uc->mu_o_fsuid = uc->mu_uid = uc->mu_fsuid;
467         uc->mu_o_gid = uc->mu_o_fsgid = uc->mu_gid = uc->mu_fsgid;
468         uc->mu_ginfo = NULL;
469         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
470                 identity = mdt_identity_get(mdt->mdt_identity_cache,
471                                             uc->mu_fsuid);
472                 if (IS_ERR(identity)) {
473                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
474                                 identity = NULL;
475                         } else {
476                                 CDEBUG(D_SEC, "Deny access without identity: "
477                                        "uid %u\n", uc->mu_fsuid);
478                                 RETURN(-EACCES);
479                         }
480                 }
481         }
482         uc->mu_identity = identity;
483
484         /* process root_squash here. */
485         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
486
487         /* remove fs privilege for non-root user. */
488         if (uc->mu_fsuid)
489                 uc->mu_cap &= ~CFS_CAP_FS_MASK;
490         uc->mu_valid = UCRED_OLD;
491
492         RETURN(0);
493 }
494
495 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
496 {
497         struct ptlrpc_request *req = mdt_info_req(info);
498         struct md_ucred       *uc  = mdt_ucred(info);
499
500         if ((uc->mu_valid == UCRED_OLD) || (uc->mu_valid == UCRED_NEW))
501                 return 0;
502
503         mdt_exit_ucred(info);
504
505         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
506                 return old_init_ucred(info, body);
507         else
508                 return new_init_ucred(info, BODY_INIT, body);
509 }
510
511 int mdt_init_ucred_reint(struct mdt_thread_info *info)
512 {
513         struct ptlrpc_request *req = mdt_info_req(info);
514         struct md_ucred       *uc  = mdt_ucred(info);
515
516         if ((uc->mu_valid == UCRED_OLD) || (uc->mu_valid == UCRED_NEW))
517                 return 0;
518
519         mdt_exit_ucred(info);
520
521         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
522                 return old_init_ucred_reint(info);
523         else
524                 return new_init_ucred(info, REC_INIT, NULL);
525 }
526
527 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
528 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm)
529 {
530         const struct lov_ost_data_v1 *lod;
531         int i;
532         __s16 stripe_count =
533                 le16_to_cpu(((struct lov_user_md*)lmm)->lmm_stripe_count);
534
535         CDEBUG(level, "objid "LPX64", magic 0x%08X, pattern %#X\n",
536                le64_to_cpu(lmm->lmm_object_id), le32_to_cpu(lmm->lmm_magic),
537                le32_to_cpu(lmm->lmm_pattern));
538         CDEBUG(level,"stripe_size=0x%x, stripe_count=0x%x\n",
539                le32_to_cpu(lmm->lmm_stripe_size),
540                le32_to_cpu(lmm->lmm_stripe_count));
541         LASSERT(stripe_count <= (__s16)LOV_MAX_STRIPE_COUNT);
542         for (i = 0, lod = lmm->lmm_objects; i < stripe_count; i++, lod++) {
543                 CDEBUG(level, "stripe %u idx %u subobj "LPX64"/"LPX64"\n",
544                        i, le32_to_cpu(lod->l_ost_idx),
545                        le64_to_cpu(lod->l_object_gr),
546                        le64_to_cpu(lod->l_object_id));
547         }
548 }
549
550 void mdt_shrink_reply(struct mdt_thread_info *info)
551 {
552         struct req_capsule *pill = info->mti_pill;
553         struct mdt_body    *body;
554         int                md_size;
555         int                acl_size;
556         ENTRY;
557
558         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
559         LASSERT(body != NULL);
560
561         if (body->valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE | OBD_MD_LINKNAME))
562                 md_size = body->eadatasize;
563         else
564                 md_size = 0;
565
566         acl_size = body->aclsize;
567
568         /* this replay - not send info to client */
569         if (info->mti_spec.no_create == 1) {
570                 md_size = 0;
571                 acl_size = 0;
572         }
573
574         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d"
575                         " MDSCAPA = "LPX64", OSSCAPA = "LPX64"\n",
576                         md_size, acl_size,
577                         body->valid & OBD_MD_FLMDSCAPA,
578                         body->valid & OBD_MD_FLOSSCAPA);
579 /*
580             &RMF_MDT_BODY,
581             &RMF_MDT_MD,
582             &RMF_ACL, or &RMF_LOGCOOKIES
583 (optional)  &RMF_CAPA1,
584 (optional)  &RMF_CAPA2,
585 (optional)  something else
586 */
587
588         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
589                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size,
590                                    RCL_SERVER);
591         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
592                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
593         else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
594                 req_capsule_shrink(pill, &RMF_LOGCOOKIES,
595                                    acl_size, RCL_SERVER);
596
597         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
598             !(body->valid & OBD_MD_FLMDSCAPA))
599                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
600
601         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
602             !(body->valid & OBD_MD_FLOSSCAPA))
603                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
604
605         /*
606          * Some more field should be shrinked if needed.
607          * This should be done by those who added fields to reply message.
608          */
609         EXIT;
610 }
611
612
613 /* if object is dying, pack the lov/llog data,
614  * parameter info->mti_attr should be valid at this point! */
615 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
616                            const struct md_attr *ma)
617 {
618         struct mdt_body       *repbody;
619         const struct lu_attr *la = &ma->ma_attr;
620         int rc;
621         ENTRY;
622
623         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
624         LASSERT(repbody != NULL);
625
626         if (ma->ma_valid & MA_INODE)
627                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
628
629         if (ma->ma_valid & MA_LOV) {
630                 __u32 mode;
631
632                 if (mdt_object_exists(mo) < 0)
633                         /* If it is a remote object, and we do not retrieve
634                          * EA back unlink reg file*/
635                         mode = S_IFREG;
636                 else
637                         mode = lu_object_attr(&mo->mot_obj.mo_lu);
638
639                 LASSERT(ma->ma_lmm_size);
640                 mdt_dump_lmm(D_INFO, ma->ma_lmm);
641                 repbody->eadatasize = ma->ma_lmm_size;
642                 if (S_ISREG(mode))
643                         repbody->valid |= OBD_MD_FLEASIZE;
644                 else if (S_ISDIR(mode))
645                         repbody->valid |= OBD_MD_FLDIREA;
646                 else
647                         LBUG();
648         }
649
650         if (ma->ma_cookie_size && (ma->ma_valid & MA_COOKIE)) {
651                 repbody->aclsize = ma->ma_cookie_size;
652                 repbody->valid |= OBD_MD_FLCOOKIE;
653         }
654
655         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
656             info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
657             repbody->valid & OBD_MD_FLEASIZE) {
658                 struct lustre_capa *capa;
659
660                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
661                 LASSERT(capa);
662                 capa->lc_opc = CAPA_OPC_OSS_DESTROY;
663                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
664                 if (rc)
665                         RETURN(rc);
666
667                 repbody->valid |= OBD_MD_FLOSSCAPA;
668         }
669
670         RETURN(0);
671 }
672
673 static inline unsigned int attr_unpack(__u64 sa_valid) {
674         unsigned int ia_valid = 0;
675
676         if (sa_valid & MDS_ATTR_MODE)
677                 ia_valid |= ATTR_MODE;
678         if (sa_valid & MDS_ATTR_UID)
679                 ia_valid |= ATTR_UID;
680         if (sa_valid & MDS_ATTR_GID)
681                 ia_valid |= ATTR_GID;
682         if (sa_valid & MDS_ATTR_SIZE)
683                 ia_valid |= ATTR_SIZE;
684         if (sa_valid & MDS_ATTR_ATIME)
685                 ia_valid |= ATTR_ATIME;
686         if (sa_valid & MDS_ATTR_MTIME)
687                 ia_valid |= ATTR_MTIME;
688         if (sa_valid & MDS_ATTR_CTIME)
689                 ia_valid |= ATTR_CTIME;
690         if (sa_valid & MDS_ATTR_ATIME_SET)
691                 ia_valid |= ATTR_ATIME_SET;
692         if (sa_valid & MDS_ATTR_MTIME_SET)
693                 ia_valid |= ATTR_MTIME_SET;
694         if (sa_valid & MDS_ATTR_FORCE)
695                 ia_valid |= ATTR_FORCE;
696         if (sa_valid & MDS_ATTR_ATTR_FLAG)
697                 ia_valid |= ATTR_ATTR_FLAG;
698         if (sa_valid & MDS_ATTR_KILL_SUID)
699                 ia_valid |=  ATTR_KILL_SUID;
700         if (sa_valid & MDS_ATTR_KILL_SGID)
701                 ia_valid |= ATTR_KILL_SGID;
702         if (sa_valid & MDS_ATTR_CTIME_SET)
703                 ia_valid |= ATTR_CTIME_SET;
704         if (sa_valid & MDS_ATTR_FROM_OPEN)
705                 ia_valid |= ATTR_FROM_OPEN;
706         if (sa_valid & MDS_ATTR_BLOCKS)
707                 ia_valid |= ATTR_BLOCKS;
708         if (sa_valid & MDS_OPEN_OWNEROVERRIDE)
709                 ia_valid |= MDS_OPEN_OWNEROVERRIDE;
710         return ia_valid;
711 }
712
713 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
714                                   struct md_attr *ma)
715 {
716         __u64 out;
717
718         out = 0;
719         if (in & ATTR_MODE)
720                 out |= LA_MODE;
721         if (in & ATTR_UID)
722                 out |= LA_UID;
723         if (in & ATTR_GID)
724                 out |= LA_GID;
725         if (in & ATTR_SIZE)
726                 out |= LA_SIZE;
727         if (in & ATTR_BLOCKS)
728                 out |= LA_BLOCKS;
729
730         if (in & ATTR_FROM_OPEN)
731                 rr->rr_flags |= MRF_SETATTR_LOCKED;
732
733         if (in & ATTR_ATIME_SET)
734                 out |= LA_ATIME;
735
736         if (in & ATTR_CTIME_SET)
737                 out |= LA_CTIME;
738
739         if (in & ATTR_MTIME_SET)
740                 out |= LA_MTIME;
741
742         if (in & ATTR_ATTR_FLAG)
743                 out |= LA_FLAGS;
744
745         if (in & MDS_OPEN_OWNEROVERRIDE)
746                 ma->ma_attr_flags |= MDS_OPEN_OWNEROVERRIDE;
747
748         if (in & (ATTR_KILL_SUID|ATTR_KILL_SGID))
749                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
750
751         /*XXX need ATTR_RAW?*/
752         in &= ~(ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_BLOCKS|
753                 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME|ATTR_FROM_OPEN|
754                 ATTR_ATIME_SET|ATTR_CTIME_SET|ATTR_MTIME_SET|
755                 ATTR_ATTR_FLAG|ATTR_RAW|MDS_OPEN_OWNEROVERRIDE|
756                 ATTR_FORCE|ATTR_KILL_SUID|ATTR_KILL_SGID);
757         if (in != 0)
758                 CERROR("Unknown attr bits: %#llx\n", in);
759         return out;
760 }
761 /* unpacking */
762
763 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
764 {
765         struct md_ucred         *uc  = mdt_ucred(info);
766         struct md_attr          *ma = &info->mti_attr;
767         struct lu_attr          *la = &ma->ma_attr;
768         struct req_capsule      *pill = info->mti_pill;
769         struct mdt_reint_record *rr = &info->mti_rr;
770         struct mdt_rec_setattr  *rec;
771         ENTRY;
772
773         CLASSERT(sizeof(struct mdt_rec_setattr)== sizeof(struct mdt_rec_reint));
774         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
775         if (rec == NULL)
776                 RETURN(-EFAULT);
777
778         uc->mu_fsuid = rec->sa_fsuid;
779         uc->mu_fsgid = rec->sa_fsgid;
780         uc->mu_cap   = rec->sa_cap;
781         uc->mu_suppgids[0] = rec->sa_suppgid;
782         uc->mu_suppgids[1] = -1;
783
784         rr->rr_fid1 = &rec->sa_fid;
785         la->la_valid = mdt_attr_valid_xlate(attr_unpack(rec->sa_valid), rr, ma);
786         la->la_mode  = rec->sa_mode;
787         la->la_flags = rec->sa_attr_flags;
788         la->la_uid   = rec->sa_uid;
789         la->la_gid   = rec->sa_gid;
790         la->la_size  = rec->sa_size;
791         la->la_blocks = rec->sa_blocks;
792         la->la_ctime = rec->sa_ctime;
793         la->la_atime = rec->sa_atime;
794         la->la_mtime = rec->sa_mtime;
795         ma->ma_valid = MA_INODE;
796
797         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
798                 mdt_set_capainfo(info, 0, rr->rr_fid1,
799                                  req_capsule_client_get(pill, &RMF_CAPA1));
800
801         RETURN(0);
802 }
803
804 static int mdt_ioepoch_unpack(struct mdt_thread_info *info)
805 {
806         struct req_capsule *pill = info->mti_pill;
807         ENTRY;
808
809         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
810                 info->mti_ioepoch =
811                         req_capsule_client_get(pill, &RMF_MDT_EPOCH);
812         else
813                 info->mti_ioepoch = NULL;
814         RETURN(info->mti_ioepoch == NULL ? -EFAULT : 0);
815 }
816
817 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
818         struct req_capsule      *pill = info->mti_pill;
819
820         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
821                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
822                 if (info->mti_dlm_req == NULL)
823                         RETURN(-EFAULT);
824         }
825
826         RETURN(0);
827 }
828
829 static int mdt_setattr_unpack(struct mdt_thread_info *info)
830 {
831         struct md_attr          *ma = &info->mti_attr;
832         struct req_capsule      *pill = info->mti_pill;
833         int rc;
834         ENTRY;
835
836         rc = mdt_setattr_unpack_rec(info);
837         if (rc)
838                 RETURN(rc);
839
840         /* Epoch may be absent */
841         mdt_ioepoch_unpack(info);
842
843         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
844         if (ma->ma_lmm_size) {
845                 ma->ma_lmm = req_capsule_client_get(pill, &RMF_EADATA);
846                 ma->ma_valid |= MA_LOV;
847         }
848
849         ma->ma_cookie_size = req_capsule_get_size(pill, &RMF_LOGCOOKIES,
850                                                   RCL_CLIENT);
851         if (ma->ma_cookie_size) {
852                 ma->ma_cookie = req_capsule_client_get(pill, &RMF_LOGCOOKIES);
853                 ma->ma_valid |= MA_COOKIE;
854         }
855
856         rc = mdt_dlmreq_unpack(info);
857         RETURN(rc);
858 }
859
860 int mdt_close_unpack(struct mdt_thread_info *info)
861 {
862         int rc;
863         ENTRY;
864
865         rc = mdt_ioepoch_unpack(info);
866         if (rc)
867                 RETURN(rc);
868
869         RETURN(mdt_setattr_unpack_rec(info));
870 }
871
872 static int mdt_create_unpack(struct mdt_thread_info *info)
873 {
874         struct md_ucred         *uc  = mdt_ucred(info);
875         struct mdt_rec_create   *rec;
876         struct lu_attr          *attr = &info->mti_attr.ma_attr;
877         struct mdt_reint_record *rr = &info->mti_rr;
878         struct req_capsule      *pill = info->mti_pill;
879         struct md_op_spec       *sp = &info->mti_spec;
880         int rc;
881         ENTRY;
882
883         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
884         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
885         if (rec == NULL)
886                 RETURN(-EFAULT);
887
888         uc->mu_fsuid = rec->cr_fsuid;
889         uc->mu_fsgid = rec->cr_fsgid;
890         uc->mu_cap   = rec->cr_cap;
891         uc->mu_suppgids[0] = rec->cr_suppgid1;
892         uc->mu_suppgids[1] = -1;
893
894         rr->rr_fid1 = &rec->cr_fid1;
895         rr->rr_fid2 = &rec->cr_fid2;
896         attr->la_mode = rec->cr_mode;
897         attr->la_rdev  = rec->cr_rdev;
898         attr->la_uid   = rec->cr_fsuid;
899         attr->la_gid   = rec->cr_fsgid;
900         attr->la_ctime = rec->cr_time;
901         attr->la_mtime = rec->cr_time;
902         attr->la_atime = rec->cr_time;
903         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
904                          LA_CTIME | LA_MTIME | LA_ATIME;
905         memset(&sp->u, 0, sizeof(sp->u));
906         sp->sp_cr_flags = rec->cr_flags;
907         sp->sp_ck_split = !!(rec->cr_bias & MDS_CHECK_SPLIT);
908         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
909
910         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
911                 mdt_set_capainfo(info, 0, rr->rr_fid1,
912                                  req_capsule_client_get(pill, &RMF_CAPA1));
913         mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
914
915         if (!info->mti_cross_ref) {
916                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
917                 rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
918                 LASSERT(rr->rr_name && rr->rr_namelen > 0);
919         } else {
920                 rr->rr_name = NULL;
921                 rr->rr_namelen = 0;
922         }
923
924 #ifdef CONFIG_FS_POSIX_ACL
925         if (sp->sp_cr_flags & MDS_CREATE_RMT_ACL) {
926                 if (S_ISDIR(attr->la_mode))
927                         sp->u.sp_pfid = rr->rr_fid1;
928                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
929                 LASSERT(req_capsule_field_present(pill, &RMF_EADATA,
930                                                   RCL_CLIENT));
931                 sp->u.sp_ea.eadata = req_capsule_client_get(pill, &RMF_EADATA);
932                 sp->u.sp_ea.eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
933                                                              RCL_CLIENT);
934                 sp->u.sp_ea.fid = rr->rr_fid1;
935                 RETURN(0);
936         }
937 #endif
938         if (S_ISDIR(attr->la_mode)) {
939                 /* pass parent fid for cross-ref cases */
940                 sp->u.sp_pfid = rr->rr_fid1;
941                 if (sp->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
942                         /* create salve object req, need
943                          * unpack split ea here
944                          */
945                        req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SLAVE);
946                        LASSERT(req_capsule_field_present(pill, &RMF_EADATA,
947                                                          RCL_CLIENT));
948                        sp->u.sp_ea.eadata = req_capsule_client_get(pill,
949                                                                    &RMF_EADATA);
950                        sp->u.sp_ea.eadatalen = req_capsule_get_size(pill,
951                                                                     &RMF_EADATA,
952                                                                     RCL_CLIENT);
953                        sp->u.sp_ea.fid = rr->rr_fid1;
954                        RETURN(0);
955                 }
956                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
957         } else if (S_ISLNK(attr->la_mode)) {
958                 const char *tgt = NULL;
959
960                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
961                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
962                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
963                         sp->u.sp_symname = tgt;
964                 }
965                 if (tgt == NULL)
966                         RETURN(-EFAULT);
967         } else {
968                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
969         }
970         rc = mdt_dlmreq_unpack(info);
971         RETURN(rc);
972 }
973
974 static int mdt_link_unpack(struct mdt_thread_info *info)
975 {
976         struct md_ucred         *uc  = mdt_ucred(info);
977         struct mdt_rec_link     *rec;
978         struct lu_attr          *attr = &info->mti_attr.ma_attr;
979         struct mdt_reint_record *rr = &info->mti_rr;
980         struct req_capsule      *pill = info->mti_pill;
981         int rc;
982         ENTRY;
983
984         CLASSERT(sizeof(struct mdt_rec_link) == sizeof(struct mdt_rec_reint));
985         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
986         if (rec == NULL)
987                 RETURN(-EFAULT);
988
989         uc->mu_fsuid = rec->lk_fsuid;
990         uc->mu_fsgid = rec->lk_fsgid;
991         uc->mu_cap   = rec->lk_cap;
992         uc->mu_suppgids[0] = rec->lk_suppgid1;
993         uc->mu_suppgids[1] = rec->lk_suppgid2;
994
995         attr->la_uid = rec->lk_fsuid;
996         attr->la_gid = rec->lk_fsgid;
997         rr->rr_fid1 = &rec->lk_fid1;
998         rr->rr_fid2 = &rec->lk_fid2;
999         attr->la_ctime = rec->lk_time;
1000         attr->la_mtime = rec->lk_time;
1001         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1002
1003         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1004                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1005                                  req_capsule_client_get(pill, &RMF_CAPA1));
1006         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1007                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1008                                  req_capsule_client_get(pill, &RMF_CAPA2));
1009
1010         info->mti_spec.sp_ck_split = !!(rec->lk_bias & MDS_CHECK_SPLIT);
1011         info->mti_cross_ref = !!(rec->lk_bias & MDS_CROSS_REF);
1012         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1013         if (rr->rr_name == NULL)
1014                 RETURN(-EFAULT);
1015         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1016         if (!info->mti_cross_ref)
1017                 LASSERT(rr->rr_namelen > 0);
1018
1019         rc = mdt_dlmreq_unpack(info);
1020         RETURN(rc);
1021 }
1022
1023 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1024 {
1025         struct md_ucred         *uc  = mdt_ucred(info);
1026         struct mdt_rec_unlink   *rec;
1027         struct md_attr          *ma = &info->mti_attr;
1028         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1029         struct mdt_reint_record *rr = &info->mti_rr;
1030         struct req_capsule      *pill = info->mti_pill;
1031         int rc;
1032         ENTRY;
1033
1034         CLASSERT(sizeof(struct mdt_rec_unlink) == sizeof(struct mdt_rec_reint));
1035         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1036         if (rec == NULL)
1037                 RETURN(-EFAULT);
1038
1039         uc->mu_fsuid = rec->ul_fsuid;
1040         uc->mu_fsgid = rec->ul_fsgid;
1041         uc->mu_cap   = rec->ul_cap;
1042         uc->mu_suppgids[0] = rec->ul_suppgid1;
1043         uc->mu_suppgids[1] = -1;
1044
1045         attr->la_uid = rec->ul_fsuid;
1046         attr->la_gid = rec->ul_fsgid;
1047         rr->rr_fid1 = &rec->ul_fid1;
1048         rr->rr_fid2 = &rec->ul_fid2;
1049         attr->la_ctime = rec->ul_time;
1050         attr->la_mtime = rec->ul_time;
1051         attr->la_mode  = rec->ul_mode;
1052         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1053
1054         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1055                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1056                                  req_capsule_client_get(pill, &RMF_CAPA1));
1057
1058         info->mti_cross_ref = !!(rec->ul_bias & MDS_CROSS_REF);
1059         if (!info->mti_cross_ref) {
1060                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1061                 rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1062                 if (rr->rr_name == NULL || rr->rr_namelen == 0)
1063                         RETURN(-EFAULT);
1064         } else {
1065                 rr->rr_name = NULL;
1066                 rr->rr_namelen = 0;
1067         }
1068         info->mti_spec.sp_ck_split = !!(rec->ul_bias & MDS_CHECK_SPLIT);
1069         if (rec->ul_bias & MDS_VTX_BYPASS)
1070                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1071         else
1072                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1073
1074         if (lustre_msg_get_flags(mdt_info_req(info)->rq_reqmsg) & MSG_REPLAY)
1075                 info->mti_spec.no_create = 1;
1076
1077         rc = mdt_dlmreq_unpack(info);
1078         RETURN(rc);
1079 }
1080
1081 static int mdt_rename_unpack(struct mdt_thread_info *info)
1082 {
1083         struct md_ucred         *uc = mdt_ucred(info);
1084         struct mdt_rec_rename   *rec;
1085         struct md_attr          *ma = &info->mti_attr;
1086         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1087         struct mdt_reint_record *rr = &info->mti_rr;
1088         struct req_capsule      *pill = info->mti_pill;
1089         int rc;
1090         ENTRY;
1091
1092         CLASSERT(sizeof(struct mdt_rec_rename) == sizeof(struct mdt_rec_reint));
1093         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1094         if (rec == NULL)
1095                 RETURN(-EFAULT);
1096
1097         uc->mu_fsuid = rec->rn_fsuid;
1098         uc->mu_fsgid = rec->rn_fsgid;
1099         uc->mu_cap   = rec->rn_cap;
1100         uc->mu_suppgids[0] = rec->rn_suppgid1;
1101         uc->mu_suppgids[1] = rec->rn_suppgid2;
1102
1103         attr->la_uid = rec->rn_fsuid;
1104         attr->la_gid = rec->rn_fsgid;
1105         rr->rr_fid1 = &rec->rn_fid1;
1106         rr->rr_fid2 = &rec->rn_fid2;
1107         attr->la_ctime = rec->rn_time;
1108         attr->la_mtime = rec->rn_time;
1109         /* rename_tgt contains the mode already */
1110         attr->la_mode = rec->rn_mode;
1111         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1112
1113         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1114                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1115                                  req_capsule_client_get(pill, &RMF_CAPA1));
1116         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1117                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1118                                  req_capsule_client_get(pill, &RMF_CAPA2));
1119
1120         info->mti_spec.sp_ck_split = !!(rec->rn_bias & MDS_CHECK_SPLIT);
1121         info->mti_cross_ref = !!(rec->rn_bias & MDS_CROSS_REF);
1122         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1123         rr->rr_tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1124         if (rr->rr_name == NULL || rr->rr_tgt == NULL)
1125                 RETURN(-EFAULT);
1126         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1127         rr->rr_tgtlen = req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT) - 1;
1128         if (!info->mti_cross_ref)
1129                 LASSERT(rr->rr_namelen > 0 && rr->rr_tgtlen > 0);
1130         if (rec->rn_bias & MDS_VTX_BYPASS)
1131                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1132         else
1133                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1134
1135         if (lustre_msg_get_flags(mdt_info_req(info)->rq_reqmsg) & MSG_REPLAY)
1136                 info->mti_spec.no_create = 1;
1137
1138         rc = mdt_dlmreq_unpack(info);
1139         RETURN(rc);
1140 }
1141
1142 static int mdt_open_unpack(struct mdt_thread_info *info)
1143 {
1144         struct md_ucred         *uc = mdt_ucred(info);
1145         struct mdt_rec_create   *rec;
1146         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1147         struct req_capsule      *pill = info->mti_pill;
1148         struct mdt_reint_record *rr   = &info->mti_rr;
1149         struct ptlrpc_request   *req  = mdt_info_req(info);
1150         struct md_op_spec       *sp   = &info->mti_spec;
1151         ENTRY;
1152
1153         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1154         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1155         if (rec == NULL)
1156                 RETURN(-EFAULT);
1157
1158         uc->mu_fsuid = rec->cr_fsuid;
1159         uc->mu_fsgid = rec->cr_fsgid;
1160         uc->mu_cap   = rec->cr_cap;
1161         uc->mu_suppgids[0] = rec->cr_suppgid1;
1162         uc->mu_suppgids[1] = rec->cr_suppgid2;
1163
1164         rr->rr_fid1   = &rec->cr_fid1;
1165         rr->rr_fid2   = &rec->cr_fid2;
1166         rr->rr_handle = &rec->cr_old_handle;
1167         attr->la_mode = rec->cr_mode;
1168         attr->la_rdev  = rec->cr_rdev;
1169         attr->la_uid   = rec->cr_fsuid;
1170         attr->la_gid   = rec->cr_fsgid;
1171         attr->la_ctime = rec->cr_time;
1172         attr->la_mtime = rec->cr_time;
1173         attr->la_atime = rec->cr_time;
1174         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1175                          LA_CTIME | LA_MTIME | LA_ATIME;
1176         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1177         info->mti_spec.sp_cr_flags = rec->cr_flags;
1178         info->mti_replayepoch = rec->cr_ioepoch;
1179
1180         info->mti_spec.sp_ck_split = !!(rec->cr_bias & MDS_CHECK_SPLIT);
1181         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1182
1183         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1184                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1185                                  req_capsule_client_get(pill, &RMF_CAPA1));
1186         if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
1187             (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))) {
1188 #if 0
1189                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1190                                  req_capsule_client_get(pill, &RMF_CAPA2));
1191 #else
1192                 /*
1193                  * FIXME: capa in replay open request might have expired,
1194                  * bypass capa check. Security hole?
1195                  */
1196                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1197                 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1198 #endif
1199         }
1200
1201         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1202         if (rr->rr_name == NULL)
1203                 RETURN(-EFAULT);
1204         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1205
1206         sp->u.sp_ea.eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1207                                                      RCL_CLIENT);
1208         if (sp->u.sp_ea.eadatalen) {
1209                 sp->u.sp_ea.eadata = req_capsule_client_get(pill, &RMF_EADATA);
1210                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1211                         sp->no_create = 1;
1212         }
1213
1214         RETURN(0);
1215 }
1216
1217 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1218 {
1219         struct mdt_reint_record   *rr   = &info->mti_rr;
1220         struct md_ucred           *uc   = mdt_ucred(info);
1221         struct lu_attr            *attr = &info->mti_attr.ma_attr;
1222         struct req_capsule        *pill = info->mti_pill;
1223         struct mdt_rec_setxattr   *rec;
1224         ENTRY;
1225
1226
1227         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1228                          sizeof(struct mdt_rec_reint));
1229
1230         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1231         if (rec == NULL)
1232                 RETURN(-EFAULT);
1233
1234         uc->mu_fsuid  = rec->sx_fsuid;
1235         uc->mu_fsgid  = rec->sx_fsgid;
1236         uc->mu_cap    = rec->sx_cap;
1237         uc->mu_suppgids[0] = rec->sx_suppgid1;
1238         uc->mu_suppgids[1] = -1;
1239
1240         rr->rr_opcode = rec->sx_opcode;
1241         rr->rr_fid1   = &rec->sx_fid;
1242         attr->la_valid = rec->sx_valid;
1243         attr->la_ctime = rec->sx_time;
1244         attr->la_size = rec->sx_size;
1245         attr->la_flags = rec->sx_flags;
1246
1247         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1248                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1249                                  req_capsule_client_get(pill, &RMF_CAPA1));
1250         else
1251                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1252
1253         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1254         if (rr->rr_name == NULL)
1255                 RETURN(-EFAULT);
1256         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1257         LASSERT(rr->rr_namelen > 0);
1258
1259         rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
1260         if (rr->rr_eadatalen > 0) {
1261                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1262                 if (rr->rr_eadata == NULL)
1263                         RETURN(-EFAULT);
1264         }
1265
1266         RETURN(0);
1267 }
1268
1269
1270 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1271
1272 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1273         [REINT_SETATTR]  = mdt_setattr_unpack,
1274         [REINT_CREATE]   = mdt_create_unpack,
1275         [REINT_LINK]     = mdt_link_unpack,
1276         [REINT_UNLINK]   = mdt_unlink_unpack,
1277         [REINT_RENAME]   = mdt_rename_unpack,
1278         [REINT_OPEN]     = mdt_open_unpack,
1279         [REINT_SETXATTR] = mdt_setxattr_unpack
1280 };
1281
1282 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1283 {
1284         int rc;
1285         ENTRY;
1286
1287         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1288         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1289                 info->mti_rr.rr_opcode = op;
1290                 rc = mdt_reint_unpackers[op](info);
1291         } else {
1292                 CERROR("Unexpected opcode %d\n", op);
1293                 rc = -EFAULT;
1294         }
1295         RETURN(rc);
1296 }