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