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