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