Whamcloud - gitweb
- changes with names on exports and another fields in llite and mds. lov_exp is
[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 "mds_internal.h"
51
52 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
53 struct group_info *groups_alloc(int ngroups)
54 {
55         struct group_info *ginfo;
56
57         LASSERT(ngroups <= NGROUPS_SMALL);
58
59         OBD_ALLOC(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
60         if (!ginfo)
61                 return NULL;
62         ginfo->ngroups = ngroups;
63         ginfo->nblocks = 1;
64         ginfo->blocks[0] = ginfo->small_block;
65         atomic_set(&ginfo->usage, 1);
66
67         return ginfo;
68 }
69
70 void groups_free(struct group_info *ginfo)
71 {
72         LASSERT(ginfo->ngroups <= NGROUPS_SMALL);
73         LASSERT(ginfo->nblocks == 1);
74         LASSERT(ginfo->blocks[0] == ginfo->small_block);
75
76         OBD_FREE(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
77 }
78
79 /* for 2.4 the group number is small, so simply search the
80  * whole array.
81  */
82 int groups_search(struct group_info *ginfo, gid_t grp)
83 {
84         int i;
85
86         if (!ginfo)
87                 return 0;
88
89         for (i = 0; i < ginfo->ngroups; i++)
90                 if (GROUP_AT(ginfo, i) == grp)
91                         return 1;
92         return 0;
93 }
94
95 #else /* >= 2.6.4 */
96
97 void groups_sort(struct group_info *ginfo)
98 {
99         int base, max, stride;
100         int gidsetsize = ginfo->ngroups;
101
102         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
103                 ; /* nothing */
104         stride /= 3;
105
106         while (stride) {
107                 max = gidsetsize - stride;
108                 for (base = 0; base < max; base++) {
109                         int left = base;
110                         int right = left + stride;
111                         gid_t tmp = GROUP_AT(ginfo, right);
112                                                                                                     
113                         while (left >= 0 && GROUP_AT(ginfo, left) > tmp) {
114                                 GROUP_AT(ginfo, right) =
115                                     GROUP_AT(ginfo, left);
116                                 right = left;
117                                 left -= stride;
118                         }
119                         GROUP_AT(ginfo, right) = tmp;
120                 }
121                 stride /= 3;
122         }
123 }
124
125 int groups_search(struct group_info *ginfo, gid_t grp)
126 {
127         int left, right;
128
129         if (!ginfo)
130                 return 0;
131
132         left = 0;
133         right = ginfo->ngroups;
134         while (left < right) {
135                 int mid = (left + right) / 2;
136                 int cmp = grp - GROUP_AT(ginfo, mid);
137                 if (cmp > 0)
138                         left = mid + 1;
139                 else if (cmp < 0)
140                         right = mid;
141                 else
142                         return 1;
143         }
144         return 0;
145 }
146 #endif
147
148 void groups_from_buffer(struct group_info *ginfo, __u32 *gids)
149 {
150         int i, ngroups = ginfo->ngroups;
151
152         for (i = 0; i < ginfo->nblocks; i++) {
153                 int count = min(NGROUPS_PER_BLOCK, ngroups);
154
155                 memcpy(ginfo->blocks[i], gids, count * sizeof(__u32));
156                 gids += NGROUPS_PER_BLOCK;
157                 ngroups -= count;
158         }
159 }
160
161 void mds_pack_dentry2id(struct obd_device *obd,
162                         struct lustre_id *id,
163                         struct dentry *dentry,
164                         int read_fid)
165 {
166         id_ino(id) = dentry->d_inum;
167         id_gen(id) = dentry->d_generation;
168
169         if (read_fid) {
170                 id_fid(id) = dentry->d_fid;
171                 id_group(id) = dentry->d_mdsnum;
172         }
173 }
174
175 void mds_pack_dentry2body(struct obd_device *obd,
176                           struct mds_body *b,
177                           struct dentry *dentry,
178                           int read_fid)
179 {
180         b->valid |= OBD_MD_FLID | OBD_MD_FLGENER |
181                 OBD_MD_MDS;
182
183         mds_pack_dentry2id(obd, &b->id1, dentry,
184                            read_fid);
185 }
186
187 int mds_pack_inode2id(struct obd_device *obd,
188                       struct lustre_id *id,
189                       struct inode *inode,
190                       int read_fid)
191 {
192         int rc = 0;
193         ENTRY;
194         
195         if (read_fid) {
196                 /* we have to avoid deadlock. */
197                 if (!down_trylock(&inode->i_sem)) {
198                         rc = mds_read_inode_sid(obd, inode, id);
199                         up(&inode->i_sem);
200                 } else {
201                         rc = mds_read_inode_sid(obd, inode, id);
202                 }
203         }
204         if (rc == 0) {
205                 id_ino(id) = inode->i_ino;
206                 id_gen(id) = inode->i_generation;
207                 id_type(id) = (S_IFMT & inode->i_mode);
208         }
209         RETURN(rc);
210 }
211
212 /* Note that we can copy all of the fields, just some will not be "valid" */
213 void mds_pack_inode2body(struct obd_device *obd, struct mds_body *b,
214                          struct inode *inode, int read_fid)
215 {
216         b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
217                 OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
218                 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
219                 OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
220
221         if (!S_ISREG(inode->i_mode)) {
222                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
223                         OBD_MD_FLATIME | OBD_MD_FLMTIME |
224                         OBD_MD_FLRDEV;
225         }
226         b->atime = LTIME_S(inode->i_atime);
227         b->mtime = LTIME_S(inode->i_mtime);
228         b->ctime = LTIME_S(inode->i_ctime);
229         b->mode = inode->i_mode;
230         b->size = inode->i_size;
231         b->blocks = inode->i_blocks;
232         b->uid = inode->i_uid;
233         b->gid = inode->i_gid;
234         b->flags = inode->i_flags;
235         b->rdev = inode->i_rdev;
236         
237         /* Return the correct link count for orphan inodes */
238         if (mds_inode_is_orphan(inode)) {
239                 b->nlink = 0;
240         } else if (S_ISDIR(inode->i_mode)) {
241                 b->nlink = 1;
242         } else {
243                 b->nlink = inode->i_nlink;
244         }
245         mds_pack_inode2id(obd, &b->id1, inode, read_fid);
246 }
247
248 /* unpacking */
249 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
250                               struct mds_update_record *r)
251 {
252         struct iattr *attr = &r->ur_iattr;
253         struct mds_rec_setattr *rec;
254         ENTRY;
255
256         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
257                                  lustre_swab_mds_rec_setattr);
258         if (rec == NULL)
259                 RETURN (-EFAULT);
260
261         r->ur_id1 = &rec->sa_id;
262         attr->ia_valid = rec->sa_valid;
263         attr->ia_mode = rec->sa_mode;
264         attr->ia_uid = rec->sa_uid;
265         attr->ia_gid = rec->sa_gid;
266         attr->ia_size = rec->sa_size;
267         LTIME_S(attr->ia_atime) = rec->sa_atime;
268         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
269         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
270         attr->ia_attr_flags = rec->sa_attr_flags;
271
272         LASSERT_REQSWAB (req, offset + 1);
273         if (req->rq_reqmsg->bufcount > offset + 1) {
274                 r->ur_eadata = lustre_msg_buf (req->rq_reqmsg,
275                                                offset + 1, 0);
276                 if (r->ur_eadata == NULL)
277                         RETURN (-EFAULT);
278                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 1];
279         }
280
281         if (req->rq_reqmsg->bufcount > offset + 2) {
282                 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
283                 if (r->ur_eadata == NULL)
284                         RETURN (-EFAULT);
285
286                 r->ur_cookielen = req->rq_reqmsg->buflens[offset + 2];
287         }
288
289         RETURN(0);
290 }
291
292 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
293                              struct mds_update_record *r)
294 {
295         struct mds_rec_create *rec;
296         ENTRY;
297
298         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
299                                   lustre_swab_mds_rec_create);
300         if (rec == NULL)
301                 RETURN (-EFAULT);
302
303         r->ur_id1 = &rec->cr_id;
304         r->ur_id2 = &rec->cr_replayid;
305         r->ur_mode = rec->cr_mode;
306         r->ur_rdev = rec->cr_rdev;
307         r->ur_time = rec->cr_time;
308         r->ur_flags = rec->cr_flags;
309
310         LASSERT_REQSWAB (req, offset + 1);
311         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
312         if (r->ur_name == NULL)
313                 RETURN (-EFAULT);
314         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
315
316         LASSERT_REQSWAB (req, offset + 2);
317         if (req->rq_reqmsg->bufcount > offset + 2) {
318                 if (S_ISLNK(r->ur_mode)) {
319                         r->ur_tgt = lustre_msg_string(req->rq_reqmsg,
320                                                       offset + 2, 0);
321                         if (r->ur_tgt == NULL)
322                                 RETURN (-EFAULT);
323                         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
324                 } else if (S_ISDIR(r->ur_mode)) {
325                         /* Stripe info for mkdir - just a 16bit integer */
326                         if (req->rq_reqmsg->buflens[offset + 2] != 2) {
327                                 CERROR("mkdir stripe info does not match "
328                                        "expected size %d vs 2\n",
329                                        req->rq_reqmsg->buflens[offset + 2]);
330                                 RETURN (-EINVAL);
331                         }
332                         r->ur_eadata = lustre_swab_buf (req->rq_reqmsg,
333                                                offset + 2, 2, __swab16s);
334                         r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
335                 } else {
336                         /* Hm, no other users so far? */
337                         LBUG();
338                 }
339         }
340         RETURN(0);
341 }
342
343 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
344                            struct mds_update_record *r)
345 {
346         struct mds_rec_link *rec;
347         ENTRY;
348
349         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
350                                   lustre_swab_mds_rec_link);
351         if (rec == NULL)
352                 RETURN (-EFAULT);
353
354         r->ur_id1 = &rec->lk_id1;
355         r->ur_id2 = &rec->lk_id2;
356         r->ur_time = rec->lk_time;
357
358         LASSERT_REQSWAB (req, offset + 1);
359         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
360         if (r->ur_name == NULL)
361                 RETURN (-EFAULT);
362         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
363         RETURN(0);
364 }
365
366 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
367                              struct mds_update_record *r)
368 {
369         struct mds_rec_unlink *rec;
370         ENTRY;
371
372         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
373                                   lustre_swab_mds_rec_unlink);
374         if (rec == NULL)
375                 RETURN(-EFAULT);
376
377         r->ur_mode = rec->ul_mode;
378         r->ur_id1 = &rec->ul_id1;
379         r->ur_id2 = &rec->ul_id2;
380         r->ur_time = rec->ul_time;
381
382         LASSERT_REQSWAB (req, offset + 1);
383         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
384         if (r->ur_name == NULL)
385                 RETURN(-EFAULT);
386         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
387         RETURN(0);
388 }
389
390 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
391                              struct mds_update_record *r)
392 {
393         struct mds_rec_rename *rec;
394         ENTRY;
395
396         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
397                                   lustre_swab_mds_rec_rename);
398         if (rec == NULL)
399                 RETURN(-EFAULT);
400
401         r->ur_id1 = &rec->rn_id1;
402         r->ur_id2 = &rec->rn_id2;
403         r->ur_time = rec->rn_time;
404
405         LASSERT_REQSWAB (req, offset + 1);
406         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
407         if (r->ur_name == NULL)
408                 RETURN(-EFAULT);
409         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
410
411         LASSERT_REQSWAB (req, offset + 2);
412         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
413         if (r->ur_tgt == NULL)
414                 RETURN(-EFAULT);
415         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
416         RETURN(0);
417 }
418
419 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
420                            struct mds_update_record *r)
421 {
422         struct mds_rec_create *rec;
423         ENTRY;
424
425         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
426                                   lustre_swab_mds_rec_create);
427         if (rec == NULL)
428                 RETURN (-EFAULT);
429
430         r->ur_id1 = &rec->cr_id;
431         r->ur_id2 = &rec->cr_replayid;
432         r->ur_mode = rec->cr_mode;
433         r->ur_rdev = rec->cr_rdev;
434         r->ur_time = rec->cr_time;
435         r->ur_flags = rec->cr_flags;
436
437         LASSERT_REQSWAB (req, offset + 1);
438         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
439         if (r->ur_name == NULL)
440                 RETURN (-EFAULT);
441         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
442
443         LASSERT_REQSWAB (req, offset + 2);
444         if (req->rq_reqmsg->bufcount > offset + 2) {
445                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
446                 if (r->ur_eadata == NULL)
447                         RETURN (-EFAULT);
448                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
449         }
450         RETURN(0);
451 }
452
453 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
454                                struct mds_update_record *r);
455
456 static update_unpacker mds_unpackers[REINT_MAX + 1] = {
457         [REINT_SETATTR] mds_setattr_unpack,
458         [REINT_CREATE] mds_create_unpack,
459         [REINT_LINK] mds_link_unpack,
460         [REINT_UNLINK] mds_unlink_unpack,
461         [REINT_RENAME] mds_rename_unpack,
462         [REINT_OPEN] mds_open_unpack,
463 };
464
465 int mds_update_unpack(struct ptlrpc_request *req, int offset,
466                       struct mds_update_record *rec)
467 {
468         __u32 *opcodep;
469         __u32  opcode;
470         int rc;
471         ENTRY;
472
473         /*
474          * NB don't lustre_swab_reqbuf() here. We're just taking a peek and we
475          * want to leave it to the specific unpacker once we've identified the
476          * message type.
477          */
478         opcodep = lustre_msg_buf (req->rq_reqmsg, offset, sizeof(*opcodep));
479         if (opcodep == NULL)
480                 RETURN(-EFAULT);
481
482         opcode = *opcodep;
483         if (lustre_msg_swabbed (req->rq_reqmsg))
484                 __swab32s (&opcode);
485
486         if (opcode > REINT_MAX ||
487             mds_unpackers[opcode] == NULL) {
488                 CERROR ("Unexpected opcode %d\n", opcode);
489                 RETURN(-EFAULT);
490         }
491
492         rec->ur_id1 = NULL;
493         rec->ur_id2 = NULL;
494         rec->ur_opcode = opcode;
495
496         rc = mds_unpackers[opcode](req, offset, rec);
497         RETURN(rc);
498 }
499
500 static inline void drop_ucred_ginfo(struct lvfs_ucred *ucred)
501 {
502         if (ucred->luc_ginfo) {
503                 put_group_info(ucred->luc_ginfo);
504                 ucred->luc_ginfo = NULL;
505         }
506 }
507
508 /*
509  * root could set any group_info if we allowed setgroups, while
510  * normal user only could 'reduce' their group members -- which
511  * is somewhat expensive.
512  */
513 int mds_init_ucred(struct lvfs_ucred *ucred, struct mds_req_sec_desc *rsd)
514 {
515         struct group_info *gnew;
516
517         ENTRY;
518         LASSERT(ucred);
519         LASSERT(rsd);
520
521         ucred->luc_fsuid = rsd->rsd_fsuid;
522         ucred->luc_fsgid = rsd->rsd_fsgid;
523         ucred->luc_cap = rsd->rsd_cap;
524         ucred->luc_uid = rsd->rsd_uid;
525         ucred->luc_ghash = mds_get_group_entry(NULL, rsd->rsd_uid);
526         ucred->luc_ginfo = NULL;
527
528         if (ucred->luc_ghash && ucred->luc_ghash->ge_group_info) {
529                 ucred->luc_ginfo = ucred->luc_ghash->ge_group_info;
530                 get_group_info(ucred->luc_ginfo);
531         }
532
533         /* everything is done if we don't allow set groups */
534         if (!mds_allow_setgroups())
535                 RETURN(0);
536
537         if (rsd->rsd_ngroups > LUSTRE_MAX_GROUPS) {
538                 CERROR("client provide too many groups: %d\n",
539                 rsd->rsd_ngroups);
540                 drop_ucred_ginfo(ucred);
541                 mds_put_group_entry(NULL, ucred->luc_ghash);
542                 RETURN(-EFAULT);
543         }
544
545         if (ucred->luc_uid == 0) {
546                 if (rsd->rsd_ngroups == 0) {
547                         drop_ucred_ginfo(ucred);
548                         RETURN(0);
549                 }
550
551                 gnew = groups_alloc(rsd->rsd_ngroups);
552                 if (!gnew) {
553                         CERROR("out of memory\n");
554                         drop_ucred_ginfo(ucred);
555                         mds_put_group_entry(NULL, ucred->luc_ghash);
556                         RETURN(-ENOMEM);
557                 }
558                 groups_from_buffer(gnew, rsd->rsd_groups);
559                 /* can't rely on client to sort them */
560                 groups_sort(gnew);
561
562                 drop_ucred_ginfo(ucred);
563                 ucred->luc_ginfo = gnew;
564         } else {
565                 __u32 set = 0, cur = 0;
566                 struct group_info *ginfo;
567
568                 /* if no group info in hash, we don't
569                  * bother createing new
570                  */
571                 if (!ucred->luc_ginfo)
572                         RETURN(0);
573
574                 /* Note: freeing a group_info count on 'nblocks' instead of
575                  * 'ngroups', thus we can safely alloc enough buffer and reduce
576                  * and ngroups number later.
577                  */
578                 gnew = groups_alloc(rsd->rsd_ngroups);
579                 if (!gnew) {
580                         CERROR("out of memory\n");
581                         drop_ucred_ginfo(ucred);
582                         mds_put_group_entry(NULL, ucred->luc_ghash);
583                         RETURN(-ENOMEM);
584                 }
585
586                 ginfo = ucred->luc_ginfo;
587                 while (cur < rsd->rsd_ngroups) {
588                         if (groups_search(ginfo, rsd->rsd_groups[cur])) {
589                                 GROUP_AT(gnew, set) = rsd->rsd_groups[cur];
590                                 set++;
591                         }
592                         cur++;
593                 }
594                 gnew->ngroups = set;
595
596                 put_group_info(ucred->luc_ginfo);
597                 ucred->luc_ginfo = gnew;
598         }
599         RETURN(0);
600 }
601
602 void mds_exit_ucred(struct lvfs_ucred *ucred)
603 {
604         ENTRY;
605
606         if (ucred->luc_ginfo)
607                 put_group_info(ucred->luc_ginfo);
608         if (ucred->luc_ghash)
609                 mds_put_group_entry(NULL, ucred->luc_ghash);
610
611         EXIT;
612 }