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