1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
22 #define DEBUG_SUBSYSTEM S_MDS
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/kernel.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
35 # include <linux/buffer_head.h> // for wait_on_buffer
37 #include <linux/unistd.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
43 #include <linux/stat.h>
44 #include <asm/uaccess.h>
45 #include <linux/slab.h>
46 #include <asm/segment.h>
48 #include <linux/obd_support.h>
49 #include <linux/lustre_lib.h>
50 #include "mds_internal.h"
52 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
53 struct group_info *groups_alloc(int ngroups)
55 struct group_info *ginfo;
57 LASSERT(ngroups <= NGROUPS_SMALL);
59 OBD_ALLOC(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
62 ginfo->ngroups = ngroups;
64 ginfo->blocks[0] = ginfo->small_block;
65 atomic_set(&ginfo->usage, 1);
70 void groups_free(struct group_info *ginfo)
72 LASSERT(ginfo->ngroups <= NGROUPS_SMALL);
73 LASSERT(ginfo->nblocks == 1);
74 LASSERT(ginfo->blocks[0] == ginfo->small_block);
76 OBD_FREE(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
79 /* for 2.4 the group number is small, so simply search the
82 int groups_search(struct group_info *ginfo, gid_t grp)
89 for (i = 0; i < ginfo->ngroups; i++)
90 if (GROUP_AT(ginfo, i) == grp)
97 void groups_sort(struct group_info *ginfo)
99 int base, max, stride;
100 int gidsetsize = ginfo->ngroups;
102 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
107 max = gidsetsize - stride;
108 for (base = 0; base < max; base++) {
110 int right = left + stride;
111 gid_t tmp = GROUP_AT(ginfo, right);
113 while (left >= 0 && GROUP_AT(ginfo, left) > tmp) {
114 GROUP_AT(ginfo, right) =
115 GROUP_AT(ginfo, left);
119 GROUP_AT(ginfo, right) = tmp;
125 int groups_search(struct group_info *ginfo, gid_t grp)
133 right = ginfo->ngroups;
134 while (left < right) {
135 int mid = (left + right) / 2;
136 int cmp = grp - GROUP_AT(ginfo, mid);
148 void groups_from_buffer(struct group_info *ginfo, __u32 *gids)
150 int i, ngroups = ginfo->ngroups;
152 for (i = 0; i < ginfo->nblocks; i++) {
153 int count = min(NGROUPS_PER_BLOCK, ngroups);
155 memcpy(ginfo->blocks[i], gids, count * sizeof(__u32));
156 gids += NGROUPS_PER_BLOCK;
161 void mds_pack_dentry2id(struct obd_device *obd,
162 struct lustre_id *id,
163 struct dentry *dentry)
165 id_ino(id) = dentry->d_inum;
166 id_gen(id) = dentry->d_generation;
167 id_fid(id) = dentry->d_fid;
168 id_group(id) = dentry->d_mdsnum;
171 void mds_pack_dentry2body(struct obd_device *obd,
173 struct dentry *dentry)
175 b->valid |= OBD_MD_FLID | OBD_MD_FLGENER |
178 mds_pack_dentry2id(obd, &b->id1, dentry);
181 int mds_pack_inode2id(struct obd_device *obd,
182 struct lustre_id *id,
188 /* we have to avoid deadlock. */
189 if (!down_trylock(&inode->i_sem)) {
190 rc = mds_read_inode_sid(obd, inode, id);
193 rc = mds_read_inode_sid(obd, inode, id);
197 id_ino(id) = inode->i_ino;
198 id_gen(id) = inode->i_generation;
199 id_type(id) = (S_IFMT & inode->i_mode);
204 /* Note that we can copy all of the fields, just some will not be "valid" */
205 void mds_pack_inode2body(struct obd_device *obd, struct mds_body *b,
208 b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
209 OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
210 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
211 OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
213 if (!S_ISREG(inode->i_mode)) {
214 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
215 OBD_MD_FLATIME | OBD_MD_FLMTIME |
218 b->atime = LTIME_S(inode->i_atime);
219 b->mtime = LTIME_S(inode->i_mtime);
220 b->ctime = LTIME_S(inode->i_ctime);
221 b->mode = inode->i_mode;
222 b->size = inode->i_size;
223 b->blocks = inode->i_blocks;
224 b->uid = inode->i_uid;
225 b->gid = inode->i_gid;
226 b->flags = inode->i_flags;
227 b->rdev = inode->i_rdev;
229 /* Return the correct link count for orphan inodes */
230 if (mds_inode_is_orphan(inode)) {
232 } else if (S_ISDIR(inode->i_mode)) {
235 b->nlink = inode->i_nlink;
237 mds_pack_inode2id(obd, &b->id1, inode);
241 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
242 struct mds_update_record *r)
244 struct iattr *attr = &r->ur_iattr;
245 struct mds_rec_setattr *rec;
248 rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
249 lustre_swab_mds_rec_setattr);
253 r->ur_id1 = &rec->sa_id;
254 attr->ia_valid = rec->sa_valid;
255 attr->ia_mode = rec->sa_mode;
256 attr->ia_uid = rec->sa_uid;
257 attr->ia_gid = rec->sa_gid;
258 attr->ia_size = rec->sa_size;
259 LTIME_S(attr->ia_atime) = rec->sa_atime;
260 LTIME_S(attr->ia_mtime) = rec->sa_mtime;
261 LTIME_S(attr->ia_ctime) = rec->sa_ctime;
262 attr->ia_attr_flags = rec->sa_attr_flags;
264 LASSERT_REQSWAB (req, offset + 1);
265 if (req->rq_reqmsg->bufcount > offset + 1) {
266 r->ur_eadata = lustre_msg_buf (req->rq_reqmsg,
268 if (r->ur_eadata == NULL)
270 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 1];
273 if (req->rq_reqmsg->bufcount > offset + 2) {
274 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
275 if (r->ur_eadata == NULL)
278 r->ur_cookielen = req->rq_reqmsg->buflens[offset + 2];
284 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
285 struct mds_update_record *r)
287 struct mds_rec_create *rec;
290 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
291 lustre_swab_mds_rec_create);
295 r->ur_id1 = &rec->cr_id;
296 r->ur_id2 = &rec->cr_replayid;
297 r->ur_mode = rec->cr_mode;
298 r->ur_rdev = rec->cr_rdev;
299 r->ur_time = rec->cr_time;
300 r->ur_flags = rec->cr_flags;
302 LASSERT_REQSWAB (req, offset + 1);
303 r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
304 if (r->ur_name == NULL)
306 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
308 LASSERT_REQSWAB (req, offset + 2);
309 if (req->rq_reqmsg->bufcount > offset + 2) {
310 if (S_ISLNK(r->ur_mode)) {
311 r->ur_tgt = lustre_msg_string(req->rq_reqmsg,
313 if (r->ur_tgt == NULL)
315 r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
316 } else if (S_ISDIR(r->ur_mode)) {
317 /* Stripe info for mkdir - just a 16bit integer */
318 if (req->rq_reqmsg->buflens[offset + 2] != 2) {
319 CERROR("mkdir stripe info does not match "
320 "expected size %d vs 2\n",
321 req->rq_reqmsg->buflens[offset + 2]);
324 r->ur_eadata = lustre_swab_buf (req->rq_reqmsg,
325 offset + 2, 2, __swab16s);
326 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
328 /* Hm, no other users so far? */
335 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
336 struct mds_update_record *r)
338 struct mds_rec_link *rec;
341 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
342 lustre_swab_mds_rec_link);
346 r->ur_id1 = &rec->lk_id1;
347 r->ur_id2 = &rec->lk_id2;
348 r->ur_time = rec->lk_time;
350 LASSERT_REQSWAB (req, offset + 1);
351 r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
352 if (r->ur_name == NULL)
354 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
358 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
359 struct mds_update_record *r)
361 struct mds_rec_unlink *rec;
364 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
365 lustre_swab_mds_rec_unlink);
369 r->ur_mode = rec->ul_mode;
370 r->ur_id1 = &rec->ul_id1;
371 r->ur_id2 = &rec->ul_id2;
372 r->ur_time = rec->ul_time;
374 LASSERT_REQSWAB (req, offset + 1);
375 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
376 if (r->ur_name == NULL)
378 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
382 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
383 struct mds_update_record *r)
385 struct mds_rec_rename *rec;
388 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
389 lustre_swab_mds_rec_rename);
393 r->ur_id1 = &rec->rn_id1;
394 r->ur_id2 = &rec->rn_id2;
395 r->ur_time = rec->rn_time;
397 LASSERT_REQSWAB (req, offset + 1);
398 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
399 if (r->ur_name == NULL)
401 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
403 LASSERT_REQSWAB (req, offset + 2);
404 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
405 if (r->ur_tgt == NULL)
407 r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
411 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
412 struct mds_update_record *r)
414 struct mds_rec_create *rec;
417 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
418 lustre_swab_mds_rec_create);
422 r->ur_id1 = &rec->cr_id;
423 r->ur_id2 = &rec->cr_replayid;
424 r->ur_mode = rec->cr_mode;
425 r->ur_rdev = rec->cr_rdev;
426 r->ur_time = rec->cr_time;
427 r->ur_flags = rec->cr_flags;
429 LASSERT_REQSWAB (req, offset + 1);
430 r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
431 if (r->ur_name == NULL)
433 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
435 LASSERT_REQSWAB (req, offset + 2);
436 if (req->rq_reqmsg->bufcount > offset + 2) {
437 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
438 if (r->ur_eadata == NULL)
440 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
445 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
446 struct mds_update_record *r);
448 static update_unpacker mds_unpackers[REINT_MAX + 1] = {
449 [REINT_SETATTR] mds_setattr_unpack,
450 [REINT_CREATE] mds_create_unpack,
451 [REINT_LINK] mds_link_unpack,
452 [REINT_UNLINK] mds_unlink_unpack,
453 [REINT_RENAME] mds_rename_unpack,
454 [REINT_OPEN] mds_open_unpack,
457 int mds_update_unpack(struct ptlrpc_request *req, int offset,
458 struct mds_update_record *rec)
466 * NB don't lustre_swab_reqbuf() here. We're just taking a peek and we
467 * want to leave it to the specific unpacker once we've identified the
470 opcodep = lustre_msg_buf (req->rq_reqmsg, offset, sizeof(*opcodep));
475 if (lustre_msg_swabbed (req->rq_reqmsg))
478 if (opcode > REINT_MAX ||
479 mds_unpackers[opcode] == NULL) {
480 CERROR ("Unexpected opcode %d\n", opcode);
486 rec->ur_opcode = opcode;
488 rc = mds_unpackers[opcode](req, offset, rec);
492 static inline void drop_ucred_ginfo(struct lvfs_ucred *ucred)
494 if (ucred->luc_ginfo) {
495 put_group_info(ucred->luc_ginfo);
496 ucred->luc_ginfo = NULL;
501 * root could set any group_info if we allowed setgroups, while
502 * normal user only could 'reduce' their group members -- which
503 * is somewhat expensive.
505 int mds_init_ucred(struct lvfs_ucred *ucred, struct mds_req_sec_desc *rsd)
507 struct group_info *gnew;
513 ucred->luc_fsuid = rsd->rsd_fsuid;
514 ucred->luc_fsgid = rsd->rsd_fsgid;
515 ucred->luc_cap = rsd->rsd_cap;
516 ucred->luc_uid = rsd->rsd_uid;
517 ucred->luc_ghash = mds_get_group_entry(NULL, rsd->rsd_uid);
518 ucred->luc_ginfo = NULL;
520 if (ucred->luc_ghash && ucred->luc_ghash->ge_group_info) {
521 ucred->luc_ginfo = ucred->luc_ghash->ge_group_info;
522 get_group_info(ucred->luc_ginfo);
525 /* everything is done if we don't allow set groups */
526 if (!mds_allow_setgroups())
529 if (rsd->rsd_ngroups > LUSTRE_MAX_GROUPS) {
530 CERROR("client provide too many groups: %d\n",
532 drop_ucred_ginfo(ucred);
533 mds_put_group_entry(NULL, ucred->luc_ghash);
537 if (ucred->luc_uid == 0) {
538 if (rsd->rsd_ngroups == 0) {
539 drop_ucred_ginfo(ucred);
543 gnew = groups_alloc(rsd->rsd_ngroups);
545 CERROR("out of memory\n");
546 drop_ucred_ginfo(ucred);
547 mds_put_group_entry(NULL, ucred->luc_ghash);
550 groups_from_buffer(gnew, rsd->rsd_groups);
551 /* can't rely on client to sort them */
554 drop_ucred_ginfo(ucred);
555 ucred->luc_ginfo = gnew;
557 __u32 set = 0, cur = 0;
558 struct group_info *ginfo;
560 /* if no group info in hash, we don't
561 * bother createing new
563 if (!ucred->luc_ginfo)
566 /* Note: freeing a group_info count on 'nblocks' instead of
567 * 'ngroups', thus we can safely alloc enough buffer and reduce
568 * and ngroups number later.
570 gnew = groups_alloc(rsd->rsd_ngroups);
572 CERROR("out of memory\n");
573 drop_ucred_ginfo(ucred);
574 mds_put_group_entry(NULL, ucred->luc_ghash);
578 ginfo = ucred->luc_ginfo;
579 while (cur < rsd->rsd_ngroups) {
580 if (groups_search(ginfo, rsd->rsd_groups[cur])) {
581 GROUP_AT(gnew, set) = rsd->rsd_groups[cur];
588 put_group_info(ucred->luc_ginfo);
589 ucred->luc_ginfo = gnew;
594 void mds_exit_ucred(struct lvfs_ucred *ucred)
598 if (ucred->luc_ginfo)
599 put_group_info(ucred->luc_ginfo);
600 if (ucred->luc_ghash)
601 mds_put_group_entry(NULL, ucred->luc_ghash);