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