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