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