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