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