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