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