Whamcloud - gitweb
b=21411 Improvement for AT.
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/liblustre/namei.c
37  *
38  * Lustre Light name resolution
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include <assert.h>
46 #include <time.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <sys/queue.h>
51
52 #include <sysio.h>
53 #ifdef HAVE_XTIO_H
54 #include <xtio.h>
55 #endif
56 #include <fs.h>
57 #include <mount.h>
58 #include <inode.h>
59 #ifdef HAVE_FILE_H
60 #include <file.h>
61 #endif
62
63 #undef LIST_HEAD
64
65 #include "llite_lib.h"
66
67 void ll_intent_drop_lock(struct lookup_intent *it)
68 {
69         struct lustre_handle *handle;
70
71         if (it->it_op && it->d.lustre.it_lock_mode) {
72                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
73                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
74                        " from it %p\n", handle->cookie, it);
75                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
76
77                 /* bug 494: intent_release may be called multiple times, from
78                  * this thread and we don't want to double-decref this lock */
79                 it->d.lustre.it_lock_mode = 0;
80         }
81 }
82
83 void ll_intent_release(struct lookup_intent *it)
84 {
85         ENTRY;
86
87         ll_intent_drop_lock(it);
88         it->it_magic = 0;
89         it->it_op_release = 0;
90         it->d.lustre.it_disposition = 0;
91         it->d.lustre.it_data = NULL;
92         EXIT;
93 }
94
95 #if 0
96 /*
97  * remove the stale inode from pnode
98  */
99 void unhook_stale_inode(struct pnode *pno)
100 {
101         struct inode *inode = pno->p_base->pb_ino;
102         ENTRY;
103
104         LASSERT(inode);
105         LASSERT(llu_i2info(inode)->lli_stale_flag);
106
107         pno->p_base->pb_ino = NULL;
108         I_RELE(inode);
109
110         if (!llu_i2info(inode)->lli_open_count) {
111                 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
112                                 inode, llu_i2info(inode)->lli_st_ino, pno);
113                 if (!inode->i_ref)
114                         _sysio_i_gone(inode);
115         }
116
117         EXIT;
118         return;
119 }
120 #endif
121
122 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
123 {
124         struct inode *inode;
125         LASSERT(it);
126         LASSERT(pnode);
127
128         inode = pnode->p_base->pb_ino;
129         if (it->d.lustre.it_lock_mode && inode != NULL) {
130                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
131                        inode, (long long)llu_i2stat(inode)->st_ino,
132                        llu_i2info(inode)->lli_st_generation);
133                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode, NULL);
134         }
135
136         /* drop lookup/getattr locks */
137         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
138                 ll_intent_release(it);
139
140 }
141
142 static inline void llu_invalidate_inode_pages(struct inode * inode)
143 {
144         /* do nothing */
145 }
146
147 int llu_mdc_blocking_ast(struct ldlm_lock *lock,
148                          struct ldlm_lock_desc *desc,
149                          void *data, int flag)
150 {
151         int rc;
152         struct lustre_handle lockh;
153         ENTRY;
154
155
156         switch (flag) {
157         case LDLM_CB_BLOCKING:
158                 ldlm_lock2handle(lock, &lockh);
159                 rc = ldlm_cli_cancel(&lockh);
160                 if (rc < 0) {
161                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
162                         RETURN(rc);
163                 }
164                 break;
165         case LDLM_CB_CANCELING: {
166                 struct inode *inode = llu_inode_from_lock(lock);
167                 struct llu_inode_info *lli;
168                 struct intnl_stat *st;
169                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
170
171                 /* Invalidate all dentries associated with this inode */
172                 if (inode == NULL)
173                         break;
174
175                 lli =  llu_i2info(inode);
176                 st = llu_i2stat(inode);
177
178                 if (bits & MDS_INODELOCK_UPDATE)
179                         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
180
181                 if (lock->l_resource->lr_name.name[0] != st->st_ino ||
182                     lock->l_resource->lr_name.name[1] !=lli->lli_st_generation){
183                         LDLM_ERROR(lock, "data mismatch with ino %llu/%lu",
184                                   (long long)st->st_ino,lli->lli_st_generation);
185                 }
186                 if (S_ISDIR(st->st_mode) &&
187                     (bits & MDS_INODELOCK_UPDATE)) {
188                         CDEBUG(D_INODE, "invalidating inode %llu\n",
189                                (long long)st->st_ino);
190
191                         llu_invalidate_inode_pages(inode);
192                 }
193
194 /*
195                 if (inode->i_sb->s_root &&
196                     inode != inode->i_sb->s_root->d_inode)
197                         ll_unhash_aliases(inode);
198 */
199                 I_RELE(inode);
200                 break;
201         }
202         default:
203                 LBUG();
204         }
205
206         RETURN(0);
207 }
208
209 static int pnode_revalidate_finish(struct ptlrpc_request *req,
210                                    int offset,
211                                    struct lookup_intent *it,
212                                    struct pnode *pnode)
213 {
214         struct inode *inode = pnode->p_base->pb_ino;
215         struct lustre_md md;
216         int rc = 0;
217         ENTRY;
218
219         LASSERT(inode);
220
221         if (!req)
222                 RETURN(0);
223
224         if (it_disposition(it, DISP_LOOKUP_NEG))
225                 RETURN(-ENOENT);
226
227         rc = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
228         if (rc)
229                 RETURN(rc);
230
231         llu_update_inode(inode, md.body, md.lsm);
232
233         RETURN(rc);
234 }
235
236 static int llu_pb_revalidate(struct pnode *pnode, int flags,
237                              struct lookup_intent *it)
238 {
239         struct pnode_base *pb = pnode->p_base;
240         struct it_cb_data icbd;
241         struct mdc_op_data op_data;
242         struct ptlrpc_request *req = NULL;
243         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
244         struct obd_export *exp;
245         int rc;
246         ENTRY;
247
248         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
249                (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
250
251         /* We don't want to cache negative dentries, so return 0 immediately.
252          * We believe that this is safe, that negative dentries cannot be
253          * pinned by someone else */
254         if (pb->pb_ino == NULL) {
255                 CDEBUG(D_INODE, "negative pb\n");
256                 RETURN(0);
257         }
258
259         /* This is due to bad interaction with libsysio. remove this when we
260          * switched to libbsdio XXX
261          */
262         {
263                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
264                 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
265                 if (lli->lli_it) {
266                         CDEBUG(D_INODE, "inode %llu still have intent "
267                                         "%p(opc 0x%x), release it\n",
268                                         (long long) st->st_ino, lli->lli_it,
269                                         lli->lli_it->it_op);
270                         ll_intent_release(lli->lli_it);
271                         OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
272                         lli->lli_it = NULL;
273                 }
274         }
275
276         exp = llu_i2mdcexp(pb->pb_ino);
277         icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
278         icbd.icbd_child = pnode;
279
280         if (!it) {
281                 it = &lookup_it;
282                 it->it_op_release = ll_intent_release;
283         }
284
285         llu_prepare_mdc_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
286                                 pb->pb_ino, pb->pb_name.name,pb->pb_name.len,0);
287
288         rc = mdc_intent_lock(exp, &op_data, NULL, 0, it, flags,
289                              &req, llu_mdc_blocking_ast,
290                              LDLM_FL_CANCEL_ON_BLOCK);
291         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
292          * if all was well, it will return 1 if it found locks, 0 otherwise. */
293         if (req == NULL && rc >= 0)
294                 GOTO(out, rc);
295
296         if (rc < 0)
297                 GOTO(out, rc = 0);
298
299         rc = pnode_revalidate_finish(req, DLM_REPLY_REC_OFF, it, pnode);
300         if (rc != 0) {
301                 ll_intent_release(it);
302                 GOTO(out, rc = 0);
303         }
304         rc = 1;
305
306         /* Note: ll_intent_lock may cause a callback, check this! */
307
308         if (it->it_op & IT_OPEN)
309                 LL_SAVE_INTENT(pb->pb_ino, it);
310
311  out:
312         if (req && rc == 1)
313                 ptlrpc_req_finished(req);
314         if (rc == 0) {
315                 LASSERT(pb->pb_ino);
316                 I_RELE(pb->pb_ino);
317                 pb->pb_ino = NULL;
318         } else {
319                 llu_lookup_finish_locks(it, pnode);
320         }
321         RETURN(rc);
322 }
323
324 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
325                             struct lookup_intent *it, void *data)
326 {
327         struct it_cb_data *icbd = data;
328         struct pnode *child = icbd->icbd_child;
329         struct inode *parent = icbd->icbd_parent;
330         struct llu_sb_info *sbi = llu_i2sbi(parent);
331         struct inode *inode = NULL;
332         int rc;
333
334         /* libsysio require us generate inode right away if success.
335          * so if mds created new inode for us we need make sure it
336          * succeeded. thus for any error we can't delay to the
337          * llu_file_open() time. */
338         if (it_disposition(it, DISP_OPEN_CREATE) &&
339             it_open_error(DISP_OPEN_CREATE, it)) {
340                 CDEBUG(D_INODE, "detect mds create error\n");
341                 return it_open_error(DISP_OPEN_CREATE, it);
342         }
343         if (it_disposition(it, DISP_OPEN_OPEN) &&
344             it_open_error(DISP_OPEN_OPEN, it)) {
345                 CDEBUG(D_INODE, "detect mds open error\n");
346                 /* undo which did by mdc_intent_lock */
347                 if (it_disposition(it, DISP_OPEN_CREATE) &&
348                     !it_open_error(DISP_OPEN_CREATE, it)) {
349                         LASSERT(request);
350                         LASSERT(atomic_read(&request->rq_refcount) > 1);
351                         CDEBUG(D_INODE, "dec a ref of req %p\n", request);
352                         ptlrpc_req_finished(request);
353                 }
354                 return it_open_error(DISP_OPEN_OPEN, it);
355         }
356
357         /* NB 1 request reference will be taken away by ll_intent_lock()
358          * when I return
359          */
360         if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
361                 struct lustre_md md;
362                 struct llu_inode_info *lli;
363                 struct intnl_stat *st;
364                 ENTRY;
365
366                 if (it_disposition(it, DISP_OPEN_CREATE))
367                         ptlrpc_req_finished(request);
368
369                 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
370                 if (rc)
371                         RETURN(rc);
372
373                 inode = llu_iget(parent->i_fs, &md);
374                 if (!inode || IS_ERR(inode)) {
375                         /* free the lsm if we allocated one above */
376                         if (md.lsm != NULL)
377                                 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
378                         RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
379                 } else if (md.lsm != NULL &&
380                            llu_i2info(inode)->lli_smd != md.lsm) {
381                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
382                 }
383
384                 lli = llu_i2info(inode);
385                 st = llu_i2stat(inode);
386
387                 /* If this is a stat, get the authoritative file size */
388                 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
389                     lli->lli_smd != NULL) {
390                         struct lov_stripe_md *lsm = lli->lli_smd;
391                         ldlm_error_t rc;
392
393                         LASSERT(lsm->lsm_object_id != 0);
394
395                         /* bug 2334: drop MDS lock before acquiring OST lock */
396                         ll_intent_drop_lock(it);
397
398                         rc = llu_glimpse_size(inode);
399                         if (rc) {
400                                 I_RELE(inode);
401                                 RETURN(rc);
402                         }
403                 }
404         } else {
405                 ENTRY;
406         }
407
408         /* intent will be further used in cases of open()/getattr() */
409         if (inode && (it->it_op & IT_OPEN))
410                 LL_SAVE_INTENT(inode, it);
411
412         child->p_base->pb_ino = inode;
413
414         RETURN(0);
415 }
416
417 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
418 {
419         struct inode *inode;
420         lock_res_and_lock(lock);
421
422         if (lock->l_ast_data) {
423                 inode = (struct inode *)lock->l_ast_data;
424                 I_REF(inode);
425         } else
426                 inode = NULL;
427
428         unlock_res_and_lock(lock);
429         return inode;
430 }
431
432 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
433                          struct lookup_intent *it, int flags)
434 {
435         struct mdc_op_data op_data;
436         struct it_cb_data icbd;
437         struct ptlrpc_request *req = NULL;
438         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
439         int rc;
440         ENTRY;
441
442         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
443                 RETURN(-ENAMETOOLONG);
444
445         if (!it) {
446                 it = &lookup_it;
447                 it->it_op_release = ll_intent_release;
448         }
449
450         icbd.icbd_child = pnode;
451         icbd.icbd_parent = parent;
452
453         llu_prepare_mdc_op_data(&op_data, parent, NULL,
454                                 pnode->p_base->pb_name.name,
455                                 pnode->p_base->pb_name.len, flags);
456
457         rc = mdc_intent_lock(llu_i2mdcexp(parent), &op_data, NULL, 0, it,
458                              flags, &req, llu_mdc_blocking_ast,
459                              LDLM_FL_CANCEL_ON_BLOCK);
460         if (rc < 0)
461                 GOTO(out, rc);
462
463         rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
464         if (rc != 0) {
465                 ll_intent_release(it);
466                 GOTO(out, rc);
467         }
468
469         llu_lookup_finish_locks(it, pnode);
470
471  out:
472         if (req)
473                 ptlrpc_req_finished(req);
474         return rc;
475 }
476
477 static struct lookup_intent*
478 translate_lookup_intent(struct intent *intent, const char *path)
479 {
480         struct lookup_intent *it;
481         int fmode;
482
483         /* libsysio trick */
484         if (!intent || path) {
485                 CDEBUG(D_VFSTRACE, "not intent needed\n");
486                 return NULL;
487         }
488
489         OBD_ALLOC(it, sizeof(*it));
490         LASSERT(it);
491
492         memset(it, 0, sizeof(*it));
493
494         /* libsysio will assign intent like following:
495          * NOTE: INT_CREAT has include INT_UPDPARENT
496          *
497          * open: INT_OPEN [| INT_CREAT]
498          * mkdir: INT_CREAT
499          * symlink: INT_CREAT
500          * unlink: INT_UPDPARENT
501          * rmdir: INT_UPDPARENT
502          * mknod: INT_CREAT
503          * stat: INT_GETATTR
504          * setattr: NULL
505          *
506          * following logic is adjusted for libsysio
507          */
508
509         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
510
511         if (intent->int_opmask & INT_OPEN) {
512                 it->it_op |= IT_OPEN;
513
514                 /* convert access mode from O_ to FMODE_ */
515                 if (it->it_flags & O_WRONLY)
516                         fmode = FMODE_WRITE;
517                 else if (it->it_flags & O_RDWR)
518                         fmode = FMODE_READ | FMODE_WRITE;
519                 else
520                         fmode = FMODE_READ;
521                 it->it_flags &= ~O_ACCMODE;
522                 it->it_flags |= fmode;
523         }
524
525         /* XXX libsysio has strange code on intent handling,
526          * more check later */
527         if (it->it_flags & O_CREAT) {
528                 it->it_op |= IT_CREAT;
529                 it->it_create_mode = *((int*)intent->int_arg1);
530                 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
531                    this lookup to return negative result, but then there is no
532                    way to find out original intent in ll_iop_open(). So we just
533                    clear O_EXCL from libsysio flags here to avoid checking
534                    for negative result. O_EXCL will be enforced by MDS. */
535                 *((int*)intent->int_arg2) &= ~O_EXCL;
536         }
537
538         if (intent->int_opmask & INT_GETATTR)
539                 it->it_op |= IT_GETATTR;
540
541         LASSERT(!(intent->int_opmask & INT_SETATTR));
542
543         /* libsysio is different to linux vfs when doing unlink/rmdir,
544          * INT_UPDPARENT was passed down during name resolution. Here
545          * we treat it as normal lookup, later unlink()/rmdir() will
546          * do the actual work */
547
548         /* conform to kernel code, if only IT_LOOKUP was set, don't
549          * pass down it */
550         if (!it->it_op || it->it_op == IT_LOOKUP) {
551                 OBD_FREE(it, sizeof(*it));
552                 it = NULL;
553         }
554         if (it)
555                 it->it_op_release = ll_intent_release;
556
557         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
558         return it;
559 }
560
561 int llu_iop_lookup(struct pnode *pnode,
562                    struct inode **inop,
563                    struct intent *intnt,
564                    const char *path)
565 {
566         struct lookup_intent *it;
567         int rc;
568         ENTRY;
569
570         liblustre_wait_event(0);
571
572         *inop = NULL;
573
574         /* the mount root inode have no name, so don't call
575          * remote in this case. but probably we need revalidate
576          * it here? FIXME */
577         if (pnode->p_mount->mnt_root == pnode) {
578                 struct inode *i = pnode->p_base->pb_ino;
579                 *inop = i;
580                 RETURN(0);
581         }
582
583         if (!pnode->p_base->pb_name.len)
584                 RETURN(-EINVAL);
585
586         it = translate_lookup_intent(intnt, path);
587
588         /* param flags is not used, let it be 0 */
589         if (llu_pb_revalidate(pnode, 0, it)) {
590                 LASSERT(pnode->p_base->pb_ino);
591                 *inop = pnode->p_base->pb_ino;
592                 GOTO(out, rc = 0);
593         }
594
595         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
596         if (!rc) {
597                 if (!pnode->p_base->pb_ino)
598                         rc = -ENOENT;
599                 else
600                         *inop = pnode->p_base->pb_ino;
601         }
602
603 out:
604         if (it)
605                 OBD_FREE(it, sizeof(*it));
606         liblustre_wait_event(0);
607         RETURN(rc);
608 }