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