Whamcloud - gitweb
3d5a7ee0c4e512b2d7df915d7c4fa71f0e9eebd4
[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 #include <sysio.h>
36 #ifdef HAVE_XTIO_H
37 #include <xtio.h>
38 #endif
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 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
79 {
80         struct inode *inode;
81         LASSERT(it);
82         LASSERT(pnode);
83
84         inode = pnode->p_base->pb_ino;
85         if (it->d.lustre.it_lock_mode && inode != NULL) {
86                 struct llu_sb_info *sbi;
87
88                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
89                        inode, (long long)llu_i2stat(inode)->st_ino,
90                        llu_i2info(inode)->lli_st_generation);
91
92                 sbi = llu_i2sbi(inode);
93                 md_set_lock_data(sbi->ll_md_exp,
94                                  &it->d.lustre.it_lock_handle, inode);
95         }
96
97         /* drop lookup/getattr locks */
98         if (it->it_op & (IT_LOOKUP | IT_GETATTR))
99                 ll_intent_release(it);
100
101 }
102
103 static inline void llu_invalidate_inode_pages(struct inode * inode)
104 {
105         /* do nothing */
106 }
107
108 int llu_md_blocking_ast(struct ldlm_lock *lock,
109                         struct ldlm_lock_desc *desc,
110                         void *data, int flag)
111 {
112         struct lustre_handle lockh;
113         int rc;
114         ENTRY;
115
116
117         switch (flag) {
118         case LDLM_CB_BLOCKING:
119                 ldlm_lock2handle(lock, &lockh);
120                 rc = ldlm_cli_cancel(&lockh);
121                 if (rc < 0) {
122                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
123                         RETURN(rc);
124                 }
125                 break;
126         case LDLM_CB_CANCELING: {
127                 struct inode *inode = llu_inode_from_lock(lock);
128                 struct llu_inode_info *lli;
129                 struct intnl_stat *st;
130                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
131                 struct lu_fid *fid;
132
133                 /* Invalidate all dentries associated with this inode */
134                 if (inode == NULL)
135                         break;
136
137                 lli =  llu_i2info(inode);
138                 st = llu_i2stat(inode);
139
140                 if (bits & MDS_INODELOCK_UPDATE)
141                         lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
142
143                 fid = &lli->lli_fid;
144                 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
145                     lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
146                     lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
147                         LDLM_ERROR(lock,"data mismatch with ino %llu/%llu/%llu",
148                                   (long long)fid_seq(fid), 
149                                   (long long)fid_oid(fid),
150                                   (long long)fid_ver(fid));
151                 }
152                 if (S_ISDIR(st->st_mode) &&
153                     (bits & MDS_INODELOCK_UPDATE)) {
154                         CDEBUG(D_INODE, "invalidating inode %llu\n",
155                                (long long)st->st_ino);
156
157                         llu_invalidate_inode_pages(inode);
158                 }
159
160 /*
161                 if (inode->i_sb->s_root &&
162                     inode != inode->i_sb->s_root->d_inode)
163                         ll_unhash_aliases(inode);
164 */
165                 I_RELE(inode);
166                 break;
167         }
168         default:
169                 LBUG();
170         }
171
172         RETURN(0);
173 }
174
175 static int pnode_revalidate_finish(struct ptlrpc_request *req,
176                                    int offset,
177                                    struct lookup_intent *it,
178                                    struct pnode *pnode)
179 {
180         struct inode *inode = pnode->p_base->pb_ino;
181         struct lustre_md md;
182         int rc = 0;
183         ENTRY;
184
185         LASSERT(inode);
186
187         if (!req)
188                 RETURN(0);
189
190         if (it_disposition(it, DISP_LOOKUP_NEG))
191                 RETURN(-ENOENT);
192
193         rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
194                               offset, llu_i2sbi(inode)->ll_dt_exp, 
195                               llu_i2sbi(inode)->ll_md_exp, &md);
196         if (rc)
197                 RETURN(rc);
198
199         llu_update_inode(inode, md.body, md.lsm);
200
201         RETURN(rc);
202 }
203
204 static int llu_pb_revalidate(struct pnode *pnode, int flags,
205                              struct lookup_intent *it)
206 {
207         struct pnode_base *pb = pnode->p_base;
208         struct it_cb_data icbd;
209         struct md_op_data op_data;
210         struct ptlrpc_request *req = NULL;
211         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
212         struct obd_export *exp;
213         int rc;
214         ENTRY;
215
216         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
217                (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
218
219         /* We don't want to cache negative dentries, so return 0 immediately.
220          * We believe that this is safe, that negative dentries cannot be
221          * pinned by someone else */
222         if (pb->pb_ino == NULL) {
223                 CDEBUG(D_INODE, "negative pb\n");
224                 RETURN(0);
225         }
226
227         /* This is due to bad interaction with libsysio. remove this when we
228          * switched to libbsdio XXX
229          */
230         {
231                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
232                 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
233                 if (lli->lli_it) {
234                         CDEBUG(D_INODE, "inode %llu still have intent "
235                                         "%p(opc 0x%x), release it\n",
236                                         (long long) st->st_ino, lli->lli_it,
237                                         lli->lli_it->it_op);
238                         ll_intent_release(lli->lli_it);
239                         OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
240                         lli->lli_it = NULL;
241                 }
242         }
243
244         exp = llu_i2mdcexp(pb->pb_ino);
245         icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
246         icbd.icbd_child = pnode;
247
248         if (!it) {
249                 it = &lookup_it;
250                 it->it_op_release = ll_intent_release;
251         }
252
253         llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
254                             pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
255                             0, LUSTRE_OPC_ANY);
256
257         rc = md_intent_lock(exp, &op_data, NULL, 0, it, flags,
258                             &req, llu_md_blocking_ast,
259                             LDLM_FL_CANCEL_ON_BLOCK);
260         /* If req is NULL, then md_intent_lock only tried to do a lock match;
261          * if all was well, it will return 1 if it found locks, 0 otherwise. */
262         if (req == NULL && rc >= 0)
263                 GOTO(out, rc);
264
265         if (rc < 0)
266                 GOTO(out, rc = 0);
267
268         rc = pnode_revalidate_finish(req, DLM_REPLY_REC_OFF, it, pnode);
269         if (rc != 0) {
270                 ll_intent_release(it);
271                 GOTO(out, rc = 0);
272         }
273         rc = 1;
274
275         /* Note: ll_intent_lock may cause a callback, check this! */
276
277         if (it->it_op & IT_OPEN)
278                 LL_SAVE_INTENT(pb->pb_ino, it);
279
280  out:
281         if (req && rc == 1)
282                 ptlrpc_req_finished(req);
283         if (rc == 0) {
284                 LASSERT(pb->pb_ino);
285                 I_RELE(pb->pb_ino);
286                 pb->pb_ino = NULL;
287         } else {
288                 llu_lookup_finish_locks(it, pnode);
289         }
290         RETURN(rc);
291 }
292
293 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
294                             struct lookup_intent *it, void *data)
295 {
296         struct it_cb_data *icbd = data;
297         struct pnode *child = icbd->icbd_child;
298         struct inode *parent = icbd->icbd_parent;
299         struct llu_sb_info *sbi = llu_i2sbi(parent);
300         struct inode *inode = NULL;
301         int rc;
302
303         /* libsysio require us generate inode right away if success.
304          * so if mds created new inode for us we need make sure it
305          * succeeded. thus for any error we can't delay to the
306          * llu_file_open() time. */
307         if (it_disposition(it, DISP_OPEN_CREATE) &&
308             it_open_error(DISP_OPEN_CREATE, it)) {
309                 CDEBUG(D_INODE, "detect mds create error\n");
310                 return it_open_error(DISP_OPEN_CREATE, it);
311         }
312         if (it_disposition(it, DISP_OPEN_OPEN) &&
313             it_open_error(DISP_OPEN_OPEN, it)) {
314                 CDEBUG(D_INODE, "detect mds open error\n");
315                 /* undo which did by md_intent_lock */
316                 if (it_disposition(it, DISP_OPEN_CREATE) &&
317                     !it_open_error(DISP_OPEN_CREATE, it)) {
318                         LASSERT(request);
319                         LASSERT(atomic_read(&request->rq_refcount) > 1);
320                         CDEBUG(D_INODE, "dec a ref of req %p\n", request);
321                         ptlrpc_req_finished(request);
322                 }
323                 return it_open_error(DISP_OPEN_OPEN, it);
324         }
325
326         /* NB 1 request reference will be taken away by ll_intent_lock()
327          * when I return
328          */
329         if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
330                 struct lustre_md md;
331                 struct llu_inode_info *lli;
332                 struct intnl_stat *st;
333                 ENTRY;
334
335                 if (it_disposition(it, DISP_OPEN_CREATE))
336                         ptlrpc_req_finished(request);
337
338                 rc = md_get_lustre_md(sbi->ll_md_exp, request, offset,
339                                       sbi->ll_dt_exp, sbi->ll_md_exp, &md);
340                 if (rc)
341                         RETURN(rc);
342
343                 inode = llu_iget(parent->i_fs, &md);
344                 if (!inode || IS_ERR(inode)) {
345                         /* free the lsm if we allocated one above */
346                         if (md.lsm != NULL)
347                                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
348                         RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
349                 } else if (md.lsm != NULL &&
350                            llu_i2info(inode)->lli_smd != md.lsm) {
351                         obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
352                 }
353
354                 lli = llu_i2info(inode);
355                 st = llu_i2stat(inode);
356
357                 /* If this is a stat, get the authoritative file size */
358                 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
359                     lli->lli_smd != NULL) {
360                         struct lov_stripe_md *lsm = lli->lli_smd;
361                         ldlm_error_t rc;
362
363                         LASSERT(lsm->lsm_object_id != 0);
364
365                         /* bug 2334: drop MDS lock before acquiring OST lock */
366                         ll_intent_drop_lock(it);
367
368                         rc = llu_glimpse_size(inode);
369                         if (rc) {
370                                 I_RELE(inode);
371                                 RETURN(rc);
372                         }
373                 }
374         } else {
375                 ENTRY;
376         }
377
378         /* intent will be further used in cases of open()/getattr() */
379         if (inode && (it->it_op & IT_OPEN))
380                 LL_SAVE_INTENT(inode, it);
381
382         child->p_base->pb_ino = inode;
383
384         RETURN(0);
385 }
386
387 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
388 {
389         struct inode *inode;
390         lock_res_and_lock(lock);
391
392         if (lock->l_ast_data) {
393                 inode = (struct inode *)lock->l_ast_data;
394                 I_REF(inode);
395         } else
396                 inode = NULL;
397
398         unlock_res_and_lock(lock);
399         return inode;
400 }
401
402 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
403                          struct lookup_intent *it, int flags)
404 {
405         struct md_op_data op_data;
406         struct it_cb_data icbd;
407         struct ptlrpc_request *req = NULL;
408         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
409         __u32 opc;
410         int rc;
411         ENTRY;
412
413         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
414                 RETURN(-ENAMETOOLONG);
415
416         if (!it) {
417                 it = &lookup_it;
418                 it->it_op_release = ll_intent_release;
419         }
420
421         icbd.icbd_child = pnode;
422         icbd.icbd_parent = parent;
423
424         if (it->it_op & IT_CREAT || 
425             (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
426                 opc = LUSTRE_OPC_CREATE;
427         } else {
428                 opc = LUSTRE_OPC_ANY;
429         }
430         
431         llu_prep_md_op_data(&op_data, parent, NULL,
432                             pnode->p_base->pb_name.name,
433                             pnode->p_base->pb_name.len, flags, opc);
434
435         rc = md_intent_lock(llu_i2mdcexp(parent), &op_data, NULL, 0, it,
436                             flags, &req, llu_md_blocking_ast,
437                             LDLM_FL_CANCEL_ON_BLOCK);
438         if (rc < 0)
439                 GOTO(out, rc);
440
441         rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, 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 }