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