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