Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[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_MDS
23 #ifndef __KERNEL__
24 # include <liblustre.h>
25 #endif
26 #include <linux/lustre_idl.h>
27 #include <linux/lustre_net.h>
28 #include <linux/lustre_mds.h>
29 #include <linux/lustre_lite.h>
30
31 void mds_readdir_pack(struct ptlrpc_request *req, __u64 offset, __u32 size,
32                       obd_id ino, int type, __u64 xid)
33 {
34         struct mds_body *b;
35
36         b = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*b));
37         b->fsuid = current->fsuid;
38         b->fsgid = current->fsgid;
39         b->capability = current->cap_effective;
40         b->fid1.id = ino;
41         b->fid1.f_type = type;
42         b->size = offset;                       /* !! */
43         b->suppgid = -1;
44         b->blocks = xid;                        /* !! */
45         b->nlink = size;                        /* !! */
46 }
47
48 static void mds_pack_body(struct mds_body *b)
49 {
50         LASSERT (b != NULL);
51
52         b->fsuid = current->fsuid;
53         b->fsgid = current->fsgid;
54         b->capability = current->cap_effective;
55 }
56
57 void mds_pack_req_body(struct ptlrpc_request *req)
58 {
59         struct mds_body *b = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*b));
60         mds_pack_body(b);
61 }
62
63 /* packing of MDS records */
64 void mds_create_pack(struct ptlrpc_request *req, int offset,
65                      struct mdc_op_data *op_data,
66                      __u32 mode, __u64 rdev, __u32 uid, __u32 gid, __u64 time,
67                      const void *data, int datalen)
68 {
69         struct mds_rec_create *rec;
70         char *tmp;
71         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
72
73         rec->cr_opcode = REINT_CREATE;
74         rec->cr_fsuid = current->fsuid;
75         rec->cr_fsgid = current->fsgid;
76         rec->cr_cap = current->cap_effective;
77         ll_ino2fid(&rec->cr_fid, op_data->ino1, op_data->gen1, op_data->typ1);
78         memset(&rec->cr_replayfid, 0, sizeof(rec->cr_replayfid));
79         rec->cr_mode = mode;
80         rec->cr_rdev = rdev;
81         rec->cr_uid = uid;
82         rec->cr_gid = gid;
83         rec->cr_time = time;
84         if (in_group_p(op_data->gid1))
85                 rec->cr_suppgid = op_data->gid1;
86         else
87                 rec->cr_suppgid = -1;
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 /* packing of MDS records */
98 void mds_open_pack(struct ptlrpc_request *req, int offset,
99                    struct mdc_op_data *op_data,
100                    __u32 mode, __u64 rdev, __u32 uid, __u32 gid, __u64 time,
101                    __u32 flags, const void *data, int datalen)
102 {
103         struct mds_rec_create *rec;
104         char *tmp;
105         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
106
107         /* XXX do something about time, uid, gid */
108         rec->cr_opcode = REINT_OPEN;
109         rec->cr_fsuid = current->fsuid;
110         rec->cr_fsgid = current->fsgid;
111         rec->cr_cap = current->cap_effective;
112         ll_ino2fid(&rec->cr_fid, op_data->ino1,
113                    op_data->gen1, op_data->typ1);
114         memset(&rec->cr_replayfid, 0, sizeof(rec->cr_replayfid));
115         rec->cr_mode = mode;
116         rec->cr_flags = flags;
117         rec->cr_rdev = rdev;
118         rec->cr_uid = uid;
119         rec->cr_gid = gid;
120         rec->cr_time = time;
121         if (in_group_p(op_data->gid1))
122                 rec->cr_suppgid = op_data->gid1;
123         else
124                 rec->cr_suppgid = -1;
125
126         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, op_data->namelen + 1);
127         LOGL0(op_data->name, op_data->namelen, tmp);
128
129         if (data) {
130                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, datalen);
131                 memcpy (tmp, data, datalen);
132         }
133 }
134 void mds_setattr_pack(struct ptlrpc_request *req,
135                       struct mdc_op_data *data,
136                       struct iattr *iattr, void *ea, int ealen)
137 {
138         struct mds_rec_setattr *rec = lustre_msg_buf(req->rq_reqmsg, 0,
139                                                      sizeof (*rec));
140         rec->sa_opcode = REINT_SETATTR;
141         rec->sa_fsuid = current->fsuid;
142         rec->sa_fsgid = current->fsgid;
143         rec->sa_cap = current->cap_effective;
144         ll_ino2fid(&rec->sa_fid, data->ino1, data->gen1, data->typ1);
145
146         if (iattr) {
147                 rec->sa_valid = iattr->ia_valid;
148                 rec->sa_mode = iattr->ia_mode;
149                 rec->sa_uid = iattr->ia_uid;
150                 rec->sa_gid = iattr->ia_gid;
151                 rec->sa_size = iattr->ia_size;
152                 rec->sa_atime = LTIME_S(iattr->ia_atime);
153                 rec->sa_mtime = LTIME_S(iattr->ia_mtime);
154                 rec->sa_ctime = LTIME_S(iattr->ia_ctime);
155                 rec->sa_attr_flags = iattr->ia_attr_flags;
156
157                 if ((iattr->ia_valid & ATTR_GID) && in_group_p(iattr->ia_gid))
158                         rec->sa_suppgid = iattr->ia_gid;
159                 else if ((iattr->ia_valid & ATTR_MODE) &&
160                          in_group_p(data->gid1))
161                         rec->sa_suppgid = data->gid1;
162                 else
163                         rec->sa_suppgid = -1;
164         }
165
166         if (ealen != 0)
167                 memcpy(lustre_msg_buf(req->rq_reqmsg, 1, ealen), ea, ealen);
168 }
169
170 void mds_unlink_pack(struct ptlrpc_request *req, int offset,
171                      struct mdc_op_data *data)
172 {
173         struct mds_rec_unlink *rec;
174         char *tmp;
175
176         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
177         LASSERT (rec != NULL);
178
179         rec->ul_opcode = REINT_UNLINK;
180         rec->ul_fsuid = current->fsuid;
181         rec->ul_fsgid = current->fsgid;
182         rec->ul_cap = current->cap_effective;
183         rec->ul_mode = data->mode;
184         if (in_group_p(data->gid1))
185                 rec->ul_suppgid = data->gid1;
186         else
187                 rec->ul_suppgid = -1;
188         ll_ino2fid(&rec->ul_fid1, data->ino1, data->gen1, data->typ1);
189         if (data->ino2)
190                 ll_ino2fid(&rec->ul_fid2, data->ino2, data->gen2, data->typ2);
191
192         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, data->namelen + 1);
193         LASSERT (tmp != NULL);
194         LOGL0(data->name, data->namelen, tmp);
195 }
196
197 void mds_link_pack(struct ptlrpc_request *req, int offset,
198                    struct mdc_op_data *data)
199 {
200         struct mds_rec_link *rec;
201         char *tmp;
202
203         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
204
205         rec->lk_opcode = REINT_LINK;
206         rec->lk_fsuid = current->fsuid;
207         rec->lk_fsgid = current->fsgid;
208         rec->lk_cap = current->cap_effective;
209         if (in_group_p(data->gid1))
210                 rec->lk_suppgid1 = data->gid1;
211         else
212                 rec->lk_suppgid1 = -1;
213         if (in_group_p(data->gid2))
214                 rec->lk_suppgid2 = data->gid2;
215         else
216                 rec->lk_suppgid2 = -1;
217         ll_ino2fid(&rec->lk_fid1, data->ino1, data->gen1, data->typ1);
218         ll_ino2fid(&rec->lk_fid2, data->ino2, data->gen2, data->typ2);
219
220         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, data->namelen + 1);
221         LOGL0(data->name, data->namelen, tmp);
222 }
223
224 void mds_rename_pack(struct ptlrpc_request *req, int offset,
225                      struct mdc_op_data *data,
226                      const char *old, int oldlen, const char *new, int newlen)
227 {
228         struct mds_rec_rename *rec;
229         char *tmp;
230
231         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
232
233         /* XXX do something about time, uid, gid */
234         rec->rn_opcode = REINT_RENAME;
235         rec->rn_fsuid = current->fsuid;
236         rec->rn_fsgid = current->fsgid;
237         rec->rn_cap = current->cap_effective;
238         if (in_group_p(data->gid1))
239                 rec->rn_suppgid1 = data->gid1;
240         else
241                 rec->rn_suppgid1 = -1;
242         if (in_group_p(data->gid2))
243                 rec->rn_suppgid2 = data->gid2;
244         else
245                 rec->rn_suppgid2 = -1;
246         ll_ino2fid(&rec->rn_fid1, data->ino1, data->gen1, data->typ1);
247         ll_ino2fid(&rec->rn_fid2, data->ino2, data->gen2, data->typ2);
248
249         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, oldlen + 1);
250         LOGL0(old, oldlen, tmp);
251
252         if (new) {
253                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, newlen + 1);
254                 LOGL0(new, newlen, tmp);
255         }
256 }
257
258 void mds_getattr_pack(struct ptlrpc_request *req, int valid, int offset,
259                       int flags, struct mdc_op_data *data)
260 {
261         struct mds_body *b;
262         b = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*b));
263
264         b->fsuid = current->fsuid;
265         b->fsgid = current->fsgid;
266         b->capability = current->cap_effective;
267         b->valid = valid;
268         b->flags = flags;
269         if (in_group_p(data->gid1))
270                 b->suppgid = data->gid1;
271         else
272                 b->suppgid = -1;
273
274         ll_ino2fid(&b->fid1, data->ino1, data->gen1, data->typ1);
275         if (data->name) {
276                 char *tmp;
277                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1,
278                                      data->namelen + 1);
279                 LOGL0(data->name, data->namelen, tmp);
280         }
281 }