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