dcache.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  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. BUG_ON(!IS_ROOT(dentry));
  511. /* detach this root from the system */
  512. spin_lock(&dcache_lock);
  513. if (!list_empty(&dentry->d_lru)) {
  514. dentry_stat.nr_unused--;
  515. list_del_init(&dentry->d_lru);
  516. }
  517. __d_drop(dentry);
  518. spin_unlock(&dcache_lock);
  519. for (;;) {
  520. /* descend to the first leaf in the current subtree */
  521. while (!list_empty(&dentry->d_subdirs)) {
  522. struct dentry *loop;
  523. /* this is a branch with children - detach all of them
  524. * from the system in one go */
  525. spin_lock(&dcache_lock);
  526. list_for_each_entry(loop, &dentry->d_subdirs,
  527. d_u.d_child) {
  528. if (!list_empty(&loop->d_lru)) {
  529. dentry_stat.nr_unused--;
  530. list_del_init(&loop->d_lru);
  531. }
  532. __d_drop(loop);
  533. cond_resched_lock(&dcache_lock);
  534. }
  535. spin_unlock(&dcache_lock);
  536. /* move to the first child */
  537. dentry = list_entry(dentry->d_subdirs.next,
  538. struct dentry, d_u.d_child);
  539. }
  540. /* consume the dentries from this leaf up through its parents
  541. * until we find one with children or run out altogether */
  542. do {
  543. struct inode *inode;
  544. if (atomic_read(&dentry->d_count) != 0) {
  545. printk(KERN_ERR
  546. "BUG: Dentry %p{i=%lx,n=%s}"
  547. " still in use (%d)"
  548. " [unmount of %s %s]\n",
  549. dentry,
  550. dentry->d_inode ?
  551. dentry->d_inode->i_ino : 0UL,
  552. dentry->d_name.name,
  553. atomic_read(&dentry->d_count),
  554. dentry->d_sb->s_type->name,
  555. dentry->d_sb->s_id);
  556. BUG();
  557. }
  558. parent = dentry->d_parent;
  559. if (parent == dentry)
  560. parent = NULL;
  561. else
  562. atomic_dec(&parent->d_count);
  563. list_del(&dentry->d_u.d_child);
  564. dentry_stat.nr_dentry--; /* For d_free, below */
  565. inode = dentry->d_inode;
  566. if (inode) {
  567. dentry->d_inode = NULL;
  568. list_del_init(&dentry->d_alias);
  569. if (dentry->d_op && dentry->d_op->d_iput)
  570. dentry->d_op->d_iput(dentry, inode);
  571. else
  572. iput(inode);
  573. }
  574. d_free(dentry);
  575. /* finished when we fall off the top of the tree,
  576. * otherwise we ascend to the parent and move to the
  577. * next sibling if there is one */
  578. if (!parent)
  579. return;
  580. dentry = parent;
  581. } while (list_empty(&dentry->d_subdirs));
  582. dentry = list_entry(dentry->d_subdirs.next,
  583. struct dentry, d_u.d_child);
  584. }
  585. }
  586. /*
  587. * destroy the dentries attached to a superblock on unmounting
  588. * - we don't need to use dentry->d_lock, and only need dcache_lock when
  589. * removing the dentry from the system lists and hashes because:
  590. * - the superblock is detached from all mountings and open files, so the
  591. * dentry trees will not be rearranged by the VFS
  592. * - s_umount is write-locked, so the memory pressure shrinker will ignore
  593. * any dentries belonging to this superblock that it comes across
  594. * - the filesystem itself is no longer permitted to rearrange the dentries
  595. * in this superblock
  596. */
  597. void shrink_dcache_for_umount(struct super_block *sb)
  598. {
  599. struct dentry *dentry;
  600. if (down_read_trylock(&sb->s_umount))
  601. BUG();
  602. dentry = sb->s_root;
  603. sb->s_root = NULL;
  604. atomic_dec(&dentry->d_count);
  605. shrink_dcache_for_umount_subtree(dentry);
  606. while (!hlist_empty(&sb->s_anon)) {
  607. dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
  608. shrink_dcache_for_umount_subtree(dentry);
  609. }
  610. }
  611. /*
  612. * Search for at least 1 mount point in the dentry's subdirs.
  613. * We descend to the next level whenever the d_subdirs
  614. * list is non-empty and continue searching.
  615. */
  616. /**
  617. * have_submounts - check for mounts over a dentry
  618. * @parent: dentry to check.
  619. *
  620. * Return true if the parent or its subdirectories contain
  621. * a mount point
  622. */
  623. int have_submounts(struct dentry *parent)
  624. {
  625. struct dentry *this_parent = parent;
  626. struct list_head *next;
  627. spin_lock(&dcache_lock);
  628. if (d_mountpoint(parent))
  629. goto positive;
  630. repeat:
  631. next = this_parent->d_subdirs.next;
  632. resume:
  633. while (next != &this_parent->d_subdirs) {
  634. struct list_head *tmp = next;
  635. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  636. next = tmp->next;
  637. /* Have we found a mount point ? */
  638. if (d_mountpoint(dentry))
  639. goto positive;
  640. if (!list_empty(&dentry->d_subdirs)) {
  641. this_parent = dentry;
  642. goto repeat;
  643. }
  644. }
  645. /*
  646. * All done at this level ... ascend and resume the search.
  647. */
  648. if (this_parent != parent) {
  649. next = this_parent->d_u.d_child.next;
  650. this_parent = this_parent->d_parent;
  651. goto resume;
  652. }
  653. spin_unlock(&dcache_lock);
  654. return 0; /* No mount points found in tree */
  655. positive:
  656. spin_unlock(&dcache_lock);
  657. return 1;
  658. }
  659. /*
  660. * Search the dentry child list for the specified parent,
  661. * and move any unused dentries to the end of the unused
  662. * list for prune_dcache(). We descend to the next level
  663. * whenever the d_subdirs list is non-empty and continue
  664. * searching.
  665. *
  666. * It returns zero iff there are no unused children,
  667. * otherwise it returns the number of children moved to
  668. * the end of the unused list. This may not be the total
  669. * number of unused children, because select_parent can
  670. * drop the lock and return early due to latency
  671. * constraints.
  672. */
  673. static int select_parent(struct dentry * parent)
  674. {
  675. struct dentry *this_parent = parent;
  676. struct list_head *next;
  677. int found = 0;
  678. spin_lock(&dcache_lock);
  679. repeat:
  680. next = this_parent->d_subdirs.next;
  681. resume:
  682. while (next != &this_parent->d_subdirs) {
  683. struct list_head *tmp = next;
  684. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  685. next = tmp->next;
  686. if (!list_empty(&dentry->d_lru)) {
  687. dentry_stat.nr_unused--;
  688. list_del_init(&dentry->d_lru);
  689. }
  690. /*
  691. * move only zero ref count dentries to the end
  692. * of the unused list for prune_dcache
  693. */
  694. if (!atomic_read(&dentry->d_count)) {
  695. list_add_tail(&dentry->d_lru, &dentry_unused);
  696. dentry_stat.nr_unused++;
  697. found++;
  698. }
  699. /*
  700. * We can return to the caller if we have found some (this
  701. * ensures forward progress). We'll be coming back to find
  702. * the rest.
  703. */
  704. if (found && need_resched())
  705. goto out;
  706. /*
  707. * Descend a level if the d_subdirs list is non-empty.
  708. */
  709. if (!list_empty(&dentry->d_subdirs)) {
  710. this_parent = dentry;
  711. goto repeat;
  712. }
  713. }
  714. /*
  715. * All done at this level ... ascend and resume the search.
  716. */
  717. if (this_parent != parent) {
  718. next = this_parent->d_u.d_child.next;
  719. this_parent = this_parent->d_parent;
  720. goto resume;
  721. }
  722. out:
  723. spin_unlock(&dcache_lock);
  724. return found;
  725. }
  726. /**
  727. * shrink_dcache_parent - prune dcache
  728. * @parent: parent of entries to prune
  729. *
  730. * Prune the dcache to remove unused children of the parent dentry.
  731. */
  732. void shrink_dcache_parent(struct dentry * parent)
  733. {
  734. int found;
  735. while ((found = select_parent(parent)) != 0)
  736. prune_dcache(found, parent->d_sb);
  737. }
  738. /*
  739. * Scan `nr' dentries and return the number which remain.
  740. *
  741. * We need to avoid reentering the filesystem if the caller is performing a
  742. * GFP_NOFS allocation attempt. One example deadlock is:
  743. *
  744. * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
  745. * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
  746. * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
  747. *
  748. * In this case we return -1 to tell the caller that we baled.
  749. */
  750. static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
  751. {
  752. if (nr) {
  753. if (!(gfp_mask & __GFP_FS))
  754. return -1;
  755. prune_dcache(nr, NULL);
  756. }
  757. return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  758. }
  759. /**
  760. * d_alloc - allocate a dcache entry
  761. * @parent: parent of entry to allocate
  762. * @name: qstr of the name
  763. *
  764. * Allocates a dentry. It returns %NULL if there is insufficient memory
  765. * available. On a success the dentry is returned. The name passed in is
  766. * copied and the copy passed in may be reused after this call.
  767. */
  768. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  769. {
  770. struct dentry *dentry;
  771. char *dname;
  772. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  773. if (!dentry)
  774. return NULL;
  775. if (name->len > DNAME_INLINE_LEN-1) {
  776. dname = kmalloc(name->len + 1, GFP_KERNEL);
  777. if (!dname) {
  778. kmem_cache_free(dentry_cache, dentry);
  779. return NULL;
  780. }
  781. } else {
  782. dname = dentry->d_iname;
  783. }
  784. dentry->d_name.name = dname;
  785. dentry->d_name.len = name->len;
  786. dentry->d_name.hash = name->hash;
  787. memcpy(dname, name->name, name->len);
  788. dname[name->len] = 0;
  789. atomic_set(&dentry->d_count, 1);
  790. dentry->d_flags = DCACHE_UNHASHED;
  791. spin_lock_init(&dentry->d_lock);
  792. dentry->d_inode = NULL;
  793. dentry->d_parent = NULL;
  794. dentry->d_sb = NULL;
  795. dentry->d_op = NULL;
  796. dentry->d_fsdata = NULL;
  797. dentry->d_mounted = 0;
  798. #ifdef CONFIG_PROFILING
  799. dentry->d_cookie = NULL;
  800. #endif
  801. INIT_HLIST_NODE(&dentry->d_hash);
  802. INIT_LIST_HEAD(&dentry->d_lru);
  803. INIT_LIST_HEAD(&dentry->d_subdirs);
  804. INIT_LIST_HEAD(&dentry->d_alias);
  805. if (parent) {
  806. dentry->d_parent = dget(parent);
  807. dentry->d_sb = parent->d_sb;
  808. } else {
  809. INIT_LIST_HEAD(&dentry->d_u.d_child);
  810. }
  811. spin_lock(&dcache_lock);
  812. if (parent)
  813. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  814. dentry_stat.nr_dentry++;
  815. spin_unlock(&dcache_lock);
  816. return dentry;
  817. }
  818. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  819. {
  820. struct qstr q;
  821. q.name = name;
  822. q.len = strlen(name);
  823. q.hash = full_name_hash(q.name, q.len);
  824. return d_alloc(parent, &q);
  825. }
  826. /**
  827. * d_instantiate - fill in inode information for a dentry
  828. * @entry: dentry to complete
  829. * @inode: inode to attach to this dentry
  830. *
  831. * Fill in inode information in the entry.
  832. *
  833. * This turns negative dentries into productive full members
  834. * of society.
  835. *
  836. * NOTE! This assumes that the inode count has been incremented
  837. * (or otherwise set) by the caller to indicate that it is now
  838. * in use by the dcache.
  839. */
  840. void d_instantiate(struct dentry *entry, struct inode * inode)
  841. {
  842. BUG_ON(!list_empty(&entry->d_alias));
  843. spin_lock(&dcache_lock);
  844. if (inode)
  845. list_add(&entry->d_alias, &inode->i_dentry);
  846. entry->d_inode = inode;
  847. fsnotify_d_instantiate(entry, inode);
  848. spin_unlock(&dcache_lock);
  849. security_d_instantiate(entry, inode);
  850. }
  851. /**
  852. * d_instantiate_unique - instantiate a non-aliased dentry
  853. * @entry: dentry to instantiate
  854. * @inode: inode to attach to this dentry
  855. *
  856. * Fill in inode information in the entry. On success, it returns NULL.
  857. * If an unhashed alias of "entry" already exists, then we return the
  858. * aliased dentry instead and drop one reference to inode.
  859. *
  860. * Note that in order to avoid conflicts with rename() etc, the caller
  861. * had better be holding the parent directory semaphore.
  862. *
  863. * This also assumes that the inode count has been incremented
  864. * (or otherwise set) by the caller to indicate that it is now
  865. * in use by the dcache.
  866. */
  867. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  868. struct inode *inode)
  869. {
  870. struct dentry *alias;
  871. int len = entry->d_name.len;
  872. const char *name = entry->d_name.name;
  873. unsigned int hash = entry->d_name.hash;
  874. if (!inode) {
  875. entry->d_inode = NULL;
  876. return NULL;
  877. }
  878. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  879. struct qstr *qstr = &alias->d_name;
  880. if (qstr->hash != hash)
  881. continue;
  882. if (alias->d_parent != entry->d_parent)
  883. continue;
  884. if (qstr->len != len)
  885. continue;
  886. if (memcmp(qstr->name, name, len))
  887. continue;
  888. dget_locked(alias);
  889. return alias;
  890. }
  891. list_add(&entry->d_alias, &inode->i_dentry);
  892. entry->d_inode = inode;
  893. fsnotify_d_instantiate(entry, inode);
  894. return NULL;
  895. }
  896. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  897. {
  898. struct dentry *result;
  899. BUG_ON(!list_empty(&entry->d_alias));
  900. spin_lock(&dcache_lock);
  901. result = __d_instantiate_unique(entry, inode);
  902. spin_unlock(&dcache_lock);
  903. if (!result) {
  904. security_d_instantiate(entry, inode);
  905. return NULL;
  906. }
  907. BUG_ON(!d_unhashed(result));
  908. iput(inode);
  909. return result;
  910. }
  911. EXPORT_SYMBOL(d_instantiate_unique);
  912. /**
  913. * d_alloc_root - allocate root dentry
  914. * @root_inode: inode to allocate the root for
  915. *
  916. * Allocate a root ("/") dentry for the inode given. The inode is
  917. * instantiated and returned. %NULL is returned if there is insufficient
  918. * memory or the inode passed is %NULL.
  919. */
  920. struct dentry * d_alloc_root(struct inode * root_inode)
  921. {
  922. struct dentry *res = NULL;
  923. if (root_inode) {
  924. static const struct qstr name = { .name = "/", .len = 1 };
  925. res = d_alloc(NULL, &name);
  926. if (res) {
  927. res->d_sb = root_inode->i_sb;
  928. res->d_parent = res;
  929. d_instantiate(res, root_inode);
  930. }
  931. }
  932. return res;
  933. }
  934. static inline struct hlist_head *d_hash(struct dentry *parent,
  935. unsigned long hash)
  936. {
  937. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  938. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  939. return dentry_hashtable + (hash & D_HASHMASK);
  940. }
  941. /**
  942. * d_alloc_anon - allocate an anonymous dentry
  943. * @inode: inode to allocate the dentry for
  944. *
  945. * This is similar to d_alloc_root. It is used by filesystems when
  946. * creating a dentry for a given inode, often in the process of
  947. * mapping a filehandle to a dentry. The returned dentry may be
  948. * anonymous, or may have a full name (if the inode was already
  949. * in the cache). The file system may need to make further
  950. * efforts to connect this dentry into the dcache properly.
  951. *
  952. * When called on a directory inode, we must ensure that
  953. * the inode only ever has one dentry. If a dentry is
  954. * found, that is returned instead of allocating a new one.
  955. *
  956. * On successful return, the reference to the inode has been transferred
  957. * to the dentry. If %NULL is returned (indicating kmalloc failure),
  958. * the reference on the inode has not been released.
  959. */
  960. struct dentry * d_alloc_anon(struct inode *inode)
  961. {
  962. static const struct qstr anonstring = { .name = "" };
  963. struct dentry *tmp;
  964. struct dentry *res;
  965. if ((res = d_find_alias(inode))) {
  966. iput(inode);
  967. return res;
  968. }
  969. tmp = d_alloc(NULL, &anonstring);
  970. if (!tmp)
  971. return NULL;
  972. tmp->d_parent = tmp; /* make sure dput doesn't croak */
  973. spin_lock(&dcache_lock);
  974. res = __d_find_alias(inode, 0);
  975. if (!res) {
  976. /* attach a disconnected dentry */
  977. res = tmp;
  978. tmp = NULL;
  979. spin_lock(&res->d_lock);
  980. res->d_sb = inode->i_sb;
  981. res->d_parent = res;
  982. res->d_inode = inode;
  983. res->d_flags |= DCACHE_DISCONNECTED;
  984. res->d_flags &= ~DCACHE_UNHASHED;
  985. list_add(&res->d_alias, &inode->i_dentry);
  986. hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
  987. spin_unlock(&res->d_lock);
  988. inode = NULL; /* don't drop reference */
  989. }
  990. spin_unlock(&dcache_lock);
  991. if (inode)
  992. iput(inode);
  993. if (tmp)
  994. dput(tmp);
  995. return res;
  996. }
  997. /**
  998. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  999. * @inode: the inode which may have a disconnected dentry
  1000. * @dentry: a negative dentry which we want to point to the inode.
  1001. *
  1002. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1003. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1004. * and return it, else simply d_add the inode to the dentry and return NULL.
  1005. *
  1006. * This is needed in the lookup routine of any filesystem that is exportable
  1007. * (via knfsd) so that we can build dcache paths to directories effectively.
  1008. *
  1009. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1010. * is returned. This matches the expected return value of ->lookup.
  1011. *
  1012. */
  1013. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1014. {
  1015. struct dentry *new = NULL;
  1016. if (inode && S_ISDIR(inode->i_mode)) {
  1017. spin_lock(&dcache_lock);
  1018. new = __d_find_alias(inode, 1);
  1019. if (new) {
  1020. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  1021. fsnotify_d_instantiate(new, inode);
  1022. spin_unlock(&dcache_lock);
  1023. security_d_instantiate(new, inode);
  1024. d_rehash(dentry);
  1025. d_move(new, dentry);
  1026. iput(inode);
  1027. } else {
  1028. /* d_instantiate takes dcache_lock, so we do it by hand */
  1029. list_add(&dentry->d_alias, &inode->i_dentry);
  1030. dentry->d_inode = inode;
  1031. fsnotify_d_instantiate(dentry, inode);
  1032. spin_unlock(&dcache_lock);
  1033. security_d_instantiate(dentry, inode);
  1034. d_rehash(dentry);
  1035. }
  1036. } else
  1037. d_add(dentry, inode);
  1038. return new;
  1039. }
  1040. /**
  1041. * d_lookup - search for a dentry
  1042. * @parent: parent dentry
  1043. * @name: qstr of name we wish to find
  1044. *
  1045. * Searches the children of the parent dentry for the name in question. If
  1046. * the dentry is found its reference count is incremented and the dentry
  1047. * is returned. The caller must use d_put to free the entry when it has
  1048. * finished using it. %NULL is returned on failure.
  1049. *
  1050. * __d_lookup is dcache_lock free. The hash list is protected using RCU.
  1051. * Memory barriers are used while updating and doing lockless traversal.
  1052. * To avoid races with d_move while rename is happening, d_lock is used.
  1053. *
  1054. * Overflows in memcmp(), while d_move, are avoided by keeping the length
  1055. * and name pointer in one structure pointed by d_qstr.
  1056. *
  1057. * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
  1058. * lookup is going on.
  1059. *
  1060. * dentry_unused list is not updated even if lookup finds the required dentry
  1061. * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
  1062. * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
  1063. * acquisition.
  1064. *
  1065. * d_lookup() is protected against the concurrent renames in some unrelated
  1066. * directory using the seqlockt_t rename_lock.
  1067. */
  1068. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  1069. {
  1070. struct dentry * dentry = NULL;
  1071. unsigned long seq;
  1072. do {
  1073. seq = read_seqbegin(&rename_lock);
  1074. dentry = __d_lookup(parent, name);
  1075. if (dentry)
  1076. break;
  1077. } while (read_seqretry(&rename_lock, seq));
  1078. return dentry;
  1079. }
  1080. struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
  1081. {
  1082. unsigned int len = name->len;
  1083. unsigned int hash = name->hash;
  1084. const unsigned char *str = name->name;
  1085. struct hlist_head *head = d_hash(parent,hash);
  1086. struct dentry *found = NULL;
  1087. struct hlist_node *node;
  1088. struct dentry *dentry;
  1089. rcu_read_lock();
  1090. hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
  1091. struct qstr *qstr;
  1092. if (dentry->d_name.hash != hash)
  1093. continue;
  1094. if (dentry->d_parent != parent)
  1095. continue;
  1096. spin_lock(&dentry->d_lock);
  1097. /*
  1098. * Recheck the dentry after taking the lock - d_move may have
  1099. * changed things. Don't bother checking the hash because we're
  1100. * about to compare the whole name anyway.
  1101. */
  1102. if (dentry->d_parent != parent)
  1103. goto next;
  1104. /*
  1105. * It is safe to compare names since d_move() cannot
  1106. * change the qstr (protected by d_lock).
  1107. */
  1108. qstr = &dentry->d_name;
  1109. if (parent->d_op && parent->d_op->d_compare) {
  1110. if (parent->d_op->d_compare(parent, qstr, name))
  1111. goto next;
  1112. } else {
  1113. if (qstr->len != len)
  1114. goto next;
  1115. if (memcmp(qstr->name, str, len))
  1116. goto next;
  1117. }
  1118. if (!d_unhashed(dentry)) {
  1119. atomic_inc(&dentry->d_count);
  1120. found = dentry;
  1121. }
  1122. spin_unlock(&dentry->d_lock);
  1123. break;
  1124. next:
  1125. spin_unlock(&dentry->d_lock);
  1126. }
  1127. rcu_read_unlock();
  1128. return found;
  1129. }
  1130. /**
  1131. * d_hash_and_lookup - hash the qstr then search for a dentry
  1132. * @dir: Directory to search in
  1133. * @name: qstr of name we wish to find
  1134. *
  1135. * On hash failure or on lookup failure NULL is returned.
  1136. */
  1137. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1138. {
  1139. struct dentry *dentry = NULL;
  1140. /*
  1141. * Check for a fs-specific hash function. Note that we must
  1142. * calculate the standard hash first, as the d_op->d_hash()
  1143. * routine may choose to leave the hash value unchanged.
  1144. */
  1145. name->hash = full_name_hash(name->name, name->len);
  1146. if (dir->d_op && dir->d_op->d_hash) {
  1147. if (dir->d_op->d_hash(dir, name) < 0)
  1148. goto out;
  1149. }
  1150. dentry = d_lookup(dir, name);
  1151. out:
  1152. return dentry;
  1153. }
  1154. /**
  1155. * d_validate - verify dentry provided from insecure source
  1156. * @dentry: The dentry alleged to be valid child of @dparent
  1157. * @dparent: The parent dentry (known to be valid)
  1158. * @hash: Hash of the dentry
  1159. * @len: Length of the name
  1160. *
  1161. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1162. * This is used by ncpfs in its readdir implementation.
  1163. * Zero is returned in the dentry is invalid.
  1164. */
  1165. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1166. {
  1167. struct hlist_head *base;
  1168. struct hlist_node *lhp;
  1169. /* Check whether the ptr might be valid at all.. */
  1170. if (!kmem_ptr_validate(dentry_cache, dentry))
  1171. goto out;
  1172. if (dentry->d_parent != dparent)
  1173. goto out;
  1174. spin_lock(&dcache_lock);
  1175. base = d_hash(dparent, dentry->d_name.hash);
  1176. hlist_for_each(lhp,base) {
  1177. /* hlist_for_each_entry_rcu() not required for d_hash list
  1178. * as it is parsed under dcache_lock
  1179. */
  1180. if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
  1181. __dget_locked(dentry);
  1182. spin_unlock(&dcache_lock);
  1183. return 1;
  1184. }
  1185. }
  1186. spin_unlock(&dcache_lock);
  1187. out:
  1188. return 0;
  1189. }
  1190. /*
  1191. * When a file is deleted, we have two options:
  1192. * - turn this dentry into a negative dentry
  1193. * - unhash this dentry and free it.
  1194. *
  1195. * Usually, we want to just turn this into
  1196. * a negative dentry, but if anybody else is
  1197. * currently using the dentry or the inode
  1198. * we can't do that and we fall back on removing
  1199. * it from the hash queues and waiting for
  1200. * it to be deleted later when it has no users
  1201. */
  1202. /**
  1203. * d_delete - delete a dentry
  1204. * @dentry: The dentry to delete
  1205. *
  1206. * Turn the dentry into a negative dentry if possible, otherwise
  1207. * remove it from the hash queues so it can be deleted later
  1208. */
  1209. void d_delete(struct dentry * dentry)
  1210. {
  1211. int isdir = 0;
  1212. /*
  1213. * Are we the only user?
  1214. */
  1215. spin_lock(&dcache_lock);
  1216. spin_lock(&dentry->d_lock);
  1217. isdir = S_ISDIR(dentry->d_inode->i_mode);
  1218. if (atomic_read(&dentry->d_count) == 1) {
  1219. dentry_iput(dentry);
  1220. fsnotify_nameremove(dentry, isdir);
  1221. /* remove this and other inotify debug checks after 2.6.18 */
  1222. dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
  1223. return;
  1224. }
  1225. if (!d_unhashed(dentry))
  1226. __d_drop(dentry);
  1227. spin_unlock(&dentry->d_lock);
  1228. spin_unlock(&dcache_lock);
  1229. fsnotify_nameremove(dentry, isdir);
  1230. }
  1231. static void __d_rehash(struct dentry * entry, struct hlist_head *list)
  1232. {
  1233. entry->d_flags &= ~DCACHE_UNHASHED;
  1234. hlist_add_head_rcu(&entry->d_hash, list);
  1235. }
  1236. static void _d_rehash(struct dentry * entry)
  1237. {
  1238. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1239. }
  1240. /**
  1241. * d_rehash - add an entry back to the hash
  1242. * @entry: dentry to add to the hash
  1243. *
  1244. * Adds a dentry to the hash according to its name.
  1245. */
  1246. void d_rehash(struct dentry * entry)
  1247. {
  1248. spin_lock(&dcache_lock);
  1249. spin_lock(&entry->d_lock);
  1250. _d_rehash(entry);
  1251. spin_unlock(&entry->d_lock);
  1252. spin_unlock(&dcache_lock);
  1253. }
  1254. #define do_switch(x,y) do { \
  1255. __typeof__ (x) __tmp = x; \
  1256. x = y; y = __tmp; } while (0)
  1257. /*
  1258. * When switching names, the actual string doesn't strictly have to
  1259. * be preserved in the target - because we're dropping the target
  1260. * anyway. As such, we can just do a simple memcpy() to copy over
  1261. * the new name before we switch.
  1262. *
  1263. * Note that we have to be a lot more careful about getting the hash
  1264. * switched - we have to switch the hash value properly even if it
  1265. * then no longer matches the actual (corrupted) string of the target.
  1266. * The hash value has to match the hash queue that the dentry is on..
  1267. */
  1268. static void switch_names(struct dentry *dentry, struct dentry *target)
  1269. {
  1270. if (dname_external(target)) {
  1271. if (dname_external(dentry)) {
  1272. /*
  1273. * Both external: swap the pointers
  1274. */
  1275. do_switch(target->d_name.name, dentry->d_name.name);
  1276. } else {
  1277. /*
  1278. * dentry:internal, target:external. Steal target's
  1279. * storage and make target internal.
  1280. */
  1281. dentry->d_name.name = target->d_name.name;
  1282. target->d_name.name = target->d_iname;
  1283. }
  1284. } else {
  1285. if (dname_external(dentry)) {
  1286. /*
  1287. * dentry:external, target:internal. Give dentry's
  1288. * storage to target and make dentry internal
  1289. */
  1290. memcpy(dentry->d_iname, target->d_name.name,
  1291. target->d_name.len + 1);
  1292. target->d_name.name = dentry->d_name.name;
  1293. dentry->d_name.name = dentry->d_iname;
  1294. } else {
  1295. /*
  1296. * Both are internal. Just copy target to dentry
  1297. */
  1298. memcpy(dentry->d_iname, target->d_name.name,
  1299. target->d_name.len + 1);
  1300. }
  1301. }
  1302. }
  1303. /*
  1304. * We cannibalize "target" when moving dentry on top of it,
  1305. * because it's going to be thrown away anyway. We could be more
  1306. * polite about it, though.
  1307. *
  1308. * This forceful removal will result in ugly /proc output if
  1309. * somebody holds a file open that got deleted due to a rename.
  1310. * We could be nicer about the deleted file, and let it show
  1311. * up under the name it got deleted rather than the name that
  1312. * deleted it.
  1313. */
  1314. /*
  1315. * d_move_locked - move a dentry
  1316. * @dentry: entry to move
  1317. * @target: new dentry
  1318. *
  1319. * Update the dcache to reflect the move of a file name. Negative
  1320. * dcache entries should not be moved in this way.
  1321. */
  1322. static void d_move_locked(struct dentry * dentry, struct dentry * target)
  1323. {
  1324. struct hlist_head *list;
  1325. if (!dentry->d_inode)
  1326. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  1327. write_seqlock(&rename_lock);
  1328. /*
  1329. * XXXX: do we really need to take target->d_lock?
  1330. */
  1331. if (target < dentry) {
  1332. spin_lock(&target->d_lock);
  1333. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1334. } else {
  1335. spin_lock(&dentry->d_lock);
  1336. spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
  1337. }
  1338. /* Move the dentry to the target hash queue, if on different bucket */
  1339. if (dentry->d_flags & DCACHE_UNHASHED)
  1340. goto already_unhashed;
  1341. hlist_del_rcu(&dentry->d_hash);
  1342. already_unhashed:
  1343. list = d_hash(target->d_parent, target->d_name.hash);
  1344. __d_rehash(dentry, list);
  1345. /* Unhash the target: dput() will then get rid of it */
  1346. __d_drop(target);
  1347. list_del(&dentry->d_u.d_child);
  1348. list_del(&target->d_u.d_child);
  1349. /* Switch the names.. */
  1350. switch_names(dentry, target);
  1351. do_switch(dentry->d_name.len, target->d_name.len);
  1352. do_switch(dentry->d_name.hash, target->d_name.hash);
  1353. /* ... and switch the parents */
  1354. if (IS_ROOT(dentry)) {
  1355. dentry->d_parent = target->d_parent;
  1356. target->d_parent = target;
  1357. INIT_LIST_HEAD(&target->d_u.d_child);
  1358. } else {
  1359. do_switch(dentry->d_parent, target->d_parent);
  1360. /* And add them back to the (new) parent lists */
  1361. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  1362. }
  1363. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1364. spin_unlock(&target->d_lock);
  1365. fsnotify_d_move(dentry);
  1366. spin_unlock(&dentry->d_lock);
  1367. write_sequnlock(&rename_lock);
  1368. }
  1369. /**
  1370. * d_move - move a dentry
  1371. * @dentry: entry to move
  1372. * @target: new dentry
  1373. *
  1374. * Update the dcache to reflect the move of a file name. Negative
  1375. * dcache entries should not be moved in this way.
  1376. */
  1377. void d_move(struct dentry * dentry, struct dentry * target)
  1378. {
  1379. spin_lock(&dcache_lock);
  1380. d_move_locked(dentry, target);
  1381. spin_unlock(&dcache_lock);
  1382. }
  1383. /*
  1384. * Helper that returns 1 if p1 is a parent of p2, else 0
  1385. */
  1386. static int d_isparent(struct dentry *p1, struct dentry *p2)
  1387. {
  1388. struct dentry *p;
  1389. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1390. if (p->d_parent == p1)
  1391. return 1;
  1392. }
  1393. return 0;
  1394. }
  1395. /*
  1396. * This helper attempts to cope with remotely renamed directories
  1397. *
  1398. * It assumes that the caller is already holding
  1399. * dentry->d_parent->d_inode->i_mutex and the dcache_lock
  1400. *
  1401. * Note: If ever the locking in lock_rename() changes, then please
  1402. * remember to update this too...
  1403. *
  1404. * On return, dcache_lock will have been unlocked.
  1405. */
  1406. static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
  1407. {
  1408. struct mutex *m1 = NULL, *m2 = NULL;
  1409. struct dentry *ret;
  1410. /* If alias and dentry share a parent, then no extra locks required */
  1411. if (alias->d_parent == dentry->d_parent)
  1412. goto out_unalias;
  1413. /* Check for loops */
  1414. ret = ERR_PTR(-ELOOP);
  1415. if (d_isparent(alias, dentry))
  1416. goto out_err;
  1417. /* See lock_rename() */
  1418. ret = ERR_PTR(-EBUSY);
  1419. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  1420. goto out_err;
  1421. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  1422. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  1423. goto out_err;
  1424. m2 = &alias->d_parent->d_inode->i_mutex;
  1425. out_unalias:
  1426. d_move_locked(alias, dentry);
  1427. ret = alias;
  1428. out_err:
  1429. spin_unlock(&dcache_lock);
  1430. if (m2)
  1431. mutex_unlock(m2);
  1432. if (m1)
  1433. mutex_unlock(m1);
  1434. return ret;
  1435. }
  1436. /*
  1437. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  1438. * named dentry in place of the dentry to be replaced.
  1439. */
  1440. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  1441. {
  1442. struct dentry *dparent, *aparent;
  1443. switch_names(dentry, anon);
  1444. do_switch(dentry->d_name.len, anon->d_name.len);
  1445. do_switch(dentry->d_name.hash, anon->d_name.hash);
  1446. dparent = dentry->d_parent;
  1447. aparent = anon->d_parent;
  1448. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  1449. list_del(&dentry->d_u.d_child);
  1450. if (!IS_ROOT(dentry))
  1451. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1452. else
  1453. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1454. anon->d_parent = (dparent == dentry) ? anon : dparent;
  1455. list_del(&anon->d_u.d_child);
  1456. if (!IS_ROOT(anon))
  1457. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  1458. else
  1459. INIT_LIST_HEAD(&anon->d_u.d_child);
  1460. anon->d_flags &= ~DCACHE_DISCONNECTED;
  1461. }
  1462. /**
  1463. * d_materialise_unique - introduce an inode into the tree
  1464. * @dentry: candidate dentry
  1465. * @inode: inode to bind to the dentry, to which aliases may be attached
  1466. *
  1467. * Introduces an dentry into the tree, substituting an extant disconnected
  1468. * root directory alias in its place if there is one
  1469. */
  1470. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  1471. {
  1472. struct dentry *actual;
  1473. BUG_ON(!d_unhashed(dentry));
  1474. spin_lock(&dcache_lock);
  1475. if (!inode) {
  1476. actual = dentry;
  1477. dentry->d_inode = NULL;
  1478. goto found_lock;
  1479. }
  1480. if (S_ISDIR(inode->i_mode)) {
  1481. struct dentry *alias;
  1482. /* Does an aliased dentry already exist? */
  1483. alias = __d_find_alias(inode, 0);
  1484. if (alias) {
  1485. actual = alias;
  1486. /* Is this an anonymous mountpoint that we could splice
  1487. * into our tree? */
  1488. if (IS_ROOT(alias)) {
  1489. spin_lock(&alias->d_lock);
  1490. __d_materialise_dentry(dentry, alias);
  1491. __d_drop(alias);
  1492. goto found;
  1493. }
  1494. /* Nope, but we must(!) avoid directory aliasing */
  1495. actual = __d_unalias(dentry, alias);
  1496. if (IS_ERR(actual))
  1497. dput(alias);
  1498. goto out_nolock;
  1499. }
  1500. }
  1501. /* Add a unique reference */
  1502. actual = __d_instantiate_unique(dentry, inode);
  1503. if (!actual)
  1504. actual = dentry;
  1505. else if (unlikely(!d_unhashed(actual)))
  1506. goto shouldnt_be_hashed;
  1507. found_lock:
  1508. spin_lock(&actual->d_lock);
  1509. found:
  1510. _d_rehash(actual);
  1511. spin_unlock(&actual->d_lock);
  1512. spin_unlock(&dcache_lock);
  1513. out_nolock:
  1514. if (actual == dentry) {
  1515. security_d_instantiate(dentry, inode);
  1516. return NULL;
  1517. }
  1518. iput(inode);
  1519. return actual;
  1520. shouldnt_be_hashed:
  1521. spin_unlock(&dcache_lock);
  1522. BUG();
  1523. goto shouldnt_be_hashed;
  1524. }
  1525. /**
  1526. * d_path - return the path of a dentry
  1527. * @dentry: dentry to report
  1528. * @vfsmnt: vfsmnt to which the dentry belongs
  1529. * @root: root dentry
  1530. * @rootmnt: vfsmnt to which the root dentry belongs
  1531. * @buffer: buffer to return value in
  1532. * @buflen: buffer length
  1533. *
  1534. * Convert a dentry into an ASCII path name. If the entry has been deleted
  1535. * the string " (deleted)" is appended. Note that this is ambiguous.
  1536. *
  1537. * Returns the buffer or an error code if the path was too long.
  1538. *
  1539. * "buflen" should be positive. Caller holds the dcache_lock.
  1540. */
  1541. static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
  1542. struct dentry *root, struct vfsmount *rootmnt,
  1543. char *buffer, int buflen)
  1544. {
  1545. char * end = buffer+buflen;
  1546. char * retval;
  1547. int namelen;
  1548. *--end = '\0';
  1549. buflen--;
  1550. if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
  1551. buflen -= 10;
  1552. end -= 10;
  1553. if (buflen < 0)
  1554. goto Elong;
  1555. memcpy(end, " (deleted)", 10);
  1556. }
  1557. if (buflen < 1)
  1558. goto Elong;
  1559. /* Get '/' right */
  1560. retval = end-1;
  1561. *retval = '/';
  1562. for (;;) {
  1563. struct dentry * parent;
  1564. if (dentry == root && vfsmnt == rootmnt)
  1565. break;
  1566. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  1567. /* Global root? */
  1568. spin_lock(&vfsmount_lock);
  1569. if (vfsmnt->mnt_parent == vfsmnt) {
  1570. spin_unlock(&vfsmount_lock);
  1571. goto global_root;
  1572. }
  1573. dentry = vfsmnt->mnt_mountpoint;
  1574. vfsmnt = vfsmnt->mnt_parent;
  1575. spin_unlock(&vfsmount_lock);
  1576. continue;
  1577. }
  1578. parent = dentry->d_parent;
  1579. prefetch(parent);
  1580. namelen = dentry->d_name.len;
  1581. buflen -= namelen + 1;
  1582. if (buflen < 0)
  1583. goto Elong;
  1584. end -= namelen;
  1585. memcpy(end, dentry->d_name.name, namelen);
  1586. *--end = '/';
  1587. retval = end;
  1588. dentry = parent;
  1589. }
  1590. return retval;
  1591. global_root:
  1592. namelen = dentry->d_name.len;
  1593. buflen -= namelen;
  1594. if (buflen < 0)
  1595. goto Elong;
  1596. retval -= namelen-1; /* hit the slash */
  1597. memcpy(retval, dentry->d_name.name, namelen);
  1598. return retval;
  1599. Elong:
  1600. return ERR_PTR(-ENAMETOOLONG);
  1601. }
  1602. /* write full pathname into buffer and return start of pathname */
  1603. char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
  1604. char *buf, int buflen)
  1605. {
  1606. char *res;
  1607. struct vfsmount *rootmnt;
  1608. struct dentry *root;
  1609. read_lock(&current->fs->lock);
  1610. rootmnt = mntget(current->fs->rootmnt);
  1611. root = dget(current->fs->root);
  1612. read_unlock(&current->fs->lock);
  1613. spin_lock(&dcache_lock);
  1614. res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
  1615. spin_unlock(&dcache_lock);
  1616. dput(root);
  1617. mntput(rootmnt);
  1618. return res;
  1619. }
  1620. /*
  1621. * NOTE! The user-level library version returns a
  1622. * character pointer. The kernel system call just
  1623. * returns the length of the buffer filled (which
  1624. * includes the ending '\0' character), or a negative
  1625. * error value. So libc would do something like
  1626. *
  1627. * char *getcwd(char * buf, size_t size)
  1628. * {
  1629. * int retval;
  1630. *
  1631. * retval = sys_getcwd(buf, size);
  1632. * if (retval >= 0)
  1633. * return buf;
  1634. * errno = -retval;
  1635. * return NULL;
  1636. * }
  1637. */
  1638. asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
  1639. {
  1640. int error;
  1641. struct vfsmount *pwdmnt, *rootmnt;
  1642. struct dentry *pwd, *root;
  1643. char *page = (char *) __get_free_page(GFP_USER);
  1644. if (!page)
  1645. return -ENOMEM;
  1646. read_lock(&current->fs->lock);
  1647. pwdmnt = mntget(current->fs->pwdmnt);
  1648. pwd = dget(current->fs->pwd);
  1649. rootmnt = mntget(current->fs->rootmnt);
  1650. root = dget(current->fs->root);
  1651. read_unlock(&current->fs->lock);
  1652. error = -ENOENT;
  1653. /* Has the current directory has been unlinked? */
  1654. spin_lock(&dcache_lock);
  1655. if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
  1656. unsigned long len;
  1657. char * cwd;
  1658. cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
  1659. spin_unlock(&dcache_lock);
  1660. error = PTR_ERR(cwd);
  1661. if (IS_ERR(cwd))
  1662. goto out;
  1663. error = -ERANGE;
  1664. len = PAGE_SIZE + page - cwd;
  1665. if (len <= size) {
  1666. error = len;
  1667. if (copy_to_user(buf, cwd, len))
  1668. error = -EFAULT;
  1669. }
  1670. } else
  1671. spin_unlock(&dcache_lock);
  1672. out:
  1673. dput(pwd);
  1674. mntput(pwdmnt);
  1675. dput(root);
  1676. mntput(rootmnt);
  1677. free_page((unsigned long) page);
  1678. return error;
  1679. }
  1680. /*
  1681. * Test whether new_dentry is a subdirectory of old_dentry.
  1682. *
  1683. * Trivially implemented using the dcache structure
  1684. */
  1685. /**
  1686. * is_subdir - is new dentry a subdirectory of old_dentry
  1687. * @new_dentry: new dentry
  1688. * @old_dentry: old dentry
  1689. *
  1690. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  1691. * Returns 0 otherwise.
  1692. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  1693. */
  1694. int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
  1695. {
  1696. int result;
  1697. struct dentry * saved = new_dentry;
  1698. unsigned long seq;
  1699. /* need rcu_readlock to protect against the d_parent trashing due to
  1700. * d_move
  1701. */
  1702. rcu_read_lock();
  1703. do {
  1704. /* for restarting inner loop in case of seq retry */
  1705. new_dentry = saved;
  1706. result = 0;
  1707. seq = read_seqbegin(&rename_lock);
  1708. for (;;) {
  1709. if (new_dentry != old_dentry) {
  1710. struct dentry * parent = new_dentry->d_parent;
  1711. if (parent == new_dentry)
  1712. break;
  1713. new_dentry = parent;
  1714. continue;
  1715. }
  1716. result = 1;
  1717. break;
  1718. }
  1719. } while (read_seqretry(&rename_lock, seq));
  1720. rcu_read_unlock();
  1721. return result;
  1722. }
  1723. void d_genocide(struct dentry *root)
  1724. {
  1725. struct dentry *this_parent = root;
  1726. struct list_head *next;
  1727. spin_lock(&dcache_lock);
  1728. repeat:
  1729. next = this_parent->d_subdirs.next;
  1730. resume:
  1731. while (next != &this_parent->d_subdirs) {
  1732. struct list_head *tmp = next;
  1733. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  1734. next = tmp->next;
  1735. if (d_unhashed(dentry)||!dentry->d_inode)
  1736. continue;
  1737. if (!list_empty(&dentry->d_subdirs)) {
  1738. this_parent = dentry;
  1739. goto repeat;
  1740. }
  1741. atomic_dec(&dentry->d_count);
  1742. }
  1743. if (this_parent != root) {
  1744. next = this_parent->d_u.d_child.next;
  1745. atomic_dec(&this_parent->d_count);
  1746. this_parent = this_parent->d_parent;
  1747. goto resume;
  1748. }
  1749. spin_unlock(&dcache_lock);
  1750. }
  1751. /**
  1752. * find_inode_number - check for dentry with name
  1753. * @dir: directory to check
  1754. * @name: Name to find.
  1755. *
  1756. * Check whether a dentry already exists for the given name,
  1757. * and return the inode number if it has an inode. Otherwise
  1758. * 0 is returned.
  1759. *
  1760. * This routine is used to post-process directory listings for
  1761. * filesystems using synthetic inode numbers, and is necessary
  1762. * to keep getcwd() working.
  1763. */
  1764. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  1765. {
  1766. struct dentry * dentry;
  1767. ino_t ino = 0;
  1768. dentry = d_hash_and_lookup(dir, name);
  1769. if (dentry) {
  1770. if (dentry->d_inode)
  1771. ino = dentry->d_inode->i_ino;
  1772. dput(dentry);
  1773. }
  1774. return ino;
  1775. }
  1776. static __initdata unsigned long dhash_entries;
  1777. static int __init set_dhash_entries(char *str)
  1778. {
  1779. if (!str)
  1780. return 0;
  1781. dhash_entries = simple_strtoul(str, &str, 0);
  1782. return 1;
  1783. }
  1784. __setup("dhash_entries=", set_dhash_entries);
  1785. static void __init dcache_init_early(void)
  1786. {
  1787. int loop;
  1788. /* If hashes are distributed across NUMA nodes, defer
  1789. * hash allocation until vmalloc space is available.
  1790. */
  1791. if (hashdist)
  1792. return;
  1793. dentry_hashtable =
  1794. alloc_large_system_hash("Dentry cache",
  1795. sizeof(struct hlist_head),
  1796. dhash_entries,
  1797. 13,
  1798. HASH_EARLY,
  1799. &d_hash_shift,
  1800. &d_hash_mask,
  1801. 0);
  1802. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  1803. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  1804. }
  1805. static void __init dcache_init(unsigned long mempages)
  1806. {
  1807. int loop;
  1808. /*
  1809. * A constructor could be added for stable state like the lists,
  1810. * but it is probably not worth it because of the cache nature
  1811. * of the dcache.
  1812. */
  1813. dentry_cache = kmem_cache_create("dentry_cache",
  1814. sizeof(struct dentry),
  1815. 0,
  1816. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1817. SLAB_MEM_SPREAD),
  1818. NULL, NULL);
  1819. set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
  1820. /* Hash may have been set up in dcache_init_early */
  1821. if (!hashdist)
  1822. return;
  1823. dentry_hashtable =
  1824. alloc_large_system_hash("Dentry cache",
  1825. sizeof(struct hlist_head),
  1826. dhash_entries,
  1827. 13,
  1828. 0,
  1829. &d_hash_shift,
  1830. &d_hash_mask,
  1831. 0);
  1832. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  1833. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  1834. }
  1835. /* SLAB cache for __getname() consumers */
  1836. kmem_cache_t *names_cachep __read_mostly;
  1837. /* SLAB cache for file structures */
  1838. kmem_cache_t *filp_cachep __read_mostly;
  1839. EXPORT_SYMBOL(d_genocide);
  1840. void __init vfs_caches_init_early(void)
  1841. {
  1842. dcache_init_early();
  1843. inode_init_early();
  1844. }
  1845. void __init vfs_caches_init(unsigned long mempages)
  1846. {
  1847. unsigned long reserve;
  1848. /* Base hash sizes on available memory, with a reserve equal to
  1849. 150% of current kernel size */
  1850. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  1851. mempages -= reserve;
  1852. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  1853. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1854. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  1855. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1856. dcache_init(mempages);
  1857. inode_init(mempages);
  1858. files_init(mempages);
  1859. mnt_init(mempages);
  1860. bdev_cache_init();
  1861. chrdev_init();
  1862. }
  1863. EXPORT_SYMBOL(d_alloc);
  1864. EXPORT_SYMBOL(d_alloc_anon);
  1865. EXPORT_SYMBOL(d_alloc_root);
  1866. EXPORT_SYMBOL(d_delete);
  1867. EXPORT_SYMBOL(d_find_alias);
  1868. EXPORT_SYMBOL(d_instantiate);
  1869. EXPORT_SYMBOL(d_invalidate);
  1870. EXPORT_SYMBOL(d_lookup);
  1871. EXPORT_SYMBOL(d_move);
  1872. EXPORT_SYMBOL_GPL(d_materialise_unique);
  1873. EXPORT_SYMBOL(d_path);
  1874. EXPORT_SYMBOL(d_prune_aliases);
  1875. EXPORT_SYMBOL(d_rehash);
  1876. EXPORT_SYMBOL(d_splice_alias);
  1877. EXPORT_SYMBOL(d_validate);
  1878. EXPORT_SYMBOL(dget_locked);
  1879. EXPORT_SYMBOL(dput);
  1880. EXPORT_SYMBOL(find_inode_number);
  1881. EXPORT_SYMBOL(have_submounts);
  1882. EXPORT_SYMBOL(names_cachep);
  1883. EXPORT_SYMBOL(shrink_dcache_parent);
  1884. EXPORT_SYMBOL(shrink_dcache_sb);