dcache.c 56 KB

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