dcache.c 59 KB

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