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