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