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