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
153                 /* Invalidate all dentries associated with this inode */
154                 if (inode == NULL)
155                         break;
156
157                 lli =  llu_i2info(inode);
158                 st = llu_i2stat(inode);
159
160                 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
161
162                 if (lock->l_resource->lr_name.name[0] != st->st_ino ||
163                     lock->l_resource->lr_name.name[1] !=lli->lli_st_generation){
164                         LDLM_ERROR(lock, "data mismatch with ino %llu/%lu",
165                                   (long long)st->st_ino,lli->lli_st_generation);
166                 }
167                 if (S_ISDIR(st->st_mode)) {
168                         CDEBUG(D_INODE, "invalidating inode %llu\n",
169                                (long long)st->st_ino);
170
171                         llu_invalidate_inode_pages(inode);
172                 }
173
174 /*
175                 if (inode->i_sb->s_root &&
176                     inode != inode->i_sb->s_root->d_inode)
177                         ll_unhash_aliases(inode);
178 */
179                 I_RELE(inode);
180                 break;
181         }
182         default:
183                 LBUG();
184         }
185
186         RETURN(0);
187 }
188
189 static int pnode_revalidate_finish(struct ptlrpc_request *req,
190                                    int offset,
191                                    struct lookup_intent *it,
192                                    struct pnode *pnode)
193 {
194         struct inode *inode = pnode->p_base->pb_ino;
195         struct lustre_md md;
196         int rc = 0;
197         ENTRY;
198
199         LASSERT(inode);
200
201         if (!req)
202                 RETURN(0);
203
204         if (it_disposition(it, DISP_LOOKUP_NEG))
205                 RETURN(-ENOENT);
206
207         rc = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
208         if (rc)
209                 RETURN(rc);
210
211         llu_update_inode(inode, md.body, md.lsm);
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 it_cb_data icbd;
221         struct mdc_op_data op_data;
222         struct ptlrpc_request *req = NULL;
223         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
224         struct obd_export *exp;
225         int rc;
226         ENTRY;
227
228         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
229                (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
230
231         /* We don't want to cache negative dentries, so return 0 immediately.
232          * We believe that this is safe, that negative dentries cannot be
233          * pinned by someone else */
234         if (pb->pb_ino == NULL) {
235                 CDEBUG(D_INODE, "negative pb\n");
236                 RETURN(0);
237         }
238
239         /* This is due to bad interaction with libsysio. remove this when we
240          * switched to libbsdio XXX
241          */
242         {
243                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
244                 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
245                 if (lli->lli_it) {
246                         CDEBUG(D_INODE, "inode %llu still have intent "
247                                         "%p(opc 0x%x), release it\n",
248                                         (long long) st->st_ino, lli->lli_it,
249                                         lli->lli_it->it_op);
250                         ll_intent_release(lli->lli_it);
251                         OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
252                         lli->lli_it = NULL;
253                 }
254         }
255
256         exp = llu_i2mdcexp(pb->pb_ino);
257         icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
258         icbd.icbd_child = pnode;
259
260         if (!it) {
261                 it = &lookup_it;
262                 it->it_op_release = ll_intent_release;
263         }
264
265         llu_prepare_mdc_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
266                                 pb->pb_ino, pb->pb_name.name,pb->pb_name.len,0);
267
268         rc = mdc_intent_lock(exp, &op_data, NULL, 0, it, flags,
269                              &req, llu_mdc_blocking_ast,
270                              LDLM_FL_CANCEL_ON_BLOCK);
271         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
272          * if all was well, it will return 1 if it found locks, 0 otherwise. */
273         if (req == NULL && rc >= 0)
274                 GOTO(out, rc);
275
276         if (rc < 0)
277                 GOTO(out, rc = 0);
278
279         rc = pnode_revalidate_finish(req, 1, it, pnode);
280         if (rc != 0) {
281                 ll_intent_release(it);
282                 GOTO(out, rc = 0);
283         }
284         rc = 1;
285
286         /* Note: ll_intent_lock may cause a callback, check this! */
287
288         if (it->it_op & IT_OPEN)
289                 LL_SAVE_INTENT(pb->pb_ino, it);
290
291  out:
292         if (req && rc == 1)
293                 ptlrpc_req_finished(req);
294         if (rc == 0) {
295                 LASSERT(pb->pb_ino);
296                 I_RELE(pb->pb_ino);
297                 pb->pb_ino = NULL;
298         } else {
299                 llu_lookup_finish_locks(it, pnode);
300         }
301         RETURN(rc);
302 }
303
304 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
305                             struct lookup_intent *it, void *data)
306 {
307         struct it_cb_data *icbd = data;
308         struct pnode *child = icbd->icbd_child;
309         struct inode *parent = icbd->icbd_parent;
310         struct llu_sb_info *sbi = llu_i2sbi(parent);
311         struct inode *inode = NULL;
312         int rc;
313
314         /* libsysio require us generate inode right away if success.
315          * so if mds created new inode for us we need make sure it
316          * succeeded. thus for any error we can't delay to the
317          * llu_file_open() time. */
318         if (it_disposition(it, DISP_OPEN_CREATE) &&
319             it_open_error(DISP_OPEN_CREATE, it)) {
320                 CDEBUG(D_INODE, "detect mds create error\n");
321                 return it_open_error(DISP_OPEN_CREATE, it);
322         }
323         if (it_disposition(it, DISP_OPEN_OPEN) &&
324             it_open_error(DISP_OPEN_OPEN, it)) {
325                 CDEBUG(D_INODE, "detect mds open error\n");
326                 /* undo which did by mdc_intent_lock */
327                 if (it_disposition(it, DISP_OPEN_CREATE) &&
328                     !it_open_error(DISP_OPEN_CREATE, it)) {
329                         LASSERT(request);
330                         LASSERT(atomic_read(&request->rq_refcount) > 1);
331                         CDEBUG(D_INODE, "dec a ref of req %p\n", request);
332                         ptlrpc_req_finished(request);
333                 }
334                 return it_open_error(DISP_OPEN_OPEN, it);
335         }
336
337         /* NB 1 request reference will be taken away by ll_intent_lock()
338          * when I return
339          */
340         if (!it_disposition(it, DISP_LOOKUP_NEG) ||
341             (it->it_op & IT_CREAT)) {
342                 struct lustre_md md;
343                 struct llu_inode_info *lli;
344                 struct intnl_stat *st;
345                 ENTRY;
346
347                 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
348                 if (rc)
349                         RETURN(rc);
350
351                 inode = llu_iget(parent->i_fs, &md);
352                 if (!inode || IS_ERR(inode)) {
353                         /* free the lsm if we allocated one above */
354                         if (md.lsm != NULL)
355                                 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
356                         RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
357                 } else if (md.lsm != NULL &&
358                            llu_i2info(inode)->lli_smd != md.lsm) {
359                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
360                 }
361
362                 lli = llu_i2info(inode);
363                 st = llu_i2stat(inode);
364
365                 /* If this is a stat, get the authoritative file size */
366                 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
367                     lli->lli_smd != NULL) {
368                         struct lov_stripe_md *lsm = lli->lli_smd;
369                         ldlm_error_t rc;
370
371                         LASSERT(lsm->lsm_object_id != 0);
372
373                         /* bug 2334: drop MDS lock before acquiring OST lock */
374                         ll_intent_drop_lock(it);
375
376                         rc = llu_glimpse_size(inode);
377                         if (rc) {
378                                 I_RELE(inode);
379                                 RETURN(rc);
380                         }
381                 }
382         } else {
383                 ENTRY;
384         }
385
386         /* intent will be further used in cases of open()/getattr() */
387         if (inode && (it->it_op & IT_OPEN))
388                 LL_SAVE_INTENT(inode, it);
389
390         child->p_base->pb_ino = inode;
391
392         RETURN(0);
393 }
394
395 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
396 {
397         struct inode *inode;
398         l_lock(&lock->l_resource->lr_namespace->ns_lock);
399
400         if (lock->l_ast_data) {
401                 inode = (struct inode *)lock->l_ast_data;
402                 I_REF(inode);
403         } else
404                 inode = NULL;
405
406         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
407         return inode;
408 }
409
410 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
411                          struct lookup_intent *it, int flags)
412 {
413         struct mdc_op_data op_data;
414         struct it_cb_data icbd;
415         struct ptlrpc_request *req = NULL;
416         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
417         int rc;
418         ENTRY;
419
420         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
421                 RETURN(-ENAMETOOLONG);
422
423         if (!it) {
424                 it = &lookup_it;
425                 it->it_op_release = ll_intent_release;
426         }
427
428         icbd.icbd_child = pnode;
429         icbd.icbd_parent = parent;
430
431         llu_prepare_mdc_op_data(&op_data, parent, NULL,
432                                 pnode->p_base->pb_name.name,
433                                 pnode->p_base->pb_name.len, flags);
434
435         rc = mdc_intent_lock(llu_i2mdcexp(parent), &op_data, NULL, 0, it,
436                              flags, &req, llu_mdc_blocking_ast,
437                              LDLM_FL_CANCEL_ON_BLOCK);
438         if (rc < 0)
439                 GOTO(out, rc);
440
441         rc = lookup_it_finish(req, 1, it, &icbd);
442         if (rc != 0) {
443                 ll_intent_release(it);
444                 GOTO(out, rc);
445         }
446
447         llu_lookup_finish_locks(it, pnode);
448
449  out:
450         if (req)
451                 ptlrpc_req_finished(req);
452         return rc;
453 }
454
455 static struct lookup_intent*
456 translate_lookup_intent(struct intent *intent, const char *path)
457 {
458         struct lookup_intent *it;
459         int fmode;
460
461         /* libsysio trick */
462         if (!intent || path) {
463                 CDEBUG(D_VFSTRACE, "not intent needed\n");
464                 return NULL;
465         }
466
467         OBD_ALLOC(it, sizeof(*it));
468         LASSERT(it);
469
470         memset(it, 0, sizeof(*it));
471
472         /* libsysio will assign intent like following:
473          * NOTE: INT_CREAT has include INT_UPDPARENT
474          *
475          * open: INT_OPEN [| INT_CREAT]
476          * mkdir: INT_CREAT
477          * symlink: INT_CREAT
478          * unlink: INT_UPDPARENT
479          * rmdir: INT_UPDPARENT
480          * mknod: INT_CREAT
481          * stat: INT_GETATTR
482          * setattr: NULL
483          *
484          * following logic is adjusted for libsysio
485          */
486
487         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
488
489         if (intent->int_opmask & INT_OPEN) {
490                 it->it_op |= IT_OPEN;
491
492                 /* convert access mode from O_ to FMODE_ */
493                 if (it->it_flags & O_WRONLY)
494                         fmode = FMODE_WRITE;
495                 else if (it->it_flags & O_RDWR)
496                         fmode = FMODE_READ | FMODE_WRITE;
497                 else
498                         fmode = FMODE_READ;
499                 it->it_flags &= ~O_ACCMODE;
500                 it->it_flags |= fmode;
501         }
502
503         /* XXX libsysio has strange code on intent handling,
504          * more check later */
505         if (it->it_flags & O_CREAT) {
506                 it->it_op |= IT_CREAT;
507                 it->it_create_mode = *((int*)intent->int_arg1);
508                 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
509                    this lookup to return negative result, but then there is no
510                    way to find out original intent in ll_iop_open(). So we just
511                    clear O_EXCL from libsysio flags here to avoid checking
512                    for negative result. O_EXCL will be enforced by MDS. */
513                 *((int*)intent->int_arg2) &= ~O_EXCL;
514         }
515
516         if (intent->int_opmask & INT_GETATTR)
517                 it->it_op |= IT_GETATTR;
518
519         LASSERT(!(intent->int_opmask & INT_SETATTR));
520
521         /* libsysio is different to linux vfs when doing unlink/rmdir,
522          * INT_UPDPARENT was passed down during name resolution. Here
523          * we treat it as normal lookup, later unlink()/rmdir() will
524          * do the actual work */
525
526         /* conform to kernel code, if only IT_LOOKUP was set, don't
527          * pass down it */
528         if (!it->it_op || it->it_op == IT_LOOKUP) {
529                 OBD_FREE(it, sizeof(*it));
530                 it = NULL;
531         }
532         if (it)
533                 it->it_op_release = ll_intent_release;
534
535         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
536         return it;
537 }
538
539 int llu_iop_lookup(struct pnode *pnode,
540                    struct inode **inop,
541                    struct intent *intnt,
542                    const char *path)
543 {
544         struct lookup_intent *it;
545         int rc;
546         ENTRY;
547
548         liblustre_wait_event(0);
549
550         *inop = NULL;
551
552         /* the mount root inode have no name, so don't call
553          * remote in this case. but probably we need revalidate
554          * it here? FIXME */
555         if (pnode->p_mount->mnt_root == pnode) {
556                 struct inode *i = pnode->p_base->pb_ino;
557                 *inop = i;
558                 RETURN(0);
559         }
560
561         if (!pnode->p_base->pb_name.len)
562                 RETURN(-EINVAL);
563
564         it = translate_lookup_intent(intnt, path);
565
566         /* param flags is not used, let it be 0 */
567         if (llu_pb_revalidate(pnode, 0, it)) {
568                 LASSERT(pnode->p_base->pb_ino);
569                 *inop = pnode->p_base->pb_ino;
570                 GOTO(out, rc = 0);
571         }
572
573         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
574         if (!rc) {
575                 if (!pnode->p_base->pb_ino)
576                         rc = -ENOENT;
577                 else
578                         *inop = pnode->p_base->pb_ino;
579         }
580
581 out:
582         liblustre_wait_event(0);
583         RETURN(rc);
584 }