Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / staging / lustre / lustre / lmv / lmv_intent.c
CommitLineData
d7e09d03
PT
1/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
6a5b99a4 18 * http://www.gnu.org/licenses/gpl-2.0.html
d7e09d03 19 *
d7e09d03
PT
20 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
1dc563a6 26 * Copyright (c) 2011, 2015, Intel Corporation.
d7e09d03
PT
27 */
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_LMV
34#include <linux/slab.h>
35#include <linux/module.h>
d7e09d03
PT
36#include <linux/pagemap.h>
37#include <asm/div64.h>
38#include <linux/seq_file.h>
39#include <linux/namei.h>
00d65ec8 40#include "../include/lustre_intent.h"
a8c495ac
GKH
41#include "../include/obd_support.h"
42#include "../include/lustre/lustre_idl.h"
43#include "../include/lustre_lib.h"
44#include "../include/lustre_net.h"
45#include "../include/lustre_dlm.h"
79496845 46#include "../include/lustre_mdc.h"
a8c495ac
GKH
47#include "../include/obd_class.h"
48#include "../include/lprocfs_status.h"
d7e09d03
PT
49#include "lmv_internal.h"
50
70a251f6
JH
51static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
52 const struct lu_fid *parent_fid,
d7e09d03
PT
53 struct ptlrpc_request **reqp,
54 ldlm_blocking_callback cb_blocking,
55 __u64 extra_lock_flags)
56{
57 struct obd_device *obd = exp->exp_obd;
58 struct lmv_obd *lmv = &obd->u.lmv;
59 struct ptlrpc_request *req = NULL;
60 struct lustre_handle plock;
61 struct md_op_data *op_data;
62 struct lmv_tgt_desc *tgt;
63 struct mdt_body *body;
64 int pmode;
65 int rc = 0;
d7e09d03
PT
66
67 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
2cbdaa45 68 if (!body)
0a3bdb00 69 return -EPROTO;
d7e09d03 70
2e1b5b8b 71 LASSERT((body->mbo_valid & OBD_MD_MDS));
d7e09d03
PT
72
73 /*
74 * Unfortunately, we have to lie to MDC/MDS to retrieve
75 * attributes llite needs and provideproper locking.
76 */
77 if (it->it_op & IT_LOOKUP)
78 it->it_op = IT_GETATTR;
79
80 /*
81 * We got LOOKUP lock, but we really need attrs.
82 */
e476f2e5 83 pmode = it->it_lock_mode;
d7e09d03 84 if (pmode) {
e476f2e5
JH
85 plock.cookie = it->it_lock_handle;
86 it->it_lock_mode = 0;
8bf86fd9 87 it->it_request = NULL;
d7e09d03
PT
88 }
89
2e1b5b8b 90 LASSERT(fid_is_sane(&body->mbo_fid1));
d7e09d03 91
2e1b5b8b 92 tgt = lmv_find_target(lmv, &body->mbo_fid1);
4d54556f
JL
93 if (IS_ERR(tgt)) {
94 rc = PTR_ERR(tgt);
95 goto out;
96 }
d7e09d03 97
8bcf30c3 98 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
76e4290c 99 if (!op_data) {
4d54556f
JL
100 rc = -ENOMEM;
101 goto out;
102 }
d7e09d03 103
2e1b5b8b 104 op_data->op_fid1 = body->mbo_fid1;
d7e09d03 105 /* Sent the parent FID to the remote MDT */
2cbdaa45 106 if (parent_fid) {
d7e09d03
PT
107 /* The parent fid is only for remote open to
108 * check whether the open is from OBF,
7f05d5bb
OD
109 * see mdt_cross_open
110 */
d7e09d03
PT
111 LASSERT(it->it_op & IT_OPEN);
112 op_data->op_fid2 = *parent_fid;
d7e09d03
PT
113 }
114
115 op_data->op_bias = MDS_CROSS_REF;
116 CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%d\n",
2e1b5b8b 117 PFID(&body->mbo_fid1), tgt->ltd_idx);
d7e09d03 118
70a251f6
JH
119 rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
120 extra_lock_flags);
d7e09d03 121 if (rc)
4d54556f 122 goto out_free_op_data;
d7e09d03
PT
123
124 /*
125 * LLite needs LOOKUP lock to track dentry revocation in order to
126 * maintain dcache consistency. Thus drop UPDATE|PERM lock here
127 * and put LOOKUP in request.
128 */
e476f2e5
JH
129 if (it->it_lock_mode != 0) {
130 it->it_remote_lock_handle =
131 it->it_lock_handle;
132 it->it_remote_lock_mode = it->it_lock_mode;
d7e09d03
PT
133 }
134
503df2c4
DE
135 if (pmode) {
136 it->it_lock_handle = plock.cookie;
137 it->it_lock_mode = pmode;
138 }
d7e09d03 139
d7e09d03 140out_free_op_data:
8bcf30c3 141 kfree(op_data);
d7e09d03
PT
142out:
143 if (rc && pmode)
144 ldlm_lock_decref(&plock, pmode);
145
146 ptlrpc_req_finished(*reqp);
147 *reqp = req;
148 return rc;
149}
150
2de35386 151int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
152 struct lmv_stripe_md *lsm,
153 ldlm_blocking_callback cb_blocking,
154 int extra_lock_flags)
155{
156 struct obd_device *obd = exp->exp_obd;
157 struct lmv_obd *lmv = &obd->u.lmv;
158 struct mdt_body *body;
159 struct md_op_data *op_data;
160 unsigned long size = 0;
161 unsigned long nlink = 0;
162 __s64 atime = 0;
163 __s64 ctime = 0;
164 __s64 mtime = 0;
165 int rc = 0, i;
166
167 /**
168 * revalidate slaves has some problems, temporarily return,
169 * we may not need that
170 */
2de35386 171 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
172 if (!op_data)
173 return -ENOMEM;
174
175 /**
176 * Loop over the stripe information, check validity and update them
177 * from MDS if needed.
178 */
179 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
180 struct lookup_intent it = { .it_op = IT_GETATTR };
181 struct ptlrpc_request *req = NULL;
182 struct lustre_handle *lockh = NULL;
183 struct lmv_tgt_desc *tgt = NULL;
184 struct inode *inode;
185 struct lu_fid fid;
186
187 fid = lsm->lsm_md_oinfo[i].lmo_fid;
188 inode = lsm->lsm_md_oinfo[i].lmo_root;
2de35386 189
190 /*
191 * Prepare op_data for revalidating. Note that @fid2 shluld be
192 * defined otherwise it will go to server and take new lock
193 * which is not needed here.
194 */
195 memset(op_data, 0, sizeof(*op_data));
196 op_data->op_fid1 = fid;
197 op_data->op_fid2 = fid;
198
199 tgt = lmv_locate_mds(lmv, op_data, &fid);
200 if (IS_ERR(tgt)) {
201 rc = PTR_ERR(tgt);
202 goto cleanup;
203 }
204
205 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%d\n",
206 PFID(&fid), tgt->ltd_idx);
207
70a251f6
JH
208 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
209 cb_blocking, extra_lock_flags);
2de35386 210 if (rc < 0)
211 goto cleanup;
212
213 lockh = (struct lustre_handle *)&it.it_lock_handle;
214 if (rc > 0 && !req) {
215 /* slave inode is still valid */
216 CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
217 PFID(&fid));
218 rc = 0;
219 } else {
220 /* refresh slave from server */
221 body = req_capsule_server_get(&req->rq_pill,
222 &RMF_MDT_BODY);
223 LASSERT(body);
8f18c8a4 224
2e1b5b8b 225 if (unlikely(body->mbo_nlink < 2)) {
2de35386 226 CERROR("%s: nlink %d < 2 corrupt stripe %d "DFID":" DFID"\n",
2e1b5b8b 227 obd->obd_name, body->mbo_nlink, i,
2de35386 228 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
229 PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
230
231 if (req)
232 ptlrpc_req_finished(req);
233
75ac62fc 234 if (it.it_lock_mode && lockh) {
235 ldlm_lock_decref(lockh, it.it_lock_mode);
236 it.it_lock_mode = 0;
237 }
238
2de35386 239 rc = -EIO;
240 goto cleanup;
241 }
242
2e1b5b8b
JH
243 i_size_write(inode, body->mbo_size);
244 set_nlink(inode, body->mbo_nlink);
245 LTIME_S(inode->i_atime) = body->mbo_atime;
246 LTIME_S(inode->i_ctime) = body->mbo_ctime;
247 LTIME_S(inode->i_mtime) = body->mbo_mtime;
2de35386 248
249 if (req)
250 ptlrpc_req_finished(req);
251 }
8f18c8a4 252
bc30c172 253 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
2de35386 254
255 if (i != 0)
256 nlink += inode->i_nlink - 2;
257 else
258 nlink += inode->i_nlink;
259
260 atime = LTIME_S(inode->i_atime) > atime ?
261 LTIME_S(inode->i_atime) : atime;
262 ctime = LTIME_S(inode->i_ctime) > ctime ?
263 LTIME_S(inode->i_ctime) : ctime;
264 mtime = LTIME_S(inode->i_mtime) > mtime ?
265 LTIME_S(inode->i_mtime) : mtime;
266
267 if (it.it_lock_mode && lockh) {
268 ldlm_lock_decref(lockh, it.it_lock_mode);
269 it.it_lock_mode = 0;
270 }
271
272 CDEBUG(D_INODE, "i %d "DFID" size %llu, nlink %u, atime %lu, mtime %lu, ctime %lu.\n",
273 i, PFID(&fid), i_size_read(inode), inode->i_nlink,
274 LTIME_S(inode->i_atime), LTIME_S(inode->i_mtime),
275 LTIME_S(inode->i_ctime));
276 }
277
278 /*
279 * update attr of master request.
280 */
281 CDEBUG(D_INODE, "Return refreshed attrs: size = %lu nlink %lu atime %llu ctime %llu mtime %llu for " DFID"\n",
282 size, nlink, atime, ctime, mtime,
283 PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
284
285 if (mbody) {
2e1b5b8b
JH
286 mbody->mbo_atime = atime;
287 mbody->mbo_ctime = ctime;
288 mbody->mbo_mtime = mtime;
2de35386 289 }
290cleanup:
291 kfree(op_data);
292 return rc;
293}
294
d7e09d03
PT
295/*
296 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
297 * may be split dir.
298 */
c8b3c83e 299static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
70a251f6
JH
300 struct lookup_intent *it,
301 struct ptlrpc_request **reqp,
c8b3c83e
SB
302 ldlm_blocking_callback cb_blocking,
303 __u64 extra_lock_flags)
d7e09d03
PT
304{
305 struct obd_device *obd = exp->exp_obd;
306 struct lmv_obd *lmv = &obd->u.lmv;
307 struct lmv_tgt_desc *tgt;
308 struct mdt_body *body;
309 int rc;
d7e09d03 310
c1b66fcc
LS
311 if (it->it_flags & MDS_OPEN_BY_FID) {
312 LASSERT(fid_is_sane(&op_data->op_fid2));
2de35386 313
c1b66fcc
LS
314 /*
315 * for striped directory, we can't know parent stripe fid
316 * without name, but we can set it to child fid, and MDT
317 * will obtain it from linkea in open in such case.
318 */
319 if (op_data->op_mea1)
320 op_data->op_fid1 = op_data->op_fid2;
2de35386 321
322 tgt = lmv_find_target(lmv, &op_data->op_fid2);
323 if (IS_ERR(tgt))
324 return PTR_ERR(tgt);
325
326 op_data->op_mds = tgt->ltd_idx;
327 } else {
c1b66fcc
LS
328 LASSERT(fid_is_sane(&op_data->op_fid1));
329 LASSERT(fid_is_zero(&op_data->op_fid2));
330 LASSERT(op_data->op_name);
331
2de35386 332 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
333 if (IS_ERR(tgt))
334 return PTR_ERR(tgt);
335 }
d7e09d03
PT
336
337 /* If it is ready to open the file by FID, do not need
7f05d5bb
OD
338 * allocate FID at all, otherwise it will confuse MDT
339 */
c1b66fcc 340 if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID)) {
d7e09d03 341 /*
c1b66fcc
LS
342 * For lookup(IT_CREATE) cases allocate new fid and setup FLD
343 * for it.
d7e09d03 344 */
8f18c8a4 345 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
d7e09d03 346 if (rc != 0)
0a3bdb00 347 return rc;
d7e09d03
PT
348 }
349
2d00bd17
JP
350 CDEBUG(D_INODE, "OPEN_INTENT with fid1=" DFID ", fid2=" DFID ", name='%s' -> mds #%d\n",
351 PFID(&op_data->op_fid1),
d7e09d03
PT
352 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
353
70a251f6
JH
354 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
355 extra_lock_flags);
d7e09d03 356 if (rc != 0)
0a3bdb00 357 return rc;
d7e09d03 358 /*
2e1b5b8b 359 * Nothing is found, do not access body->mbo_fid1 as it is zero and thus
d7e09d03
PT
360 * pointless.
361 */
e476f2e5
JH
362 if ((it->it_disposition & DISP_LOOKUP_NEG) &&
363 !(it->it_disposition & DISP_OPEN_CREATE) &&
364 !(it->it_disposition & DISP_OPEN_OPEN))
0a3bdb00 365 return rc;
d7e09d03
PT
366
367 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
2cbdaa45 368 if (!body)
0a3bdb00 369 return -EPROTO;
d7e09d03 370
2de35386 371 /* Not cross-ref case, just get out of here. */
2e1b5b8b 372 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
70a251f6
JH
373 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
374 cb_blocking, extra_lock_flags);
2de35386 375 if (rc != 0)
376 return rc;
377
378 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
379 if (!body)
380 return -EPROTO;
d7e09d03
PT
381 }
382
0a3bdb00 383 return rc;
d7e09d03
PT
384}
385
386/*
387 * Handler for: getattr, lookup and revalidate cases.
388 */
c8b3c83e
SB
389static int lmv_intent_lookup(struct obd_export *exp,
390 struct md_op_data *op_data,
70a251f6
JH
391 struct lookup_intent *it,
392 struct ptlrpc_request **reqp,
c8b3c83e
SB
393 ldlm_blocking_callback cb_blocking,
394 __u64 extra_lock_flags)
d7e09d03 395{
79496845 396 struct lmv_stripe_md *lsm = op_data->op_mea1;
d7e09d03
PT
397 struct obd_device *obd = exp->exp_obd;
398 struct lmv_obd *lmv = &obd->u.lmv;
399 struct lmv_tgt_desc *tgt = NULL;
400 struct mdt_body *body;
401 int rc = 0;
d7e09d03 402
893ab747 403 /*
404 * If it returns ERR_PTR(-EBADFD) then it is an unknown hash type
405 * it will try all stripes to locate the object
406 */
d7e09d03 407 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
893ab747 408 if (IS_ERR(tgt) && (PTR_ERR(tgt) != -EBADFD))
0a3bdb00 409 return PTR_ERR(tgt);
d7e09d03 410
893ab747 411 /*
412 * Both migrating dir and unknown hash dir need to try
413 * all of sub-stripes
414 */
415 if (lsm && !lmv_is_known_hash_type(lsm)) {
416 struct lmv_oinfo *oinfo = &lsm->lsm_md_oinfo[0];
417
418 op_data->op_fid1 = oinfo->lmo_fid;
419 op_data->op_mds = oinfo->lmo_mds;
420 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
421 if (IS_ERR(tgt))
422 return PTR_ERR(tgt);
423 }
424
d7e09d03
PT
425 if (!fid_is_sane(&op_data->op_fid2))
426 fid_zero(&op_data->op_fid2);
427
79496845 428 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID", name='%s' -> mds #%d lsm=%p lsm_magic=%x\n",
429 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
d7e09d03 430 op_data->op_name ? op_data->op_name : "<NULL>",
79496845 431 tgt->ltd_idx, lsm, !lsm ? -1 : lsm->lsm_md_magic);
d7e09d03
PT
432
433 op_data->op_bias &= ~MDS_CROSS_REF;
434
70a251f6
JH
435 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
436 extra_lock_flags);
2de35386 437 if (rc < 0)
438 return rc;
439
440 if (!*reqp) {
441 /*
442 * If RPC happens, lsm information will be revalidated
443 * during update_inode process (see ll_update_lsm_md)
444 */
445 if (op_data->op_mea2) {
446 rc = lmv_revalidate_slaves(exp, NULL, op_data->op_mea2,
447 cb_blocking,
448 extra_lock_flags);
449 if (rc != 0)
450 return rc;
451 }
0a3bdb00 452 return rc;
79496845 453 } else if (it_disposition(it, DISP_LOOKUP_NEG) && lsm &&
893ab747 454 lmv_need_try_all_stripes(lsm)) {
79496845 455 /*
893ab747 456 * For migrating and unknown hash type directory, it will
457 * try to target the entry on other stripes
79496845 458 */
893ab747 459 int stripe_index;
460
461 for (stripe_index = 1;
462 stripe_index < lsm->lsm_md_stripe_count &&
463 it_disposition(it, DISP_LOOKUP_NEG); stripe_index++) {
464 struct lmv_oinfo *oinfo;
465
466 /* release the previous request */
467 ptlrpc_req_finished(*reqp);
468 it->it_request = NULL;
469 *reqp = NULL;
470
471 oinfo = &lsm->lsm_md_oinfo[stripe_index];
472 tgt = lmv_find_target(lmv, &oinfo->lmo_fid);
473 if (IS_ERR(tgt))
474 return PTR_ERR(tgt);
475
476 CDEBUG(D_INODE, "Try other stripes " DFID"\n",
477 PFID(&oinfo->lmo_fid));
478
479 op_data->op_fid1 = oinfo->lmo_fid;
480 it->it_disposition &= ~DISP_ENQ_COMPLETE;
70a251f6 481 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp,
893ab747 482 cb_blocking, extra_lock_flags);
483 if (rc)
484 return rc;
485 }
2de35386 486 }
d7e09d03
PT
487
488 /*
489 * MDS has returned success. Probably name has been resolved in
490 * remote inode. Let's check this.
491 */
492 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
2cbdaa45 493 if (!body)
0a3bdb00 494 return -EPROTO;
d7e09d03 495
2de35386 496 /* Not cross-ref case, just get out of here. */
2e1b5b8b 497 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
70a251f6
JH
498 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
499 extra_lock_flags);
2de35386 500 if (rc != 0)
501 return rc;
502 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
503 if (!body)
504 return -EPROTO;
505 }
d7e09d03 506
0a3bdb00 507 return rc;
d7e09d03
PT
508}
509
510int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
70a251f6 511 struct lookup_intent *it, struct ptlrpc_request **reqp,
d7e09d03
PT
512 ldlm_blocking_callback cb_blocking,
513 __u64 extra_lock_flags)
514{
515 struct obd_device *obd = exp->exp_obd;
516 int rc;
d7e09d03 517
d7e09d03
PT
518 LASSERT(fid_is_sane(&op_data->op_fid1));
519
c1b66fcc
LS
520 CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%*s' on "DFID"\n",
521 LL_IT2STR(it), PFID(&op_data->op_fid2), op_data->op_namelen,
522 op_data->op_name, PFID(&op_data->op_fid1));
d7e09d03
PT
523
524 rc = lmv_check_connect(obd);
525 if (rc)
0a3bdb00 526 return rc;
d7e09d03
PT
527
528 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
70a251f6 529 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
d7e09d03
PT
530 extra_lock_flags);
531 else if (it->it_op & IT_OPEN)
70a251f6 532 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
d7e09d03
PT
533 extra_lock_flags);
534 else
535 LBUG();
0a3bdb00 536 return rc;
d7e09d03 537}
This page took 0.546537 seconds and 5 git commands to generate.