dcache.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  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. INIT_HLIST_NODE(&dentry->d_hash);
  862. INIT_LIST_HEAD(&dentry->d_lru);
  863. INIT_LIST_HEAD(&dentry->d_subdirs);
  864. INIT_LIST_HEAD(&dentry->d_alias);
  865. if (parent) {
  866. dentry->d_parent = dget(parent);
  867. dentry->d_sb = parent->d_sb;
  868. } else {
  869. INIT_LIST_HEAD(&dentry->d_u.d_child);
  870. }
  871. spin_lock(&dcache_lock);
  872. if (parent)
  873. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  874. dentry_stat.nr_dentry++;
  875. spin_unlock(&dcache_lock);
  876. return dentry;
  877. }
  878. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  879. {
  880. struct qstr q;
  881. q.name = name;
  882. q.len = strlen(name);
  883. q.hash = full_name_hash(q.name, q.len);
  884. return d_alloc(parent, &q);
  885. }
  886. /* the caller must hold dcache_lock */
  887. static void __d_instantiate(struct dentry *dentry, struct inode *inode)
  888. {
  889. if (inode)
  890. list_add(&dentry->d_alias, &inode->i_dentry);
  891. dentry->d_inode = inode;
  892. fsnotify_d_instantiate(dentry, inode);
  893. }
  894. /**
  895. * d_instantiate - fill in inode information for a dentry
  896. * @entry: dentry to complete
  897. * @inode: inode to attach to this dentry
  898. *
  899. * Fill in inode information in the entry.
  900. *
  901. * This turns negative dentries into productive full members
  902. * of society.
  903. *
  904. * NOTE! This assumes that the inode count has been incremented
  905. * (or otherwise set) by the caller to indicate that it is now
  906. * in use by the dcache.
  907. */
  908. void d_instantiate(struct dentry *entry, struct inode * inode)
  909. {
  910. BUG_ON(!list_empty(&entry->d_alias));
  911. spin_lock(&dcache_lock);
  912. __d_instantiate(entry, inode);
  913. spin_unlock(&dcache_lock);
  914. security_d_instantiate(entry, inode);
  915. }
  916. /**
  917. * d_instantiate_unique - instantiate a non-aliased dentry
  918. * @entry: dentry to instantiate
  919. * @inode: inode to attach to this dentry
  920. *
  921. * Fill in inode information in the entry. On success, it returns NULL.
  922. * If an unhashed alias of "entry" already exists, then we return the
  923. * aliased dentry instead and drop one reference to inode.
  924. *
  925. * Note that in order to avoid conflicts with rename() etc, the caller
  926. * had better be holding the parent directory semaphore.
  927. *
  928. * This also assumes that the inode count has been incremented
  929. * (or otherwise set) by the caller to indicate that it is now
  930. * in use by the dcache.
  931. */
  932. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  933. struct inode *inode)
  934. {
  935. struct dentry *alias;
  936. int len = entry->d_name.len;
  937. const char *name = entry->d_name.name;
  938. unsigned int hash = entry->d_name.hash;
  939. if (!inode) {
  940. __d_instantiate(entry, NULL);
  941. return NULL;
  942. }
  943. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  944. struct qstr *qstr = &alias->d_name;
  945. if (qstr->hash != hash)
  946. continue;
  947. if (alias->d_parent != entry->d_parent)
  948. continue;
  949. if (qstr->len != len)
  950. continue;
  951. if (memcmp(qstr->name, name, len))
  952. continue;
  953. dget_locked(alias);
  954. return alias;
  955. }
  956. __d_instantiate(entry, inode);
  957. return NULL;
  958. }
  959. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  960. {
  961. struct dentry *result;
  962. BUG_ON(!list_empty(&entry->d_alias));
  963. spin_lock(&dcache_lock);
  964. result = __d_instantiate_unique(entry, inode);
  965. spin_unlock(&dcache_lock);
  966. if (!result) {
  967. security_d_instantiate(entry, inode);
  968. return NULL;
  969. }
  970. BUG_ON(!d_unhashed(result));
  971. iput(inode);
  972. return result;
  973. }
  974. EXPORT_SYMBOL(d_instantiate_unique);
  975. /**
  976. * d_alloc_root - allocate root dentry
  977. * @root_inode: inode to allocate the root for
  978. *
  979. * Allocate a root ("/") dentry for the inode given. The inode is
  980. * instantiated and returned. %NULL is returned if there is insufficient
  981. * memory or the inode passed is %NULL.
  982. */
  983. struct dentry * d_alloc_root(struct inode * root_inode)
  984. {
  985. struct dentry *res = NULL;
  986. if (root_inode) {
  987. static const struct qstr name = { .name = "/", .len = 1 };
  988. res = d_alloc(NULL, &name);
  989. if (res) {
  990. res->d_sb = root_inode->i_sb;
  991. res->d_parent = res;
  992. d_instantiate(res, root_inode);
  993. }
  994. }
  995. return res;
  996. }
  997. static inline struct hlist_head *d_hash(struct dentry *parent,
  998. unsigned long hash)
  999. {
  1000. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  1001. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  1002. return dentry_hashtable + (hash & D_HASHMASK);
  1003. }
  1004. /**
  1005. * d_obtain_alias - find or allocate a dentry for a given inode
  1006. * @inode: inode to allocate the dentry for
  1007. *
  1008. * Obtain a dentry for an inode resulting from NFS filehandle conversion or
  1009. * similar open by handle operations. The returned dentry may be anonymous,
  1010. * or may have a full name (if the inode was already in the cache).
  1011. *
  1012. * When called on a directory inode, we must ensure that the inode only ever
  1013. * has one dentry. If a dentry is found, that is returned instead of
  1014. * allocating a new one.
  1015. *
  1016. * On successful return, the reference to the inode has been transferred
  1017. * to the dentry. In case of an error the reference on the inode is released.
  1018. * To make it easier to use in export operations a %NULL or IS_ERR inode may
  1019. * be passed in and will be the error will be propagate to the return value,
  1020. * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
  1021. */
  1022. struct dentry *d_obtain_alias(struct inode *inode)
  1023. {
  1024. static const struct qstr anonstring = { .name = "" };
  1025. struct dentry *tmp;
  1026. struct dentry *res;
  1027. if (!inode)
  1028. return ERR_PTR(-ESTALE);
  1029. if (IS_ERR(inode))
  1030. return ERR_CAST(inode);
  1031. res = d_find_alias(inode);
  1032. if (res)
  1033. goto out_iput;
  1034. tmp = d_alloc(NULL, &anonstring);
  1035. if (!tmp) {
  1036. res = ERR_PTR(-ENOMEM);
  1037. goto out_iput;
  1038. }
  1039. tmp->d_parent = tmp; /* make sure dput doesn't croak */
  1040. spin_lock(&dcache_lock);
  1041. res = __d_find_alias(inode, 0);
  1042. if (res) {
  1043. spin_unlock(&dcache_lock);
  1044. dput(tmp);
  1045. goto out_iput;
  1046. }
  1047. /* attach a disconnected dentry */
  1048. spin_lock(&tmp->d_lock);
  1049. tmp->d_sb = inode->i_sb;
  1050. tmp->d_inode = inode;
  1051. tmp->d_flags |= DCACHE_DISCONNECTED;
  1052. tmp->d_flags &= ~DCACHE_UNHASHED;
  1053. list_add(&tmp->d_alias, &inode->i_dentry);
  1054. hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
  1055. spin_unlock(&tmp->d_lock);
  1056. spin_unlock(&dcache_lock);
  1057. return tmp;
  1058. out_iput:
  1059. iput(inode);
  1060. return res;
  1061. }
  1062. EXPORT_SYMBOL(d_obtain_alias);
  1063. /**
  1064. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  1065. * @inode: the inode which may have a disconnected dentry
  1066. * @dentry: a negative dentry which we want to point to the inode.
  1067. *
  1068. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1069. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1070. * and return it, else simply d_add the inode to the dentry and return NULL.
  1071. *
  1072. * This is needed in the lookup routine of any filesystem that is exportable
  1073. * (via knfsd) so that we can build dcache paths to directories effectively.
  1074. *
  1075. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1076. * is returned. This matches the expected return value of ->lookup.
  1077. *
  1078. */
  1079. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1080. {
  1081. struct dentry *new = NULL;
  1082. if (inode && S_ISDIR(inode->i_mode)) {
  1083. spin_lock(&dcache_lock);
  1084. new = __d_find_alias(inode, 1);
  1085. if (new) {
  1086. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  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. /* already taking dcache_lock, so d_add() by hand */
  1094. __d_instantiate(dentry, inode);
  1095. spin_unlock(&dcache_lock);
  1096. security_d_instantiate(dentry, inode);
  1097. d_rehash(dentry);
  1098. }
  1099. } else
  1100. d_add(dentry, inode);
  1101. return new;
  1102. }
  1103. /**
  1104. * d_add_ci - lookup or allocate new dentry with case-exact name
  1105. * @inode: the inode case-insensitive lookup has found
  1106. * @dentry: the negative dentry that was passed to the parent's lookup func
  1107. * @name: the case-exact name to be associated with the returned dentry
  1108. *
  1109. * This is to avoid filling the dcache with case-insensitive names to the
  1110. * same inode, only the actual correct case is stored in the dcache for
  1111. * case-insensitive filesystems.
  1112. *
  1113. * For a case-insensitive lookup match and if the the case-exact dentry
  1114. * already exists in in the dcache, use it and return it.
  1115. *
  1116. * If no entry exists with the exact case name, allocate new dentry with
  1117. * the exact case, and return the spliced entry.
  1118. */
  1119. struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
  1120. struct qstr *name)
  1121. {
  1122. int error;
  1123. struct dentry *found;
  1124. struct dentry *new;
  1125. /* Does a dentry matching the name exist already? */
  1126. found = d_hash_and_lookup(dentry->d_parent, name);
  1127. /* If not, create it now and return */
  1128. if (!found) {
  1129. new = d_alloc(dentry->d_parent, name);
  1130. if (!new) {
  1131. error = -ENOMEM;
  1132. goto err_out;
  1133. }
  1134. found = d_splice_alias(inode, new);
  1135. if (found) {
  1136. dput(new);
  1137. return found;
  1138. }
  1139. return new;
  1140. }
  1141. /* Matching dentry exists, check if it is negative. */
  1142. if (found->d_inode) {
  1143. if (unlikely(found->d_inode != inode)) {
  1144. /* This can't happen because bad inodes are unhashed. */
  1145. BUG_ON(!is_bad_inode(inode));
  1146. BUG_ON(!is_bad_inode(found->d_inode));
  1147. }
  1148. /*
  1149. * Already have the inode and the dentry attached, decrement
  1150. * the reference count to balance the iget() done
  1151. * earlier on. We found the dentry using d_lookup() so it
  1152. * cannot be disconnected and thus we do not need to worry
  1153. * about any NFS/disconnectedness issues here.
  1154. */
  1155. iput(inode);
  1156. return found;
  1157. }
  1158. /*
  1159. * Negative dentry: instantiate it unless the inode is a directory and
  1160. * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
  1161. * in which case d_move() that in place of the found dentry.
  1162. */
  1163. if (!S_ISDIR(inode->i_mode)) {
  1164. /* Not a directory; everything is easy. */
  1165. d_instantiate(found, inode);
  1166. return found;
  1167. }
  1168. spin_lock(&dcache_lock);
  1169. if (list_empty(&inode->i_dentry)) {
  1170. /*
  1171. * Directory without a 'disconnected' dentry; we need to do
  1172. * d_instantiate() by hand because it takes dcache_lock which
  1173. * we already hold.
  1174. */
  1175. __d_instantiate(found, inode);
  1176. spin_unlock(&dcache_lock);
  1177. security_d_instantiate(found, inode);
  1178. return found;
  1179. }
  1180. /*
  1181. * Directory with a 'disconnected' dentry; get a reference to the
  1182. * 'disconnected' dentry.
  1183. */
  1184. new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
  1185. dget_locked(new);
  1186. spin_unlock(&dcache_lock);
  1187. /* Do security vodoo. */
  1188. security_d_instantiate(found, inode);
  1189. /* Move new in place of found. */
  1190. d_move(new, found);
  1191. /* Balance the iget() we did above. */
  1192. iput(inode);
  1193. /* Throw away found. */
  1194. dput(found);
  1195. /* Use new as the actual dentry. */
  1196. return new;
  1197. err_out:
  1198. iput(inode);
  1199. return ERR_PTR(error);
  1200. }
  1201. /**
  1202. * d_lookup - search for a dentry
  1203. * @parent: parent dentry
  1204. * @name: qstr of name we wish to find
  1205. *
  1206. * Searches the children of the parent dentry for the name in question. If
  1207. * the dentry is found its reference count is incremented and the dentry
  1208. * is returned. The caller must use dput to free the entry when it has
  1209. * finished using it. %NULL is returned on failure.
  1210. *
  1211. * __d_lookup is dcache_lock free. The hash list is protected using RCU.
  1212. * Memory barriers are used while updating and doing lockless traversal.
  1213. * To avoid races with d_move while rename is happening, d_lock is used.
  1214. *
  1215. * Overflows in memcmp(), while d_move, are avoided by keeping the length
  1216. * and name pointer in one structure pointed by d_qstr.
  1217. *
  1218. * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
  1219. * lookup is going on.
  1220. *
  1221. * The dentry unused LRU is not updated even if lookup finds the required dentry
  1222. * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
  1223. * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
  1224. * acquisition.
  1225. *
  1226. * d_lookup() is protected against the concurrent renames in some unrelated
  1227. * directory using the seqlockt_t rename_lock.
  1228. */
  1229. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  1230. {
  1231. struct dentry * dentry = NULL;
  1232. unsigned long seq;
  1233. do {
  1234. seq = read_seqbegin(&rename_lock);
  1235. dentry = __d_lookup(parent, name);
  1236. if (dentry)
  1237. break;
  1238. } while (read_seqretry(&rename_lock, seq));
  1239. return dentry;
  1240. }
  1241. struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
  1242. {
  1243. unsigned int len = name->len;
  1244. unsigned int hash = name->hash;
  1245. const unsigned char *str = name->name;
  1246. struct hlist_head *head = d_hash(parent,hash);
  1247. struct dentry *found = NULL;
  1248. struct hlist_node *node;
  1249. struct dentry *dentry;
  1250. rcu_read_lock();
  1251. hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
  1252. struct qstr *qstr;
  1253. if (dentry->d_name.hash != hash)
  1254. continue;
  1255. if (dentry->d_parent != parent)
  1256. continue;
  1257. spin_lock(&dentry->d_lock);
  1258. /*
  1259. * Recheck the dentry after taking the lock - d_move may have
  1260. * changed things. Don't bother checking the hash because we're
  1261. * about to compare the whole name anyway.
  1262. */
  1263. if (dentry->d_parent != parent)
  1264. goto next;
  1265. /* non-existing due to RCU? */
  1266. if (d_unhashed(dentry))
  1267. goto next;
  1268. /*
  1269. * It is safe to compare names since d_move() cannot
  1270. * change the qstr (protected by d_lock).
  1271. */
  1272. qstr = &dentry->d_name;
  1273. if (parent->d_op && parent->d_op->d_compare) {
  1274. if (parent->d_op->d_compare(parent, qstr, name))
  1275. goto next;
  1276. } else {
  1277. if (qstr->len != len)
  1278. goto next;
  1279. if (memcmp(qstr->name, str, len))
  1280. goto next;
  1281. }
  1282. atomic_inc(&dentry->d_count);
  1283. found = dentry;
  1284. spin_unlock(&dentry->d_lock);
  1285. break;
  1286. next:
  1287. spin_unlock(&dentry->d_lock);
  1288. }
  1289. rcu_read_unlock();
  1290. return found;
  1291. }
  1292. /**
  1293. * d_hash_and_lookup - hash the qstr then search for a dentry
  1294. * @dir: Directory to search in
  1295. * @name: qstr of name we wish to find
  1296. *
  1297. * On hash failure or on lookup failure NULL is returned.
  1298. */
  1299. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1300. {
  1301. struct dentry *dentry = NULL;
  1302. /*
  1303. * Check for a fs-specific hash function. Note that we must
  1304. * calculate the standard hash first, as the d_op->d_hash()
  1305. * routine may choose to leave the hash value unchanged.
  1306. */
  1307. name->hash = full_name_hash(name->name, name->len);
  1308. if (dir->d_op && dir->d_op->d_hash) {
  1309. if (dir->d_op->d_hash(dir, name) < 0)
  1310. goto out;
  1311. }
  1312. dentry = d_lookup(dir, name);
  1313. out:
  1314. return dentry;
  1315. }
  1316. /**
  1317. * d_validate - verify dentry provided from insecure source
  1318. * @dentry: The dentry alleged to be valid child of @dparent
  1319. * @dparent: The parent dentry (known to be valid)
  1320. *
  1321. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1322. * This is used by ncpfs in its readdir implementation.
  1323. * Zero is returned in the dentry is invalid.
  1324. */
  1325. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1326. {
  1327. struct hlist_head *base;
  1328. struct hlist_node *lhp;
  1329. /* Check whether the ptr might be valid at all.. */
  1330. if (!kmem_ptr_validate(dentry_cache, dentry))
  1331. goto out;
  1332. if (dentry->d_parent != dparent)
  1333. goto out;
  1334. spin_lock(&dcache_lock);
  1335. base = d_hash(dparent, dentry->d_name.hash);
  1336. hlist_for_each(lhp,base) {
  1337. /* hlist_for_each_entry_rcu() not required for d_hash list
  1338. * as it is parsed under dcache_lock
  1339. */
  1340. if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
  1341. __dget_locked(dentry);
  1342. spin_unlock(&dcache_lock);
  1343. return 1;
  1344. }
  1345. }
  1346. spin_unlock(&dcache_lock);
  1347. out:
  1348. return 0;
  1349. }
  1350. /*
  1351. * When a file is deleted, we have two options:
  1352. * - turn this dentry into a negative dentry
  1353. * - unhash this dentry and free it.
  1354. *
  1355. * Usually, we want to just turn this into
  1356. * a negative dentry, but if anybody else is
  1357. * currently using the dentry or the inode
  1358. * we can't do that and we fall back on removing
  1359. * it from the hash queues and waiting for
  1360. * it to be deleted later when it has no users
  1361. */
  1362. /**
  1363. * d_delete - delete a dentry
  1364. * @dentry: The dentry to delete
  1365. *
  1366. * Turn the dentry into a negative dentry if possible, otherwise
  1367. * remove it from the hash queues so it can be deleted later
  1368. */
  1369. void d_delete(struct dentry * dentry)
  1370. {
  1371. int isdir = 0;
  1372. /*
  1373. * Are we the only user?
  1374. */
  1375. spin_lock(&dcache_lock);
  1376. spin_lock(&dentry->d_lock);
  1377. isdir = S_ISDIR(dentry->d_inode->i_mode);
  1378. if (atomic_read(&dentry->d_count) == 1) {
  1379. dentry_iput(dentry);
  1380. fsnotify_nameremove(dentry, isdir);
  1381. return;
  1382. }
  1383. if (!d_unhashed(dentry))
  1384. __d_drop(dentry);
  1385. spin_unlock(&dentry->d_lock);
  1386. spin_unlock(&dcache_lock);
  1387. fsnotify_nameremove(dentry, isdir);
  1388. }
  1389. static void __d_rehash(struct dentry * entry, struct hlist_head *list)
  1390. {
  1391. entry->d_flags &= ~DCACHE_UNHASHED;
  1392. hlist_add_head_rcu(&entry->d_hash, list);
  1393. }
  1394. static void _d_rehash(struct dentry * entry)
  1395. {
  1396. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1397. }
  1398. /**
  1399. * d_rehash - add an entry back to the hash
  1400. * @entry: dentry to add to the hash
  1401. *
  1402. * Adds a dentry to the hash according to its name.
  1403. */
  1404. void d_rehash(struct dentry * entry)
  1405. {
  1406. spin_lock(&dcache_lock);
  1407. spin_lock(&entry->d_lock);
  1408. _d_rehash(entry);
  1409. spin_unlock(&entry->d_lock);
  1410. spin_unlock(&dcache_lock);
  1411. }
  1412. /*
  1413. * When switching names, the actual string doesn't strictly have to
  1414. * be preserved in the target - because we're dropping the target
  1415. * anyway. As such, we can just do a simple memcpy() to copy over
  1416. * the new name before we switch.
  1417. *
  1418. * Note that we have to be a lot more careful about getting the hash
  1419. * switched - we have to switch the hash value properly even if it
  1420. * then no longer matches the actual (corrupted) string of the target.
  1421. * The hash value has to match the hash queue that the dentry is on..
  1422. */
  1423. static void switch_names(struct dentry *dentry, struct dentry *target)
  1424. {
  1425. if (dname_external(target)) {
  1426. if (dname_external(dentry)) {
  1427. /*
  1428. * Both external: swap the pointers
  1429. */
  1430. swap(target->d_name.name, dentry->d_name.name);
  1431. } else {
  1432. /*
  1433. * dentry:internal, target:external. Steal target's
  1434. * storage and make target internal.
  1435. */
  1436. memcpy(target->d_iname, dentry->d_name.name,
  1437. dentry->d_name.len + 1);
  1438. dentry->d_name.name = target->d_name.name;
  1439. target->d_name.name = target->d_iname;
  1440. }
  1441. } else {
  1442. if (dname_external(dentry)) {
  1443. /*
  1444. * dentry:external, target:internal. Give dentry's
  1445. * storage to target and make dentry internal
  1446. */
  1447. memcpy(dentry->d_iname, target->d_name.name,
  1448. target->d_name.len + 1);
  1449. target->d_name.name = dentry->d_name.name;
  1450. dentry->d_name.name = dentry->d_iname;
  1451. } else {
  1452. /*
  1453. * Both are internal. Just copy target to dentry
  1454. */
  1455. memcpy(dentry->d_iname, target->d_name.name,
  1456. target->d_name.len + 1);
  1457. dentry->d_name.len = target->d_name.len;
  1458. return;
  1459. }
  1460. }
  1461. swap(dentry->d_name.len, target->d_name.len);
  1462. }
  1463. /*
  1464. * We cannibalize "target" when moving dentry on top of it,
  1465. * because it's going to be thrown away anyway. We could be more
  1466. * polite about it, though.
  1467. *
  1468. * This forceful removal will result in ugly /proc output if
  1469. * somebody holds a file open that got deleted due to a rename.
  1470. * We could be nicer about the deleted file, and let it show
  1471. * up under the name it had before it was deleted rather than
  1472. * under the original name of the file that was moved on top of it.
  1473. */
  1474. /*
  1475. * d_move_locked - move a dentry
  1476. * @dentry: entry to move
  1477. * @target: new dentry
  1478. *
  1479. * Update the dcache to reflect the move of a file name. Negative
  1480. * dcache entries should not be moved in this way.
  1481. */
  1482. static void d_move_locked(struct dentry * dentry, struct dentry * target)
  1483. {
  1484. struct hlist_head *list;
  1485. if (!dentry->d_inode)
  1486. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  1487. write_seqlock(&rename_lock);
  1488. /*
  1489. * XXXX: do we really need to take target->d_lock?
  1490. */
  1491. if (target < dentry) {
  1492. spin_lock(&target->d_lock);
  1493. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1494. } else {
  1495. spin_lock(&dentry->d_lock);
  1496. spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
  1497. }
  1498. /* Move the dentry to the target hash queue, if on different bucket */
  1499. if (d_unhashed(dentry))
  1500. goto already_unhashed;
  1501. hlist_del_rcu(&dentry->d_hash);
  1502. already_unhashed:
  1503. list = d_hash(target->d_parent, target->d_name.hash);
  1504. __d_rehash(dentry, list);
  1505. /* Unhash the target: dput() will then get rid of it */
  1506. __d_drop(target);
  1507. list_del(&dentry->d_u.d_child);
  1508. list_del(&target->d_u.d_child);
  1509. /* Switch the names.. */
  1510. switch_names(dentry, target);
  1511. swap(dentry->d_name.hash, target->d_name.hash);
  1512. /* ... and switch the parents */
  1513. if (IS_ROOT(dentry)) {
  1514. dentry->d_parent = target->d_parent;
  1515. target->d_parent = target;
  1516. INIT_LIST_HEAD(&target->d_u.d_child);
  1517. } else {
  1518. swap(dentry->d_parent, target->d_parent);
  1519. /* And add them back to the (new) parent lists */
  1520. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  1521. }
  1522. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1523. spin_unlock(&target->d_lock);
  1524. fsnotify_d_move(dentry);
  1525. spin_unlock(&dentry->d_lock);
  1526. write_sequnlock(&rename_lock);
  1527. }
  1528. /**
  1529. * d_move - move a dentry
  1530. * @dentry: entry to move
  1531. * @target: new dentry
  1532. *
  1533. * Update the dcache to reflect the move of a file name. Negative
  1534. * dcache entries should not be moved in this way.
  1535. */
  1536. void d_move(struct dentry * dentry, struct dentry * target)
  1537. {
  1538. spin_lock(&dcache_lock);
  1539. d_move_locked(dentry, target);
  1540. spin_unlock(&dcache_lock);
  1541. }
  1542. /**
  1543. * d_ancestor - search for an ancestor
  1544. * @p1: ancestor dentry
  1545. * @p2: child dentry
  1546. *
  1547. * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
  1548. * an ancestor of p2, else NULL.
  1549. */
  1550. struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
  1551. {
  1552. struct dentry *p;
  1553. for (p = p2; !IS_ROOT(p); p = p->d_parent) {
  1554. if (p->d_parent == p1)
  1555. return p;
  1556. }
  1557. return NULL;
  1558. }
  1559. /*
  1560. * This helper attempts to cope with remotely renamed directories
  1561. *
  1562. * It assumes that the caller is already holding
  1563. * dentry->d_parent->d_inode->i_mutex and the dcache_lock
  1564. *
  1565. * Note: If ever the locking in lock_rename() changes, then please
  1566. * remember to update this too...
  1567. */
  1568. static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
  1569. __releases(dcache_lock)
  1570. {
  1571. struct mutex *m1 = NULL, *m2 = NULL;
  1572. struct dentry *ret;
  1573. /* If alias and dentry share a parent, then no extra locks required */
  1574. if (alias->d_parent == dentry->d_parent)
  1575. goto out_unalias;
  1576. /* Check for loops */
  1577. ret = ERR_PTR(-ELOOP);
  1578. if (d_ancestor(alias, dentry))
  1579. goto out_err;
  1580. /* See lock_rename() */
  1581. ret = ERR_PTR(-EBUSY);
  1582. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  1583. goto out_err;
  1584. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  1585. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  1586. goto out_err;
  1587. m2 = &alias->d_parent->d_inode->i_mutex;
  1588. out_unalias:
  1589. d_move_locked(alias, dentry);
  1590. ret = alias;
  1591. out_err:
  1592. spin_unlock(&dcache_lock);
  1593. if (m2)
  1594. mutex_unlock(m2);
  1595. if (m1)
  1596. mutex_unlock(m1);
  1597. return ret;
  1598. }
  1599. /*
  1600. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  1601. * named dentry in place of the dentry to be replaced.
  1602. */
  1603. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  1604. {
  1605. struct dentry *dparent, *aparent;
  1606. switch_names(dentry, anon);
  1607. swap(dentry->d_name.hash, anon->d_name.hash);
  1608. dparent = dentry->d_parent;
  1609. aparent = anon->d_parent;
  1610. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  1611. list_del(&dentry->d_u.d_child);
  1612. if (!IS_ROOT(dentry))
  1613. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1614. else
  1615. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1616. anon->d_parent = (dparent == dentry) ? anon : dparent;
  1617. list_del(&anon->d_u.d_child);
  1618. if (!IS_ROOT(anon))
  1619. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  1620. else
  1621. INIT_LIST_HEAD(&anon->d_u.d_child);
  1622. anon->d_flags &= ~DCACHE_DISCONNECTED;
  1623. }
  1624. /**
  1625. * d_materialise_unique - introduce an inode into the tree
  1626. * @dentry: candidate dentry
  1627. * @inode: inode to bind to the dentry, to which aliases may be attached
  1628. *
  1629. * Introduces an dentry into the tree, substituting an extant disconnected
  1630. * root directory alias in its place if there is one
  1631. */
  1632. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  1633. {
  1634. struct dentry *actual;
  1635. BUG_ON(!d_unhashed(dentry));
  1636. spin_lock(&dcache_lock);
  1637. if (!inode) {
  1638. actual = dentry;
  1639. __d_instantiate(dentry, NULL);
  1640. goto found_lock;
  1641. }
  1642. if (S_ISDIR(inode->i_mode)) {
  1643. struct dentry *alias;
  1644. /* Does an aliased dentry already exist? */
  1645. alias = __d_find_alias(inode, 0);
  1646. if (alias) {
  1647. actual = alias;
  1648. /* Is this an anonymous mountpoint that we could splice
  1649. * into our tree? */
  1650. if (IS_ROOT(alias)) {
  1651. spin_lock(&alias->d_lock);
  1652. __d_materialise_dentry(dentry, alias);
  1653. __d_drop(alias);
  1654. goto found;
  1655. }
  1656. /* Nope, but we must(!) avoid directory aliasing */
  1657. actual = __d_unalias(dentry, alias);
  1658. if (IS_ERR(actual))
  1659. dput(alias);
  1660. goto out_nolock;
  1661. }
  1662. }
  1663. /* Add a unique reference */
  1664. actual = __d_instantiate_unique(dentry, inode);
  1665. if (!actual)
  1666. actual = dentry;
  1667. else if (unlikely(!d_unhashed(actual)))
  1668. goto shouldnt_be_hashed;
  1669. found_lock:
  1670. spin_lock(&actual->d_lock);
  1671. found:
  1672. _d_rehash(actual);
  1673. spin_unlock(&actual->d_lock);
  1674. spin_unlock(&dcache_lock);
  1675. out_nolock:
  1676. if (actual == dentry) {
  1677. security_d_instantiate(dentry, inode);
  1678. return NULL;
  1679. }
  1680. iput(inode);
  1681. return actual;
  1682. shouldnt_be_hashed:
  1683. spin_unlock(&dcache_lock);
  1684. BUG();
  1685. }
  1686. static int prepend(char **buffer, int *buflen, const char *str, int namelen)
  1687. {
  1688. *buflen -= namelen;
  1689. if (*buflen < 0)
  1690. return -ENAMETOOLONG;
  1691. *buffer -= namelen;
  1692. memcpy(*buffer, str, namelen);
  1693. return 0;
  1694. }
  1695. static int prepend_name(char **buffer, int *buflen, struct qstr *name)
  1696. {
  1697. return prepend(buffer, buflen, name->name, name->len);
  1698. }
  1699. /**
  1700. * __d_path - return the path of a dentry
  1701. * @path: the dentry/vfsmount to report
  1702. * @root: root vfsmnt/dentry (may be modified by this function)
  1703. * @buffer: buffer to return value in
  1704. * @buflen: buffer length
  1705. *
  1706. * Convert a dentry into an ASCII path name. If the entry has been deleted
  1707. * the string " (deleted)" is appended. Note that this is ambiguous.
  1708. *
  1709. * Returns a pointer into the buffer or an error code if the
  1710. * path was too long.
  1711. *
  1712. * "buflen" should be positive. Caller holds the dcache_lock.
  1713. *
  1714. * If path is not reachable from the supplied root, then the value of
  1715. * root is changed (without modifying refcounts).
  1716. */
  1717. char *__d_path(const struct path *path, struct path *root,
  1718. char *buffer, int buflen)
  1719. {
  1720. struct dentry *dentry = path->dentry;
  1721. struct vfsmount *vfsmnt = path->mnt;
  1722. char *end = buffer + buflen;
  1723. char *retval;
  1724. spin_lock(&vfsmount_lock);
  1725. prepend(&end, &buflen, "\0", 1);
  1726. if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
  1727. (prepend(&end, &buflen, " (deleted)", 10) != 0))
  1728. goto Elong;
  1729. if (buflen < 1)
  1730. goto Elong;
  1731. /* Get '/' right */
  1732. retval = end-1;
  1733. *retval = '/';
  1734. for (;;) {
  1735. struct dentry * parent;
  1736. if (dentry == root->dentry && vfsmnt == root->mnt)
  1737. break;
  1738. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  1739. /* Global root? */
  1740. if (vfsmnt->mnt_parent == vfsmnt) {
  1741. goto global_root;
  1742. }
  1743. dentry = vfsmnt->mnt_mountpoint;
  1744. vfsmnt = vfsmnt->mnt_parent;
  1745. continue;
  1746. }
  1747. parent = dentry->d_parent;
  1748. prefetch(parent);
  1749. if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
  1750. (prepend(&end, &buflen, "/", 1) != 0))
  1751. goto Elong;
  1752. retval = end;
  1753. dentry = parent;
  1754. }
  1755. out:
  1756. spin_unlock(&vfsmount_lock);
  1757. return retval;
  1758. global_root:
  1759. retval += 1; /* hit the slash */
  1760. if (prepend_name(&retval, &buflen, &dentry->d_name) != 0)
  1761. goto Elong;
  1762. root->mnt = vfsmnt;
  1763. root->dentry = dentry;
  1764. goto out;
  1765. Elong:
  1766. retval = ERR_PTR(-ENAMETOOLONG);
  1767. goto out;
  1768. }
  1769. /**
  1770. * d_path - return the path of a dentry
  1771. * @path: path to report
  1772. * @buf: buffer to return value in
  1773. * @buflen: buffer length
  1774. *
  1775. * Convert a dentry into an ASCII path name. If the entry has been deleted
  1776. * the string " (deleted)" is appended. Note that this is ambiguous.
  1777. *
  1778. * Returns a pointer into the buffer or an error code if the path was
  1779. * too long. Note: Callers should use the returned pointer, not the passed
  1780. * in buffer, to use the name! The implementation often starts at an offset
  1781. * into the buffer, and may leave 0 bytes at the start.
  1782. *
  1783. * "buflen" should be positive.
  1784. */
  1785. char *d_path(const struct path *path, char *buf, int buflen)
  1786. {
  1787. char *res;
  1788. struct path root;
  1789. struct path tmp;
  1790. /*
  1791. * We have various synthetic filesystems that never get mounted. On
  1792. * these filesystems dentries are never used for lookup purposes, and
  1793. * thus don't need to be hashed. They also don't need a name until a
  1794. * user wants to identify the object in /proc/pid/fd/. The little hack
  1795. * below allows us to generate a name for these objects on demand:
  1796. */
  1797. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  1798. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  1799. read_lock(&current->fs->lock);
  1800. root = current->fs->root;
  1801. path_get(&root);
  1802. read_unlock(&current->fs->lock);
  1803. spin_lock(&dcache_lock);
  1804. tmp = root;
  1805. res = __d_path(path, &tmp, buf, buflen);
  1806. spin_unlock(&dcache_lock);
  1807. path_put(&root);
  1808. return res;
  1809. }
  1810. /*
  1811. * Helper function for dentry_operations.d_dname() members
  1812. */
  1813. char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
  1814. const char *fmt, ...)
  1815. {
  1816. va_list args;
  1817. char temp[64];
  1818. int sz;
  1819. va_start(args, fmt);
  1820. sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
  1821. va_end(args);
  1822. if (sz > sizeof(temp) || sz > buflen)
  1823. return ERR_PTR(-ENAMETOOLONG);
  1824. buffer += buflen - sz;
  1825. return memcpy(buffer, temp, sz);
  1826. }
  1827. /*
  1828. * Write full pathname from the root of the filesystem into the buffer.
  1829. */
  1830. char *dentry_path(struct dentry *dentry, char *buf, int buflen)
  1831. {
  1832. char *end = buf + buflen;
  1833. char *retval;
  1834. spin_lock(&dcache_lock);
  1835. prepend(&end, &buflen, "\0", 1);
  1836. if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
  1837. (prepend(&end, &buflen, "//deleted", 9) != 0))
  1838. goto Elong;
  1839. if (buflen < 1)
  1840. goto Elong;
  1841. /* Get '/' right */
  1842. retval = end-1;
  1843. *retval = '/';
  1844. while (!IS_ROOT(dentry)) {
  1845. struct dentry *parent = dentry->d_parent;
  1846. prefetch(parent);
  1847. if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
  1848. (prepend(&end, &buflen, "/", 1) != 0))
  1849. goto Elong;
  1850. retval = end;
  1851. dentry = parent;
  1852. }
  1853. spin_unlock(&dcache_lock);
  1854. return retval;
  1855. Elong:
  1856. spin_unlock(&dcache_lock);
  1857. return ERR_PTR(-ENAMETOOLONG);
  1858. }
  1859. /*
  1860. * NOTE! The user-level library version returns a
  1861. * character pointer. The kernel system call just
  1862. * returns the length of the buffer filled (which
  1863. * includes the ending '\0' character), or a negative
  1864. * error value. So libc would do something like
  1865. *
  1866. * char *getcwd(char * buf, size_t size)
  1867. * {
  1868. * int retval;
  1869. *
  1870. * retval = sys_getcwd(buf, size);
  1871. * if (retval >= 0)
  1872. * return buf;
  1873. * errno = -retval;
  1874. * return NULL;
  1875. * }
  1876. */
  1877. SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
  1878. {
  1879. int error;
  1880. struct path pwd, root;
  1881. char *page = (char *) __get_free_page(GFP_USER);
  1882. if (!page)
  1883. return -ENOMEM;
  1884. read_lock(&current->fs->lock);
  1885. pwd = current->fs->pwd;
  1886. path_get(&pwd);
  1887. root = current->fs->root;
  1888. path_get(&root);
  1889. read_unlock(&current->fs->lock);
  1890. error = -ENOENT;
  1891. /* Has the current directory has been unlinked? */
  1892. spin_lock(&dcache_lock);
  1893. if (IS_ROOT(pwd.dentry) || !d_unhashed(pwd.dentry)) {
  1894. unsigned long len;
  1895. struct path tmp = root;
  1896. char * cwd;
  1897. cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
  1898. spin_unlock(&dcache_lock);
  1899. error = PTR_ERR(cwd);
  1900. if (IS_ERR(cwd))
  1901. goto out;
  1902. error = -ERANGE;
  1903. len = PAGE_SIZE + page - cwd;
  1904. if (len <= size) {
  1905. error = len;
  1906. if (copy_to_user(buf, cwd, len))
  1907. error = -EFAULT;
  1908. }
  1909. } else
  1910. spin_unlock(&dcache_lock);
  1911. out:
  1912. path_put(&pwd);
  1913. path_put(&root);
  1914. free_page((unsigned long) page);
  1915. return error;
  1916. }
  1917. /*
  1918. * Test whether new_dentry is a subdirectory of old_dentry.
  1919. *
  1920. * Trivially implemented using the dcache structure
  1921. */
  1922. /**
  1923. * is_subdir - is new dentry a subdirectory of old_dentry
  1924. * @new_dentry: new dentry
  1925. * @old_dentry: old dentry
  1926. *
  1927. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  1928. * Returns 0 otherwise.
  1929. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  1930. */
  1931. int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
  1932. {
  1933. int result;
  1934. unsigned long seq;
  1935. /* FIXME: This is old behavior, needed? Please check callers. */
  1936. if (new_dentry == old_dentry)
  1937. return 1;
  1938. /*
  1939. * Need rcu_readlock to protect against the d_parent trashing
  1940. * due to d_move
  1941. */
  1942. rcu_read_lock();
  1943. do {
  1944. /* for restarting inner loop in case of seq retry */
  1945. seq = read_seqbegin(&rename_lock);
  1946. if (d_ancestor(old_dentry, new_dentry))
  1947. result = 1;
  1948. else
  1949. result = 0;
  1950. } while (read_seqretry(&rename_lock, seq));
  1951. rcu_read_unlock();
  1952. return result;
  1953. }
  1954. void d_genocide(struct dentry *root)
  1955. {
  1956. struct dentry *this_parent = root;
  1957. struct list_head *next;
  1958. spin_lock(&dcache_lock);
  1959. repeat:
  1960. next = this_parent->d_subdirs.next;
  1961. resume:
  1962. while (next != &this_parent->d_subdirs) {
  1963. struct list_head *tmp = next;
  1964. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  1965. next = tmp->next;
  1966. if (d_unhashed(dentry)||!dentry->d_inode)
  1967. continue;
  1968. if (!list_empty(&dentry->d_subdirs)) {
  1969. this_parent = dentry;
  1970. goto repeat;
  1971. }
  1972. atomic_dec(&dentry->d_count);
  1973. }
  1974. if (this_parent != root) {
  1975. next = this_parent->d_u.d_child.next;
  1976. atomic_dec(&this_parent->d_count);
  1977. this_parent = this_parent->d_parent;
  1978. goto resume;
  1979. }
  1980. spin_unlock(&dcache_lock);
  1981. }
  1982. /**
  1983. * find_inode_number - check for dentry with name
  1984. * @dir: directory to check
  1985. * @name: Name to find.
  1986. *
  1987. * Check whether a dentry already exists for the given name,
  1988. * and return the inode number if it has an inode. Otherwise
  1989. * 0 is returned.
  1990. *
  1991. * This routine is used to post-process directory listings for
  1992. * filesystems using synthetic inode numbers, and is necessary
  1993. * to keep getcwd() working.
  1994. */
  1995. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  1996. {
  1997. struct dentry * dentry;
  1998. ino_t ino = 0;
  1999. dentry = d_hash_and_lookup(dir, name);
  2000. if (dentry) {
  2001. if (dentry->d_inode)
  2002. ino = dentry->d_inode->i_ino;
  2003. dput(dentry);
  2004. }
  2005. return ino;
  2006. }
  2007. static __initdata unsigned long dhash_entries;
  2008. static int __init set_dhash_entries(char *str)
  2009. {
  2010. if (!str)
  2011. return 0;
  2012. dhash_entries = simple_strtoul(str, &str, 0);
  2013. return 1;
  2014. }
  2015. __setup("dhash_entries=", set_dhash_entries);
  2016. static void __init dcache_init_early(void)
  2017. {
  2018. int loop;
  2019. /* If hashes are distributed across NUMA nodes, defer
  2020. * hash allocation until vmalloc space is available.
  2021. */
  2022. if (hashdist)
  2023. return;
  2024. dentry_hashtable =
  2025. alloc_large_system_hash("Dentry cache",
  2026. sizeof(struct hlist_head),
  2027. dhash_entries,
  2028. 13,
  2029. HASH_EARLY,
  2030. &d_hash_shift,
  2031. &d_hash_mask,
  2032. 0);
  2033. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2034. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  2035. }
  2036. static void __init dcache_init(void)
  2037. {
  2038. int loop;
  2039. /*
  2040. * A constructor could be added for stable state like the lists,
  2041. * but it is probably not worth it because of the cache nature
  2042. * of the dcache.
  2043. */
  2044. dentry_cache = KMEM_CACHE(dentry,
  2045. SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
  2046. register_shrinker(&dcache_shrinker);
  2047. /* Hash may have been set up in dcache_init_early */
  2048. if (!hashdist)
  2049. return;
  2050. dentry_hashtable =
  2051. alloc_large_system_hash("Dentry cache",
  2052. sizeof(struct hlist_head),
  2053. dhash_entries,
  2054. 13,
  2055. 0,
  2056. &d_hash_shift,
  2057. &d_hash_mask,
  2058. 0);
  2059. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2060. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  2061. }
  2062. /* SLAB cache for __getname() consumers */
  2063. struct kmem_cache *names_cachep __read_mostly;
  2064. EXPORT_SYMBOL(d_genocide);
  2065. void __init vfs_caches_init_early(void)
  2066. {
  2067. dcache_init_early();
  2068. inode_init_early();
  2069. }
  2070. void __init vfs_caches_init(unsigned long mempages)
  2071. {
  2072. unsigned long reserve;
  2073. /* Base hash sizes on available memory, with a reserve equal to
  2074. 150% of current kernel size */
  2075. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  2076. mempages -= reserve;
  2077. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  2078. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2079. dcache_init();
  2080. inode_init();
  2081. files_init(mempages);
  2082. mnt_init();
  2083. bdev_cache_init();
  2084. chrdev_init();
  2085. }
  2086. EXPORT_SYMBOL(d_alloc);
  2087. EXPORT_SYMBOL(d_alloc_root);
  2088. EXPORT_SYMBOL(d_delete);
  2089. EXPORT_SYMBOL(d_find_alias);
  2090. EXPORT_SYMBOL(d_instantiate);
  2091. EXPORT_SYMBOL(d_invalidate);
  2092. EXPORT_SYMBOL(d_lookup);
  2093. EXPORT_SYMBOL(d_move);
  2094. EXPORT_SYMBOL_GPL(d_materialise_unique);
  2095. EXPORT_SYMBOL(d_path);
  2096. EXPORT_SYMBOL(d_prune_aliases);
  2097. EXPORT_SYMBOL(d_rehash);
  2098. EXPORT_SYMBOL(d_splice_alias);
  2099. EXPORT_SYMBOL(d_add_ci);
  2100. EXPORT_SYMBOL(d_validate);
  2101. EXPORT_SYMBOL(dget_locked);
  2102. EXPORT_SYMBOL(dput);
  2103. EXPORT_SYMBOL(find_inode_number);
  2104. EXPORT_SYMBOL(have_submounts);
  2105. EXPORT_SYMBOL(names_cachep);
  2106. EXPORT_SYMBOL(shrink_dcache_parent);
  2107. EXPORT_SYMBOL(shrink_dcache_sb);