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