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