1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_MDC
40 # include <liblustre.h>
42 #include <lustre_net.h>
43 #include <lustre/lustre_idl.h>
44 #include "mdc_internal.h"
47 /* some liblustre hackings here */
53 static void __mdc_pack_body(struct mdt_body *b, __u32 suppgid)
58 b->uid = cfs_curproc_uid();
59 b->gid = cfs_curproc_gid();
60 b->fsuid = cfs_curproc_fsuid();
61 b->fsgid = cfs_curproc_fsgid();
62 b->capability = cfs_curproc_cap_pack();
65 void mdc_pack_capa(struct ptlrpc_request *req, const struct req_msg_field *field,
68 struct req_capsule *pill = &req->rq_pill;
69 struct lustre_capa *c;
72 LASSERT(req_capsule_get_size(pill, field, RCL_CLIENT) == 0);
76 c = req_capsule_client_get(pill, field);
79 DEBUG_CAPA(D_SEC, c, "pack");
82 void mdc_is_subdir_pack(struct ptlrpc_request *req, const struct lu_fid *pfid,
83 const struct lu_fid *cfid, int flags)
85 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
90 b->valid = OBD_MD_FLID;
97 void mdc_pack_body(struct ptlrpc_request *req,
98 const struct lu_fid *fid, struct obd_capa *oc,
99 __u64 valid, int ea_size, __u32 suppgid, int flags)
101 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
105 b->eadatasize = ea_size;
107 __mdc_pack_body(b, suppgid);
110 b->valid |= OBD_MD_FLID;
111 mdc_pack_capa(req, &RMF_CAPA1, oc);
115 void mdc_readdir_pack(struct ptlrpc_request *req, __u64 pgoff,
116 __u32 size, const struct lu_fid *fid, struct obd_capa *oc)
118 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
121 b->valid |= OBD_MD_FLID;
122 b->size = pgoff; /* !! */
123 b->nlink = size; /* !! */
124 __mdc_pack_body(b, -1);
125 b->mode = LUDA_FID | LUDA_TYPE;
127 mdc_pack_capa(req, &RMF_CAPA1, oc);
130 /* packing of MDS records */
131 void mdc_create_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
132 const void *data, int datalen, __u32 mode,
133 __u32 uid, __u32 gid, cfs_cap_t cap_effective, __u64 rdev)
135 struct mdt_rec_create *rec;
138 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_create));
139 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
142 rec->cr_opcode = REINT_CREATE;
145 rec->cr_cap = cap_effective;
146 rec->cr_fid1 = op_data->op_fid1;
147 rec->cr_fid2 = op_data->op_fid2;
150 rec->cr_time = op_data->op_mod_time;
151 rec->cr_suppgid1 = op_data->op_suppgids[0];
152 rec->cr_suppgid2 = op_data->op_suppgids[1];
153 set_mrc_cr_flags(rec, op_data->op_flags & MF_SOM_LOCAL_FLAGS);
154 rec->cr_bias = op_data->op_bias;
156 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
158 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
159 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
162 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
163 memcpy(tmp, data, datalen);
167 static __u64 mds_pack_open_flags(__u32 flags, __u32 mode)
169 __u64 cr_flags = (flags & (FMODE_READ | FMODE_WRITE |
170 MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS |
171 MDS_OPEN_OWNEROVERRIDE | MDS_OPEN_LOCK));
173 cr_flags |= MDS_OPEN_CREAT;
175 cr_flags |= MDS_OPEN_EXCL;
177 cr_flags |= MDS_OPEN_TRUNC;
178 if (flags & O_APPEND)
179 cr_flags |= MDS_OPEN_APPEND;
181 cr_flags |= MDS_OPEN_SYNC;
182 if (flags & O_DIRECTORY)
183 cr_flags |= MDS_OPEN_DIRECTORY;
185 if (flags & FMODE_EXEC)
186 cr_flags |= MDS_FMODE_EXEC;
188 if (flags & O_LOV_DELAY_CREATE)
189 cr_flags |= MDS_OPEN_DELAY_CREATE;
191 if ((flags & O_NOACCESS) || (flags & O_NONBLOCK))
192 cr_flags |= MDS_OPEN_NORESTORE;
197 /* packing of MDS records */
198 void mdc_open_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
199 __u32 mode, __u64 rdev, __u32 flags, const void *lmm,
202 struct mdt_rec_create *rec;
206 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_create));
207 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
209 /* XXX do something about time, uid, gid */
210 rec->cr_opcode = REINT_OPEN;
211 rec->cr_fsuid = cfs_curproc_fsuid();
212 rec->cr_fsgid = cfs_curproc_fsgid();
213 rec->cr_cap = cfs_curproc_cap_pack();
214 if (op_data != NULL) {
215 rec->cr_fid1 = op_data->op_fid1;
216 rec->cr_fid2 = op_data->op_fid2;
219 cr_flags = mds_pack_open_flags(flags, mode);
221 rec->cr_time = op_data->op_mod_time;
222 rec->cr_suppgid1 = op_data->op_suppgids[0];
223 rec->cr_suppgid2 = op_data->op_suppgids[1];
224 rec->cr_bias = op_data->op_bias;
226 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
227 /* the next buffer is child capa, which is used for replay,
228 * will be packed from the data in reply message. */
230 if (op_data->op_name) {
231 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
232 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
236 cr_flags |= MDS_OPEN_HAS_EA;
238 /*XXX a hack for liblustre to set EA (LL_IOC_LOV_SETSTRIPE) */
239 rec->cr_fid2 = op_data->op_fid2;
241 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
242 memcpy (tmp, lmm, lmmlen);
244 set_mrc_cr_flags(rec, cr_flags);
247 static inline __u64 attr_pack(unsigned int ia_valid) {
250 if (ia_valid & ATTR_MODE)
251 sa_valid |= MDS_ATTR_MODE;
252 if (ia_valid & ATTR_UID)
253 sa_valid |= MDS_ATTR_UID;
254 if (ia_valid & ATTR_GID)
255 sa_valid |= MDS_ATTR_GID;
256 if (ia_valid & ATTR_SIZE)
257 sa_valid |= MDS_ATTR_SIZE;
258 if (ia_valid & ATTR_ATIME)
259 sa_valid |= MDS_ATTR_ATIME;
260 if (ia_valid & ATTR_MTIME)
261 sa_valid |= MDS_ATTR_MTIME;
262 if (ia_valid & ATTR_CTIME)
263 sa_valid |= MDS_ATTR_CTIME;
264 if (ia_valid & ATTR_ATIME_SET)
265 sa_valid |= MDS_ATTR_ATIME_SET;
266 if (ia_valid & ATTR_MTIME_SET)
267 sa_valid |= MDS_ATTR_MTIME_SET;
268 if (ia_valid & ATTR_FORCE)
269 sa_valid |= MDS_ATTR_FORCE;
270 if (ia_valid & ATTR_ATTR_FLAG)
271 sa_valid |= MDS_ATTR_ATTR_FLAG;
272 if (ia_valid & ATTR_KILL_SUID)
273 sa_valid |= MDS_ATTR_KILL_SUID;
274 if (ia_valid & ATTR_KILL_SGID)
275 sa_valid |= MDS_ATTR_KILL_SGID;
276 if (ia_valid & ATTR_CTIME_SET)
277 sa_valid |= MDS_ATTR_CTIME_SET;
278 if (ia_valid & ATTR_FROM_OPEN)
279 sa_valid |= MDS_ATTR_FROM_OPEN;
280 if (ia_valid & ATTR_BLOCKS)
281 sa_valid |= MDS_ATTR_BLOCKS;
282 if (ia_valid & MDS_OPEN_OWNEROVERRIDE)
283 /* NFSD hack (see bug 5781) */
284 sa_valid |= MDS_OPEN_OWNEROVERRIDE;
288 static void mdc_setattr_pack_rec(struct mdt_rec_setattr *rec,
289 struct md_op_data *op_data)
291 rec->sa_opcode = REINT_SETATTR;
292 rec->sa_fsuid = cfs_curproc_fsuid();
293 rec->sa_fsgid = cfs_curproc_fsgid();
294 rec->sa_cap = cfs_curproc_cap_pack();
295 rec->sa_suppgid = -1;
297 rec->sa_fid = op_data->op_fid1;
298 rec->sa_valid = attr_pack(op_data->op_attr.ia_valid);
299 rec->sa_mode = op_data->op_attr.ia_mode;
300 rec->sa_uid = op_data->op_attr.ia_uid;
301 rec->sa_gid = op_data->op_attr.ia_gid;
302 rec->sa_size = op_data->op_attr.ia_size;
303 rec->sa_blocks = op_data->op_attr_blocks;
304 rec->sa_atime = LTIME_S(op_data->op_attr.ia_atime);
305 rec->sa_mtime = LTIME_S(op_data->op_attr.ia_mtime);
306 rec->sa_ctime = LTIME_S(op_data->op_attr.ia_ctime);
307 rec->sa_attr_flags = ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
308 if ((op_data->op_attr.ia_valid & ATTR_GID) &&
309 cfs_curproc_is_in_groups(op_data->op_attr.ia_gid))
310 rec->sa_suppgid = op_data->op_attr.ia_gid;
312 rec->sa_suppgid = op_data->op_suppgids[0];
315 static void mdc_ioepoch_pack(struct mdt_ioepoch *epoch,
316 struct md_op_data *op_data)
318 memcpy(&epoch->handle, &op_data->op_handle, sizeof(epoch->handle));
319 epoch->ioepoch = op_data->op_ioepoch;
320 epoch->flags = op_data->op_flags & MF_SOM_LOCAL_FLAGS;
323 void mdc_setattr_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
324 void *ea, int ealen, void *ea2, int ea2len)
326 struct mdt_rec_setattr *rec;
327 struct mdt_ioepoch *epoch;
328 struct lov_user_md *lum = NULL;
330 CLASSERT(sizeof(struct mdt_rec_reint) ==sizeof(struct mdt_rec_setattr));
331 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
332 mdc_setattr_pack_rec(rec, op_data);
334 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
336 if (op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) {
337 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
338 mdc_ioepoch_pack(epoch, op_data);
344 lum = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
345 if (ea == NULL) { /* Remove LOV EA */
346 lum->lmm_magic = LOV_USER_MAGIC_V1;
347 lum->lmm_stripe_size = 0;
348 lum->lmm_stripe_count = 0;
349 lum->lmm_stripe_offset = (typeof(lum->lmm_stripe_offset))(-1);
351 memcpy(lum, ea, ealen);
357 memcpy(req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES), ea2,
361 void mdc_unlink_pack(struct ptlrpc_request *req, struct md_op_data *op_data)
363 struct mdt_rec_unlink *rec;
366 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_unlink));
367 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
368 LASSERT (rec != NULL);
370 rec->ul_opcode = REINT_UNLINK;
371 rec->ul_fsuid = op_data->op_fsuid;
372 rec->ul_fsgid = op_data->op_fsgid;
373 rec->ul_cap = op_data->op_cap;
374 rec->ul_mode = op_data->op_mode;
375 rec->ul_suppgid1= op_data->op_suppgids[0];
376 rec->ul_suppgid2= -1;
377 rec->ul_fid1 = op_data->op_fid1;
378 rec->ul_fid2 = op_data->op_fid2;
379 rec->ul_time = op_data->op_mod_time;
380 rec->ul_bias = op_data->op_bias;
382 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
384 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
385 LASSERT(tmp != NULL);
386 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
389 void mdc_link_pack(struct ptlrpc_request *req, struct md_op_data *op_data)
391 struct mdt_rec_link *rec;
394 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_link));
395 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
396 LASSERT (rec != NULL);
398 rec->lk_opcode = REINT_LINK;
399 rec->lk_fsuid = op_data->op_fsuid;//current->fsuid;
400 rec->lk_fsgid = op_data->op_fsgid;//current->fsgid;
401 rec->lk_cap = op_data->op_cap;//current->cap_effective;
402 rec->lk_suppgid1 = op_data->op_suppgids[0];
403 rec->lk_suppgid2 = op_data->op_suppgids[1];
404 rec->lk_fid1 = op_data->op_fid1;
405 rec->lk_fid2 = op_data->op_fid2;
406 rec->lk_time = op_data->op_mod_time;
407 rec->lk_bias = op_data->op_bias;
409 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
410 mdc_pack_capa(req, &RMF_CAPA2, op_data->op_capa2);
412 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
413 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
416 void mdc_rename_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
417 const char *old, int oldlen, const char *new, int newlen)
419 struct mdt_rec_rename *rec;
422 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_rename));
423 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
425 /* XXX do something about time, uid, gid */
426 rec->rn_opcode = REINT_RENAME;
427 rec->rn_fsuid = op_data->op_fsuid;
428 rec->rn_fsgid = op_data->op_fsgid;
429 rec->rn_cap = op_data->op_cap;
430 rec->rn_suppgid1 = op_data->op_suppgids[0];
431 rec->rn_suppgid2 = op_data->op_suppgids[1];
432 rec->rn_fid1 = op_data->op_fid1;
433 rec->rn_fid2 = op_data->op_fid2;
434 rec->rn_time = op_data->op_mod_time;
435 rec->rn_mode = op_data->op_mode;
436 rec->rn_bias = op_data->op_bias;
438 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
439 mdc_pack_capa(req, &RMF_CAPA2, op_data->op_capa2);
441 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
442 LOGL0(old, oldlen, tmp);
445 tmp = req_capsule_client_get(&req->rq_pill, &RMF_SYMTGT);
446 LOGL0(new, newlen, tmp);
450 void mdc_getattr_pack(struct ptlrpc_request *req, __u64 valid, int flags,
451 struct md_op_data *op_data, int ea_size)
453 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
457 if (op_data->op_bias & MDS_CHECK_SPLIT)
458 b->valid |= OBD_MD_FLCKSPLIT;
459 if (op_data->op_bias & MDS_CROSS_REF)
460 b->valid |= OBD_MD_FLCROSSREF;
461 b->eadatasize = ea_size;
463 __mdc_pack_body(b, op_data->op_suppgids[0]);
465 b->fid1 = op_data->op_fid1;
466 b->fid2 = op_data->op_fid2;
467 b->valid |= OBD_MD_FLID;
469 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
471 if (op_data->op_name) {
472 char *tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
473 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
478 void mdc_close_pack(struct ptlrpc_request *req, struct md_op_data *op_data)
480 struct mdt_ioepoch *epoch;
481 struct mdt_rec_setattr *rec;
483 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
484 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
486 mdc_setattr_pack_rec(rec, op_data);
487 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
488 mdc_ioepoch_pack(epoch, op_data);
491 static int mdc_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
495 client_obd_list_lock(&cli->cl_loi_list_lock);
496 rc = cfs_list_empty(&mcw->mcw_entry);
497 client_obd_list_unlock(&cli->cl_loi_list_lock);
501 /* We record requests in flight in cli->cl_r_in_flight here.
502 * There is only one write rpc possible in mdc anyway. If this to change
503 * in the future - the code may need to be revisited. */
504 void mdc_enter_request(struct client_obd *cli)
506 struct mdc_cache_waiter mcw;
507 struct l_wait_info lwi = { 0 };
509 client_obd_list_lock(&cli->cl_loi_list_lock);
510 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
511 cfs_list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
512 cfs_waitq_init(&mcw.mcw_waitq);
513 client_obd_list_unlock(&cli->cl_loi_list_lock);
514 l_wait_event(mcw.mcw_waitq, mdc_req_avail(cli, &mcw), &lwi);
516 cli->cl_r_in_flight++;
517 client_obd_list_unlock(&cli->cl_loi_list_lock);
521 void mdc_exit_request(struct client_obd *cli)
524 struct mdc_cache_waiter *mcw;
526 client_obd_list_lock(&cli->cl_loi_list_lock);
527 cli->cl_r_in_flight--;
528 cfs_list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
529 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
530 /* No free request slots anymore */
534 mcw = cfs_list_entry(l, struct mdc_cache_waiter, mcw_entry);
535 cfs_list_del_init(&mcw->mcw_entry);
536 cli->cl_r_in_flight++;
537 cfs_waitq_signal(&mcw->mcw_waitq);
539 /* Empty waiting list? Decrease reqs in-flight number */
541 client_obd_list_unlock(&cli->cl_loi_list_lock);