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