Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[fs/lustre-release.git] / lustre / mdc / mdc_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 Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDS
26 #ifndef __KERNEL__
27 # include <fcntl.h>
28 # include <liblustre.h>
29 #endif
30 #include <linux/lustre_idl.h>
31 #include <linux/lustre_net.h>
32 #include <linux/lustre_mds.h>
33 #include "mdc_internal.h"
34
35 #ifndef __KERNEL__
36 /* some liblustre hackings here */
37 #ifndef O_DIRECTORY
38 #define O_DIRECTORY     0
39 #endif
40 #endif
41
42 void mdc_readdir_pack(struct ptlrpc_request *req, __u64 offset, __u32 size,
43                       struct ll_fid *mdc_fid)
44 {
45         struct mds_body *b;
46
47         b = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*b));
48         b->fsuid = current->fsuid;
49         b->fsgid = current->fsgid;
50         b->capability = current->cap_effective;
51         b->fid1 = *mdc_fid;
52         b->size = offset;                       /* !! */
53         b->suppgid = -1;
54         b->nlink = size;                        /* !! */
55 }
56
57 static void mdc_pack_body(struct mds_body *b)
58 {
59         LASSERT (b != NULL);
60
61         b->fsuid = current->fsuid;
62         b->fsgid = current->fsgid;
63         b->capability = current->cap_effective;
64 }
65
66 void mdc_pack_req_body(struct ptlrpc_request *req)
67 {
68         struct mds_body *b = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*b));
69         mdc_pack_body(b);
70 }
71 static __u32 mds_pack_open_flags(__u32 flags)
72 {
73         return
74                 (flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC |
75                           MDS_OPEN_DELAY_CREATE | MDS_OPEN_HAS_EA |
76                           MDS_OPEN_HAS_OBJS)) |
77                 ((flags & O_CREAT) ? MDS_OPEN_CREAT : 0) |
78                 ((flags & O_EXCL) ? MDS_OPEN_EXCL : 0) |
79                 ((flags & O_TRUNC) ? MDS_OPEN_TRUNC : 0) |
80                 ((flags & O_APPEND) ? MDS_OPEN_APPEND : 0) |
81                 ((flags & O_SYNC) ? MDS_OPEN_SYNC : 0) |
82                 ((flags & O_DIRECTORY) ? MDS_OPEN_DIRECTORY : 0) |
83                 0;
84 }
85 /* packing of MDS records */
86 void mdc_open_pack(struct lustre_msg *msg, int offset,
87                    struct mdc_op_data *op_data, __u32 mode, __u64 rdev,
88                    __u32 flags, const void *lmm, int lmmlen)
89 {
90         struct mds_rec_create *rec;
91         char *tmp;
92         rec = lustre_msg_buf(msg, offset, sizeof (*rec));
93
94         /* XXX do something about time, uid, gid */
95         rec->cr_opcode = REINT_OPEN;
96         rec->cr_fsuid = current->fsuid;
97         rec->cr_fsgid = current->fsgid;
98         rec->cr_cap = current->cap_effective;
99         if (op_data != NULL)
100                 rec->cr_fid = op_data->fid1;
101         memset(&rec->cr_replayfid, 0, sizeof(rec->cr_replayfid));
102         rec->cr_mode = mode;
103         rec->cr_flags = mds_pack_open_flags(flags);
104         rec->cr_rdev = rdev;
105         rec->cr_time = op_data->mod_time;
106         rec->cr_suppgid = op_data->ctxt.gid1;
107
108         if (op_data->name) {
109                 tmp = lustre_msg_buf(msg, offset + 1,
110                                      op_data->namelen + 1);
111                 LOGL0(op_data->name, op_data->namelen, tmp);
112         }
113
114         if (lmm) {
115                 rec->cr_flags |= MDS_OPEN_HAS_EA;
116                 tmp = lustre_msg_buf(msg, offset + 2, lmmlen);
117                 memcpy (tmp, lmm, lmmlen);
118         }
119 }
120 void mdc_getattr_pack(struct lustre_msg *msg, int valid, int offset,
121                       int flags, struct mdc_op_data *data)
122 {
123         struct mds_body *b;
124         b = lustre_msg_buf(msg, offset, sizeof (*b));
125
126         b->fsuid = current->fsuid;
127         b->fsgid = current->fsgid;
128         b->capability = current->cap_effective;
129         b->valid = valid;
130         b->flags = flags;
131         b->suppgid = data->ctxt.gid1;
132
133         b->fid1 = data->fid1;
134         b->fid2 = data->fid2;
135         if (data->name) {
136                 char *tmp;
137                 tmp = lustre_msg_buf(msg, offset + 1,
138                                      data->namelen + 1);
139                 LOGL0(data->name, data->namelen, tmp);
140         }
141 }
142
143 void mdc_close_pack(struct ptlrpc_request *req, int offset, struct obdo *oa,
144                     int valid, struct obd_client_handle *och)
145 {
146         struct mds_body *body;
147
148         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
149
150         mdc_pack_fid(&body->fid1, oa->o_id, 0, oa->o_mode);
151         memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
152         if (oa->o_valid & OBD_MD_FLATIME) {
153                 body->atime = oa->o_atime;
154                 body->valid |= OBD_MD_FLATIME;
155         }
156         if (oa->o_valid & OBD_MD_FLMTIME) {
157                 body->mtime = oa->o_mtime;
158                 body->valid |= OBD_MD_FLMTIME;
159         }
160         if (oa->o_valid & OBD_MD_FLCTIME) {
161                 body->ctime = oa->o_ctime;
162                 body->valid |= OBD_MD_FLCTIME;
163         }
164         if (oa->o_valid & OBD_MD_FLSIZE) {
165                 body->size = oa->o_size;
166                 body->valid |= OBD_MD_FLSIZE;
167         }
168         if (oa->o_valid & OBD_MD_FLBLOCKS) {
169                 body->blocks = oa->o_blocks;
170                 body->valid |= OBD_MD_FLBLOCKS;
171         }
172         if (oa->o_valid & OBD_MD_FLFLAGS) {
173                 body->flags = oa->o_flags;
174                 body->valid |= OBD_MD_FLFLAGS;
175         }
176 }