Whamcloud - gitweb
Branch b1_4
[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 #define DEBUG_SUBSYSTEM S_MDC
23 #ifndef __KERNEL__
24 # include <fcntl.h>
25 # include <liblustre.h>
26 #endif
27 #include <linux/lustre_idl.h>
28 #include <linux/lustre_net.h>
29 #include <linux/lustre_mds.h>
30 #include "mdc_internal.h"
31
32 #ifndef __KERNEL__
33 /* some liblustre hackings here */
34 #ifndef O_DIRECTORY
35 #define O_DIRECTORY     0
36 #endif
37 #endif
38
39 void mdc_readdir_pack(struct ptlrpc_request *req, __u64 offset, __u32 size,
40                       struct ll_fid *mdc_fid)
41 {
42         struct mds_body *b;
43
44         b = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*b));
45         b->fsuid = current->fsuid;
46         b->fsgid = current->fsgid;
47         b->capability = current->cap_effective;
48         b->fid1 = *mdc_fid;
49         b->size = offset;                       /* !! */
50         b->suppgid = -1;
51         b->nlink = size;                        /* !! */
52 }
53
54 static void mdc_pack_body(struct mds_body *b)
55 {
56         LASSERT (b != NULL);
57
58         b->fsuid = current->fsuid;
59         b->fsgid = current->fsgid;
60         b->capability = current->cap_effective;
61 }
62
63 void mdc_pack_req_body(struct ptlrpc_request *req)
64 {
65         struct mds_body *b = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*b));
66         mdc_pack_body(b);
67 }
68
69 /* packing of MDS records */
70 void mdc_create_pack(struct ptlrpc_request *req, int offset,
71                      struct mdc_op_data *op_data, __u32 mode, __u64 rdev,
72                      const void *data, int datalen)
73 {
74         struct mds_rec_create *rec;
75         char *tmp;
76         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
77
78         rec->cr_opcode = REINT_CREATE;
79         rec->cr_fsuid = current->fsuid;
80         rec->cr_fsgid = current->fsgid;
81         rec->cr_cap = current->cap_effective;
82         rec->cr_fid = op_data->fid1;
83         memset(&rec->cr_replayfid, 0, sizeof(rec->cr_replayfid));
84         rec->cr_mode = mode;
85         rec->cr_rdev = rdev;
86         rec->cr_time = op_data->mod_time;
87         rec->cr_suppgid = op_data->ctxt.gid1;
88
89         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, op_data->namelen + 1);
90         LOGL0(op_data->name, op_data->namelen, tmp);
91
92         if (data) {
93                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, datalen);
94                 memcpy (tmp, data, datalen);
95         }
96 }
97
98 static __u32 mds_pack_open_flags(__u32 flags)
99 {
100         return
101                 (flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC |
102                           MDS_OPEN_DELAY_CREATE | MDS_OPEN_HAS_EA |
103                           MDS_OPEN_HAS_OBJS | MDS_OPEN_OWNEROVERRIDE)) |
104                 ((flags & O_CREAT) ? MDS_OPEN_CREAT : 0) |
105                 ((flags & O_EXCL) ? MDS_OPEN_EXCL : 0) |
106                 ((flags & O_TRUNC) ? MDS_OPEN_TRUNC : 0) |
107                 ((flags & O_APPEND) ? MDS_OPEN_APPEND : 0) |
108                 ((flags & O_SYNC) ? MDS_OPEN_SYNC : 0) |
109                 ((flags & O_DIRECTORY) ? MDS_OPEN_DIRECTORY : 0) |
110                 0;
111 }
112
113 /* packing of MDS records */
114 void mdc_open_pack(struct ptlrpc_request *req, int offset,
115                    struct mdc_op_data *op_data, __u32 mode, __u64 rdev,
116                    __u32 flags, const void *lmm, int lmmlen)
117 {
118         struct mds_rec_create *rec;
119         char *tmp;
120         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
121
122         /* XXX do something about time, uid, gid */
123         rec->cr_opcode = REINT_OPEN;
124         rec->cr_fsuid = current->fsuid;
125         rec->cr_fsgid = current->fsgid;
126         rec->cr_cap = current->cap_effective;
127         if (op_data != NULL)
128                 rec->cr_fid = op_data->fid1;
129         memset(&rec->cr_replayfid, 0, sizeof(rec->cr_replayfid));
130         rec->cr_mode = mode;
131         rec->cr_flags = mds_pack_open_flags(flags);
132         rec->cr_rdev = rdev;
133         rec->cr_time = op_data->mod_time;
134         rec->cr_suppgid = op_data->ctxt.gid1;
135
136         if (op_data->name) {
137                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1,
138                                      op_data->namelen + 1);
139                 LOGL0(op_data->name, op_data->namelen, tmp);
140         }
141
142         if (lmm) {
143                 rec->cr_flags |= MDS_OPEN_HAS_EA;
144                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, lmmlen);
145                 memcpy (tmp, lmm, lmmlen);
146         }
147 }
148
149 void mdc_setattr_pack(struct ptlrpc_request *req, struct mdc_op_data *data,
150                       struct iattr *iattr, void *ea, int ealen,
151                       void *ea2, int ea2len)
152 {
153         struct mds_rec_setattr *rec = lustre_msg_buf(req->rq_reqmsg, 0,
154                                                      sizeof (*rec));
155         rec->sa_opcode = REINT_SETATTR;
156         rec->sa_fsuid = current->fsuid;
157         rec->sa_fsgid = current->fsgid;
158         rec->sa_cap = current->cap_effective;
159         rec->sa_fid = data->fid1;
160
161         if (iattr) {
162                 rec->sa_valid = iattr->ia_valid;
163                 rec->sa_mode = iattr->ia_mode;
164                 rec->sa_uid = iattr->ia_uid;
165                 rec->sa_gid = iattr->ia_gid;
166                 rec->sa_size = iattr->ia_size;
167                 rec->sa_atime = LTIME_S(iattr->ia_atime);
168                 rec->sa_mtime = LTIME_S(iattr->ia_mtime);
169                 rec->sa_ctime = LTIME_S(iattr->ia_ctime);
170                 rec->sa_attr_flags = iattr->ia_attr_flags;
171                 if ((iattr->ia_valid & ATTR_GID) && in_group_p(iattr->ia_gid))
172                         rec->sa_suppgid = iattr->ia_gid;
173                 else if ((iattr->ia_valid & ATTR_MODE) &&
174                          in_group_p(iattr->ia_gid))
175                         rec->sa_suppgid = data->ctxt.gid1;
176                 else if ((iattr->ia_valid & (ATTR_MTIME|ATTR_CTIME)) &&
177                          data->ctxt.gid1 != -1)
178                         rec->sa_suppgid = data->ctxt.gid1;
179         }
180
181         if (ealen == 0)
182                 return;
183
184         memcpy(lustre_msg_buf(req->rq_reqmsg, 1, ealen), ea, ealen);
185
186         if (ea2len == 0)
187                 return;
188
189         memcpy(lustre_msg_buf(req->rq_reqmsg, 2, ea2len), ea2, ea2len);
190 }
191
192 void mdc_unlink_pack(struct ptlrpc_request *req, int offset,
193                      struct mdc_op_data *data)
194 {
195         struct mds_rec_unlink *rec;
196         char *tmp;
197
198         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
199         LASSERT (rec != NULL);
200
201         rec->ul_opcode = REINT_UNLINK;
202         rec->ul_fsuid = current->fsuid;
203         rec->ul_fsgid = current->fsgid;
204         rec->ul_cap = current->cap_effective;
205         rec->ul_mode = data->create_mode;
206         rec->ul_suppgid = data->ctxt.gid1;
207         rec->ul_fid1 = data->fid1;
208         rec->ul_fid2 = data->fid2;
209         rec->ul_time = data->mod_time;
210
211         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, data->namelen + 1);
212         LASSERT (tmp != NULL);
213         LOGL0(data->name, data->namelen, tmp);
214 }
215
216 void mdc_link_pack(struct ptlrpc_request *req, int offset,
217                    struct mdc_op_data *data)
218 {
219         struct mds_rec_link *rec;
220         char *tmp;
221
222         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
223
224         rec->lk_opcode = REINT_LINK;
225         rec->lk_fsuid = current->fsuid;
226         rec->lk_fsgid = current->fsgid;
227         rec->lk_cap = current->cap_effective;
228         rec->lk_suppgid1 = data->ctxt.gid1;
229         rec->lk_suppgid2 = data->ctxt.gid2;
230         rec->lk_fid1 = data->fid1;
231         rec->lk_fid2 = data->fid2;
232         rec->lk_time = data->mod_time;
233
234         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, data->namelen + 1);
235         LOGL0(data->name, data->namelen, tmp);
236 }
237
238 void mdc_rename_pack(struct ptlrpc_request *req, int offset,
239                      struct mdc_op_data *data,
240                      const char *old, int oldlen, const char *new, int newlen)
241 {
242         struct mds_rec_rename *rec;
243         char *tmp;
244
245         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
246
247         /* XXX do something about time, uid, gid */
248         rec->rn_opcode = REINT_RENAME;
249         rec->rn_fsuid = current->fsuid;
250         rec->rn_fsgid = current->fsgid;
251         rec->rn_cap = current->cap_effective;
252         rec->rn_suppgid1 = data->ctxt.gid1;
253         rec->rn_suppgid2 = data->ctxt.gid2;
254         rec->rn_fid1 = data->fid1;
255         rec->rn_fid2 = data->fid2;
256         rec->rn_time = data->mod_time;
257
258         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, oldlen + 1);
259         LOGL0(old, oldlen, tmp);
260
261         if (new) {
262                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, newlen + 1);
263                 LOGL0(new, newlen, tmp);
264         }
265 }
266
267 void mdc_getattr_pack(struct ptlrpc_request *req, int valid, int offset,
268                       int flags, struct mdc_op_data *data)
269 {
270         struct mds_body *b;
271         b = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*b));
272
273         b->fsuid = current->fsuid;
274         b->fsgid = current->fsgid;
275         b->capability = current->cap_effective;
276         b->valid = valid;
277         b->flags = flags;
278         b->suppgid = data->ctxt.gid1;
279
280         b->fid1 = data->fid1;
281         if (data->name) {
282                 char *tmp;
283                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1,
284                                      data->namelen + 1);
285                 LOGL0(data->name, data->namelen, tmp);
286         }
287 }
288
289 void mdc_close_pack(struct ptlrpc_request *req, int offset, struct obdo *oa,
290                     int valid, struct obd_client_handle *och)
291 {
292         struct mds_body *body;
293
294         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
295
296         mdc_pack_fid(&body->fid1, oa->o_id, 0, oa->o_mode);
297         memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
298         if (oa->o_valid & OBD_MD_FLATIME) {
299                 body->atime = oa->o_atime;
300                 body->valid |= OBD_MD_FLATIME;
301         }
302         if (oa->o_valid & OBD_MD_FLMTIME) {
303                 body->mtime = oa->o_mtime;
304                 body->valid |= OBD_MD_FLMTIME;
305         }
306         if (oa->o_valid & OBD_MD_FLCTIME) {
307                 body->ctime = oa->o_ctime;
308                 body->valid |= OBD_MD_FLCTIME;
309         }
310         if (oa->o_valid & OBD_MD_FLSIZE) {
311                 body->size = oa->o_size;
312                 body->valid |= OBD_MD_FLSIZE;
313         }
314         if (oa->o_valid & OBD_MD_FLBLOCKS) {
315                 body->blocks = oa->o_blocks;
316                 body->valid |= OBD_MD_FLBLOCKS;
317         }
318         if (oa->o_valid & OBD_MD_FLFLAGS) {
319                 body->flags = oa->o_flags;
320                 body->valid |= OBD_MD_FLFLAGS;
321         }
322 }