dcache.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  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. * Notes on the allocation strategy:
  10. *
  11. * The dcache is a master of the icache - whenever a dcache entry
  12. * exists, the inode will always exist. "iput()" is done either when
  13. * the dcache entry is deleted or garbage collected.
  14. */
  15. #include <linux/syscalls.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/fs.h>
  19. #include <linux/fsnotify.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/hash.h>
  24. #include <linux/cache.h>
  25. #include <linux/module.h>
  26. #include <linux/mount.h>
  27. #include <linux/file.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/security.h>
  30. #include <linux/seqlock.h>
  31. #include <linux/swap.h>
  32. #include <linux/bootmem.h>
  33. #include "internal.h"
  34. int sysctl_vfs_cache_pressure __read_mostly = 100;
  35. EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
  36. __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
  37. static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
  38. EXPORT_SYMBOL(dcache_lock);
  39. static kmem_cache_t *dentry_cache __read_mostly;
  40. #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
  41. /*
  42. * This is the single most critical data structure when it comes
  43. * to the dcache: the hashtable for lookups. Somebody should try
  44. * to make this good - I've just made it work.
  45. *
  46. * This hash-function tries to avoid losing too many bits of hash
  47. * information, yet avoid using a prime hash-size or similar.
  48. */
  49. #define D_HASHBITS d_hash_shift
  50. #define D_HASHMASK d_hash_mask
  51. static unsigned int d_hash_mask __read_mostly;
  52. static unsigned int d_hash_shift __read_mostly;
  53. static struct hlist_head *dentry_hashtable __read_mostly;
  54. static LIST_HEAD(dentry_unused);
  55. /* Statistics gathering. */
  56. struct dentry_stat_t dentry_stat = {
  57. .age_limit = 45,
  58. };
  59. static void d_callback(struct rcu_head *head)
  60. {
  61. struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
  62. if (dname_external(dentry))
  63. kfree(dentry->d_name.name);
  64. kmem_cache_free(dentry_cache, dentry);
  65. }
  66. /*
  67. * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
  68. * inside dcache_lock.
  69. */
  70. static void d_free(struct dentry *dentry)
  71. {
  72. if (dentry->d_op && dentry->d_op->d_release)
  73. dentry->d_op->d_release(dentry);
  74. call_rcu(&dentry->d_u.d_rcu, d_callback);
  75. }
  76. /*
  77. * Release the dentry's inode, using the filesystem
  78. * d_iput() operation if defined.
  79. * Called with dcache_lock and per dentry lock held, drops both.
  80. */
  81. static void dentry_iput(struct dentry * dentry)
  82. {
  83. struct inode *inode = dentry->d_inode;
  84. if (inode) {
  85. dentry->d_inode = NULL;
  86. list_del_init(&dentry->d_alias);
  87. spin_unlock(&dentry->d_lock);
  88. spin_unlock(&dcache_lock);
  89. if (!inode->i_nlink)
  90. fsnotify_inoderemove(inode);
  91. if (dentry->d_op && dentry->d_op->d_iput)
  92. dentry->d_op->d_iput(dentry, inode);
  93. else
  94. iput(inode);
  95. } else {
  96. spin_unlock(&dentry->d_lock);
  97. spin_unlock(&dcache_lock);
  98. }
  99. }
  100. /*
  101. * This is dput
  102. *
  103. * This is complicated by the fact that we do not want to put
  104. * dentries that are no longer on any hash chain on the unused
  105. * list: we'd much rather just get rid of them immediately.
  106. *
  107. * However, that implies that we have to traverse the dentry
  108. * tree upwards to the parents which might _also_ now be
  109. * scheduled for deletion (it may have been only waiting for
  110. * its last child to go away).
  111. *
  112. * This tail recursion is done by hand as we don't want to depend
  113. * on the compiler to always get this right (gcc generally doesn't).
  114. * Real recursion would eat up our stack space.
  115. */
  116. /*
  117. * dput - release a dentry
  118. * @dentry: dentry to release
  119. *
  120. * Release a dentry. This will drop the usage count and if appropriate
  121. * call the dentry unlink method as well as removing it from the queues and
  122. * releasing its resources. If the parent dentries were scheduled for release
  123. * they too may now get deleted.
  124. *
  125. * no dcache lock, please.
  126. */
  127. void dput(struct dentry *dentry)
  128. {
  129. if (!dentry)
  130. return;
  131. repeat:
  132. if (atomic_read(&dentry->d_count) == 1)
  133. might_sleep();
  134. if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
  135. return;
  136. spin_lock(&dentry->d_lock);
  137. if (atomic_read(&dentry->d_count)) {
  138. spin_unlock(&dentry->d_lock);
  139. spin_unlock(&dcache_lock);
  140. return;
  141. }
  142. /*
  143. * AV: ->d_delete() is _NOT_ allowed to block now.
  144. */
  145. if (dentry->d_op && dentry->d_op->d_delete) {
  146. if (dentry->d_op->d_delete(dentry))
  147. goto unhash_it;
  148. }
  149. /* Unreachable? Get rid of it */
  150. if (d_unhashed(dentry))
  151. goto kill_it;
  152. if (list_empty(&dentry->d_lru)) {
  153. dentry->d_flags |= DCACHE_REFERENCED;
  154. list_add(&dentry->d_lru, &dentry_unused);
  155. dentry_stat.nr_unused++;
  156. }
  157. spin_unlock(&dentry->d_lock);
  158. spin_unlock(&dcache_lock);
  159. return;
  160. unhash_it:
  161. __d_drop(dentry);
  162. kill_it: {
  163. struct dentry *parent;
  164. /* If dentry was on d_lru list
  165. * delete it from there
  166. */
  167. if (!list_empty(&dentry->d_lru)) {
  168. list_del(&dentry->d_lru);
  169. dentry_stat.nr_unused--;
  170. }
  171. list_del(&dentry->d_u.d_child);
  172. dentry_stat.nr_dentry--; /* For d_free, below */
  173. /*drops the locks, at that point nobody can reach this dentry */
  174. dentry_iput(dentry);
  175. parent = dentry->d_parent;
  176. d_free(dentry);
  177. if (dentry == parent)
  178. return;
  179. dentry = parent;
  180. goto repeat;
  181. }
  182. }
  183. /**
  184. * d_invalidate - invalidate a dentry
  185. * @dentry: dentry to invalidate
  186. *
  187. * Try to invalidate the dentry if it turns out to be
  188. * possible. If there are other dentries that can be
  189. * reached through this one we can't delete it and we
  190. * return -EBUSY. On success we return 0.
  191. *
  192. * no dcache lock.
  193. */
  194. int d_invalidate(struct dentry * dentry)
  195. {
  196. /*
  197. * If it's already been dropped, return OK.
  198. */
  199. spin_lock(&dcache_lock);
  200. if (d_unhashed(dentry)) {
  201. spin_unlock(&dcache_lock);
  202. return 0;
  203. }
  204. /*
  205. * Check whether to do a partial shrink_dcache
  206. * to get rid of unused child entries.
  207. */
  208. if (!list_empty(&dentry->d_subdirs)) {
  209. spin_unlock(&dcache_lock);
  210. shrink_dcache_parent(dentry);
  211. spin_lock(&dcache_lock);
  212. }
  213. /*
  214. * Somebody else still using it?
  215. *
  216. * If it's a directory, we can't drop it
  217. * for fear of somebody re-populating it
  218. * with children (even though dropping it
  219. * would make it unreachable from the root,
  220. * we might still populate it if it was a
  221. * working directory or similar).
  222. */
  223. spin_lock(&dentry->d_lock);
  224. if (atomic_read(&dentry->d_count) > 1) {
  225. if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
  226. spin_unlock(&dentry->d_lock);
  227. spin_unlock(&dcache_lock);
  228. return -EBUSY;
  229. }
  230. }
  231. __d_drop(dentry);
  232. spin_unlock(&dentry->d_lock);
  233. spin_unlock(&dcache_lock);
  234. return 0;
  235. }
  236. /* This should be called _only_ with dcache_lock held */
  237. static inline struct dentry * __dget_locked(struct dentry *dentry)
  238. {
  239. atomic_inc(&dentry->d_count);
  240. if (!list_empty(&dentry->d_lru)) {
  241. dentry_stat.nr_unused--;
  242. list_del_init(&dentry->d_lru);
  243. }
  244. return dentry;
  245. }
  246. struct dentry * dget_locked(struct dentry *dentry)
  247. {
  248. return __dget_locked(dentry);
  249. }
  250. /**
  251. * d_find_alias - grab a hashed alias of inode
  252. * @inode: inode in question
  253. * @want_discon: flag, used by d_splice_alias, to request
  254. * that only a DISCONNECTED alias be returned.
  255. *
  256. * If inode has a hashed alias, or is a directory and has any alias,
  257. * acquire the reference to alias and return it. Otherwise return NULL.
  258. * Notice that if inode is a directory there can be only one alias and
  259. * it can be unhashed only if it has no children, or if it is the root
  260. * of a filesystem.
  261. *
  262. * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
  263. * any other hashed alias over that one unless @want_discon is set,
  264. * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
  265. */
  266. static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
  267. {
  268. struct list_head *head, *next, *tmp;
  269. struct dentry *alias, *discon_alias=NULL;
  270. head = &inode->i_dentry;
  271. next = inode->i_dentry.next;
  272. while (next != head) {
  273. tmp = next;
  274. next = tmp->next;
  275. prefetch(next);
  276. alias = list_entry(tmp, struct dentry, d_alias);
  277. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  278. if (IS_ROOT(alias) &&
  279. (alias->d_flags & DCACHE_DISCONNECTED))
  280. discon_alias = alias;
  281. else if (!want_discon) {
  282. __dget_locked(alias);
  283. return alias;
  284. }
  285. }
  286. }
  287. if (discon_alias)
  288. __dget_locked(discon_alias);
  289. return discon_alias;
  290. }
  291. struct dentry * d_find_alias(struct inode *inode)
  292. {
  293. struct dentry *de = NULL;
  294. if (!list_empty(&inode->i_dentry)) {
  295. spin_lock(&dcache_lock);
  296. de = __d_find_alias(inode, 0);
  297. spin_unlock(&dcache_lock);
  298. }
  299. return de;
  300. }
  301. /*
  302. * Try to kill dentries associated with this inode.
  303. * WARNING: you must own a reference to inode.
  304. */
  305. void d_prune_aliases(struct inode *inode)
  306. {
  307. struct dentry *dentry;
  308. restart:
  309. spin_lock(&dcache_lock);
  310. list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  311. spin_lock(&dentry->d_lock);
  312. if (!atomic_read(&dentry->d_count)) {
  313. __dget_locked(dentry);
  314. __d_drop(dentry);
  315. spin_unlock(&dentry->d_lock);
  316. spin_unlock(&dcache_lock);
  317. dput(dentry);
  318. goto restart;
  319. }
  320. spin_unlock(&dentry->d_lock);
  321. }
  322. spin_unlock(&dcache_lock);
  323. }
  324. /*
  325. * Throw away a dentry - free the inode, dput the parent. This requires that
  326. * the LRU list has already been removed.
  327. *
  328. * Called with dcache_lock, drops it and then regains.
  329. * Called with dentry->d_lock held, drops it.
  330. */
  331. static void prune_one_dentry(struct dentry * dentry)
  332. {
  333. struct dentry * parent;
  334. __d_drop(dentry);
  335. list_del(&dentry->d_u.d_child);
  336. dentry_stat.nr_dentry--; /* For d_free, below */
  337. dentry_iput(dentry);
  338. parent = dentry->d_parent;
  339. d_free(dentry);
  340. if (parent != dentry)
  341. dput(parent);
  342. spin_lock(&dcache_lock);
  343. }
  344. /**
  345. * prune_dcache - shrink the dcache
  346. * @count: number of entries to try and free
  347. * @sb: if given, ignore dentries for other superblocks
  348. * which are being unmounted.
  349. *
  350. * Shrink the dcache. This is done when we need
  351. * more memory, or simply when we need to unmount
  352. * something (at which point we need to unuse
  353. * all dentries).
  354. *
  355. * This function may fail to free any resources if
  356. * all the dentries are in use.
  357. */
  358. static void prune_dcache(int count, struct super_block *sb)
  359. {
  360. spin_lock(&dcache_lock);
  361. for (; count ; count--) {
  362. struct dentry *dentry;
  363. struct list_head *tmp;
  364. struct rw_semaphore *s_umount;
  365. cond_resched_lock(&dcache_lock);
  366. tmp = dentry_unused.prev;
  367. if (sb) {
  368. /* Try to find a dentry for this sb, but don't try
  369. * too hard, if they aren't near the tail they will
  370. * be moved down again soon
  371. */
  372. int skip = count;
  373. while (skip && tmp != &dentry_unused &&
  374. list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
  375. skip--;
  376. tmp = tmp->prev;
  377. }
  378. }
  379. if (tmp == &dentry_unused)
  380. break;
  381. list_del_init(tmp);
  382. prefetch(dentry_unused.prev);
  383. dentry_stat.nr_unused--;
  384. dentry = list_entry(tmp, struct dentry, d_lru);
  385. spin_lock(&dentry->d_lock);
  386. /*
  387. * We found an inuse dentry which was not removed from
  388. * dentry_unused because of laziness during lookup. Do not free
  389. * it - just keep it off the dentry_unused list.
  390. */
  391. if (atomic_read(&dentry->d_count)) {
  392. spin_unlock(&dentry->d_lock);
  393. continue;
  394. }
  395. /* If the dentry was recently referenced, don't free it. */
  396. if (dentry->d_flags & DCACHE_REFERENCED) {
  397. dentry->d_flags &= ~DCACHE_REFERENCED;
  398. list_add(&dentry->d_lru, &dentry_unused);
  399. dentry_stat.nr_unused++;
  400. spin_unlock(&dentry->d_lock);
  401. continue;
  402. }
  403. /*
  404. * If the dentry is not DCACHED_REFERENCED, it is time
  405. * to remove it from the dcache, provided the super block is
  406. * NULL (which means we are trying to reclaim memory)
  407. * or this dentry belongs to the same super block that
  408. * we want to shrink.
  409. */
  410. /*
  411. * If this dentry is for "my" filesystem, then I can prune it
  412. * without taking the s_umount lock (I already hold it).
  413. */
  414. if (sb && dentry->d_sb == sb) {
  415. prune_one_dentry(dentry);
  416. continue;
  417. }
  418. /*
  419. * ...otherwise we need to be sure this filesystem isn't being
  420. * unmounted, otherwise we could race with
  421. * generic_shutdown_super(), and end up holding a reference to
  422. * an inode while the filesystem is unmounted.
  423. * So we try to get s_umount, and make sure s_root isn't NULL.
  424. * (Take a local copy of s_umount to avoid a use-after-free of
  425. * `dentry').
  426. */
  427. s_umount = &dentry->d_sb->s_umount;
  428. if (down_read_trylock(s_umount)) {
  429. if (dentry->d_sb->s_root != NULL) {
  430. prune_one_dentry(dentry);
  431. up_read(s_umount);
  432. continue;
  433. }
  434. up_read(s_umount);
  435. }
  436. spin_unlock(&dentry->d_lock);
  437. /*
  438. * Insert dentry at the head of the list as inserting at the
  439. * tail leads to a cycle.
  440. */
  441. list_add(&dentry->d_lru, &dentry_unused);
  442. dentry_stat.nr_unused++;
  443. }
  444. spin_unlock(&dcache_lock);
  445. }
  446. /*
  447. * Shrink the dcache for the specified super block.
  448. * This allows us to unmount a device without disturbing
  449. * the dcache for the other devices.
  450. *
  451. * This implementation makes just two traversals of the
  452. * unused list. On the first pass we move the selected
  453. * dentries to the most recent end, and on the second
  454. * pass we free them. The second pass must restart after
  455. * each dput(), but since the target dentries are all at
  456. * the end, it's really just a single traversal.
  457. */
  458. /**
  459. * shrink_dcache_sb - shrink dcache for a superblock
  460. * @sb: superblock
  461. *
  462. * Shrink the dcache for the specified super block. This
  463. * is used to free the dcache before unmounting a file
  464. * system
  465. */
  466. void shrink_dcache_sb(struct super_block * sb)
  467. {
  468. struct list_head *tmp, *next;
  469. struct dentry *dentry;
  470. /*
  471. * Pass one ... move the dentries for the specified
  472. * superblock to the most recent end of the unused list.
  473. */
  474. spin_lock(&dcache_lock);
  475. list_for_each_safe(tmp, next, &dentry_unused) {
  476. dentry = list_entry(tmp, struct dentry, d_lru);
  477. if (dentry->d_sb != sb)
  478. continue;
  479. list_move(tmp, &dentry_unused);
  480. }
  481. /*
  482. * Pass two ... free the dentries for this superblock.
  483. */
  484. repeat:
  485. list_for_each_safe(tmp, next, &dentry_unused) {
  486. dentry = list_entry(tmp, struct dentry, d_lru);
  487. if (dentry->d_sb != sb)
  488. continue;
  489. dentry_stat.nr_unused--;
  490. list_del_init(tmp);
  491. spin_lock(&dentry->d_lock);
  492. if (atomic_read(&dentry->d_count)) {
  493. spin_unlock(&dentry->d_lock);
  494. continue;
  495. }
  496. prune_one_dentry(dentry);
  497. cond_resched_lock(&dcache_lock);
  498. goto repeat;
  499. }
  500. spin_unlock(&dcache_lock);
  501. }
  502. /*
  503. * destroy a single subtree of dentries for unmount
  504. * - see the comments on shrink_dcache_for_umount() for a description of the
  505. * locking
  506. */
  507. static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
  508. {
  509. struct dentry *parent;
  510. unsigned detached = 0;
  511. BUG_ON(!IS_ROOT(dentry));
  512. /* detach this root from the system */
  513. spin_lock(&dcache_lock);
  514. if (!list_empty(&dentry->d_lru)) {
  515. dentry_stat.nr_unused--;
  516. list_del_init(&dentry->d_lru);
  517. }
  518. __d_drop(dentry);
  519. spin_unlock(&dcache_lock);
  520. for (;;) {
  521. /* descend to the first leaf in the current subtree */
  522. while (!list_empty(&dentry->d_subdirs)) {
  523. struct dentry *loop;
  524. /* this is a branch with children - detach all of them
  525. * from the system in one go */
  526. spin_lock(&dcache_lock);
  527. list_for_each_entry(loop, &dentry->d_subdirs,
  528. d_u.d_child) {
  529. if (!list_empty(&loop->d_lru)) {
  530. dentry_stat.nr_unused--;
  531. list_del_init(&loop->d_lru);
  532. }
  533. __d_drop(loop);
  534. cond_resched_lock(&dcache_lock);
  535. }
  536. spin_unlock(&dcache_lock);
  537. /* move to the first child */
  538. dentry = list_entry(dentry->d_subdirs.next,
  539. struct dentry, d_u.d_child);
  540. }
  541. /* consume the dentries from this leaf up through its parents
  542. * until we find one with children or run out altogether */
  543. do {
  544. struct inode *inode;
  545. if (atomic_read(&dentry->d_count) != 0) {
  546. printk(KERN_ERR
  547. "BUG: Dentry %p{i=%lx,n=%s}"
  548. " still in use (%d)"
  549. " [unmount of %s %s]\n",
  550. dentry,
  551. dentry->d_inode ?
  552. dentry->d_inode->i_ino : 0UL,
  553. dentry->d_name.name,
  554. atomic_read(&dentry->d_count),
  555. dentry->d_sb->s_type->name,
  556. dentry->d_sb->s_id);
  557. BUG();
  558. }
  559. parent = dentry->d_parent;
  560. if (parent == dentry)
  561. parent = NULL;
  562. else
  563. atomic_dec(&parent->d_count);
  564. list_del(&dentry->d_u.d_child);
  565. detached++;
  566. inode = dentry->d_inode;
  567. if (inode) {
  568. dentry->d_inode = NULL;
  569. list_del_init(&dentry->d_alias);
  570. if (dentry->d_op && dentry->d_op->d_iput)
  571. dentry->d_op->d_iput(dentry, inode);
  572. else
  573. iput(inode);
  574. }
  575. d_free(dentry);
  576. /* finished when we fall off the top of the tree,
  577. * otherwise we ascend to the parent and move to the
  578. * next sibling if there is one */
  579. if (!parent)
  580. goto out;
  581. dentry = parent;
  582. } while (list_empty(&dentry->d_subdirs));
  583. dentry = list_entry(dentry->d_subdirs.next,
  584. struct dentry, d_u.d_child);
  585. }
  586. out:
  587. /* several dentries were freed, need to correct nr_dentry */
  588. spin_lock(&dcache_lock);
  589. dentry_stat.nr_dentry -= detached;
  590. spin_unlock(&dcache_lock);
  591. }
  592. /*
  593. * destroy the dentries attached to a superblock on unmounting
  594. * - we don't need to use dentry->d_lock, and only need dcache_lock when
  595. * removing the dentry from the system lists and hashes because:
  596. * - the superblock is detached from all mountings and open files, so the
  597. * dentry trees will not be rearranged by the VFS
  598. * - s_umount is write-locked, so the memory pressure shrinker will ignore
  599. * any dentries belonging to this superblock that it comes across
  600. * - the filesystem itself is no longer permitted to rearrange the dentries
  601. * in this superblock
  602. */
  603. void shrink_dcache_for_umount(struct super_block *sb)
  604. {
  605. struct dentry *dentry;
  606. if (down_read_trylock(&sb->s_umount))
  607. BUG();
  608. dentry = sb->s_root;
  609. sb->s_root = NULL;
  610. atomic_dec(&dentry->d_count);
  611. shrink_dcache_for_umount_subtree(dentry);
  612. while (!hlist_empty(&sb->s_anon)) {
  613. dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
  614. shrink_dcache_for_umount_subtree(dentry);
  615. }
  616. }
  617. /*
  618. * Search for at least 1 mount point in the dentry's subdirs.
  619. * We descend to the next level whenever the d_subdirs
  620. * list is non-empty and continue searching.
  621. */
  622. /**
  623. * have_submounts - check for mounts over a dentry
  624. * @parent: dentry to check.
  625. *
  626. * Return true if the parent or its subdirectories contain
  627. * a mount point
  628. */
  629. int have_submounts(struct dentry *parent)
  630. {
  631. struct dentry *this_parent = parent;
  632. struct list_head *next;
  633. spin_lock(&dcache_lock);
  634. if (d_mountpoint(parent))
  635. goto positive;
  636. repeat:
  637. next = this_parent->d_subdirs.next;
  638. resume:
  639. while (next != &this_parent->d_subdirs) {
  640. struct list_head *tmp = next;
  641. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  642. next = tmp->next;
  643. /* Have we found a mount point ? */
  644. if (d_mountpoint(dentry))
  645. goto positive;
  646. if (!list_empty(&dentry->d_subdirs)) {
  647. this_parent = dentry;
  648. goto repeat;
  649. }
  650. }
  651. /*
  652. * All done at this level ... ascend and resume the search.
  653. */
  654. if (this_parent != parent) {
  655. next = this_parent->d_u.d_child.next;
  656. this_parent = this_parent->d_parent;
  657. goto resume;
  658. }
  659. spin_unlock(&dcache_lock);
  660. return 0; /* No mount points found in tree */
  661. positive:
  662. spin_unlock(&dcache_lock);
  663. return 1;
  664. }
  665. /*
  666. * Search the dentry child list for the specified parent,
  667. * and move any unused dentries to the end of the unused
  668. * list for prune_dcache(). We descend to the next level
  669. * whenever the d_subdirs list is non-empty and continue
  670. * searching.
  671. *
  672. * It returns zero iff there are no unused children,
  673. * otherwise it returns the number of children moved to
  674. * the end of the unused list. This may not be the total
  675. * number of unused children, because select_parent can
  676. * drop the lock and return early due to latency
  677. * constraints.
  678. */
  679. static int select_parent(struct dentry * parent)
  680. {
  681. struct dentry *this_parent = parent;
  682. struct list_head *next;
  683. int found = 0;
  684. spin_lock(&dcache_lock);
  685. repeat:
  686. next = this_parent->d_subdirs.next;
  687. resume:
  688. while (next != &this_parent->d_subdirs) {
  689. struct list_head *tmp = next;
  690. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  691. next = tmp->next;
  692. if (!list_empty(&dentry->d_lru)) {
  693. dentry_stat.nr_unused--;
  694. list_del_init(&dentry->d_lru);
  695. }
  696. /*
  697. * move only zero ref count dentries to the end
  698. * of the unused list for prune_dcache
  699. */
  700. if (!atomic_read(&dentry->d_count)) {
  701. list_add_tail(&dentry->d_lru, &dentry_unused);
  702. dentry_stat.nr_unused++;
  703. found++;
  704. }
  705. /*
  706. * We can return to the caller if we have found some (this
  707. * ensures forward progress). We'll be coming back to find
  708. * the rest.
  709. */
  710. if (found && need_resched())
  711. goto out;
  712. /*
  713. * Descend a level if the d_subdirs list is non-empty.
  714. */
  715. if (!list_empty(&dentry->d_subdirs)) {
  716. this_parent = dentry;
  717. goto repeat;
  718. }
  719. }
  720. /*
  721. * All done at this level ... ascend and resume the search.
  722. */
  723. if (this_parent != parent) {
  724. next = this_parent->d_u.d_child.next;
  725. this_parent = this_parent->d_parent;
  726. goto resume;
  727. }
  728. out:
  729. spin_unlock(&dcache_lock);
  730. return found;
  731. }
  732. /**
  733. * shrink_dcache_parent - prune dcache
  734. * @parent: parent of entries to prune
  735. *
  736. * Prune the dcache to remove unused children of the parent dentry.
  737. */
  738. void shrink_dcache_parent(struct dentry * parent)
  739. {
  740. int found;
  741. while ((found = select_parent(parent)) != 0)
  742. prune_dcache(found, parent->d_sb);
  743. }
  744. /*
  745. * Scan `nr' dentries and return the number which remain.
  746. *
  747. * We need to avoid reentering the filesystem if the caller is performing a
  748. * GFP_NOFS allocation attempt. One example deadlock is:
  749. *
  750. * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
  751. * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
  752. * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
  753. *
  754. * In this case we return -1 to tell the caller that we baled.
  755. */
  756. static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
  757. {
  758. if (nr) {
  759. if (!(gfp_mask & __GFP_FS))
  760. return -1;
  761. prune_dcache(nr, NULL);
  762. }
  763. return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  764. }
  765. /**
  766. * d_alloc - allocate a dcache entry
  767. * @parent: parent of entry to allocate
  768. * @name: qstr of the name
  769. *
  770. * Allocates a dentry. It returns %NULL if there is insufficient memory
  771. * available. On a success the dentry is returned. The name passed in is
  772. * copied and the copy passed in may be reused after this call.
  773. */
  774. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  775. {
  776. struct dentry *dentry;
  777. char *dname;
  778. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  779. if (!dentry)
  780. return NULL;
  781. if (name->len > DNAME_INLINE_LEN-1) {
  782. dname = kmalloc(name->len + 1, GFP_KERNEL);
  783. if (!dname) {
  784. kmem_cache_free(dentry_cache, dentry);
  785. return NULL;
  786. }
  787. } else {
  788. dname = dentry->d_iname;
  789. }
  790. dentry->d_name.name = dname;
  791. dentry->d_name.len = name->len;
  792. dentry->d_name.hash = name->hash;
  793. memcpy(dname, name->name, name->len);
  794. dname[name->len] = 0;
  795. atomic_set(&dentry->d_count, 1);
  796. dentry->d_flags = DCACHE_UNHASHED;
  797. spin_lock_init(&dentry->d_lock);
  798. dentry->d_inode = NULL;
  799. dentry->d_parent = NULL;
  800. dentry->d_sb = NULL;
  801. dentry->d_op = NULL;
  802. dentry->d_fsdata = NULL;
  803. dentry->d_mounted = 0;
  804. #ifdef CONFIG_PROFILING
  805. dentry->d_cookie = NULL;
  806. #endif
  807. INIT_HLIST_NODE(&dentry->d_hash);
  808. INIT_LIST_HEAD(&dentry->d_lru);
  809. INIT_LIST_HEAD(&dentry->d_subdirs);
  810. INIT_LIST_HEAD(&dentry->d_alias);
  811. if (parent) {
  812. dentry->d_parent = dget(parent);
  813. dentry->d_sb = parent->d_sb;
  814. } else {
  815. INIT_LIST_HEAD(&dentry->d_u.d_child);
  816. }
  817. spin_lock(&dcache_lock);
  818. if (parent)
  819. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  820. dentry_stat.nr_dentry++;
  821. spin_unlock(&dcache_lock);
  822. return dentry;
  823. }
  824. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  825. {
  826. struct qstr q;
  827. q.name = name;
  828. q.len = strlen(name);
  829. q.hash = full_name_hash(q.name, q.len);
  830. return d_alloc(parent, &q);
  831. }
  832. /**
  833. * d_instantiate - fill in inode information for a dentry
  834. * @entry: dentry to complete
  835. * @inode: inode to attach to this dentry
  836. *
  837. * Fill in inode information in the entry.
  838. *
  839. * This turns negative dentries into productive full members
  840. * of society.
  841. *
  842. * NOTE! This assumes that the inode count has been incremented
  843. * (or otherwise set) by the caller to indicate that it is now
  844. * in use by the dcache.
  845. */
  846. void d_instantiate(struct dentry *entry, struct inode * inode)
  847. {
  848. BUG_ON(!list_empty(&entry->d_alias));
  849. spin_lock(&dcache_lock);
  850. if (inode)
  851. list_add(&entry->d_alias, &inode->i_dentry);
  852. entry->d_inode = inode;
  853. fsnotify_d_instantiate(entry, inode);
  854. spin_unlock(&dcache_lock);
  855. security_d_instantiate(entry, inode);
  856. }
  857. /**
  858. * d_instantiate_unique - instantiate a non-aliased dentry
  859. * @entry: dentry to instantiate
  860. * @inode: inode to attach to this dentry
  861. *
  862. * Fill in inode information in the entry. On success, it returns NULL.
  863. * If an unhashed alias of "entry" already exists, then we return the
  864. * aliased dentry instead and drop one reference to inode.
  865. *
  866. * Note that in order to avoid conflicts with rename() etc, the caller
  867. * had better be holding the parent directory semaphore.
  868. *
  869. * This also assumes that the inode count has been incremented
  870. * (or otherwise set) by the caller to indicate that it is now
  871. * in use by the dcache.
  872. */
  873. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  874. struct inode *inode)
  875. {
  876. struct dentry *alias;
  877. int len = entry->d_name.len;
  878. const char *name = entry->d_name.name;
  879. unsigned int hash = entry->d_name.hash;
  880. if (!inode) {
  881. entry->d_inode = NULL;
  882. return NULL;
  883. }
  884. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  885. struct qstr *qstr = &alias->d_name;
  886. if (qstr->hash != hash)
  887. continue;
  888. if (alias->d_parent != entry->d_parent)
  889. continue;
  890. if (qstr->len != len)
  891. continue;
  892. if (memcmp(qstr->name, name, len))
  893. continue;
  894. dget_locked(alias);
  895. return alias;
  896. }
  897. list_add(&entry->d_alias, &inode->i_dentry);
  898. entry->d_inode = inode;
  899. fsnotify_d_instantiate(entry, inode);
  900. return NULL;
  901. }
  902. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  903. {
  904. struct dentry *result;
  905. BUG_ON(!list_empty(&entry->d_alias));
  906. spin_lock(&dcache_lock);
  907. result = __d_instantiate_unique(entry, inode);
  908. spin_unlock(&dcache_lock);
  909. if (!result) {
  910. security_d_instantiate(entry, inode);
  911. return NULL;
  912. }
  913. BUG_ON(!d_unhashed(result));
  914. iput(inode);
  915. return result;
  916. }
  917. EXPORT_SYMBOL(d_instantiate_unique);
  918. /**
  919. * d_alloc_root - allocate root dentry
  920. * @root_inode: inode to allocate the root for
  921. *
  922. * Allocate a root ("/") dentry for the inode given. The inode is
  923. * instantiated and returned. %NULL is returned if there is insufficient
  924. * memory or the inode passed is %NULL.
  925. */
  926. struct dentry * d_alloc_root(struct inode * root_inode)
  927. {
  928. struct dentry *res = NULL;
  929. if (root_inode) {
  930. static const struct qstr name = { .name = "/", .len = 1 };
  931. res = d_alloc(NULL, &name);
  932. if (res) {
  933. res->d_sb = root_inode->i_sb;
  934. res->d_parent = res;
  935. d_instantiate(res, root_inode);
  936. }
  937. }
  938. return res;
  939. }
  940. static inline struct hlist_head *d_hash(struct dentry *parent,
  941. unsigned long hash)
  942. {
  943. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  944. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  945. return dentry_hashtable + (hash & D_HASHMASK);
  946. }
  947. /**
  948. * d_alloc_anon - allocate an anonymous dentry
  949. * @inode: inode to allocate the dentry for
  950. *
  951. * This is similar to d_alloc_root. It is used by filesystems when
  952. * creating a dentry for a given inode, often in the process of
  953. * mapping a filehandle to a dentry. The returned dentry may be
  954. * anonymous, or may have a full name (if the inode was already
  955. * in the cache). The file system may need to make further
  956. * efforts to connect this dentry into the dcache properly.
  957. *
  958. * When called on a directory inode, we must ensure that
  959. * the inode only ever has one dentry. If a dentry is
  960. * found, that is returned instead of allocating a new one.
  961. *
  962. * On successful return, the reference to the inode has been transferred
  963. * to the dentry. If %NULL is returned (indicating kmalloc failure),
  964. * the reference on the inode has not been released.
  965. */
  966. struct dentry * d_alloc_anon(struct inode *inode)
  967. {
  968. static const struct qstr anonstring = { .name = "" };
  969. struct dentry *tmp;
  970. struct dentry *res;
  971. if ((res = d_find_alias(inode))) {
  972. iput(inode);
  973. return res;
  974. }
  975. tmp = d_alloc(NULL, &anonstring);
  976. if (!tmp)
  977. return NULL;
  978. tmp->d_parent = tmp; /* make sure dput doesn't croak */
  979. spin_lock(&dcache_lock);
  980. res = __d_find_alias(inode, 0);
  981. if (!res) {
  982. /* attach a disconnected dentry */
  983. res = tmp;
  984. tmp = NULL;
  985. spin_lock(&res->d_lock);
  986. res->d_sb = inode->i_sb;
  987. res->d_parent = res;
  988. res->d_inode = inode;
  989. res->d_flags |= DCACHE_DISCONNECTED;
  990. res->d_flags &= ~DCACHE_UNHASHED;
  991. list_add(&res->d_alias, &inode->i_dentry);
  992. hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
  993. spin_unlock(&res->d_lock);
  994. inode = NULL; /* don't drop reference */
  995. }
  996. spin_unlock(&dcache_lock);
  997. if (inode)
  998. iput(inode);
  999. if (tmp)
  1000. dput(tmp);
  1001. return res;
  1002. }
  1003. /**
  1004. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  1005. * @inode: the inode which may have a disconnected dentry
  1006. * @dentry: a negative dentry which we want to point to the inode.
  1007. *
  1008. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1009. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1010. * and return it, else simply d_add the inode to the dentry and return NULL.
  1011. *
  1012. * This is needed in the lookup routine of any filesystem that is exportable
  1013. * (via knfsd) so that we can build dcache paths to directories effectively.
  1014. *
  1015. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1016. * is returned. This matches the expected return value of ->lookup.
  1017. *
  1018. */
  1019. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1020. {
  1021. struct dentry *new = NULL;
  1022. if (inode && S_ISDIR(inode->i_mode)) {
  1023. spin_lock(&dcache_lock);
  1024. new = __d_find_alias(inode, 1);
  1025. if (new) {
  1026. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  1027. fsnotify_d_instantiate(new, inode);
  1028. spin_unlock(&dcache_lock);
  1029. security_d_instantiate(new, inode);
  1030. d_rehash(dentry);
  1031. d_move(new, dentry);
  1032. iput(inode);
  1033. } else {
  1034. /* d_instantiate takes dcache_lock, so we do it by hand */
  1035. list_add(&dentry->d_alias, &inode->i_dentry);
  1036. dentry->d_inode = inode;
  1037. fsnotify_d_instantiate(dentry, inode);
  1038. spin_unlock(&dcache_lock);
  1039. security_d_instantiate(dentry, inode);
  1040. d_rehash(dentry);
  1041. }
  1042. } else
  1043. d_add(dentry, inode);
  1044. return new;
  1045. }
  1046. /**
  1047. * d_lookup - search for a dentry
  1048. * @parent: parent dentry
  1049. * @name: qstr of name we wish to find
  1050. *
  1051. * Searches the children of the parent dentry for the name in question. If
  1052. * the dentry is found its reference count is incremented and the dentry
  1053. * is returned. The caller must use d_put to free the entry when it has
  1054. * finished using it. %NULL is returned on failure.
  1055. *
  1056. * __d_lookup is dcache_lock free. The hash list is protected using RCU.
  1057. * Memory barriers are used while updating and doing lockless traversal.
  1058. * To avoid races with d_move while rename is happening, d_lock is used.
  1059. *
  1060. * Overflows in memcmp(), while d_move, are avoided by keeping the length
  1061. * and name pointer in one structure pointed by d_qstr.
  1062. *
  1063. * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
  1064. * lookup is going on.
  1065. *
  1066. * dentry_unused list is not updated even if lookup finds the required dentry
  1067. * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
  1068. * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
  1069. * acquisition.
  1070. *
  1071. * d_lookup() is protected against the concurrent renames in some unrelated
  1072. * directory using the seqlockt_t rename_lock.
  1073. */
  1074. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  1075. {
  1076. struct dentry * dentry = NULL;
  1077. unsigned long seq;
  1078. do {
  1079. seq = read_seqbegin(&rename_lock);
  1080. dentry = __d_lookup(parent, name);
  1081. if (dentry)
  1082. break;
  1083. } while (read_seqretry(&rename_lock, seq));
  1084. return dentry;
  1085. }
  1086. struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
  1087. {
  1088. unsigned int len = name->len;
  1089. unsigned int hash = name->hash;
  1090. const unsigned char *str = name->name;
  1091. struct hlist_head *head = d_hash(parent,hash);
  1092. struct dentry *found = NULL;
  1093. struct hlist_node *node;
  1094. struct dentry *dentry;
  1095. rcu_read_lock();
  1096. hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
  1097. struct qstr *qstr;
  1098. if (dentry->d_name.hash != hash)
  1099. continue;
  1100. if (dentry->d_parent != parent)
  1101. continue;
  1102. spin_lock(&dentry->d_lock);
  1103. /*
  1104. * Recheck the dentry after taking the lock - d_move may have
  1105. * changed things. Don't bother checking the hash because we're
  1106. * about to compare the whole name anyway.
  1107. */
  1108. if (dentry->d_parent != parent)
  1109. goto next;
  1110. /*
  1111. * It is safe to compare names since d_move() cannot
  1112. * change the qstr (protected by d_lock).
  1113. */
  1114. qstr = &dentry->d_name;
  1115. if (parent->d_op && parent->d_op->d_compare) {
  1116. if (parent->d_op->d_compare(parent, qstr, name))
  1117. goto next;
  1118. } else {
  1119. if (qstr->len != len)
  1120. goto next;
  1121. if (memcmp(qstr->name, str, len))
  1122. goto next;
  1123. }
  1124. if (!d_unhashed(dentry)) {
  1125. atomic_inc(&dentry->d_count);
  1126. found = dentry;
  1127. }
  1128. spin_unlock(&dentry->d_lock);
  1129. break;
  1130. next:
  1131. spin_unlock(&dentry->d_lock);
  1132. }
  1133. rcu_read_unlock();
  1134. return found;
  1135. }
  1136. /**
  1137. * d_hash_and_lookup - hash the qstr then search for a dentry
  1138. * @dir: Directory to search in
  1139. * @name: qstr of name we wish to find
  1140. *
  1141. * On hash failure or on lookup failure NULL is returned.
  1142. */
  1143. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1144. {
  1145. struct dentry *dentry = NULL;
  1146. /*
  1147. * Check for a fs-specific hash function. Note that we must
  1148. * calculate the standard hash first, as the d_op->d_hash()
  1149. * routine may choose to leave the hash value unchanged.
  1150. */
  1151. name->hash = full_name_hash(name->name, name->len);
  1152. if (dir->d_op && dir->d_op->d_hash) {
  1153. if (dir->d_op->d_hash(dir, name) < 0)
  1154. goto out;
  1155. }
  1156. dentry = d_lookup(dir, name);
  1157. out:
  1158. return dentry;
  1159. }
  1160. /**
  1161. * d_validate - verify dentry provided from insecure source
  1162. * @dentry: The dentry alleged to be valid child of @dparent
  1163. * @dparent: The parent dentry (known to be valid)
  1164. * @hash: Hash of the dentry
  1165. * @len: Length of the name
  1166. *
  1167. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1168. * This is used by ncpfs in its readdir implementation.
  1169. * Zero is returned in the dentry is invalid.
  1170. */
  1171. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1172. {
  1173. struct hlist_head *base;
  1174. struct hlist_node *lhp;
  1175. /* Check whether the ptr might be valid at all.. */
  1176. if (!kmem_ptr_validate(dentry_cache, dentry))
  1177. goto out;
  1178. if (dentry->d_parent != dparent)
  1179. goto out;
  1180. spin_lock(&dcache_lock);
  1181. base = d_hash(dparent, dentry->d_name.hash);
  1182. hlist_for_each(lhp,base) {
  1183. /* hlist_for_each_entry_rcu() not required for d_hash list
  1184. * as it is parsed under dcache_lock
  1185. */
  1186. if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
  1187. __dget_locked(dentry);
  1188. spin_unlock(&dcache_lock);
  1189. return 1;
  1190. }
  1191. }
  1192. spin_unlock(&dcache_lock);
  1193. out:
  1194. return 0;
  1195. }
  1196. /*
  1197. * When a file is deleted, we have two options:
  1198. * - turn this dentry into a negative dentry
  1199. * - unhash this dentry and free it.
  1200. *
  1201. * Usually, we want to just turn this into
  1202. * a negative dentry, but if anybody else is
  1203. * currently using the dentry or the inode
  1204. * we can't do that and we fall back on removing
  1205. * it from the hash queues and waiting for
  1206. * it to be deleted later when it has no users
  1207. */
  1208. /**
  1209. * d_delete - delete a dentry
  1210. * @dentry: The dentry to delete
  1211. *
  1212. * Turn the dentry into a negative dentry if possible, otherwise
  1213. * remove it from the hash queues so it can be deleted later
  1214. */
  1215. void d_delete(struct dentry * dentry)
  1216. {
  1217. int isdir = 0;
  1218. /*
  1219. * Are we the only user?
  1220. */
  1221. spin_lock(&dcache_lock);
  1222. spin_lock(&dentry->d_lock);
  1223. isdir = S_ISDIR(dentry->d_inode->i_mode);
  1224. if (atomic_read(&dentry->d_count) == 1) {
  1225. dentry_iput(dentry);
  1226. fsnotify_nameremove(dentry, isdir);
  1227. /* remove this and other inotify debug checks after 2.6.18 */
  1228. dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
  1229. return;
  1230. }
  1231. if (!d_unhashed(dentry))
  1232. __d_drop(dentry);
  1233. spin_unlock(&dentry->d_lock);
  1234. spin_unlock(&dcache_lock);
  1235. fsnotify_nameremove(dentry, isdir);
  1236. }
  1237. static void __d_rehash(struct dentry * entry, struct hlist_head *list)
  1238. {
  1239. entry->d_flags &= ~DCACHE_UNHASHED;
  1240. hlist_add_head_rcu(&entry->d_hash, list);
  1241. }
  1242. static void _d_rehash(struct dentry * entry)
  1243. {
  1244. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1245. }
  1246. /**
  1247. * d_rehash - add an entry back to the hash
  1248. * @entry: dentry to add to the hash
  1249. *
  1250. * Adds a dentry to the hash according to its name.
  1251. */
  1252. void d_rehash(struct dentry * entry)
  1253. {
  1254. spin_lock(&dcache_lock);
  1255. spin_lock(&entry->d_lock);
  1256. _d_rehash(entry);
  1257. spin_unlock(&entry->d_lock);
  1258. spin_unlock(&dcache_lock);
  1259. }
  1260. #define do_switch(x,y) do { \
  1261. __typeof__ (x) __tmp = x; \
  1262. x = y; y = __tmp; } while (0)
  1263. /*
  1264. * When switching names, the actual string doesn't strictly have to
  1265. * be preserved in the target - because we're dropping the target
  1266. * anyway. As such, we can just do a simple memcpy() to copy over
  1267. * the new name before we switch.
  1268. *
  1269. * Note that we have to be a lot more careful about getting the hash
  1270. * switched - we have to switch the hash value properly even if it
  1271. * then no longer matches the actual (corrupted) string of the target.
  1272. * The hash value has to match the hash queue that the dentry is on..
  1273. */
  1274. static void switch_names(struct dentry *dentry, struct dentry *target)
  1275. {
  1276. if (dname_external(target)) {
  1277. if (dname_external(dentry)) {
  1278. /*
  1279. * Both external: swap the pointers
  1280. */
  1281. do_switch(target->d_name.name, dentry->d_name.name);
  1282. } else {
  1283. /*
  1284. * dentry:internal, target:external. Steal target's
  1285. * storage and make target internal.
  1286. */
  1287. dentry->d_name.name = target->d_name.name;
  1288. target->d_name.name = target->d_iname;
  1289. }
  1290. } else {
  1291. if (dname_external(dentry)) {
  1292. /*
  1293. * dentry:external, target:internal. Give dentry's
  1294. * storage to target and make dentry internal
  1295. */
  1296. memcpy(dentry->d_iname, target->d_name.name,
  1297. target->d_name.len + 1);
  1298. target->d_name.name = dentry->d_name.name;
  1299. dentry->d_name.name = dentry->d_iname;
  1300. } else {
  1301. /*
  1302. * Both are internal. Just copy target to dentry
  1303. */
  1304. memcpy(dentry->d_iname, target->d_name.name,
  1305. target->d_name.len + 1);
  1306. }
  1307. }
  1308. }
  1309. /*
  1310. * We cannibalize "target" when moving dentry on top of it,
  1311. * because it's going to be thrown away anyway. We could be more
  1312. * polite about it, though.
  1313. *
  1314. * This forceful removal will result in ugly /proc output if
  1315. * somebody holds a file open that got deleted due to a rename.
  1316. * We could be nicer about the deleted file, and let it show
  1317. * up under the name it got deleted rather than the name that
  1318. * deleted it.
  1319. */
  1320. /*
  1321. * d_move_locked - move a dentry
  1322. * @dentry: entry to move
  1323. * @target: new dentry
  1324. *
  1325. * Update the dcache to reflect the move of a file name. Negative
  1326. * dcache entries should not be moved in this way.
  1327. */
  1328. static void d_move_locked(struct dentry * dentry, struct dentry * target)
  1329. {
  1330. struct hlist_head *list;
  1331. if (!dentry->d_inode)
  1332. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  1333. write_seqlock(&rename_lock);
  1334. /*
  1335. * XXXX: do we really need to take target->d_lock?
  1336. */
  1337. if (target < dentry) {
  1338. spin_lock(&target->d_lock);
  1339. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1340. } else {
  1341. spin_lock(&dentry->d_lock);
  1342. spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
  1343. }
  1344. /* Move the dentry to the target hash queue, if on different bucket */
  1345. if (dentry->d_flags & DCACHE_UNHASHED)
  1346. goto already_unhashed;
  1347. hlist_del_rcu(&dentry->d_hash);
  1348. already_unhashed:
  1349. list = d_hash(target->d_parent, target->d_name.hash);
  1350. __d_rehash(dentry, list);
  1351. /* Unhash the target: dput() will then get rid of it */
  1352. __d_drop(target);
  1353. list_del(&dentry->d_u.d_child);
  1354. list_del(&target->d_u.d_child);
  1355. /* Switch the names.. */
  1356. switch_names(dentry, target);
  1357. do_switch(dentry->d_name.len, target->d_name.len);
  1358. do_switch(dentry->d_name.hash, target->d_name.hash);
  1359. /* ... and switch the parents */
  1360. if (IS_ROOT(dentry)) {
  1361. dentry->d_parent = target->d_parent;
  1362. target->d_parent = target;
  1363. INIT_LIST_HEAD(&target->d_u.d_child);
  1364. } else {
  1365. do_switch(dentry->d_parent, target->d_parent);
  1366. /* And add them back to the (new) parent lists */
  1367. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  1368. }
  1369. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1370. spin_unlock(&target->d_lock);
  1371. fsnotify_d_move(dentry);
  1372. spin_unlock(&dentry->d_lock);
  1373. write_sequnlock(&rename_lock);
  1374. }
  1375. /**
  1376. * d_move - move a dentry
  1377. * @dentry: entry to move
  1378. * @target: new dentry
  1379. *
  1380. * Update the dcache to reflect the move of a file name. Negative
  1381. * dcache entries should not be moved in this way.
  1382. */
  1383. void d_move(struct dentry * dentry, struct dentry * target)
  1384. {
  1385. spin_lock(&dcache_lock);
  1386. d_move_locked(dentry, target);
  1387. spin_unlock(&dcache_lock);
  1388. }
  1389. /*
  1390. * Helper that returns 1 if p1 is a parent of p2, else 0
  1391. */
  1392. static int d_isparent(struct dentry *p1, struct dentry *p2)
  1393. {
  1394. struct dentry *p;
  1395. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1396. if (p->d_parent == p1)
  1397. return 1;
  1398. }
  1399. return 0;
  1400. }
  1401. /*
  1402. * This helper attempts to cope with remotely renamed directories
  1403. *
  1404. * It assumes that the caller is already holding
  1405. * dentry->d_parent->d_inode->i_mutex and the dcache_lock
  1406. *
  1407. * Note: If ever the locking in lock_rename() changes, then please
  1408. * remember to update this too...
  1409. *
  1410. * On return, dcache_lock will have been unlocked.
  1411. */
  1412. static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
  1413. {
  1414. struct mutex *m1 = NULL, *m2 = NULL;
  1415. struct dentry *ret;
  1416. /* If alias and dentry share a parent, then no extra locks required */
  1417. if (alias->d_parent == dentry->d_parent)
  1418. goto out_unalias;
  1419. /* Check for loops */
  1420. ret = ERR_PTR(-ELOOP);
  1421. if (d_isparent(alias, dentry))
  1422. goto out_err;
  1423. /* See lock_rename() */
  1424. ret = ERR_PTR(-EBUSY);
  1425. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  1426. goto out_err;
  1427. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  1428. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  1429. goto out_err;
  1430. m2 = &alias->d_parent->d_inode->i_mutex;
  1431. out_unalias:
  1432. d_move_locked(alias, dentry);
  1433. ret = alias;
  1434. out_err:
  1435. spin_unlock(&dcache_lock);
  1436. if (m2)
  1437. mutex_unlock(m2);
  1438. if (m1)
  1439. mutex_unlock(m1);
  1440. return ret;
  1441. }
  1442. /*
  1443. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  1444. * named dentry in place of the dentry to be replaced.
  1445. */
  1446. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  1447. {
  1448. struct dentry *dparent, *aparent;
  1449. switch_names(dentry, anon);
  1450. do_switch(dentry->d_name.len, anon->d_name.len);
  1451. do_switch(dentry->d_name.hash, anon->d_name.hash);
  1452. dparent = dentry->d_parent;
  1453. aparent = anon->d_parent;
  1454. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  1455. list_del(&dentry->d_u.d_child);
  1456. if (!IS_ROOT(dentry))
  1457. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1458. else
  1459. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1460. anon->d_parent = (dparent == dentry) ? anon : dparent;
  1461. list_del(&anon->d_u.d_child);
  1462. if (!IS_ROOT(anon))
  1463. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  1464. else
  1465. INIT_LIST_HEAD(&anon->d_u.d_child);
  1466. anon->d_flags &= ~DCACHE_DISCONNECTED;
  1467. }
  1468. /**
  1469. * d_materialise_unique - introduce an inode into the tree
  1470. * @dentry: candidate dentry
  1471. * @inode: inode to bind to the dentry, to which aliases may be attached
  1472. *
  1473. * Introduces an dentry into the tree, substituting an extant disconnected
  1474. * root directory alias in its place if there is one
  1475. */
  1476. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  1477. {
  1478. struct dentry *actual;
  1479. BUG_ON(!d_unhashed(dentry));
  1480. spin_lock(&dcache_lock);
  1481. if (!inode) {
  1482. actual = dentry;
  1483. dentry->d_inode = NULL;
  1484. goto found_lock;
  1485. }
  1486. if (S_ISDIR(inode->i_mode)) {
  1487. struct dentry *alias;
  1488. /* Does an aliased dentry already exist? */
  1489. alias = __d_find_alias(inode, 0);
  1490. if (alias) {
  1491. actual = alias;
  1492. /* Is this an anonymous mountpoint that we could splice
  1493. * into our tree? */
  1494. if (IS_ROOT(alias)) {
  1495. spin_lock(&alias->d_lock);
  1496. __d_materialise_dentry(dentry, alias);
  1497. __d_drop(alias);
  1498. goto found;
  1499. }
  1500. /* Nope, but we must(!) avoid directory aliasing */
  1501. actual = __d_unalias(dentry, alias);
  1502. if (IS_ERR(actual))
  1503. dput(alias);
  1504. goto out_nolock;
  1505. }
  1506. }
  1507. /* Add a unique reference */
  1508. actual = __d_instantiate_unique(dentry, inode);
  1509. if (!actual)
  1510. actual = dentry;
  1511. else if (unlikely(!d_unhashed(actual)))
  1512. goto shouldnt_be_hashed;
  1513. found_lock:
  1514. spin_lock(&actual->d_lock);
  1515. found:
  1516. _d_rehash(actual);
  1517. spin_unlock(&actual->d_lock);
  1518. spin_unlock(&dcache_lock);
  1519. out_nolock:
  1520. if (actual == dentry) {
  1521. security_d_instantiate(dentry, inode);
  1522. return NULL;
  1523. }
  1524. iput(inode);
  1525. return actual;
  1526. shouldnt_be_hashed:
  1527. spin_unlock(&dcache_lock);
  1528. BUG();
  1529. goto shouldnt_be_hashed;
  1530. }
  1531. /**
  1532. * d_path - return the path of a dentry
  1533. * @dentry: dentry to report
  1534. * @vfsmnt: vfsmnt to which the dentry belongs
  1535. * @root: root dentry
  1536. * @rootmnt: vfsmnt to which the root dentry belongs
  1537. * @buffer: buffer to return value in
  1538. * @buflen: buffer length
  1539. *
  1540. * Convert a dentry into an ASCII path name. If the entry has been deleted
  1541. * the string " (deleted)" is appended. Note that this is ambiguous.
  1542. *
  1543. * Returns the buffer or an error code if the path was too long.
  1544. *
  1545. * "buflen" should be positive. Caller holds the dcache_lock.
  1546. */
  1547. static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
  1548. struct dentry *root, struct vfsmount *rootmnt,
  1549. char *buffer, int buflen)
  1550. {
  1551. char * end = buffer+buflen;
  1552. char * retval;
  1553. int namelen;
  1554. *--end = '\0';
  1555. buflen--;
  1556. if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
  1557. buflen -= 10;
  1558. end -= 10;
  1559. if (buflen < 0)
  1560. goto Elong;
  1561. memcpy(end, " (deleted)", 10);
  1562. }
  1563. if (buflen < 1)
  1564. goto Elong;
  1565. /* Get '/' right */
  1566. retval = end-1;
  1567. *retval = '/';
  1568. for (;;) {
  1569. struct dentry * parent;
  1570. if (dentry == root && vfsmnt == rootmnt)
  1571. break;
  1572. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  1573. /* Global root? */
  1574. spin_lock(&vfsmount_lock);
  1575. if (vfsmnt->mnt_parent == vfsmnt) {
  1576. spin_unlock(&vfsmount_lock);
  1577. goto global_root;
  1578. }
  1579. dentry = vfsmnt->mnt_mountpoint;
  1580. vfsmnt = vfsmnt->mnt_parent;
  1581. spin_unlock(&vfsmount_lock);
  1582. continue;
  1583. }
  1584. parent = dentry->d_parent;
  1585. prefetch(parent);
  1586. namelen = dentry->d_name.len;
  1587. buflen -= namelen + 1;
  1588. if (buflen < 0)
  1589. goto Elong;
  1590. end -= namelen;
  1591. memcpy(end, dentry->d_name.name, namelen);
  1592. *--end = '/';
  1593. retval = end;
  1594. dentry = parent;
  1595. }
  1596. return retval;
  1597. global_root:
  1598. namelen = dentry->d_name.len;
  1599. buflen -= namelen;
  1600. if (buflen < 0)
  1601. goto Elong;
  1602. retval -= namelen-1; /* hit the slash */
  1603. memcpy(retval, dentry->d_name.name, namelen);
  1604. return retval;
  1605. Elong:
  1606. return ERR_PTR(-ENAMETOOLONG);
  1607. }
  1608. /* write full pathname into buffer and return start of pathname */
  1609. char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
  1610. char *buf, int buflen)
  1611. {
  1612. char *res;
  1613. struct vfsmount *rootmnt;
  1614. struct dentry *root;
  1615. read_lock(&current->fs->lock);
  1616. rootmnt = mntget(current->fs->rootmnt);
  1617. root = dget(current->fs->root);
  1618. read_unlock(&current->fs->lock);
  1619. spin_lock(&dcache_lock);
  1620. res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
  1621. spin_unlock(&dcache_lock);
  1622. dput(root);
  1623. mntput(rootmnt);
  1624. return res;
  1625. }
  1626. /*
  1627. * NOTE! The user-level library version returns a
  1628. * character pointer. The kernel system call just
  1629. * returns the length of the buffer filled (which
  1630. * includes the ending '\0' character), or a negative
  1631. * error value. So libc would do something like
  1632. *
  1633. * char *getcwd(char * buf, size_t size)
  1634. * {
  1635. * int retval;
  1636. *
  1637. * retval = sys_getcwd(buf, size);
  1638. * if (retval >= 0)
  1639. * return buf;
  1640. * errno = -retval;
  1641. * return NULL;
  1642. * }
  1643. */
  1644. asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
  1645. {
  1646. int error;
  1647. struct vfsmount *pwdmnt, *rootmnt;
  1648. struct dentry *pwd, *root;
  1649. char *page = (char *) __get_free_page(GFP_USER);
  1650. if (!page)
  1651. return -ENOMEM;
  1652. read_lock(&current->fs->lock);
  1653. pwdmnt = mntget(current->fs->pwdmnt);
  1654. pwd = dget(current->fs->pwd);
  1655. rootmnt = mntget(current->fs->rootmnt);
  1656. root = dget(current->fs->root);
  1657. read_unlock(&current->fs->lock);
  1658. error = -ENOENT;
  1659. /* Has the current directory has been unlinked? */
  1660. spin_lock(&dcache_lock);
  1661. if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
  1662. unsigned long len;
  1663. char * cwd;
  1664. cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
  1665. spin_unlock(&dcache_lock);
  1666. error = PTR_ERR(cwd);
  1667. if (IS_ERR(cwd))
  1668. goto out;
  1669. error = -ERANGE;
  1670. len = PAGE_SIZE + page - cwd;
  1671. if (len <= size) {
  1672. error = len;
  1673. if (copy_to_user(buf, cwd, len))
  1674. error = -EFAULT;
  1675. }
  1676. } else
  1677. spin_unlock(&dcache_lock);
  1678. out:
  1679. dput(pwd);
  1680. mntput(pwdmnt);
  1681. dput(root);
  1682. mntput(rootmnt);
  1683. free_page((unsigned long) page);
  1684. return error;
  1685. }
  1686. /*
  1687. * Test whether new_dentry is a subdirectory of old_dentry.
  1688. *
  1689. * Trivially implemented using the dcache structure
  1690. */
  1691. /**
  1692. * is_subdir - is new dentry a subdirectory of old_dentry
  1693. * @new_dentry: new dentry
  1694. * @old_dentry: old dentry
  1695. *
  1696. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  1697. * Returns 0 otherwise.
  1698. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  1699. */
  1700. int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
  1701. {
  1702. int result;
  1703. struct dentry * saved = new_dentry;
  1704. unsigned long seq;
  1705. /* need rcu_readlock to protect against the d_parent trashing due to
  1706. * d_move
  1707. */
  1708. rcu_read_lock();
  1709. do {
  1710. /* for restarting inner loop in case of seq retry */
  1711. new_dentry = saved;
  1712. result = 0;
  1713. seq = read_seqbegin(&rename_lock);
  1714. for (;;) {
  1715. if (new_dentry != old_dentry) {
  1716. struct dentry * parent = new_dentry->d_parent;
  1717. if (parent == new_dentry)
  1718. break;
  1719. new_dentry = parent;
  1720. continue;
  1721. }
  1722. result = 1;
  1723. break;
  1724. }
  1725. } while (read_seqretry(&rename_lock, seq));
  1726. rcu_read_unlock();
  1727. return result;
  1728. }
  1729. void d_genocide(struct dentry *root)
  1730. {
  1731. struct dentry *this_parent = root;
  1732. struct list_head *next;
  1733. spin_lock(&dcache_lock);
  1734. repeat:
  1735. next = this_parent->d_subdirs.next;
  1736. resume:
  1737. while (next != &this_parent->d_subdirs) {
  1738. struct list_head *tmp = next;
  1739. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  1740. next = tmp->next;
  1741. if (d_unhashed(dentry)||!dentry->d_inode)
  1742. continue;
  1743. if (!list_empty(&dentry->d_subdirs)) {
  1744. this_parent = dentry;
  1745. goto repeat;
  1746. }
  1747. atomic_dec(&dentry->d_count);
  1748. }
  1749. if (this_parent != root) {
  1750. next = this_parent->d_u.d_child.next;
  1751. atomic_dec(&this_parent->d_count);
  1752. this_parent = this_parent->d_parent;
  1753. goto resume;
  1754. }
  1755. spin_unlock(&dcache_lock);
  1756. }
  1757. /**
  1758. * find_inode_number - check for dentry with name
  1759. * @dir: directory to check
  1760. * @name: Name to find.
  1761. *
  1762. * Check whether a dentry already exists for the given name,
  1763. * and return the inode number if it has an inode. Otherwise
  1764. * 0 is returned.
  1765. *
  1766. * This routine is used to post-process directory listings for
  1767. * filesystems using synthetic inode numbers, and is necessary
  1768. * to keep getcwd() working.
  1769. */
  1770. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  1771. {
  1772. struct dentry * dentry;
  1773. ino_t ino = 0;
  1774. dentry = d_hash_and_lookup(dir, name);
  1775. if (dentry) {
  1776. if (dentry->d_inode)
  1777. ino = dentry->d_inode->i_ino;
  1778. dput(dentry);
  1779. }
  1780. return ino;
  1781. }
  1782. static __initdata unsigned long dhash_entries;
  1783. static int __init set_dhash_entries(char *str)
  1784. {
  1785. if (!str)
  1786. return 0;
  1787. dhash_entries = simple_strtoul(str, &str, 0);
  1788. return 1;
  1789. }
  1790. __setup("dhash_entries=", set_dhash_entries);
  1791. static void __init dcache_init_early(void)
  1792. {
  1793. int loop;
  1794. /* If hashes are distributed across NUMA nodes, defer
  1795. * hash allocation until vmalloc space is available.
  1796. */
  1797. if (hashdist)
  1798. return;
  1799. dentry_hashtable =
  1800. alloc_large_system_hash("Dentry cache",
  1801. sizeof(struct hlist_head),
  1802. dhash_entries,
  1803. 13,
  1804. HASH_EARLY,
  1805. &d_hash_shift,
  1806. &d_hash_mask,
  1807. 0);
  1808. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  1809. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  1810. }
  1811. static void __init dcache_init(unsigned long mempages)
  1812. {
  1813. int loop;
  1814. /*
  1815. * A constructor could be added for stable state like the lists,
  1816. * but it is probably not worth it because of the cache nature
  1817. * of the dcache.
  1818. */
  1819. dentry_cache = kmem_cache_create("dentry_cache",
  1820. sizeof(struct dentry),
  1821. 0,
  1822. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1823. SLAB_MEM_SPREAD),
  1824. NULL, NULL);
  1825. set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
  1826. /* Hash may have been set up in dcache_init_early */
  1827. if (!hashdist)
  1828. return;
  1829. dentry_hashtable =
  1830. alloc_large_system_hash("Dentry cache",
  1831. sizeof(struct hlist_head),
  1832. dhash_entries,
  1833. 13,
  1834. 0,
  1835. &d_hash_shift,
  1836. &d_hash_mask,
  1837. 0);
  1838. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  1839. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  1840. }
  1841. /* SLAB cache for __getname() consumers */
  1842. kmem_cache_t *names_cachep __read_mostly;
  1843. /* SLAB cache for file structures */
  1844. kmem_cache_t *filp_cachep __read_mostly;
  1845. EXPORT_SYMBOL(d_genocide);
  1846. void __init vfs_caches_init_early(void)
  1847. {
  1848. dcache_init_early();
  1849. inode_init_early();
  1850. }
  1851. void __init vfs_caches_init(unsigned long mempages)
  1852. {
  1853. unsigned long reserve;
  1854. /* Base hash sizes on available memory, with a reserve equal to
  1855. 150% of current kernel size */
  1856. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  1857. mempages -= reserve;
  1858. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  1859. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1860. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  1861. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1862. dcache_init(mempages);
  1863. inode_init(mempages);
  1864. files_init(mempages);
  1865. mnt_init(mempages);
  1866. bdev_cache_init();
  1867. chrdev_init();
  1868. }
  1869. EXPORT_SYMBOL(d_alloc);
  1870. EXPORT_SYMBOL(d_alloc_anon);
  1871. EXPORT_SYMBOL(d_alloc_root);
  1872. EXPORT_SYMBOL(d_delete);
  1873. EXPORT_SYMBOL(d_find_alias);
  1874. EXPORT_SYMBOL(d_instantiate);
  1875. EXPORT_SYMBOL(d_invalidate);
  1876. EXPORT_SYMBOL(d_lookup);
  1877. EXPORT_SYMBOL(d_move);
  1878. EXPORT_SYMBOL_GPL(d_materialise_unique);
  1879. EXPORT_SYMBOL(d_path);
  1880. EXPORT_SYMBOL(d_prune_aliases);
  1881. EXPORT_SYMBOL(d_rehash);
  1882. EXPORT_SYMBOL(d_splice_alias);
  1883. EXPORT_SYMBOL(d_validate);
  1884. EXPORT_SYMBOL(dget_locked);
  1885. EXPORT_SYMBOL(dput);
  1886. EXPORT_SYMBOL(find_inode_number);
  1887. EXPORT_SYMBOL(have_submounts);
  1888. EXPORT_SYMBOL(names_cachep);
  1889. EXPORT_SYMBOL(shrink_dcache_parent);
  1890. EXPORT_SYMBOL(shrink_dcache_sb);