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