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