Whamcloud - gitweb
b423941e597749a72f3d5cb50ac323707c9e374e
[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 #include <linux/buffer_head.h>   // for wait_on_buffer
38 #include <linux/unistd.h>
39
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42
43 #include <linux/fs.h>
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <linux/slab.h>
47 #include <asm/segment.h>
48
49 #include <obd_support.h>
50 #include <lustre_lib.h>
51 #include "mds_internal.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 /* Note that we can copy all of the fields, just some will not be "valid" */
61 void mds_pack_inode2body(struct mds_body *b, struct inode *inode)
62 {
63         b->valid |= OBD_MD_FLID | OBD_MD_FLCTIME | OBD_MD_FLUID |
64                     OBD_MD_FLGID | OBD_MD_FLFLAGS | OBD_MD_FLTYPE |
65                     OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLGENER |
66                     OBD_MD_FLATIME | OBD_MD_FLMTIME; /* bug 2020 */
67
68         if (!S_ISREG(inode->i_mode))
69                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLATIME |
70                             OBD_MD_FLMTIME | OBD_MD_FLRDEV;
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 = i_size_read(inode);
78         b->blocks = inode->i_blocks;
79         b->uid = inode->i_uid;
80         b->gid = inode->i_gid;
81         b->flags = ll_inode_to_ext_flags(b->flags, inode->i_flags);
82         b->rdev = inode->i_rdev;
83         /* Return the correct link count for orphan inodes */
84         b->nlink = mds_inode_is_orphan(inode) ? 0 : inode->i_nlink;
85         b->generation = inode->i_generation;
86         b->suppgid = -1;
87 }
88
89 /* unpacking */
90 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
91                               struct mds_update_record *r)
92 {
93         struct iattr *attr = &r->ur_iattr;
94         struct mds_rec_setattr *rec;
95         ENTRY;
96
97         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
98                                  lustre_swab_mds_rec_setattr);
99         if (rec == NULL)
100                 RETURN (-EFAULT);
101
102         r->ur_uc.luc_fsuid = rec->sa_fsuid;
103         r->ur_uc.luc_fsgid = rec->sa_fsgid;
104         r->ur_uc.luc_cap = rec->sa_cap;
105 #if 0
106         r->ur_uc.luc_suppgid1 = rec->sa_suppgid;
107         r->ur_uc.luc_suppgid2 = -1;
108 #endif
109         r->ur_fid1 = &rec->sa_fid;
110         attr->ia_valid = rec->sa_valid;
111         attr->ia_mode = rec->sa_mode;
112         attr->ia_uid = rec->sa_uid;
113         attr->ia_gid = rec->sa_gid;
114         attr->ia_size = rec->sa_size;
115         LTIME_S(attr->ia_atime) = rec->sa_atime;
116         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
117         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
118         r->ur_flags = rec->sa_attr_flags;
119
120         LASSERT_REQSWAB (req, offset + 1);
121         r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
122         if (r->ur_eadatalen) {
123                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 1, 0);
124                 if (r->ur_eadata == NULL)
125                         RETURN(-EFAULT);
126         }
127         r->ur_cookielen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
128         if (r->ur_cookielen) {
129                 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
130                 if (r->ur_eadata == NULL)
131                         RETURN (-EFAULT);
132         }
133         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
134                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
135                                                sizeof(*r->ur_dlm),
136                                                lustre_swab_ldlm_request); 
137                 if (r->ur_dlm == NULL)
138                         RETURN (-EFAULT);
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         r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
176         if (r->ur_tgtlen) {
177                 /* NB for now, we only seem to pass NULL terminated symlink
178                  * target strings here.  If this ever changes, we'll have
179                  * to stop checking for a buffer filled completely with a
180                  * NULL terminated string here, and make the callers check
181                  * depending on what they expect.  We should probably stash
182                  * it in r->ur_eadata in that case, so it's obvious... -eeb
183                  */
184                 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
185                 if (r->ur_tgt == NULL)
186                         RETURN (-EFAULT);
187         }
188         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
189                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
190                                                sizeof(*r->ur_dlm),
191                                                lustre_swab_ldlm_request); 
192                 if (r->ur_dlm == NULL)
193                         RETURN (-EFAULT);
194         }
195         RETURN(0);
196 }
197
198 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
199                            struct mds_update_record *r)
200 {
201         struct mds_rec_link *rec;
202         ENTRY;
203
204         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
205                                  lustre_swab_mds_rec_link);
206         if (rec == NULL)
207                 RETURN (-EFAULT);
208
209         r->ur_uc.luc_fsuid = rec->lk_fsuid;
210         r->ur_uc.luc_fsgid = rec->lk_fsgid;
211         r->ur_uc.luc_cap = rec->lk_cap;
212 #if 0
213         r->ur_uc.luc_suppgid1 = rec->lk_suppgid1;
214         r->ur_uc.luc_suppgid2 = rec->lk_suppgid2;
215 #endif
216         r->ur_fid1 = &rec->lk_fid1;
217         r->ur_fid2 = &rec->lk_fid2;
218         r->ur_time = rec->lk_time;
219
220         LASSERT_REQSWAB(req, offset + 1);
221         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
222         if (r->ur_name == NULL)
223                 RETURN (-EFAULT);
224         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
225         if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
226                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
227                                                sizeof(*r->ur_dlm),
228                                                lustre_swab_ldlm_request); 
229                 if (r->ur_dlm == NULL)
230                         RETURN (-EFAULT);
231         }
232         RETURN(0);
233 }
234
235 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
236                              struct mds_update_record *r)
237 {
238         struct mds_rec_unlink *rec;
239         ENTRY;
240
241         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
242                                  lustre_swab_mds_rec_unlink);
243         if (rec == NULL)
244                 RETURN(-EFAULT);
245
246         r->ur_uc.luc_fsuid = rec->ul_fsuid;
247         r->ur_uc.luc_fsgid = rec->ul_fsgid;
248         r->ur_uc.luc_cap = rec->ul_cap;
249 #if 0
250         r->ur_uc.luc_suppgid1 = rec->ul_suppgid;
251         r->ur_uc.luc_suppgid2 = -1;
252 #endif
253         r->ur_mode = rec->ul_mode;
254         r->ur_fid1 = &rec->ul_fid1;
255         r->ur_fid2 = &rec->ul_fid2;
256         r->ur_time = rec->ul_time;
257
258         LASSERT_REQSWAB(req, offset + 1);
259         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
260         if (r->ur_name == NULL)
261                 RETURN(-EFAULT);
262         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
263         
264         if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
265                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
266                                                sizeof(*r->ur_dlm),
267                                                lustre_swab_ldlm_request); 
268                 if (r->ur_dlm == NULL)
269                         RETURN (-EFAULT);
270         }
271         RETURN(0);
272 }
273
274 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
275                              struct mds_update_record *r)
276 {
277         struct mds_rec_rename *rec;
278         ENTRY;
279
280         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
281                                  lustre_swab_mds_rec_rename);
282         if (rec == NULL)
283                 RETURN(-EFAULT);
284
285         r->ur_uc.luc_fsuid = rec->rn_fsuid;
286         r->ur_uc.luc_fsgid = rec->rn_fsgid;
287         r->ur_uc.luc_cap = rec->rn_cap;
288 #if 0
289         r->ur_uc.luc_suppgid1 = rec->rn_suppgid1;
290         r->ur_uc.luc_suppgid2 = rec->rn_suppgid2;
291 #endif
292         r->ur_fid1 = &rec->rn_fid1;
293         r->ur_fid2 = &rec->rn_fid2;
294         r->ur_time = rec->rn_time;
295
296         LASSERT_REQSWAB (req, offset + 1);
297         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
298         if (r->ur_name == NULL)
299                 RETURN(-EFAULT);
300         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
301
302         LASSERT_REQSWAB (req, offset + 2);
303         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
304         if (r->ur_tgt == NULL)
305                 RETURN(-EFAULT);
306         r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
307         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
308                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
309                                                sizeof(*r->ur_dlm),
310                                                lustre_swab_ldlm_request); 
311                 if (r->ur_dlm == NULL)
312                         RETURN (-EFAULT);
313         }
314         RETURN(0);
315 }
316
317 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
318                            struct mds_update_record *r)
319 {
320         struct mds_rec_create *rec;
321         ENTRY;
322
323         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
324                                  lustre_swab_mds_rec_create);
325         if (rec == NULL)
326                 RETURN(-EFAULT);
327
328         r->ur_uc.luc_fsuid = rec->cr_fsuid;
329         r->ur_uc.luc_fsgid = rec->cr_fsgid;
330         r->ur_uc.luc_cap = rec->cr_cap;
331 #if 0
332         r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
333         r->ur_uc.luc_suppgid2 = -1;
334 #endif
335         r->ur_fid1 = &rec->cr_fid;
336         r->ur_fid2 = &rec->cr_replayfid;
337         r->ur_mode = rec->cr_mode;
338         r->ur_rdev = rec->cr_rdev;
339         r->ur_time = rec->cr_time;
340         r->ur_flags = rec->cr_flags;
341
342         LASSERT_REQSWAB(req, offset + 1);
343         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
344         if (r->ur_name == NULL)
345                 RETURN(-EFAULT);
346         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
347
348         LASSERT_REQSWAB(req, offset + 2);
349         r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
350         if (r->ur_eadatalen) {
351                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
352                 if (r->ur_eadata == NULL)
353                         RETURN (-EFAULT);
354         }
355         RETURN(0);
356 }
357
358 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
359                                struct mds_update_record *r);
360
361 static update_unpacker mds_unpackers[REINT_MAX] = {
362         [REINT_SETATTR] mds_setattr_unpack,
363         [REINT_CREATE] mds_create_unpack,
364         [REINT_LINK] mds_link_unpack,
365         [REINT_UNLINK] mds_unlink_unpack,
366         [REINT_RENAME] mds_rename_unpack,
367         [REINT_OPEN] mds_open_unpack,
368 };
369
370 int mds_update_unpack(struct ptlrpc_request *req, int offset,
371                       struct mds_update_record *rec)
372 {
373         mds_reint_t opcode, *opcodep;
374         int rc;
375         ENTRY;
376
377         /* NB don't lustre_swab_reqbuf() here.  We're just taking a peek
378          * and we want to leave it to the specific unpacker once we've
379          * identified the message type */
380         opcodep = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*opcodep));
381         if (opcodep == NULL)
382                 RETURN(-EFAULT);
383
384         opcode = *opcodep;
385         if (lustre_msg_swabbed(req->rq_reqmsg))
386                 __swab32s(&opcode);
387
388         if (opcode >= REINT_MAX || mds_unpackers[opcode] == NULL) {
389                 CERROR("Unexpected opcode %d\n", opcode);
390                 RETURN(-EFAULT);
391         }
392
393         rec->ur_opcode = opcode;
394         rc = mds_unpackers[opcode](req, offset, rec);
395
396         RETURN(rc);
397 }
398
399 int mds_init_ucred(struct lvfs_ucred *ucred, struct ptlrpc_request *req,
400                    int offset)
401 {
402         struct mds_body *body = lustre_msg_buf(req->rq_reqmsg, offset,
403                                                sizeof(*body));
404 #if 0
405         struct mds_obd *mds = mds_req2mds(req);
406         int rc;
407 #endif
408
409         LASSERT(body != NULL); /* previously verified & swabbed by caller */
410
411 #ifdef CRAY_XT3
412         if (req->rq_uid != LNET_UID_ANY) {
413                 /* Non-root local cluster client */
414                 LASSERT (req->rq_uid != 0);
415                 ucred->luc_fsuid = req->rq_uid;
416         } else
417 #endif
418         {
419                 ucred->luc_fsuid = body->fsuid;
420                 ucred->luc_fsgid = body->fsgid;
421                 ucred->luc_cap = body->capability;
422         }
423
424 #if 0
425         ucred->luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
426                                                 ucred->luc_fsuid,
427                                                 ucred->luc_fsgid, 1,
428                                                 &body->suppgid);
429         if (IS_ERR(ucred->luc_uce)) {
430                 rc = PTR_ERR(ucred->luc_uce);
431                 ucred->luc_uce = NULL;
432                 return rc;
433         }
434
435 #ifdef CRAY_XT3
436         if (ucred->luc_uce)
437                 ucred->luc_fsgid = ucred->luc_uce->ue_primary;
438 #endif
439 #endif
440
441         return 0;
442 }
443
444 void mds_exit_ucred(struct lvfs_ucred *ucred, struct mds_obd *mds)
445 {
446 #if 0
447         upcall_cache_put_entry(mds->mds_group_hash, ucred->luc_uce);
448 #endif
449 }