Whamcloud - gitweb
Branch HEAD
[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 static inline unsigned int attr_unpack(__u64 sa_valid) {
90         unsigned int ia_valid = 0;
91
92         if (sa_valid & MDS_ATTR_MODE)
93                 ia_valid |= ATTR_MODE;
94         if (sa_valid & MDS_ATTR_UID)
95                 ia_valid |= ATTR_UID;
96         if (sa_valid & MDS_ATTR_GID)
97                 ia_valid |= ATTR_GID;
98         if (sa_valid & MDS_ATTR_SIZE)
99                 ia_valid |= ATTR_SIZE;
100         if (sa_valid & MDS_ATTR_ATIME)
101                 ia_valid |= ATTR_ATIME;
102         if (sa_valid & MDS_ATTR_MTIME)
103                 ia_valid |= ATTR_MTIME;
104         if (sa_valid & MDS_ATTR_CTIME)
105                 ia_valid |= ATTR_CTIME;
106         if (sa_valid & MDS_ATTR_ATIME_SET)
107                 ia_valid |= ATTR_ATIME_SET;
108         if (sa_valid & MDS_ATTR_MTIME_SET)
109                 ia_valid |= ATTR_MTIME_SET;
110         if (sa_valid & MDS_ATTR_FORCE)
111                 ia_valid |= ATTR_FORCE;
112         if (sa_valid & MDS_ATTR_ATTR_FLAG)
113                 ia_valid |= ATTR_ATTR_FLAG;
114         if (sa_valid & MDS_ATTR_KILL_SUID)
115                 ia_valid |=  ATTR_KILL_SUID;
116         if (sa_valid & MDS_ATTR_KILL_SGID)
117                 ia_valid |= ATTR_KILL_SGID;
118         if (sa_valid & MDS_ATTR_CTIME_SET)
119                 ia_valid |= ATTR_CTIME_SET;
120         if (sa_valid & MDS_ATTR_FROM_OPEN)
121                 ia_valid |= ATTR_FROM_OPEN;
122         if (sa_valid & MDS_ATTR_BLOCKS)
123                 ia_valid |= ATTR_BLOCKS;
124         return ia_valid;
125 }
126
127 /* unpacking */
128 static int mds_setattr_unpack(struct ptlrpc_request *req, int offset,
129                               struct mds_update_record *r)
130 {
131         struct iattr *attr = &r->ur_iattr;
132         struct mds_rec_setattr *rec;
133         ENTRY;
134
135         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
136                                  lustre_swab_mds_rec_setattr);
137         if (rec == NULL)
138                 RETURN (-EFAULT);
139
140         r->ur_uc.luc_fsuid = rec->sa_fsuid;
141         r->ur_uc.luc_fsgid = rec->sa_fsgid;
142         r->ur_uc.luc_cap = rec->sa_cap;
143 #if 0
144         r->ur_uc.luc_suppgid1 = rec->sa_suppgid;
145         r->ur_uc.luc_suppgid2 = -1;
146 #endif
147         r->ur_fid1 = &rec->sa_fid;
148         attr->ia_valid = attr_unpack(rec->sa_valid);
149         attr->ia_mode = rec->sa_mode;
150         attr->ia_uid = rec->sa_uid;
151         attr->ia_gid = rec->sa_gid;
152         attr->ia_size = rec->sa_size;
153         LTIME_S(attr->ia_atime) = rec->sa_atime;
154         LTIME_S(attr->ia_mtime) = rec->sa_mtime;
155         LTIME_S(attr->ia_ctime) = rec->sa_ctime;
156         r->ur_flags = rec->sa_attr_flags;
157
158         lustre_set_req_swabbed(req, offset + 1);
159         r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
160         if (r->ur_eadatalen) {
161                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 1, 0);
162                 if (r->ur_eadata == NULL)
163                         RETURN(-EFAULT);
164         }
165         r->ur_cookielen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
166         if (r->ur_cookielen) {
167                 r->ur_logcookies = lustre_msg_buf(req->rq_reqmsg, offset + 2,0);
168                 if (r->ur_eadata == NULL)
169                         RETURN (-EFAULT);
170         }
171         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
172                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
173                                                sizeof(*r->ur_dlm),
174                                                lustre_swab_ldlm_request); 
175                 if (r->ur_dlm == NULL)
176                         RETURN (-EFAULT);
177         }
178         RETURN(0);
179 }
180
181 static int mds_create_unpack(struct ptlrpc_request *req, int offset,
182                              struct mds_update_record *r)
183 {
184         struct mds_rec_create *rec;
185         ENTRY;
186
187         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
188                                  lustre_swab_mds_rec_create);
189         if (rec == NULL)
190                 RETURN (-EFAULT);
191
192         r->ur_uc.luc_fsuid = rec->cr_fsuid;
193         r->ur_uc.luc_fsgid = rec->cr_fsgid;
194         r->ur_uc.luc_cap = rec->cr_cap;
195 #if 0
196         r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
197         r->ur_uc.luc_suppgid2 = -1;
198 #endif
199         r->ur_fid1 = &rec->cr_fid;
200         r->ur_fid2 = &rec->cr_replayfid;
201         r->ur_mode = rec->cr_mode;
202         r->ur_rdev = rec->cr_rdev;
203         r->ur_time = rec->cr_time;
204         r->ur_flags = rec->cr_flags;
205
206         lustre_set_req_swabbed(req, offset + 1);
207         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
208         if (r->ur_name == NULL)
209                 RETURN (-EFAULT);
210         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
211
212         lustre_set_req_swabbed(req, offset + 2);
213         r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
214         if (r->ur_tgtlen) {
215                 /* NB for now, we only seem to pass NULL terminated symlink
216                  * target strings here.  If this ever changes, we'll have
217                  * to stop checking for a buffer filled completely with a
218                  * NULL terminated string here, and make the callers check
219                  * depending on what they expect.  We should probably stash
220                  * it in r->ur_eadata in that case, so it's obvious... -eeb
221                  */
222                 r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
223                 if (r->ur_tgt == NULL)
224                         RETURN (-EFAULT);
225         }
226         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
227                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
228                                                sizeof(*r->ur_dlm),
229                                                lustre_swab_ldlm_request); 
230                 if (r->ur_dlm == NULL)
231                         RETURN (-EFAULT);
232         }
233         RETURN(0);
234 }
235
236 static int mds_link_unpack(struct ptlrpc_request *req, int offset,
237                            struct mds_update_record *r)
238 {
239         struct mds_rec_link *rec;
240         ENTRY;
241
242         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
243                                  lustre_swab_mds_rec_link);
244         if (rec == NULL)
245                 RETURN (-EFAULT);
246
247         r->ur_uc.luc_fsuid = rec->lk_fsuid;
248         r->ur_uc.luc_fsgid = rec->lk_fsgid;
249         r->ur_uc.luc_cap = rec->lk_cap;
250 #if 0
251         r->ur_uc.luc_suppgid1 = rec->lk_suppgid1;
252         r->ur_uc.luc_suppgid2 = rec->lk_suppgid2;
253 #endif
254         r->ur_fid1 = &rec->lk_fid1;
255         r->ur_fid2 = &rec->lk_fid2;
256         r->ur_time = rec->lk_time;
257
258         lustre_set_req_swabbed(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         if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
264                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
265                                                sizeof(*r->ur_dlm),
266                                                lustre_swab_ldlm_request); 
267                 if (r->ur_dlm == NULL)
268                         RETURN (-EFAULT);
269         }
270         RETURN(0);
271 }
272
273 static int mds_unlink_unpack(struct ptlrpc_request *req, int offset,
274                              struct mds_update_record *r)
275 {
276         struct mds_rec_unlink *rec;
277         ENTRY;
278
279         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
280                                  lustre_swab_mds_rec_unlink);
281         if (rec == NULL)
282                 RETURN(-EFAULT);
283
284         r->ur_uc.luc_fsuid = rec->ul_fsuid;
285         r->ur_uc.luc_fsgid = rec->ul_fsgid;
286         r->ur_uc.luc_cap = rec->ul_cap;
287 #if 0
288         r->ur_uc.luc_suppgid1 = rec->ul_suppgid;
289         r->ur_uc.luc_suppgid2 = -1;
290 #endif
291         r->ur_mode = rec->ul_mode;
292         r->ur_fid1 = &rec->ul_fid1;
293         r->ur_fid2 = &rec->ul_fid2;
294         r->ur_time = rec->ul_time;
295
296         lustre_set_req_swabbed(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         if (lustre_msg_buflen(req->rq_reqmsg, offset + 2)) {
303                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 2,
304                                                sizeof(*r->ur_dlm),
305                                                lustre_swab_ldlm_request); 
306                 if (r->ur_dlm == NULL)
307                         RETURN (-EFAULT);
308         }
309         RETURN(0);
310 }
311
312 static int mds_rename_unpack(struct ptlrpc_request *req, int offset,
313                              struct mds_update_record *r)
314 {
315         struct mds_rec_rename *rec;
316         ENTRY;
317
318         rec = lustre_swab_reqbuf(req, offset, sizeof (*rec),
319                                  lustre_swab_mds_rec_rename);
320         if (rec == NULL)
321                 RETURN(-EFAULT);
322
323         r->ur_uc.luc_fsuid = rec->rn_fsuid;
324         r->ur_uc.luc_fsgid = rec->rn_fsgid;
325         r->ur_uc.luc_cap = rec->rn_cap;
326 #if 0
327         r->ur_uc.luc_suppgid1 = rec->rn_suppgid1;
328         r->ur_uc.luc_suppgid2 = rec->rn_suppgid2;
329 #endif
330         r->ur_fid1 = &rec->rn_fid1;
331         r->ur_fid2 = &rec->rn_fid2;
332         r->ur_time = rec->rn_time;
333
334         lustre_set_req_swabbed(req, offset + 1);
335         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
336         if (r->ur_name == NULL)
337                 RETURN(-EFAULT);
338         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
339
340         lustre_set_req_swabbed(req, offset + 2);
341         r->ur_tgt = lustre_msg_string(req->rq_reqmsg, offset + 2, 0);
342         if (r->ur_tgt == NULL)
343                 RETURN(-EFAULT);
344         r->ur_tgtlen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
345         if (lustre_msg_buflen(req->rq_reqmsg, offset + 3)) {
346                 r->ur_dlm = lustre_swab_reqbuf(req, offset + 3,
347                                                sizeof(*r->ur_dlm),
348                                                lustre_swab_ldlm_request); 
349                 if (r->ur_dlm == NULL)
350                         RETURN (-EFAULT);
351         }
352         RETURN(0);
353 }
354
355 static int mds_open_unpack(struct ptlrpc_request *req, int offset,
356                            struct mds_update_record *r)
357 {
358         struct mds_rec_create *rec;
359         ENTRY;
360
361         rec = lustre_swab_reqbuf(req, offset, sizeof(*rec),
362                                  lustre_swab_mds_rec_create);
363         if (rec == NULL)
364                 RETURN(-EFAULT);
365
366         r->ur_uc.luc_fsuid = rec->cr_fsuid;
367         r->ur_uc.luc_fsgid = rec->cr_fsgid;
368         r->ur_uc.luc_cap = rec->cr_cap;
369 #if 0
370         r->ur_uc.luc_suppgid1 = rec->cr_suppgid;
371         r->ur_uc.luc_suppgid2 = -1;
372 #endif
373         r->ur_fid1 = &rec->cr_fid;
374         r->ur_fid2 = &rec->cr_replayfid;
375         r->ur_mode = rec->cr_mode;
376         r->ur_rdev = rec->cr_rdev;
377         r->ur_time = rec->cr_time;
378         r->ur_flags = rec->cr_flags;
379
380         lustre_set_req_swabbed(req, offset + 1);
381         r->ur_name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
382         if (r->ur_name == NULL)
383                 RETURN(-EFAULT);
384         r->ur_namelen = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
385
386         lustre_set_req_swabbed(req, offset + 2);
387         r->ur_eadatalen = lustre_msg_buflen(req->rq_reqmsg, offset + 2);
388         if (r->ur_eadatalen) {
389                 r->ur_eadata = lustre_msg_buf(req->rq_reqmsg, offset + 2, 0);
390                 if (r->ur_eadata == NULL)
391                         RETURN (-EFAULT);
392         }
393         RETURN(0);
394 }
395
396 typedef int (*update_unpacker)(struct ptlrpc_request *req, int offset,
397                                struct mds_update_record *r);
398
399 static update_unpacker mds_unpackers[REINT_MAX] = {
400         [REINT_SETATTR] mds_setattr_unpack,
401         [REINT_CREATE] mds_create_unpack,
402         [REINT_LINK] mds_link_unpack,
403         [REINT_UNLINK] mds_unlink_unpack,
404         [REINT_RENAME] mds_rename_unpack,
405         [REINT_OPEN] mds_open_unpack,
406 };
407
408 int mds_update_unpack(struct ptlrpc_request *req, int offset,
409                       struct mds_update_record *rec)
410 {
411         mds_reint_t opcode, *opcodep;
412         int rc;
413         ENTRY;
414
415         /* NB don't lustre_swab_reqbuf() here.  We're just taking a peek
416          * and we want to leave it to the specific unpacker once we've
417          * identified the message type */
418         opcodep = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*opcodep));
419         if (opcodep == NULL)
420                 RETURN(-EFAULT);
421
422         opcode = *opcodep;
423         if (lustre_msg_swabbed(req->rq_reqmsg))
424                 __swab32s(&opcode);
425
426         if (opcode >= REINT_MAX || mds_unpackers[opcode] == NULL) {
427                 CERROR("Unexpected opcode %d\n", opcode);
428                 RETURN(-EFAULT);
429         }
430
431         rec->ur_opcode = opcode;
432         rc = mds_unpackers[opcode](req, offset, rec);
433
434         RETURN(rc);
435 }
436
437 int mds_init_ucred(struct lvfs_ucred *ucred, struct ptlrpc_request *req,
438                    int offset)
439 {
440         struct mds_body *body = lustre_msg_buf(req->rq_reqmsg, offset,
441                                                sizeof(*body));
442 #if 0
443         struct mds_obd *mds = mds_req2mds(req);
444         int rc;
445 #endif
446
447         LASSERT(body != NULL); /* previously verified & swabbed by caller */
448
449 #ifdef CRAY_XT3
450         if (req->rq_uid != LNET_UID_ANY) {
451                 /* Non-root local cluster client */
452                 LASSERT (req->rq_uid != 0);
453                 ucred->luc_fsuid = req->rq_uid;
454         } else
455 #endif
456         {
457                 ucred->luc_fsuid = body->fsuid;
458                 ucred->luc_fsgid = body->fsgid;
459                 ucred->luc_cap = body->capability;
460         }
461
462 #if 0
463         ucred->luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
464                                                 ucred->luc_fsuid,
465                                                 ucred->luc_fsgid, 1,
466                                                 &body->suppgid);
467         if (IS_ERR(ucred->luc_uce)) {
468                 rc = PTR_ERR(ucred->luc_uce);
469                 ucred->luc_uce = NULL;
470                 return rc;
471         }
472
473 #ifdef CRAY_XT3
474         if (ucred->luc_uce)
475                 ucred->luc_fsgid = ucred->luc_uce->ue_primary;
476 #endif
477 #endif
478
479         return 0;
480 }
481
482 void mds_exit_ucred(struct lvfs_ucred *ucred, struct mds_obd *mds)
483 {
484 #if 0
485         upcall_cache_put_entry(mds->mds_group_hash, ucred->luc_uce);
486 #endif
487 }