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