Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / liblustre / namei.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/fcntl.h>
33 #include <sys/queue.h>
34
35 #include <sysio.h>
36 #include <fs.h>
37 #include <mount.h>
38 #include <inode.h>
39 #include <file.h>
40
41 #include "llite_lib.h"
42
43 static void ll_intent_release(struct lookup_intent *it)
44 {
45         struct lustre_handle *handle;
46         ENTRY;
47
48         /* LASSERT(ll_d2d(de) != NULL); */
49
50         if (it->d.lustre.it_lock_mode) {
51                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
52                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
53                        " from it %p\n",
54                        handle->cookie, it);
55                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
56
57                 /* intent_release may be called multiple times, from
58                    this thread and we don't want to double-decref this
59                    lock (see bug 494) */
60                 it->d.lustre.it_lock_mode = 0;
61         }
62         it->it_magic = 0;
63         it->it_op_release = 0;
64         EXIT;
65 }
66
67 #if 0
68 static void llu_mdc_lock_set_inode(struct lustre_handle *lockh,
69                                    struct inode *inode)
70 {
71         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
72         ENTRY;
73
74         LASSERT(lock != NULL);
75         lock->l_data = inode;
76         LDLM_LOCK_PUT(lock);
77         EXIT;
78 }
79
80 static int pnode_revalidate_finish(struct ptlrpc_request *request,
81                                    struct inode *parent, struct pnode *pnode,
82                                    struct lookup_intent *it, int offset,
83                                    obd_id ino)
84 {
85         struct llu_sb_info    *sbi = llu_i2sbi(parent);
86         struct pnode_base     *pb = pnode->p_base;
87         struct mds_body       *body;
88         struct lov_stripe_md  *lsm = NULL;
89         struct lov_mds_md     *lmm;
90         int                    lmmsize;
91         int                    rc = 0;
92         ENTRY;
93
94         /* NB 1 request reference will be taken away by ll_intent_lock()
95          * when I return */
96
97         if (it_disposition(it, DISP_LOOKUP_NEG))
98                 RETURN(-ENOENT);
99
100         /* We only get called if the mdc_enqueue() called from
101          * ll_intent_lock() was successful.  Therefore the mds_body is
102          * present and correct, and the eadata is present (but still
103          * opaque, so only obd_unpackmd() can check the size) */
104         body = lustre_msg_buf(request->rq_repmsg, offset, sizeof (*body));
105         LASSERT (body != NULL);
106         LASSERT_REPSWABBED (request, offset);
107
108         if (body->valid & OBD_MD_FLEASIZE) {
109                 /* Only bother with this if inodes's LSM not set? */
110
111                 if (body->eadatasize == 0) {
112                         CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
113                         GOTO (out, rc = -EPROTO);
114                 }
115                 lmmsize = body->eadatasize;
116                 lmm = lustre_msg_buf (request->rq_repmsg, offset + 1, lmmsize);
117                 LASSERT (lmm != NULL);
118                 LASSERT_REPSWABBED (request, offset + 1);
119
120                 rc = obd_unpackmd (&sbi->ll_osc_conn,
121                                    &lsm, lmm, lmmsize);
122                 if (rc < 0) {
123                         CERROR ("Error %d unpacking eadata\n", rc);
124                         LBUG();
125                         /* XXX don't know if I should do this... */
126                         GOTO (out, rc);
127                         /* or skip the ll_update_inode but still do
128                          * mdc_lock_set_inode() */
129                 }
130                 LASSERT (rc >= sizeof (*lsm));
131                 rc = 0;
132         }
133
134         llu_update_inode(pb->pb_ino, body, lsm);
135
136         if (lsm != NULL &&
137             llu_i2info(pb->pb_ino)->lli_smd != lsm)
138                 obd_free_memmd (&sbi->ll_osc_conn, &lsm);
139
140         llu_mdc_lock_set_inode((struct lustre_handle *)&it->d.lustre.it_lock_handle,
141                                pb->pb_ino);
142  out:
143         RETURN(rc);
144 }
145 #endif
146
147 /*
148  * remove the stale inode from pnode
149  */
150 void unhook_stale_inode(struct pnode *pno)
151 {
152         struct inode *inode = pno->p_base->pb_ino;
153         ENTRY;
154
155         LASSERT(inode);
156         LASSERT(llu_i2info(inode)->lli_stale_flag);
157
158         pno->p_base->pb_ino = NULL;
159
160         if (!llu_i2info(inode)->lli_open_count) {
161                 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
162                                 inode, llu_i2info(inode)->lli_st_ino, pno);
163                 I_RELE(inode);
164                 if (!inode->i_ref)
165                         _sysio_i_gone(inode);
166         }
167
168         EXIT;
169         return;
170 }
171
172 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
173 {
174         LASSERT(it);
175         LASSERT(pnode);
176
177         if (it && pnode->p_base->pb_ino != NULL) {
178                 struct inode *inode = pnode->p_base->pb_ino;
179                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%lu)\n",
180                        inode, llu_i2info(inode)->lli_st_ino,
181                        llu_i2info(inode)->lli_st_generation);
182                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
183         }
184
185         /* drop IT_LOOKUP locks */
186         if (it->it_op == IT_LOOKUP)
187                 ll_intent_release(it);
188
189 }
190
191 static inline void ll_invalidate_inode_pages(struct inode * inode)
192 {
193         /* do nothing */
194 }
195
196 static int llu_mdc_blocking_ast(struct ldlm_lock *lock,
197                                 struct ldlm_lock_desc *desc,
198                                 void *data, int flag)
199 {
200         int rc;
201         struct lustre_handle lockh;
202         ENTRY;
203
204
205         switch (flag) {
206         case LDLM_CB_BLOCKING:
207                 ldlm_lock2handle(lock, &lockh);
208                 rc = ldlm_cli_cancel(&lockh);
209                 if (rc < 0) {
210                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
211                         RETURN(rc);
212                 }
213                 break;
214         case LDLM_CB_CANCELING: {
215                 struct inode *inode = llu_inode_from_lock(lock);
216                 struct llu_inode_info *lli;
217
218                 /* Invalidate all dentries associated with this inode */
219                 if (inode == NULL)
220                         break;
221
222                 lli =  llu_i2info(inode);
223
224                 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
225
226                 if (lock->l_resource->lr_name.name[0] != lli->lli_st_ino ||
227                     lock->l_resource->lr_name.name[1] != lli->lli_st_generation) {
228                         LDLM_ERROR(lock, "data mismatch with ino %lu/%lu",
229                                    lli->lli_st_ino, lli->lli_st_generation);
230                 }
231                 if (S_ISDIR(lli->lli_st_mode)) {
232                         CDEBUG(D_INODE, "invalidating inode %lu\n",
233                                lli->lli_st_ino);
234
235                         ll_invalidate_inode_pages(inode);
236                 }
237
238 /*
239                 if (inode->i_sb->s_root &&
240                     inode != inode->i_sb->s_root->d_inode)
241                         ll_unhash_aliases(inode);
242 */
243                 I_RELE(inode);
244                 break;
245         }
246         default:
247                 LBUG();
248         }
249
250         RETURN(0);
251 }
252
253 int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
254 {
255         struct pnode_base *pb = pnode->p_base;
256         struct ll_fid pfid, cfid;
257         struct it_cb_data icbd;
258         struct ll_uctxt ctxt;
259         struct ptlrpc_request *req = NULL;
260         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
261         struct obd_export *exp;
262         int rc;
263         ENTRY;
264
265         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%x\n",
266                pb->pb_name.name, it ? it->it_op : 0);
267
268         /* We don't want to cache negative dentries, so return 0 immediately.
269          * We believe that this is safe, that negative dentries cannot be
270          * pinned by someone else */
271         if (pb->pb_ino == NULL) {
272                 CDEBUG(D_INODE, "negative pb\n");
273                 RETURN(0);
274         }
275
276         /* check stale inode */
277         if (llu_i2info(pb->pb_ino)->lli_stale_flag)
278                 unhook_stale_inode(pnode);
279
280         /* check again because unhook_stale_inode() might generate
281          * negative pnode */
282         if (pb->pb_ino == NULL) {
283                 CDEBUG(D_INODE, "negative pb\n");
284                 RETURN(0);
285         }
286
287         /* This is due to bad interaction with libsysio. remove this when we
288          * switched to libbsdio
289          */
290         {
291                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
292                 if (lli->lli_it) {
293                         CDEBUG(D_INODE, "inode %lu still have intent "
294                                         "%p(opc 0x%x), release it\n",
295                                         lli->lli_st_ino, lli->lli_it,
296                                         lli->lli_it->it_op);
297                         ll_intent_release(lli->lli_it);
298                         OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
299                         lli->lli_it = NULL;
300                 }
301         }
302
303         exp = llu_i2mdcexp(pb->pb_ino);
304         ll_inode2fid(&pfid, pnode->p_parent->p_base->pb_ino);
305         ll_inode2fid(&cfid, pb->pb_ino);
306         icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
307         icbd.icbd_child = pnode;
308
309         if (!it) {
310                 it = &lookup_it;
311                 it->it_op_release = ll_intent_release;
312         }
313
314         ll_i2uctxt(&ctxt, pnode->p_parent->p_base->pb_ino, pb->pb_ino);
315
316         rc = mdc_intent_lock(exp, &ctxt, &pfid,
317                              pb->pb_name.name, pb->pb_name.len,
318                              NULL, 0, &cfid, it, flags, &req,
319                              llu_mdc_blocking_ast);
320         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
321          * if all was well, it will return 1 if it found locks, 0 otherwise. */
322         if (req == NULL && rc >= 0)
323                 GOTO(out, rc);
324
325         /* unfortunately ll_intent_lock may cause a callback and revoke our
326            dentry */
327         /*
328         spin_lock(&dcache_lock);
329         list_del_init(&de->d_hash);
330         spin_unlock(&dcache_lock);
331         d_rehash(de);
332         */
333         if (it->it_op & (IT_OPEN | IT_GETATTR))
334                 LL_SAVE_INTENT(pb->pb_ino, it);
335         RETURN(1);
336  out:
337         if (req)
338                 ptlrpc_req_finished(req);
339         if (rc == 0) {
340                 LASSERT(pb->pb_ino);
341                 if (S_ISDIR(llu_i2info(pb->pb_ino)->lli_st_mode))
342                         ll_invalidate_inode_pages(pb->pb_ino);
343                 llu_i2info(pb->pb_ino)->lli_stale_flag = 1;
344                 unhook_stale_inode(pnode);
345         } else {
346                 llu_lookup_finish_locks(it, pnode);
347                 llu_i2info(pb->pb_ino)->lli_stale_flag = 0;
348                 if (it->it_op & (IT_OPEN | IT_GETATTR))
349                         LL_SAVE_INTENT(pb->pb_ino, it);
350         }
351         RETURN(rc);
352 }
353
354 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
355                             struct lookup_intent *it, void *data)
356 {
357         struct it_cb_data *icbd = data;
358         struct pnode *child = icbd->icbd_child;
359         struct inode *parent = icbd->icbd_parent;
360         struct llu_sb_info *sbi = llu_i2sbi(parent);
361         struct inode *inode = NULL;
362         int rc;
363
364         /* NB 1 request reference will be taken away by ll_intent_lock()
365          * when I return */
366         /* XXX libsysio require the inode must be generated here XXX */
367         if ((it->it_op & IT_CREAT) || !it_disposition(it, DISP_LOOKUP_NEG)) {
368                 struct lustre_md md;
369                 struct llu_inode_info *lli;
370                 ENTRY;
371
372                 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
373                 if (rc)
374                         RETURN(rc);
375
376                 inode = llu_iget(parent->i_fs, &md);
377                 if (!inode) {
378                         /* free the lsm if we allocated one above */
379                         if (md.lsm != NULL)
380                                 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
381                         RETURN(-ENOMEM);
382                 } else if (md.lsm != NULL &&
383                            llu_i2info(inode)->lli_smd != md.lsm) {
384                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
385                 }
386
387                 lli = llu_i2info(inode);
388
389                 /* If this is a stat, get the authoritative file size */
390                 if (it->it_op == IT_GETATTR && S_ISREG(lli->lli_st_mode) &&
391                     lli->lli_smd != NULL) {
392                         struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
393                         struct lustre_handle lockh = {0};
394                         struct lov_stripe_md *lsm = lli->lli_smd;
395                         ldlm_error_t rc;
396
397                         LASSERT(lsm->lsm_object_id != 0);
398
399                         rc = llu_extent_lock(NULL, inode, lsm, LCK_PR, &extent,
400                                             &lockh);
401                         if (rc != ELDLM_OK) {
402                                 I_RELE(inode);
403                                 RETURN(-EIO);
404                         }
405                         llu_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
406                 }
407         } else {
408                 ENTRY;
409         }
410
411         if (inode && (it->it_op & (IT_OPEN | IT_GETATTR)))
412                 LL_SAVE_INTENT(inode, it);
413 /*
414         dentry->d_op = &ll_d_ops;
415         ll_set_dd(dentry);
416
417         if (dentry == saved)
418                 d_add(dentry, inode);
419 */
420         child->p_base->pb_ino = inode;
421
422         RETURN(0);
423 }
424
425 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
426 {
427         struct inode *inode;
428         l_lock(&lock->l_resource->lr_namespace->ns_lock);
429
430         if (lock->l_ast_data) {
431                 inode = (struct inode *)lock->l_ast_data;
432                 I_REF(inode);
433         } else
434                 inode = NULL;
435
436         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
437         return inode;
438 }
439
440 /* XXX */
441 #define EXT2_NAME_LEN (255)
442
443 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
444                          struct lookup_intent *it, int flags)
445 {
446         struct ll_fid pfid;
447         struct ll_uctxt ctxt;
448         struct it_cb_data icbd;
449         struct ptlrpc_request *req = NULL;
450         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
451         int rc;
452         ENTRY;
453
454         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
455                 RETURN(-ENAMETOOLONG);
456
457
458 /*
459         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p),intent=%s\n",
460                dentry->d_name.name, parent->i_ino, parent->i_generation,
461                parent, LL_IT2STR(it));
462
463         if (d_mountpoint(dentry))
464                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
465
466         ll_frob_intent(&it, &lookup_it);
467 */
468
469         if (!it) {
470                 it = &lookup_it;
471                 it->it_op_release = ll_intent_release;
472         }
473
474         icbd.icbd_child = pnode;
475         icbd.icbd_parent = parent;
476         icbd.icbd_child = pnode;
477         ll_inode2fid(&pfid, parent);
478         ll_i2uctxt(&ctxt, parent, NULL);
479
480         rc = mdc_intent_lock(llu_i2mdcexp(parent), &ctxt, &pfid,
481                              pnode->p_base->pb_name.name,
482                              pnode->p_base->pb_name.len,
483                              NULL, 0, NULL, it, flags, &req,
484                              llu_mdc_blocking_ast);
485         if (rc < 0)
486                 GOTO(out, rc);
487         
488         rc = lookup_it_finish(req, 1, it, &icbd);
489         if (rc != 0) {
490                 ll_intent_release(it);
491                 GOTO(out, rc);
492         }
493
494         llu_lookup_finish_locks(it, pnode);
495
496 /*
497         if (dentry == save)
498                 GOTO(out, retval = NULL);
499         else
500                 GOTO(out, retval = dentry);
501 */
502  out:
503         if (req)
504                 ptlrpc_req_finished(req);
505         return rc;
506 }
507
508 static struct lookup_intent*
509 translate_lookup_intent(struct intent *intent, const char *path)
510 {
511         struct lookup_intent *it;
512         int fmode;
513
514         /* libsysio trick */
515         if (!intent || path) {
516                 CDEBUG(D_VFSTRACE, "not intent needed\n");
517                 return NULL;
518         }
519
520         OBD_ALLOC(it, sizeof(*it));
521         LASSERT(it);
522
523         memset(it, 0, sizeof(*it));
524
525         /* libsysio will assign intent like following:
526          * NOTE: INT_CREAT has include INT_UPDPARENT
527          *
528          * open: INT_OPEN [| INT_CREAT]
529          * mkdir: INT_CREAT
530          * symlink: INT_CREAT
531          * unlink: INT_UPDPARENT
532          * rmdir: INT_UPDPARENT
533          * mknod: INT_CREAT
534          * stat: INT_GETATTR
535          * setattr: NULL
536          *
537          * following logic is adjusted for libsysio
538          */
539
540         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
541
542         if (intent->int_opmask & INT_OPEN) {
543                 it->it_op |= IT_OPEN;
544
545                 /* convert access mode from O_ to FMODE_ */
546                 if (it->it_flags & O_WRONLY)
547                         fmode = FMODE_WRITE;
548                 else if (it->it_flags & O_RDWR)
549                         fmode = FMODE_READ | FMODE_WRITE;
550                 else
551                         fmode = FMODE_READ;
552                 it->it_flags &= ~O_ACCMODE;
553                 it->it_flags |= fmode;
554         }
555
556         /*
557         else if (intent->int_opmask & INT_CREAT)
558                 it->it_op |= IT_LOOKUP;
559          */
560
561         /* FIXME libsysio has strange code on intent handling,
562          * more check later */
563         if (it->it_flags & O_CREAT) {
564                 it->it_op |= IT_CREAT;
565                 it->it_create_mode = *((int*)intent->int_arg1);
566         }
567
568         if (intent->int_opmask & INT_GETATTR)
569                 it->it_op |= IT_GETATTR;
570         /* XXX */
571         if (intent->int_opmask & INT_SETATTR)
572                 LBUG();
573
574         /* libsysio is different to linux vfs when doing unlink/rmdir,
575          * INT_UPDPARENT was passed down during name resolution. Here
576          * we treat it as normal lookup, later unlink()/rmdir() will
577          * do the actual work */
578
579         /* conform to kernel code, if only IT_LOOKUP was set, don't
580          * pass down it */
581         if (!it->it_op || it->it_op == IT_LOOKUP) {
582                 OBD_FREE(it, sizeof(*it));
583                 it = NULL;
584         }
585         if (it)
586                 it->it_op_release = ll_intent_release;
587
588         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
589         return it;
590 }
591
592 int llu_iop_lookup(struct pnode *pnode,
593                    struct inode **inop,
594                    struct intent *intnt,
595                    const char *path)
596 {
597         struct lookup_intent *it;
598         int rc;
599         ENTRY;
600
601         *inop = NULL;
602
603         /* the mount root inode have no name, so don't call
604          * remote in this case. but probably we need revalidate
605          * it here? FIXME */
606         if (pnode->p_mount->mnt_root == pnode) {
607                 struct inode *i = pnode->p_base->pb_ino;
608                 *inop = i;
609                 return 0;
610         }
611
612         if (!pnode->p_base->pb_name.len)
613                 RETURN(-EINVAL);
614
615         it = translate_lookup_intent(intnt, path);
616
617         /* param flags is not used, let it be 0 */
618         if (llu_pb_revalidate(pnode, 0, it)) {
619                 LASSERT(pnode->p_base->pb_ino);
620                 *inop = pnode->p_base->pb_ino;
621                 RETURN(0);
622         }
623
624         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
625         if (!rc) {
626                 if (!pnode->p_base->pb_ino)
627                         rc = -ENOENT;
628                 else
629                         *inop = pnode->p_base->pb_ino;
630         }
631
632         RETURN(rc);
633 }
634