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