dcache.c 60 KB

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