blob: 9ad855fa5e8cb08313ab5241c3e7de0bb4a00184 [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
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050026 * Copyright (c) 2011, 2015, Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080027 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 */
32
33#define DEBUG_SUBSYSTEM S_MDC
34
35# include <linux/module.h>
36# include <linux/pagemap.h>
37# include <linux/miscdevice.h>
38# include <linux/init.h>
39# include <linux/utsname.h>
40
Greg Kroah-Hartman05932302014-07-11 22:04:56 -070041#include "../include/lustre_acl.h"
John L. Hammond8877d3b2016-08-16 16:18:51 -040042#include "../include/lustre/lustre_ioctl.h"
Greg Kroah-Hartman05932302014-07-11 22:04:56 -070043#include "../include/obd_class.h"
wang di8e9dfe82016-07-21 22:43:58 -040044#include "../include/lustre_lmv.h"
Greg Kroah-Hartman05932302014-07-11 22:04:56 -070045#include "../include/lustre_fid.h"
46#include "../include/lprocfs_status.h"
47#include "../include/lustre_param.h"
48#include "../include/lustre_log.h"
frank zagoe2780472015-12-23 16:24:43 -050049#include "../include/lustre_kernelcomm.h"
Peng Taod7e09d02013-05-02 16:46:55 +080050
51#include "mdc_internal.h"
52
53#define REQUEST_MINOR 244
54
Peng Taod7e09d02013-05-02 16:46:55 +080055static int mdc_cleanup(struct obd_device *obd);
56
Peng Taod7e09d02013-05-02 16:46:55 +080057static inline int mdc_queue_wait(struct ptlrpc_request *req)
58{
59 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
60 int rc;
61
Fan Yong1d5d5ec2016-08-16 16:18:48 -040062 /* obd_get_request_slot() ensures that this client has no more
Peng Taod7e09d02013-05-02 16:46:55 +080063 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
Oleg Drokin1df232e2016-02-24 22:00:33 -050064 * against an MDT.
65 */
Fan Yong1d5d5ec2016-08-16 16:18:48 -040066 rc = obd_get_request_slot(cli);
Peng Taod7e09d02013-05-02 16:46:55 +080067 if (rc != 0)
68 return rc;
69
70 rc = ptlrpc_queue_wait(req);
Fan Yong1d5d5ec2016-08-16 16:18:48 -040071 obd_put_request_slot(cli);
Peng Taod7e09d02013-05-02 16:46:55 +080072
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);
Oleg Drokin34e3ff92016-02-16 00:46:53 -050085 if (!req)
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);
Oleg Drokin34e3ff92016-02-16 00:46:53 -050098 if (!body) {
Julia Lawalld5fdc202014-08-28 12:10:35 +020099 rc = -EPROTO;
100 goto out;
101 }
Peng Taod7e09d02013-05-02 16:46:55 +0800102
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400103 *rootfid = body->mbo_fid1;
Peng Taod7e09d02013-05-02 16:46:55 +0800104 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);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500138 if (!body)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800139 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800140
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400141 CDEBUG(D_NET, "mode: %o\n", body->mbo_mode);
Peng Taod7e09d02013-05-02 16:46:55 +0800142
wang di483eec02016-05-04 10:28:55 -0400143 mdc_update_max_ea_from_body(exp, body);
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400144 if (body->mbo_eadatasize != 0) {
Peng Taod7e09d02013-05-02 16:46:55 +0800145 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400146 body->mbo_eadatasize);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500147 if (!eadata)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800148 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800149 }
150
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800151 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800152}
153
Janet Liu60ebee32014-11-30 18:08:10 +0800154static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
Oleg Drokin22e0bc62016-02-26 01:50:06 -0500155 struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800156{
157 struct ptlrpc_request *req;
158 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800159
160 /* Single MDS without an LMV case */
161 if (op_data->op_flags & MF_GET_MDT_IDX) {
162 op_data->op_mds = 0;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800163 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800164 }
165 *request = NULL;
166 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500167 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800168 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800169
Peng Taod7e09d02013-05-02 16:46:55 +0800170 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
171 if (rc) {
172 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800173 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800174 }
175
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400176 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
177 op_data->op_mode, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +0800178
179 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
180 op_data->op_mode);
Peng Taod7e09d02013-05-02 16:46:55 +0800181 ptlrpc_request_set_replen(req);
182
183 rc = mdc_getattr_common(exp, req);
184 if (rc)
185 ptlrpc_req_finished(req);
186 else
187 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800188 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800189}
190
Janet Liu60ebee32014-11-30 18:08:10 +0800191static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
Oleg Drokin22e0bc62016-02-26 01:50:06 -0500192 struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800193{
194 struct ptlrpc_request *req;
195 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800196
197 *request = NULL;
198 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
199 &RQF_MDS_GETATTR_NAME);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500200 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800201 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800202
Peng Taod7e09d02013-05-02 16:46:55 +0800203 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
204 op_data->op_namelen + 1);
205
206 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
207 if (rc) {
208 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800209 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800210 }
211
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400212 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
213 op_data->op_mode, op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +0800214
215 if (op_data->op_name) {
216 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
Srikrishan Malik7436d072014-08-11 23:57:33 +0530217
Peng Taod7e09d02013-05-02 16:46:55 +0800218 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
219 op_data->op_namelen);
220 memcpy(name, op_data->op_name, op_data->op_namelen);
221 }
222
223 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
224 op_data->op_mode);
225 ptlrpc_request_set_replen(req);
226
227 rc = mdc_getattr_common(exp, req);
228 if (rc)
229 ptlrpc_req_finished(req);
230 else
231 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800232 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800233}
234
235static int mdc_is_subdir(struct obd_export *exp,
236 const struct lu_fid *pfid,
237 const struct lu_fid *cfid,
238 struct ptlrpc_request **request)
239{
240 struct ptlrpc_request *req;
241 int rc;
242
Peng Taod7e09d02013-05-02 16:46:55 +0800243 *request = NULL;
244 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
245 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
246 MDS_IS_SUBDIR);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500247 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800248 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800249
250 mdc_is_subdir_pack(req, pfid, cfid, 0);
251 ptlrpc_request_set_replen(req);
252
253 rc = ptlrpc_queue_wait(req);
254 if (rc && rc != -EREMOTE)
255 ptlrpc_req_finished(req);
256 else
257 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800258 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800259}
260
Srikrishan Malik301af902014-08-11 23:57:31 +0530261static int mdc_xattr_common(struct obd_export *exp,
262 const struct req_format *fmt,
Peng Taod7e09d02013-05-02 16:46:55 +0800263 const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400264 int opcode, u64 valid,
Peng Taod7e09d02013-05-02 16:46:55 +0800265 const char *xattr_name, const char *input,
266 int input_size, int output_size, int flags,
267 __u32 suppgid, struct ptlrpc_request **request)
268{
269 struct ptlrpc_request *req;
270 int xattr_namelen = 0;
271 char *tmp;
272 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800273
274 *request = NULL;
275 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500276 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800277 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800278
Peng Taod7e09d02013-05-02 16:46:55 +0800279 if (xattr_name) {
280 xattr_namelen = strlen(xattr_name) + 1;
281 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
282 xattr_namelen);
283 }
284 if (input_size) {
285 LASSERT(input);
286 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
287 input_size);
288 }
289
Andrew Perepechkoe93a3082014-02-09 02:51:48 -0500290 /* Flush local XATTR locks to get rid of a possible cancel RPC */
291 if (opcode == MDS_REINT && fid_is_sane(fid) &&
292 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
293 LIST_HEAD(cancels);
294 int count;
295
296 /* Without that packing would fail */
297 if (input_size == 0)
298 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
299 RCL_CLIENT, 0);
300
301 count = mdc_resource_get_unused(exp, fid,
302 &cancels, LCK_EX,
303 MDS_INODELOCK_XATTR);
304
305 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
306 if (rc) {
307 ptlrpc_request_free(req);
308 return rc;
309 }
310 } else {
311 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
312 if (rc) {
313 ptlrpc_request_free(req);
314 return rc;
315 }
Peng Taod7e09d02013-05-02 16:46:55 +0800316 }
317
318 if (opcode == MDS_REINT) {
319 struct mdt_rec_setxattr *rec;
320
321 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
322 sizeof(struct mdt_rec_reint));
323 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
324 rec->sx_opcode = REINT_SETXATTR;
Peng Tao4b1a25f2013-07-15 22:27:14 +0800325 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
326 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
Peng Taod7e09d02013-05-02 16:46:55 +0800327 rec->sx_cap = cfs_curproc_cap_pack();
328 rec->sx_suppgid1 = suppgid;
329 rec->sx_suppgid2 = -1;
330 rec->sx_fid = *fid;
331 rec->sx_valid = valid | OBD_MD_FLCTIME;
Arnd Bergmann14e3f922015-09-27 16:45:27 -0400332 rec->sx_time = ktime_get_real_seconds();
Peng Taod7e09d02013-05-02 16:46:55 +0800333 rec->sx_size = output_size;
334 rec->sx_flags = flags;
335
Peng Taod7e09d02013-05-02 16:46:55 +0800336 } else {
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400337 mdc_pack_body(req, fid, valid, output_size, suppgid, flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800338 }
339
340 if (xattr_name) {
341 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
342 memcpy(tmp, xattr_name, xattr_namelen);
343 }
344 if (input_size) {
345 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
346 memcpy(tmp, input, input_size);
347 }
348
349 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
350 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
351 RCL_SERVER, output_size);
352 ptlrpc_request_set_replen(req);
353
354 /* make rpc */
355 if (opcode == MDS_REINT)
356 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
357
358 rc = ptlrpc_queue_wait(req);
359
360 if (opcode == MDS_REINT)
361 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
362
363 if (rc)
364 ptlrpc_req_finished(req);
365 else
366 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800367 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800368}
369
Janet Liu60ebee32014-11-30 18:08:10 +0800370static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400371 u64 valid, const char *xattr_name,
372 const char *input, int input_size, int output_size,
373 int flags, __u32 suppgid,
374 struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800375{
376 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400377 fid, MDS_REINT, valid, xattr_name,
Peng Taod7e09d02013-05-02 16:46:55 +0800378 input, input_size, output_size, flags,
379 suppgid, request);
380}
381
Janet Liu60ebee32014-11-30 18:08:10 +0800382static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400383 u64 valid, const char *xattr_name,
384 const char *input, int input_size, int output_size,
385 int flags, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800386{
387 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400388 fid, MDS_GETXATTR, valid, xattr_name,
Peng Taod7e09d02013-05-02 16:46:55 +0800389 input, input_size, output_size, flags,
390 -1, request);
391}
392
393#ifdef CONFIG_FS_POSIX_ACL
394static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
395{
396 struct req_capsule *pill = &req->rq_pill;
397 struct mdt_body *body = md->body;
398 struct posix_acl *acl;
399 void *buf;
400 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800401
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400402 if (!body->mbo_aclsize)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800403 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800404
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400405 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->mbo_aclsize);
Peng Taod7e09d02013-05-02 16:46:55 +0800406
407 if (!buf)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800408 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800409
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400410 acl = posix_acl_from_xattr(&init_user_ns, buf, body->mbo_aclsize);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500411 if (!acl)
Christopher J. Morronef4289402015-03-25 21:53:18 -0400412 return 0;
413
Peng Taod7e09d02013-05-02 16:46:55 +0800414 if (IS_ERR(acl)) {
415 rc = PTR_ERR(acl);
416 CERROR("convert xattr to acl: %d\n", rc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800417 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800418 }
419
Eric W. Biederman0d4d7172016-06-27 16:04:06 -0500420 rc = posix_acl_valid(&init_user_ns, acl);
Peng Taod7e09d02013-05-02 16:46:55 +0800421 if (rc) {
422 CERROR("validate acl: %d\n", rc);
423 posix_acl_release(acl);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800424 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800425 }
426
427 md->posix_acl = acl;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800428 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800429}
430#else
431#define mdc_unpack_acl(req, md) 0
432#endif
433
Shraddha Barke358855b2015-10-30 20:59:03 +0530434static int mdc_get_lustre_md(struct obd_export *exp,
435 struct ptlrpc_request *req,
436 struct obd_export *dt_exp,
437 struct obd_export *md_exp,
438 struct lustre_md *md)
Peng Taod7e09d02013-05-02 16:46:55 +0800439{
440 struct req_capsule *pill = &req->rq_pill;
441 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800442
443 LASSERT(md);
444 memset(md, 0, sizeof(*md));
445
446 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
Peng Taod7e09d02013-05-02 16:46:55 +0800447
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400448 if (md->body->mbo_valid & OBD_MD_FLEASIZE) {
Peng Taod7e09d02013-05-02 16:46:55 +0800449 int lmmsize;
450 struct lov_mds_md *lmm;
451
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400452 if (!S_ISREG(md->body->mbo_mode)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530453 CDEBUG(D_INFO,
454 "OBD_MD_FLEASIZE set, should be a regular file, but is not\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200455 rc = -EPROTO;
456 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800457 }
458
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400459 if (md->body->mbo_eadatasize == 0) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530460 CDEBUG(D_INFO,
461 "OBD_MD_FLEASIZE set, but eadatasize 0\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200462 rc = -EPROTO;
463 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800464 }
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400465 lmmsize = md->body->mbo_eadatasize;
Peng Taod7e09d02013-05-02 16:46:55 +0800466 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200467 if (!lmm) {
468 rc = -EPROTO;
469 goto out;
470 }
Peng Taod7e09d02013-05-02 16:46:55 +0800471
472 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
473 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200474 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800475
476 if (rc < sizeof(*md->lsm)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530477 CDEBUG(D_INFO,
478 "lsm size too small: rc < sizeof (*md->lsm) (%d < %d)\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800479 rc, (int)sizeof(*md->lsm));
Julia Lawalld5fdc202014-08-28 12:10:35 +0200480 rc = -EPROTO;
481 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800482 }
483
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400484 } else if (md->body->mbo_valid & OBD_MD_FLDIREA) {
Peng Taod7e09d02013-05-02 16:46:55 +0800485 int lmvsize;
486 struct lov_mds_md *lmv;
487
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400488 if (!S_ISDIR(md->body->mbo_mode)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530489 CDEBUG(D_INFO,
490 "OBD_MD_FLDIREA set, should be a directory, but is not\n");
Julia Lawalld5fdc202014-08-28 12:10:35 +0200491 rc = -EPROTO;
492 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800493 }
494
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400495 if (md->body->mbo_eadatasize == 0) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530496 CDEBUG(D_INFO,
497 "OBD_MD_FLDIREA is set, but eadatasize 0\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800498 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +0800499 }
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400500 if (md->body->mbo_valid & OBD_MD_MEA) {
501 lmvsize = md->body->mbo_eadatasize;
Peng Taod7e09d02013-05-02 16:46:55 +0800502 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
503 lmvsize);
Julia Lawalld5fdc202014-08-28 12:10:35 +0200504 if (!lmv) {
505 rc = -EPROTO;
506 goto out;
507 }
Peng Taod7e09d02013-05-02 16:46:55 +0800508
wang dif6718d12016-07-21 22:43:55 -0400509 rc = obd_unpackmd(md_exp, (void *)&md->lmv, lmv,
Peng Taod7e09d02013-05-02 16:46:55 +0800510 lmvsize);
511 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200512 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800513
wang dif6718d12016-07-21 22:43:55 -0400514 if (rc < sizeof(*md->lmv)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530515 CDEBUG(D_INFO,
wang dif6718d12016-07-21 22:43:55 -0400516 "size too small: rc < sizeof(*md->lmv) (%d < %d)\n",
517 rc, (int)sizeof(*md->lmv));
Julia Lawalld5fdc202014-08-28 12:10:35 +0200518 rc = -EPROTO;
519 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800520 }
521 }
522 }
523 rc = 0;
524
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400525 if (md->body->mbo_valid & OBD_MD_FLACL) {
Peng Taod7e09d02013-05-02 16:46:55 +0800526 /* for ACL, it's possible that FLACL is set but aclsize is zero.
527 * only when aclsize != 0 there's an actual segment for ACL
528 * in reply buffer.
529 */
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400530 if (md->body->mbo_aclsize) {
Peng Taod7e09d02013-05-02 16:46:55 +0800531 rc = mdc_unpack_acl(req, md);
532 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +0200533 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800534#ifdef CONFIG_FS_POSIX_ACL
535 } else {
536 md->posix_acl = NULL;
537#endif
538 }
539 }
Peng Taod7e09d02013-05-02 16:46:55 +0800540
Peng Taod7e09d02013-05-02 16:46:55 +0800541out:
542 if (rc) {
Peng Taod7e09d02013-05-02 16:46:55 +0800543#ifdef CONFIG_FS_POSIX_ACL
544 posix_acl_release(md->posix_acl);
545#endif
546 if (md->lsm)
547 obd_free_memmd(dt_exp, &md->lsm);
548 }
549 return rc;
550}
551
Shraddha Barke358855b2015-10-30 20:59:03 +0530552static int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
Peng Taod7e09d02013-05-02 16:46:55 +0800553{
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800554 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800555}
556
557/**
558 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
559 * RPC chains.
560 */
561void mdc_replay_open(struct ptlrpc_request *req)
562{
563 struct md_open_data *mod = req->rq_cb_data;
564 struct ptlrpc_request *close_req;
565 struct obd_client_handle *och;
566 struct lustre_handle old;
567 struct mdt_body *body;
Peng Taod7e09d02013-05-02 16:46:55 +0800568
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500569 if (!mod) {
Peng Taod7e09d02013-05-02 16:46:55 +0800570 DEBUG_REQ(D_ERROR, req,
571 "Can't properly replay without open data.");
Peng Taod7e09d02013-05-02 16:46:55 +0800572 return;
573 }
574
575 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
Peng Taod7e09d02013-05-02 16:46:55 +0800576
577 och = mod->mod_och;
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500578 if (och) {
Peng Taod7e09d02013-05-02 16:46:55 +0800579 struct lustre_handle *file_fh;
580
581 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
582
583 file_fh = &och->och_fh;
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700584 CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400585 file_fh->cookie, body->mbo_handle.cookie);
Peng Taod7e09d02013-05-02 16:46:55 +0800586 old = *file_fh;
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400587 *file_fh = body->mbo_handle;
Peng Taod7e09d02013-05-02 16:46:55 +0800588 }
589 close_req = mod->mod_close_req;
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500590 if (close_req) {
Peng Taod7e09d02013-05-02 16:46:55 +0800591 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
592 struct mdt_ioepoch *epoch;
593
594 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
595 epoch = req_capsule_client_get(&close_req->rq_pill,
596 &RMF_MDT_EPOCH);
597 LASSERT(epoch);
598
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500599 if (och)
Peng Taod7e09d02013-05-02 16:46:55 +0800600 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
601 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400602 epoch->handle = body->mbo_handle;
Peng Taod7e09d02013-05-02 16:46:55 +0800603 }
Peng Taod7e09d02013-05-02 16:46:55 +0800604}
605
606void mdc_commit_open(struct ptlrpc_request *req)
607{
608 struct md_open_data *mod = req->rq_cb_data;
Srikrishan Malik7436d072014-08-11 23:57:33 +0530609
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500610 if (!mod)
Peng Taod7e09d02013-05-02 16:46:55 +0800611 return;
612
613 /**
614 * No need to touch md_open_data::mod_och, it holds a reference on
615 * \var mod and will zero references to each other, \var mod will be
616 * freed after that when md_open_data::mod_och will put the reference.
617 */
618
619 /**
620 * Do not let open request to disappear as it still may be needed
621 * for close rpc to happen (it may happen on evict only, otherwise
622 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
623 * called), just mark this rpc as committed to distinguish these 2
624 * cases, see mdc_close() for details. The open request reference will
625 * be put along with freeing \var mod.
626 */
627 ptlrpc_request_addref(req);
628 spin_lock(&req->rq_lock);
629 req->rq_committed = 1;
630 spin_unlock(&req->rq_lock);
631 req->rq_cb_data = NULL;
632 obd_mod_put(mod);
633}
634
635int mdc_set_open_replay_data(struct obd_export *exp,
636 struct obd_client_handle *och,
Hongchao Zhang63d42572014-02-28 21:16:37 -0500637 struct lookup_intent *it)
Peng Taod7e09d02013-05-02 16:46:55 +0800638{
639 struct md_open_data *mod;
640 struct mdt_rec_create *rec;
641 struct mdt_body *body;
John L. Hammond8bf86fd2016-06-20 16:55:40 -0400642 struct ptlrpc_request *open_req = it->it_request;
Peng Taod7e09d02013-05-02 16:46:55 +0800643 struct obd_import *imp = open_req->rq_import;
Peng Taod7e09d02013-05-02 16:46:55 +0800644
645 if (!open_req->rq_replay)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800646 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800647
648 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
649 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500650 LASSERT(rec);
Peng Taod7e09d02013-05-02 16:46:55 +0800651 /* Incoming message in my byte order (it's been swabbed). */
652 /* Outgoing messages always in my byte order. */
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500653 LASSERT(body);
Peng Taod7e09d02013-05-02 16:46:55 +0800654
655 /* Only if the import is replayable, we set replay_open data */
656 if (och && imp->imp_replayable) {
657 mod = obd_mod_alloc();
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500658 if (!mod) {
Peng Taod7e09d02013-05-02 16:46:55 +0800659 DEBUG_REQ(D_ERROR, open_req,
660 "Can't allocate md_open_data");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800661 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800662 }
663
664 /**
665 * Take a reference on \var mod, to be freed on mdc_close().
666 * It protects \var mod from being freed on eviction (commit
667 * callback is called despite rq_replay flag).
668 * Another reference for \var och.
669 */
670 obd_mod_get(mod);
671 obd_mod_get(mod);
672
673 spin_lock(&open_req->rq_lock);
674 och->och_mod = mod;
675 mod->mod_och = och;
Hongchao Zhang63d42572014-02-28 21:16:37 -0500676 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
677 it_disposition(it, DISP_OPEN_STRIPE);
Peng Taod7e09d02013-05-02 16:46:55 +0800678 mod->mod_open_req = open_req;
679 open_req->rq_cb_data = mod;
680 open_req->rq_commit_cb = mdc_commit_open;
681 spin_unlock(&open_req->rq_lock);
682 }
683
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400684 rec->cr_fid2 = body->mbo_fid1;
685 rec->cr_ioepoch = body->mbo_ioepoch;
686 rec->cr_old_handle.cookie = body->mbo_handle.cookie;
Peng Taod7e09d02013-05-02 16:46:55 +0800687 open_req->rq_replay_cb = mdc_replay_open;
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400688 if (!fid_is_sane(&body->mbo_fid1)) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530689 DEBUG_REQ(D_ERROR, open_req,
690 "Saving replay request with insane fid");
Peng Taod7e09d02013-05-02 16:46:55 +0800691 LBUG();
692 }
693
694 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800695 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800696}
697
Hongchao Zhang63d42572014-02-28 21:16:37 -0500698static void mdc_free_open(struct md_open_data *mod)
699{
700 int committed = 0;
701
702 if (mod->mod_is_create == 0 &&
703 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
704 committed = 1;
705
706 LASSERT(mod->mod_open_req->rq_replay == 0);
707
708 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
709
710 ptlrpc_request_committed(mod->mod_open_req, committed);
711 if (mod->mod_close_req)
712 ptlrpc_request_committed(mod->mod_close_req, committed);
713}
714
Shraddha Barke358855b2015-10-30 20:59:03 +0530715static int mdc_clear_open_replay_data(struct obd_export *exp,
716 struct obd_client_handle *och)
Peng Taod7e09d02013-05-02 16:46:55 +0800717{
718 struct md_open_data *mod = och->och_mod;
Peng Taod7e09d02013-05-02 16:46:55 +0800719
720 /**
721 * It is possible to not have \var mod in a case of eviction between
722 * lookup and ll_file_open().
723 **/
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500724 if (!mod)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800725 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800726
727 LASSERT(mod != LP_POISON);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500728 LASSERT(mod->mod_open_req);
Hongchao Zhang63d42572014-02-28 21:16:37 -0500729 mdc_free_open(mod);
Peng Taod7e09d02013-05-02 16:46:55 +0800730
731 mod->mod_och = NULL;
732 och->och_mod = NULL;
733 obd_mod_put(mod);
734
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800735 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800736}
737
738/* Prepares the request for the replay by the given reply */
739static void mdc_close_handle_reply(struct ptlrpc_request *req,
740 struct md_op_data *op_data, int rc) {
741 struct mdt_body *repbody;
742 struct mdt_ioepoch *epoch;
743
744 if (req && rc == -EAGAIN) {
745 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
746 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
747
748 epoch->flags |= MF_SOM_AU;
John L. Hammond2e1b5b82016-08-16 16:19:08 -0400749 if (repbody->mbo_valid & OBD_MD_FLGETATTRLOCK)
Peng Taod7e09d02013-05-02 16:46:55 +0800750 op_data->op_flags |= MF_GETATTR_LOCK;
751 }
752}
753
Luca Ceresolif6219c12015-01-18 15:06:47 +0100754static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
755 struct md_open_data *mod, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800756{
757 struct obd_device *obd = class_exp2obd(exp);
758 struct ptlrpc_request *req;
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800759 struct req_format *req_fmt;
760 int rc;
761 int saved_rc = 0;
762
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800763 req_fmt = &RQF_MDS_CLOSE;
764 if (op_data->op_bias & MDS_HSM_RELEASE) {
765 req_fmt = &RQF_MDS_RELEASE_CLOSE;
766
767 /* allocate a FID for volatile file */
wang di8f18c8a2016-08-16 16:18:54 -0400768 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800769 if (rc < 0) {
770 CERROR("%s: "DFID" failed to allocate FID: %d\n",
771 obd->obd_name, PFID(&op_data->op_fid1), rc);
772 /* save the errcode and proceed to close */
773 saved_rc = rc;
774 }
775 }
Peng Taod7e09d02013-05-02 16:46:55 +0800776
777 *request = NULL;
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800778 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500779 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800780 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800781
Peng Taod7e09d02013-05-02 16:46:55 +0800782 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
783 if (rc) {
784 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800785 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800786 }
787
788 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
789 * portal whose threads are not taking any DLM locks and are therefore
Oleg Drokin1df232e2016-02-24 22:00:33 -0500790 * always progressing
791 */
Peng Taod7e09d02013-05-02 16:46:55 +0800792 req->rq_request_portal = MDS_READPAGE_PORTAL;
793 ptlrpc_at_set_req_timeout(req);
794
795 /* Ensure that this close's handle is fixed up during replay. */
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500796 if (likely(mod)) {
797 LASSERTF(mod->mod_open_req &&
Peng Taod7e09d02013-05-02 16:46:55 +0800798 mod->mod_open_req->rq_type != LI_POISON,
799 "POISONED open %p!\n", mod->mod_open_req);
800
801 mod->mod_close_req = req;
802
803 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
804 /* We no longer want to preserve this open for replay even
Oleg Drokin1df232e2016-02-24 22:00:33 -0500805 * though the open was committed. b=3632, b=3633
806 */
Peng Taod7e09d02013-05-02 16:46:55 +0800807 spin_lock(&mod->mod_open_req->rq_lock);
808 mod->mod_open_req->rq_replay = 0;
809 spin_unlock(&mod->mod_open_req->rq_lock);
810 } else {
Srikrishan Malike5e663a2014-08-11 23:57:30 +0530811 CDEBUG(D_HA,
812 "couldn't find open req; expecting close error\n");
Peng Taod7e09d02013-05-02 16:46:55 +0800813 }
814
815 mdc_close_pack(req, op_data);
816
817 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
Brian Behlendorf44779342014-04-27 13:06:47 -0400818 obd->u.cli.cl_default_mds_easize);
Peng Taod7e09d02013-05-02 16:46:55 +0800819 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
Brian Behlendorf44779342014-04-27 13:06:47 -0400820 obd->u.cli.cl_default_mds_cookiesize);
Peng Taod7e09d02013-05-02 16:46:55 +0800821
822 ptlrpc_request_set_replen(req);
823
824 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
825 rc = ptlrpc_queue_wait(req);
826 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
827
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500828 if (!req->rq_repmsg) {
Peng Taod7e09d02013-05-02 16:46:55 +0800829 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
830 req->rq_status);
831 if (rc == 0)
832 rc = req->rq_status ?: -EIO;
833 } else if (rc == 0 || rc == -EAGAIN) {
834 struct mdt_body *body;
835
836 rc = lustre_msg_get_status(req->rq_repmsg);
837 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
Srikrishan Malikee990b32014-08-13 19:31:16 +0530838 DEBUG_REQ(D_ERROR, req,
839 "type == PTL_RPC_MSG_ERR, err = %d", rc);
Peng Taod7e09d02013-05-02 16:46:55 +0800840 if (rc > 0)
841 rc = -rc;
842 }
843 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500844 if (!body)
Peng Taod7e09d02013-05-02 16:46:55 +0800845 rc = -EPROTO;
846 } else if (rc == -ESTALE) {
847 /**
848 * it can be allowed error after 3633 if open was committed and
849 * server failed before close was sent. Let's check if mod
850 * exists and return no error in that case
851 */
852 if (mod) {
853 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
Peng Taod7e09d02013-05-02 16:46:55 +0800854 if (mod->mod_open_req->rq_committed)
855 rc = 0;
856 }
857 }
858
859 if (mod) {
860 if (rc != 0)
861 mod->mod_close_req = NULL;
862 /* Since now, mod is accessed through open_req only,
Oleg Drokin1df232e2016-02-24 22:00:33 -0500863 * thus close req does not keep a reference on mod anymore.
864 */
Peng Taod7e09d02013-05-02 16:46:55 +0800865 obd_mod_put(mod);
866 }
867 *request = req;
868 mdc_close_handle_reply(req, op_data, rc);
Jinshan Xiong48d23e62013-12-03 21:58:48 +0800869 return rc < 0 ? rc : saved_rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800870}
871
Luca Ceresolif6219c12015-01-18 15:06:47 +0100872static int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
873 struct md_open_data *mod)
Peng Taod7e09d02013-05-02 16:46:55 +0800874{
875 struct obd_device *obd = class_exp2obd(exp);
876 struct ptlrpc_request *req;
877 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800878
879 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
880 &RQF_MDS_DONE_WRITING);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500881 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800882 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800883
Peng Taod7e09d02013-05-02 16:46:55 +0800884 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
885 if (rc) {
886 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800887 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800888 }
889
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500890 if (mod) {
891 LASSERTF(mod->mod_open_req &&
Peng Taod7e09d02013-05-02 16:46:55 +0800892 mod->mod_open_req->rq_type != LI_POISON,
893 "POISONED setattr %p!\n", mod->mod_open_req);
894
895 mod->mod_close_req = req;
896 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
897 /* We no longer want to preserve this setattr for replay even
Oleg Drokin1df232e2016-02-24 22:00:33 -0500898 * though the open was committed. b=3632, b=3633
899 */
Peng Taod7e09d02013-05-02 16:46:55 +0800900 spin_lock(&mod->mod_open_req->rq_lock);
901 mod->mod_open_req->rq_replay = 0;
902 spin_unlock(&mod->mod_open_req->rq_lock);
903 }
904
905 mdc_close_pack(req, op_data);
906 ptlrpc_request_set_replen(req);
907
908 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
909 rc = ptlrpc_queue_wait(req);
910 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
911
912 if (rc == -ESTALE) {
913 /**
914 * it can be allowed error after 3633 if open or setattr were
915 * committed and server failed before close was sent.
916 * Let's check if mod exists and return no error in that case
917 */
918 if (mod) {
Peng Taod7e09d02013-05-02 16:46:55 +0800919 if (mod->mod_open_req->rq_committed)
920 rc = 0;
921 }
922 }
923
924 if (mod) {
925 if (rc != 0)
926 mod->mod_close_req = NULL;
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500927 LASSERT(mod->mod_open_req);
Hongchao Zhang63d42572014-02-28 21:16:37 -0500928 mdc_free_open(mod);
929
Peng Taod7e09d02013-05-02 16:46:55 +0800930 /* Since now, mod is accessed through setattr req only,
Oleg Drokin1df232e2016-02-24 22:00:33 -0500931 * thus DW req does not keep a reference on mod anymore.
932 */
Peng Taod7e09d02013-05-02 16:46:55 +0800933 obd_mod_put(mod);
934 }
935
936 mdc_close_handle_reply(req, op_data, rc);
937 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800938 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800939}
940
Luca Ceresolif6219c12015-01-18 15:06:47 +0100941static int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
942 struct page **pages, struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +0800943{
944 struct ptlrpc_request *req;
945 struct ptlrpc_bulk_desc *desc;
946 int i;
947 wait_queue_head_t waitq;
948 int resends = 0;
949 struct l_wait_info lwi;
950 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800951
952 *request = NULL;
953 init_waitqueue_head(&waitq);
954
955restart_bulk:
956 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500957 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800958 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800959
Peng Taod7e09d02013-05-02 16:46:55 +0800960 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
961 if (rc) {
962 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800963 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800964 }
965
966 req->rq_request_portal = MDS_READPAGE_PORTAL;
967 ptlrpc_at_set_req_timeout(req);
968
969 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
970 MDS_BULK_PORTAL);
Oleg Drokin34e3ff92016-02-16 00:46:53 -0500971 if (!desc) {
Peng Taod7e09d02013-05-02 16:46:55 +0800972 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800973 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800974 }
975
976 /* NB req now owns desc and will free it when it gets freed */
977 for (i = 0; i < op_data->op_npages; i++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300978 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_SIZE);
Peng Taod7e09d02013-05-02 16:46:55 +0800979
980 mdc_readdir_pack(req, op_data->op_offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300981 PAGE_SIZE * op_data->op_npages,
Oleg Drokinef2e0f52015-09-27 16:45:46 -0400982 &op_data->op_fid1);
Peng Taod7e09d02013-05-02 16:46:55 +0800983
984 ptlrpc_request_set_replen(req);
985 rc = ptlrpc_queue_wait(req);
986 if (rc) {
987 ptlrpc_req_finished(req);
988 if (rc != -ETIMEDOUT)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800989 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800990
991 resends++;
992 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
993 CERROR("too many resend retries, returning error\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800994 return -EIO;
Peng Taod7e09d02013-05-02 16:46:55 +0800995 }
Srikrishan Malike5e663a2014-08-11 23:57:30 +0530996 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends),
997 NULL, NULL, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +0800998 l_wait_event(waitq, 0, &lwi);
999
1000 goto restart_bulk;
1001 }
1002
1003 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1004 req->rq_bulk->bd_nob_transferred);
1005 if (rc < 0) {
1006 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001007 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001008 }
1009
1010 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1011 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
Oleg Drokin22e0bc62016-02-26 01:50:06 -05001012 req->rq_bulk->bd_nob_transferred,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001013 PAGE_SIZE * op_data->op_npages);
Peng Taod7e09d02013-05-02 16:46:55 +08001014 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001015 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08001016 }
1017
1018 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001019 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001020}
1021
wang di4f76f0e2016-08-19 14:07:26 -04001022static int mdc_getpage(struct obd_export *exp, const struct lu_fid *fid,
1023 u64 offset, struct page **pages, int npages,
1024 struct ptlrpc_request **request)
1025{
1026 struct ptlrpc_bulk_desc *desc;
1027 struct ptlrpc_request *req;
1028 wait_queue_head_t waitq;
1029 struct l_wait_info lwi;
1030 int resends = 0;
1031 int rc;
1032 int i;
1033
1034 *request = NULL;
1035 init_waitqueue_head(&waitq);
1036
1037restart_bulk:
1038 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1039 if (!req)
1040 return -ENOMEM;
1041
1042 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1043 if (rc) {
1044 ptlrpc_request_free(req);
1045 return rc;
1046 }
1047
1048 req->rq_request_portal = MDS_READPAGE_PORTAL;
1049 ptlrpc_at_set_req_timeout(req);
1050
1051 desc = ptlrpc_prep_bulk_imp(req, npages, 1, BULK_PUT_SINK,
1052 MDS_BULK_PORTAL);
1053 if (!desc) {
1054 ptlrpc_request_free(req);
1055 return -ENOMEM;
1056 }
1057
1058 /* NB req now owns desc and will free it when it gets freed */
1059 for (i = 0; i < npages; i++)
1060 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_SIZE);
1061
1062 mdc_readdir_pack(req, offset, PAGE_SIZE * npages, fid);
1063
1064 ptlrpc_request_set_replen(req);
1065 rc = ptlrpc_queue_wait(req);
1066 if (rc) {
1067 ptlrpc_req_finished(req);
1068 if (rc != -ETIMEDOUT)
1069 return rc;
1070
1071 resends++;
1072 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1073 CERROR("%s: too many resend retries: rc = %d\n",
1074 exp->exp_obd->obd_name, -EIO);
1075 return -EIO;
1076 }
1077 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL,
1078 NULL);
1079 l_wait_event(waitq, 0, &lwi);
1080
1081 goto restart_bulk;
1082 }
1083
1084 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1085 req->rq_bulk->bd_nob_transferred);
1086 if (rc < 0) {
1087 ptlrpc_req_finished(req);
1088 return rc;
1089 }
1090
1091 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1092 CERROR("%s: unexpected bytes transferred: %d (%ld expected)\n",
1093 exp->exp_obd->obd_name, req->rq_bulk->bd_nob_transferred,
1094 PAGE_SIZE * npages);
1095 ptlrpc_req_finished(req);
1096 return -EPROTO;
1097 }
1098
1099 *request = req;
1100 return 0;
1101}
1102
1103static void mdc_release_page(struct page *page, int remove)
1104{
1105 if (remove) {
1106 lock_page(page);
1107 if (likely(page->mapping))
1108 truncate_complete_page(page->mapping, page);
1109 unlock_page(page);
1110 }
1111 put_page(page);
1112}
1113
1114static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
1115 __u64 *start, __u64 *end, int hash64)
1116{
1117 /*
1118 * Complement of hash is used as an index so that
1119 * radix_tree_gang_lookup() can be used to find a page with starting
1120 * hash _smaller_ than one we are looking for.
1121 */
1122 unsigned long offset = hash_x_index(*hash, hash64);
1123 struct page *page;
1124 int found;
1125
1126 spin_lock_irq(&mapping->tree_lock);
1127 found = radix_tree_gang_lookup(&mapping->page_tree,
1128 (void **)&page, offset, 1);
1129 if (found > 0 && !radix_tree_exceptional_entry(page)) {
1130 struct lu_dirpage *dp;
1131
1132 get_page(page);
1133 spin_unlock_irq(&mapping->tree_lock);
1134 /*
1135 * In contrast to find_lock_page() we are sure that directory
1136 * page cannot be truncated (while DLM lock is held) and,
1137 * hence, can avoid restart.
1138 *
1139 * In fact, page cannot be locked here at all, because
1140 * mdc_read_page_remote does synchronous io.
1141 */
1142 wait_on_page_locked(page);
1143 if (PageUptodate(page)) {
1144 dp = kmap(page);
1145 if (BITS_PER_LONG == 32 && hash64) {
1146 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1147 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1148 *hash = *hash >> 32;
1149 } else {
1150 *start = le64_to_cpu(dp->ldp_hash_start);
1151 *end = le64_to_cpu(dp->ldp_hash_end);
1152 }
1153 if (unlikely(*start == 1 && *hash == 0))
1154 *hash = *start;
1155 else
1156 LASSERTF(*start <= *hash, "start = %#llx,end = %#llx,hash = %#llx\n",
1157 *start, *end, *hash);
1158 CDEBUG(D_VFSTRACE, "offset %lx [%#llx %#llx], hash %#llx\n",
1159 offset, *start, *end, *hash);
1160 if (*hash > *end) {
1161 kunmap(page);
1162 mdc_release_page(page, 0);
1163 page = NULL;
1164 } else if (*end != *start && *hash == *end) {
1165 /*
1166 * upon hash collision, remove this page,
1167 * otherwise put page reference, and
1168 * mdc_read_page_remote() will issue RPC to
1169 * fetch the page we want.
1170 */
1171 kunmap(page);
1172 mdc_release_page(page,
1173 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1174 page = NULL;
1175 }
1176 } else {
1177 put_page(page);
1178 page = ERR_PTR(-EIO);
1179 }
1180 } else {
1181 spin_unlock_irq(&mapping->tree_lock);
1182 page = NULL;
1183 }
1184 return page;
1185}
1186
1187/*
1188 * Adjust a set of pages, each page containing an array of lu_dirpages,
1189 * so that each page can be used as a single logical lu_dirpage.
1190 *
1191 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1192 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1193 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
1194 * value is used as a cookie to request the next lu_dirpage in a
1195 * directory listing that spans multiple pages (two in this example):
1196 * ________
1197 * | |
1198 * .|--------v------- -----.
1199 * |s|e|f|p|ent|ent| ... |ent|
1200 * '--|-------------- -----' Each PAGE contains a single
1201 * '------. lu_dirpage.
1202 * .---------v------- -----.
1203 * |s|e|f|p|ent| 0 | ... | 0 |
1204 * '----------------- -----'
1205 *
1206 * However, on hosts where the native VM page size (PAGE_SIZE) is
1207 * larger than LU_PAGE_SIZE, a single host page may contain multiple
1208 * lu_dirpages. After reading the lu_dirpages from the MDS, the
1209 * ldp_hash_end of the first lu_dirpage refers to the one immediately
1210 * after it in the same PAGE (arrows simplified for brevity, but
1211 * in general e0==s1, e1==s2, etc.):
1212 *
1213 * .-------------------- -----.
1214 * |s0|e0|f0|p|ent|ent| ... |ent|
1215 * |---v---------------- -----|
1216 * |s1|e1|f1|p|ent|ent| ... |ent|
1217 * |---v---------------- -----| Here, each PAGE contains
1218 * ... multiple lu_dirpages.
1219 * |---v---------------- -----|
1220 * |s'|e'|f'|p|ent|ent| ... |ent|
1221 * '---|---------------- -----'
1222 * v
1223 * .----------------------------.
1224 * | next PAGE |
1225 *
1226 * This structure is transformed into a single logical lu_dirpage as follows:
1227 *
1228 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1229 * labeled 'next PAGE'.
1230 *
1231 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1232 * a hash collision with the next page exists.
1233 *
1234 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1235 * to the first entry of the next lu_dirpage.
1236 */
1237#if PAGE_SIZE > LU_PAGE_SIZE
1238static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
1239{
1240 int i;
1241
1242 for (i = 0; i < cfs_pgs; i++) {
1243 __u64 hash_end = le64_to_cpu(dp->ldp_hash_end);
1244 __u32 flags = le32_to_cpu(dp->ldp_flags);
1245 struct lu_dirpage *dp = kmap(pages[i]);
1246 struct lu_dirpage *first = dp;
1247 struct lu_dirent *end_dirent = NULL;
1248 struct lu_dirent *ent;
1249
1250 while (--lu_pgs > 0) {
1251 ent = lu_dirent_start(dp);
1252 for (end_dirent = ent; ent;
1253 end_dirent = ent, ent = lu_dirent_next(ent));
1254
1255 /* Advance dp to next lu_dirpage. */
1256 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1257
1258 /* Check if we've reached the end of the CFS_PAGE. */
1259 if (!((unsigned long)dp & ~PAGE_MASK))
1260 break;
1261
1262 /* Save the hash and flags of this lu_dirpage. */
1263 hash_end = le64_to_cpu(dp->ldp_hash_end);
1264 flags = le32_to_cpu(dp->ldp_flags);
1265
1266 /* Check if lu_dirpage contains no entries. */
1267 if (!end_dirent)
1268 break;
1269
1270 /*
1271 * Enlarge the end entry lde_reclen from 0 to
1272 * first entry of next lu_dirpage.
1273 */
1274 LASSERT(!le16_to_cpu(end_dirent->lde_reclen));
1275 end_dirent->lde_reclen =
1276 cpu_to_le16((char *)(dp->ldp_entries) -
1277 (char *)end_dirent);
1278 }
1279
1280 first->ldp_hash_end = hash_end;
1281 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
1282 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
1283
1284 kunmap(pages[i]);
1285 }
1286 LASSERTF(lu_pgs == 0, "left = %d", lu_pgs);
1287}
1288#else
1289#define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1290#endif /* PAGE_SIZE > LU_PAGE_SIZE */
1291
1292/* parameters for readdir page */
1293struct readpage_param {
1294 struct md_op_data *rp_mod;
1295 __u64 rp_off;
1296 int rp_hash64;
1297 struct obd_export *rp_exp;
1298 struct md_callback *rp_cb;
1299};
1300
1301/**
1302 * Read pages from server.
1303 *
1304 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
1305 * a header lu_dirpage which describes the start/end hash, and whether this
1306 * page is empty (contains no dir entry) or hash collide with next page.
1307 * After client receives reply, several pages will be integrated into dir page
1308 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
1309 * lu_dirpage for this integrated page will be adjusted.
1310 **/
1311static int mdc_read_page_remote(void *data, struct page *page0)
1312{
1313 struct readpage_param *rp = data;
1314 struct page **page_pool;
1315 struct page *page;
1316 struct lu_dirpage *dp;
1317 int rd_pgs = 0; /* number of pages read actually */
1318 int npages;
1319 struct md_op_data *op_data = rp->rp_mod;
1320 struct ptlrpc_request *req;
1321 int max_pages = op_data->op_max_pages;
1322 struct inode *inode;
1323 struct lu_fid *fid;
1324 int i;
1325 int rc;
1326
1327 LASSERT(max_pages > 0 && max_pages <= PTLRPC_MAX_BRW_PAGES);
1328 inode = op_data->op_data;
1329 fid = &op_data->op_fid1;
1330 LASSERT(inode);
1331
1332 page_pool = kcalloc(max_pages, sizeof(page), GFP_NOFS);
1333 if (page_pool) {
1334 page_pool[0] = page0;
1335 } else {
1336 page_pool = &page0;
1337 max_pages = 1;
1338 }
1339
1340 for (npages = 1; npages < max_pages; npages++) {
1341 page = page_cache_alloc_cold(inode->i_mapping);
1342 if (!page)
1343 break;
1344 page_pool[npages] = page;
1345 }
1346
1347 rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, page_pool, npages, &req);
1348 if (!rc) {
1349 int lu_pgs = req->rq_bulk->bd_nob_transferred;
1350
1351 rd_pgs = (req->rq_bulk->bd_nob_transferred +
1352 PAGE_SIZE - 1) >> PAGE_SHIFT;
1353 lu_pgs >>= LU_PAGE_SHIFT;
1354 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1355
1356 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", rd_pgs, lu_pgs,
1357 op_data->op_npages);
1358
1359 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1360
1361 SetPageUptodate(page0);
1362 }
1363
1364 unlock_page(page0);
1365 ptlrpc_req_finished(req);
1366 CDEBUG(D_CACHE, "read %d/%d pages\n", rd_pgs, npages);
1367 for (i = 1; i < npages; i++) {
1368 unsigned long offset;
1369 __u64 hash;
1370 int ret;
1371
1372 page = page_pool[i];
1373
1374 if (rc < 0 || i >= rd_pgs) {
1375 put_page(page);
1376 continue;
1377 }
1378
1379 SetPageUptodate(page);
1380
1381 dp = kmap(page);
1382 hash = le64_to_cpu(dp->ldp_hash_start);
1383 kunmap(page);
1384
1385 offset = hash_x_index(hash, rp->rp_hash64);
1386
1387 prefetchw(&page->flags);
1388 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1389 GFP_KERNEL);
1390 if (!ret)
1391 unlock_page(page);
1392 else
1393 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed: rc = %d\n",
1394 offset, ret);
1395 put_page(page);
1396 }
1397
1398 if (page_pool != &page0)
1399 kfree(page_pool);
1400
1401 return rc;
1402}
1403
1404/**
1405 * Read dir page from cache first, if it can not find it, read it from
1406 * server and add into the cache.
1407 *
1408 * \param[in] exp MDC export
1409 * \param[in] op_data client MD stack parameters, transferring parameters
1410 * between different layers on client MD stack.
1411 * \param[in] cb_op callback required for ldlm lock enqueue during
1412 * read page
1413 * \param[in] hash_offset the hash offset of the page to be read
1414 * \param[in] ppage the page to be read
1415 *
1416 * retval = 0 get the page successfully
1417 * errno(<0) get the page failed
1418 */
1419static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1420 struct md_callback *cb_op, __u64 hash_offset,
1421 struct page **ppage)
1422{
1423 struct lookup_intent it = { .it_op = IT_READDIR };
1424 struct page *page;
1425 struct inode *dir = op_data->op_data;
1426 struct address_space *mapping;
1427 struct lu_dirpage *dp;
1428 __u64 start = 0;
1429 __u64 end = 0;
1430 struct lustre_handle lockh;
1431 struct ptlrpc_request *enq_req = NULL;
1432 struct readpage_param rp_param;
1433 int rc;
1434
1435 *ppage = NULL;
1436
1437 LASSERT(dir);
1438 mapping = dir->i_mapping;
1439
1440 rc = mdc_intent_lock(exp, op_data, NULL, 0, &it, 0, &enq_req,
1441 cb_op->md_blocking_ast, 0);
1442 if (enq_req)
1443 ptlrpc_req_finished(enq_req);
1444
1445 if (rc < 0) {
1446 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1447 exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1448 return rc;
1449 }
1450
1451 rc = 0;
1452 mdc_set_lock_data(exp, &it.it_lock_handle, dir, NULL);
1453
1454 rp_param.rp_off = hash_offset;
1455 rp_param.rp_hash64 = op_data->op_cli_flags & CLI_HASH64;
1456 page = mdc_page_locate(mapping, &rp_param.rp_off, &start, &end,
1457 rp_param.rp_hash64);
1458 if (IS_ERR(page)) {
1459 CERROR("%s: dir page locate: "DFID" at %llu: rc %ld\n",
1460 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1461 rp_param.rp_off, PTR_ERR(page));
1462 rc = PTR_ERR(page);
1463 goto out_unlock;
1464 } else if (page) {
1465 /*
1466 * XXX nikita: not entirely correct handling of a corner case:
1467 * suppose hash chain of entries with hash value HASH crosses
1468 * border between pages P0 and P1. First both P0 and P1 are
1469 * cached, seekdir() is called for some entry from the P0 part
1470 * of the chain. Later P0 goes out of cache. telldir(HASH)
1471 * happens and finds P1, as it starts with matching hash
1472 * value. Remaining entries from P0 part of the chain are
1473 * skipped. (Is that really a bug?)
1474 *
1475 * Possible solutions: 0. don't cache P1 is such case, handle
1476 * it as an "overflow" page. 1. invalidate all pages at
1477 * once. 2. use HASH|1 as an index for P1.
1478 */
1479 goto hash_collision;
1480 }
1481
1482 rp_param.rp_exp = exp;
1483 rp_param.rp_mod = op_data;
1484 page = read_cache_page(mapping,
1485 hash_x_index(rp_param.rp_off,
1486 rp_param.rp_hash64),
1487 mdc_read_page_remote, &rp_param);
1488 if (IS_ERR(page)) {
1489 CERROR("%s: read cache page: "DFID" at %llu: rc %ld\n",
1490 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1491 rp_param.rp_off, PTR_ERR(page));
1492 rc = PTR_ERR(page);
1493 goto out_unlock;
1494 }
1495
1496 wait_on_page_locked(page);
1497 (void)kmap(page);
1498 if (!PageUptodate(page)) {
1499 CERROR("%s: page not updated: "DFID" at %llu: rc %d\n",
1500 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1501 rp_param.rp_off, -5);
1502 goto fail;
1503 }
1504 if (!PageChecked(page))
1505 SetPageChecked(page);
1506 if (PageError(page)) {
1507 CERROR("%s: page error: "DFID" at %llu: rc %d\n",
1508 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1509 rp_param.rp_off, -5);
1510 goto fail;
1511 }
1512
1513hash_collision:
1514 dp = page_address(page);
1515 if (BITS_PER_LONG == 32 && rp_param.rp_hash64) {
1516 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1517 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1518 rp_param.rp_off = hash_offset >> 32;
1519 } else {
1520 start = le64_to_cpu(dp->ldp_hash_start);
1521 end = le64_to_cpu(dp->ldp_hash_end);
1522 rp_param.rp_off = hash_offset;
1523 }
1524 if (end == start) {
1525 LASSERT(start == rp_param.rp_off);
1526 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
1527#if BITS_PER_LONG == 32
1528 CWARN("Real page-wide hash collision at [%llu %llu] with hash %llu\n",
1529 le64_to_cpu(dp->ldp_hash_start),
1530 le64_to_cpu(dp->ldp_hash_end), hash_offset);
1531#endif
1532 /*
1533 * Fetch whole overflow chain...
1534 *
1535 * XXX not yet.
1536 */
1537 goto fail;
1538 }
1539 *ppage = page;
1540out_unlock:
1541 lockh.cookie = it.it_lock_handle;
1542 ldlm_lock_decref(&lockh, it.it_lock_mode);
1543 it.it_lock_handle = 0;
1544 return rc;
1545fail:
1546 kunmap(page);
1547 mdc_release_page(page, 1);
1548 rc = -EIO;
1549 goto out_unlock;
1550}
1551
Peng Taod7e09d02013-05-02 16:46:55 +08001552static int mdc_statfs(const struct lu_env *env,
1553 struct obd_export *exp, struct obd_statfs *osfs,
1554 __u64 max_age, __u32 flags)
1555{
1556 struct obd_device *obd = class_exp2obd(exp);
1557 struct ptlrpc_request *req;
1558 struct obd_statfs *msfs;
1559 struct obd_import *imp = NULL;
1560 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001561
1562 /*
1563 * Since the request might also come from lprocfs, so we need
1564 * sync this with client_disconnect_export Bug15684
1565 */
1566 down_read(&obd->u.cli.cl_sem);
1567 if (obd->u.cli.cl_import)
1568 imp = class_import_get(obd->u.cli.cl_import);
1569 up_read(&obd->u.cli.cl_sem);
1570 if (!imp)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001571 return -ENODEV;
Peng Taod7e09d02013-05-02 16:46:55 +08001572
1573 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1574 LUSTRE_MDS_VERSION, MDS_STATFS);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001575 if (!req) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001576 rc = -ENOMEM;
1577 goto output;
1578 }
Peng Taod7e09d02013-05-02 16:46:55 +08001579
1580 ptlrpc_request_set_replen(req);
1581
1582 if (flags & OBD_STATFS_NODELAY) {
1583 /* procfs requests not want stay in wait for avoid deadlock */
1584 req->rq_no_resend = 1;
1585 req->rq_no_delay = 1;
1586 }
1587
1588 rc = ptlrpc_queue_wait(req);
1589 if (rc) {
1590 /* check connection error first */
1591 if (imp->imp_connect_error)
1592 rc = imp->imp_connect_error;
Julia Lawalld5fdc202014-08-28 12:10:35 +02001593 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001594 }
1595
1596 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001597 if (!msfs) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001598 rc = -EPROTO;
1599 goto out;
1600 }
Peng Taod7e09d02013-05-02 16:46:55 +08001601
1602 *osfs = *msfs;
Peng Taod7e09d02013-05-02 16:46:55 +08001603out:
1604 ptlrpc_req_finished(req);
1605output:
1606 class_import_put(imp);
1607 return rc;
1608}
1609
1610static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1611{
1612 __u32 keylen, vallen;
1613 void *key;
1614 int rc;
1615
1616 if (gf->gf_pathlen > PATH_MAX)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001617 return -ENAMETOOLONG;
Peng Taod7e09d02013-05-02 16:46:55 +08001618 if (gf->gf_pathlen < 2)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001619 return -EOVERFLOW;
Peng Taod7e09d02013-05-02 16:46:55 +08001620
1621 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1622 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
Julia Lawall7b817792015-05-01 17:51:17 +02001623 key = kzalloc(keylen, GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02001624 if (!key)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001625 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001626 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1627 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1628
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001629 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001630 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1631
Julia Lawalld5fdc202014-08-28 12:10:35 +02001632 if (!fid_is_sane(&gf->gf_fid)) {
1633 rc = -EINVAL;
1634 goto out;
1635 }
Peng Taod7e09d02013-05-02 16:46:55 +08001636
1637 /* Val is struct getinfo_fid2path result plus path */
1638 vallen = sizeof(*gf) + gf->gf_pathlen;
1639
1640 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1641 if (rc != 0 && rc != -EREMOTE)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001642 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001643
Julia Lawalld5fdc202014-08-28 12:10:35 +02001644 if (vallen <= sizeof(*gf)) {
1645 rc = -EPROTO;
1646 goto out;
1647 } else if (vallen > sizeof(*gf) + gf->gf_pathlen) {
1648 rc = -EOVERFLOW;
1649 goto out;
1650 }
Peng Taod7e09d02013-05-02 16:46:55 +08001651
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001652 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n%s\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001653 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1654
1655out:
Julia Lawall7b817792015-05-01 17:51:17 +02001656 kfree(key);
Peng Taod7e09d02013-05-02 16:46:55 +08001657 return rc;
1658}
1659
1660static int mdc_ioc_hsm_progress(struct obd_export *exp,
1661 struct hsm_progress_kernel *hpk)
1662{
1663 struct obd_import *imp = class_exp2cliimp(exp);
1664 struct hsm_progress_kernel *req_hpk;
1665 struct ptlrpc_request *req;
1666 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001667
1668 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1669 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001670 if (!req) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001671 rc = -ENOMEM;
1672 goto out;
1673 }
Peng Taod7e09d02013-05-02 16:46:55 +08001674
Fan Yong341f1f02016-06-19 22:53:53 -04001675 mdc_pack_body(req, NULL, 0, 0, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001676
1677 /* Copy hsm_progress struct */
1678 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001679 if (!req_hpk) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001680 rc = -EPROTO;
1681 goto out;
1682 }
Peng Taod7e09d02013-05-02 16:46:55 +08001683
1684 *req_hpk = *hpk;
Li Wei2d58de72013-07-23 00:06:32 +08001685 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
Peng Taod7e09d02013-05-02 16:46:55 +08001686
1687 ptlrpc_request_set_replen(req);
1688
1689 rc = mdc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08001690out:
1691 ptlrpc_req_finished(req);
1692 return rc;
1693}
1694
1695static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1696{
1697 __u32 *archive_mask;
1698 struct ptlrpc_request *req;
1699 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001700
1701 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1702 LUSTRE_MDS_VERSION,
1703 MDS_HSM_CT_REGISTER);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001704 if (!req) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001705 rc = -ENOMEM;
1706 goto out;
1707 }
Peng Taod7e09d02013-05-02 16:46:55 +08001708
Fan Yong341f1f02016-06-19 22:53:53 -04001709 mdc_pack_body(req, NULL, 0, 0, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001710
1711 /* Copy hsm_progress struct */
1712 archive_mask = req_capsule_client_get(&req->rq_pill,
1713 &RMF_MDS_HSM_ARCHIVE);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001714 if (!archive_mask) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001715 rc = -EPROTO;
1716 goto out;
1717 }
Peng Taod7e09d02013-05-02 16:46:55 +08001718
1719 *archive_mask = archives;
1720
1721 ptlrpc_request_set_replen(req);
1722
1723 rc = mdc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08001724out:
1725 ptlrpc_req_finished(req);
1726 return rc;
1727}
1728
1729static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1730 struct md_op_data *op_data)
1731{
1732 struct hsm_current_action *hca = op_data->op_data;
1733 struct hsm_current_action *req_hca;
1734 struct ptlrpc_request *req;
1735 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001736
1737 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1738 &RQF_MDS_HSM_ACTION);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001739 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001740 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001741
Peng Taod7e09d02013-05-02 16:46:55 +08001742 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1743 if (rc) {
1744 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001745 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001746 }
1747
Fan Yong341f1f02016-06-19 22:53:53 -04001748 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001749 op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001750
1751 ptlrpc_request_set_replen(req);
1752
1753 rc = mdc_queue_wait(req);
1754 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001755 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001756
1757 req_hca = req_capsule_server_get(&req->rq_pill,
1758 &RMF_MDS_HSM_CURRENT_ACTION);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001759 if (!req_hca) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001760 rc = -EPROTO;
1761 goto out;
1762 }
Peng Taod7e09d02013-05-02 16:46:55 +08001763
1764 *hca = *req_hca;
1765
Peng Taod7e09d02013-05-02 16:46:55 +08001766out:
1767 ptlrpc_req_finished(req);
1768 return rc;
1769}
1770
1771static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1772{
1773 struct ptlrpc_request *req;
1774 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001775
1776 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1777 LUSTRE_MDS_VERSION,
1778 MDS_HSM_CT_UNREGISTER);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001779 if (!req) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001780 rc = -ENOMEM;
1781 goto out;
1782 }
Peng Taod7e09d02013-05-02 16:46:55 +08001783
Fan Yong341f1f02016-06-19 22:53:53 -04001784 mdc_pack_body(req, NULL, 0, 0, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001785
1786 ptlrpc_request_set_replen(req);
1787
1788 rc = mdc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08001789out:
1790 ptlrpc_req_finished(req);
1791 return rc;
1792}
1793
1794static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1795 struct md_op_data *op_data)
1796{
1797 struct hsm_user_state *hus = op_data->op_data;
1798 struct hsm_user_state *req_hus;
1799 struct ptlrpc_request *req;
1800 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001801
1802 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1803 &RQF_MDS_HSM_STATE_GET);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001804 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001805 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001806
Peng Taod7e09d02013-05-02 16:46:55 +08001807 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1808 if (rc != 0) {
1809 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001810 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001811 }
1812
Fan Yong341f1f02016-06-19 22:53:53 -04001813 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001814 op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001815
1816 ptlrpc_request_set_replen(req);
1817
1818 rc = mdc_queue_wait(req);
1819 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02001820 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001821
1822 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001823 if (!req_hus) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001824 rc = -EPROTO;
1825 goto out;
1826 }
Peng Taod7e09d02013-05-02 16:46:55 +08001827
1828 *hus = *req_hus;
1829
Peng Taod7e09d02013-05-02 16:46:55 +08001830out:
1831 ptlrpc_req_finished(req);
1832 return rc;
1833}
1834
1835static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1836 struct md_op_data *op_data)
1837{
1838 struct hsm_state_set *hss = op_data->op_data;
1839 struct hsm_state_set *req_hss;
1840 struct ptlrpc_request *req;
1841 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001842
1843 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1844 &RQF_MDS_HSM_STATE_SET);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001845 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001846 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001847
Peng Taod7e09d02013-05-02 16:46:55 +08001848 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1849 if (rc) {
1850 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001851 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001852 }
1853
Fan Yong341f1f02016-06-19 22:53:53 -04001854 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
Oleg Drokinef2e0f52015-09-27 16:45:46 -04001855 op_data->op_suppgids[0], 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001856
1857 /* Copy states */
1858 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001859 if (!req_hss) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001860 rc = -EPROTO;
1861 goto out;
1862 }
Peng Taod7e09d02013-05-02 16:46:55 +08001863 *req_hss = *hss;
1864
1865 ptlrpc_request_set_replen(req);
1866
1867 rc = mdc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08001868out:
1869 ptlrpc_req_finished(req);
1870 return rc;
1871}
1872
1873static int mdc_ioc_hsm_request(struct obd_export *exp,
1874 struct hsm_user_request *hur)
1875{
1876 struct obd_import *imp = class_exp2cliimp(exp);
1877 struct ptlrpc_request *req;
1878 struct hsm_request *req_hr;
1879 struct hsm_user_item *req_hui;
1880 char *req_opaque;
1881 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001882
1883 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001884 if (!req) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001885 rc = -ENOMEM;
1886 goto out;
1887 }
Peng Taod7e09d02013-05-02 16:46:55 +08001888
1889 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1890 hur->hur_request.hr_itemcount
1891 * sizeof(struct hsm_user_item));
1892 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1893 hur->hur_request.hr_data_len);
1894
1895 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1896 if (rc) {
1897 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001898 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001899 }
1900
Fan Yong341f1f02016-06-19 22:53:53 -04001901 mdc_pack_body(req, NULL, 0, 0, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08001902
1903 /* Copy hsm_request struct */
1904 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001905 if (!req_hr) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001906 rc = -EPROTO;
1907 goto out;
1908 }
Peng Taod7e09d02013-05-02 16:46:55 +08001909 *req_hr = hur->hur_request;
1910
1911 /* Copy hsm_user_item structs */
1912 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001913 if (!req_hui) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001914 rc = -EPROTO;
1915 goto out;
1916 }
Peng Taod7e09d02013-05-02 16:46:55 +08001917 memcpy(req_hui, hur->hur_user_item,
1918 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1919
1920 /* Copy opaque field */
1921 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05001922 if (!req_opaque) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02001923 rc = -EPROTO;
1924 goto out;
1925 }
Peng Taod7e09d02013-05-02 16:46:55 +08001926 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1927
1928 ptlrpc_request_set_replen(req);
1929
1930 rc = mdc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08001931out:
1932 ptlrpc_req_finished(req);
1933 return rc;
1934}
1935
1936static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1937{
1938 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1939
Oleg Drokin18e042f2014-01-23 23:45:07 -05001940 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
Peng Taod7e09d02013-05-02 16:46:55 +08001941
1942 lh->kuc_magic = KUC_MAGIC;
1943 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1944 lh->kuc_flags = flags;
1945 lh->kuc_msgtype = CL_RECORD;
1946 lh->kuc_msglen = len;
1947 return lh;
1948}
1949
1950#define D_CHANGELOG 0
1951
1952struct changelog_show {
1953 __u64 cs_startrec;
1954 __u32 cs_flags;
1955 struct file *cs_fp;
1956 char *cs_buf;
1957 struct obd_device *cs_obd;
1958};
1959
Andreas Dilgere3779882013-06-03 21:40:52 +08001960static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
Peng Taod7e09d02013-05-02 16:46:55 +08001961 struct llog_rec_hdr *hdr, void *data)
1962{
1963 struct changelog_show *cs = data;
1964 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1965 struct kuc_hdr *lh;
1966 int len, rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001967
Andreas Dilgere3779882013-06-03 21:40:52 +08001968 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1969 rc = -EINVAL;
1970 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1971 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1972 rec->cr.cr_type, rc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001973 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001974 }
1975
1976 if (rec->cr.cr_index < cs->cs_startrec) {
1977 /* Skip entries earlier than what we are interested in */
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001978 CDEBUG(D_CHANGELOG, "rec=%llu start=%llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001979 rec->cr.cr_index, cs->cs_startrec);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001980 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001981 }
1982
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07001983 CDEBUG(D_CHANGELOG, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID
Peng Taod7e09d02013-05-02 16:46:55 +08001984 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1985 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1986 rec->cr.cr_flags & CLF_FLAGMASK,
1987 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1988 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1989
1990 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1991
1992 /* Set up the message */
1993 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1994 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1995
1996 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
Srikrishan Malik301af902014-08-11 23:57:31 +05301997 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len, rc);
Peng Taod7e09d02013-05-02 16:46:55 +08001998
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001999 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002000}
2001
2002static int mdc_changelog_send_thread(void *csdata)
2003{
2004 struct changelog_show *cs = csdata;
2005 struct llog_ctxt *ctxt = NULL;
2006 struct llog_handle *llh = NULL;
2007 struct kuc_hdr *kuch;
2008 int rc;
2009
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07002010 CDEBUG(D_CHANGELOG, "changelog to fp=%p start %llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +08002011 cs->cs_fp, cs->cs_startrec);
2012
Julia Lawall7b817792015-05-01 17:51:17 +02002013 cs->cs_buf = kzalloc(KUC_CHANGELOG_MSG_MAXSIZE, GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02002014 if (!cs->cs_buf) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02002015 rc = -ENOMEM;
2016 goto out;
2017 }
Peng Taod7e09d02013-05-02 16:46:55 +08002018
2019 /* Set up the remote catalog handle */
2020 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05002021 if (!ctxt) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02002022 rc = -ENOENT;
2023 goto out;
2024 }
Peng Taod7e09d02013-05-02 16:46:55 +08002025 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
2026 LLOG_OPEN_EXISTS);
2027 if (rc) {
2028 CERROR("%s: fail to open changelog catalog: rc = %d\n",
2029 cs->cs_obd->obd_name, rc);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002030 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002031 }
2032 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
2033 if (rc) {
2034 CERROR("llog_init_handle failed %d\n", rc);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002035 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002036 }
2037
Andreas Dilgere3779882013-06-03 21:40:52 +08002038 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08002039
2040 /* Send EOF no matter what our result */
Chi Pham4a87df32014-03-09 12:51:19 +01002041 kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
2042 if (kuch) {
Peng Taod7e09d02013-05-02 16:46:55 +08002043 kuch->kuc_msgtype = CL_EOF;
2044 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
2045 }
2046
2047out:
2048 fput(cs->cs_fp);
2049 if (llh)
2050 llog_cat_close(NULL, llh);
2051 if (ctxt)
2052 llog_ctxt_put(ctxt);
Julia Lawall6fa7cbab32015-05-01 21:37:56 +02002053 kfree(cs->cs_buf);
Julia Lawall7b817792015-05-01 17:51:17 +02002054 kfree(cs);
Peng Taod7e09d02013-05-02 16:46:55 +08002055 return rc;
2056}
2057
2058static int mdc_ioc_changelog_send(struct obd_device *obd,
2059 struct ioc_changelog *icc)
2060{
2061 struct changelog_show *cs;
John L. Hammond060c2822016-02-15 10:25:49 -05002062 struct task_struct *task;
Peng Taod7e09d02013-05-02 16:46:55 +08002063 int rc;
2064
2065 /* Freed in mdc_changelog_send_thread */
Julia Lawall7b817792015-05-01 17:51:17 +02002066 cs = kzalloc(sizeof(*cs), GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +08002067 if (!cs)
2068 return -ENOMEM;
2069
2070 cs->cs_obd = obd;
2071 cs->cs_startrec = icc->icc_recno;
2072 /* matching fput in mdc_changelog_send_thread */
2073 cs->cs_fp = fget(icc->icc_id);
2074 cs->cs_flags = icc->icc_flags;
2075
2076 /*
2077 * New thread because we should return to user app before
2078 * writing into our pipe
2079 */
John L. Hammond060c2822016-02-15 10:25:49 -05002080 task = kthread_run(mdc_changelog_send_thread, cs,
2081 "mdc_clg_send_thread");
2082 if (IS_ERR(task)) {
2083 rc = PTR_ERR(task);
2084 CERROR("%s: can't start changelog thread: rc = %d\n",
2085 obd->obd_name, rc);
2086 kfree(cs);
2087 } else {
2088 rc = 0;
2089 CDEBUG(D_CHANGELOG, "%s: started changelog thread\n",
2090 obd->obd_name);
Peng Taod7e09d02013-05-02 16:46:55 +08002091 }
2092
2093 CERROR("Failed to start changelog thread: %d\n", rc);
Peng Taod7e09d02013-05-02 16:46:55 +08002094 return rc;
2095}
2096
2097static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2098 struct lustre_kernelcomm *lk);
2099
2100static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
2101 struct obd_quotactl *oqctl)
2102{
2103 struct client_obd *cli = &exp->exp_obd->u.cli;
2104 struct ptlrpc_request *req;
2105 struct obd_quotactl *body;
2106 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002107
2108 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2109 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
2110 MDS_QUOTACHECK);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05002111 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002112 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002113
2114 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2115 *body = *oqctl;
2116
2117 ptlrpc_request_set_replen(req);
2118
2119 /* the next poll will find -ENODATA, that means quotacheck is
Oleg Drokin1df232e2016-02-24 22:00:33 -05002120 * going on
2121 */
Peng Taod7e09d02013-05-02 16:46:55 +08002122 cli->cl_qchk_stat = -ENODATA;
2123 rc = ptlrpc_queue_wait(req);
2124 if (rc)
2125 cli->cl_qchk_stat = rc;
2126 ptlrpc_req_finished(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002127 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002128}
2129
2130static int mdc_quota_poll_check(struct obd_export *exp,
2131 struct if_quotacheck *qchk)
2132{
2133 struct client_obd *cli = &exp->exp_obd->u.cli;
2134 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002135
2136 qchk->obd_uuid = cli->cl_target_uuid;
2137 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
2138
2139 rc = cli->cl_qchk_stat;
2140 /* the client is not the previous one */
2141 if (rc == CL_NOT_QUOTACHECKED)
2142 rc = -EINTR;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002143 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002144}
2145
2146static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2147 struct obd_quotactl *oqctl)
2148{
2149 struct ptlrpc_request *req;
2150 struct obd_quotactl *oqc;
2151 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002152
2153 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2154 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
2155 MDS_QUOTACTL);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05002156 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002157 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002158
2159 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2160 *oqc = *oqctl;
2161
2162 ptlrpc_request_set_replen(req);
2163 ptlrpc_at_set_req_timeout(req);
2164 req->rq_no_resend = 1;
2165
2166 rc = ptlrpc_queue_wait(req);
2167 if (rc)
2168 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
2169
Chi Pham4a87df32014-03-09 12:51:19 +01002170 if (req->rq_repmsg) {
2171 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2172 if (oqc) {
2173 *oqctl = *oqc;
2174 } else if (!rc) {
Srikrishan Malikffdac6ce2014-08-11 23:57:27 +05302175 CERROR("Can't unpack obd_quotactl\n");
Chi Pham4a87df32014-03-09 12:51:19 +01002176 rc = -EPROTO;
2177 }
Peng Taod7e09d02013-05-02 16:46:55 +08002178 } else if (!rc) {
Chi Pham4a87df32014-03-09 12:51:19 +01002179 CERROR("Can't unpack obd_quotactl\n");
Peng Taod7e09d02013-05-02 16:46:55 +08002180 rc = -EPROTO;
2181 }
2182 ptlrpc_req_finished(req);
2183
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002184 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002185}
2186
2187static int mdc_ioc_swap_layouts(struct obd_export *exp,
2188 struct md_op_data *op_data)
2189{
2190 LIST_HEAD(cancels);
2191 struct ptlrpc_request *req;
2192 int rc, count;
2193 struct mdc_swap_layouts *msl, *payload;
Peng Taod7e09d02013-05-02 16:46:55 +08002194
2195 msl = op_data->op_data;
2196
2197 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2198 * first thing it will do is to cancel the 2 layout
2199 * locks hold by this client.
2200 * So the client must cancel its layout locks on the 2 fids
2201 * with the request RPC to avoid extra RPC round trips
2202 */
2203 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
John L. Hammond29792c82016-08-16 16:18:36 -04002204 LCK_CR, MDS_INODELOCK_LAYOUT |
2205 MDS_INODELOCK_XATTR);
Peng Taod7e09d02013-05-02 16:46:55 +08002206 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
John L. Hammond29792c82016-08-16 16:18:36 -04002207 LCK_CR, MDS_INODELOCK_LAYOUT |
2208 MDS_INODELOCK_XATTR);
Peng Taod7e09d02013-05-02 16:46:55 +08002209
2210 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2211 &RQF_MDS_SWAP_LAYOUTS);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05002212 if (!req) {
Peng Taod7e09d02013-05-02 16:46:55 +08002213 ldlm_lock_list_put(&cancels, l_bl_ast, count);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002214 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002215 }
2216
Peng Taod7e09d02013-05-02 16:46:55 +08002217 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2218 if (rc) {
2219 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002220 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002221 }
2222
2223 mdc_swap_layouts_pack(req, op_data);
2224
2225 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2226 LASSERT(payload);
2227
2228 *payload = *msl;
2229
2230 ptlrpc_request_set_replen(req);
2231
2232 rc = ptlrpc_queue_wait(req);
Peng Taod7e09d02013-05-02 16:46:55 +08002233
Peng Taod7e09d02013-05-02 16:46:55 +08002234 ptlrpc_req_finished(req);
2235 return rc;
2236}
2237
2238static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
Oleg Drokine09bee32016-01-03 12:05:43 -05002239 void *karg, void __user *uarg)
Peng Taod7e09d02013-05-02 16:46:55 +08002240{
2241 struct obd_device *obd = exp->exp_obd;
2242 struct obd_ioctl_data *data = karg;
2243 struct obd_import *imp = obd->u.cli.cl_import;
Peng Taod7e09d02013-05-02 16:46:55 +08002244 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002245
2246 if (!try_module_get(THIS_MODULE)) {
James Nunez19b20562016-03-07 18:10:21 -05002247 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2248 module_name(THIS_MODULE));
Peng Taod7e09d02013-05-02 16:46:55 +08002249 return -EINVAL;
2250 }
2251 switch (cmd) {
2252 case OBD_IOC_CHANGELOG_SEND:
2253 rc = mdc_ioc_changelog_send(obd, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002254 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002255 case OBD_IOC_CHANGELOG_CLEAR: {
2256 struct ioc_changelog *icc = karg;
Srikrishan Malik1a4cd3e2014-08-11 23:57:36 +05302257 struct changelog_setinfo cs = {
2258 .cs_recno = icc->icc_recno,
2259 .cs_id = icc->icc_id
2260 };
2261
Peng Taod7e09d02013-05-02 16:46:55 +08002262 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
2263 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
2264 NULL);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002265 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002266 }
2267 case OBD_IOC_FID2PATH:
2268 rc = mdc_ioc_fid2path(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002269 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002270 case LL_IOC_HSM_CT_START:
2271 rc = mdc_ioc_hsm_ct_start(exp, karg);
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002272 /* ignore if it was already registered on this MDS. */
2273 if (rc == -EEXIST)
2274 rc = 0;
Julia Lawalld5fdc202014-08-28 12:10:35 +02002275 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002276 case LL_IOC_HSM_PROGRESS:
2277 rc = mdc_ioc_hsm_progress(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002278 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002279 case LL_IOC_HSM_STATE_GET:
2280 rc = mdc_ioc_hsm_state_get(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002281 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002282 case LL_IOC_HSM_STATE_SET:
2283 rc = mdc_ioc_hsm_state_set(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002284 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002285 case LL_IOC_HSM_ACTION:
2286 rc = mdc_ioc_hsm_current_action(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002287 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002288 case LL_IOC_HSM_REQUEST:
2289 rc = mdc_ioc_hsm_request(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002290 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002291 case OBD_IOC_CLIENT_RECOVER:
2292 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2293 if (rc < 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +02002294 goto out;
2295 rc = 0;
2296 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002297 case IOC_OSC_SET_ACTIVE:
2298 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002299 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002300 case OBD_IOC_POLL_QUOTACHECK:
2301 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002302 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002303 case OBD_IOC_PING_TARGET:
2304 rc = ptlrpc_obd_ping(obd);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002305 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002306 /*
2307 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2308 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2309 * there'd be no LMV layer thus we might be called here. Eventually
2310 * this code should be removed.
2311 * bz20731, LU-592.
2312 */
2313 case IOC_OBD_STATFS: {
2314 struct obd_statfs stat_buf = {0};
2315
Oleg Drokin9797fb02016-06-18 23:53:12 -04002316 if (*((__u32 *)data->ioc_inlbuf2) != 0) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02002317 rc = -ENODEV;
2318 goto out;
2319 }
Peng Taod7e09d02013-05-02 16:46:55 +08002320
2321 /* copy UUID */
2322 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
Dan Carpenteref4356bf2015-01-06 12:57:08 +03002323 min_t(size_t, data->ioc_plen2,
Oleg Drokin22e0bc62016-02-26 01:50:06 -05002324 sizeof(struct obd_uuid)))) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02002325 rc = -EFAULT;
2326 goto out;
2327 }
Peng Taod7e09d02013-05-02 16:46:55 +08002328
2329 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2330 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2331 0);
2332 if (rc != 0)
Julia Lawalld5fdc202014-08-28 12:10:35 +02002333 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002334
2335 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
Dan Carpenteref4356bf2015-01-06 12:57:08 +03002336 min_t(size_t, data->ioc_plen1,
Oleg Drokin22e0bc62016-02-26 01:50:06 -05002337 sizeof(stat_buf)))) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02002338 rc = -EFAULT;
2339 goto out;
2340 }
Peng Taod7e09d02013-05-02 16:46:55 +08002341
Julia Lawalld5fdc202014-08-28 12:10:35 +02002342 rc = 0;
2343 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002344 }
2345 case OBD_IOC_QUOTACTL: {
2346 struct if_quotactl *qctl = karg;
2347 struct obd_quotactl *oqctl;
2348
Julia Lawall7b817792015-05-01 17:51:17 +02002349 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
Julia Lawallbb144d02015-06-20 18:59:05 +02002350 if (!oqctl) {
Julia Lawalld5fdc202014-08-28 12:10:35 +02002351 rc = -ENOMEM;
2352 goto out;
2353 }
Peng Taod7e09d02013-05-02 16:46:55 +08002354
2355 QCTL_COPY(oqctl, qctl);
2356 rc = obd_quotactl(exp, oqctl);
2357 if (rc == 0) {
2358 QCTL_COPY(qctl, oqctl);
2359 qctl->qc_valid = QC_MDTIDX;
2360 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2361 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08002362
Julia Lawall7b817792015-05-01 17:51:17 +02002363 kfree(oqctl);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002364 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002365 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08002366 case LL_IOC_GET_CONNECT_FLAGS:
2367 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
Julia Lawalld5fdc202014-08-28 12:10:35 +02002368 sizeof(*exp_connect_flags_ptr(exp)))) {
2369 rc = -EFAULT;
2370 goto out;
2371 }
John L. Hammondc1f3d682013-11-26 10:04:59 +08002372
Julia Lawalld5fdc202014-08-28 12:10:35 +02002373 rc = 0;
2374 goto out;
John L. Hammondc1f3d682013-11-26 10:04:59 +08002375 case LL_IOC_LOV_SWAP_LAYOUTS:
Peng Taod7e09d02013-05-02 16:46:55 +08002376 rc = mdc_ioc_swap_layouts(exp, karg);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002377 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002378 default:
John L. Hammondc1f3d682013-11-26 10:04:59 +08002379 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002380 rc = -ENOTTY;
2381 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002382 }
2383out:
2384 module_put(THIS_MODULE);
2385
2386 return rc;
2387}
2388
Luca Ceresolif6219c12015-01-18 15:06:47 +01002389static int mdc_get_info_rpc(struct obd_export *exp,
2390 u32 keylen, void *key,
2391 int vallen, void *val)
Peng Taod7e09d02013-05-02 16:46:55 +08002392{
2393 struct obd_import *imp = class_exp2cliimp(exp);
2394 struct ptlrpc_request *req;
2395 char *tmp;
2396 int rc = -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002397
2398 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05002399 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002400 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002401
2402 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2403 RCL_CLIENT, keylen);
2404 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2405 RCL_CLIENT, sizeof(__u32));
2406
2407 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2408 if (rc) {
2409 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002410 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002411 }
2412
2413 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2414 memcpy(tmp, key, keylen);
2415 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2416 memcpy(tmp, &vallen, sizeof(__u32));
2417
2418 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2419 RCL_SERVER, vallen);
2420 ptlrpc_request_set_replen(req);
2421
2422 rc = ptlrpc_queue_wait(req);
2423 /* -EREMOTE means the get_info result is partial, and it needs to
Oleg Drokin1df232e2016-02-24 22:00:33 -05002424 * continue on another MDT, see fid2path part in lmv_iocontrol
2425 */
Peng Taod7e09d02013-05-02 16:46:55 +08002426 if (rc == 0 || rc == -EREMOTE) {
2427 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2428 memcpy(val, tmp, vallen);
2429 if (ptlrpc_rep_need_swab(req)) {
2430 if (KEY_IS(KEY_FID2PATH))
2431 lustre_swab_fid2path(val);
2432 }
2433 }
2434 ptlrpc_req_finished(req);
2435
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002436 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002437}
2438
2439static void lustre_swab_hai(struct hsm_action_item *h)
2440{
2441 __swab32s(&h->hai_len);
2442 __swab32s(&h->hai_action);
2443 lustre_swab_lu_fid(&h->hai_fid);
2444 lustre_swab_lu_fid(&h->hai_dfid);
2445 __swab64s(&h->hai_cookie);
2446 __swab64s(&h->hai_extent.offset);
2447 __swab64s(&h->hai_extent.length);
2448 __swab64s(&h->hai_gid);
2449}
2450
2451static void lustre_swab_hal(struct hsm_action_list *h)
2452{
2453 struct hsm_action_item *hai;
2454 int i;
2455
2456 __swab32s(&h->hal_version);
2457 __swab32s(&h->hal_count);
2458 __swab32s(&h->hal_archive_id);
2459 __swab64s(&h->hal_flags);
JC Lafoucriere18ecab02016-04-04 21:36:52 -04002460 hai = hai_first(h);
JC Lafoucriere18dfaeb2013-11-26 10:05:01 +08002461 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
Peng Taod7e09d02013-05-02 16:46:55 +08002462 lustre_swab_hai(hai);
Peng Taod7e09d02013-05-02 16:46:55 +08002463}
2464
2465static void lustre_swab_kuch(struct kuc_hdr *l)
2466{
2467 __swab16s(&l->kuc_magic);
2468 /* __u8 l->kuc_transport */
2469 __swab16s(&l->kuc_msgtype);
2470 __swab16s(&l->kuc_msglen);
2471}
2472
2473static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2474 struct lustre_kernelcomm *lk)
2475{
2476 struct obd_import *imp = class_exp2cliimp(exp);
2477 __u32 archive = lk->lk_data;
2478 int rc = 0;
2479
2480 if (lk->lk_group != KUC_GRP_HSM) {
2481 CERROR("Bad copytool group %d\n", lk->lk_group);
2482 return -EINVAL;
2483 }
2484
2485 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2486 lk->lk_uid, lk->lk_group, lk->lk_flags);
2487
2488 if (lk->lk_flags & LK_FLG_STOP) {
Peng Taod7e09d02013-05-02 16:46:55 +08002489 /* Unregister with the coordinator */
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002490 rc = mdc_ioc_hsm_ct_unregister(imp);
Peng Taod7e09d02013-05-02 16:46:55 +08002491 } else {
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002492 rc = mdc_ioc_hsm_ct_register(imp, archive);
Peng Taod7e09d02013-05-02 16:46:55 +08002493 }
2494
2495 return rc;
2496}
2497
2498/**
2499 * Send a message to any listening copytools
2500 * @param val KUC message (kuc_hdr + hsm_action_list)
2501 * @param len total length of message
2502 */
2503static int mdc_hsm_copytool_send(int len, void *val)
2504{
2505 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2506 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
Peng Taod7e09d02013-05-02 16:46:55 +08002507
2508 if (len < sizeof(*lh) + sizeof(*hal)) {
2509 CERROR("Short HSM message %d < %d\n", len,
Oleg Drokin9797fb02016-06-18 23:53:12 -04002510 (int)(sizeof(*lh) + sizeof(*hal)));
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002511 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08002512 }
2513 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2514 lustre_swab_kuch(lh);
2515 lustre_swab_hal(hal);
2516 } else if (lh->kuc_magic != KUC_MAGIC) {
2517 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002518 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08002519 }
2520
Srikrishan Malikee990b32014-08-13 19:31:16 +05302521 CDEBUG(D_HSM,
2522 "Received message mg=%x t=%d m=%d l=%d actions=%d on %s\n",
Peng Taod7e09d02013-05-02 16:46:55 +08002523 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2524 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2525
2526 /* Broadcast to HSM listeners */
Prasanna Karthikda2a7272015-06-16 13:42:02 +00002527 return libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
Peng Taod7e09d02013-05-02 16:46:55 +08002528}
2529
2530/**
2531 * callback function passed to kuc for re-registering each HSM copytool
2532 * running on MDC, after MDT shutdown/recovery.
Henri Doreau406fc912015-12-23 16:24:41 -05002533 * @param data copytool registration data
Peng Taod7e09d02013-05-02 16:46:55 +08002534 * @param cb_arg callback argument (obd_import)
2535 */
Henri Doreau406fc912015-12-23 16:24:41 -05002536static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
Peng Taod7e09d02013-05-02 16:46:55 +08002537{
Henri Doreau406fc912015-12-23 16:24:41 -05002538 struct kkuc_ct_data *kcd = data;
Peng Taod7e09d02013-05-02 16:46:55 +08002539 struct obd_import *imp = (struct obd_import *)cb_arg;
Peng Taod7e09d02013-05-02 16:46:55 +08002540 int rc;
2541
Henri Doreau406fc912015-12-23 16:24:41 -05002542 if (!kcd || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2543 return -EPROTO;
2544
2545 if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2546 return 0;
2547
2548 CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2549 imp->imp_obd->obd_name, kcd->kcd_archive);
2550 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
Peng Taod7e09d02013-05-02 16:46:55 +08002551
2552 /* ignore error if the copytool is already registered */
Henri Doreau406fc912015-12-23 16:24:41 -05002553 return (rc == -EEXIST) ? 0 : rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002554}
2555
Luca Ceresolif6219c12015-01-18 15:06:47 +01002556static int mdc_set_info_async(const struct lu_env *env,
2557 struct obd_export *exp,
2558 u32 keylen, void *key,
2559 u32 vallen, void *val,
2560 struct ptlrpc_request_set *set)
Peng Taod7e09d02013-05-02 16:46:55 +08002561{
2562 struct obd_import *imp = class_exp2cliimp(exp);
2563 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002564
2565 if (KEY_IS(KEY_READ_ONLY)) {
2566 if (vallen != sizeof(int))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002567 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002568
2569 spin_lock(&imp->imp_lock);
2570 if (*((int *)val)) {
2571 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2572 imp->imp_connect_data.ocd_connect_flags |=
2573 OBD_CONNECT_RDONLY;
2574 } else {
2575 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2576 imp->imp_connect_data.ocd_connect_flags &=
2577 ~OBD_CONNECT_RDONLY;
2578 }
2579 spin_unlock(&imp->imp_lock);
2580
2581 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2582 keylen, key, vallen, val, set);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002583 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002584 }
2585 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2586 sptlrpc_conf_client_adapt(exp->exp_obd);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002587 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002588 }
2589 if (KEY_IS(KEY_FLUSH_CTX)) {
2590 sptlrpc_import_flush_my_ctx(imp);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002591 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002592 }
Peng Taod7e09d02013-05-02 16:46:55 +08002593 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2594 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2595 keylen, key, vallen, val, set);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002596 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002597 }
2598 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2599 rc = mdc_hsm_copytool_send(vallen, val);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002600 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002601 }
2602
2603 CERROR("Unknown key %s\n", (char *)key);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002604 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002605}
2606
Luca Ceresolif6219c12015-01-18 15:06:47 +01002607static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2608 __u32 keylen, void *key, __u32 *vallen, void *val,
2609 struct lov_stripe_md *lsm)
Peng Taod7e09d02013-05-02 16:46:55 +08002610{
2611 int rc = -EINVAL;
2612
2613 if (KEY_IS(KEY_MAX_EASIZE)) {
2614 int mdsize, *max_easize;
2615
2616 if (*vallen != sizeof(int))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002617 return -EINVAL;
Brian Behlendorf44779342014-04-27 13:06:47 -04002618 mdsize = *(int *)val;
Peng Taod7e09d02013-05-02 16:46:55 +08002619 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2620 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2621 max_easize = val;
2622 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002623 return 0;
Brian Behlendorf44779342014-04-27 13:06:47 -04002624 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2625 int *default_easize;
2626
2627 if (*vallen != sizeof(int))
2628 return -EINVAL;
2629 default_easize = val;
2630 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2631 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002632 } else if (KEY_IS(KEY_CONN_DATA)) {
2633 struct obd_import *imp = class_exp2cliimp(exp);
2634 struct obd_connect_data *data = val;
2635
2636 if (*vallen != sizeof(*data))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002637 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002638
2639 *data = imp->imp_connect_data;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002640 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002641 } else if (KEY_IS(KEY_TGT_COUNT)) {
2642 *((int *)val) = 1;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002643 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002644 }
2645
2646 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2647
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002648 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002649}
2650
Luca Ceresolif6219c12015-01-18 15:06:47 +01002651static int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
Oleg Drokinef2e0f52015-09-27 16:45:46 -04002652 struct ptlrpc_request **request)
Peng Taod7e09d02013-05-02 16:46:55 +08002653{
2654 struct ptlrpc_request *req;
2655 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002656
2657 *request = NULL;
2658 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
Oleg Drokin34e3ff92016-02-16 00:46:53 -05002659 if (!req)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002660 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002661
Peng Taod7e09d02013-05-02 16:46:55 +08002662 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2663 if (rc) {
2664 ptlrpc_request_free(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002665 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002666 }
2667
Oleg Drokinef2e0f52015-09-27 16:45:46 -04002668 mdc_pack_body(req, fid, 0, 0, -1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08002669
2670 ptlrpc_request_set_replen(req);
2671
2672 rc = ptlrpc_queue_wait(req);
2673 if (rc)
2674 ptlrpc_req_finished(req);
2675 else
2676 *request = req;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002677 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002678}
2679
2680static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2681 enum obd_import_event event)
2682{
2683 int rc = 0;
2684
2685 LASSERT(imp->imp_obd == obd);
2686
2687 switch (event) {
2688 case IMP_EVENT_DISCON: {
2689#if 0
2690 /* XXX Pass event up to OBDs stack. used only for FLD now */
2691 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2692#endif
2693 break;
2694 }
2695 case IMP_EVENT_INACTIVE: {
2696 struct client_obd *cli = &obd->u.cli;
2697 /*
2698 * Flush current sequence to make client obtain new one
2699 * from server in case of disconnect/reconnect.
2700 */
Oleg Drokin34e3ff92016-02-16 00:46:53 -05002701 if (cli->cl_seq)
Peng Taod7e09d02013-05-02 16:46:55 +08002702 seq_client_flush(cli->cl_seq);
2703
2704 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2705 break;
2706 }
2707 case IMP_EVENT_INVALIDATE: {
2708 struct ldlm_namespace *ns = obd->obd_namespace;
2709
2710 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2711
2712 break;
2713 }
2714 case IMP_EVENT_ACTIVE:
2715 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
Thomas Leibovici78eb90922013-07-25 01:17:24 +08002716 /* redo the kuc registration after reconnecting */
Peng Taod7e09d02013-05-02 16:46:55 +08002717 if (rc == 0)
Shivani Bhardwajdd40ca42015-11-08 22:22:58 +05302718 /* re-register HSM agents */
2719 rc = libcfs_kkuc_group_foreach(KUC_GRP_HSM,
2720 mdc_hsm_ct_reregister,
2721 (void *)imp);
Peng Taod7e09d02013-05-02 16:46:55 +08002722 break;
2723 case IMP_EVENT_OCD:
2724 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2725 break;
2726 case IMP_EVENT_DEACTIVATE:
2727 case IMP_EVENT_ACTIVATE:
2728 break;
2729 default:
2730 CERROR("Unknown import event %x\n", event);
2731 LBUG();
2732 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002733 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002734}
2735
wang di8f18c8a2016-08-16 16:18:54 -04002736int mdc_fid_alloc(const struct lu_env *env, struct obd_export *exp,
2737 struct lu_fid *fid, struct md_op_data *op_data)
Peng Taod7e09d02013-05-02 16:46:55 +08002738{
2739 struct client_obd *cli = &exp->exp_obd->u.cli;
2740 struct lu_client_seq *seq = cli->cl_seq;
Greg Kroah-Hartman29aaf492013-08-02 18:14:51 +08002741
wang di8f18c8a2016-08-16 16:18:54 -04002742 return seq_client_alloc_fid(env, seq, fid);
Peng Taod7e09d02013-05-02 16:46:55 +08002743}
2744
Luca Ceresolif6219c12015-01-18 15:06:47 +01002745static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
Srikrishan Malik982ec912014-08-11 23:57:29 +05302746{
Peng Taod7e09d02013-05-02 16:46:55 +08002747 struct client_obd *cli = &exp->exp_obd->u.cli;
Srikrishan Malik7436d072014-08-11 23:57:33 +05302748
Peng Taod7e09d02013-05-02 16:46:55 +08002749 return &cli->cl_target_uuid;
2750}
2751
2752/**
2753 * Determine whether the lock can be canceled before replaying it during
2754 * recovery, non zero value will be return if the lock can be canceled,
2755 * or zero returned for not
2756 */
Jinshan Xiong7d443332016-03-30 19:49:02 -04002757static int mdc_cancel_weight(struct ldlm_lock *lock)
Peng Taod7e09d02013-05-02 16:46:55 +08002758{
2759 if (lock->l_resource->lr_type != LDLM_IBITS)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002760 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002761
2762 /* FIXME: if we ever get into a situation where there are too many
2763 * opened files with open locks on a single node, then we really
Oleg Drokin1df232e2016-02-24 22:00:33 -05002764 * should replay these open locks to reget it
2765 */
Peng Taod7e09d02013-05-02 16:46:55 +08002766 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002767 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002768
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002769 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08002770}
2771
2772static int mdc_resource_inode_free(struct ldlm_resource *res)
2773{
2774 if (res->lr_lvb_inode)
2775 res->lr_lvb_inode = NULL;
2776
2777 return 0;
2778}
2779
Luca Ceresolif6219c12015-01-18 15:06:47 +01002780static struct ldlm_valblock_ops inode_lvbo = {
Emil Goode805e5172013-07-28 00:38:56 +02002781 .lvbo_free = mdc_resource_inode_free,
Peng Taod7e09d02013-05-02 16:46:55 +08002782};
2783
John L. Hammond903af112014-09-05 15:08:12 -05002784static int mdc_llog_init(struct obd_device *obd)
2785{
2786 struct obd_llog_group *olg = &obd->obd_olg;
2787 struct llog_ctxt *ctxt;
2788 int rc;
2789
2790 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2791 &llog_client_ops);
2792 if (rc)
2793 return rc;
2794
2795 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2796 llog_initiator_connect(ctxt);
2797 llog_ctxt_put(ctxt);
2798
2799 return 0;
2800}
2801
2802static void mdc_llog_finish(struct obd_device *obd)
2803{
2804 struct llog_ctxt *ctxt;
2805
2806 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2807 if (ctxt)
2808 llog_cleanup(NULL, ctxt);
2809}
2810
Peng Taod7e09d02013-05-02 16:46:55 +08002811static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2812{
2813 struct client_obd *cli = &obd->u.cli;
Radek Dostalea7893b2014-07-27 23:22:57 +02002814 struct lprocfs_static_vars lvars = { NULL };
Peng Taod7e09d02013-05-02 16:46:55 +08002815 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002816
Julia Lawall7b817792015-05-01 17:51:17 +02002817 cli->cl_rpc_lock = kzalloc(sizeof(*cli->cl_rpc_lock), GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +08002818 if (!cli->cl_rpc_lock)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002819 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002820 mdc_init_rpc_lock(cli->cl_rpc_lock);
2821
Swapnil Pimpalecc3b7752016-04-12 16:14:09 -04002822 rc = ptlrpcd_addref();
2823 if (rc < 0)
2824 goto err_rpc_lock;
Peng Taod7e09d02013-05-02 16:46:55 +08002825
Julia Lawall7b817792015-05-01 17:51:17 +02002826 cli->cl_close_lock = kzalloc(sizeof(*cli->cl_close_lock), GFP_NOFS);
Julia Lawalld5fdc202014-08-28 12:10:35 +02002827 if (!cli->cl_close_lock) {
2828 rc = -ENOMEM;
Swapnil Pimpalecc3b7752016-04-12 16:14:09 -04002829 goto err_ptlrpcd_decref;
Julia Lawalld5fdc202014-08-28 12:10:35 +02002830 }
Peng Taod7e09d02013-05-02 16:46:55 +08002831 mdc_init_rpc_lock(cli->cl_close_lock);
2832
2833 rc = client_obd_setup(obd, cfg);
2834 if (rc)
Julia Lawalld5fdc202014-08-28 12:10:35 +02002835 goto err_close_lock;
Peng Taod7e09d02013-05-02 16:46:55 +08002836 lprocfs_mdc_init_vars(&lvars);
Oleg Drokin9b801302015-05-21 15:32:16 -04002837 lprocfs_obd_setup(obd, lvars.obd_vars, lvars.sysfs_vars);
Peng Taod7e09d02013-05-02 16:46:55 +08002838 sptlrpc_lprocfs_cliobd_attach(obd);
2839 ptlrpc_lprocfs_register_obd(obd);
2840
Jinshan Xiong7d443332016-03-30 19:49:02 -04002841 ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
Peng Taod7e09d02013-05-02 16:46:55 +08002842
2843 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2844
John L. Hammond903af112014-09-05 15:08:12 -05002845 rc = mdc_llog_init(obd);
Peng Taod7e09d02013-05-02 16:46:55 +08002846 if (rc) {
2847 mdc_cleanup(obd);
2848 CERROR("failed to setup llogging subsystems\n");
2849 }
2850
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002851 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002852
2853err_close_lock:
Julia Lawall7b817792015-05-01 17:51:17 +02002854 kfree(cli->cl_close_lock);
Swapnil Pimpalecc3b7752016-04-12 16:14:09 -04002855err_ptlrpcd_decref:
2856 ptlrpcd_decref();
Peng Taod7e09d02013-05-02 16:46:55 +08002857err_rpc_lock:
Julia Lawall7b817792015-05-01 17:51:17 +02002858 kfree(cli->cl_rpc_lock);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002859 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002860}
2861
2862/* Initialize the default and maximum LOV EA and cookie sizes. This allows
Brian Behlendorf44779342014-04-27 13:06:47 -04002863 * us to make MDS RPCs with large enough reply buffers to hold a default
2864 * sized EA and cookie without having to calculate this (via a call into the
2865 * LOV + OSCs) each time we make an RPC. The maximum size is also tracked
2866 * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2867 * a large number of stripes is possible. If a larger reply buffer is
2868 * required it will be reallocated in the ptlrpc layer due to overflow.
2869 */
Peng Taod7e09d02013-05-02 16:46:55 +08002870static int mdc_init_ea_size(struct obd_export *exp, int easize,
Brian Behlendorf44779342014-04-27 13:06:47 -04002871 int def_easize, int cookiesize, int def_cookiesize)
Peng Taod7e09d02013-05-02 16:46:55 +08002872{
2873 struct obd_device *obd = exp->exp_obd;
2874 struct client_obd *cli = &obd->u.cli;
Peng Taod7e09d02013-05-02 16:46:55 +08002875
2876 if (cli->cl_max_mds_easize < easize)
2877 cli->cl_max_mds_easize = easize;
2878
2879 if (cli->cl_default_mds_easize < def_easize)
2880 cli->cl_default_mds_easize = def_easize;
2881
2882 if (cli->cl_max_mds_cookiesize < cookiesize)
2883 cli->cl_max_mds_cookiesize = cookiesize;
2884
Brian Behlendorf44779342014-04-27 13:06:47 -04002885 if (cli->cl_default_mds_cookiesize < def_cookiesize)
2886 cli->cl_default_mds_cookiesize = def_cookiesize;
2887
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002888 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002889}
2890
2891static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2892{
Peng Taod7e09d02013-05-02 16:46:55 +08002893 switch (stage) {
2894 case OBD_CLEANUP_EARLY:
2895 break;
2896 case OBD_CLEANUP_EXPORTS:
2897 /* Failsafe, ok if racy */
2898 if (obd->obd_type->typ_refcnt <= 1)
Hongchao Zhang17328952015-12-23 16:24:44 -05002899 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
Peng Taod7e09d02013-05-02 16:46:55 +08002900
2901 obd_cleanup_client_import(obd);
2902 ptlrpc_lprocfs_unregister_obd(obd);
2903 lprocfs_obd_cleanup(obd);
2904
John L. Hammond903af112014-09-05 15:08:12 -05002905 mdc_llog_finish(obd);
Peng Taod7e09d02013-05-02 16:46:55 +08002906 break;
2907 }
Tina Johnsoneb967122014-09-14 18:36:38 +05302908 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002909}
2910
2911static int mdc_cleanup(struct obd_device *obd)
2912{
2913 struct client_obd *cli = &obd->u.cli;
2914
Julia Lawall7b817792015-05-01 17:51:17 +02002915 kfree(cli->cl_rpc_lock);
2916 kfree(cli->cl_close_lock);
Peng Taod7e09d02013-05-02 16:46:55 +08002917
2918 ptlrpcd_decref();
2919
2920 return client_obd_cleanup(obd);
2921}
2922
Oleg Drokin21aef7d2014-08-15 12:55:56 -04002923static int mdc_process_config(struct obd_device *obd, u32 len, void *buf)
Peng Taod7e09d02013-05-02 16:46:55 +08002924{
2925 struct lustre_cfg *lcfg = buf;
Radek Dostalea7893b2014-07-27 23:22:57 +02002926 struct lprocfs_static_vars lvars = { NULL };
Peng Taod7e09d02013-05-02 16:46:55 +08002927 int rc = 0;
2928
2929 lprocfs_mdc_init_vars(&lvars);
2930 switch (lcfg->lcfg_command) {
2931 default:
2932 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2933 lcfg, obd);
2934 if (rc > 0)
2935 rc = 0;
2936 break;
2937 }
Srikrishan Malikeb445202014-08-11 23:57:38 +05302938 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002939}
2940
Luca Ceresolif6219c12015-01-18 15:06:47 +01002941static struct obd_ops mdc_obd_ops = {
Dan Carpentera13b1f32015-10-29 12:24:40 +03002942 .owner = THIS_MODULE,
2943 .setup = mdc_setup,
2944 .precleanup = mdc_precleanup,
2945 .cleanup = mdc_cleanup,
2946 .add_conn = client_import_add_conn,
2947 .del_conn = client_import_del_conn,
2948 .connect = client_connect_import,
2949 .disconnect = client_disconnect_export,
2950 .iocontrol = mdc_iocontrol,
2951 .set_info_async = mdc_set_info_async,
2952 .statfs = mdc_statfs,
2953 .fid_init = client_fid_init,
2954 .fid_fini = client_fid_fini,
2955 .fid_alloc = mdc_fid_alloc,
2956 .import_event = mdc_import_event,
2957 .get_info = mdc_get_info,
2958 .process_config = mdc_process_config,
2959 .get_uuid = mdc_get_uuid,
2960 .quotactl = mdc_quotactl,
2961 .quotacheck = mdc_quotacheck
Peng Taod7e09d02013-05-02 16:46:55 +08002962};
2963
Luca Ceresolif6219c12015-01-18 15:06:47 +01002964static struct md_ops mdc_md_ops = {
Dan Carpenterdf18a802015-10-29 12:26:36 +03002965 .getstatus = mdc_getstatus,
2966 .null_inode = mdc_null_inode,
2967 .find_cbdata = mdc_find_cbdata,
2968 .close = mdc_close,
2969 .create = mdc_create,
2970 .done_writing = mdc_done_writing,
2971 .enqueue = mdc_enqueue,
2972 .getattr = mdc_getattr,
2973 .getattr_name = mdc_getattr_name,
2974 .intent_lock = mdc_intent_lock,
2975 .link = mdc_link,
2976 .is_subdir = mdc_is_subdir,
2977 .rename = mdc_rename,
2978 .setattr = mdc_setattr,
2979 .setxattr = mdc_setxattr,
2980 .getxattr = mdc_getxattr,
2981 .sync = mdc_sync,
2982 .readpage = mdc_readpage,
wang di4f76f0e2016-08-19 14:07:26 -04002983 .read_page = mdc_read_page,
Dan Carpenterdf18a802015-10-29 12:26:36 +03002984 .unlink = mdc_unlink,
2985 .cancel_unused = mdc_cancel_unused,
2986 .init_ea_size = mdc_init_ea_size,
2987 .set_lock_data = mdc_set_lock_data,
2988 .lock_match = mdc_lock_match,
2989 .get_lustre_md = mdc_get_lustre_md,
2990 .free_lustre_md = mdc_free_lustre_md,
2991 .set_open_replay_data = mdc_set_open_replay_data,
2992 .clear_open_replay_data = mdc_clear_open_replay_data,
Dan Carpenterdf18a802015-10-29 12:26:36 +03002993 .intent_getattr_async = mdc_intent_getattr_async,
2994 .revalidate_lock = mdc_revalidate_lock
Peng Taod7e09d02013-05-02 16:46:55 +08002995};
2996
Luca Ceresolif6219c12015-01-18 15:06:47 +01002997static int __init mdc_init(void)
Peng Taod7e09d02013-05-02 16:46:55 +08002998{
Radek Dostalea7893b2014-07-27 23:22:57 +02002999 struct lprocfs_static_vars lvars = { NULL };
Srikrishan Malik7436d072014-08-11 23:57:33 +05303000
Peng Taod7e09d02013-05-02 16:46:55 +08003001 lprocfs_mdc_init_vars(&lvars);
3002
Oleg Drokin2962b442015-05-21 15:32:13 -04003003 return class_register_type(&mdc_obd_ops, &mdc_md_ops,
Peng Taod7e09d02013-05-02 16:46:55 +08003004 LUSTRE_MDC_NAME, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +08003005}
3006
3007static void /*__exit*/ mdc_exit(void)
3008{
3009 class_unregister_type(LUSTRE_MDC_NAME);
3010}
3011
James Simmonsa0455472015-11-04 13:40:02 -05003012MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
Peng Taod7e09d02013-05-02 16:46:55 +08003013MODULE_DESCRIPTION("Lustre Metadata Client");
James Simmons5b0e50b2016-02-26 11:36:03 -05003014MODULE_VERSION(LUSTRE_VERSION_STRING);
Peng Taod7e09d02013-05-02 16:46:55 +08003015MODULE_LICENSE("GPL");
3016
3017module_init(mdc_init);
3018module_exit(mdc_exit);