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