Whamcloud - gitweb
Branch b1_4_mountconf
[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 static 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 static 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, 1, 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) ||
344             (it->it_op & IT_CREAT)) {
345                 struct lustre_md md;
346                 struct llu_inode_info *lli;
347                 struct intnl_stat *st;
348                 ENTRY;
349
350                 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
351                 if (rc)
352                         RETURN(rc);
353
354                 inode = llu_iget(parent->i_fs, &md);
355                 if (!inode || IS_ERR(inode)) {
356                         /* free the lsm if we allocated one above */
357                         if (md.lsm != NULL)
358                                 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
359                         RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
360                 } else if (md.lsm != NULL &&
361                            llu_i2info(inode)->lli_smd != md.lsm) {
362                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
363                 }
364
365                 lli = llu_i2info(inode);
366                 st = llu_i2stat(inode);
367
368                 /* If this is a stat, get the authoritative file size */
369                 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
370                     lli->lli_smd != NULL) {
371                         struct lov_stripe_md *lsm = lli->lli_smd;
372                         ldlm_error_t rc;
373
374                         LASSERT(lsm->lsm_object_id != 0);
375
376                         /* bug 2334: drop MDS lock before acquiring OST lock */
377                         ll_intent_drop_lock(it);
378
379                         rc = llu_glimpse_size(inode);
380                         if (rc) {
381                                 I_RELE(inode);
382                                 RETURN(rc);
383                         }
384                 }
385         } else {
386                 ENTRY;
387         }
388
389         /* intent will be further used in cases of open()/getattr() */
390         if (inode && (it->it_op & IT_OPEN))
391                 LL_SAVE_INTENT(inode, it);
392
393         child->p_base->pb_ino = inode;
394
395         RETURN(0);
396 }
397
398 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
399 {
400         struct inode *inode;
401         l_lock(&lock->l_resource->lr_namespace->ns_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         l_unlock(&lock->l_resource->lr_namespace->ns_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 mdc_op_data op_data;
417         struct it_cb_data icbd;
418         struct ptlrpc_request *req = NULL;
419         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
420         int rc;
421         ENTRY;
422
423         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
424                 RETURN(-ENAMETOOLONG);
425
426         if (!it) {
427                 it = &lookup_it;
428                 it->it_op_release = ll_intent_release;
429         }
430
431         icbd.icbd_child = pnode;
432         icbd.icbd_parent = parent;
433
434         llu_prepare_mdc_op_data(&op_data, parent, NULL,
435                                 pnode->p_base->pb_name.name,
436                                 pnode->p_base->pb_name.len, flags);
437
438         rc = mdc_intent_lock(llu_i2mdcexp(parent), &op_data, NULL, 0, it,
439                              flags, &req, llu_mdc_blocking_ast,
440                              LDLM_FL_CANCEL_ON_BLOCK);
441         if (rc < 0)
442                 GOTO(out, rc);
443
444         rc = lookup_it_finish(req, 1, it, &icbd);
445         if (rc != 0) {
446                 ll_intent_release(it);
447                 GOTO(out, rc);
448         }
449
450         llu_lookup_finish_locks(it, pnode);
451
452  out:
453         if (req)
454                 ptlrpc_req_finished(req);
455         return rc;
456 }
457
458 static struct lookup_intent*
459 translate_lookup_intent(struct intent *intent, const char *path)
460 {
461         struct lookup_intent *it;
462         int fmode;
463
464         /* libsysio trick */
465         if (!intent || path) {
466                 CDEBUG(D_VFSTRACE, "not intent needed\n");
467                 return NULL;
468         }
469
470         OBD_ALLOC(it, sizeof(*it));
471         LASSERT(it);
472
473         memset(it, 0, sizeof(*it));
474
475         /* libsysio will assign intent like following:
476          * NOTE: INT_CREAT has include INT_UPDPARENT
477          *
478          * open: INT_OPEN [| INT_CREAT]
479          * mkdir: INT_CREAT
480          * symlink: INT_CREAT
481          * unlink: INT_UPDPARENT
482          * rmdir: INT_UPDPARENT
483          * mknod: INT_CREAT
484          * stat: INT_GETATTR
485          * setattr: NULL
486          *
487          * following logic is adjusted for libsysio
488          */
489
490         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
491
492         if (intent->int_opmask & INT_OPEN) {
493                 it->it_op |= IT_OPEN;
494
495                 /* convert access mode from O_ to FMODE_ */
496                 if (it->it_flags & O_WRONLY)
497                         fmode = FMODE_WRITE;
498                 else if (it->it_flags & O_RDWR)
499                         fmode = FMODE_READ | FMODE_WRITE;
500                 else
501                         fmode = FMODE_READ;
502                 it->it_flags &= ~O_ACCMODE;
503                 it->it_flags |= fmode;
504         }
505
506         /* XXX libsysio has strange code on intent handling,
507          * more check later */
508         if (it->it_flags & O_CREAT) {
509                 it->it_op |= IT_CREAT;
510                 it->it_create_mode = *((int*)intent->int_arg1);
511                 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
512                    this lookup to return negative result, but then there is no
513                    way to find out original intent in ll_iop_open(). So we just
514                    clear O_EXCL from libsysio flags here to avoid checking
515                    for negative result. O_EXCL will be enforced by MDS. */
516                 *((int*)intent->int_arg2) &= ~O_EXCL;
517         }
518
519         if (intent->int_opmask & INT_GETATTR)
520                 it->it_op |= IT_GETATTR;
521
522         LASSERT(!(intent->int_opmask & INT_SETATTR));
523
524         /* libsysio is different to linux vfs when doing unlink/rmdir,
525          * INT_UPDPARENT was passed down during name resolution. Here
526          * we treat it as normal lookup, later unlink()/rmdir() will
527          * do the actual work */
528
529         /* conform to kernel code, if only IT_LOOKUP was set, don't
530          * pass down it */
531         if (!it->it_op || it->it_op == IT_LOOKUP) {
532                 OBD_FREE(it, sizeof(*it));
533                 it = NULL;
534         }
535         if (it)
536                 it->it_op_release = ll_intent_release;
537
538         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
539         return it;
540 }
541
542 int llu_iop_lookup(struct pnode *pnode,
543                    struct inode **inop,
544                    struct intent *intnt,
545                    const char *path)
546 {
547         struct lookup_intent *it;
548         int rc;
549         ENTRY;
550
551         liblustre_wait_event(0);
552
553         *inop = NULL;
554
555         /* the mount root inode have no name, so don't call
556          * remote in this case. but probably we need revalidate
557          * it here? FIXME */
558         if (pnode->p_mount->mnt_root == pnode) {
559                 struct inode *i = pnode->p_base->pb_ino;
560                 *inop = i;
561                 RETURN(0);
562         }
563
564         if (!pnode->p_base->pb_name.len)
565                 RETURN(-EINVAL);
566
567         it = translate_lookup_intent(intnt, path);
568
569         /* param flags is not used, let it be 0 */
570         if (llu_pb_revalidate(pnode, 0, it)) {
571                 LASSERT(pnode->p_base->pb_ino);
572                 *inop = pnode->p_base->pb_ino;
573                 GOTO(out, rc = 0);
574         }
575
576         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
577         if (!rc) {
578                 if (!pnode->p_base->pb_ino)
579                         rc = -ENOENT;
580                 else
581                         *inop = pnode->p_base->pb_ino;
582         }
583
584 out:
585         liblustre_wait_event(0);
586         RETURN(rc);
587 }