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