dcache.c 54 KB

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