Whamcloud - gitweb
liblustre: b=2862
[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, 2003 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 <sys/fcntl.h>
33 #include <sys/queue.h>
34
35 #include <sysio.h>
36 #include <fs.h>
37 #include <mount.h>
38 #include <inode.h>
39 #include <file.h>
40
41 #undef LIST_HEAD
42
43 #include "llite_lib.h"
44
45 static void ll_intent_drop_lock(struct lookup_intent *it)
46 {
47         struct lustre_handle *handle;
48
49         if (it->it_op && it->d.lustre.it_lock_mode) {
50                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
51                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
52                        " from it %p\n", handle->cookie, it);
53                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
54
55                 /* bug 494: intent_release may be called multiple times, from
56                  * this thread and we don't want to double-decref this lock */
57                 it->d.lustre.it_lock_mode = 0;
58         }
59 }
60
61 static void ll_intent_release(struct lookup_intent *it)
62 {
63         ENTRY;
64
65         ll_intent_drop_lock(it);
66         it->it_magic = 0;
67         it->it_op_release = 0;
68         it->d.lustre.it_disposition = 0;
69         it->d.lustre.it_data = NULL;
70         EXIT;
71 }
72
73 /*
74  * remove the stale inode from pnode
75  */
76 void unhook_stale_inode(struct pnode *pno)
77 {
78         struct inode *inode = pno->p_base->pb_ino;
79         ENTRY;
80
81         LASSERT(inode);
82         LASSERT(llu_i2info(inode)->lli_stale_flag);
83
84         pno->p_base->pb_ino = NULL;
85         I_RELE(inode);
86
87         if (!llu_i2info(inode)->lli_open_count) {
88                 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
89                                 inode, llu_i2info(inode)->lli_st_ino, pno);
90                 if (!inode->i_ref)
91                         _sysio_i_gone(inode);
92         }
93
94         EXIT;
95         return;
96 }
97
98 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
99 {
100         LASSERT(it);
101         LASSERT(pnode);
102
103         if (it && pnode->p_base->pb_ino != NULL) {
104                 struct inode *inode = pnode->p_base->pb_ino;
105                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%lu)\n",
106                        inode, llu_i2info(inode)->lli_st_ino,
107                        llu_i2info(inode)->lli_st_generation);
108                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
109         }
110
111         /* drop lookup/getattr locks */
112         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
113                 ll_intent_release(it);
114
115 }
116
117 static inline void llu_invalidate_inode_pages(struct inode * inode)
118 {
119         /* do nothing */
120 }
121
122 int llu_mdc_blocking_ast(struct ldlm_lock *lock,
123                          struct ldlm_lock_desc *desc,
124                          void *data, int flag)
125 {
126         int rc;
127         struct lustre_handle lockh;
128         ENTRY;
129
130
131         switch (flag) {
132         case LDLM_CB_BLOCKING:
133                 ldlm_lock2handle(lock, &lockh);
134                 rc = ldlm_cli_cancel(&lockh);
135                 if (rc < 0) {
136                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
137                         RETURN(rc);
138                 }
139                 break;
140         case LDLM_CB_CANCELING: {
141                 struct inode *inode = llu_inode_from_lock(lock);
142                 struct llu_inode_info *lli;
143                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
144
145                 /* Invalidate all dentries associated with this inode */
146                 if (inode == NULL)
147                         break;
148
149                 lli =  llu_i2info(inode);
150
151                 if (bits & MDS_INODELOCK_UPDATE)
152                         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
153
154                 if (lock->l_resource->lr_name.name[0] != lli->lli_st_ino ||
155                     lock->l_resource->lr_name.name[1] != lli->lli_st_generation) {
156                         LDLM_ERROR(lock, "data mismatch with ino %lu/%lu",
157                                    lli->lli_st_ino, lli->lli_st_generation);
158                 }
159                 if (S_ISDIR(lli->lli_st_mode) &&
160                     (bits & MDS_INODELOCK_UPDATE)) {
161                         CDEBUG(D_INODE, "invalidating inode %lu\n",
162                                lli->lli_st_ino);
163
164                         llu_invalidate_inode_pages(inode);
165                 }
166
167 /*
168                 if (inode->i_sb->s_root &&
169                     inode != inode->i_sb->s_root->d_inode)
170                         ll_unhash_aliases(inode);
171 */
172                 I_RELE(inode);
173                 break;
174         }
175         default:
176                 LBUG();
177         }
178
179         RETURN(0);
180 }
181
182 static int pnode_revalidate_finish(struct ptlrpc_request *req,
183                                    int offset,
184                                    struct lookup_intent *it,
185                                    struct pnode *pnode)
186 {
187         struct inode *inode = pnode->p_base->pb_ino;
188         struct lustre_md md;
189         int rc = 0;
190         ENTRY;
191
192         LASSERT(inode);
193
194         if (!req)
195                 RETURN(0);
196
197         if (it_disposition(it, DISP_LOOKUP_NEG))
198                 RETURN(-ENOENT);
199
200         rc = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
201         if (rc)
202                 RETURN(rc);
203
204         llu_update_inode(inode, md.body, md.lsm);
205
206         RETURN(rc);
207 }
208
209 int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
210 {
211         struct pnode_base *pb = pnode->p_base;
212         struct ll_fid pfid, cfid;
213         struct it_cb_data icbd;
214         struct ll_uctxt ctxt;
215         struct ptlrpc_request *req = NULL;
216         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
217         struct obd_export *exp;
218         int rc;
219         ENTRY;
220
221         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%x\n",
222                pb->pb_name.name, it ? it->it_op : 0);
223
224         /* We don't want to cache negative dentries, so return 0 immediately.
225          * We believe that this is safe, that negative dentries cannot be
226          * pinned by someone else */
227         if (pb->pb_ino == NULL) {
228                 CDEBUG(D_INODE, "negative pb\n");
229                 RETURN(0);
230         }
231
232         /* check stale inode */
233         if (llu_i2info(pb->pb_ino)->lli_stale_flag)
234                 unhook_stale_inode(pnode);
235
236         /* check again because unhook_stale_inode() might generate
237          * negative pnode */
238         if (pb->pb_ino == NULL) {
239                 CDEBUG(D_INODE, "negative pb\n");
240                 RETURN(0);
241         }
242
243         /* This is due to bad interaction with libsysio. remove this when we
244          * switched to libbsdio XXX
245          */
246         {
247                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
248                 if (lli->lli_it) {
249                         CDEBUG(D_INODE, "inode %lu still have intent "
250                                         "%p(opc 0x%x), release it\n",
251                                         lli->lli_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         ll_inode2fid(&pfid, pnode->p_parent->p_base->pb_ino);
261         ll_inode2fid(&cfid, pb->pb_ino);
262         icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
263         icbd.icbd_child = pnode;
264
265         if (!it) {
266                 it = &lookup_it;
267                 it->it_op_release = ll_intent_release;
268         }
269
270         ll_i2uctxt(&ctxt, pnode->p_parent->p_base->pb_ino, pb->pb_ino);
271
272         rc = mdc_intent_lock(exp, &ctxt, &pfid,
273                              pb->pb_name.name, pb->pb_name.len,
274                              NULL, 0, &cfid, it, flags, &req,
275                              llu_mdc_blocking_ast);
276         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
277          * if all was well, it will return 1 if it found locks, 0 otherwise. */
278         if (req == NULL && rc >= 0)
279                 GOTO(out, rc);
280
281         if (rc < 0)
282                 GOTO(out, rc = 0);
283
284         rc = pnode_revalidate_finish(req, 1, it, pnode);
285         if (rc != 0) {
286                 ll_intent_release(it);
287                 GOTO(out, rc = 0);
288         }
289         rc = 1;
290
291         /* Note: ll_intent_lock may cause a callback, check this! */
292
293         if (it->it_op & IT_OPEN)
294                 LL_SAVE_INTENT(pb->pb_ino, it);
295
296  out:
297         if (req && rc == 1)
298                 ptlrpc_req_finished(req);
299         if (rc == 0) {
300                 LASSERT(pb->pb_ino);
301                 I_RELE(pb->pb_ino);
302                 pb->pb_ino = NULL;
303         } else {
304                 llu_lookup_finish_locks(it, pnode);
305                 llu_i2info(pb->pb_ino)->lli_stale_flag = 0;
306         }
307         RETURN(rc);
308 }
309
310 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
311                             struct lookup_intent *it, void *data)
312 {
313         struct it_cb_data *icbd = data;
314         struct pnode *child = icbd->icbd_child;
315         struct inode *parent = icbd->icbd_parent;
316         struct llu_sb_info *sbi = llu_i2sbi(parent);
317         struct inode *inode = NULL;
318         int rc;
319
320         /* NB 1 request reference will be taken away by ll_intent_lock()
321          * when I return
322          */
323         /* FIXME: for CREAT, libsysio require the inode must be generated here
324          * currently here we don't know the whether the create is successful
325          * or failed on mds. thus blinded return -EPERM in llu_iget(). need
326          * a fix later.
327          */
328         if ((it->it_op & IT_CREAT) || !it_disposition(it, DISP_LOOKUP_NEG)) {
329                 struct lustre_md md;
330                 struct llu_inode_info *lli;
331                 ENTRY;
332
333                 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
334                 if (rc)
335                         RETURN(rc);
336
337                 inode = llu_iget(parent->i_fs, &md);
338                 if (!inode || IS_ERR(inode)) {
339                         /* free the lsm if we allocated one above */
340                         if (md.lsm != NULL)
341                                 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
342                         RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
343                 } else if (md.lsm != NULL &&
344                            llu_i2info(inode)->lli_smd != md.lsm) {
345                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
346                 }
347
348                 lli = llu_i2info(inode);
349
350                 /* If this is a stat, get the authoritative file size */
351                 if (it->it_op == IT_GETATTR && S_ISREG(lli->lli_st_mode) &&
352                     lli->lli_smd != NULL) {
353                         struct lov_stripe_md *lsm = lli->lli_smd;
354                         struct ost_lvb lvb;
355                         ldlm_error_t rc;
356
357                         LASSERT(lsm->lsm_object_id != 0);
358
359                         /* bug 2334: drop MDS lock before acquiring OST lock */
360                         ll_intent_drop_lock(it);
361
362                         rc = llu_glimpse_size(inode, &lvb);
363                         if (rc) {
364                                 I_RELE(inode);
365                                 RETURN(rc);
366                         }
367                         lli->lli_st_size = lvb.lvb_size;
368                 }
369         } else {
370                 ENTRY;
371         }
372
373         /* intent will be further used in cases of open()/getattr() */
374         if (inode && (it->it_op & IT_OPEN))
375                 LL_SAVE_INTENT(inode, it);
376
377         child->p_base->pb_ino = inode;
378
379         RETURN(0);
380 }
381
382 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
383 {
384         struct inode *inode;
385         l_lock(&lock->l_resource->lr_namespace->ns_lock);
386
387         if (lock->l_ast_data) {
388                 inode = (struct inode *)lock->l_ast_data;
389                 I_REF(inode);
390         } else
391                 inode = NULL;
392
393         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
394         return inode;
395 }
396
397 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
398                          struct lookup_intent *it, int flags)
399 {
400         struct ll_fid pfid;
401         struct ll_uctxt ctxt;
402         struct it_cb_data icbd;
403         struct ptlrpc_request *req = NULL;
404         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
405         int rc;
406         ENTRY;
407
408         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
409                 RETURN(-ENAMETOOLONG);
410
411         if (!it) {
412                 it = &lookup_it;
413                 it->it_op_release = ll_intent_release;
414         }
415
416         icbd.icbd_child = pnode;
417         icbd.icbd_parent = parent;
418         icbd.icbd_child = pnode;
419         ll_inode2fid(&pfid, parent);
420         ll_i2uctxt(&ctxt, parent, NULL);
421
422         rc = mdc_intent_lock(llu_i2mdcexp(parent), &ctxt, &pfid,
423                              pnode->p_base->pb_name.name,
424                              pnode->p_base->pb_name.len,
425                              NULL, 0, NULL, it, flags, &req,
426                              llu_mdc_blocking_ast);
427         if (rc < 0)
428                 GOTO(out, rc);
429         
430         rc = lookup_it_finish(req, 1, it, &icbd);
431         if (rc != 0) {
432                 ll_intent_release(it);
433                 GOTO(out, rc);
434         }
435
436         llu_lookup_finish_locks(it, pnode);
437
438  out:
439         if (req)
440                 ptlrpc_req_finished(req);
441         return rc;
442 }
443
444 static struct lookup_intent*
445 translate_lookup_intent(struct intent *intent, const char *path)
446 {
447         struct lookup_intent *it;
448         int fmode;
449
450         /* libsysio trick */
451         if (!intent || path) {
452                 CDEBUG(D_VFSTRACE, "not intent needed\n");
453                 return NULL;
454         }
455
456         OBD_ALLOC(it, sizeof(*it));
457         LASSERT(it);
458
459         memset(it, 0, sizeof(*it));
460
461         /* libsysio will assign intent like following:
462          * NOTE: INT_CREAT has include INT_UPDPARENT
463          *
464          * open: INT_OPEN [| INT_CREAT]
465          * mkdir: INT_CREAT
466          * symlink: INT_CREAT
467          * unlink: INT_UPDPARENT
468          * rmdir: INT_UPDPARENT
469          * mknod: INT_CREAT
470          * stat: INT_GETATTR
471          * setattr: NULL
472          *
473          * following logic is adjusted for libsysio
474          */
475
476         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
477
478         if (intent->int_opmask & INT_OPEN) {
479                 it->it_op |= IT_OPEN;
480
481                 /* convert access mode from O_ to FMODE_ */
482                 if (it->it_flags & O_WRONLY)
483                         fmode = FMODE_WRITE;
484                 else if (it->it_flags & O_RDWR)
485                         fmode = FMODE_READ | FMODE_WRITE;
486                 else
487                         fmode = FMODE_READ;
488                 it->it_flags &= ~O_ACCMODE;
489                 it->it_flags |= fmode;
490         }
491
492         /* XXX libsysio has strange code on intent handling,
493          * more check later */
494         if (it->it_flags & O_CREAT) {
495                 it->it_op |= IT_CREAT;
496                 it->it_create_mode = *((int*)intent->int_arg1);
497         }
498
499         if (intent->int_opmask & INT_GETATTR)
500                 it->it_op |= IT_GETATTR;
501
502         LASSERT(!(intent->int_opmask & INT_SETATTR));
503
504         /* libsysio is different to linux vfs when doing unlink/rmdir,
505          * INT_UPDPARENT was passed down during name resolution. Here
506          * we treat it as normal lookup, later unlink()/rmdir() will
507          * do the actual work */
508
509         /* conform to kernel code, if only IT_LOOKUP was set, don't
510          * pass down it */
511         if (!it->it_op || it->it_op == IT_LOOKUP) {
512                 OBD_FREE(it, sizeof(*it));
513                 it = NULL;
514         }
515         if (it)
516                 it->it_op_release = ll_intent_release;
517
518         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
519         return it;
520 }
521
522 int llu_iop_lookup(struct pnode *pnode,
523                    struct inode **inop,
524                    struct intent *intnt,
525                    const char *path)
526 {
527         struct lookup_intent *it;
528         int rc;
529         ENTRY;
530
531         *inop = NULL;
532
533         /* the mount root inode have no name, so don't call
534          * remote in this case. but probably we need revalidate
535          * it here? FIXME */
536         if (pnode->p_mount->mnt_root == pnode) {
537                 struct inode *i = pnode->p_base->pb_ino;
538                 *inop = i;
539                 return 0;
540         }
541
542         if (!pnode->p_base->pb_name.len)
543                 RETURN(-EINVAL);
544
545         it = translate_lookup_intent(intnt, path);
546
547         /* param flags is not used, let it be 0 */
548         if (llu_pb_revalidate(pnode, 0, it)) {
549                 LASSERT(pnode->p_base->pb_ino);
550                 *inop = pnode->p_base->pb_ino;
551                 RETURN(0);
552         }
553
554         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
555         if (!rc) {
556                 if (!pnode->p_base->pb_ino)
557                         rc = -ENOENT;
558                 else
559                         *inop = pnode->p_base->pb_ino;
560         }
561
562         RETURN(rc);
563 }
564