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