Whamcloud - gitweb
f3a0d42afe857dd74f40846b243a9936219dc7d0
[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 #include <linux/uidgid.h>
49
50 #include "mdt_internal.h"
51 #include <uapi/linux/lnet/nidstr.h>
52 #include <lustre_nodemap.h>
53
54 typedef enum ucred_init_type {
55         NONE_INIT       = 0,
56         BODY_INIT       = 1,
57         REC_INIT        = 2
58 } ucred_init_type_t;
59
60 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
61 {
62         return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
63 }
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 spinlock *rsi_lock,
87                                struct list_head *nidlist,
88                                lnet_nid_t peernid)
89 {
90         int rc;
91         ENTRY;
92         spin_lock(rsi_lock);
93         rc = cfs_match_nid(peernid, nidlist);
94         spin_unlock(rsi_lock);
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_lock,
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 void ucred_set_nid(struct mdt_thread_info *info, struct lu_ucred *uc)
144 {
145         if (info && info->mti_exp && info->mti_exp->exp_connection)
146                 uc->uc_nid = info->mti_exp->exp_connection->c_peer.nid;
147         else
148                 uc->uc_nid = LNET_NID_ANY;
149 }
150
151 static void ucred_set_audit_enabled(struct mdt_thread_info *info,
152                                     struct lu_ucred *uc)
153 {
154         struct lu_nodemap *nodemap = NULL;
155         bool audit = true;
156
157         if (info && info->mti_exp) {
158                 nodemap = nodemap_get_from_exp(info->mti_exp);
159                 if (nodemap && !IS_ERR(nodemap)) {
160                         audit = nodemap->nmf_enable_audit;
161                         nodemap_putref(nodemap);
162                 }
163         }
164
165         uc->uc_enable_audit = audit;
166 }
167
168 static int new_init_ucred(struct mdt_thread_info *info, ucred_init_type_t type,
169                           void *buf, bool drop_fs_cap)
170 {
171         struct ptlrpc_request *req = mdt_info_req(info);
172         struct mdt_device *mdt = info->mti_mdt;
173         struct ptlrpc_user_desc *pud = req->rq_user_desc;
174         struct lu_ucred *ucred = mdt_ucred(info);
175         struct lu_nodemap *nodemap;
176         lnet_nid_t peernid = req->rq_peer.nid;
177         __u32 perm = 0;
178         int setuid;
179         int setgid;
180         bool is_nm_gid_squashed = false;
181         int rc = 0;
182
183         ENTRY;
184
185         LASSERT(req->rq_auth_gss);
186         LASSERT(!req->rq_auth_usr_mdt);
187         LASSERT(req->rq_user_desc);
188         LASSERT(ucred != NULL);
189
190         ucred->uc_valid = UCRED_INVALID;
191
192         nodemap = nodemap_get_from_exp(info->mti_exp);
193         if (IS_ERR(nodemap))
194                 RETURN(PTR_ERR(nodemap));
195
196         pud->pud_uid = nodemap_map_id(nodemap, NODEMAP_UID,
197                                        NODEMAP_CLIENT_TO_FS, pud->pud_uid);
198         pud->pud_gid = nodemap_map_id(nodemap, NODEMAP_GID,
199                                        NODEMAP_CLIENT_TO_FS, pud->pud_gid);
200         pud->pud_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
201                                        NODEMAP_CLIENT_TO_FS, pud->pud_fsuid);
202         pud->pud_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
203                                        NODEMAP_CLIENT_TO_FS, pud->pud_fsgid);
204
205         ucred->uc_o_uid = pud->pud_uid;
206         ucred->uc_o_gid = pud->pud_gid;
207         ucred->uc_o_fsuid = pud->pud_fsuid;
208         ucred->uc_o_fsgid = pud->pud_fsgid;
209
210         if (nodemap && ucred->uc_o_uid == nodemap->nm_squash_uid) {
211                 /* deny access before we get identity ref */
212                 if (nodemap->nmf_deny_unknown) {
213                         nodemap_putref(nodemap);
214                         RETURN(-EACCES);
215                 }
216
217                 ucred->uc_fsuid = nodemap->nm_squash_uid;
218                 ucred->uc_fsgid = nodemap->nm_squash_gid;
219                 ucred->uc_cap = 0;
220                 ucred->uc_suppgids[0] = -1;
221                 ucred->uc_suppgids[1] = -1;
222         }
223
224         if (nodemap && ucred->uc_o_gid == nodemap->nm_squash_gid)
225                 is_nm_gid_squashed = true;
226
227         nodemap_putref(nodemap);
228
229         if (type == BODY_INIT) {
230                 struct mdt_body *body = (struct mdt_body *)buf;
231
232                 ucred->uc_suppgids[0] = body->mbo_suppgid;
233                 ucred->uc_suppgids[1] = -1;
234         }
235
236         if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
237             req->rq_auth_uid != pud->pud_uid) {
238                 CDEBUG(D_SEC, "local client %s: auth uid %u "
239                        "while client claims %u:%u/%u:%u\n",
240                        libcfs_nid2str(peernid), req->rq_auth_uid,
241                        pud->pud_uid, pud->pud_gid,
242                        pud->pud_fsuid, pud->pud_fsgid);
243                 RETURN(-EACCES);
244         }
245
246         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
247                 ucred->uc_identity = NULL;
248                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM | CFS_SETGRP_PERM;
249         } else {
250                 struct md_identity *identity;
251
252                 identity = mdt_identity_get(mdt->mdt_identity_cache,
253                                             pud->pud_uid);
254                 if (IS_ERR(identity)) {
255                         if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
256                                 ucred->uc_identity = NULL;
257                                 perm = CFS_SETUID_PERM | CFS_SETGID_PERM |
258                                        CFS_SETGRP_PERM;
259                         } else {
260                                 CDEBUG(D_SEC,
261                                        "Deny access without identity: uid %u\n",
262                                        pud->pud_uid);
263                                 RETURN(-EACCES);
264                         }
265                 } else {
266                         ucred->uc_identity = identity;
267                         perm = mdt_identity_get_perm(ucred->uc_identity,
268                                                      peernid);
269                 }
270         }
271
272         /* find out the setuid/setgid attempt */
273         setuid = (pud->pud_uid != pud->pud_fsuid);
274         setgid = ((pud->pud_gid != pud->pud_fsgid) ||
275                   (ucred->uc_identity &&
276                    (pud->pud_gid != ucred->uc_identity->mi_gid)));
277
278         /* check permission of setuid */
279         if (setuid && !(perm & CFS_SETUID_PERM)) {
280                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
281                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
282                 GOTO(out, rc = -EACCES);
283         }
284
285         /* check permission of setgid */
286         if (setgid && !(perm & CFS_SETGID_PERM)) {
287                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
288                        "from %s\n", pud->pud_uid, pud->pud_gid,
289                        pud->pud_fsuid, pud->pud_fsgid,
290                        ucred->uc_identity->mi_gid, libcfs_nid2str(peernid));
291                 GOTO(out, rc = -EACCES);
292         }
293
294         if (perm & CFS_SETGRP_PERM) {
295                 /* only set groups if GID is not squashed */
296                 if (pud->pud_ngroups && !is_nm_gid_squashed) {
297                         /* setgroups for local client */
298                         ucred->uc_ginfo = groups_alloc(pud->pud_ngroups);
299                         if (!ucred->uc_ginfo) {
300                                 CERROR("failed to alloc %d groups\n",
301                                        pud->pud_ngroups);
302                                 GOTO(out, rc = -ENOMEM);
303                         }
304
305                         lustre_groups_from_list(ucred->uc_ginfo,
306                                                 pud->pud_groups);
307                         lustre_groups_sort(ucred->uc_ginfo);
308                 } else {
309                         ucred->uc_suppgids[0] = -1;
310                         ucred->uc_suppgids[1] = -1;
311                         ucred->uc_ginfo = NULL;
312                 }
313         } else {
314                 ucred->uc_suppgids[0] = -1;
315                 ucred->uc_suppgids[1] = -1;
316                 ucred->uc_ginfo = NULL;
317         }
318
319         ucred->uc_uid = pud->pud_uid;
320         ucred->uc_gid = pud->pud_gid;
321         ucred->uc_fsuid = pud->pud_fsuid;
322         ucred->uc_fsgid = pud->pud_fsgid;
323
324         /* process root_squash here. */
325         mdt_root_squash(info, peernid);
326
327         /* remove fs privilege for non-root user. */
328         if (ucred->uc_fsuid && drop_fs_cap)
329                 ucred->uc_cap = pud->pud_cap & ~CFS_CAP_FS_MASK;
330         else
331                 ucred->uc_cap = pud->pud_cap;
332         ucred->uc_valid = UCRED_NEW;
333         ucred_set_jobid(info, ucred);
334         ucred_set_nid(info, ucred);
335         ucred_set_audit_enabled(info, ucred);
336
337         EXIT;
338
339 out:
340         if (rc) {
341                 if (ucred->uc_ginfo) {
342                         put_group_info(ucred->uc_ginfo);
343                         ucred->uc_ginfo = NULL;
344                 }
345                 if (ucred->uc_identity) {
346                         mdt_identity_put(mdt->mdt_identity_cache,
347                                          ucred->uc_identity);
348                         ucred->uc_identity = NULL;
349                 }
350         }
351
352         return rc;
353 }
354
355 /**
356  * Check whether allow the client to set supplementary group IDs or not.
357  *
358  * \param[in] info      pointer to the thread context
359  * \param[in] uc        pointer to the RPC user descriptor
360  *
361  * \retval              true if allow to set supplementary group IDs
362  * \retval              false for other cases
363  */
364 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc)
365 {
366         __u32 perm;
367
368         /* 1. If identity_upcall is disabled,
369          *    permit local client to do anything. */
370         if (is_identity_get_disabled(info->mti_mdt->mdt_identity_cache))
371                 return true;
372
373         /* 2. If fail to get related identities, then forbid any client to
374          *    set supplementary group IDs. */
375         if (uc->uc_identity == NULL)
376                 return false;
377
378         /* 3. Check the permission in the identities. */
379         perm = mdt_identity_get_perm(uc->uc_identity,
380                                      mdt_info_req(info)->rq_peer.nid);
381         if (perm & CFS_SETGRP_PERM)
382                 return true;
383
384         return false;
385 }
386
387 int mdt_check_ucred(struct mdt_thread_info *info)
388 {
389         struct ptlrpc_request   *req = mdt_info_req(info);
390         struct mdt_device       *mdt = info->mti_mdt;
391         struct ptlrpc_user_desc *pud = req->rq_user_desc;
392         struct lu_ucred         *ucred = mdt_ucred(info);
393         struct md_identity      *identity = NULL;
394         lnet_nid_t               peernid = req->rq_peer.nid;
395         __u32                    perm = 0;
396         int                      setuid;
397         int                      setgid;
398         int                      rc = 0;
399
400         ENTRY;
401
402         LASSERT(ucred != NULL);
403         if ((ucred->uc_valid == UCRED_OLD) || (ucred->uc_valid == UCRED_NEW))
404                 RETURN(0);
405
406         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
407                 RETURN(0);
408
409         /* sanity check: if we use strong authentication, we expect the
410          * uid which client claimed is true */
411         if (!flvr_is_rootonly(req->rq_flvr.sf_rpc) &&
412             req->rq_auth_uid != pud->pud_uid) {
413                 CDEBUG(D_SEC, "local client %s: auth uid %u "
414                        "while client claims %u:%u/%u:%u\n",
415                        libcfs_nid2str(peernid), req->rq_auth_uid,
416                        pud->pud_uid, pud->pud_gid,
417                        pud->pud_fsuid, pud->pud_fsgid);
418                 RETURN(-EACCES);
419         }
420
421         if (is_identity_get_disabled(mdt->mdt_identity_cache))
422                 RETURN(0);
423
424         identity = mdt_identity_get(mdt->mdt_identity_cache, pud->pud_uid);
425         if (IS_ERR(identity)) {
426                 if (unlikely(PTR_ERR(identity) == -EREMCHG)) {
427                         RETURN(0);
428                 } else {
429                         CDEBUG(D_SEC, "Deny access without identity: uid %u\n",
430                                pud->pud_uid);
431                         RETURN(-EACCES);
432                 }
433         }
434
435         perm = mdt_identity_get_perm(identity, peernid);
436         /* find out the setuid/setgid attempt */
437         setuid = (pud->pud_uid != pud->pud_fsuid);
438         setgid = (pud->pud_gid != pud->pud_fsgid ||
439                   pud->pud_gid != identity->mi_gid);
440
441         /* check permission of setuid */
442         if (setuid && !(perm & CFS_SETUID_PERM)) {
443                 CDEBUG(D_SEC, "mdt blocked setuid attempt (%u -> %u) from %s\n",
444                        pud->pud_uid, pud->pud_fsuid, libcfs_nid2str(peernid));
445                 GOTO(out, rc = -EACCES);
446         }
447
448         /* check permission of setgid */
449         if (setgid && !(perm & CFS_SETGID_PERM)) {
450                 CDEBUG(D_SEC, "mdt blocked setgid attempt (%u:%u/%u:%u -> %u) "
451                        "from %s\n", pud->pud_uid, pud->pud_gid,
452                        pud->pud_fsuid, pud->pud_fsgid, identity->mi_gid,
453                        libcfs_nid2str(peernid));
454                 GOTO(out, rc = -EACCES);
455         }
456
457         EXIT;
458
459 out:
460         mdt_identity_put(mdt->mdt_identity_cache, identity);
461         return rc;
462 }
463
464 static int old_init_ucred_common(struct mdt_thread_info *info,
465                                  struct lu_nodemap *nodemap,
466                                  bool drop_fs_cap)
467 {
468         struct lu_ucred         *uc = mdt_ucred(info);
469         struct mdt_device       *mdt = info->mti_mdt;
470         struct md_identity      *identity = NULL;
471
472         if (nodemap && uc->uc_o_uid == nodemap->nm_squash_uid) {
473                 /* deny access before we get identity ref */
474                 if (nodemap->nmf_deny_unknown)
475                         RETURN(-EACCES);
476
477                 uc->uc_fsuid = nodemap->nm_squash_uid;
478                 uc->uc_fsgid = nodemap->nm_squash_gid;
479                 uc->uc_cap = 0;
480                 uc->uc_suppgids[0] = -1;
481                 uc->uc_suppgids[1] = -1;
482         }
483
484         if (!is_identity_get_disabled(mdt->mdt_identity_cache)) {
485                 identity = mdt_identity_get(mdt->mdt_identity_cache,
486                                             uc->uc_fsuid);
487                 if (IS_ERR(identity)) {
488                         if (unlikely(PTR_ERR(identity) == -EREMCHG ||
489                                      uc->uc_cap & CFS_CAP_FS_MASK)) {
490                                 identity = NULL;
491                         } else {
492                                 CDEBUG(D_SEC, "Deny access without identity: "
493                                        "uid %u\n", uc->uc_fsuid);
494                                 RETURN(-EACCES);
495                         }
496                 }
497         }
498         uc->uc_identity = identity;
499
500         /* process root_squash here. */
501         mdt_root_squash(info, mdt_info_req(info)->rq_peer.nid);
502
503         /* remove fs privilege for non-root user. */
504         if (uc->uc_fsuid && drop_fs_cap)
505                 uc->uc_cap &= ~CFS_CAP_FS_MASK;
506         uc->uc_valid = UCRED_OLD;
507         ucred_set_jobid(info, uc);
508         ucred_set_nid(info, uc);
509         ucred_set_audit_enabled(info, uc);
510
511         EXIT;
512
513         return 0;
514 }
515
516 static int old_init_ucred(struct mdt_thread_info *info,
517                           struct mdt_body *body, bool drop_fs_cap)
518 {
519         struct lu_ucred *uc = mdt_ucred(info);
520         struct lu_nodemap *nodemap;
521         int rc;
522         ENTRY;
523
524         nodemap = nodemap_get_from_exp(info->mti_exp);
525         if (IS_ERR(nodemap))
526                 RETURN(PTR_ERR(nodemap));
527
528         body->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
529                                        NODEMAP_CLIENT_TO_FS, body->mbo_uid);
530         body->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
531                                        NODEMAP_CLIENT_TO_FS, body->mbo_gid);
532         body->mbo_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
533                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsuid);
534         body->mbo_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
535                                        NODEMAP_CLIENT_TO_FS, body->mbo_fsgid);
536
537         LASSERT(uc != NULL);
538         uc->uc_valid = UCRED_INVALID;
539         uc->uc_o_uid = uc->uc_uid = body->mbo_uid;
540         uc->uc_o_gid = uc->uc_gid = body->mbo_gid;
541         uc->uc_o_fsuid = uc->uc_fsuid = body->mbo_fsuid;
542         uc->uc_o_fsgid = uc->uc_fsgid = body->mbo_fsgid;
543         uc->uc_suppgids[0] = body->mbo_suppgid;
544         uc->uc_suppgids[1] = -1;
545         uc->uc_ginfo = NULL;
546         uc->uc_cap = body->mbo_capability;
547
548         rc = old_init_ucred_common(info, nodemap, drop_fs_cap);
549         nodemap_putref(nodemap);
550
551         RETURN(rc);
552 }
553
554 static int old_init_ucred_reint(struct mdt_thread_info *info)
555 {
556         struct lu_ucred *uc = mdt_ucred(info);
557         struct lu_nodemap *nodemap;
558         int rc;
559         ENTRY;
560
561         nodemap = nodemap_get_from_exp(info->mti_exp);
562         if (IS_ERR(nodemap))
563                 RETURN(PTR_ERR(nodemap));
564
565         LASSERT(uc != NULL);
566
567         uc->uc_fsuid = nodemap_map_id(nodemap, NODEMAP_UID,
568                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsuid);
569         uc->uc_fsgid = nodemap_map_id(nodemap, NODEMAP_GID,
570                                       NODEMAP_CLIENT_TO_FS, uc->uc_fsgid);
571
572         uc->uc_valid = UCRED_INVALID;
573         uc->uc_o_uid = uc->uc_o_fsuid = uc->uc_uid = uc->uc_fsuid;
574         uc->uc_o_gid = uc->uc_o_fsgid = uc->uc_gid = uc->uc_fsgid;
575         uc->uc_ginfo = NULL;
576
577         rc = old_init_ucred_common(info, nodemap, true); /* drop_fs_cap=true */
578         nodemap_putref(nodemap);
579
580         RETURN(rc);
581 }
582
583 static inline int __mdt_init_ucred(struct mdt_thread_info *info,
584                                    struct mdt_body *body,
585                                    bool drop_fs_cap)
586 {
587         struct ptlrpc_request   *req = mdt_info_req(info);
588         struct lu_ucred         *uc  = mdt_ucred(info);
589
590         LASSERT(uc != NULL);
591         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
592                 return 0;
593
594         mdt_exit_ucred(info);
595
596         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
597                 return old_init_ucred(info, body, drop_fs_cap);
598         else
599                 return new_init_ucred(info, BODY_INIT, body, drop_fs_cap);
600 }
601
602 int mdt_init_ucred(struct mdt_thread_info *info, struct mdt_body *body)
603 {
604         return __mdt_init_ucred(info, body, true);
605 }
606
607 /* LU-6528 when "no_subtree_check" is set for NFS export, nfsd_set_fh_dentry()
608  * doesn't set correct fsuid explicitely, but raise capability to allow
609  * exportfs_decode_fh() to reconnect disconnected dentry into dcache. So for
610  * lookup (i.e. intent_getattr), we should keep FS capability, otherwise it
611  * will fail permission check. */
612 int mdt_init_ucred_intent_getattr(struct mdt_thread_info *info,
613                                   struct mdt_body *body)
614 {
615         return __mdt_init_ucred(info, body, false);
616 }
617
618 int mdt_init_ucred_reint(struct mdt_thread_info *info)
619 {
620         struct ptlrpc_request *req = mdt_info_req(info);
621         struct lu_ucred       *uc  = mdt_ucred(info);
622         struct md_attr        *ma  = &info->mti_attr;
623
624         LASSERT(uc != NULL);
625         if ((uc->uc_valid == UCRED_OLD) || (uc->uc_valid == UCRED_NEW))
626                 return 0;
627
628         /* LU-5564: for normal close request, skip permission check */
629         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE &&
630             !(ma->ma_attr_flags & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)))
631                 uc->uc_cap |= CFS_CAP_FS_MASK;
632
633         mdt_exit_ucred(info);
634
635         if (!req->rq_auth_gss || req->rq_auth_usr_mdt || !req->rq_user_desc)
636                 return old_init_ucred_reint(info);
637         else
638                 return new_init_ucred(info, REC_INIT, NULL, true);
639 }
640
641 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
642 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid)
643 {
644         const struct lov_ost_data_v1 *lod;
645         __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
646         __u16 count;
647         int i;
648
649         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
650                 return;
651
652         CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
653                POSTID(&lmm->lmm_oi), lmm_magic,
654                le32_to_cpu(lmm->lmm_pattern));
655
656         /* No support for compount layouts yet */
657         if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
658                 return;
659
660         count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
661         CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
662                le32_to_cpu(lmm->lmm_stripe_size), count);
663
664         /* If it's a directory or a released file, then there are
665          * no actual objects to print, so bail out. */
666         if (valid & OBD_MD_FLDIREA ||
667             le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
668                 return;
669
670         LASSERT(count <= LOV_MAX_STRIPE_COUNT);
671         for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
672                 struct ost_id oi;
673
674                 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
675                 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
676                        i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
677         }
678 }
679
680 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
681 {
682         const struct lmv_mds_md_v1 *lmm1;
683         const struct lmv_foreign_md *lfm;
684         int                        i;
685
686         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
687                 return;
688
689         /* foreign LMV case */
690         lfm = &lmv->lmv_foreign_md;
691         if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN) {
692                 CDEBUG_LIMIT(level,
693                              "foreign magic 0x%08X, length %u, type %u, flags %u, value '%.*s'\n",
694                              le32_to_cpu(lfm->lfm_magic),
695                              le32_to_cpu(lfm->lfm_length),
696                              le32_to_cpu(lfm->lfm_type),
697                              le32_to_cpu(lfm->lfm_flags),
698                              le32_to_cpu(lfm->lfm_length), lfm->lfm_value);
699                 return;
700         }
701
702         lmm1 = &lmv->lmv_md_v1;
703         CDEBUG(level,
704                "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
705                le32_to_cpu(lmm1->lmv_magic),
706                le32_to_cpu(lmm1->lmv_master_mdt_index),
707                le32_to_cpu(lmm1->lmv_stripe_count),
708                le32_to_cpu(lmm1->lmv_hash_type));
709
710         if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
711                 return;
712
713         for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
714                 struct lu_fid fid;
715
716                 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
717                 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
718         }
719 }
720
721 /* Shrink and/or grow reply buffers */
722 int mdt_fix_reply(struct mdt_thread_info *info)
723 {
724         struct req_capsule *pill = info->mti_pill;
725         struct mdt_body    *body;
726         int                md_size, md_packed = 0;
727         int                acl_size;
728         int                rc = 0;
729         ENTRY;
730
731         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
732         LASSERT(body != NULL);
733
734         if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
735                                OBD_MD_LINKNAME))
736                 md_size = body->mbo_eadatasize;
737         else
738                 md_size = 0;
739
740         acl_size = body->mbo_aclsize;
741
742         /* this replay - not send info to client */
743         if (info->mti_spec.no_create) {
744                 md_size = 0;
745                 acl_size = 0;
746         }
747
748         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
749                md_size, acl_size);
750 /*
751             &RMF_MDT_BODY,
752             &RMF_MDT_MD,
753             &RMF_ACL, or &RMF_LOGCOOKIES
754 (optional)  &RMF_CAPA1,
755 (optional)  &RMF_CAPA2,
756 (optional)  something else
757 */
758
759         /* MDT_MD buffer may be bigger than packed value, let's shrink all
760          * buffers before growing it */
761         if (info->mti_big_lmm_used) {
762                 /* big_lmm buffer may be used even without packing the result
763                  * into reply, just for internal server needs */
764                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
765                         md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
766                                                          RCL_SERVER);
767
768                 /* free big lmm if md_size is not needed */
769                 if (md_size == 0 || md_packed == 0) {
770                         info->mti_big_lmm_used = 0;
771                 } else {
772                         /* buffer must be allocated separately */
773                         LASSERT(info->mti_attr.ma_lmm !=
774                                 req_capsule_server_get(pill, &RMF_MDT_MD));
775                         req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
776                 }
777         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
778                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
779         }
780
781         if (info->mti_big_acl_used) {
782                 if (acl_size == 0)
783                         info->mti_big_acl_used = 0;
784                 else
785                         req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
786         } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
787                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
788         } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
789                 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
790         }
791
792         /* Shrink optional SECCTX buffer if it is not used */
793         if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
794             req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0 &&
795             !(body->mbo_valid & OBD_MD_SECCTX))
796                 req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0, RCL_SERVER);
797
798         /* Shrink optional ENCCTX buffer if it is not used */
799         if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
800             req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0 &&
801             !(body->mbo_valid & OBD_MD_ENCCTX))
802                 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0, RCL_SERVER);
803
804         /*
805          * Some more field should be shrinked if needed.
806          * This should be done by those who added fields to reply message.
807          */
808
809         /* Grow MD buffer if needed finally */
810         if (info->mti_big_lmm_used) {
811                 void *lmm;
812
813                 LASSERT(md_size > md_packed);
814                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
815                        md_size - md_packed);
816                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
817                 if (rc) {
818                         /* we can't answer with proper LOV EA, drop flags,
819                          * the rc is also returned so this request is
820                          * considered as failed */
821                         body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
822                         /* don't return transno along with error */
823                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
824                 } else {
825                         /* now we need to pack right LOV/LMV EA */
826                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
827                         if (info->mti_attr.ma_valid & MA_LOV) {
828                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
829                                                              RCL_SERVER) ==
830                                                 info->mti_attr.ma_lmm_size);
831                                 memcpy(lmm, info->mti_attr.ma_lmm,
832                                        info->mti_attr.ma_lmm_size);
833                         } else if (info->mti_attr.ma_valid & MA_LMV) {
834                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
835                                                              RCL_SERVER) ==
836                                                 info->mti_attr.ma_lmv_size);
837                                 memcpy(lmm, info->mti_attr.ma_lmv,
838                                        info->mti_attr.ma_lmv_size);
839                         }
840                 }
841
842                 /* update mdt_max_mdsize so clients will be aware about that */
843                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
844                         info->mti_mdt->mdt_max_mdsize =
845                                                 info->mti_attr.ma_lmm_size;
846                 info->mti_big_lmm_used = 0;
847         }
848
849         if (info->mti_big_acl_used) {
850                 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
851                        acl_size);
852
853                 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
854                 if (rc) {
855                         body->mbo_valid &= ~OBD_MD_FLACL;
856                 } else {
857                         void *acl = req_capsule_server_get(pill, &RMF_ACL);
858
859                         memcpy(acl, info->mti_big_acl, acl_size);
860                 }
861
862                 info->mti_big_acl_used = 0;
863         }
864
865         RETURN(rc);
866 }
867
868
869 /* if object is dying, pack the lov/llog data,
870  * parameter info->mti_attr should be valid at this point!
871  * Also implements RAoLU policy */
872 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
873                            struct md_attr *ma)
874 {
875         struct mdt_body *repbody = NULL;
876         const struct lu_attr *la = &ma->ma_attr;
877         struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
878         int rc;
879         __u64 need = 0;
880         struct hsm_action_item hai = {
881                 .hai_len = sizeof(hai),
882                 .hai_action = HSMA_REMOVE,
883                 .hai_extent.length = -1,
884                 .hai_cookie = 0,
885                 .hai_gid = 0,
886         };
887         int archive_id;
888
889         ENTRY;
890
891         if (mdt_info_req(info) != NULL) {
892                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
893                 LASSERT(repbody != NULL);
894         } else {
895                 CDEBUG(D_INFO, "not running in a request/reply context\n");
896         }
897
898         if ((ma->ma_valid & MA_INODE) && repbody != NULL)
899                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
900
901         if (ma->ma_valid & MA_LOV) {
902                 CERROR("No need in LOV EA upon unlink\n");
903                 dump_stack();
904         }
905         if (repbody != NULL)
906                 repbody->mbo_eadatasize = 0;
907
908         /* Only check unlinked and archived if RAoLU and upon last close */
909         if (!cdt->cdt_remove_archive_on_last_unlink ||
910             atomic_read(&mo->mot_open_count) != 0)
911                 RETURN(0);
912
913         /* mdt_attr_get_complex will clear ma_valid, so check here first */
914         if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
915                 RETURN(0);
916
917         if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
918                 RETURN(0);
919
920         need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
921         if (need != 0) {
922                 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
923                  * try setting them */
924                 ma->ma_need |= need;
925                 rc = mdt_attr_get_complex(info, mo, ma);
926                 if (rc) {
927                         CERROR("%s: unable to fetch missing attributes of"
928                                DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
929                                PFID(mdt_object_fid(mo)), rc);
930                         RETURN(0);
931                 }
932
933                 if (need & MA_INODE) {
934                         if (ma->ma_valid & MA_INODE) {
935                                 if (ma->ma_attr.la_nlink != 0)
936                                         RETURN(0);
937                         } else {
938                                 RETURN(0);
939                         }
940                 }
941
942                 if (need & MA_HSM) {
943                         if (ma->ma_valid & MA_HSM) {
944                                 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
945                                         RETURN(0);
946                         } else {
947                                 RETURN(0);
948                         }
949                 }
950         }
951
952         /* RAoLU policy is active, last close on file has occured,
953          * file is unlinked, file is archived, so create remove request
954          * for copytool!
955          * If CDT is not running, requests will be logged for later. */
956         if (ma->ma_hsm.mh_arch_id != 0)
957                 archive_id = ma->ma_hsm.mh_arch_id;
958         else
959                 archive_id = cdt->cdt_default_archive_id;
960
961         hai.hai_fid = *mdt_object_fid(mo);
962
963         rc = mdt_agent_record_add(info->mti_env, info->mti_mdt, archive_id, 0,
964                                   &hai);
965         if (rc)
966                 CERROR("%s: unable to add HSM remove request for "DFID
967                        ": rc=%d\n", mdt_obd_name(info->mti_mdt),
968                        PFID(mdt_object_fid(mo)), rc);
969
970         RETURN(0);
971 }
972
973 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
974                                   struct md_attr *ma)
975 {
976         __u64 out;
977
978         out = 0;
979         if (in & MDS_ATTR_MODE)
980                 out |= LA_MODE;
981         if (in & MDS_ATTR_UID)
982                 out |= LA_UID;
983         if (in & MDS_ATTR_GID)
984                 out |= LA_GID;
985         if (in & MDS_ATTR_SIZE)
986                 out |= LA_SIZE;
987         if (in & MDS_ATTR_BLOCKS)
988                 out |= LA_BLOCKS;
989         if (in & MDS_ATTR_ATIME_SET)
990                 out |= LA_ATIME;
991         if (in & MDS_ATTR_CTIME_SET)
992                 out |= LA_CTIME;
993         if (in & MDS_ATTR_MTIME_SET)
994                 out |= LA_MTIME;
995         if (in & MDS_ATTR_ATTR_FLAG)
996                 out |= LA_FLAGS;
997         if (in & MDS_ATTR_KILL_SUID)
998                 out |= LA_KILL_SUID;
999         if (in & MDS_ATTR_KILL_SGID)
1000                 out |= LA_KILL_SGID;
1001         if (in & MDS_ATTR_PROJID)
1002                 out |= LA_PROJID;
1003         if (in & MDS_ATTR_LSIZE)
1004                 out |= LA_LSIZE;
1005         if (in & MDS_ATTR_LBLOCKS)
1006                 out |= LA_LBLOCKS;
1007
1008         if (in & MDS_ATTR_FROM_OPEN)
1009                 rr->rr_flags |= MRF_OPEN_TRUNC;
1010         if (in & MDS_ATTR_OVERRIDE)
1011                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
1012         if (in & MDS_ATTR_FORCE)
1013                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1014
1015         in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
1016                 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
1017                 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
1018                 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
1019                 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
1020                 MDS_ATTR_FROM_OPEN | MDS_ATTR_LSIZE | MDS_ATTR_LBLOCKS |
1021                 MDS_ATTR_OVERRIDE);
1022         if (in != 0)
1023                 CDEBUG(D_INFO, "Unknown attr bits: %#llx\n", in);
1024         return out;
1025 }
1026
1027 /* unpacking */
1028
1029 int mdt_name_unpack(struct req_capsule *pill,
1030                     const struct req_msg_field *field,
1031                     struct lu_name *ln,
1032                     enum mdt_name_flags flags)
1033 {
1034         ln->ln_name = req_capsule_client_get(pill, field);
1035         ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
1036
1037         if (!lu_name_is_valid(ln)) {
1038                 ln->ln_name = NULL;
1039                 ln->ln_namelen = 0;
1040
1041                 return -EPROTO;
1042         }
1043
1044         if ((flags & MNF_FIX_ANON) &&
1045             ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
1046                 /* Newer (3.x) kernels use a name of "/" for the
1047                  * "anonymous" disconnected dentries from NFS
1048                  * filehandle conversion. See d_obtain_alias(). */
1049                 ln->ln_name = NULL;
1050                 ln->ln_namelen = 0;
1051         }
1052
1053         return 0;
1054 }
1055
1056 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1057                                   const char **secctx_name,
1058                                   void **secctx, size_t *secctx_size)
1059 {
1060         const char *name;
1061         size_t name_size;
1062
1063         *secctx_name = NULL;
1064         *secctx = NULL;
1065         *secctx_size = 0;
1066
1067         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1068             !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1069                 return 0;
1070
1071         name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1072                                          RCL_CLIENT);
1073         if (name_size == 0)
1074                 return 0;
1075
1076         if (name_size > XATTR_NAME_MAX + 1)
1077                 return -EPROTO;
1078
1079         name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1080         if (strnlen(name, name_size) != name_size - 1)
1081                 return -EPROTO;
1082
1083         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1084             !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1085                 return -EPROTO;
1086
1087         *secctx_name = name;
1088         *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1089         *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1090
1091         return 0;
1092 }
1093
1094 static int mdt_file_encctx_unpack(struct req_capsule *pill,
1095                                   void **encctx, size_t *encctx_size)
1096 {
1097         *encctx = NULL;
1098         *encctx_size = 0;
1099
1100         if (!exp_connect_encrypt(pill->rc_req->rq_export))
1101                 return 0;
1102
1103         if (!req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_CLIENT) ||
1104             !req_capsule_field_present(pill, &RMF_FILE_ENCCTX, RCL_CLIENT))
1105                 return -EPROTO;
1106
1107         *encctx_size = req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_CLIENT);
1108         if (*encctx_size == 0)
1109                 return 0;
1110
1111         *encctx = req_capsule_client_get(pill, &RMF_FILE_ENCCTX);
1112
1113         return 0;
1114 }
1115
1116 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1117 {
1118         struct lu_ucred *uc = mdt_ucred(info);
1119         struct md_attr *ma = &info->mti_attr;
1120         struct lu_attr *la = &ma->ma_attr;
1121         struct req_capsule *pill = info->mti_pill;
1122         struct mdt_reint_record *rr = &info->mti_rr;
1123         struct mdt_rec_setattr *rec;
1124         struct lu_nodemap *nodemap;
1125
1126         ENTRY;
1127
1128         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1129         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1130         if (rec == NULL)
1131                 RETURN(-EFAULT);
1132
1133         /* This prior initialization is needed for old_init_ucred_reint() */
1134         uc->uc_fsuid = rec->sa_fsuid;
1135         uc->uc_fsgid = rec->sa_fsgid;
1136         uc->uc_cap   = rec->sa_cap;
1137         uc->uc_suppgids[0] = rec->sa_suppgid;
1138         uc->uc_suppgids[1] = -1;
1139
1140         rr->rr_fid1 = &rec->sa_fid;
1141         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1142         la->la_mode  = rec->sa_mode;
1143         la->la_flags = rec->sa_attr_flags;
1144
1145         nodemap = nodemap_get_from_exp(info->mti_exp);
1146         if (IS_ERR(nodemap))
1147                 RETURN(PTR_ERR(nodemap));
1148
1149         la->la_uid   = nodemap_map_id(nodemap, NODEMAP_UID,
1150                                       NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1151         la->la_gid   = nodemap_map_id(nodemap, NODEMAP_GID,
1152                                       NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1153         la->la_projid = rec->sa_projid;
1154         nodemap_putref(nodemap);
1155
1156         la->la_size  = rec->sa_size;
1157         la->la_blocks = rec->sa_blocks;
1158         la->la_ctime = rec->sa_ctime;
1159         la->la_atime = rec->sa_atime;
1160         la->la_mtime = rec->sa_mtime;
1161         ma->ma_valid = MA_INODE;
1162
1163         ma->ma_attr_flags |= rec->sa_bias & (MDS_CLOSE_INTENT |
1164                                 MDS_DATA_MODIFIED | MDS_TRUNC_KEEP_LEASE |
1165                                 MDS_PCC_ATTACH);
1166         RETURN(0);
1167 }
1168
1169 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1170 {
1171         struct req_capsule *pill = info->mti_pill;
1172         struct mdt_ioepoch *ioepoch;
1173         ENTRY;
1174
1175         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1176                 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1177         else
1178                 ioepoch = NULL;
1179
1180         if (ioepoch == NULL)
1181                 RETURN(-EPROTO);
1182
1183         info->mti_open_handle = ioepoch->mio_open_handle;
1184
1185         RETURN(0);
1186 }
1187
1188 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1189         struct req_capsule      *pill = info->mti_pill;
1190
1191         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1192                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1193                 if (info->mti_dlm_req == NULL)
1194                         RETURN(-EFAULT);
1195         }
1196
1197         RETURN(0);
1198 }
1199
1200 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1201 {
1202         struct mdt_reint_record *rr = &info->mti_rr;
1203         struct md_attr          *ma = &info->mti_attr;
1204         struct req_capsule      *pill = info->mti_pill;
1205         int rc;
1206         ENTRY;
1207
1208         rc = mdt_setattr_unpack_rec(info);
1209         if (rc)
1210                 RETURN(rc);
1211
1212         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1213                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1214                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1215                                                         RCL_CLIENT);
1216
1217                 if (rr->rr_eadatalen > 0) {
1218                         const struct lmv_user_md        *lum;
1219
1220                         lum = rr->rr_eadata;
1221                         /* Sigh ma_valid(from req) does not indicate whether
1222                          * it will set LOV/LMV EA, so we have to check magic */
1223                         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1224                                 ma->ma_valid |= MA_LMV;
1225                                 ma->ma_lmv = (void *)rr->rr_eadata;
1226                                 ma->ma_lmv_size = rr->rr_eadatalen;
1227                         } else {
1228                                 ma->ma_valid |= MA_LOV;
1229                                 ma->ma_lmm = (void *)rr->rr_eadata;
1230                                 ma->ma_lmm_size = rr->rr_eadatalen;
1231                         }
1232                 }
1233         }
1234
1235         rc = mdt_dlmreq_unpack(info);
1236         RETURN(rc);
1237 }
1238
1239 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1240 {
1241         struct md_attr          *ma = &info->mti_attr;
1242         struct req_capsule      *pill = info->mti_pill;
1243         ENTRY;
1244
1245         if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1246                 RETURN(0);
1247
1248         req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1249
1250         if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1251             req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1252                 RETURN(-EFAULT);
1253
1254         RETURN(0);
1255 }
1256
1257 int mdt_close_unpack(struct mdt_thread_info *info)
1258 {
1259         int rc;
1260         ENTRY;
1261
1262         rc = mdt_close_handle_unpack(info);
1263         if (rc)
1264                 RETURN(rc);
1265
1266         rc = mdt_setattr_unpack_rec(info);
1267         if (rc)
1268                 RETURN(rc);
1269
1270         rc = mdt_close_intent_unpack(info);
1271         if (rc)
1272                 RETURN(rc);
1273
1274         RETURN(mdt_init_ucred_reint(info));
1275 }
1276
1277 static int mdt_create_unpack(struct mdt_thread_info *info)
1278 {
1279         struct lu_ucred *uc  = mdt_ucred(info);
1280         struct mdt_rec_create *rec;
1281         struct lu_attr *attr = &info->mti_attr.ma_attr;
1282         struct mdt_reint_record *rr = &info->mti_rr;
1283         struct req_capsule      *pill = info->mti_pill;
1284         struct md_op_spec       *sp = &info->mti_spec;
1285         int rc;
1286
1287         ENTRY;
1288
1289         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1290         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1291         if (rec == NULL)
1292                 RETURN(-EFAULT);
1293
1294         /* This prior initialization is needed for old_init_ucred_reint() */
1295         uc->uc_fsuid = rec->cr_fsuid;
1296         uc->uc_fsgid = rec->cr_fsgid;
1297         uc->uc_cap   = rec->cr_cap;
1298         uc->uc_suppgids[0] = rec->cr_suppgid1;
1299         uc->uc_suppgids[1] = -1;
1300         uc->uc_umask = rec->cr_umask;
1301
1302         rr->rr_fid1 = &rec->cr_fid1;
1303         rr->rr_fid2 = &rec->cr_fid2;
1304         attr->la_mode = rec->cr_mode;
1305         attr->la_rdev  = rec->cr_rdev;
1306         attr->la_uid   = rec->cr_fsuid;
1307         attr->la_gid   = rec->cr_fsgid;
1308         attr->la_ctime = rec->cr_time;
1309         attr->la_mtime = rec->cr_time;
1310         attr->la_atime = rec->cr_time;
1311         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1312                          LA_CTIME | LA_MTIME | LA_ATIME;
1313         memset(&sp->u, 0, sizeof(sp->u));
1314         sp->sp_cr_flags = get_mrc_cr_flags(rec);
1315
1316         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1317         if (rc < 0)
1318                 RETURN(rc);
1319
1320         if (S_ISLNK(attr->la_mode)) {
1321                 const char *tgt = NULL;
1322
1323                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1324                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1325                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1326                         sp->u.sp_symname = tgt;
1327                 }
1328                 if (tgt == NULL)
1329                         RETURN(-EFAULT);
1330         } else {
1331                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1332                 if (S_ISDIR(attr->la_mode) &&
1333                     req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1334                         sp->u.sp_ea.eadata =
1335                                 req_capsule_client_get(pill, &RMF_EADATA);
1336                         sp->u.sp_ea.eadatalen =
1337                                 req_capsule_get_size(pill, &RMF_EADATA,
1338                                                      RCL_CLIENT);
1339                         sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1340                 }
1341         }
1342
1343         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1344                                     &sp->sp_cr_file_secctx,
1345                                     &sp->sp_cr_file_secctx_size);
1346         if (rc < 0)
1347                 RETURN(rc);
1348
1349         rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1350                                     &sp->sp_cr_file_encctx_size);
1351         if (rc < 0)
1352                 RETURN(rc);
1353
1354         rc = req_check_sepol(pill);
1355         if (rc)
1356                 RETURN(rc);
1357
1358         rc = mdt_dlmreq_unpack(info);
1359         RETURN(rc);
1360 }
1361
1362 static int mdt_link_unpack(struct mdt_thread_info *info)
1363 {
1364         struct lu_ucred *uc  = mdt_ucred(info);
1365         struct mdt_rec_link *rec;
1366         struct lu_attr *attr = &info->mti_attr.ma_attr;
1367         struct mdt_reint_record *rr = &info->mti_rr;
1368         struct req_capsule *pill = info->mti_pill;
1369         int rc;
1370
1371         ENTRY;
1372
1373         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1374         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1375         if (rec == NULL)
1376                 RETURN(-EFAULT);
1377
1378         /* This prior initialization is needed for old_init_ucred_reint() */
1379         uc->uc_fsuid = rec->lk_fsuid;
1380         uc->uc_fsgid = rec->lk_fsgid;
1381         uc->uc_cap   = rec->lk_cap;
1382         uc->uc_suppgids[0] = rec->lk_suppgid1;
1383         uc->uc_suppgids[1] = rec->lk_suppgid2;
1384
1385         attr->la_uid = rec->lk_fsuid;
1386         attr->la_gid = rec->lk_fsgid;
1387         rr->rr_fid1 = &rec->lk_fid1;
1388         rr->rr_fid2 = &rec->lk_fid2;
1389         attr->la_ctime = rec->lk_time;
1390         attr->la_mtime = rec->lk_time;
1391         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
1392
1393         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1394         if (rc < 0)
1395                 RETURN(rc);
1396
1397         rc = req_check_sepol(pill);
1398         if (rc)
1399                 RETURN(rc);
1400
1401         rc = mdt_dlmreq_unpack(info);
1402
1403         RETURN(rc);
1404 }
1405
1406 static int mdt_unlink_unpack(struct mdt_thread_info *info)
1407 {
1408         struct lu_ucred *uc  = mdt_ucred(info);
1409         struct mdt_rec_unlink *rec;
1410         struct lu_attr *attr = &info->mti_attr.ma_attr;
1411         struct mdt_reint_record *rr = &info->mti_rr;
1412         struct req_capsule      *pill = info->mti_pill;
1413         int rc;
1414
1415         ENTRY;
1416
1417         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1418         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1419         if (rec == NULL)
1420                 RETURN(-EFAULT);
1421
1422         /* This prior initialization is needed for old_init_ucred_reint() */
1423         uc->uc_fsuid = rec->ul_fsuid;
1424         uc->uc_fsgid = rec->ul_fsgid;
1425         uc->uc_cap   = rec->ul_cap;
1426         uc->uc_suppgids[0] = rec->ul_suppgid1;
1427         uc->uc_suppgids[1] = -1;
1428
1429         attr->la_uid = rec->ul_fsuid;
1430         attr->la_gid = rec->ul_fsgid;
1431         rr->rr_fid1 = &rec->ul_fid1;
1432         rr->rr_fid2 = &rec->ul_fid2;
1433         attr->la_ctime = rec->ul_time;
1434         attr->la_mtime = rec->ul_time;
1435         attr->la_mode  = rec->ul_mode;
1436         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1437
1438         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1439         if (rc < 0)
1440                 RETURN(rc);
1441
1442         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1443
1444         rc = req_check_sepol(pill);
1445         if (rc)
1446                 RETURN(rc);
1447
1448         rc = mdt_dlmreq_unpack(info);
1449         RETURN(rc);
1450 }
1451
1452 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1453 {
1454         info->mti_spec.sp_rm_entry = 1;
1455         return mdt_unlink_unpack(info);
1456 }
1457
1458 static int mdt_rename_unpack(struct mdt_thread_info *info)
1459 {
1460         struct lu_ucred *uc = mdt_ucred(info);
1461         struct mdt_rec_rename *rec;
1462         struct lu_attr *attr = &info->mti_attr.ma_attr;
1463         struct mdt_reint_record *rr = &info->mti_rr;
1464         struct req_capsule *pill = info->mti_pill;
1465         struct md_op_spec *spec = &info->mti_spec;
1466         int rc;
1467
1468         ENTRY;
1469
1470         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1471         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1472         if (rec == NULL)
1473                 RETURN(-EFAULT);
1474
1475         /* This prior initialization is needed for old_init_ucred_reint() */
1476         uc->uc_fsuid = rec->rn_fsuid;
1477         uc->uc_fsgid = rec->rn_fsgid;
1478         uc->uc_cap   = rec->rn_cap;
1479         uc->uc_suppgids[0] = rec->rn_suppgid1;
1480         uc->uc_suppgids[1] = rec->rn_suppgid2;
1481
1482         attr->la_uid = rec->rn_fsuid;
1483         attr->la_gid = rec->rn_fsgid;
1484         rr->rr_fid1 = &rec->rn_fid1;
1485         rr->rr_fid2 = &rec->rn_fid2;
1486         attr->la_ctime = rec->rn_time;
1487         attr->la_mtime = rec->rn_time;
1488         /* rename_tgt contains the mode already */
1489         attr->la_mode = rec->rn_mode;
1490         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1491
1492         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1493         if (rc < 0)
1494                 RETURN(rc);
1495
1496         rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1497         if (rc < 0)
1498                 RETURN(rc);
1499
1500         spec->no_create = !!req_is_replay(mdt_info_req(info));
1501
1502         rc = req_check_sepol(pill);
1503         if (rc)
1504                 RETURN(rc);
1505
1506         rc = mdt_dlmreq_unpack(info);
1507
1508         RETURN(rc);
1509 }
1510
1511 static int mdt_migrate_unpack(struct mdt_thread_info *info)
1512 {
1513         struct lu_ucred *uc = mdt_ucred(info);
1514         struct mdt_rec_rename *rec;
1515         struct lu_attr *attr = &info->mti_attr.ma_attr;
1516         struct mdt_reint_record *rr = &info->mti_rr;
1517         struct req_capsule *pill = info->mti_pill;
1518         struct md_op_spec *spec = &info->mti_spec;
1519         int rc;
1520
1521         ENTRY;
1522
1523         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1524         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1525         if (rec == NULL)
1526                 RETURN(-EFAULT);
1527
1528         /* This prior initialization is needed for old_init_ucred_reint() */
1529         uc->uc_fsuid = rec->rn_fsuid;
1530         uc->uc_fsgid = rec->rn_fsgid;
1531         uc->uc_cap   = rec->rn_cap;
1532         uc->uc_suppgids[0] = rec->rn_suppgid1;
1533         uc->uc_suppgids[1] = rec->rn_suppgid2;
1534
1535         attr->la_uid = rec->rn_fsuid;
1536         attr->la_gid = rec->rn_fsgid;
1537         rr->rr_fid1 = &rec->rn_fid1;
1538         rr->rr_fid2 = &rec->rn_fid2;
1539         attr->la_ctime = rec->rn_time;
1540         attr->la_mtime = rec->rn_time;
1541         /* rename_tgt contains the mode already */
1542         attr->la_mode = rec->rn_mode;
1543         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1544         spec->sp_cr_flags = 0;
1545
1546         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1547         if (rc < 0)
1548                 RETURN(rc);
1549
1550         if (rec->rn_bias & MDS_CLOSE_MIGRATE) {
1551                 rc = mdt_close_handle_unpack(info);
1552                 if (rc)
1553                         RETURN(rc);
1554
1555                 spec->sp_migrate_close = 1;
1556         } else {
1557                 spec->sp_migrate_close = 0;
1558         }
1559         spec->sp_migrate_nsonly = 0;
1560
1561         /* lustre version > 2.11 migration packs lum */
1562         if (req_capsule_has_field(pill, &RMF_EADATA, RCL_CLIENT)) {
1563                 if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1564                         rr->rr_eadatalen = req_capsule_get_size(pill,
1565                                                                 &RMF_EADATA,
1566                                                                 RCL_CLIENT);
1567
1568                         if (rr->rr_eadatalen > 0) {
1569                                 rr->rr_eadata = req_capsule_client_get(pill,
1570                                                                 &RMF_EADATA);
1571                                 spec->u.sp_ea.eadatalen = rr->rr_eadatalen;
1572                                 spec->u.sp_ea.eadata = rr->rr_eadata;
1573                                 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1574                         }
1575                 } else {
1576                         /* old client doesn't provide lum. */
1577                         RETURN(-EOPNOTSUPP);
1578                 }
1579         }
1580
1581         spec->no_create = !!req_is_replay(mdt_info_req(info));
1582
1583         rc = mdt_dlmreq_unpack(info);
1584
1585         RETURN(rc);
1586 }
1587
1588 /*
1589  * please see comment above LOV_MAGIC_V1_DEFINED
1590  */
1591 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1592 {
1593         struct lov_user_md_v1   *v1 = eadata;
1594
1595         LASSERT(v1);
1596
1597         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1598                 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1599                         v1->lmm_magic |= LOV_MAGIC_DEFINED;
1600                 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1601                          __swab32(LOV_MAGIC_MAGIC))
1602                         v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1603         }
1604 }
1605
1606 static int mdt_open_unpack(struct mdt_thread_info *info)
1607 {
1608         struct lu_ucred *uc = mdt_ucred(info);
1609         struct mdt_rec_create *rec;
1610         struct lu_attr *attr = &info->mti_attr.ma_attr;
1611         struct req_capsule *pill = info->mti_pill;
1612         struct mdt_reint_record *rr = &info->mti_rr;
1613         struct ptlrpc_request *req = mdt_info_req(info);
1614         struct md_op_spec *sp = &info->mti_spec;
1615         int rc;
1616         ENTRY;
1617
1618         BUILD_BUG_ON(sizeof(struct mdt_rec_create) !=
1619                      sizeof(struct mdt_rec_reint));
1620         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1621         if (rec == NULL)
1622                 RETURN(-EFAULT);
1623
1624         /* This prior initialization is needed for old_init_ucred_reint() */
1625         uc->uc_fsuid = rec->cr_fsuid;
1626         uc->uc_fsgid = rec->cr_fsgid;
1627         uc->uc_cap   = rec->cr_cap;
1628         uc->uc_suppgids[0] = rec->cr_suppgid1;
1629         uc->uc_suppgids[1] = rec->cr_suppgid2;
1630         uc->uc_umask = rec->cr_umask;
1631
1632         rr->rr_fid1   = &rec->cr_fid1;
1633         rr->rr_fid2   = &rec->cr_fid2;
1634         rr->rr_open_handle = &rec->cr_open_handle_old;
1635         attr->la_mode = rec->cr_mode;
1636         attr->la_rdev  = rec->cr_rdev;
1637         attr->la_uid   = rec->cr_fsuid;
1638         attr->la_gid   = rec->cr_fsgid;
1639         attr->la_ctime = rec->cr_time;
1640         attr->la_mtime = rec->cr_time;
1641         attr->la_atime = rec->cr_time;
1642         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1643                          LA_CTIME | LA_MTIME | LA_ATIME;
1644         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1645         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1646         /* Do not trigger ASSERTION if client miss to set such flags. */
1647         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1648                 RETURN(-EPROTO);
1649
1650         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1651
1652         mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1653
1654         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1655                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1656                                                         RCL_CLIENT);
1657
1658                 if (rr->rr_eadatalen > 0) {
1659                         rr->rr_eadata = req_capsule_client_get(pill,
1660                                                                &RMF_EADATA);
1661                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1662                         sp->u.sp_ea.eadata = rr->rr_eadata;
1663                         sp->sp_archive_id = rec->cr_archive_id;
1664                         sp->no_create = !!req_is_replay(req);
1665                         mdt_fix_lov_magic(info, rr->rr_eadata);
1666                 }
1667
1668                 /*
1669                  * Client default md_size may be 0 right after client start,
1670                  * until all osc are connected, set here just some reasonable
1671                  * value to prevent misbehavior.
1672                  */
1673                 if (rr->rr_eadatalen == 0 &&
1674                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1675                         rr->rr_eadatalen = MIN_MD_SIZE;
1676         }
1677
1678         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1679                                     &sp->sp_cr_file_secctx,
1680                                     &sp->sp_cr_file_secctx_size);
1681         if (rc < 0)
1682                 RETURN(rc);
1683
1684         rc = mdt_file_encctx_unpack(pill, &sp->sp_cr_file_encctx,
1685                                     &sp->sp_cr_file_encctx_size);
1686         if (rc < 0)
1687                 RETURN(rc);
1688
1689         rc = req_check_sepol(pill);
1690         if (rc)
1691                 RETURN(rc);
1692
1693         RETURN(rc);
1694 }
1695
1696 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1697 {
1698         struct mdt_reint_record *rr = &info->mti_rr;
1699         struct lu_ucred *uc = mdt_ucred(info);
1700         struct lu_attr *attr = &info->mti_attr.ma_attr;
1701         struct req_capsule *pill = info->mti_pill;
1702         struct mdt_rec_setxattr *rec;
1703         int rc;
1704         ENTRY;
1705
1706
1707         BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
1708                      sizeof(struct mdt_rec_reint));
1709         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1710         if (rec == NULL)
1711                 RETURN(-EFAULT);
1712
1713         /* This prior initialization is needed for old_init_ucred_reint() */
1714         uc->uc_fsuid  = rec->sx_fsuid;
1715         uc->uc_fsgid  = rec->sx_fsgid;
1716         uc->uc_cap    = rec->sx_cap;
1717         uc->uc_suppgids[0] = rec->sx_suppgid1;
1718         uc->uc_suppgids[1] = -1;
1719
1720         rr->rr_opcode = rec->sx_opcode;
1721         rr->rr_fid1   = &rec->sx_fid;
1722         attr->la_valid = rec->sx_valid;
1723         attr->la_ctime = rec->sx_time;
1724         attr->la_size = rec->sx_size;
1725         attr->la_flags = rec->sx_flags;
1726
1727         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1728         if (rc < 0)
1729                 RETURN(rc);
1730
1731         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1732                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1733                                                         RCL_CLIENT);
1734
1735                 if (rr->rr_eadatalen > info->mti_mdt->mdt_max_ea_size)
1736                         RETURN(-E2BIG);
1737
1738                 if (rr->rr_eadatalen > 0) {
1739                         rr->rr_eadata = req_capsule_client_get(pill,
1740                                                                &RMF_EADATA);
1741                         if (rr->rr_eadata == NULL)
1742                                 RETURN(-EFAULT);
1743                 } else {
1744                         rr->rr_eadata = NULL;
1745                 }
1746         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1747                 CDEBUG(D_INFO, "no xattr data supplied\n");
1748                 RETURN(-EFAULT);
1749         }
1750
1751         rc = req_check_sepol(pill);
1752         if (rc)
1753                 RETURN(rc);
1754
1755         if (mdt_dlmreq_unpack(info) < 0)
1756                 RETURN(-EPROTO);
1757
1758         RETURN(0);
1759 }
1760
1761 static int mdt_resync_unpack(struct mdt_thread_info *info)
1762 {
1763         struct req_capsule      *pill = info->mti_pill;
1764         struct mdt_reint_record *rr   = &info->mti_rr;
1765         struct lu_ucred         *uc     = mdt_ucred(info);
1766         struct mdt_rec_resync   *rec;
1767         ENTRY;
1768
1769         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
1770         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1771         if (rec == NULL)
1772                 RETURN(-EFAULT);
1773
1774         /* This prior initialization is needed for old_init_ucred_reint() */
1775         uc->uc_fsuid = rec->rs_fsuid;
1776         uc->uc_fsgid = rec->rs_fsgid;
1777         uc->uc_cap   = rec->rs_cap;
1778
1779         rr->rr_fid1   = &rec->rs_fid;
1780         rr->rr_mirror_id = rec->rs_mirror_id;
1781
1782         /* cookie doesn't need to be swapped but it has been swapped
1783          * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1784          * restoring. */
1785         if (ptlrpc_req_need_swab(mdt_info_req(info)))
1786                 __swab64s(&rec->rs_lease_handle.cookie);
1787         rr->rr_lease_handle = &rec->rs_lease_handle;
1788
1789         RETURN(mdt_dlmreq_unpack(info));
1790 }
1791
1792 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1793
1794 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1795         [REINT_SETATTR]  = mdt_setattr_unpack,
1796         [REINT_CREATE]   = mdt_create_unpack,
1797         [REINT_LINK]     = mdt_link_unpack,
1798         [REINT_UNLINK]   = mdt_unlink_unpack,
1799         [REINT_RENAME]   = mdt_rename_unpack,
1800         [REINT_OPEN]     = mdt_open_unpack,
1801         [REINT_SETXATTR] = mdt_setxattr_unpack,
1802         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1803         [REINT_MIGRATE]  = mdt_migrate_unpack,
1804         [REINT_RESYNC]   = mdt_resync_unpack,
1805 };
1806
1807 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1808 {
1809         int rc;
1810         ENTRY;
1811
1812         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1813         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1814                 info->mti_rr.rr_opcode = op;
1815                 rc = mdt_reint_unpackers[op](info);
1816         } else {
1817                 CERROR("Unexpected opcode %d\n", op);
1818                 rc = -EFAULT;
1819         }
1820         RETURN(rc);
1821 }
1822
1823 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1824                              struct mdt_object *child)
1825 {
1826         char *secctx_name;
1827         struct lu_buf *buffer;
1828         struct mdt_body *repbody;
1829         struct req_capsule *pill = info->mti_pill;
1830         int rc = 0;
1831
1832         if (req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_SERVER) &&
1833             req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_SERVER) != 0) {
1834                 secctx_name =
1835                         req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1836                 buffer = &info->mti_buf;
1837
1838                 /* fill reply buffer with security context now */
1839                 buffer->lb_len = req_capsule_get_size(pill, &RMF_FILE_SECCTX,
1840                                                       RCL_SERVER);
1841                 buffer->lb_buf = req_capsule_server_get(info->mti_pill,
1842                                                         &RMF_FILE_SECCTX);
1843                 rc = mo_xattr_get(info->mti_env, mdt_object_child(child),
1844                                   buffer, secctx_name);
1845                 if (rc >= 0) {
1846                         CDEBUG(D_SEC,
1847                                "found security context of size %d for "DFID"\n",
1848                                rc, PFID(mdt_object_fid(child)));
1849
1850                         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1851                         repbody->mbo_valid |= OBD_MD_SECCTX;
1852                         if (rc < buffer->lb_len)
1853                                 req_capsule_shrink(pill, &RMF_FILE_SECCTX, rc,
1854                                                    RCL_SERVER);
1855                         rc = 0;
1856                 } else {
1857                         CDEBUG(D_SEC,
1858                              "security context not found for "DFID": rc = %d\n",
1859                              PFID(mdt_object_fid(child)), rc);
1860                         req_capsule_shrink(pill, &RMF_FILE_SECCTX, 0,
1861                                            RCL_SERVER);
1862                         /* handling -ENOENT is important because it may change
1863                          * object state in DNE env dropping LOHA_EXISTS flag,
1864                          * it is important to return that to the caller.
1865                          * Check LU-13115 for details.
1866                          */
1867                         if (rc != -ENOENT)
1868                                 rc = 0;
1869                 }
1870         }
1871         return rc;
1872 }
1873
1874 /* check whether two FIDs belong to different MDT. */
1875 static int mdt_fids_different_target(struct mdt_thread_info *info,
1876                                      const struct lu_fid *fid1,
1877                                      const struct lu_fid *fid2)
1878 {
1879         const struct lu_env *env = info->mti_env;
1880         struct mdt_device *mdt = info->mti_mdt;
1881         struct lu_seq_range *range = &info->mti_range;
1882         struct seq_server_site *ss;
1883         __u32 index1, index2;
1884         int rc;
1885
1886         if (fid_seq(fid1) == fid_seq(fid2))
1887                 return 0;
1888
1889         ss = mdt->mdt_lu_dev.ld_site->ld_seq_site;
1890
1891         range->lsr_flags = LU_SEQ_RANGE_MDT;
1892         rc = fld_server_lookup(env, ss->ss_server_fld, fid1->f_seq, range);
1893         if (rc)
1894                 return rc;
1895
1896         index1 = range->lsr_index;
1897
1898         rc = fld_server_lookup(env, ss->ss_server_fld, fid2->f_seq, range);
1899         if (rc)
1900                 return rc;
1901
1902         index2 = range->lsr_index;
1903
1904         return index1 != index2;
1905 }
1906
1907 static bool mdt_object_is_shard(struct mdt_thread_info *info,
1908                                 struct mdt_object *obj)
1909 {
1910         struct lmv_mds_md_v1 *lmv = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
1911         struct lu_buf buf;
1912         int rc;
1913
1914         if (!mdt_object_exists(obj))
1915                 return false;
1916
1917         if (!S_ISDIR(lu_object_attr(&obj->mot_obj)))
1918                 return false;
1919
1920         buf.lb_buf = lmv;
1921         buf.lb_len = sizeof(*lmv);
1922         rc = mo_xattr_get(info->mti_env, mdt_object_child(obj), &buf,
1923                           XATTR_NAME_LMV);
1924         if (rc < 0)
1925                 return false;
1926
1927         return lmv->lmv_magic == cpu_to_le32(LMV_MAGIC_STRIPE);
1928 }
1929
1930 /**
1931  * Check whether \a child is remote object on \a parent.
1932  *
1933  * \param[in]  info     thread environment
1934  * \param[in]  parent   parent object, it's the same as child object in
1935  *                      getattr_by_fid
1936  * \param[in]  child    child object
1937  *
1938  * \retval 1    is remote object.
1939  * \retval 0    isn't remote object.
1940  * \retval < 1  error code
1941  */
1942 int mdt_is_remote_object(struct mdt_thread_info *info,
1943                          struct mdt_object *parent,
1944                          struct mdt_object *child)
1945 {
1946         struct lu_buf *buf = &info->mti_big_buf;
1947         struct linkea_data ldata = { NULL };
1948         struct link_ea_header *leh;
1949         struct link_ea_entry *lee;
1950         struct lu_name name;
1951         struct lu_fid pfid;
1952         int reclen;
1953         int i;
1954         int rc;
1955
1956         ENTRY;
1957
1958         if (fid_is_root(mdt_object_fid(child)))
1959                 RETURN(0);
1960
1961         if (likely(parent != child)) {
1962                 if (mdt_object_remote(parent) ^ mdt_object_remote(child)) {
1963                         /* don't treat shard as remote object, otherwise client
1964                          * need to revalidate shards all the time.
1965                          */
1966                         if (mdt_object_is_shard(info, child))
1967                                 RETURN(0);
1968                         RETURN(1);
1969                 }
1970
1971                 if (!mdt_object_remote(parent) && !mdt_object_remote(child))
1972                         RETURN(0);
1973
1974                 rc = mdt_fids_different_target(info, mdt_object_fid(parent),
1975                                                mdt_object_fid(child));
1976                 RETURN(rc);
1977         }
1978
1979         /* client < 2.13.52 getattr_by_fid parent and child are the same */
1980         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1981         if (!buf->lb_buf)
1982                 RETURN(-ENOMEM);
1983
1984         ldata.ld_buf = buf;
1985         rc = mdt_links_read(info, child, &ldata);
1986         /* can't read linkea, just assume it's remote object */
1987         if (rc == -ENOENT || rc == -ENODATA)
1988                 RETURN(1);
1989         if (rc)
1990                 RETURN(rc);
1991
1992         leh = buf->lb_buf;
1993         lee = (struct link_ea_entry *)(leh + 1);
1994         for (i = 0; i < leh->leh_reccount; i++) {
1995                 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1996                 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1997                 if (mdt_fids_different_target(info, &pfid,
1998                                               mdt_object_fid(child)))
1999                         RETURN(1);
2000         }
2001
2002         RETURN(0);
2003 }
2004
2005 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
2006                              struct mdt_object *child)
2007 {
2008         struct lu_buf *buffer;
2009         struct mdt_body *repbody;
2010         struct req_capsule *pill = info->mti_pill;
2011         struct obd_export *exp = mdt_info_req(info)->rq_export;
2012         int rc = 0;
2013
2014         if (!exp_connect_encrypt(exp))
2015                 return rc;
2016
2017         if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX, RCL_SERVER) &&
2018             req_capsule_get_size(pill, &RMF_FILE_ENCCTX, RCL_SERVER) != 0) {
2019                 struct lu_attr la = { 0 };
2020                 struct dt_object *dt = mdt_obj2dt(child);
2021
2022                 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
2023                         dt_attr_get(info->mti_env, mdt_obj2dt(child), &la);
2024
2025                 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL) {
2026                         buffer = &info->mti_buf;
2027
2028                         /* fill reply buffer with encryption context now */
2029                         buffer->lb_len =
2030                                 req_capsule_get_size(pill, &RMF_FILE_ENCCTX,
2031                                                      RCL_SERVER);
2032                         buffer->lb_buf =
2033                                 req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
2034                         rc = mo_xattr_get(info->mti_env,
2035                                           mdt_object_child(child),
2036                                           buffer,
2037                                           LL_XATTR_NAME_ENCRYPTION_CONTEXT);
2038                         if (rc >= 0) {
2039                                 CDEBUG(D_SEC,
2040                                        "found encryption ctx of size %d for "DFID"\n",
2041                                        rc, PFID(mdt_object_fid(child)));
2042
2043                                 repbody = req_capsule_server_get(pill,
2044                                                                  &RMF_MDT_BODY);
2045                                 repbody->mbo_valid |= OBD_MD_ENCCTX;
2046                                 if (rc < buffer->lb_len)
2047                                         req_capsule_shrink(pill,
2048                                                            &RMF_FILE_ENCCTX, rc,
2049                                                            RCL_SERVER);
2050                                 rc = 0;
2051                         } else {
2052                                 CDEBUG(D_SEC,
2053                                        "encryption ctx not found for "DFID": rc = %d\n",
2054                                        PFID(mdt_object_fid(child)), rc);
2055                                 req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2056                                                    RCL_SERVER);
2057                                 /* handling -ENOENT is important because it may
2058                                  * change object state in DNE env dropping
2059                                  * LOHA_EXISTS flag, it is important to return
2060                                  * that to the caller.
2061                                  * Check LU-13115 for details.
2062                                  */
2063                                 if (rc != -ENOENT)
2064                                         rc = 0;
2065                         }
2066                 } else {
2067                         req_capsule_shrink(pill, &RMF_FILE_ENCCTX, 0,
2068                                            RCL_SERVER);
2069                 }
2070         }
2071         return rc;
2072 }