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