Whamcloud - gitweb
LU-9727 lustre: add client NID to Changelogs entries
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_lib.c
33  *
34  * Lustre Metadata Target (mdt) request unpacking helper.
35  *
36  * Author: Peter Braam <braam@clusterfs.com>
37  * Author: Andreas Dilger <adilger@clusterfs.com>
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Mike Shaver <shaver@clusterfs.com>
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  * Author: Huang Hua <huanghua@clusterfs.com>
42  * Author: Fan Yong <fanyong@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_MDS
46
47 #include <linux/user_namespace.h>
48 #ifdef HAVE_UIDGID_HEADER
49 # include <linux/uidgid.h>
50 #endif
51 #include "mdt_internal.h"
52 #include <uapi/linux/lnet/nidstr.h>
53 #include <lustre_nodemap.h>
54
55 typedef enum ucred_init_type {
56         NONE_INIT       = 0,
57         BODY_INIT       = 1,
58         REC_INIT        = 2
59 } ucred_init_type_t;
60
61 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
62 {
63         return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
64 }
65
66 void mdt_exit_ucred(struct mdt_thread_info *info)
67 {
68         struct lu_ucred   *uc  = mdt_ucred(info);
69         struct mdt_device *mdt = info->mti_mdt;
70
71         LASSERT(uc != NULL);
72         if (uc->uc_valid != UCRED_INIT) {
73                 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
74                 if (uc->uc_ginfo) {
75                         put_group_info(uc->uc_ginfo);
76                         uc->uc_ginfo = NULL;
77                 }
78                 if (uc->uc_identity) {
79                         mdt_identity_put(mdt->mdt_identity_cache,
80                                          uc->uc_identity);
81                         uc->uc_identity = NULL;
82                 }
83                 uc->uc_valid = UCRED_INIT;
84         }
85 }
86
87 static int match_nosquash_list(struct rw_semaphore *sem,
88                                struct list_head *nidlist,
89                                lnet_nid_t peernid)
90 {
91         int rc;
92         ENTRY;
93         down_read(sem);
94         rc = cfs_match_nid(peernid, nidlist);
95         up_read(sem);
96         RETURN(rc);
97 }
98
99 /* root_squash for inter-MDS operations */
100 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
101 {
102         struct lu_ucred *ucred = mdt_ucred(info);
103         struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
104         ENTRY;
105
106         LASSERT(ucred != NULL);
107         if (!squash->rsi_uid || ucred->uc_fsuid)
108                 RETURN(0);
109
110         if (match_nosquash_list(&squash->rsi_sem,
111                                 &squash->rsi_nosquash_nids,
112                                 peernid)) {
113                 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
114                        libcfs_nid2str(peernid));
115                 RETURN(0);
116         }
117
118         CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
119                libcfs_nid2str(peernid),
120                ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
121                squash->rsi_uid, squash->rsi_gid, 0);
122
123         ucred->uc_fsuid = squash->rsi_uid;
124         ucred->uc_fsgid = squash->rsi_gid;
125         ucred->uc_cap = 0;
126         ucred->uc_suppgids[0] = -1;
127         ucred->uc_suppgids[1] = -1;
128
129         RETURN(0);
130 }
131
132 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
133 {
134         struct ptlrpc_request   *req = mdt_info_req(info);
135         const char              *jobid = mdt_req_get_jobid(req);
136
137         /* set jobid if specified. */
138         if (jobid)
139                 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
140         else
141                 uc->uc_jobid[0] = '\0';
142 }
143
144 static void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
145 {
146         if (info && info->mti_exp && info->mti_exp->exp_connection)
147                 uc->uc_nid = info->mti_exp->exp_connection->c_peer.nid;
148         else
149                 uc->uc_nid = LNET_NID_ANY;
150 }
151
152 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
153                           void *buf, bool drop_fs_cap)
154 {
155         struct ptlrpc_request *req = mdt_info_req(info);
156         struct mdt_device *mdt = info->mti_mdt;
157         struct ptlrpc_user_desc *pud = req->rq_user_desc;
158         struct lu_ucred *ucred = mdt_ucred(info);
159         struct lu_nodemap *nodemap;
160         lnet_nid_t peernid = req->rq_peer.nid;
161         __u32 perm = 0;
162         int setuid;
163         int setgid;
164         int rc = 0;
165
166         ENTRY;
167
168         LASSERT(req->rq_auth_gss);
169         LASSERT(!req->rq_auth_usr_mdt);
170         LASSERT(req->rq_user_desc);
171         LASSERT(ucred != NULL);
172
173         ucred->uc_valid = UCRED_INVALID;
174
175         nodemap = nodemap_get_from_exp(info->mti_exp);
176         if (IS_ERR(nodemap))
177                 RETURN(PTR_ERR(nodemap));
178
179         pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
180                                        NODEMAP_CLIENT_TO_FS, pud->pud_uid);
181         pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
182                                        NODEMAP_CLIENT_TO_FS, pud->pud_gid);
183         pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
184                                        NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
185         pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
186                                        NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
187
188         ucred->uc_o_uid = pud->pud_uid;
189         ucred->uc_o_gid = pud->pud_gid;
190         ucred->uc_o_fsuid = pud->pud_fsuid;
191         ucred->uc_o_fsgid = pud->pud_fsgid;
192
193         if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
194                 /* deny access before we get identity ref */
195                 if (nodemap->nmf_deny_unknown) {
196                         nodemap_putref(nodemap);
197                         RETURN(-EACCES);
198                 }
199
200                 ucred->uc_fsuid = nodemap->nm_squash_uid;
201                 ucred->uc_fsgid = nodemap->nm_squash_gid;
202                 ucred->uc_cap = 0;
203                 ucred->uc_suppgids[0] = -1;
204                 ucred->uc_suppgids[1] = -1;
205         }
206         nodemap_putref(nodemap);
207
208         if (type == BODY_INIT) {
209                 struct mdt_body *body = (struct mdt_body *)buf;
210
211                 ucred->uc_suppgids[0] = body->mbo_suppgid;
212                 ucred->uc_suppgids[1] = -1;
213         }
214
215         if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
216             req->rq_auth_uid != pud->pud_uid) {
217                 CDEBUG(D_SEC, "local client %s: auth uid %u "
218                        "while client claims %u:%u/%u:%u\n",
219                        libcfs_nid2str(peernid), req->rq_auth_uid,
220                        pud->pud_uid, pud->pud_gid,
221                        pud->pud_fsuid, pud->pud_fsgid);
222                 RETURN(-EACCES);
223         }
224
225         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
226                 ucred->uc_identity = NULL;
227                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
228         } else {
229                 struct md_identity *identity;
230
231                 identity = mdt_identity_get(mdt->mdt_identity_cache,
232                                             pud->pud_uid);
233                 if (IS_ERR(identity)) {
234                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
235                                 ucred->uc_identity = NULL;
236                                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
237                                        CFS_SETGRP_PERM;
238                         } else {
239                                 CDEBUG(D_SEC,
240                                        "Deny access without identity: uid %u\n",
241                                        pud->pud_uid);
242                                 RETURN(-EACCES);
243                         }
244                 } else {
245                         ucred->uc_identity = identity;
246                         perm = mdt_identity_get_perm(ucred->uc_identity,
247                                                      peernid);
248                 }
249         }
250
251         /* find out the setuid/setgid attempt */
252         setuid = (pud->pud_uid != pud->pud_fsuid);
253         setgid = ((pud->pud_gid != pud->pud_fsgid) ||
254                   (ucred->uc_identity &&
255                    (pud->pud_gid != ucred->uc_identity->mi_gid)));
256
257         /* check permission of setuid */
258         if (setuid && !(perm & CFS_SETUID_PERM)) {
259                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
260                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
261                 GOTO(out, rc = -EACCES);
262         }
263
264         /* check permission of setgid */
265         if (setgid && !(perm & CFS_SETGID_PERM)) {
266                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
267                        "from %s\n", pud->pud_uid, pud->pud_gid,
268                        pud->pud_fsuid, pud->pud_fsgid,
269                        ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
270                 GOTO(out, rc = -EACCES);
271         }
272
273         if (perm & CFS_SETGRP_PERM) {
274                 if (pud->pud_ngroups) {
275                         /* setgroups for local client */
276                         ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
277                         if (!ucred->uc_ginfo) {
278                                 CERROR("failed to alloc %d groups\n",
279                                        pud->pud_ngroups);
280                                 GOTO(out, rc = -ENOMEM);
281                         }
282
283                         lustre_groups_from_list(ucred->uc_ginfo,
284                                                 pud->pud_groups);
285                         lustre_groups_sort(ucred->uc_ginfo);
286                 } else {
287                         ucred->uc_ginfo = NULL;
288                 }
289         } else {
290                 ucred->uc_suppgids[0] = -1;
291                 ucred->uc_suppgids[1] = -1;
292                 ucred->uc_ginfo = NULL;
293         }
294
295         ucred->uc_uid = pud->pud_uid;
296         ucred->uc_gid = pud->pud_gid;
297         ucred->uc_fsuid = pud->pud_fsuid;
298         ucred->uc_fsgid = pud->pud_fsgid;
299
300         /* process root_squash here. */
301         mdt_root_squash(info, peernid);
302
303         /* remove fs privilege for non-root user. */
304         if (ucred->uc_fsuid && drop_fs_cap)
305                 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
306         else
307                 ucred->uc_cap = pud->pud_cap;
308         ucred->uc_valid = UCRED_NEW;
309         ucred_set_jobid(info, ucred);
310         ucred_set_nid(info, ucred);
311
312         EXIT;
313
314 out:
315         if (rc) {
316                 if (ucred->uc_ginfo) {
317                         put_group_info(ucred->uc_ginfo);
318                         ucred->uc_ginfo = NULL;
319                 }
320                 if (ucred->uc_identity) {
321                         mdt_identity_put(mdt->mdt_identity_cache,
322                                          ucred->uc_identity);
323                         ucred->uc_identity = NULL;
324                 }
325         }
326
327         return rc;
328 }
329
330 /**
331  * Check whether allow the client to set supplementary group IDs or not.
332  *
333  * \param[in] info      pointer to the thread context
334  * \param[in] uc        pointer to the RPC user descriptor
335  *
336  * \retval              true if allow to set supplementary group IDs
337  * \retval              false for other cases
338  */
339 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
340 {
341         __u32 perm;
342
343         /* 1. If identity_upcall is disabled,
344          *    permit local client to do anything. */
345         if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
346                 return true;
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,
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         int                      setuid;
372         int                      setgid;
373         int                      rc = 0;
374
375         ENTRY;
376
377         LASSERT(ucred != NULL);
378         if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
379                 RETURN(0);
380
381         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
382                 RETURN(0);
383
384         /* sanity check: if we use strong authentication, we expect the
385          * uid which client claimed is true */
386         if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
387             req->rq_auth_uid != pud->pud_uid) {
388                 CDEBUG(D_SEC, "local client %s: auth uid %u "
389                        "while client claims %u:%u/%u:%u\n",
390                        libcfs_nid2str(peernid), req->rq_auth_uid,
391                        pud->pud_uid, pud->pud_gid,
392                        pud->pud_fsuid, pud->pud_fsgid);
393                 RETURN(-EACCES);
394         }
395
396         if (is_identity_get_disabled(mdt->mdt_identity_cache))
397                 RETURN(0);
398
399         identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
400         if (IS_ERR(identity)) {
401                 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
402                         RETURN(0);
403                 } else {
404                         CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
405                                pud->pud_uid);
406                         RETURN(-EACCES);
407                 }
408         }
409
410         perm = mdt_identity_get_perm(identity, peernid);
411         /* find out the setuid/setgid attempt */
412         setuid = (pud->pud_uid != pud->pud_fsuid);
413         setgid = (pud->pud_gid != pud->pud_fsgid ||
414                   pud->pud_gid != identity->mi_gid);
415
416         /* check permission of setuid */
417         if (setuid && !(perm & CFS_SETUID_PERM)) {
418                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
419                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
420                 GOTO(out, rc = -EACCES);
421         }
422
423         /* check permission of setgid */
424         if (setgid && !(perm & CFS_SETGID_PERM)) {
425                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
426                        "from %s\n", pud->pud_uid, pud->pud_gid,
427                        pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
428                        libcfs_nid2str(peernid));
429                 GOTO(out, rc = -EACCES);
430         }
431
432         EXIT;
433
434 out:
435         mdt_identity_put(mdt->mdt_identity_cache, identity);
436         return rc;
437 }
438
439 static int old_init_ucred_common(struct mdt_thread_info *info,
440                                  struct lu_nodemap *nodemap,
441                                  bool drop_fs_cap)
442 {
443         struct lu_ucred         *uc = mdt_ucred(info);
444         struct mdt_device       *mdt = info->mti_mdt;
445         struct md_identity      *identity = NULL;
446
447         if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
448                 /* deny access before we get identity ref */
449                 if (nodemap->nmf_deny_unknown)
450                         RETURN(-EACCES);
451
452                 uc->uc_fsuid = nodemap->nm_squash_uid;
453                 uc->uc_fsgid = nodemap->nm_squash_gid;
454                 uc->uc_cap = 0;
455                 uc->uc_suppgids[0] = -1;
456                 uc->uc_suppgids[1] = -1;
457         }
458
459         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
460                 identity = mdt_identity_get(mdt->mdt_identity_cache,
461                                             uc->uc_fsuid);
462                 if (IS_ERR(identity)) {
463                         if (unlikely(PTR_ERR(identity) == -EREMCHG ||
464                                      uc->uc_cap & CFS_CAP_FS_MASK)) {
465                                 identity = NULL;
466                         } else {
467                                 CDEBUG(D_SEC, "Deny access without identity: "
468                                        "uid %u\n", uc->uc_fsuid);
469                                 RETURN(-EACCES);
470                         }
471                 }
472         }
473         uc->uc_identity = identity;
474
475         /* process root_squash here. */
476         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
477
478         /* remove fs privilege for non-root user. */
479         if (uc->uc_fsuid && drop_fs_cap)
480                 uc->uc_cap &= ~CFS_CAP_FS_MASK;
481         uc->uc_valid = UCRED_OLD;
482         ucred_set_jobid(info, uc);
483         ucred_set_nid(info, uc);
484
485         EXIT;
486
487         return 0;
488 }
489
490 static int old_init_ucred(struct mdt_thread_info *info,
491                           struct mdt_body *body, bool drop_fs_cap)
492 {
493         struct lu_ucred *uc = mdt_ucred(info);
494         struct lu_nodemap *nodemap;
495         int rc;
496         ENTRY;
497
498         nodemap = nodemap_get_from_exp(info->mti_exp);
499         if (IS_ERR(nodemap))
500                 RETURN(PTR_ERR(nodemap));
501
502         body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
503                                        NODEMAP_CLIENT_TO_FS, body->mbo_uid);
504         body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
505                                        NODEMAP_CLIENT_TO_FS, body->mbo_gid);
506         body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
507                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
508         body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
509                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
510
511         LASSERT(uc != NULL);
512         uc->uc_valid = UCRED_INVALID;
513         uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
514         uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
515         uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
516         uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
517         uc->uc_suppgids[0] = body->mbo_suppgid;
518         uc->uc_suppgids[1] = -1;
519         uc->uc_ginfo = NULL;
520         uc->uc_cap = body->mbo_capability;
521
522         rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
523         nodemap_putref(nodemap);
524
525         RETURN(rc);
526 }
527
528 static int old_init_ucred_reint(struct mdt_thread_info *info)
529 {
530         struct lu_ucred *uc = mdt_ucred(info);
531         struct lu_nodemap *nodemap;
532         int rc;
533         ENTRY;
534
535         nodemap = nodemap_get_from_exp(info->mti_exp);
536         if (IS_ERR(nodemap))
537                 RETURN(PTR_ERR(nodemap));
538
539         LASSERT(uc != NULL);
540
541         uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
542                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
543         uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
544                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
545
546         uc->uc_valid = UCRED_INVALID;
547         uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
548         uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
549         uc->uc_ginfo = NULL;
550
551         rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
552         nodemap_putref(nodemap);
553
554         RETURN(rc);
555 }
556
557 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
558                                    struct mdt_body *body,
559                                    bool drop_fs_cap)
560 {
561         struct ptlrpc_request   *req = mdt_info_req(info);
562         struct lu_ucred         *uc  = mdt_ucred(info);
563
564         LASSERT(uc != NULL);
565         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
566                 return 0;
567
568         mdt_exit_ucred(info);
569
570         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
571                 return old_init_ucred(info, body, drop_fs_cap);
572         else
573                 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
574 }
575
576 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
577 {
578         return __mdt_init_ucred(info, body, true);
579 }
580
581 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
582  * doesn't set correct fsuid explicitely, but raise capability to allow
583  * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
584  * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
585  * will fail permission check. */
586 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
587                                   struct mdt_body *body)
588 {
589         return __mdt_init_ucred(info, body, false);
590 }
591
592 int mdt_init_ucred_reint(struct mdt_thread_info *info)
593 {
594         struct ptlrpc_request *req = mdt_info_req(info);
595         struct lu_ucred       *uc  = mdt_ucred(info);
596         struct md_attr        *ma  = &info->mti_attr;
597
598         LASSERT(uc != NULL);
599         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
600                 return 0;
601
602         /* LU-5564: for normal close request, skip permission check */
603         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
604             !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
605                 uc->uc_cap |= CFS_CAP_FS_MASK;
606
607         mdt_exit_ucred(info);
608
609         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
610                 return old_init_ucred_reint(info);
611         else
612                 return new_init_ucred(info, REC_INIT, NULL, true);
613 }
614
615 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
616 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
617 {
618         const struct lov_ost_data_v1 *lod;
619         __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
620         __u16 count;
621         int i;
622
623         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
624                 return;
625
626         CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
627                POSTID(&lmm->lmm_oi), lmm_magic,
628                le32_to_cpu(lmm->lmm_pattern));
629
630         /* No support for compount layouts yet */
631         if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
632                 return;
633
634         count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
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,
664                "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
665                le32_to_cpu(lmm1->lmv_magic),
666                le32_to_cpu(lmm1->lmv_master_mdt_index),
667                le32_to_cpu(lmm1->lmv_stripe_count),
668                le32_to_cpu(lmm1->lmv_hash_type));
669
670         if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
671                 return;
672
673         for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
674                 struct lu_fid fid;
675
676                 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
677                 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
678         }
679 }
680
681 /* Shrink and/or grow reply buffers */
682 int mdt_fix_reply(struct mdt_thread_info *info)
683 {
684         struct req_capsule *pill = info->mti_pill;
685         struct mdt_body    *body;
686         int                md_size, md_packed = 0;
687         int                acl_size;
688         int                rc = 0;
689         ENTRY;
690
691         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
692         LASSERT(body != NULL);
693
694         if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
695                                OBD_MD_LINKNAME))
696                 md_size = body->mbo_eadatasize;
697         else
698                 md_size = 0;
699
700         acl_size = body->mbo_aclsize;
701
702         /* this replay - not send info to client */
703         if (info->mti_spec.no_create) {
704                 md_size = 0;
705                 acl_size = 0;
706         }
707
708         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
709                md_size, acl_size);
710 /*
711             &RMF_MDT_BODY,
712             &RMF_MDT_MD,
713             &RMF_ACL, or &RMF_LOGCOOKIES
714 (optional)  &RMF_CAPA1,
715 (optional)  &RMF_CAPA2,
716 (optional)  something else
717 */
718
719         /* MDT_MD buffer may be bigger than packed value, let's shrink all
720          * buffers before growing it */
721         if (info->mti_big_lmm_used) {
722                 /* big_lmm buffer may be used even without packing the result
723                  * into reply, just for internal server needs */
724                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
725                         md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
726                                                          RCL_SERVER);
727
728                 /* free big lmm if md_size is not needed */
729                 if (md_size == 0 || md_packed == 0) {
730                         info->mti_big_lmm_used = 0;
731                 } else {
732                         /* buffer must be allocated separately */
733                         LASSERT(info->mti_attr.ma_lmm !=
734                                 req_capsule_server_get(pill, &RMF_MDT_MD));
735                         req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
736                 }
737         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
738                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
739         }
740
741         if (info->mti_big_acl_used) {
742                 if (acl_size == 0)
743                         info->mti_big_acl_used = 0;
744                 else
745                         req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
746         } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
747                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
748         } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
749                 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
750         }
751
752         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
753             !(body->mbo_valid & OBD_MD_FLMDSCAPA))
754                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
755
756         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
757             !(body->mbo_valid & OBD_MD_FLOSSCAPA))
758                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
759
760         /*
761          * Some more field should be shrinked if needed.
762          * This should be done by those who added fields to reply message.
763          */
764
765         /* Grow MD buffer if needed finally */
766         if (info->mti_big_lmm_used) {
767                 void *lmm;
768
769                 LASSERT(md_size > md_packed);
770                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
771                        md_size - md_packed);
772                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
773                 if (rc) {
774                         /* we can't answer with proper LOV EA, drop flags,
775                          * the rc is also returned so this request is
776                          * considered as failed */
777                         body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
778                         /* don't return transno along with error */
779                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
780                 } else {
781                         /* now we need to pack right LOV/LMV EA */
782                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
783                         if (info->mti_attr.ma_valid & MA_LOV) {
784                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
785                                                              RCL_SERVER) ==
786                                                 info->mti_attr.ma_lmm_size);
787                                 memcpy(lmm, info->mti_attr.ma_lmm,
788                                        info->mti_attr.ma_lmm_size);
789                         } else if (info->mti_attr.ma_valid & MA_LMV) {
790                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
791                                                              RCL_SERVER) ==
792                                                 info->mti_attr.ma_lmv_size);
793                                 memcpy(lmm, info->mti_attr.ma_lmv,
794                                        info->mti_attr.ma_lmv_size);
795                         }
796                 }
797
798                 /* update mdt_max_mdsize so clients will be aware about that */
799                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
800                         info->mti_mdt->mdt_max_mdsize =
801                                                 info->mti_attr.ma_lmm_size;
802                 info->mti_big_lmm_used = 0;
803         }
804
805         if (info->mti_big_acl_used) {
806                 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
807                        acl_size);
808
809                 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
810                 if (rc) {
811                         body->mbo_valid &= ~OBD_MD_FLACL;
812                 } else {
813                         void *acl = req_capsule_server_get(pill, &RMF_ACL);
814
815                         memcpy(acl, info->mti_big_acl, acl_size);
816                 }
817
818                 info->mti_big_acl_used = 0;
819         }
820
821         RETURN(rc);
822 }
823
824
825 /* if object is dying, pack the lov/llog data,
826  * parameter info->mti_attr should be valid at this point!
827  * Also implements RAoLU policy */
828 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
829                            struct md_attr *ma)
830 {
831         struct mdt_body *repbody = NULL;
832         const struct lu_attr *la = &ma->ma_attr;
833         struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
834         int rc;
835         __u64 need = 0;
836         struct hsm_action_item hai = {
837                 .hai_len = sizeof(hai),
838                 .hai_action = HSMA_REMOVE,
839                 .hai_extent.length = -1,
840                 .hai_cookie = 0,
841                 .hai_gid = 0,
842         };
843         __u64 compound_id;
844         int archive_id;
845
846         ENTRY;
847
848         if (mdt_info_req(info) != NULL) {
849                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
850                 LASSERT(repbody != NULL);
851         } else {
852                 CDEBUG(D_INFO, "not running in a request/reply context\n");
853         }
854
855         if ((ma->ma_valid & MA_INODE) && repbody != NULL)
856                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
857
858         if (ma->ma_valid & MA_LOV) {
859                 CERROR("No need in LOV EA upon unlink\n");
860                 dump_stack();
861         }
862         if (repbody != NULL)
863                 repbody->mbo_eadatasize = 0;
864
865         /* Only check unlinked and archived if RAoLU and upon last close */
866         if (!cdt->cdt_remove_archive_on_last_unlink ||
867             atomic_read(&mo->mot_open_count) != 0)
868                 RETURN(0);
869
870         /* mdt_attr_get_complex will clear ma_valid, so check here first */
871         if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
872                 RETURN(0);
873
874         if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
875                 RETURN(0);
876
877         need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
878         if (need != 0) {
879                 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
880                  * try setting them */
881                 ma->ma_need |= need;
882                 rc = mdt_attr_get_complex(info, mo, ma);
883                 if (rc) {
884                         CERROR("%s: unable to fetch missing attributes of"
885                                DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
886                                PFID(mdt_object_fid(mo)), rc);
887                         RETURN(0);
888                 }
889
890                 if (need & MA_INODE) {
891                         if (ma->ma_valid & MA_INODE) {
892                                 if (ma->ma_attr.la_nlink != 0)
893                                         RETURN(0);
894                         } else {
895                                 RETURN(0);
896                         }
897                 }
898
899                 if (need & MA_HSM) {
900                         if (ma->ma_valid & MA_HSM) {
901                                 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
902                                         RETURN(0);
903                         } else {
904                                 RETURN(0);
905                         }
906                 }
907         }
908
909         /* RAoLU policy is active, last close on file has occured,
910          * file is unlinked, file is archived, so create remove request
911          * for copytool!
912          * If CDT is not running, requests will be logged for later. */
913         compound_id = atomic_inc_return(&cdt->cdt_compound_id);
914         if (ma->ma_hsm.mh_arch_id != 0)
915                 archive_id = ma->ma_hsm.mh_arch_id;
916         else
917                 archive_id = cdt->cdt_default_archive_id;
918
919         hai.hai_fid = *mdt_object_fid(mo);
920
921         rc = mdt_agent_record_add(info->mti_env, info->mti_mdt,
922                                   compound_id, archive_id, 0, &hai);
923         if (rc)
924                 CERROR("%s: unable to add HSM remove request for "DFID
925                        ": rc=%d\n", mdt_obd_name(info->mti_mdt),
926                        PFID(mdt_object_fid(mo)), rc);
927
928         RETURN(0);
929 }
930
931 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
932                                   struct md_attr *ma)
933 {
934         __u64 out;
935
936         out = 0;
937         if (in & MDS_ATTR_MODE)
938                 out |= LA_MODE;
939         if (in & MDS_ATTR_UID)
940                 out |= LA_UID;
941         if (in & MDS_ATTR_GID)
942                 out |= LA_GID;
943         if (in & MDS_ATTR_SIZE)
944                 out |= LA_SIZE;
945         if (in & MDS_ATTR_BLOCKS)
946                 out |= LA_BLOCKS;
947         if (in & MDS_ATTR_ATIME_SET)
948                 out |= LA_ATIME;
949         if (in & MDS_ATTR_CTIME_SET)
950                 out |= LA_CTIME;
951         if (in & MDS_ATTR_MTIME_SET)
952                 out |= LA_MTIME;
953         if (in & MDS_ATTR_ATTR_FLAG)
954                 out |= LA_FLAGS;
955         if (in & MDS_ATTR_KILL_SUID)
956                 out |= LA_KILL_SUID;
957         if (in & MDS_ATTR_KILL_SGID)
958                 out |= LA_KILL_SGID;
959         if (in & MDS_ATTR_PROJID)
960                 out |= LA_PROJID;
961
962         if (in & MDS_ATTR_FROM_OPEN)
963                 rr->rr_flags |= MRF_OPEN_TRUNC;
964         if (in & MDS_OPEN_OWNEROVERRIDE)
965                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
966         if (in & MDS_ATTR_FORCE)
967                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
968
969         in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
970                 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
971                 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
972                 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
973                 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
974                 MDS_ATTR_FROM_OPEN | MDS_OPEN_OWNEROVERRIDE);
975         if (in != 0)
976                 CERROR("Unknown attr bits: %#llx\n", in);
977         return out;
978 }
979
980 /* unpacking */
981
982 int mdt_name_unpack(struct req_capsule *pill,
983                     const struct req_msg_field *field,
984                     struct lu_name *ln,
985                     enum mdt_name_flags flags)
986 {
987         ln->ln_name = req_capsule_client_get(pill, field);
988         ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
989
990         if (!lu_name_is_valid(ln)) {
991                 ln->ln_name = NULL;
992                 ln->ln_namelen = 0;
993
994                 return -EPROTO;
995         }
996
997         if ((flags & MNF_FIX_ANON) &&
998             ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
999                 /* Newer (3.x) kernels use a name of "/" for the
1000                  * "anonymous" disconnected dentries from NFS
1001                  * filehandle conversion. See d_obtain_alias(). */
1002                 ln->ln_name = NULL;
1003                 ln->ln_namelen = 0;
1004         }
1005
1006         return 0;
1007 }
1008
1009 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1010                                   const char **secctx_name,
1011                                   void **secctx, size_t *secctx_size)
1012 {
1013         const char *name;
1014         size_t name_size;
1015
1016         *secctx_name = NULL;
1017         *secctx = NULL;
1018         *secctx_size = 0;
1019
1020         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1021             !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1022                 return 0;
1023
1024         name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1025                                          RCL_CLIENT);
1026         if (name_size == 0)
1027                 return 0;
1028
1029         name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1030         if (strnlen(name, name_size) != name_size - 1)
1031                 return -EPROTO;
1032
1033         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1034             !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1035                 return -EPROTO;
1036
1037         *secctx_name = name;
1038         *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1039         *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1040
1041         return 0;
1042 }
1043
1044 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1045 {
1046         struct lu_ucred *uc = mdt_ucred(info);
1047         struct md_attr *ma = &info->mti_attr;
1048         struct lu_attr *la = &ma->ma_attr;
1049         struct req_capsule *pill = info->mti_pill;
1050         struct mdt_reint_record *rr = &info->mti_rr;
1051         struct mdt_rec_setattr *rec;
1052         struct lu_nodemap *nodemap;
1053
1054         ENTRY;
1055
1056         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1057         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1058         if (rec == NULL)
1059                 RETURN(-EFAULT);
1060
1061         /* This prior initialization is needed for old_init_ucred_reint() */
1062         uc->uc_fsuid = rec->sa_fsuid;
1063         uc->uc_fsgid = rec->sa_fsgid;
1064         uc->uc_cap   = rec->sa_cap;
1065         uc->uc_suppgids[0] = rec->sa_suppgid;
1066         uc->uc_suppgids[1] = -1;
1067
1068         rr->rr_fid1 = &rec->sa_fid;
1069         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1070         la->la_mode  = rec->sa_mode;
1071         la->la_flags = rec->sa_attr_flags;
1072
1073         nodemap = nodemap_get_from_exp(info->mti_exp);
1074         if (IS_ERR(nodemap))
1075                 RETURN(PTR_ERR(nodemap));
1076
1077         la->la_uid   = nodemap_map_id(nodemap, NODEMAP_UID,
1078                                       NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1079         la->la_gid   = nodemap_map_id(nodemap, NODEMAP_GID,
1080                                       NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1081         la->la_projid = rec->sa_projid;
1082         nodemap_putref(nodemap);
1083
1084         la->la_size  = rec->sa_size;
1085         la->la_blocks = rec->sa_blocks;
1086         la->la_ctime = rec->sa_ctime;
1087         la->la_atime = rec->sa_atime;
1088         la->la_mtime = rec->sa_mtime;
1089         ma->ma_valid = MA_INODE;
1090
1091         if (rec->sa_bias & MDS_DATA_MODIFIED)
1092                 ma->ma_attr_flags |= MDS_DATA_MODIFIED;
1093         else
1094                 ma->ma_attr_flags &= ~MDS_DATA_MODIFIED;
1095
1096         ma->ma_attr_flags &= ~MDS_CLOSE_INTENT;
1097         ma->ma_attr_flags |= rec->sa_bias & MDS_CLOSE_INTENT;
1098         RETURN(0);
1099 }
1100
1101 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1102 {
1103         struct req_capsule *pill = info->mti_pill;
1104         struct mdt_ioepoch *ioepoch;
1105         ENTRY;
1106
1107         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1108                 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1109         else
1110                 ioepoch = NULL;
1111
1112         if (ioepoch == NULL)
1113                 RETURN(-EPROTO);
1114
1115         info->mti_close_handle = ioepoch->mio_handle;
1116
1117         RETURN(0);
1118 }
1119
1120 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1121         struct req_capsule      *pill = info->mti_pill;
1122
1123         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1124                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1125                 if (info->mti_dlm_req == NULL)
1126                         RETURN(-EFAULT);
1127         }
1128
1129         RETURN(0);
1130 }
1131
1132 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1133 {
1134         struct mdt_reint_record *rr = &info->mti_rr;
1135         struct md_attr          *ma = &info->mti_attr;
1136         struct req_capsule      *pill = info->mti_pill;
1137         int rc;
1138         ENTRY;
1139
1140         rc = mdt_setattr_unpack_rec(info);
1141         if (rc)
1142                 RETURN(rc);
1143
1144         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1145                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1146                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1147                                                         RCL_CLIENT);
1148                 if (rr->rr_eadatalen > 0) {
1149                         const struct lmv_user_md        *lum;
1150
1151                         lum = rr->rr_eadata;
1152                         /* Sigh ma_valid(from req) does not indicate whether
1153                          * it will set LOV/LMV EA, so we have to check magic */
1154                         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1155                                 ma->ma_valid |= MA_LMV;
1156                                 ma->ma_lmv = (void *)rr->rr_eadata;
1157                                 ma->ma_lmv_size = rr->rr_eadatalen;
1158                         } else {
1159                                 ma->ma_valid |= MA_LOV;
1160                                 ma->ma_lmm = (void *)rr->rr_eadata;
1161                                 ma->ma_lmm_size = rr->rr_eadatalen;
1162                         }
1163                 }
1164         }
1165
1166         rc = mdt_dlmreq_unpack(info);
1167         RETURN(rc);
1168 }
1169
1170 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1171 {
1172         struct md_attr          *ma = &info->mti_attr;
1173         struct req_capsule      *pill = info->mti_pill;
1174         ENTRY;
1175
1176         if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1177                 RETURN(0);
1178
1179         req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1180
1181         if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1182             req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1183                 RETURN(-EFAULT);
1184
1185         RETURN(0);
1186 }
1187
1188 int mdt_close_unpack(struct mdt_thread_info *info)
1189 {
1190         int rc;
1191         ENTRY;
1192
1193         rc = mdt_close_handle_unpack(info);
1194         if (rc)
1195                 RETURN(rc);
1196
1197         rc = mdt_setattr_unpack_rec(info);
1198         if (rc)
1199                 RETURN(rc);
1200
1201         rc = mdt_close_intent_unpack(info);
1202         if (rc)
1203                 RETURN(rc);
1204
1205         RETURN(mdt_init_ucred_reint(info));
1206 }
1207
1208 static int mdt_create_unpack(struct mdt_thread_info *info)
1209 {
1210         struct lu_ucred *uc  = mdt_ucred(info);
1211         struct mdt_rec_create *rec;
1212         struct lu_attr *attr = &info->mti_attr.ma_attr;
1213         struct mdt_reint_record *rr = &info->mti_rr;
1214         struct req_capsule *pill = info->mti_pill;
1215         struct md_op_spec *sp = &info->mti_spec;
1216         int rc;
1217
1218         ENTRY;
1219
1220         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1221         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1222         if (rec == NULL)
1223                 RETURN(-EFAULT);
1224
1225         /* This prior initialization is needed for old_init_ucred_reint() */
1226         uc->uc_fsuid = rec->cr_fsuid;
1227         uc->uc_fsgid = rec->cr_fsgid;
1228         uc->uc_cap   = rec->cr_cap;
1229         uc->uc_suppgids[0] = rec->cr_suppgid1;
1230         uc->uc_suppgids[1] = -1;
1231         uc->uc_umask = rec->cr_umask;
1232
1233         rr->rr_fid1 = &rec->cr_fid1;
1234         rr->rr_fid2 = &rec->cr_fid2;
1235         attr->la_mode = rec->cr_mode;
1236         attr->la_rdev  = rec->cr_rdev;
1237         attr->la_uid   = rec->cr_fsuid;
1238         attr->la_gid   = rec->cr_fsgid;
1239         attr->la_ctime = rec->cr_time;
1240         attr->la_mtime = rec->cr_time;
1241         attr->la_atime = rec->cr_time;
1242         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1243                          LA_CTIME | LA_MTIME | LA_ATIME;
1244         memset(&sp->u, 0, sizeof(sp->u));
1245         sp->sp_cr_flags = get_mrc_cr_flags(rec);
1246
1247         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1248         if (rc < 0)
1249                 RETURN(rc);
1250
1251         if (S_ISLNK(attr->la_mode)) {
1252                 const char *tgt = NULL;
1253
1254                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1255                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1256                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1257                         sp->u.sp_symname = tgt;
1258                 }
1259                 if (tgt == NULL)
1260                         RETURN(-EFAULT);
1261         } else {
1262                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1263                 if (S_ISDIR(attr->la_mode) &&
1264                     req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1265                         sp->u.sp_ea.eadata =
1266                                 req_capsule_client_get(pill, &RMF_EADATA);
1267                         sp->u.sp_ea.eadatalen =
1268                                 req_capsule_get_size(pill, &RMF_EADATA,
1269                                                      RCL_CLIENT);
1270                         sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1271                 }
1272         }
1273
1274         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1275                                     &sp->sp_cr_file_secctx,
1276                                     &sp->sp_cr_file_secctx_size);
1277         if (rc < 0)
1278                 RETURN(rc);
1279
1280         rc = mdt_dlmreq_unpack(info);
1281         RETURN(rc);
1282 }
1283
1284 static int mdt_link_unpack(struct mdt_thread_info *info)
1285 {
1286         struct lu_ucred *uc  = mdt_ucred(info);
1287         struct mdt_rec_link *rec;
1288         struct lu_attr *attr = &info->mti_attr.ma_attr;
1289         struct mdt_reint_record *rr = &info->mti_rr;
1290         struct req_capsule *pill = info->mti_pill;
1291         int rc;
1292
1293         ENTRY;
1294
1295         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1296         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1297         if (rec == NULL)
1298                 RETURN(-EFAULT);
1299
1300         /* This prior initialization is needed for old_init_ucred_reint() */
1301         uc->uc_fsuid = rec->lk_fsuid;
1302         uc->uc_fsgid = rec->lk_fsgid;
1303         uc->uc_cap   = rec->lk_cap;
1304         uc->uc_suppgids[0] = rec->lk_suppgid1;
1305         uc->uc_suppgids[1] = rec->lk_suppgid2;
1306
1307         attr->la_uid = rec->lk_fsuid;
1308         attr->la_gid = rec->lk_fsgid;
1309         rr->rr_fid1 = &rec->lk_fid1;
1310         rr->rr_fid2 = &rec->lk_fid2;
1311         attr->la_ctime = rec->lk_time;
1312         attr->la_mtime = rec->lk_time;
1313         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1314
1315         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1316         if (rc < 0)
1317                 RETURN(rc);
1318
1319         rc = mdt_dlmreq_unpack(info);
1320
1321         RETURN(rc);
1322 }
1323
1324 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1325 {
1326         struct lu_ucred *uc  = mdt_ucred(info);
1327         struct mdt_rec_unlink *rec;
1328         struct md_attr *ma = &info->mti_attr;
1329         struct lu_attr *attr = &info->mti_attr.ma_attr;
1330         struct mdt_reint_record *rr = &info->mti_rr;
1331         struct req_capsule *pill = info->mti_pill;
1332         int rc;
1333
1334         ENTRY;
1335
1336         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1337         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1338         if (rec == NULL)
1339                 RETURN(-EFAULT);
1340
1341         /* This prior initialization is needed for old_init_ucred_reint() */
1342         uc->uc_fsuid = rec->ul_fsuid;
1343         uc->uc_fsgid = rec->ul_fsgid;
1344         uc->uc_cap   = rec->ul_cap;
1345         uc->uc_suppgids[0] = rec->ul_suppgid1;
1346         uc->uc_suppgids[1] = -1;
1347
1348         attr->la_uid = rec->ul_fsuid;
1349         attr->la_gid = rec->ul_fsgid;
1350         rr->rr_fid1 = &rec->ul_fid1;
1351         rr->rr_fid2 = &rec->ul_fid2;
1352         attr->la_ctime = rec->ul_time;
1353         attr->la_mtime = rec->ul_time;
1354         attr->la_mode  = rec->ul_mode;
1355         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1356
1357         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1358         if (rc < 0)
1359                 RETURN(rc);
1360
1361         if (rec->ul_bias & MDS_VTX_BYPASS)
1362                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1363         else
1364                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1365
1366         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1367
1368         rc = mdt_dlmreq_unpack(info);
1369         RETURN(rc);
1370 }
1371
1372 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1373 {
1374         info->mti_spec.sp_rm_entry = 1;
1375         return mdt_unlink_unpack(info);
1376 }
1377
1378 static int mdt_rename_unpack(struct mdt_thread_info *info)
1379 {
1380         struct lu_ucred *uc = mdt_ucred(info);
1381         struct mdt_rec_rename *rec;
1382         struct md_attr *ma = &info->mti_attr;
1383         struct lu_attr *attr = &info->mti_attr.ma_attr;
1384         struct mdt_reint_record *rr = &info->mti_rr;
1385         struct req_capsule *pill = info->mti_pill;
1386         int rc;
1387
1388         ENTRY;
1389
1390         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1391         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1392         if (rec == NULL)
1393                 RETURN(-EFAULT);
1394
1395         /* This prior initialization is needed for old_init_ucred_reint() */
1396         uc->uc_fsuid = rec->rn_fsuid;
1397         uc->uc_fsgid = rec->rn_fsgid;
1398         uc->uc_cap   = rec->rn_cap;
1399         uc->uc_suppgids[0] = rec->rn_suppgid1;
1400         uc->uc_suppgids[1] = rec->rn_suppgid2;
1401
1402         attr->la_uid = rec->rn_fsuid;
1403         attr->la_gid = rec->rn_fsgid;
1404         rr->rr_fid1 = &rec->rn_fid1;
1405         rr->rr_fid2 = &rec->rn_fid2;
1406         attr->la_ctime = rec->rn_time;
1407         attr->la_mtime = rec->rn_time;
1408         /* rename_tgt contains the mode already */
1409         attr->la_mode = rec->rn_mode;
1410         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1411
1412         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1413         if (rc < 0)
1414                 RETURN(rc);
1415
1416         rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1417         if (rc < 0)
1418                 RETURN(rc);
1419
1420         if (rec->rn_bias & MDS_VTX_BYPASS)
1421                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1422         else
1423                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1424
1425         if (rec->rn_bias & MDS_RENAME_MIGRATE) {
1426                 req_capsule_extend(info->mti_pill, &RQF_MDS_REINT_MIGRATE);
1427                 rc = mdt_close_handle_unpack(info);
1428                 if (rc < 0)
1429                         RETURN(rc);
1430                 info->mti_spec.sp_migrate_close = 1;
1431         }
1432
1433         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1434
1435
1436         rc = mdt_dlmreq_unpack(info);
1437
1438         RETURN(rc);
1439 }
1440
1441 /*
1442  * please see comment above LOV_MAGIC_V1_DEFINED
1443  */
1444 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1445 {
1446         struct lov_user_md_v1   *v1 = eadata;
1447
1448         LASSERT(v1);
1449
1450         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1451                 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1452                         v1->lmm_magic |= LOV_MAGIC_DEFINED;
1453                 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1454                          __swab32(LOV_MAGIC_MAGIC))
1455                         v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1456         }
1457 }
1458
1459 static int mdt_open_unpack(struct mdt_thread_info *info)
1460 {
1461         struct lu_ucred *uc = mdt_ucred(info);
1462         struct mdt_rec_create *rec;
1463         struct lu_attr *attr = &info->mti_attr.ma_attr;
1464         struct req_capsule *pill = info->mti_pill;
1465         struct mdt_reint_record *rr = &info->mti_rr;
1466         struct ptlrpc_request *req = mdt_info_req(info);
1467         struct md_op_spec *sp = &info->mti_spec;
1468         int rc;
1469         ENTRY;
1470
1471         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1472         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1473         if (rec == NULL)
1474                 RETURN(-EFAULT);
1475
1476         /* This prior initialization is needed for old_init_ucred_reint() */
1477         uc->uc_fsuid = rec->cr_fsuid;
1478         uc->uc_fsgid = rec->cr_fsgid;
1479         uc->uc_cap   = rec->cr_cap;
1480         uc->uc_suppgids[0] = rec->cr_suppgid1;
1481         uc->uc_suppgids[1] = rec->cr_suppgid2;
1482         uc->uc_umask = rec->cr_umask;
1483
1484         rr->rr_fid1   = &rec->cr_fid1;
1485         rr->rr_fid2   = &rec->cr_fid2;
1486         rr->rr_handle = &rec->cr_old_handle;
1487         attr->la_mode = rec->cr_mode;
1488         attr->la_rdev  = rec->cr_rdev;
1489         attr->la_uid   = rec->cr_fsuid;
1490         attr->la_gid   = rec->cr_fsgid;
1491         attr->la_ctime = rec->cr_time;
1492         attr->la_mtime = rec->cr_time;
1493         attr->la_atime = rec->cr_time;
1494         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1495                          LA_CTIME | LA_MTIME | LA_ATIME;
1496         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1497         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1498         /* Do not trigger ASSERTION if client miss to set such flags. */
1499         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1500                 RETURN(-EPROTO);
1501
1502         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1503
1504         mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1505
1506         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1507                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1508                                                         RCL_CLIENT);
1509                 if (rr->rr_eadatalen > 0) {
1510                         rr->rr_eadata = req_capsule_client_get(pill,
1511                                                                &RMF_EADATA);
1512                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1513                         sp->u.sp_ea.eadata = rr->rr_eadata;
1514                         sp->no_create = !!req_is_replay(req);
1515                         mdt_fix_lov_magic(info, rr->rr_eadata);
1516                 }
1517
1518                 /*
1519                  * Client default md_size may be 0 right after client start,
1520                  * until all osc are connected, set here just some reasonable
1521                  * value to prevent misbehavior.
1522                  */
1523                 if (rr->rr_eadatalen == 0 &&
1524                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1525                         rr->rr_eadatalen = MIN_MD_SIZE;
1526         }
1527
1528         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1529                                     &sp->sp_cr_file_secctx,
1530                                     &sp->sp_cr_file_secctx_size);
1531
1532         RETURN(rc);
1533 }
1534
1535 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1536 {
1537         struct mdt_reint_record *rr = &info->mti_rr;
1538         struct lu_ucred *uc = mdt_ucred(info);
1539         struct lu_attr *attr = &info->mti_attr.ma_attr;
1540         struct req_capsule *pill = info->mti_pill;
1541         struct mdt_rec_setxattr *rec;
1542         int rc;
1543         ENTRY;
1544
1545
1546         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1547                  sizeof(struct mdt_rec_reint));
1548
1549         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1550         if (rec == NULL)
1551                 RETURN(-EFAULT);
1552
1553         /* This prior initialization is needed for old_init_ucred_reint() */
1554         uc->uc_fsuid  = rec->sx_fsuid;
1555         uc->uc_fsgid  = rec->sx_fsgid;
1556         uc->uc_cap    = rec->sx_cap;
1557         uc->uc_suppgids[0] = rec->sx_suppgid1;
1558         uc->uc_suppgids[1] = -1;
1559
1560         rr->rr_opcode = rec->sx_opcode;
1561         rr->rr_fid1   = &rec->sx_fid;
1562         attr->la_valid = rec->sx_valid;
1563         attr->la_ctime = rec->sx_time;
1564         attr->la_size = rec->sx_size;
1565         attr->la_flags = rec->sx_flags;
1566
1567         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1568         if (rc < 0)
1569                 RETURN(rc);
1570
1571         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1572                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1573                                                         RCL_CLIENT);
1574                 if (rr->rr_eadatalen > 0) {
1575                         rr->rr_eadata = req_capsule_client_get(pill,
1576                                                                &RMF_EADATA);
1577                         if (rr->rr_eadata == NULL)
1578                                 RETURN(-EFAULT);
1579                 } else {
1580                         rr->rr_eadata = NULL;
1581                 }
1582         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1583                 CDEBUG(D_INFO, "no xattr data supplied\n");
1584                 RETURN(-EFAULT);
1585         }
1586
1587         if (mdt_dlmreq_unpack(info) < 0)
1588                 RETURN(-EPROTO);
1589
1590         RETURN(0);
1591 }
1592
1593 static int mdt_resync_unpack(struct mdt_thread_info *info)
1594 {
1595         struct req_capsule      *pill = info->mti_pill;
1596         struct mdt_reint_record *rr   = &info->mti_rr;
1597         struct lu_ucred         *uc     = mdt_ucred(info);
1598         struct mdt_rec_resync   *rec;
1599         ENTRY;
1600
1601         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1602         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1603         if (rec == NULL)
1604                 RETURN(-EFAULT);
1605
1606         /* This prior initialization is needed for old_init_ucred_reint() */
1607         uc->uc_fsuid = rec->rs_fsuid;
1608         uc->uc_fsgid = rec->rs_fsgid;
1609         uc->uc_cap   = rec->rs_cap;
1610
1611         rr->rr_fid1   = &rec->rs_fid;
1612
1613         /* cookie doesn't need to be swapped but it has been swapped
1614          * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1615          * restoring. */
1616         if (ptlrpc_req_need_swab(mdt_info_req(info)))
1617                 __swab64s(&rec->rs_handle.cookie);
1618         rr->rr_handle = &rec->rs_handle;
1619
1620         RETURN(mdt_dlmreq_unpack(info));
1621 }
1622
1623 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1624
1625 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1626         [REINT_SETATTR]  = mdt_setattr_unpack,
1627         [REINT_CREATE]   = mdt_create_unpack,
1628         [REINT_LINK]     = mdt_link_unpack,
1629         [REINT_UNLINK]   = mdt_unlink_unpack,
1630         [REINT_RENAME]   = mdt_rename_unpack,
1631         [REINT_OPEN]     = mdt_open_unpack,
1632         [REINT_SETXATTR] = mdt_setxattr_unpack,
1633         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1634         [REINT_MIGRATE]  = mdt_rename_unpack,
1635         [REINT_RESYNC]   = mdt_resync_unpack,
1636 };
1637
1638 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1639 {
1640         int rc;
1641         ENTRY;
1642
1643         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1644         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1645                 info->mti_rr.rr_opcode = op;
1646                 rc = mdt_reint_unpackers[op](info);
1647         } else {
1648                 CERROR("Unexpected opcode %d\n", op);
1649                 rc = -EFAULT;
1650         }
1651         RETURN(rc);
1652 }