1/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
17#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/fsnotify.h>
22#include <linux/slab.h>
23#include <linux/init.h>
24#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
34#include <linux/fs_struct.h>
35#include <linux/hardirq.h>
36#include "internal.h"
37
38int sysctl_vfs_cache_pressure __read_mostly = 100;
39EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
40
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
42__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
43
44EXPORT_SYMBOL(dcache_lock);
45
46static struct kmem_cache *dentry_cache __read_mostly;
47
48#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
49
50/*
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
54 *
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
57 */
58#define D_HASHBITS     d_hash_shift
59#define D_HASHMASK     d_hash_mask
60
61static unsigned int d_hash_mask __read_mostly;
62static unsigned int d_hash_shift __read_mostly;
63static struct hlist_head *dentry_hashtable __read_mostly;
64
65/* Statistics gathering. */
66struct dentry_stat_t dentry_stat = {
67	.age_limit = 45,
68};
69
70static void __d_free(struct dentry *dentry)
71{
72	WARN_ON(!list_empty(&dentry->d_alias));
73	if (dname_external(dentry))
74		kfree(dentry->d_name.name);
75	kmem_cache_free(dentry_cache, dentry);
76}
77
78static void d_callback(struct rcu_head *head)
79{
80	struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
81	__d_free(dentry);
82}
83
84/*
85 * no dcache_lock, please.  The caller must decrement dentry_stat.nr_dentry
86 * inside dcache_lock.
87 */
88static void d_free(struct dentry *dentry)
89{
90	if (dentry->d_op && dentry->d_op->d_release)
91		dentry->d_op->d_release(dentry);
92	/* if dentry was never inserted into hash, immediate free is OK */
93	if (hlist_unhashed(&dentry->d_hash))
94		__d_free(dentry);
95	else
96		call_rcu(&dentry->d_u.d_rcu, d_callback);
97}
98
99/*
100 * Release the dentry's inode, using the filesystem
101 * d_iput() operation if defined.
102 */
103static void dentry_iput(struct dentry * dentry)
104	__releases(dentry->d_lock)
105	__releases(dcache_lock)
106{
107	struct inode *inode = dentry->d_inode;
108	if (inode) {
109		dentry->d_inode = NULL;
110		list_del_init(&dentry->d_alias);
111		spin_unlock(&dentry->d_lock);
112		spin_unlock(&dcache_lock);
113		if (!inode->i_nlink)
114			fsnotify_inoderemove(inode);
115		if (dentry->d_op && dentry->d_op->d_iput)
116			dentry->d_op->d_iput(dentry, inode);
117		else
118			iput(inode);
119	} else {
120		spin_unlock(&dentry->d_lock);
121		spin_unlock(&dcache_lock);
122	}
123}
124
125/*
126 * dentry_lru_(add|add_tail|del|del_init) must be called with dcache_lock held.
127 */
128static void dentry_lru_add(struct dentry *dentry)
129{
130	list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
131	dentry->d_sb->s_nr_dentry_unused++;
132	dentry_stat.nr_unused++;
133}
134
135static void dentry_lru_add_tail(struct dentry *dentry)
136{
137	list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
138	dentry->d_sb->s_nr_dentry_unused++;
139	dentry_stat.nr_unused++;
140}
141
142static void dentry_lru_del(struct dentry *dentry)
143{
144	if (!list_empty(&dentry->d_lru)) {
145		list_del(&dentry->d_lru);
146		dentry->d_sb->s_nr_dentry_unused--;
147		dentry_stat.nr_unused--;
148	}
149}
150
151static void dentry_lru_del_init(struct dentry *dentry)
152{
153	if (likely(!list_empty(&dentry->d_lru))) {
154		list_del_init(&dentry->d_lru);
155		dentry->d_sb->s_nr_dentry_unused--;
156		dentry_stat.nr_unused--;
157	}
158}
159
160/**
161 * d_kill - kill dentry and return parent
162 * @dentry: dentry to kill
163 *
164 * The dentry must already be unhashed and removed from the LRU.
165 *
166 * If this is the root of the dentry tree, return NULL.
167 */
168static struct dentry *d_kill(struct dentry *dentry)
169	__releases(dentry->d_lock)
170	__releases(dcache_lock)
171{
172	struct dentry *parent;
173
174	list_del(&dentry->d_u.d_child);
175	dentry_stat.nr_dentry--;	/* For d_free, below */
176	/*drops the locks, at that point nobody can reach this dentry */
177	dentry_iput(dentry);
178	if (IS_ROOT(dentry))
179		parent = NULL;
180	else
181		parent = dentry->d_parent;
182	d_free(dentry);
183	return parent;
184}
185
186/*
187 * This is dput
188 *
189 * This is complicated by the fact that we do not want to put
190 * dentries that are no longer on any hash chain on the unused
191 * list: we'd much rather just get rid of them immediately.
192 *
193 * However, that implies that we have to traverse the dentry
194 * tree upwards to the parents which might _also_ now be
195 * scheduled for deletion (it may have been only waiting for
196 * its last child to go away).
197 *
198 * This tail recursion is done by hand as we don't want to depend
199 * on the compiler to always get this right (gcc generally doesn't).
200 * Real recursion would eat up our stack space.
201 */
202
203/*
204 * dput - release a dentry
205 * @dentry: dentry to release
206 *
207 * Release a dentry. This will drop the usage count and if appropriate
208 * call the dentry unlink method as well as removing it from the queues and
209 * releasing its resources. If the parent dentries were scheduled for release
210 * they too may now get deleted.
211 *
212 * no dcache lock, please.
213 */
214
215void dput(struct dentry *dentry)
216{
217	if (!dentry)
218		return;
219
220repeat:
221	if (atomic_read(&dentry->d_count) == 1)
222		might_sleep();
223	if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
224		return;
225
226	spin_lock(&dentry->d_lock);
227	if (atomic_read(&dentry->d_count)) {
228		spin_unlock(&dentry->d_lock);
229		spin_unlock(&dcache_lock);
230		return;
231	}
232
233	/*
234	 * AV: ->d_delete() is _NOT_ allowed to block now.
235	 */
236	if (dentry->d_op && dentry->d_op->d_delete) {
237		if (dentry->d_op->d_delete(dentry))
238			goto unhash_it;
239	}
240	/* Unreachable? Get rid of it */
241 	if (d_unhashed(dentry))
242		goto kill_it;
243  	if (list_empty(&dentry->d_lru)) {
244  		dentry->d_flags |= DCACHE_REFERENCED;
245		dentry_lru_add(dentry);
246  	}
247 	spin_unlock(&dentry->d_lock);
248	spin_unlock(&dcache_lock);
249	return;
250
251unhash_it:
252	__d_drop(dentry);
253kill_it:
254	/* if dentry was on the d_lru list delete it from there */
255	dentry_lru_del(dentry);
256	dentry = d_kill(dentry);
257	if (dentry)
258		goto repeat;
259}
260EXPORT_SYMBOL(dput);
261
262/**
263 * d_invalidate - invalidate a dentry
264 * @dentry: dentry to invalidate
265 *
266 * Try to invalidate the dentry if it turns out to be
267 * possible. If there are other dentries that can be
268 * reached through this one we can't delete it and we
269 * return -EBUSY. On success we return 0.
270 *
271 * no dcache lock.
272 */
273
274int d_invalidate(struct dentry * dentry)
275{
276	/*
277	 * If it's already been dropped, return OK.
278	 */
279	spin_lock(&dcache_lock);
280	if (d_unhashed(dentry)) {
281		spin_unlock(&dcache_lock);
282		return 0;
283	}
284	/*
285	 * Check whether to do a partial shrink_dcache
286	 * to get rid of unused child entries.
287	 */
288	if (!list_empty(&dentry->d_subdirs)) {
289		spin_unlock(&dcache_lock);
290		shrink_dcache_parent(dentry);
291		spin_lock(&dcache_lock);
292	}
293
294	/*
295	 * Somebody else still using it?
296	 *
297	 * If it's a directory, we can't drop it
298	 * for fear of somebody re-populating it
299	 * with children (even though dropping it
300	 * would make it unreachable from the root,
301	 * we might still populate it if it was a
302	 * working directory or similar).
303	 */
304	spin_lock(&dentry->d_lock);
305	if (atomic_read(&dentry->d_count) > 1) {
306		if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
307			spin_unlock(&dentry->d_lock);
308			spin_unlock(&dcache_lock);
309			return -EBUSY;
310		}
311	}
312
313	__d_drop(dentry);
314	spin_unlock(&dentry->d_lock);
315	spin_unlock(&dcache_lock);
316	return 0;
317}
318EXPORT_SYMBOL(d_invalidate);
319
320/* This should be called _only_ with dcache_lock held */
321
322static inline struct dentry * __dget_locked(struct dentry *dentry)
323{
324	atomic_inc(&dentry->d_count);
325	dentry_lru_del_init(dentry);
326	return dentry;
327}
328
329struct dentry * dget_locked(struct dentry *dentry)
330{
331	return __dget_locked(dentry);
332}
333EXPORT_SYMBOL(dget_locked);
334
335/**
336 * d_find_alias - grab a hashed alias of inode
337 * @inode: inode in question
338 * @want_discon:  flag, used by d_splice_alias, to request
339 *          that only a DISCONNECTED alias be returned.
340 *
341 * If inode has a hashed alias, or is a directory and has any alias,
342 * acquire the reference to alias and return it. Otherwise return NULL.
343 * Notice that if inode is a directory there can be only one alias and
344 * it can be unhashed only if it has no children, or if it is the root
345 * of a filesystem.
346 *
347 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
348 * any other hashed alias over that one unless @want_discon is set,
349 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
350 */
351
352static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
353{
354	struct list_head *head, *next, *tmp;
355	struct dentry *alias, *discon_alias=NULL;
356
357	head = &inode->i_dentry;
358	next = inode->i_dentry.next;
359	while (next != head) {
360		tmp = next;
361		next = tmp->next;
362		prefetch(next);
363		alias = list_entry(tmp, struct dentry, d_alias);
364 		if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
365			if (IS_ROOT(alias) &&
366			    (alias->d_flags & DCACHE_DISCONNECTED))
367				discon_alias = alias;
368			else if (!want_discon) {
369				__dget_locked(alias);
370				return alias;
371			}
372		}
373	}
374	if (discon_alias)
375		__dget_locked(discon_alias);
376	return discon_alias;
377}
378
379struct dentry * d_find_alias(struct inode *inode)
380{
381	struct dentry *de = NULL;
382
383	if (!list_empty(&inode->i_dentry)) {
384		spin_lock(&dcache_lock);
385		de = __d_find_alias(inode, 0);
386		spin_unlock(&dcache_lock);
387	}
388	return de;
389}
390EXPORT_SYMBOL(d_find_alias);
391
392/*
393 *	Try to kill dentries associated with this inode.
394 * WARNING: you must own a reference to inode.
395 */
396void d_prune_aliases(struct inode *inode)
397{
398	struct dentry *dentry;
399restart:
400	spin_lock(&dcache_lock);
401	list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
402		spin_lock(&dentry->d_lock);
403		if (!atomic_read(&dentry->d_count)) {
404			__dget_locked(dentry);
405			__d_drop(dentry);
406			spin_unlock(&dentry->d_lock);
407			spin_unlock(&dcache_lock);
408			dput(dentry);
409			goto restart;
410		}
411		spin_unlock(&dentry->d_lock);
412	}
413	spin_unlock(&dcache_lock);
414}
415EXPORT_SYMBOL(d_prune_aliases);
416
417/*
418 * Throw away a dentry - free the inode, dput the parent.  This requires that
419 * the LRU list has already been removed.
420 *
421 * Try to prune ancestors as well.  This is necessary to prevent
422 * quadratic behavior of shrink_dcache_parent(), but is also expected
423 * to be beneficial in reducing dentry cache fragmentation.
424 */
425static void prune_one_dentry(struct dentry * dentry)
426	__releases(dentry->d_lock)
427	__releases(dcache_lock)
428	__acquires(dcache_lock)
429{
430	__d_drop(dentry);
431	dentry = d_kill(dentry);
432
433	/*
434	 * Prune ancestors.  Locking is simpler than in dput(),
435	 * because dcache_lock needs to be taken anyway.
436	 */
437	spin_lock(&dcache_lock);
438	while (dentry) {
439		if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
440			return;
441
442		if (dentry->d_op && dentry->d_op->d_delete)
443			dentry->d_op->d_delete(dentry);
444		dentry_lru_del_init(dentry);
445		__d_drop(dentry);
446		dentry = d_kill(dentry);
447		spin_lock(&dcache_lock);
448	}
449}
450
451/*
452 * Shrink the dentry LRU on a given superblock.
453 * @sb   : superblock to shrink dentry LRU.
454 * @count: If count is NULL, we prune all dentries on superblock.
455 * @flags: If flags is non-zero, we need to do special processing based on
456 * which flags are set. This means we don't need to maintain multiple
457 * similar copies of this loop.
458 */
459static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
460{
461	LIST_HEAD(referenced);
462	LIST_HEAD(tmp);
463	struct dentry *dentry;
464	int cnt = 0;
465
466	BUG_ON(!sb);
467	BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);
468	spin_lock(&dcache_lock);
469	if (count != NULL)
470		/* called from prune_dcache() and shrink_dcache_parent() */
471		cnt = *count;
472restart:
473	if (count == NULL)
474		list_splice_init(&sb->s_dentry_lru, &tmp);
475	else {
476		while (!list_empty(&sb->s_dentry_lru)) {
477			dentry = list_entry(sb->s_dentry_lru.prev,
478					struct dentry, d_lru);
479			BUG_ON(dentry->d_sb != sb);
480
481			spin_lock(&dentry->d_lock);
482			/*
483			 * If we are honouring the DCACHE_REFERENCED flag and
484			 * the dentry has this flag set, don't free it. Clear
485			 * the flag and put it back on the LRU.
486			 */
487			if ((flags & DCACHE_REFERENCED)
488				&& (dentry->d_flags & DCACHE_REFERENCED)) {
489				dentry->d_flags &= ~DCACHE_REFERENCED;
490				list_move(&dentry->d_lru, &referenced);
491				spin_unlock(&dentry->d_lock);
492			} else {
493				list_move_tail(&dentry->d_lru, &tmp);
494				spin_unlock(&dentry->d_lock);
495				cnt--;
496				if (!cnt)
497					break;
498			}
499			cond_resched_lock(&dcache_lock);
500		}
501	}
502	while (!list_empty(&tmp)) {
503		dentry = list_entry(tmp.prev, struct dentry, d_lru);
504		dentry_lru_del_init(dentry);
505		spin_lock(&dentry->d_lock);
506		/*
507		 * We found an inuse dentry which was not removed from
508		 * the LRU because of laziness during lookup.  Do not free
509		 * it - just keep it off the LRU list.
510		 */
511		if (atomic_read(&dentry->d_count)) {
512			spin_unlock(&dentry->d_lock);
513			continue;
514		}
515		prune_one_dentry(dentry);
516		/* dentry->d_lock was dropped in prune_one_dentry() */
517		cond_resched_lock(&dcache_lock);
518	}
519	if (count == NULL && !list_empty(&sb->s_dentry_lru))
520		goto restart;
521	if (count != NULL)
522		*count = cnt;
523	if (!list_empty(&referenced))
524		list_splice(&referenced, &sb->s_dentry_lru);
525	spin_unlock(&dcache_lock);
526}
527
528/**
529 * prune_dcache - shrink the dcache
530 * @count: number of entries to try to free
531 *
532 * Shrink the dcache. This is done when we need more memory, or simply when we
533 * need to unmount something (at which point we need to unuse all dentries).
534 *
535 * This function may fail to free any resources if all the dentries are in use.
536 */
537static void prune_dcache(int count)
538{
539	struct super_block *sb, *p = NULL;
540	int w_count;
541	int unused = dentry_stat.nr_unused;
542	int prune_ratio;
543	int pruned;
544
545	if (unused == 0 || count == 0)
546		return;
547	spin_lock(&dcache_lock);
548	if (count >= unused)
549		prune_ratio = 1;
550	else
551		prune_ratio = unused / count;
552	spin_lock(&sb_lock);
553	list_for_each_entry(sb, &super_blocks, s_list) {
554		if (list_empty(&sb->s_instances))
555			continue;
556		if (sb->s_nr_dentry_unused == 0)
557			continue;
558		sb->s_count++;
559		/* Now, we reclaim unused dentrins with fairness.
560		 * We reclaim them same percentage from each superblock.
561		 * We calculate number of dentries to scan on this sb
562		 * as follows, but the implementation is arranged to avoid
563		 * overflows:
564		 * number of dentries to scan on this sb =
565		 * count * (number of dentries on this sb /
566		 * number of dentries in the machine)
567		 */
568		spin_unlock(&sb_lock);
569		if (prune_ratio != 1)
570			w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
571		else
572			w_count = sb->s_nr_dentry_unused;
573		pruned = w_count;
574		/*
575		 * We need to be sure this filesystem isn't being unmounted,
576		 * otherwise we could race with generic_shutdown_super(), and
577		 * end up holding a reference to an inode while the filesystem
578		 * is unmounted.  So we try to get s_umount, and make sure
579		 * s_root isn't NULL.
580		 */
581		if (down_read_trylock(&sb->s_umount)) {
582			if ((sb->s_root != NULL) &&
583			    (!list_empty(&sb->s_dentry_lru))) {
584				spin_unlock(&dcache_lock);
585				__shrink_dcache_sb(sb, &w_count,
586						DCACHE_REFERENCED);
587				pruned -= w_count;
588				spin_lock(&dcache_lock);
589			}
590			up_read(&sb->s_umount);
591		}
592		spin_lock(&sb_lock);
593		if (p)
594			__put_super(p);
595		count -= pruned;
596		p = sb;
597		/* more work left to do? */
598		if (count <= 0)
599			break;
600	}
601	if (p)
602		__put_super(p);
603	spin_unlock(&sb_lock);
604	spin_unlock(&dcache_lock);
605}
606
607/**
608 * shrink_dcache_sb - shrink dcache for a superblock
609 * @sb: superblock
610 *
611 * Shrink the dcache for the specified super block. This
612 * is used to free the dcache before unmounting a file
613 * system
614 */
615void shrink_dcache_sb(struct super_block * sb)
616{
617	__shrink_dcache_sb(sb, NULL, 0);
618}
619EXPORT_SYMBOL(shrink_dcache_sb);
620
621/*
622 * destroy a single subtree of dentries for unmount
623 * - see the comments on shrink_dcache_for_umount() for a description of the
624 *   locking
625 */
626static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
627{
628	struct dentry *parent;
629	unsigned detached = 0;
630
631	BUG_ON(!IS_ROOT(dentry));
632
633	/* detach this root from the system */
634	spin_lock(&dcache_lock);
635	dentry_lru_del_init(dentry);
636	__d_drop(dentry);
637	spin_unlock(&dcache_lock);
638
639	for (;;) {
640		/* descend to the first leaf in the current subtree */
641		while (!list_empty(&dentry->d_subdirs)) {
642			struct dentry *loop;
643
644			/* this is a branch with children - detach all of them
645			 * from the system in one go */
646			spin_lock(&dcache_lock);
647			list_for_each_entry(loop, &dentry->d_subdirs,
648					    d_u.d_child) {
649				dentry_lru_del_init(loop);
650				__d_drop(loop);
651				cond_resched_lock(&dcache_lock);
652			}
653			spin_unlock(&dcache_lock);
654
655			/* move to the first child */
656			dentry = list_entry(dentry->d_subdirs.next,
657					    struct dentry, d_u.d_child);
658		}
659
660		/* consume the dentries from this leaf up through its parents
661		 * until we find one with children or run out altogether */
662		do {
663			struct inode *inode;
664
665			if (atomic_read(&dentry->d_count) != 0) {
666				printk(KERN_ERR
667				       "BUG: Dentry %p{i=%lx,n=%s}"
668				       " still in use (%d)"
669				       " [unmount of %s %s]\n",
670				       dentry,
671				       dentry->d_inode ?
672				       dentry->d_inode->i_ino : 0UL,
673				       dentry->d_name.name,
674				       atomic_read(&dentry->d_count),
675				       dentry->d_sb->s_type->name,
676				       dentry->d_sb->s_id);
677				BUG();
678			}
679
680			if (IS_ROOT(dentry))
681				parent = NULL;
682			else {
683				parent = dentry->d_parent;
684				atomic_dec(&parent->d_count);
685			}
686
687			list_del(&dentry->d_u.d_child);
688			detached++;
689
690			inode = dentry->d_inode;
691			if (inode) {
692				dentry->d_inode = NULL;
693				list_del_init(&dentry->d_alias);
694				if (dentry->d_op && dentry->d_op->d_iput)
695					dentry->d_op->d_iput(dentry, inode);
696				else
697					iput(inode);
698			}
699
700			d_free(dentry);
701
702			/* finished when we fall off the top of the tree,
703			 * otherwise we ascend to the parent and move to the
704			 * next sibling if there is one */
705			if (!parent)
706				goto out;
707
708			dentry = parent;
709
710		} while (list_empty(&dentry->d_subdirs));
711
712		dentry = list_entry(dentry->d_subdirs.next,
713				    struct dentry, d_u.d_child);
714	}
715out:
716	/* several dentries were freed, need to correct nr_dentry */
717	spin_lock(&dcache_lock);
718	dentry_stat.nr_dentry -= detached;
719	spin_unlock(&dcache_lock);
720}
721
722/*
723 * destroy the dentries attached to a superblock on unmounting
724 * - we don't need to use dentry->d_lock, and only need dcache_lock when
725 *   removing the dentry from the system lists and hashes because:
726 *   - the superblock is detached from all mountings and open files, so the
727 *     dentry trees will not be rearranged by the VFS
728 *   - s_umount is write-locked, so the memory pressure shrinker will ignore
729 *     any dentries belonging to this superblock that it comes across
730 *   - the filesystem itself is no longer permitted to rearrange the dentries
731 *     in this superblock
732 */
733void shrink_dcache_for_umount(struct super_block *sb)
734{
735	struct dentry *dentry;
736
737	if (down_read_trylock(&sb->s_umount))
738		BUG();
739
740	dentry = sb->s_root;
741	sb->s_root = NULL;
742	atomic_dec(&dentry->d_count);
743	shrink_dcache_for_umount_subtree(dentry);
744
745	while (!hlist_empty(&sb->s_anon)) {
746		dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
747		shrink_dcache_for_umount_subtree(dentry);
748	}
749}
750
751/*
752 * Search for at least 1 mount point in the dentry's subdirs.
753 * We descend to the next level whenever the d_subdirs
754 * list is non-empty and continue searching.
755 */
756
757/**
758 * have_submounts - check for mounts over a dentry
759 * @parent: dentry to check.
760 *
761 * Return true if the parent or its subdirectories contain
762 * a mount point
763 */
764
765int have_submounts(struct dentry *parent)
766{
767	struct dentry *this_parent = parent;
768	struct list_head *next;
769
770	spin_lock(&dcache_lock);
771	if (d_mountpoint(parent))
772		goto positive;
773repeat:
774	next = this_parent->d_subdirs.next;
775resume:
776	while (next != &this_parent->d_subdirs) {
777		struct list_head *tmp = next;
778		struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
779		next = tmp->next;
780		/* Have we found a mount point ? */
781		if (d_mountpoint(dentry))
782			goto positive;
783		if (!list_empty(&dentry->d_subdirs)) {
784			this_parent = dentry;
785			goto repeat;
786		}
787	}
788	/*
789	 * All done at this level ... ascend and resume the search.
790	 */
791	if (this_parent != parent) {
792		next = this_parent->d_u.d_child.next;
793		this_parent = this_parent->d_parent;
794		goto resume;
795	}
796	spin_unlock(&dcache_lock);
797	return 0; /* No mount points found in tree */
798positive:
799	spin_unlock(&dcache_lock);
800	return 1;
801}
802EXPORT_SYMBOL(have_submounts);
803
804/*
805 * Search the dentry child list for the specified parent,
806 * and move any unused dentries to the end of the unused
807 * list for prune_dcache(). We descend to the next level
808 * whenever the d_subdirs list is non-empty and continue
809 * searching.
810 *
811 * It returns zero iff there are no unused children,
812 * otherwise  it returns the number of children moved to
813 * the end of the unused list. This may not be the total
814 * number of unused children, because select_parent can
815 * drop the lock and return early due to latency
816 * constraints.
817 */
818static int select_parent(struct dentry * parent)
819{
820	struct dentry *this_parent = parent;
821	struct list_head *next;
822	int found = 0;
823
824	spin_lock(&dcache_lock);
825repeat:
826	next = this_parent->d_subdirs.next;
827resume:
828	while (next != &this_parent->d_subdirs) {
829		struct list_head *tmp = next;
830		struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
831		next = tmp->next;
832
833		dentry_lru_del_init(dentry);
834		/*
835		 * move only zero ref count dentries to the end
836		 * of the unused list for prune_dcache
837		 */
838		if (!atomic_read(&dentry->d_count)) {
839			dentry_lru_add_tail(dentry);
840			found++;
841		}
842
843		/*
844		 * We can return to the caller if we have found some (this
845		 * ensures forward progress). We'll be coming back to find
846		 * the rest.
847		 */
848		if (found && need_resched())
849			goto out;
850
851		/*
852		 * Descend a level if the d_subdirs list is non-empty.
853		 */
854		if (!list_empty(&dentry->d_subdirs)) {
855			this_parent = dentry;
856			goto repeat;
857		}
858	}
859	/*
860	 * All done at this level ... ascend and resume the search.
861	 */
862	if (this_parent != parent) {
863		next = this_parent->d_u.d_child.next;
864		this_parent = this_parent->d_parent;
865		goto resume;
866	}
867out:
868	spin_unlock(&dcache_lock);
869	return found;
870}
871
872/**
873 * shrink_dcache_parent - prune dcache
874 * @parent: parent of entries to prune
875 *
876 * Prune the dcache to remove unused children of the parent dentry.
877 */
878
879void shrink_dcache_parent(struct dentry * parent)
880{
881	struct super_block *sb = parent->d_sb;
882	int found;
883
884	while ((found = select_parent(parent)) != 0)
885		__shrink_dcache_sb(sb, &found, 0);
886}
887EXPORT_SYMBOL(shrink_dcache_parent);
888
889/*
890 * Scan `nr' dentries and return the number which remain.
891 *
892 * We need to avoid reentering the filesystem if the caller is performing a
893 * GFP_NOFS allocation attempt.  One example deadlock is:
894 *
895 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
896 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
897 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
898 *
899 * In this case we return -1 to tell the caller that we baled.
900 */
901static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
902{
903	if (nr) {
904		if (!(gfp_mask & __GFP_FS))
905			return -1;
906		prune_dcache(nr);
907	}
908	return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
909}
910
911static struct shrinker dcache_shrinker = {
912	.shrink = shrink_dcache_memory,
913	.seeks = DEFAULT_SEEKS,
914};
915
916/**
917 * d_alloc	-	allocate a dcache entry
918 * @parent: parent of entry to allocate
919 * @name: qstr of the name
920 *
921 * Allocates a dentry. It returns %NULL if there is insufficient memory
922 * available. On a success the dentry is returned. The name passed in is
923 * copied and the copy passed in may be reused after this call.
924 */
925
926struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
927{
928	struct dentry *dentry;
929	char *dname;
930
931	dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
932	if (!dentry)
933		return NULL;
934
935	if (name->len > DNAME_INLINE_LEN-1) {
936		dname = kmalloc(name->len + 1, GFP_KERNEL);
937		if (!dname) {
938			kmem_cache_free(dentry_cache, dentry);
939			return NULL;
940		}
941	} else  {
942		dname = dentry->d_iname;
943	}
944	dentry->d_name.name = dname;
945
946	dentry->d_name.len = name->len;
947	dentry->d_name.hash = name->hash;
948	memcpy(dname, name->name, name->len);
949	dname[name->len] = 0;
950
951	atomic_set(&dentry->d_count, 1);
952	dentry->d_flags = DCACHE_UNHASHED;
953	spin_lock_init(&dentry->d_lock);
954	dentry->d_inode = NULL;
955	dentry->d_parent = NULL;
956	dentry->d_sb = NULL;
957	dentry->d_op = NULL;
958	dentry->d_fsdata = NULL;
959	dentry->d_mounted = 0;
960	INIT_HLIST_NODE(&dentry->d_hash);
961	INIT_LIST_HEAD(&dentry->d_lru);
962	INIT_LIST_HEAD(&dentry->d_subdirs);
963	INIT_LIST_HEAD(&dentry->d_alias);
964
965	if (parent) {
966		dentry->d_parent = dget(parent);
967		dentry->d_sb = parent->d_sb;
968	} else {
969		INIT_LIST_HEAD(&dentry->d_u.d_child);
970	}
971
972	spin_lock(&dcache_lock);
973	if (parent)
974		list_add(&dentry->d_u.d_child, &parent->d_subdirs);
975	dentry_stat.nr_dentry++;
976	spin_unlock(&dcache_lock);
977
978	return dentry;
979}
980EXPORT_SYMBOL(d_alloc);
981
982struct dentry *d_alloc_name(struct dentry *parent, const char *name)
983{
984	struct qstr q;
985
986	q.name = name;
987	q.len = strlen(name);
988	q.hash = full_name_hash(q.name, q.len);
989	return d_alloc(parent, &q);
990}
991EXPORT_SYMBOL(d_alloc_name);
992
993/* the caller must hold dcache_lock */
994static void __d_instantiate(struct dentry *dentry, struct inode *inode)
995{
996	if (inode)
997		list_add(&dentry->d_alias, &inode->i_dentry);
998	dentry->d_inode = inode;
999	fsnotify_d_instantiate(dentry, inode);
1000}
1001
1002/**
1003 * d_instantiate - fill in inode information for a dentry
1004 * @entry: dentry to complete
1005 * @inode: inode to attach to this dentry
1006 *
1007 * Fill in inode information in the entry.
1008 *
1009 * This turns negative dentries into productive full members
1010 * of society.
1011 *
1012 * NOTE! This assumes that the inode count has been incremented
1013 * (or otherwise set) by the caller to indicate that it is now
1014 * in use by the dcache.
1015 */
1016
1017void d_instantiate(struct dentry *entry, struct inode * inode)
1018{
1019	BUG_ON(!list_empty(&entry->d_alias));
1020	spin_lock(&dcache_lock);
1021	__d_instantiate(entry, inode);
1022	spin_unlock(&dcache_lock);
1023	security_d_instantiate(entry, inode);
1024}
1025EXPORT_SYMBOL(d_instantiate);
1026
1027/**
1028 * d_instantiate_unique - instantiate a non-aliased dentry
1029 * @entry: dentry to instantiate
1030 * @inode: inode to attach to this dentry
1031 *
1032 * Fill in inode information in the entry. On success, it returns NULL.
1033 * If an unhashed alias of "entry" already exists, then we return the
1034 * aliased dentry instead and drop one reference to inode.
1035 *
1036 * Note that in order to avoid conflicts with rename() etc, the caller
1037 * had better be holding the parent directory semaphore.
1038 *
1039 * This also assumes that the inode count has been incremented
1040 * (or otherwise set) by the caller to indicate that it is now
1041 * in use by the dcache.
1042 */
1043static struct dentry *__d_instantiate_unique(struct dentry *entry,
1044					     struct inode *inode)
1045{
1046	struct dentry *alias;
1047	int len = entry->d_name.len;
1048	const char *name = entry->d_name.name;
1049	unsigned int hash = entry->d_name.hash;
1050
1051	if (!inode) {
1052		__d_instantiate(entry, NULL);
1053		return NULL;
1054	}
1055
1056	list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1057		struct qstr *qstr = &alias->d_name;
1058
1059		if (qstr->hash != hash)
1060			continue;
1061		if (alias->d_parent != entry->d_parent)
1062			continue;
1063		if (qstr->len != len)
1064			continue;
1065		if (memcmp(qstr->name, name, len))
1066			continue;
1067		dget_locked(alias);
1068		return alias;
1069	}
1070
1071	__d_instantiate(entry, inode);
1072	return NULL;
1073}
1074
1075struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1076{
1077	struct dentry *result;
1078
1079	BUG_ON(!list_empty(&entry->d_alias));
1080
1081	spin_lock(&dcache_lock);
1082	result = __d_instantiate_unique(entry, inode);
1083	spin_unlock(&dcache_lock);
1084
1085	if (!result) {
1086		security_d_instantiate(entry, inode);
1087		return NULL;
1088	}
1089
1090	BUG_ON(!d_unhashed(result));
1091	iput(inode);
1092	return result;
1093}
1094
1095EXPORT_SYMBOL(d_instantiate_unique);
1096
1097/**
1098 * d_alloc_root - allocate root dentry
1099 * @root_inode: inode to allocate the root for
1100 *
1101 * Allocate a root ("/") dentry for the inode given. The inode is
1102 * instantiated and returned. %NULL is returned if there is insufficient
1103 * memory or the inode passed is %NULL.
1104 */
1105
1106struct dentry * d_alloc_root(struct inode * root_inode)
1107{
1108	struct dentry *res = NULL;
1109
1110	if (root_inode) {
1111		static const struct qstr name = { .name = "/", .len = 1 };
1112
1113		res = d_alloc(NULL, &name);
1114		if (res) {
1115			res->d_sb = root_inode->i_sb;
1116			res->d_parent = res;
1117			d_instantiate(res, root_inode);
1118		}
1119	}
1120	return res;
1121}
1122EXPORT_SYMBOL(d_alloc_root);
1123
1124static inline struct hlist_head *d_hash(struct dentry *parent,
1125					unsigned long hash)
1126{
1127	hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1128	hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1129	return dentry_hashtable + (hash & D_HASHMASK);
1130}
1131
1132/**
1133 * d_obtain_alias - find or allocate a dentry for a given inode
1134 * @inode: inode to allocate the dentry for
1135 *
1136 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1137 * similar open by handle operations.  The returned dentry may be anonymous,
1138 * or may have a full name (if the inode was already in the cache).
1139 *
1140 * When called on a directory inode, we must ensure that the inode only ever
1141 * has one dentry.  If a dentry is found, that is returned instead of
1142 * allocating a new one.
1143 *
1144 * On successful return, the reference to the inode has been transferred
1145 * to the dentry.  In case of an error the reference on the inode is released.
1146 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1147 * be passed in and will be the error will be propagate to the return value,
1148 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1149 */
1150struct dentry *d_obtain_alias(struct inode *inode)
1151{
1152	static const struct qstr anonstring = { .name = "" };
1153	struct dentry *tmp;
1154	struct dentry *res;
1155
1156	if (!inode)
1157		return ERR_PTR(-ESTALE);
1158	if (IS_ERR(inode))
1159		return ERR_CAST(inode);
1160
1161	res = d_find_alias(inode);
1162	if (res)
1163		goto out_iput;
1164
1165	tmp = d_alloc(NULL, &anonstring);
1166	if (!tmp) {
1167		res = ERR_PTR(-ENOMEM);
1168		goto out_iput;
1169	}
1170	tmp->d_parent = tmp; /* make sure dput doesn't croak */
1171
1172	spin_lock(&dcache_lock);
1173	res = __d_find_alias(inode, 0);
1174	if (res) {
1175		spin_unlock(&dcache_lock);
1176		dput(tmp);
1177		goto out_iput;
1178	}
1179
1180	/* attach a disconnected dentry */
1181	spin_lock(&tmp->d_lock);
1182	tmp->d_sb = inode->i_sb;
1183	tmp->d_inode = inode;
1184	tmp->d_flags |= DCACHE_DISCONNECTED;
1185	tmp->d_flags &= ~DCACHE_UNHASHED;
1186	list_add(&tmp->d_alias, &inode->i_dentry);
1187	hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1188	spin_unlock(&tmp->d_lock);
1189
1190	spin_unlock(&dcache_lock);
1191	return tmp;
1192
1193 out_iput:
1194	iput(inode);
1195	return res;
1196}
1197EXPORT_SYMBOL(d_obtain_alias);
1198
1199/**
1200 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1201 * @inode:  the inode which may have a disconnected dentry
1202 * @dentry: a negative dentry which we want to point to the inode.
1203 *
1204 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1205 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1206 * and return it, else simply d_add the inode to the dentry and return NULL.
1207 *
1208 * This is needed in the lookup routine of any filesystem that is exportable
1209 * (via knfsd) so that we can build dcache paths to directories effectively.
1210 *
1211 * If a dentry was found and moved, then it is returned.  Otherwise NULL
1212 * is returned.  This matches the expected return value of ->lookup.
1213 *
1214 */
1215struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1216{
1217	struct dentry *new = NULL;
1218
1219	if (inode && S_ISDIR(inode->i_mode)) {
1220		spin_lock(&dcache_lock);
1221		new = __d_find_alias(inode, 1);
1222		if (new) {
1223			BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1224			spin_unlock(&dcache_lock);
1225			security_d_instantiate(new, inode);
1226			d_move(new, dentry);
1227			iput(inode);
1228		} else {
1229			/* already taking dcache_lock, so d_add() by hand */
1230			__d_instantiate(dentry, inode);
1231			spin_unlock(&dcache_lock);
1232			security_d_instantiate(dentry, inode);
1233			d_rehash(dentry);
1234		}
1235	} else
1236		d_add(dentry, inode);
1237	return new;
1238}
1239EXPORT_SYMBOL(d_splice_alias);
1240
1241/**
1242 * d_add_ci - lookup or allocate new dentry with case-exact name
1243 * @inode:  the inode case-insensitive lookup has found
1244 * @dentry: the negative dentry that was passed to the parent's lookup func
1245 * @name:   the case-exact name to be associated with the returned dentry
1246 *
1247 * This is to avoid filling the dcache with case-insensitive names to the
1248 * same inode, only the actual correct case is stored in the dcache for
1249 * case-insensitive filesystems.
1250 *
1251 * For a case-insensitive lookup match and if the the case-exact dentry
1252 * already exists in in the dcache, use it and return it.
1253 *
1254 * If no entry exists with the exact case name, allocate new dentry with
1255 * the exact case, and return the spliced entry.
1256 */
1257struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1258			struct qstr *name)
1259{
1260	int error;
1261	struct dentry *found;
1262	struct dentry *new;
1263
1264	/*
1265	 * First check if a dentry matching the name already exists,
1266	 * if not go ahead and create it now.
1267	 */
1268	found = d_hash_and_lookup(dentry->d_parent, name);
1269	if (!found) {
1270		new = d_alloc(dentry->d_parent, name);
1271		if (!new) {
1272			error = -ENOMEM;
1273			goto err_out;
1274		}
1275
1276		found = d_splice_alias(inode, new);
1277		if (found) {
1278			dput(new);
1279			return found;
1280		}
1281		return new;
1282	}
1283
1284	/*
1285	 * If a matching dentry exists, and it's not negative use it.
1286	 *
1287	 * Decrement the reference count to balance the iget() done
1288	 * earlier on.
1289	 */
1290	if (found->d_inode) {
1291		if (unlikely(found->d_inode != inode)) {
1292			/* This can't happen because bad inodes are unhashed. */
1293			BUG_ON(!is_bad_inode(inode));
1294			BUG_ON(!is_bad_inode(found->d_inode));
1295		}
1296		iput(inode);
1297		return found;
1298	}
1299
1300	/*
1301	 * Negative dentry: instantiate it unless the inode is a directory and
1302	 * already has a dentry.
1303	 */
1304	spin_lock(&dcache_lock);
1305	if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1306		__d_instantiate(found, inode);
1307		spin_unlock(&dcache_lock);
1308		security_d_instantiate(found, inode);
1309		return found;
1310	}
1311
1312	/*
1313	 * In case a directory already has a (disconnected) entry grab a
1314	 * reference to it, move it in place and use it.
1315	 */
1316	new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1317	dget_locked(new);
1318	spin_unlock(&dcache_lock);
1319	security_d_instantiate(found, inode);
1320	d_move(new, found);
1321	iput(inode);
1322	dput(found);
1323	return new;
1324
1325err_out:
1326	iput(inode);
1327	return ERR_PTR(error);
1328}
1329EXPORT_SYMBOL(d_add_ci);
1330
1331/**
1332 * d_lookup - search for a dentry
1333 * @parent: parent dentry
1334 * @name: qstr of name we wish to find
1335 * Returns: dentry, or NULL
1336 *
1337 * d_lookup searches the children of the parent dentry for the name in
1338 * question. If the dentry is found its reference count is incremented and the
1339 * dentry is returned. The caller must use dput to free the entry when it has
1340 * finished using it. %NULL is returned if the dentry does not exist.
1341 */
1342struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1343{
1344	struct dentry * dentry = NULL;
1345	unsigned long seq;
1346
1347        do {
1348                seq = read_seqbegin(&rename_lock);
1349                dentry = __d_lookup(parent, name);
1350                if (dentry)
1351			break;
1352	} while (read_seqretry(&rename_lock, seq));
1353	return dentry;
1354}
1355EXPORT_SYMBOL(d_lookup);
1356
1357/*
1358 * __d_lookup - search for a dentry (racy)
1359 * @parent: parent dentry
1360 * @name: qstr of name we wish to find
1361 * Returns: dentry, or NULL
1362 *
1363 * __d_lookup is like d_lookup, however it may (rarely) return a
1364 * false-negative result due to unrelated rename activity.
1365 *
1366 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1367 * however it must be used carefully, eg. with a following d_lookup in
1368 * the case of failure.
1369 *
1370 * __d_lookup callers must be commented.
1371 */
1372struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1373{
1374	unsigned int len = name->len;
1375	unsigned int hash = name->hash;
1376	const unsigned char *str = name->name;
1377	struct hlist_head *head = d_hash(parent,hash);
1378	struct dentry *found = NULL;
1379	struct hlist_node *node;
1380	struct dentry *dentry;
1381
1382	/*
1383	 * The hash list is protected using RCU.
1384	 *
1385	 * Take d_lock when comparing a candidate dentry, to avoid races
1386	 * with d_move().
1387	 *
1388	 * It is possible that concurrent renames can mess up our list
1389	 * walk here and result in missing our dentry, resulting in the
1390	 * false-negative result. d_lookup() protects against concurrent
1391	 * renames using rename_lock seqlock.
1392	 *
1393	 * See Documentation/vfs/dcache-locking.txt for more details.
1394	 */
1395	rcu_read_lock();
1396
1397	hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1398		struct qstr *qstr;
1399
1400		if (dentry->d_name.hash != hash)
1401			continue;
1402		if (dentry->d_parent != parent)
1403			continue;
1404
1405		spin_lock(&dentry->d_lock);
1406
1407		/*
1408		 * Recheck the dentry after taking the lock - d_move may have
1409		 * changed things. Don't bother checking the hash because
1410		 * we're about to compare the whole name anyway.
1411		 */
1412		if (dentry->d_parent != parent)
1413			goto next;
1414
1415		/* non-existing due to RCU? */
1416		if (d_unhashed(dentry))
1417			goto next;
1418
1419		/*
1420		 * It is safe to compare names since d_move() cannot
1421		 * change the qstr (protected by d_lock).
1422		 */
1423		qstr = &dentry->d_name;
1424		if (parent->d_op && parent->d_op->d_compare) {
1425			if (parent->d_op->d_compare(parent, qstr, name))
1426				goto next;
1427		} else {
1428			if (qstr->len != len)
1429				goto next;
1430			if (memcmp(qstr->name, str, len))
1431				goto next;
1432		}
1433
1434		atomic_inc(&dentry->d_count);
1435		found = dentry;
1436		spin_unlock(&dentry->d_lock);
1437		break;
1438next:
1439		spin_unlock(&dentry->d_lock);
1440 	}
1441 	rcu_read_unlock();
1442
1443 	return found;
1444}
1445
1446/**
1447 * d_hash_and_lookup - hash the qstr then search for a dentry
1448 * @dir: Directory to search in
1449 * @name: qstr of name we wish to find
1450 *
1451 * On hash failure or on lookup failure NULL is returned.
1452 */
1453struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1454{
1455	struct dentry *dentry = NULL;
1456
1457	/*
1458	 * Check for a fs-specific hash function. Note that we must
1459	 * calculate the standard hash first, as the d_op->d_hash()
1460	 * routine may choose to leave the hash value unchanged.
1461	 */
1462	name->hash = full_name_hash(name->name, name->len);
1463	if (dir->d_op && dir->d_op->d_hash) {
1464		if (dir->d_op->d_hash(dir, name) < 0)
1465			goto out;
1466	}
1467	dentry = d_lookup(dir, name);
1468out:
1469	return dentry;
1470}
1471
1472/**
1473 * d_validate - verify dentry provided from insecure source
1474 * @dentry: The dentry alleged to be valid child of @dparent
1475 * @dparent: The parent dentry (known to be valid)
1476 *
1477 * An insecure source has sent us a dentry, here we verify it and dget() it.
1478 * This is used by ncpfs in its readdir implementation.
1479 * Zero is returned in the dentry is invalid.
1480 */
1481
1482int d_validate(struct dentry *dentry, struct dentry *dparent)
1483{
1484	struct hlist_head *base;
1485	struct hlist_node *lhp;
1486
1487	/* Check whether the ptr might be valid at all.. */
1488	if (!kmem_ptr_validate(dentry_cache, dentry))
1489		goto out;
1490
1491	if (dentry->d_parent != dparent)
1492		goto out;
1493
1494	spin_lock(&dcache_lock);
1495	base = d_hash(dparent, dentry->d_name.hash);
1496	hlist_for_each(lhp,base) {
1497		/* hlist_for_each_entry_rcu() not required for d_hash list
1498		 * as it is parsed under dcache_lock
1499		 */
1500		if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1501			__dget_locked(dentry);
1502			spin_unlock(&dcache_lock);
1503			return 1;
1504		}
1505	}
1506	spin_unlock(&dcache_lock);
1507out:
1508	return 0;
1509}
1510EXPORT_SYMBOL(d_validate);
1511
1512/*
1513 * When a file is deleted, we have two options:
1514 * - turn this dentry into a negative dentry
1515 * - unhash this dentry and free it.
1516 *
1517 * Usually, we want to just turn this into
1518 * a negative dentry, but if anybody else is
1519 * currently using the dentry or the inode
1520 * we can't do that and we fall back on removing
1521 * it from the hash queues and waiting for
1522 * it to be deleted later when it has no users
1523 */
1524
1525/**
1526 * d_delete - delete a dentry
1527 * @dentry: The dentry to delete
1528 *
1529 * Turn the dentry into a negative dentry if possible, otherwise
1530 * remove it from the hash queues so it can be deleted later
1531 */
1532
1533void d_delete(struct dentry * dentry)
1534{
1535	int isdir = 0;
1536	/*
1537	 * Are we the only user?
1538	 */
1539	spin_lock(&dcache_lock);
1540	spin_lock(&dentry->d_lock);
1541	isdir = S_ISDIR(dentry->d_inode->i_mode);
1542	if (atomic_read(&dentry->d_count) == 1) {
1543		dentry->d_flags &= ~DCACHE_CANT_MOUNT;
1544		dentry_iput(dentry);
1545		fsnotify_nameremove(dentry, isdir);
1546		return;
1547	}
1548
1549	if (!d_unhashed(dentry))
1550		__d_drop(dentry);
1551
1552	spin_unlock(&dentry->d_lock);
1553	spin_unlock(&dcache_lock);
1554
1555	fsnotify_nameremove(dentry, isdir);
1556}
1557EXPORT_SYMBOL(d_delete);
1558
1559static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1560{
1561
1562 	entry->d_flags &= ~DCACHE_UNHASHED;
1563 	hlist_add_head_rcu(&entry->d_hash, list);
1564}
1565
1566static void _d_rehash(struct dentry * entry)
1567{
1568	__d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1569}
1570
1571/**
1572 * d_rehash	- add an entry back to the hash
1573 * @entry: dentry to add to the hash
1574 *
1575 * Adds a dentry to the hash according to its name.
1576 */
1577
1578void d_rehash(struct dentry * entry)
1579{
1580	spin_lock(&dcache_lock);
1581	spin_lock(&entry->d_lock);
1582	_d_rehash(entry);
1583	spin_unlock(&entry->d_lock);
1584	spin_unlock(&dcache_lock);
1585}
1586EXPORT_SYMBOL(d_rehash);
1587
1588/*
1589 * When switching names, the actual string doesn't strictly have to
1590 * be preserved in the target - because we're dropping the target
1591 * anyway. As such, we can just do a simple memcpy() to copy over
1592 * the new name before we switch.
1593 *
1594 * Note that we have to be a lot more careful about getting the hash
1595 * switched - we have to switch the hash value properly even if it
1596 * then no longer matches the actual (corrupted) string of the target.
1597 * The hash value has to match the hash queue that the dentry is on..
1598 */
1599static void switch_names(struct dentry *dentry, struct dentry *target)
1600{
1601	if (dname_external(target)) {
1602		if (dname_external(dentry)) {
1603			/*
1604			 * Both external: swap the pointers
1605			 */
1606			swap(target->d_name.name, dentry->d_name.name);
1607		} else {
1608			/*
1609			 * dentry:internal, target:external.  Steal target's
1610			 * storage and make target internal.
1611			 */
1612			memcpy(target->d_iname, dentry->d_name.name,
1613					dentry->d_name.len + 1);
1614			dentry->d_name.name = target->d_name.name;
1615			target->d_name.name = target->d_iname;
1616		}
1617	} else {
1618		if (dname_external(dentry)) {
1619			/*
1620			 * dentry:external, target:internal.  Give dentry's
1621			 * storage to target and make dentry internal
1622			 */
1623			memcpy(dentry->d_iname, target->d_name.name,
1624					target->d_name.len + 1);
1625			target->d_name.name = dentry->d_name.name;
1626			dentry->d_name.name = dentry->d_iname;
1627		} else {
1628			/*
1629			 * Both are internal.  Just copy target to dentry
1630			 */
1631			memcpy(dentry->d_iname, target->d_name.name,
1632					target->d_name.len + 1);
1633			dentry->d_name.len = target->d_name.len;
1634			return;
1635		}
1636	}
1637	swap(dentry->d_name.len, target->d_name.len);
1638}
1639
1640/*
1641 * We cannibalize "target" when moving dentry on top of it,
1642 * because it's going to be thrown away anyway. We could be more
1643 * polite about it, though.
1644 *
1645 * This forceful removal will result in ugly /proc output if
1646 * somebody holds a file open that got deleted due to a rename.
1647 * We could be nicer about the deleted file, and let it show
1648 * up under the name it had before it was deleted rather than
1649 * under the original name of the file that was moved on top of it.
1650 */
1651
1652/*
1653 * d_move_locked - move a dentry
1654 * @dentry: entry to move
1655 * @target: new dentry
1656 *
1657 * Update the dcache to reflect the move of a file name. Negative
1658 * dcache entries should not be moved in this way.
1659 */
1660static void d_move_locked(struct dentry * dentry, struct dentry * target)
1661{
1662	struct hlist_head *list;
1663
1664	if (!dentry->d_inode)
1665		printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1666
1667	write_seqlock(&rename_lock);
1668	if (target < dentry) {
1669		spin_lock(&target->d_lock);
1670		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1671	} else {
1672		spin_lock(&dentry->d_lock);
1673		spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1674	}
1675
1676	/* Move the dentry to the target hash queue, if on different bucket */
1677	if (d_unhashed(dentry))
1678		goto already_unhashed;
1679
1680	hlist_del_rcu(&dentry->d_hash);
1681
1682already_unhashed:
1683	list = d_hash(target->d_parent, target->d_name.hash);
1684	__d_rehash(dentry, list);
1685
1686	/* Unhash the target: dput() will then get rid of it */
1687	__d_drop(target);
1688
1689	list_del(&dentry->d_u.d_child);
1690	list_del(&target->d_u.d_child);
1691
1692	/* Switch the names.. */
1693	switch_names(dentry, target);
1694	swap(dentry->d_name.hash, target->d_name.hash);
1695
1696	/* ... and switch the parents */
1697	if (IS_ROOT(dentry)) {
1698		dentry->d_parent = target->d_parent;
1699		target->d_parent = target;
1700		INIT_LIST_HEAD(&target->d_u.d_child);
1701	} else {
1702		swap(dentry->d_parent, target->d_parent);
1703
1704		/* And add them back to the (new) parent lists */
1705		list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1706	}
1707
1708	list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1709	spin_unlock(&target->d_lock);
1710	fsnotify_d_move(dentry);
1711	spin_unlock(&dentry->d_lock);
1712	write_sequnlock(&rename_lock);
1713}
1714
1715/**
1716 * d_move - move a dentry
1717 * @dentry: entry to move
1718 * @target: new dentry
1719 *
1720 * Update the dcache to reflect the move of a file name. Negative
1721 * dcache entries should not be moved in this way.
1722 */
1723
1724void d_move(struct dentry * dentry, struct dentry * target)
1725{
1726	spin_lock(&dcache_lock);
1727	d_move_locked(dentry, target);
1728	spin_unlock(&dcache_lock);
1729}
1730EXPORT_SYMBOL(d_move);
1731
1732/**
1733 * d_ancestor - search for an ancestor
1734 * @p1: ancestor dentry
1735 * @p2: child dentry
1736 *
1737 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1738 * an ancestor of p2, else NULL.
1739 */
1740struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
1741{
1742	struct dentry *p;
1743
1744	for (p = p2; !IS_ROOT(p); p = p->d_parent) {
1745		if (p->d_parent == p1)
1746			return p;
1747	}
1748	return NULL;
1749}
1750
1751/*
1752 * This helper attempts to cope with remotely renamed directories
1753 *
1754 * It assumes that the caller is already holding
1755 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1756 *
1757 * Note: If ever the locking in lock_rename() changes, then please
1758 * remember to update this too...
1759 */
1760static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1761	__releases(dcache_lock)
1762{
1763	struct mutex *m1 = NULL, *m2 = NULL;
1764	struct dentry *ret;
1765
1766	/* If alias and dentry share a parent, then no extra locks required */
1767	if (alias->d_parent == dentry->d_parent)
1768		goto out_unalias;
1769
1770	/* Check for loops */
1771	ret = ERR_PTR(-ELOOP);
1772	if (d_ancestor(alias, dentry))
1773		goto out_err;
1774
1775	/* See lock_rename() */
1776	ret = ERR_PTR(-EBUSY);
1777	if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1778		goto out_err;
1779	m1 = &dentry->d_sb->s_vfs_rename_mutex;
1780	if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1781		goto out_err;
1782	m2 = &alias->d_parent->d_inode->i_mutex;
1783out_unalias:
1784	d_move_locked(alias, dentry);
1785	ret = alias;
1786out_err:
1787	spin_unlock(&dcache_lock);
1788	if (m2)
1789		mutex_unlock(m2);
1790	if (m1)
1791		mutex_unlock(m1);
1792	return ret;
1793}
1794
1795/*
1796 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1797 * named dentry in place of the dentry to be replaced.
1798 */
1799static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1800{
1801	struct dentry *dparent, *aparent;
1802
1803	switch_names(dentry, anon);
1804	swap(dentry->d_name.hash, anon->d_name.hash);
1805
1806	dparent = dentry->d_parent;
1807	aparent = anon->d_parent;
1808
1809	dentry->d_parent = (aparent == anon) ? dentry : aparent;
1810	list_del(&dentry->d_u.d_child);
1811	if (!IS_ROOT(dentry))
1812		list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1813	else
1814		INIT_LIST_HEAD(&dentry->d_u.d_child);
1815
1816	anon->d_parent = (dparent == dentry) ? anon : dparent;
1817	list_del(&anon->d_u.d_child);
1818	if (!IS_ROOT(anon))
1819		list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1820	else
1821		INIT_LIST_HEAD(&anon->d_u.d_child);
1822
1823	anon->d_flags &= ~DCACHE_DISCONNECTED;
1824}
1825
1826/**
1827 * d_materialise_unique - introduce an inode into the tree
1828 * @dentry: candidate dentry
1829 * @inode: inode to bind to the dentry, to which aliases may be attached
1830 *
1831 * Introduces an dentry into the tree, substituting an extant disconnected
1832 * root directory alias in its place if there is one
1833 */
1834struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1835{
1836	struct dentry *actual;
1837
1838	BUG_ON(!d_unhashed(dentry));
1839
1840	spin_lock(&dcache_lock);
1841
1842	if (!inode) {
1843		actual = dentry;
1844		__d_instantiate(dentry, NULL);
1845		goto found_lock;
1846	}
1847
1848	if (S_ISDIR(inode->i_mode)) {
1849		struct dentry *alias;
1850
1851		/* Does an aliased dentry already exist? */
1852		alias = __d_find_alias(inode, 0);
1853		if (alias) {
1854			actual = alias;
1855			/* Is this an anonymous mountpoint that we could splice
1856			 * into our tree? */
1857			if (IS_ROOT(alias)) {
1858				spin_lock(&alias->d_lock);
1859				__d_materialise_dentry(dentry, alias);
1860				__d_drop(alias);
1861				goto found;
1862			}
1863			/* Nope, but we must(!) avoid directory aliasing */
1864			actual = __d_unalias(dentry, alias);
1865			if (IS_ERR(actual))
1866				dput(alias);
1867			goto out_nolock;
1868		}
1869	}
1870
1871	/* Add a unique reference */
1872	actual = __d_instantiate_unique(dentry, inode);
1873	if (!actual)
1874		actual = dentry;
1875	else if (unlikely(!d_unhashed(actual)))
1876		goto shouldnt_be_hashed;
1877
1878found_lock:
1879	spin_lock(&actual->d_lock);
1880found:
1881	_d_rehash(actual);
1882	spin_unlock(&actual->d_lock);
1883	spin_unlock(&dcache_lock);
1884out_nolock:
1885	if (actual == dentry) {
1886		security_d_instantiate(dentry, inode);
1887		return NULL;
1888	}
1889
1890	iput(inode);
1891	return actual;
1892
1893shouldnt_be_hashed:
1894	spin_unlock(&dcache_lock);
1895	BUG();
1896}
1897EXPORT_SYMBOL_GPL(d_materialise_unique);
1898
1899static int prepend(char **buffer, int *buflen, const char *str, int namelen)
1900{
1901	*buflen -= namelen;
1902	if (*buflen < 0)
1903		return -ENAMETOOLONG;
1904	*buffer -= namelen;
1905	memcpy(*buffer, str, namelen);
1906	return 0;
1907}
1908
1909static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1910{
1911	return prepend(buffer, buflen, name->name, name->len);
1912}
1913
1914/**
1915 * Prepend path string to a buffer
1916 *
1917 * @path: the dentry/vfsmount to report
1918 * @root: root vfsmnt/dentry (may be modified by this function)
1919 * @buffer: pointer to the end of the buffer
1920 * @buflen: pointer to buffer length
1921 *
1922 * Caller holds the dcache_lock.
1923 *
1924 * If path is not reachable from the supplied root, then the value of
1925 * root is changed (without modifying refcounts).
1926 */
1927static int prepend_path(const struct path *path, struct path *root,
1928			char **buffer, int *buflen)
1929{
1930	struct dentry *dentry = path->dentry;
1931	struct vfsmount *vfsmnt = path->mnt;
1932	bool slash = false;
1933	int error = 0;
1934
1935	br_read_lock(vfsmount_lock);
1936	while (dentry != root->dentry || vfsmnt != root->mnt) {
1937		struct dentry * parent;
1938
1939		if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1940			/* Global root? */
1941			if (vfsmnt->mnt_parent == vfsmnt) {
1942				goto global_root;
1943			}
1944			dentry = vfsmnt->mnt_mountpoint;
1945			vfsmnt = vfsmnt->mnt_parent;
1946			continue;
1947		}
1948		parent = dentry->d_parent;
1949		prefetch(parent);
1950		error = prepend_name(buffer, buflen, &dentry->d_name);
1951		if (!error)
1952			error = prepend(buffer, buflen, "/", 1);
1953		if (error)
1954			break;
1955
1956		slash = true;
1957		dentry = parent;
1958	}
1959
1960out:
1961	if (!error && !slash)
1962		error = prepend(buffer, buflen, "/", 1);
1963
1964	br_read_unlock(vfsmount_lock);
1965	return error;
1966
1967global_root:
1968	/*
1969	 * Filesystems needing to implement special "root names"
1970	 * should do so with ->d_dname()
1971	 */
1972	if (IS_ROOT(dentry) &&
1973	    (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
1974		WARN(1, "Root dentry has weird name <%.*s>\n",
1975		     (int) dentry->d_name.len, dentry->d_name.name);
1976	}
1977	root->mnt = vfsmnt;
1978	root->dentry = dentry;
1979	goto out;
1980}
1981
1982/**
1983 * __d_path - return the path of a dentry
1984 * @path: the dentry/vfsmount to report
1985 * @root: root vfsmnt/dentry (may be modified by this function)
1986 * @buf: buffer to return value in
1987 * @buflen: buffer length
1988 *
1989 * Convert a dentry into an ASCII path name.
1990 *
1991 * Returns a pointer into the buffer or an error code if the
1992 * path was too long.
1993 *
1994 * "buflen" should be positive. Caller holds the dcache_lock.
1995 *
1996 * If path is not reachable from the supplied root, then the value of
1997 * root is changed (without modifying refcounts).
1998 */
1999char *__d_path(const struct path *path, struct path *root,
2000	       char *buf, int buflen)
2001{
2002	char *res = buf + buflen;
2003	int error;
2004
2005	prepend(&res, &buflen, "\0", 1);
2006	error = prepend_path(path, root, &res, &buflen);
2007	if (error)
2008		return ERR_PTR(error);
2009
2010	return res;
2011}
2012
2013/*
2014 * same as __d_path but appends "(deleted)" for unlinked files.
2015 */
2016static int path_with_deleted(const struct path *path, struct path *root,
2017				 char **buf, int *buflen)
2018{
2019	prepend(buf, buflen, "\0", 1);
2020	if (d_unlinked(path->dentry)) {
2021		int error = prepend(buf, buflen, " (deleted)", 10);
2022		if (error)
2023			return error;
2024	}
2025
2026	return prepend_path(path, root, buf, buflen);
2027}
2028
2029static int prepend_unreachable(char **buffer, int *buflen)
2030{
2031	return prepend(buffer, buflen, "(unreachable)", 13);
2032}
2033
2034/**
2035 * d_path - return the path of a dentry
2036 * @path: path to report
2037 * @buf: buffer to return value in
2038 * @buflen: buffer length
2039 *
2040 * Convert a dentry into an ASCII path name. If the entry has been deleted
2041 * the string " (deleted)" is appended. Note that this is ambiguous.
2042 *
2043 * Returns a pointer into the buffer or an error code if the path was
2044 * too long. Note: Callers should use the returned pointer, not the passed
2045 * in buffer, to use the name! The implementation often starts at an offset
2046 * into the buffer, and may leave 0 bytes at the start.
2047 *
2048 * "buflen" should be positive.
2049 */
2050char *d_path(const struct path *path, char *buf, int buflen)
2051{
2052	char *res = buf + buflen;
2053	struct path root;
2054	struct path tmp;
2055	int error;
2056
2057	/*
2058	 * We have various synthetic filesystems that never get mounted.  On
2059	 * these filesystems dentries are never used for lookup purposes, and
2060	 * thus don't need to be hashed.  They also don't need a name until a
2061	 * user wants to identify the object in /proc/pid/fd/.  The little hack
2062	 * below allows us to generate a name for these objects on demand:
2063	 */
2064	if (path->dentry->d_op && path->dentry->d_op->d_dname)
2065		return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2066
2067	get_fs_root(current->fs, &root);
2068	spin_lock(&dcache_lock);
2069	tmp = root;
2070	error = path_with_deleted(path, &tmp, &res, &buflen);
2071	if (error)
2072		res = ERR_PTR(error);
2073	spin_unlock(&dcache_lock);
2074	path_put(&root);
2075	return res;
2076}
2077EXPORT_SYMBOL(d_path);
2078
2079/**
2080 * d_path_with_unreachable - return the path of a dentry
2081 * @path: path to report
2082 * @buf: buffer to return value in
2083 * @buflen: buffer length
2084 *
2085 * The difference from d_path() is that this prepends "(unreachable)"
2086 * to paths which are unreachable from the current process' root.
2087 */
2088char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2089{
2090	char *res = buf + buflen;
2091	struct path root;
2092	struct path tmp;
2093	int error;
2094
2095	if (path->dentry->d_op && path->dentry->d_op->d_dname)
2096		return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2097
2098	get_fs_root(current->fs, &root);
2099	spin_lock(&dcache_lock);
2100	tmp = root;
2101	error = path_with_deleted(path, &tmp, &res, &buflen);
2102	if (!error && !path_equal(&tmp, &root))
2103		error = prepend_unreachable(&res, &buflen);
2104	spin_unlock(&dcache_lock);
2105	path_put(&root);
2106	if (error)
2107		res =  ERR_PTR(error);
2108
2109	return res;
2110}
2111
2112/*
2113 * Helper function for dentry_operations.d_dname() members
2114 */
2115char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2116			const char *fmt, ...)
2117{
2118	va_list args;
2119	char temp[64];
2120	int sz;
2121
2122	va_start(args, fmt);
2123	sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2124	va_end(args);
2125
2126	if (sz > sizeof(temp) || sz > buflen)
2127		return ERR_PTR(-ENAMETOOLONG);
2128
2129	buffer += buflen - sz;
2130	return memcpy(buffer, temp, sz);
2131}
2132
2133/*
2134 * Write full pathname from the root of the filesystem into the buffer.
2135 */
2136char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2137{
2138	char *end = buf + buflen;
2139	char *retval;
2140
2141	prepend(&end, &buflen, "\0", 1);
2142	if (buflen < 1)
2143		goto Elong;
2144	/* Get '/' right */
2145	retval = end-1;
2146	*retval = '/';
2147
2148	while (!IS_ROOT(dentry)) {
2149		struct dentry *parent = dentry->d_parent;
2150
2151		prefetch(parent);
2152		if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2153		    (prepend(&end, &buflen, "/", 1) != 0))
2154			goto Elong;
2155
2156		retval = end;
2157		dentry = parent;
2158	}
2159	return retval;
2160Elong:
2161	return ERR_PTR(-ENAMETOOLONG);
2162}
2163EXPORT_SYMBOL(__dentry_path);
2164
2165char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2166{
2167	char *p = NULL;
2168	char *retval;
2169
2170	spin_lock(&dcache_lock);
2171	if (d_unlinked(dentry)) {
2172		p = buf + buflen;
2173		if (prepend(&p, &buflen, "//deleted", 10) != 0)
2174			goto Elong;
2175		buflen++;
2176	}
2177	retval = __dentry_path(dentry, buf, buflen);
2178	spin_unlock(&dcache_lock);
2179	if (!IS_ERR(retval) && p)
2180		*p = '/';	/* restore '/' overriden with '\0' */
2181	return retval;
2182Elong:
2183	spin_unlock(&dcache_lock);
2184	return ERR_PTR(-ENAMETOOLONG);
2185}
2186
2187/*
2188 * NOTE! The user-level library version returns a
2189 * character pointer. The kernel system call just
2190 * returns the length of the buffer filled (which
2191 * includes the ending '\0' character), or a negative
2192 * error value. So libc would do something like
2193 *
2194 *	char *getcwd(char * buf, size_t size)
2195 *	{
2196 *		int retval;
2197 *
2198 *		retval = sys_getcwd(buf, size);
2199 *		if (retval >= 0)
2200 *			return buf;
2201 *		errno = -retval;
2202 *		return NULL;
2203 *	}
2204 */
2205SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2206{
2207	int error;
2208	struct path pwd, root;
2209	char *page = (char *) __get_free_page(GFP_USER);
2210
2211	if (!page)
2212		return -ENOMEM;
2213
2214	get_fs_root_and_pwd(current->fs, &root, &pwd);
2215
2216	error = -ENOENT;
2217	spin_lock(&dcache_lock);
2218	if (!d_unlinked(pwd.dentry)) {
2219		unsigned long len;
2220		struct path tmp = root;
2221		char *cwd = page + PAGE_SIZE;
2222		int buflen = PAGE_SIZE;
2223
2224		prepend(&cwd, &buflen, "\0", 1);
2225		error = prepend_path(&pwd, &tmp, &cwd, &buflen);
2226		spin_unlock(&dcache_lock);
2227
2228		if (error)
2229			goto out;
2230
2231		/* Unreachable from current root */
2232		if (!path_equal(&tmp, &root)) {
2233			error = prepend_unreachable(&cwd, &buflen);
2234			if (error)
2235				goto out;
2236		}
2237
2238		error = -ERANGE;
2239		len = PAGE_SIZE + page - cwd;
2240		if (len <= size) {
2241			error = len;
2242			if (copy_to_user(buf, cwd, len))
2243				error = -EFAULT;
2244		}
2245	} else
2246		spin_unlock(&dcache_lock);
2247
2248out:
2249	path_put(&pwd);
2250	path_put(&root);
2251	free_page((unsigned long) page);
2252	return error;
2253}
2254
2255/*
2256 * Test whether new_dentry is a subdirectory of old_dentry.
2257 *
2258 * Trivially implemented using the dcache structure
2259 */
2260
2261/**
2262 * is_subdir - is new dentry a subdirectory of old_dentry
2263 * @new_dentry: new dentry
2264 * @old_dentry: old dentry
2265 *
2266 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2267 * Returns 0 otherwise.
2268 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2269 */
2270
2271int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2272{
2273	int result;
2274	unsigned long seq;
2275
2276	if (new_dentry == old_dentry)
2277		return 1;
2278
2279	/*
2280	 * Need rcu_readlock to protect against the d_parent trashing
2281	 * due to d_move
2282	 */
2283	rcu_read_lock();
2284	do {
2285		/* for restarting inner loop in case of seq retry */
2286		seq = read_seqbegin(&rename_lock);
2287		if (d_ancestor(old_dentry, new_dentry))
2288			result = 1;
2289		else
2290			result = 0;
2291	} while (read_seqretry(&rename_lock, seq));
2292	rcu_read_unlock();
2293
2294	return result;
2295}
2296
2297int path_is_under(struct path *path1, struct path *path2)
2298{
2299	struct vfsmount *mnt = path1->mnt;
2300	struct dentry *dentry = path1->dentry;
2301	int res;
2302
2303	br_read_lock(vfsmount_lock);
2304	if (mnt != path2->mnt) {
2305		for (;;) {
2306			if (mnt->mnt_parent == mnt) {
2307				br_read_unlock(vfsmount_lock);
2308				return 0;
2309			}
2310			if (mnt->mnt_parent == path2->mnt)
2311				break;
2312			mnt = mnt->mnt_parent;
2313		}
2314		dentry = mnt->mnt_mountpoint;
2315	}
2316	res = is_subdir(dentry, path2->dentry);
2317	br_read_unlock(vfsmount_lock);
2318	return res;
2319}
2320EXPORT_SYMBOL(path_is_under);
2321
2322void d_genocide(struct dentry *root)
2323{
2324	struct dentry *this_parent = root;
2325	struct list_head *next;
2326
2327	spin_lock(&dcache_lock);
2328repeat:
2329	next = this_parent->d_subdirs.next;
2330resume:
2331	while (next != &this_parent->d_subdirs) {
2332		struct list_head *tmp = next;
2333		struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2334		next = tmp->next;
2335		if (d_unhashed(dentry)||!dentry->d_inode)
2336			continue;
2337		if (!list_empty(&dentry->d_subdirs)) {
2338			this_parent = dentry;
2339			goto repeat;
2340		}
2341		atomic_dec(&dentry->d_count);
2342	}
2343	if (this_parent != root) {
2344		next = this_parent->d_u.d_child.next;
2345		atomic_dec(&this_parent->d_count);
2346		this_parent = this_parent->d_parent;
2347		goto resume;
2348	}
2349	spin_unlock(&dcache_lock);
2350}
2351
2352/**
2353 * find_inode_number - check for dentry with name
2354 * @dir: directory to check
2355 * @name: Name to find.
2356 *
2357 * Check whether a dentry already exists for the given name,
2358 * and return the inode number if it has an inode. Otherwise
2359 * 0 is returned.
2360 *
2361 * This routine is used to post-process directory listings for
2362 * filesystems using synthetic inode numbers, and is necessary
2363 * to keep getcwd() working.
2364 */
2365
2366ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2367{
2368	struct dentry * dentry;
2369	ino_t ino = 0;
2370
2371	dentry = d_hash_and_lookup(dir, name);
2372	if (dentry) {
2373		if (dentry->d_inode)
2374			ino = dentry->d_inode->i_ino;
2375		dput(dentry);
2376	}
2377	return ino;
2378}
2379EXPORT_SYMBOL(find_inode_number);
2380
2381static __initdata unsigned long dhash_entries;
2382static int __init set_dhash_entries(char *str)
2383{
2384	if (!str)
2385		return 0;
2386	dhash_entries = simple_strtoul(str, &str, 0);
2387	return 1;
2388}
2389__setup("dhash_entries=", set_dhash_entries);
2390
2391static void __init dcache_init_early(void)
2392{
2393	int loop;
2394
2395	/* If hashes are distributed across NUMA nodes, defer
2396	 * hash allocation until vmalloc space is available.
2397	 */
2398	if (hashdist)
2399		return;
2400
2401	dentry_hashtable =
2402		alloc_large_system_hash("Dentry cache",
2403					sizeof(struct hlist_head),
2404					dhash_entries,
2405					13,
2406					HASH_EARLY,
2407					&d_hash_shift,
2408					&d_hash_mask,
2409					0);
2410
2411	for (loop = 0; loop < (1 << d_hash_shift); loop++)
2412		INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2413}
2414
2415static void __init dcache_init(void)
2416{
2417	int loop;
2418
2419	/*
2420	 * A constructor could be added for stable state like the lists,
2421	 * but it is probably not worth it because of the cache nature
2422	 * of the dcache.
2423	 */
2424	dentry_cache = KMEM_CACHE(dentry,
2425		SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2426
2427	register_shrinker(&dcache_shrinker);
2428
2429	/* Hash may have been set up in dcache_init_early */
2430	if (!hashdist)
2431		return;
2432
2433	dentry_hashtable =
2434		alloc_large_system_hash("Dentry cache",
2435					sizeof(struct hlist_head),
2436					dhash_entries,
2437					13,
2438					0,
2439					&d_hash_shift,
2440					&d_hash_mask,
2441					0);
2442
2443	for (loop = 0; loop < (1 << d_hash_shift); loop++)
2444		INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2445}
2446
2447/* SLAB cache for __getname() consumers */
2448struct kmem_cache *names_cachep __read_mostly;
2449EXPORT_SYMBOL(names_cachep);
2450
2451EXPORT_SYMBOL(d_genocide);
2452
2453void __init vfs_caches_init_early(void)
2454{
2455	dcache_init_early();
2456	inode_init_early();
2457}
2458
2459void __init vfs_caches_init(unsigned long mempages)
2460{
2461	unsigned long reserve;
2462
2463	/* Base hash sizes on available memory, with a reserve equal to
2464           150% of current kernel size */
2465
2466	reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2467	mempages -= reserve;
2468
2469	names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2470			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2471
2472	dcache_init();
2473	inode_init();
2474	files_init(mempages);
2475	mnt_init();
2476	bdev_cache_init();
2477	chrdev_init();
2478}
2479