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