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