Whamcloud - gitweb
83203dedac0e36438753efdcb1df7e3f289de756
[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 #ifndef AUTOCONF_INCLUDED
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 = i_size_read(inode);
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         r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
126         if (r->ur_eadatalen) {
127                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 1, 0);
128                 if (r->ur_eadata == NULL)
129                         RETURN(-EFAULT);
130         }
131         r->ur_cookielen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
132         if (r->ur_cookielen) {
133                 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
134                 if (r->ur_eadata == NULL)
135                         RETURN (-EFAULT);
136         }
137         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
138                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
139                                                sizeof(*r->ur_dlm),
140                                                lustre_swab_ldlm_request); 
141                 if (r->ur_dlm == NULL)
142                         RETURN (-EFAULT);
143         }
144         RETURN(0);
145 }
146
147 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
148                              struct mds_update_record *r)
149 {
150         struct mds_rec_create *rec;
151         ENTRY;
152
153         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
154                                  lustre_swab_mds_rec_create);
155         if (rec == NULL)
156                 RETURN (-EFAULT);
157
158         r->ur_uc.luc_fsuid = rec->cr_fsuid;
159         r->ur_uc.luc_fsgid = rec->cr_fsgid;
160         r->ur_uc.luc_cap = rec->cr_cap;
161 #if 0
162         r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
163         r->ur_uc.luc_suppgid2 = -1;
164 #endif
165         r->ur_fid1 = &rec->cr_fid;
166         r->ur_fid2 = &rec->cr_replayfid;
167         r->ur_mode = rec->cr_mode;
168         r->ur_rdev = rec->cr_rdev;
169         r->ur_time = rec->cr_time;
170         r->ur_flags = rec->cr_flags;
171
172         LASSERT_REQSWAB(req, offset + 1);
173         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
174         if (r->ur_name == NULL)
175                 RETURN (-EFAULT);
176         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
177
178         LASSERT_REQSWAB(req, offset + 2);
179         r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
180         if (r->ur_tgtlen) {
181                 /* NB for now, we only seem to pass NULL terminated symlink
182                  * target strings here.  If this ever changes, we'll have
183                  * to stop checking for a buffer filled completely with a
184                  * NULL terminated string here, and make the callers check
185                  * depending on what they expect.  We should probably stash
186                  * it in r->ur_eadata in that case, so it's obvious... -eeb
187                  */
188                 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
189                 if (r->ur_tgt == NULL)
190                         RETURN (-EFAULT);
191         }
192         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
193                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
194                                                sizeof(*r->ur_dlm),
195                                                lustre_swab_ldlm_request); 
196                 if (r->ur_dlm == NULL)
197                         RETURN (-EFAULT);
198         }
199         RETURN(0);
200 }
201
202 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
203                            struct mds_update_record *r)
204 {
205         struct mds_rec_link *rec;
206         ENTRY;
207
208         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
209                                  lustre_swab_mds_rec_link);
210         if (rec == NULL)
211                 RETURN (-EFAULT);
212
213         r->ur_uc.luc_fsuid = rec->lk_fsuid;
214         r->ur_uc.luc_fsgid = rec->lk_fsgid;
215         r->ur_uc.luc_cap = rec->lk_cap;
216 #if 0
217         r->ur_uc.luc_suppgid1 = rec->lk_suppgid1;
218         r->ur_uc.luc_suppgid2 = rec->lk_suppgid2;
219 #endif
220         r->ur_fid1 = &rec->lk_fid1;
221         r->ur_fid2 = &rec->lk_fid2;
222         r->ur_time = rec->lk_time;
223
224         LASSERT_REQSWAB(req, offset + 1);
225         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
226         if (r->ur_name == NULL)
227                 RETURN (-EFAULT);
228         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
229         if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
230                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
231                                                sizeof(*r->ur_dlm),
232                                                lustre_swab_ldlm_request); 
233                 if (r->ur_dlm == NULL)
234                         RETURN (-EFAULT);
235         }
236         RETURN(0);
237 }
238
239 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
240                              struct mds_update_record *r)
241 {
242         struct mds_rec_unlink *rec;
243         ENTRY;
244
245         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
246                                  lustre_swab_mds_rec_unlink);
247         if (rec == NULL)
248                 RETURN(-EFAULT);
249
250         r->ur_uc.luc_fsuid = rec->ul_fsuid;
251         r->ur_uc.luc_fsgid = rec->ul_fsgid;
252         r->ur_uc.luc_cap = rec->ul_cap;
253 #if 0
254         r->ur_uc.luc_suppgid1 = rec->ul_suppgid;
255         r->ur_uc.luc_suppgid2 = -1;
256 #endif
257         r->ur_mode = rec->ul_mode;
258         r->ur_fid1 = &rec->ul_fid1;
259         r->ur_fid2 = &rec->ul_fid2;
260         r->ur_time = rec->ul_time;
261
262         LASSERT_REQSWAB(req, offset + 1);
263         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
264         if (r->ur_name == NULL)
265                 RETURN(-EFAULT);
266         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
267         
268         if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
269                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
270                                                sizeof(*r->ur_dlm),
271                                                lustre_swab_ldlm_request); 
272                 if (r->ur_dlm == NULL)
273                         RETURN (-EFAULT);
274         }
275         RETURN(0);
276 }
277
278 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
279                              struct mds_update_record *r)
280 {
281         struct mds_rec_rename *rec;
282         ENTRY;
283
284         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
285                                  lustre_swab_mds_rec_rename);
286         if (rec == NULL)
287                 RETURN(-EFAULT);
288
289         r->ur_uc.luc_fsuid = rec->rn_fsuid;
290         r->ur_uc.luc_fsgid = rec->rn_fsgid;
291         r->ur_uc.luc_cap = rec->rn_cap;
292 #if 0
293         r->ur_uc.luc_suppgid1 = rec->rn_suppgid1;
294         r->ur_uc.luc_suppgid2 = rec->rn_suppgid2;
295 #endif
296         r->ur_fid1 = &rec->rn_fid1;
297         r->ur_fid2 = &rec->rn_fid2;
298         r->ur_time = rec->rn_time;
299
300         LASSERT_REQSWAB (req, offset + 1);
301         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
302         if (r->ur_name == NULL)
303                 RETURN(-EFAULT);
304         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
305
306         LASSERT_REQSWAB (req, offset + 2);
307         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
308         if (r->ur_tgt == NULL)
309                 RETURN(-EFAULT);
310         r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
311         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
312                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
313                                                sizeof(*r->ur_dlm),
314                                                lustre_swab_ldlm_request); 
315                 if (r->ur_dlm == NULL)
316                         RETURN (-EFAULT);
317         }
318         RETURN(0);
319 }
320
321 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
322                            struct mds_update_record *r)
323 {
324         struct mds_rec_create *rec;
325         ENTRY;
326
327         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
328                                  lustre_swab_mds_rec_create);
329         if (rec == NULL)
330                 RETURN(-EFAULT);
331
332         r->ur_uc.luc_fsuid = rec->cr_fsuid;
333         r->ur_uc.luc_fsgid = rec->cr_fsgid;
334         r->ur_uc.luc_cap = rec->cr_cap;
335 #if 0
336         r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
337         r->ur_uc.luc_suppgid2 = -1;
338 #endif
339         r->ur_fid1 = &rec->cr_fid;
340         r->ur_fid2 = &rec->cr_replayfid;
341         r->ur_mode = rec->cr_mode;
342         r->ur_rdev = rec->cr_rdev;
343         r->ur_time = rec->cr_time;
344         r->ur_flags = rec->cr_flags;
345
346         LASSERT_REQSWAB(req, offset + 1);
347         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
348         if (r->ur_name == NULL)
349                 RETURN(-EFAULT);
350         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
351
352         LASSERT_REQSWAB(req, offset + 2);
353         r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
354         if (r->ur_eadatalen) {
355                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
356                 if (r->ur_eadata == NULL)
357                         RETURN (-EFAULT);
358         }
359         RETURN(0);
360 }
361
362 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
363                                struct mds_update_record *r);
364
365 static update_unpacker mds_unpackers[REINT_MAX] = {
366         [REINT_SETATTR] mds_setattr_unpack,
367         [REINT_CREATE] mds_create_unpack,
368         [REINT_LINK] mds_link_unpack,
369         [REINT_UNLINK] mds_unlink_unpack,
370         [REINT_RENAME] mds_rename_unpack,
371         [REINT_OPEN] mds_open_unpack,
372 };
373
374 int mds_update_unpack(struct ptlrpc_request *req, int offset,
375                       struct mds_update_record *rec)
376 {
377         mds_reint_t opcode, *opcodep;
378         int rc;
379         ENTRY;
380
381         /* NB don't lustre_swab_reqbuf() here.  We're just taking a peek
382          * and we want to leave it to the specific unpacker once we've
383          * identified the message type */
384         opcodep = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*opcodep));
385         if (opcodep == NULL)
386                 RETURN(-EFAULT);
387
388         opcode = *opcodep;
389         if (lustre_msg_swabbed(req->rq_reqmsg))
390                 __swab32s(&opcode);
391
392         if (opcode >= REINT_MAX || mds_unpackers[opcode] == NULL) {
393                 CERROR("Unexpected opcode %d\n", opcode);
394                 RETURN(-EFAULT);
395         }
396
397         rec->ur_opcode = opcode;
398         rc = mds_unpackers[opcode](req, offset, rec);
399
400         RETURN(rc);
401 }
402
403 int mds_init_ucred(struct lvfs_ucred *ucred, struct ptlrpc_request *req,
404                    int offset)
405 {
406         struct mds_body *body = lustre_msg_buf(req->rq_reqmsg, offset,
407                                                sizeof(*body));
408 #if 0
409         struct mds_obd *mds = mds_req2mds(req);
410         int rc;
411 #endif
412
413         LASSERT(body != NULL); /* previously verified & swabbed by caller */
414
415 #ifdef CRAY_XT3
416         if (req->rq_uid != LNET_UID_ANY) {
417                 /* Non-root local cluster client */
418                 LASSERT (req->rq_uid != 0);
419                 ucred->luc_fsuid = req->rq_uid;
420         } else
421 #endif
422         {
423                 ucred->luc_fsuid = body->fsuid;
424                 ucred->luc_fsgid = body->fsgid;
425                 ucred->luc_cap = body->capability;
426         }
427
428 #if 0
429         ucred->luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
430                                                 ucred->luc_fsuid,
431                                                 ucred->luc_fsgid, 1,
432                                                 &body->suppgid);
433         if (IS_ERR(ucred->luc_uce)) {
434                 rc = PTR_ERR(ucred->luc_uce);
435                 ucred->luc_uce = NULL;
436                 return rc;
437         }
438
439 #ifdef CRAY_XT3
440         if (ucred->luc_uce)
441                 ucred->luc_fsgid = ucred->luc_uce->ue_primary;
442 #endif
443 #endif
444
445         return 0;
446 }
447
448 void mds_exit_ucred(struct lvfs_ucred *ucred, struct mds_obd *mds)
449 {
450 #if 0
451         upcall_cache_put_entry(mds->mds_group_hash, ucred->luc_uce);
452 #endif
453 }