dcache.c 60 KB

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