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