Whamcloud - gitweb
336abee9763e44805c4be647bd3e6861d223eac3
[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 <lustre/lustre_idl.h>
31 #include <lustre_net.h>
32 #include "mdc_internal.h"
33
34 #ifndef __KERNEL__
35 /* some liblustre hackings here */
36 #ifndef O_DIRECTORY
37 #define O_DIRECTORY     0
38 #endif
39 #endif
40
41 void mdc_readdir_pack(struct ptlrpc_request *req, int pos, __u64 offset,
42                       __u32 size, const struct lu_fid *fid)
43 {
44         struct mdt_body *b;
45
46         b = lustre_msg_buf(req->rq_reqmsg, pos, sizeof(*b));
47         b->fsuid = current->fsuid;
48         b->fsgid = current->fsgid;
49         b->capability = current->cap_effective;
50         b->fid1 = *fid;
51         b->size = offset;                       /* !! */
52         b->suppgid = -1;
53         b->nlink = size;                        /* !! */
54 }
55
56 static void mdc_pack_body(struct mdt_body *b)
57 {
58         LASSERT (b != NULL);
59
60         b->fsuid = current->fsuid;
61         b->fsgid = current->fsgid;
62         b->capability = current->cap_effective;
63 }
64
65 void mdc_pack_req_body(struct ptlrpc_request *req, int offset,
66                        __u64 valid, const struct lu_fid *fid,
67                        int ea_size, int flags)
68 {
69         struct mdt_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         b->flags = flags;
76         mdc_pack_body(b);
77 }
78 /* packing of MDS records */
79 void mdc_create_pack(struct ptlrpc_request *req, int offset,
80                      struct md_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 mdt_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_fid1 = op_data->fid1;
93         rec->cr_fid2 = op_data->fid2;
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 |
112                           MDS_OPEN_DELAY_CREATE | MDS_OPEN_HAS_EA |
113                           MDS_OPEN_HAS_OBJS | MDS_OPEN_OWNEROVERRIDE |
114                           MDS_OPEN_LOCK)) |
115                 ((flags & O_CREAT) ? MDS_OPEN_CREAT : 0) |
116                 ((flags & O_EXCL) ? MDS_OPEN_EXCL : 0) |
117                 ((flags & O_TRUNC) ? MDS_OPEN_TRUNC : 0) |
118                 ((flags & O_APPEND) ? MDS_OPEN_APPEND : 0) |
119                 ((flags & O_SYNC) ? MDS_OPEN_SYNC : 0) |
120                 ((flags & O_DIRECTORY) ? MDS_OPEN_DIRECTORY : 0) |
121                 ((flags & O_JOIN_FILE) ? MDS_OPEN_JOIN_FILE : 0) |
122 #ifdef FMODE_EXEC
123                 ((flags & FMODE_EXEC) ? MDS_FMODE_EXEC : 0) |
124 #endif
125                 0;
126 }
127
128 /* packing of MDS records */
129 void mdc_join_pack(struct ptlrpc_request *req, int offset,
130                    struct md_op_data *op_data, __u64 head_size)
131 {
132         struct mdt_rec_join *rec;
133
134         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*rec));
135         LASSERT(rec != NULL);
136         rec->jr_fid = op_data->fid2;
137         rec->jr_headsize = head_size;
138 }
139
140 void mdc_open_pack(struct ptlrpc_request *req, int offset,
141                    struct md_op_data *op_data, __u32 mode, __u64 rdev,
142                    __u32 flags, const void *lmm, int lmmlen)
143 {
144         struct mdt_rec_create *rec;
145         char *tmp;
146         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
147
148         /* XXX do something about time, uid, gid */
149         rec->cr_opcode = REINT_OPEN;
150         rec->cr_fsuid = current->fsuid;
151         rec->cr_fsgid = current->fsgid;
152         rec->cr_cap = current->cap_effective;
153         if (op_data != NULL) {
154                 rec->cr_fid1 = op_data->fid1;
155                 rec->cr_fid2 = op_data->fid2;
156         }
157         rec->cr_mode = mode;
158         rec->cr_flags = mds_pack_open_flags(flags);
159         rec->cr_rdev = rdev;
160         rec->cr_time = op_data->mod_time;
161         rec->cr_suppgid = op_data->suppgids[0];
162
163         if (op_data->name) {
164                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1,
165                                      op_data->namelen + 1);
166                 LOGL0(op_data->name, op_data->namelen, tmp);
167         }
168
169         if (lmm) {
170                 rec->cr_flags |= MDS_OPEN_HAS_EA;
171 #ifndef __KERNEL__
172                 /*XXX a hack for liblustre to set EA (LL_IOC_LOV_SETSTRIPE) */
173                 rec->cr_fid2 = op_data->fid2;
174 #endif
175                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, lmmlen);
176                 memcpy (tmp, lmm, lmmlen);
177         }
178 }
179
180 void mdc_setattr_pack(struct ptlrpc_request *req, int offset,
181                       struct md_op_data *op_data, struct iattr *iattr,
182                       void *ea, int ealen, void *ea2, int ea2len)
183 {
184         struct mdt_rec_setattr *rec = lustre_msg_buf(req->rq_reqmsg, offset,
185                                                      sizeof (*rec));
186         rec->sa_opcode = REINT_SETATTR;
187         rec->sa_fsuid = current->fsuid;
188         rec->sa_fsgid = current->fsgid;
189         rec->sa_cap = current->cap_effective;
190         rec->sa_fid = op_data->fid1;
191         rec->sa_suppgid = -1;
192
193         if (iattr) {
194                 rec->sa_valid = iattr->ia_valid;
195                 rec->sa_mode = iattr->ia_mode;
196                 rec->sa_uid = iattr->ia_uid;
197                 rec->sa_gid = iattr->ia_gid;
198                 rec->sa_size = iattr->ia_size;
199                 rec->sa_atime = LTIME_S(iattr->ia_atime);
200                 rec->sa_mtime = LTIME_S(iattr->ia_mtime);
201                 rec->sa_ctime = LTIME_S(iattr->ia_ctime);
202                 rec->sa_attr_flags =
203                                ((struct ll_iattr_struct *)iattr)->ia_attr_flags;
204                 if ((iattr->ia_valid & ATTR_GID) && in_group_p(iattr->ia_gid))
205                         rec->sa_suppgid = iattr->ia_gid;
206                 else
207                         rec->sa_suppgid = op_data->suppgids[0];
208         }
209
210         if (ealen == 0)
211                 return;
212
213         memcpy(lustre_msg_buf(req->rq_reqmsg, offset + 1, ealen), ea, ealen);
214
215         if (ea2len == 0)
216                 return;
217
218         memcpy(lustre_msg_buf(req->rq_reqmsg, offset + 2, ea2len), ea2, ea2len);
219 }
220
221 void mdc_unlink_pack(struct ptlrpc_request *req, int offset,
222                      struct md_op_data *op_data)
223 {
224         struct mdt_rec_unlink *rec;
225         char *tmp;
226
227         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
228         LASSERT (rec != NULL);
229
230         rec->ul_opcode = REINT_UNLINK;
231         rec->ul_fsuid = op_data->fsuid;//current->fsuid;
232         rec->ul_fsgid = op_data->fsgid;//current->fsgid;
233         rec->ul_cap = current->cap_effective;
234         rec->ul_mode = op_data->create_mode;
235         rec->ul_suppgid = op_data->suppgids[0];
236         rec->ul_fid1 = op_data->fid1;
237         rec->ul_fid2 = op_data->fid2;
238         rec->ul_time = op_data->mod_time;
239
240         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, op_data->namelen + 1);
241         LASSERT (tmp != NULL);
242         LOGL0(op_data->name, op_data->namelen, tmp);
243 }
244
245 void mdc_link_pack(struct ptlrpc_request *req, int offset,
246                    struct md_op_data *op_data)
247 {
248         struct mdt_rec_link *rec;
249         char *tmp;
250
251         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
252
253         rec->lk_opcode = REINT_LINK;
254         rec->lk_fsuid = op_data->fsuid;//current->fsuid;
255         rec->lk_fsgid = op_data->fsgid;//current->fsgid;
256         rec->lk_cap = current->cap_effective;
257         rec->lk_suppgid1 = op_data->suppgids[0];
258         rec->lk_suppgid2 = op_data->suppgids[1];
259         rec->lk_fid1 = op_data->fid1;
260         rec->lk_fid2 = op_data->fid2;
261         rec->lk_time = op_data->mod_time;
262
263         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, op_data->namelen + 1);
264         LOGL0(op_data->name, op_data->namelen, tmp);
265 }
266
267 void mdc_rename_pack(struct ptlrpc_request *req, int offset,
268                      struct md_op_data *op_data,
269                      const char *old, int oldlen, const char *new, int newlen)
270 {
271         struct mdt_rec_rename *rec;
272         char *tmp;
273
274         rec = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*rec));
275
276         /* XXX do something about time, uid, gid */
277         rec->rn_opcode = REINT_RENAME;
278         rec->rn_fsuid = op_data->fsuid;//current->fsuid;
279         rec->rn_fsgid = op_data->fsgid;//current->fsgid;
280         rec->rn_cap = current->cap_effective;
281         rec->rn_suppgid1 = op_data->suppgids[0];
282         rec->rn_suppgid2 = op_data->suppgids[1];
283         rec->rn_fid1 = op_data->fid1;
284         rec->rn_fid2 = op_data->fid2;
285         rec->rn_time = op_data->mod_time;
286
287         tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1, oldlen + 1);
288         LOGL0(old, oldlen, tmp);
289
290         if (new) {
291                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, newlen + 1);
292                 LOGL0(new, newlen, tmp);
293         }
294 }
295
296 void mdc_getattr_pack(struct ptlrpc_request *req, int offset, __u64 valid,
297                       int flags, struct md_op_data *op_data)
298 {
299         struct mdt_body *b;
300         b = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*b));
301
302         b->fsuid = current->fsuid;
303         b->fsgid = current->fsgid;
304         b->capability = current->cap_effective;
305         b->valid = valid;
306         b->flags = flags | MDS_BFLAG_EXT_FLAGS;
307         b->suppgid = op_data->suppgids[0];
308
309         b->fid1 = op_data->fid1;
310         b->fid2 = op_data->fid2;
311         if (op_data->name) {
312                 char *tmp;
313                 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 1,
314                                      op_data->namelen + 1);
315                 LOGL0(op_data->name, op_data->namelen, tmp);
316         }
317 }
318
319 void mdc_close_pack(struct ptlrpc_request *req, int offset,
320                     struct md_op_data *op_data, __u64  valid,
321                     struct obd_client_handle *och)
322 {
323         struct mdt_body *body;
324
325         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
326
327         body->fid1 = op_data->fid1;
328         memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
329         if (op_data->valid & OBD_MD_FLATIME) {
330                 body->atime = op_data->atime;
331                 body->valid |= OBD_MD_FLATIME;
332         }
333         if (op_data->valid & OBD_MD_FLMTIME) {
334                 body->mtime = op_data->mtime;
335                 body->valid |= OBD_MD_FLMTIME;
336         }
337         if (op_data->valid & OBD_MD_FLCTIME) {
338                 body->ctime = op_data->ctime;
339                 body->valid |= OBD_MD_FLCTIME;
340         }
341         if (op_data->valid & OBD_MD_FLSIZE) {
342                 body->size = op_data->size;
343                 body->valid |= OBD_MD_FLSIZE;
344         }
345         if (op_data->valid & OBD_MD_FLBLOCKS) {
346                 body->blocks = op_data->blocks;
347                 body->valid |= OBD_MD_FLBLOCKS;
348         }
349         if (op_data->valid & OBD_MD_FLFLAGS) {
350                 body->flags = op_data->flags;
351                 body->valid |= OBD_MD_FLFLAGS;
352         }
353 }
354
355 struct mdc_cache_waiter {       
356         struct list_head        mcw_entry;
357         wait_queue_head_t       mcw_waitq;
358 };
359
360 static int mdc_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
361 {
362         int rc;
363         ENTRY;
364         spin_lock(&cli->cl_loi_list_lock);
365         rc = list_empty(&mcw->mcw_entry);
366         spin_unlock(&cli->cl_loi_list_lock);
367         RETURN(rc);
368 };
369
370 /* We record requests in flight in cli->cl_r_in_flight here.
371  * There is only one write rpc possible in mdc anyway. If this to change
372  * in the future - the code may need to be revisited. */
373 void mdc_enter_request(struct client_obd *cli)
374 {
375         struct mdc_cache_waiter mcw;
376         struct l_wait_info lwi = { 0 };
377
378         spin_lock(&cli->cl_loi_list_lock);
379         if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
380                 list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
381                 init_waitqueue_head(&mcw.mcw_waitq);
382                 spin_unlock(&cli->cl_loi_list_lock);
383                 l_wait_event(mcw.mcw_waitq, mdc_req_avail(cli, &mcw), &lwi);
384         } else {
385                 cli->cl_r_in_flight++;
386                 spin_unlock(&cli->cl_loi_list_lock);
387         }
388 }
389
390 void mdc_exit_request(struct client_obd *cli)
391 {
392         struct list_head *l, *tmp;
393         struct mdc_cache_waiter *mcw;
394
395         spin_lock(&cli->cl_loi_list_lock);
396         cli->cl_r_in_flight--;
397         list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
398                 
399                 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
400                         /* No free request slots anymore */
401                         break;
402                 }
403
404                 mcw = list_entry(l, struct mdc_cache_waiter, mcw_entry);
405                 list_del_init(&mcw->mcw_entry);
406                 cli->cl_r_in_flight++;
407                 wake_up(&mcw->mcw_waitq);
408         }
409         /* Empty waiting list? Decrease reqs in-flight number */
410         
411         spin_unlock(&cli->cl_loi_list_lock);
412 }