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