dcache.c 60 KB

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