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