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