Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[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 <linux/lustre_mds.h>
51 #include <linux/lustre_lite.h>
52
53 void mds_pack_inode2fid(struct ll_fid *fid, struct inode *inode)
54 {
55         fid->id = inode->i_ino;
56         fid->generation = inode->i_generation;
57         fid->f_type = (S_IFMT & inode->i_mode);
58 }
59
60 void mds_pack_inode2body(struct mds_body *b, struct inode *inode)
61 {
62         b->valid = OBD_MD_FLID | OBD_MD_FLATIME | OBD_MD_FLMTIME |
63                 OBD_MD_FLCTIME | OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
64                 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
65                 OBD_MD_FLNLINK | OBD_MD_FLGENER;
66
67         /* The MDS file size isn't authoritative for regular files, so don't
68          * even pretend. */
69         if (S_ISREG(inode->i_mode))
70                 b->valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
71
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 = inode->i_size;
78         b->blocks = inode->i_blocks;
79         b->uid = inode->i_uid;
80         b->gid = inode->i_gid;
81         b->flags = inode->i_flags;
82         b->rdev = b->rdev;
83         b->nlink = inode->i_nlink;
84         b->generation = inode->i_generation;
85         b->suppgid = -1;
86 }
87 /* unpacking */
88 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
89                               struct mds_update_record *r)
90 {
91         struct iattr *attr = &r->ur_iattr;
92         struct mds_rec_setattr *rec;
93         ENTRY;
94
95         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
96                                   lustre_swab_mds_rec_setattr);
97         if (rec == NULL)
98                 RETURN (-EFAULT);
99
100         r->ur_fsuid = rec->sa_fsuid;
101         r->ur_fsgid = rec->sa_fsgid;
102         r->ur_cap = rec->sa_cap;
103         r->ur_suppgid1 = rec->sa_suppgid;
104         r->ur_suppgid2 = -1;
105         r->ur_fid1 = &rec->sa_fid;
106         attr->ia_valid = rec->sa_valid;
107         attr->ia_mode = rec->sa_mode;
108         attr->ia_uid = rec->sa_uid;
109         attr->ia_gid = rec->sa_gid;
110         attr->ia_size = rec->sa_size;
111         LTIME_S(attr->ia_atime) = rec->sa_atime;
112         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
113         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
114         attr->ia_attr_flags = rec->sa_attr_flags;
115
116         LASSERT_REQSWAB (req, offset + 1);
117         if (req->rq_reqmsg->bufcount > offset + 1) {
118                 r->ur_eadata = lustre_msg_buf (req->rq_reqmsg,
119                                                offset + 1, 0);
120                 if (r->ur_eadata == NULL)
121                         RETURN (-EFAULT);
122                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 1];
123         } else {
124                 r->ur_eadata = NULL;
125                 r->ur_eadatalen = 0;
126         }
127
128         RETURN(0);
129 }
130
131 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
132                              struct mds_update_record *r)
133 {
134         struct mds_rec_create *rec;
135         ENTRY;
136
137         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
138                                   lustre_swab_mds_rec_create);
139         if (rec == NULL)
140                 RETURN (-EFAULT);
141
142         r->ur_fsuid = rec->cr_fsuid;
143         r->ur_fsgid = rec->cr_fsgid;
144         r->ur_cap = rec->cr_cap;
145         r->ur_fid1 = &rec->cr_fid;
146         r->ur_fid2 = &rec->cr_replayfid;
147         r->ur_mode = rec->cr_mode;
148         r->ur_rdev = rec->cr_rdev;
149         r->ur_uid = rec->cr_uid;
150         r->ur_gid = rec->cr_gid;
151         r->ur_time = rec->cr_time;
152         r->ur_flags = rec->cr_flags;
153         r->ur_suppgid1 = rec->cr_suppgid;
154         r->ur_suppgid2 = -1;
155
156         LASSERT_REQSWAB (req, offset + 1);
157         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
158         if (r->ur_name == NULL)
159                 RETURN (-EFAULT);
160         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
161
162         LASSERT_REQSWAB (req, offset + 2);
163         if (req->rq_reqmsg->bufcount > offset + 2) {
164                 /* NB for now, we only seem to pass NULL terminated symlink
165                  * target strings here.  If this ever changes, we'll have
166                  * to stop checking for a buffer filled completely with a
167                  * NULL terminated string here, and make the callers check
168                  * depending on what they expect.  We should probably stash
169                  * it in r->ur_eadata in that case, so it's obvious... -eeb
170                  */
171                 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
172                 if (r->ur_tgt == NULL)
173                         RETURN (-EFAULT);
174                 r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
175         } else {
176                 r->ur_tgt = NULL;
177                 r->ur_tgtlen = 0;
178         }
179         RETURN(0);
180 }
181
182 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
183                            struct mds_update_record *r)
184 {
185         struct mds_rec_link *rec;
186         ENTRY;
187
188         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
189                                   lustre_swab_mds_rec_link);
190         if (rec == NULL)
191                 RETURN (-EFAULT);
192
193         r->ur_fsuid = rec->lk_fsuid;
194         r->ur_fsgid = rec->lk_fsgid;
195         r->ur_cap = rec->lk_cap;
196         r->ur_suppgid1 = rec->lk_suppgid1;
197         r->ur_suppgid2 = rec->lk_suppgid2;
198         r->ur_fid1 = &rec->lk_fid1;
199         r->ur_fid2 = &rec->lk_fid2;
200
201         LASSERT_REQSWAB (req, offset + 1);
202         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
203         if (r->ur_name == NULL)
204                 RETURN (-EFAULT);
205         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
206         RETURN(0);
207 }
208
209 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
210                              struct mds_update_record *r)
211 {
212         struct mds_rec_unlink *rec;
213         ENTRY;
214
215         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
216                                   lustre_swab_mds_rec_unlink);
217         if (rec == NULL)
218                 RETURN(-EFAULT);
219
220         r->ur_fsuid = rec->ul_fsuid;
221         r->ur_fsgid = rec->ul_fsgid;
222         r->ur_cap = rec->ul_cap;
223         r->ur_mode = rec->ul_mode;
224         r->ur_suppgid1 = rec->ul_suppgid;
225         r->ur_suppgid2 = -1;
226         r->ur_fid1 = &rec->ul_fid1;
227         r->ur_fid2 = &rec->ul_fid2;
228
229         LASSERT_REQSWAB (req, offset + 1);
230         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
231         if (r->ur_name == NULL)
232                 RETURN(-EFAULT);
233         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
234         RETURN(0);
235 }
236
237 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
238                              struct mds_update_record *r)
239 {
240         struct mds_rec_rename *rec;
241         ENTRY;
242
243         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
244                                   lustre_swab_mds_rec_unlink);
245         if (rec == NULL)
246                 RETURN(-EFAULT);
247
248         r->ur_fsuid = rec->rn_fsuid;
249         r->ur_fsgid = rec->rn_fsgid;
250         r->ur_cap = rec->rn_cap;
251         r->ur_suppgid1 = rec->rn_suppgid1;
252         r->ur_suppgid2 = rec->rn_suppgid2;
253         r->ur_fid1 = &rec->rn_fid1;
254         r->ur_fid2 = &rec->rn_fid2;
255
256         LASSERT_REQSWAB (req, offset + 1);
257         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
258         if (r->ur_name == NULL)
259                 RETURN(-EFAULT);
260         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
261
262         LASSERT_REQSWAB (req, offset + 2);
263         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
264         if (r->ur_tgt == NULL)
265                 RETURN(-EFAULT);
266         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
267         RETURN(0);
268 }
269
270 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
271                                struct mds_update_record *r);
272
273 static update_unpacker mds_unpackers[REINT_MAX + 1] = {
274         [REINT_SETATTR] mds_setattr_unpack,
275         [REINT_CREATE] mds_create_unpack,
276         [REINT_LINK] mds_link_unpack,
277         [REINT_UNLINK] mds_unlink_unpack,
278         [REINT_RENAME] mds_rename_unpack,
279         [REINT_OPEN] mds_create_unpack,
280 };
281
282 int mds_update_unpack(struct ptlrpc_request *req, int offset,
283                       struct mds_update_record *rec)
284 {
285         __u32 *opcodep;
286         __u32  opcode;
287         int rc;
288         ENTRY;
289
290         /* NB don't lustre_swab_reqbuf() here.  We're just taking a peek
291          * and we want to leave it to the specific unpacker once we've
292          * identified the message type */
293         opcodep = lustre_msg_buf (req->rq_reqmsg, offset, sizeof (*opcodep));
294         if (opcodep == NULL)
295                 RETURN(-EFAULT);
296
297         opcode = *opcodep;
298         if (lustre_msg_swabbed (req->rq_reqmsg))
299                 __swab32s (&opcode);
300
301         if (opcode > REINT_MAX ||
302             mds_unpackers[opcode] == NULL) {
303                 CERROR ("Unexpected opcode %d\n", opcode);
304                 RETURN(-EFAULT);
305         }
306
307         rec->ur_opcode = opcode;
308         rc = mds_unpackers[opcode](req, offset, rec);
309         RETURN(rc);
310 }