Whamcloud - gitweb
b64ba2e7f1c00c4b71a7e8b01eb0a03281248df4
[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         if (read_fid)
184                 b->valid |= OBD_MD_FID;
185         
186         mds_pack_dentry2id(obd, &b->id1, dentry,
187                            read_fid);
188 }
189
190 int mds_pack_inode2id(struct obd_device *obd,
191                       struct lustre_id *id,
192                       struct inode *inode,
193                       int read_fid)
194 {
195         int rc = 0;
196         ENTRY;
197         
198         if (read_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         if (rc == 0) {
208                 id_ino(id) = inode->i_ino;
209                 id_gen(id) = inode->i_generation;
210                 id_type(id) = (S_IFMT & inode->i_mode);
211         }
212         RETURN(rc);
213 }
214
215 /* Note that we can copy all of the fields, just some will not be "valid" */
216 void mds_pack_inode2body(struct obd_device *obd, struct mds_body *b,
217                          struct inode *inode, int read_fid)
218 {
219         b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
220                 OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
221                 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
222                 OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
223
224         if (!S_ISREG(inode->i_mode)) {
225                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
226                         OBD_MD_FLATIME | OBD_MD_FLMTIME |
227                         OBD_MD_FLRDEV;
228         }
229         b->atime = LTIME_S(inode->i_atime);
230         b->mtime = LTIME_S(inode->i_mtime);
231         b->ctime = LTIME_S(inode->i_ctime);
232         b->mode = inode->i_mode;
233         b->size = inode->i_size;
234         b->blocks = inode->i_blocks;
235         b->uid = inode->i_uid;
236         b->gid = inode->i_gid;
237         b->flags = inode->i_flags;
238         b->rdev = inode->i_rdev;
239         
240         /* Return the correct link count for orphan inodes */
241         if (mds_inode_is_orphan(inode)) {
242                 b->nlink = 0;
243         } else if (S_ISDIR(inode->i_mode)) {
244                 b->nlink = 1;
245         } else {
246                 b->nlink = inode->i_nlink;
247         }
248         if (read_fid)
249                 b->valid |= OBD_MD_FID;
250         mds_pack_inode2id(obd, &b->id1, inode, read_fid);
251 }
252
253 /* unpacking */
254 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
255                               struct mds_update_record *r)
256 {
257         struct iattr *attr = &r->ur_iattr;
258         struct mds_rec_setattr *rec;
259         ENTRY;
260
261         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
262                                  lustre_swab_mds_rec_setattr);
263         if (rec == NULL)
264                 RETURN (-EFAULT);
265
266         r->ur_id1 = &rec->sa_id;
267         attr->ia_valid = rec->sa_valid;
268         attr->ia_mode = rec->sa_mode;
269         attr->ia_uid = rec->sa_uid;
270         attr->ia_gid = rec->sa_gid;
271         attr->ia_size = rec->sa_size;
272         LTIME_S(attr->ia_atime) = rec->sa_atime;
273         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
274         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
275         attr->ia_attr_flags = rec->sa_attr_flags;
276
277         LASSERT_REQSWAB (req, offset + 1);
278         if (req->rq_reqmsg->bufcount > offset + 1) {
279                 r->ur_eadata = lustre_msg_buf (req->rq_reqmsg,
280                                                offset + 1, 0);
281                 if (r->ur_eadata == NULL)
282                         RETURN (-EFAULT);
283                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 1];
284         }
285
286         if (req->rq_reqmsg->bufcount > offset + 2) {
287                 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
288                 if (r->ur_eadata == NULL)
289                         RETURN (-EFAULT);
290
291                 r->ur_cookielen = req->rq_reqmsg->buflens[offset + 2];
292         }
293
294         RETURN(0);
295 }
296
297 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
298                              struct mds_update_record *r)
299 {
300         struct mds_rec_create *rec;
301         ENTRY;
302
303         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
304                                   lustre_swab_mds_rec_create);
305         if (rec == NULL)
306                 RETURN (-EFAULT);
307
308         r->ur_id1 = &rec->cr_id;
309         r->ur_id2 = &rec->cr_replayid;
310         r->ur_mode = rec->cr_mode;
311         r->ur_rdev = rec->cr_rdev;
312         r->ur_time = rec->cr_time;
313         r->ur_flags = rec->cr_flags;
314
315         LASSERT_REQSWAB (req, offset + 1);
316         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
317         if (r->ur_name == NULL)
318                 RETURN (-EFAULT);
319         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
320
321         LASSERT_REQSWAB (req, offset + 2);
322         if (req->rq_reqmsg->bufcount > offset + 2) {
323                 if (S_ISLNK(r->ur_mode)) {
324                         r->ur_tgt = lustre_msg_string(req->rq_reqmsg,
325                                                       offset + 2, 0);
326                         if (r->ur_tgt == NULL)
327                                 RETURN (-EFAULT);
328                         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
329                 } else if (S_ISDIR(r->ur_mode)) {
330                         /* Stripe info for mkdir - just a 16bit integer */
331                         if (req->rq_reqmsg->buflens[offset + 2] != 2) {
332                                 CERROR("mkdir stripe info does not match "
333                                        "expected size %d vs 2\n",
334                                        req->rq_reqmsg->buflens[offset + 2]);
335                                 RETURN (-EINVAL);
336                         }
337                         r->ur_eadata = lustre_swab_buf (req->rq_reqmsg,
338                                                offset + 2, 2, __swab16s);
339                         r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
340                 } else {
341                         /* Hm, no other users so far? */
342                         LBUG();
343                 }
344         }
345         RETURN(0);
346 }
347
348 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
349                            struct mds_update_record *r)
350 {
351         struct mds_rec_link *rec;
352         ENTRY;
353
354         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
355                                   lustre_swab_mds_rec_link);
356         if (rec == NULL)
357                 RETURN (-EFAULT);
358
359         r->ur_id1 = &rec->lk_id1;
360         r->ur_id2 = &rec->lk_id2;
361         r->ur_time = rec->lk_time;
362
363         LASSERT_REQSWAB (req, offset + 1);
364         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
365         if (r->ur_name == NULL)
366                 RETURN (-EFAULT);
367         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
368         RETURN(0);
369 }
370
371 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
372                              struct mds_update_record *r)
373 {
374         struct mds_rec_unlink *rec;
375         ENTRY;
376
377         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
378                                   lustre_swab_mds_rec_unlink);
379         if (rec == NULL)
380                 RETURN(-EFAULT);
381
382         r->ur_mode = rec->ul_mode;
383         r->ur_id1 = &rec->ul_id1;
384         r->ur_id2 = &rec->ul_id2;
385         r->ur_time = rec->ul_time;
386
387         LASSERT_REQSWAB (req, offset + 1);
388         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
389         if (r->ur_name == NULL)
390                 RETURN(-EFAULT);
391         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
392         RETURN(0);
393 }
394
395 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
396                              struct mds_update_record *r)
397 {
398         struct mds_rec_rename *rec;
399         ENTRY;
400
401         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
402                                   lustre_swab_mds_rec_rename);
403         if (rec == NULL)
404                 RETURN(-EFAULT);
405
406         r->ur_id1 = &rec->rn_id1;
407         r->ur_id2 = &rec->rn_id2;
408         r->ur_time = rec->rn_time;
409
410         LASSERT_REQSWAB (req, offset + 1);
411         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
412         if (r->ur_name == NULL)
413                 RETURN(-EFAULT);
414         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
415
416         LASSERT_REQSWAB (req, offset + 2);
417         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
418         if (r->ur_tgt == NULL)
419                 RETURN(-EFAULT);
420         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
421         RETURN(0);
422 }
423
424 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
425                            struct mds_update_record *r)
426 {
427         struct mds_rec_create *rec;
428         ENTRY;
429
430         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
431                                   lustre_swab_mds_rec_create);
432         if (rec == NULL)
433                 RETURN (-EFAULT);
434
435         r->ur_id1 = &rec->cr_id;
436         r->ur_id2 = &rec->cr_replayid;
437         r->ur_mode = rec->cr_mode;
438         r->ur_rdev = rec->cr_rdev;
439         r->ur_time = rec->cr_time;
440         r->ur_flags = rec->cr_flags;
441
442         LASSERT_REQSWAB (req, offset + 1);
443         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
444         if (r->ur_name == NULL)
445                 RETURN (-EFAULT);
446         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
447
448         LASSERT_REQSWAB (req, offset + 2);
449         if (req->rq_reqmsg->bufcount > offset + 2) {
450                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
451                 if (r->ur_eadata == NULL)
452                         RETURN (-EFAULT);
453                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
454         }
455         RETURN(0);
456 }
457
458 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
459                                struct mds_update_record *r);
460
461 static update_unpacker mds_unpackers[REINT_MAX + 1] = {
462         [REINT_SETATTR] mds_setattr_unpack,
463         [REINT_CREATE] mds_create_unpack,
464         [REINT_LINK] mds_link_unpack,
465         [REINT_UNLINK] mds_unlink_unpack,
466         [REINT_RENAME] mds_rename_unpack,
467         [REINT_OPEN] mds_open_unpack,
468 };
469
470 int mds_update_unpack(struct ptlrpc_request *req, int offset,
471                       struct mds_update_record *rec)
472 {
473         __u32 *opcodep;
474         __u32  opcode;
475         int rc;
476         ENTRY;
477
478         /*
479          * NB don't lustre_swab_reqbuf() here. We're just taking a peek and we
480          * want to leave it to the specific unpacker once we've identified the
481          * message type.
482          */
483         opcodep = lustre_msg_buf (req->rq_reqmsg, offset, sizeof(*opcodep));
484         if (opcodep == NULL)
485                 RETURN(-EFAULT);
486
487         opcode = *opcodep;
488         if (lustre_msg_swabbed (req->rq_reqmsg))
489                 __swab32s (&opcode);
490
491         if (opcode > REINT_MAX ||
492             mds_unpackers[opcode] == NULL) {
493                 CERROR ("Unexpected opcode %d\n", opcode);
494                 RETURN(-EFAULT);
495         }
496
497         rec->ur_id1 = NULL;
498         rec->ur_id2 = NULL;
499         rec->ur_opcode = opcode;
500
501         rc = mds_unpackers[opcode](req, offset, rec);
502         RETURN(rc);
503 }
504
505 static inline void drop_ucred_ginfo(struct lvfs_ucred *ucred)
506 {
507         if (ucred->luc_ginfo) {
508                 put_group_info(ucred->luc_ginfo);
509                 ucred->luc_ginfo = NULL;
510         }
511 }
512
513 /*
514  * root could set any group_info if we allowed setgroups, while
515  * normal user only could 'reduce' their group members -- which
516  * is somewhat expensive.
517  */
518 int mds_init_ucred(struct lvfs_ucred *ucred, struct mds_req_sec_desc *rsd)
519 {
520         struct group_info *gnew;
521
522         ENTRY;
523         LASSERT(ucred);
524         LASSERT(rsd);
525
526         ucred->luc_fsuid = rsd->rsd_fsuid;
527         ucred->luc_fsgid = rsd->rsd_fsgid;
528         ucred->luc_cap = rsd->rsd_cap;
529         ucred->luc_uid = rsd->rsd_uid;
530         ucred->luc_ghash = mds_get_group_entry(NULL, rsd->rsd_uid);
531         ucred->luc_ginfo = NULL;
532
533         if (ucred->luc_ghash && ucred->luc_ghash->ge_group_info) {
534                 ucred->luc_ginfo = ucred->luc_ghash->ge_group_info;
535                 get_group_info(ucred->luc_ginfo);
536         }
537
538         /* everything is done if we don't allow set groups */
539         if (!mds_allow_setgroups())
540                 RETURN(0);
541
542         if (rsd->rsd_ngroups > LUSTRE_MAX_GROUPS) {
543                 CERROR("client provide too many groups: %d\n",
544                 rsd->rsd_ngroups);
545                 drop_ucred_ginfo(ucred);
546                 mds_put_group_entry(NULL, ucred->luc_ghash);
547                 RETURN(-EFAULT);
548         }
549
550         if (ucred->luc_uid == 0) {
551                 if (rsd->rsd_ngroups == 0) {
552                         drop_ucred_ginfo(ucred);
553                         RETURN(0);
554                 }
555
556                 gnew = groups_alloc(rsd->rsd_ngroups);
557                 if (!gnew) {
558                         CERROR("out of memory\n");
559                         drop_ucred_ginfo(ucred);
560                         mds_put_group_entry(NULL, ucred->luc_ghash);
561                         RETURN(-ENOMEM);
562                 }
563                 groups_from_buffer(gnew, rsd->rsd_groups);
564                 /* can't rely on client to sort them */
565                 groups_sort(gnew);
566
567                 drop_ucred_ginfo(ucred);
568                 ucred->luc_ginfo = gnew;
569         } else {
570                 __u32 set = 0, cur = 0;
571                 struct group_info *ginfo;
572
573                 /* if no group info in hash, we don't
574                  * bother createing new
575                  */
576                 if (!ucred->luc_ginfo)
577                         RETURN(0);
578
579                 /* Note: freeing a group_info count on 'nblocks' instead of
580                  * 'ngroups', thus we can safely alloc enough buffer and reduce
581                  * and ngroups number later.
582                  */
583                 gnew = groups_alloc(rsd->rsd_ngroups);
584                 if (!gnew) {
585                         CERROR("out of memory\n");
586                         drop_ucred_ginfo(ucred);
587                         mds_put_group_entry(NULL, ucred->luc_ghash);
588                         RETURN(-ENOMEM);
589                 }
590
591                 ginfo = ucred->luc_ginfo;
592                 while (cur < rsd->rsd_ngroups) {
593                         if (groups_search(ginfo, rsd->rsd_groups[cur])) {
594                                 GROUP_AT(gnew, set) = rsd->rsd_groups[cur];
595                                 set++;
596                         }
597                         cur++;
598                 }
599                 gnew->ngroups = set;
600
601                 put_group_info(ucred->luc_ginfo);
602                 ucred->luc_ginfo = gnew;
603         }
604         RETURN(0);
605 }
606
607 void mds_exit_ucred(struct lvfs_ucred *ucred)
608 {
609         ENTRY;
610
611         if (ucred->luc_ginfo)
612                 put_group_info(ucred->luc_ginfo);
613         if (ucred->luc_ghash)
614                 mds_put_group_entry(NULL, ucred->luc_ghash);
615
616         EXIT;
617 }