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, *n;
  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. if (count >= unused)
  501. prune_ratio = 1;
  502. else
  503. prune_ratio = unused / count;
  504. spin_lock(&sb_lock);
  505. list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
  506. if (list_empty(&sb->s_instances))
  507. continue;
  508. if (sb->s_nr_dentry_unused == 0)
  509. continue;
  510. sb->s_count++;
  511. /* Now, we reclaim unused dentrins with fairness.
  512. * We reclaim them same percentage from each superblock.
  513. * We calculate number of dentries to scan on this sb
  514. * as follows, but the implementation is arranged to avoid
  515. * overflows:
  516. * number of dentries to scan on this sb =
  517. * count * (number of dentries on this sb /
  518. * number of dentries in the machine)
  519. */
  520. spin_unlock(&sb_lock);
  521. if (prune_ratio != 1)
  522. w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
  523. else
  524. w_count = sb->s_nr_dentry_unused;
  525. pruned = w_count;
  526. /*
  527. * We need to be sure this filesystem isn't being unmounted,
  528. * otherwise we could race with generic_shutdown_super(), and
  529. * end up holding a reference to an inode while the filesystem
  530. * is unmounted. So we try to get s_umount, and make sure
  531. * s_root isn't NULL.
  532. */
  533. if (down_read_trylock(&sb->s_umount)) {
  534. if ((sb->s_root != NULL) &&
  535. (!list_empty(&sb->s_dentry_lru))) {
  536. spin_unlock(&dcache_lock);
  537. __shrink_dcache_sb(sb, &w_count,
  538. DCACHE_REFERENCED);
  539. pruned -= w_count;
  540. spin_lock(&dcache_lock);
  541. }
  542. up_read(&sb->s_umount);
  543. }
  544. spin_lock(&sb_lock);
  545. /* lock was dropped, must reset next */
  546. list_safe_reset_next(sb, n, s_list);
  547. count -= pruned;
  548. __put_super(sb);
  549. /* more work left to do? */
  550. if (count <= 0)
  551. break;
  552. }
  553. spin_unlock(&sb_lock);
  554. spin_unlock(&dcache_lock);
  555. }
  556. /**
  557. * shrink_dcache_sb - shrink dcache for a superblock
  558. * @sb: superblock
  559. *
  560. * Shrink the dcache for the specified super block. This
  561. * is used to free the dcache before unmounting a file
  562. * system
  563. */
  564. void shrink_dcache_sb(struct super_block * sb)
  565. {
  566. __shrink_dcache_sb(sb, NULL, 0);
  567. }
  568. EXPORT_SYMBOL(shrink_dcache_sb);
  569. /*
  570. * destroy a single subtree of dentries for unmount
  571. * - see the comments on shrink_dcache_for_umount() for a description of the
  572. * locking
  573. */
  574. static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
  575. {
  576. struct dentry *parent;
  577. unsigned detached = 0;
  578. BUG_ON(!IS_ROOT(dentry));
  579. /* detach this root from the system */
  580. spin_lock(&dcache_lock);
  581. dentry_lru_del_init(dentry);
  582. __d_drop(dentry);
  583. spin_unlock(&dcache_lock);
  584. for (;;) {
  585. /* descend to the first leaf in the current subtree */
  586. while (!list_empty(&dentry->d_subdirs)) {
  587. struct dentry *loop;
  588. /* this is a branch with children - detach all of them
  589. * from the system in one go */
  590. spin_lock(&dcache_lock);
  591. list_for_each_entry(loop, &dentry->d_subdirs,
  592. d_u.d_child) {
  593. dentry_lru_del_init(loop);
  594. __d_drop(loop);
  595. cond_resched_lock(&dcache_lock);
  596. }
  597. spin_unlock(&dcache_lock);
  598. /* move to the first child */
  599. dentry = list_entry(dentry->d_subdirs.next,
  600. struct dentry, d_u.d_child);
  601. }
  602. /* consume the dentries from this leaf up through its parents
  603. * until we find one with children or run out altogether */
  604. do {
  605. struct inode *inode;
  606. if (atomic_read(&dentry->d_count) != 0) {
  607. printk(KERN_ERR
  608. "BUG: Dentry %p{i=%lx,n=%s}"
  609. " still in use (%d)"
  610. " [unmount of %s %s]\n",
  611. dentry,
  612. dentry->d_inode ?
  613. dentry->d_inode->i_ino : 0UL,
  614. dentry->d_name.name,
  615. atomic_read(&dentry->d_count),
  616. dentry->d_sb->s_type->name,
  617. dentry->d_sb->s_id);
  618. BUG();
  619. }
  620. if (IS_ROOT(dentry))
  621. parent = NULL;
  622. else {
  623. parent = dentry->d_parent;
  624. atomic_dec(&parent->d_count);
  625. }
  626. list_del(&dentry->d_u.d_child);
  627. detached++;
  628. inode = dentry->d_inode;
  629. if (inode) {
  630. dentry->d_inode = NULL;
  631. list_del_init(&dentry->d_alias);
  632. if (dentry->d_op && dentry->d_op->d_iput)
  633. dentry->d_op->d_iput(dentry, inode);
  634. else
  635. iput(inode);
  636. }
  637. d_free(dentry);
  638. /* finished when we fall off the top of the tree,
  639. * otherwise we ascend to the parent and move to the
  640. * next sibling if there is one */
  641. if (!parent)
  642. goto out;
  643. dentry = parent;
  644. } while (list_empty(&dentry->d_subdirs));
  645. dentry = list_entry(dentry->d_subdirs.next,
  646. struct dentry, d_u.d_child);
  647. }
  648. out:
  649. /* several dentries were freed, need to correct nr_dentry */
  650. spin_lock(&dcache_lock);
  651. dentry_stat.nr_dentry -= detached;
  652. spin_unlock(&dcache_lock);
  653. }
  654. /*
  655. * destroy the dentries attached to a superblock on unmounting
  656. * - we don't need to use dentry->d_lock, and only need dcache_lock when
  657. * removing the dentry from the system lists and hashes because:
  658. * - the superblock is detached from all mountings and open files, so the
  659. * dentry trees will not be rearranged by the VFS
  660. * - s_umount is write-locked, so the memory pressure shrinker will ignore
  661. * any dentries belonging to this superblock that it comes across
  662. * - the filesystem itself is no longer permitted to rearrange the dentries
  663. * in this superblock
  664. */
  665. void shrink_dcache_for_umount(struct super_block *sb)
  666. {
  667. struct dentry *dentry;
  668. if (down_read_trylock(&sb->s_umount))
  669. BUG();
  670. dentry = sb->s_root;
  671. sb->s_root = NULL;
  672. atomic_dec(&dentry->d_count);
  673. shrink_dcache_for_umount_subtree(dentry);
  674. while (!hlist_empty(&sb->s_anon)) {
  675. dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
  676. shrink_dcache_for_umount_subtree(dentry);
  677. }
  678. }
  679. /*
  680. * Search for at least 1 mount point in the dentry's subdirs.
  681. * We descend to the next level whenever the d_subdirs
  682. * list is non-empty and continue searching.
  683. */
  684. /**
  685. * have_submounts - check for mounts over a dentry
  686. * @parent: dentry to check.
  687. *
  688. * Return true if the parent or its subdirectories contain
  689. * a mount point
  690. */
  691. int have_submounts(struct dentry *parent)
  692. {
  693. struct dentry *this_parent = parent;
  694. struct list_head *next;
  695. spin_lock(&dcache_lock);
  696. if (d_mountpoint(parent))
  697. goto positive;
  698. repeat:
  699. next = this_parent->d_subdirs.next;
  700. resume:
  701. while (next != &this_parent->d_subdirs) {
  702. struct list_head *tmp = next;
  703. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  704. next = tmp->next;
  705. /* Have we found a mount point ? */
  706. if (d_mountpoint(dentry))
  707. goto positive;
  708. if (!list_empty(&dentry->d_subdirs)) {
  709. this_parent = dentry;
  710. goto repeat;
  711. }
  712. }
  713. /*
  714. * All done at this level ... ascend and resume the search.
  715. */
  716. if (this_parent != parent) {
  717. next = this_parent->d_u.d_child.next;
  718. this_parent = this_parent->d_parent;
  719. goto resume;
  720. }
  721. spin_unlock(&dcache_lock);
  722. return 0; /* No mount points found in tree */
  723. positive:
  724. spin_unlock(&dcache_lock);
  725. return 1;
  726. }
  727. EXPORT_SYMBOL(have_submounts);
  728. /*
  729. * Search the dentry child list for the specified parent,
  730. * and move any unused dentries to the end of the unused
  731. * list for prune_dcache(). We descend to the next level
  732. * whenever the d_subdirs list is non-empty and continue
  733. * searching.
  734. *
  735. * It returns zero iff there are no unused children,
  736. * otherwise it returns the number of children moved to
  737. * the end of the unused list. This may not be the total
  738. * number of unused children, because select_parent can
  739. * drop the lock and return early due to latency
  740. * constraints.
  741. */
  742. static int select_parent(struct dentry * parent)
  743. {
  744. struct dentry *this_parent = parent;
  745. struct list_head *next;
  746. int found = 0;
  747. spin_lock(&dcache_lock);
  748. repeat:
  749. next = this_parent->d_subdirs.next;
  750. resume:
  751. while (next != &this_parent->d_subdirs) {
  752. struct list_head *tmp = next;
  753. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  754. next = tmp->next;
  755. dentry_lru_del_init(dentry);
  756. /*
  757. * move only zero ref count dentries to the end
  758. * of the unused list for prune_dcache
  759. */
  760. if (!atomic_read(&dentry->d_count)) {
  761. dentry_lru_add_tail(dentry);
  762. found++;
  763. }
  764. /*
  765. * We can return to the caller if we have found some (this
  766. * ensures forward progress). We'll be coming back to find
  767. * the rest.
  768. */
  769. if (found && need_resched())
  770. goto out;
  771. /*
  772. * Descend a level if the d_subdirs list is non-empty.
  773. */
  774. if (!list_empty(&dentry->d_subdirs)) {
  775. this_parent = dentry;
  776. goto repeat;
  777. }
  778. }
  779. /*
  780. * All done at this level ... ascend and resume the search.
  781. */
  782. if (this_parent != parent) {
  783. next = this_parent->d_u.d_child.next;
  784. this_parent = this_parent->d_parent;
  785. goto resume;
  786. }
  787. out:
  788. spin_unlock(&dcache_lock);
  789. return found;
  790. }
  791. /**
  792. * shrink_dcache_parent - prune dcache
  793. * @parent: parent of entries to prune
  794. *
  795. * Prune the dcache to remove unused children of the parent dentry.
  796. */
  797. void shrink_dcache_parent(struct dentry * parent)
  798. {
  799. struct super_block *sb = parent->d_sb;
  800. int found;
  801. while ((found = select_parent(parent)) != 0)
  802. __shrink_dcache_sb(sb, &found, 0);
  803. }
  804. EXPORT_SYMBOL(shrink_dcache_parent);
  805. /*
  806. * Scan `nr' dentries and return the number which remain.
  807. *
  808. * We need to avoid reentering the filesystem if the caller is performing a
  809. * GFP_NOFS allocation attempt. One example deadlock is:
  810. *
  811. * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
  812. * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
  813. * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
  814. *
  815. * In this case we return -1 to tell the caller that we baled.
  816. */
  817. static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
  818. {
  819. if (nr) {
  820. if (!(gfp_mask & __GFP_FS))
  821. return -1;
  822. prune_dcache(nr);
  823. }
  824. return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  825. }
  826. static struct shrinker dcache_shrinker = {
  827. .shrink = shrink_dcache_memory,
  828. .seeks = DEFAULT_SEEKS,
  829. };
  830. /**
  831. * d_alloc - allocate a dcache entry
  832. * @parent: parent of entry to allocate
  833. * @name: qstr of the name
  834. *
  835. * Allocates a dentry. It returns %NULL if there is insufficient memory
  836. * available. On a success the dentry is returned. The name passed in is
  837. * copied and the copy passed in may be reused after this call.
  838. */
  839. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  840. {
  841. struct dentry *dentry;
  842. char *dname;
  843. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  844. if (!dentry)
  845. return NULL;
  846. if (name->len > DNAME_INLINE_LEN-1) {
  847. dname = kmalloc(name->len + 1, GFP_KERNEL);
  848. if (!dname) {
  849. kmem_cache_free(dentry_cache, dentry);
  850. return NULL;
  851. }
  852. } else {
  853. dname = dentry->d_iname;
  854. }
  855. dentry->d_name.name = dname;
  856. dentry->d_name.len = name->len;
  857. dentry->d_name.hash = name->hash;
  858. memcpy(dname, name->name, name->len);
  859. dname[name->len] = 0;
  860. atomic_set(&dentry->d_count, 1);
  861. dentry->d_flags = DCACHE_UNHASHED;
  862. spin_lock_init(&dentry->d_lock);
  863. dentry->d_inode = NULL;
  864. dentry->d_parent = NULL;
  865. dentry->d_sb = NULL;
  866. dentry->d_op = NULL;
  867. dentry->d_fsdata = NULL;
  868. dentry->d_mounted = 0;
  869. INIT_HLIST_NODE(&dentry->d_hash);
  870. INIT_LIST_HEAD(&dentry->d_lru);
  871. INIT_LIST_HEAD(&dentry->d_subdirs);
  872. INIT_LIST_HEAD(&dentry->d_alias);
  873. if (parent) {
  874. dentry->d_parent = dget(parent);
  875. dentry->d_sb = parent->d_sb;
  876. } else {
  877. INIT_LIST_HEAD(&dentry->d_u.d_child);
  878. }
  879. spin_lock(&dcache_lock);
  880. if (parent)
  881. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  882. dentry_stat.nr_dentry++;
  883. spin_unlock(&dcache_lock);
  884. return dentry;
  885. }
  886. EXPORT_SYMBOL(d_alloc);
  887. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  888. {
  889. struct qstr q;
  890. q.name = name;
  891. q.len = strlen(name);
  892. q.hash = full_name_hash(q.name, q.len);
  893. return d_alloc(parent, &q);
  894. }
  895. EXPORT_SYMBOL(d_alloc_name);
  896. /* the caller must hold dcache_lock */
  897. static void __d_instantiate(struct dentry *dentry, struct inode *inode)
  898. {
  899. if (inode)
  900. list_add(&dentry->d_alias, &inode->i_dentry);
  901. dentry->d_inode = inode;
  902. fsnotify_d_instantiate(dentry, inode);
  903. }
  904. /**
  905. * d_instantiate - fill in inode information for a dentry
  906. * @entry: dentry to complete
  907. * @inode: inode to attach to this dentry
  908. *
  909. * Fill in inode information in the entry.
  910. *
  911. * This turns negative dentries into productive full members
  912. * of society.
  913. *
  914. * NOTE! This assumes that the inode count has been incremented
  915. * (or otherwise set) by the caller to indicate that it is now
  916. * in use by the dcache.
  917. */
  918. void d_instantiate(struct dentry *entry, struct inode * inode)
  919. {
  920. BUG_ON(!list_empty(&entry->d_alias));
  921. spin_lock(&dcache_lock);
  922. __d_instantiate(entry, inode);
  923. spin_unlock(&dcache_lock);
  924. security_d_instantiate(entry, inode);
  925. }
  926. EXPORT_SYMBOL(d_instantiate);
  927. /**
  928. * d_instantiate_unique - instantiate a non-aliased dentry
  929. * @entry: dentry to instantiate
  930. * @inode: inode to attach to this dentry
  931. *
  932. * Fill in inode information in the entry. On success, it returns NULL.
  933. * If an unhashed alias of "entry" already exists, then we return the
  934. * aliased dentry instead and drop one reference to inode.
  935. *
  936. * Note that in order to avoid conflicts with rename() etc, the caller
  937. * had better be holding the parent directory semaphore.
  938. *
  939. * This also assumes that the inode count has been incremented
  940. * (or otherwise set) by the caller to indicate that it is now
  941. * in use by the dcache.
  942. */
  943. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  944. struct inode *inode)
  945. {
  946. struct dentry *alias;
  947. int len = entry->d_name.len;
  948. const char *name = entry->d_name.name;
  949. unsigned int hash = entry->d_name.hash;
  950. if (!inode) {
  951. __d_instantiate(entry, NULL);
  952. return NULL;
  953. }
  954. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  955. struct qstr *qstr = &alias->d_name;
  956. if (qstr->hash != hash)
  957. continue;
  958. if (alias->d_parent != entry->d_parent)
  959. continue;
  960. if (qstr->len != len)
  961. continue;
  962. if (memcmp(qstr->name, name, len))
  963. continue;
  964. dget_locked(alias);
  965. return alias;
  966. }
  967. __d_instantiate(entry, inode);
  968. return NULL;
  969. }
  970. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  971. {
  972. struct dentry *result;
  973. BUG_ON(!list_empty(&entry->d_alias));
  974. spin_lock(&dcache_lock);
  975. result = __d_instantiate_unique(entry, inode);
  976. spin_unlock(&dcache_lock);
  977. if (!result) {
  978. security_d_instantiate(entry, inode);
  979. return NULL;
  980. }
  981. BUG_ON(!d_unhashed(result));
  982. iput(inode);
  983. return result;
  984. }
  985. EXPORT_SYMBOL(d_instantiate_unique);
  986. /**
  987. * d_alloc_root - allocate root dentry
  988. * @root_inode: inode to allocate the root for
  989. *
  990. * Allocate a root ("/") dentry for the inode given. The inode is
  991. * instantiated and returned. %NULL is returned if there is insufficient
  992. * memory or the inode passed is %NULL.
  993. */
  994. struct dentry * d_alloc_root(struct inode * root_inode)
  995. {
  996. struct dentry *res = NULL;
  997. if (root_inode) {
  998. static const struct qstr name = { .name = "/", .len = 1 };
  999. res = d_alloc(NULL, &name);
  1000. if (res) {
  1001. res->d_sb = root_inode->i_sb;
  1002. res->d_parent = res;
  1003. d_instantiate(res, root_inode);
  1004. }
  1005. }
  1006. return res;
  1007. }
  1008. EXPORT_SYMBOL(d_alloc_root);
  1009. static inline struct hlist_head *d_hash(struct dentry *parent,
  1010. unsigned long hash)
  1011. {
  1012. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  1013. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  1014. return dentry_hashtable + (hash & D_HASHMASK);
  1015. }
  1016. /**
  1017. * d_obtain_alias - find or allocate a dentry for a given inode
  1018. * @inode: inode to allocate the dentry for
  1019. *
  1020. * Obtain a dentry for an inode resulting from NFS filehandle conversion or
  1021. * similar open by handle operations. The returned dentry may be anonymous,
  1022. * or may have a full name (if the inode was already in the cache).
  1023. *
  1024. * When called on a directory inode, we must ensure that the inode only ever
  1025. * has one dentry. If a dentry is found, that is returned instead of
  1026. * allocating a new one.
  1027. *
  1028. * On successful return, the reference to the inode has been transferred
  1029. * to the dentry. In case of an error the reference on the inode is released.
  1030. * To make it easier to use in export operations a %NULL or IS_ERR inode may
  1031. * be passed in and will be the error will be propagate to the return value,
  1032. * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
  1033. */
  1034. struct dentry *d_obtain_alias(struct inode *inode)
  1035. {
  1036. static const struct qstr anonstring = { .name = "" };
  1037. struct dentry *tmp;
  1038. struct dentry *res;
  1039. if (!inode)
  1040. return ERR_PTR(-ESTALE);
  1041. if (IS_ERR(inode))
  1042. return ERR_CAST(inode);
  1043. res = d_find_alias(inode);
  1044. if (res)
  1045. goto out_iput;
  1046. tmp = d_alloc(NULL, &anonstring);
  1047. if (!tmp) {
  1048. res = ERR_PTR(-ENOMEM);
  1049. goto out_iput;
  1050. }
  1051. tmp->d_parent = tmp; /* make sure dput doesn't croak */
  1052. spin_lock(&dcache_lock);
  1053. res = __d_find_alias(inode, 0);
  1054. if (res) {
  1055. spin_unlock(&dcache_lock);
  1056. dput(tmp);
  1057. goto out_iput;
  1058. }
  1059. /* attach a disconnected dentry */
  1060. spin_lock(&tmp->d_lock);
  1061. tmp->d_sb = inode->i_sb;
  1062. tmp->d_inode = inode;
  1063. tmp->d_flags |= DCACHE_DISCONNECTED;
  1064. tmp->d_flags &= ~DCACHE_UNHASHED;
  1065. list_add(&tmp->d_alias, &inode->i_dentry);
  1066. hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
  1067. spin_unlock(&tmp->d_lock);
  1068. spin_unlock(&dcache_lock);
  1069. return tmp;
  1070. out_iput:
  1071. iput(inode);
  1072. return res;
  1073. }
  1074. EXPORT_SYMBOL(d_obtain_alias);
  1075. /**
  1076. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  1077. * @inode: the inode which may have a disconnected dentry
  1078. * @dentry: a negative dentry which we want to point to the inode.
  1079. *
  1080. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1081. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1082. * and return it, else simply d_add the inode to the dentry and return NULL.
  1083. *
  1084. * This is needed in the lookup routine of any filesystem that is exportable
  1085. * (via knfsd) so that we can build dcache paths to directories effectively.
  1086. *
  1087. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1088. * is returned. This matches the expected return value of ->lookup.
  1089. *
  1090. */
  1091. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1092. {
  1093. struct dentry *new = NULL;
  1094. if (inode && S_ISDIR(inode->i_mode)) {
  1095. spin_lock(&dcache_lock);
  1096. new = __d_find_alias(inode, 1);
  1097. if (new) {
  1098. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  1099. spin_unlock(&dcache_lock);
  1100. security_d_instantiate(new, inode);
  1101. d_move(new, dentry);
  1102. iput(inode);
  1103. } else {
  1104. /* already taking dcache_lock, so d_add() by hand */
  1105. __d_instantiate(dentry, inode);
  1106. spin_unlock(&dcache_lock);
  1107. security_d_instantiate(dentry, inode);
  1108. d_rehash(dentry);
  1109. }
  1110. } else
  1111. d_add(dentry, inode);
  1112. return new;
  1113. }
  1114. EXPORT_SYMBOL(d_splice_alias);
  1115. /**
  1116. * d_add_ci - lookup or allocate new dentry with case-exact name
  1117. * @inode: the inode case-insensitive lookup has found
  1118. * @dentry: the negative dentry that was passed to the parent's lookup func
  1119. * @name: the case-exact name to be associated with the returned dentry
  1120. *
  1121. * This is to avoid filling the dcache with case-insensitive names to the
  1122. * same inode, only the actual correct case is stored in the dcache for
  1123. * case-insensitive filesystems.
  1124. *
  1125. * For a case-insensitive lookup match and if the the case-exact dentry
  1126. * already exists in in the dcache, use it and return it.
  1127. *
  1128. * If no entry exists with the exact case name, allocate new dentry with
  1129. * the exact case, and return the spliced entry.
  1130. */
  1131. struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
  1132. struct qstr *name)
  1133. {
  1134. int error;
  1135. struct dentry *found;
  1136. struct dentry *new;
  1137. /*
  1138. * First check if a dentry matching the name already exists,
  1139. * if not go ahead and create it now.
  1140. */
  1141. found = d_hash_and_lookup(dentry->d_parent, name);
  1142. if (!found) {
  1143. new = d_alloc(dentry->d_parent, name);
  1144. if (!new) {
  1145. error = -ENOMEM;
  1146. goto err_out;
  1147. }
  1148. found = d_splice_alias(inode, new);
  1149. if (found) {
  1150. dput(new);
  1151. return found;
  1152. }
  1153. return new;
  1154. }
  1155. /*
  1156. * If a matching dentry exists, and it's not negative use it.
  1157. *
  1158. * Decrement the reference count to balance the iget() done
  1159. * earlier on.
  1160. */
  1161. if (found->d_inode) {
  1162. if (unlikely(found->d_inode != inode)) {
  1163. /* This can't happen because bad inodes are unhashed. */
  1164. BUG_ON(!is_bad_inode(inode));
  1165. BUG_ON(!is_bad_inode(found->d_inode));
  1166. }
  1167. iput(inode);
  1168. return found;
  1169. }
  1170. /*
  1171. * Negative dentry: instantiate it unless the inode is a directory and
  1172. * already has a dentry.
  1173. */
  1174. spin_lock(&dcache_lock);
  1175. if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
  1176. __d_instantiate(found, inode);
  1177. spin_unlock(&dcache_lock);
  1178. security_d_instantiate(found, inode);
  1179. return found;
  1180. }
  1181. /*
  1182. * In case a directory already has a (disconnected) entry grab a
  1183. * reference to it, move it in place and use it.
  1184. */
  1185. new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
  1186. dget_locked(new);
  1187. spin_unlock(&dcache_lock);
  1188. security_d_instantiate(found, inode);
  1189. d_move(new, found);
  1190. iput(inode);
  1191. dput(found);
  1192. return new;
  1193. err_out:
  1194. iput(inode);
  1195. return ERR_PTR(error);
  1196. }
  1197. EXPORT_SYMBOL(d_add_ci);
  1198. /**
  1199. * d_lookup - search for a dentry
  1200. * @parent: parent dentry
  1201. * @name: qstr of name we wish to find
  1202. *
  1203. * Searches the children of the parent dentry for the name in question. If
  1204. * the dentry is found its reference count is incremented and the dentry
  1205. * is returned. The caller must use dput to free the entry when it has
  1206. * finished using it. %NULL is returned on failure.
  1207. *
  1208. * __d_lookup is dcache_lock free. The hash list is protected using RCU.
  1209. * Memory barriers are used while updating and doing lockless traversal.
  1210. * To avoid races with d_move while rename is happening, d_lock is used.
  1211. *
  1212. * Overflows in memcmp(), while d_move, are avoided by keeping the length
  1213. * and name pointer in one structure pointed by d_qstr.
  1214. *
  1215. * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
  1216. * lookup is going on.
  1217. *
  1218. * The dentry unused LRU is not updated even if lookup finds the required dentry
  1219. * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
  1220. * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
  1221. * acquisition.
  1222. *
  1223. * d_lookup() is protected against the concurrent renames in some unrelated
  1224. * directory using the seqlockt_t rename_lock.
  1225. */
  1226. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  1227. {
  1228. struct dentry * dentry = NULL;
  1229. unsigned long seq;
  1230. do {
  1231. seq = read_seqbegin(&rename_lock);
  1232. dentry = __d_lookup(parent, name);
  1233. if (dentry)
  1234. break;
  1235. } while (read_seqretry(&rename_lock, seq));
  1236. return dentry;
  1237. }
  1238. EXPORT_SYMBOL(d_lookup);
  1239. struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
  1240. {
  1241. unsigned int len = name->len;
  1242. unsigned int hash = name->hash;
  1243. const unsigned char *str = name->name;
  1244. struct hlist_head *head = d_hash(parent,hash);
  1245. struct dentry *found = NULL;
  1246. struct hlist_node *node;
  1247. struct dentry *dentry;
  1248. rcu_read_lock();
  1249. hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
  1250. struct qstr *qstr;
  1251. if (dentry->d_name.hash != hash)
  1252. continue;
  1253. if (dentry->d_parent != parent)
  1254. continue;
  1255. spin_lock(&dentry->d_lock);
  1256. /*
  1257. * Recheck the dentry after taking the lock - d_move may have
  1258. * changed things. Don't bother checking the hash because we're
  1259. * about to compare the whole name anyway.
  1260. */
  1261. if (dentry->d_parent != parent)
  1262. goto next;
  1263. /* non-existing due to RCU? */
  1264. if (d_unhashed(dentry))
  1265. goto next;
  1266. /*
  1267. * It is safe to compare names since d_move() cannot
  1268. * change the qstr (protected by d_lock).
  1269. */
  1270. qstr = &dentry->d_name;
  1271. if (parent->d_op && parent->d_op->d_compare) {
  1272. if (parent->d_op->d_compare(parent, qstr, name))
  1273. goto next;
  1274. } else {
  1275. if (qstr->len != len)
  1276. goto next;
  1277. if (memcmp(qstr->name, str, len))
  1278. goto next;
  1279. }
  1280. atomic_inc(&dentry->d_count);
  1281. found = dentry;
  1282. spin_unlock(&dentry->d_lock);
  1283. break;
  1284. next:
  1285. spin_unlock(&dentry->d_lock);
  1286. }
  1287. rcu_read_unlock();
  1288. return found;
  1289. }
  1290. /**
  1291. * d_hash_and_lookup - hash the qstr then search for a dentry
  1292. * @dir: Directory to search in
  1293. * @name: qstr of name we wish to find
  1294. *
  1295. * On hash failure or on lookup failure NULL is returned.
  1296. */
  1297. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1298. {
  1299. struct dentry *dentry = NULL;
  1300. /*
  1301. * Check for a fs-specific hash function. Note that we must
  1302. * calculate the standard hash first, as the d_op->d_hash()
  1303. * routine may choose to leave the hash value unchanged.
  1304. */
  1305. name->hash = full_name_hash(name->name, name->len);
  1306. if (dir->d_op && dir->d_op->d_hash) {
  1307. if (dir->d_op->d_hash(dir, name) < 0)
  1308. goto out;
  1309. }
  1310. dentry = d_lookup(dir, name);
  1311. out:
  1312. return dentry;
  1313. }
  1314. /**
  1315. * d_validate - verify dentry provided from insecure source
  1316. * @dentry: The dentry alleged to be valid child of @dparent
  1317. * @dparent: The parent dentry (known to be valid)
  1318. *
  1319. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1320. * This is used by ncpfs in its readdir implementation.
  1321. * Zero is returned in the dentry is invalid.
  1322. */
  1323. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1324. {
  1325. struct hlist_head *base;
  1326. struct hlist_node *lhp;
  1327. /* Check whether the ptr might be valid at all.. */
  1328. if (!kmem_ptr_validate(dentry_cache, dentry))
  1329. goto out;
  1330. if (dentry->d_parent != dparent)
  1331. goto out;
  1332. spin_lock(&dcache_lock);
  1333. base = d_hash(dparent, dentry->d_name.hash);
  1334. hlist_for_each(lhp,base) {
  1335. /* hlist_for_each_entry_rcu() not required for d_hash list
  1336. * as it is parsed under dcache_lock
  1337. */
  1338. if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
  1339. __dget_locked(dentry);
  1340. spin_unlock(&dcache_lock);
  1341. return 1;
  1342. }
  1343. }
  1344. spin_unlock(&dcache_lock);
  1345. out:
  1346. return 0;
  1347. }
  1348. EXPORT_SYMBOL(d_validate);
  1349. /*
  1350. * When a file is deleted, we have two options:
  1351. * - turn this dentry into a negative dentry
  1352. * - unhash this dentry and free it.
  1353. *
  1354. * Usually, we want to just turn this into
  1355. * a negative dentry, but if anybody else is
  1356. * currently using the dentry or the inode
  1357. * we can't do that and we fall back on removing
  1358. * it from the hash queues and waiting for
  1359. * it to be deleted later when it has no users
  1360. */
  1361. /**
  1362. * d_delete - delete a dentry
  1363. * @dentry: The dentry to delete
  1364. *
  1365. * Turn the dentry into a negative dentry if possible, otherwise
  1366. * remove it from the hash queues so it can be deleted later
  1367. */
  1368. void d_delete(struct dentry * dentry)
  1369. {
  1370. int isdir = 0;
  1371. /*
  1372. * Are we the only user?
  1373. */
  1374. spin_lock(&dcache_lock);
  1375. spin_lock(&dentry->d_lock);
  1376. isdir = S_ISDIR(dentry->d_inode->i_mode);
  1377. if (atomic_read(&dentry->d_count) == 1) {
  1378. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  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. }