Whamcloud - gitweb
bbfc4f16541f6b284d3e849b8c85cc27ba9735e3
[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 /* Shrink and/or grow reply buffers */
554 int mdt_fix_reply(struct mdt_thread_info *info)
555 {
556         struct req_capsule *pill = info->mti_pill;
557         struct mdt_body    *body;
558         int                md_size, md_packed = 0;
559         int                acl_size;
560         int                rc = 0;
561         ENTRY;
562
563         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
564         LASSERT(body != NULL);
565
566         if (body->valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE | OBD_MD_LINKNAME))
567                 md_size = body->eadatasize;
568         else
569                 md_size = 0;
570
571         acl_size = body->aclsize;
572
573         /* this replay - not send info to client */
574         if (info->mti_spec.no_create == 1) {
575                 md_size = 0;
576                 acl_size = 0;
577         }
578
579         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d"
580                         " MDSCAPA = %llx, OSSCAPA = %llx\n",
581                         md_size, acl_size,
582                         (unsigned long long)(body->valid & OBD_MD_FLMDSCAPA),
583                         (unsigned long long)(body->valid & OBD_MD_FLOSSCAPA));
584 /*
585             &RMF_MDT_BODY,
586             &RMF_MDT_MD,
587             &RMF_ACL, or &RMF_LOGCOOKIES
588 (optional)  &RMF_CAPA1,
589 (optional)  &RMF_CAPA2,
590 (optional)  something else
591 */
592
593         /* MDT_MD buffer may be bigger than packed value, let's shrink all
594          * buffers before growing it */
595         if (info->mti_attr.ma_big_lmm_used) {
596                 LASSERT(req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER));
597                 md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
598                                                  RCL_SERVER);
599                 LASSERT(md_packed > 0);
600                 /* buffer must be allocated separately */
601                 LASSERT(info->mti_attr.ma_lmm !=
602                         req_capsule_server_get(pill, &RMF_MDT_MD));
603                 req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
604                 /* free big lmm if md_size is not needed */
605                 if (md_size == 0)
606                         info->mti_attr.ma_big_lmm_used = 0;
607         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
608                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
609         }
610
611         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
612                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
613         else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
614                 req_capsule_shrink(pill, &RMF_LOGCOOKIES,
615                                    acl_size, RCL_SERVER);
616
617         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
618             !(body->valid & OBD_MD_FLMDSCAPA))
619                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
620
621         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
622             !(body->valid & OBD_MD_FLOSSCAPA))
623                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
624
625         /*
626          * Some more field should be shrinked if needed.
627          * This should be done by those who added fields to reply message.
628          */
629
630         /* Grow MD buffer if needed finally */
631         if (info->mti_attr.ma_big_lmm_used) {
632                 void *lmm;
633
634                 LASSERT(md_size > md_packed);
635                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
636                        md_size - md_packed);
637                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
638                 if (rc) {
639                         /* we can't answer with proper LOV EA, drop flags,
640                          * the rc is also returned so this request is
641                          * considered as failed */
642                         body->valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
643                         /* don't return transno along with error */
644                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
645                 } else {
646                         /* now we need to pack right LOV EA */
647                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
648                         LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
649                                                      RCL_SERVER) ==
650                                 info->mti_attr.ma_lmm_size);
651                         memcpy(lmm, info->mti_attr.ma_lmm,
652                                info->mti_attr.ma_lmm_size);
653                 }
654                 /* update mdt_max_mdsize so clients will be aware about that */
655                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
656                         info->mti_mdt->mdt_max_mdsize =
657                                                     info->mti_attr.ma_lmm_size;
658                 info->mti_attr.ma_big_lmm_used = 0;
659         }
660         RETURN(rc);
661 }
662
663
664 /* if object is dying, pack the lov/llog data,
665  * parameter info->mti_attr should be valid at this point! */
666 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
667                            const struct md_attr *ma)
668 {
669         struct mdt_body       *repbody;
670         const struct lu_attr *la = &ma->ma_attr;
671         int rc;
672         ENTRY;
673
674         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
675         LASSERT(repbody != NULL);
676
677         if (ma->ma_valid & MA_INODE)
678                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
679
680         if (ma->ma_valid & MA_LOV) {
681                 __u32 mode;
682
683                 if (mdt_object_exists(mo) < 0)
684                         /* If it is a remote object, and we do not retrieve
685                          * EA back unlink reg file*/
686                         mode = S_IFREG;
687                 else
688                         mode = lu_object_attr(&mo->mot_obj.mo_lu);
689
690                 LASSERT(ma->ma_lmm_size);
691                 mdt_dump_lmm(D_INFO, ma->ma_lmm);
692                 repbody->eadatasize = ma->ma_lmm_size;
693                 if (S_ISREG(mode))
694                         repbody->valid |= OBD_MD_FLEASIZE;
695                 else if (S_ISDIR(mode))
696                         repbody->valid |= OBD_MD_FLDIREA;
697                 else
698                         LBUG();
699         }
700
701         if (ma->ma_cookie_size && (ma->ma_valid & MA_COOKIE)) {
702                 repbody->aclsize = ma->ma_cookie_size;
703                 repbody->valid |= OBD_MD_FLCOOKIE;
704         }
705
706         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
707             info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
708             repbody->valid & OBD_MD_FLEASIZE) {
709                 struct lustre_capa *capa;
710
711                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
712                 LASSERT(capa);
713                 capa->lc_opc = CAPA_OPC_OSS_DESTROY;
714                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
715                 if (rc)
716                         RETURN(rc);
717
718                 repbody->valid |= OBD_MD_FLOSSCAPA;
719         }
720
721         RETURN(0);
722 }
723
724 static inline unsigned int attr_unpack(__u64 sa_valid) {
725         unsigned int ia_valid = 0;
726
727         if (sa_valid & MDS_ATTR_MODE)
728                 ia_valid |= ATTR_MODE;
729         if (sa_valid & MDS_ATTR_UID)
730                 ia_valid |= ATTR_UID;
731         if (sa_valid & MDS_ATTR_GID)
732                 ia_valid |= ATTR_GID;
733         if (sa_valid & MDS_ATTR_SIZE)
734                 ia_valid |= ATTR_SIZE;
735         if (sa_valid & MDS_ATTR_ATIME)
736                 ia_valid |= ATTR_ATIME;
737         if (sa_valid & MDS_ATTR_MTIME)
738                 ia_valid |= ATTR_MTIME;
739         if (sa_valid & MDS_ATTR_CTIME)
740                 ia_valid |= ATTR_CTIME;
741         if (sa_valid & MDS_ATTR_ATIME_SET)
742                 ia_valid |= ATTR_ATIME_SET;
743         if (sa_valid & MDS_ATTR_MTIME_SET)
744                 ia_valid |= ATTR_MTIME_SET;
745         if (sa_valid & MDS_ATTR_FORCE)
746                 ia_valid |= ATTR_FORCE;
747         if (sa_valid & MDS_ATTR_ATTR_FLAG)
748                 ia_valid |= ATTR_ATTR_FLAG;
749         if (sa_valid & MDS_ATTR_KILL_SUID)
750                 ia_valid |=  ATTR_KILL_SUID;
751         if (sa_valid & MDS_ATTR_KILL_SGID)
752                 ia_valid |= ATTR_KILL_SGID;
753         if (sa_valid & MDS_ATTR_CTIME_SET)
754                 ia_valid |= ATTR_CTIME_SET;
755         if (sa_valid & MDS_ATTR_FROM_OPEN)
756                 ia_valid |= ATTR_FROM_OPEN;
757         if (sa_valid & MDS_ATTR_BLOCKS)
758                 ia_valid |= ATTR_BLOCKS;
759         if (sa_valid & MDS_OPEN_OWNEROVERRIDE)
760                 ia_valid |= MDS_OPEN_OWNEROVERRIDE;
761         return ia_valid;
762 }
763
764 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
765                                   struct md_attr *ma)
766 {
767         __u64 out;
768
769         out = 0;
770         if (in & ATTR_MODE)
771                 out |= LA_MODE;
772         if (in & ATTR_UID)
773                 out |= LA_UID;
774         if (in & ATTR_GID)
775                 out |= LA_GID;
776         if (in & ATTR_SIZE)
777                 out |= LA_SIZE;
778         if (in & ATTR_BLOCKS)
779                 out |= LA_BLOCKS;
780
781         if (in & ATTR_FROM_OPEN)
782                 rr->rr_flags |= MRF_OPEN_TRUNC;
783
784         if (in & ATTR_ATIME_SET)
785                 out |= LA_ATIME;
786
787         if (in & ATTR_CTIME_SET)
788                 out |= LA_CTIME;
789
790         if (in & ATTR_MTIME_SET)
791                 out |= LA_MTIME;
792
793         if (in & ATTR_ATTR_FLAG)
794                 out |= LA_FLAGS;
795
796         if (in & ATTR_KILL_SUID)
797                 out |= LA_KILL_SUID;
798
799         if (in & ATTR_KILL_SGID)
800                 out |= LA_KILL_SGID;
801
802         if (in & MDS_OPEN_OWNEROVERRIDE)
803                 ma->ma_attr_flags |= MDS_OPEN_OWNEROVERRIDE;
804
805         if (in & ATTR_FORCE)
806                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
807
808         /*XXX need ATTR_RAW?*/
809         in &= ~(ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_BLOCKS|
810                 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME|ATTR_FROM_OPEN|
811                 ATTR_ATIME_SET|ATTR_CTIME_SET|ATTR_MTIME_SET|
812                 ATTR_ATTR_FLAG|ATTR_RAW|MDS_OPEN_OWNEROVERRIDE|
813                 ATTR_FORCE|ATTR_KILL_SUID|ATTR_KILL_SGID);
814         if (in != 0)
815                 CERROR("Unknown attr bits: "LPX64"\n", in);
816         return out;
817 }
818 /* unpacking */
819
820 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
821 {
822         struct md_ucred         *uc  = mdt_ucred(info);
823         struct md_attr          *ma = &info->mti_attr;
824         struct lu_attr          *la = &ma->ma_attr;
825         struct req_capsule      *pill = info->mti_pill;
826         struct mdt_reint_record *rr = &info->mti_rr;
827         struct mdt_rec_setattr  *rec;
828         ENTRY;
829
830         CLASSERT(sizeof(struct mdt_rec_setattr)== sizeof(struct mdt_rec_reint));
831         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
832         if (rec == NULL)
833                 RETURN(-EFAULT);
834
835         uc->mu_fsuid = rec->sa_fsuid;
836         uc->mu_fsgid = rec->sa_fsgid;
837         uc->mu_cap   = rec->sa_cap;
838         uc->mu_suppgids[0] = rec->sa_suppgid;
839         uc->mu_suppgids[1] = -1;
840
841         rr->rr_fid1 = &rec->sa_fid;
842         la->la_valid = mdt_attr_valid_xlate(attr_unpack(rec->sa_valid), rr, ma);
843         la->la_mode  = rec->sa_mode;
844         la->la_flags = rec->sa_attr_flags;
845         la->la_uid   = rec->sa_uid;
846         la->la_gid   = rec->sa_gid;
847         la->la_size  = rec->sa_size;
848         la->la_blocks = rec->sa_blocks;
849         la->la_ctime = rec->sa_ctime;
850         la->la_atime = rec->sa_atime;
851         la->la_mtime = rec->sa_mtime;
852         ma->ma_valid = MA_INODE;
853
854         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
855                 mdt_set_capainfo(info, 0, rr->rr_fid1,
856                                  req_capsule_client_get(pill, &RMF_CAPA1));
857
858         RETURN(0);
859 }
860
861 static int mdt_ioepoch_unpack(struct mdt_thread_info *info)
862 {
863         struct req_capsule *pill = info->mti_pill;
864         ENTRY;
865
866         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
867                 info->mti_ioepoch =
868                         req_capsule_client_get(pill, &RMF_MDT_EPOCH);
869         else
870                 info->mti_ioepoch = NULL;
871         RETURN(info->mti_ioepoch == NULL ? -EFAULT : 0);
872 }
873
874 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
875         struct req_capsule      *pill = info->mti_pill;
876
877         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
878                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
879                 if (info->mti_dlm_req == NULL)
880                         RETURN(-EFAULT);
881         }
882
883         RETURN(0);
884 }
885
886 static int mdt_setattr_unpack(struct mdt_thread_info *info)
887 {
888         struct mdt_reint_record *rr = &info->mti_rr;
889         struct md_attr          *ma = &info->mti_attr;
890         struct req_capsule      *pill = info->mti_pill;
891         int rc;
892         ENTRY;
893
894         rc = mdt_setattr_unpack_rec(info);
895         if (rc)
896                 RETURN(rc);
897
898         /* Epoch may be absent */
899         mdt_ioepoch_unpack(info);
900
901         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
902                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
903                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
904                                                         RCL_CLIENT);
905                 ma->ma_lmm_size = rr->rr_eadatalen;
906                 if (ma->ma_lmm_size > 0) {
907                         ma->ma_lmm = (void *)rr->rr_eadata;
908                         ma->ma_valid |= MA_LOV;
909                 }
910         }
911
912         ma->ma_cookie_size = req_capsule_get_size(pill, &RMF_LOGCOOKIES,
913                                                   RCL_CLIENT);
914         if (ma->ma_cookie_size) {
915                 ma->ma_cookie = req_capsule_client_get(pill, &RMF_LOGCOOKIES);
916                 ma->ma_valid |= MA_COOKIE;
917         }
918
919         rc = mdt_dlmreq_unpack(info);
920         RETURN(rc);
921 }
922
923 int mdt_close_unpack(struct mdt_thread_info *info)
924 {
925         int rc;
926         ENTRY;
927
928         rc = mdt_ioepoch_unpack(info);
929         if (rc)
930                 RETURN(rc);
931
932         RETURN(mdt_setattr_unpack_rec(info));
933 }
934
935 static int mdt_create_unpack(struct mdt_thread_info *info)
936 {
937         struct md_ucred         *uc  = mdt_ucred(info);
938         struct mdt_rec_create   *rec;
939         struct lu_attr          *attr = &info->mti_attr.ma_attr;
940         struct mdt_reint_record *rr = &info->mti_rr;
941         struct req_capsule      *pill = info->mti_pill;
942         struct md_op_spec       *sp = &info->mti_spec;
943         int rc;
944         ENTRY;
945
946         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
947         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
948         if (rec == NULL)
949                 RETURN(-EFAULT);
950
951         uc->mu_fsuid = rec->cr_fsuid;
952         uc->mu_fsgid = rec->cr_fsgid;
953         uc->mu_cap   = rec->cr_cap;
954         uc->mu_suppgids[0] = rec->cr_suppgid1;
955         uc->mu_suppgids[1] = -1;
956
957         rr->rr_fid1 = &rec->cr_fid1;
958         rr->rr_fid2 = &rec->cr_fid2;
959         attr->la_mode = rec->cr_mode;
960         attr->la_rdev  = rec->cr_rdev;
961         attr->la_uid   = rec->cr_fsuid;
962         attr->la_gid   = rec->cr_fsgid;
963         attr->la_ctime = rec->cr_time;
964         attr->la_mtime = rec->cr_time;
965         attr->la_atime = rec->cr_time;
966         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
967                          LA_CTIME | LA_MTIME | LA_ATIME;
968         memset(&sp->u, 0, sizeof(sp->u));
969         sp->sp_cr_flags = get_mrc_cr_flags(rec);
970         sp->sp_ck_split = !!(rec->cr_bias & MDS_CHECK_SPLIT);
971         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
972
973         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
974                 mdt_set_capainfo(info, 0, rr->rr_fid1,
975                                  req_capsule_client_get(pill, &RMF_CAPA1));
976         mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
977
978         if (!info->mti_cross_ref) {
979                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
980                 rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME,
981                                                       RCL_CLIENT) - 1;
982                 LASSERT(rr->rr_name && rr->rr_namelen > 0);
983         } else {
984                 rr->rr_name = NULL;
985                 rr->rr_namelen = 0;
986         }
987
988 #ifdef CONFIG_FS_POSIX_ACL
989         if (sp->sp_cr_flags & MDS_CREATE_RMT_ACL) {
990                 if (S_ISDIR(attr->la_mode))
991                         sp->u.sp_pfid = rr->rr_fid1;
992                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
993                 LASSERT(req_capsule_field_present(pill, &RMF_EADATA,
994                                                   RCL_CLIENT));
995                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
996                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
997                                                         RCL_CLIENT);
998                 sp->u.sp_ea.eadata = rr->rr_eadata;
999                 sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1000                 sp->u.sp_ea.fid = rr->rr_fid1;
1001                 RETURN(0);
1002         }
1003 #endif
1004         if (S_ISDIR(attr->la_mode)) {
1005                 /* pass parent fid for cross-ref cases */
1006                 sp->u.sp_pfid = rr->rr_fid1;
1007                 if (sp->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
1008                         /* create salve object req, need
1009                          * unpack split ea here
1010                          */
1011                        req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SLAVE);
1012                        LASSERT(req_capsule_field_present(pill, &RMF_EADATA,
1013                                                          RCL_CLIENT));
1014                        rr->rr_eadata = req_capsule_client_get(pill,
1015                                                               &RMF_EADATA);
1016                        rr->rr_eadatalen = req_capsule_get_size(pill,
1017                                                                &RMF_EADATA,
1018                                                                RCL_CLIENT);
1019                        sp->u.sp_ea.eadata = rr->rr_eadata;
1020                        sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1021                        sp->u.sp_ea.fid = rr->rr_fid1;
1022                        RETURN(0);
1023                 }
1024                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
1025         } else if (S_ISLNK(attr->la_mode)) {
1026                 const char *tgt = NULL;
1027
1028                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1029                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1030                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1031                         sp->u.sp_symname = tgt;
1032                 }
1033                 if (tgt == NULL)
1034                         RETURN(-EFAULT);
1035         } else {
1036                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
1037         }
1038         rc = mdt_dlmreq_unpack(info);
1039         RETURN(rc);
1040 }
1041
1042 static int mdt_link_unpack(struct mdt_thread_info *info)
1043 {
1044         struct md_ucred         *uc  = mdt_ucred(info);
1045         struct mdt_rec_link     *rec;
1046         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1047         struct mdt_reint_record *rr = &info->mti_rr;
1048         struct req_capsule      *pill = info->mti_pill;
1049         int rc;
1050         ENTRY;
1051
1052         CLASSERT(sizeof(struct mdt_rec_link) == sizeof(struct mdt_rec_reint));
1053         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1054         if (rec == NULL)
1055                 RETURN(-EFAULT);
1056
1057         uc->mu_fsuid = rec->lk_fsuid;
1058         uc->mu_fsgid = rec->lk_fsgid;
1059         uc->mu_cap   = rec->lk_cap;
1060         uc->mu_suppgids[0] = rec->lk_suppgid1;
1061         uc->mu_suppgids[1] = rec->lk_suppgid2;
1062
1063         attr->la_uid = rec->lk_fsuid;
1064         attr->la_gid = rec->lk_fsgid;
1065         rr->rr_fid1 = &rec->lk_fid1;
1066         rr->rr_fid2 = &rec->lk_fid2;
1067         attr->la_ctime = rec->lk_time;
1068         attr->la_mtime = rec->lk_time;
1069         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1070
1071         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1072                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1073                                  req_capsule_client_get(pill, &RMF_CAPA1));
1074         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1075                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1076                                  req_capsule_client_get(pill, &RMF_CAPA2));
1077
1078         info->mti_spec.sp_ck_split = !!(rec->lk_bias & MDS_CHECK_SPLIT);
1079         info->mti_cross_ref = !!(rec->lk_bias & MDS_CROSS_REF);
1080         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1081         if (rr->rr_name == NULL)
1082                 RETURN(-EFAULT);
1083         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1084         if (!info->mti_cross_ref)
1085                 LASSERT(rr->rr_namelen > 0);
1086
1087         rc = mdt_dlmreq_unpack(info);
1088         RETURN(rc);
1089 }
1090
1091 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1092 {
1093         struct md_ucred         *uc  = mdt_ucred(info);
1094         struct mdt_rec_unlink   *rec;
1095         struct md_attr          *ma = &info->mti_attr;
1096         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1097         struct mdt_reint_record *rr = &info->mti_rr;
1098         struct req_capsule      *pill = info->mti_pill;
1099         int rc;
1100         ENTRY;
1101
1102         CLASSERT(sizeof(struct mdt_rec_unlink) == sizeof(struct mdt_rec_reint));
1103         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1104         if (rec == NULL)
1105                 RETURN(-EFAULT);
1106
1107         uc->mu_fsuid = rec->ul_fsuid;
1108         uc->mu_fsgid = rec->ul_fsgid;
1109         uc->mu_cap   = rec->ul_cap;
1110         uc->mu_suppgids[0] = rec->ul_suppgid1;
1111         uc->mu_suppgids[1] = -1;
1112
1113         attr->la_uid = rec->ul_fsuid;
1114         attr->la_gid = rec->ul_fsgid;
1115         rr->rr_fid1 = &rec->ul_fid1;
1116         rr->rr_fid2 = &rec->ul_fid2;
1117         attr->la_ctime = rec->ul_time;
1118         attr->la_mtime = rec->ul_time;
1119         attr->la_mode  = rec->ul_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
1126         info->mti_cross_ref = !!(rec->ul_bias & MDS_CROSS_REF);
1127         if (!info->mti_cross_ref) {
1128                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1129                 rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1130                 if (rr->rr_name == NULL || rr->rr_namelen == 0)
1131                         RETURN(-EFAULT);
1132         } else {
1133                 rr->rr_name = NULL;
1134                 rr->rr_namelen = 0;
1135         }
1136         info->mti_spec.sp_ck_split = !!(rec->ul_bias & MDS_CHECK_SPLIT);
1137         if (rec->ul_bias & MDS_VTX_BYPASS)
1138                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1139         else
1140                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1141
1142         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1143         /* last unlink need LOV EA sent back */
1144         rr->rr_eadatalen = info->mti_mdt->mdt_max_mdsize;
1145
1146         rc = mdt_dlmreq_unpack(info);
1147         RETURN(rc);
1148 }
1149
1150 static int mdt_rename_unpack(struct mdt_thread_info *info)
1151 {
1152         struct md_ucred         *uc = mdt_ucred(info);
1153         struct mdt_rec_rename   *rec;
1154         struct md_attr          *ma = &info->mti_attr;
1155         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1156         struct mdt_reint_record *rr = &info->mti_rr;
1157         struct req_capsule      *pill = info->mti_pill;
1158         int rc;
1159         ENTRY;
1160
1161         CLASSERT(sizeof(struct mdt_rec_rename) == 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->rn_fsuid;
1167         uc->mu_fsgid = rec->rn_fsgid;
1168         uc->mu_cap   = rec->rn_cap;
1169         uc->mu_suppgids[0] = rec->rn_suppgid1;
1170         uc->mu_suppgids[1] = rec->rn_suppgid2;
1171
1172         attr->la_uid = rec->rn_fsuid;
1173         attr->la_gid = rec->rn_fsgid;
1174         rr->rr_fid1 = &rec->rn_fid1;
1175         rr->rr_fid2 = &rec->rn_fid2;
1176         attr->la_ctime = rec->rn_time;
1177         attr->la_mtime = rec->rn_time;
1178         /* rename_tgt contains the mode already */
1179         attr->la_mode = rec->rn_mode;
1180         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1181
1182         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1183                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1184                                  req_capsule_client_get(pill, &RMF_CAPA1));
1185         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1186                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1187                                  req_capsule_client_get(pill, &RMF_CAPA2));
1188
1189         info->mti_spec.sp_ck_split = !!(rec->rn_bias & MDS_CHECK_SPLIT);
1190         info->mti_cross_ref = !!(rec->rn_bias & MDS_CROSS_REF);
1191         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1192         rr->rr_tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1193         if (rr->rr_name == NULL || rr->rr_tgt == NULL)
1194                 RETURN(-EFAULT);
1195         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1196         rr->rr_tgtlen = req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT) - 1;
1197         if (!info->mti_cross_ref)
1198                 LASSERT(rr->rr_namelen > 0 && rr->rr_tgtlen > 0);
1199         if (rec->rn_bias & MDS_VTX_BYPASS)
1200                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1201         else
1202                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1203
1204         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1205         /* rename may contain unlink so we might need LOV EA sent back */
1206         rr->rr_eadatalen = info->mti_mdt->mdt_max_mdsize;
1207
1208         rc = mdt_dlmreq_unpack(info);
1209         RETURN(rc);
1210 }
1211
1212 static int mdt_open_unpack(struct mdt_thread_info *info)
1213 {
1214         struct md_ucred         *uc = mdt_ucred(info);
1215         struct mdt_rec_create   *rec;
1216         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1217         struct req_capsule      *pill = info->mti_pill;
1218         struct mdt_reint_record *rr   = &info->mti_rr;
1219         struct ptlrpc_request   *req  = mdt_info_req(info);
1220         struct md_op_spec       *sp   = &info->mti_spec;
1221         ENTRY;
1222
1223         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1224         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1225         if (rec == NULL)
1226                 RETURN(-EFAULT);
1227
1228         uc->mu_fsuid = rec->cr_fsuid;
1229         uc->mu_fsgid = rec->cr_fsgid;
1230         uc->mu_cap   = rec->cr_cap;
1231         uc->mu_suppgids[0] = rec->cr_suppgid1;
1232         uc->mu_suppgids[1] = rec->cr_suppgid2;
1233
1234         rr->rr_fid1   = &rec->cr_fid1;
1235         rr->rr_fid2   = &rec->cr_fid2;
1236         rr->rr_handle = &rec->cr_old_handle;
1237         attr->la_mode = rec->cr_mode;
1238         attr->la_rdev  = rec->cr_rdev;
1239         attr->la_uid   = rec->cr_fsuid;
1240         attr->la_gid   = rec->cr_fsgid;
1241         attr->la_ctime = rec->cr_time;
1242         attr->la_mtime = rec->cr_time;
1243         attr->la_atime = rec->cr_time;
1244         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1245                          LA_CTIME | LA_MTIME | LA_ATIME;
1246         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1247         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1248         /* Do not trigger ASSERTION if client miss to set such flags. */
1249         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1250                 RETURN(-EPROTO);
1251         info->mti_replayepoch = rec->cr_ioepoch;
1252
1253         info->mti_spec.sp_ck_split = !!(rec->cr_bias & MDS_CHECK_SPLIT);
1254         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1255
1256         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1257                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1258                                  req_capsule_client_get(pill, &RMF_CAPA1));
1259         if (req_is_replay(req) &&
1260             req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT)) {
1261 #if 0
1262                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1263                                  req_capsule_client_get(pill, &RMF_CAPA2));
1264 #else
1265                 /*
1266                  * FIXME: capa in replay open request might have expired,
1267                  * bypass capa check. Security hole?
1268                  */
1269                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1270                 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1271 #endif
1272         }
1273
1274         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1275         if (rr->rr_name == NULL)
1276                 RETURN(-EFAULT);
1277         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1278
1279         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1280                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1281                                                         RCL_CLIENT);
1282                 if (rr->rr_eadatalen > 0) {
1283                         rr->rr_eadata = req_capsule_client_get(pill,
1284                                                                &RMF_EADATA);
1285                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1286                         sp->u.sp_ea.eadata = rr->rr_eadata;
1287                         sp->no_create = !!req_is_replay(req);
1288                 }
1289
1290                 /*
1291                  * Client default md_size may be 0 right after client start,
1292                  * until all osc are connected, set here just some reasonable
1293                  * value to prevent misbehavior.
1294                  */
1295                 if (rr->rr_eadatalen == 0 &&
1296                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1297                         rr->rr_eadatalen = MIN_MD_SIZE;        }
1298
1299         RETURN(0);
1300 }
1301
1302 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1303 {
1304         struct mdt_reint_record   *rr   = &info->mti_rr;
1305         struct md_ucred           *uc   = mdt_ucred(info);
1306         struct lu_attr            *attr = &info->mti_attr.ma_attr;
1307         struct req_capsule        *pill = info->mti_pill;
1308         struct mdt_rec_setxattr   *rec;
1309         ENTRY;
1310
1311
1312         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1313                          sizeof(struct mdt_rec_reint));
1314
1315         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1316         if (rec == NULL)
1317                 RETURN(-EFAULT);
1318
1319         uc->mu_fsuid  = rec->sx_fsuid;
1320         uc->mu_fsgid  = rec->sx_fsgid;
1321         uc->mu_cap    = rec->sx_cap;
1322         uc->mu_suppgids[0] = rec->sx_suppgid1;
1323         uc->mu_suppgids[1] = -1;
1324
1325         rr->rr_opcode = rec->sx_opcode;
1326         rr->rr_fid1   = &rec->sx_fid;
1327         attr->la_valid = rec->sx_valid;
1328         attr->la_ctime = rec->sx_time;
1329         attr->la_size = rec->sx_size;
1330         attr->la_flags = rec->sx_flags;
1331
1332         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1333                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1334                                  req_capsule_client_get(pill, &RMF_CAPA1));
1335         else
1336                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1337
1338         rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
1339         if (rr->rr_name == NULL)
1340                 RETURN(-EFAULT);
1341         rr->rr_namelen = req_capsule_get_size(pill, &RMF_NAME, RCL_CLIENT) - 1;
1342         LASSERT(rr->rr_namelen > 0);
1343
1344         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1345                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1346                                                         RCL_CLIENT);
1347                 if (rr->rr_eadatalen > 0) {
1348                         rr->rr_eadata = req_capsule_client_get(pill,
1349                                                                &RMF_EADATA);
1350                         if (rr->rr_eadata == NULL)
1351                                 RETURN(-EFAULT);
1352                 } else {
1353                         rr->rr_eadata = NULL;
1354                 }
1355         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1356                 CDEBUG(D_INFO, "no xattr data supplied\n");
1357                 RETURN(-EFAULT);
1358         }
1359
1360         RETURN(0);
1361 }
1362
1363
1364 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1365
1366 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1367         [REINT_SETATTR]  = mdt_setattr_unpack,
1368         [REINT_CREATE]   = mdt_create_unpack,
1369         [REINT_LINK]     = mdt_link_unpack,
1370         [REINT_UNLINK]   = mdt_unlink_unpack,
1371         [REINT_RENAME]   = mdt_rename_unpack,
1372         [REINT_OPEN]     = mdt_open_unpack,
1373         [REINT_SETXATTR] = mdt_setxattr_unpack
1374 };
1375
1376 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1377 {
1378         int rc;
1379         ENTRY;
1380
1381         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1382         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1383                 info->mti_rr.rr_opcode = op;
1384                 rc = mdt_reint_unpackers[op](info);
1385         } else {
1386                 CERROR("Unexpected opcode %d\n", op);
1387                 rc = -EFAULT;
1388         }
1389         RETURN(rc);
1390 }