blob: e172be1bc1c6eb4e84bdf1b4a53275971838fe64 [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
Peng Taod7e09d02013-05-02 16:46:55 +080056static int mdc_cleanup(struct obd_device *obd);
57
Peng Taod7e09d02013-05-02 16:46:55 +080058static inline int mdc_queue_wait(struct ptlrpc_request *req)
59{
60 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
61 int rc;
62
63 /* mdc_enter_request() ensures that this client has no more
64 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
65 * against an MDT. */
66 rc = mdc_enter_request(cli);
67 if (rc != 0)
68 return rc;
69
70 rc = ptlrpc_queue_wait(req);
71 mdc_exit_request(cli);
72
73 return rc;
74}
75
Oleg Drokinef2e0f52015-09-27 16:45:46 -040076static int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid)
Peng Taod7e09d02013-05-02 16:46:55 +080077{
78 struct ptlrpc_request *req;
79 struct mdt_body *body;
80 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +080081
Oleg Drokinef2e0f52015-09-27 16:45:46 -040082 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
83 &RQF_MDS_GETSTATUS,
Peng Taod7e09d02013-05-02 16:46:55 +080084 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
85 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080086 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +080087
Oleg Drokinef2e0f52015-09-27 16:45:46 -040088 mdc_pack_body(req, NULL, 0, 0, -1, 0);
89 req->rq_send_state = LUSTRE_IMP_FULL;
Peng Taod7e09d02013-05-02 16:46:55 +080090
91 ptlrpc_request_set_replen(req);
92
93 rc = ptlrpc_queue_wait(req);
94 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +020095 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +080096
97 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
Julia Lawalld5fdc202014-08-28 12:10:35 +020098 if (body == NULL) {
99 rc = -EPROTO;
100 goto out;
101 }
Peng Taod7e09d02013-05-02 16:46:55 +0800102
Peng Taod7e09d02013-05-02 16:46:55 +0800103 *rootfid = body->fid1;
104 CDEBUG(D_NET,
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700105 "root fid="DFID", last_committed=%llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800106 PFID(rootfid),
107 lustre_msg_get_last_committed(req->rq_repmsg));
Peng Taod7e09d02013-05-02 16:46:55 +0800108out:
109 ptlrpc_req_finished(req);
110 return rc;
111}
112
Peng Taod7e09d02013-05-02 16:46:55 +0800113/*
114 * This function now is known to always saying that it will receive 4 buffers
115 * from server. Even for cases when acl_size and md_size is zero, RPC header
116 * will contain 4 fields and RPC itself will contain zero size fields. This is
117 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
118 * and thus zero, it shrinks it, making zero size. The same story about
119 * md_size. And this is course of problem when client waits for smaller number
120 * of fields. This issue will be fixed later when client gets aware of RPC
121 * layouts. --umka
122 */
123static int mdc_getattr_common(struct obd_export *exp,
124 struct ptlrpc_request *req)
125{
126 struct req_capsule *pill = &req->rq_pill;
127 struct mdt_body *body;
128 void *eadata;
129 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800130
131 /* Request message already built. */
132 rc = ptlrpc_queue_wait(req);
133 if (rc != 0)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800134 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800135
136 /* sanity check for the reply */
137 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
138 if (body == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800139 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800140
141 CDEBUG(D_NET, "mode: %o\n", body->mode);
142
143 if (body->eadatasize != 0) {
144 mdc_update_max_ea_from_body(exp, body);
145
146 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
147 body->eadatasize);
148 if (eadata == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800149 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800150 }
151
152 if (body->valid & OBD_MD_FLRMTPERM) {
153 struct mdt_remote_perm *perm;
154
155 LASSERT(client_is_remote(exp));
156 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
157 lustre_swab_mdt_remote_perm);
158 if (perm == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800159 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800160 }
161
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800162 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800163}
164
Janet Liu60ebee32014-11-30 18:08:10 +0800165static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
Peng Taod7e09d02013-05-02 16:46:55 +0800166 struct ptlrpc_request **request)
167{
168 struct ptlrpc_request *req;
169 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800170
171 /* Single MDS without an LMV case */
172 if (op_data->op_flags & MF_GET_MDT_IDX) {
173 op_data->op_mds = 0;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800174 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800175 }
176 *request = NULL;
177 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
178 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800179 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800180
Peng Taod7e09d02013-05-02 16:46:55 +0800181 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
182 if (rc) {
183 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800184 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800185 }
186
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400187 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
188 op_data->op_mode, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +0800189
190 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
191 op_data->op_mode);
192 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
193 LASSERT(client_is_remote(exp));
194 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
195 sizeof(struct mdt_remote_perm));
196 }
197 ptlrpc_request_set_replen(req);
198
199 rc = mdc_getattr_common(exp, req);
200 if (rc)
201 ptlrpc_req_finished(req);
202 else
203 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800204 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800205}
206
Janet Liu60ebee32014-11-30 18:08:10 +0800207static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
Peng Taod7e09d02013-05-02 16:46:55 +0800208 struct ptlrpc_request **request)
209{
210 struct ptlrpc_request *req;
211 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800212
213 *request = NULL;
214 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
215 &RQF_MDS_GETATTR_NAME);
216 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800217 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800218
Peng Taod7e09d02013-05-02 16:46:55 +0800219 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
220 op_data->op_namelen + 1);
221
222 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
223 if (rc) {
224 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800225 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800226 }
227
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400228 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
229 op_data->op_mode, op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +0800230
231 if (op_data->op_name) {
232 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
Srikrishan Malik7436d072014-08-11 23:57:33 +0530233
Peng Taod7e09d02013-05-02 16:46:55 +0800234 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
235 op_data->op_namelen);
236 memcpy(name, op_data->op_name, op_data->op_namelen);
237 }
238
239 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
240 op_data->op_mode);
241 ptlrpc_request_set_replen(req);
242
243 rc = mdc_getattr_common(exp, req);
244 if (rc)
245 ptlrpc_req_finished(req);
246 else
247 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800248 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800249}
250
251static int mdc_is_subdir(struct obd_export *exp,
252 const struct lu_fid *pfid,
253 const struct lu_fid *cfid,
254 struct ptlrpc_request **request)
255{
256 struct ptlrpc_request *req;
257 int rc;
258
Peng Taod7e09d02013-05-02 16:46:55 +0800259 *request = NULL;
260 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
261 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
262 MDS_IS_SUBDIR);
263 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800264 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800265
266 mdc_is_subdir_pack(req, pfid, cfid, 0);
267 ptlrpc_request_set_replen(req);
268
269 rc = ptlrpc_queue_wait(req);
270 if (rc && rc != -EREMOTE)
271 ptlrpc_req_finished(req);
272 else
273 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800274 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800275}
276
Srikrishan Malik301af902014-08-11 23:57:31 +0530277static int mdc_xattr_common(struct obd_export *exp,
278 const struct req_format *fmt,
Peng Taod7e09d02013-05-02 16:46:55 +0800279 const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400280 int opcode, u64 valid,
Peng Taod7e09d02013-05-02 16:46:55 +0800281 const char *xattr_name, const char *input,
282 int input_size, int output_size, int flags,
283 __u32 suppgid, struct ptlrpc_request **request)
284{
285 struct ptlrpc_request *req;
286 int xattr_namelen = 0;
287 char *tmp;
288 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800289
290 *request = NULL;
291 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
292 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800293 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800294
Peng Taod7e09d02013-05-02 16:46:55 +0800295 if (xattr_name) {
296 xattr_namelen = strlen(xattr_name) + 1;
297 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
298 xattr_namelen);
299 }
300 if (input_size) {
301 LASSERT(input);
302 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
303 input_size);
304 }
305
Andrew Perepechkoe93a3082014-02-09 02:51:48 -0500306 /* Flush local XATTR locks to get rid of a possible cancel RPC */
307 if (opcode == MDS_REINT && fid_is_sane(fid) &&
308 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
309 LIST_HEAD(cancels);
310 int count;
311
312 /* Without that packing would fail */
313 if (input_size == 0)
314 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
315 RCL_CLIENT, 0);
316
317 count = mdc_resource_get_unused(exp, fid,
318 &cancels, LCK_EX,
319 MDS_INODELOCK_XATTR);
320
321 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
322 if (rc) {
323 ptlrpc_request_free(req);
324 return rc;
325 }
326 } else {
327 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
328 if (rc) {
329 ptlrpc_request_free(req);
330 return rc;
331 }
Peng Taod7e09d02013-05-02 16:46:55 +0800332 }
333
334 if (opcode == MDS_REINT) {
335 struct mdt_rec_setxattr *rec;
336
337 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
338 sizeof(struct mdt_rec_reint));
339 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
340 rec->sx_opcode = REINT_SETXATTR;
Peng Tao4b1a25f2013-07-15 22:27:14 +0800341 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
342 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
Peng Taod7e09d02013-05-02 16:46:55 +0800343 rec->sx_cap = cfs_curproc_cap_pack();
344 rec->sx_suppgid1 = suppgid;
345 rec->sx_suppgid2 = -1;
346 rec->sx_fid = *fid;
347 rec->sx_valid = valid | OBD_MD_FLCTIME;
Arnd Bergmann14e3f922015-09-27 16:45:27 -0400348 rec->sx_time = ktime_get_real_seconds();
Peng Taod7e09d02013-05-02 16:46:55 +0800349 rec->sx_size = output_size;
350 rec->sx_flags = flags;
351
Peng Taod7e09d02013-05-02 16:46:55 +0800352 } else {
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400353 mdc_pack_body(req, fid, valid, output_size, suppgid, flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800354 }
355
356 if (xattr_name) {
357 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
358 memcpy(tmp, xattr_name, xattr_namelen);
359 }
360 if (input_size) {
361 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
362 memcpy(tmp, input, input_size);
363 }
364
365 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
366 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
367 RCL_SERVER, output_size);
368 ptlrpc_request_set_replen(req);
369
370 /* make rpc */
371 if (opcode == MDS_REINT)
372 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
373
374 rc = ptlrpc_queue_wait(req);
375
376 if (opcode == MDS_REINT)
377 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
378
379 if (rc)
380 ptlrpc_req_finished(req);
381 else
382 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800383 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800384}
385
Janet Liu60ebee32014-11-30 18:08:10 +0800386static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400387 u64 valid, const char *xattr_name,
388 const char *input, int input_size, int output_size,
389 int flags, __u32 suppgid,
390 struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800391{
392 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400393 fid, MDS_REINT, valid, xattr_name,
Peng Taod7e09d02013-05-02 16:46:55 +0800394 input, input_size, output_size, flags,
395 suppgid, request);
396}
397
Janet Liu60ebee32014-11-30 18:08:10 +0800398static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400399 u64 valid, const char *xattr_name,
400 const char *input, int input_size, int output_size,
401 int flags, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800402{
403 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400404 fid, MDS_GETXATTR, valid, xattr_name,
Peng Taod7e09d02013-05-02 16:46:55 +0800405 input, input_size, output_size, flags,
406 -1, request);
407}
408
409#ifdef CONFIG_FS_POSIX_ACL
410static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
411{
412 struct req_capsule *pill = &req->rq_pill;
413 struct mdt_body *body = md->body;
414 struct posix_acl *acl;
415 void *buf;
416 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800417
418 if (!body->aclsize)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800419 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800420
421 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
422
423 if (!buf)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800424 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800425
426 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
Christopher J. Morronef4289402015-03-25 21:53:18 -0400427 if (acl == NULL)
428 return 0;
429
Peng Taod7e09d02013-05-02 16:46:55 +0800430 if (IS_ERR(acl)) {
431 rc = PTR_ERR(acl);
432 CERROR("convert xattr to acl: %d\n", rc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800433 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800434 }
435
436 rc = posix_acl_valid(acl);
437 if (rc) {
438 CERROR("validate acl: %d\n", rc);
439 posix_acl_release(acl);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800440 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800441 }
442
443 md->posix_acl = acl;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800444 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800445}
446#else
447#define mdc_unpack_acl(req, md) 0
448#endif
449
Shraddha Barke358855b2015-10-30 20:59:03 +0530450static int mdc_get_lustre_md(struct obd_export *exp,
451 struct ptlrpc_request *req,
452 struct obd_export *dt_exp,
453 struct obd_export *md_exp,
454 struct lustre_md *md)
Peng Taod7e09d02013-05-02 16:46:55 +0800455{
456 struct req_capsule *pill = &req->rq_pill;
457 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800458
459 LASSERT(md);
460 memset(md, 0, sizeof(*md));
461
462 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
463 LASSERT(md->body != NULL);
464
465 if (md->body->valid & OBD_MD_FLEASIZE) {
466 int lmmsize;
467 struct lov_mds_md *lmm;
468
469 if (!S_ISREG(md->body->mode)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530470 CDEBUG(D_INFO,
471 "OBD_MD_FLEASIZE set, should be a regular file, but is not\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200472 rc = -EPROTO;
473 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800474 }
475
476 if (md->body->eadatasize == 0) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530477 CDEBUG(D_INFO,
478 "OBD_MD_FLEASIZE set, but eadatasize 0\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200479 rc = -EPROTO;
480 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800481 }
482 lmmsize = md->body->eadatasize;
483 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200484 if (!lmm) {
485 rc = -EPROTO;
486 goto out;
487 }
Peng Taod7e09d02013-05-02 16:46:55 +0800488
489 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
490 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200491 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800492
493 if (rc < sizeof(*md->lsm)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530494 CDEBUG(D_INFO,
495 "lsm size too small: rc < sizeof (*md->lsm) (%d < %d)\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800496 rc, (int)sizeof(*md->lsm));
Julia Lawalld5fdc202014-08-28 12:10:35 +0200497 rc = -EPROTO;
498 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800499 }
500
501 } else if (md->body->valid & OBD_MD_FLDIREA) {
502 int lmvsize;
503 struct lov_mds_md *lmv;
504
Srikrishan Malik301af902014-08-11 23:57:31 +0530505 if (!S_ISDIR(md->body->mode)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530506 CDEBUG(D_INFO,
507 "OBD_MD_FLDIREA set, should be a directory, but is not\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200508 rc = -EPROTO;
509 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800510 }
511
512 if (md->body->eadatasize == 0) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530513 CDEBUG(D_INFO,
514 "OBD_MD_FLDIREA is set, but eadatasize 0\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800515 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800516 }
517 if (md->body->valid & OBD_MD_MEA) {
518 lmvsize = md->body->eadatasize;
519 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
520 lmvsize);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200521 if (!lmv) {
522 rc = -EPROTO;
523 goto out;
524 }
Peng Taod7e09d02013-05-02 16:46:55 +0800525
526 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
527 lmvsize);
528 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200529 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800530
531 if (rc < sizeof(*md->mea)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530532 CDEBUG(D_INFO,
533 "size too small: rc < sizeof(*md->mea) (%d < %d)\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800534 rc, (int)sizeof(*md->mea));
Julia Lawalld5fdc202014-08-28 12:10:35 +0200535 rc = -EPROTO;
536 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800537 }
538 }
539 }
540 rc = 0;
541
542 if (md->body->valid & OBD_MD_FLRMTPERM) {
543 /* remote permission */
544 LASSERT(client_is_remote(exp));
545 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
546 lustre_swab_mdt_remote_perm);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200547 if (!md->remote_perm) {
548 rc = -EPROTO;
549 goto out;
550 }
Srikrishan Malik78dd0792014-08-11 23:57:37 +0530551 } else if (md->body->valid & OBD_MD_FLACL) {
Peng Taod7e09d02013-05-02 16:46:55 +0800552 /* for ACL, it's possible that FLACL is set but aclsize is zero.
553 * only when aclsize != 0 there's an actual segment for ACL
554 * in reply buffer.
555 */
556 if (md->body->aclsize) {
557 rc = mdc_unpack_acl(req, md);
558 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200559 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800560#ifdef CONFIG_FS_POSIX_ACL
561 } else {
562 md->posix_acl = NULL;
563#endif
564 }
565 }
Peng Taod7e09d02013-05-02 16:46:55 +0800566
Peng Taod7e09d02013-05-02 16:46:55 +0800567out:
568 if (rc) {
Peng Taod7e09d02013-05-02 16:46:55 +0800569#ifdef CONFIG_FS_POSIX_ACL
570 posix_acl_release(md->posix_acl);
571#endif
572 if (md->lsm)
573 obd_free_memmd(dt_exp, &md->lsm);
574 }
575 return rc;
576}
577
Shraddha Barke358855b2015-10-30 20:59:03 +0530578static int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
Peng Taod7e09d02013-05-02 16:46:55 +0800579{
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800580 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800581}
582
583/**
584 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
585 * RPC chains.
586 */
587void mdc_replay_open(struct ptlrpc_request *req)
588{
589 struct md_open_data *mod = req->rq_cb_data;
590 struct ptlrpc_request *close_req;
591 struct obd_client_handle *och;
592 struct lustre_handle old;
593 struct mdt_body *body;
Peng Taod7e09d02013-05-02 16:46:55 +0800594
595 if (mod == NULL) {
596 DEBUG_REQ(D_ERROR, req,
597 "Can't properly replay without open data.");
Peng Taod7e09d02013-05-02 16:46:55 +0800598 return;
599 }
600
601 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
602 LASSERT(body != NULL);
603
604 och = mod->mod_och;
605 if (och != NULL) {
606 struct lustre_handle *file_fh;
607
608 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
609
610 file_fh = &och->och_fh;
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700611 CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800612 file_fh->cookie, body->handle.cookie);
613 old = *file_fh;
614 *file_fh = body->handle;
615 }
616 close_req = mod->mod_close_req;
617 if (close_req != NULL) {
618 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
619 struct mdt_ioepoch *epoch;
620
621 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
622 epoch = req_capsule_client_get(&close_req->rq_pill,
623 &RMF_MDT_EPOCH);
624 LASSERT(epoch);
625
626 if (och != NULL)
627 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
628 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
629 epoch->handle = body->handle;
630 }
Peng Taod7e09d02013-05-02 16:46:55 +0800631}
632
633void mdc_commit_open(struct ptlrpc_request *req)
634{
635 struct md_open_data *mod = req->rq_cb_data;
Srikrishan Malik7436d072014-08-11 23:57:33 +0530636
Peng Taod7e09d02013-05-02 16:46:55 +0800637 if (mod == NULL)
638 return;
639
640 /**
641 * No need to touch md_open_data::mod_och, it holds a reference on
642 * \var mod and will zero references to each other, \var mod will be
643 * freed after that when md_open_data::mod_och will put the reference.
644 */
645
646 /**
647 * Do not let open request to disappear as it still may be needed
648 * for close rpc to happen (it may happen on evict only, otherwise
649 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
650 * called), just mark this rpc as committed to distinguish these 2
651 * cases, see mdc_close() for details. The open request reference will
652 * be put along with freeing \var mod.
653 */
654 ptlrpc_request_addref(req);
655 spin_lock(&req->rq_lock);
656 req->rq_committed = 1;
657 spin_unlock(&req->rq_lock);
658 req->rq_cb_data = NULL;
659 obd_mod_put(mod);
660}
661
662int mdc_set_open_replay_data(struct obd_export *exp,
663 struct obd_client_handle *och,
Hongchao Zhang63d42572014-02-28 21:16:37 -0500664 struct lookup_intent *it)
Peng Taod7e09d02013-05-02 16:46:55 +0800665{
666 struct md_open_data *mod;
667 struct mdt_rec_create *rec;
668 struct mdt_body *body;
Hongchao Zhang63d42572014-02-28 21:16:37 -0500669 struct ptlrpc_request *open_req = it->d.lustre.it_data;
Peng Taod7e09d02013-05-02 16:46:55 +0800670 struct obd_import *imp = open_req->rq_import;
Peng Taod7e09d02013-05-02 16:46:55 +0800671
672 if (!open_req->rq_replay)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800673 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800674
675 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
676 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
677 LASSERT(rec != NULL);
678 /* Incoming message in my byte order (it's been swabbed). */
679 /* Outgoing messages always in my byte order. */
680 LASSERT(body != NULL);
681
682 /* Only if the import is replayable, we set replay_open data */
683 if (och && imp->imp_replayable) {
684 mod = obd_mod_alloc();
685 if (mod == NULL) {
686 DEBUG_REQ(D_ERROR, open_req,
687 "Can't allocate md_open_data");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800688 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800689 }
690
691 /**
692 * Take a reference on \var mod, to be freed on mdc_close().
693 * It protects \var mod from being freed on eviction (commit
694 * callback is called despite rq_replay flag).
695 * Another reference for \var och.
696 */
697 obd_mod_get(mod);
698 obd_mod_get(mod);
699
700 spin_lock(&open_req->rq_lock);
701 och->och_mod = mod;
702 mod->mod_och = och;
Hongchao Zhang63d42572014-02-28 21:16:37 -0500703 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
704 it_disposition(it, DISP_OPEN_STRIPE);
Peng Taod7e09d02013-05-02 16:46:55 +0800705 mod->mod_open_req = open_req;
706 open_req->rq_cb_data = mod;
707 open_req->rq_commit_cb = mdc_commit_open;
708 spin_unlock(&open_req->rq_lock);
709 }
710
711 rec->cr_fid2 = body->fid1;
712 rec->cr_ioepoch = body->ioepoch;
713 rec->cr_old_handle.cookie = body->handle.cookie;
714 open_req->rq_replay_cb = mdc_replay_open;
715 if (!fid_is_sane(&body->fid1)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530716 DEBUG_REQ(D_ERROR, open_req,
717 "Saving replay request with insane fid");
Peng Taod7e09d02013-05-02 16:46:55 +0800718 LBUG();
719 }
720
721 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800722 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800723}
724
Hongchao Zhang63d42572014-02-28 21:16:37 -0500725static void mdc_free_open(struct md_open_data *mod)
726{
727 int committed = 0;
728
729 if (mod->mod_is_create == 0 &&
730 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
731 committed = 1;
732
733 LASSERT(mod->mod_open_req->rq_replay == 0);
734
735 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
736
737 ptlrpc_request_committed(mod->mod_open_req, committed);
738 if (mod->mod_close_req)
739 ptlrpc_request_committed(mod->mod_close_req, committed);
740}
741
Shraddha Barke358855b2015-10-30 20:59:03 +0530742static int mdc_clear_open_replay_data(struct obd_export *exp,
743 struct obd_client_handle *och)
Peng Taod7e09d02013-05-02 16:46:55 +0800744{
745 struct md_open_data *mod = och->och_mod;
Peng Taod7e09d02013-05-02 16:46:55 +0800746
747 /**
748 * It is possible to not have \var mod in a case of eviction between
749 * lookup and ll_file_open().
750 **/
751 if (mod == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800752 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800753
754 LASSERT(mod != LP_POISON);
Hongchao Zhang63d42572014-02-28 21:16:37 -0500755 LASSERT(mod->mod_open_req != NULL);
756 mdc_free_open(mod);
Peng Taod7e09d02013-05-02 16:46:55 +0800757
758 mod->mod_och = NULL;
759 och->och_mod = NULL;
760 obd_mod_put(mod);
761
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800762 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800763}
764
765/* Prepares the request for the replay by the given reply */
766static void mdc_close_handle_reply(struct ptlrpc_request *req,
767 struct md_op_data *op_data, int rc) {
768 struct mdt_body *repbody;
769 struct mdt_ioepoch *epoch;
770
771 if (req && rc == -EAGAIN) {
772 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
773 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
774
775 epoch->flags |= MF_SOM_AU;
776 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
777 op_data->op_flags |= MF_GETATTR_LOCK;
778 }
779}
780
Luca Ceresolif6219c12015-01-18 15:06:47 +0100781static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
782 struct md_open_data *mod, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800783{
784 struct obd_device *obd = class_exp2obd(exp);
785 struct ptlrpc_request *req;
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800786 struct req_format *req_fmt;
787 int rc;
788 int saved_rc = 0;
789
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800790 req_fmt = &RQF_MDS_CLOSE;
791 if (op_data->op_bias & MDS_HSM_RELEASE) {
792 req_fmt = &RQF_MDS_RELEASE_CLOSE;
793
794 /* allocate a FID for volatile file */
795 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
796 if (rc < 0) {
797 CERROR("%s: "DFID" failed to allocate FID: %d\n",
798 obd->obd_name, PFID(&op_data->op_fid1), rc);
799 /* save the errcode and proceed to close */
800 saved_rc = rc;
801 }
802 }
Peng Taod7e09d02013-05-02 16:46:55 +0800803
804 *request = NULL;
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800805 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
Peng Taod7e09d02013-05-02 16:46:55 +0800806 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800807 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800808
Peng Taod7e09d02013-05-02 16:46:55 +0800809 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
810 if (rc) {
811 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800812 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800813 }
814
815 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
816 * portal whose threads are not taking any DLM locks and are therefore
817 * always progressing */
818 req->rq_request_portal = MDS_READPAGE_PORTAL;
819 ptlrpc_at_set_req_timeout(req);
820
821 /* Ensure that this close's handle is fixed up during replay. */
822 if (likely(mod != NULL)) {
823 LASSERTF(mod->mod_open_req != NULL &&
824 mod->mod_open_req->rq_type != LI_POISON,
825 "POISONED open %p!\n", mod->mod_open_req);
826
827 mod->mod_close_req = req;
828
829 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
830 /* We no longer want to preserve this open for replay even
831 * though the open was committed. b=3632, b=3633 */
832 spin_lock(&mod->mod_open_req->rq_lock);
833 mod->mod_open_req->rq_replay = 0;
834 spin_unlock(&mod->mod_open_req->rq_lock);
835 } else {
Srikrishan Malike5e663a2014-08-11 23:57:30 +0530836 CDEBUG(D_HA,
837 "couldn't find open req; expecting close error\n");
Peng Taod7e09d02013-05-02 16:46:55 +0800838 }
839
840 mdc_close_pack(req, op_data);
841
842 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
Brian Behlendorf44779342014-04-27 13:06:47 -0400843 obd->u.cli.cl_default_mds_easize);
Peng Taod7e09d02013-05-02 16:46:55 +0800844 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
Brian Behlendorf44779342014-04-27 13:06:47 -0400845 obd->u.cli.cl_default_mds_cookiesize);
Peng Taod7e09d02013-05-02 16:46:55 +0800846
847 ptlrpc_request_set_replen(req);
848
849 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
850 rc = ptlrpc_queue_wait(req);
851 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
852
853 if (req->rq_repmsg == NULL) {
854 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
855 req->rq_status);
856 if (rc == 0)
857 rc = req->rq_status ?: -EIO;
858 } else if (rc == 0 || rc == -EAGAIN) {
859 struct mdt_body *body;
860
861 rc = lustre_msg_get_status(req->rq_repmsg);
862 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530863 DEBUG_REQ(D_ERROR, req,
864 "type == PTL_RPC_MSG_ERR, err = %d", rc);
Peng Taod7e09d02013-05-02 16:46:55 +0800865 if (rc > 0)
866 rc = -rc;
867 }
868 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
869 if (body == NULL)
870 rc = -EPROTO;
871 } else if (rc == -ESTALE) {
872 /**
873 * it can be allowed error after 3633 if open was committed and
874 * server failed before close was sent. Let's check if mod
875 * exists and return no error in that case
876 */
877 if (mod) {
878 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
879 LASSERT(mod->mod_open_req != NULL);
880 if (mod->mod_open_req->rq_committed)
881 rc = 0;
882 }
883 }
884
885 if (mod) {
886 if (rc != 0)
887 mod->mod_close_req = NULL;
888 /* Since now, mod is accessed through open_req only,
889 * thus close req does not keep a reference on mod anymore. */
890 obd_mod_put(mod);
891 }
892 *request = req;
893 mdc_close_handle_reply(req, op_data, rc);
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800894 return rc < 0 ? rc : saved_rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800895}
896
Luca Ceresolif6219c12015-01-18 15:06:47 +0100897static int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
898 struct md_open_data *mod)
Peng Taod7e09d02013-05-02 16:46:55 +0800899{
900 struct obd_device *obd = class_exp2obd(exp);
901 struct ptlrpc_request *req;
902 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800903
904 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
905 &RQF_MDS_DONE_WRITING);
906 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800907 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800908
Peng Taod7e09d02013-05-02 16:46:55 +0800909 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
910 if (rc) {
911 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800912 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800913 }
914
915 if (mod != NULL) {
916 LASSERTF(mod->mod_open_req != NULL &&
917 mod->mod_open_req->rq_type != LI_POISON,
918 "POISONED setattr %p!\n", mod->mod_open_req);
919
920 mod->mod_close_req = req;
921 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
922 /* We no longer want to preserve this setattr for replay even
923 * though the open was committed. b=3632, b=3633 */
924 spin_lock(&mod->mod_open_req->rq_lock);
925 mod->mod_open_req->rq_replay = 0;
926 spin_unlock(&mod->mod_open_req->rq_lock);
927 }
928
929 mdc_close_pack(req, op_data);
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 (rc == -ESTALE) {
937 /**
938 * it can be allowed error after 3633 if open or setattr were
939 * committed and server failed before close was sent.
940 * Let's check if mod exists and return no error in that case
941 */
942 if (mod) {
943 LASSERT(mod->mod_open_req != NULL);
944 if (mod->mod_open_req->rq_committed)
945 rc = 0;
946 }
947 }
948
949 if (mod) {
950 if (rc != 0)
951 mod->mod_close_req = NULL;
Hongchao Zhang63d42572014-02-28 21:16:37 -0500952 LASSERT(mod->mod_open_req != NULL);
953 mdc_free_open(mod);
954
Peng Taod7e09d02013-05-02 16:46:55 +0800955 /* Since now, mod is accessed through setattr req only,
956 * thus DW req does not keep a reference on mod anymore. */
957 obd_mod_put(mod);
958 }
959
960 mdc_close_handle_reply(req, op_data, rc);
961 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800962 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800963}
964
Luca Ceresolif6219c12015-01-18 15:06:47 +0100965static int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
966 struct page **pages, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800967{
968 struct ptlrpc_request *req;
969 struct ptlrpc_bulk_desc *desc;
970 int i;
971 wait_queue_head_t waitq;
972 int resends = 0;
973 struct l_wait_info lwi;
974 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800975
976 *request = NULL;
977 init_waitqueue_head(&waitq);
978
979restart_bulk:
980 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
981 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800982 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800983
Peng Taod7e09d02013-05-02 16:46:55 +0800984 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
985 if (rc) {
986 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800987 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800988 }
989
990 req->rq_request_portal = MDS_READPAGE_PORTAL;
991 ptlrpc_at_set_req_timeout(req);
992
993 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
994 MDS_BULK_PORTAL);
995 if (desc == NULL) {
996 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800997 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800998 }
999
1000 /* NB req now owns desc and will free it when it gets freed */
1001 for (i = 0; i < op_data->op_npages; i++)
1002 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1003
1004 mdc_readdir_pack(req, op_data->op_offset,
1005 PAGE_CACHE_SIZE * op_data->op_npages,
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001006 &op_data->op_fid1);
Peng Taod7e09d02013-05-02 16:46:55 +08001007
1008 ptlrpc_request_set_replen(req);
1009 rc = ptlrpc_queue_wait(req);
1010 if (rc) {
1011 ptlrpc_req_finished(req);
1012 if (rc != -ETIMEDOUT)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001013 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001014
1015 resends++;
1016 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1017 CERROR("too many resend retries, returning error\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001018 return -EIO;
Peng Taod7e09d02013-05-02 16:46:55 +08001019 }
Srikrishan Malike5e663a2014-08-11 23:57:30 +05301020 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends),
1021 NULL, NULL, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +08001022 l_wait_event(waitq, 0, &lwi);
1023
1024 goto restart_bulk;
1025 }
1026
1027 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1028 req->rq_bulk->bd_nob_transferred);
1029 if (rc < 0) {
1030 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001031 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001032 }
1033
1034 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1035 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1036 req->rq_bulk->bd_nob_transferred,
1037 PAGE_CACHE_SIZE * op_data->op_npages);
1038 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001039 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08001040 }
1041
1042 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001043 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001044}
1045
1046static int mdc_statfs(const struct lu_env *env,
1047 struct obd_export *exp, struct obd_statfs *osfs,
1048 __u64 max_age, __u32 flags)
1049{
1050 struct obd_device *obd = class_exp2obd(exp);
1051 struct ptlrpc_request *req;
1052 struct obd_statfs *msfs;
1053 struct obd_import *imp = NULL;
1054 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001055
1056 /*
1057 * Since the request might also come from lprocfs, so we need
1058 * sync this with client_disconnect_export Bug15684
1059 */
1060 down_read(&obd->u.cli.cl_sem);
1061 if (obd->u.cli.cl_import)
1062 imp = class_import_get(obd->u.cli.cl_import);
1063 up_read(&obd->u.cli.cl_sem);
1064 if (!imp)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001065 return -ENODEV;
Peng Taod7e09d02013-05-02 16:46:55 +08001066
1067 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1068 LUSTRE_MDS_VERSION, MDS_STATFS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001069 if (req == NULL) {
1070 rc = -ENOMEM;
1071 goto output;
1072 }
Peng Taod7e09d02013-05-02 16:46:55 +08001073
1074 ptlrpc_request_set_replen(req);
1075
1076 if (flags & OBD_STATFS_NODELAY) {
1077 /* procfs requests not want stay in wait for avoid deadlock */
1078 req->rq_no_resend = 1;
1079 req->rq_no_delay = 1;
1080 }
1081
1082 rc = ptlrpc_queue_wait(req);
1083 if (rc) {
1084 /* check connection error first */
1085 if (imp->imp_connect_error)
1086 rc = imp->imp_connect_error;
Julia Lawalld5fdc202014-08-28 12:10:35 +02001087 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001088 }
1089
1090 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001091 if (msfs == NULL) {
1092 rc = -EPROTO;
1093 goto out;
1094 }
Peng Taod7e09d02013-05-02 16:46:55 +08001095
1096 *osfs = *msfs;
Peng Taod7e09d02013-05-02 16:46:55 +08001097out:
1098 ptlrpc_req_finished(req);
1099output:
1100 class_import_put(imp);
1101 return rc;
1102}
1103
1104static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1105{
1106 __u32 keylen, vallen;
1107 void *key;
1108 int rc;
1109
1110 if (gf->gf_pathlen > PATH_MAX)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001111 return -ENAMETOOLONG;
Peng Taod7e09d02013-05-02 16:46:55 +08001112 if (gf->gf_pathlen < 2)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001113 return -EOVERFLOW;
Peng Taod7e09d02013-05-02 16:46:55 +08001114
1115 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1116 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
Julia Lawall7b817792015-05-01 17:51:17 +02001117 key = kzalloc(keylen, GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02001118 if (!key)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001119 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001120 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1121 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1122
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001123 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001124 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1125
Julia Lawalld5fdc202014-08-28 12:10:35 +02001126 if (!fid_is_sane(&gf->gf_fid)) {
1127 rc = -EINVAL;
1128 goto out;
1129 }
Peng Taod7e09d02013-05-02 16:46:55 +08001130
1131 /* Val is struct getinfo_fid2path result plus path */
1132 vallen = sizeof(*gf) + gf->gf_pathlen;
1133
1134 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1135 if (rc != 0 && rc != -EREMOTE)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001136 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001137
Julia Lawalld5fdc202014-08-28 12:10:35 +02001138 if (vallen <= sizeof(*gf)) {
1139 rc = -EPROTO;
1140 goto out;
1141 } else if (vallen > sizeof(*gf) + gf->gf_pathlen) {
1142 rc = -EOVERFLOW;
1143 goto out;
1144 }
Peng Taod7e09d02013-05-02 16:46:55 +08001145
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001146 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n%s\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001147 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1148
1149out:
Julia Lawall7b817792015-05-01 17:51:17 +02001150 kfree(key);
Peng Taod7e09d02013-05-02 16:46:55 +08001151 return rc;
1152}
1153
1154static int mdc_ioc_hsm_progress(struct obd_export *exp,
1155 struct hsm_progress_kernel *hpk)
1156{
1157 struct obd_import *imp = class_exp2cliimp(exp);
1158 struct hsm_progress_kernel *req_hpk;
1159 struct ptlrpc_request *req;
1160 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001161
1162 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1163 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001164 if (req == NULL) {
1165 rc = -ENOMEM;
1166 goto out;
1167 }
Peng Taod7e09d02013-05-02 16:46:55 +08001168
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001169 mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001170
1171 /* Copy hsm_progress struct */
1172 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001173 if (req_hpk == NULL) {
1174 rc = -EPROTO;
1175 goto out;
1176 }
Peng Taod7e09d02013-05-02 16:46:55 +08001177
1178 *req_hpk = *hpk;
Li Wei2d58de72013-07-23 00:06:32 +08001179 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
Peng Taod7e09d02013-05-02 16:46:55 +08001180
1181 ptlrpc_request_set_replen(req);
1182
1183 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001184 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001185out:
1186 ptlrpc_req_finished(req);
1187 return rc;
1188}
1189
1190static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1191{
1192 __u32 *archive_mask;
1193 struct ptlrpc_request *req;
1194 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001195
1196 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1197 LUSTRE_MDS_VERSION,
1198 MDS_HSM_CT_REGISTER);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001199 if (req == NULL) {
1200 rc = -ENOMEM;
1201 goto out;
1202 }
Peng Taod7e09d02013-05-02 16:46:55 +08001203
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001204 mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001205
1206 /* Copy hsm_progress struct */
1207 archive_mask = req_capsule_client_get(&req->rq_pill,
1208 &RMF_MDS_HSM_ARCHIVE);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001209 if (archive_mask == NULL) {
1210 rc = -EPROTO;
1211 goto out;
1212 }
Peng Taod7e09d02013-05-02 16:46:55 +08001213
1214 *archive_mask = archives;
1215
1216 ptlrpc_request_set_replen(req);
1217
1218 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001219 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001220out:
1221 ptlrpc_req_finished(req);
1222 return rc;
1223}
1224
1225static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1226 struct md_op_data *op_data)
1227{
1228 struct hsm_current_action *hca = op_data->op_data;
1229 struct hsm_current_action *req_hca;
1230 struct ptlrpc_request *req;
1231 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001232
1233 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1234 &RQF_MDS_HSM_ACTION);
1235 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001236 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001237
Peng Taod7e09d02013-05-02 16:46:55 +08001238 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1239 if (rc) {
1240 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001241 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001242 }
1243
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001244 mdc_pack_body(req, &op_data->op_fid1, OBD_MD_FLRMTPERM, 0,
1245 op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001246
1247 ptlrpc_request_set_replen(req);
1248
1249 rc = mdc_queue_wait(req);
1250 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001251 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001252
1253 req_hca = req_capsule_server_get(&req->rq_pill,
1254 &RMF_MDS_HSM_CURRENT_ACTION);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001255 if (req_hca == NULL) {
1256 rc = -EPROTO;
1257 goto out;
1258 }
Peng Taod7e09d02013-05-02 16:46:55 +08001259
1260 *hca = *req_hca;
1261
Peng Taod7e09d02013-05-02 16:46:55 +08001262out:
1263 ptlrpc_req_finished(req);
1264 return rc;
1265}
1266
1267static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1268{
1269 struct ptlrpc_request *req;
1270 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001271
1272 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1273 LUSTRE_MDS_VERSION,
1274 MDS_HSM_CT_UNREGISTER);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001275 if (req == NULL) {
1276 rc = -ENOMEM;
1277 goto out;
1278 }
Peng Taod7e09d02013-05-02 16:46:55 +08001279
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001280 mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001281
1282 ptlrpc_request_set_replen(req);
1283
1284 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001285 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001286out:
1287 ptlrpc_req_finished(req);
1288 return rc;
1289}
1290
1291static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1292 struct md_op_data *op_data)
1293{
1294 struct hsm_user_state *hus = op_data->op_data;
1295 struct hsm_user_state *req_hus;
1296 struct ptlrpc_request *req;
1297 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001298
1299 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1300 &RQF_MDS_HSM_STATE_GET);
1301 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001302 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001303
Peng Taod7e09d02013-05-02 16:46:55 +08001304 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1305 if (rc != 0) {
1306 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001307 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001308 }
1309
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001310 mdc_pack_body(req, &op_data->op_fid1, OBD_MD_FLRMTPERM, 0,
1311 op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001312
1313 ptlrpc_request_set_replen(req);
1314
1315 rc = mdc_queue_wait(req);
1316 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001317 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001318
1319 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001320 if (req_hus == NULL) {
1321 rc = -EPROTO;
1322 goto out;
1323 }
Peng Taod7e09d02013-05-02 16:46:55 +08001324
1325 *hus = *req_hus;
1326
Peng Taod7e09d02013-05-02 16:46:55 +08001327out:
1328 ptlrpc_req_finished(req);
1329 return rc;
1330}
1331
1332static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1333 struct md_op_data *op_data)
1334{
1335 struct hsm_state_set *hss = op_data->op_data;
1336 struct hsm_state_set *req_hss;
1337 struct ptlrpc_request *req;
1338 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001339
1340 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1341 &RQF_MDS_HSM_STATE_SET);
1342 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001343 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001344
Peng Taod7e09d02013-05-02 16:46:55 +08001345 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1346 if (rc) {
1347 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001348 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001349 }
1350
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001351 mdc_pack_body(req, &op_data->op_fid1, OBD_MD_FLRMTPERM, 0,
1352 op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001353
1354 /* Copy states */
1355 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001356 if (req_hss == NULL) {
1357 rc = -EPROTO;
1358 goto out;
1359 }
Peng Taod7e09d02013-05-02 16:46:55 +08001360 *req_hss = *hss;
1361
1362 ptlrpc_request_set_replen(req);
1363
1364 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001365 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001366
Peng Taod7e09d02013-05-02 16:46:55 +08001367out:
1368 ptlrpc_req_finished(req);
1369 return rc;
1370}
1371
1372static int mdc_ioc_hsm_request(struct obd_export *exp,
1373 struct hsm_user_request *hur)
1374{
1375 struct obd_import *imp = class_exp2cliimp(exp);
1376 struct ptlrpc_request *req;
1377 struct hsm_request *req_hr;
1378 struct hsm_user_item *req_hui;
1379 char *req_opaque;
1380 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001381
1382 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001383 if (req == NULL) {
1384 rc = -ENOMEM;
1385 goto out;
1386 }
Peng Taod7e09d02013-05-02 16:46:55 +08001387
1388 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1389 hur->hur_request.hr_itemcount
1390 * sizeof(struct hsm_user_item));
1391 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1392 hur->hur_request.hr_data_len);
1393
1394 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1395 if (rc) {
1396 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001397 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001398 }
1399
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001400 mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001401
1402 /* Copy hsm_request struct */
1403 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001404 if (req_hr == NULL) {
1405 rc = -EPROTO;
1406 goto out;
1407 }
Peng Taod7e09d02013-05-02 16:46:55 +08001408 *req_hr = hur->hur_request;
1409
1410 /* Copy hsm_user_item structs */
1411 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001412 if (req_hui == NULL) {
1413 rc = -EPROTO;
1414 goto out;
1415 }
Peng Taod7e09d02013-05-02 16:46:55 +08001416 memcpy(req_hui, hur->hur_user_item,
1417 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1418
1419 /* Copy opaque field */
1420 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001421 if (req_opaque == NULL) {
1422 rc = -EPROTO;
1423 goto out;
1424 }
Peng Taod7e09d02013-05-02 16:46:55 +08001425 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1426
1427 ptlrpc_request_set_replen(req);
1428
1429 rc = mdc_queue_wait(req);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001430 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001431
1432out:
1433 ptlrpc_req_finished(req);
1434 return rc;
1435}
1436
1437static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1438{
1439 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1440
Oleg Drokin18e042f2014-01-23 23:45:07 -05001441 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
Peng Taod7e09d02013-05-02 16:46:55 +08001442
1443 lh->kuc_magic = KUC_MAGIC;
1444 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1445 lh->kuc_flags = flags;
1446 lh->kuc_msgtype = CL_RECORD;
1447 lh->kuc_msglen = len;
1448 return lh;
1449}
1450
1451#define D_CHANGELOG 0
1452
1453struct changelog_show {
1454 __u64 cs_startrec;
1455 __u32 cs_flags;
1456 struct file *cs_fp;
1457 char *cs_buf;
1458 struct obd_device *cs_obd;
1459};
1460
Andreas Dilgere3779882013-06-03 21:40:52 +08001461static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
Peng Taod7e09d02013-05-02 16:46:55 +08001462 struct llog_rec_hdr *hdr, void *data)
1463{
1464 struct changelog_show *cs = data;
1465 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1466 struct kuc_hdr *lh;
1467 int len, rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001468
Andreas Dilgere3779882013-06-03 21:40:52 +08001469 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1470 rc = -EINVAL;
1471 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1472 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1473 rec->cr.cr_type, rc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001474 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001475 }
1476
1477 if (rec->cr.cr_index < cs->cs_startrec) {
1478 /* Skip entries earlier than what we are interested in */
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001479 CDEBUG(D_CHANGELOG, "rec=%llu start=%llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001480 rec->cr.cr_index, cs->cs_startrec);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001481 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001482 }
1483
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001484 CDEBUG(D_CHANGELOG, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID
Peng Taod7e09d02013-05-02 16:46:55 +08001485 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1486 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1487 rec->cr.cr_flags & CLF_FLAGMASK,
1488 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1489 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1490
1491 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1492
1493 /* Set up the message */
1494 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1495 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1496
1497 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
Srikrishan Malik301af902014-08-11 23:57:31 +05301498 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len, rc);
Peng Taod7e09d02013-05-02 16:46:55 +08001499
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001500 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001501}
1502
1503static int mdc_changelog_send_thread(void *csdata)
1504{
1505 struct changelog_show *cs = csdata;
1506 struct llog_ctxt *ctxt = NULL;
1507 struct llog_handle *llh = NULL;
1508 struct kuc_hdr *kuch;
1509 int rc;
1510
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001511 CDEBUG(D_CHANGELOG, "changelog to fp=%p start %llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001512 cs->cs_fp, cs->cs_startrec);
1513
Julia Lawall7b817792015-05-01 17:51:17 +02001514 cs->cs_buf = kzalloc(KUC_CHANGELOG_MSG_MAXSIZE, GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02001515 if (!cs->cs_buf) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001516 rc = -ENOMEM;
1517 goto out;
1518 }
Peng Taod7e09d02013-05-02 16:46:55 +08001519
1520 /* Set up the remote catalog handle */
1521 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001522 if (ctxt == NULL) {
1523 rc = -ENOENT;
1524 goto out;
1525 }
Peng Taod7e09d02013-05-02 16:46:55 +08001526 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1527 LLOG_OPEN_EXISTS);
1528 if (rc) {
1529 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1530 cs->cs_obd->obd_name, rc);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001531 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001532 }
1533 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1534 if (rc) {
1535 CERROR("llog_init_handle failed %d\n", rc);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001536 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001537 }
1538
Andreas Dilgere3779882013-06-03 21:40:52 +08001539 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001540
1541 /* Send EOF no matter what our result */
Chi Pham4a87df32014-03-09 12:51:19 +01001542 kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
1543 if (kuch) {
Peng Taod7e09d02013-05-02 16:46:55 +08001544 kuch->kuc_msgtype = CL_EOF;
1545 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1546 }
1547
1548out:
1549 fput(cs->cs_fp);
1550 if (llh)
1551 llog_cat_close(NULL, llh);
1552 if (ctxt)
1553 llog_ctxt_put(ctxt);
Julia Lawall6fa7cbab32015-05-01 21:37:56 +02001554 kfree(cs->cs_buf);
Julia Lawall7b817792015-05-01 17:51:17 +02001555 kfree(cs);
Peng Taod7e09d02013-05-02 16:46:55 +08001556 return rc;
1557}
1558
1559static int mdc_ioc_changelog_send(struct obd_device *obd,
1560 struct ioc_changelog *icc)
1561{
1562 struct changelog_show *cs;
1563 int rc;
1564
1565 /* Freed in mdc_changelog_send_thread */
Julia Lawall7b817792015-05-01 17:51:17 +02001566 cs = kzalloc(sizeof(*cs), GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +08001567 if (!cs)
1568 return -ENOMEM;
1569
1570 cs->cs_obd = obd;
1571 cs->cs_startrec = icc->icc_recno;
1572 /* matching fput in mdc_changelog_send_thread */
1573 cs->cs_fp = fget(icc->icc_id);
1574 cs->cs_flags = icc->icc_flags;
1575
1576 /*
1577 * New thread because we should return to user app before
1578 * writing into our pipe
1579 */
1580 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1581 "mdc_clg_send_thread"));
1582 if (!IS_ERR_VALUE(rc)) {
1583 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1584 return 0;
1585 }
1586
1587 CERROR("Failed to start changelog thread: %d\n", rc);
Julia Lawall7b817792015-05-01 17:51:17 +02001588 kfree(cs);
Peng Taod7e09d02013-05-02 16:46:55 +08001589 return rc;
1590}
1591
1592static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1593 struct lustre_kernelcomm *lk);
1594
1595static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1596 struct obd_quotactl *oqctl)
1597{
1598 struct client_obd *cli = &exp->exp_obd->u.cli;
1599 struct ptlrpc_request *req;
1600 struct obd_quotactl *body;
1601 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001602
1603 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1604 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1605 MDS_QUOTACHECK);
1606 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001607 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001608
1609 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1610 *body = *oqctl;
1611
1612 ptlrpc_request_set_replen(req);
1613
1614 /* the next poll will find -ENODATA, that means quotacheck is
1615 * going on */
1616 cli->cl_qchk_stat = -ENODATA;
1617 rc = ptlrpc_queue_wait(req);
1618 if (rc)
1619 cli->cl_qchk_stat = rc;
1620 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001621 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001622}
1623
1624static int mdc_quota_poll_check(struct obd_export *exp,
1625 struct if_quotacheck *qchk)
1626{
1627 struct client_obd *cli = &exp->exp_obd->u.cli;
1628 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001629
1630 qchk->obd_uuid = cli->cl_target_uuid;
1631 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1632
1633 rc = cli->cl_qchk_stat;
1634 /* the client is not the previous one */
1635 if (rc == CL_NOT_QUOTACHECKED)
1636 rc = -EINTR;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001637 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001638}
1639
1640static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1641 struct obd_quotactl *oqctl)
1642{
1643 struct ptlrpc_request *req;
1644 struct obd_quotactl *oqc;
1645 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001646
1647 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1648 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1649 MDS_QUOTACTL);
1650 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001651 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001652
1653 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1654 *oqc = *oqctl;
1655
1656 ptlrpc_request_set_replen(req);
1657 ptlrpc_at_set_req_timeout(req);
1658 req->rq_no_resend = 1;
1659
1660 rc = ptlrpc_queue_wait(req);
1661 if (rc)
1662 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1663
Chi Pham4a87df32014-03-09 12:51:19 +01001664 if (req->rq_repmsg) {
1665 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1666 if (oqc) {
1667 *oqctl = *oqc;
1668 } else if (!rc) {
Srikrishan Malikffdac6ce2014-08-11 23:57:27 +05301669 CERROR("Can't unpack obd_quotactl\n");
Chi Pham4a87df32014-03-09 12:51:19 +01001670 rc = -EPROTO;
1671 }
Peng Taod7e09d02013-05-02 16:46:55 +08001672 } else if (!rc) {
Chi Pham4a87df32014-03-09 12:51:19 +01001673 CERROR("Can't unpack obd_quotactl\n");
Peng Taod7e09d02013-05-02 16:46:55 +08001674 rc = -EPROTO;
1675 }
1676 ptlrpc_req_finished(req);
1677
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001678 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001679}
1680
1681static int mdc_ioc_swap_layouts(struct obd_export *exp,
1682 struct md_op_data *op_data)
1683{
1684 LIST_HEAD(cancels);
1685 struct ptlrpc_request *req;
1686 int rc, count;
1687 struct mdc_swap_layouts *msl, *payload;
Peng Taod7e09d02013-05-02 16:46:55 +08001688
1689 msl = op_data->op_data;
1690
1691 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1692 * first thing it will do is to cancel the 2 layout
1693 * locks hold by this client.
1694 * So the client must cancel its layout locks on the 2 fids
1695 * with the request RPC to avoid extra RPC round trips
1696 */
1697 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1698 LCK_CR, MDS_INODELOCK_LAYOUT);
1699 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1700 LCK_CR, MDS_INODELOCK_LAYOUT);
1701
1702 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1703 &RQF_MDS_SWAP_LAYOUTS);
1704 if (req == NULL) {
1705 ldlm_lock_list_put(&cancels, l_bl_ast, count);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001706 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001707 }
1708
Peng Taod7e09d02013-05-02 16:46:55 +08001709 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1710 if (rc) {
1711 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001712 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001713 }
1714
1715 mdc_swap_layouts_pack(req, op_data);
1716
1717 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1718 LASSERT(payload);
1719
1720 *payload = *msl;
1721
1722 ptlrpc_request_set_replen(req);
1723
1724 rc = ptlrpc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08001725
Peng Taod7e09d02013-05-02 16:46:55 +08001726 ptlrpc_req_finished(req);
1727 return rc;
1728}
1729
1730static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1731 void *karg, void *uarg)
1732{
1733 struct obd_device *obd = exp->exp_obd;
1734 struct obd_ioctl_data *data = karg;
1735 struct obd_import *imp = obd->u.cli.cl_import;
Peng Taod7e09d02013-05-02 16:46:55 +08001736 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001737
1738 if (!try_module_get(THIS_MODULE)) {
1739 CERROR("Can't get module. Is it alive?");
1740 return -EINVAL;
1741 }
1742 switch (cmd) {
1743 case OBD_IOC_CHANGELOG_SEND:
1744 rc = mdc_ioc_changelog_send(obd, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001745 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001746 case OBD_IOC_CHANGELOG_CLEAR: {
1747 struct ioc_changelog *icc = karg;
Srikrishan Malik1a4cd3e2014-08-11 23:57:36 +05301748 struct changelog_setinfo cs = {
1749 .cs_recno = icc->icc_recno,
1750 .cs_id = icc->icc_id
1751 };
1752
Peng Taod7e09d02013-05-02 16:46:55 +08001753 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1754 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1755 NULL);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001756 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001757 }
1758 case OBD_IOC_FID2PATH:
1759 rc = mdc_ioc_fid2path(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001760 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001761 case LL_IOC_HSM_CT_START:
1762 rc = mdc_ioc_hsm_ct_start(exp, karg);
Thomas Leibovici78eb90922013-07-25 01:17:24 +08001763 /* ignore if it was already registered on this MDS. */
1764 if (rc == -EEXIST)
1765 rc = 0;
Julia Lawalld5fdc202014-08-28 12:10:35 +02001766 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001767 case LL_IOC_HSM_PROGRESS:
1768 rc = mdc_ioc_hsm_progress(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001769 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001770 case LL_IOC_HSM_STATE_GET:
1771 rc = mdc_ioc_hsm_state_get(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001772 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001773 case LL_IOC_HSM_STATE_SET:
1774 rc = mdc_ioc_hsm_state_set(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001775 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001776 case LL_IOC_HSM_ACTION:
1777 rc = mdc_ioc_hsm_current_action(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001778 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001779 case LL_IOC_HSM_REQUEST:
1780 rc = mdc_ioc_hsm_request(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001781 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001782 case OBD_IOC_CLIENT_RECOVER:
1783 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1784 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001785 goto out;
1786 rc = 0;
1787 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001788 case IOC_OSC_SET_ACTIVE:
1789 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001790 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001791 case OBD_IOC_POLL_QUOTACHECK:
1792 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001793 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001794 case OBD_IOC_PING_TARGET:
1795 rc = ptlrpc_obd_ping(obd);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001796 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001797 /*
1798 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1799 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1800 * there'd be no LMV layer thus we might be called here. Eventually
1801 * this code should be removed.
1802 * bz20731, LU-592.
1803 */
1804 case IOC_OBD_STATFS: {
1805 struct obd_statfs stat_buf = {0};
1806
Julia Lawalld5fdc202014-08-28 12:10:35 +02001807 if (*((__u32 *) data->ioc_inlbuf2) != 0) {
1808 rc = -ENODEV;
1809 goto out;
1810 }
Peng Taod7e09d02013-05-02 16:46:55 +08001811
1812 /* copy UUID */
1813 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
Dan Carpenteref4356bf2015-01-06 12:57:08 +03001814 min_t(size_t, data->ioc_plen2,
1815 sizeof(struct obd_uuid)))) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001816 rc = -EFAULT;
1817 goto out;
1818 }
Peng Taod7e09d02013-05-02 16:46:55 +08001819
1820 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1821 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1822 0);
1823 if (rc != 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001824 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001825
1826 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
Dan Carpenteref4356bf2015-01-06 12:57:08 +03001827 min_t(size_t, data->ioc_plen1,
1828 sizeof(stat_buf)))) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001829 rc = -EFAULT;
1830 goto out;
1831 }
Peng Taod7e09d02013-05-02 16:46:55 +08001832
Julia Lawalld5fdc202014-08-28 12:10:35 +02001833 rc = 0;
1834 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001835 }
1836 case OBD_IOC_QUOTACTL: {
1837 struct if_quotactl *qctl = karg;
1838 struct obd_quotactl *oqctl;
1839
Julia Lawall7b817792015-05-01 17:51:17 +02001840 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02001841 if (!oqctl) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001842 rc = -ENOMEM;
1843 goto out;
1844 }
Peng Taod7e09d02013-05-02 16:46:55 +08001845
1846 QCTL_COPY(oqctl, qctl);
1847 rc = obd_quotactl(exp, oqctl);
1848 if (rc == 0) {
1849 QCTL_COPY(qctl, oqctl);
1850 qctl->qc_valid = QC_MDTIDX;
1851 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1852 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08001853
Julia Lawall7b817792015-05-01 17:51:17 +02001854 kfree(oqctl);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001855 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001856 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08001857 case LL_IOC_GET_CONNECT_FLAGS:
1858 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
Julia Lawalld5fdc202014-08-28 12:10:35 +02001859 sizeof(*exp_connect_flags_ptr(exp)))) {
1860 rc = -EFAULT;
1861 goto out;
1862 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08001863
Julia Lawalld5fdc202014-08-28 12:10:35 +02001864 rc = 0;
1865 goto out;
John L. Hammondc1f3d682013-11-26 10:04:59 +08001866 case LL_IOC_LOV_SWAP_LAYOUTS:
Peng Taod7e09d02013-05-02 16:46:55 +08001867 rc = mdc_ioc_swap_layouts(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001868 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001869 default:
John L. Hammondc1f3d682013-11-26 10:04:59 +08001870 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
Julia Lawalld5fdc202014-08-28 12:10:35 +02001871 rc = -ENOTTY;
1872 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001873 }
1874out:
1875 module_put(THIS_MODULE);
1876
1877 return rc;
1878}
1879
Luca Ceresolif6219c12015-01-18 15:06:47 +01001880static int mdc_get_info_rpc(struct obd_export *exp,
1881 u32 keylen, void *key,
1882 int vallen, void *val)
Peng Taod7e09d02013-05-02 16:46:55 +08001883{
1884 struct obd_import *imp = class_exp2cliimp(exp);
1885 struct ptlrpc_request *req;
1886 char *tmp;
1887 int rc = -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08001888
1889 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1890 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001891 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001892
1893 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1894 RCL_CLIENT, keylen);
1895 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1896 RCL_CLIENT, sizeof(__u32));
1897
1898 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1899 if (rc) {
1900 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001901 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001902 }
1903
1904 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1905 memcpy(tmp, key, keylen);
1906 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1907 memcpy(tmp, &vallen, sizeof(__u32));
1908
1909 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1910 RCL_SERVER, vallen);
1911 ptlrpc_request_set_replen(req);
1912
1913 rc = ptlrpc_queue_wait(req);
1914 /* -EREMOTE means the get_info result is partial, and it needs to
1915 * continue on another MDT, see fid2path part in lmv_iocontrol */
1916 if (rc == 0 || rc == -EREMOTE) {
1917 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1918 memcpy(val, tmp, vallen);
1919 if (ptlrpc_rep_need_swab(req)) {
1920 if (KEY_IS(KEY_FID2PATH))
1921 lustre_swab_fid2path(val);
1922 }
1923 }
1924 ptlrpc_req_finished(req);
1925
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001926 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001927}
1928
1929static void lustre_swab_hai(struct hsm_action_item *h)
1930{
1931 __swab32s(&h->hai_len);
1932 __swab32s(&h->hai_action);
1933 lustre_swab_lu_fid(&h->hai_fid);
1934 lustre_swab_lu_fid(&h->hai_dfid);
1935 __swab64s(&h->hai_cookie);
1936 __swab64s(&h->hai_extent.offset);
1937 __swab64s(&h->hai_extent.length);
1938 __swab64s(&h->hai_gid);
1939}
1940
1941static void lustre_swab_hal(struct hsm_action_list *h)
1942{
1943 struct hsm_action_item *hai;
1944 int i;
1945
1946 __swab32s(&h->hal_version);
1947 __swab32s(&h->hal_count);
1948 __swab32s(&h->hal_archive_id);
1949 __swab64s(&h->hal_flags);
1950 hai = hai_zero(h);
JC Lafoucriere18dfaeb2013-11-26 10:05:01 +08001951 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
Peng Taod7e09d02013-05-02 16:46:55 +08001952 lustre_swab_hai(hai);
Peng Taod7e09d02013-05-02 16:46:55 +08001953}
1954
1955static void lustre_swab_kuch(struct kuc_hdr *l)
1956{
1957 __swab16s(&l->kuc_magic);
1958 /* __u8 l->kuc_transport */
1959 __swab16s(&l->kuc_msgtype);
1960 __swab16s(&l->kuc_msglen);
1961}
1962
1963static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1964 struct lustre_kernelcomm *lk)
1965{
1966 struct obd_import *imp = class_exp2cliimp(exp);
1967 __u32 archive = lk->lk_data;
1968 int rc = 0;
1969
1970 if (lk->lk_group != KUC_GRP_HSM) {
1971 CERROR("Bad copytool group %d\n", lk->lk_group);
1972 return -EINVAL;
1973 }
1974
1975 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1976 lk->lk_uid, lk->lk_group, lk->lk_flags);
1977
1978 if (lk->lk_flags & LK_FLG_STOP) {
Peng Taod7e09d02013-05-02 16:46:55 +08001979 /* Unregister with the coordinator */
Thomas Leibovici78eb90922013-07-25 01:17:24 +08001980 rc = mdc_ioc_hsm_ct_unregister(imp);
Peng Taod7e09d02013-05-02 16:46:55 +08001981 } else {
Thomas Leibovici78eb90922013-07-25 01:17:24 +08001982 rc = mdc_ioc_hsm_ct_register(imp, archive);
Peng Taod7e09d02013-05-02 16:46:55 +08001983 }
1984
1985 return rc;
1986}
1987
1988/**
1989 * Send a message to any listening copytools
1990 * @param val KUC message (kuc_hdr + hsm_action_list)
1991 * @param len total length of message
1992 */
1993static int mdc_hsm_copytool_send(int len, void *val)
1994{
1995 struct kuc_hdr *lh = (struct kuc_hdr *)val;
1996 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
Peng Taod7e09d02013-05-02 16:46:55 +08001997
1998 if (len < sizeof(*lh) + sizeof(*hal)) {
1999 CERROR("Short HSM message %d < %d\n", len,
2000 (int) (sizeof(*lh) + sizeof(*hal)));
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002001 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08002002 }
2003 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2004 lustre_swab_kuch(lh);
2005 lustre_swab_hal(hal);
2006 } else if (lh->kuc_magic != KUC_MAGIC) {
2007 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002008 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08002009 }
2010
Srikrishan Malikee990b32014-08-13 19:31:16 +05302011 CDEBUG(D_HSM,
2012 "Received message mg=%x t=%d m=%d l=%d actions=%d on %s\n",
Peng Taod7e09d02013-05-02 16:46:55 +08002013 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2014 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2015
2016 /* Broadcast to HSM listeners */
Prasanna Karthikda2a7272015-06-16 13:42:02 +00002017 return libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
Peng Taod7e09d02013-05-02 16:46:55 +08002018}
2019
2020/**
2021 * callback function passed to kuc for re-registering each HSM copytool
2022 * running on MDC, after MDT shutdown/recovery.
2023 * @param data archive id served by the copytool
2024 * @param cb_arg callback argument (obd_import)
2025 */
2026static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2027{
2028 struct obd_import *imp = (struct obd_import *)cb_arg;
2029 __u32 archive = data;
2030 int rc;
2031
2032 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2033 archive);
2034 rc = mdc_ioc_hsm_ct_register(imp, archive);
2035
2036 /* ignore error if the copytool is already registered */
2037 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2038}
2039
Luca Ceresolif6219c12015-01-18 15:06:47 +01002040static int mdc_set_info_async(const struct lu_env *env,
2041 struct obd_export *exp,
2042 u32 keylen, void *key,
2043 u32 vallen, void *val,
2044 struct ptlrpc_request_set *set)
Peng Taod7e09d02013-05-02 16:46:55 +08002045{
2046 struct obd_import *imp = class_exp2cliimp(exp);
2047 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002048
2049 if (KEY_IS(KEY_READ_ONLY)) {
2050 if (vallen != sizeof(int))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002051 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002052
2053 spin_lock(&imp->imp_lock);
2054 if (*((int *)val)) {
2055 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2056 imp->imp_connect_data.ocd_connect_flags |=
2057 OBD_CONNECT_RDONLY;
2058 } else {
2059 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2060 imp->imp_connect_data.ocd_connect_flags &=
2061 ~OBD_CONNECT_RDONLY;
2062 }
2063 spin_unlock(&imp->imp_lock);
2064
2065 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2066 keylen, key, vallen, val, set);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002067 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002068 }
2069 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2070 sptlrpc_conf_client_adapt(exp->exp_obd);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002071 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002072 }
2073 if (KEY_IS(KEY_FLUSH_CTX)) {
2074 sptlrpc_import_flush_my_ctx(imp);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002075 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002076 }
Peng Taod7e09d02013-05-02 16:46:55 +08002077 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2078 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2079 keylen, key, vallen, val, set);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002080 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002081 }
2082 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2083 rc = mdc_hsm_copytool_send(vallen, val);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002084 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002085 }
2086
2087 CERROR("Unknown key %s\n", (char *)key);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002088 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002089}
2090
Luca Ceresolif6219c12015-01-18 15:06:47 +01002091static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2092 __u32 keylen, void *key, __u32 *vallen, void *val,
2093 struct lov_stripe_md *lsm)
Peng Taod7e09d02013-05-02 16:46:55 +08002094{
2095 int rc = -EINVAL;
2096
2097 if (KEY_IS(KEY_MAX_EASIZE)) {
2098 int mdsize, *max_easize;
2099
2100 if (*vallen != sizeof(int))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002101 return -EINVAL;
Brian Behlendorf44779342014-04-27 13:06:47 -04002102 mdsize = *(int *)val;
Peng Taod7e09d02013-05-02 16:46:55 +08002103 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2104 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2105 max_easize = val;
2106 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002107 return 0;
Brian Behlendorf44779342014-04-27 13:06:47 -04002108 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2109 int *default_easize;
2110
2111 if (*vallen != sizeof(int))
2112 return -EINVAL;
2113 default_easize = val;
2114 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2115 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002116 } else if (KEY_IS(KEY_CONN_DATA)) {
2117 struct obd_import *imp = class_exp2cliimp(exp);
2118 struct obd_connect_data *data = val;
2119
2120 if (*vallen != sizeof(*data))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002121 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002122
2123 *data = imp->imp_connect_data;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002124 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002125 } else if (KEY_IS(KEY_TGT_COUNT)) {
2126 *((int *)val) = 1;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002127 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002128 }
2129
2130 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2131
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002132 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002133}
2134
Luca Ceresolif6219c12015-01-18 15:06:47 +01002135static int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -04002136 struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +08002137{
2138 struct ptlrpc_request *req;
2139 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002140
2141 *request = NULL;
2142 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2143 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002144 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002145
Peng Taod7e09d02013-05-02 16:46:55 +08002146 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2147 if (rc) {
2148 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002149 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002150 }
2151
Oleg Drokinef2e0f52015-09-27 16:45:46 -04002152 mdc_pack_body(req, fid, 0, 0, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08002153
2154 ptlrpc_request_set_replen(req);
2155
2156 rc = ptlrpc_queue_wait(req);
2157 if (rc)
2158 ptlrpc_req_finished(req);
2159 else
2160 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002161 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002162}
2163
2164static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2165 enum obd_import_event event)
2166{
2167 int rc = 0;
2168
2169 LASSERT(imp->imp_obd == obd);
2170
2171 switch (event) {
2172 case IMP_EVENT_DISCON: {
2173#if 0
2174 /* XXX Pass event up to OBDs stack. used only for FLD now */
2175 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2176#endif
2177 break;
2178 }
2179 case IMP_EVENT_INACTIVE: {
2180 struct client_obd *cli = &obd->u.cli;
2181 /*
2182 * Flush current sequence to make client obtain new one
2183 * from server in case of disconnect/reconnect.
2184 */
2185 if (cli->cl_seq != NULL)
2186 seq_client_flush(cli->cl_seq);
2187
2188 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2189 break;
2190 }
2191 case IMP_EVENT_INVALIDATE: {
2192 struct ldlm_namespace *ns = obd->obd_namespace;
2193
2194 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2195
2196 break;
2197 }
2198 case IMP_EVENT_ACTIVE:
2199 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002200 /* redo the kuc registration after reconnecting */
Peng Taod7e09d02013-05-02 16:46:55 +08002201 if (rc == 0)
Shivani Bhardwajdd40ca42015-11-08 22:22:58 +05302202 /* re-register HSM agents */
2203 rc = libcfs_kkuc_group_foreach(KUC_GRP_HSM,
2204 mdc_hsm_ct_reregister,
2205 (void *)imp);
Peng Taod7e09d02013-05-02 16:46:55 +08002206 break;
2207 case IMP_EVENT_OCD:
2208 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2209 break;
2210 case IMP_EVENT_DEACTIVATE:
2211 case IMP_EVENT_ACTIVATE:
2212 break;
2213 default:
2214 CERROR("Unknown import event %x\n", event);
2215 LBUG();
2216 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002217 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002218}
2219
2220int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2221 struct md_op_data *op_data)
2222{
2223 struct client_obd *cli = &exp->exp_obd->u.cli;
2224 struct lu_client_seq *seq = cli->cl_seq;
Greg Kroah-Hartman29aaf492013-08-02 18:14:51 +08002225
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002226 return seq_client_alloc_fid(NULL, seq, fid);
Peng Taod7e09d02013-05-02 16:46:55 +08002227}
2228
Luca Ceresolif6219c12015-01-18 15:06:47 +01002229static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
Srikrishan Malik982ec912014-08-11 23:57:29 +05302230{
Peng Taod7e09d02013-05-02 16:46:55 +08002231 struct client_obd *cli = &exp->exp_obd->u.cli;
Srikrishan Malik7436d072014-08-11 23:57:33 +05302232
Peng Taod7e09d02013-05-02 16:46:55 +08002233 return &cli->cl_target_uuid;
2234}
2235
2236/**
2237 * Determine whether the lock can be canceled before replaying it during
2238 * recovery, non zero value will be return if the lock can be canceled,
2239 * or zero returned for not
2240 */
2241static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2242{
2243 if (lock->l_resource->lr_type != LDLM_IBITS)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002244 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002245
2246 /* FIXME: if we ever get into a situation where there are too many
2247 * opened files with open locks on a single node, then we really
2248 * should replay these open locks to reget it */
2249 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002250 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002251
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002252 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08002253}
2254
2255static int mdc_resource_inode_free(struct ldlm_resource *res)
2256{
2257 if (res->lr_lvb_inode)
2258 res->lr_lvb_inode = NULL;
2259
2260 return 0;
2261}
2262
Luca Ceresolif6219c12015-01-18 15:06:47 +01002263static struct ldlm_valblock_ops inode_lvbo = {
Emil Goode805e5172013-07-28 00:38:56 +02002264 .lvbo_free = mdc_resource_inode_free,
Peng Taod7e09d02013-05-02 16:46:55 +08002265};
2266
John L. Hammond903af112014-09-05 15:08:12 -05002267static int mdc_llog_init(struct obd_device *obd)
2268{
2269 struct obd_llog_group *olg = &obd->obd_olg;
2270 struct llog_ctxt *ctxt;
2271 int rc;
2272
2273 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2274 &llog_client_ops);
2275 if (rc)
2276 return rc;
2277
2278 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2279 llog_initiator_connect(ctxt);
2280 llog_ctxt_put(ctxt);
2281
2282 return 0;
2283}
2284
2285static void mdc_llog_finish(struct obd_device *obd)
2286{
2287 struct llog_ctxt *ctxt;
2288
2289 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2290 if (ctxt)
2291 llog_cleanup(NULL, ctxt);
2292}
2293
Peng Taod7e09d02013-05-02 16:46:55 +08002294static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2295{
2296 struct client_obd *cli = &obd->u.cli;
Radek Dostalea7893b2014-07-27 23:22:57 +02002297 struct lprocfs_static_vars lvars = { NULL };
Peng Taod7e09d02013-05-02 16:46:55 +08002298 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002299
Julia Lawall7b817792015-05-01 17:51:17 +02002300 cli->cl_rpc_lock = kzalloc(sizeof(*cli->cl_rpc_lock), GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +08002301 if (!cli->cl_rpc_lock)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002302 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002303 mdc_init_rpc_lock(cli->cl_rpc_lock);
2304
2305 ptlrpcd_addref();
2306
Julia Lawall7b817792015-05-01 17:51:17 +02002307 cli->cl_close_lock = kzalloc(sizeof(*cli->cl_close_lock), GFP_NOFS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002308 if (!cli->cl_close_lock) {
2309 rc = -ENOMEM;
2310 goto err_rpc_lock;
2311 }
Peng Taod7e09d02013-05-02 16:46:55 +08002312 mdc_init_rpc_lock(cli->cl_close_lock);
2313
2314 rc = client_obd_setup(obd, cfg);
2315 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02002316 goto err_close_lock;
Peng Taod7e09d02013-05-02 16:46:55 +08002317 lprocfs_mdc_init_vars(&lvars);
Oleg Drokin9b801302015-05-21 15:32:16 -04002318 lprocfs_obd_setup(obd, lvars.obd_vars, lvars.sysfs_vars);
Peng Taod7e09d02013-05-02 16:46:55 +08002319 sptlrpc_lprocfs_cliobd_attach(obd);
2320 ptlrpc_lprocfs_register_obd(obd);
2321
2322 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2323
2324 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2325
John L. Hammond903af112014-09-05 15:08:12 -05002326 rc = mdc_llog_init(obd);
Peng Taod7e09d02013-05-02 16:46:55 +08002327 if (rc) {
2328 mdc_cleanup(obd);
2329 CERROR("failed to setup llogging subsystems\n");
2330 }
2331
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002332 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002333
2334err_close_lock:
Julia Lawall7b817792015-05-01 17:51:17 +02002335 kfree(cli->cl_close_lock);
Peng Taod7e09d02013-05-02 16:46:55 +08002336err_rpc_lock:
Julia Lawall7b817792015-05-01 17:51:17 +02002337 kfree(cli->cl_rpc_lock);
Peng Taod7e09d02013-05-02 16:46:55 +08002338 ptlrpcd_decref();
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002339 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002340}
2341
2342/* Initialize the default and maximum LOV EA and cookie sizes. This allows
Brian Behlendorf44779342014-04-27 13:06:47 -04002343 * us to make MDS RPCs with large enough reply buffers to hold a default
2344 * sized EA and cookie without having to calculate this (via a call into the
2345 * LOV + OSCs) each time we make an RPC. The maximum size is also tracked
2346 * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2347 * a large number of stripes is possible. If a larger reply buffer is
2348 * required it will be reallocated in the ptlrpc layer due to overflow.
2349 */
Peng Taod7e09d02013-05-02 16:46:55 +08002350static int mdc_init_ea_size(struct obd_export *exp, int easize,
Brian Behlendorf44779342014-04-27 13:06:47 -04002351 int def_easize, int cookiesize, int def_cookiesize)
Peng Taod7e09d02013-05-02 16:46:55 +08002352{
2353 struct obd_device *obd = exp->exp_obd;
2354 struct client_obd *cli = &obd->u.cli;
Peng Taod7e09d02013-05-02 16:46:55 +08002355
2356 if (cli->cl_max_mds_easize < easize)
2357 cli->cl_max_mds_easize = easize;
2358
2359 if (cli->cl_default_mds_easize < def_easize)
2360 cli->cl_default_mds_easize = def_easize;
2361
2362 if (cli->cl_max_mds_cookiesize < cookiesize)
2363 cli->cl_max_mds_cookiesize = cookiesize;
2364
Brian Behlendorf44779342014-04-27 13:06:47 -04002365 if (cli->cl_default_mds_cookiesize < def_cookiesize)
2366 cli->cl_default_mds_cookiesize = def_cookiesize;
2367
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002368 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002369}
2370
2371static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2372{
Peng Taod7e09d02013-05-02 16:46:55 +08002373 switch (stage) {
2374 case OBD_CLEANUP_EARLY:
2375 break;
2376 case OBD_CLEANUP_EXPORTS:
2377 /* Failsafe, ok if racy */
2378 if (obd->obd_type->typ_refcnt <= 1)
2379 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2380
2381 obd_cleanup_client_import(obd);
2382 ptlrpc_lprocfs_unregister_obd(obd);
2383 lprocfs_obd_cleanup(obd);
2384
John L. Hammond903af112014-09-05 15:08:12 -05002385 mdc_llog_finish(obd);
Peng Taod7e09d02013-05-02 16:46:55 +08002386 break;
2387 }
Tina Johnsoneb967122014-09-14 18:36:38 +05302388 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002389}
2390
2391static int mdc_cleanup(struct obd_device *obd)
2392{
2393 struct client_obd *cli = &obd->u.cli;
2394
Julia Lawall7b817792015-05-01 17:51:17 +02002395 kfree(cli->cl_rpc_lock);
2396 kfree(cli->cl_close_lock);
Peng Taod7e09d02013-05-02 16:46:55 +08002397
2398 ptlrpcd_decref();
2399
2400 return client_obd_cleanup(obd);
2401}
2402
Oleg Drokin21aef7d2014-08-15 12:55:56 -04002403static int mdc_process_config(struct obd_device *obd, u32 len, void *buf)
Peng Taod7e09d02013-05-02 16:46:55 +08002404{
2405 struct lustre_cfg *lcfg = buf;
Radek Dostalea7893b2014-07-27 23:22:57 +02002406 struct lprocfs_static_vars lvars = { NULL };
Peng Taod7e09d02013-05-02 16:46:55 +08002407 int rc = 0;
2408
2409 lprocfs_mdc_init_vars(&lvars);
2410 switch (lcfg->lcfg_command) {
2411 default:
2412 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2413 lcfg, obd);
2414 if (rc > 0)
2415 rc = 0;
2416 break;
2417 }
Srikrishan Malikeb445202014-08-11 23:57:38 +05302418 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002419}
2420
Peng Taod7e09d02013-05-02 16:46:55 +08002421/* get remote permission for current user on fid */
Luca Ceresolif6219c12015-01-18 15:06:47 +01002422static int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -04002423 __u32 suppgid, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +08002424{
2425 struct ptlrpc_request *req;
2426 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002427
2428 LASSERT(client_is_remote(exp));
2429
2430 *request = NULL;
2431 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2432 if (req == NULL)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002433 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002434
Peng Taod7e09d02013-05-02 16:46:55 +08002435 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2436 if (rc) {
2437 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002438 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002439 }
2440
Oleg Drokinef2e0f52015-09-27 16:45:46 -04002441 mdc_pack_body(req, fid, OBD_MD_FLRMTPERM, 0, suppgid, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08002442
2443 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2444 sizeof(struct mdt_remote_perm));
2445
2446 ptlrpc_request_set_replen(req);
2447
2448 rc = ptlrpc_queue_wait(req);
2449 if (rc)
2450 ptlrpc_req_finished(req);
2451 else
2452 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002453 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002454}
2455
Luca Ceresolif6219c12015-01-18 15:06:47 +01002456static struct obd_ops mdc_obd_ops = {
Dan Carpentera13b1f32015-10-29 12:24:40 +03002457 .owner = THIS_MODULE,
2458 .setup = mdc_setup,
2459 .precleanup = mdc_precleanup,
2460 .cleanup = mdc_cleanup,
2461 .add_conn = client_import_add_conn,
2462 .del_conn = client_import_del_conn,
2463 .connect = client_connect_import,
2464 .disconnect = client_disconnect_export,
2465 .iocontrol = mdc_iocontrol,
2466 .set_info_async = mdc_set_info_async,
2467 .statfs = mdc_statfs,
2468 .fid_init = client_fid_init,
2469 .fid_fini = client_fid_fini,
2470 .fid_alloc = mdc_fid_alloc,
2471 .import_event = mdc_import_event,
2472 .get_info = mdc_get_info,
2473 .process_config = mdc_process_config,
2474 .get_uuid = mdc_get_uuid,
2475 .quotactl = mdc_quotactl,
2476 .quotacheck = mdc_quotacheck
Peng Taod7e09d02013-05-02 16:46:55 +08002477};
2478
Luca Ceresolif6219c12015-01-18 15:06:47 +01002479static struct md_ops mdc_md_ops = {
Dan Carpenterdf18a802015-10-29 12:26:36 +03002480 .getstatus = mdc_getstatus,
2481 .null_inode = mdc_null_inode,
2482 .find_cbdata = mdc_find_cbdata,
2483 .close = mdc_close,
2484 .create = mdc_create,
2485 .done_writing = mdc_done_writing,
2486 .enqueue = mdc_enqueue,
2487 .getattr = mdc_getattr,
2488 .getattr_name = mdc_getattr_name,
2489 .intent_lock = mdc_intent_lock,
2490 .link = mdc_link,
2491 .is_subdir = mdc_is_subdir,
2492 .rename = mdc_rename,
2493 .setattr = mdc_setattr,
2494 .setxattr = mdc_setxattr,
2495 .getxattr = mdc_getxattr,
2496 .sync = mdc_sync,
2497 .readpage = mdc_readpage,
2498 .unlink = mdc_unlink,
2499 .cancel_unused = mdc_cancel_unused,
2500 .init_ea_size = mdc_init_ea_size,
2501 .set_lock_data = mdc_set_lock_data,
2502 .lock_match = mdc_lock_match,
2503 .get_lustre_md = mdc_get_lustre_md,
2504 .free_lustre_md = mdc_free_lustre_md,
2505 .set_open_replay_data = mdc_set_open_replay_data,
2506 .clear_open_replay_data = mdc_clear_open_replay_data,
2507 .get_remote_perm = mdc_get_remote_perm,
2508 .intent_getattr_async = mdc_intent_getattr_async,
2509 .revalidate_lock = mdc_revalidate_lock
Peng Taod7e09d02013-05-02 16:46:55 +08002510};
2511
Luca Ceresolif6219c12015-01-18 15:06:47 +01002512static int __init mdc_init(void)
Peng Taod7e09d02013-05-02 16:46:55 +08002513{
Radek Dostalea7893b2014-07-27 23:22:57 +02002514 struct lprocfs_static_vars lvars = { NULL };
Srikrishan Malik7436d072014-08-11 23:57:33 +05302515
Peng Taod7e09d02013-05-02 16:46:55 +08002516 lprocfs_mdc_init_vars(&lvars);
2517
Oleg Drokin2962b442015-05-21 15:32:13 -04002518 return class_register_type(&mdc_obd_ops, &mdc_md_ops,
Peng Taod7e09d02013-05-02 16:46:55 +08002519 LUSTRE_MDC_NAME, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +08002520}
2521
2522static void /*__exit*/ mdc_exit(void)
2523{
2524 class_unregister_type(LUSTRE_MDC_NAME);
2525}
2526
James Simmonsa0455472015-11-04 13:40:02 -05002527MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
Peng Taod7e09d02013-05-02 16:46:55 +08002528MODULE_DESCRIPTION("Lustre Metadata Client");
2529MODULE_LICENSE("GPL");
2530
2531module_init(mdc_init);
2532module_exit(mdc_exit);