dcache.c 63 KB

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