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