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 the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
25 #define DEBUG_SUBSYSTEM S_MDS
27 #ifndef AUTOCONF_INCLUDED
28 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/version.h>
37 #include <linux/buffer_head.h> // for wait_on_buffer
38 #include <linux/unistd.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <linux/slab.h>
47 #include <asm/segment.h>
49 #include <obd_support.h>
50 #include <lustre_lib.h>
51 #include "mds_internal.h"
53 void mds_pack_inode2fid(struct ll_fid *fid, struct inode *inode)
55 fid->id = inode->i_ino;
56 fid->generation = inode->i_generation;
57 fid->f_type = (S_IFMT & inode->i_mode);
60 /* Note that we can copy all of the fields, just some will not be "valid" */
61 void mds_pack_inode2body(struct mds_body *b, struct inode *inode)
63 b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
64 OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
65 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
66 OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
68 if (!S_ISREG(inode->i_mode))
69 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLATIME |
70 OBD_MD_FLMTIME | OBD_MD_FLRDEV;
72 b->ino = inode->i_ino;
73 b->atime = LTIME_S(inode->i_atime);
74 b->mtime = LTIME_S(inode->i_mtime);
75 b->ctime = LTIME_S(inode->i_ctime);
76 b->mode = inode->i_mode;
77 b->size = i_size_read(inode);
78 b->blocks = inode->i_blocks;
79 b->uid = inode->i_uid;
80 b->gid = inode->i_gid;
81 b->flags = ll_inode_to_ext_flags(b->flags, inode->i_flags);
82 b->rdev = inode->i_rdev;
83 /* Return the correct link count for orphan inodes */
84 b->nlink = mds_inode_is_orphan(inode) ? 0 : inode->i_nlink;
85 b->generation = inode->i_generation;
90 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
91 struct mds_update_record *r)
93 struct iattr *attr = &r->ur_iattr;
94 struct mds_rec_setattr *rec;
97 rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
98 lustre_swab_mds_rec_setattr);
102 r->ur_uc.luc_fsuid = rec->sa_fsuid;
103 r->ur_uc.luc_fsgid = rec->sa_fsgid;
104 r->ur_uc.luc_cap = rec->sa_cap;
106 r->ur_uc.luc_suppgid1 = rec->sa_suppgid;
107 r->ur_uc.luc_suppgid2 = -1;
109 r->ur_fid1 = &rec->sa_fid;
110 attr->ia_valid = rec->sa_valid;
111 attr->ia_mode = rec->sa_mode;
112 attr->ia_uid = rec->sa_uid;
113 attr->ia_gid = rec->sa_gid;
114 attr->ia_size = rec->sa_size;
115 LTIME_S(attr->ia_atime) = rec->sa_atime;
116 LTIME_S(attr->ia_mtime) = rec->sa_mtime;
117 LTIME_S(attr->ia_ctime) = rec->sa_ctime;
118 r->ur_flags = rec->sa_attr_flags;
120 LASSERT_REQSWAB (req, offset + 1);
121 r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
122 if (r->ur_eadatalen) {
123 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 1, 0);
124 if (r->ur_eadata == NULL)
127 r->ur_cookielen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
128 if (r->ur_cookielen) {
129 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
130 if (r->ur_eadata == NULL)
133 if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
134 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
136 lustre_swab_ldlm_request);
137 if (r->ur_dlm == NULL)
143 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
144 struct mds_update_record *r)
146 struct mds_rec_create *rec;
149 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
150 lustre_swab_mds_rec_create);
154 r->ur_uc.luc_fsuid = rec->cr_fsuid;
155 r->ur_uc.luc_fsgid = rec->cr_fsgid;
156 r->ur_uc.luc_cap = rec->cr_cap;
158 r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
159 r->ur_uc.luc_suppgid2 = -1;
161 r->ur_fid1 = &rec->cr_fid;
162 r->ur_fid2 = &rec->cr_replayfid;
163 r->ur_mode = rec->cr_mode;
164 r->ur_rdev = rec->cr_rdev;
165 r->ur_time = rec->cr_time;
166 r->ur_flags = rec->cr_flags;
168 LASSERT_REQSWAB(req, offset + 1);
169 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
170 if (r->ur_name == NULL)
172 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
174 LASSERT_REQSWAB(req, offset + 2);
175 r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
177 /* NB for now, we only seem to pass NULL terminated symlink
178 * target strings here. If this ever changes, we'll have
179 * to stop checking for a buffer filled completely with a
180 * NULL terminated string here, and make the callers check
181 * depending on what they expect. We should probably stash
182 * it in r->ur_eadata in that case, so it's obvious... -eeb
184 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
185 if (r->ur_tgt == NULL)
188 if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
189 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
191 lustre_swab_ldlm_request);
192 if (r->ur_dlm == NULL)
198 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
199 struct mds_update_record *r)
201 struct mds_rec_link *rec;
204 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
205 lustre_swab_mds_rec_link);
209 r->ur_uc.luc_fsuid = rec->lk_fsuid;
210 r->ur_uc.luc_fsgid = rec->lk_fsgid;
211 r->ur_uc.luc_cap = rec->lk_cap;
213 r->ur_uc.luc_suppgid1 = rec->lk_suppgid1;
214 r->ur_uc.luc_suppgid2 = rec->lk_suppgid2;
216 r->ur_fid1 = &rec->lk_fid1;
217 r->ur_fid2 = &rec->lk_fid2;
218 r->ur_time = rec->lk_time;
220 LASSERT_REQSWAB(req, offset + 1);
221 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
222 if (r->ur_name == NULL)
224 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
225 if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
226 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
228 lustre_swab_ldlm_request);
229 if (r->ur_dlm == NULL)
235 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
236 struct mds_update_record *r)
238 struct mds_rec_unlink *rec;
241 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
242 lustre_swab_mds_rec_unlink);
246 r->ur_uc.luc_fsuid = rec->ul_fsuid;
247 r->ur_uc.luc_fsgid = rec->ul_fsgid;
248 r->ur_uc.luc_cap = rec->ul_cap;
250 r->ur_uc.luc_suppgid1 = rec->ul_suppgid;
251 r->ur_uc.luc_suppgid2 = -1;
253 r->ur_mode = rec->ul_mode;
254 r->ur_fid1 = &rec->ul_fid1;
255 r->ur_fid2 = &rec->ul_fid2;
256 r->ur_time = rec->ul_time;
258 LASSERT_REQSWAB(req, offset + 1);
259 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
260 if (r->ur_name == NULL)
262 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
264 if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
265 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
267 lustre_swab_ldlm_request);
268 if (r->ur_dlm == NULL)
274 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
275 struct mds_update_record *r)
277 struct mds_rec_rename *rec;
280 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
281 lustre_swab_mds_rec_rename);
285 r->ur_uc.luc_fsuid = rec->rn_fsuid;
286 r->ur_uc.luc_fsgid = rec->rn_fsgid;
287 r->ur_uc.luc_cap = rec->rn_cap;
289 r->ur_uc.luc_suppgid1 = rec->rn_suppgid1;
290 r->ur_uc.luc_suppgid2 = rec->rn_suppgid2;
292 r->ur_fid1 = &rec->rn_fid1;
293 r->ur_fid2 = &rec->rn_fid2;
294 r->ur_time = rec->rn_time;
296 LASSERT_REQSWAB (req, offset + 1);
297 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
298 if (r->ur_name == NULL)
300 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
302 LASSERT_REQSWAB (req, offset + 2);
303 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
304 if (r->ur_tgt == NULL)
306 r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
307 if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
308 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
310 lustre_swab_ldlm_request);
311 if (r->ur_dlm == NULL)
317 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
318 struct mds_update_record *r)
320 struct mds_rec_create *rec;
323 rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
324 lustre_swab_mds_rec_create);
328 r->ur_uc.luc_fsuid = rec->cr_fsuid;
329 r->ur_uc.luc_fsgid = rec->cr_fsgid;
330 r->ur_uc.luc_cap = rec->cr_cap;
332 r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
333 r->ur_uc.luc_suppgid2 = -1;
335 r->ur_fid1 = &rec->cr_fid;
336 r->ur_fid2 = &rec->cr_replayfid;
337 r->ur_mode = rec->cr_mode;
338 r->ur_rdev = rec->cr_rdev;
339 r->ur_time = rec->cr_time;
340 r->ur_flags = rec->cr_flags;
342 LASSERT_REQSWAB(req, offset + 1);
343 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
344 if (r->ur_name == NULL)
346 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
348 LASSERT_REQSWAB(req, offset + 2);
349 r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
350 if (r->ur_eadatalen) {
351 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
352 if (r->ur_eadata == NULL)
358 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
359 struct mds_update_record *r);
361 static update_unpacker mds_unpackers[REINT_MAX] = {
362 [REINT_SETATTR] mds_setattr_unpack,
363 [REINT_CREATE] mds_create_unpack,
364 [REINT_LINK] mds_link_unpack,
365 [REINT_UNLINK] mds_unlink_unpack,
366 [REINT_RENAME] mds_rename_unpack,
367 [REINT_OPEN] mds_open_unpack,
370 int mds_update_unpack(struct ptlrpc_request *req, int offset,
371 struct mds_update_record *rec)
373 mds_reint_t opcode, *opcodep;
377 /* NB don't lustre_swab_reqbuf() here. We're just taking a peek
378 * and we want to leave it to the specific unpacker once we've
379 * identified the message type */
380 opcodep = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*opcodep));
385 if (lustre_msg_swabbed(req->rq_reqmsg))
388 if (opcode >= REINT_MAX || mds_unpackers[opcode] == NULL) {
389 CERROR("Unexpected opcode %d\n", opcode);
393 rec->ur_opcode = opcode;
394 rc = mds_unpackers[opcode](req, offset, rec);
399 int mds_init_ucred(struct lvfs_ucred *ucred, struct ptlrpc_request *req,
402 struct mds_body *body = lustre_msg_buf(req->rq_reqmsg, offset,
405 struct mds_obd *mds = mds_req2mds(req);
409 LASSERT(body != NULL); /* previously verified & swabbed by caller */
412 if (req->rq_uid != LNET_UID_ANY) {
413 /* Non-root local cluster client */
414 LASSERT (req->rq_uid != 0);
415 ucred->luc_fsuid = req->rq_uid;
419 ucred->luc_fsuid = body->fsuid;
420 ucred->luc_fsgid = body->fsgid;
421 ucred->luc_cap = body->capability;
425 ucred->luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
429 if (IS_ERR(ucred->luc_uce)) {
430 rc = PTR_ERR(ucred->luc_uce);
431 ucred->luc_uce = NULL;
437 ucred->luc_fsgid = ucred->luc_uce->ue_primary;
444 void mds_exit_ucred(struct lvfs_ucred *ucred, struct mds_obd *mds)
447 upcall_cache_put_entry(mds->mds_group_hash, ucred->luc_uce);