blob: fd6fc10349c3097c0267a97296d8c267b60fa3da [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Greg Kroah-Hartman05932302014-07-11 22:04:56 -070045#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"
Peng Taod7e09d02013-05-02 16:46:55 +080051
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
Janet Liu60ebee32014-11-30 18:08:10 +080063static int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
Peng Taod7e09d02013-05-02 16:46:55 +080064 const struct req_msg_field *field, struct obd_capa **oc)
65{
66 struct lustre_capa *capa;
67 struct obd_capa *c;
Peng Taod7e09d02013-05-02 16:46:55 +080068
69 /* swabbed already in mdc_enqueue */
70 capa = req_capsule_server_get(&req->rq_pill, field);
71 if (capa == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080072 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +080073
74 c = alloc_capa(CAPA_SITE_CLIENT);
75 if (IS_ERR(c)) {
76 CDEBUG(D_INFO, "alloc capa failed!\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080077 return PTR_ERR(c);
Peng Taod7e09d02013-05-02 16:46:55 +080078 }
Antonio Murdaca71e8dd92015-06-08 21:48:41 +020079
80 c->c_capa = *capa;
81 *oc = c;
82 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +080083}
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;
Peng Taod7e09d02013-05-02 16:46:55 +0800111
112 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
113 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
114 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800115 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800116
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)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200125 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800126
127 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200128 if (body == NULL) {
129 rc = -EPROTO;
130 goto out;
131 }
Peng Taod7e09d02013-05-02 16:46:55 +0800132
133 if (body->valid & OBD_MD_FLMDSCAPA) {
134 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
135 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200136 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800137 }
138
139 *rootfid = body->fid1;
140 CDEBUG(D_NET,
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700141 "root fid="DFID", last_committed=%llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800142 PFID(rootfid),
143 lustre_msg_get_last_committed(req->rq_repmsg));
Peng Taod7e09d02013-05-02 16:46:55 +0800144out:
145 ptlrpc_req_finished(req);
146 return rc;
147}
148
149/* This should be mdc_get_info("rootfid") */
Janet Liu60ebee32014-11-30 18:08:10 +0800150static int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
Peng Taod7e09d02013-05-02 16:46:55 +0800151 struct obd_capa **pc)
152{
153 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
154 LUSTRE_IMP_FULL, 0);
155}
156
157/*
158 * This function now is known to always saying that it will receive 4 buffers
159 * from server. Even for cases when acl_size and md_size is zero, RPC header
160 * will contain 4 fields and RPC itself will contain zero size fields. This is
161 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
162 * and thus zero, it shrinks it, making zero size. The same story about
163 * md_size. And this is course of problem when client waits for smaller number
164 * of fields. This issue will be fixed later when client gets aware of RPC
165 * layouts. --umka
166 */
167static int mdc_getattr_common(struct obd_export *exp,
168 struct ptlrpc_request *req)
169{
170 struct req_capsule *pill = &req->rq_pill;
171 struct mdt_body *body;
172 void *eadata;
173 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800174
175 /* Request message already built. */
176 rc = ptlrpc_queue_wait(req);
177 if (rc != 0)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800178 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800179
180 /* sanity check for the reply */
181 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
182 if (body == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800183 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800184
185 CDEBUG(D_NET, "mode: %o\n", body->mode);
186
187 if (body->eadatasize != 0) {
188 mdc_update_max_ea_from_body(exp, body);
189
190 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
191 body->eadatasize);
192 if (eadata == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800193 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800194 }
195
196 if (body->valid & OBD_MD_FLRMTPERM) {
197 struct mdt_remote_perm *perm;
198
199 LASSERT(client_is_remote(exp));
200 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
201 lustre_swab_mdt_remote_perm);
202 if (perm == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800203 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800204 }
205
206 if (body->valid & OBD_MD_FLMDSCAPA) {
207 struct lustre_capa *capa;
Srikrishan Malik7436d072014-08-11 23:57:33 +0530208
Peng Taod7e09d02013-05-02 16:46:55 +0800209 capa = req_capsule_server_get(pill, &RMF_CAPA1);
210 if (capa == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800211 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800212 }
213
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800214 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800215}
216
Janet Liu60ebee32014-11-30 18:08:10 +0800217static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
Peng Taod7e09d02013-05-02 16:46:55 +0800218 struct ptlrpc_request **request)
219{
220 struct ptlrpc_request *req;
221 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800222
223 /* Single MDS without an LMV case */
224 if (op_data->op_flags & MF_GET_MDT_IDX) {
225 op_data->op_mds = 0;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800226 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800227 }
228 *request = NULL;
229 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
230 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800231 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800232
233 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
234
235 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
236 if (rc) {
237 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800238 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800239 }
240
241 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
242 op_data->op_valid, op_data->op_mode, -1, 0);
243
244 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
245 op_data->op_mode);
246 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
247 LASSERT(client_is_remote(exp));
248 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
249 sizeof(struct mdt_remote_perm));
250 }
251 ptlrpc_request_set_replen(req);
252
253 rc = mdc_getattr_common(exp, req);
254 if (rc)
255 ptlrpc_req_finished(req);
256 else
257 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800258 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800259}
260
Janet Liu60ebee32014-11-30 18:08:10 +0800261static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
Peng Taod7e09d02013-05-02 16:46:55 +0800262 struct ptlrpc_request **request)
263{
264 struct ptlrpc_request *req;
265 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800266
267 *request = NULL;
268 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
269 &RQF_MDS_GETATTR_NAME);
270 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800271 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800272
273 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
274 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
275 op_data->op_namelen + 1);
276
277 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
278 if (rc) {
279 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800280 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800281 }
282
283 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
284 op_data->op_valid, op_data->op_mode,
285 op_data->op_suppgids[0], 0);
286
287 if (op_data->op_name) {
288 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
Srikrishan Malik7436d072014-08-11 23:57:33 +0530289
Peng Taod7e09d02013-05-02 16:46:55 +0800290 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
291 op_data->op_namelen);
292 memcpy(name, op_data->op_name, op_data->op_namelen);
293 }
294
295 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
296 op_data->op_mode);
297 ptlrpc_request_set_replen(req);
298
299 rc = mdc_getattr_common(exp, req);
300 if (rc)
301 ptlrpc_req_finished(req);
302 else
303 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800304 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800305}
306
307static int mdc_is_subdir(struct obd_export *exp,
308 const struct lu_fid *pfid,
309 const struct lu_fid *cfid,
310 struct ptlrpc_request **request)
311{
312 struct ptlrpc_request *req;
313 int rc;
314
Peng Taod7e09d02013-05-02 16:46:55 +0800315 *request = NULL;
316 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
317 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
318 MDS_IS_SUBDIR);
319 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800320 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800321
322 mdc_is_subdir_pack(req, pfid, cfid, 0);
323 ptlrpc_request_set_replen(req);
324
325 rc = ptlrpc_queue_wait(req);
326 if (rc && rc != -EREMOTE)
327 ptlrpc_req_finished(req);
328 else
329 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800330 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800331}
332
Srikrishan Malik301af902014-08-11 23:57:31 +0530333static int mdc_xattr_common(struct obd_export *exp,
334 const struct req_format *fmt,
Peng Taod7e09d02013-05-02 16:46:55 +0800335 const struct lu_fid *fid,
Oleg Drokin21aef7d2014-08-15 12:55:56 -0400336 struct obd_capa *oc, int opcode, u64 valid,
Peng Taod7e09d02013-05-02 16:46:55 +0800337 const char *xattr_name, const char *input,
338 int input_size, int output_size, int flags,
339 __u32 suppgid, struct ptlrpc_request **request)
340{
341 struct ptlrpc_request *req;
342 int xattr_namelen = 0;
343 char *tmp;
344 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800345
346 *request = NULL;
347 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
348 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800349 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800350
351 mdc_set_capa_size(req, &RMF_CAPA1, oc);
352 if (xattr_name) {
353 xattr_namelen = strlen(xattr_name) + 1;
354 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
355 xattr_namelen);
356 }
357 if (input_size) {
358 LASSERT(input);
359 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
360 input_size);
361 }
362
Andrew Perepechkoe93a3082014-02-09 02:51:48 -0500363 /* Flush local XATTR locks to get rid of a possible cancel RPC */
364 if (opcode == MDS_REINT && fid_is_sane(fid) &&
365 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
366 LIST_HEAD(cancels);
367 int count;
368
369 /* Without that packing would fail */
370 if (input_size == 0)
371 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
372 RCL_CLIENT, 0);
373
374 count = mdc_resource_get_unused(exp, fid,
375 &cancels, LCK_EX,
376 MDS_INODELOCK_XATTR);
377
378 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
379 if (rc) {
380 ptlrpc_request_free(req);
381 return rc;
382 }
383 } else {
384 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
385 if (rc) {
386 ptlrpc_request_free(req);
387 return rc;
388 }
Peng Taod7e09d02013-05-02 16:46:55 +0800389 }
390
391 if (opcode == MDS_REINT) {
392 struct mdt_rec_setxattr *rec;
393
394 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
395 sizeof(struct mdt_rec_reint));
396 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
397 rec->sx_opcode = REINT_SETXATTR;
Peng Tao4b1a25f2013-07-15 22:27:14 +0800398 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
399 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
Peng Taod7e09d02013-05-02 16:46:55 +0800400 rec->sx_cap = cfs_curproc_cap_pack();
401 rec->sx_suppgid1 = suppgid;
402 rec->sx_suppgid2 = -1;
403 rec->sx_fid = *fid;
404 rec->sx_valid = valid | OBD_MD_FLCTIME;
Arnd Bergmann14e3f922015-09-27 16:45:27 -0400405 rec->sx_time = ktime_get_real_seconds();
Peng Taod7e09d02013-05-02 16:46:55 +0800406 rec->sx_size = output_size;
407 rec->sx_flags = flags;
408
409 mdc_pack_capa(req, &RMF_CAPA1, oc);
410 } else {
411 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
412 }
413
414 if (xattr_name) {
415 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
416 memcpy(tmp, xattr_name, xattr_namelen);
417 }
418 if (input_size) {
419 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
420 memcpy(tmp, input, input_size);
421 }
422
423 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
424 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
425 RCL_SERVER, output_size);
426 ptlrpc_request_set_replen(req);
427
428 /* make rpc */
429 if (opcode == MDS_REINT)
430 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
431
432 rc = ptlrpc_queue_wait(req);
433
434 if (opcode == MDS_REINT)
435 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
436
437 if (rc)
438 ptlrpc_req_finished(req);
439 else
440 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800441 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800442}
443
Janet Liu60ebee32014-11-30 18:08:10 +0800444static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokin21aef7d2014-08-15 12:55:56 -0400445 struct obd_capa *oc, u64 valid, const char *xattr_name,
Peng Taod7e09d02013-05-02 16:46:55 +0800446 const char *input, int input_size, int output_size,
447 int flags, __u32 suppgid, struct ptlrpc_request **request)
448{
449 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
450 fid, oc, MDS_REINT, valid, xattr_name,
451 input, input_size, output_size, flags,
452 suppgid, request);
453}
454
Janet Liu60ebee32014-11-30 18:08:10 +0800455static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokin21aef7d2014-08-15 12:55:56 -0400456 struct obd_capa *oc, u64 valid, const char *xattr_name,
Peng Taod7e09d02013-05-02 16:46:55 +0800457 const char *input, int input_size, int output_size,
458 int flags, struct ptlrpc_request **request)
459{
460 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
461 fid, oc, MDS_GETXATTR, valid, xattr_name,
462 input, input_size, output_size, flags,
463 -1, request);
464}
465
466#ifdef CONFIG_FS_POSIX_ACL
467static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
468{
469 struct req_capsule *pill = &req->rq_pill;
470 struct mdt_body *body = md->body;
471 struct posix_acl *acl;
472 void *buf;
473 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800474
475 if (!body->aclsize)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800476 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800477
478 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
479
480 if (!buf)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800481 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800482
483 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
Christopher J. Morronef4289402015-03-25 21:53:18 -0400484 if (acl == NULL)
485 return 0;
486
Peng Taod7e09d02013-05-02 16:46:55 +0800487 if (IS_ERR(acl)) {
488 rc = PTR_ERR(acl);
489 CERROR("convert xattr to acl: %d\n", rc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800490 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800491 }
492
493 rc = posix_acl_valid(acl);
494 if (rc) {
495 CERROR("validate acl: %d\n", rc);
496 posix_acl_release(acl);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800497 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800498 }
499
500 md->posix_acl = acl;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800501 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800502}
503#else
504#define mdc_unpack_acl(req, md) 0
505#endif
506
507int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
508 struct obd_export *dt_exp, struct obd_export *md_exp,
509 struct lustre_md *md)
510{
511 struct req_capsule *pill = &req->rq_pill;
512 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800513
514 LASSERT(md);
515 memset(md, 0, sizeof(*md));
516
517 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
518 LASSERT(md->body != NULL);
519
520 if (md->body->valid & OBD_MD_FLEASIZE) {
521 int lmmsize;
522 struct lov_mds_md *lmm;
523
524 if (!S_ISREG(md->body->mode)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530525 CDEBUG(D_INFO,
526 "OBD_MD_FLEASIZE set, should be a regular file, but is not\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200527 rc = -EPROTO;
528 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800529 }
530
531 if (md->body->eadatasize == 0) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530532 CDEBUG(D_INFO,
533 "OBD_MD_FLEASIZE set, but eadatasize 0\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200534 rc = -EPROTO;
535 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800536 }
537 lmmsize = md->body->eadatasize;
538 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200539 if (!lmm) {
540 rc = -EPROTO;
541 goto out;
542 }
Peng Taod7e09d02013-05-02 16:46:55 +0800543
544 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
545 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200546 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800547
548 if (rc < sizeof(*md->lsm)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530549 CDEBUG(D_INFO,
550 "lsm size too small: rc < sizeof (*md->lsm) (%d < %d)\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800551 rc, (int)sizeof(*md->lsm));
Julia Lawalld5fdc202014-08-28 12:10:35 +0200552 rc = -EPROTO;
553 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800554 }
555
556 } else if (md->body->valid & OBD_MD_FLDIREA) {
557 int lmvsize;
558 struct lov_mds_md *lmv;
559
Srikrishan Malik301af902014-08-11 23:57:31 +0530560 if (!S_ISDIR(md->body->mode)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530561 CDEBUG(D_INFO,
562 "OBD_MD_FLDIREA set, should be a directory, but is not\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200563 rc = -EPROTO;
564 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800565 }
566
567 if (md->body->eadatasize == 0) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530568 CDEBUG(D_INFO,
569 "OBD_MD_FLDIREA is set, but eadatasize 0\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800570 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800571 }
572 if (md->body->valid & OBD_MD_MEA) {
573 lmvsize = md->body->eadatasize;
574 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
575 lmvsize);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200576 if (!lmv) {
577 rc = -EPROTO;
578 goto out;
579 }
Peng Taod7e09d02013-05-02 16:46:55 +0800580
581 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
582 lmvsize);
583 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200584 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800585
586 if (rc < sizeof(*md->mea)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530587 CDEBUG(D_INFO,
588 "size too small: rc < sizeof(*md->mea) (%d < %d)\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800589 rc, (int)sizeof(*md->mea));
Julia Lawalld5fdc202014-08-28 12:10:35 +0200590 rc = -EPROTO;
591 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800592 }
593 }
594 }
595 rc = 0;
596
597 if (md->body->valid & OBD_MD_FLRMTPERM) {
598 /* remote permission */
599 LASSERT(client_is_remote(exp));
600 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
601 lustre_swab_mdt_remote_perm);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200602 if (!md->remote_perm) {
603 rc = -EPROTO;
604 goto out;
605 }
Srikrishan Malik78dd0792014-08-11 23:57:37 +0530606 } else if (md->body->valid & OBD_MD_FLACL) {
Peng Taod7e09d02013-05-02 16:46:55 +0800607 /* for ACL, it's possible that FLACL is set but aclsize is zero.
608 * only when aclsize != 0 there's an actual segment for ACL
609 * in reply buffer.
610 */
611 if (md->body->aclsize) {
612 rc = mdc_unpack_acl(req, md);
613 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200614 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800615#ifdef CONFIG_FS_POSIX_ACL
616 } else {
617 md->posix_acl = NULL;
618#endif
619 }
620 }
621 if (md->body->valid & OBD_MD_FLMDSCAPA) {
622 struct obd_capa *oc = NULL;
623
624 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
625 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200626 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800627 md->mds_capa = oc;
628 }
629
630 if (md->body->valid & OBD_MD_FLOSSCAPA) {
631 struct obd_capa *oc = NULL;
632
633 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
634 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200635 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800636 md->oss_capa = oc;
637 }
638
Peng Taod7e09d02013-05-02 16:46:55 +0800639out:
640 if (rc) {
641 if (md->oss_capa) {
642 capa_put(md->oss_capa);
643 md->oss_capa = NULL;
644 }
645 if (md->mds_capa) {
646 capa_put(md->mds_capa);
647 md->mds_capa = NULL;
648 }
649#ifdef CONFIG_FS_POSIX_ACL
650 posix_acl_release(md->posix_acl);
651#endif
652 if (md->lsm)
653 obd_free_memmd(dt_exp, &md->lsm);
654 }
655 return rc;
656}
657
658int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
659{
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800660 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800661}
662
663/**
664 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
665 * RPC chains.
666 */
667void mdc_replay_open(struct ptlrpc_request *req)
668{
669 struct md_open_data *mod = req->rq_cb_data;
670 struct ptlrpc_request *close_req;
671 struct obd_client_handle *och;
672 struct lustre_handle old;
673 struct mdt_body *body;
Peng Taod7e09d02013-05-02 16:46:55 +0800674
675 if (mod == NULL) {
676 DEBUG_REQ(D_ERROR, req,
677 "Can't properly replay without open data.");
Peng Taod7e09d02013-05-02 16:46:55 +0800678 return;
679 }
680
681 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
682 LASSERT(body != NULL);
683
684 och = mod->mod_och;
685 if (och != NULL) {
686 struct lustre_handle *file_fh;
687
688 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
689
690 file_fh = &och->och_fh;
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700691 CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800692 file_fh->cookie, body->handle.cookie);
693 old = *file_fh;
694 *file_fh = body->handle;
695 }
696 close_req = mod->mod_close_req;
697 if (close_req != NULL) {
698 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
699 struct mdt_ioepoch *epoch;
700
701 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
702 epoch = req_capsule_client_get(&close_req->rq_pill,
703 &RMF_MDT_EPOCH);
704 LASSERT(epoch);
705
706 if (och != NULL)
707 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
708 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
709 epoch->handle = body->handle;
710 }
Peng Taod7e09d02013-05-02 16:46:55 +0800711}
712
713void mdc_commit_open(struct ptlrpc_request *req)
714{
715 struct md_open_data *mod = req->rq_cb_data;
Srikrishan Malik7436d072014-08-11 23:57:33 +0530716
Peng Taod7e09d02013-05-02 16:46:55 +0800717 if (mod == NULL)
718 return;
719
720 /**
721 * No need to touch md_open_data::mod_och, it holds a reference on
722 * \var mod and will zero references to each other, \var mod will be
723 * freed after that when md_open_data::mod_och will put the reference.
724 */
725
726 /**
727 * Do not let open request to disappear as it still may be needed
728 * for close rpc to happen (it may happen on evict only, otherwise
729 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
730 * called), just mark this rpc as committed to distinguish these 2
731 * cases, see mdc_close() for details. The open request reference will
732 * be put along with freeing \var mod.
733 */
734 ptlrpc_request_addref(req);
735 spin_lock(&req->rq_lock);
736 req->rq_committed = 1;
737 spin_unlock(&req->rq_lock);
738 req->rq_cb_data = NULL;
739 obd_mod_put(mod);
740}
741
742int mdc_set_open_replay_data(struct obd_export *exp,
743 struct obd_client_handle *och,
Hongchao Zhang63d42572014-02-28 21:16:37 -0500744 struct lookup_intent *it)
Peng Taod7e09d02013-05-02 16:46:55 +0800745{
746 struct md_open_data *mod;
747 struct mdt_rec_create *rec;
748 struct mdt_body *body;
Hongchao Zhang63d42572014-02-28 21:16:37 -0500749 struct ptlrpc_request *open_req = it->d.lustre.it_data;
Peng Taod7e09d02013-05-02 16:46:55 +0800750 struct obd_import *imp = open_req->rq_import;
Peng Taod7e09d02013-05-02 16:46:55 +0800751
752 if (!open_req->rq_replay)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800753 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800754
755 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
756 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
757 LASSERT(rec != NULL);
758 /* Incoming message in my byte order (it's been swabbed). */
759 /* Outgoing messages always in my byte order. */
760 LASSERT(body != NULL);
761
762 /* Only if the import is replayable, we set replay_open data */
763 if (och && imp->imp_replayable) {
764 mod = obd_mod_alloc();
765 if (mod == NULL) {
766 DEBUG_REQ(D_ERROR, open_req,
767 "Can't allocate md_open_data");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800768 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800769 }
770
771 /**
772 * Take a reference on \var mod, to be freed on mdc_close().
773 * It protects \var mod from being freed on eviction (commit
774 * callback is called despite rq_replay flag).
775 * Another reference for \var och.
776 */
777 obd_mod_get(mod);
778 obd_mod_get(mod);
779
780 spin_lock(&open_req->rq_lock);
781 och->och_mod = mod;
782 mod->mod_och = och;
Hongchao Zhang63d42572014-02-28 21:16:37 -0500783 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
784 it_disposition(it, DISP_OPEN_STRIPE);
Peng Taod7e09d02013-05-02 16:46:55 +0800785 mod->mod_open_req = open_req;
786 open_req->rq_cb_data = mod;
787 open_req->rq_commit_cb = mdc_commit_open;
788 spin_unlock(&open_req->rq_lock);
789 }
790
791 rec->cr_fid2 = body->fid1;
792 rec->cr_ioepoch = body->ioepoch;
793 rec->cr_old_handle.cookie = body->handle.cookie;
794 open_req->rq_replay_cb = mdc_replay_open;
795 if (!fid_is_sane(&body->fid1)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530796 DEBUG_REQ(D_ERROR, open_req,
797 "Saving replay request with insane fid");
Peng Taod7e09d02013-05-02 16:46:55 +0800798 LBUG();
799 }
800
801 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800802 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800803}
804
Hongchao Zhang63d42572014-02-28 21:16:37 -0500805static void mdc_free_open(struct md_open_data *mod)
806{
807 int committed = 0;
808
809 if (mod->mod_is_create == 0 &&
810 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
811 committed = 1;
812
813 LASSERT(mod->mod_open_req->rq_replay == 0);
814
815 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
816
817 ptlrpc_request_committed(mod->mod_open_req, committed);
818 if (mod->mod_close_req)
819 ptlrpc_request_committed(mod->mod_close_req, committed);
820}
821
Peng Taod7e09d02013-05-02 16:46:55 +0800822int mdc_clear_open_replay_data(struct obd_export *exp,
823 struct obd_client_handle *och)
824{
825 struct md_open_data *mod = och->och_mod;
Peng Taod7e09d02013-05-02 16:46:55 +0800826
827 /**
828 * It is possible to not have \var mod in a case of eviction between
829 * lookup and ll_file_open().
830 **/
831 if (mod == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800832 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800833
834 LASSERT(mod != LP_POISON);
Hongchao Zhang63d42572014-02-28 21:16:37 -0500835 LASSERT(mod->mod_open_req != NULL);
836 mdc_free_open(mod);
Peng Taod7e09d02013-05-02 16:46:55 +0800837
838 mod->mod_och = NULL;
839 och->och_mod = NULL;
840 obd_mod_put(mod);
841
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800842 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800843}
844
845/* Prepares the request for the replay by the given reply */
846static void mdc_close_handle_reply(struct ptlrpc_request *req,
847 struct md_op_data *op_data, int rc) {
848 struct mdt_body *repbody;
849 struct mdt_ioepoch *epoch;
850
851 if (req && rc == -EAGAIN) {
852 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
853 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
854
855 epoch->flags |= MF_SOM_AU;
856 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
857 op_data->op_flags |= MF_GETATTR_LOCK;
858 }
859}
860
Luca Ceresolif6219c12015-01-18 15:06:47 +0100861static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
862 struct md_open_data *mod, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800863{
864 struct obd_device *obd = class_exp2obd(exp);
865 struct ptlrpc_request *req;
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800866 struct req_format *req_fmt;
867 int rc;
868 int saved_rc = 0;
869
870
871 req_fmt = &RQF_MDS_CLOSE;
872 if (op_data->op_bias & MDS_HSM_RELEASE) {
873 req_fmt = &RQF_MDS_RELEASE_CLOSE;
874
875 /* allocate a FID for volatile file */
876 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
877 if (rc < 0) {
878 CERROR("%s: "DFID" failed to allocate FID: %d\n",
879 obd->obd_name, PFID(&op_data->op_fid1), rc);
880 /* save the errcode and proceed to close */
881 saved_rc = rc;
882 }
883 }
Peng Taod7e09d02013-05-02 16:46:55 +0800884
885 *request = NULL;
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800886 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
Peng Taod7e09d02013-05-02 16:46:55 +0800887 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800888 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800889
890 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
891
892 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
893 if (rc) {
894 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800895 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800896 }
897
898 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
899 * portal whose threads are not taking any DLM locks and are therefore
900 * always progressing */
901 req->rq_request_portal = MDS_READPAGE_PORTAL;
902 ptlrpc_at_set_req_timeout(req);
903
904 /* Ensure that this close's handle is fixed up during replay. */
905 if (likely(mod != NULL)) {
906 LASSERTF(mod->mod_open_req != NULL &&
907 mod->mod_open_req->rq_type != LI_POISON,
908 "POISONED open %p!\n", mod->mod_open_req);
909
910 mod->mod_close_req = req;
911
912 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
913 /* We no longer want to preserve this open for replay even
914 * though the open was committed. b=3632, b=3633 */
915 spin_lock(&mod->mod_open_req->rq_lock);
916 mod->mod_open_req->rq_replay = 0;
917 spin_unlock(&mod->mod_open_req->rq_lock);
918 } else {
Srikrishan Malike5e663a2014-08-11 23:57:30 +0530919 CDEBUG(D_HA,
920 "couldn't find open req; expecting close error\n");
Peng Taod7e09d02013-05-02 16:46:55 +0800921 }
922
923 mdc_close_pack(req, op_data);
924
925 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
Brian Behlendorf44779342014-04-27 13:06:47 -0400926 obd->u.cli.cl_default_mds_easize);
Peng Taod7e09d02013-05-02 16:46:55 +0800927 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
Brian Behlendorf44779342014-04-27 13:06:47 -0400928 obd->u.cli.cl_default_mds_cookiesize);
Peng Taod7e09d02013-05-02 16:46:55 +0800929
930 ptlrpc_request_set_replen(req);
931
932 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
933 rc = ptlrpc_queue_wait(req);
934 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
935
936 if (req->rq_repmsg == NULL) {
937 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
938 req->rq_status);
939 if (rc == 0)
940 rc = req->rq_status ?: -EIO;
941 } else if (rc == 0 || rc == -EAGAIN) {
942 struct mdt_body *body;
943
944 rc = lustre_msg_get_status(req->rq_repmsg);
945 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530946 DEBUG_REQ(D_ERROR, req,
947 "type == PTL_RPC_MSG_ERR, err = %d", rc);
Peng Taod7e09d02013-05-02 16:46:55 +0800948 if (rc > 0)
949 rc = -rc;
950 }
951 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
952 if (body == NULL)
953 rc = -EPROTO;
954 } else if (rc == -ESTALE) {
955 /**
956 * it can be allowed error after 3633 if open was committed and
957 * server failed before close was sent. Let's check if mod
958 * exists and return no error in that case
959 */
960 if (mod) {
961 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
962 LASSERT(mod->mod_open_req != NULL);
963 if (mod->mod_open_req->rq_committed)
964 rc = 0;
965 }
966 }
967
968 if (mod) {
969 if (rc != 0)
970 mod->mod_close_req = NULL;
971 /* Since now, mod is accessed through open_req only,
972 * thus close req does not keep a reference on mod anymore. */
973 obd_mod_put(mod);
974 }
975 *request = req;
976 mdc_close_handle_reply(req, op_data, rc);
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800977 return rc < 0 ? rc : saved_rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800978}
979
Luca Ceresolif6219c12015-01-18 15:06:47 +0100980static int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
981 struct md_open_data *mod)
Peng Taod7e09d02013-05-02 16:46:55 +0800982{
983 struct obd_device *obd = class_exp2obd(exp);
984 struct ptlrpc_request *req;
985 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800986
987 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
988 &RQF_MDS_DONE_WRITING);
989 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800990 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800991
992 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
993 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
994 if (rc) {
995 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800996 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800997 }
998
999 if (mod != NULL) {
1000 LASSERTF(mod->mod_open_req != NULL &&
1001 mod->mod_open_req->rq_type != LI_POISON,
1002 "POISONED setattr %p!\n", mod->mod_open_req);
1003
1004 mod->mod_close_req = req;
1005 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
1006 /* We no longer want to preserve this setattr for replay even
1007 * though the open was committed. b=3632, b=3633 */
1008 spin_lock(&mod->mod_open_req->rq_lock);
1009 mod->mod_open_req->rq_replay = 0;
1010 spin_unlock(&mod->mod_open_req->rq_lock);
1011 }
1012
1013 mdc_close_pack(req, op_data);
1014 ptlrpc_request_set_replen(req);
1015
1016 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1017 rc = ptlrpc_queue_wait(req);
1018 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1019
1020 if (rc == -ESTALE) {
1021 /**
1022 * it can be allowed error after 3633 if open or setattr were
1023 * committed and server failed before close was sent.
1024 * Let's check if mod exists and return no error in that case
1025 */
1026 if (mod) {
1027 LASSERT(mod->mod_open_req != NULL);
1028 if (mod->mod_open_req->rq_committed)
1029 rc = 0;
1030 }
1031 }
1032
1033 if (mod) {
1034 if (rc != 0)
1035 mod->mod_close_req = NULL;
Hongchao Zhang63d42572014-02-28 21:16:37 -05001036 LASSERT(mod->mod_open_req != NULL);
1037 mdc_free_open(mod);
1038
Peng Taod7e09d02013-05-02 16:46:55 +08001039 /* Since now, mod is accessed through setattr req only,
1040 * thus DW req does not keep a reference on mod anymore. */
1041 obd_mod_put(mod);
1042 }
1043
1044 mdc_close_handle_reply(req, op_data, rc);
1045 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001046 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001047}
1048
1049
Luca Ceresolif6219c12015-01-18 15:06:47 +01001050static int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1051 struct page **pages, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +08001052{
1053 struct ptlrpc_request *req;
1054 struct ptlrpc_bulk_desc *desc;
1055 int i;
1056 wait_queue_head_t waitq;
1057 int resends = 0;
1058 struct l_wait_info lwi;
1059 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001060
1061 *request = NULL;
1062 init_waitqueue_head(&waitq);
1063
1064restart_bulk:
1065 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1066 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001067 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001068
1069 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1070
1071 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1072 if (rc) {
1073 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001074 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001075 }
1076
1077 req->rq_request_portal = MDS_READPAGE_PORTAL;
1078 ptlrpc_at_set_req_timeout(req);
1079
1080 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1081 MDS_BULK_PORTAL);
1082 if (desc == NULL) {
1083 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001084 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001085 }
1086
1087 /* NB req now owns desc and will free it when it gets freed */
1088 for (i = 0; i < op_data->op_npages; i++)
1089 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1090
1091 mdc_readdir_pack(req, op_data->op_offset,
1092 PAGE_CACHE_SIZE * op_data->op_npages,
1093 &op_data->op_fid1, op_data->op_capa1);
1094
1095 ptlrpc_request_set_replen(req);
1096 rc = ptlrpc_queue_wait(req);
1097 if (rc) {
1098 ptlrpc_req_finished(req);
1099 if (rc != -ETIMEDOUT)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001100 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001101
1102 resends++;
1103 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1104 CERROR("too many resend retries, returning error\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001105 return -EIO;
Peng Taod7e09d02013-05-02 16:46:55 +08001106 }
Srikrishan Malike5e663a2014-08-11 23:57:30 +05301107 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends),
1108 NULL, NULL, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +08001109 l_wait_event(waitq, 0, &lwi);
1110
1111 goto restart_bulk;
1112 }
1113
1114 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1115 req->rq_bulk->bd_nob_transferred);
1116 if (rc < 0) {
1117 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001118 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001119 }
1120
1121 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1122 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1123 req->rq_bulk->bd_nob_transferred,
1124 PAGE_CACHE_SIZE * op_data->op_npages);
1125 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001126 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08001127 }
1128
1129 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001130 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001131}
1132
1133static int mdc_statfs(const struct lu_env *env,
1134 struct obd_export *exp, struct obd_statfs *osfs,
1135 __u64 max_age, __u32 flags)
1136{
1137 struct obd_device *obd = class_exp2obd(exp);
1138 struct ptlrpc_request *req;
1139 struct obd_statfs *msfs;
1140 struct obd_import *imp = NULL;
1141 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001142
1143 /*
1144 * Since the request might also come from lprocfs, so we need
1145 * sync this with client_disconnect_export Bug15684
1146 */
1147 down_read(&obd->u.cli.cl_sem);
1148 if (obd->u.cli.cl_import)
1149 imp = class_import_get(obd->u.cli.cl_import);
1150 up_read(&obd->u.cli.cl_sem);
1151 if (!imp)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001152 return -ENODEV;
Peng Taod7e09d02013-05-02 16:46:55 +08001153
1154 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1155 LUSTRE_MDS_VERSION, MDS_STATFS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001156 if (req == NULL) {
1157 rc = -ENOMEM;
1158 goto output;
1159 }
Peng Taod7e09d02013-05-02 16:46:55 +08001160
1161 ptlrpc_request_set_replen(req);
1162
1163 if (flags & OBD_STATFS_NODELAY) {
1164 /* procfs requests not want stay in wait for avoid deadlock */
1165 req->rq_no_resend = 1;
1166 req->rq_no_delay = 1;
1167 }
1168
1169 rc = ptlrpc_queue_wait(req);
1170 if (rc) {
1171 /* check connection error first */
1172 if (imp->imp_connect_error)
1173 rc = imp->imp_connect_error;
Julia Lawalld5fdc202014-08-28 12:10:35 +02001174 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001175 }
1176
1177 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001178 if (msfs == NULL) {
1179 rc = -EPROTO;
1180 goto out;
1181 }
Peng Taod7e09d02013-05-02 16:46:55 +08001182
1183 *osfs = *msfs;
Peng Taod7e09d02013-05-02 16:46:55 +08001184out:
1185 ptlrpc_req_finished(req);
1186output:
1187 class_import_put(imp);
1188 return rc;
1189}
1190
1191static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1192{
1193 __u32 keylen, vallen;
1194 void *key;
1195 int rc;
1196
1197 if (gf->gf_pathlen > PATH_MAX)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001198 return -ENAMETOOLONG;
Peng Taod7e09d02013-05-02 16:46:55 +08001199 if (gf->gf_pathlen < 2)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001200 return -EOVERFLOW;
Peng Taod7e09d02013-05-02 16:46:55 +08001201
1202 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1203 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
Julia Lawall7b817792015-05-01 17:51:17 +02001204 key = kzalloc(keylen, GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02001205 if (!key)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001206 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001207 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1208 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1209
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001210 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001211 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1212
Julia Lawalld5fdc202014-08-28 12:10:35 +02001213 if (!fid_is_sane(&gf->gf_fid)) {
1214 rc = -EINVAL;
1215 goto out;
1216 }
Peng Taod7e09d02013-05-02 16:46:55 +08001217
1218 /* Val is struct getinfo_fid2path result plus path */
1219 vallen = sizeof(*gf) + gf->gf_pathlen;
1220
1221 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1222 if (rc != 0 && rc != -EREMOTE)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001223 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001224
Julia Lawalld5fdc202014-08-28 12:10:35 +02001225 if (vallen <= sizeof(*gf)) {
1226 rc = -EPROTO;
1227 goto out;
1228 } else if (vallen > sizeof(*gf) + gf->gf_pathlen) {
1229 rc = -EOVERFLOW;
1230 goto out;
1231 }
Peng Taod7e09d02013-05-02 16:46:55 +08001232
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001233 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n%s\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001234 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1235
1236out:
Julia Lawall7b817792015-05-01 17:51:17 +02001237 kfree(key);
Peng Taod7e09d02013-05-02 16:46:55 +08001238 return rc;
1239}
1240
1241static int mdc_ioc_hsm_progress(struct obd_export *exp,
1242 struct hsm_progress_kernel *hpk)
1243{
1244 struct obd_import *imp = class_exp2cliimp(exp);
1245 struct hsm_progress_kernel *req_hpk;
1246 struct ptlrpc_request *req;
1247 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001248
1249 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1250 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001251 if (req == NULL) {
1252 rc = -ENOMEM;
1253 goto out;
1254 }
Peng Taod7e09d02013-05-02 16:46:55 +08001255
1256 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1257
1258 /* Copy hsm_progress struct */
1259 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001260 if (req_hpk == NULL) {
1261 rc = -EPROTO;
1262 goto out;
1263 }
Peng Taod7e09d02013-05-02 16:46:55 +08001264
1265 *req_hpk = *hpk;
Li Wei2d58de72013-07-23 00:06:32 +08001266 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
Peng Taod7e09d02013-05-02 16:46:55 +08001267
1268 ptlrpc_request_set_replen(req);
1269
1270 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001271 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001272out:
1273 ptlrpc_req_finished(req);
1274 return rc;
1275}
1276
1277static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1278{
1279 __u32 *archive_mask;
1280 struct ptlrpc_request *req;
1281 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001282
1283 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1284 LUSTRE_MDS_VERSION,
1285 MDS_HSM_CT_REGISTER);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001286 if (req == NULL) {
1287 rc = -ENOMEM;
1288 goto out;
1289 }
Peng Taod7e09d02013-05-02 16:46:55 +08001290
1291 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1292
1293 /* Copy hsm_progress struct */
1294 archive_mask = req_capsule_client_get(&req->rq_pill,
1295 &RMF_MDS_HSM_ARCHIVE);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001296 if (archive_mask == NULL) {
1297 rc = -EPROTO;
1298 goto out;
1299 }
Peng Taod7e09d02013-05-02 16:46:55 +08001300
1301 *archive_mask = archives;
1302
1303 ptlrpc_request_set_replen(req);
1304
1305 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001306 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001307out:
1308 ptlrpc_req_finished(req);
1309 return rc;
1310}
1311
1312static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1313 struct md_op_data *op_data)
1314{
1315 struct hsm_current_action *hca = op_data->op_data;
1316 struct hsm_current_action *req_hca;
1317 struct ptlrpc_request *req;
1318 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001319
1320 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1321 &RQF_MDS_HSM_ACTION);
1322 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001323 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001324
1325 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1326
1327 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1328 if (rc) {
1329 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001330 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001331 }
1332
1333 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1334 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1335
1336 ptlrpc_request_set_replen(req);
1337
1338 rc = mdc_queue_wait(req);
1339 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001340 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001341
1342 req_hca = req_capsule_server_get(&req->rq_pill,
1343 &RMF_MDS_HSM_CURRENT_ACTION);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001344 if (req_hca == NULL) {
1345 rc = -EPROTO;
1346 goto out;
1347 }
Peng Taod7e09d02013-05-02 16:46:55 +08001348
1349 *hca = *req_hca;
1350
Peng Taod7e09d02013-05-02 16:46:55 +08001351out:
1352 ptlrpc_req_finished(req);
1353 return rc;
1354}
1355
1356static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1357{
1358 struct ptlrpc_request *req;
1359 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001360
1361 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1362 LUSTRE_MDS_VERSION,
1363 MDS_HSM_CT_UNREGISTER);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001364 if (req == NULL) {
1365 rc = -ENOMEM;
1366 goto out;
1367 }
Peng Taod7e09d02013-05-02 16:46:55 +08001368
1369 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1370
1371 ptlrpc_request_set_replen(req);
1372
1373 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001374 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001375out:
1376 ptlrpc_req_finished(req);
1377 return rc;
1378}
1379
1380static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1381 struct md_op_data *op_data)
1382{
1383 struct hsm_user_state *hus = op_data->op_data;
1384 struct hsm_user_state *req_hus;
1385 struct ptlrpc_request *req;
1386 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001387
1388 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1389 &RQF_MDS_HSM_STATE_GET);
1390 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001391 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001392
1393 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1394
1395 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1396 if (rc != 0) {
1397 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001398 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001399 }
1400
1401 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1402 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1403
1404 ptlrpc_request_set_replen(req);
1405
1406 rc = mdc_queue_wait(req);
1407 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001408 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001409
1410 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001411 if (req_hus == NULL) {
1412 rc = -EPROTO;
1413 goto out;
1414 }
Peng Taod7e09d02013-05-02 16:46:55 +08001415
1416 *hus = *req_hus;
1417
Peng Taod7e09d02013-05-02 16:46:55 +08001418out:
1419 ptlrpc_req_finished(req);
1420 return rc;
1421}
1422
1423static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1424 struct md_op_data *op_data)
1425{
1426 struct hsm_state_set *hss = op_data->op_data;
1427 struct hsm_state_set *req_hss;
1428 struct ptlrpc_request *req;
1429 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001430
1431 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1432 &RQF_MDS_HSM_STATE_SET);
1433 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001434 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001435
1436 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1437
1438 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1439 if (rc) {
1440 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001441 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001442 }
1443
1444 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1445 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1446
1447 /* Copy states */
1448 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001449 if (req_hss == NULL) {
1450 rc = -EPROTO;
1451 goto out;
1452 }
Peng Taod7e09d02013-05-02 16:46:55 +08001453 *req_hss = *hss;
1454
1455 ptlrpc_request_set_replen(req);
1456
1457 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001458 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001459
Peng Taod7e09d02013-05-02 16:46:55 +08001460out:
1461 ptlrpc_req_finished(req);
1462 return rc;
1463}
1464
1465static int mdc_ioc_hsm_request(struct obd_export *exp,
1466 struct hsm_user_request *hur)
1467{
1468 struct obd_import *imp = class_exp2cliimp(exp);
1469 struct ptlrpc_request *req;
1470 struct hsm_request *req_hr;
1471 struct hsm_user_item *req_hui;
1472 char *req_opaque;
1473 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001474
1475 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001476 if (req == NULL) {
1477 rc = -ENOMEM;
1478 goto out;
1479 }
Peng Taod7e09d02013-05-02 16:46:55 +08001480
1481 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1482 hur->hur_request.hr_itemcount
1483 * sizeof(struct hsm_user_item));
1484 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1485 hur->hur_request.hr_data_len);
1486
1487 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1488 if (rc) {
1489 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001490 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001491 }
1492
1493 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1494
1495 /* Copy hsm_request struct */
1496 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001497 if (req_hr == NULL) {
1498 rc = -EPROTO;
1499 goto out;
1500 }
Peng Taod7e09d02013-05-02 16:46:55 +08001501 *req_hr = hur->hur_request;
1502
1503 /* Copy hsm_user_item structs */
1504 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001505 if (req_hui == NULL) {
1506 rc = -EPROTO;
1507 goto out;
1508 }
Peng Taod7e09d02013-05-02 16:46:55 +08001509 memcpy(req_hui, hur->hur_user_item,
1510 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1511
1512 /* Copy opaque field */
1513 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001514 if (req_opaque == NULL) {
1515 rc = -EPROTO;
1516 goto out;
1517 }
Peng Taod7e09d02013-05-02 16:46:55 +08001518 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1519
1520 ptlrpc_request_set_replen(req);
1521
1522 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001523 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001524
1525out:
1526 ptlrpc_req_finished(req);
1527 return rc;
1528}
1529
1530static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1531{
1532 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1533
Oleg Drokin18e042f2014-01-23 23:45:07 -05001534 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
Peng Taod7e09d02013-05-02 16:46:55 +08001535
1536 lh->kuc_magic = KUC_MAGIC;
1537 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1538 lh->kuc_flags = flags;
1539 lh->kuc_msgtype = CL_RECORD;
1540 lh->kuc_msglen = len;
1541 return lh;
1542}
1543
1544#define D_CHANGELOG 0
1545
1546struct changelog_show {
1547 __u64 cs_startrec;
1548 __u32 cs_flags;
1549 struct file *cs_fp;
1550 char *cs_buf;
1551 struct obd_device *cs_obd;
1552};
1553
Andreas Dilgere3779882013-06-03 21:40:52 +08001554static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
Peng Taod7e09d02013-05-02 16:46:55 +08001555 struct llog_rec_hdr *hdr, void *data)
1556{
1557 struct changelog_show *cs = data;
1558 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1559 struct kuc_hdr *lh;
1560 int len, rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001561
Andreas Dilgere3779882013-06-03 21:40:52 +08001562 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1563 rc = -EINVAL;
1564 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1565 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1566 rec->cr.cr_type, rc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001567 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001568 }
1569
1570 if (rec->cr.cr_index < cs->cs_startrec) {
1571 /* Skip entries earlier than what we are interested in */
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001572 CDEBUG(D_CHANGELOG, "rec=%llu start=%llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001573 rec->cr.cr_index, cs->cs_startrec);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001574 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001575 }
1576
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001577 CDEBUG(D_CHANGELOG, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID
Peng Taod7e09d02013-05-02 16:46:55 +08001578 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1579 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1580 rec->cr.cr_flags & CLF_FLAGMASK,
1581 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1582 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1583
1584 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1585
1586 /* Set up the message */
1587 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1588 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1589
1590 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
Srikrishan Malik301af902014-08-11 23:57:31 +05301591 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len, rc);
Peng Taod7e09d02013-05-02 16:46:55 +08001592
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001593 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001594}
1595
1596static int mdc_changelog_send_thread(void *csdata)
1597{
1598 struct changelog_show *cs = csdata;
1599 struct llog_ctxt *ctxt = NULL;
1600 struct llog_handle *llh = NULL;
1601 struct kuc_hdr *kuch;
1602 int rc;
1603
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001604 CDEBUG(D_CHANGELOG, "changelog to fp=%p start %llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001605 cs->cs_fp, cs->cs_startrec);
1606
Julia Lawall7b817792015-05-01 17:51:17 +02001607 cs->cs_buf = kzalloc(KUC_CHANGELOG_MSG_MAXSIZE, GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02001608 if (!cs->cs_buf) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001609 rc = -ENOMEM;
1610 goto out;
1611 }
Peng Taod7e09d02013-05-02 16:46:55 +08001612
1613 /* Set up the remote catalog handle */
1614 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001615 if (ctxt == NULL) {
1616 rc = -ENOENT;
1617 goto out;
1618 }
Peng Taod7e09d02013-05-02 16:46:55 +08001619 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1620 LLOG_OPEN_EXISTS);
1621 if (rc) {
1622 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1623 cs->cs_obd->obd_name, rc);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001624 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001625 }
1626 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1627 if (rc) {
1628 CERROR("llog_init_handle failed %d\n", rc);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001629 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001630 }
1631
Andreas Dilgere3779882013-06-03 21:40:52 +08001632 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001633
1634 /* Send EOF no matter what our result */
Chi Pham4a87df32014-03-09 12:51:19 +01001635 kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
1636 if (kuch) {
Peng Taod7e09d02013-05-02 16:46:55 +08001637 kuch->kuc_msgtype = CL_EOF;
1638 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1639 }
1640
1641out:
1642 fput(cs->cs_fp);
1643 if (llh)
1644 llog_cat_close(NULL, llh);
1645 if (ctxt)
1646 llog_ctxt_put(ctxt);
Julia Lawall6fa7cbab32015-05-01 21:37:56 +02001647 kfree(cs->cs_buf);
Julia Lawall7b817792015-05-01 17:51:17 +02001648 kfree(cs);
Peng Taod7e09d02013-05-02 16:46:55 +08001649 return rc;
1650}
1651
1652static int mdc_ioc_changelog_send(struct obd_device *obd,
1653 struct ioc_changelog *icc)
1654{
1655 struct changelog_show *cs;
1656 int rc;
1657
1658 /* Freed in mdc_changelog_send_thread */
Julia Lawall7b817792015-05-01 17:51:17 +02001659 cs = kzalloc(sizeof(*cs), GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +08001660 if (!cs)
1661 return -ENOMEM;
1662
1663 cs->cs_obd = obd;
1664 cs->cs_startrec = icc->icc_recno;
1665 /* matching fput in mdc_changelog_send_thread */
1666 cs->cs_fp = fget(icc->icc_id);
1667 cs->cs_flags = icc->icc_flags;
1668
1669 /*
1670 * New thread because we should return to user app before
1671 * writing into our pipe
1672 */
1673 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1674 "mdc_clg_send_thread"));
1675 if (!IS_ERR_VALUE(rc)) {
1676 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1677 return 0;
1678 }
1679
1680 CERROR("Failed to start changelog thread: %d\n", rc);
Julia Lawall7b817792015-05-01 17:51:17 +02001681 kfree(cs);
Peng Taod7e09d02013-05-02 16:46:55 +08001682 return rc;
1683}
1684
1685static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1686 struct lustre_kernelcomm *lk);
1687
1688static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1689 struct obd_quotactl *oqctl)
1690{
1691 struct client_obd *cli = &exp->exp_obd->u.cli;
1692 struct ptlrpc_request *req;
1693 struct obd_quotactl *body;
1694 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001695
1696 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1697 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1698 MDS_QUOTACHECK);
1699 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001700 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001701
1702 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1703 *body = *oqctl;
1704
1705 ptlrpc_request_set_replen(req);
1706
1707 /* the next poll will find -ENODATA, that means quotacheck is
1708 * going on */
1709 cli->cl_qchk_stat = -ENODATA;
1710 rc = ptlrpc_queue_wait(req);
1711 if (rc)
1712 cli->cl_qchk_stat = rc;
1713 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001714 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001715}
1716
1717static int mdc_quota_poll_check(struct obd_export *exp,
1718 struct if_quotacheck *qchk)
1719{
1720 struct client_obd *cli = &exp->exp_obd->u.cli;
1721 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001722
1723 qchk->obd_uuid = cli->cl_target_uuid;
1724 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1725
1726 rc = cli->cl_qchk_stat;
1727 /* the client is not the previous one */
1728 if (rc == CL_NOT_QUOTACHECKED)
1729 rc = -EINTR;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001730 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001731}
1732
1733static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1734 struct obd_quotactl *oqctl)
1735{
1736 struct ptlrpc_request *req;
1737 struct obd_quotactl *oqc;
1738 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001739
1740 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1741 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1742 MDS_QUOTACTL);
1743 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001744 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001745
1746 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1747 *oqc = *oqctl;
1748
1749 ptlrpc_request_set_replen(req);
1750 ptlrpc_at_set_req_timeout(req);
1751 req->rq_no_resend = 1;
1752
1753 rc = ptlrpc_queue_wait(req);
1754 if (rc)
1755 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1756
Chi Pham4a87df32014-03-09 12:51:19 +01001757 if (req->rq_repmsg) {
1758 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1759 if (oqc) {
1760 *oqctl = *oqc;
1761 } else if (!rc) {
Srikrishan Malikffdac6ce2014-08-11 23:57:27 +05301762 CERROR("Can't unpack obd_quotactl\n");
Chi Pham4a87df32014-03-09 12:51:19 +01001763 rc = -EPROTO;
1764 }
Peng Taod7e09d02013-05-02 16:46:55 +08001765 } else if (!rc) {
Chi Pham4a87df32014-03-09 12:51:19 +01001766 CERROR("Can't unpack obd_quotactl\n");
Peng Taod7e09d02013-05-02 16:46:55 +08001767 rc = -EPROTO;
1768 }
1769 ptlrpc_req_finished(req);
1770
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001771 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001772}
1773
1774static int mdc_ioc_swap_layouts(struct obd_export *exp,
1775 struct md_op_data *op_data)
1776{
1777 LIST_HEAD(cancels);
1778 struct ptlrpc_request *req;
1779 int rc, count;
1780 struct mdc_swap_layouts *msl, *payload;
Peng Taod7e09d02013-05-02 16:46:55 +08001781
1782 msl = op_data->op_data;
1783
1784 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1785 * first thing it will do is to cancel the 2 layout
1786 * locks hold by this client.
1787 * So the client must cancel its layout locks on the 2 fids
1788 * with the request RPC to avoid extra RPC round trips
1789 */
1790 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1791 LCK_CR, MDS_INODELOCK_LAYOUT);
1792 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1793 LCK_CR, MDS_INODELOCK_LAYOUT);
1794
1795 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1796 &RQF_MDS_SWAP_LAYOUTS);
1797 if (req == NULL) {
1798 ldlm_lock_list_put(&cancels, l_bl_ast, count);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001799 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001800 }
1801
1802 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1803 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1804
1805 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1806 if (rc) {
1807 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001808 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001809 }
1810
1811 mdc_swap_layouts_pack(req, op_data);
1812
1813 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1814 LASSERT(payload);
1815
1816 *payload = *msl;
1817
1818 ptlrpc_request_set_replen(req);
1819
1820 rc = ptlrpc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08001821
Peng Taod7e09d02013-05-02 16:46:55 +08001822 ptlrpc_req_finished(req);
1823 return rc;
1824}
1825
1826static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1827 void *karg, void *uarg)
1828{
1829 struct obd_device *obd = exp->exp_obd;
1830 struct obd_ioctl_data *data = karg;
1831 struct obd_import *imp = obd->u.cli.cl_import;
Peng Taod7e09d02013-05-02 16:46:55 +08001832 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001833
1834 if (!try_module_get(THIS_MODULE)) {
1835 CERROR("Can't get module. Is it alive?");
1836 return -EINVAL;
1837 }
1838 switch (cmd) {
1839 case OBD_IOC_CHANGELOG_SEND:
1840 rc = mdc_ioc_changelog_send(obd, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001841 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001842 case OBD_IOC_CHANGELOG_CLEAR: {
1843 struct ioc_changelog *icc = karg;
Srikrishan Malik1a4cd3e2014-08-11 23:57:36 +05301844 struct changelog_setinfo cs = {
1845 .cs_recno = icc->icc_recno,
1846 .cs_id = icc->icc_id
1847 };
1848
Peng Taod7e09d02013-05-02 16:46:55 +08001849 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1850 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1851 NULL);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001852 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001853 }
1854 case OBD_IOC_FID2PATH:
1855 rc = mdc_ioc_fid2path(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001856 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001857 case LL_IOC_HSM_CT_START:
1858 rc = mdc_ioc_hsm_ct_start(exp, karg);
Thomas Leibovici78eb90922013-07-25 01:17:24 +08001859 /* ignore if it was already registered on this MDS. */
1860 if (rc == -EEXIST)
1861 rc = 0;
Julia Lawalld5fdc202014-08-28 12:10:35 +02001862 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001863 case LL_IOC_HSM_PROGRESS:
1864 rc = mdc_ioc_hsm_progress(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001865 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001866 case LL_IOC_HSM_STATE_GET:
1867 rc = mdc_ioc_hsm_state_get(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001868 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001869 case LL_IOC_HSM_STATE_SET:
1870 rc = mdc_ioc_hsm_state_set(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001871 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001872 case LL_IOC_HSM_ACTION:
1873 rc = mdc_ioc_hsm_current_action(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001874 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001875 case LL_IOC_HSM_REQUEST:
1876 rc = mdc_ioc_hsm_request(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001877 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001878 case OBD_IOC_CLIENT_RECOVER:
1879 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1880 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001881 goto out;
1882 rc = 0;
1883 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001884 case IOC_OSC_SET_ACTIVE:
1885 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001886 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001887 case OBD_IOC_POLL_QUOTACHECK:
1888 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001889 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001890 case OBD_IOC_PING_TARGET:
1891 rc = ptlrpc_obd_ping(obd);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001892 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001893 /*
1894 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1895 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1896 * there'd be no LMV layer thus we might be called here. Eventually
1897 * this code should be removed.
1898 * bz20731, LU-592.
1899 */
1900 case IOC_OBD_STATFS: {
1901 struct obd_statfs stat_buf = {0};
1902
Julia Lawalld5fdc202014-08-28 12:10:35 +02001903 if (*((__u32 *) data->ioc_inlbuf2) != 0) {
1904 rc = -ENODEV;
1905 goto out;
1906 }
Peng Taod7e09d02013-05-02 16:46:55 +08001907
1908 /* copy UUID */
1909 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
Dan Carpenteref4356bf2015-01-06 12:57:08 +03001910 min_t(size_t, data->ioc_plen2,
1911 sizeof(struct obd_uuid)))) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001912 rc = -EFAULT;
1913 goto out;
1914 }
Peng Taod7e09d02013-05-02 16:46:55 +08001915
1916 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1917 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1918 0);
1919 if (rc != 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001920 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001921
1922 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
Dan Carpenteref4356bf2015-01-06 12:57:08 +03001923 min_t(size_t, data->ioc_plen1,
1924 sizeof(stat_buf)))) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001925 rc = -EFAULT;
1926 goto out;
1927 }
Peng Taod7e09d02013-05-02 16:46:55 +08001928
Julia Lawalld5fdc202014-08-28 12:10:35 +02001929 rc = 0;
1930 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001931 }
1932 case OBD_IOC_QUOTACTL: {
1933 struct if_quotactl *qctl = karg;
1934 struct obd_quotactl *oqctl;
1935
Julia Lawall7b817792015-05-01 17:51:17 +02001936 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02001937 if (!oqctl) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001938 rc = -ENOMEM;
1939 goto out;
1940 }
Peng Taod7e09d02013-05-02 16:46:55 +08001941
1942 QCTL_COPY(oqctl, qctl);
1943 rc = obd_quotactl(exp, oqctl);
1944 if (rc == 0) {
1945 QCTL_COPY(qctl, oqctl);
1946 qctl->qc_valid = QC_MDTIDX;
1947 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1948 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08001949
Julia Lawall7b817792015-05-01 17:51:17 +02001950 kfree(oqctl);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001951 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001952 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08001953 case LL_IOC_GET_CONNECT_FLAGS:
1954 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
Julia Lawalld5fdc202014-08-28 12:10:35 +02001955 sizeof(*exp_connect_flags_ptr(exp)))) {
1956 rc = -EFAULT;
1957 goto out;
1958 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08001959
Julia Lawalld5fdc202014-08-28 12:10:35 +02001960 rc = 0;
1961 goto out;
John L. Hammondc1f3d682013-11-26 10:04:59 +08001962 case LL_IOC_LOV_SWAP_LAYOUTS:
Peng Taod7e09d02013-05-02 16:46:55 +08001963 rc = mdc_ioc_swap_layouts(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001964 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001965 default:
John L. Hammondc1f3d682013-11-26 10:04:59 +08001966 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001967 rc = -ENOTTY;
1968 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001969 }
1970out:
1971 module_put(THIS_MODULE);
1972
1973 return rc;
1974}
1975
Luca Ceresolif6219c12015-01-18 15:06:47 +01001976static int mdc_get_info_rpc(struct obd_export *exp,
1977 u32 keylen, void *key,
1978 int vallen, void *val)
Peng Taod7e09d02013-05-02 16:46:55 +08001979{
1980 struct obd_import *imp = class_exp2cliimp(exp);
1981 struct ptlrpc_request *req;
1982 char *tmp;
1983 int rc = -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08001984
1985 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1986 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001987 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001988
1989 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1990 RCL_CLIENT, keylen);
1991 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1992 RCL_CLIENT, sizeof(__u32));
1993
1994 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1995 if (rc) {
1996 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001997 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001998 }
1999
2000 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2001 memcpy(tmp, key, keylen);
2002 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2003 memcpy(tmp, &vallen, sizeof(__u32));
2004
2005 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2006 RCL_SERVER, vallen);
2007 ptlrpc_request_set_replen(req);
2008
2009 rc = ptlrpc_queue_wait(req);
2010 /* -EREMOTE means the get_info result is partial, and it needs to
2011 * continue on another MDT, see fid2path part in lmv_iocontrol */
2012 if (rc == 0 || rc == -EREMOTE) {
2013 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2014 memcpy(val, tmp, vallen);
2015 if (ptlrpc_rep_need_swab(req)) {
2016 if (KEY_IS(KEY_FID2PATH))
2017 lustre_swab_fid2path(val);
2018 }
2019 }
2020 ptlrpc_req_finished(req);
2021
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002022 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002023}
2024
2025static void lustre_swab_hai(struct hsm_action_item *h)
2026{
2027 __swab32s(&h->hai_len);
2028 __swab32s(&h->hai_action);
2029 lustre_swab_lu_fid(&h->hai_fid);
2030 lustre_swab_lu_fid(&h->hai_dfid);
2031 __swab64s(&h->hai_cookie);
2032 __swab64s(&h->hai_extent.offset);
2033 __swab64s(&h->hai_extent.length);
2034 __swab64s(&h->hai_gid);
2035}
2036
2037static void lustre_swab_hal(struct hsm_action_list *h)
2038{
2039 struct hsm_action_item *hai;
2040 int i;
2041
2042 __swab32s(&h->hal_version);
2043 __swab32s(&h->hal_count);
2044 __swab32s(&h->hal_archive_id);
2045 __swab64s(&h->hal_flags);
2046 hai = hai_zero(h);
JC Lafoucriere18dfaeb2013-11-26 10:05:01 +08002047 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
Peng Taod7e09d02013-05-02 16:46:55 +08002048 lustre_swab_hai(hai);
Peng Taod7e09d02013-05-02 16:46:55 +08002049}
2050
2051static void lustre_swab_kuch(struct kuc_hdr *l)
2052{
2053 __swab16s(&l->kuc_magic);
2054 /* __u8 l->kuc_transport */
2055 __swab16s(&l->kuc_msgtype);
2056 __swab16s(&l->kuc_msglen);
2057}
2058
2059static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2060 struct lustre_kernelcomm *lk)
2061{
2062 struct obd_import *imp = class_exp2cliimp(exp);
2063 __u32 archive = lk->lk_data;
2064 int rc = 0;
2065
2066 if (lk->lk_group != KUC_GRP_HSM) {
2067 CERROR("Bad copytool group %d\n", lk->lk_group);
2068 return -EINVAL;
2069 }
2070
2071 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2072 lk->lk_uid, lk->lk_group, lk->lk_flags);
2073
2074 if (lk->lk_flags & LK_FLG_STOP) {
Peng Taod7e09d02013-05-02 16:46:55 +08002075 /* Unregister with the coordinator */
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002076 rc = mdc_ioc_hsm_ct_unregister(imp);
Peng Taod7e09d02013-05-02 16:46:55 +08002077 } else {
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002078 rc = mdc_ioc_hsm_ct_register(imp, archive);
Peng Taod7e09d02013-05-02 16:46:55 +08002079 }
2080
2081 return rc;
2082}
2083
2084/**
2085 * Send a message to any listening copytools
2086 * @param val KUC message (kuc_hdr + hsm_action_list)
2087 * @param len total length of message
2088 */
2089static int mdc_hsm_copytool_send(int len, void *val)
2090{
2091 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2092 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
Peng Taod7e09d02013-05-02 16:46:55 +08002093
2094 if (len < sizeof(*lh) + sizeof(*hal)) {
2095 CERROR("Short HSM message %d < %d\n", len,
2096 (int) (sizeof(*lh) + sizeof(*hal)));
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002097 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08002098 }
2099 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2100 lustre_swab_kuch(lh);
2101 lustre_swab_hal(hal);
2102 } else if (lh->kuc_magic != KUC_MAGIC) {
2103 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002104 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08002105 }
2106
Srikrishan Malikee990b32014-08-13 19:31:16 +05302107 CDEBUG(D_HSM,
2108 "Received message mg=%x t=%d m=%d l=%d actions=%d on %s\n",
Peng Taod7e09d02013-05-02 16:46:55 +08002109 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2110 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2111
2112 /* Broadcast to HSM listeners */
Prasanna Karthikda2a7272015-06-16 13:42:02 +00002113 return libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
Peng Taod7e09d02013-05-02 16:46:55 +08002114}
2115
2116/**
2117 * callback function passed to kuc for re-registering each HSM copytool
2118 * running on MDC, after MDT shutdown/recovery.
2119 * @param data archive id served by the copytool
2120 * @param cb_arg callback argument (obd_import)
2121 */
2122static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2123{
2124 struct obd_import *imp = (struct obd_import *)cb_arg;
2125 __u32 archive = data;
2126 int rc;
2127
2128 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2129 archive);
2130 rc = mdc_ioc_hsm_ct_register(imp, archive);
2131
2132 /* ignore error if the copytool is already registered */
2133 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2134}
2135
2136/**
2137 * Re-establish all kuc contexts with MDT
2138 * after MDT shutdown/recovery.
2139 */
2140static int mdc_kuc_reregister(struct obd_import *imp)
2141{
2142 /* re-register HSM agents */
2143 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2144 (void *)imp);
2145}
2146
Luca Ceresolif6219c12015-01-18 15:06:47 +01002147static int mdc_set_info_async(const struct lu_env *env,
2148 struct obd_export *exp,
2149 u32 keylen, void *key,
2150 u32 vallen, void *val,
2151 struct ptlrpc_request_set *set)
Peng Taod7e09d02013-05-02 16:46:55 +08002152{
2153 struct obd_import *imp = class_exp2cliimp(exp);
2154 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002155
2156 if (KEY_IS(KEY_READ_ONLY)) {
2157 if (vallen != sizeof(int))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002158 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002159
2160 spin_lock(&imp->imp_lock);
2161 if (*((int *)val)) {
2162 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2163 imp->imp_connect_data.ocd_connect_flags |=
2164 OBD_CONNECT_RDONLY;
2165 } else {
2166 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2167 imp->imp_connect_data.ocd_connect_flags &=
2168 ~OBD_CONNECT_RDONLY;
2169 }
2170 spin_unlock(&imp->imp_lock);
2171
2172 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2173 keylen, key, vallen, val, set);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002174 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002175 }
2176 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2177 sptlrpc_conf_client_adapt(exp->exp_obd);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002178 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002179 }
2180 if (KEY_IS(KEY_FLUSH_CTX)) {
2181 sptlrpc_import_flush_my_ctx(imp);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002182 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002183 }
Peng Taod7e09d02013-05-02 16:46:55 +08002184 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2185 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2186 keylen, key, vallen, val, set);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002187 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002188 }
2189 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2190 rc = mdc_hsm_copytool_send(vallen, val);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002191 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002192 }
2193
2194 CERROR("Unknown key %s\n", (char *)key);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002195 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002196}
2197
Luca Ceresolif6219c12015-01-18 15:06:47 +01002198static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2199 __u32 keylen, void *key, __u32 *vallen, void *val,
2200 struct lov_stripe_md *lsm)
Peng Taod7e09d02013-05-02 16:46:55 +08002201{
2202 int rc = -EINVAL;
2203
2204 if (KEY_IS(KEY_MAX_EASIZE)) {
2205 int mdsize, *max_easize;
2206
2207 if (*vallen != sizeof(int))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002208 return -EINVAL;
Brian Behlendorf44779342014-04-27 13:06:47 -04002209 mdsize = *(int *)val;
Peng Taod7e09d02013-05-02 16:46:55 +08002210 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2211 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2212 max_easize = val;
2213 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002214 return 0;
Brian Behlendorf44779342014-04-27 13:06:47 -04002215 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2216 int *default_easize;
2217
2218 if (*vallen != sizeof(int))
2219 return -EINVAL;
2220 default_easize = val;
2221 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2222 return 0;
2223 } else if (KEY_IS(KEY_MAX_COOKIESIZE)) {
2224 int mdsize, *max_cookiesize;
2225
2226 if (*vallen != sizeof(int))
2227 return -EINVAL;
2228 mdsize = *(int *)val;
2229 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_cookiesize)
2230 exp->exp_obd->u.cli.cl_max_mds_cookiesize = mdsize;
2231 max_cookiesize = val;
2232 *max_cookiesize = exp->exp_obd->u.cli.cl_max_mds_cookiesize;
2233 return 0;
2234 } else if (KEY_IS(KEY_DEFAULT_COOKIESIZE)) {
2235 int *default_cookiesize;
2236
2237 if (*vallen != sizeof(int))
2238 return -EINVAL;
2239 default_cookiesize = val;
2240 *default_cookiesize =
2241 exp->exp_obd->u.cli.cl_default_mds_cookiesize;
2242 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002243 } else if (KEY_IS(KEY_CONN_DATA)) {
2244 struct obd_import *imp = class_exp2cliimp(exp);
2245 struct obd_connect_data *data = val;
2246
2247 if (*vallen != sizeof(*data))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002248 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002249
2250 *data = imp->imp_connect_data;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002251 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002252 } else if (KEY_IS(KEY_TGT_COUNT)) {
2253 *((int *)val) = 1;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002254 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002255 }
2256
2257 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2258
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002259 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002260}
2261
Luca Ceresolif6219c12015-01-18 15:06:47 +01002262static int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2263 struct obd_capa *oc, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +08002264{
2265 struct ptlrpc_request *req;
2266 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002267
2268 *request = NULL;
2269 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2270 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002271 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002272
2273 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2274
2275 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2276 if (rc) {
2277 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002278 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002279 }
2280
2281 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2282
2283 ptlrpc_request_set_replen(req);
2284
2285 rc = ptlrpc_queue_wait(req);
2286 if (rc)
2287 ptlrpc_req_finished(req);
2288 else
2289 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002290 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002291}
2292
2293static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2294 enum obd_import_event event)
2295{
2296 int rc = 0;
2297
2298 LASSERT(imp->imp_obd == obd);
2299
2300 switch (event) {
2301 case IMP_EVENT_DISCON: {
2302#if 0
2303 /* XXX Pass event up to OBDs stack. used only for FLD now */
2304 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2305#endif
2306 break;
2307 }
2308 case IMP_EVENT_INACTIVE: {
2309 struct client_obd *cli = &obd->u.cli;
2310 /*
2311 * Flush current sequence to make client obtain new one
2312 * from server in case of disconnect/reconnect.
2313 */
2314 if (cli->cl_seq != NULL)
2315 seq_client_flush(cli->cl_seq);
2316
2317 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2318 break;
2319 }
2320 case IMP_EVENT_INVALIDATE: {
2321 struct ldlm_namespace *ns = obd->obd_namespace;
2322
2323 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2324
2325 break;
2326 }
2327 case IMP_EVENT_ACTIVE:
2328 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002329 /* redo the kuc registration after reconnecting */
Peng Taod7e09d02013-05-02 16:46:55 +08002330 if (rc == 0)
2331 rc = mdc_kuc_reregister(imp);
2332 break;
2333 case IMP_EVENT_OCD:
2334 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2335 break;
2336 case IMP_EVENT_DEACTIVATE:
2337 case IMP_EVENT_ACTIVATE:
2338 break;
2339 default:
2340 CERROR("Unknown import event %x\n", event);
2341 LBUG();
2342 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002343 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002344}
2345
2346int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2347 struct md_op_data *op_data)
2348{
2349 struct client_obd *cli = &exp->exp_obd->u.cli;
2350 struct lu_client_seq *seq = cli->cl_seq;
Greg Kroah-Hartman29aaf492013-08-02 18:14:51 +08002351
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002352 return seq_client_alloc_fid(NULL, seq, fid);
Peng Taod7e09d02013-05-02 16:46:55 +08002353}
2354
Luca Ceresolif6219c12015-01-18 15:06:47 +01002355static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
Srikrishan Malik982ec912014-08-11 23:57:29 +05302356{
Peng Taod7e09d02013-05-02 16:46:55 +08002357 struct client_obd *cli = &exp->exp_obd->u.cli;
Srikrishan Malik7436d072014-08-11 23:57:33 +05302358
Peng Taod7e09d02013-05-02 16:46:55 +08002359 return &cli->cl_target_uuid;
2360}
2361
2362/**
2363 * Determine whether the lock can be canceled before replaying it during
2364 * recovery, non zero value will be return if the lock can be canceled,
2365 * or zero returned for not
2366 */
2367static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2368{
2369 if (lock->l_resource->lr_type != LDLM_IBITS)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002370 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002371
2372 /* FIXME: if we ever get into a situation where there are too many
2373 * opened files with open locks on a single node, then we really
2374 * should replay these open locks to reget it */
2375 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002376 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002377
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002378 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08002379}
2380
2381static int mdc_resource_inode_free(struct ldlm_resource *res)
2382{
2383 if (res->lr_lvb_inode)
2384 res->lr_lvb_inode = NULL;
2385
2386 return 0;
2387}
2388
Luca Ceresolif6219c12015-01-18 15:06:47 +01002389static struct ldlm_valblock_ops inode_lvbo = {
Emil Goode805e5172013-07-28 00:38:56 +02002390 .lvbo_free = mdc_resource_inode_free,
Peng Taod7e09d02013-05-02 16:46:55 +08002391};
2392
John L. Hammond903af112014-09-05 15:08:12 -05002393static int mdc_llog_init(struct obd_device *obd)
2394{
2395 struct obd_llog_group *olg = &obd->obd_olg;
2396 struct llog_ctxt *ctxt;
2397 int rc;
2398
2399 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2400 &llog_client_ops);
2401 if (rc)
2402 return rc;
2403
2404 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2405 llog_initiator_connect(ctxt);
2406 llog_ctxt_put(ctxt);
2407
2408 return 0;
2409}
2410
2411static void mdc_llog_finish(struct obd_device *obd)
2412{
2413 struct llog_ctxt *ctxt;
2414
2415 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2416 if (ctxt)
2417 llog_cleanup(NULL, ctxt);
2418}
2419
Peng Taod7e09d02013-05-02 16:46:55 +08002420static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2421{
2422 struct client_obd *cli = &obd->u.cli;
Radek Dostalea7893b2014-07-27 23:22:57 +02002423 struct lprocfs_static_vars lvars = { NULL };
Peng Taod7e09d02013-05-02 16:46:55 +08002424 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002425
Julia Lawall7b817792015-05-01 17:51:17 +02002426 cli->cl_rpc_lock = kzalloc(sizeof(*cli->cl_rpc_lock), GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +08002427 if (!cli->cl_rpc_lock)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002428 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002429 mdc_init_rpc_lock(cli->cl_rpc_lock);
2430
2431 ptlrpcd_addref();
2432
Julia Lawall7b817792015-05-01 17:51:17 +02002433 cli->cl_close_lock = kzalloc(sizeof(*cli->cl_close_lock), GFP_NOFS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002434 if (!cli->cl_close_lock) {
2435 rc = -ENOMEM;
2436 goto err_rpc_lock;
2437 }
Peng Taod7e09d02013-05-02 16:46:55 +08002438 mdc_init_rpc_lock(cli->cl_close_lock);
2439
2440 rc = client_obd_setup(obd, cfg);
2441 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02002442 goto err_close_lock;
Peng Taod7e09d02013-05-02 16:46:55 +08002443 lprocfs_mdc_init_vars(&lvars);
Oleg Drokin9b801302015-05-21 15:32:16 -04002444 lprocfs_obd_setup(obd, lvars.obd_vars, lvars.sysfs_vars);
Peng Taod7e09d02013-05-02 16:46:55 +08002445 sptlrpc_lprocfs_cliobd_attach(obd);
2446 ptlrpc_lprocfs_register_obd(obd);
2447
2448 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2449
2450 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2451
John L. Hammond903af112014-09-05 15:08:12 -05002452 rc = mdc_llog_init(obd);
Peng Taod7e09d02013-05-02 16:46:55 +08002453 if (rc) {
2454 mdc_cleanup(obd);
2455 CERROR("failed to setup llogging subsystems\n");
2456 }
2457
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002458 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002459
2460err_close_lock:
Julia Lawall7b817792015-05-01 17:51:17 +02002461 kfree(cli->cl_close_lock);
Peng Taod7e09d02013-05-02 16:46:55 +08002462err_rpc_lock:
Julia Lawall7b817792015-05-01 17:51:17 +02002463 kfree(cli->cl_rpc_lock);
Peng Taod7e09d02013-05-02 16:46:55 +08002464 ptlrpcd_decref();
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002465 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002466}
2467
2468/* Initialize the default and maximum LOV EA and cookie sizes. This allows
Brian Behlendorf44779342014-04-27 13:06:47 -04002469 * us to make MDS RPCs with large enough reply buffers to hold a default
2470 * sized EA and cookie without having to calculate this (via a call into the
2471 * LOV + OSCs) each time we make an RPC. The maximum size is also tracked
2472 * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2473 * a large number of stripes is possible. If a larger reply buffer is
2474 * required it will be reallocated in the ptlrpc layer due to overflow.
2475 */
Peng Taod7e09d02013-05-02 16:46:55 +08002476static int mdc_init_ea_size(struct obd_export *exp, int easize,
Brian Behlendorf44779342014-04-27 13:06:47 -04002477 int def_easize, int cookiesize, int def_cookiesize)
Peng Taod7e09d02013-05-02 16:46:55 +08002478{
2479 struct obd_device *obd = exp->exp_obd;
2480 struct client_obd *cli = &obd->u.cli;
Peng Taod7e09d02013-05-02 16:46:55 +08002481
2482 if (cli->cl_max_mds_easize < easize)
2483 cli->cl_max_mds_easize = easize;
2484
2485 if (cli->cl_default_mds_easize < def_easize)
2486 cli->cl_default_mds_easize = def_easize;
2487
2488 if (cli->cl_max_mds_cookiesize < cookiesize)
2489 cli->cl_max_mds_cookiesize = cookiesize;
2490
Brian Behlendorf44779342014-04-27 13:06:47 -04002491 if (cli->cl_default_mds_cookiesize < def_cookiesize)
2492 cli->cl_default_mds_cookiesize = def_cookiesize;
2493
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002494 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002495}
2496
2497static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2498{
Peng Taod7e09d02013-05-02 16:46:55 +08002499 switch (stage) {
2500 case OBD_CLEANUP_EARLY:
2501 break;
2502 case OBD_CLEANUP_EXPORTS:
2503 /* Failsafe, ok if racy */
2504 if (obd->obd_type->typ_refcnt <= 1)
2505 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2506
2507 obd_cleanup_client_import(obd);
2508 ptlrpc_lprocfs_unregister_obd(obd);
2509 lprocfs_obd_cleanup(obd);
2510
John L. Hammond903af112014-09-05 15:08:12 -05002511 mdc_llog_finish(obd);
Peng Taod7e09d02013-05-02 16:46:55 +08002512 break;
2513 }
Tina Johnsoneb967122014-09-14 18:36:38 +05302514 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002515}
2516
2517static int mdc_cleanup(struct obd_device *obd)
2518{
2519 struct client_obd *cli = &obd->u.cli;
2520
Julia Lawall7b817792015-05-01 17:51:17 +02002521 kfree(cli->cl_rpc_lock);
2522 kfree(cli->cl_close_lock);
Peng Taod7e09d02013-05-02 16:46:55 +08002523
2524 ptlrpcd_decref();
2525
2526 return client_obd_cleanup(obd);
2527}
2528
Oleg Drokin21aef7d2014-08-15 12:55:56 -04002529static int mdc_process_config(struct obd_device *obd, u32 len, void *buf)
Peng Taod7e09d02013-05-02 16:46:55 +08002530{
2531 struct lustre_cfg *lcfg = buf;
Radek Dostalea7893b2014-07-27 23:22:57 +02002532 struct lprocfs_static_vars lvars = { NULL };
Peng Taod7e09d02013-05-02 16:46:55 +08002533 int rc = 0;
2534
2535 lprocfs_mdc_init_vars(&lvars);
2536 switch (lcfg->lcfg_command) {
2537 default:
2538 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2539 lcfg, obd);
2540 if (rc > 0)
2541 rc = 0;
2542 break;
2543 }
Srikrishan Malikeb445202014-08-11 23:57:38 +05302544 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002545}
2546
2547
2548/* get remote permission for current user on fid */
Luca Ceresolif6219c12015-01-18 15:06:47 +01002549static int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2550 struct obd_capa *oc, __u32 suppgid,
2551 struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +08002552{
2553 struct ptlrpc_request *req;
2554 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002555
2556 LASSERT(client_is_remote(exp));
2557
2558 *request = NULL;
2559 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2560 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002561 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002562
2563 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2564
2565 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2566 if (rc) {
2567 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002568 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002569 }
2570
2571 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2572
2573 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2574 sizeof(struct mdt_remote_perm));
2575
2576 ptlrpc_request_set_replen(req);
2577
2578 rc = ptlrpc_queue_wait(req);
2579 if (rc)
2580 ptlrpc_req_finished(req);
2581 else
2582 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002583 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002584}
2585
2586static int mdc_interpret_renew_capa(const struct lu_env *env,
2587 struct ptlrpc_request *req, void *args,
2588 int status)
2589{
2590 struct mdc_renew_capa_args *ra = args;
2591 struct mdt_body *body = NULL;
2592 struct lustre_capa *capa;
Peng Taod7e09d02013-05-02 16:46:55 +08002593
Julia Lawalld5fdc202014-08-28 12:10:35 +02002594 if (status) {
2595 capa = ERR_PTR(status);
2596 goto out;
2597 }
Peng Taod7e09d02013-05-02 16:46:55 +08002598
2599 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002600 if (body == NULL) {
2601 capa = ERR_PTR(-EFAULT);
2602 goto out;
2603 }
Peng Taod7e09d02013-05-02 16:46:55 +08002604
Julia Lawalld5fdc202014-08-28 12:10:35 +02002605 if ((body->valid & OBD_MD_FLOSSCAPA) == 0) {
2606 capa = ERR_PTR(-ENOENT);
2607 goto out;
2608 }
Peng Taod7e09d02013-05-02 16:46:55 +08002609
2610 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002611 if (!capa) {
2612 capa = ERR_PTR(-EFAULT);
2613 goto out;
2614 }
Peng Taod7e09d02013-05-02 16:46:55 +08002615out:
2616 ra->ra_cb(ra->ra_oc, capa);
2617 return 0;
2618}
2619
2620static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2621 renew_capa_cb_t cb)
2622{
2623 struct ptlrpc_request *req;
2624 struct mdc_renew_capa_args *ra;
Peng Taod7e09d02013-05-02 16:46:55 +08002625
2626 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2627 LUSTRE_MDS_VERSION, MDS_GETATTR);
2628 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002629 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002630
2631 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2632 * capa to renew is oss capa.
2633 */
2634 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2635 ptlrpc_request_set_replen(req);
2636
2637 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2638 ra = ptlrpc_req_async_args(req);
2639 ra->ra_oc = oc;
2640 ra->ra_cb = cb;
2641 req->rq_interpret_reply = mdc_interpret_renew_capa;
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002642 ptlrpcd_add_req(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002643 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002644}
2645
Luca Ceresolif6219c12015-01-18 15:06:47 +01002646static struct obd_ops mdc_obd_ops = {
Peng Taod7e09d02013-05-02 16:46:55 +08002647 .o_owner = THIS_MODULE,
2648 .o_setup = mdc_setup,
2649 .o_precleanup = mdc_precleanup,
2650 .o_cleanup = mdc_cleanup,
2651 .o_add_conn = client_import_add_conn,
2652 .o_del_conn = client_import_del_conn,
Mikhail Pershina7c6d5a2013-12-09 22:56:56 +08002653 .o_connect = client_connect_import,
Peng Taod7e09d02013-05-02 16:46:55 +08002654 .o_disconnect = client_disconnect_export,
2655 .o_iocontrol = mdc_iocontrol,
2656 .o_set_info_async = mdc_set_info_async,
2657 .o_statfs = mdc_statfs,
Peng Taod7e09d02013-05-02 16:46:55 +08002658 .o_fid_init = client_fid_init,
2659 .o_fid_fini = client_fid_fini,
2660 .o_fid_alloc = mdc_fid_alloc,
2661 .o_import_event = mdc_import_event,
Peng Taod7e09d02013-05-02 16:46:55 +08002662 .o_get_info = mdc_get_info,
2663 .o_process_config = mdc_process_config,
2664 .o_get_uuid = mdc_get_uuid,
2665 .o_quotactl = mdc_quotactl,
2666 .o_quotacheck = mdc_quotacheck
2667};
2668
Luca Ceresolif6219c12015-01-18 15:06:47 +01002669static struct md_ops mdc_md_ops = {
Peng Taod7e09d02013-05-02 16:46:55 +08002670 .m_getstatus = mdc_getstatus,
2671 .m_null_inode = mdc_null_inode,
2672 .m_find_cbdata = mdc_find_cbdata,
2673 .m_close = mdc_close,
2674 .m_create = mdc_create,
2675 .m_done_writing = mdc_done_writing,
2676 .m_enqueue = mdc_enqueue,
2677 .m_getattr = mdc_getattr,
2678 .m_getattr_name = mdc_getattr_name,
2679 .m_intent_lock = mdc_intent_lock,
2680 .m_link = mdc_link,
2681 .m_is_subdir = mdc_is_subdir,
2682 .m_rename = mdc_rename,
2683 .m_setattr = mdc_setattr,
2684 .m_setxattr = mdc_setxattr,
2685 .m_getxattr = mdc_getxattr,
2686 .m_sync = mdc_sync,
2687 .m_readpage = mdc_readpage,
2688 .m_unlink = mdc_unlink,
2689 .m_cancel_unused = mdc_cancel_unused,
2690 .m_init_ea_size = mdc_init_ea_size,
2691 .m_set_lock_data = mdc_set_lock_data,
2692 .m_lock_match = mdc_lock_match,
2693 .m_get_lustre_md = mdc_get_lustre_md,
2694 .m_free_lustre_md = mdc_free_lustre_md,
2695 .m_set_open_replay_data = mdc_set_open_replay_data,
2696 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2697 .m_renew_capa = mdc_renew_capa,
2698 .m_unpack_capa = mdc_unpack_capa,
2699 .m_get_remote_perm = mdc_get_remote_perm,
2700 .m_intent_getattr_async = mdc_intent_getattr_async,
2701 .m_revalidate_lock = mdc_revalidate_lock
2702};
2703
Luca Ceresolif6219c12015-01-18 15:06:47 +01002704static int __init mdc_init(void)
Peng Taod7e09d02013-05-02 16:46:55 +08002705{
Radek Dostalea7893b2014-07-27 23:22:57 +02002706 struct lprocfs_static_vars lvars = { NULL };
Srikrishan Malik7436d072014-08-11 23:57:33 +05302707
Peng Taod7e09d02013-05-02 16:46:55 +08002708 lprocfs_mdc_init_vars(&lvars);
2709
Oleg Drokin2962b442015-05-21 15:32:13 -04002710 return class_register_type(&mdc_obd_ops, &mdc_md_ops,
Peng Taod7e09d02013-05-02 16:46:55 +08002711 LUSTRE_MDC_NAME, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +08002712}
2713
2714static void /*__exit*/ mdc_exit(void)
2715{
2716 class_unregister_type(LUSTRE_MDC_NAME);
2717}
2718
2719MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2720MODULE_DESCRIPTION("Lustre Metadata Client");
2721MODULE_LICENSE("GPL");
2722
2723module_init(mdc_init);
2724module_exit(mdc_exit);