Whamcloud - gitweb
b49d78291cd67434bf7ec38cb5c6fb64992c2261
[fs/lustre-release.git] / lustre / llite / dcache.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/sched.h>
24 #include <linux/smp_lock.h>
25 #include <linux/quotaops.h>
26
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <obd_support.h>
30 #include <lustre_lite.h>
31 #include <lustre/lustre_idl.h>
32 #include <lustre_dlm.h>
33 #include <linux/lustre_version.h>
34
35 #include "llite_internal.h"
36
37 /* should NOT be called with the dcache lock, see fs/dcache.c */
38 void ll_release(struct dentry *de)
39 {
40         struct ll_dentry_data *lld;
41         ENTRY;
42         LASSERT(de != NULL);
43         lld = ll_d2d(de);
44         if (lld == NULL) { /* NFS copies the de->d_op methods (bug 4655) */
45                 EXIT;
46                 return;
47         }
48 #ifndef HAVE_VFS_INTENT_PATCHES
49         if (lld->lld_it) {
50                 ll_intent_release(lld->lld_it);
51                 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
52         }
53 #endif
54         LASSERT(lld->lld_cwd_count == 0);
55         LASSERT(lld->lld_mnt_count == 0);
56         OBD_FREE(de->d_fsdata, sizeof(*lld));
57
58         EXIT;
59 }
60
61 #ifdef DCACHE_LUSTRE_INVALID
62 /* Compare if two dentries are the same.  Don't match if the existing dentry
63  * is marked DCACHE_LUSTRE_INVALID.  Returns 1 if different, 0 if the same.
64  *
65  * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
66  * an AST before calling d_revalidate_it().  The dentry still exists (marked
67  * INVALID) so d_lookup() matches it, but we have no lock on it (so
68  * lock_match() fails) and we spin around real_lookup(). */
69 int ll_dcompare(struct dentry *parent, struct qstr *d_name, struct qstr *name)
70 {
71         struct dentry *dchild;
72         ENTRY;
73
74         if (d_name->len != name->len)
75                 RETURN(1);
76
77         if (memcmp(d_name->name, name->name, name->len))
78                 RETURN(1);
79
80         /* XXX: d_name must be in-dentry structure */
81         dchild = container_of(d_name, struct dentry, d_name); /* ugh */
82         if (dchild->d_flags & DCACHE_LUSTRE_INVALID) {
83                 CDEBUG(D_DENTRY,"INVALID dentry %p not matched, was bug 3784\n",
84                        dchild);
85                 RETURN(1);
86         }
87
88         RETURN(0);
89 }
90 #endif
91
92 /* should NOT be called with the dcache lock, see fs/dcache.c */
93 static int ll_ddelete(struct dentry *de)
94 {
95         ENTRY;
96         LASSERT(de);
97 #ifndef DCACHE_LUSTRE_INVALID
98 #define DCACHE_LUSTRE_INVALID 0
99 #endif
100
101         CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
102                (de->d_flags & DCACHE_LUSTRE_INVALID ? "deleting" : "keeping"),
103                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
104                d_unhashed(de) ? "" : "hashed,",
105                list_empty(&de->d_subdirs) ? "" : "subdirs");
106 #if DCACHE_LUSTRE_INVALID == 0
107 #undef DCACHE_LUSTRE_INVALID
108 #endif
109
110         RETURN(0);
111 }
112
113 void ll_set_dd(struct dentry *de)
114 {
115         ENTRY;
116         LASSERT(de != NULL);
117
118         CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
119                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
120                atomic_read(&de->d_count));
121
122         if (de->d_fsdata == NULL) {
123                 struct ll_dentry_data *lld;
124
125                 OBD_ALLOC(lld, sizeof(struct ll_dentry_data));
126                 if (likely(lld != NULL)) {
127                         cfs_waitq_init(&lld->lld_waitq);
128                         lock_dentry(de);
129                         if (likely(de->d_fsdata == NULL))
130                                 de->d_fsdata = lld;
131                         else
132                                 OBD_FREE(lld, sizeof(struct ll_dentry_data));
133                         unlock_dentry(de);
134                 }
135         }
136
137         EXIT;
138 }
139
140 void ll_intent_drop_lock(struct lookup_intent *it)
141 {
142         struct lustre_handle *handle;
143
144         if (it->it_op && it->d.lustre.it_lock_mode) {
145                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
146                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
147                        " from it %p\n", handle->cookie, it);
148                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
149
150                 /* bug 494: intent_release may be called multiple times, from
151                  * this thread and we don't want to double-decref this lock */
152                 it->d.lustre.it_lock_mode = 0;
153         }
154 }
155
156 void ll_intent_release(struct lookup_intent *it)
157 {
158         ENTRY;
159
160         ll_intent_drop_lock(it);
161 #ifdef HAVE_VFS_INTENT_PATCHES
162         it->it_magic = 0;
163         it->it_op_release = 0;
164 #endif
165         /* We are still holding extra reference on a request, need to free it */
166         if (it_disposition(it, DISP_ENQ_OPEN_REF)) /* open req for llfile_open*/
167                 ptlrpc_req_finished(it->d.lustre.it_data);
168         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
169                 ptlrpc_req_finished(it->d.lustre.it_data);
170         if (it_disposition(it, DISP_ENQ_COMPLETE)) /* saved req from revalidate
171                                                     * to lookup */
172                 ptlrpc_req_finished(it->d.lustre.it_data);
173
174         it->d.lustre.it_disposition = 0;
175         it->d.lustre.it_data = NULL;
176         EXIT;
177 }
178
179 /* Drop dentry if it is not used already, unhash otherwise.
180    Should be called with dcache lock held!
181    Returns: 1 if dentry was dropped, 0 if unhashed. */
182 int ll_drop_dentry(struct dentry *dentry)
183 {
184         lock_dentry(dentry);
185         if (atomic_read(&dentry->d_count) == 0) {
186                 CDEBUG(D_DENTRY, "deleting dentry %.*s (%p) parent %p "
187                        "inode %p\n", dentry->d_name.len,
188                        dentry->d_name.name, dentry, dentry->d_parent,
189                        dentry->d_inode);
190                 dget_locked(dentry);
191                 __d_drop(dentry);
192                 unlock_dentry(dentry);
193                 spin_unlock(&dcache_lock);
194                 dput(dentry);
195                 spin_lock(&dcache_lock);
196                 return 1;
197         }
198         /* disconected dentry can not be find without lookup, because we 
199          * not need his to unhash or mark invalid. */
200         if (dentry->d_flags & DCACHE_DISCONNECTED) {
201                 unlock_dentry(dentry);
202                 RETURN (0);
203         }
204
205 #ifdef DCACHE_LUSTRE_INVALID
206         if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
207 #else
208         if (!d_unhashed(dentry)) {
209 #endif
210                 CDEBUG(D_DENTRY, "unhashing dentry %.*s (%p) parent %p "
211                        "inode %p refc %d\n", dentry->d_name.len,
212                        dentry->d_name.name, dentry, dentry->d_parent,
213                        dentry->d_inode, atomic_read(&dentry->d_count));
214                 /* actually we don't unhash the dentry, rather just
215                  * mark it inaccessible for to __d_lookup(). otherwise
216                  * sys_getcwd() could return -ENOENT -bzzz */
217 #ifdef DCACHE_LUSTRE_INVALID
218                 dentry->d_flags |= DCACHE_LUSTRE_INVALID;
219 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
220                 __d_drop(dentry);
221                 if (dentry->d_inode) {
222                         /* Put positive dentries to orphan list */
223                         list_add(&dentry->d_hash,
224                                  &ll_i2sbi(dentry->d_inode)->ll_orphan_dentry_list);
225                 }
226 #endif
227 #else
228                 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
229                         __d_drop(dentry);
230 #endif
231
232         }
233         unlock_dentry(dentry);
234         return 0;
235 }
236
237 void ll_unhash_aliases(struct inode *inode)
238 {
239         struct list_head *tmp, *head;
240         ENTRY;
241
242         if (inode == NULL) {
243                 CERROR("unexpected NULL inode, tell phil\n");
244                 return;
245         }
246
247         CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
248                inode->i_ino, inode->i_generation, inode);
249
250         head = &inode->i_dentry;
251         spin_lock(&dcache_lock);
252 restart:
253         tmp = head;
254         while ((tmp = tmp->next) != head) {
255                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
256
257                 if (dentry->d_name.len == 1 && dentry->d_name.name[0] == '/') {
258                         CERROR("called on root (?) dentry=%p, inode=%p "
259                                "ino=%lu\n", dentry, inode, inode->i_ino);
260                         lustre_dump_dentry(dentry, 1);
261                         libcfs_debug_dumpstack(NULL);
262                 } else if (d_mountpoint(dentry)) {
263                         /* For mountpoints we skip removal of the dentry
264                            which happens solely because we have a lock on it
265                            obtained when this dentry was not a mountpoint yet */
266                         CDEBUG(D_DENTRY, "Skippind mountpoint dentry removal "
267                                          "%.*s (%p) parent %p\n",
268                                           dentry->d_name.len,
269                                           dentry->d_name.name,
270                                           dentry, dentry->d_parent);
271
272                         continue;
273                 }
274
275                 if (ll_drop_dentry(dentry))
276                           goto restart;
277         }
278         spin_unlock(&dcache_lock);
279         EXIT;
280 }
281
282 int revalidate_it_finish(struct ptlrpc_request *request, int offset,
283                          struct lookup_intent *it, struct dentry *de)
284 {
285         int rc = 0;
286         ENTRY;
287
288         if (!request)
289                 RETURN(0);
290
291         if (it_disposition(it, DISP_LOOKUP_NEG))
292                 RETURN(-ENOENT);
293
294         rc = ll_prep_inode(ll_i2sbi(de->d_inode)->ll_osc_exp, &de->d_inode,
295                            request, offset, NULL);
296
297         RETURN(rc);
298 }
299
300 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
301 {
302         LASSERT(it != NULL);
303         LASSERT(dentry != NULL);
304
305         if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
306                 struct inode *inode = dentry->d_inode;
307                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
308                        inode, inode->i_ino, inode->i_generation);
309                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
310         }
311
312         /* drop lookup or getattr locks immediately */
313         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
314 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
315                 /* on 2.6 there are situation when several lookups and
316                  * revalidations may be requested during single operation.
317                  * therefore, we don't release intent here -bzzz */
318                 ll_intent_drop_lock(it);
319 #else
320                 ll_intent_release(it);
321 #endif
322         }
323 }
324
325 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
326 {
327         struct lookup_intent *it = *itp;
328 #if defined(HAVE_VFS_INTENT_PATCHES)&&(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
329         if (it) {
330                 LASSERTF(it->it_magic == INTENT_MAGIC, "bad intent magic: %x\n",
331                          it->it_magic);
332         }
333 #endif
334
335         if (!it || it->it_op == IT_GETXATTR)
336                 it = *itp = deft;
337
338 #ifdef HAVE_VFS_INTENT_PATCHES
339         it->it_op_release = ll_intent_release;
340 #endif
341 }
342
343 int ll_revalidate_it(struct dentry *de, int lookup_flags,
344                      struct lookup_intent *it)
345 {
346         struct mdc_op_data op_data;
347         struct ptlrpc_request *req = NULL;
348         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
349         struct obd_export *exp;
350         int first = 0, rc;
351
352         ENTRY;
353         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
354                LL_IT2STR(it));
355
356         if (de->d_inode == NULL) {
357                 /* We can only use negative dentries if this is stat or lookup,
358                    for opens and stuff we do need to query server. */
359                 /* If there is IT_CREAT in intent op set, then we must throw
360                    away this negative dentry and actually do the request to
361                    kernel to create whatever needs to be created (if possible)*/
362                 if (it && (it->it_op & IT_CREAT))
363                         RETURN(0);
364
365 #ifdef DCACHE_LUSTRE_INVALID
366                 if (de->d_flags & DCACHE_LUSTRE_INVALID)
367                         RETURN(0);
368 #endif
369
370                 rc = ll_have_md_lock(de->d_parent->d_inode,
371                                      MDS_INODELOCK_UPDATE);
372                 GOTO(out_sa, rc);
373         }
374
375         exp = ll_i2mdcexp(de->d_inode);
376
377         /* Never execute intents for mount points.
378          * Attributes will be fixed up in ll_inode_revalidate_it */
379         if (d_mountpoint(de))
380                 GOTO(out_sa, rc = 1);
381
382         /* Root of the lustre tree. Always valid.
383          * Attributes will be fixed up in ll_inode_revalidate_it */
384         if (de == de->d_sb->s_root)
385                 GOTO(out_sa, rc = 1);
386
387         OBD_FAIL_TIMEOUT(OBD_FAIL_MDC_REVALIDATE_PAUSE, 5);
388         ll_frob_intent(&it, &lookup_it);
389         LASSERT(it);
390
391         ll_prepare_mdc_op_data(&op_data, de->d_parent->d_inode, de->d_inode,
392                                de->d_name.name, de->d_name.len, 0, NULL);
393
394         if ((it->it_op == IT_OPEN) && de->d_inode) {
395                 struct inode *inode = de->d_inode;
396                 struct ll_inode_info *lli = ll_i2info(inode);
397                 struct obd_client_handle **och_p;
398                 __u64 *och_usecount;
399                 /* We used to check for MDS_INODELOCK_OPEN here, but in fact
400                  * just having LOOKUP lock is enough to justify inode is the
401                  * same. And if inode is the same and we have suitable
402                  * openhandle, then there is no point in doing another OPEN RPC
403                  * just to throw away newly received openhandle.
404                  * There are no security implications too, if file owner or
405                  * access mode is change, LOOKUP lock is revoked */
406
407                 if (it->it_flags & FMODE_WRITE) {
408                         och_p = &lli->lli_mds_write_och;
409                         och_usecount = &lli->lli_open_fd_write_count;
410                 } else if (it->it_flags & FMODE_EXEC) {
411                         och_p = &lli->lli_mds_exec_och;
412                         och_usecount = &lli->lli_open_fd_exec_count;
413                 } else {
414                         och_p = &lli->lli_mds_read_och;
415                         och_usecount = &lli->lli_open_fd_read_count;
416                 }
417                 /* Check for the proper lock. */
418                 if (!ll_have_md_lock(inode, MDS_INODELOCK_LOOKUP))
419                         goto do_lock;
420                 down(&lli->lli_och_sem);
421                 if (*och_p) { /* Everything is open already, do nothing */
422                         /*(*och_usecount)++;  Do not let them steal our open
423                                               handle from under us */
424                         /* XXX The code above was my original idea, but in case
425                            we have the handle, but we cannot use it due to later
426                            checks (e.g. O_CREAT|O_EXCL flags set), nobody
427                            would decrement counter increased here. So we just
428                            hope the lock won't be invalidated in between. But
429                            if it would be, we'll reopen the open request to
430                            MDS later during file open path */
431                         up(&lli->lli_och_sem);
432                         RETURN(1);
433                 } else {
434                         up(&lli->lli_och_sem);
435                 }
436         }
437
438         if (it->it_op == IT_GETATTR)
439                 first = ll_statahead_enter(de->d_parent->d_inode, &de, 0);
440
441 do_lock:
442         it->it_create_mode &= ~current->fs->umask;
443
444         rc = mdc_intent_lock(exp, &op_data, NULL, 0, it, lookup_flags,
445                              &req, ll_mdc_blocking_ast, 0);
446         if (it->it_op == IT_GETATTR && !first)
447                 ll_statahead_exit(de, rc);
448         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
449          * if all was well, it will return 1 if it found locks, 0 otherwise. */
450         if (req == NULL && rc >= 0) {
451                 if (!rc)
452                         goto do_lookup;
453                 GOTO(out, rc);
454         }
455
456         if (rc < 0) {
457                 if (rc != -ESTALE) {
458                         CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
459                                "%d\n", rc, it->d.lustre.it_status);
460                 }
461                 GOTO(out, rc = 0);
462         }
463
464 revalidate_finish:
465         rc = revalidate_it_finish(req, DLM_REPLY_REC_OFF, it, de);
466         if (rc != 0) {
467                 ll_intent_release(it);
468                 GOTO(out, rc = 0);
469         }
470         if ((it->it_op & IT_OPEN) && de->d_inode && 
471             !S_ISREG(de->d_inode->i_mode) && 
472             !S_ISDIR(de->d_inode->i_mode)) {
473                 ll_release_openhandle(de, it);
474         }
475         rc = 1;
476
477         /* unfortunately ll_intent_lock may cause a callback and revoke our
478          * dentry */
479         spin_lock(&dcache_lock);
480         lock_dentry(de);
481         __d_drop(de);
482         unlock_dentry(de);
483         d_rehash_cond(de, 0);
484         spin_unlock(&dcache_lock);
485
486  out:
487         /* We do not free request as it may be reused during following lookup
488          * (see comment in mdc/mdc_locks.c::mdc_intent_lock()), request will
489          * be freed in ll_lookup_it or in ll_intent_release. But if
490          * request was not completed, we need to free it. (bug 5154, 9903) */
491         if (req != NULL && !it_disposition(it, DISP_ENQ_COMPLETE))
492                 ptlrpc_req_finished(req);
493         if (rc == 0) {
494 #ifdef DCACHE_LUSTRE_INVALID
495                 ll_unhash_aliases(de->d_inode);
496                 /* done in ll_unhash_aliases()
497                 dentry->d_flags |= DCACHE_LUSTRE_INVALID; */
498 #else
499                 /* We do not want d_invalidate to kill all child dentries too */
500                 d_drop(de);
501 #endif
502         } else {
503                 CDEBUG(D_DENTRY, "revalidated dentry %.*s (%p) parent %p "
504                                "inode %p refc %d\n", de->d_name.len,
505                                de->d_name.name, de, de->d_parent, de->d_inode,
506                                atomic_read(&de->d_count));
507                 ll_lookup_finish_locks(it, de);
508 #ifdef DCACHE_LUSTRE_INVALID
509                 lock_dentry(de);
510                 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
511                 unlock_dentry(de);
512 #endif
513         }
514         RETURN(rc);
515 /* This part is here to combat evil-evil race in real_lookup on 2.6 kernels.
516  * The race details are: We enter do_lookup() looking for some name,
517  * there is nothing in dcache for this name yet and d_lookup() returns NULL.
518  * We proceed to real_lookup(), and while we do this, another process does
519  * open on the same file we looking up (most simple reproducer), open succeeds
520  * and the dentry is added. Now back to us. In real_lookup() we do d_lookup()
521  * again and suddenly find the dentry, so we call d_revalidate on it, but there
522  * is no lock, so without this code we would return 0, but unpatched
523  * real_lookup just returns -ENOENT in such a case instead of retrying the
524  * lookup. Once this is dealt with in real_lookup(), all of this ugly mess
525  * can go and we can just check locks in ->d_revalidate without doing any
526  * RPCs ever. */
527 do_lookup:
528         if (it != &lookup_it) {
529                 ll_lookup_finish_locks(it, de);
530                 it = &lookup_it;
531         }
532         /*do real lookup here */
533         ll_prepare_mdc_op_data(&op_data, de->d_parent->d_inode, NULL,
534                                de->d_name.name, de->d_name.len, 0, NULL);
535         rc = mdc_intent_lock(exp, &op_data, NULL, 0,  it, 0, &req,
536                              ll_mdc_blocking_ast, 0);
537         if (rc >= 0) {
538                 struct mds_body *mds_body = lustre_msg_buf(req->rq_repmsg,
539                                                            DLM_REPLY_REC_OFF,
540                                                            sizeof(*mds_body));
541                 struct ll_fid fid = { 0 };
542
543                 if (de->d_inode)
544                          ll_inode2fid(&fid, de->d_inode);
545
546                 /* see if we got same inode, if not - return error */
547                 if(!memcmp(&fid, &mds_body->fid1, sizeof(struct ll_fid)))
548                         goto revalidate_finish;
549                 ll_intent_release(it);
550         }
551         GOTO(out, rc = 0);
552
553 out_sa:
554         /*
555          * For rc == 1 case, should not return directly to prevent losing
556          * statahead windows; for rc == 0 case, the "lookup" will be done later.
557          */
558         if (it && it->it_op == IT_GETATTR && rc == 1) {
559                 first = ll_statahead_enter(de->d_parent->d_inode, &de, 0);
560                 if (!first)
561                         ll_statahead_exit(de, rc);
562         }
563
564         return rc;
565 }
566
567 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
568 {
569         struct inode *inode= de->d_inode;
570         struct ll_sb_info *sbi = ll_i2sbi(inode);
571         struct ll_dentry_data *ldd = ll_d2d(de);
572         struct obd_client_handle *handle;
573         int rc = 0;
574         ENTRY;
575         LASSERT(ldd);
576
577         lock_kernel();
578         /* Strictly speaking this introduces an additional race: the
579          * increments should wait until the rpc has returned.
580          * However, given that at present the function is void, this
581          * issue is moot. */
582         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
583                 unlock_kernel();
584                 EXIT;
585                 return;
586         }
587
588         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
589                 unlock_kernel();
590                 EXIT;
591                 return;
592         }
593         unlock_kernel();
594
595         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
596         rc = obd_pin(sbi->ll_mdc_exp, inode->i_ino, inode->i_generation,
597                      inode->i_mode & S_IFMT, handle, flag);
598
599         if (rc) {
600                 lock_kernel();
601                 memset(handle, 0, sizeof(*handle));
602                 if (flag == 0)
603                         ldd->lld_cwd_count--;
604                 else
605                         ldd->lld_mnt_count--;
606                 unlock_kernel();
607         }
608
609         EXIT;
610         return;
611 }
612
613 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
614 {
615         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
616         struct ll_dentry_data *ldd = ll_d2d(de);
617         struct obd_client_handle handle;
618         int count, rc = 0;
619         ENTRY;
620         LASSERT(ldd);
621
622         lock_kernel();
623         /* Strictly speaking this introduces an additional race: the
624          * increments should wait until the rpc has returned.
625          * However, given that at present the function is void, this
626          * issue is moot. */
627         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
628         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
629                 /* the "pin" failed */
630                 unlock_kernel();
631                 EXIT;
632                 return;
633         }
634
635         if (flag)
636                 count = --ldd->lld_mnt_count;
637         else
638                 count = --ldd->lld_cwd_count;
639         unlock_kernel();
640
641         if (count != 0) {
642                 EXIT;
643                 return;
644         }
645
646         rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
647         EXIT;
648         return;
649 }
650
651 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
652 #ifdef HAVE_VFS_INTENT_PATCHES
653 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
654 {
655         int rc;
656         ENTRY;
657
658         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
659                 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
660         else
661                 rc = ll_revalidate_it(dentry, 0, NULL);
662
663         RETURN(rc);
664 }
665 #else
666 int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
667 {
668         int rc;
669         ENTRY;
670
671         if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
672                 struct lookup_intent *it;
673                 it = ll_convert_intent(&nd->intent.open, nd->flags);
674                 if (IS_ERR(it))
675                         RETURN(0);
676                 if (it->it_op == (IT_OPEN|IT_CREAT))
677                         if (nd->intent.open.flags & O_EXCL) {
678                                 CDEBUG(D_VFSTRACE, "create O_EXCL, returning 0\n");
679                                 rc = 0;
680                                 goto out_it;
681                         }
682
683                 rc = ll_revalidate_it(dentry, nd->flags, it);
684
685                 if (rc && (nd->flags & LOOKUP_OPEN) &&
686                     it_disposition(it, DISP_OPEN_OPEN)) {/*Open*/
687 #ifdef HAVE_FILE_IN_STRUCT_INTENT
688 // XXX Code duplication with ll_lookup_nd
689                         if (S_ISFIFO(dentry->d_inode->i_mode)) {
690                                 // We cannot call open here as it would
691                                 // deadlock.
692                                 ptlrpc_req_finished(
693                                                (struct ptlrpc_request *)
694                                                   it->d.lustre.it_data);
695                         } else {
696                                 struct file *filp;
697
698                                 nd->intent.open.file->private_data = it;
699                                 filp = lookup_instantiate_filp(nd, dentry,NULL);
700 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
701 /* 2.6.1[456] have a bug in open_namei() that forgets to check
702  * nd->intent.open.file for error, so we need to return it as lookup's result
703  * instead */
704                                 if (IS_ERR(filp))
705                                         rc = 0;
706 #endif
707                         }
708 #else
709                         ll_release_openhandle(dentry, it);
710 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
711                 }
712                 if (!rc && (nd->flags & LOOKUP_CREATE) &&
713                     it_disposition(it, DISP_OPEN_CREATE)) {
714                         /* We created something but we may only return
715                          * negative dentry here, so save request in dentry,
716                          * if lookup will be called later on, it will
717                          * pick the request, otherwise it would be freed
718                          * with dentry */
719                         ll_d2d(dentry)->lld_it = it;
720                         it = NULL; /* avoid freeing */
721                 }
722                         
723 out_it:
724                 if (it) {
725                         ll_intent_release(it);
726                         OBD_FREE(it, sizeof(*it));
727                 }
728         } else {
729                 rc = ll_revalidate_it(dentry, 0, NULL);
730         }
731
732         RETURN(rc);
733 }
734 #endif
735 #endif
736
737 struct dentry_operations ll_d_ops = {
738 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
739         .d_revalidate = ll_revalidate_nd,
740 #else
741         .d_revalidate_it = ll_revalidate_it,
742 #endif
743         .d_release = ll_release,
744         .d_delete = ll_ddelete,
745 #ifdef DCACHE_LUSTRE_INVALID
746         .d_compare = ll_dcompare,
747 #endif
748 #if 0
749         .d_pin = ll_pin,
750         .d_unpin = ll_unpin,
751 #endif
752 };
753
754 static int ll_fini_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
755 {
756         ENTRY;
757         /* need lookup */
758         RETURN(0);
759 }
760
761 struct dentry_operations ll_fini_d_ops = {
762         .d_revalidate = ll_fini_revalidate_nd,
763         .d_release = ll_release,
764 };
765
766 /*
767  * It is for the following race condition:
768  * When someone (maybe statahead thread) adds the dentry to the dentry hash
769  * table, the dentry's "d_op" maybe NULL, at the same time, another (maybe
770  * "ls -l") process finds such dentry by "do_lookup()" without "do_revalidate()"
771  * called. It causes statahead window lost, and maybe other issues. --Fan Yong
772  */
773 static int ll_init_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
774 {
775         struct l_wait_info lwi = { 0 };
776         struct ll_dentry_data *lld;
777         ENTRY;
778
779         ll_set_dd(dentry);
780         lld = ll_d2d(dentry);
781         if (unlikely(lld == NULL))
782                 RETURN(-ENOMEM);
783
784         l_wait_event(lld->lld_waitq, dentry->d_op != &ll_init_d_ops, &lwi);
785         if (likely(dentry->d_op == &ll_d_ops))
786                 RETURN(ll_revalidate_nd(dentry, nd));
787         else
788                 RETURN(dentry->d_op == &ll_fini_d_ops ? 0 : -EINVAL);
789 }
790
791 struct dentry_operations ll_init_d_ops = {
792         .d_revalidate = ll_init_revalidate_nd,
793         .d_release = ll_release,
794 };