Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #define DEBUG_SUBSYSTEM S_MDS
26
27 #ifdef HAVE_KERNEL_CONFIG_H
28 #include <linux/config.h>
29 #endif
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/version.h>
37 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
38 # include <linux/locks.h>   // for wait_on_buffer
39 #else
40 # include <linux/buffer_head.h>   // for wait_on_buffer
41 #endif
42 #include <linux/unistd.h>
43
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/fs.h>
48 #include <linux/stat.h>
49 #include <asm/uaccess.h>
50 #include <linux/slab.h>
51 #include <asm/segment.h>
52
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include "mds_internal.h"
56
57 void mds_pack_inode2fid(struct ll_fid *fid, struct inode *inode)
58 {
59         fid->id = inode->i_ino;
60         fid->generation = inode->i_generation;
61         fid->f_type = (S_IFMT & inode->i_mode);
62 }
63
64 /* Note that we can copy all of the fields, just some will not be "valid" */
65 void mds_pack_inode2body(struct mds_body *b, struct inode *inode)
66 {
67         b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
68                     OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
69                     OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
70                     OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
71
72         if (!S_ISREG(inode->i_mode))
73                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLATIME |
74                             OBD_MD_FLMTIME | OBD_MD_FLRDEV;
75
76         b->ino = inode->i_ino;
77         b->atime = LTIME_S(inode->i_atime);
78         b->mtime = LTIME_S(inode->i_mtime);
79         b->ctime = LTIME_S(inode->i_ctime);
80         b->mode = inode->i_mode;
81         b->size = inode->i_size;
82         b->blocks = inode->i_blocks;
83         b->uid = inode->i_uid;
84         b->gid = inode->i_gid;
85         b->flags = ll_inode_to_ext_flags(b->flags, inode->i_flags);
86         b->rdev = inode->i_rdev;
87         /* Return the correct link count for orphan inodes */
88         b->nlink = mds_inode_is_orphan(inode) ? 0 : inode->i_nlink;
89         b->generation = inode->i_generation;
90         b->suppgid = -1;
91 }
92
93 /* unpacking */
94 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
95                               struct mds_update_record *r)
96 {
97         struct iattr *attr = &r->ur_iattr;
98         struct mds_rec_setattr *rec;
99         ENTRY;
100
101         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
102                                  lustre_swab_mds_rec_setattr);
103         if (rec == NULL)
104                 RETURN (-EFAULT);
105
106         r->ur_uc.luc_fsuid = rec->sa_fsuid;
107         r->ur_uc.luc_fsgid = rec->sa_fsgid;
108         r->ur_uc.luc_cap = rec->sa_cap;
109 #if 0
110         r->ur_uc.luc_suppgid1 = rec->sa_suppgid;
111         r->ur_uc.luc_suppgid2 = -1;
112 #endif
113         r->ur_fid1 = &rec->sa_fid;
114         attr->ia_valid = rec->sa_valid;
115         attr->ia_mode = rec->sa_mode;
116         attr->ia_uid = rec->sa_uid;
117         attr->ia_gid = rec->sa_gid;
118         attr->ia_size = rec->sa_size;
119         LTIME_S(attr->ia_atime) = rec->sa_atime;
120         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
121         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
122         r->ur_flags = rec->sa_attr_flags;
123
124         LASSERT_REQSWAB (req, offset + 1);
125         if (lustre_msg_bufcount(req->rq_reqmsg) > offset + 1) {
126                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 1, 0);
127                 if (r->ur_eadata == NULL)
128                         RETURN(-EFAULT);
129                 r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
130         }
131
132         if (lustre_msg_bufcount(req->rq_reqmsg) > offset + 2) {
133                 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
134                 if (r->ur_eadata == NULL)
135                         RETURN (-EFAULT);
136
137                 r->ur_cookielen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
138         }
139
140         RETURN(0);
141 }
142
143 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
144                              struct mds_update_record *r)
145 {
146         struct mds_rec_create *rec;
147         ENTRY;
148
149         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
150                                  lustre_swab_mds_rec_create);
151         if (rec == NULL)
152                 RETURN (-EFAULT);
153
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;
157 #if 0
158         r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
159         r->ur_uc.luc_suppgid2 = -1;
160 #endif
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;
167
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)
171                 RETURN (-EFAULT);
172         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
173
174         LASSERT_REQSWAB(req, offset + 2);
175         if (lustre_msg_bufcount(req->rq_reqmsg) > offset + 2) {
176                 /* NB for now, we only seem to pass NULL terminated symlink
177                  * target strings here.  If this ever changes, we'll have
178                  * to stop checking for a buffer filled completely with a
179                  * NULL terminated string here, and make the callers check
180                  * depending on what they expect.  We should probably stash
181                  * it in r->ur_eadata in that case, so it's obvious... -eeb
182                  */
183                 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
184                 if (r->ur_tgt == NULL)
185                         RETURN (-EFAULT);
186                 r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
187         }
188         RETURN(0);
189 }
190
191 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
192                            struct mds_update_record *r)
193 {
194         struct mds_rec_link *rec;
195         ENTRY;
196
197         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
198                                  lustre_swab_mds_rec_link);
199         if (rec == NULL)
200                 RETURN (-EFAULT);
201
202         r->ur_uc.luc_fsuid = rec->lk_fsuid;
203         r->ur_uc.luc_fsgid = rec->lk_fsgid;
204         r->ur_uc.luc_cap = rec->lk_cap;
205 #if 0
206         r->ur_uc.luc_suppgid1 = rec->lk_suppgid1;
207         r->ur_uc.luc_suppgid2 = rec->lk_suppgid2;
208 #endif
209         r->ur_fid1 = &rec->lk_fid1;
210         r->ur_fid2 = &rec->lk_fid2;
211         r->ur_time = rec->lk_time;
212
213         LASSERT_REQSWAB(req, offset + 1);
214         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
215         if (r->ur_name == NULL)
216                 RETURN (-EFAULT);
217         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
218         RETURN(0);
219 }
220
221 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
222                              struct mds_update_record *r)
223 {
224         struct mds_rec_unlink *rec;
225         ENTRY;
226
227         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
228                                  lustre_swab_mds_rec_unlink);
229         if (rec == NULL)
230                 RETURN(-EFAULT);
231
232         r->ur_uc.luc_fsuid = rec->ul_fsuid;
233         r->ur_uc.luc_fsgid = rec->ul_fsgid;
234         r->ur_uc.luc_cap = rec->ul_cap;
235 #if 0
236         r->ur_uc.luc_suppgid1 = rec->ul_suppgid;
237         r->ur_uc.luc_suppgid2 = -1;
238 #endif
239         r->ur_mode = rec->ul_mode;
240         r->ur_fid1 = &rec->ul_fid1;
241         r->ur_fid2 = &rec->ul_fid2;
242         r->ur_time = rec->ul_time;
243
244         LASSERT_REQSWAB(req, offset + 1);
245         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
246         if (r->ur_name == NULL)
247                 RETURN(-EFAULT);
248         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
249         RETURN(0);
250 }
251
252 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
253                              struct mds_update_record *r)
254 {
255         struct mds_rec_rename *rec;
256         ENTRY;
257
258         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
259                                  lustre_swab_mds_rec_rename);
260         if (rec == NULL)
261                 RETURN(-EFAULT);
262
263         r->ur_uc.luc_fsuid = rec->rn_fsuid;
264         r->ur_uc.luc_fsgid = rec->rn_fsgid;
265         r->ur_uc.luc_cap = rec->rn_cap;
266 #if 0
267         r->ur_uc.luc_suppgid1 = rec->rn_suppgid1;
268         r->ur_uc.luc_suppgid2 = rec->rn_suppgid2;
269 #endif
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 = lustre_msg_buflen(req->rq_reqmsg, 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 = lustre_msg_buflen(req->rq_reqmsg, 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_uc.luc_fsuid = rec->cr_fsuid;
300         r->ur_uc.luc_fsgid = rec->cr_fsgid;
301         r->ur_uc.luc_cap = rec->cr_cap;
302 #if 0
303         r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
304         r->ur_uc.luc_suppgid2 = -1;
305 #endif
306         r->ur_fid1 = &rec->cr_fid;
307         r->ur_fid2 = &rec->cr_replayfid;
308         r->ur_mode = rec->cr_mode;
309         r->ur_rdev = rec->cr_rdev;
310         r->ur_time = rec->cr_time;
311         r->ur_flags = rec->cr_flags;
312
313         LASSERT_REQSWAB(req, offset + 1);
314         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
315         if (r->ur_name == NULL)
316                 RETURN(-EFAULT);
317         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
318
319         LASSERT_REQSWAB(req, offset + 2);
320         if (lustre_msg_bufcount(req->rq_reqmsg) > offset + 2) {
321                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
322                 if (r->ur_eadata == NULL)
323                         RETURN (-EFAULT);
324                 r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
325         }
326         RETURN(0);
327 }
328
329 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
330                                struct mds_update_record *r);
331
332 static update_unpacker mds_unpackers[REINT_MAX] = {
333         [REINT_SETATTR] mds_setattr_unpack,
334         [REINT_CREATE] mds_create_unpack,
335         [REINT_LINK] mds_link_unpack,
336         [REINT_UNLINK] mds_unlink_unpack,
337         [REINT_RENAME] mds_rename_unpack,
338         [REINT_OPEN] mds_open_unpack,
339 };
340
341 int mds_update_unpack(struct ptlrpc_request *req, int offset,
342                       struct mds_update_record *rec)
343 {
344         mds_reint_t opcode, *opcodep;
345         int rc;
346         ENTRY;
347
348         /* NB don't lustre_swab_reqbuf() here.  We're just taking a peek
349          * and we want to leave it to the specific unpacker once we've
350          * identified the message type */
351         opcodep = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*opcodep));
352         if (opcodep == NULL)
353                 RETURN(-EFAULT);
354
355         opcode = *opcodep;
356         if (lustre_msg_swabbed(req->rq_reqmsg))
357                 __swab32s(&opcode);
358
359         if (opcode >= REINT_MAX || 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
367         RETURN(rc);
368 }
369
370 int mds_init_ucred(struct lvfs_ucred *ucred, struct ptlrpc_request *req,
371                    int offset)
372 {
373         struct mds_body *body = lustre_msg_buf(req->rq_reqmsg, offset,
374                                                sizeof(*body));
375 #if 0
376         struct mds_obd *mds = mds_req2mds(req);
377         int rc;
378 #endif
379
380         LASSERT(body != NULL); /* previously verified & swabbed by caller */
381
382 #ifdef CRAY_XT3
383         if (req->rq_uid != LNET_UID_ANY) {
384                 /* Non-root local cluster client */
385                 LASSERT (req->rq_uid != 0);
386                 ucred->luc_fsuid = req->rq_uid;
387         } else
388 #endif
389         {
390                 ucred->luc_fsuid = body->fsuid;
391                 ucred->luc_fsgid = body->fsgid;
392                 ucred->luc_cap = body->capability;
393         }
394
395 #if 0
396         ucred->luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
397                                                 ucred->luc_fsuid,
398                                                 ucred->luc_fsgid, 1,
399                                                 &body->suppgid);
400         if (IS_ERR(ucred->luc_uce)) {
401                 rc = PTR_ERR(ucred->luc_uce);
402                 ucred->luc_uce = NULL;
403                 return rc;
404         }
405
406 #ifdef CRAY_XT3
407         if (ucred->luc_uce)
408                 ucred->luc_fsgid = ucred->luc_uce->ue_primary;
409 #endif
410 #endif
411
412         return 0;
413 }
414
415 void mds_exit_ucred(struct lvfs_ucred *ucred, struct mds_obd *mds)
416 {
417 #if 0
418         upcall_cache_put_entry(mds->mds_group_hash, ucred->luc_uce);
419 #endif
420 }