Whamcloud - gitweb
* add a bit in ptlrpc_request to indicate ptlrpcs error, gss recovery
[fs/lustre-release.git] / lustre / mds / mds_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define DEBUG_SUBSYSTEM S_MDS
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/version.h>
32 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
33 # include <linux/locks.h>   // for wait_on_buffer
34 #else
35 # include <linux/buffer_head.h>   // for wait_on_buffer
36 #endif
37 #include <linux/unistd.h>
38
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41
42 #include <linux/fs.h>
43 #include <linux/stat.h>
44 #include <asm/uaccess.h>
45 #include <linux/slab.h>
46 #include <asm/segment.h>
47
48 #include <linux/obd_support.h>
49 #include <linux/lustre_lib.h>
50 #include <linux/lustre_ucache.h>
51 #include "mds_internal.h"
52
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
54 struct group_info *groups_alloc(int ngroups)
55 {
56         struct group_info *ginfo;
57
58         LASSERT(ngroups <= NGROUPS_SMALL);
59
60         OBD_ALLOC(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
61         if (!ginfo)
62                 return NULL;
63         ginfo->ngroups = ngroups;
64         ginfo->nblocks = 1;
65         ginfo->blocks[0] = ginfo->small_block;
66         atomic_set(&ginfo->usage, 1);
67
68         return ginfo;
69 }
70
71 void groups_free(struct group_info *ginfo)
72 {
73         LASSERT(ginfo->ngroups <= NGROUPS_SMALL);
74         LASSERT(ginfo->nblocks == 1);
75         LASSERT(ginfo->blocks[0] == ginfo->small_block);
76
77         OBD_FREE(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
78 }
79
80 /* for 2.4 the group number is small, so simply search the
81  * whole array.
82  */
83 int groups_search(struct group_info *ginfo, gid_t grp)
84 {
85         int i;
86
87         if (!ginfo)
88                 return 0;
89
90         for (i = 0; i < ginfo->ngroups; i++)
91                 if (GROUP_AT(ginfo, i) == grp)
92                         return 1;
93         return 0;
94 }
95
96 #else /* >= 2.6.4 */
97
98 void groups_sort(struct group_info *ginfo)
99 {
100         int base, max, stride;
101         int gidsetsize = ginfo->ngroups;
102
103         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
104                 ; /* nothing */
105         stride /= 3;
106
107         while (stride) {
108                 max = gidsetsize - stride;
109                 for (base = 0; base < max; base++) {
110                         int left = base;
111                         int right = left + stride;
112                         gid_t tmp = GROUP_AT(ginfo, right);
113                                                                                                     
114                         while (left >= 0 && GROUP_AT(ginfo, left) > tmp) {
115                                 GROUP_AT(ginfo, right) =
116                                     GROUP_AT(ginfo, left);
117                                 right = left;
118                                 left -= stride;
119                         }
120                         GROUP_AT(ginfo, right) = tmp;
121                 }
122                 stride /= 3;
123         }
124 }
125
126 int groups_search(struct group_info *ginfo, gid_t grp)
127 {
128         int left, right;
129
130         if (!ginfo)
131                 return 0;
132
133         left = 0;
134         right = ginfo->ngroups;
135         while (left < right) {
136                 int mid = (left + right) / 2;
137                 int cmp = grp - GROUP_AT(ginfo, mid);
138                 if (cmp > 0)
139                         left = mid + 1;
140                 else if (cmp < 0)
141                         right = mid;
142                 else
143                         return 1;
144         }
145         return 0;
146 }
147 #endif
148
149 void groups_from_buffer(struct group_info *ginfo, __u32 *gids)
150 {
151         int i, ngroups = ginfo->ngroups;
152
153         for (i = 0; i < ginfo->nblocks; i++) {
154                 int count = min(NGROUPS_PER_BLOCK, ngroups);
155
156                 memcpy(ginfo->blocks[i], gids, count * sizeof(__u32));
157                 gids += NGROUPS_PER_BLOCK;
158                 ngroups -= count;
159         }
160 }
161
162 void mds_pack_dentry2id(struct obd_device *obd,
163                         struct lustre_id *id,
164                         struct dentry *dentry,
165                         int fid)
166 {
167         id_ino(id) = dentry->d_inum;
168         id_gen(id) = dentry->d_generation;
169         
170         if (fid) {
171                 id_fid(id) = dentry->d_fid;
172                 id_group(id) = dentry->d_mdsnum;
173         }
174 }
175
176 void mds_pack_dentry2body(struct obd_device *obd,
177                           struct mds_body *b,
178                           struct dentry *dentry,
179                           int fid)
180 {
181         b->valid |= OBD_MD_FLID | OBD_MD_FLGENER |
182                 OBD_MD_MDS;
183
184         if (fid)
185                 b->valid |= OBD_MD_FID;
186         
187         mds_pack_dentry2id(obd, &b->id1, dentry, fid);
188 }
189
190 int mds_pack_inode2id(struct obd_device *obd,
191                       struct lustre_id *id,
192                       struct inode *inode,
193                       int fid)
194 {
195         int rc = 0;
196         ENTRY;
197
198         if (fid) {
199                 /* we have to avoid deadlock. */
200                 if (!down_trylock(&inode->i_sem)) {
201                         rc = mds_read_inode_sid(obd, inode, id);
202                         up(&inode->i_sem);
203                 } else {
204                         rc = mds_read_inode_sid(obd, inode, id);
205                 }
206         }
207
208         if (rc == 0) {
209                 id_ino(id) = inode->i_ino;
210                 id_gen(id) = inode->i_generation;
211                 id_type(id) = (S_IFMT & inode->i_mode);
212         }
213         RETURN(rc);
214 }
215
216 /* Note that we can copy all of the fields, just some will not be "valid" */
217 void mds_pack_inode2body(struct obd_device *obd, struct mds_body *b,
218                          struct inode *inode, int fid)
219 {
220         b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
221                 OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
222                 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
223                 OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
224
225         if (!S_ISREG(inode->i_mode)) {
226                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
227                         OBD_MD_FLATIME | OBD_MD_FLMTIME |
228                         OBD_MD_FLRDEV;
229         }
230         b->atime = LTIME_S(inode->i_atime);
231         b->mtime = LTIME_S(inode->i_mtime);
232         b->ctime = LTIME_S(inode->i_ctime);
233         b->mode = inode->i_mode;
234         b->size = inode->i_size;
235         b->blocks = inode->i_blocks;
236         b->uid = inode->i_uid;
237         b->gid = inode->i_gid;
238         b->flags = inode->i_flags;
239         b->rdev = inode->i_rdev;
240         
241         /* Return the correct link count for orphan inodes */
242         if (mds_inode_is_orphan(inode)) {
243                 b->nlink = 0;
244         } else if (S_ISDIR(inode->i_mode)) {
245                 b->nlink = 1;
246         } else {
247                 b->nlink = inode->i_nlink;
248         }
249
250         if (fid)
251                 b->valid |= OBD_MD_FID;
252         
253         mds_pack_inode2id(obd, &b->id1, inode, fid);
254 }
255
256 /* unpacking */
257 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
258                               struct mds_update_record *r)
259 {
260         struct iattr *attr = &r->ur_iattr;
261         struct mds_rec_setattr *rec;
262         ENTRY;
263
264         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
265                                  lustre_swab_mds_rec_setattr);
266         if (rec == NULL)
267                 RETURN (-EFAULT);
268
269         r->ur_id1 = &rec->sa_id;
270         attr->ia_valid = rec->sa_valid;
271         attr->ia_mode = rec->sa_mode;
272         attr->ia_uid = rec->sa_uid;
273         attr->ia_gid = rec->sa_gid;
274         attr->ia_size = rec->sa_size;
275         LTIME_S(attr->ia_atime) = rec->sa_atime;
276         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
277         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
278         attr->ia_attr_flags = rec->sa_attr_flags;
279
280         LASSERT_REQSWAB (req, offset + 1);
281         if (req->rq_reqmsg->bufcount > offset + 1) {
282                 r->ur_eadata = lustre_msg_buf (req->rq_reqmsg,
283                                                offset + 1, 0);
284                 if (r->ur_eadata == NULL)
285                         RETURN (-EFAULT);
286                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 1];
287         }
288
289         if (req->rq_reqmsg->bufcount > offset + 2) {
290                 r->ur_ea2data = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
291                 if (r->ur_ea2data == NULL)
292                         RETURN (-EFAULT);
293
294                 r->ur_ea2datalen = req->rq_reqmsg->buflens[offset + 2];
295         }
296
297         RETURN(0);
298 }
299
300 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
301                              struct mds_update_record *r)
302 {
303         struct mds_rec_create *rec;
304         ENTRY;
305
306         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
307                                   lustre_swab_mds_rec_create);
308         if (rec == NULL)
309                 RETURN (-EFAULT);
310
311         r->ur_id1 = &rec->cr_id;
312         r->ur_id2 = &rec->cr_replayid;
313         r->ur_mode = rec->cr_mode;
314         r->ur_rdev = rec->cr_rdev;
315         r->ur_time = rec->cr_time;
316         r->ur_flags = rec->cr_flags;
317
318         LASSERT_REQSWAB (req, offset + 1);
319         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
320         if (r->ur_name == NULL)
321                 RETURN (-EFAULT);
322         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
323
324         LASSERT_REQSWAB (req, offset + 2);
325         if (req->rq_reqmsg->bufcount > offset + 2) {
326                 if (S_ISLNK(r->ur_mode)) {
327                         r->ur_tgt = lustre_msg_string(req->rq_reqmsg,
328                                                       offset + 2, 0);
329                         if (r->ur_tgt == NULL)
330                                 RETURN (-EFAULT);
331                         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
332                 } else if (S_ISDIR(r->ur_mode)) {
333                         /* Stripe info for mkdir - just a 16bit integer */
334                         if (req->rq_reqmsg->buflens[offset + 2] != 2) {
335                                 CERROR("mkdir stripe info does not match "
336                                        "expected size %d vs 2\n",
337                                        req->rq_reqmsg->buflens[offset + 2]);
338                                 RETURN (-EINVAL);
339                         }
340                         r->ur_eadata = lustre_swab_buf (req->rq_reqmsg,
341                                                offset + 2, 2, __swab16s);
342                         r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
343                 } else {
344                         /* Hm, no other users so far? */
345                         LBUG();
346                 }
347         }
348         RETURN(0);
349 }
350
351 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
352                            struct mds_update_record *r)
353 {
354         struct mds_rec_link *rec;
355         ENTRY;
356
357         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
358                                   lustre_swab_mds_rec_link);
359         if (rec == NULL)
360                 RETURN (-EFAULT);
361
362         r->ur_id1 = &rec->lk_id1;
363         r->ur_id2 = &rec->lk_id2;
364         r->ur_time = rec->lk_time;
365
366         LASSERT_REQSWAB (req, offset + 1);
367         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
368         if (r->ur_name == NULL)
369                 RETURN (-EFAULT);
370         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
371         RETURN(0);
372 }
373
374 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
375                              struct mds_update_record *r)
376 {
377         struct mds_rec_unlink *rec;
378         ENTRY;
379
380         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
381                                   lustre_swab_mds_rec_unlink);
382         if (rec == NULL)
383                 RETURN(-EFAULT);
384
385         r->ur_mode = rec->ul_mode;
386         r->ur_id1 = &rec->ul_id1;
387         r->ur_id2 = &rec->ul_id2;
388         r->ur_time = rec->ul_time;
389
390         LASSERT_REQSWAB (req, offset + 1);
391         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
392         if (r->ur_name == NULL)
393                 RETURN(-EFAULT);
394         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
395         RETURN(0);
396 }
397
398 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
399                              struct mds_update_record *r)
400 {
401         struct mds_rec_rename *rec;
402         ENTRY;
403
404         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
405                                   lustre_swab_mds_rec_rename);
406         if (rec == NULL)
407                 RETURN(-EFAULT);
408
409         r->ur_id1 = &rec->rn_id1;
410         r->ur_id2 = &rec->rn_id2;
411         r->ur_time = rec->rn_time;
412
413         LASSERT_REQSWAB (req, offset + 1);
414         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
415         if (r->ur_name == NULL)
416                 RETURN(-EFAULT);
417         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
418
419         LASSERT_REQSWAB (req, offset + 2);
420         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
421         if (r->ur_tgt == NULL)
422                 RETURN(-EFAULT);
423         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
424         RETURN(0);
425 }
426
427 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
428                            struct mds_update_record *r)
429 {
430         struct mds_rec_create *rec;
431         ENTRY;
432
433         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
434                                   lustre_swab_mds_rec_create);
435         if (rec == NULL)
436                 RETURN(-EFAULT);
437
438         r->ur_id1 = &rec->cr_id;
439         r->ur_id2 = &rec->cr_replayid;
440         r->ur_mode = rec->cr_mode;
441         r->ur_rdev = rec->cr_rdev;
442         r->ur_time = rec->cr_time;
443         r->ur_flags = rec->cr_flags;
444
445         LASSERT_REQSWAB (req, offset + 1);
446         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
447         if (r->ur_name == NULL)
448                 RETURN (-EFAULT);
449         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
450
451         LASSERT_REQSWAB (req, offset + 2);
452         if (req->rq_reqmsg->bufcount > offset + 2) {
453                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
454                 if (r->ur_eadata == NULL)
455                         RETURN(-EFAULT);
456                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
457         }
458         RETURN(0);
459 }
460
461 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
462                                struct mds_update_record *r);
463
464 static update_unpacker mds_unpackers[REINT_MAX + 1] = {
465         [REINT_SETATTR] mds_setattr_unpack,
466         [REINT_CREATE] mds_create_unpack,
467         [REINT_LINK] mds_link_unpack,
468         [REINT_UNLINK] mds_unlink_unpack,
469         [REINT_RENAME] mds_rename_unpack,
470         [REINT_OPEN] mds_open_unpack,
471 };
472
473 int mds_update_unpack(struct ptlrpc_request *req, int offset,
474                       struct mds_update_record *rec)
475 {
476         __u32 *opcodep;
477         __u32  opcode;
478         int rc;
479         ENTRY;
480
481         /*
482          * NB don't lustre_swab_reqbuf() here. We're just taking a peek and we
483          * want to leave it to the specific unpacker once we've identified the
484          * message type.
485          */
486         opcodep = lustre_msg_buf (req->rq_reqmsg, offset, sizeof(*opcodep));
487         if (opcodep == NULL)
488                 RETURN(-EFAULT);
489
490         opcode = *opcodep;
491         if (lustre_msg_swabbed (req->rq_reqmsg))
492                 __swab32s (&opcode);
493
494         if (opcode > REINT_MAX ||
495             mds_unpackers[opcode] == NULL) {
496                 CERROR ("Unexpected opcode %d\n", opcode);
497                 RETURN(-EFAULT);
498         }
499
500         rec->ur_id1 = NULL;
501         rec->ur_id2 = NULL;
502         rec->ur_opcode = opcode;
503
504         rc = mds_unpackers[opcode](req, offset, rec);
505         
506 #if CRAY_PORTALS
507         rec->ur_fsuid = req->rq_uid;
508 #endif
509         RETURN(rc);
510 }
511
512 /********************************
513  * MDS uid/gid mapping handling *
514  ********************************/
515
516 static
517 struct mds_idmap_entry* idmap_alloc_entry(__u32 rmt_id, __u32 lcl_id)
518 {
519         struct mds_idmap_entry *e;
520
521         OBD_ALLOC(e, sizeof(*e));
522         if (!e)
523                 return NULL;
524
525         INIT_LIST_HEAD(&e->rmt_hash);
526         INIT_LIST_HEAD(&e->lcl_hash);
527         atomic_set(&e->refcount, 1);
528         e->rmt_id = rmt_id;
529         e->lcl_id = lcl_id;
530
531         return e;
532 }
533
534 void idmap_free_entry(struct mds_idmap_entry *e)
535 {
536         if (!list_empty(&e->rmt_hash))
537                 list_del(&e->rmt_hash);
538         if (!list_empty(&e->lcl_hash))
539                 list_del(&e->lcl_hash);
540         OBD_FREE(e, sizeof(*e));
541 }
542
543 static
544 int idmap_insert_entry(struct list_head *rmt_hash, struct list_head *lcl_hash,
545                        struct mds_idmap_entry *new, const char *warn_msg)
546 {
547         struct list_head *rmt_head = &rmt_hash[MDS_IDMAP_HASHFUNC(new->rmt_id)];
548         struct list_head *lcl_head = &lcl_hash[MDS_IDMAP_HASHFUNC(new->lcl_id)];
549         struct mds_idmap_entry *e;
550
551         list_for_each_entry(e, rmt_head, rmt_hash) {
552                 if (e->rmt_id == new->rmt_id &&
553                     e->lcl_id == new->lcl_id) {
554                         atomic_inc(&e->refcount);
555                         return 1;
556                 }
557                 if (e->rmt_id == new->rmt_id && warn_msg)
558                         CWARN("%s: rmt id %u already map to %u (new %u)\n",
559                               warn_msg, e->rmt_id, e->lcl_id, new->lcl_id);
560                 if (e->lcl_id == new->lcl_id && warn_msg)
561                         CWARN("%s: lcl id %u already be mapped from %u "
562                               "(new %u)\n", warn_msg,
563                               e->lcl_id, e->rmt_id, new->rmt_id);
564         }
565
566         list_add_tail(rmt_head, &new->rmt_hash);
567         list_add_tail(lcl_head, &new->lcl_hash);
568         return 0;
569 }
570
571 static
572 int idmap_remove_entry(struct list_head *rmt_hash, struct list_head *lcl_hash,
573                        __u32 rmt_id, __u32 lcl_id)
574 {
575         struct list_head *rmt_head = &rmt_hash[MDS_IDMAP_HASHFUNC(rmt_id)];
576         struct mds_idmap_entry *e;
577
578         list_for_each_entry(e, rmt_head, rmt_hash) {
579                 if (e->rmt_id == rmt_id && e->lcl_id == lcl_id) {
580                         if (atomic_dec_and_test(&e->refcount)) {
581                                 list_del(&e->rmt_hash);
582                                 list_del(&e->lcl_hash);
583                                 OBD_FREE(e, sizeof(*e));
584                                 return 0;
585                         } else
586                                 return 1;
587                 }
588         }
589         return -ENOENT;
590 }
591
592 int mds_idmap_add(struct mds_idmap_table *tbl,
593                   uid_t rmt_uid, uid_t lcl_uid,
594                   gid_t rmt_gid, gid_t lcl_gid)
595 {
596         struct mds_idmap_entry *ue, *ge;
597         ENTRY;
598
599         if (!tbl)
600                 RETURN(-EPERM);
601
602         ue = idmap_alloc_entry(rmt_uid, lcl_uid);
603         if (!ue)
604                 RETURN(-ENOMEM);
605         ge = idmap_alloc_entry(rmt_gid, lcl_gid);
606         if (!ge) {
607                 idmap_free_entry(ue);
608                 RETURN(-ENOMEM);
609         }
610
611         spin_lock(&tbl->mit_lock);
612
613         if (idmap_insert_entry(tbl->mit_idmaps[MDS_RMT_UIDMAP_IDX],
614                                tbl->mit_idmaps[MDS_LCL_UIDMAP_IDX],
615                                ue, "UID mapping")) {
616                 idmap_free_entry(ue);
617         }
618
619         if (idmap_insert_entry(tbl->mit_idmaps[MDS_RMT_GIDMAP_IDX],
620                                tbl->mit_idmaps[MDS_LCL_GIDMAP_IDX],
621                                ge, "GID mapping")) {
622                 idmap_free_entry(ge);
623         }
624
625         spin_unlock(&tbl->mit_lock);
626         RETURN(0);
627 }
628
629 int mds_idmap_del(struct mds_idmap_table *tbl,
630                   uid_t rmt_uid, uid_t lcl_uid,
631                   gid_t rmt_gid, gid_t lcl_gid)
632 {
633         ENTRY;
634
635         if (!tbl)
636                 RETURN(0);
637
638         spin_lock(&tbl->mit_lock);
639         idmap_remove_entry(tbl->mit_idmaps[MDS_RMT_UIDMAP_IDX],
640                            tbl->mit_idmaps[MDS_LCL_UIDMAP_IDX],
641                            rmt_uid, lcl_uid);
642         idmap_remove_entry(tbl->mit_idmaps[MDS_RMT_GIDMAP_IDX],
643                            tbl->mit_idmaps[MDS_LCL_GIDMAP_IDX],
644                            rmt_gid, lcl_gid);
645         spin_unlock(&tbl->mit_lock);
646         RETURN(0);
647 }
648
649 static
650 __u32 idmap_lookup_id(struct list_head *hash, int reverse, __u32 id)
651 {
652         struct list_head *head = &hash[MDS_IDMAP_HASHFUNC(id)];
653         struct mds_idmap_entry *e;
654
655         if (!reverse) {
656                 list_for_each_entry(e, head, rmt_hash) {
657                         if (e->rmt_id == id)
658                                 return e->lcl_id;
659                 }
660                 return MDS_IDMAP_NOTFOUND;
661         } else {
662                 list_for_each_entry(e, head, lcl_hash) {
663                         if (e->lcl_id == id)
664                                 return e->rmt_id;
665                 }
666                 return MDS_IDMAP_NOTFOUND;
667         }
668 }
669
670 int mds_idmap_lookup_uid(struct mds_idmap_table *tbl, int reverse, uid_t uid)
671 {
672         struct list_head *hash;
673
674         if (!tbl)
675                 return MDS_IDMAP_NOTFOUND;
676
677         if (!reverse)
678                 hash = tbl->mit_idmaps[MDS_RMT_UIDMAP_IDX];
679         else
680                 hash = tbl->mit_idmaps[MDS_LCL_UIDMAP_IDX];
681
682         spin_lock(&tbl->mit_lock);
683         uid = idmap_lookup_id(hash, reverse, uid);
684         spin_unlock(&tbl->mit_lock);
685
686         return uid;
687 }
688
689 int mds_idmap_lookup_gid(struct mds_idmap_table *tbl, int reverse, gid_t gid)
690 {
691         struct list_head *hash;
692
693         if (!tbl)
694                 return MDS_IDMAP_NOTFOUND;
695
696         if (!reverse)
697                 hash = tbl->mit_idmaps[MDS_RMT_GIDMAP_IDX];
698         else
699                 hash = tbl->mit_idmaps[MDS_LCL_GIDMAP_IDX];
700
701         spin_lock(&tbl->mit_lock);
702         gid = idmap_lookup_id(hash, reverse, gid);
703         spin_unlock(&tbl->mit_lock);
704
705         return gid;
706 }
707
708 struct mds_idmap_table *mds_idmap_alloc()
709 {
710         struct mds_idmap_table *tbl;
711         int i, j;
712
713         OBD_ALLOC(tbl, sizeof(*tbl));
714         if (!tbl)
715                 return NULL;
716
717         spin_lock_init(&tbl->mit_lock);
718         for (i = 0; i < MDS_IDMAP_N_HASHES; i++)
719                 for (j = 0; j < MDS_IDMAP_HASHSIZE; j++)
720                         INIT_LIST_HEAD(&tbl->mit_idmaps[i][j]);
721
722         return tbl;
723 }
724
725 static void idmap_clear_rmt_hash(struct list_head *list)
726 {
727         struct mds_idmap_entry *e;
728         int i;
729
730         for (i = 0; i < MDS_IDMAP_HASHSIZE; i++) {
731                 while (!list_empty(&list[i])) {
732                         e = list_entry(list[i].next, struct mds_idmap_entry,
733                                        rmt_hash);
734                         idmap_free_entry(e);
735                 }
736         }
737 }
738
739 void mds_idmap_free(struct mds_idmap_table *tbl)
740 {
741         int i;
742
743         spin_lock(&tbl->mit_lock);
744         idmap_clear_rmt_hash(tbl->mit_idmaps[MDS_RMT_UIDMAP_IDX]);
745         idmap_clear_rmt_hash(tbl->mit_idmaps[MDS_RMT_GIDMAP_IDX]);
746
747         /* paranoid checking */
748         for (i = 0; i < MDS_IDMAP_HASHSIZE; i++) {
749                 LASSERT(list_empty(&tbl->mit_idmaps[MDS_LCL_UIDMAP_IDX][i]));
750                 LASSERT(list_empty(&tbl->mit_idmaps[MDS_LCL_GIDMAP_IDX][i]));
751         }
752         spin_unlock(&tbl->mit_lock);
753
754         OBD_FREE(tbl, sizeof(*tbl));
755 }
756
757 /*********************************
758  * helpers doing mapping for MDS *
759  *********************************/
760
761 /*
762  * we allow remote setuid/setgid to an "authencated" one,
763  * this policy probably change later.
764  */
765 static
766 int mds_req_secdesc_do_map(struct mds_export_data *med,
767                            struct mds_req_sec_desc *rsd)
768 {
769         struct mds_idmap_table *idmap = med->med_idmap;
770         uid_t uid, fsuid;
771         gid_t gid, fsgid;
772
773         uid = mds_idmap_lookup_uid(idmap, 0, rsd->rsd_uid);
774         if (uid == MDS_IDMAP_NOTFOUND) {
775                 CERROR("can't find map for uid %u\n", rsd->rsd_uid);
776                 return -EPERM;
777         }
778
779         if (rsd->rsd_uid == rsd->rsd_fsuid)
780                 fsuid = uid;
781         else {
782                 fsuid = mds_idmap_lookup_uid(idmap, 0, rsd->rsd_fsuid);
783                 if (fsuid == MDS_IDMAP_NOTFOUND) {
784                         CERROR("can't find map for fsuid %u\n", rsd->rsd_fsuid);
785                         return -EPERM;
786                 }
787         }
788
789         gid = mds_idmap_lookup_gid(idmap, 0, rsd->rsd_gid);
790         if (gid == MDS_IDMAP_NOTFOUND) {
791                 CERROR("can't find map for gid %u\n", rsd->rsd_gid);
792                 return -EPERM;
793         }
794
795         if (rsd->rsd_gid == rsd->rsd_fsgid)
796                 fsgid = gid;
797         else {
798                 fsgid = mds_idmap_lookup_gid(idmap, 0, rsd->rsd_fsgid);
799                 if (fsgid == MDS_IDMAP_NOTFOUND) {
800                         CERROR("can't find map for fsgid %u\n", rsd->rsd_fsgid);
801                         return -EPERM;
802                 }
803         }
804
805         rsd->rsd_uid = uid;
806         rsd->rsd_gid = gid;
807         rsd->rsd_fsuid = fsuid;
808         rsd->rsd_fsgid = fsgid;
809
810         return 0;
811 }
812
813 void mds_body_do_reverse_map(struct mds_export_data *med,
814                              struct mds_body *body)
815 {
816         uid_t uid;
817         gid_t gid;
818
819         if (!med->med_remote)
820                 return;
821
822         ENTRY;
823         if (body->valid & OBD_MD_FLUID) {
824                 uid = mds_idmap_lookup_uid(med->med_idmap, 1, body->uid);
825                 if (uid == MDS_IDMAP_NOTFOUND) {
826                         uid = med->med_nllu;
827                         if (body->valid & OBD_MD_FLMODE) {
828                                 body->mode = (body->mode & ~S_IRWXU) |
829                                              ((body->mode & S_IRWXO) << 6);
830                         }
831                 }
832                 body->uid = uid;
833         }
834         if (body->valid & OBD_MD_FLGID) {
835                 gid = mds_idmap_lookup_gid(med->med_idmap, 1, body->gid);
836                 if (gid == MDS_IDMAP_NOTFOUND) {
837                         gid = med->med_nllg;
838                         if (body->valid & OBD_MD_FLMODE) {
839                                 body->mode = (body->mode & ~S_IRWXG) |
840                                              ((body->mode & S_IRWXO) << 3);
841                         }
842                 }
843                 body->gid = gid;
844         }
845
846         EXIT;
847 }
848
849 /**********************
850  * MDS ucred handling *
851  **********************/
852
853 static inline void drop_ucred_ginfo(struct lvfs_ucred *ucred)
854 {
855         if (ucred->luc_ginfo) {
856                 put_group_info(ucred->luc_ginfo);
857                 ucred->luc_ginfo = NULL;
858         }
859 }
860
861 static inline void drop_ucred_lsd(struct lvfs_ucred *ucred)
862 {
863         if (ucred->luc_lsd) {
864                 mds_put_lsd(ucred->luc_lsd);
865                 ucred->luc_lsd = NULL;
866         }
867 }
868
869 /*
870  * the heart of the uid/gid handling and security checking.
871  *
872  * root could set any group_info if we allowed setgroups, while
873  * normal user only could 'reduce' their group members -- which
874  * is somewhat expensive.
875  */
876 int mds_init_ucred(struct lvfs_ucred *ucred,
877                    struct ptlrpc_request *req,
878                    struct mds_req_sec_desc *rsd)
879 {
880         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
881         struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
882         struct lustre_sec_desc *lsd;
883         ptl_nid_t peernid = req->rq_peer.peer_id.nid;
884         struct group_info *gnew;
885         unsigned int setuid, setgid, strong_sec, root_squashed;
886         __u32 lsd_perms;
887         ENTRY;
888
889         LASSERT(ucred);
890         LASSERT(rsd);
891         LASSERT(rsd->rsd_ngroups <= LUSTRE_MAX_GROUPS);
892
893         /* XXX We'v no dedicated bits indicating whether GSS is used,
894          * and authenticated/mapped uid is valid. currently we suppose
895          * gss must initialize rq_sec_svcdata.
896          */
897         if (req->rq_sec_svcdata && req->rq_auth_uid == -1) {
898                 CWARN("user not authenticated, deny access\n");
899                 RETURN(-EPERM);
900         }
901
902         strong_sec = (req->rq_auth_uid != -1);
903         LASSERT(!(req->rq_remote_realm && !strong_sec));
904
905         /* if we use strong authentication for a local client, we
906          * expect the uid which client claimed is true.
907          */
908         if (!med->med_remote && strong_sec &&
909             req->rq_auth_uid != rsd->rsd_uid) {
910                 CWARN("nid "LPX64": UID %u was authenticated while client "
911                       "claimed %u, enforce to be %u\n",
912                       peernid, req->rq_auth_uid, rsd->rsd_uid,
913                       req->rq_auth_uid);
914                 if (rsd->rsd_uid != rsd->rsd_fsuid)
915                         rsd->rsd_uid = req->rq_auth_uid;
916                 else
917                         rsd->rsd_uid = rsd->rsd_fsuid = req->rq_auth_uid;
918         }
919
920         if (med->med_remote) {
921                 int rc;
922
923                 if (req->rq_mapped_uid == MDS_IDMAP_NOTFOUND) {
924                         CWARN("no mapping found, deny\n");
925                         RETURN(-EPERM);
926                 }
927
928                 rc = mds_req_secdesc_do_map(med, rsd);
929                 if (rc)
930                         RETURN(rc);
931         }
932
933         /* now lsd come into play */
934         ucred->luc_ginfo = NULL;
935         ucred->luc_lsd = lsd = mds_get_lsd(rsd->rsd_uid);
936
937         if (!lsd) {
938                 CERROR("Deny access without LSD: uid %d\n", rsd->rsd_uid);
939                 RETURN(-EPERM);
940         }
941
942         /* find out the setuid/setgid attempt */
943         setuid = (rsd->rsd_uid != rsd->rsd_fsuid);
944         setgid = (rsd->rsd_gid != rsd->rsd_fsgid ||
945                   rsd->rsd_gid != lsd->lsd_gid);
946
947         lsd_perms = mds_lsd_get_perms(lsd, med->med_remote, 0, peernid);
948
949         /* check permission of setuid */
950         if (setuid && !(lsd_perms & LSD_PERM_SETUID)) {
951                 CWARN("mds blocked setuid attempt: %u -> %u\n",
952                       rsd->rsd_uid, rsd->rsd_fsuid);
953                 RETURN(-EPERM);
954         }
955
956         /* check permission of setgid */
957         if (setgid && !(lsd_perms & LSD_PERM_SETGID)) {
958                 CWARN("mds blocked setgid attempt: %u -> %u\n",
959                       rsd->rsd_gid, rsd->rsd_fsgid);
960                 RETURN(-EPERM);
961         }
962
963         root_squashed = mds_squash_root(mds, rsd, &peernid); 
964
965         /* remove privilege for non-root user */
966         if (rsd->rsd_fsuid)
967                 rsd->rsd_cap &= ~CAP_FS_MASK;
968
969         /* by now every fields other than groups in rsd have been granted */
970         ucred->luc_uid = rsd->rsd_uid;
971         ucred->luc_gid = rsd->rsd_gid;
972         ucred->luc_fsuid = rsd->rsd_fsuid;
973         ucred->luc_fsgid = rsd->rsd_fsgid;
974         ucred->luc_cap = rsd->rsd_cap;
975
976         /* don't use any supplementary group for remote client or
977          * we squashed root */
978         if (med->med_remote || root_squashed)
979                 RETURN(0);
980
981         /* install groups from LSD */
982         if (lsd->lsd_ginfo) {
983                 ucred->luc_ginfo = lsd->lsd_ginfo;
984                 get_group_info(ucred->luc_ginfo);
985         }
986
987         /* everything is done if we don't allow setgroups */
988         if (!(lsd_perms & LSD_PERM_SETGRP))
989                 RETURN(0);
990
991         /* root could set any groups as he want (if allowed), normal
992          * users only could reduce his group array.
993          */
994         if (ucred->luc_uid == 0) {
995                 drop_ucred_ginfo(ucred);
996
997                 if (rsd->rsd_ngroups == 0)
998                         RETURN(0);
999
1000                 gnew = groups_alloc(rsd->rsd_ngroups);
1001                 if (!gnew) {
1002                         CERROR("out of memory\n");
1003                         drop_ucred_lsd(ucred);
1004                         RETURN(-ENOMEM);
1005                 }
1006                 groups_from_buffer(gnew, rsd->rsd_groups);
1007                 groups_sort(gnew); /* don't rely on client doing this */
1008
1009                 ucred->luc_ginfo = gnew;
1010         } else {
1011                 __u32 set = 0, cur = 0;
1012                 struct group_info *ginfo = ucred->luc_ginfo;
1013
1014                 if (!ginfo)
1015                         RETURN(0);
1016
1017                 /* Note: freeing a group_info count on 'nblocks' instead of
1018                  * 'ngroups', thus we can safely alloc enough buffer and reduce
1019                  * and ngroups number later.
1020                  */
1021                 gnew = groups_alloc(rsd->rsd_ngroups);
1022                 if (!gnew) {
1023                         CERROR("out of memory\n");
1024                         drop_ucred_ginfo(ucred);
1025                         drop_ucred_lsd(ucred);
1026                         RETURN(-ENOMEM);
1027                 }
1028
1029                 while (cur < rsd->rsd_ngroups) {
1030                         if (groups_search(ginfo, rsd->rsd_groups[cur])) {
1031                                 GROUP_AT(gnew, set) = rsd->rsd_groups[cur];
1032                                 set++;
1033                         }
1034                         cur++;
1035                 }
1036                 gnew->ngroups = set;
1037
1038                 put_group_info(ucred->luc_ginfo);
1039                 ucred->luc_ginfo = gnew;
1040         }
1041         RETURN(0);
1042 }
1043
1044 void mds_exit_ucred(struct lvfs_ucred *ucred)
1045 {
1046         ENTRY;
1047         drop_ucred_ginfo(ucred);
1048         drop_ucred_lsd(ucred);
1049         EXIT;
1050 }