4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
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
38 #include <lustre_net.h>
39 #include <lustre/lustre_idl.h>
40 #include "mdc_internal.h"
43 static void __mdc_pack_body(struct mdt_body *b, __u32 suppgid)
48 b->uid = current_uid();
49 b->gid = current_gid();
50 b->fsuid = current_fsuid();
51 b->fsgid = current_fsgid();
52 b->capability = cfs_curproc_cap_pack();
55 void mdc_pack_capa(struct ptlrpc_request *req, const struct req_msg_field *field,
58 struct req_capsule *pill = &req->rq_pill;
59 struct lustre_capa *c;
62 LASSERT(req_capsule_get_size(pill, field, RCL_CLIENT) == 0);
66 c = req_capsule_client_get(pill, field);
69 DEBUG_CAPA(D_SEC, c, "pack");
72 void mdc_is_subdir_pack(struct ptlrpc_request *req, const struct lu_fid *pfid,
73 const struct lu_fid *cfid, int flags)
75 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
80 b->valid = OBD_MD_FLID;
87 void mdc_swap_layouts_pack(struct ptlrpc_request *req,
88 struct md_op_data *op_data)
90 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
93 __mdc_pack_body(b, op_data->op_suppgids[0]);
94 b->fid1 = op_data->op_fid1;
95 b->fid2 = op_data->op_fid2;
96 b->valid |= OBD_MD_FLID;
98 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
99 mdc_pack_capa(req, &RMF_CAPA2, op_data->op_capa2);
102 void mdc_pack_body(struct ptlrpc_request *req,
103 const struct lu_fid *fid, struct obd_capa *oc,
104 __u64 valid, int ea_size, __u32 suppgid, int flags)
106 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
110 b->eadatasize = ea_size;
112 __mdc_pack_body(b, suppgid);
115 b->valid |= OBD_MD_FLID;
116 mdc_pack_capa(req, &RMF_CAPA1, oc);
120 void mdc_readdir_pack(struct ptlrpc_request *req, __u64 pgoff,
121 __u32 size, const struct lu_fid *fid, struct obd_capa *oc)
123 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
126 b->valid |= OBD_MD_FLID;
127 b->size = pgoff; /* !! */
128 b->nlink = size; /* !! */
129 __mdc_pack_body(b, -1);
130 b->mode = LUDA_FID | LUDA_TYPE;
132 mdc_pack_capa(req, &RMF_CAPA1, oc);
135 /* packing of MDS records */
136 void mdc_create_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
137 const void *data, int datalen, __u32 mode,
138 __u32 uid, __u32 gid, cfs_cap_t cap_effective, __u64 rdev)
140 struct mdt_rec_create *rec;
144 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_create));
145 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
148 rec->cr_opcode = REINT_CREATE;
151 rec->cr_cap = cap_effective;
152 rec->cr_fid1 = op_data->op_fid1;
153 rec->cr_fid2 = op_data->op_fid2;
156 rec->cr_time = op_data->op_mod_time;
157 rec->cr_suppgid1 = op_data->op_suppgids[0];
158 rec->cr_suppgid2 = op_data->op_suppgids[1];
159 flags = op_data->op_flags & MF_SOM_LOCAL_FLAGS;
160 if (op_data->op_bias & MDS_CREATE_VOLATILE)
161 flags |= MDS_OPEN_VOLATILE;
162 set_mrc_cr_flags(rec, flags);
163 rec->cr_bias = op_data->op_bias;
164 rec->cr_umask = current_umask();
166 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
168 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
169 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
172 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
173 memcpy(tmp, data, datalen);
177 static __u64 mds_pack_open_flags(__u32 flags, __u32 mode)
179 __u64 cr_flags = (flags & (FMODE_READ | FMODE_WRITE |
180 MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS |
181 MDS_OPEN_OWNEROVERRIDE | MDS_OPEN_LOCK |
184 cr_flags |= MDS_OPEN_CREAT;
186 cr_flags |= MDS_OPEN_EXCL;
188 cr_flags |= MDS_OPEN_TRUNC;
189 if (flags & O_APPEND)
190 cr_flags |= MDS_OPEN_APPEND;
192 cr_flags |= MDS_OPEN_SYNC;
193 if (flags & O_DIRECTORY)
194 cr_flags |= MDS_OPEN_DIRECTORY;
196 if (flags & FMODE_EXEC)
197 cr_flags |= MDS_FMODE_EXEC;
199 if (flags & O_LOV_DELAY_CREATE)
200 cr_flags |= MDS_OPEN_DELAY_CREATE;
202 if (flags & O_NONBLOCK)
203 cr_flags |= MDS_OPEN_NORESTORE;
208 /* packing of MDS records */
209 void mdc_open_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
210 __u32 mode, __u64 rdev, __u32 flags, const void *lmm,
213 struct mdt_rec_create *rec;
217 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_create));
218 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
220 /* XXX do something about time, uid, gid */
221 rec->cr_opcode = REINT_OPEN;
222 rec->cr_fsuid = current_fsuid();
223 rec->cr_fsgid = current_fsgid();
224 rec->cr_cap = cfs_curproc_cap_pack();
225 if (op_data != NULL) {
226 rec->cr_fid1 = op_data->op_fid1;
227 rec->cr_fid2 = op_data->op_fid2;
230 cr_flags = mds_pack_open_flags(flags, mode);
232 rec->cr_time = op_data->op_mod_time;
233 rec->cr_suppgid1 = op_data->op_suppgids[0];
234 rec->cr_suppgid2 = op_data->op_suppgids[1];
235 rec->cr_bias = op_data->op_bias;
236 rec->cr_umask = current_umask();
238 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
239 /* the next buffer is child capa, which is used for replay,
240 * will be packed from the data in reply message. */
242 if (op_data->op_name) {
243 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
244 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
245 if (op_data->op_bias & MDS_CREATE_VOLATILE)
246 cr_flags |= MDS_OPEN_VOLATILE;
250 cr_flags |= MDS_OPEN_HAS_EA;
251 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
252 memcpy(tmp, lmm, lmmlen);
254 set_mrc_cr_flags(rec, cr_flags);
257 static inline __u64 attr_pack(unsigned int ia_valid) {
260 if (ia_valid & ATTR_MODE)
261 sa_valid |= MDS_ATTR_MODE;
262 if (ia_valid & ATTR_UID)
263 sa_valid |= MDS_ATTR_UID;
264 if (ia_valid & ATTR_GID)
265 sa_valid |= MDS_ATTR_GID;
266 if (ia_valid & ATTR_SIZE)
267 sa_valid |= MDS_ATTR_SIZE;
268 if (ia_valid & ATTR_ATIME)
269 sa_valid |= MDS_ATTR_ATIME;
270 if (ia_valid & ATTR_MTIME)
271 sa_valid |= MDS_ATTR_MTIME;
272 if (ia_valid & ATTR_CTIME)
273 sa_valid |= MDS_ATTR_CTIME;
274 if (ia_valid & ATTR_ATIME_SET)
275 sa_valid |= MDS_ATTR_ATIME_SET;
276 if (ia_valid & ATTR_MTIME_SET)
277 sa_valid |= MDS_ATTR_MTIME_SET;
278 if (ia_valid & ATTR_FORCE)
279 sa_valid |= MDS_ATTR_FORCE;
280 if (ia_valid & ATTR_ATTR_FLAG)
281 sa_valid |= MDS_ATTR_ATTR_FLAG;
282 if (ia_valid & ATTR_KILL_SUID)
283 sa_valid |= MDS_ATTR_KILL_SUID;
284 if (ia_valid & ATTR_KILL_SGID)
285 sa_valid |= MDS_ATTR_KILL_SGID;
286 if (ia_valid & ATTR_CTIME_SET)
287 sa_valid |= MDS_ATTR_CTIME_SET;
288 if (ia_valid & ATTR_FROM_OPEN)
289 sa_valid |= MDS_ATTR_FROM_OPEN;
290 if (ia_valid & ATTR_BLOCKS)
291 sa_valid |= MDS_ATTR_BLOCKS;
292 if (ia_valid & MDS_OPEN_OWNEROVERRIDE)
293 /* NFSD hack (see bug 5781) */
294 sa_valid |= MDS_OPEN_OWNEROVERRIDE;
298 static void mdc_setattr_pack_rec(struct mdt_rec_setattr *rec,
299 struct md_op_data *op_data)
301 rec->sa_opcode = REINT_SETATTR;
302 rec->sa_fsuid = current_fsuid();
303 rec->sa_fsgid = current_fsgid();
304 rec->sa_cap = cfs_curproc_cap_pack();
305 rec->sa_suppgid = -1;
307 rec->sa_fid = op_data->op_fid1;
308 rec->sa_valid = attr_pack(op_data->op_attr.ia_valid);
309 rec->sa_mode = op_data->op_attr.ia_mode;
310 rec->sa_uid = op_data->op_attr.ia_uid;
311 rec->sa_gid = op_data->op_attr.ia_gid;
312 rec->sa_size = op_data->op_attr.ia_size;
313 rec->sa_blocks = op_data->op_attr_blocks;
314 rec->sa_atime = LTIME_S(op_data->op_attr.ia_atime);
315 rec->sa_mtime = LTIME_S(op_data->op_attr.ia_mtime);
316 rec->sa_ctime = LTIME_S(op_data->op_attr.ia_ctime);
317 rec->sa_attr_flags = ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
318 if ((op_data->op_attr.ia_valid & ATTR_GID) &&
319 current_is_in_group(op_data->op_attr.ia_gid))
320 rec->sa_suppgid = op_data->op_attr.ia_gid;
322 rec->sa_suppgid = op_data->op_suppgids[0];
324 rec->sa_bias = op_data->op_bias;
327 static void mdc_ioepoch_pack(struct mdt_ioepoch *epoch,
328 struct md_op_data *op_data)
330 memcpy(&epoch->handle, &op_data->op_handle, sizeof(epoch->handle));
331 epoch->ioepoch = op_data->op_ioepoch;
332 epoch->flags = op_data->op_flags & MF_SOM_LOCAL_FLAGS;
335 void mdc_setattr_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
336 void *ea, int ealen, void *ea2, int ea2len)
338 struct mdt_rec_setattr *rec;
339 struct mdt_ioepoch *epoch;
340 struct lov_user_md *lum = NULL;
342 CLASSERT(sizeof(struct mdt_rec_reint) ==sizeof(struct mdt_rec_setattr));
343 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
344 mdc_setattr_pack_rec(rec, op_data);
346 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
348 if (op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) {
349 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
350 mdc_ioepoch_pack(epoch, op_data);
356 lum = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
357 if (ea == NULL) { /* Remove LOV EA */
358 lum->lmm_magic = LOV_USER_MAGIC_V1;
359 lum->lmm_stripe_size = 0;
360 lum->lmm_stripe_count = 0;
361 lum->lmm_stripe_offset = (typeof(lum->lmm_stripe_offset))(-1);
363 memcpy(lum, ea, ealen);
369 memcpy(req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES), ea2,
373 void mdc_unlink_pack(struct ptlrpc_request *req, struct md_op_data *op_data)
375 struct mdt_rec_unlink *rec;
378 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_unlink));
379 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
380 LASSERT(rec != NULL);
382 rec->ul_opcode = op_data->op_cli_flags & CLI_RM_ENTRY ?
383 REINT_RMENTRY : REINT_UNLINK;
384 rec->ul_fsuid = op_data->op_fsuid;
385 rec->ul_fsgid = op_data->op_fsgid;
386 rec->ul_cap = op_data->op_cap;
387 rec->ul_mode = op_data->op_mode;
388 rec->ul_suppgid1= op_data->op_suppgids[0];
389 rec->ul_suppgid2= -1;
390 rec->ul_fid1 = op_data->op_fid1;
391 rec->ul_fid2 = op_data->op_fid2;
392 rec->ul_time = op_data->op_mod_time;
393 rec->ul_bias = op_data->op_bias;
395 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
397 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
398 LASSERT(tmp != NULL);
399 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
402 void mdc_link_pack(struct ptlrpc_request *req, struct md_op_data *op_data)
404 struct mdt_rec_link *rec;
407 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_link));
408 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
409 LASSERT (rec != NULL);
411 rec->lk_opcode = REINT_LINK;
412 rec->lk_fsuid = op_data->op_fsuid;//current->fsuid;
413 rec->lk_fsgid = op_data->op_fsgid;//current->fsgid;
414 rec->lk_cap = op_data->op_cap;//current->cap_effective;
415 rec->lk_suppgid1 = op_data->op_suppgids[0];
416 rec->lk_suppgid2 = op_data->op_suppgids[1];
417 rec->lk_fid1 = op_data->op_fid1;
418 rec->lk_fid2 = op_data->op_fid2;
419 rec->lk_time = op_data->op_mod_time;
420 rec->lk_bias = op_data->op_bias;
422 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
423 mdc_pack_capa(req, &RMF_CAPA2, op_data->op_capa2);
425 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
426 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
429 void mdc_rename_pack(struct ptlrpc_request *req, struct md_op_data *op_data,
430 const char *old, int oldlen, const char *new, int newlen)
432 struct mdt_rec_rename *rec;
435 CLASSERT(sizeof(struct mdt_rec_reint) == sizeof(struct mdt_rec_rename));
436 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
438 /* XXX do something about time, uid, gid */
439 rec->rn_opcode = REINT_RENAME;
440 rec->rn_fsuid = op_data->op_fsuid;
441 rec->rn_fsgid = op_data->op_fsgid;
442 rec->rn_cap = op_data->op_cap;
443 rec->rn_suppgid1 = op_data->op_suppgids[0];
444 rec->rn_suppgid2 = op_data->op_suppgids[1];
445 rec->rn_fid1 = op_data->op_fid1;
446 rec->rn_fid2 = op_data->op_fid2;
447 rec->rn_time = op_data->op_mod_time;
448 rec->rn_mode = op_data->op_mode;
449 rec->rn_bias = op_data->op_bias;
451 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
452 mdc_pack_capa(req, &RMF_CAPA2, op_data->op_capa2);
454 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
455 LOGL0(old, oldlen, tmp);
458 tmp = req_capsule_client_get(&req->rq_pill, &RMF_SYMTGT);
459 LOGL0(new, newlen, tmp);
463 void mdc_getattr_pack(struct ptlrpc_request *req, __u64 valid, int flags,
464 struct md_op_data *op_data, int ea_size)
466 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
470 if (op_data->op_bias & MDS_CHECK_SPLIT)
471 b->valid |= OBD_MD_FLCKSPLIT;
472 if (op_data->op_bias & MDS_CROSS_REF)
473 b->valid |= OBD_MD_FLCROSSREF;
474 b->eadatasize = ea_size;
476 __mdc_pack_body(b, op_data->op_suppgids[0]);
478 b->fid1 = op_data->op_fid1;
479 b->fid2 = op_data->op_fid2;
480 b->valid |= OBD_MD_FLID;
482 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
484 if (op_data->op_name) {
485 char *tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
486 LOGL0(op_data->op_name, op_data->op_namelen, tmp);
491 void mdc_close_pack(struct ptlrpc_request *req, struct md_op_data *op_data)
493 struct mdt_ioepoch *epoch;
494 struct mdt_rec_setattr *rec;
496 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
497 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
499 mdc_setattr_pack_rec(rec, op_data);
500 mdc_pack_capa(req, &RMF_CAPA1, op_data->op_capa1);
501 mdc_ioepoch_pack(epoch, op_data);
504 static int mdc_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
508 client_obd_list_lock(&cli->cl_loi_list_lock);
509 rc = list_empty(&mcw->mcw_entry);
510 client_obd_list_unlock(&cli->cl_loi_list_lock);
514 /* We record requests in flight in cli->cl_r_in_flight here.
515 * There is only one write rpc possible in mdc anyway. If this to change
516 * in the future - the code may need to be revisited. */
517 int mdc_enter_request(struct client_obd *cli)
520 struct mdc_cache_waiter mcw;
521 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
523 client_obd_list_lock(&cli->cl_loi_list_lock);
524 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
525 list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
526 init_waitqueue_head(&mcw.mcw_waitq);
527 client_obd_list_unlock(&cli->cl_loi_list_lock);
528 rc = l_wait_event(mcw.mcw_waitq, mdc_req_avail(cli, &mcw), &lwi);
530 client_obd_list_lock(&cli->cl_loi_list_lock);
531 if (list_empty(&mcw.mcw_entry))
532 cli->cl_r_in_flight--;
533 list_del_init(&mcw.mcw_entry);
534 client_obd_list_unlock(&cli->cl_loi_list_lock);
537 cli->cl_r_in_flight++;
538 client_obd_list_unlock(&cli->cl_loi_list_lock);
543 void mdc_exit_request(struct client_obd *cli)
545 struct list_head *l, *tmp;
546 struct mdc_cache_waiter *mcw;
548 client_obd_list_lock(&cli->cl_loi_list_lock);
549 cli->cl_r_in_flight--;
550 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
551 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
552 /* No free request slots anymore */
556 mcw = list_entry(l, struct mdc_cache_waiter, mcw_entry);
557 list_del_init(&mcw->mcw_entry);
558 cli->cl_r_in_flight++;
559 wake_up(&mcw->mcw_waitq);
561 /* Empty waiting list? Decrease reqs in-flight number */
563 client_obd_list_unlock(&cli->cl_loi_list_lock);