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