dcache.c 55 KB

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