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