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