Whamcloud - gitweb
LU-2675 obd: decruft md_enqueue() and md_intent_lock()
[fs/lustre-release.git] / lustre / liblustre / namei.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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, 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  * lustre/liblustre/namei.c
37  *
38  * Lustre Light name resolution
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <assert.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/stat.h>
49 #include <sys/types.h>
50 #include <libcfs/libcfs.h>
51 #include <lustre/lustre_idl.h>
52 #include <liblustre.h>
53 #include <lclient.h>
54 #include <lustre_dlm.h>
55 #include <lustre_fid.h>
56 #include <lustre_mdc.h>
57 #include <lustre_net.h>
58 #include <obd.h>
59 #include <obd_class.h>
60 #include <obd_support.h>
61 #include "llite_lib.h"
62
63 static void ll_intent_drop_lock(struct lookup_intent *it)
64 {
65         struct lustre_handle *handle;
66
67         if (it->it_op && it->d.lustre.it_lock_mode) {
68                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
69                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
70                        " from it %p\n", handle->cookie, it);
71                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
72
73                 /* bug 494: intent_release may be called multiple times, from
74                  * this thread and we don't want to double-decref this lock */
75                 it->d.lustre.it_lock_mode = 0;
76         }
77 }
78
79 static void ll_intent_release(struct lookup_intent *it)
80 {
81         ENTRY;
82
83         ll_intent_drop_lock(it);
84         it->it_magic = 0;
85         it->it_op_release = 0;
86         it->d.lustre.it_disposition = 0;
87         it->d.lustre.it_data = NULL;
88         EXIT;
89 }
90
91 static void
92 llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
93 {
94         struct inode *inode;
95         LASSERT(it);
96         LASSERT(pnode);
97
98         inode = pnode->p_base->pb_ino;
99         if (it->d.lustre.it_lock_mode && inode != NULL) {
100                 struct llu_sb_info *sbi;
101
102                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
103                        inode, (long long)llu_i2stat(inode)->st_ino,
104                        llu_i2info(inode)->lli_st_generation);
105
106                 sbi = llu_i2sbi(inode);
107                 md_set_lock_data(sbi->ll_md_exp,
108                                  &it->d.lustre.it_lock_handle, inode, NULL);
109         }
110
111         /* drop lookup/getattr locks */
112         if (it->it_op & (IT_LOOKUP | IT_GETATTR))
113                 ll_intent_release(it);
114
115 }
116
117 static inline void llu_invalidate_inode_pages(struct inode * inode)
118 {
119         /* do nothing */
120 }
121
122 int llu_md_blocking_ast(struct ldlm_lock *lock,
123                         struct ldlm_lock_desc *desc,
124                         void *data, int flag)
125 {
126         struct lustre_handle lockh;
127         int rc;
128         ENTRY;
129
130
131         switch (flag) {
132         case LDLM_CB_BLOCKING:
133                 ldlm_lock2handle(lock, &lockh);
134                 rc = ldlm_cli_cancel(&lockh, 0);
135                 if (rc < 0) {
136                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
137                         RETURN(rc);
138                 }
139                 break;
140         case LDLM_CB_CANCELING: {
141                 struct inode *inode = llu_inode_from_resource_lock(lock);
142                 struct llu_inode_info *lli;
143                 struct intnl_stat *st;
144                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
145                 struct lu_fid *fid;
146
147                 /* Inode is set to lock->l_resource->lr_lvb_inode
148                 * for mdc - bug 24555 */
149                 LASSERT(lock->l_ast_data == NULL);
150
151                 /* Invalidate all dentries associated with this inode */
152                 if (inode == NULL)
153                         break;
154
155                 lli =  llu_i2info(inode);
156                 st = llu_i2stat(inode);
157
158                 if (bits & MDS_INODELOCK_UPDATE)
159                         lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
160
161                 fid = &lli->lli_fid;
162                 if (!fid_res_name_eq(fid, &lock->l_resource->lr_name))
163                         LDLM_ERROR(lock, "data mismatch with object "
164                                    DFID" (%p)", PFID(fid), inode);
165                 if (S_ISDIR(st->st_mode) &&
166                     (bits & MDS_INODELOCK_UPDATE)) {
167                         CDEBUG(D_INODE, "invalidating inode %llu\n",
168                                (long long)st->st_ino);
169
170                         llu_invalidate_inode_pages(inode);
171                 }
172
173 /*
174                 if (inode->i_sb->s_root &&
175                     inode != inode->i_sb->s_root->d_inode)
176                         ll_unhash_aliases(inode);
177 */
178                 I_RELE(inode);
179                 break;
180         }
181         default:
182                 LBUG();
183         }
184
185         RETURN(0);
186 }
187
188 static int pnode_revalidate_finish(struct ptlrpc_request *req,
189                                    struct lookup_intent *it,
190                                    struct pnode *pnode)
191 {
192         struct inode *inode = pnode->p_base->pb_ino;
193         struct lustre_md md;
194         int rc = 0;
195         ENTRY;
196
197         LASSERT(inode);
198
199         if (!req)
200                 RETURN(0);
201
202         if (it_disposition(it, DISP_LOOKUP_NEG))
203                 RETURN(-ENOENT);
204
205         rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
206                               llu_i2sbi(inode)->ll_dt_exp, 
207                               llu_i2sbi(inode)->ll_md_exp, &md);
208         if (rc)
209                 RETURN(rc);
210
211         llu_update_inode(inode, &md);
212
213         RETURN(rc);
214 }
215
216 static int llu_pb_revalidate(struct pnode *pnode, int flags,
217                              struct lookup_intent *it)
218 {
219         struct pnode_base *pb = pnode->p_base;
220         struct md_op_data op_data = {{ 0 }};
221         struct ptlrpc_request *req = NULL;
222         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
223         struct obd_export *exp;
224         int rc;
225         ENTRY;
226
227         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
228                (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
229
230         /* We don't want to cache negative dentries, so return 0 immediately.
231          * We believe that this is safe, that negative dentries cannot be
232          * pinned by someone else */
233         if (pb->pb_ino == NULL) {
234                 CDEBUG(D_INODE, "negative pb\n");
235                 RETURN(0);
236         }
237
238         /* This is due to bad interaction with libsysio. remove this when we
239          * switched to libbsdio XXX
240          */
241         {
242                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
243                 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
244                 if (lli->lli_it) {
245                         CDEBUG(D_INODE, "inode %llu still have intent "
246                                         "%p(opc 0x%x), release it\n",
247                                         (long long) st->st_ino, lli->lli_it,
248                                         lli->lli_it->it_op);
249                         ll_intent_release(lli->lli_it);
250                         OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
251                         lli->lli_it = NULL;
252                 }
253         }
254
255         exp = llu_i2mdexp(pb->pb_ino);
256
257         if (!it) {
258                 it = &lookup_it;
259                 it->it_op_release = ll_intent_release;
260         }
261
262         llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
263                             pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
264                             0, LUSTRE_OPC_ANY);
265
266         rc = md_intent_lock(exp, &op_data, it, &req, &llu_md_blocking_ast,
267                             LDLM_FL_CANCEL_ON_BLOCK);
268         /* If req is NULL, then md_intent_lock only tried to do a lock match;
269          * if all was well, it will return 1 if it found locks, 0 otherwise. */
270         if (req == NULL && rc >= 0)
271                 GOTO(out, rc);
272
273         if (rc < 0)
274                 GOTO(out, rc = 0);
275
276         rc = pnode_revalidate_finish(req, it, pnode);
277         if (rc != 0) {
278                 ll_intent_release(it);
279                 GOTO(out, rc = 0);
280         }
281         rc = 1;
282
283         /* Note: ll_intent_lock may cause a callback, check this! */
284
285         if (it->it_op & IT_OPEN)
286                 LL_SAVE_INTENT(pb->pb_ino, it);
287
288  out:
289         if (req && rc == 1)
290                 ptlrpc_req_finished(req);
291         if (rc == 0) {
292                 LASSERT(pb->pb_ino);
293                 I_RELE(pb->pb_ino);
294                 pb->pb_ino = NULL;
295         } else {
296                 llu_lookup_finish_locks(it, pnode);
297         }
298         RETURN(rc);
299 }
300
301 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
302                             struct lookup_intent *it,
303                             struct inode *parent, struct pnode *child)
304 {
305         struct llu_sb_info *sbi = llu_i2sbi(parent);
306         struct inode *inode = NULL;
307         int rc;
308
309         /* libsysio require us generate inode right away if success.
310          * so if mds created new inode for us we need make sure it
311          * succeeded. thus for any error we can't delay to the
312          * llu_file_open() time. */
313         if (it_disposition(it, DISP_OPEN_CREATE) &&
314             it_open_error(DISP_OPEN_CREATE, it)) {
315                 CDEBUG(D_INODE, "detect mds create error\n");
316                 return it_open_error(DISP_OPEN_CREATE, it);
317         }
318         if (it_disposition(it, DISP_OPEN_OPEN) &&
319             it_open_error(DISP_OPEN_OPEN, it)) {
320                 CDEBUG(D_INODE, "detect mds open error\n");
321                 /* undo which did by md_intent_lock */
322                 if (it_disposition(it, DISP_OPEN_CREATE) &&
323                     !it_open_error(DISP_OPEN_CREATE, it)) {
324                         LASSERT(request);
325                         LASSERT(atomic_read(&request->rq_refcount) > 1);
326                         CDEBUG(D_INODE, "dec a ref of req %p\n", request);
327                         ptlrpc_req_finished(request);
328                 }
329                 return it_open_error(DISP_OPEN_OPEN, it);
330         }
331
332         /* NB 1 request reference will be taken away by ll_intent_lock()
333          * when I return
334          */
335         if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
336                 struct lustre_md md;
337                 struct llu_inode_info *lli;
338                 struct intnl_stat *st;
339                 ENTRY;
340
341                 if (it_disposition(it, DISP_OPEN_CREATE))
342                         ptlrpc_req_finished(request);
343
344                 rc = md_get_lustre_md(sbi->ll_md_exp, request,
345                                       sbi->ll_dt_exp, sbi->ll_md_exp, &md);
346                 if (rc)
347                         RETURN(rc);
348
349                 inode = llu_iget(parent->i_fs, &md);
350                 if (!inode || IS_ERR(inode)) {
351                         /* free the lsm if we allocated one above */
352                         if (md.lsm != NULL)
353                                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
354                         RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
355                 } else if (md.lsm != NULL) {
356                         obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
357                 }
358
359                 lli = llu_i2info(inode);
360                 st = llu_i2stat(inode);
361
362                 /* If this is a stat, get the authoritative file size */
363                 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
364                     lli->lli_has_smd) {
365                         ldlm_error_t rc;
366
367                         /* bug 2334: drop MDS lock before acquiring OST lock */
368                         ll_intent_drop_lock(it);
369
370                         rc = cl_glimpse_size(inode);
371                         if (rc) {
372                                 I_RELE(inode);
373                                 RETURN(rc);
374                         }
375                 }
376         } else {
377                 ENTRY;
378         }
379
380         /* intent will be further used in cases of open()/getattr() */
381         if (inode && (it->it_op & IT_OPEN))
382                 LL_SAVE_INTENT(inode, it);
383
384         child->p_base->pb_ino = inode;
385
386         RETURN(0);
387 }
388
389 struct inode *llu_inode_from_resource_lock(struct ldlm_lock *lock)
390 {
391         struct inode *inode;
392         lock_res_and_lock(lock);
393
394         if (lock->l_resource->lr_lvb_inode) {
395                 inode = (struct inode *)lock->l_resource->lr_lvb_inode;
396                 I_REF(inode);
397         } else
398                 inode = NULL;
399
400         unlock_res_and_lock(lock);
401         return inode;
402 }
403
404 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
405                          struct lookup_intent *it, int flags)
406 {
407         struct md_op_data op_data = {{ 0 }};
408         struct ptlrpc_request *req = NULL;
409         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
410         __u32 opc;
411         int rc;
412         ENTRY;
413
414         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
415                 RETURN(-ENAMETOOLONG);
416
417         if (!it) {
418                 it = &lookup_it;
419                 it->it_op_release = ll_intent_release;
420         }
421
422         if (it->it_op & IT_CREAT)
423                 opc = LUSTRE_OPC_CREATE;
424         else
425                 opc = LUSTRE_OPC_ANY;
426
427         llu_prep_md_op_data(&op_data, parent, NULL,
428                             pnode->p_base->pb_name.name,
429                             pnode->p_base->pb_name.len, flags, opc);
430
431         rc = md_intent_lock(llu_i2mdexp(parent), &op_data, it, &req,
432                             &llu_md_blocking_ast, LDLM_FL_CANCEL_ON_BLOCK);
433         if (rc < 0)
434                 GOTO(out, rc);
435
436         rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, parent, pnode);
437         if (rc != 0) {
438                 ll_intent_release(it);
439                 GOTO(out, rc);
440         }
441
442         llu_lookup_finish_locks(it, pnode);
443
444  out:
445         if (req)
446                 ptlrpc_req_finished(req);
447         return rc;
448 }
449
450 static struct lookup_intent*
451 translate_lookup_intent(struct intent *intent, const char *path)
452 {
453         struct lookup_intent *it;
454         int fmode;
455
456         /* libsysio trick */
457         if (!intent || path) {
458                 CDEBUG(D_VFSTRACE, "not intent needed\n");
459                 return NULL;
460         }
461
462         OBD_ALLOC(it, sizeof(*it));
463         LASSERT(it);
464
465         memset(it, 0, sizeof(*it));
466
467         /* libsysio will assign intent like following:
468          * NOTE: INT_CREAT has include INT_UPDPARENT
469          *
470          * open: INT_OPEN [| INT_CREAT]
471          * mkdir: INT_CREAT
472          * symlink: INT_CREAT
473          * unlink: INT_UPDPARENT
474          * rmdir: INT_UPDPARENT
475          * mknod: INT_CREAT
476          * stat: INT_GETATTR
477          * setattr: NULL
478          *
479          * following logic is adjusted for libsysio
480          */
481
482         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
483
484         if (intent->int_opmask & INT_OPEN) {
485                 it->it_op |= IT_OPEN;
486
487                 /* convert access mode from O_ to FMODE_ */
488                 if (it->it_flags & O_WRONLY)
489                         fmode = FMODE_WRITE;
490                 else if (it->it_flags & O_RDWR)
491                         fmode = FMODE_READ | FMODE_WRITE;
492                 else
493                         fmode = FMODE_READ;
494                 it->it_flags &= ~O_ACCMODE;
495                 it->it_flags |= fmode;
496         }
497
498         /* XXX libsysio has strange code on intent handling,
499          * more check later */
500         if (it->it_flags & O_CREAT) {
501                 it->it_op |= IT_CREAT;
502                 it->it_create_mode = *((int*)intent->int_arg1);
503                 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
504                    this lookup to return negative result, but then there is no
505                    way to find out original intent in ll_iop_open(). So we just
506                    clear O_EXCL from libsysio flags here to avoid checking
507                    for negative result. O_EXCL will be enforced by MDS. */
508                 *((int*)intent->int_arg2) &= ~O_EXCL;
509         }
510
511         if (intent->int_opmask & INT_GETATTR)
512                 it->it_op |= IT_GETATTR;
513
514         LASSERT(!(intent->int_opmask & INT_SETATTR));
515
516         /* libsysio is different to linux vfs when doing unlink/rmdir,
517          * INT_UPDPARENT was passed down during name resolution. Here
518          * we treat it as normal lookup, later unlink()/rmdir() will
519          * do the actual work */
520
521         /* conform to kernel code, if only IT_LOOKUP was set, don't
522          * pass down it */
523         if (!it->it_op || it->it_op & IT_LOOKUP) {
524                 OBD_FREE(it, sizeof(*it));
525                 it = NULL;
526         }
527         if (it)
528                 it->it_op_release = ll_intent_release;
529
530         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
531         return it;
532 }
533
534 int llu_iop_lookup(struct pnode *pnode,
535                    struct inode **inop,
536                    struct intent *intnt,
537                    const char *path)
538 {
539         struct lookup_intent *it;
540         int rc;
541         ENTRY;
542
543         liblustre_wait_event(0);
544
545         *inop = NULL;
546
547         /* the mount root inode have no name, so don't call
548          * remote in this case. but probably we need revalidate
549          * it here? FIXME */
550         if (pnode->p_mount->mnt_root == pnode) {
551                 struct inode *i = pnode->p_base->pb_ino;
552                 *inop = i;
553                 RETURN(0);
554         }
555
556         if (!pnode->p_base->pb_name.len)
557                 RETURN(-EINVAL);
558
559         it = translate_lookup_intent(intnt, path);
560
561         /* param flags is not used, let it be 0 */
562         if (llu_pb_revalidate(pnode, 0, it)) {
563                 LASSERT(pnode->p_base->pb_ino);
564                 *inop = pnode->p_base->pb_ino;
565                 GOTO(out, rc = 0);
566         }
567
568         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
569         if (!rc) {
570                 if (!pnode->p_base->pb_ino)
571                         rc = -ENOENT;
572                 else
573                         *inop = pnode->p_base->pb_ino;
574         }
575
576 out:
577         if (it)
578                 OBD_FREE(it, sizeof(*it));
579         liblustre_wait_event(0);
580         RETURN(rc);
581 }