dcache.c 60 KB

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