Whamcloud - gitweb
209cc92e8825e3e0eb20441fc1baf5795516106d
[fs/lustre-release.git] / lustre / liblustre / namei.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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, 0);
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_resource_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                 /* Inode is set to lock->l_resource->lr_lvb_inode
138                 * for mdc - bug 24555 */
139                 LASSERT(lock->l_ast_data == NULL);
140
141                 /* Invalidate all dentries associated with this inode */
142                 if (inode == NULL)
143                         break;
144
145                 lli =  llu_i2info(inode);
146                 st = llu_i2stat(inode);
147
148                 if (bits & MDS_INODELOCK_UPDATE)
149                         lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
150
151                 fid = &lli->lli_fid;
152                 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
153                     lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
154                     lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
155                         LDLM_ERROR(lock,"data mismatch with ino %llu/%llu/%llu",
156                                   (long long)fid_seq(fid), 
157                                   (long long)fid_oid(fid),
158                                   (long long)fid_ver(fid));
159                 }
160                 if (S_ISDIR(st->st_mode) &&
161                     (bits & MDS_INODELOCK_UPDATE)) {
162                         CDEBUG(D_INODE, "invalidating inode %llu\n",
163                                (long long)st->st_ino);
164
165                         llu_invalidate_inode_pages(inode);
166                 }
167
168 /*
169                 if (inode->i_sb->s_root &&
170                     inode != inode->i_sb->s_root->d_inode)
171                         ll_unhash_aliases(inode);
172 */
173                 I_RELE(inode);
174                 break;
175         }
176         default:
177                 LBUG();
178         }
179
180         RETURN(0);
181 }
182
183 static int pnode_revalidate_finish(struct ptlrpc_request *req,
184                                    struct lookup_intent *it,
185                                    struct pnode *pnode)
186 {
187         struct inode *inode = pnode->p_base->pb_ino;
188         struct lustre_md md;
189         int rc = 0;
190         ENTRY;
191
192         LASSERT(inode);
193
194         if (!req)
195                 RETURN(0);
196
197         if (it_disposition(it, DISP_LOOKUP_NEG))
198                 RETURN(-ENOENT);
199
200         rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
201                               llu_i2sbi(inode)->ll_dt_exp, 
202                               llu_i2sbi(inode)->ll_md_exp, &md);
203         if (rc)
204                 RETURN(rc);
205
206         llu_update_inode(inode, &md);
207
208         RETURN(rc);
209 }
210
211 static int llu_pb_revalidate(struct pnode *pnode, int flags,
212                              struct lookup_intent *it)
213 {
214         struct pnode_base *pb = pnode->p_base;
215         struct md_op_data op_data = {{ 0 }};
216         struct ptlrpc_request *req = NULL;
217         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
218         struct obd_export *exp;
219         int rc;
220         ENTRY;
221
222         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
223                (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
224
225         /* We don't want to cache negative dentries, so return 0 immediately.
226          * We believe that this is safe, that negative dentries cannot be
227          * pinned by someone else */
228         if (pb->pb_ino == NULL) {
229                 CDEBUG(D_INODE, "negative pb\n");
230                 RETURN(0);
231         }
232
233         /* This is due to bad interaction with libsysio. remove this when we
234          * switched to libbsdio XXX
235          */
236         {
237                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
238                 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
239                 if (lli->lli_it) {
240                         CDEBUG(D_INODE, "inode %llu still have intent "
241                                         "%p(opc 0x%x), release it\n",
242                                         (long long) st->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
252         if (!it) {
253                 it = &lookup_it;
254                 it->it_op_release = ll_intent_release;
255         }
256
257         llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
258                             pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
259                             0, LUSTRE_OPC_ANY);
260
261         rc = md_intent_lock(exp, &op_data, NULL, 0, it, flags,
262                             &req, llu_md_blocking_ast,
263                             LDLM_FL_CANCEL_ON_BLOCK);
264         /* If req is NULL, then md_intent_lock only tried to do a lock match;
265          * if all was well, it will return 1 if it found locks, 0 otherwise. */
266         if (req == NULL && rc >= 0)
267                 GOTO(out, rc);
268
269         if (rc < 0)
270                 GOTO(out, rc = 0);
271
272         rc = pnode_revalidate_finish(req, it, pnode);
273         if (rc != 0) {
274                 ll_intent_release(it);
275                 GOTO(out, rc = 0);
276         }
277         rc = 1;
278
279         /* Note: ll_intent_lock may cause a callback, check this! */
280
281         if (it->it_op & IT_OPEN)
282                 LL_SAVE_INTENT(pb->pb_ino, it);
283
284  out:
285         if (req && rc == 1)
286                 ptlrpc_req_finished(req);
287         if (rc == 0) {
288                 LASSERT(pb->pb_ino);
289                 I_RELE(pb->pb_ino);
290                 pb->pb_ino = NULL;
291         } else {
292                 llu_lookup_finish_locks(it, pnode);
293         }
294         RETURN(rc);
295 }
296
297 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
298                             struct lookup_intent *it, void *data)
299 {
300         struct it_cb_data *icbd = data;
301         struct pnode *child = icbd->icbd_child;
302         struct inode *parent = icbd->icbd_parent;
303         struct llu_sb_info *sbi = llu_i2sbi(parent);
304         struct inode *inode = NULL;
305         int rc;
306
307         /* libsysio require us generate inode right away if success.
308          * so if mds created new inode for us we need make sure it
309          * succeeded. thus for any error we can't delay to the
310          * llu_file_open() time. */
311         if (it_disposition(it, DISP_OPEN_CREATE) &&
312             it_open_error(DISP_OPEN_CREATE, it)) {
313                 CDEBUG(D_INODE, "detect mds create error\n");
314                 return it_open_error(DISP_OPEN_CREATE, it);
315         }
316         if (it_disposition(it, DISP_OPEN_OPEN) &&
317             it_open_error(DISP_OPEN_OPEN, it)) {
318                 CDEBUG(D_INODE, "detect mds open error\n");
319                 /* undo which did by md_intent_lock */
320                 if (it_disposition(it, DISP_OPEN_CREATE) &&
321                     !it_open_error(DISP_OPEN_CREATE, it)) {
322                         LASSERT(request);
323                         LASSERT(cfs_atomic_read(&request->rq_refcount) > 1);
324                         CDEBUG(D_INODE, "dec a ref of req %p\n", request);
325                         ptlrpc_req_finished(request);
326                 }
327                 return it_open_error(DISP_OPEN_OPEN, it);
328         }
329
330         /* NB 1 request reference will be taken away by ll_intent_lock()
331          * when I return
332          */
333         if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
334                 struct lustre_md md;
335                 struct llu_inode_info *lli;
336                 struct intnl_stat *st;
337                 ENTRY;
338
339                 if (it_disposition(it, DISP_OPEN_CREATE))
340                         ptlrpc_req_finished(request);
341
342                 rc = md_get_lustre_md(sbi->ll_md_exp, request,
343                                       sbi->ll_dt_exp, sbi->ll_md_exp, &md);
344                 if (rc)
345                         RETURN(rc);
346
347                 inode = llu_iget(parent->i_fs, &md);
348                 if (!inode || IS_ERR(inode)) {
349                         /* free the lsm if we allocated one above */
350                         if (md.lsm != NULL)
351                                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
352                         RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
353                 } else if (md.lsm != NULL) {
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_has_smd) {
363                         ldlm_error_t rc;
364
365                         /* bug 2334: drop MDS lock before acquiring OST lock */
366                         ll_intent_drop_lock(it);
367
368                         rc = cl_glimpse_size(inode);
369                         if (rc) {
370                                 I_RELE(inode);
371                                 RETURN(rc);
372                         }
373                 }
374         } else {
375                 ENTRY;
376         }
377
378         /* intent will be further used in cases of open()/getattr() */
379         if (inode && (it->it_op & IT_OPEN))
380                 LL_SAVE_INTENT(inode, it);
381
382         child->p_base->pb_ino = inode;
383
384         RETURN(0);
385 }
386
387 struct inode *llu_inode_from_resource_lock(struct ldlm_lock *lock)
388 {
389         struct inode *inode;
390         lock_res_and_lock(lock);
391
392         if (lock->l_resource->lr_lvb_inode) {
393                 inode = (struct inode *)lock->l_resource->lr_lvb_inode;
394                 I_REF(inode);
395         } else
396                 inode = NULL;
397
398         unlock_res_and_lock(lock);
399         return inode;
400 }
401
402 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
403 {
404         struct inode *inode;
405         lock_res_and_lock(lock);
406
407         if (lock->l_ast_data) {
408                 inode = (struct inode *)lock->l_ast_data;
409                 I_REF(inode);
410         } else
411                 inode = NULL;
412
413         unlock_res_and_lock(lock);
414         return inode;
415 }
416
417 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
418                          struct lookup_intent *it, int flags)
419 {
420         struct md_op_data op_data = {{ 0 }};
421         struct it_cb_data icbd;
422         struct ptlrpc_request *req = NULL;
423         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
424         __u32 opc;
425         int rc;
426         ENTRY;
427
428         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
429                 RETURN(-ENAMETOOLONG);
430
431         if (!it) {
432                 it = &lookup_it;
433                 it->it_op_release = ll_intent_release;
434         }
435
436         icbd.icbd_child = pnode;
437         icbd.icbd_parent = parent;
438
439         if (it->it_op & IT_CREAT || 
440             (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
441                 opc = LUSTRE_OPC_CREATE;
442         } else {
443                 opc = LUSTRE_OPC_ANY;
444         }
445         
446         llu_prep_md_op_data(&op_data, parent, NULL,
447                             pnode->p_base->pb_name.name,
448                             pnode->p_base->pb_name.len, flags, opc);
449
450         rc = md_intent_lock(llu_i2mdexp(parent), &op_data, NULL, 0, it,
451                             flags, &req, llu_md_blocking_ast,
452                             LDLM_FL_CANCEL_ON_BLOCK);
453         if (rc < 0)
454                 GOTO(out, rc);
455
456         rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
457         if (rc != 0) {
458                 ll_intent_release(it);
459                 GOTO(out, rc);
460         }
461
462         llu_lookup_finish_locks(it, pnode);
463
464  out:
465         if (req)
466                 ptlrpc_req_finished(req);
467         return rc;
468 }
469
470 static struct lookup_intent*
471 translate_lookup_intent(struct intent *intent, const char *path)
472 {
473         struct lookup_intent *it;
474         int fmode;
475
476         /* libsysio trick */
477         if (!intent || path) {
478                 CDEBUG(D_VFSTRACE, "not intent needed\n");
479                 return NULL;
480         }
481
482         OBD_ALLOC(it, sizeof(*it));
483         LASSERT(it);
484
485         memset(it, 0, sizeof(*it));
486
487         /* libsysio will assign intent like following:
488          * NOTE: INT_CREAT has include INT_UPDPARENT
489          *
490          * open: INT_OPEN [| INT_CREAT]
491          * mkdir: INT_CREAT
492          * symlink: INT_CREAT
493          * unlink: INT_UPDPARENT
494          * rmdir: INT_UPDPARENT
495          * mknod: INT_CREAT
496          * stat: INT_GETATTR
497          * setattr: NULL
498          *
499          * following logic is adjusted for libsysio
500          */
501
502         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
503
504         if (intent->int_opmask & INT_OPEN) {
505                 it->it_op |= IT_OPEN;
506
507                 /* convert access mode from O_ to FMODE_ */
508                 if (it->it_flags & O_WRONLY)
509                         fmode = FMODE_WRITE;
510                 else if (it->it_flags & O_RDWR)
511                         fmode = FMODE_READ | FMODE_WRITE;
512                 else
513                         fmode = FMODE_READ;
514                 it->it_flags &= ~O_ACCMODE;
515                 it->it_flags |= fmode;
516         }
517
518         /* XXX libsysio has strange code on intent handling,
519          * more check later */
520         if (it->it_flags & O_CREAT) {
521                 it->it_op |= IT_CREAT;
522                 it->it_create_mode = *((int*)intent->int_arg1);
523                 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
524                    this lookup to return negative result, but then there is no
525                    way to find out original intent in ll_iop_open(). So we just
526                    clear O_EXCL from libsysio flags here to avoid checking
527                    for negative result. O_EXCL will be enforced by MDS. */
528                 *((int*)intent->int_arg2) &= ~O_EXCL;
529         }
530
531         if (intent->int_opmask & INT_GETATTR)
532                 it->it_op |= IT_GETATTR;
533
534         LASSERT(!(intent->int_opmask & INT_SETATTR));
535
536         /* libsysio is different to linux vfs when doing unlink/rmdir,
537          * INT_UPDPARENT was passed down during name resolution. Here
538          * we treat it as normal lookup, later unlink()/rmdir() will
539          * do the actual work */
540
541         /* conform to kernel code, if only IT_LOOKUP was set, don't
542          * pass down it */
543         if (!it->it_op || it->it_op & IT_LOOKUP) {
544                 OBD_FREE(it, sizeof(*it));
545                 it = NULL;
546         }
547         if (it)
548                 it->it_op_release = ll_intent_release;
549
550         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
551         return it;
552 }
553
554 int llu_iop_lookup(struct pnode *pnode,
555                    struct inode **inop,
556                    struct intent *intnt,
557                    const char *path)
558 {
559         struct lookup_intent *it;
560         int rc;
561         ENTRY;
562
563         liblustre_wait_event(0);
564
565         *inop = NULL;
566
567         /* the mount root inode have no name, so don't call
568          * remote in this case. but probably we need revalidate
569          * it here? FIXME */
570         if (pnode->p_mount->mnt_root == pnode) {
571                 struct inode *i = pnode->p_base->pb_ino;
572                 *inop = i;
573                 RETURN(0);
574         }
575
576         if (!pnode->p_base->pb_name.len)
577                 RETURN(-EINVAL);
578
579         it = translate_lookup_intent(intnt, path);
580
581         /* param flags is not used, let it be 0 */
582         if (llu_pb_revalidate(pnode, 0, it)) {
583                 LASSERT(pnode->p_base->pb_ino);
584                 *inop = pnode->p_base->pb_ino;
585                 GOTO(out, rc = 0);
586         }
587
588         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
589         if (!rc) {
590                 if (!pnode->p_base->pb_ino)
591                         rc = -ENOENT;
592                 else
593                         *inop = pnode->p_base->pb_ino;
594         }
595
596 out:
597         if (it)
598                 OBD_FREE(it, sizeof(*it));
599         liblustre_wait_event(0);
600         RETURN(rc);
601 }