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