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