Whamcloud - gitweb
30fcaf0b84f7e05df94308e081548778dc296aba
[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                                struct list_head *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->mbo_suppgid;
159                 ucred->uc_suppgids[1] = -1;
160         }
161
162         /* sanity check: we expect the uid which client claimed is true */
163         if (remote) {
164                 if (!uid_valid(make_kuid(&init_user_ns, req->rq_auth_mapped_uid))) {
165                         CDEBUG(D_SEC, "remote user not mapped, deny access!\n");
166                         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 (!uid_valid(make_kuid(&init_user_ns, req->rq_auth_mapped_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->mbo_uid;
424         uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
425         uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
426         uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
427         uc->uc_suppgids[0] = body->mbo_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->mbo_capability & ~CFS_CAP_FS_MASK;
451         else
452                 uc->uc_cap = body->mbo_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 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
566 {
567         const struct lmv_mds_md_v1 *lmm1;
568         int                        i;
569
570         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
571                 return;
572
573         lmm1 = &lmv->lmv_md_v1;
574         CDEBUG(level, "magic 0x%08X, master %#X stripe_count %#x\n",
575                le32_to_cpu(lmm1->lmv_magic),
576                le32_to_cpu(lmm1->lmv_master_mdt_index),
577                le32_to_cpu(lmm1->lmv_stripe_count));
578
579         if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
580                 return;
581
582         for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
583                 struct lu_fid fid;
584
585                 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
586                 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
587         }
588 }
589
590 /* Shrink and/or grow reply buffers */
591 int mdt_fix_reply(struct mdt_thread_info *info)
592 {
593         struct req_capsule *pill = info->mti_pill;
594         struct mdt_body    *body;
595         int                md_size, md_packed = 0;
596         int                acl_size;
597         int                rc = 0;
598         ENTRY;
599
600         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
601         LASSERT(body != NULL);
602
603         if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
604                                OBD_MD_LINKNAME))
605                 md_size = body->mbo_eadatasize;
606         else
607                 md_size = 0;
608
609         acl_size = body->mbo_aclsize;
610
611         /* this replay - not send info to client */
612         if (info->mti_spec.no_create) {
613                 md_size = 0;
614                 acl_size = 0;
615         }
616
617         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d"
618                " MDSCAPA = %llx, OSSCAPA = %llx\n",
619                md_size, acl_size,
620                (unsigned long long)(body->mbo_valid & OBD_MD_FLMDSCAPA),
621                (unsigned long long)(body->mbo_valid & OBD_MD_FLOSSCAPA));
622 /*
623             &RMF_MDT_BODY,
624             &RMF_MDT_MD,
625             &RMF_ACL, or &RMF_LOGCOOKIES
626 (optional)  &RMF_CAPA1,
627 (optional)  &RMF_CAPA2,
628 (optional)  something else
629 */
630
631         /* MDT_MD buffer may be bigger than packed value, let's shrink all
632          * buffers before growing it */
633         if (info->mti_big_lmm_used) {
634                 /* big_lmm buffer may be used even without packing the result
635                  * into reply, just for internal server needs */
636                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
637                         md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
638                                                          RCL_SERVER);
639
640                 /* free big lmm if md_size is not needed */
641                 if (md_size == 0 || md_packed == 0) {
642                         info->mti_big_lmm_used = 0;
643                 } else {
644                         /* buffer must be allocated separately */
645                         LASSERT(info->mti_attr.ma_lmm !=
646                                 req_capsule_server_get(pill, &RMF_MDT_MD));
647                         req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
648                 }
649         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
650                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
651         }
652
653         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
654                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
655         else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
656                 req_capsule_shrink(pill, &RMF_LOGCOOKIES,
657                                    acl_size, RCL_SERVER);
658
659         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
660             !(body->mbo_valid & OBD_MD_FLMDSCAPA))
661                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
662
663         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
664             !(body->mbo_valid & OBD_MD_FLOSSCAPA))
665                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
666
667         /*
668          * Some more field should be shrinked if needed.
669          * This should be done by those who added fields to reply message.
670          */
671
672         /* Grow MD buffer if needed finally */
673         if (info->mti_big_lmm_used) {
674                 void *lmm;
675
676                 LASSERT(md_size > md_packed);
677                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
678                        md_size - md_packed);
679                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
680                 if (rc) {
681                         /* we can't answer with proper LOV EA, drop flags,
682                          * the rc is also returned so this request is
683                          * considered as failed */
684                         body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
685                         /* don't return transno along with error */
686                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
687                 } else {
688                         /* now we need to pack right LOV/LMV EA */
689                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
690                         if (info->mti_attr.ma_valid & MA_LOV) {
691                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
692                                                              RCL_SERVER) ==
693                                                 info->mti_attr.ma_lmm_size);
694                                 memcpy(lmm, info->mti_attr.ma_lmm,
695                                        info->mti_attr.ma_lmm_size);
696                         } else if (info->mti_attr.ma_valid & MA_LMV) {
697                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
698                                                              RCL_SERVER) ==
699                                                 info->mti_attr.ma_lmv_size);
700                                 memcpy(lmm, info->mti_attr.ma_lmv,
701                                        info->mti_attr.ma_lmv_size);
702                         }
703                 }
704                 /* update mdt_max_mdsize so clients will be aware about that */
705                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
706                         info->mti_mdt->mdt_max_mdsize =
707                                                     info->mti_attr.ma_lmm_size;
708                 info->mti_big_lmm_used = 0;
709         }
710         RETURN(rc);
711 }
712
713
714 /* if object is dying, pack the lov/llog data,
715  * parameter info->mti_attr should be valid at this point! */
716 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
717                            const struct md_attr *ma)
718 {
719         struct mdt_body       *repbody;
720         const struct lu_attr *la = &ma->ma_attr;
721         int rc;
722         ENTRY;
723
724         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
725         LASSERT(repbody != NULL);
726
727         if (ma->ma_valid & MA_INODE)
728                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
729
730         if (ma->ma_valid & MA_LOV) {
731                 CERROR("No need in LOV EA upon unlink\n");
732                 dump_stack();
733         }
734         repbody->mbo_eadatasize = 0;
735
736         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
737             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
738             repbody->mbo_valid & OBD_MD_FLEASIZE) {
739                 struct lustre_capa *capa;
740
741                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
742                 LASSERT(capa);
743                 capa->lc_opc = CAPA_OPC_OSS_DESTROY;
744                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
745                 if (rc)
746                         RETURN(rc);
747
748                 repbody->mbo_valid |= OBD_MD_FLOSSCAPA;
749         }
750
751         RETURN(0);
752 }
753
754 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
755                                   struct md_attr *ma)
756 {
757         __u64 out;
758
759         out = 0;
760         if (in & MDS_ATTR_MODE)
761                 out |= LA_MODE;
762         if (in & MDS_ATTR_UID)
763                 out |= LA_UID;
764         if (in & MDS_ATTR_GID)
765                 out |= LA_GID;
766         if (in & MDS_ATTR_SIZE)
767                 out |= LA_SIZE;
768         if (in & MDS_ATTR_BLOCKS)
769                 out |= LA_BLOCKS;
770         if (in & MDS_ATTR_ATIME_SET)
771                 out |= LA_ATIME;
772         if (in & MDS_ATTR_CTIME_SET)
773                 out |= LA_CTIME;
774         if (in & MDS_ATTR_MTIME_SET)
775                 out |= LA_MTIME;
776         if (in & MDS_ATTR_ATTR_FLAG)
777                 out |= LA_FLAGS;
778         if (in & MDS_ATTR_KILL_SUID)
779                 out |= LA_KILL_SUID;
780         if (in & MDS_ATTR_KILL_SGID)
781                 out |= LA_KILL_SGID;
782
783         if (in & MDS_ATTR_FROM_OPEN)
784                 rr->rr_flags |= MRF_OPEN_TRUNC;
785         if (in & MDS_OPEN_OWNEROVERRIDE)
786                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
787         if (in & MDS_ATTR_FORCE)
788                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
789
790         in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID |
791                 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
792                 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
793                 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
794                 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
795                 MDS_ATTR_FROM_OPEN | MDS_OPEN_OWNEROVERRIDE);
796         if (in != 0)
797                 CERROR("Unknown attr bits: "LPX64"\n", in);
798         return out;
799 }
800
801 void mdt_set_capainfo(struct mdt_thread_info *info, int offset,
802                       const struct lu_fid *fid, struct lustre_capa *capa)
803 {
804         struct lu_capainfo *lci;
805
806         LASSERT(offset >= 0 && offset < LU_CAPAINFO_MAX);
807         if (!info->mti_mdt->mdt_lut.lut_mds_capa ||
808             !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA))
809                 return;
810
811         lci = lu_capainfo_get(info->mti_env);
812         LASSERT(lci);
813         lci->lci_fid[offset]  = *fid;
814         lci->lci_capa[offset] = capa;
815 }
816
817 #ifdef DEBUG_CAPA
818 void mdt_dump_capainfo(struct mdt_thread_info *info)
819 {
820         struct lu_capainfo *lci = lu_capainfo_get(info->mti_env);
821         int i;
822
823         if (lci == NULL)
824                 return;
825
826         for (i = 0; i < LU_CAPAINFO_MAX; i++) {
827                 if (lci->lci_capa[i] == NULL) {
828                         CERROR("no capa for index %d "DFID"\n",
829                                i, PFID(&lci->lci_fid[i]));
830                         continue;
831                 }
832                 if (lci->lci_capa[i] == BYPASS_CAPA) {
833                         CERROR("bypass for index %d "DFID"\n",
834                                i, PFID(&lci->lci_fid[i]));
835                         continue;
836                 }
837                 DEBUG_CAPA(D_ERROR, lci->lci_capa[i], "index %d", i);
838         }
839 }
840 #endif /* DEBUG_CAPA */
841
842 /* unpacking */
843
844 int mdt_name_unpack(struct req_capsule *pill,
845                     const struct req_msg_field *field,
846                     struct lu_name *ln,
847                     enum mdt_name_flags flags)
848 {
849         ln->ln_name = req_capsule_client_get(pill, field);
850         ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
851
852         if (!lu_name_is_valid(ln)) {
853                 ln->ln_name = NULL;
854                 ln->ln_namelen = 0;
855
856                 return -EPROTO;
857         }
858
859         if ((flags & MNF_FIX_ANON) &&
860             ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
861                 /* Newer (3.x) kernels use a name of "/" for the
862                  * "anonymous" disconnected dentries from NFS
863                  * filehandle conversion. See d_obtain_alias(). */
864                 ln->ln_name = NULL;
865                 ln->ln_namelen = 0;
866         }
867
868         return 0;
869 }
870
871 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
872 {
873         struct lu_ucred         *uc  = mdt_ucred(info);
874         struct md_attr          *ma = &info->mti_attr;
875         struct lu_attr          *la = &ma->ma_attr;
876         struct req_capsule      *pill = info->mti_pill;
877         struct mdt_reint_record *rr = &info->mti_rr;
878         struct mdt_rec_setattr  *rec;
879         ENTRY;
880
881         CLASSERT(sizeof(struct mdt_rec_setattr)== sizeof(struct mdt_rec_reint));
882         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
883         if (rec == NULL)
884                 RETURN(-EFAULT);
885
886         /* This prior initialization is needed for old_init_ucred_reint() */
887         uc->uc_fsuid = rec->sa_fsuid;
888         uc->uc_fsgid = rec->sa_fsgid;
889         uc->uc_cap   = rec->sa_cap;
890         uc->uc_suppgids[0] = rec->sa_suppgid;
891         uc->uc_suppgids[1] = -1;
892
893         rr->rr_fid1 = &rec->sa_fid;
894         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
895         /*  If MDS_ATTR_xTIME is set without MDS_ATTR_xTIME_SET and
896          *  the client does not have OBD_CONNECT_FULL20, convert it
897          *  to LA_xTIME. LU-3036 */
898         if (!(exp_connect_flags(info->mti_exp) & OBD_CONNECT_FULL20)) {
899                 if (!(rec->sa_valid & MDS_ATTR_ATIME_SET) &&
900                      (rec->sa_valid & MDS_ATTR_ATIME))
901                         la->la_valid |= LA_ATIME;
902                 if (!(rec->sa_valid & MDS_ATTR_MTIME_SET) &&
903                      (rec->sa_valid & MDS_ATTR_MTIME))
904                         la->la_valid |= LA_MTIME;
905                 if (!(rec->sa_valid & MDS_ATTR_CTIME_SET) &&
906                      (rec->sa_valid & MDS_ATTR_CTIME))
907                         la->la_valid |= LA_CTIME;
908         }
909         la->la_mode  = rec->sa_mode;
910         la->la_flags = rec->sa_attr_flags;
911         la->la_uid   = rec->sa_uid;
912         la->la_gid   = rec->sa_gid;
913         la->la_size  = rec->sa_size;
914         la->la_blocks = rec->sa_blocks;
915         la->la_ctime = rec->sa_ctime;
916         la->la_atime = rec->sa_atime;
917         la->la_mtime = rec->sa_mtime;
918         ma->ma_valid = MA_INODE;
919
920         if (rec->sa_bias & MDS_DATA_MODIFIED)
921                 ma->ma_attr_flags |= MDS_DATA_MODIFIED;
922         else
923                 ma->ma_attr_flags &= ~MDS_DATA_MODIFIED;
924
925         if (rec->sa_bias & MDS_HSM_RELEASE)
926                 ma->ma_attr_flags |= MDS_HSM_RELEASE;
927         else
928                 ma->ma_attr_flags &= ~MDS_HSM_RELEASE;
929
930         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
931                 mdt_set_capainfo(info, 0, rr->rr_fid1,
932                                  req_capsule_client_get(pill, &RMF_CAPA1));
933
934         RETURN(0);
935 }
936
937 static int mdt_ioepoch_unpack(struct mdt_thread_info *info)
938 {
939         struct req_capsule *pill = info->mti_pill;
940         ENTRY;
941
942         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
943                 info->mti_ioepoch =
944                         req_capsule_client_get(pill, &RMF_MDT_EPOCH);
945         else
946                 info->mti_ioepoch = NULL;
947         RETURN(info->mti_ioepoch == NULL ? -EFAULT : 0);
948 }
949
950 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
951         struct req_capsule      *pill = info->mti_pill;
952
953         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
954                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
955                 if (info->mti_dlm_req == NULL)
956                         RETURN(-EFAULT);
957         }
958
959         RETURN(0);
960 }
961
962 static int mdt_setattr_unpack(struct mdt_thread_info *info)
963 {
964         struct mdt_reint_record *rr = &info->mti_rr;
965         struct md_attr          *ma = &info->mti_attr;
966         struct req_capsule      *pill = info->mti_pill;
967         int rc;
968         ENTRY;
969
970         rc = mdt_setattr_unpack_rec(info);
971         if (rc)
972                 RETURN(rc);
973
974         /* Epoch may be absent */
975         mdt_ioepoch_unpack(info);
976
977         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
978                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
979                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
980                                                         RCL_CLIENT);
981                 if (rr->rr_eadatalen > 0) {
982                         const struct lmv_user_md        *lum;
983
984                         lum = rr->rr_eadata;
985                         /* Sigh ma_valid(from req) does not indicate whether
986                          * it will set LOV/LMV EA, so we have to check magic */
987                         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
988                                 ma->ma_valid |= MA_LMV;
989                                 ma->ma_lmv = (void *)rr->rr_eadata;
990                                 ma->ma_lmv_size = rr->rr_eadatalen;
991                         } else {
992                                 ma->ma_valid |= MA_LOV;
993                                 ma->ma_lmm = (void *)rr->rr_eadata;
994                                 ma->ma_lmm_size = rr->rr_eadatalen;
995                         }
996                 }
997         }
998
999         rc = mdt_dlmreq_unpack(info);
1000         RETURN(rc);
1001 }
1002
1003 static int mdt_hsm_release_unpack(struct mdt_thread_info *info)
1004 {
1005         struct md_attr          *ma = &info->mti_attr;
1006         struct req_capsule      *pill = info->mti_pill;
1007         ENTRY;
1008
1009         if (!(ma->ma_attr_flags & MDS_HSM_RELEASE))
1010                 RETURN(0);
1011
1012         req_capsule_extend(pill, &RQF_MDS_RELEASE_CLOSE);
1013
1014         if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1015             req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1016                 RETURN(-EFAULT);
1017
1018         RETURN(0);
1019 }
1020
1021 int mdt_close_unpack(struct mdt_thread_info *info)
1022 {
1023         int rc;
1024         ENTRY;
1025
1026         rc = mdt_ioepoch_unpack(info);
1027         if (rc)
1028                 RETURN(rc);
1029
1030         rc = mdt_setattr_unpack_rec(info);
1031         if (rc)
1032                 RETURN(rc);
1033
1034         rc = mdt_hsm_release_unpack(info);
1035         if (rc)
1036                 RETURN(rc);
1037
1038         RETURN(mdt_init_ucred_reint(info));
1039 }
1040
1041 static int mdt_create_unpack(struct mdt_thread_info *info)
1042 {
1043         struct lu_ucred         *uc  = mdt_ucred(info);
1044         struct mdt_rec_create   *rec;
1045         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1046         struct mdt_reint_record *rr = &info->mti_rr;
1047         struct req_capsule      *pill = info->mti_pill;
1048         struct md_op_spec       *sp = &info->mti_spec;
1049         int rc;
1050         ENTRY;
1051
1052         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1053         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1054         if (rec == NULL)
1055                 RETURN(-EFAULT);
1056
1057         /* This prior initialization is needed for old_init_ucred_reint() */
1058         uc->uc_fsuid = rec->cr_fsuid;
1059         uc->uc_fsgid = rec->cr_fsgid;
1060         uc->uc_cap   = rec->cr_cap;
1061         uc->uc_suppgids[0] = rec->cr_suppgid1;
1062         uc->uc_suppgids[1] = -1;
1063         uc->uc_umask = rec->cr_umask;
1064
1065         rr->rr_fid1 = &rec->cr_fid1;
1066         rr->rr_fid2 = &rec->cr_fid2;
1067         attr->la_mode = rec->cr_mode;
1068         attr->la_rdev  = rec->cr_rdev;
1069         attr->la_uid   = rec->cr_fsuid;
1070         attr->la_gid   = rec->cr_fsgid;
1071         attr->la_ctime = rec->cr_time;
1072         attr->la_mtime = rec->cr_time;
1073         attr->la_atime = rec->cr_time;
1074         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1075                          LA_CTIME | LA_MTIME | LA_ATIME;
1076         memset(&sp->u, 0, sizeof(sp->u));
1077         sp->sp_cr_flags = get_mrc_cr_flags(rec);
1078
1079         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1080                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1081                                  req_capsule_client_get(pill, &RMF_CAPA1));
1082         mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1083
1084         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1085         if (rc < 0)
1086                 RETURN(rc);
1087
1088         if (S_ISLNK(attr->la_mode)) {
1089                 const char *tgt = NULL;
1090
1091                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1092                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1093                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1094                         sp->u.sp_symname = tgt;
1095                 }
1096                 if (tgt == NULL)
1097                         RETURN(-EFAULT);
1098         } else {
1099                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_RMT_ACL);
1100                 if (S_ISDIR(attr->la_mode) &&
1101                     req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1102                         sp->u.sp_ea.eadata =
1103                                 req_capsule_client_get(pill, &RMF_EADATA);
1104                         sp->u.sp_ea.eadatalen =
1105                                 req_capsule_get_size(pill, &RMF_EADATA,
1106                                                      RCL_CLIENT);
1107                         sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1108                 }
1109         }
1110
1111         rc = mdt_dlmreq_unpack(info);
1112         RETURN(rc);
1113 }
1114
1115 static int mdt_link_unpack(struct mdt_thread_info *info)
1116 {
1117         struct lu_ucred         *uc  = mdt_ucred(info);
1118         struct mdt_rec_link     *rec;
1119         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1120         struct mdt_reint_record *rr = &info->mti_rr;
1121         struct req_capsule      *pill = info->mti_pill;
1122         int rc;
1123         ENTRY;
1124
1125         CLASSERT(sizeof(struct mdt_rec_link) == sizeof(struct mdt_rec_reint));
1126         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1127         if (rec == NULL)
1128                 RETURN(-EFAULT);
1129
1130         /* This prior initialization is needed for old_init_ucred_reint() */
1131         uc->uc_fsuid = rec->lk_fsuid;
1132         uc->uc_fsgid = rec->lk_fsgid;
1133         uc->uc_cap   = rec->lk_cap;
1134         uc->uc_suppgids[0] = rec->lk_suppgid1;
1135         uc->uc_suppgids[1] = rec->lk_suppgid2;
1136
1137         attr->la_uid = rec->lk_fsuid;
1138         attr->la_gid = rec->lk_fsgid;
1139         rr->rr_fid1 = &rec->lk_fid1;
1140         rr->rr_fid2 = &rec->lk_fid2;
1141         attr->la_ctime = rec->lk_time;
1142         attr->la_mtime = rec->lk_time;
1143         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1144
1145         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1146                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1147                                  req_capsule_client_get(pill, &RMF_CAPA1));
1148         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1149                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1150                                  req_capsule_client_get(pill, &RMF_CAPA2));
1151
1152         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1153         if (rc < 0)
1154                 RETURN(rc);
1155
1156         rc = mdt_dlmreq_unpack(info);
1157
1158         RETURN(rc);
1159 }
1160
1161 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1162 {
1163         struct lu_ucred         *uc  = mdt_ucred(info);
1164         struct mdt_rec_unlink   *rec;
1165         struct md_attr          *ma = &info->mti_attr;
1166         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1167         struct mdt_reint_record *rr = &info->mti_rr;
1168         struct req_capsule      *pill = info->mti_pill;
1169         int rc;
1170         ENTRY;
1171
1172         CLASSERT(sizeof(struct mdt_rec_unlink) == sizeof(struct mdt_rec_reint));
1173         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1174         if (rec == NULL)
1175                 RETURN(-EFAULT);
1176
1177         /* This prior initialization is needed for old_init_ucred_reint() */
1178         uc->uc_fsuid = rec->ul_fsuid;
1179         uc->uc_fsgid = rec->ul_fsgid;
1180         uc->uc_cap   = rec->ul_cap;
1181         uc->uc_suppgids[0] = rec->ul_suppgid1;
1182         uc->uc_suppgids[1] = -1;
1183
1184         attr->la_uid = rec->ul_fsuid;
1185         attr->la_gid = rec->ul_fsgid;
1186         rr->rr_fid1 = &rec->ul_fid1;
1187         rr->rr_fid2 = &rec->ul_fid2;
1188         attr->la_ctime = rec->ul_time;
1189         attr->la_mtime = rec->ul_time;
1190         attr->la_mode  = rec->ul_mode;
1191         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1192
1193         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1194                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1195                                  req_capsule_client_get(pill, &RMF_CAPA1));
1196
1197         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1198         if (rc < 0)
1199                 RETURN(rc);
1200
1201         if (rec->ul_bias & MDS_VTX_BYPASS)
1202                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1203         else
1204                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1205
1206         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1207
1208         rc = mdt_dlmreq_unpack(info);
1209         RETURN(rc);
1210 }
1211
1212 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1213 {
1214         info->mti_spec.sp_rm_entry = 1;
1215         return mdt_unlink_unpack(info);
1216 }
1217
1218 static int mdt_rename_unpack(struct mdt_thread_info *info)
1219 {
1220         struct lu_ucred         *uc = mdt_ucred(info);
1221         struct mdt_rec_rename   *rec;
1222         struct md_attr          *ma = &info->mti_attr;
1223         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1224         struct mdt_reint_record *rr = &info->mti_rr;
1225         struct req_capsule      *pill = info->mti_pill;
1226         int rc;
1227         ENTRY;
1228
1229         CLASSERT(sizeof(struct mdt_rec_rename) == sizeof(struct mdt_rec_reint));
1230         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1231         if (rec == NULL)
1232                 RETURN(-EFAULT);
1233
1234         /* This prior initialization is needed for old_init_ucred_reint() */
1235         uc->uc_fsuid = rec->rn_fsuid;
1236         uc->uc_fsgid = rec->rn_fsgid;
1237         uc->uc_cap   = rec->rn_cap;
1238         uc->uc_suppgids[0] = rec->rn_suppgid1;
1239         uc->uc_suppgids[1] = rec->rn_suppgid2;
1240
1241         attr->la_uid = rec->rn_fsuid;
1242         attr->la_gid = rec->rn_fsgid;
1243         rr->rr_fid1 = &rec->rn_fid1;
1244         rr->rr_fid2 = &rec->rn_fid2;
1245         attr->la_ctime = rec->rn_time;
1246         attr->la_mtime = rec->rn_time;
1247         /* rename_tgt contains the mode already */
1248         attr->la_mode = rec->rn_mode;
1249         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1250
1251         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1252                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1253                                  req_capsule_client_get(pill, &RMF_CAPA1));
1254         if (req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT))
1255                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1256                                  req_capsule_client_get(pill, &RMF_CAPA2));
1257
1258         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1259         if (rc < 0)
1260                 RETURN(rc);
1261
1262         rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1263         if (rc < 0)
1264                 RETURN(rc);
1265
1266         if (rec->rn_bias & MDS_VTX_BYPASS)
1267                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1268         else
1269                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1270
1271         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1272
1273
1274         rc = mdt_dlmreq_unpack(info);
1275
1276         RETURN(rc);
1277 }
1278
1279 /*
1280  * please see comment above LOV_MAGIC_V1_DEF
1281  */
1282 static void mdt_fix_lov_magic(struct mdt_thread_info *info)
1283 {
1284         struct mdt_reint_record *rr = &info->mti_rr;
1285         struct lov_user_md_v1   *v1;
1286
1287         v1 = (void *)rr->rr_eadata;
1288         LASSERT(v1);
1289
1290         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1291                 if (v1->lmm_magic == LOV_USER_MAGIC_V1) {
1292                         v1->lmm_magic = LOV_MAGIC_V1_DEF;
1293                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
1294                         v1->lmm_magic = __swab32(LOV_MAGIC_V1_DEF);
1295                 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
1296                         v1->lmm_magic = LOV_MAGIC_V3_DEF;
1297                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
1298                         v1->lmm_magic = __swab32(LOV_MAGIC_V3_DEF);
1299                 }
1300         }
1301 }
1302
1303 static int mdt_open_unpack(struct mdt_thread_info *info)
1304 {
1305         struct lu_ucred         *uc = mdt_ucred(info);
1306         struct mdt_rec_create   *rec;
1307         struct lu_attr          *attr = &info->mti_attr.ma_attr;
1308         struct req_capsule      *pill = info->mti_pill;
1309         struct mdt_reint_record *rr   = &info->mti_rr;
1310         struct ptlrpc_request   *req  = mdt_info_req(info);
1311         struct md_op_spec       *sp   = &info->mti_spec;
1312         ENTRY;
1313
1314         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1315         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1316         if (rec == NULL)
1317                 RETURN(-EFAULT);
1318
1319         /* This prior initialization is needed for old_init_ucred_reint() */
1320         uc->uc_fsuid = rec->cr_fsuid;
1321         uc->uc_fsgid = rec->cr_fsgid;
1322         uc->uc_cap   = rec->cr_cap;
1323         uc->uc_suppgids[0] = rec->cr_suppgid1;
1324         uc->uc_suppgids[1] = rec->cr_suppgid2;
1325         uc->uc_umask = rec->cr_umask;
1326
1327         rr->rr_fid1   = &rec->cr_fid1;
1328         rr->rr_fid2   = &rec->cr_fid2;
1329         rr->rr_handle = &rec->cr_old_handle;
1330         attr->la_mode = rec->cr_mode;
1331         attr->la_rdev  = rec->cr_rdev;
1332         attr->la_uid   = rec->cr_fsuid;
1333         attr->la_gid   = rec->cr_fsgid;
1334         attr->la_ctime = rec->cr_time;
1335         attr->la_mtime = rec->cr_time;
1336         attr->la_atime = rec->cr_time;
1337         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1338                          LA_CTIME | LA_MTIME | LA_ATIME;
1339         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1340         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1341         /* Do not trigger ASSERTION if client miss to set such flags. */
1342         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1343                 RETURN(-EPROTO);
1344         info->mti_replayepoch = rec->cr_ioepoch;
1345
1346         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
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         if (req_is_replay(req) &&
1352             req_capsule_get_size(pill, &RMF_CAPA2, RCL_CLIENT)) {
1353 #if 0
1354                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1355                                  req_capsule_client_get(pill, &RMF_CAPA2));
1356 #else
1357                 /*
1358                  * FIXME: capa in replay open request might have expired,
1359                  * bypass capa check. Security hole?
1360                  */
1361                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1362                 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1363 #endif
1364         }
1365
1366         mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1367
1368         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1369                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1370                                                         RCL_CLIENT);
1371                 if (rr->rr_eadatalen > 0) {
1372                         rr->rr_eadata = req_capsule_client_get(pill,
1373                                                                &RMF_EADATA);
1374                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1375                         sp->u.sp_ea.eadata = rr->rr_eadata;
1376                         sp->no_create = !!req_is_replay(req);
1377                         mdt_fix_lov_magic(info);
1378                 }
1379
1380                 /*
1381                  * Client default md_size may be 0 right after client start,
1382                  * until all osc are connected, set here just some reasonable
1383                  * value to prevent misbehavior.
1384                  */
1385                 if (rr->rr_eadatalen == 0 &&
1386                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1387                         rr->rr_eadatalen = MIN_MD_SIZE;
1388         }
1389
1390         RETURN(0);
1391 }
1392
1393 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1394 {
1395         struct mdt_reint_record *rr     = &info->mti_rr;
1396         struct lu_ucred         *uc     = mdt_ucred(info);
1397         struct lu_attr          *attr   = &info->mti_attr.ma_attr;
1398         struct req_capsule      *pill   = info->mti_pill;
1399         struct mdt_rec_setxattr *rec;
1400         int                      rc;
1401         ENTRY;
1402
1403
1404         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1405                          sizeof(struct mdt_rec_reint));
1406
1407         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1408         if (rec == NULL)
1409                 RETURN(-EFAULT);
1410
1411         /* This prior initialization is needed for old_init_ucred_reint() */
1412         uc->uc_fsuid  = rec->sx_fsuid;
1413         uc->uc_fsgid  = rec->sx_fsgid;
1414         uc->uc_cap    = rec->sx_cap;
1415         uc->uc_suppgids[0] = rec->sx_suppgid1;
1416         uc->uc_suppgids[1] = -1;
1417
1418         rr->rr_opcode = rec->sx_opcode;
1419         rr->rr_fid1   = &rec->sx_fid;
1420         attr->la_valid = rec->sx_valid;
1421         attr->la_ctime = rec->sx_time;
1422         attr->la_size = rec->sx_size;
1423         attr->la_flags = rec->sx_flags;
1424
1425         if (req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1426                 mdt_set_capainfo(info, 0, rr->rr_fid1,
1427                                  req_capsule_client_get(pill, &RMF_CAPA1));
1428         else
1429                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
1430
1431         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1432         if (rc < 0)
1433                 RETURN(rc);
1434
1435         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1436                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1437                                                         RCL_CLIENT);
1438                 if (rr->rr_eadatalen > 0) {
1439                         rr->rr_eadata = req_capsule_client_get(pill,
1440                                                                &RMF_EADATA);
1441                         if (rr->rr_eadata == NULL)
1442                                 RETURN(-EFAULT);
1443                 } else {
1444                         rr->rr_eadata = NULL;
1445                 }
1446         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1447                 CDEBUG(D_INFO, "no xattr data supplied\n");
1448                 RETURN(-EFAULT);
1449         }
1450
1451         if (mdt_dlmreq_unpack(info) < 0)
1452                 RETURN(-EPROTO);
1453
1454         RETURN(0);
1455 }
1456
1457
1458 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1459
1460 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1461         [REINT_SETATTR]  = mdt_setattr_unpack,
1462         [REINT_CREATE]   = mdt_create_unpack,
1463         [REINT_LINK]     = mdt_link_unpack,
1464         [REINT_UNLINK]   = mdt_unlink_unpack,
1465         [REINT_RENAME]   = mdt_rename_unpack,
1466         [REINT_OPEN]     = mdt_open_unpack,
1467         [REINT_SETXATTR] = mdt_setxattr_unpack,
1468         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1469         [REINT_MIGRATE]  = mdt_rename_unpack,
1470 };
1471
1472 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1473 {
1474         int rc;
1475         ENTRY;
1476
1477         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1478         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1479                 info->mti_rr.rr_opcode = op;
1480                 rc = mdt_reint_unpackers[op](info);
1481         } else {
1482                 CERROR("Unexpected opcode %d\n", op);
1483                 rc = -EFAULT;
1484         }
1485         RETURN(rc);
1486 }