~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/dcache.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/config.h>
 18 #include <linux/string.h>
 19 #include <linux/mm.h>
 20 #include <linux/fs.h>
 21 #include <linux/malloc.h>
 22 #include <linux/slab.h>
 23 #include <linux/init.h>
 24 #include <linux/smp_lock.h>
 25 #include <linux/cache.h>
 26 
 27 #include <asm/uaccess.h>
 28 
 29 #define DCACHE_PARANOIA 1
 30 /* #define DCACHE_DEBUG 1 */
 31 
 32 spinlock_t dcache_lock = SPIN_LOCK_UNLOCKED;
 33 
 34 /* Right now the dcache depends on the kernel lock */
 35 #define check_lock()    if (!kernel_locked()) BUG()
 36 
 37 static kmem_cache_t *dentry_cache; 
 38 
 39 /*
 40  * This is the single most critical data structure when it comes
 41  * to the dcache: the hashtable for lookups. Somebody should try
 42  * to make this good - I've just made it work.
 43  *
 44  * This hash-function tries to avoid losing too many bits of hash
 45  * information, yet avoid using a prime hash-size or similar.
 46  */
 47 #define D_HASHBITS     d_hash_shift
 48 #define D_HASHMASK     d_hash_mask
 49 
 50 static unsigned int d_hash_mask;
 51 static unsigned int d_hash_shift;
 52 static struct list_head *dentry_hashtable;
 53 static LIST_HEAD(dentry_unused);
 54 
 55 struct {
 56         int nr_dentry;
 57         int nr_unused;
 58         int age_limit;          /* age in seconds */
 59         int want_pages;         /* pages requested by system */
 60         int dummy[2];
 61 } dentry_stat = {0, 0, 45, 0,};
 62 
 63 /* no dcache_lock, please */
 64 static inline void d_free(struct dentry *dentry)
 65 {
 66         if (dentry->d_op && dentry->d_op->d_release)
 67                 dentry->d_op->d_release(dentry);
 68         if (dname_external(dentry)) 
 69                 kfree(dentry->d_name.name);
 70         kmem_cache_free(dentry_cache, dentry); 
 71         dentry_stat.nr_dentry--;
 72 }
 73 
 74 /*
 75  * Release the dentry's inode, using the fileystem
 76  * d_iput() operation if defined.
 77  * Called with dcache_lock held, drops it.
 78  */
 79 static inline void dentry_iput(struct dentry * dentry)
 80 {
 81         struct inode *inode = dentry->d_inode;
 82         if (inode) {
 83                 dentry->d_inode = NULL;
 84                 list_del_init(&dentry->d_alias);
 85                 spin_unlock(&dcache_lock);
 86                 if (dentry->d_op && dentry->d_op->d_iput)
 87                         dentry->d_op->d_iput(dentry, inode);
 88                 else
 89                         iput(inode);
 90         } else
 91                 spin_unlock(&dcache_lock);
 92 }
 93 
 94 /* 
 95  * This is dput
 96  *
 97  * This is complicated by the fact that we do not want to put
 98  * dentries that are no longer on any hash chain on the unused
 99  * list: we'd much rather just get rid of them immediately.
100  *
101  * However, that implies that we have to traverse the dentry
102  * tree upwards to the parents which might _also_ now be
103  * scheduled for deletion (it may have been only waiting for
104  * its last child to go away).
105  *
106  * This tail recursion is done by hand as we don't want to depend
107  * on the compiler to always get this right (gcc generally doesn't).
108  * Real recursion would eat up our stack space.
109  */
110 
111 /*
112  * dput - release a dentry
113  * @dentry: dentry to release 
114  *
115  * Release a dentry. This will drop the usage count and if appropriate
116  * call the dentry unlink method as well as removing it from the queues and
117  * releasing its resources. If the parent dentries were scheduled for release
118  * they too may now get deleted.
119  *
120  * no dcache lock, please.
121  */
122 
123 void dput(struct dentry *dentry)
124 {
125         if (!dentry)
126                 return;
127 
128 repeat:
129         if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
130                 return;
131 
132         /* dput on a free dentry? */
133         if (!list_empty(&dentry->d_lru))
134                 BUG();
135         /*
136          * AV: ->d_delete() is _NOT_ allowed to block now.
137          */
138         if (dentry->d_op && dentry->d_op->d_delete) {
139                 if (dentry->d_op->d_delete(dentry))
140                         goto unhash_it;
141         }
142         /* Unreachable? Get rid of it */
143         if (list_empty(&dentry->d_hash))
144                 goto kill_it;
145         list_add(&dentry->d_lru, &dentry_unused);
146         dentry_stat.nr_unused++;
147         /*
148          * Update the timestamp
149          */
150         dentry->d_reftime = jiffies;
151         spin_unlock(&dcache_lock);
152         return;
153 
154 unhash_it:
155         list_del_init(&dentry->d_hash);
156 
157 kill_it: {
158                 struct dentry *parent;
159                 list_del(&dentry->d_child);
160                 /* drops the lock, at that point nobody can reach this dentry */
161                 dentry_iput(dentry);
162                 parent = dentry->d_parent;
163                 d_free(dentry);
164                 if (dentry == parent)
165                         return;
166                 dentry = parent;
167                 goto repeat;
168         }
169 }
170 
171 /**
172  * d_invalidate - invalidate a dentry
173  * @dentry: dentry to invalidate
174  *
175  * Try to invalidate the dentry if it turns out to be
176  * possible. If there are other dentries that can be
177  * reached through this one we can't delete it and we
178  * return -EBUSY. On success we return 0.
179  *
180  * no dcache lock.
181  */
182  
183 int d_invalidate(struct dentry * dentry)
184 {
185         /*
186          * If it's already been dropped, return OK.
187          */
188         spin_lock(&dcache_lock);
189         if (list_empty(&dentry->d_hash)) {
190                 spin_unlock(&dcache_lock);
191                 return 0;
192         }
193         /*
194          * Check whether to do a partial shrink_dcache
195          * to get rid of unused child entries.
196          */
197         if (!list_empty(&dentry->d_subdirs)) {
198                 spin_unlock(&dcache_lock);
199                 shrink_dcache_parent(dentry);
200                 spin_lock(&dcache_lock);
201         }
202 
203         /*
204          * Somebody else still using it?
205          *
206          * If it's a directory, we can't drop it
207          * for fear of somebody re-populating it
208          * with children (even though dropping it
209          * would make it unreachable from the root,
210          * we might still populate it if it was a
211          * working directory or similar).
212          */
213         if (atomic_read(&dentry->d_count) > 1) {
214                 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
215                         spin_unlock(&dcache_lock);
216                         return -EBUSY;
217                 }
218         }
219 
220         list_del_init(&dentry->d_hash);
221         spin_unlock(&dcache_lock);
222         return 0;
223 }
224 
225 /* This should be called _only_ with dcache_lock held */
226 
227 static inline struct dentry * __dget_locked(struct dentry *dentry)
228 {
229         atomic_inc(&dentry->d_count);
230         if (atomic_read(&dentry->d_count) == 1) {
231                 dentry_stat.nr_unused--;
232                 list_del(&dentry->d_lru);
233                 INIT_LIST_HEAD(&dentry->d_lru);         /* make "list_empty()" work */
234         }
235         return dentry;
236 }
237 
238 struct dentry * dget_locked(struct dentry *dentry)
239 {
240         return __dget_locked(dentry);
241 }
242 
243 /**
244  * d_find_alias - grab a hashed alias of inode
245  * @inode: inode in question
246  *
247  * If inode has a hashed alias - acquire the reference to alias and
248  * return it. Otherwise return NULL. Notice that if inode is a directory
249  * there can be only one alias and it can be unhashed only if it has
250  * no children.
251  */
252 
253 struct dentry * d_find_alias(struct inode *inode)
254 {
255         struct list_head *head, *next, *tmp;
256         struct dentry *alias;
257 
258         spin_lock(&dcache_lock);
259         head = &inode->i_dentry;
260         next = inode->i_dentry.next;
261         while (next != head) {
262                 tmp = next;
263                 next = tmp->next;
264                 alias = list_entry(tmp, struct dentry, d_alias);
265                 if (!list_empty(&alias->d_hash)) {
266                         __dget_locked(alias);
267                         spin_unlock(&dcache_lock);
268                         return alias;
269                 }
270         }
271         spin_unlock(&dcache_lock);
272         return NULL;
273 }
274 
275 /*
276  *      Try to kill dentries associated with this inode.
277  * WARNING: you must own a reference to inode.
278  */
279 void d_prune_aliases(struct inode *inode)
280 {
281         struct list_head *tmp, *head = &inode->i_dentry;
282 restart:
283         spin_lock(&dcache_lock);
284         tmp = head;
285         while ((tmp = tmp->next) != head) {
286                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
287                 if (!atomic_read(&dentry->d_count)) {
288                         __dget_locked(dentry);
289                         spin_unlock(&dcache_lock);
290                         d_drop(dentry);
291                         dput(dentry);
292                         goto restart;
293                 }
294         }
295         spin_unlock(&dcache_lock);
296 }
297 
298 /*
299  * Throw away a dentry - free the inode, dput the parent.
300  * This requires that the LRU list has already been
301  * removed.
302  * Called with dcache_lock, drops it and then regains.
303  */
304 static inline void prune_one_dentry(struct dentry * dentry)
305 {
306         struct dentry * parent;
307 
308         list_del_init(&dentry->d_hash);
309         list_del(&dentry->d_child);
310         dentry_iput(dentry);
311         parent = dentry->d_parent;
312         d_free(dentry);
313         if (parent != dentry)
314                 dput(parent);
315         spin_lock(&dcache_lock);
316 }
317 
318 /**
319  * prune_dcache - shrink the dcache
320  * @count: number of entries to try and free
321  *
322  * Shrink the dcache. This is done when we need
323  * more memory, or simply when we need to unmount
324  * something (at which point we need to unuse
325  * all dentries).
326  *
327  * This function may fail to free any resources if
328  * all the dentries are in use.
329  */
330  
331 void prune_dcache(int count)
332 {
333         spin_lock(&dcache_lock);
334         for (;;) {
335                 struct dentry *dentry;
336                 struct list_head *tmp;
337 
338                 tmp = dentry_unused.prev;
339 
340                 if (tmp == &dentry_unused)
341                         break;
342                 list_del_init(tmp);
343                 dentry = list_entry(tmp, struct dentry, d_lru);
344 
345                 /* If the dentry was recently referenced, don't free it. */
346                 if (dentry->d_flags & DCACHE_REFERENCED) {
347                         dentry->d_flags &= ~DCACHE_REFERENCED;
348                         list_add(&dentry->d_lru, &dentry_unused);
349                         count--;
350                         continue;
351                 }
352                 dentry_stat.nr_unused--;
353 
354                 /* Unused dentry with a count? */
355                 if (atomic_read(&dentry->d_count))
356                         BUG();
357 
358                 prune_one_dentry(dentry);
359                 if (!--count)
360                         break;
361         }
362         spin_unlock(&dcache_lock);
363 }
364 
365 /*
366  * Shrink the dcache for the specified super block.
367  * This allows us to unmount a device without disturbing
368  * the dcache for the other devices.
369  *
370  * This implementation makes just two traversals of the
371  * unused list.  On the first pass we move the selected
372  * dentries to the most recent end, and on the second
373  * pass we free them.  The second pass must restart after
374  * each dput(), but since the target dentries are all at
375  * the end, it's really just a single traversal.
376  */
377 
378 /**
379  * shrink_dcache_sb - shrink dcache for a superblock
380  * @sb: superblock
381  *
382  * Shrink the dcache for the specified super block. This
383  * is used to free the dcache before unmounting a file
384  * system
385  */
386 
387 void shrink_dcache_sb(struct super_block * sb)
388 {
389         struct list_head *tmp, *next;
390         struct dentry *dentry;
391 
392         /*
393          * Pass one ... move the dentries for the specified
394          * superblock to the most recent end of the unused list.
395          */
396         spin_lock(&dcache_lock);
397         next = dentry_unused.next;
398         while (next != &dentry_unused) {
399                 tmp = next;
400                 next = tmp->next;
401                 dentry = list_entry(tmp, struct dentry, d_lru);
402                 if (dentry->d_sb != sb)
403                         continue;
404                 list_del(tmp);
405                 list_add(tmp, &dentry_unused);
406         }
407 
408         /*
409          * Pass two ... free the dentries for this superblock.
410          */
411 repeat:
412         next = dentry_unused.next;
413         while (next != &dentry_unused) {
414                 tmp = next;
415                 next = tmp->next;
416                 dentry = list_entry(tmp, struct dentry, d_lru);
417                 if (dentry->d_sb != sb)
418                         continue;
419                 if (atomic_read(&dentry->d_count))
420                         continue;
421                 dentry_stat.nr_unused--;
422                 list_del(tmp);
423                 INIT_LIST_HEAD(tmp);
424                 prune_one_dentry(dentry);
425                 goto repeat;
426         }
427         spin_unlock(&dcache_lock);
428 }
429 
430 /*
431  * Search for at least 1 mount point in the dentry's subdirs.
432  * We descend to the next level whenever the d_subdirs
433  * list is non-empty and continue searching.
434  */
435  
436 /**
437  * have_submounts - check for mounts over a dentry
438  * @parent: dentry to check.
439  *
440  * Return true if the parent or its subdirectories contain
441  * a mount point
442  */
443  
444 int have_submounts(struct dentry *parent)
445 {
446         struct dentry *this_parent = parent;
447         struct list_head *next;
448 
449         spin_lock(&dcache_lock);
450         if (d_mountpoint(parent))
451                 goto positive;
452 repeat:
453         next = this_parent->d_subdirs.next;
454 resume:
455         while (next != &this_parent->d_subdirs) {
456                 struct list_head *tmp = next;
457                 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
458                 next = tmp->next;
459                 /* Have we found a mount point ? */
460                 if (d_mountpoint(dentry))
461                         goto positive;
462                 if (!list_empty(&dentry->d_subdirs)) {
463                         this_parent = dentry;
464                         goto repeat;
465                 }
466         }
467         /*
468          * All done at this level ... ascend and resume the search.
469          */
470         if (this_parent != parent) {
471                 next = this_parent->d_child.next; 
472                 this_parent = this_parent->d_parent;
473                 goto resume;
474         }
475         spin_unlock(&dcache_lock);
476         return 0; /* No mount points found in tree */
477 positive:
478         spin_unlock(&dcache_lock);
479         return 1;
480 }
481 
482 /*
483  * Search the dentry child list for the specified parent,
484  * and move any unused dentries to the end of the unused
485  * list for prune_dcache(). We descend to the next level
486  * whenever the d_subdirs list is non-empty and continue
487  * searching.
488  */
489 static int select_parent(struct dentry * parent)
490 {
491         struct dentry *this_parent = parent;
492         struct list_head *next;
493         int found = 0;
494 
495         spin_lock(&dcache_lock);
496 repeat:
497         next = this_parent->d_subdirs.next;
498 resume:
499         while (next != &this_parent->d_subdirs) {
500                 struct list_head *tmp = next;
501                 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
502                 next = tmp->next;
503                 if (!atomic_read(&dentry->d_count)) {
504                         list_del(&dentry->d_lru);
505                         list_add(&dentry->d_lru, dentry_unused.prev);
506                         found++;
507                 }
508                 /*
509                  * Descend a level if the d_subdirs list is non-empty.
510                  */
511                 if (!list_empty(&dentry->d_subdirs)) {
512                         this_parent = dentry;
513 #ifdef DCACHE_DEBUG
514 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
515 dentry->d_parent->d_name.name, dentry->d_name.name, found);
516 #endif
517                         goto repeat;
518                 }
519         }
520         /*
521          * All done at this level ... ascend and resume the search.
522          */
523         if (this_parent != parent) {
524                 next = this_parent->d_child.next; 
525                 this_parent = this_parent->d_parent;
526 #ifdef DCACHE_DEBUG
527 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
528 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
529 #endif
530                 goto resume;
531         }
532         spin_unlock(&dcache_lock);
533         return found;
534 }
535 
536 /**
537  * shrink_dcache_parent - prune dcache
538  * @parent: parent of entries to prune
539  *
540  * Prune the dcache to remove unused children of the parent dentry.
541  */
542  
543 void shrink_dcache_parent(struct dentry * parent)
544 {
545         int found;
546 
547         while ((found = select_parent(parent)) != 0)
548                 prune_dcache(found);
549 }
550 
551 /*
552  * This is called from kswapd when we think we need some
553  * more memory, but aren't really sure how much. So we
554  * carefully try to free a _bit_ of our dcache, but not
555  * too much.
556  *
557  * Priority:
558  *   0 - very urgent: shrink everything
559  *  ...
560  *   6 - base-level: try to shrink a bit.
561  */
562 void shrink_dcache_memory(int priority, unsigned int gfp_mask)
563 {
564         int count = 0;
565 
566         /*
567          * Nasty deadlock avoidance.
568          *
569          * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
570          * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->
571          * put_inode->ext2_discard_prealloc->ext2_free_blocks->lock_super->
572          * DEADLOCK.
573          *
574          * We should make sure we don't hold the superblock lock over
575          * block allocations, but for now:
576          */
577         if (!(gfp_mask & __GFP_IO))
578                 return;
579 
580         if (priority)
581                 count = dentry_stat.nr_unused / priority;
582 
583         prune_dcache(count);
584         kmem_cache_shrink(dentry_cache);
585 }
586 
587 #define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
588 
589 /**
590  * d_alloc      -       allocate a dcache entry
591  * @parent: parent of entry to allocate
592  * @name: qstr of the name
593  *
594  * Allocates a dentry. It returns %NULL if there is insufficient memory
595  * available. On a success the dentry is returned. The name passed in is
596  * copied and the copy passed in may be reused after this call.
597  */
598  
599 struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
600 {
601         char * str;
602         struct dentry *dentry;
603 
604         dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); 
605         if (!dentry)
606                 return NULL;
607 
608         if (name->len > DNAME_INLINE_LEN-1) {
609                 str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
610                 if (!str) {
611                         kmem_cache_free(dentry_cache, dentry); 
612                         return NULL;
613                 }
614         } else
615                 str = dentry->d_iname; 
616 
617         memcpy(str, name->name, name->len);
618         str[name->len] = 0;
619 
620         atomic_set(&dentry->d_count, 1);
621         dentry->d_flags = 0;
622         dentry->d_inode = NULL;
623         dentry->d_parent = NULL;
624         dentry->d_sb = NULL;
625         dentry->d_name.name = str;
626         dentry->d_name.len = name->len;
627         dentry->d_name.hash = name->hash;
628         dentry->d_op = NULL;
629         dentry->d_fsdata = NULL;
630         INIT_LIST_HEAD(&dentry->d_vfsmnt);
631         INIT_LIST_HEAD(&dentry->d_hash);
632         INIT_LIST_HEAD(&dentry->d_lru);
633         INIT_LIST_HEAD(&dentry->d_subdirs);
634         INIT_LIST_HEAD(&dentry->d_alias);
635         if (parent) {
636                 dentry->d_parent = dget(parent);
637                 dentry->d_sb = parent->d_sb;
638                 spin_lock(&dcache_lock);
639                 list_add(&dentry->d_child, &parent->d_subdirs);
640                 spin_unlock(&dcache_lock);
641         } else
642                 INIT_LIST_HEAD(&dentry->d_child);
643 
644         dentry_stat.nr_dentry++;
645         return dentry;
646 }
647 
648 /**
649  * d_instantiate - fill in inode information for a dentry
650  * @entry: dentry to complete
651  * @inode: inode to attach to this dentry
652  *
653  * Fill in inode information in the entry.
654  *
655  * This turns negative dentries into productive full members
656  * of society.
657  *
658  * NOTE! This assumes that the inode count has been incremented
659  * (or otherwise set) by the caller to indicate that it is now
660  * in use by the dcache.
661  */
662  
663 void d_instantiate(struct dentry *entry, struct inode * inode)
664 {
665         spin_lock(&dcache_lock);
666         if (inode)
667                 list_add(&entry->d_alias, &inode->i_dentry);
668         entry->d_inode = inode;
669         spin_unlock(&dcache_lock);
670 }
671 
672 /**
673  * d_alloc_root - allocate root dentry
674  * @root_inode: inode to allocate the root for
675  *
676  * Allocate a root ("/") dentry for the inode given. The inode is
677  * instantiated and returned. %NULL is returned if there is insufficient
678  * memory or the inode passed is %NULL.
679  */
680  
681 struct dentry * d_alloc_root(struct inode * root_inode)
682 {
683         struct dentry *res = NULL;
684 
685         if (root_inode) {
686                 res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
687                 if (res) {
688                         res->d_sb = root_inode->i_sb;
689                         res->d_parent = res;
690                         d_instantiate(res, root_inode);
691                 }
692         }
693         return res;
694 }
695 
696 static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
697 {
698         hash += (unsigned long) parent / L1_CACHE_BYTES;
699         hash = hash ^ (hash >> D_HASHBITS) ^ (hash >> D_HASHBITS*2);
700         return dentry_hashtable + (hash & D_HASHMASK);
701 }
702 
703 /**
704  * d_lookup - search for a dentry
705  * @parent: parent dentry
706  * @name: qstr of name we wish to find
707  *
708  * Searches the children of the parent dentry for the name in question. If
709  * the dentry is found its reference count is incremented and the dentry
710  * is returned. The caller must use d_put to free the entry when it has
711  * finished using it. %NULL is returned on failure.
712  */
713  
714 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
715 {
716         unsigned int len = name->len;
717         unsigned int hash = name->hash;
718         const unsigned char *str = name->name;
719         struct list_head *head = d_hash(parent,hash);
720         struct list_head *tmp;
721 
722         spin_lock(&dcache_lock);
723         tmp = head->next;
724         for (;;) {
725                 struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
726                 if (tmp == head)
727                         break;
728                 tmp = tmp->next;
729                 if (dentry->d_name.hash != hash)
730                         continue;
731                 if (dentry->d_parent != parent)
732                         continue;
733                 if (parent->d_op && parent->d_op->d_compare) {
734                         if (parent->d_op->d_compare(parent, &dentry->d_name, name))
735                                 continue;
736                 } else {
737                         if (dentry->d_name.len != len)
738                                 continue;
739                         if (memcmp(dentry->d_name.name, str, len))
740                                 continue;
741                 }
742                 __dget_locked(dentry);
743                 dentry->d_flags |= DCACHE_REFERENCED;
744                 spin_unlock(&dcache_lock);
745                 return dentry;
746         }
747         spin_unlock(&dcache_lock);
748         return NULL;
749 }
750 
751 /**
752  * d_validate - verify dentry provided from insecure source
753  * @dentry: The dentry alleged to be valid
754  * @dparent: The parent dentry
755  * @hash: Hash of the dentry
756  * @len: Length of the name
757  *
758  * An insecure source has sent us a dentry, here we verify it and dget() it.
759  * This is used by ncpfs in its readdir implementation.
760  * Zero is returned in the dentry is invalid.
761  *
762  * NOTE: This function does _not_ dereference the pointers before we have
763  * validated them. We can test the pointer values, but we
764  * must not actually use them until we have found a valid
765  * copy of the pointer in kernel space..
766  */
767  
768 int d_validate(struct dentry *dentry, struct dentry *dparent,
769                unsigned int hash, unsigned int len)
770 {
771         struct list_head *base, *lhp;
772         int valid = 1;
773 
774         spin_lock(&dcache_lock);
775         if (dentry != dparent) {
776                 base = d_hash(dparent, hash);
777                 lhp = base;
778                 while ((lhp = lhp->next) != base) {
779                         if (dentry == list_entry(lhp, struct dentry, d_hash)) {
780                                 __dget_locked(dentry);
781                                 goto out;
782                         }
783                 }
784         } else {
785                 /*
786                  * Special case: local mount points don't live in
787                  * the hashes, so we search the super blocks.
788                  */
789                 struct super_block *sb = sb_entry(super_blocks.next);
790 
791                 for (; sb != sb_entry(&super_blocks); 
792                      sb = sb_entry(sb->s_list.next)) {
793                         if (!sb->s_dev)
794                                 continue;
795                         if (sb->s_root == dentry) {
796                                 __dget_locked(dentry);
797                                 goto out;
798                         }
799                 }
800         }
801         valid = 0;
802 out:
803         spin_unlock(&dcache_lock);
804         return valid;
805 }
806 
807 /*
808  * When a file is deleted, we have two options:
809  * - turn this dentry into a negative dentry
810  * - unhash this dentry and free it.
811  *
812  * Usually, we want to just turn this into
813  * a negative dentry, but if anybody else is
814  * currently using the dentry or the inode
815  * we can't do that and we fall back on removing
816  * it from the hash queues and waiting for
817  * it to be deleted later when it has no users
818  */
819  
820 /**
821  * d_delete - delete a dentry
822  * @dentry: The dentry to delete
823  *
824  * Turn the dentry into a negative dentry if possible, otherwise
825  * remove it from the hash queues so it can be deleted later
826  */
827  
828 void d_delete(struct dentry * dentry)
829 {
830         /*
831          * Are we the only user?
832          */
833         spin_lock(&dcache_lock);
834         if (atomic_read(&dentry->d_count) == 1) {
835                 dentry_iput(dentry);
836                 return;
837         }
838         spin_unlock(&dcache_lock);
839 
840         /*
841          * If not, just drop the dentry and let dput
842          * pick up the tab..
843          */
844         d_drop(dentry);
845 }
846 
847 /**
848  * d_rehash     - add an entry back to the hash
849  * @entry: dentry to add to the hash
850  *
851  * Adds a dentry to the hash according to its name.
852  */
853  
854 void d_rehash(struct dentry * entry)
855 {
856         struct list_head *list = d_hash(entry->d_parent, entry->d_name.hash);
857         spin_lock(&dcache_lock);
858         list_add(&entry->d_hash, list);
859         spin_unlock(&dcache_lock);
860 }
861 
862 #define do_switch(x,y) do { \
863         __typeof__ (x) __tmp = x; \
864         x = y; y = __tmp; } while (0)
865 
866 /*
867  * When switching names, the actual string doesn't strictly have to
868  * be preserved in the target - because we're dropping the target
869  * anyway. As such, we can just do a simple memcpy() to copy over
870  * the new name before we switch.
871  *
872  * Note that we have to be a lot more careful about getting the hash
873  * switched - we have to switch the hash value properly even if it
874  * then no longer matches the actual (corrupted) string of the target.
875  * The hash value has to match the hash queue that the dentry is on..
876  */
877 static inline void switch_names(struct dentry * dentry, struct dentry * target)
878 {
879         const unsigned char *old_name, *new_name;
880 
881         check_lock();
882         memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN); 
883         old_name = target->d_name.name;
884         new_name = dentry->d_name.name;
885         if (old_name == target->d_iname)
886                 old_name = dentry->d_iname;
887         if (new_name == dentry->d_iname)
888                 new_name = target->d_iname;
889         target->d_name.name = new_name;
890         dentry->d_name.name = old_name;
891 }
892 
893 /*
894  * We cannibalize "target" when moving dentry on top of it,
895  * because it's going to be thrown away anyway. We could be more
896  * polite about it, though.
897  *
898  * This forceful removal will result in ugly /proc output if
899  * somebody holds a file open that got deleted due to a rename.
900  * We could be nicer about the deleted file, and let it show
901  * up under the name it got deleted rather than the name that
902  * deleted it.
903  *
904  * Careful with the hash switch. The hash switch depends on
905  * the fact that any list-entry can be a head of the list.
906  * Think about it.
907  */
908  
909 /**
910  * d_move - move a dentry
911  * @dentry: entry to move
912  * @target: new dentry
913  *
914  * Update the dcache to reflect the move of a file name. Negative
915  * dcache entries should not be moved in this way.
916  */
917   
918 void d_move(struct dentry * dentry, struct dentry * target)
919 {
920         check_lock();
921 
922         if (!dentry->d_inode)
923                 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
924 
925         spin_lock(&dcache_lock);
926         /* Move the dentry to the target hash queue */
927         list_del(&dentry->d_hash);
928         list_add(&dentry->d_hash, &target->d_hash);
929 
930         /* Unhash the target: dput() will then get rid of it */
931         list_del(&target->d_hash);
932         INIT_LIST_HEAD(&target->d_hash);
933 
934         list_del(&dentry->d_child);
935         list_del(&target->d_child);
936 
937         /* Switch the parents and the names.. */
938         switch_names(dentry, target);
939         do_switch(dentry->d_parent, target->d_parent);
940         do_switch(dentry->d_name.len, target->d_name.len);
941         do_switch(dentry->d_name.hash, target->d_name.hash);
942 
943         /* And add them back to the (new) parent lists */
944         list_add(&target->d_child, &target->d_parent->d_subdirs);
945         list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
946         spin_unlock(&dcache_lock);
947 }
948 
949 /**
950  * d_path - return the path of a dentry
951  * @dentry: dentry to report
952  * @vfsmnt: vfsmnt to which the dentry belongs
953  * @root: root dentry
954  * @rootmnt: vfsmnt to which the root dentry belongs
955  * @buffer: buffer to return value in
956  * @buflen: buffer length
957  *
958  * Convert a dentry into an ASCII path name. If the entry has been deleted
959  * the string " (deleted)" is appended. Note that this is ambiguous. Returns
960  * the buffer.
961  *
962  * "buflen" should be %PAGE_SIZE or more. Caller holds the dcache_lock.
963  */
964 char * __d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
965                 struct dentry *root, struct vfsmount *rootmnt,
966                 char *buffer, int buflen)
967 {
968         char * end = buffer+buflen;
969         char * retval;
970         int namelen;
971 
972         *--end = '\0';
973         buflen--;
974         if (!IS_ROOT(dentry) && list_empty(&dentry->d_hash)) {
975                 buflen -= 10;
976                 end -= 10;
977                 memcpy(end, " (deleted)", 10);
978         }
979 
980         /* Get '/' right */
981         retval = end-1;
982         *retval = '/';
983 
984         for (;;) {
985                 struct dentry * parent;
986 
987                 if (dentry == root && vfsmnt == rootmnt)
988                         break;
989                 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
990                         /* Global root? */
991                         if (vfsmnt->mnt_parent == vfsmnt)
992                                 goto global_root;
993                         dentry = vfsmnt->mnt_mountpoint;
994                         vfsmnt = vfsmnt->mnt_parent;
995                         continue;
996                 }
997                 parent = dentry->d_parent;
998                 namelen = dentry->d_name.len;
999                 buflen -= namelen + 1;
1000                 if (buflen < 0)
1001                         break;
1002                 end -= namelen;
1003                 memcpy(end, dentry->d_name.name, namelen);
1004                 *--end = '/';
1005                 retval = end;
1006                 dentry = parent;
1007         }
1008         return retval;
1009 global_root:
1010         namelen = dentry->d_name.len;
1011         buflen -= namelen;
1012         if (buflen >= 0) {
1013                 retval -= namelen-1;    /* hit the slash */
1014                 memcpy(retval, dentry->d_name.name, namelen);
1015         }
1016         return retval;
1017 }
1018 
1019 /*
1020  * NOTE! The user-level library version returns a
1021  * character pointer. The kernel system call just
1022  * returns the length of the buffer filled (which
1023  * includes the ending '\0' character), or a negative
1024  * error value. So libc would do something like
1025  *
1026  *      char *getcwd(char * buf, size_t size)
1027  *      {
1028  *              int retval;
1029  *
1030  *              retval = sys_getcwd(buf, size);
1031  *              if (retval >= 0)
1032  *                      return buf;
1033  *              errno = -retval;
1034  *              return NULL;
1035  *      }
1036  */
1037 asmlinkage long sys_getcwd(char *buf, unsigned long size)
1038 {
1039         int error;
1040         struct vfsmount *pwdmnt, *rootmnt;
1041         struct dentry *pwd, *root;
1042         char *page = (char *) __get_free_page(GFP_USER);
1043 
1044         if (!page)
1045                 return -ENOMEM;
1046 
1047         read_lock(&current->fs->lock);
1048         pwdmnt = mntget(current->fs->pwdmnt);
1049         pwd = dget(current->fs->pwd);
1050         rootmnt = mntget(current->fs->rootmnt);
1051         root = dget(current->fs->root);
1052         read_unlock(&current->fs->lock);
1053 
1054         error = -ENOENT;
1055         /* Has the current directory has been unlinked? */
1056         spin_lock(&dcache_lock);
1057         if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
1058                 unsigned long len;
1059                 char * cwd;
1060 
1061                 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1062                 spin_unlock(&dcache_lock);
1063 
1064                 error = -ERANGE;
1065                 len = PAGE_SIZE + page - cwd;
1066                 if (len <= size) {
1067                         error = len;
1068                         if (copy_to_user(buf, cwd, len))
1069                                 error = -EFAULT;
1070                 }
1071         } else
1072                 spin_unlock(&dcache_lock);
1073         dput(pwd);
1074         mntput(pwdmnt);
1075         dput(root);
1076         mntput(rootmnt);
1077         free_page((unsigned long) page);
1078         return error;
1079 }
1080 
1081 /*
1082  * Test whether new_dentry is a subdirectory of old_dentry.
1083  *
1084  * Trivially implemented using the dcache structure
1085  */
1086 
1087 /**
1088  * is_subdir - is new dentry a subdirectory of old_dentry
1089  * @new_dentry: new dentry
1090  * @old_dentry: old dentry
1091  *
1092  * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1093  * Returns 0 otherwise.
1094  */
1095   
1096 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1097 {
1098         int result;
1099 
1100         result = 0;
1101         for (;;) {
1102                 if (new_dentry != old_dentry) {
1103                         struct dentry * parent = new_dentry->d_parent;
1104                         if (parent == new_dentry)
1105                                 break;
1106                         new_dentry = parent;
1107                         continue;
1108                 }
1109                 result = 1;
1110                 break;
1111         }
1112         return result;
1113 }
1114 
1115 void d_genocide(struct dentry *root)
1116 {
1117         struct dentry *this_parent = root;
1118         struct list_head *next;
1119 
1120         spin_lock(&dcache_lock);
1121 repeat:
1122         next = this_parent->d_subdirs.next;
1123 resume:
1124         while (next != &this_parent->d_subdirs) {
1125                 struct list_head *tmp = next;
1126                 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1127                 next = tmp->next;
1128                 if (d_unhashed(dentry)||!dentry->d_inode)
1129                         continue;
1130                 if (!list_empty(&dentry->d_subdirs)) {
1131                         this_parent = dentry;
1132                         goto repeat;
1133                 }
1134                 atomic_dec(&dentry->d_count);
1135         }
1136         if (this_parent != root) {
1137                 next = this_parent->d_child.next; 
1138                 atomic_dec(&this_parent->d_count);
1139                 this_parent = this_parent->d_parent;
1140                 goto resume;
1141         }
1142         spin_unlock(&dcache_lock);
1143 }
1144 
1145 /**
1146  * find_inode_number - check for dentry with name
1147  * @dir: directory to check
1148  * @name: Name to find.
1149  *
1150  * Check whether a dentry already exists for the given name,
1151  * and return the inode number if it has an inode. Otherwise
1152  * 0 is returned.
1153  *
1154  * This routine is used to post-process directory listings for
1155  * filesystems using synthetic inode numbers, and is necessary
1156  * to keep getcwd() working.
1157  */
1158  
1159 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1160 {
1161         struct dentry * dentry;
1162         ino_t ino = 0;
1163 
1164         /*
1165          * Check for a fs-specific hash function. Note that we must
1166          * calculate the standard hash first, as the d_op->d_hash()
1167          * routine may choose to leave the hash value unchanged.
1168          */
1169         name->hash = full_name_hash(name->name, name->len);
1170         if (dir->d_op && dir->d_op->d_hash)
1171         {
1172                 if (dir->d_op->d_hash(dir, name) != 0)
1173                         goto out;
1174         }
1175 
1176         dentry = d_lookup(dir, name);
1177         if (dentry)
1178         {
1179                 if (dentry->d_inode)
1180                         ino = dentry->d_inode->i_ino;
1181                 dput(dentry);
1182         }
1183 out:
1184         return ino;
1185 }
1186 
1187 static void __init dcache_init(unsigned long mempages)
1188 {
1189         struct list_head *d;
1190         unsigned long order;
1191         unsigned int nr_hash;
1192         int i;
1193 
1194         /* 
1195          * A constructor could be added for stable state like the lists,
1196          * but it is probably not worth it because of the cache nature
1197          * of the dcache. 
1198          * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
1199          * flag could be removed here, to hint to the allocator that
1200          * it should not try to get multiple page regions.  
1201          */
1202         dentry_cache = kmem_cache_create("dentry_cache",
1203                                          sizeof(struct dentry),
1204                                          0,
1205                                          SLAB_HWCACHE_ALIGN,
1206                                          NULL, NULL);
1207         if (!dentry_cache)
1208                 panic("Cannot create dentry cache");
1209 
1210 #if PAGE_SHIFT < 13
1211         mempages >>= (13 - PAGE_SHIFT);
1212 #endif
1213         mempages *= sizeof(struct list_head);
1214         for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1215                 ;
1216 
1217         do {
1218                 unsigned long tmp;
1219 
1220                 nr_hash = (1UL << order) * PAGE_SIZE /
1221                         sizeof(struct list_head);
1222                 d_hash_mask = (nr_hash - 1);
1223 
1224                 tmp = nr_hash;
1225                 d_hash_shift = 0;
1226                 while ((tmp >>= 1UL) != 0UL)
1227                         d_hash_shift++;
1228 
1229                 dentry_hashtable = (struct list_head *)
1230                         __get_free_pages(GFP_ATOMIC, order);
1231         } while (dentry_hashtable == NULL && --order >= 0);
1232 
1233         printk("Dentry-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1234                         nr_hash, order, (PAGE_SIZE << order));
1235 
1236         if (!dentry_hashtable)
1237                 panic("Failed to allocate dcache hash table\n");
1238 
1239         d = dentry_hashtable;
1240         i = nr_hash;
1241         do {
1242                 INIT_LIST_HEAD(d);
1243                 d++;
1244                 i--;
1245         } while (i);
1246 }
1247 
1248 /* SLAB cache for __getname() consumers */
1249 kmem_cache_t *names_cachep;
1250 
1251 /* SLAB cache for file structures */
1252 kmem_cache_t *filp_cachep;
1253 
1254 /* SLAB cache for dquot structures */
1255 kmem_cache_t *dquot_cachep;
1256 
1257 /* SLAB cache for buffer_head structures */
1258 kmem_cache_t *bh_cachep;
1259 
1260 void __init vfs_caches_init(unsigned long mempages)
1261 {
1262         bh_cachep = kmem_cache_create("buffer_head",
1263                         sizeof(struct buffer_head), 0,
1264                         SLAB_HWCACHE_ALIGN, NULL, NULL);
1265         if(!bh_cachep)
1266                 panic("Cannot create buffer head SLAB cache");
1267 
1268         names_cachep = kmem_cache_create("names_cache", 
1269                         PATH_MAX + 1, 0, 
1270                         SLAB_HWCACHE_ALIGN, NULL, NULL);
1271         if (!names_cachep)
1272                 panic("Cannot create names SLAB cache");
1273 
1274         filp_cachep = kmem_cache_create("filp", 
1275                         sizeof(struct file), 0,
1276                         SLAB_HWCACHE_ALIGN, NULL, NULL);
1277         if(!filp_cachep)
1278                 panic("Cannot create filp SLAB cache");
1279 
1280 #if defined (CONFIG_QUOTA)
1281         dquot_cachep = kmem_cache_create("dquot", 
1282                         sizeof(struct dquot), sizeof(unsigned long) * 4,
1283                         SLAB_HWCACHE_ALIGN, NULL, NULL);
1284         if (!dquot_cachep)
1285                 panic("Cannot create dquot SLAB cache");
1286 #endif
1287 
1288         dcache_init(mempages);
1289 }
1290 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.