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