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 void mds_pack_inode2fid(struct ll_fid *fid, struct inode *inode)
54 fid->id = inode->i_ino;
55 fid->generation = inode->i_generation;
56 fid->f_type = (S_IFMT & inode->i_mode);
59 /* Note that we can copy all of the fields, just some will not be "valid" */
60 void mds_pack_inode2body(struct mds_body *b, struct inode *inode)
62 b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
63 OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
64 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
65 OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
67 if (!S_ISREG(inode->i_mode))
68 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLATIME |
69 OBD_MD_FLMTIME | OBD_MD_FLRDEV;
71 b->ino = inode->i_ino;
72 b->atime = LTIME_S(inode->i_atime);
73 b->mtime = LTIME_S(inode->i_mtime);
74 b->ctime = LTIME_S(inode->i_ctime);
75 b->mode = inode->i_mode;
76 b->size = inode->i_size;
77 b->blocks = inode->i_blocks;
78 b->uid = inode->i_uid;
79 b->gid = inode->i_gid;
80 b->flags = inode->i_flags;
81 b->rdev = inode->i_rdev;
82 /* Return the correct link count for orphan inodes */
83 b->nlink = mds_inode_is_orphan(inode) ? 0 : inode->i_nlink;
84 b->generation = inode->i_generation;
89 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
90 struct mds_update_record *r)
92 struct iattr *attr = &r->ur_iattr;
93 struct mds_rec_setattr *rec;
96 rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
97 lustre_swab_mds_rec_setattr);
101 r->ur_fsuid = rec->sa_fsuid;
102 r->ur_fsgid = rec->sa_fsgid;
103 r->ur_cap = rec->sa_cap;
104 r->ur_suppgid1 = rec->sa_suppgid;
106 r->ur_fid1 = &rec->sa_fid;
107 attr->ia_valid = rec->sa_valid;
108 attr->ia_mode = rec->sa_mode;
109 attr->ia_uid = rec->sa_uid;
110 attr->ia_gid = rec->sa_gid;
111 attr->ia_size = rec->sa_size;
112 LTIME_S(attr->ia_atime) = rec->sa_atime;
113 LTIME_S(attr->ia_mtime) = rec->sa_mtime;
114 LTIME_S(attr->ia_ctime) = rec->sa_ctime;
115 attr->ia_attr_flags = rec->sa_attr_flags;
117 LASSERT_REQSWAB (req, offset + 1);
118 if (req->rq_reqmsg->bufcount > offset + 1) {
119 r->ur_eadata = lustre_msg_buf (req->rq_reqmsg,
121 if (r->ur_eadata == NULL)
123 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 1];
126 if (req->rq_reqmsg->bufcount > offset + 2) {
127 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
128 if (r->ur_eadata == NULL)
131 r->ur_cookielen = req->rq_reqmsg->buflens[offset + 2];
137 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
138 struct mds_update_record *r)
140 struct mds_rec_create *rec;
143 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
144 lustre_swab_mds_rec_create);
148 r->ur_fsuid = rec->cr_fsuid;
149 r->ur_fsgid = rec->cr_fsgid;
150 r->ur_cap = rec->cr_cap;
151 r->ur_fid1 = &rec->cr_fid;
152 r->ur_fid2 = &rec->cr_replayfid;
153 r->ur_mode = rec->cr_mode;
154 r->ur_rdev = rec->cr_rdev;
155 r->ur_time = rec->cr_time;
156 r->ur_flags = rec->cr_flags;
157 r->ur_suppgid1 = rec->cr_suppgid;
160 LASSERT_REQSWAB (req, offset + 1);
161 r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
162 if (r->ur_name == NULL)
164 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
166 LASSERT_REQSWAB (req, offset + 2);
167 if (req->rq_reqmsg->bufcount > offset + 2) {
168 /* NB for now, we only seem to pass NULL terminated symlink
169 * target strings here. If this ever changes, we'll have
170 * to stop checking for a buffer filled completely with a
171 * NULL terminated string here, and make the callers check
172 * depending on what they expect. We should probably stash
173 * it in r->ur_eadata in that case, so it's obvious... -eeb
175 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
176 if (r->ur_tgt == NULL)
178 r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
183 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
184 struct mds_update_record *r)
186 struct mds_rec_link *rec;
189 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
190 lustre_swab_mds_rec_link);
194 r->ur_fsuid = rec->lk_fsuid;
195 r->ur_fsgid = rec->lk_fsgid;
196 r->ur_cap = rec->lk_cap;
197 r->ur_suppgid1 = rec->lk_suppgid1;
198 r->ur_suppgid2 = rec->lk_suppgid2;
199 r->ur_fid1 = &rec->lk_fid1;
200 r->ur_fid2 = &rec->lk_fid2;
201 r->ur_time = rec->lk_time;
203 LASSERT_REQSWAB (req, offset + 1);
204 r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
205 if (r->ur_name == NULL)
207 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
211 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
212 struct mds_update_record *r)
214 struct mds_rec_unlink *rec;
217 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
218 lustre_swab_mds_rec_unlink);
222 r->ur_fsuid = rec->ul_fsuid;
223 r->ur_fsgid = rec->ul_fsgid;
224 r->ur_cap = rec->ul_cap;
225 r->ur_mode = rec->ul_mode;
226 r->ur_suppgid1 = rec->ul_suppgid;
228 r->ur_fid1 = &rec->ul_fid1;
229 r->ur_fid2 = &rec->ul_fid2;
230 r->ur_time = rec->ul_time;
232 LASSERT_REQSWAB (req, offset + 1);
233 r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
234 if (r->ur_name == NULL)
236 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
240 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
241 struct mds_update_record *r)
243 struct mds_rec_rename *rec;
246 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
247 lustre_swab_mds_rec_rename);
251 r->ur_fsuid = rec->rn_fsuid;
252 r->ur_fsgid = rec->rn_fsgid;
253 r->ur_cap = rec->rn_cap;
254 r->ur_suppgid1 = rec->rn_suppgid1;
255 r->ur_suppgid2 = rec->rn_suppgid2;
256 r->ur_fid1 = &rec->rn_fid1;
257 r->ur_fid2 = &rec->rn_fid2;
258 r->ur_time = rec->rn_time;
260 LASSERT_REQSWAB (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 = req->rq_reqmsg->buflens[offset + 1];
266 LASSERT_REQSWAB (req, offset + 2);
267 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
268 if (r->ur_tgt == NULL)
270 r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
274 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
275 struct mds_update_record *r)
277 struct mds_rec_create *rec;
280 rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
281 lustre_swab_mds_rec_create);
285 r->ur_fsuid = rec->cr_fsuid;
286 r->ur_fsgid = rec->cr_fsgid;
287 r->ur_cap = rec->cr_cap;
288 r->ur_fid1 = &rec->cr_fid;
289 r->ur_fid2 = &rec->cr_replayfid;
290 r->ur_mode = rec->cr_mode;
291 r->ur_rdev = rec->cr_rdev;
292 r->ur_time = rec->cr_time;
293 r->ur_flags = rec->cr_flags;
294 r->ur_suppgid1 = rec->cr_suppgid;
297 LASSERT_REQSWAB (req, offset + 1);
298 r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
299 if (r->ur_name == NULL)
301 r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
303 LASSERT_REQSWAB (req, offset + 2);
304 if (req->rq_reqmsg->bufcount > offset + 2) {
305 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
306 if (r->ur_eadata == NULL)
308 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
313 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
314 struct mds_update_record *r);
316 static update_unpacker mds_unpackers[REINT_MAX] = {
317 [REINT_SETATTR] mds_setattr_unpack,
318 [REINT_CREATE] mds_create_unpack,
319 [REINT_LINK] mds_link_unpack,
320 [REINT_UNLINK] mds_unlink_unpack,
321 [REINT_RENAME] mds_rename_unpack,
322 [REINT_OPEN] mds_open_unpack,
325 int mds_update_unpack(struct ptlrpc_request *req, int offset,
326 struct mds_update_record *rec)
328 mds_reint_t opcode, *opcodep;
332 /* NB don't lustre_swab_reqbuf() here. We're just taking a peek
333 * and we want to leave it to the specific unpacker once we've
334 * identified the message type */
335 opcodep = lustre_msg_buf (req->rq_reqmsg, offset, sizeof (*opcodep));
340 if (lustre_msg_swabbed (req->rq_reqmsg))
343 if (opcode >= REINT_MAX || mds_unpackers[opcode] == NULL) {
344 CERROR("Unexpected opcode %d\n", opcode);
348 rec->ur_opcode = opcode;
349 rc = mds_unpackers[opcode](req, offset, rec);
351 rec->ur_fsuid = req->rq_uid;