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;
89 static inline unsigned int attr_unpack(__u64 sa_valid) {
90 unsigned int ia_valid = 0;
92 if (sa_valid & MDS_ATTR_MODE)
93 ia_valid |= ATTR_MODE;
94 if (sa_valid & MDS_ATTR_UID)
96 if (sa_valid & MDS_ATTR_GID)
98 if (sa_valid & MDS_ATTR_SIZE)
99 ia_valid |= ATTR_SIZE;
100 if (sa_valid & MDS_ATTR_ATIME)
101 ia_valid |= ATTR_ATIME;
102 if (sa_valid & MDS_ATTR_MTIME)
103 ia_valid |= ATTR_MTIME;
104 if (sa_valid & MDS_ATTR_CTIME)
105 ia_valid |= ATTR_CTIME;
106 if (sa_valid & MDS_ATTR_ATIME_SET)
107 ia_valid |= ATTR_ATIME_SET;
108 if (sa_valid & MDS_ATTR_MTIME_SET)
109 ia_valid |= ATTR_MTIME_SET;
110 if (sa_valid & MDS_ATTR_FORCE)
111 ia_valid |= ATTR_FORCE;
112 if (sa_valid & MDS_ATTR_ATTR_FLAG)
113 ia_valid |= ATTR_ATTR_FLAG;
114 if (sa_valid & MDS_ATTR_KILL_SUID)
115 ia_valid |= ATTR_KILL_SUID;
116 if (sa_valid & MDS_ATTR_KILL_SGID)
117 ia_valid |= ATTR_KILL_SGID;
118 if (sa_valid & MDS_ATTR_CTIME_SET)
119 ia_valid |= ATTR_CTIME_SET;
120 if (sa_valid & MDS_ATTR_FROM_OPEN)
121 ia_valid |= ATTR_FROM_OPEN;
122 if (sa_valid & MDS_ATTR_BLOCKS)
123 ia_valid |= ATTR_BLOCKS;
124 if (sa_valid & MDS_OPEN_OWNEROVERRIDE)
125 ia_valid |= MDS_OPEN_OWNEROVERRIDE;
130 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
131 struct mds_update_record *r)
133 struct iattr *attr = &r->ur_iattr;
134 struct mds_rec_setattr *rec;
137 rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
138 lustre_swab_mds_rec_setattr);
142 r->ur_uc.luc_fsuid = rec->sa_fsuid;
143 r->ur_uc.luc_fsgid = rec->sa_fsgid;
144 r->ur_uc.luc_cap = rec->sa_cap;
146 r->ur_uc.luc_suppgid1 = rec->sa_suppgid;
147 r->ur_uc.luc_suppgid2 = -1;
149 r->ur_fid1 = &rec->sa_fid;
150 attr->ia_valid = attr_unpack(rec->sa_valid);
151 attr->ia_mode = rec->sa_mode;
152 attr->ia_uid = rec->sa_uid;
153 attr->ia_gid = rec->sa_gid;
154 attr->ia_size = rec->sa_size;
155 LTIME_S(attr->ia_atime) = rec->sa_atime;
156 LTIME_S(attr->ia_mtime) = rec->sa_mtime;
157 LTIME_S(attr->ia_ctime) = rec->sa_ctime;
158 r->ur_flags = rec->sa_attr_flags;
160 lustre_set_req_swabbed(req, offset + 1);
161 r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
162 if (r->ur_eadatalen) {
163 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 1, 0);
164 if (r->ur_eadata == NULL)
167 r->ur_cookielen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
168 if (r->ur_cookielen) {
169 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
170 if (r->ur_eadata == NULL)
173 if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
174 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
176 lustre_swab_ldlm_request);
177 if (r->ur_dlm == NULL)
183 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
184 struct mds_update_record *r)
186 struct mds_rec_create *rec;
189 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
190 lustre_swab_mds_rec_create);
194 r->ur_uc.luc_fsuid = rec->cr_fsuid;
195 r->ur_uc.luc_fsgid = rec->cr_fsgid;
196 r->ur_uc.luc_cap = rec->cr_cap;
198 r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
199 r->ur_uc.luc_suppgid2 = -1;
201 r->ur_fid1 = &rec->cr_fid;
202 r->ur_fid2 = &rec->cr_replayfid;
203 r->ur_mode = rec->cr_mode;
204 r->ur_rdev = rec->cr_rdev;
205 r->ur_time = rec->cr_time;
206 r->ur_flags = rec->cr_flags;
208 lustre_set_req_swabbed(req, offset + 1);
209 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
210 if (r->ur_name == NULL)
212 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
214 lustre_set_req_swabbed(req, offset + 2);
215 r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
217 /* NB for now, we only seem to pass NULL terminated symlink
218 * target strings here. If this ever changes, we'll have
219 * to stop checking for a buffer filled completely with a
220 * NULL terminated string here, and make the callers check
221 * depending on what they expect. We should probably stash
222 * it in r->ur_eadata in that case, so it's obvious... -eeb
224 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
225 if (r->ur_tgt == NULL)
228 if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
229 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
231 lustre_swab_ldlm_request);
232 if (r->ur_dlm == NULL)
238 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
239 struct mds_update_record *r)
241 struct mds_rec_link *rec;
244 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
245 lustre_swab_mds_rec_link);
249 r->ur_uc.luc_fsuid = rec->lk_fsuid;
250 r->ur_uc.luc_fsgid = rec->lk_fsgid;
251 r->ur_uc.luc_cap = rec->lk_cap;
253 r->ur_uc.luc_suppgid1 = rec->lk_suppgid1;
254 r->ur_uc.luc_suppgid2 = rec->lk_suppgid2;
256 r->ur_fid1 = &rec->lk_fid1;
257 r->ur_fid2 = &rec->lk_fid2;
258 r->ur_time = rec->lk_time;
260 lustre_set_req_swabbed(req, offset + 1);
261 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
262 if (r->ur_name == NULL)
264 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
265 if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
266 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
268 lustre_swab_ldlm_request);
269 if (r->ur_dlm == NULL)
275 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
276 struct mds_update_record *r)
278 struct mds_rec_unlink *rec;
281 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
282 lustre_swab_mds_rec_unlink);
286 r->ur_uc.luc_fsuid = rec->ul_fsuid;
287 r->ur_uc.luc_fsgid = rec->ul_fsgid;
288 r->ur_uc.luc_cap = rec->ul_cap;
290 r->ur_uc.luc_suppgid1 = rec->ul_suppgid;
291 r->ur_uc.luc_suppgid2 = -1;
293 r->ur_mode = rec->ul_mode;
294 r->ur_fid1 = &rec->ul_fid1;
295 r->ur_fid2 = &rec->ul_fid2;
296 r->ur_time = rec->ul_time;
298 lustre_set_req_swabbed(req, offset + 1);
299 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
300 if (r->ur_name == NULL)
302 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
304 if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
305 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
307 lustre_swab_ldlm_request);
308 if (r->ur_dlm == NULL)
314 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
315 struct mds_update_record *r)
317 struct mds_rec_rename *rec;
320 rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
321 lustre_swab_mds_rec_rename);
325 r->ur_uc.luc_fsuid = rec->rn_fsuid;
326 r->ur_uc.luc_fsgid = rec->rn_fsgid;
327 r->ur_uc.luc_cap = rec->rn_cap;
329 r->ur_uc.luc_suppgid1 = rec->rn_suppgid1;
330 r->ur_uc.luc_suppgid2 = rec->rn_suppgid2;
332 r->ur_fid1 = &rec->rn_fid1;
333 r->ur_fid2 = &rec->rn_fid2;
334 r->ur_time = rec->rn_time;
336 lustre_set_req_swabbed(req, offset + 1);
337 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
338 if (r->ur_name == NULL)
340 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
342 lustre_set_req_swabbed(req, offset + 2);
343 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
344 if (r->ur_tgt == NULL)
346 r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
347 if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
348 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
350 lustre_swab_ldlm_request);
351 if (r->ur_dlm == NULL)
357 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
358 struct mds_update_record *r)
360 struct mds_rec_create *rec;
363 rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
364 lustre_swab_mds_rec_create);
368 r->ur_uc.luc_fsuid = rec->cr_fsuid;
369 r->ur_uc.luc_fsgid = rec->cr_fsgid;
370 r->ur_uc.luc_cap = rec->cr_cap;
372 r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
373 r->ur_uc.luc_suppgid2 = -1;
375 r->ur_fid1 = &rec->cr_fid;
376 r->ur_fid2 = &rec->cr_replayfid;
377 r->ur_mode = rec->cr_mode;
378 r->ur_rdev = rec->cr_rdev;
379 r->ur_time = rec->cr_time;
380 r->ur_flags = rec->cr_flags;
382 lustre_set_req_swabbed(req, offset + 1);
383 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
384 if (r->ur_name == NULL)
386 r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
388 lustre_set_req_swabbed(req, offset + 2);
389 r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
390 if (r->ur_eadatalen) {
391 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
392 if (r->ur_eadata == NULL)
398 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
399 struct mds_update_record *r);
401 static update_unpacker mds_unpackers[REINT_MAX] = {
402 [REINT_SETATTR] mds_setattr_unpack,
403 [REINT_CREATE] mds_create_unpack,
404 [REINT_LINK] mds_link_unpack,
405 [REINT_UNLINK] mds_unlink_unpack,
406 [REINT_RENAME] mds_rename_unpack,
407 [REINT_OPEN] mds_open_unpack,
410 int mds_update_unpack(struct ptlrpc_request *req, int offset,
411 struct mds_update_record *rec)
413 mds_reint_t opcode, *opcodep;
417 /* NB don't lustre_swab_reqbuf() here. We're just taking a peek
418 * and we want to leave it to the specific unpacker once we've
419 * identified the message type */
420 opcodep = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*opcodep));
425 if (lustre_msg_swabbed(req->rq_reqmsg))
428 if (opcode >= REINT_MAX || mds_unpackers[opcode] == NULL) {
429 CERROR("Unexpected opcode %d\n", opcode);
433 rec->ur_opcode = opcode;
434 rc = mds_unpackers[opcode](req, offset, rec);
439 int mds_init_ucred(struct lvfs_ucred *ucred, struct ptlrpc_request *req,
442 struct mds_body *body = lustre_msg_buf(req->rq_reqmsg, offset,
445 struct mds_obd *mds = mds_req2mds(req);
449 LASSERT(body != NULL); /* previously verified & swabbed by caller */
452 if (req->rq_uid != LNET_UID_ANY) {
453 /* Non-root local cluster client */
454 LASSERT (req->rq_uid != 0);
455 ucred->luc_fsuid = req->rq_uid;
459 ucred->luc_fsuid = body->fsuid;
460 ucred->luc_fsgid = body->fsgid;
461 ucred->luc_cap = body->capability;
465 ucred->luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
469 if (IS_ERR(ucred->luc_uce)) {
470 rc = PTR_ERR(ucred->luc_uce);
471 ucred->luc_uce = NULL;
477 ucred->luc_fsgid = ucred->luc_uce->ue_primary;
484 void mds_exit_ucred(struct lvfs_ucred *ucred, struct mds_obd *mds)
487 upcall_cache_put_entry(mds->mds_group_hash, ucred->luc_uce);