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