Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/fcntl.h>
33 #include <sys/queue.h>
34
35 #include <sysio.h>
36 #include <fs.h>
37 #include <mount.h>
38 #include <inode.h>
39 #include <file.h>
40
41 #undef LIST_HEAD
42
43 #include "llite_lib.h"
44
45 static void ll_intent_drop_lock(struct lookup_intent *it)
46 {
47         struct lustre_handle *handle;
48
49         if (it->it_op && it->d.lustre.it_lock_mode) {
50                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
51                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
52                        " from it %p\n", handle->cookie, it);
53                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
54
55                 /* bug 494: intent_release may be called multiple times, from
56                  * this thread and we don't want to double-decref this lock */
57                 it->d.lustre.it_lock_mode = 0;
58         }
59 }
60
61 static void ll_intent_release(struct lookup_intent *it)
62 {
63         ENTRY;
64
65         ll_intent_drop_lock(it);
66         it->it_magic = 0;
67         it->it_op_release = 0;
68         it->d.lustre.it_disposition = 0;
69         it->d.lustre.it_data = NULL;
70         EXIT;
71 }
72
73 /*
74  * remove the stale inode from pnode
75  */
76 void unhook_stale_inode(struct pnode *pno)
77 {
78         struct inode *inode = pno->p_base->pb_ino;
79         ENTRY;
80
81         LASSERT(inode);
82         LASSERT(llu_i2info(inode)->lli_stale_flag);
83
84         pno->p_base->pb_ino = NULL;
85         I_RELE(inode);
86
87         if (!llu_i2info(inode)->lli_open_count) {
88                 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
89                                 inode, llu_i2info(inode)->lli_st_ino, pno);
90                 if (!inode->i_ref)
91                         _sysio_i_gone(inode);
92         }
93
94         EXIT;
95         return;
96 }
97
98 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
99 {
100         LASSERT(it);
101         LASSERT(pnode);
102
103         if (it && pnode->p_base->pb_ino != NULL) {
104                 struct inode *inode = pnode->p_base->pb_ino;
105                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%lu)\n",
106                        inode, llu_i2info(inode)->lli_st_ino,
107                        llu_i2info(inode)->lli_st_generation);
108                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
109         }
110
111         /* drop lookup/getattr locks */
112         if (it->it_op == IT_LOOKUP || it->it_op == 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_mdc_blocking_ast(struct ldlm_lock *lock,
123                          struct ldlm_lock_desc *desc,
124                          void *data, int flag)
125 {
126         int rc;
127         struct lustre_handle lockh;
128         ENTRY;
129
130
131         switch (flag) {
132         case LDLM_CB_BLOCKING:
133                 ldlm_lock2handle(lock, &lockh);
134                 rc = ldlm_cli_cancel(&lockh);
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_lock(lock);
142                 struct llu_inode_info *lli;
143
144                 /* Invalidate all dentries associated with this inode */
145                 if (inode == NULL)
146                         break;
147
148                 lli =  llu_i2info(inode);
149
150                 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
151
152                 if (lock->l_resource->lr_name.name[0] != lli->lli_st_ino ||
153                     lock->l_resource->lr_name.name[1] != lli->lli_st_generation) {
154                         LDLM_ERROR(lock, "data mismatch with ino %lu/%lu",
155                                    lli->lli_st_ino, lli->lli_st_generation);
156                 }
157                 if (S_ISDIR(lli->lli_st_mode)) {
158                         CDEBUG(D_INODE, "invalidating inode %lu\n",
159                                lli->lli_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                                    int offset,
181                                    struct lookup_intent *it,
182                                    struct pnode *pnode)
183 {
184         struct inode *inode = pnode->p_base->pb_ino;
185         struct lustre_md md;
186         int rc = 0;
187         ENTRY;
188
189         LASSERT(inode);
190
191         if (!req)
192                 RETURN(0);
193
194         if (it_disposition(it, DISP_LOOKUP_NEG))
195                 RETURN(-ENOENT);
196
197         rc = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
198         if (rc)
199                 RETURN(rc);
200
201         llu_update_inode(inode, md.body, md.lsm);
202
203         RETURN(rc);
204 }
205
206 int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
207 {
208         struct pnode_base *pb = pnode->p_base;
209         struct ll_fid pfid, cfid;
210         struct it_cb_data icbd;
211         struct ll_uctxt ctxt;
212         struct ptlrpc_request *req = NULL;
213         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
214         struct obd_export *exp;
215         int rc;
216         ENTRY;
217
218         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%x\n",
219                pb->pb_name.name, it ? it->it_op : 0);
220
221         /* We don't want to cache negative dentries, so return 0 immediately.
222          * We believe that this is safe, that negative dentries cannot be
223          * pinned by someone else */
224         if (pb->pb_ino == NULL) {
225                 CDEBUG(D_INODE, "negative pb\n");
226                 RETURN(0);
227         }
228
229         /* check stale inode */
230         if (llu_i2info(pb->pb_ino)->lli_stale_flag)
231                 unhook_stale_inode(pnode);
232
233         /* check again because unhook_stale_inode() might generate
234          * negative pnode */
235         if (pb->pb_ino == NULL) {
236                 CDEBUG(D_INODE, "negative pb\n");
237                 RETURN(0);
238         }
239
240         /* This is due to bad interaction with libsysio. remove this when we
241          * switched to libbsdio XXX
242          */
243         {
244                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
245                 if (lli->lli_it) {
246                         CDEBUG(D_INODE, "inode %lu still have intent "
247                                         "%p(opc 0x%x), release it\n",
248                                         lli->lli_st_ino, lli->lli_it,
249                                         lli->lli_it->it_op);
250                         ll_intent_release(lli->lli_it);
251                         OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
252                         lli->lli_it = NULL;
253                 }
254         }
255
256         exp = llu_i2mdcexp(pb->pb_ino);
257         ll_inode2fid(&pfid, pnode->p_parent->p_base->pb_ino);
258         ll_inode2fid(&cfid, 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         ll_i2uctxt(&ctxt, pnode->p_parent->p_base->pb_ino, pb->pb_ino);
268
269         rc = mdc_intent_lock(exp, &ctxt, &pfid,
270                              pb->pb_name.name, pb->pb_name.len,
271                              NULL, 0, &cfid, it, flags, &req,
272                              llu_mdc_blocking_ast);
273         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
274          * if all was well, it will return 1 if it found locks, 0 otherwise. */
275         if (req == NULL && rc >= 0)
276                 GOTO(out, rc);
277
278         if (rc < 0)
279                 GOTO(out, rc = 0);
280
281         rc = pnode_revalidate_finish(req, 1, it, pnode);
282         if (rc != 0) {
283                 ll_intent_release(it);
284                 GOTO(out, rc = 0);
285         }
286         rc = 1;
287
288         /* Note: ll_intent_lock may cause a callback, check this! */
289
290         if (it->it_op & IT_OPEN)
291                 LL_SAVE_INTENT(pb->pb_ino, it);
292
293  out:
294         if (req && rc == 1)
295                 ptlrpc_req_finished(req);
296         if (rc == 0) {
297                 LASSERT(pb->pb_ino);
298                 I_RELE(pb->pb_ino);
299                 pb->pb_ino = NULL;
300         } else {
301                 llu_lookup_finish_locks(it, pnode);
302                 llu_i2info(pb->pb_ino)->lli_stale_flag = 0;
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         /* NB 1 request reference will be taken away by ll_intent_lock()
318          * when I return
319          * Note: libsysio require the inode must be generated here
320          */
321         if ((it->it_op & IT_CREAT) || !it_disposition(it, DISP_LOOKUP_NEG)) {
322                 struct lustre_md md;
323                 struct llu_inode_info *lli;
324                 ENTRY;
325
326                 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
327                 if (rc)
328                         RETURN(rc);
329
330                 inode = llu_iget(parent->i_fs, &md);
331                 if (!inode) {
332                         /* free the lsm if we allocated one above */
333                         if (md.lsm != NULL)
334                                 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
335                         RETURN(-ENOMEM);
336                 } else if (md.lsm != NULL &&
337                            llu_i2info(inode)->lli_smd != md.lsm) {
338                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
339                 }
340
341                 lli = llu_i2info(inode);
342
343                 /* If this is a stat, get the authoritative file size */
344                 if (it->it_op == IT_GETATTR && S_ISREG(lli->lli_st_mode) &&
345                     lli->lli_smd != NULL) {
346                         struct lov_stripe_md *lsm = lli->lli_smd;
347                         struct ost_lvb lvb;
348                         ldlm_error_t rc;
349
350                         LASSERT(lsm->lsm_object_id != 0);
351
352                         /* bug 2334: drop MDS lock before acquiring OST lock */
353                         ll_intent_drop_lock(it);
354
355                         rc = llu_glimpse_size(inode, &lvb);
356                         if (rc) {
357                                 I_RELE(inode);
358                                 RETURN(rc);
359                         }
360                         lli->lli_st_size = lvb.lvb_size;
361                 }
362         } else {
363                 ENTRY;
364         }
365
366         /* intent will be further used in cases of open()/getattr() */
367         if (inode && (it->it_op & IT_OPEN))
368                 LL_SAVE_INTENT(inode, it);
369
370         child->p_base->pb_ino = inode;
371
372         RETURN(0);
373 }
374
375 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
376 {
377         struct inode *inode;
378         l_lock(&lock->l_resource->lr_namespace->ns_lock);
379
380         if (lock->l_ast_data) {
381                 inode = (struct inode *)lock->l_ast_data;
382                 I_REF(inode);
383         } else
384                 inode = NULL;
385
386         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
387         return inode;
388 }
389
390 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
391                          struct lookup_intent *it, int flags)
392 {
393         struct ll_fid pfid;
394         struct ll_uctxt ctxt;
395         struct it_cb_data icbd;
396         struct ptlrpc_request *req = NULL;
397         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
398         int rc;
399         ENTRY;
400
401         if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
402                 RETURN(-ENAMETOOLONG);
403
404         if (!it) {
405                 it = &lookup_it;
406                 it->it_op_release = ll_intent_release;
407         }
408
409         icbd.icbd_child = pnode;
410         icbd.icbd_parent = parent;
411         icbd.icbd_child = pnode;
412         ll_inode2fid(&pfid, parent);
413         ll_i2uctxt(&ctxt, parent, NULL);
414
415         rc = mdc_intent_lock(llu_i2mdcexp(parent), &ctxt, &pfid,
416                              pnode->p_base->pb_name.name,
417                              pnode->p_base->pb_name.len,
418                              NULL, 0, NULL, it, flags, &req,
419                              llu_mdc_blocking_ast);
420         if (rc < 0)
421                 GOTO(out, rc);
422         
423         rc = lookup_it_finish(req, 1, it, &icbd);
424         if (rc != 0) {
425                 ll_intent_release(it);
426                 GOTO(out, rc);
427         }
428
429         llu_lookup_finish_locks(it, pnode);
430
431  out:
432         if (req)
433                 ptlrpc_req_finished(req);
434         return rc;
435 }
436
437 static struct lookup_intent*
438 translate_lookup_intent(struct intent *intent, const char *path)
439 {
440         struct lookup_intent *it;
441         int fmode;
442
443         /* libsysio trick */
444         if (!intent || path) {
445                 CDEBUG(D_VFSTRACE, "not intent needed\n");
446                 return NULL;
447         }
448
449         OBD_ALLOC(it, sizeof(*it));
450         LASSERT(it);
451
452         memset(it, 0, sizeof(*it));
453
454         /* libsysio will assign intent like following:
455          * NOTE: INT_CREAT has include INT_UPDPARENT
456          *
457          * open: INT_OPEN [| INT_CREAT]
458          * mkdir: INT_CREAT
459          * symlink: INT_CREAT
460          * unlink: INT_UPDPARENT
461          * rmdir: INT_UPDPARENT
462          * mknod: INT_CREAT
463          * stat: INT_GETATTR
464          * setattr: NULL
465          *
466          * following logic is adjusted for libsysio
467          */
468
469         it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
470
471         if (intent->int_opmask & INT_OPEN) {
472                 it->it_op |= IT_OPEN;
473
474                 /* convert access mode from O_ to FMODE_ */
475                 if (it->it_flags & O_WRONLY)
476                         fmode = FMODE_WRITE;
477                 else if (it->it_flags & O_RDWR)
478                         fmode = FMODE_READ | FMODE_WRITE;
479                 else
480                         fmode = FMODE_READ;
481                 it->it_flags &= ~O_ACCMODE;
482                 it->it_flags |= fmode;
483         }
484
485         /* XXX libsysio has strange code on intent handling,
486          * more check later */
487         if (it->it_flags & O_CREAT) {
488                 it->it_op |= IT_CREAT;
489                 it->it_create_mode = *((int*)intent->int_arg1);
490         }
491
492         if (intent->int_opmask & INT_GETATTR)
493                 it->it_op |= IT_GETATTR;
494
495         LASSERT(!(intent->int_opmask & INT_SETATTR));
496
497         /* libsysio is different to linux vfs when doing unlink/rmdir,
498          * INT_UPDPARENT was passed down during name resolution. Here
499          * we treat it as normal lookup, later unlink()/rmdir() will
500          * do the actual work */
501
502         /* conform to kernel code, if only IT_LOOKUP was set, don't
503          * pass down it */
504         if (!it->it_op || it->it_op == IT_LOOKUP) {
505                 OBD_FREE(it, sizeof(*it));
506                 it = NULL;
507         }
508         if (it)
509                 it->it_op_release = ll_intent_release;
510
511         CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
512         return it;
513 }
514
515 int llu_iop_lookup(struct pnode *pnode,
516                    struct inode **inop,
517                    struct intent *intnt,
518                    const char *path)
519 {
520         struct lookup_intent *it;
521         int rc;
522         ENTRY;
523
524         *inop = NULL;
525
526         /* the mount root inode have no name, so don't call
527          * remote in this case. but probably we need revalidate
528          * it here? FIXME */
529         if (pnode->p_mount->mnt_root == pnode) {
530                 struct inode *i = pnode->p_base->pb_ino;
531                 *inop = i;
532                 return 0;
533         }
534
535         if (!pnode->p_base->pb_name.len)
536                 RETURN(-EINVAL);
537
538         it = translate_lookup_intent(intnt, path);
539
540         /* param flags is not used, let it be 0 */
541         if (llu_pb_revalidate(pnode, 0, it)) {
542                 LASSERT(pnode->p_base->pb_ino);
543                 *inop = pnode->p_base->pb_ino;
544                 RETURN(0);
545         }
546
547         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
548         if (!rc) {
549                 if (!pnode->p_base->pb_ino)
550                         rc = -ENOENT;
551                 else
552                         *inop = pnode->p_base->pb_ino;
553         }
554
555         RETURN(rc);
556 }
557