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