Whamcloud - gitweb
744b5e2bb10ae5e1c1a00f19a0af3722d47c2333
[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 <uapi/linux/lnet/nidstr.h>
53 #include <lustre_nodemap.h>
54
55 typedef enum ucred_init_type {
56         NONE_INIT       = 0,
57         BODY_INIT       = 1,
58         REC_INIT        = 2
59 } ucred_init_type_t;
60
61 static __u64 get_mrc_cr_flags(struct mdt_rec_create *mrc)
62 {
63         return (__u64)(mrc->cr_flags_l) | ((__u64)mrc->cr_flags_h << 32);
64 }
65
66 void mdt_exit_ucred(struct mdt_thread_info *info)
67 {
68         struct lu_ucred   *uc  = mdt_ucred(info);
69         struct mdt_device *mdt = info->mti_mdt;
70
71         LASSERT(uc != NULL);
72         if (uc->uc_valid != UCRED_INIT) {
73                 uc->uc_suppgids[0] = uc->uc_suppgids[1] = -1;
74                 if (uc->uc_ginfo) {
75                         put_group_info(uc->uc_ginfo);
76                         uc->uc_ginfo = NULL;
77                 }
78                 if (uc->uc_identity) {
79                         mdt_identity_put(mdt->mdt_identity_cache,
80                                          uc->uc_identity);
81                         uc->uc_identity = NULL;
82                 }
83                 uc->uc_valid = UCRED_INIT;
84         }
85 }
86
87 static int match_nosquash_list(struct rw_semaphore *sem,
88                                struct list_head *nidlist,
89                                lnet_nid_t peernid)
90 {
91         int rc;
92         ENTRY;
93         down_read(sem);
94         rc = cfs_match_nid(peernid, nidlist);
95         up_read(sem);
96         RETURN(rc);
97 }
98
99 /* root_squash for inter-MDS operations */
100 static int mdt_root_squash(struct mdt_thread_info *info, lnet_nid_t peernid)
101 {
102         struct lu_ucred *ucred = mdt_ucred(info);
103         struct root_squash_info *squash = &info->mti_mdt->mdt_squash;
104         ENTRY;
105
106         LASSERT(ucred != NULL);
107         if (!squash->rsi_uid || ucred->uc_fsuid)
108                 RETURN(0);
109
110         if (match_nosquash_list(&squash->rsi_sem,
111                                 &squash->rsi_nosquash_nids,
112                                 peernid)) {
113                 CDEBUG(D_OTHER, "%s is in nosquash_nids list\n",
114                        libcfs_nid2str(peernid));
115                 RETURN(0);
116         }
117
118         CDEBUG(D_OTHER, "squash req from %s, (%d:%d/%x)=>(%d:%d/%x)\n",
119                libcfs_nid2str(peernid),
120                ucred->uc_fsuid, ucred->uc_fsgid, ucred->uc_cap,
121                squash->rsi_uid, squash->rsi_gid, 0);
122
123         ucred->uc_fsuid = squash->rsi_uid;
124         ucred->uc_fsgid = squash->rsi_gid;
125         ucred->uc_cap = 0;
126         ucred->uc_suppgids[0] = -1;
127         ucred->uc_suppgids[1] = -1;
128
129         RETURN(0);
130 }
131
132 static void ucred_set_jobid(struct mdt_thread_info *info, struct lu_ucred *uc)
133 {
134         struct ptlrpc_request   *req = mdt_info_req(info);
135         const char              *jobid = mdt_req_get_jobid(req);
136
137         /* set jobid if specified. */
138         if (jobid)
139                 strlcpy(uc->uc_jobid, jobid, sizeof(uc->uc_jobid));
140         else
141                 uc->uc_jobid[0] = '\0';
142 }
143
144 static 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         __u32 lmm_magic = le32_to_cpu(lmm->lmm_magic);
610         __u16 count;
611         int i;
612
613         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
614                 return;
615
616         CDEBUG(level, "objid "DOSTID", magic 0x%08X, pattern %#X\n",
617                POSTID(&lmm->lmm_oi), lmm_magic,
618                le32_to_cpu(lmm->lmm_pattern));
619
620         /* No support for compount layouts yet */
621         if (lmm_magic != LOV_MAGIC_V1 && lmm_magic != LOV_MAGIC_V3)
622                 return;
623
624         count = le16_to_cpu(((struct lov_user_md *)lmm)->lmm_stripe_count);
625         CDEBUG(level, "stripe_size=0x%x, stripe_count=0x%x\n",
626                le32_to_cpu(lmm->lmm_stripe_size), count);
627
628         /* If it's a directory or a released file, then there are
629          * no actual objects to print, so bail out. */
630         if (valid & OBD_MD_FLDIREA ||
631             le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
632                 return;
633
634         LASSERT(count <= LOV_MAX_STRIPE_COUNT);
635         for (i = 0, lod = lmm->lmm_objects; i < count; i++, lod++) {
636                 struct ost_id oi;
637
638                 ostid_le_to_cpu(&lod->l_ost_oi, &oi);
639                 CDEBUG(level, "stripe %u idx %u subobj "DOSTID"\n",
640                        i, le32_to_cpu(lod->l_ost_idx), POSTID(&oi));
641         }
642 }
643
644 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv)
645 {
646         const struct lmv_mds_md_v1 *lmm1;
647         int                        i;
648
649         if (likely(!cfs_cdebug_show(level, DEBUG_SUBSYSTEM)))
650                 return;
651
652         lmm1 = &lmv->lmv_md_v1;
653         CDEBUG(level,
654                "magic 0x%08X, master %#X stripe_count %#x hash_type %#x\n",
655                le32_to_cpu(lmm1->lmv_magic),
656                le32_to_cpu(lmm1->lmv_master_mdt_index),
657                le32_to_cpu(lmm1->lmv_stripe_count),
658                le32_to_cpu(lmm1->lmv_hash_type));
659
660         if (le32_to_cpu(lmm1->lmv_magic) == LMV_MAGIC_STRIPE)
661                 return;
662
663         for (i = 0; i < le32_to_cpu(lmm1->lmv_stripe_count); i++) {
664                 struct lu_fid fid;
665
666                 fid_le_to_cpu(&fid, &lmm1->lmv_stripe_fids[i]);
667                 CDEBUG(level, "idx %u subobj "DFID"\n", i, PFID(&fid));
668         }
669 }
670
671 /* Shrink and/or grow reply buffers */
672 int mdt_fix_reply(struct mdt_thread_info *info)
673 {
674         struct req_capsule *pill = info->mti_pill;
675         struct mdt_body    *body;
676         int                md_size, md_packed = 0;
677         int                acl_size;
678         int                rc = 0;
679         ENTRY;
680
681         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
682         LASSERT(body != NULL);
683
684         if (body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE |
685                                OBD_MD_LINKNAME))
686                 md_size = body->mbo_eadatasize;
687         else
688                 md_size = 0;
689
690         acl_size = body->mbo_aclsize;
691
692         /* this replay - not send info to client */
693         if (info->mti_spec.no_create) {
694                 md_size = 0;
695                 acl_size = 0;
696         }
697
698         CDEBUG(D_INFO, "Shrink to md_size = %d cookie/acl_size = %d\n",
699                md_size, acl_size);
700 /*
701             &RMF_MDT_BODY,
702             &RMF_MDT_MD,
703             &RMF_ACL, or &RMF_LOGCOOKIES
704 (optional)  &RMF_CAPA1,
705 (optional)  &RMF_CAPA2,
706 (optional)  something else
707 */
708
709         /* MDT_MD buffer may be bigger than packed value, let's shrink all
710          * buffers before growing it */
711         if (info->mti_big_lmm_used) {
712                 /* big_lmm buffer may be used even without packing the result
713                  * into reply, just for internal server needs */
714                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
715                         md_packed = req_capsule_get_size(pill, &RMF_MDT_MD,
716                                                          RCL_SERVER);
717
718                 /* free big lmm if md_size is not needed */
719                 if (md_size == 0 || md_packed == 0) {
720                         info->mti_big_lmm_used = 0;
721                 } else {
722                         /* buffer must be allocated separately */
723                         LASSERT(info->mti_attr.ma_lmm !=
724                                 req_capsule_server_get(pill, &RMF_MDT_MD));
725                         req_capsule_shrink(pill, &RMF_MDT_MD, 0, RCL_SERVER);
726                 }
727         } else if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
728                 req_capsule_shrink(pill, &RMF_MDT_MD, md_size, RCL_SERVER);
729         }
730
731         if (info->mti_big_acl_used) {
732                 if (acl_size == 0)
733                         info->mti_big_acl_used = 0;
734                 else
735                         req_capsule_shrink(pill, &RMF_ACL, 0, RCL_SERVER);
736         } else if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER)) {
737                 req_capsule_shrink(pill, &RMF_ACL, acl_size, RCL_SERVER);
738         } else if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
739                 req_capsule_shrink(pill, &RMF_LOGCOOKIES, acl_size, RCL_SERVER);
740         }
741
742         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_SERVER) &&
743             !(body->mbo_valid & OBD_MD_FLMDSCAPA))
744                 req_capsule_shrink(pill, &RMF_CAPA1, 0, RCL_SERVER);
745
746         if (req_capsule_has_field(pill, &RMF_CAPA2, RCL_SERVER) &&
747             !(body->mbo_valid & OBD_MD_FLOSSCAPA))
748                 req_capsule_shrink(pill, &RMF_CAPA2, 0, RCL_SERVER);
749
750         /*
751          * Some more field should be shrinked if needed.
752          * This should be done by those who added fields to reply message.
753          */
754
755         /* Grow MD buffer if needed finally */
756         if (info->mti_big_lmm_used) {
757                 void *lmm;
758
759                 LASSERT(md_size > md_packed);
760                 CDEBUG(D_INFO, "Enlarge reply buffer, need extra %d bytes\n",
761                        md_size - md_packed);
762                 rc = req_capsule_server_grow(pill, &RMF_MDT_MD, md_size);
763                 if (rc) {
764                         /* we can't answer with proper LOV EA, drop flags,
765                          * the rc is also returned so this request is
766                          * considered as failed */
767                         body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_FLEASIZE);
768                         /* don't return transno along with error */
769                         lustre_msg_set_transno(pill->rc_req->rq_repmsg, 0);
770                 } else {
771                         /* now we need to pack right LOV/LMV EA */
772                         lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
773                         if (info->mti_attr.ma_valid & MA_LOV) {
774                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
775                                                              RCL_SERVER) ==
776                                                 info->mti_attr.ma_lmm_size);
777                                 memcpy(lmm, info->mti_attr.ma_lmm,
778                                        info->mti_attr.ma_lmm_size);
779                         } else if (info->mti_attr.ma_valid & MA_LMV) {
780                                 LASSERT(req_capsule_get_size(pill, &RMF_MDT_MD,
781                                                              RCL_SERVER) ==
782                                                 info->mti_attr.ma_lmv_size);
783                                 memcpy(lmm, info->mti_attr.ma_lmv,
784                                        info->mti_attr.ma_lmv_size);
785                         }
786                 }
787
788                 /* update mdt_max_mdsize so clients will be aware about that */
789                 if (info->mti_mdt->mdt_max_mdsize < info->mti_attr.ma_lmm_size)
790                         info->mti_mdt->mdt_max_mdsize =
791                                                 info->mti_attr.ma_lmm_size;
792                 info->mti_big_lmm_used = 0;
793         }
794
795         if (info->mti_big_acl_used) {
796                 CDEBUG(D_INFO, "Enlarge reply ACL buffer to %d bytes\n",
797                        acl_size);
798
799                 rc = req_capsule_server_grow(pill, &RMF_ACL, acl_size);
800                 if (rc) {
801                         body->mbo_valid &= ~OBD_MD_FLACL;
802                 } else {
803                         void *acl = req_capsule_server_get(pill, &RMF_ACL);
804
805                         memcpy(acl, info->mti_big_acl, acl_size);
806                 }
807
808                 info->mti_big_acl_used = 0;
809         }
810
811         RETURN(rc);
812 }
813
814
815 /* if object is dying, pack the lov/llog data,
816  * parameter info->mti_attr should be valid at this point!
817  * Also implements RAoLU policy */
818 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
819                            struct md_attr *ma)
820 {
821         struct mdt_body *repbody = NULL;
822         const struct lu_attr *la = &ma->ma_attr;
823         struct coordinator *cdt = &info->mti_mdt->mdt_coordinator;
824         int rc;
825         __u64 need = 0;
826         struct hsm_action_item hai = {
827                 .hai_len = sizeof(hai),
828                 .hai_action = HSMA_REMOVE,
829                 .hai_extent.length = -1,
830                 .hai_cookie = 0,
831                 .hai_gid = 0,
832         };
833         __u64 compound_id;
834         int archive_id;
835
836         ENTRY;
837
838         if (mdt_info_req(info) != NULL) {
839                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
840                 LASSERT(repbody != NULL);
841         } else {
842                 CDEBUG(D_INFO, "not running in a request/reply context\n");
843         }
844
845         if ((ma->ma_valid & MA_INODE) && repbody != NULL)
846                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(mo));
847
848         if (ma->ma_valid & MA_LOV) {
849                 CERROR("No need in LOV EA upon unlink\n");
850                 dump_stack();
851         }
852         if (repbody != NULL)
853                 repbody->mbo_eadatasize = 0;
854
855         /* Only check unlinked and archived if RAoLU and upon last close */
856         if (!cdt->cdt_remove_archive_on_last_unlink ||
857             atomic_read(&mo->mot_open_count) != 0)
858                 RETURN(0);
859
860         /* mdt_attr_get_complex will clear ma_valid, so check here first */
861         if ((ma->ma_valid & MA_INODE) && (ma->ma_attr.la_nlink != 0))
862                 RETURN(0);
863
864         if ((ma->ma_valid & MA_HSM) && (!(ma->ma_hsm.mh_flags & HS_EXISTS)))
865                 RETURN(0);
866
867         need |= (MA_INODE | MA_HSM) & ~ma->ma_valid;
868         if (need != 0) {
869                 /* ma->ma_valid is missing either MA_INODE, MA_HSM, or both,
870                  * try setting them */
871                 ma->ma_need |= need;
872                 rc = mdt_attr_get_complex(info, mo, ma);
873                 if (rc) {
874                         CERROR("%s: unable to fetch missing attributes of"
875                                DFID": rc=%d\n", mdt_obd_name(info->mti_mdt),
876                                PFID(mdt_object_fid(mo)), rc);
877                         RETURN(0);
878                 }
879
880                 if (need & MA_INODE) {
881                         if (ma->ma_valid & MA_INODE) {
882                                 if (ma->ma_attr.la_nlink != 0)
883                                         RETURN(0);
884                         } else {
885                                 RETURN(0);
886                         }
887                 }
888
889                 if (need & MA_HSM) {
890                         if (ma->ma_valid & MA_HSM) {
891                                 if (!(ma->ma_hsm.mh_flags & HS_EXISTS))
892                                         RETURN(0);
893                         } else {
894                                 RETURN(0);
895                         }
896                 }
897         }
898
899         /* RAoLU policy is active, last close on file has occured,
900          * file is unlinked, file is archived, so create remove request
901          * for copytool!
902          * If CDT is not running, requests will be logged for later. */
903         compound_id = atomic_inc_return(&cdt->cdt_compound_id);
904         if (ma->ma_hsm.mh_arch_id != 0)
905                 archive_id = ma->ma_hsm.mh_arch_id;
906         else
907                 archive_id = cdt->cdt_default_archive_id;
908
909         hai.hai_fid = *mdt_object_fid(mo);
910
911         rc = mdt_agent_record_add(info->mti_env, info->mti_mdt,
912                                   compound_id, archive_id, 0, &hai);
913         if (rc)
914                 CERROR("%s: unable to add HSM remove request for "DFID
915                        ": rc=%d\n", mdt_obd_name(info->mti_mdt),
916                        PFID(mdt_object_fid(mo)), rc);
917
918         RETURN(0);
919 }
920
921 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
922                                   struct md_attr *ma)
923 {
924         __u64 out;
925
926         out = 0;
927         if (in & MDS_ATTR_MODE)
928                 out |= LA_MODE;
929         if (in & MDS_ATTR_UID)
930                 out |= LA_UID;
931         if (in & MDS_ATTR_GID)
932                 out |= LA_GID;
933         if (in & MDS_ATTR_SIZE)
934                 out |= LA_SIZE;
935         if (in & MDS_ATTR_BLOCKS)
936                 out |= LA_BLOCKS;
937         if (in & MDS_ATTR_ATIME_SET)
938                 out |= LA_ATIME;
939         if (in & MDS_ATTR_CTIME_SET)
940                 out |= LA_CTIME;
941         if (in & MDS_ATTR_MTIME_SET)
942                 out |= LA_MTIME;
943         if (in & MDS_ATTR_ATTR_FLAG)
944                 out |= LA_FLAGS;
945         if (in & MDS_ATTR_KILL_SUID)
946                 out |= LA_KILL_SUID;
947         if (in & MDS_ATTR_KILL_SGID)
948                 out |= LA_KILL_SGID;
949         if (in & MDS_ATTR_PROJID)
950                 out |= LA_PROJID;
951
952         if (in & MDS_ATTR_FROM_OPEN)
953                 rr->rr_flags |= MRF_OPEN_TRUNC;
954         if (in & MDS_OPEN_OWNEROVERRIDE)
955                 ma->ma_attr_flags |= MDS_OWNEROVERRIDE;
956         if (in & MDS_ATTR_FORCE)
957                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
958
959         in &= ~(MDS_ATTR_MODE | MDS_ATTR_UID | MDS_ATTR_GID | MDS_ATTR_PROJID |
960                 MDS_ATTR_ATIME | MDS_ATTR_MTIME | MDS_ATTR_CTIME |
961                 MDS_ATTR_ATIME_SET | MDS_ATTR_CTIME_SET | MDS_ATTR_MTIME_SET |
962                 MDS_ATTR_SIZE | MDS_ATTR_BLOCKS | MDS_ATTR_ATTR_FLAG |
963                 MDS_ATTR_FORCE | MDS_ATTR_KILL_SUID | MDS_ATTR_KILL_SGID |
964                 MDS_ATTR_FROM_OPEN | MDS_OPEN_OWNEROVERRIDE);
965         if (in != 0)
966                 CERROR("Unknown attr bits: %#llx\n", in);
967         return out;
968 }
969
970 /* unpacking */
971
972 int mdt_name_unpack(struct req_capsule *pill,
973                     const struct req_msg_field *field,
974                     struct lu_name *ln,
975                     enum mdt_name_flags flags)
976 {
977         ln->ln_name = req_capsule_client_get(pill, field);
978         ln->ln_namelen = req_capsule_get_size(pill, field, RCL_CLIENT) - 1;
979
980         if (!lu_name_is_valid(ln)) {
981                 ln->ln_name = NULL;
982                 ln->ln_namelen = 0;
983
984                 return -EPROTO;
985         }
986
987         if ((flags & MNF_FIX_ANON) &&
988             ln->ln_namelen == 1 && ln->ln_name[0] == '/') {
989                 /* Newer (3.x) kernels use a name of "/" for the
990                  * "anonymous" disconnected dentries from NFS
991                  * filehandle conversion. See d_obtain_alias(). */
992                 ln->ln_name = NULL;
993                 ln->ln_namelen = 0;
994         }
995
996         return 0;
997 }
998
999 static int mdt_file_secctx_unpack(struct req_capsule *pill,
1000                                   const char **secctx_name,
1001                                   void **secctx, size_t *secctx_size)
1002 {
1003         const char *name;
1004         size_t name_size;
1005
1006         *secctx_name = NULL;
1007         *secctx = NULL;
1008         *secctx_size = 0;
1009
1010         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT) ||
1011             !req_capsule_field_present(pill, &RMF_FILE_SECCTX_NAME, RCL_CLIENT))
1012                 return 0;
1013
1014         name_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
1015                                          RCL_CLIENT);
1016         if (name_size == 0)
1017                 return 0;
1018
1019         name = req_capsule_client_get(pill, &RMF_FILE_SECCTX_NAME);
1020         if (strnlen(name, name_size) != name_size - 1)
1021                 return -EPROTO;
1022
1023         if (!req_capsule_has_field(pill, &RMF_FILE_SECCTX, RCL_CLIENT) ||
1024             !req_capsule_field_present(pill, &RMF_FILE_SECCTX, RCL_CLIENT))
1025                 return -EPROTO;
1026
1027         *secctx_name = name;
1028         *secctx = req_capsule_client_get(pill, &RMF_FILE_SECCTX);
1029         *secctx_size = req_capsule_get_size(pill, &RMF_FILE_SECCTX, RCL_CLIENT);
1030
1031         return 0;
1032 }
1033
1034 static int mdt_setattr_unpack_rec(struct mdt_thread_info *info)
1035 {
1036         struct lu_ucred *uc = mdt_ucred(info);
1037         struct md_attr *ma = &info->mti_attr;
1038         struct lu_attr *la = &ma->ma_attr;
1039         struct req_capsule *pill = info->mti_pill;
1040         struct mdt_reint_record *rr = &info->mti_rr;
1041         struct mdt_rec_setattr *rec;
1042         struct lu_nodemap *nodemap;
1043
1044         ENTRY;
1045
1046         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1047         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1048         if (rec == NULL)
1049                 RETURN(-EFAULT);
1050
1051         /* This prior initialization is needed for old_init_ucred_reint() */
1052         uc->uc_fsuid = rec->sa_fsuid;
1053         uc->uc_fsgid = rec->sa_fsgid;
1054         uc->uc_cap   = rec->sa_cap;
1055         uc->uc_suppgids[0] = rec->sa_suppgid;
1056         uc->uc_suppgids[1] = -1;
1057
1058         rr->rr_fid1 = &rec->sa_fid;
1059         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma);
1060         la->la_mode  = rec->sa_mode;
1061         la->la_flags = rec->sa_attr_flags;
1062
1063         nodemap = nodemap_get_from_exp(info->mti_exp);
1064         if (IS_ERR(nodemap))
1065                 RETURN(PTR_ERR(nodemap));
1066
1067         la->la_uid   = nodemap_map_id(nodemap, NODEMAP_UID,
1068                                       NODEMAP_CLIENT_TO_FS, rec->sa_uid);
1069         la->la_gid   = nodemap_map_id(nodemap, NODEMAP_GID,
1070                                       NODEMAP_CLIENT_TO_FS, rec->sa_gid);
1071         la->la_projid = rec->sa_projid;
1072         nodemap_putref(nodemap);
1073
1074         la->la_size  = rec->sa_size;
1075         la->la_blocks = rec->sa_blocks;
1076         la->la_ctime = rec->sa_ctime;
1077         la->la_atime = rec->sa_atime;
1078         la->la_mtime = rec->sa_mtime;
1079         ma->ma_valid = MA_INODE;
1080
1081         if (rec->sa_bias & MDS_DATA_MODIFIED)
1082                 ma->ma_attr_flags |= MDS_DATA_MODIFIED;
1083         else
1084                 ma->ma_attr_flags &= ~MDS_DATA_MODIFIED;
1085
1086         ma->ma_attr_flags &= ~MDS_CLOSE_INTENT;
1087         ma->ma_attr_flags |= rec->sa_bias & MDS_CLOSE_INTENT;
1088         RETURN(0);
1089 }
1090
1091 static int mdt_close_handle_unpack(struct mdt_thread_info *info)
1092 {
1093         struct req_capsule *pill = info->mti_pill;
1094         struct mdt_ioepoch *ioepoch;
1095         ENTRY;
1096
1097         if (req_capsule_get_size(pill, &RMF_MDT_EPOCH, RCL_CLIENT))
1098                 ioepoch = req_capsule_client_get(pill, &RMF_MDT_EPOCH);
1099         else
1100                 ioepoch = NULL;
1101
1102         if (ioepoch == NULL)
1103                 RETURN(-EPROTO);
1104
1105         info->mti_close_handle = ioepoch->mio_handle;
1106
1107         RETURN(0);
1108 }
1109
1110 static inline int mdt_dlmreq_unpack(struct mdt_thread_info *info) {
1111         struct req_capsule      *pill = info->mti_pill;
1112
1113         if (req_capsule_get_size(pill, &RMF_DLM_REQ, RCL_CLIENT)) {
1114                 info->mti_dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
1115                 if (info->mti_dlm_req == NULL)
1116                         RETURN(-EFAULT);
1117         }
1118
1119         RETURN(0);
1120 }
1121
1122 static int mdt_setattr_unpack(struct mdt_thread_info *info)
1123 {
1124         struct mdt_reint_record *rr = &info->mti_rr;
1125         struct md_attr          *ma = &info->mti_attr;
1126         struct req_capsule      *pill = info->mti_pill;
1127         int rc;
1128         ENTRY;
1129
1130         rc = mdt_setattr_unpack_rec(info);
1131         if (rc)
1132                 RETURN(rc);
1133
1134         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1135                 rr->rr_eadata = req_capsule_client_get(pill, &RMF_EADATA);
1136                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1137                                                         RCL_CLIENT);
1138                 if (rr->rr_eadatalen > 0) {
1139                         const struct lmv_user_md        *lum;
1140
1141                         lum = rr->rr_eadata;
1142                         /* Sigh ma_valid(from req) does not indicate whether
1143                          * it will set LOV/LMV EA, so we have to check magic */
1144                         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1145                                 ma->ma_valid |= MA_LMV;
1146                                 ma->ma_lmv = (void *)rr->rr_eadata;
1147                                 ma->ma_lmv_size = rr->rr_eadatalen;
1148                         } else {
1149                                 ma->ma_valid |= MA_LOV;
1150                                 ma->ma_lmm = (void *)rr->rr_eadata;
1151                                 ma->ma_lmm_size = rr->rr_eadatalen;
1152                         }
1153                 }
1154         }
1155
1156         rc = mdt_dlmreq_unpack(info);
1157         RETURN(rc);
1158 }
1159
1160 static int mdt_close_intent_unpack(struct mdt_thread_info *info)
1161 {
1162         struct md_attr          *ma = &info->mti_attr;
1163         struct req_capsule      *pill = info->mti_pill;
1164         ENTRY;
1165
1166         if (!(ma->ma_attr_flags & MDS_CLOSE_INTENT))
1167                 RETURN(0);
1168
1169         req_capsule_extend(pill, &RQF_MDS_CLOSE_INTENT);
1170
1171         if (!(req_capsule_has_field(pill, &RMF_CLOSE_DATA, RCL_CLIENT) &&
1172             req_capsule_field_present(pill, &RMF_CLOSE_DATA, RCL_CLIENT)))
1173                 RETURN(-EFAULT);
1174
1175         RETURN(0);
1176 }
1177
1178 int mdt_close_unpack(struct mdt_thread_info *info)
1179 {
1180         int rc;
1181         ENTRY;
1182
1183         rc = mdt_close_handle_unpack(info);
1184         if (rc)
1185                 RETURN(rc);
1186
1187         rc = mdt_setattr_unpack_rec(info);
1188         if (rc)
1189                 RETURN(rc);
1190
1191         rc = mdt_close_intent_unpack(info);
1192         if (rc)
1193                 RETURN(rc);
1194
1195         RETURN(mdt_init_ucred_reint(info));
1196 }
1197
1198 static int mdt_create_unpack(struct mdt_thread_info *info)
1199 {
1200         struct lu_ucred *uc  = mdt_ucred(info);
1201         struct mdt_rec_create *rec;
1202         struct lu_attr *attr = &info->mti_attr.ma_attr;
1203         struct mdt_reint_record *rr = &info->mti_rr;
1204         struct req_capsule *pill = info->mti_pill;
1205         struct md_op_spec *sp = &info->mti_spec;
1206         int rc;
1207
1208         ENTRY;
1209
1210         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1211         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1212         if (rec == NULL)
1213                 RETURN(-EFAULT);
1214
1215         /* This prior initialization is needed for old_init_ucred_reint() */
1216         uc->uc_fsuid = rec->cr_fsuid;
1217         uc->uc_fsgid = rec->cr_fsgid;
1218         uc->uc_cap   = rec->cr_cap;
1219         uc->uc_suppgids[0] = rec->cr_suppgid1;
1220         uc->uc_suppgids[1] = -1;
1221         uc->uc_umask = rec->cr_umask;
1222
1223         rr->rr_fid1 = &rec->cr_fid1;
1224         rr->rr_fid2 = &rec->cr_fid2;
1225         attr->la_mode = rec->cr_mode;
1226         attr->la_rdev  = rec->cr_rdev;
1227         attr->la_uid   = rec->cr_fsuid;
1228         attr->la_gid   = rec->cr_fsgid;
1229         attr->la_ctime = rec->cr_time;
1230         attr->la_mtime = rec->cr_time;
1231         attr->la_atime = rec->cr_time;
1232         attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_TYPE |
1233                          LA_CTIME | LA_MTIME | LA_ATIME;
1234         memset(&sp->u, 0, sizeof(sp->u));
1235         sp->sp_cr_flags = get_mrc_cr_flags(rec);
1236
1237         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1238         if (rc < 0)
1239                 RETURN(rc);
1240
1241         if (S_ISLNK(attr->la_mode)) {
1242                 const char *tgt = NULL;
1243
1244                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_SYM);
1245                 if (req_capsule_get_size(pill, &RMF_SYMTGT, RCL_CLIENT)) {
1246                         tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
1247                         sp->u.sp_symname = tgt;
1248                 }
1249                 if (tgt == NULL)
1250                         RETURN(-EFAULT);
1251         } else {
1252                 req_capsule_extend(pill, &RQF_MDS_REINT_CREATE_ACL);
1253                 if (S_ISDIR(attr->la_mode) &&
1254                     req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT) > 0) {
1255                         sp->u.sp_ea.eadata =
1256                                 req_capsule_client_get(pill, &RMF_EADATA);
1257                         sp->u.sp_ea.eadatalen =
1258                                 req_capsule_get_size(pill, &RMF_EADATA,
1259                                                      RCL_CLIENT);
1260                         sp->sp_cr_flags |= MDS_OPEN_HAS_EA;
1261                 }
1262         }
1263
1264         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1265                                     &sp->sp_cr_file_secctx,
1266                                     &sp->sp_cr_file_secctx_size);
1267         if (rc < 0)
1268                 RETURN(rc);
1269
1270         rc = mdt_dlmreq_unpack(info);
1271         RETURN(rc);
1272 }
1273
1274 static int mdt_link_unpack(struct mdt_thread_info *info)
1275 {
1276         struct lu_ucred *uc  = mdt_ucred(info);
1277         struct mdt_rec_link *rec;
1278         struct lu_attr *attr = &info->mti_attr.ma_attr;
1279         struct mdt_reint_record *rr = &info->mti_rr;
1280         struct req_capsule *pill = info->mti_pill;
1281         int rc;
1282
1283         ENTRY;
1284
1285         CLASSERT(sizeof(*rec) == 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
1324         ENTRY;
1325
1326         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1327         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1328         if (rec == NULL)
1329                 RETURN(-EFAULT);
1330
1331         /* This prior initialization is needed for old_init_ucred_reint() */
1332         uc->uc_fsuid = rec->ul_fsuid;
1333         uc->uc_fsgid = rec->ul_fsgid;
1334         uc->uc_cap   = rec->ul_cap;
1335         uc->uc_suppgids[0] = rec->ul_suppgid1;
1336         uc->uc_suppgids[1] = -1;
1337
1338         attr->la_uid = rec->ul_fsuid;
1339         attr->la_gid = rec->ul_fsgid;
1340         rr->rr_fid1 = &rec->ul_fid1;
1341         rr->rr_fid2 = &rec->ul_fid2;
1342         attr->la_ctime = rec->ul_time;
1343         attr->la_mtime = rec->ul_time;
1344         attr->la_mode  = rec->ul_mode;
1345         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1346
1347         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1348         if (rc < 0)
1349                 RETURN(rc);
1350
1351         if (rec->ul_bias & MDS_VTX_BYPASS)
1352                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1353         else
1354                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1355
1356         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1357
1358         rc = mdt_dlmreq_unpack(info);
1359         RETURN(rc);
1360 }
1361
1362 static int mdt_rmentry_unpack(struct mdt_thread_info *info)
1363 {
1364         info->mti_spec.sp_rm_entry = 1;
1365         return mdt_unlink_unpack(info);
1366 }
1367
1368 static int mdt_rename_unpack(struct mdt_thread_info *info)
1369 {
1370         struct lu_ucred *uc = mdt_ucred(info);
1371         struct mdt_rec_rename *rec;
1372         struct md_attr *ma = &info->mti_attr;
1373         struct lu_attr *attr = &info->mti_attr.ma_attr;
1374         struct mdt_reint_record *rr = &info->mti_rr;
1375         struct req_capsule *pill = info->mti_pill;
1376         int rc;
1377
1378         ENTRY;
1379
1380         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1381         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1382         if (rec == NULL)
1383                 RETURN(-EFAULT);
1384
1385         /* This prior initialization is needed for old_init_ucred_reint() */
1386         uc->uc_fsuid = rec->rn_fsuid;
1387         uc->uc_fsgid = rec->rn_fsgid;
1388         uc->uc_cap   = rec->rn_cap;
1389         uc->uc_suppgids[0] = rec->rn_suppgid1;
1390         uc->uc_suppgids[1] = rec->rn_suppgid2;
1391
1392         attr->la_uid = rec->rn_fsuid;
1393         attr->la_gid = rec->rn_fsgid;
1394         rr->rr_fid1 = &rec->rn_fid1;
1395         rr->rr_fid2 = &rec->rn_fid2;
1396         attr->la_ctime = rec->rn_time;
1397         attr->la_mtime = rec->rn_time;
1398         /* rename_tgt contains the mode already */
1399         attr->la_mode = rec->rn_mode;
1400         attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
1401
1402         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1403         if (rc < 0)
1404                 RETURN(rc);
1405
1406         rc = mdt_name_unpack(pill, &RMF_SYMTGT, &rr->rr_tgt_name, 0);
1407         if (rc < 0)
1408                 RETURN(rc);
1409
1410         if (rec->rn_bias & MDS_VTX_BYPASS)
1411                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
1412         else
1413                 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
1414
1415         if (rec->rn_bias & MDS_RENAME_MIGRATE) {
1416                 req_capsule_extend(info->mti_pill, &RQF_MDS_REINT_MIGRATE);
1417                 rc = mdt_close_handle_unpack(info);
1418                 if (rc < 0)
1419                         RETURN(rc);
1420                 info->mti_spec.sp_migrate_close = 1;
1421         }
1422
1423         info->mti_spec.no_create = !!req_is_replay(mdt_info_req(info));
1424
1425
1426         rc = mdt_dlmreq_unpack(info);
1427
1428         RETURN(rc);
1429 }
1430
1431 /*
1432  * please see comment above LOV_MAGIC_V1_DEFINED
1433  */
1434 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata)
1435 {
1436         struct lov_user_md_v1   *v1 = eadata;
1437
1438         LASSERT(v1);
1439
1440         if (unlikely(req_is_replay(mdt_info_req(info)))) {
1441                 if ((v1->lmm_magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
1442                         v1->lmm_magic |= LOV_MAGIC_DEFINED;
1443                 else if ((v1->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
1444                          __swab32(LOV_MAGIC_MAGIC))
1445                         v1->lmm_magic |= __swab32(LOV_MAGIC_DEFINED);
1446         }
1447 }
1448
1449 static int mdt_open_unpack(struct mdt_thread_info *info)
1450 {
1451         struct lu_ucred *uc = mdt_ucred(info);
1452         struct mdt_rec_create *rec;
1453         struct lu_attr *attr = &info->mti_attr.ma_attr;
1454         struct req_capsule *pill = info->mti_pill;
1455         struct mdt_reint_record *rr = &info->mti_rr;
1456         struct ptlrpc_request *req = mdt_info_req(info);
1457         struct md_op_spec *sp = &info->mti_spec;
1458         int rc;
1459         ENTRY;
1460
1461         CLASSERT(sizeof(struct mdt_rec_create) == sizeof(struct mdt_rec_reint));
1462         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1463         if (rec == NULL)
1464                 RETURN(-EFAULT);
1465
1466         /* This prior initialization is needed for old_init_ucred_reint() */
1467         uc->uc_fsuid = rec->cr_fsuid;
1468         uc->uc_fsgid = rec->cr_fsgid;
1469         uc->uc_cap   = rec->cr_cap;
1470         uc->uc_suppgids[0] = rec->cr_suppgid1;
1471         uc->uc_suppgids[1] = rec->cr_suppgid2;
1472         uc->uc_umask = rec->cr_umask;
1473
1474         rr->rr_fid1   = &rec->cr_fid1;
1475         rr->rr_fid2   = &rec->cr_fid2;
1476         rr->rr_handle = &rec->cr_old_handle;
1477         attr->la_mode = rec->cr_mode;
1478         attr->la_rdev  = rec->cr_rdev;
1479         attr->la_uid   = rec->cr_fsuid;
1480         attr->la_gid   = rec->cr_fsgid;
1481         attr->la_ctime = rec->cr_time;
1482         attr->la_mtime = rec->cr_time;
1483         attr->la_atime = rec->cr_time;
1484         attr->la_valid = LA_MODE  | LA_RDEV  | LA_UID   | LA_GID |
1485                          LA_CTIME | LA_MTIME | LA_ATIME;
1486         memset(&info->mti_spec.u, 0, sizeof(info->mti_spec.u));
1487         info->mti_spec.sp_cr_flags = get_mrc_cr_flags(rec);
1488         /* Do not trigger ASSERTION if client miss to set such flags. */
1489         if (unlikely(info->mti_spec.sp_cr_flags == 0))
1490                 RETURN(-EPROTO);
1491
1492         info->mti_cross_ref = !!(rec->cr_bias & MDS_CROSS_REF);
1493
1494         mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, MNF_FIX_ANON);
1495
1496         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1497                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1498                                                         RCL_CLIENT);
1499                 if (rr->rr_eadatalen > 0) {
1500                         rr->rr_eadata = req_capsule_client_get(pill,
1501                                                                &RMF_EADATA);
1502                         sp->u.sp_ea.eadatalen = rr->rr_eadatalen;
1503                         sp->u.sp_ea.eadata = rr->rr_eadata;
1504                         sp->no_create = !!req_is_replay(req);
1505                         mdt_fix_lov_magic(info, rr->rr_eadata);
1506                 }
1507
1508                 /*
1509                  * Client default md_size may be 0 right after client start,
1510                  * until all osc are connected, set here just some reasonable
1511                  * value to prevent misbehavior.
1512                  */
1513                 if (rr->rr_eadatalen == 0 &&
1514                     !(info->mti_spec.sp_cr_flags & MDS_OPEN_DELAY_CREATE))
1515                         rr->rr_eadatalen = MIN_MD_SIZE;
1516         }
1517
1518         rc = mdt_file_secctx_unpack(pill, &sp->sp_cr_file_secctx_name,
1519                                     &sp->sp_cr_file_secctx,
1520                                     &sp->sp_cr_file_secctx_size);
1521
1522         RETURN(rc);
1523 }
1524
1525 static int mdt_setxattr_unpack(struct mdt_thread_info *info)
1526 {
1527         struct mdt_reint_record *rr = &info->mti_rr;
1528         struct lu_ucred *uc = mdt_ucred(info);
1529         struct lu_attr *attr = &info->mti_attr.ma_attr;
1530         struct req_capsule *pill = info->mti_pill;
1531         struct mdt_rec_setxattr *rec;
1532         int rc;
1533         ENTRY;
1534
1535
1536         CLASSERT(sizeof(struct mdt_rec_setxattr) ==
1537                  sizeof(struct mdt_rec_reint));
1538
1539         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1540         if (rec == NULL)
1541                 RETURN(-EFAULT);
1542
1543         /* This prior initialization is needed for old_init_ucred_reint() */
1544         uc->uc_fsuid  = rec->sx_fsuid;
1545         uc->uc_fsgid  = rec->sx_fsgid;
1546         uc->uc_cap    = rec->sx_cap;
1547         uc->uc_suppgids[0] = rec->sx_suppgid1;
1548         uc->uc_suppgids[1] = -1;
1549
1550         rr->rr_opcode = rec->sx_opcode;
1551         rr->rr_fid1   = &rec->sx_fid;
1552         attr->la_valid = rec->sx_valid;
1553         attr->la_ctime = rec->sx_time;
1554         attr->la_size = rec->sx_size;
1555         attr->la_flags = rec->sx_flags;
1556
1557         rc = mdt_name_unpack(pill, &RMF_NAME, &rr->rr_name, 0);
1558         if (rc < 0)
1559                 RETURN(rc);
1560
1561         if (req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
1562                 rr->rr_eadatalen = req_capsule_get_size(pill, &RMF_EADATA,
1563                                                         RCL_CLIENT);
1564                 if (rr->rr_eadatalen > 0) {
1565                         rr->rr_eadata = req_capsule_client_get(pill,
1566                                                                &RMF_EADATA);
1567                         if (rr->rr_eadata == NULL)
1568                                 RETURN(-EFAULT);
1569                 } else {
1570                         rr->rr_eadata = NULL;
1571                 }
1572         } else if (!(attr->la_valid & OBD_MD_FLXATTRRM)) {
1573                 CDEBUG(D_INFO, "no xattr data supplied\n");
1574                 RETURN(-EFAULT);
1575         }
1576
1577         if (mdt_dlmreq_unpack(info) < 0)
1578                 RETURN(-EPROTO);
1579
1580         RETURN(0);
1581 }
1582
1583 static int mdt_resync_unpack(struct mdt_thread_info *info)
1584 {
1585         struct req_capsule      *pill = info->mti_pill;
1586         struct mdt_reint_record *rr   = &info->mti_rr;
1587         struct lu_ucred         *uc     = mdt_ucred(info);
1588         struct mdt_rec_resync   *rec;
1589         ENTRY;
1590
1591         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
1592         rec = req_capsule_client_get(pill, &RMF_REC_REINT);
1593         if (rec == NULL)
1594                 RETURN(-EFAULT);
1595
1596         /* This prior initialization is needed for old_init_ucred_reint() */
1597         uc->uc_fsuid = rec->rs_fsuid;
1598         uc->uc_fsgid = rec->rs_fsgid;
1599         uc->uc_cap   = rec->rs_cap;
1600
1601         rr->rr_fid1   = &rec->rs_fid;
1602
1603         /* cookie doesn't need to be swapped but it has been swapped
1604          * in lustre_swab_mdt_rec_reint() as rr_mtime, so here it needs
1605          * restoring. */
1606         if (ptlrpc_req_need_swab(mdt_info_req(info)))
1607                 __swab64s(&rec->rs_handle.cookie);
1608         rr->rr_handle = &rec->rs_handle;
1609
1610         RETURN(mdt_dlmreq_unpack(info));
1611 }
1612
1613 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
1614
1615 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
1616         [REINT_SETATTR]  = mdt_setattr_unpack,
1617         [REINT_CREATE]   = mdt_create_unpack,
1618         [REINT_LINK]     = mdt_link_unpack,
1619         [REINT_UNLINK]   = mdt_unlink_unpack,
1620         [REINT_RENAME]   = mdt_rename_unpack,
1621         [REINT_OPEN]     = mdt_open_unpack,
1622         [REINT_SETXATTR] = mdt_setxattr_unpack,
1623         [REINT_RMENTRY]  = mdt_rmentry_unpack,
1624         [REINT_MIGRATE]  = mdt_rename_unpack,
1625         [REINT_RESYNC]   = mdt_resync_unpack,
1626 };
1627
1628 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
1629 {
1630         int rc;
1631         ENTRY;
1632
1633         memset(&info->mti_rr, 0, sizeof(info->mti_rr));
1634         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
1635                 info->mti_rr.rr_opcode = op;
1636                 rc = mdt_reint_unpackers[op](info);
1637         } else {
1638                 CERROR("Unexpected opcode %d\n", op);
1639                 rc = -EFAULT;
1640         }
1641         RETURN(rc);
1642 }