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