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