dcache.c 59 KB

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