dcache.c 60 KB

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