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