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