dcache.c 60 KB

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