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