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