staging: lustre: remove ENTRY/EXIT_NESTING stuff
[deliverable/linux.git] / drivers / staging / lustre / lustre / mdc / mdc_request.c
CommitLineData
d7e09d03
PT
1/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
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
19 *
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
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26/*
27 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2011, 2012, Intel Corporation.
31 */
32/*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 */
36
37#define DEBUG_SUBSYSTEM S_MDC
38
39# include <linux/module.h>
40# include <linux/pagemap.h>
41# include <linux/miscdevice.h>
42# include <linux/init.h>
43# include <linux/utsname.h>
44
45#include <lustre_acl.h>
46#include <obd_class.h>
47#include <lustre_fid.h>
48#include <lprocfs_status.h>
49#include <lustre_param.h>
50#include <lustre_log.h>
51
52#include "mdc_internal.h"
53
54#define REQUEST_MINOR 244
55
56struct mdc_renew_capa_args {
57 struct obd_capa *ra_oc;
58 renew_capa_cb_t ra_cb;
59};
60
61static int mdc_cleanup(struct obd_device *obd);
62
63int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
64 const struct req_msg_field *field, struct obd_capa **oc)
65{
66 struct lustre_capa *capa;
67 struct obd_capa *c;
68 ENTRY;
69
70 /* swabbed already in mdc_enqueue */
71 capa = req_capsule_server_get(&req->rq_pill, field);
72 if (capa == NULL)
73 RETURN(-EPROTO);
74
75 c = alloc_capa(CAPA_SITE_CLIENT);
76 if (IS_ERR(c)) {
77 CDEBUG(D_INFO, "alloc capa failed!\n");
78 RETURN(PTR_ERR(c));
79 } else {
80 c->c_capa = *capa;
81 *oc = c;
82 RETURN(0);
83 }
84}
85
86static inline int mdc_queue_wait(struct ptlrpc_request *req)
87{
88 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
89 int rc;
90
91 /* mdc_enter_request() ensures that this client has no more
92 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
93 * against an MDT. */
94 rc = mdc_enter_request(cli);
95 if (rc != 0)
96 return rc;
97
98 rc = ptlrpc_queue_wait(req);
99 mdc_exit_request(cli);
100
101 return rc;
102}
103
104/* Helper that implements most of mdc_getstatus and signal_completed_replay. */
105/* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
106static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
107 struct obd_capa **pc, int level, int msg_flags)
108{
109 struct ptlrpc_request *req;
110 struct mdt_body *body;
111 int rc;
112 ENTRY;
113
114 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
115 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
116 if (req == NULL)
117 RETURN(-ENOMEM);
118
119 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
120 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
121 req->rq_send_state = level;
122
123 ptlrpc_request_set_replen(req);
124
125 rc = ptlrpc_queue_wait(req);
126 if (rc)
127 GOTO(out, rc);
128
129 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
130 if (body == NULL)
131 GOTO(out, rc = -EPROTO);
132
133 if (body->valid & OBD_MD_FLMDSCAPA) {
134 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
135 if (rc)
136 GOTO(out, rc);
137 }
138
139 *rootfid = body->fid1;
140 CDEBUG(D_NET,
141 "root fid="DFID", last_committed="LPU64"\n",
142 PFID(rootfid),
143 lustre_msg_get_last_committed(req->rq_repmsg));
144 EXIT;
145out:
146 ptlrpc_req_finished(req);
147 return rc;
148}
149
150/* This should be mdc_get_info("rootfid") */
151int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
152 struct obd_capa **pc)
153{
154 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
155 LUSTRE_IMP_FULL, 0);
156}
157
158/*
159 * This function now is known to always saying that it will receive 4 buffers
160 * from server. Even for cases when acl_size and md_size is zero, RPC header
161 * will contain 4 fields and RPC itself will contain zero size fields. This is
162 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
163 * and thus zero, it shrinks it, making zero size. The same story about
164 * md_size. And this is course of problem when client waits for smaller number
165 * of fields. This issue will be fixed later when client gets aware of RPC
166 * layouts. --umka
167 */
168static int mdc_getattr_common(struct obd_export *exp,
169 struct ptlrpc_request *req)
170{
171 struct req_capsule *pill = &req->rq_pill;
172 struct mdt_body *body;
173 void *eadata;
174 int rc;
175 ENTRY;
176
177 /* Request message already built. */
178 rc = ptlrpc_queue_wait(req);
179 if (rc != 0)
180 RETURN(rc);
181
182 /* sanity check for the reply */
183 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
184 if (body == NULL)
185 RETURN(-EPROTO);
186
187 CDEBUG(D_NET, "mode: %o\n", body->mode);
188
189 if (body->eadatasize != 0) {
190 mdc_update_max_ea_from_body(exp, body);
191
192 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
193 body->eadatasize);
194 if (eadata == NULL)
195 RETURN(-EPROTO);
196 }
197
198 if (body->valid & OBD_MD_FLRMTPERM) {
199 struct mdt_remote_perm *perm;
200
201 LASSERT(client_is_remote(exp));
202 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
203 lustre_swab_mdt_remote_perm);
204 if (perm == NULL)
205 RETURN(-EPROTO);
206 }
207
208 if (body->valid & OBD_MD_FLMDSCAPA) {
209 struct lustre_capa *capa;
210 capa = req_capsule_server_get(pill, &RMF_CAPA1);
211 if (capa == NULL)
212 RETURN(-EPROTO);
213 }
214
215 RETURN(0);
216}
217
218int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
219 struct ptlrpc_request **request)
220{
221 struct ptlrpc_request *req;
222 int rc;
223 ENTRY;
224
225 /* Single MDS without an LMV case */
226 if (op_data->op_flags & MF_GET_MDT_IDX) {
227 op_data->op_mds = 0;
228 RETURN(0);
229 }
230 *request = NULL;
231 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
232 if (req == NULL)
233 RETURN(-ENOMEM);
234
235 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
236
237 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
238 if (rc) {
239 ptlrpc_request_free(req);
240 RETURN(rc);
241 }
242
243 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
244 op_data->op_valid, op_data->op_mode, -1, 0);
245
246 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
247 op_data->op_mode);
248 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
249 LASSERT(client_is_remote(exp));
250 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
251 sizeof(struct mdt_remote_perm));
252 }
253 ptlrpc_request_set_replen(req);
254
255 rc = mdc_getattr_common(exp, req);
256 if (rc)
257 ptlrpc_req_finished(req);
258 else
259 *request = req;
260 RETURN(rc);
261}
262
263int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
264 struct ptlrpc_request **request)
265{
266 struct ptlrpc_request *req;
267 int rc;
268 ENTRY;
269
270 *request = NULL;
271 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
272 &RQF_MDS_GETATTR_NAME);
273 if (req == NULL)
274 RETURN(-ENOMEM);
275
276 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
277 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
278 op_data->op_namelen + 1);
279
280 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
281 if (rc) {
282 ptlrpc_request_free(req);
283 RETURN(rc);
284 }
285
286 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
287 op_data->op_valid, op_data->op_mode,
288 op_data->op_suppgids[0], 0);
289
290 if (op_data->op_name) {
291 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
292 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
293 op_data->op_namelen);
294 memcpy(name, op_data->op_name, op_data->op_namelen);
295 }
296
297 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
298 op_data->op_mode);
299 ptlrpc_request_set_replen(req);
300
301 rc = mdc_getattr_common(exp, req);
302 if (rc)
303 ptlrpc_req_finished(req);
304 else
305 *request = req;
306 RETURN(rc);
307}
308
309static int mdc_is_subdir(struct obd_export *exp,
310 const struct lu_fid *pfid,
311 const struct lu_fid *cfid,
312 struct ptlrpc_request **request)
313{
314 struct ptlrpc_request *req;
315 int rc;
316
317 ENTRY;
318
319 *request = NULL;
320 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
321 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
322 MDS_IS_SUBDIR);
323 if (req == NULL)
324 RETURN(-ENOMEM);
325
326 mdc_is_subdir_pack(req, pfid, cfid, 0);
327 ptlrpc_request_set_replen(req);
328
329 rc = ptlrpc_queue_wait(req);
330 if (rc && rc != -EREMOTE)
331 ptlrpc_req_finished(req);
332 else
333 *request = req;
334 RETURN(rc);
335}
336
337static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
338 const struct lu_fid *fid,
339 struct obd_capa *oc, int opcode, obd_valid valid,
340 const char *xattr_name, const char *input,
341 int input_size, int output_size, int flags,
342 __u32 suppgid, struct ptlrpc_request **request)
343{
344 struct ptlrpc_request *req;
345 int xattr_namelen = 0;
346 char *tmp;
347 int rc;
348 ENTRY;
349
350 *request = NULL;
351 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
352 if (req == NULL)
353 RETURN(-ENOMEM);
354
355 mdc_set_capa_size(req, &RMF_CAPA1, oc);
356 if (xattr_name) {
357 xattr_namelen = strlen(xattr_name) + 1;
358 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
359 xattr_namelen);
360 }
361 if (input_size) {
362 LASSERT(input);
363 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
364 input_size);
365 }
366
367 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
368 if (rc) {
369 ptlrpc_request_free(req);
370 RETURN(rc);
371 }
372
373 if (opcode == MDS_REINT) {
374 struct mdt_rec_setxattr *rec;
375
376 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
377 sizeof(struct mdt_rec_reint));
378 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
379 rec->sx_opcode = REINT_SETXATTR;
4b1a25f0
PT
380 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
381 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
d7e09d03
PT
382 rec->sx_cap = cfs_curproc_cap_pack();
383 rec->sx_suppgid1 = suppgid;
384 rec->sx_suppgid2 = -1;
385 rec->sx_fid = *fid;
386 rec->sx_valid = valid | OBD_MD_FLCTIME;
387 rec->sx_time = cfs_time_current_sec();
388 rec->sx_size = output_size;
389 rec->sx_flags = flags;
390
391 mdc_pack_capa(req, &RMF_CAPA1, oc);
392 } else {
393 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
394 }
395
396 if (xattr_name) {
397 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
398 memcpy(tmp, xattr_name, xattr_namelen);
399 }
400 if (input_size) {
401 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
402 memcpy(tmp, input, input_size);
403 }
404
405 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
406 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
407 RCL_SERVER, output_size);
408 ptlrpc_request_set_replen(req);
409
410 /* make rpc */
411 if (opcode == MDS_REINT)
412 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
413
414 rc = ptlrpc_queue_wait(req);
415
416 if (opcode == MDS_REINT)
417 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
418
419 if (rc)
420 ptlrpc_req_finished(req);
421 else
422 *request = req;
423 RETURN(rc);
424}
425
426int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
427 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
428 const char *input, int input_size, int output_size,
429 int flags, __u32 suppgid, struct ptlrpc_request **request)
430{
431 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
432 fid, oc, MDS_REINT, valid, xattr_name,
433 input, input_size, output_size, flags,
434 suppgid, request);
435}
436
437int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
438 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
439 const char *input, int input_size, int output_size,
440 int flags, struct ptlrpc_request **request)
441{
442 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
443 fid, oc, MDS_GETXATTR, valid, xattr_name,
444 input, input_size, output_size, flags,
445 -1, request);
446}
447
448#ifdef CONFIG_FS_POSIX_ACL
449static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
450{
451 struct req_capsule *pill = &req->rq_pill;
452 struct mdt_body *body = md->body;
453 struct posix_acl *acl;
454 void *buf;
455 int rc;
456 ENTRY;
457
458 if (!body->aclsize)
459 RETURN(0);
460
461 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
462
463 if (!buf)
464 RETURN(-EPROTO);
465
466 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
467 if (IS_ERR(acl)) {
468 rc = PTR_ERR(acl);
469 CERROR("convert xattr to acl: %d\n", rc);
470 RETURN(rc);
471 }
472
473 rc = posix_acl_valid(acl);
474 if (rc) {
475 CERROR("validate acl: %d\n", rc);
476 posix_acl_release(acl);
477 RETURN(rc);
478 }
479
480 md->posix_acl = acl;
481 RETURN(0);
482}
483#else
484#define mdc_unpack_acl(req, md) 0
485#endif
486
487int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
488 struct obd_export *dt_exp, struct obd_export *md_exp,
489 struct lustre_md *md)
490{
491 struct req_capsule *pill = &req->rq_pill;
492 int rc;
493 ENTRY;
494
495 LASSERT(md);
496 memset(md, 0, sizeof(*md));
497
498 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
499 LASSERT(md->body != NULL);
500
501 if (md->body->valid & OBD_MD_FLEASIZE) {
502 int lmmsize;
503 struct lov_mds_md *lmm;
504
505 if (!S_ISREG(md->body->mode)) {
506 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
507 "regular file, but is not\n");
508 GOTO(out, rc = -EPROTO);
509 }
510
511 if (md->body->eadatasize == 0) {
512 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
513 "but eadatasize 0\n");
514 GOTO(out, rc = -EPROTO);
515 }
516 lmmsize = md->body->eadatasize;
517 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
518 if (!lmm)
519 GOTO(out, rc = -EPROTO);
520
521 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
522 if (rc < 0)
523 GOTO(out, rc);
524
525 if (rc < sizeof(*md->lsm)) {
526 CDEBUG(D_INFO, "lsm size too small: "
527 "rc < sizeof (*md->lsm) (%d < %d)\n",
528 rc, (int)sizeof(*md->lsm));
529 GOTO(out, rc = -EPROTO);
530 }
531
532 } else if (md->body->valid & OBD_MD_FLDIREA) {
533 int lmvsize;
534 struct lov_mds_md *lmv;
535
536 if(!S_ISDIR(md->body->mode)) {
537 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
538 "directory, but is not\n");
539 GOTO(out, rc = -EPROTO);
540 }
541
542 if (md->body->eadatasize == 0) {
543 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
544 "but eadatasize 0\n");
545 RETURN(-EPROTO);
546 }
547 if (md->body->valid & OBD_MD_MEA) {
548 lmvsize = md->body->eadatasize;
549 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
550 lmvsize);
551 if (!lmv)
552 GOTO(out, rc = -EPROTO);
553
554 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
555 lmvsize);
556 if (rc < 0)
557 GOTO(out, rc);
558
559 if (rc < sizeof(*md->mea)) {
560 CDEBUG(D_INFO, "size too small: "
561 "rc < sizeof(*md->mea) (%d < %d)\n",
562 rc, (int)sizeof(*md->mea));
563 GOTO(out, rc = -EPROTO);
564 }
565 }
566 }
567 rc = 0;
568
569 if (md->body->valid & OBD_MD_FLRMTPERM) {
570 /* remote permission */
571 LASSERT(client_is_remote(exp));
572 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
573 lustre_swab_mdt_remote_perm);
574 if (!md->remote_perm)
575 GOTO(out, rc = -EPROTO);
576 }
577 else if (md->body->valid & OBD_MD_FLACL) {
578 /* for ACL, it's possible that FLACL is set but aclsize is zero.
579 * only when aclsize != 0 there's an actual segment for ACL
580 * in reply buffer.
581 */
582 if (md->body->aclsize) {
583 rc = mdc_unpack_acl(req, md);
584 if (rc)
585 GOTO(out, rc);
586#ifdef CONFIG_FS_POSIX_ACL
587 } else {
588 md->posix_acl = NULL;
589#endif
590 }
591 }
592 if (md->body->valid & OBD_MD_FLMDSCAPA) {
593 struct obd_capa *oc = NULL;
594
595 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
596 if (rc)
597 GOTO(out, rc);
598 md->mds_capa = oc;
599 }
600
601 if (md->body->valid & OBD_MD_FLOSSCAPA) {
602 struct obd_capa *oc = NULL;
603
604 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
605 if (rc)
606 GOTO(out, rc);
607 md->oss_capa = oc;
608 }
609
610 EXIT;
611out:
612 if (rc) {
613 if (md->oss_capa) {
614 capa_put(md->oss_capa);
615 md->oss_capa = NULL;
616 }
617 if (md->mds_capa) {
618 capa_put(md->mds_capa);
619 md->mds_capa = NULL;
620 }
621#ifdef CONFIG_FS_POSIX_ACL
622 posix_acl_release(md->posix_acl);
623#endif
624 if (md->lsm)
625 obd_free_memmd(dt_exp, &md->lsm);
626 }
627 return rc;
628}
629
630int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
631{
632 ENTRY;
633 RETURN(0);
634}
635
636/**
637 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
638 * RPC chains.
639 */
640void mdc_replay_open(struct ptlrpc_request *req)
641{
642 struct md_open_data *mod = req->rq_cb_data;
643 struct ptlrpc_request *close_req;
644 struct obd_client_handle *och;
645 struct lustre_handle old;
646 struct mdt_body *body;
647 ENTRY;
648
649 if (mod == NULL) {
650 DEBUG_REQ(D_ERROR, req,
651 "Can't properly replay without open data.");
652 EXIT;
653 return;
654 }
655
656 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
657 LASSERT(body != NULL);
658
659 och = mod->mod_och;
660 if (och != NULL) {
661 struct lustre_handle *file_fh;
662
663 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
664
665 file_fh = &och->och_fh;
666 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
667 file_fh->cookie, body->handle.cookie);
668 old = *file_fh;
669 *file_fh = body->handle;
670 }
671 close_req = mod->mod_close_req;
672 if (close_req != NULL) {
673 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
674 struct mdt_ioepoch *epoch;
675
676 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
677 epoch = req_capsule_client_get(&close_req->rq_pill,
678 &RMF_MDT_EPOCH);
679 LASSERT(epoch);
680
681 if (och != NULL)
682 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
683 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
684 epoch->handle = body->handle;
685 }
686 EXIT;
687}
688
689void mdc_commit_open(struct ptlrpc_request *req)
690{
691 struct md_open_data *mod = req->rq_cb_data;
692 if (mod == NULL)
693 return;
694
695 /**
696 * No need to touch md_open_data::mod_och, it holds a reference on
697 * \var mod and will zero references to each other, \var mod will be
698 * freed after that when md_open_data::mod_och will put the reference.
699 */
700
701 /**
702 * Do not let open request to disappear as it still may be needed
703 * for close rpc to happen (it may happen on evict only, otherwise
704 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
705 * called), just mark this rpc as committed to distinguish these 2
706 * cases, see mdc_close() for details. The open request reference will
707 * be put along with freeing \var mod.
708 */
709 ptlrpc_request_addref(req);
710 spin_lock(&req->rq_lock);
711 req->rq_committed = 1;
712 spin_unlock(&req->rq_lock);
713 req->rq_cb_data = NULL;
714 obd_mod_put(mod);
715}
716
717int mdc_set_open_replay_data(struct obd_export *exp,
718 struct obd_client_handle *och,
719 struct ptlrpc_request *open_req)
720{
721 struct md_open_data *mod;
722 struct mdt_rec_create *rec;
723 struct mdt_body *body;
724 struct obd_import *imp = open_req->rq_import;
725 ENTRY;
726
727 if (!open_req->rq_replay)
728 RETURN(0);
729
730 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
731 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
732 LASSERT(rec != NULL);
733 /* Incoming message in my byte order (it's been swabbed). */
734 /* Outgoing messages always in my byte order. */
735 LASSERT(body != NULL);
736
737 /* Only if the import is replayable, we set replay_open data */
738 if (och && imp->imp_replayable) {
739 mod = obd_mod_alloc();
740 if (mod == NULL) {
741 DEBUG_REQ(D_ERROR, open_req,
742 "Can't allocate md_open_data");
743 RETURN(0);
744 }
745
746 /**
747 * Take a reference on \var mod, to be freed on mdc_close().
748 * It protects \var mod from being freed on eviction (commit
749 * callback is called despite rq_replay flag).
750 * Another reference for \var och.
751 */
752 obd_mod_get(mod);
753 obd_mod_get(mod);
754
755 spin_lock(&open_req->rq_lock);
756 och->och_mod = mod;
757 mod->mod_och = och;
758 mod->mod_open_req = open_req;
759 open_req->rq_cb_data = mod;
760 open_req->rq_commit_cb = mdc_commit_open;
761 spin_unlock(&open_req->rq_lock);
762 }
763
764 rec->cr_fid2 = body->fid1;
765 rec->cr_ioepoch = body->ioepoch;
766 rec->cr_old_handle.cookie = body->handle.cookie;
767 open_req->rq_replay_cb = mdc_replay_open;
768 if (!fid_is_sane(&body->fid1)) {
769 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
770 "insane fid");
771 LBUG();
772 }
773
774 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
775 RETURN(0);
776}
777
778int mdc_clear_open_replay_data(struct obd_export *exp,
779 struct obd_client_handle *och)
780{
781 struct md_open_data *mod = och->och_mod;
782 ENTRY;
783
784 /**
785 * It is possible to not have \var mod in a case of eviction between
786 * lookup and ll_file_open().
787 **/
788 if (mod == NULL)
789 RETURN(0);
790
791 LASSERT(mod != LP_POISON);
792
793 mod->mod_och = NULL;
794 och->och_mod = NULL;
795 obd_mod_put(mod);
796
797 RETURN(0);
798}
799
800/* Prepares the request for the replay by the given reply */
801static void mdc_close_handle_reply(struct ptlrpc_request *req,
802 struct md_op_data *op_data, int rc) {
803 struct mdt_body *repbody;
804 struct mdt_ioepoch *epoch;
805
806 if (req && rc == -EAGAIN) {
807 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
808 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
809
810 epoch->flags |= MF_SOM_AU;
811 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
812 op_data->op_flags |= MF_GETATTR_LOCK;
813 }
814}
815
816int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
817 struct md_open_data *mod, struct ptlrpc_request **request)
818{
819 struct obd_device *obd = class_exp2obd(exp);
820 struct ptlrpc_request *req;
821 int rc;
822 ENTRY;
823
824 *request = NULL;
825 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
826 if (req == NULL)
827 RETURN(-ENOMEM);
828
829 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
830
831 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
832 if (rc) {
833 ptlrpc_request_free(req);
834 RETURN(rc);
835 }
836
837 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
838 * portal whose threads are not taking any DLM locks and are therefore
839 * always progressing */
840 req->rq_request_portal = MDS_READPAGE_PORTAL;
841 ptlrpc_at_set_req_timeout(req);
842
843 /* Ensure that this close's handle is fixed up during replay. */
844 if (likely(mod != NULL)) {
845 LASSERTF(mod->mod_open_req != NULL &&
846 mod->mod_open_req->rq_type != LI_POISON,
847 "POISONED open %p!\n", mod->mod_open_req);
848
849 mod->mod_close_req = req;
850
851 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
852 /* We no longer want to preserve this open for replay even
853 * though the open was committed. b=3632, b=3633 */
854 spin_lock(&mod->mod_open_req->rq_lock);
855 mod->mod_open_req->rq_replay = 0;
856 spin_unlock(&mod->mod_open_req->rq_lock);
857 } else {
858 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
859 }
860
861 mdc_close_pack(req, op_data);
862
863 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
864 obd->u.cli.cl_max_mds_easize);
865 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
866 obd->u.cli.cl_max_mds_cookiesize);
867
868 ptlrpc_request_set_replen(req);
869
870 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
871 rc = ptlrpc_queue_wait(req);
872 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
873
874 if (req->rq_repmsg == NULL) {
875 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
876 req->rq_status);
877 if (rc == 0)
878 rc = req->rq_status ?: -EIO;
879 } else if (rc == 0 || rc == -EAGAIN) {
880 struct mdt_body *body;
881
882 rc = lustre_msg_get_status(req->rq_repmsg);
883 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
884 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
885 "= %d", rc);
886 if (rc > 0)
887 rc = -rc;
888 }
889 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
890 if (body == NULL)
891 rc = -EPROTO;
892 } else if (rc == -ESTALE) {
893 /**
894 * it can be allowed error after 3633 if open was committed and
895 * server failed before close was sent. Let's check if mod
896 * exists and return no error in that case
897 */
898 if (mod) {
899 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
900 LASSERT(mod->mod_open_req != NULL);
901 if (mod->mod_open_req->rq_committed)
902 rc = 0;
903 }
904 }
905
906 if (mod) {
907 if (rc != 0)
908 mod->mod_close_req = NULL;
909 /* Since now, mod is accessed through open_req only,
910 * thus close req does not keep a reference on mod anymore. */
911 obd_mod_put(mod);
912 }
913 *request = req;
914 mdc_close_handle_reply(req, op_data, rc);
915 RETURN(rc);
916}
917
918int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
919 struct md_open_data *mod)
920{
921 struct obd_device *obd = class_exp2obd(exp);
922 struct ptlrpc_request *req;
923 int rc;
924 ENTRY;
925
926 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
927 &RQF_MDS_DONE_WRITING);
928 if (req == NULL)
929 RETURN(-ENOMEM);
930
931 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
932 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
933 if (rc) {
934 ptlrpc_request_free(req);
935 RETURN(rc);
936 }
937
938 if (mod != NULL) {
939 LASSERTF(mod->mod_open_req != NULL &&
940 mod->mod_open_req->rq_type != LI_POISON,
941 "POISONED setattr %p!\n", mod->mod_open_req);
942
943 mod->mod_close_req = req;
944 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
945 /* We no longer want to preserve this setattr for replay even
946 * though the open was committed. b=3632, b=3633 */
947 spin_lock(&mod->mod_open_req->rq_lock);
948 mod->mod_open_req->rq_replay = 0;
949 spin_unlock(&mod->mod_open_req->rq_lock);
950 }
951
952 mdc_close_pack(req, op_data);
953 ptlrpc_request_set_replen(req);
954
955 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
956 rc = ptlrpc_queue_wait(req);
957 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
958
959 if (rc == -ESTALE) {
960 /**
961 * it can be allowed error after 3633 if open or setattr were
962 * committed and server failed before close was sent.
963 * Let's check if mod exists and return no error in that case
964 */
965 if (mod) {
966 LASSERT(mod->mod_open_req != NULL);
967 if (mod->mod_open_req->rq_committed)
968 rc = 0;
969 }
970 }
971
972 if (mod) {
973 if (rc != 0)
974 mod->mod_close_req = NULL;
975 /* Since now, mod is accessed through setattr req only,
976 * thus DW req does not keep a reference on mod anymore. */
977 obd_mod_put(mod);
978 }
979
980 mdc_close_handle_reply(req, op_data, rc);
981 ptlrpc_req_finished(req);
982 RETURN(rc);
983}
984
985
986int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
987 struct page **pages, struct ptlrpc_request **request)
988{
989 struct ptlrpc_request *req;
990 struct ptlrpc_bulk_desc *desc;
991 int i;
992 wait_queue_head_t waitq;
993 int resends = 0;
994 struct l_wait_info lwi;
995 int rc;
996 ENTRY;
997
998 *request = NULL;
999 init_waitqueue_head(&waitq);
1000
1001restart_bulk:
1002 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1003 if (req == NULL)
1004 RETURN(-ENOMEM);
1005
1006 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1007
1008 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1009 if (rc) {
1010 ptlrpc_request_free(req);
1011 RETURN(rc);
1012 }
1013
1014 req->rq_request_portal = MDS_READPAGE_PORTAL;
1015 ptlrpc_at_set_req_timeout(req);
1016
1017 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1018 MDS_BULK_PORTAL);
1019 if (desc == NULL) {
1020 ptlrpc_request_free(req);
1021 RETURN(-ENOMEM);
1022 }
1023
1024 /* NB req now owns desc and will free it when it gets freed */
1025 for (i = 0; i < op_data->op_npages; i++)
1026 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1027
1028 mdc_readdir_pack(req, op_data->op_offset,
1029 PAGE_CACHE_SIZE * op_data->op_npages,
1030 &op_data->op_fid1, op_data->op_capa1);
1031
1032 ptlrpc_request_set_replen(req);
1033 rc = ptlrpc_queue_wait(req);
1034 if (rc) {
1035 ptlrpc_req_finished(req);
1036 if (rc != -ETIMEDOUT)
1037 RETURN(rc);
1038
1039 resends++;
1040 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1041 CERROR("too many resend retries, returning error\n");
1042 RETURN(-EIO);
1043 }
1044 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1045 l_wait_event(waitq, 0, &lwi);
1046
1047 goto restart_bulk;
1048 }
1049
1050 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1051 req->rq_bulk->bd_nob_transferred);
1052 if (rc < 0) {
1053 ptlrpc_req_finished(req);
1054 RETURN(rc);
1055 }
1056
1057 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1058 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1059 req->rq_bulk->bd_nob_transferred,
1060 PAGE_CACHE_SIZE * op_data->op_npages);
1061 ptlrpc_req_finished(req);
1062 RETURN(-EPROTO);
1063 }
1064
1065 *request = req;
1066 RETURN(0);
1067}
1068
1069static int mdc_statfs(const struct lu_env *env,
1070 struct obd_export *exp, struct obd_statfs *osfs,
1071 __u64 max_age, __u32 flags)
1072{
1073 struct obd_device *obd = class_exp2obd(exp);
1074 struct ptlrpc_request *req;
1075 struct obd_statfs *msfs;
1076 struct obd_import *imp = NULL;
1077 int rc;
1078 ENTRY;
1079
1080 /*
1081 * Since the request might also come from lprocfs, so we need
1082 * sync this with client_disconnect_export Bug15684
1083 */
1084 down_read(&obd->u.cli.cl_sem);
1085 if (obd->u.cli.cl_import)
1086 imp = class_import_get(obd->u.cli.cl_import);
1087 up_read(&obd->u.cli.cl_sem);
1088 if (!imp)
1089 RETURN(-ENODEV);
1090
1091 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1092 LUSTRE_MDS_VERSION, MDS_STATFS);
1093 if (req == NULL)
1094 GOTO(output, rc = -ENOMEM);
1095
1096 ptlrpc_request_set_replen(req);
1097
1098 if (flags & OBD_STATFS_NODELAY) {
1099 /* procfs requests not want stay in wait for avoid deadlock */
1100 req->rq_no_resend = 1;
1101 req->rq_no_delay = 1;
1102 }
1103
1104 rc = ptlrpc_queue_wait(req);
1105 if (rc) {
1106 /* check connection error first */
1107 if (imp->imp_connect_error)
1108 rc = imp->imp_connect_error;
1109 GOTO(out, rc);
1110 }
1111
1112 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1113 if (msfs == NULL)
1114 GOTO(out, rc = -EPROTO);
1115
1116 *osfs = *msfs;
1117 EXIT;
1118out:
1119 ptlrpc_req_finished(req);
1120output:
1121 class_import_put(imp);
1122 return rc;
1123}
1124
1125static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1126{
1127 __u32 keylen, vallen;
1128 void *key;
1129 int rc;
1130
1131 if (gf->gf_pathlen > PATH_MAX)
1132 RETURN(-ENAMETOOLONG);
1133 if (gf->gf_pathlen < 2)
1134 RETURN(-EOVERFLOW);
1135
1136 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1137 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1138 OBD_ALLOC(key, keylen);
1139 if (key == NULL)
1140 RETURN(-ENOMEM);
1141 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1142 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1143
1144 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1145 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1146
1147 if (!fid_is_sane(&gf->gf_fid))
1148 GOTO(out, rc = -EINVAL);
1149
1150 /* Val is struct getinfo_fid2path result plus path */
1151 vallen = sizeof(*gf) + gf->gf_pathlen;
1152
1153 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1154 if (rc != 0 && rc != -EREMOTE)
1155 GOTO(out, rc);
1156
1157 if (vallen <= sizeof(*gf))
1158 GOTO(out, rc = -EPROTO);
1159 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1160 GOTO(out, rc = -EOVERFLOW);
1161
1162 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1163 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1164
1165out:
1166 OBD_FREE(key, keylen);
1167 return rc;
1168}
1169
1170static int mdc_ioc_hsm_progress(struct obd_export *exp,
1171 struct hsm_progress_kernel *hpk)
1172{
1173 struct obd_import *imp = class_exp2cliimp(exp);
1174 struct hsm_progress_kernel *req_hpk;
1175 struct ptlrpc_request *req;
1176 int rc;
1177 ENTRY;
1178
1179 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1180 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1181 if (req == NULL)
1182 GOTO(out, rc = -ENOMEM);
1183
1184 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1185
1186 /* Copy hsm_progress struct */
1187 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1188 if (req_hpk == NULL)
1189 GOTO(out, rc = -EPROTO);
1190
1191 *req_hpk = *hpk;
2d58de78 1192 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
d7e09d03
PT
1193
1194 ptlrpc_request_set_replen(req);
1195
1196 rc = mdc_queue_wait(req);
1197 GOTO(out, rc);
1198out:
1199 ptlrpc_req_finished(req);
1200 return rc;
1201}
1202
1203static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1204{
1205 __u32 *archive_mask;
1206 struct ptlrpc_request *req;
1207 int rc;
1208 ENTRY;
1209
1210 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1211 LUSTRE_MDS_VERSION,
1212 MDS_HSM_CT_REGISTER);
1213 if (req == NULL)
1214 GOTO(out, rc = -ENOMEM);
1215
1216 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1217
1218 /* Copy hsm_progress struct */
1219 archive_mask = req_capsule_client_get(&req->rq_pill,
1220 &RMF_MDS_HSM_ARCHIVE);
1221 if (archive_mask == NULL)
1222 GOTO(out, rc = -EPROTO);
1223
1224 *archive_mask = archives;
1225
1226 ptlrpc_request_set_replen(req);
1227
1228 rc = mdc_queue_wait(req);
1229 GOTO(out, rc);
1230out:
1231 ptlrpc_req_finished(req);
1232 return rc;
1233}
1234
1235static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1236 struct md_op_data *op_data)
1237{
1238 struct hsm_current_action *hca = op_data->op_data;
1239 struct hsm_current_action *req_hca;
1240 struct ptlrpc_request *req;
1241 int rc;
1242 ENTRY;
1243
1244 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1245 &RQF_MDS_HSM_ACTION);
1246 if (req == NULL)
1247 RETURN(-ENOMEM);
1248
1249 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1250
1251 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1252 if (rc) {
1253 ptlrpc_request_free(req);
1254 RETURN(rc);
1255 }
1256
1257 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1258 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1259
1260 ptlrpc_request_set_replen(req);
1261
1262 rc = mdc_queue_wait(req);
1263 if (rc)
1264 GOTO(out, rc);
1265
1266 req_hca = req_capsule_server_get(&req->rq_pill,
1267 &RMF_MDS_HSM_CURRENT_ACTION);
1268 if (req_hca == NULL)
1269 GOTO(out, rc = -EPROTO);
1270
1271 *hca = *req_hca;
1272
1273 EXIT;
1274out:
1275 ptlrpc_req_finished(req);
1276 return rc;
1277}
1278
1279static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1280{
1281 struct ptlrpc_request *req;
1282 int rc;
1283 ENTRY;
1284
1285 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1286 LUSTRE_MDS_VERSION,
1287 MDS_HSM_CT_UNREGISTER);
1288 if (req == NULL)
1289 GOTO(out, rc = -ENOMEM);
1290
1291 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1292
1293 ptlrpc_request_set_replen(req);
1294
1295 rc = mdc_queue_wait(req);
1296 GOTO(out, rc);
1297out:
1298 ptlrpc_req_finished(req);
1299 return rc;
1300}
1301
1302static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1303 struct md_op_data *op_data)
1304{
1305 struct hsm_user_state *hus = op_data->op_data;
1306 struct hsm_user_state *req_hus;
1307 struct ptlrpc_request *req;
1308 int rc;
1309 ENTRY;
1310
1311 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1312 &RQF_MDS_HSM_STATE_GET);
1313 if (req == NULL)
1314 RETURN(-ENOMEM);
1315
1316 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1317
1318 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1319 if (rc != 0) {
1320 ptlrpc_request_free(req);
1321 RETURN(rc);
1322 }
1323
1324 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1325 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1326
1327 ptlrpc_request_set_replen(req);
1328
1329 rc = mdc_queue_wait(req);
1330 if (rc)
1331 GOTO(out, rc);
1332
1333 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1334 if (req_hus == NULL)
1335 GOTO(out, rc = -EPROTO);
1336
1337 *hus = *req_hus;
1338
1339 EXIT;
1340out:
1341 ptlrpc_req_finished(req);
1342 return rc;
1343}
1344
1345static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1346 struct md_op_data *op_data)
1347{
1348 struct hsm_state_set *hss = op_data->op_data;
1349 struct hsm_state_set *req_hss;
1350 struct ptlrpc_request *req;
1351 int rc;
1352 ENTRY;
1353
1354 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1355 &RQF_MDS_HSM_STATE_SET);
1356 if (req == NULL)
1357 RETURN(-ENOMEM);
1358
1359 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1360
1361 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1362 if (rc) {
1363 ptlrpc_request_free(req);
1364 RETURN(rc);
1365 }
1366
1367 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1368 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1369
1370 /* Copy states */
1371 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1372 if (req_hss == NULL)
1373 GOTO(out, rc = -EPROTO);
1374 *req_hss = *hss;
1375
1376 ptlrpc_request_set_replen(req);
1377
1378 rc = mdc_queue_wait(req);
1379 GOTO(out, rc);
1380
1381 EXIT;
1382out:
1383 ptlrpc_req_finished(req);
1384 return rc;
1385}
1386
1387static int mdc_ioc_hsm_request(struct obd_export *exp,
1388 struct hsm_user_request *hur)
1389{
1390 struct obd_import *imp = class_exp2cliimp(exp);
1391 struct ptlrpc_request *req;
1392 struct hsm_request *req_hr;
1393 struct hsm_user_item *req_hui;
1394 char *req_opaque;
1395 int rc;
1396 ENTRY;
1397
1398 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1399 if (req == NULL)
1400 GOTO(out, rc = -ENOMEM);
1401
1402 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1403 hur->hur_request.hr_itemcount
1404 * sizeof(struct hsm_user_item));
1405 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1406 hur->hur_request.hr_data_len);
1407
1408 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1409 if (rc) {
1410 ptlrpc_request_free(req);
1411 RETURN(rc);
1412 }
1413
1414 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1415
1416 /* Copy hsm_request struct */
1417 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1418 if (req_hr == NULL)
1419 GOTO(out, rc = -EPROTO);
1420 *req_hr = hur->hur_request;
1421
1422 /* Copy hsm_user_item structs */
1423 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1424 if (req_hui == NULL)
1425 GOTO(out, rc = -EPROTO);
1426 memcpy(req_hui, hur->hur_user_item,
1427 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1428
1429 /* Copy opaque field */
1430 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1431 if (req_opaque == NULL)
1432 GOTO(out, rc = -EPROTO);
1433 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1434
1435 ptlrpc_request_set_replen(req);
1436
1437 rc = mdc_queue_wait(req);
1438 GOTO(out, rc);
1439
1440out:
1441 ptlrpc_req_finished(req);
1442 return rc;
1443}
1444
1445static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1446{
1447 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1448
1449 LASSERT(len <= CR_MAXSIZE);
1450
1451 lh->kuc_magic = KUC_MAGIC;
1452 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1453 lh->kuc_flags = flags;
1454 lh->kuc_msgtype = CL_RECORD;
1455 lh->kuc_msglen = len;
1456 return lh;
1457}
1458
1459#define D_CHANGELOG 0
1460
1461struct changelog_show {
1462 __u64 cs_startrec;
1463 __u32 cs_flags;
1464 struct file *cs_fp;
1465 char *cs_buf;
1466 struct obd_device *cs_obd;
1467};
1468
e377988e 1469static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
d7e09d03
PT
1470 struct llog_rec_hdr *hdr, void *data)
1471{
1472 struct changelog_show *cs = data;
1473 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1474 struct kuc_hdr *lh;
1475 int len, rc;
1476 ENTRY;
1477
e377988e
AD
1478 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1479 rc = -EINVAL;
1480 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1481 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1482 rec->cr.cr_type, rc);
1483 RETURN(rc);
d7e09d03
PT
1484 }
1485
1486 if (rec->cr.cr_index < cs->cs_startrec) {
1487 /* Skip entries earlier than what we are interested in */
1488 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1489 rec->cr.cr_index, cs->cs_startrec);
1490 RETURN(0);
1491 }
1492
1493 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1494 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1495 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1496 rec->cr.cr_flags & CLF_FLAGMASK,
1497 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1498 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1499
1500 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1501
1502 /* Set up the message */
1503 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1504 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1505
1506 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1507 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1508
1509 RETURN(rc);
1510}
1511
1512static int mdc_changelog_send_thread(void *csdata)
1513{
1514 struct changelog_show *cs = csdata;
1515 struct llog_ctxt *ctxt = NULL;
1516 struct llog_handle *llh = NULL;
1517 struct kuc_hdr *kuch;
1518 int rc;
1519
1520 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1521 cs->cs_fp, cs->cs_startrec);
1522
1523 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1524 if (cs->cs_buf == NULL)
1525 GOTO(out, rc = -ENOMEM);
1526
1527 /* Set up the remote catalog handle */
1528 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1529 if (ctxt == NULL)
1530 GOTO(out, rc = -ENOENT);
1531 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1532 LLOG_OPEN_EXISTS);
1533 if (rc) {
1534 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1535 cs->cs_obd->obd_name, rc);
1536 GOTO(out, rc);
1537 }
1538 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1539 if (rc) {
1540 CERROR("llog_init_handle failed %d\n", rc);
1541 GOTO(out, rc);
1542 }
1543
e377988e 1544 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
d7e09d03
PT
1545
1546 /* Send EOF no matter what our result */
1547 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1548 cs->cs_flags))) {
1549 kuch->kuc_msgtype = CL_EOF;
1550 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1551 }
1552
1553out:
1554 fput(cs->cs_fp);
1555 if (llh)
1556 llog_cat_close(NULL, llh);
1557 if (ctxt)
1558 llog_ctxt_put(ctxt);
1559 if (cs->cs_buf)
1560 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1561 OBD_FREE_PTR(cs);
1562 return rc;
1563}
1564
1565static int mdc_ioc_changelog_send(struct obd_device *obd,
1566 struct ioc_changelog *icc)
1567{
1568 struct changelog_show *cs;
1569 int rc;
1570
1571 /* Freed in mdc_changelog_send_thread */
1572 OBD_ALLOC_PTR(cs);
1573 if (!cs)
1574 return -ENOMEM;
1575
1576 cs->cs_obd = obd;
1577 cs->cs_startrec = icc->icc_recno;
1578 /* matching fput in mdc_changelog_send_thread */
1579 cs->cs_fp = fget(icc->icc_id);
1580 cs->cs_flags = icc->icc_flags;
1581
1582 /*
1583 * New thread because we should return to user app before
1584 * writing into our pipe
1585 */
1586 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1587 "mdc_clg_send_thread"));
1588 if (!IS_ERR_VALUE(rc)) {
1589 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1590 return 0;
1591 }
1592
1593 CERROR("Failed to start changelog thread: %d\n", rc);
1594 OBD_FREE_PTR(cs);
1595 return rc;
1596}
1597
1598static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1599 struct lustre_kernelcomm *lk);
1600
1601static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1602 struct obd_quotactl *oqctl)
1603{
1604 struct client_obd *cli = &exp->exp_obd->u.cli;
1605 struct ptlrpc_request *req;
1606 struct obd_quotactl *body;
1607 int rc;
1608 ENTRY;
1609
1610 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1611 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1612 MDS_QUOTACHECK);
1613 if (req == NULL)
1614 RETURN(-ENOMEM);
1615
1616 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1617 *body = *oqctl;
1618
1619 ptlrpc_request_set_replen(req);
1620
1621 /* the next poll will find -ENODATA, that means quotacheck is
1622 * going on */
1623 cli->cl_qchk_stat = -ENODATA;
1624 rc = ptlrpc_queue_wait(req);
1625 if (rc)
1626 cli->cl_qchk_stat = rc;
1627 ptlrpc_req_finished(req);
1628 RETURN(rc);
1629}
1630
1631static int mdc_quota_poll_check(struct obd_export *exp,
1632 struct if_quotacheck *qchk)
1633{
1634 struct client_obd *cli = &exp->exp_obd->u.cli;
1635 int rc;
1636 ENTRY;
1637
1638 qchk->obd_uuid = cli->cl_target_uuid;
1639 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1640
1641 rc = cli->cl_qchk_stat;
1642 /* the client is not the previous one */
1643 if (rc == CL_NOT_QUOTACHECKED)
1644 rc = -EINTR;
1645 RETURN(rc);
1646}
1647
1648static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1649 struct obd_quotactl *oqctl)
1650{
1651 struct ptlrpc_request *req;
1652 struct obd_quotactl *oqc;
1653 int rc;
1654 ENTRY;
1655
1656 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1657 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1658 MDS_QUOTACTL);
1659 if (req == NULL)
1660 RETURN(-ENOMEM);
1661
1662 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1663 *oqc = *oqctl;
1664
1665 ptlrpc_request_set_replen(req);
1666 ptlrpc_at_set_req_timeout(req);
1667 req->rq_no_resend = 1;
1668
1669 rc = ptlrpc_queue_wait(req);
1670 if (rc)
1671 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1672
1673 if (req->rq_repmsg &&
1674 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1675 *oqctl = *oqc;
1676 } else if (!rc) {
1677 CERROR ("Can't unpack obd_quotactl\n");
1678 rc = -EPROTO;
1679 }
1680 ptlrpc_req_finished(req);
1681
1682 RETURN(rc);
1683}
1684
1685static int mdc_ioc_swap_layouts(struct obd_export *exp,
1686 struct md_op_data *op_data)
1687{
1688 LIST_HEAD(cancels);
1689 struct ptlrpc_request *req;
1690 int rc, count;
1691 struct mdc_swap_layouts *msl, *payload;
1692 ENTRY;
1693
1694 msl = op_data->op_data;
1695
1696 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1697 * first thing it will do is to cancel the 2 layout
1698 * locks hold by this client.
1699 * So the client must cancel its layout locks on the 2 fids
1700 * with the request RPC to avoid extra RPC round trips
1701 */
1702 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1703 LCK_CR, MDS_INODELOCK_LAYOUT);
1704 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1705 LCK_CR, MDS_INODELOCK_LAYOUT);
1706
1707 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1708 &RQF_MDS_SWAP_LAYOUTS);
1709 if (req == NULL) {
1710 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1711 RETURN(-ENOMEM);
1712 }
1713
1714 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1715 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1716
1717 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1718 if (rc) {
1719 ptlrpc_request_free(req);
1720 RETURN(rc);
1721 }
1722
1723 mdc_swap_layouts_pack(req, op_data);
1724
1725 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1726 LASSERT(payload);
1727
1728 *payload = *msl;
1729
1730 ptlrpc_request_set_replen(req);
1731
1732 rc = ptlrpc_queue_wait(req);
1733 if (rc)
1734 GOTO(out, rc);
1735 EXIT;
1736
1737out:
1738 ptlrpc_req_finished(req);
1739 return rc;
1740}
1741
1742static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1743 void *karg, void *uarg)
1744{
1745 struct obd_device *obd = exp->exp_obd;
1746 struct obd_ioctl_data *data = karg;
1747 struct obd_import *imp = obd->u.cli.cl_import;
1748 struct llog_ctxt *ctxt;
1749 int rc;
1750 ENTRY;
1751
1752 if (!try_module_get(THIS_MODULE)) {
1753 CERROR("Can't get module. Is it alive?");
1754 return -EINVAL;
1755 }
1756 switch (cmd) {
1757 case OBD_IOC_CHANGELOG_SEND:
1758 rc = mdc_ioc_changelog_send(obd, karg);
1759 GOTO(out, rc);
1760 case OBD_IOC_CHANGELOG_CLEAR: {
1761 struct ioc_changelog *icc = karg;
1762 struct changelog_setinfo cs =
1763 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1764 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1765 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1766 NULL);
1767 GOTO(out, rc);
1768 }
1769 case OBD_IOC_FID2PATH:
1770 rc = mdc_ioc_fid2path(exp, karg);
1771 GOTO(out, rc);
1772 case LL_IOC_HSM_CT_START:
1773 rc = mdc_ioc_hsm_ct_start(exp, karg);
78eb9092
TL
1774 /* ignore if it was already registered on this MDS. */
1775 if (rc == -EEXIST)
1776 rc = 0;
d7e09d03
PT
1777 GOTO(out, rc);
1778 case LL_IOC_HSM_PROGRESS:
1779 rc = mdc_ioc_hsm_progress(exp, karg);
1780 GOTO(out, rc);
1781 case LL_IOC_HSM_STATE_GET:
1782 rc = mdc_ioc_hsm_state_get(exp, karg);
1783 GOTO(out, rc);
1784 case LL_IOC_HSM_STATE_SET:
1785 rc = mdc_ioc_hsm_state_set(exp, karg);
1786 case LL_IOC_HSM_ACTION:
1787 rc = mdc_ioc_hsm_current_action(exp, karg);
1788 GOTO(out, rc);
1789 case LL_IOC_HSM_REQUEST:
1790 rc = mdc_ioc_hsm_request(exp, karg);
1791 GOTO(out, rc);
1792 case OBD_IOC_CLIENT_RECOVER:
1793 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1794 if (rc < 0)
1795 GOTO(out, rc);
1796 GOTO(out, rc = 0);
1797 case IOC_OSC_SET_ACTIVE:
1798 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1799 GOTO(out, rc);
1800 case OBD_IOC_PARSE: {
1801 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1802 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1803 NULL);
1804 llog_ctxt_put(ctxt);
1805 GOTO(out, rc);
1806 }
1807 case OBD_IOC_LLOG_INFO:
1808 case OBD_IOC_LLOG_PRINT: {
1809 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1810 rc = llog_ioctl(NULL, ctxt, cmd, data);
1811 llog_ctxt_put(ctxt);
1812 GOTO(out, rc);
1813 }
1814 case OBD_IOC_POLL_QUOTACHECK:
1815 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1816 GOTO(out, rc);
1817 case OBD_IOC_PING_TARGET:
1818 rc = ptlrpc_obd_ping(obd);
1819 GOTO(out, rc);
1820 /*
1821 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1822 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1823 * there'd be no LMV layer thus we might be called here. Eventually
1824 * this code should be removed.
1825 * bz20731, LU-592.
1826 */
1827 case IOC_OBD_STATFS: {
1828 struct obd_statfs stat_buf = {0};
1829
1830 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1831 GOTO(out, rc = -ENODEV);
1832
1833 /* copy UUID */
1834 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1835 min((int) data->ioc_plen2,
1836 (int) sizeof(struct obd_uuid))))
1837 GOTO(out, rc = -EFAULT);
1838
1839 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1840 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1841 0);
1842 if (rc != 0)
1843 GOTO(out, rc);
1844
1845 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1846 min((int) data->ioc_plen1,
1847 (int) sizeof(stat_buf))))
1848 GOTO(out, rc = -EFAULT);
1849
1850 GOTO(out, rc = 0);
1851 }
1852 case OBD_IOC_QUOTACTL: {
1853 struct if_quotactl *qctl = karg;
1854 struct obd_quotactl *oqctl;
1855
1856 OBD_ALLOC_PTR(oqctl);
1857 if (!oqctl)
1858 RETURN(-ENOMEM);
1859
1860 QCTL_COPY(oqctl, qctl);
1861 rc = obd_quotactl(exp, oqctl);
1862 if (rc == 0) {
1863 QCTL_COPY(qctl, oqctl);
1864 qctl->qc_valid = QC_MDTIDX;
1865 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1866 }
1867 OBD_FREE_PTR(oqctl);
1868 break;
1869 }
1870 case LL_IOC_GET_CONNECT_FLAGS: {
1871 if (copy_to_user(uarg,
1872 exp_connect_flags_ptr(exp),
1873 sizeof(__u64)))
1874 GOTO(out, rc = -EFAULT);
1875 else
1876 GOTO(out, rc = 0);
1877 }
1878 case LL_IOC_LOV_SWAP_LAYOUTS: {
1879 rc = mdc_ioc_swap_layouts(exp, karg);
1880 break;
1881 }
1882 default:
1883 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1884 GOTO(out, rc = -ENOTTY);
1885 }
1886out:
1887 module_put(THIS_MODULE);
1888
1889 return rc;
1890}
1891
1892int mdc_get_info_rpc(struct obd_export *exp,
1893 obd_count keylen, void *key,
1894 int vallen, void *val)
1895{
1896 struct obd_import *imp = class_exp2cliimp(exp);
1897 struct ptlrpc_request *req;
1898 char *tmp;
1899 int rc = -EINVAL;
1900 ENTRY;
1901
1902 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1903 if (req == NULL)
1904 RETURN(-ENOMEM);
1905
1906 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1907 RCL_CLIENT, keylen);
1908 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1909 RCL_CLIENT, sizeof(__u32));
1910
1911 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1912 if (rc) {
1913 ptlrpc_request_free(req);
1914 RETURN(rc);
1915 }
1916
1917 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1918 memcpy(tmp, key, keylen);
1919 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1920 memcpy(tmp, &vallen, sizeof(__u32));
1921
1922 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1923 RCL_SERVER, vallen);
1924 ptlrpc_request_set_replen(req);
1925
1926 rc = ptlrpc_queue_wait(req);
1927 /* -EREMOTE means the get_info result is partial, and it needs to
1928 * continue on another MDT, see fid2path part in lmv_iocontrol */
1929 if (rc == 0 || rc == -EREMOTE) {
1930 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1931 memcpy(val, tmp, vallen);
1932 if (ptlrpc_rep_need_swab(req)) {
1933 if (KEY_IS(KEY_FID2PATH))
1934 lustre_swab_fid2path(val);
1935 }
1936 }
1937 ptlrpc_req_finished(req);
1938
1939 RETURN(rc);
1940}
1941
1942static void lustre_swab_hai(struct hsm_action_item *h)
1943{
1944 __swab32s(&h->hai_len);
1945 __swab32s(&h->hai_action);
1946 lustre_swab_lu_fid(&h->hai_fid);
1947 lustre_swab_lu_fid(&h->hai_dfid);
1948 __swab64s(&h->hai_cookie);
1949 __swab64s(&h->hai_extent.offset);
1950 __swab64s(&h->hai_extent.length);
1951 __swab64s(&h->hai_gid);
1952}
1953
1954static void lustre_swab_hal(struct hsm_action_list *h)
1955{
1956 struct hsm_action_item *hai;
1957 int i;
1958
1959 __swab32s(&h->hal_version);
1960 __swab32s(&h->hal_count);
1961 __swab32s(&h->hal_archive_id);
1962 __swab64s(&h->hal_flags);
1963 hai = hai_zero(h);
1964 for (i = 0; i < h->hal_count; i++) {
1965 lustre_swab_hai(hai);
1966 hai = hai_next(hai);
1967 }
1968}
1969
1970static void lustre_swab_kuch(struct kuc_hdr *l)
1971{
1972 __swab16s(&l->kuc_magic);
1973 /* __u8 l->kuc_transport */
1974 __swab16s(&l->kuc_msgtype);
1975 __swab16s(&l->kuc_msglen);
1976}
1977
1978static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1979 struct lustre_kernelcomm *lk)
1980{
1981 struct obd_import *imp = class_exp2cliimp(exp);
1982 __u32 archive = lk->lk_data;
1983 int rc = 0;
1984
1985 if (lk->lk_group != KUC_GRP_HSM) {
1986 CERROR("Bad copytool group %d\n", lk->lk_group);
1987 return -EINVAL;
1988 }
1989
1990 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1991 lk->lk_uid, lk->lk_group, lk->lk_flags);
1992
1993 if (lk->lk_flags & LK_FLG_STOP) {
d7e09d03 1994 /* Unregister with the coordinator */
78eb9092 1995 rc = mdc_ioc_hsm_ct_unregister(imp);
d7e09d03 1996 } else {
78eb9092 1997 rc = mdc_ioc_hsm_ct_register(imp, archive);
d7e09d03
PT
1998 }
1999
2000 return rc;
2001}
2002
2003/**
2004 * Send a message to any listening copytools
2005 * @param val KUC message (kuc_hdr + hsm_action_list)
2006 * @param len total length of message
2007 */
2008static int mdc_hsm_copytool_send(int len, void *val)
2009{
2010 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2011 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2012 int rc;
2013 ENTRY;
2014
2015 if (len < sizeof(*lh) + sizeof(*hal)) {
2016 CERROR("Short HSM message %d < %d\n", len,
2017 (int) (sizeof(*lh) + sizeof(*hal)));
2018 RETURN(-EPROTO);
2019 }
2020 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2021 lustre_swab_kuch(lh);
2022 lustre_swab_hal(hal);
2023 } else if (lh->kuc_magic != KUC_MAGIC) {
2024 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2025 RETURN(-EPROTO);
2026 }
2027
2028 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2029 "on %s\n",
2030 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2031 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2032
2033 /* Broadcast to HSM listeners */
2034 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2035
2036 RETURN(rc);
2037}
2038
2039/**
2040 * callback function passed to kuc for re-registering each HSM copytool
2041 * running on MDC, after MDT shutdown/recovery.
2042 * @param data archive id served by the copytool
2043 * @param cb_arg callback argument (obd_import)
2044 */
2045static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2046{
2047 struct obd_import *imp = (struct obd_import *)cb_arg;
2048 __u32 archive = data;
2049 int rc;
2050
2051 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2052 archive);
2053 rc = mdc_ioc_hsm_ct_register(imp, archive);
2054
2055 /* ignore error if the copytool is already registered */
2056 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2057}
2058
2059/**
2060 * Re-establish all kuc contexts with MDT
2061 * after MDT shutdown/recovery.
2062 */
2063static int mdc_kuc_reregister(struct obd_import *imp)
2064{
2065 /* re-register HSM agents */
2066 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2067 (void *)imp);
2068}
2069
2070int mdc_set_info_async(const struct lu_env *env,
2071 struct obd_export *exp,
2072 obd_count keylen, void *key,
2073 obd_count vallen, void *val,
2074 struct ptlrpc_request_set *set)
2075{
2076 struct obd_import *imp = class_exp2cliimp(exp);
2077 int rc;
2078 ENTRY;
2079
2080 if (KEY_IS(KEY_READ_ONLY)) {
2081 if (vallen != sizeof(int))
2082 RETURN(-EINVAL);
2083
2084 spin_lock(&imp->imp_lock);
2085 if (*((int *)val)) {
2086 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2087 imp->imp_connect_data.ocd_connect_flags |=
2088 OBD_CONNECT_RDONLY;
2089 } else {
2090 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2091 imp->imp_connect_data.ocd_connect_flags &=
2092 ~OBD_CONNECT_RDONLY;
2093 }
2094 spin_unlock(&imp->imp_lock);
2095
2096 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2097 keylen, key, vallen, val, set);
2098 RETURN(rc);
2099 }
2100 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2101 sptlrpc_conf_client_adapt(exp->exp_obd);
2102 RETURN(0);
2103 }
2104 if (KEY_IS(KEY_FLUSH_CTX)) {
2105 sptlrpc_import_flush_my_ctx(imp);
2106 RETURN(0);
2107 }
2108 if (KEY_IS(KEY_MDS_CONN)) {
2109 /* mds-mds import */
2110 spin_lock(&imp->imp_lock);
2111 imp->imp_server_timeout = 1;
2112 spin_unlock(&imp->imp_lock);
2113 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2114 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2115 RETURN(0);
2116 }
2117 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2118 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2119 keylen, key, vallen, val, set);
2120 RETURN(rc);
2121 }
2122 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2123 rc = mdc_hsm_copytool_send(vallen, val);
2124 RETURN(rc);
2125 }
2126
2127 CERROR("Unknown key %s\n", (char *)key);
2128 RETURN(-EINVAL);
2129}
2130
2131int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2132 __u32 keylen, void *key, __u32 *vallen, void *val,
2133 struct lov_stripe_md *lsm)
2134{
2135 int rc = -EINVAL;
2136
2137 if (KEY_IS(KEY_MAX_EASIZE)) {
2138 int mdsize, *max_easize;
2139
2140 if (*vallen != sizeof(int))
2141 RETURN(-EINVAL);
2142 mdsize = *(int*)val;
2143 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2144 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2145 max_easize = val;
2146 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2147 RETURN(0);
2148 } else if (KEY_IS(KEY_CONN_DATA)) {
2149 struct obd_import *imp = class_exp2cliimp(exp);
2150 struct obd_connect_data *data = val;
2151
2152 if (*vallen != sizeof(*data))
2153 RETURN(-EINVAL);
2154
2155 *data = imp->imp_connect_data;
2156 RETURN(0);
2157 } else if (KEY_IS(KEY_TGT_COUNT)) {
2158 *((int *)val) = 1;
2159 RETURN(0);
2160 }
2161
2162 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2163
2164 RETURN(rc);
2165}
2166
2167static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2168 struct obd_capa *oc, struct obd_client_handle *handle,
2169 int flags)
2170{
2171 struct ptlrpc_request *req;
2172 struct mdt_body *body;
2173 int rc;
2174 ENTRY;
2175
2176 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2177 if (req == NULL)
2178 RETURN(-ENOMEM);
2179
2180 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2181
2182 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2183 if (rc) {
2184 ptlrpc_request_free(req);
2185 RETURN(rc);
2186 }
2187
2188 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2189
2190 ptlrpc_request_set_replen(req);
2191
2192 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2193 rc = ptlrpc_queue_wait(req);
2194 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2195 if (rc) {
2196 CERROR("Pin failed: %d\n", rc);
2197 GOTO(err_out, rc);
2198 }
2199
2200 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2201 if (body == NULL)
2202 GOTO(err_out, rc = -EPROTO);
2203
2204 handle->och_fh = body->handle;
2205 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2206
2207 handle->och_mod = obd_mod_alloc();
2208 if (handle->och_mod == NULL) {
2209 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2210 GOTO(err_out, rc = -ENOMEM);
2211 }
2212 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2213
2214 RETURN(0);
2215
2216err_out:
2217 ptlrpc_req_finished(req);
2218 RETURN(rc);
2219}
2220
2221static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2222 int flag)
2223{
2224 struct ptlrpc_request *req;
2225 struct mdt_body *body;
2226 int rc;
2227 ENTRY;
2228
2229 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2230 LUSTRE_MDS_VERSION, MDS_UNPIN);
2231 if (req == NULL)
2232 RETURN(-ENOMEM);
2233
2234 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2235 body->handle = handle->och_fh;
2236 body->flags = flag;
2237
2238 ptlrpc_request_set_replen(req);
2239
2240 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2241 rc = ptlrpc_queue_wait(req);
2242 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2243
2244 if (rc != 0)
2245 CERROR("Unpin failed: %d\n", rc);
2246
2247 ptlrpc_req_finished(req);
2248 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2249
2250 obd_mod_put(handle->och_mod);
2251 RETURN(rc);
2252}
2253
2254int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2255 struct obd_capa *oc, struct ptlrpc_request **request)
2256{
2257 struct ptlrpc_request *req;
2258 int rc;
2259 ENTRY;
2260
2261 *request = NULL;
2262 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2263 if (req == NULL)
2264 RETURN(-ENOMEM);
2265
2266 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2267
2268 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2269 if (rc) {
2270 ptlrpc_request_free(req);
2271 RETURN(rc);
2272 }
2273
2274 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2275
2276 ptlrpc_request_set_replen(req);
2277
2278 rc = ptlrpc_queue_wait(req);
2279 if (rc)
2280 ptlrpc_req_finished(req);
2281 else
2282 *request = req;
2283 RETURN(rc);
2284}
2285
2286static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2287 enum obd_import_event event)
2288{
2289 int rc = 0;
2290
2291 LASSERT(imp->imp_obd == obd);
2292
2293 switch (event) {
2294 case IMP_EVENT_DISCON: {
2295#if 0
2296 /* XXX Pass event up to OBDs stack. used only for FLD now */
2297 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2298#endif
2299 break;
2300 }
2301 case IMP_EVENT_INACTIVE: {
2302 struct client_obd *cli = &obd->u.cli;
2303 /*
2304 * Flush current sequence to make client obtain new one
2305 * from server in case of disconnect/reconnect.
2306 */
2307 if (cli->cl_seq != NULL)
2308 seq_client_flush(cli->cl_seq);
2309
2310 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2311 break;
2312 }
2313 case IMP_EVENT_INVALIDATE: {
2314 struct ldlm_namespace *ns = obd->obd_namespace;
2315
2316 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2317
2318 break;
2319 }
2320 case IMP_EVENT_ACTIVE:
2321 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
78eb9092 2322 /* redo the kuc registration after reconnecting */
d7e09d03
PT
2323 if (rc == 0)
2324 rc = mdc_kuc_reregister(imp);
2325 break;
2326 case IMP_EVENT_OCD:
2327 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2328 break;
2329 case IMP_EVENT_DEACTIVATE:
2330 case IMP_EVENT_ACTIVATE:
2331 break;
2332 default:
2333 CERROR("Unknown import event %x\n", event);
2334 LBUG();
2335 }
2336 RETURN(rc);
2337}
2338
2339int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2340 struct md_op_data *op_data)
2341{
2342 struct client_obd *cli = &exp->exp_obd->u.cli;
2343 struct lu_client_seq *seq = cli->cl_seq;
2344 ENTRY;
2345 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2346}
2347
2348struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2349 struct client_obd *cli = &exp->exp_obd->u.cli;
2350 return &cli->cl_target_uuid;
2351}
2352
2353/**
2354 * Determine whether the lock can be canceled before replaying it during
2355 * recovery, non zero value will be return if the lock can be canceled,
2356 * or zero returned for not
2357 */
2358static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2359{
2360 if (lock->l_resource->lr_type != LDLM_IBITS)
2361 RETURN(0);
2362
2363 /* FIXME: if we ever get into a situation where there are too many
2364 * opened files with open locks on a single node, then we really
2365 * should replay these open locks to reget it */
2366 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2367 RETURN(0);
2368
2369 RETURN(1);
2370}
2371
2372static int mdc_resource_inode_free(struct ldlm_resource *res)
2373{
2374 if (res->lr_lvb_inode)
2375 res->lr_lvb_inode = NULL;
2376
2377 return 0;
2378}
2379
2380struct ldlm_valblock_ops inode_lvbo = {
805e517a 2381 .lvbo_free = mdc_resource_inode_free,
d7e09d03
PT
2382};
2383
2384static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2385{
2386 struct client_obd *cli = &obd->u.cli;
2387 struct lprocfs_static_vars lvars = { 0 };
2388 int rc;
2389 ENTRY;
2390
2391 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2392 if (!cli->cl_rpc_lock)
2393 RETURN(-ENOMEM);
2394 mdc_init_rpc_lock(cli->cl_rpc_lock);
2395
2396 ptlrpcd_addref();
2397
2398 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2399 if (!cli->cl_close_lock)
2400 GOTO(err_rpc_lock, rc = -ENOMEM);
2401 mdc_init_rpc_lock(cli->cl_close_lock);
2402
2403 rc = client_obd_setup(obd, cfg);
2404 if (rc)
2405 GOTO(err_close_lock, rc);
2406 lprocfs_mdc_init_vars(&lvars);
2407 lprocfs_obd_setup(obd, lvars.obd_vars);
2408 sptlrpc_lprocfs_cliobd_attach(obd);
2409 ptlrpc_lprocfs_register_obd(obd);
2410
2411 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2412
2413 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2414
2415 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2416 if (rc) {
2417 mdc_cleanup(obd);
2418 CERROR("failed to setup llogging subsystems\n");
2419 }
2420
2421 RETURN(rc);
2422
2423err_close_lock:
2424 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2425err_rpc_lock:
2426 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2427 ptlrpcd_decref();
2428 RETURN(rc);
2429}
2430
2431/* Initialize the default and maximum LOV EA and cookie sizes. This allows
2432 * us to make MDS RPCs with large enough reply buffers to hold the
2433 * maximum-sized (= maximum striped) EA and cookie without having to
2434 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2435static int mdc_init_ea_size(struct obd_export *exp, int easize,
2436 int def_easize, int cookiesize)
2437{
2438 struct obd_device *obd = exp->exp_obd;
2439 struct client_obd *cli = &obd->u.cli;
2440 ENTRY;
2441
2442 if (cli->cl_max_mds_easize < easize)
2443 cli->cl_max_mds_easize = easize;
2444
2445 if (cli->cl_default_mds_easize < def_easize)
2446 cli->cl_default_mds_easize = def_easize;
2447
2448 if (cli->cl_max_mds_cookiesize < cookiesize)
2449 cli->cl_max_mds_cookiesize = cookiesize;
2450
2451 RETURN(0);
2452}
2453
2454static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2455{
2456 int rc = 0;
2457 ENTRY;
2458
2459 switch (stage) {
2460 case OBD_CLEANUP_EARLY:
2461 break;
2462 case OBD_CLEANUP_EXPORTS:
2463 /* Failsafe, ok if racy */
2464 if (obd->obd_type->typ_refcnt <= 1)
2465 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2466
2467 obd_cleanup_client_import(obd);
2468 ptlrpc_lprocfs_unregister_obd(obd);
2469 lprocfs_obd_cleanup(obd);
2470
2471 rc = obd_llog_finish(obd, 0);
2472 if (rc != 0)
2473 CERROR("failed to cleanup llogging subsystems\n");
2474 break;
2475 }
2476 RETURN(rc);
2477}
2478
2479static int mdc_cleanup(struct obd_device *obd)
2480{
2481 struct client_obd *cli = &obd->u.cli;
2482
2483 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2484 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2485
2486 ptlrpcd_decref();
2487
2488 return client_obd_cleanup(obd);
2489}
2490
2491
2492static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2493 struct obd_device *tgt, int *index)
2494{
2495 struct llog_ctxt *ctxt;
2496 int rc;
2497
2498 ENTRY;
2499
2500 LASSERT(olg == &obd->obd_olg);
2501
2502 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2503 &llog_client_ops);
2504 if (rc)
2505 RETURN(rc);
2506
2507 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2508 llog_initiator_connect(ctxt);
2509 llog_ctxt_put(ctxt);
2510
2511 RETURN(0);
2512}
2513
2514static int mdc_llog_finish(struct obd_device *obd, int count)
2515{
2516 struct llog_ctxt *ctxt;
2517
2518 ENTRY;
2519
2520 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2521 if (ctxt)
2522 llog_cleanup(NULL, ctxt);
2523
2524 RETURN(0);
2525}
2526
2527static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2528{
2529 struct lustre_cfg *lcfg = buf;
2530 struct lprocfs_static_vars lvars = { 0 };
2531 int rc = 0;
2532
2533 lprocfs_mdc_init_vars(&lvars);
2534 switch (lcfg->lcfg_command) {
2535 default:
2536 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2537 lcfg, obd);
2538 if (rc > 0)
2539 rc = 0;
2540 break;
2541 }
2542 return(rc);
2543}
2544
2545
2546/* get remote permission for current user on fid */
2547int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2548 struct obd_capa *oc, __u32 suppgid,
2549 struct ptlrpc_request **request)
2550{
2551 struct ptlrpc_request *req;
2552 int rc;
2553 ENTRY;
2554
2555 LASSERT(client_is_remote(exp));
2556
2557 *request = NULL;
2558 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2559 if (req == NULL)
2560 RETURN(-ENOMEM);
2561
2562 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2563
2564 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2565 if (rc) {
2566 ptlrpc_request_free(req);
2567 RETURN(rc);
2568 }
2569
2570 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2571
2572 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2573 sizeof(struct mdt_remote_perm));
2574
2575 ptlrpc_request_set_replen(req);
2576
2577 rc = ptlrpc_queue_wait(req);
2578 if (rc)
2579 ptlrpc_req_finished(req);
2580 else
2581 *request = req;
2582 RETURN(rc);
2583}
2584
2585static int mdc_interpret_renew_capa(const struct lu_env *env,
2586 struct ptlrpc_request *req, void *args,
2587 int status)
2588{
2589 struct mdc_renew_capa_args *ra = args;
2590 struct mdt_body *body = NULL;
2591 struct lustre_capa *capa;
2592 ENTRY;
2593
2594 if (status)
2595 GOTO(out, capa = ERR_PTR(status));
2596
2597 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2598 if (body == NULL)
2599 GOTO(out, capa = ERR_PTR(-EFAULT));
2600
2601 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2602 GOTO(out, capa = ERR_PTR(-ENOENT));
2603
2604 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2605 if (!capa)
2606 GOTO(out, capa = ERR_PTR(-EFAULT));
2607 EXIT;
2608out:
2609 ra->ra_cb(ra->ra_oc, capa);
2610 return 0;
2611}
2612
2613static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2614 renew_capa_cb_t cb)
2615{
2616 struct ptlrpc_request *req;
2617 struct mdc_renew_capa_args *ra;
2618 ENTRY;
2619
2620 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2621 LUSTRE_MDS_VERSION, MDS_GETATTR);
2622 if (req == NULL)
2623 RETURN(-ENOMEM);
2624
2625 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2626 * capa to renew is oss capa.
2627 */
2628 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2629 ptlrpc_request_set_replen(req);
2630
2631 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2632 ra = ptlrpc_req_async_args(req);
2633 ra->ra_oc = oc;
2634 ra->ra_cb = cb;
2635 req->rq_interpret_reply = mdc_interpret_renew_capa;
2636 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2637 RETURN(0);
2638}
2639
2640static int mdc_connect(const struct lu_env *env,
2641 struct obd_export **exp,
2642 struct obd_device *obd, struct obd_uuid *cluuid,
2643 struct obd_connect_data *data,
2644 void *localdata)
2645{
2646 struct obd_import *imp = obd->u.cli.cl_import;
2647
2648 /* mds-mds import features */
2649 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2650 spin_lock(&imp->imp_lock);
2651 imp->imp_server_timeout = 1;
2652 spin_unlock(&imp->imp_lock);
2653 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2654 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2655 obd->obd_name);
2656 }
2657
2658 return client_connect_import(env, exp, obd, cluuid, data, NULL);
2659}
2660
2661struct obd_ops mdc_obd_ops = {
2662 .o_owner = THIS_MODULE,
2663 .o_setup = mdc_setup,
2664 .o_precleanup = mdc_precleanup,
2665 .o_cleanup = mdc_cleanup,
2666 .o_add_conn = client_import_add_conn,
2667 .o_del_conn = client_import_del_conn,
2668 .o_connect = mdc_connect,
2669 .o_disconnect = client_disconnect_export,
2670 .o_iocontrol = mdc_iocontrol,
2671 .o_set_info_async = mdc_set_info_async,
2672 .o_statfs = mdc_statfs,
2673 .o_pin = mdc_pin,
2674 .o_unpin = mdc_unpin,
2675 .o_fid_init = client_fid_init,
2676 .o_fid_fini = client_fid_fini,
2677 .o_fid_alloc = mdc_fid_alloc,
2678 .o_import_event = mdc_import_event,
2679 .o_llog_init = mdc_llog_init,
2680 .o_llog_finish = mdc_llog_finish,
2681 .o_get_info = mdc_get_info,
2682 .o_process_config = mdc_process_config,
2683 .o_get_uuid = mdc_get_uuid,
2684 .o_quotactl = mdc_quotactl,
2685 .o_quotacheck = mdc_quotacheck
2686};
2687
2688struct md_ops mdc_md_ops = {
2689 .m_getstatus = mdc_getstatus,
2690 .m_null_inode = mdc_null_inode,
2691 .m_find_cbdata = mdc_find_cbdata,
2692 .m_close = mdc_close,
2693 .m_create = mdc_create,
2694 .m_done_writing = mdc_done_writing,
2695 .m_enqueue = mdc_enqueue,
2696 .m_getattr = mdc_getattr,
2697 .m_getattr_name = mdc_getattr_name,
2698 .m_intent_lock = mdc_intent_lock,
2699 .m_link = mdc_link,
2700 .m_is_subdir = mdc_is_subdir,
2701 .m_rename = mdc_rename,
2702 .m_setattr = mdc_setattr,
2703 .m_setxattr = mdc_setxattr,
2704 .m_getxattr = mdc_getxattr,
2705 .m_sync = mdc_sync,
2706 .m_readpage = mdc_readpage,
2707 .m_unlink = mdc_unlink,
2708 .m_cancel_unused = mdc_cancel_unused,
2709 .m_init_ea_size = mdc_init_ea_size,
2710 .m_set_lock_data = mdc_set_lock_data,
2711 .m_lock_match = mdc_lock_match,
2712 .m_get_lustre_md = mdc_get_lustre_md,
2713 .m_free_lustre_md = mdc_free_lustre_md,
2714 .m_set_open_replay_data = mdc_set_open_replay_data,
2715 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2716 .m_renew_capa = mdc_renew_capa,
2717 .m_unpack_capa = mdc_unpack_capa,
2718 .m_get_remote_perm = mdc_get_remote_perm,
2719 .m_intent_getattr_async = mdc_intent_getattr_async,
2720 .m_revalidate_lock = mdc_revalidate_lock
2721};
2722
2723int __init mdc_init(void)
2724{
2725 int rc;
2726 struct lprocfs_static_vars lvars = { 0 };
2727 lprocfs_mdc_init_vars(&lvars);
2728
2729 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2730 LUSTRE_MDC_NAME, NULL);
2731 RETURN(rc);
2732}
2733
2734static void /*__exit*/ mdc_exit(void)
2735{
2736 class_unregister_type(LUSTRE_MDC_NAME);
2737}
2738
2739MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2740MODULE_DESCRIPTION("Lustre Metadata Client");
2741MODULE_LICENSE("GPL");
2742
2743module_init(mdc_init);
2744module_exit(mdc_exit);
This page took 0.223267 seconds and 5 git commands to generate.