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