Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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
52 void mds_pack_inode2fid(struct obd_device *obd, struct ll_fid *fid,
53                                 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         fid->mds = obd->u.mds.mds_num;
59 }
60
61 /* Note that we can copy all of the fields, just some will not be "valid" */
62 void mds_pack_inode2body(struct obd_device *obd, struct mds_body *b,
63                                 struct inode *inode)
64 {
65         b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
66                     OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
67                     OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
68                     OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
69
70         if (!S_ISREG(inode->i_mode))
71                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLATIME |
72                             OBD_MD_FLMTIME | OBD_MD_FLRDEV;
73
74         b->ino = inode->i_ino;
75         b->atime = LTIME_S(inode->i_atime);
76         b->mtime = LTIME_S(inode->i_mtime);
77         b->ctime = LTIME_S(inode->i_ctime);
78         b->mode = inode->i_mode;
79         b->size = inode->i_size;
80         b->blocks = inode->i_blocks;
81         b->uid = inode->i_uid;
82         b->gid = inode->i_gid;
83         b->flags = inode->i_flags;
84         b->rdev = inode->i_rdev;
85         /* Return the correct link count for orphan inodes */
86         b->nlink = mds_inode_is_orphan(inode) ? 0 : inode->i_nlink;
87         b->generation = inode->i_generation;
88         b->suppgid = -1;
89         b->mds = obd->u.mds.mds_num;
90 }
91
92 /* unpacking */
93 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
94                               struct mds_update_record *r)
95 {
96         struct iattr *attr = &r->ur_iattr;
97         struct mds_rec_setattr *rec;
98         ENTRY;
99
100         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
101                                  lustre_swab_mds_rec_setattr);
102         if (rec == NULL)
103                 RETURN (-EFAULT);
104
105         r->ur_fsuid = rec->sa_fsuid;
106         r->ur_fsgid = rec->sa_fsgid;
107         r->ur_cap = rec->sa_cap;
108         r->ur_suppgid1 = rec->sa_suppgid;
109         r->ur_suppgid2 = -1;
110         r->ur_fid1 = &rec->sa_fid;
111         attr->ia_valid = rec->sa_valid;
112         attr->ia_mode = rec->sa_mode;
113         attr->ia_uid = rec->sa_uid;
114         attr->ia_gid = rec->sa_gid;
115         attr->ia_size = rec->sa_size;
116         LTIME_S(attr->ia_atime) = rec->sa_atime;
117         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
118         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
119         attr->ia_attr_flags = rec->sa_attr_flags;
120
121         LASSERT_REQSWAB (req, offset + 1);
122         if (req->rq_reqmsg->bufcount > offset + 1) {
123                 r->ur_eadata = lustre_msg_buf (req->rq_reqmsg,
124                                                offset + 1, 0);
125                 if (r->ur_eadata == NULL)
126                         RETURN (-EFAULT);
127                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 1];
128         }
129
130         if (req->rq_reqmsg->bufcount > offset + 2) {
131                 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
132                 if (r->ur_eadata == NULL)
133                         RETURN (-EFAULT);
134
135                 r->ur_cookielen = req->rq_reqmsg->buflens[offset + 2];
136         }
137
138         RETURN(0);
139 }
140
141 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
142                              struct mds_update_record *r)
143 {
144         struct mds_rec_create *rec;
145         ENTRY;
146
147         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
148                                   lustre_swab_mds_rec_create);
149         if (rec == NULL)
150                 RETURN (-EFAULT);
151
152         r->ur_fsuid = rec->cr_fsuid;
153         r->ur_fsgid = rec->cr_fsgid;
154         r->ur_cap = rec->cr_cap;
155         r->ur_fid1 = &rec->cr_fid;
156         r->ur_fid2 = &rec->cr_replayfid;
157         r->ur_mode = rec->cr_mode;
158         r->ur_rdev = rec->cr_rdev;
159         r->ur_time = rec->cr_time;
160         r->ur_flags = rec->cr_flags;
161         r->ur_suppgid1 = rec->cr_suppgid;
162         r->ur_suppgid2 = -1;
163
164         LASSERT_REQSWAB (req, offset + 1);
165         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
166         if (r->ur_name == NULL)
167                 RETURN (-EFAULT);
168         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
169
170         LASSERT_REQSWAB (req, offset + 2);
171         if (req->rq_reqmsg->bufcount > offset + 2) {
172                 if (S_ISLNK(r->ur_mode)) {
173                         r->ur_tgt = lustre_msg_string(req->rq_reqmsg,
174                                                       offset + 2, 0);
175                         if (r->ur_tgt == NULL)
176                                 RETURN (-EFAULT);
177                         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
178                 } else if (S_ISDIR(r->ur_mode)) {
179                         /* Stripe info for mkdir - just a 16bit integer */
180                         if (req->rq_reqmsg->buflens[offset + 2] != 2) {
181                                 CERROR("mkdir stripe info does not match "
182                                        "expected size %d vs 2\n",
183                                        req->rq_reqmsg->buflens[offset + 2]);
184                                 RETURN (-EINVAL);
185                         }
186                         r->ur_eadata = lustre_swab_buf (req->rq_reqmsg,
187                                                offset + 2, 2, __swab16s);
188                         r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
189                 } else {
190                         /* Hm, no other users so far? */
191                         LBUG();
192                 }
193         }
194         RETURN(0);
195 }
196
197 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
198                            struct mds_update_record *r)
199 {
200         struct mds_rec_link *rec;
201         ENTRY;
202
203         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
204                                   lustre_swab_mds_rec_link);
205         if (rec == NULL)
206                 RETURN (-EFAULT);
207
208         r->ur_fsuid = rec->lk_fsuid;
209         r->ur_fsgid = rec->lk_fsgid;
210         r->ur_cap = rec->lk_cap;
211         r->ur_suppgid1 = rec->lk_suppgid1;
212         r->ur_suppgid2 = rec->lk_suppgid2;
213         r->ur_fid1 = &rec->lk_fid1;
214         r->ur_fid2 = &rec->lk_fid2;
215         r->ur_time = rec->lk_time;
216
217         LASSERT_REQSWAB (req, offset + 1);
218         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
219         if (r->ur_name == NULL)
220                 RETURN (-EFAULT);
221         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
222         RETURN(0);
223 }
224
225 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
226                              struct mds_update_record *r)
227 {
228         struct mds_rec_unlink *rec;
229         ENTRY;
230
231         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
232                                   lustre_swab_mds_rec_unlink);
233         if (rec == NULL)
234                 RETURN(-EFAULT);
235
236         r->ur_fsuid = rec->ul_fsuid;
237         r->ur_fsgid = rec->ul_fsgid;
238         r->ur_cap = rec->ul_cap;
239         r->ur_mode = rec->ul_mode;
240         r->ur_suppgid1 = rec->ul_suppgid;
241         r->ur_suppgid2 = -1;
242         r->ur_fid1 = &rec->ul_fid1;
243         r->ur_fid2 = &rec->ul_fid2;
244         r->ur_time = rec->ul_time;
245
246         LASSERT_REQSWAB (req, offset + 1);
247         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
248         if (r->ur_name == NULL)
249                 RETURN(-EFAULT);
250         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
251         RETURN(0);
252 }
253
254 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
255                              struct mds_update_record *r)
256 {
257         struct mds_rec_rename *rec;
258         ENTRY;
259
260         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
261                                   lustre_swab_mds_rec_unlink);
262         if (rec == NULL)
263                 RETURN(-EFAULT);
264
265         r->ur_fsuid = rec->rn_fsuid;
266         r->ur_fsgid = rec->rn_fsgid;
267         r->ur_cap = rec->rn_cap;
268         r->ur_suppgid1 = rec->rn_suppgid1;
269         r->ur_suppgid2 = rec->rn_suppgid2;
270         r->ur_fid1 = &rec->rn_fid1;
271         r->ur_fid2 = &rec->rn_fid2;
272         r->ur_time = rec->rn_time;
273
274         LASSERT_REQSWAB (req, offset + 1);
275         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
276         if (r->ur_name == NULL)
277                 RETURN(-EFAULT);
278         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
279
280         LASSERT_REQSWAB (req, offset + 2);
281         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
282         if (r->ur_tgt == NULL)
283                 RETURN(-EFAULT);
284         r->ur_tgtlen = req->rq_reqmsg->buflens[offset + 2];
285         RETURN(0);
286 }
287
288 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
289                            struct mds_update_record *r)
290 {
291         struct mds_rec_create *rec;
292         ENTRY;
293
294         rec = lustre_swab_reqbuf (req, offset, sizeof (*rec),
295                                   lustre_swab_mds_rec_create);
296         if (rec == NULL)
297                 RETURN (-EFAULT);
298
299         r->ur_fsuid = rec->cr_fsuid;
300         r->ur_fsgid = rec->cr_fsgid;
301         r->ur_cap = rec->cr_cap;
302         r->ur_fid1 = &rec->cr_fid;
303         r->ur_fid2 = &rec->cr_replayfid;
304         r->ur_mode = rec->cr_mode;
305         r->ur_rdev = rec->cr_rdev;
306         r->ur_time = rec->cr_time;
307         r->ur_flags = rec->cr_flags;
308         r->ur_suppgid1 = rec->cr_suppgid;
309         r->ur_suppgid2 = -1;
310
311         LASSERT_REQSWAB (req, offset + 1);
312         r->ur_name = lustre_msg_string (req->rq_reqmsg, offset + 1, 0);
313         if (r->ur_name == NULL)
314                 RETURN (-EFAULT);
315         r->ur_namelen = req->rq_reqmsg->buflens[offset + 1];
316
317         LASSERT_REQSWAB (req, offset + 2);
318         if (req->rq_reqmsg->bufcount > offset + 2) {
319                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
320                 if (r->ur_eadata == NULL)
321                         RETURN (-EFAULT);
322                 r->ur_eadatalen = req->rq_reqmsg->buflens[offset + 2];
323         }
324         RETURN(0);
325 }
326
327 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
328                                struct mds_update_record *r);
329
330 static update_unpacker mds_unpackers[REINT_MAX + 1] = {
331         [REINT_SETATTR] mds_setattr_unpack,
332         [REINT_CREATE] mds_create_unpack,
333         [REINT_LINK] mds_link_unpack,
334         [REINT_UNLINK] mds_unlink_unpack,
335         [REINT_RENAME] mds_rename_unpack,
336         [REINT_OPEN] mds_open_unpack,
337 };
338
339 int mds_update_unpack(struct ptlrpc_request *req, int offset,
340                       struct mds_update_record *rec)
341 {
342         __u32 *opcodep;
343         __u32  opcode;
344         int rc;
345         ENTRY;
346
347         /* NB don't lustre_swab_reqbuf() here.  We're just taking a peek
348          * and we want to leave it to the specific unpacker once we've
349          * identified the message type */
350         opcodep = lustre_msg_buf (req->rq_reqmsg, offset, sizeof (*opcodep));
351         if (opcodep == NULL)
352                 RETURN(-EFAULT);
353
354         opcode = *opcodep;
355         if (lustre_msg_swabbed (req->rq_reqmsg))
356                 __swab32s (&opcode);
357
358         if (opcode > REINT_MAX ||
359             mds_unpackers[opcode] == NULL) {
360                 CERROR ("Unexpected opcode %d\n", opcode);
361                 RETURN(-EFAULT);
362         }
363
364         rec->ur_opcode = opcode;
365         rc = mds_unpackers[opcode](req, offset, rec);
366         RETURN(rc);
367 }