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