dcache.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023
  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 <linux/bit_spinlock.h>
  35. #include <linux/rculist_bl.h>
  36. #include <linux/prefetch.h>
  37. #include "internal.h"
  38. /*
  39. * Usage:
  40. * dcache->d_inode->i_lock protects:
  41. * - i_dentry, d_alias, d_inode of aliases
  42. * dcache_hash_bucket lock protects:
  43. * - the dcache hash table
  44. * s_anon bl list spinlock protects:
  45. * - the s_anon list (see __d_drop)
  46. * dcache_lru_lock protects:
  47. * - the dcache lru lists and counters
  48. * d_lock protects:
  49. * - d_flags
  50. * - d_name
  51. * - d_lru
  52. * - d_count
  53. * - d_unhashed()
  54. * - d_parent and d_subdirs
  55. * - childrens' d_child and d_parent
  56. * - d_alias, d_inode
  57. *
  58. * Ordering:
  59. * dentry->d_inode->i_lock
  60. * dentry->d_lock
  61. * dcache_lru_lock
  62. * dcache_hash_bucket lock
  63. * s_anon lock
  64. *
  65. * If there is an ancestor relationship:
  66. * dentry->d_parent->...->d_parent->d_lock
  67. * ...
  68. * dentry->d_parent->d_lock
  69. * dentry->d_lock
  70. *
  71. * If no ancestor relationship:
  72. * if (dentry1 < dentry2)
  73. * dentry1->d_lock
  74. * dentry2->d_lock
  75. */
  76. int sysctl_vfs_cache_pressure __read_mostly = 100;
  77. EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
  78. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
  79. __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
  80. EXPORT_SYMBOL(rename_lock);
  81. static struct kmem_cache *dentry_cache __read_mostly;
  82. /*
  83. * This is the single most critical data structure when it comes
  84. * to the dcache: the hashtable for lookups. Somebody should try
  85. * to make this good - I've just made it work.
  86. *
  87. * This hash-function tries to avoid losing too many bits of hash
  88. * information, yet avoid using a prime hash-size or similar.
  89. */
  90. #define D_HASHBITS d_hash_shift
  91. #define D_HASHMASK d_hash_mask
  92. static unsigned int d_hash_mask __read_mostly;
  93. static unsigned int d_hash_shift __read_mostly;
  94. static struct hlist_bl_head *dentry_hashtable __read_mostly;
  95. static inline struct hlist_bl_head *d_hash(struct dentry *parent,
  96. unsigned long hash)
  97. {
  98. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  99. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  100. return dentry_hashtable + (hash & D_HASHMASK);
  101. }
  102. /* Statistics gathering. */
  103. struct dentry_stat_t dentry_stat = {
  104. .age_limit = 45,
  105. };
  106. static DEFINE_PER_CPU(unsigned int, nr_dentry);
  107. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  108. static int get_nr_dentry(void)
  109. {
  110. int i;
  111. int sum = 0;
  112. for_each_possible_cpu(i)
  113. sum += per_cpu(nr_dentry, i);
  114. return sum < 0 ? 0 : sum;
  115. }
  116. int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
  117. size_t *lenp, loff_t *ppos)
  118. {
  119. dentry_stat.nr_dentry = get_nr_dentry();
  120. return proc_dointvec(table, write, buffer, lenp, ppos);
  121. }
  122. #endif
  123. static void __d_free(struct rcu_head *head)
  124. {
  125. struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
  126. WARN_ON(!list_empty(&dentry->d_alias));
  127. if (dname_external(dentry))
  128. kfree(dentry->d_name.name);
  129. kmem_cache_free(dentry_cache, dentry);
  130. }
  131. /*
  132. * no locks, please.
  133. */
  134. static void d_free(struct dentry *dentry)
  135. {
  136. BUG_ON(dentry->d_count);
  137. this_cpu_dec(nr_dentry);
  138. if (dentry->d_op && dentry->d_op->d_release)
  139. dentry->d_op->d_release(dentry);
  140. /* if dentry was never visible to RCU, immediate free is OK */
  141. if (!(dentry->d_flags & DCACHE_RCUACCESS))
  142. __d_free(&dentry->d_u.d_rcu);
  143. else
  144. call_rcu(&dentry->d_u.d_rcu, __d_free);
  145. }
  146. /**
  147. * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
  148. * @dentry: the target dentry
  149. * After this call, in-progress rcu-walk path lookup will fail. This
  150. * should be called after unhashing, and after changing d_inode (if
  151. * the dentry has not already been unhashed).
  152. */
  153. static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
  154. {
  155. assert_spin_locked(&dentry->d_lock);
  156. /* Go through a barrier */
  157. write_seqcount_barrier(&dentry->d_seq);
  158. }
  159. /*
  160. * Release the dentry's inode, using the filesystem
  161. * d_iput() operation if defined. Dentry has no refcount
  162. * and is unhashed.
  163. */
  164. static void dentry_iput(struct dentry * dentry)
  165. __releases(dentry->d_lock)
  166. __releases(dentry->d_inode->i_lock)
  167. {
  168. struct inode *inode = dentry->d_inode;
  169. if (inode) {
  170. dentry->d_inode = NULL;
  171. list_del_init(&dentry->d_alias);
  172. spin_unlock(&dentry->d_lock);
  173. spin_unlock(&inode->i_lock);
  174. if (!inode->i_nlink)
  175. fsnotify_inoderemove(inode);
  176. if (dentry->d_op && dentry->d_op->d_iput)
  177. dentry->d_op->d_iput(dentry, inode);
  178. else
  179. iput(inode);
  180. } else {
  181. spin_unlock(&dentry->d_lock);
  182. }
  183. }
  184. /*
  185. * Release the dentry's inode, using the filesystem
  186. * d_iput() operation if defined. dentry remains in-use.
  187. */
  188. static void dentry_unlink_inode(struct dentry * dentry)
  189. __releases(dentry->d_lock)
  190. __releases(dentry->d_inode->i_lock)
  191. {
  192. struct inode *inode = dentry->d_inode;
  193. dentry->d_inode = NULL;
  194. list_del_init(&dentry->d_alias);
  195. dentry_rcuwalk_barrier(dentry);
  196. spin_unlock(&dentry->d_lock);
  197. spin_unlock(&inode->i_lock);
  198. if (!inode->i_nlink)
  199. fsnotify_inoderemove(inode);
  200. if (dentry->d_op && dentry->d_op->d_iput)
  201. dentry->d_op->d_iput(dentry, inode);
  202. else
  203. iput(inode);
  204. }
  205. /*
  206. * dentry_lru_(add|del|move_tail) must be called with d_lock held.
  207. */
  208. static void dentry_lru_add(struct dentry *dentry)
  209. {
  210. if (list_empty(&dentry->d_lru)) {
  211. spin_lock(&dcache_lru_lock);
  212. list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
  213. dentry->d_sb->s_nr_dentry_unused++;
  214. dentry_stat.nr_unused++;
  215. spin_unlock(&dcache_lru_lock);
  216. }
  217. }
  218. static void __dentry_lru_del(struct dentry *dentry)
  219. {
  220. list_del_init(&dentry->d_lru);
  221. dentry->d_sb->s_nr_dentry_unused--;
  222. dentry_stat.nr_unused--;
  223. }
  224. static void dentry_lru_del(struct dentry *dentry)
  225. {
  226. if (!list_empty(&dentry->d_lru)) {
  227. spin_lock(&dcache_lru_lock);
  228. __dentry_lru_del(dentry);
  229. spin_unlock(&dcache_lru_lock);
  230. }
  231. }
  232. static void dentry_lru_move_tail(struct dentry *dentry)
  233. {
  234. spin_lock(&dcache_lru_lock);
  235. if (list_empty(&dentry->d_lru)) {
  236. list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
  237. dentry->d_sb->s_nr_dentry_unused++;
  238. dentry_stat.nr_unused++;
  239. } else {
  240. list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
  241. }
  242. spin_unlock(&dcache_lru_lock);
  243. }
  244. /**
  245. * d_kill - kill dentry and return parent
  246. * @dentry: dentry to kill
  247. * @parent: parent dentry
  248. *
  249. * The dentry must already be unhashed and removed from the LRU.
  250. *
  251. * If this is the root of the dentry tree, return NULL.
  252. *
  253. * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
  254. * d_kill.
  255. */
  256. static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
  257. __releases(dentry->d_lock)
  258. __releases(parent->d_lock)
  259. __releases(dentry->d_inode->i_lock)
  260. {
  261. list_del(&dentry->d_u.d_child);
  262. /*
  263. * Inform try_to_ascend() that we are no longer attached to the
  264. * dentry tree
  265. */
  266. dentry->d_flags |= DCACHE_DISCONNECTED;
  267. if (parent)
  268. spin_unlock(&parent->d_lock);
  269. dentry_iput(dentry);
  270. /*
  271. * dentry_iput drops the locks, at which point nobody (except
  272. * transient RCU lookups) can reach this dentry.
  273. */
  274. d_free(dentry);
  275. return parent;
  276. }
  277. /**
  278. * d_drop - drop a dentry
  279. * @dentry: dentry to drop
  280. *
  281. * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
  282. * be found through a VFS lookup any more. Note that this is different from
  283. * deleting the dentry - d_delete will try to mark the dentry negative if
  284. * possible, giving a successful _negative_ lookup, while d_drop will
  285. * just make the cache lookup fail.
  286. *
  287. * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
  288. * reason (NFS timeouts or autofs deletes).
  289. *
  290. * __d_drop requires dentry->d_lock.
  291. */
  292. void __d_drop(struct dentry *dentry)
  293. {
  294. if (!d_unhashed(dentry)) {
  295. struct hlist_bl_head *b;
  296. if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
  297. b = &dentry->d_sb->s_anon;
  298. else
  299. b = d_hash(dentry->d_parent, dentry->d_name.hash);
  300. hlist_bl_lock(b);
  301. __hlist_bl_del(&dentry->d_hash);
  302. dentry->d_hash.pprev = NULL;
  303. hlist_bl_unlock(b);
  304. dentry_rcuwalk_barrier(dentry);
  305. }
  306. }
  307. EXPORT_SYMBOL(__d_drop);
  308. void d_drop(struct dentry *dentry)
  309. {
  310. spin_lock(&dentry->d_lock);
  311. __d_drop(dentry);
  312. spin_unlock(&dentry->d_lock);
  313. }
  314. EXPORT_SYMBOL(d_drop);
  315. /*
  316. * d_clear_need_lookup - drop a dentry from cache and clear the need lookup flag
  317. * @dentry: dentry to drop
  318. *
  319. * This is called when we do a lookup on a placeholder dentry that needed to be
  320. * looked up. The dentry should have been hashed in order for it to be found by
  321. * the lookup code, but now needs to be unhashed while we do the actual lookup
  322. * and clear the DCACHE_NEED_LOOKUP flag.
  323. */
  324. void d_clear_need_lookup(struct dentry *dentry)
  325. {
  326. spin_lock(&dentry->d_lock);
  327. __d_drop(dentry);
  328. dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  329. spin_unlock(&dentry->d_lock);
  330. }
  331. EXPORT_SYMBOL(d_clear_need_lookup);
  332. /*
  333. * Finish off a dentry we've decided to kill.
  334. * dentry->d_lock must be held, returns with it unlocked.
  335. * If ref is non-zero, then decrement the refcount too.
  336. * Returns dentry requiring refcount drop, or NULL if we're done.
  337. */
  338. static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
  339. __releases(dentry->d_lock)
  340. {
  341. struct inode *inode;
  342. struct dentry *parent;
  343. inode = dentry->d_inode;
  344. if (inode && !spin_trylock(&inode->i_lock)) {
  345. relock:
  346. spin_unlock(&dentry->d_lock);
  347. cpu_relax();
  348. return dentry; /* try again with same dentry */
  349. }
  350. if (IS_ROOT(dentry))
  351. parent = NULL;
  352. else
  353. parent = dentry->d_parent;
  354. if (parent && !spin_trylock(&parent->d_lock)) {
  355. if (inode)
  356. spin_unlock(&inode->i_lock);
  357. goto relock;
  358. }
  359. if (ref)
  360. dentry->d_count--;
  361. /* if dentry was on the d_lru list delete it from there */
  362. dentry_lru_del(dentry);
  363. /* if it was on the hash then remove it */
  364. __d_drop(dentry);
  365. return d_kill(dentry, parent);
  366. }
  367. /*
  368. * This is dput
  369. *
  370. * This is complicated by the fact that we do not want to put
  371. * dentries that are no longer on any hash chain on the unused
  372. * list: we'd much rather just get rid of them immediately.
  373. *
  374. * However, that implies that we have to traverse the dentry
  375. * tree upwards to the parents which might _also_ now be
  376. * scheduled for deletion (it may have been only waiting for
  377. * its last child to go away).
  378. *
  379. * This tail recursion is done by hand as we don't want to depend
  380. * on the compiler to always get this right (gcc generally doesn't).
  381. * Real recursion would eat up our stack space.
  382. */
  383. /*
  384. * dput - release a dentry
  385. * @dentry: dentry to release
  386. *
  387. * Release a dentry. This will drop the usage count and if appropriate
  388. * call the dentry unlink method as well as removing it from the queues and
  389. * releasing its resources. If the parent dentries were scheduled for release
  390. * they too may now get deleted.
  391. */
  392. void dput(struct dentry *dentry)
  393. {
  394. if (!dentry)
  395. return;
  396. repeat:
  397. if (dentry->d_count == 1)
  398. might_sleep();
  399. spin_lock(&dentry->d_lock);
  400. BUG_ON(!dentry->d_count);
  401. if (dentry->d_count > 1) {
  402. dentry->d_count--;
  403. spin_unlock(&dentry->d_lock);
  404. return;
  405. }
  406. if (dentry->d_flags & DCACHE_OP_DELETE) {
  407. if (dentry->d_op->d_delete(dentry))
  408. goto kill_it;
  409. }
  410. /* Unreachable? Get rid of it */
  411. if (d_unhashed(dentry))
  412. goto kill_it;
  413. /*
  414. * If this dentry needs lookup, don't set the referenced flag so that it
  415. * is more likely to be cleaned up by the dcache shrinker in case of
  416. * memory pressure.
  417. */
  418. if (!d_need_lookup(dentry))
  419. dentry->d_flags |= DCACHE_REFERENCED;
  420. dentry_lru_add(dentry);
  421. dentry->d_count--;
  422. spin_unlock(&dentry->d_lock);
  423. return;
  424. kill_it:
  425. dentry = dentry_kill(dentry, 1);
  426. if (dentry)
  427. goto repeat;
  428. }
  429. EXPORT_SYMBOL(dput);
  430. /**
  431. * d_invalidate - invalidate a dentry
  432. * @dentry: dentry to invalidate
  433. *
  434. * Try to invalidate the dentry if it turns out to be
  435. * possible. If there are other dentries that can be
  436. * reached through this one we can't delete it and we
  437. * return -EBUSY. On success we return 0.
  438. *
  439. * no dcache lock.
  440. */
  441. int d_invalidate(struct dentry * dentry)
  442. {
  443. /*
  444. * If it's already been dropped, return OK.
  445. */
  446. spin_lock(&dentry->d_lock);
  447. if (d_unhashed(dentry)) {
  448. spin_unlock(&dentry->d_lock);
  449. return 0;
  450. }
  451. /*
  452. * Check whether to do a partial shrink_dcache
  453. * to get rid of unused child entries.
  454. */
  455. if (!list_empty(&dentry->d_subdirs)) {
  456. spin_unlock(&dentry->d_lock);
  457. shrink_dcache_parent(dentry);
  458. spin_lock(&dentry->d_lock);
  459. }
  460. /*
  461. * Somebody else still using it?
  462. *
  463. * If it's a directory, we can't drop it
  464. * for fear of somebody re-populating it
  465. * with children (even though dropping it
  466. * would make it unreachable from the root,
  467. * we might still populate it if it was a
  468. * working directory or similar).
  469. */
  470. if (dentry->d_count > 1) {
  471. if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
  472. spin_unlock(&dentry->d_lock);
  473. return -EBUSY;
  474. }
  475. }
  476. __d_drop(dentry);
  477. spin_unlock(&dentry->d_lock);
  478. return 0;
  479. }
  480. EXPORT_SYMBOL(d_invalidate);
  481. /* This must be called with d_lock held */
  482. static inline void __dget_dlock(struct dentry *dentry)
  483. {
  484. dentry->d_count++;
  485. }
  486. static inline void __dget(struct dentry *dentry)
  487. {
  488. spin_lock(&dentry->d_lock);
  489. __dget_dlock(dentry);
  490. spin_unlock(&dentry->d_lock);
  491. }
  492. struct dentry *dget_parent(struct dentry *dentry)
  493. {
  494. struct dentry *ret;
  495. repeat:
  496. /*
  497. * Don't need rcu_dereference because we re-check it was correct under
  498. * the lock.
  499. */
  500. rcu_read_lock();
  501. ret = dentry->d_parent;
  502. spin_lock(&ret->d_lock);
  503. if (unlikely(ret != dentry->d_parent)) {
  504. spin_unlock(&ret->d_lock);
  505. rcu_read_unlock();
  506. goto repeat;
  507. }
  508. rcu_read_unlock();
  509. BUG_ON(!ret->d_count);
  510. ret->d_count++;
  511. spin_unlock(&ret->d_lock);
  512. return ret;
  513. }
  514. EXPORT_SYMBOL(dget_parent);
  515. /**
  516. * d_find_alias - grab a hashed alias of inode
  517. * @inode: inode in question
  518. * @want_discon: flag, used by d_splice_alias, to request
  519. * that only a DISCONNECTED alias be returned.
  520. *
  521. * If inode has a hashed alias, or is a directory and has any alias,
  522. * acquire the reference to alias and return it. Otherwise return NULL.
  523. * Notice that if inode is a directory there can be only one alias and
  524. * it can be unhashed only if it has no children, or if it is the root
  525. * of a filesystem.
  526. *
  527. * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
  528. * any other hashed alias over that one unless @want_discon is set,
  529. * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
  530. */
  531. static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
  532. {
  533. struct dentry *alias, *discon_alias;
  534. again:
  535. discon_alias = NULL;
  536. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  537. spin_lock(&alias->d_lock);
  538. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  539. if (IS_ROOT(alias) &&
  540. (alias->d_flags & DCACHE_DISCONNECTED)) {
  541. discon_alias = alias;
  542. } else if (!want_discon) {
  543. __dget_dlock(alias);
  544. spin_unlock(&alias->d_lock);
  545. return alias;
  546. }
  547. }
  548. spin_unlock(&alias->d_lock);
  549. }
  550. if (discon_alias) {
  551. alias = discon_alias;
  552. spin_lock(&alias->d_lock);
  553. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  554. if (IS_ROOT(alias) &&
  555. (alias->d_flags & DCACHE_DISCONNECTED)) {
  556. __dget_dlock(alias);
  557. spin_unlock(&alias->d_lock);
  558. return alias;
  559. }
  560. }
  561. spin_unlock(&alias->d_lock);
  562. goto again;
  563. }
  564. return NULL;
  565. }
  566. struct dentry *d_find_alias(struct inode *inode)
  567. {
  568. struct dentry *de = NULL;
  569. if (!list_empty(&inode->i_dentry)) {
  570. spin_lock(&inode->i_lock);
  571. de = __d_find_alias(inode, 0);
  572. spin_unlock(&inode->i_lock);
  573. }
  574. return de;
  575. }
  576. EXPORT_SYMBOL(d_find_alias);
  577. /*
  578. * Try to kill dentries associated with this inode.
  579. * WARNING: you must own a reference to inode.
  580. */
  581. void d_prune_aliases(struct inode *inode)
  582. {
  583. struct dentry *dentry;
  584. restart:
  585. spin_lock(&inode->i_lock);
  586. list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  587. spin_lock(&dentry->d_lock);
  588. if (!dentry->d_count) {
  589. __dget_dlock(dentry);
  590. __d_drop(dentry);
  591. spin_unlock(&dentry->d_lock);
  592. spin_unlock(&inode->i_lock);
  593. dput(dentry);
  594. goto restart;
  595. }
  596. spin_unlock(&dentry->d_lock);
  597. }
  598. spin_unlock(&inode->i_lock);
  599. }
  600. EXPORT_SYMBOL(d_prune_aliases);
  601. /*
  602. * Try to throw away a dentry - free the inode, dput the parent.
  603. * Requires dentry->d_lock is held, and dentry->d_count == 0.
  604. * Releases dentry->d_lock.
  605. *
  606. * This may fail if locks cannot be acquired no problem, just try again.
  607. */
  608. static void try_prune_one_dentry(struct dentry *dentry)
  609. __releases(dentry->d_lock)
  610. {
  611. struct dentry *parent;
  612. parent = dentry_kill(dentry, 0);
  613. /*
  614. * If dentry_kill returns NULL, we have nothing more to do.
  615. * if it returns the same dentry, trylocks failed. In either
  616. * case, just loop again.
  617. *
  618. * Otherwise, we need to prune ancestors too. This is necessary
  619. * to prevent quadratic behavior of shrink_dcache_parent(), but
  620. * is also expected to be beneficial in reducing dentry cache
  621. * fragmentation.
  622. */
  623. if (!parent)
  624. return;
  625. if (parent == dentry)
  626. return;
  627. /* Prune ancestors. */
  628. dentry = parent;
  629. while (dentry) {
  630. spin_lock(&dentry->d_lock);
  631. if (dentry->d_count > 1) {
  632. dentry->d_count--;
  633. spin_unlock(&dentry->d_lock);
  634. return;
  635. }
  636. dentry = dentry_kill(dentry, 1);
  637. }
  638. }
  639. static void shrink_dentry_list(struct list_head *list)
  640. {
  641. struct dentry *dentry;
  642. rcu_read_lock();
  643. for (;;) {
  644. dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
  645. if (&dentry->d_lru == list)
  646. break; /* empty */
  647. spin_lock(&dentry->d_lock);
  648. if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
  649. spin_unlock(&dentry->d_lock);
  650. continue;
  651. }
  652. /*
  653. * We found an inuse dentry which was not removed from
  654. * the LRU because of laziness during lookup. Do not free
  655. * it - just keep it off the LRU list.
  656. */
  657. if (dentry->d_count) {
  658. dentry_lru_del(dentry);
  659. spin_unlock(&dentry->d_lock);
  660. continue;
  661. }
  662. rcu_read_unlock();
  663. try_prune_one_dentry(dentry);
  664. rcu_read_lock();
  665. }
  666. rcu_read_unlock();
  667. }
  668. /**
  669. * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
  670. * @sb: superblock to shrink dentry LRU.
  671. * @count: number of entries to prune
  672. * @flags: flags to control the dentry processing
  673. *
  674. * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
  675. */
  676. static void __shrink_dcache_sb(struct super_block *sb, int count, int flags)
  677. {
  678. struct dentry *dentry;
  679. LIST_HEAD(referenced);
  680. LIST_HEAD(tmp);
  681. relock:
  682. spin_lock(&dcache_lru_lock);
  683. while (!list_empty(&sb->s_dentry_lru)) {
  684. dentry = list_entry(sb->s_dentry_lru.prev,
  685. struct dentry, d_lru);
  686. BUG_ON(dentry->d_sb != sb);
  687. if (!spin_trylock(&dentry->d_lock)) {
  688. spin_unlock(&dcache_lru_lock);
  689. cpu_relax();
  690. goto relock;
  691. }
  692. /*
  693. * If we are honouring the DCACHE_REFERENCED flag and the
  694. * dentry has this flag set, don't free it. Clear the flag
  695. * and put it back on the LRU.
  696. */
  697. if (flags & DCACHE_REFERENCED &&
  698. dentry->d_flags & DCACHE_REFERENCED) {
  699. dentry->d_flags &= ~DCACHE_REFERENCED;
  700. list_move(&dentry->d_lru, &referenced);
  701. spin_unlock(&dentry->d_lock);
  702. } else {
  703. list_move_tail(&dentry->d_lru, &tmp);
  704. spin_unlock(&dentry->d_lock);
  705. if (!--count)
  706. break;
  707. }
  708. cond_resched_lock(&dcache_lru_lock);
  709. }
  710. if (!list_empty(&referenced))
  711. list_splice(&referenced, &sb->s_dentry_lru);
  712. spin_unlock(&dcache_lru_lock);
  713. shrink_dentry_list(&tmp);
  714. }
  715. /**
  716. * prune_dcache_sb - shrink the dcache
  717. * @nr_to_scan: number of entries to try to free
  718. *
  719. * Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is
  720. * done when we need more memory an called from the superblock shrinker
  721. * function.
  722. *
  723. * This function may fail to free any resources if all the dentries are in
  724. * use.
  725. */
  726. void prune_dcache_sb(struct super_block *sb, int nr_to_scan)
  727. {
  728. __shrink_dcache_sb(sb, nr_to_scan, DCACHE_REFERENCED);
  729. }
  730. /**
  731. * shrink_dcache_sb - shrink dcache for a superblock
  732. * @sb: superblock
  733. *
  734. * Shrink the dcache for the specified super block. This is used to free
  735. * the dcache before unmounting a file system.
  736. */
  737. void shrink_dcache_sb(struct super_block *sb)
  738. {
  739. LIST_HEAD(tmp);
  740. spin_lock(&dcache_lru_lock);
  741. while (!list_empty(&sb->s_dentry_lru)) {
  742. list_splice_init(&sb->s_dentry_lru, &tmp);
  743. spin_unlock(&dcache_lru_lock);
  744. shrink_dentry_list(&tmp);
  745. spin_lock(&dcache_lru_lock);
  746. }
  747. spin_unlock(&dcache_lru_lock);
  748. }
  749. EXPORT_SYMBOL(shrink_dcache_sb);
  750. /*
  751. * destroy a single subtree of dentries for unmount
  752. * - see the comments on shrink_dcache_for_umount() for a description of the
  753. * locking
  754. */
  755. static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
  756. {
  757. struct dentry *parent;
  758. unsigned detached = 0;
  759. BUG_ON(!IS_ROOT(dentry));
  760. /* detach this root from the system */
  761. spin_lock(&dentry->d_lock);
  762. dentry_lru_del(dentry);
  763. __d_drop(dentry);
  764. spin_unlock(&dentry->d_lock);
  765. for (;;) {
  766. /* descend to the first leaf in the current subtree */
  767. while (!list_empty(&dentry->d_subdirs)) {
  768. struct dentry *loop;
  769. /* this is a branch with children - detach all of them
  770. * from the system in one go */
  771. spin_lock(&dentry->d_lock);
  772. list_for_each_entry(loop, &dentry->d_subdirs,
  773. d_u.d_child) {
  774. spin_lock_nested(&loop->d_lock,
  775. DENTRY_D_LOCK_NESTED);
  776. dentry_lru_del(loop);
  777. __d_drop(loop);
  778. spin_unlock(&loop->d_lock);
  779. }
  780. spin_unlock(&dentry->d_lock);
  781. /* move to the first child */
  782. dentry = list_entry(dentry->d_subdirs.next,
  783. struct dentry, d_u.d_child);
  784. }
  785. /* consume the dentries from this leaf up through its parents
  786. * until we find one with children or run out altogether */
  787. do {
  788. struct inode *inode;
  789. if (dentry->d_count != 0) {
  790. printk(KERN_ERR
  791. "BUG: Dentry %p{i=%lx,n=%s}"
  792. " still in use (%d)"
  793. " [unmount of %s %s]\n",
  794. dentry,
  795. dentry->d_inode ?
  796. dentry->d_inode->i_ino : 0UL,
  797. dentry->d_name.name,
  798. dentry->d_count,
  799. dentry->d_sb->s_type->name,
  800. dentry->d_sb->s_id);
  801. BUG();
  802. }
  803. if (IS_ROOT(dentry)) {
  804. parent = NULL;
  805. list_del(&dentry->d_u.d_child);
  806. } else {
  807. parent = dentry->d_parent;
  808. spin_lock(&parent->d_lock);
  809. parent->d_count--;
  810. list_del(&dentry->d_u.d_child);
  811. spin_unlock(&parent->d_lock);
  812. }
  813. detached++;
  814. inode = dentry->d_inode;
  815. if (inode) {
  816. dentry->d_inode = NULL;
  817. list_del_init(&dentry->d_alias);
  818. if (dentry->d_op && dentry->d_op->d_iput)
  819. dentry->d_op->d_iput(dentry, inode);
  820. else
  821. iput(inode);
  822. }
  823. d_free(dentry);
  824. /* finished when we fall off the top of the tree,
  825. * otherwise we ascend to the parent and move to the
  826. * next sibling if there is one */
  827. if (!parent)
  828. return;
  829. dentry = parent;
  830. } while (list_empty(&dentry->d_subdirs));
  831. dentry = list_entry(dentry->d_subdirs.next,
  832. struct dentry, d_u.d_child);
  833. }
  834. }
  835. /*
  836. * destroy the dentries attached to a superblock on unmounting
  837. * - we don't need to use dentry->d_lock because:
  838. * - the superblock is detached from all mountings and open files, so the
  839. * dentry trees will not be rearranged by the VFS
  840. * - s_umount is write-locked, so the memory pressure shrinker will ignore
  841. * any dentries belonging to this superblock that it comes across
  842. * - the filesystem itself is no longer permitted to rearrange the dentries
  843. * in this superblock
  844. */
  845. void shrink_dcache_for_umount(struct super_block *sb)
  846. {
  847. struct dentry *dentry;
  848. if (down_read_trylock(&sb->s_umount))
  849. BUG();
  850. dentry = sb->s_root;
  851. sb->s_root = NULL;
  852. spin_lock(&dentry->d_lock);
  853. dentry->d_count--;
  854. spin_unlock(&dentry->d_lock);
  855. shrink_dcache_for_umount_subtree(dentry);
  856. while (!hlist_bl_empty(&sb->s_anon)) {
  857. dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
  858. shrink_dcache_for_umount_subtree(dentry);
  859. }
  860. }
  861. /*
  862. * This tries to ascend one level of parenthood, but
  863. * we can race with renaming, so we need to re-check
  864. * the parenthood after dropping the lock and check
  865. * that the sequence number still matches.
  866. */
  867. static struct dentry *try_to_ascend(struct dentry *old, int locked, unsigned seq)
  868. {
  869. struct dentry *new = old->d_parent;
  870. rcu_read_lock();
  871. spin_unlock(&old->d_lock);
  872. spin_lock(&new->d_lock);
  873. /*
  874. * might go back up the wrong parent if we have had a rename
  875. * or deletion
  876. */
  877. if (new != old->d_parent ||
  878. (old->d_flags & DCACHE_DISCONNECTED) ||
  879. (!locked && read_seqretry(&rename_lock, seq))) {
  880. spin_unlock(&new->d_lock);
  881. new = NULL;
  882. }
  883. rcu_read_unlock();
  884. return new;
  885. }
  886. /*
  887. * Search for at least 1 mount point in the dentry's subdirs.
  888. * We descend to the next level whenever the d_subdirs
  889. * list is non-empty and continue searching.
  890. */
  891. /**
  892. * have_submounts - check for mounts over a dentry
  893. * @parent: dentry to check.
  894. *
  895. * Return true if the parent or its subdirectories contain
  896. * a mount point
  897. */
  898. int have_submounts(struct dentry *parent)
  899. {
  900. struct dentry *this_parent;
  901. struct list_head *next;
  902. unsigned seq;
  903. int locked = 0;
  904. seq = read_seqbegin(&rename_lock);
  905. again:
  906. this_parent = parent;
  907. if (d_mountpoint(parent))
  908. goto positive;
  909. spin_lock(&this_parent->d_lock);
  910. repeat:
  911. next = this_parent->d_subdirs.next;
  912. resume:
  913. while (next != &this_parent->d_subdirs) {
  914. struct list_head *tmp = next;
  915. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  916. next = tmp->next;
  917. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  918. /* Have we found a mount point ? */
  919. if (d_mountpoint(dentry)) {
  920. spin_unlock(&dentry->d_lock);
  921. spin_unlock(&this_parent->d_lock);
  922. goto positive;
  923. }
  924. if (!list_empty(&dentry->d_subdirs)) {
  925. spin_unlock(&this_parent->d_lock);
  926. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  927. this_parent = dentry;
  928. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  929. goto repeat;
  930. }
  931. spin_unlock(&dentry->d_lock);
  932. }
  933. /*
  934. * All done at this level ... ascend and resume the search.
  935. */
  936. if (this_parent != parent) {
  937. struct dentry *child = this_parent;
  938. this_parent = try_to_ascend(this_parent, locked, seq);
  939. if (!this_parent)
  940. goto rename_retry;
  941. next = child->d_u.d_child.next;
  942. goto resume;
  943. }
  944. spin_unlock(&this_parent->d_lock);
  945. if (!locked && read_seqretry(&rename_lock, seq))
  946. goto rename_retry;
  947. if (locked)
  948. write_sequnlock(&rename_lock);
  949. return 0; /* No mount points found in tree */
  950. positive:
  951. if (!locked && read_seqretry(&rename_lock, seq))
  952. goto rename_retry;
  953. if (locked)
  954. write_sequnlock(&rename_lock);
  955. return 1;
  956. rename_retry:
  957. locked = 1;
  958. write_seqlock(&rename_lock);
  959. goto again;
  960. }
  961. EXPORT_SYMBOL(have_submounts);
  962. /*
  963. * Search the dentry child list for the specified parent,
  964. * and move any unused dentries to the end of the unused
  965. * list for prune_dcache(). We descend to the next level
  966. * whenever the d_subdirs list is non-empty and continue
  967. * searching.
  968. *
  969. * It returns zero iff there are no unused children,
  970. * otherwise it returns the number of children moved to
  971. * the end of the unused list. This may not be the total
  972. * number of unused children, because select_parent can
  973. * drop the lock and return early due to latency
  974. * constraints.
  975. */
  976. static int select_parent(struct dentry * parent)
  977. {
  978. struct dentry *this_parent;
  979. struct list_head *next;
  980. unsigned seq;
  981. int found = 0;
  982. int locked = 0;
  983. seq = read_seqbegin(&rename_lock);
  984. again:
  985. this_parent = parent;
  986. spin_lock(&this_parent->d_lock);
  987. repeat:
  988. next = this_parent->d_subdirs.next;
  989. resume:
  990. while (next != &this_parent->d_subdirs) {
  991. struct list_head *tmp = next;
  992. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  993. next = tmp->next;
  994. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  995. /*
  996. * move only zero ref count dentries to the end
  997. * of the unused list for prune_dcache
  998. */
  999. if (!dentry->d_count) {
  1000. dentry_lru_move_tail(dentry);
  1001. found++;
  1002. } else {
  1003. dentry_lru_del(dentry);
  1004. }
  1005. /*
  1006. * We can return to the caller if we have found some (this
  1007. * ensures forward progress). We'll be coming back to find
  1008. * the rest.
  1009. */
  1010. if (found && need_resched()) {
  1011. spin_unlock(&dentry->d_lock);
  1012. goto out;
  1013. }
  1014. /*
  1015. * Descend a level if the d_subdirs list is non-empty.
  1016. */
  1017. if (!list_empty(&dentry->d_subdirs)) {
  1018. spin_unlock(&this_parent->d_lock);
  1019. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  1020. this_parent = dentry;
  1021. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  1022. goto repeat;
  1023. }
  1024. spin_unlock(&dentry->d_lock);
  1025. }
  1026. /*
  1027. * All done at this level ... ascend and resume the search.
  1028. */
  1029. if (this_parent != parent) {
  1030. struct dentry *child = this_parent;
  1031. this_parent = try_to_ascend(this_parent, locked, seq);
  1032. if (!this_parent)
  1033. goto rename_retry;
  1034. next = child->d_u.d_child.next;
  1035. goto resume;
  1036. }
  1037. out:
  1038. spin_unlock(&this_parent->d_lock);
  1039. if (!locked && read_seqretry(&rename_lock, seq))
  1040. goto rename_retry;
  1041. if (locked)
  1042. write_sequnlock(&rename_lock);
  1043. return found;
  1044. rename_retry:
  1045. if (found)
  1046. return found;
  1047. locked = 1;
  1048. write_seqlock(&rename_lock);
  1049. goto again;
  1050. }
  1051. /**
  1052. * shrink_dcache_parent - prune dcache
  1053. * @parent: parent of entries to prune
  1054. *
  1055. * Prune the dcache to remove unused children of the parent dentry.
  1056. */
  1057. void shrink_dcache_parent(struct dentry * parent)
  1058. {
  1059. struct super_block *sb = parent->d_sb;
  1060. int found;
  1061. while ((found = select_parent(parent)) != 0)
  1062. __shrink_dcache_sb(sb, found, 0);
  1063. }
  1064. EXPORT_SYMBOL(shrink_dcache_parent);
  1065. /**
  1066. * __d_alloc - allocate a dcache entry
  1067. * @sb: filesystem it will belong to
  1068. * @name: qstr of the name
  1069. *
  1070. * Allocates a dentry. It returns %NULL if there is insufficient memory
  1071. * available. On a success the dentry is returned. The name passed in is
  1072. * copied and the copy passed in may be reused after this call.
  1073. */
  1074. struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
  1075. {
  1076. struct dentry *dentry;
  1077. char *dname;
  1078. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  1079. if (!dentry)
  1080. return NULL;
  1081. if (name->len > DNAME_INLINE_LEN-1) {
  1082. dname = kmalloc(name->len + 1, GFP_KERNEL);
  1083. if (!dname) {
  1084. kmem_cache_free(dentry_cache, dentry);
  1085. return NULL;
  1086. }
  1087. } else {
  1088. dname = dentry->d_iname;
  1089. }
  1090. dentry->d_name.name = dname;
  1091. dentry->d_name.len = name->len;
  1092. dentry->d_name.hash = name->hash;
  1093. memcpy(dname, name->name, name->len);
  1094. dname[name->len] = 0;
  1095. dentry->d_count = 1;
  1096. dentry->d_flags = 0;
  1097. spin_lock_init(&dentry->d_lock);
  1098. seqcount_init(&dentry->d_seq);
  1099. dentry->d_inode = NULL;
  1100. dentry->d_parent = dentry;
  1101. dentry->d_sb = sb;
  1102. dentry->d_op = NULL;
  1103. dentry->d_fsdata = NULL;
  1104. INIT_HLIST_BL_NODE(&dentry->d_hash);
  1105. INIT_LIST_HEAD(&dentry->d_lru);
  1106. INIT_LIST_HEAD(&dentry->d_subdirs);
  1107. INIT_LIST_HEAD(&dentry->d_alias);
  1108. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1109. d_set_d_op(dentry, dentry->d_sb->s_d_op);
  1110. this_cpu_inc(nr_dentry);
  1111. return dentry;
  1112. }
  1113. /**
  1114. * d_alloc - allocate a dcache entry
  1115. * @parent: parent of entry to allocate
  1116. * @name: qstr of the name
  1117. *
  1118. * Allocates a dentry. It returns %NULL if there is insufficient memory
  1119. * available. On a success the dentry is returned. The name passed in is
  1120. * copied and the copy passed in may be reused after this call.
  1121. */
  1122. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  1123. {
  1124. struct dentry *dentry = __d_alloc(parent->d_sb, name);
  1125. if (!dentry)
  1126. return NULL;
  1127. spin_lock(&parent->d_lock);
  1128. /*
  1129. * don't need child lock because it is not subject
  1130. * to concurrency here
  1131. */
  1132. __dget_dlock(parent);
  1133. dentry->d_parent = parent;
  1134. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  1135. spin_unlock(&parent->d_lock);
  1136. return dentry;
  1137. }
  1138. EXPORT_SYMBOL(d_alloc);
  1139. struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
  1140. {
  1141. struct dentry *dentry = __d_alloc(sb, name);
  1142. if (dentry)
  1143. dentry->d_flags |= DCACHE_DISCONNECTED;
  1144. return dentry;
  1145. }
  1146. EXPORT_SYMBOL(d_alloc_pseudo);
  1147. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  1148. {
  1149. struct qstr q;
  1150. q.name = name;
  1151. q.len = strlen(name);
  1152. q.hash = full_name_hash(q.name, q.len);
  1153. return d_alloc(parent, &q);
  1154. }
  1155. EXPORT_SYMBOL(d_alloc_name);
  1156. void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
  1157. {
  1158. WARN_ON_ONCE(dentry->d_op);
  1159. WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
  1160. DCACHE_OP_COMPARE |
  1161. DCACHE_OP_REVALIDATE |
  1162. DCACHE_OP_DELETE ));
  1163. dentry->d_op = op;
  1164. if (!op)
  1165. return;
  1166. if (op->d_hash)
  1167. dentry->d_flags |= DCACHE_OP_HASH;
  1168. if (op->d_compare)
  1169. dentry->d_flags |= DCACHE_OP_COMPARE;
  1170. if (op->d_revalidate)
  1171. dentry->d_flags |= DCACHE_OP_REVALIDATE;
  1172. if (op->d_delete)
  1173. dentry->d_flags |= DCACHE_OP_DELETE;
  1174. }
  1175. EXPORT_SYMBOL(d_set_d_op);
  1176. static void __d_instantiate(struct dentry *dentry, struct inode *inode)
  1177. {
  1178. spin_lock(&dentry->d_lock);
  1179. if (inode) {
  1180. if (unlikely(IS_AUTOMOUNT(inode)))
  1181. dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
  1182. list_add(&dentry->d_alias, &inode->i_dentry);
  1183. }
  1184. dentry->d_inode = inode;
  1185. dentry_rcuwalk_barrier(dentry);
  1186. spin_unlock(&dentry->d_lock);
  1187. fsnotify_d_instantiate(dentry, inode);
  1188. }
  1189. /**
  1190. * d_instantiate - fill in inode information for a dentry
  1191. * @entry: dentry to complete
  1192. * @inode: inode to attach to this dentry
  1193. *
  1194. * Fill in inode information in the entry.
  1195. *
  1196. * This turns negative dentries into productive full members
  1197. * of society.
  1198. *
  1199. * NOTE! This assumes that the inode count has been incremented
  1200. * (or otherwise set) by the caller to indicate that it is now
  1201. * in use by the dcache.
  1202. */
  1203. void d_instantiate(struct dentry *entry, struct inode * inode)
  1204. {
  1205. BUG_ON(!list_empty(&entry->d_alias));
  1206. if (inode)
  1207. spin_lock(&inode->i_lock);
  1208. __d_instantiate(entry, inode);
  1209. if (inode)
  1210. spin_unlock(&inode->i_lock);
  1211. security_d_instantiate(entry, inode);
  1212. }
  1213. EXPORT_SYMBOL(d_instantiate);
  1214. /**
  1215. * d_instantiate_unique - instantiate a non-aliased dentry
  1216. * @entry: dentry to instantiate
  1217. * @inode: inode to attach to this dentry
  1218. *
  1219. * Fill in inode information in the entry. On success, it returns NULL.
  1220. * If an unhashed alias of "entry" already exists, then we return the
  1221. * aliased dentry instead and drop one reference to inode.
  1222. *
  1223. * Note that in order to avoid conflicts with rename() etc, the caller
  1224. * had better be holding the parent directory semaphore.
  1225. *
  1226. * This also assumes that the inode count has been incremented
  1227. * (or otherwise set) by the caller to indicate that it is now
  1228. * in use by the dcache.
  1229. */
  1230. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  1231. struct inode *inode)
  1232. {
  1233. struct dentry *alias;
  1234. int len = entry->d_name.len;
  1235. const char *name = entry->d_name.name;
  1236. unsigned int hash = entry->d_name.hash;
  1237. if (!inode) {
  1238. __d_instantiate(entry, NULL);
  1239. return NULL;
  1240. }
  1241. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  1242. struct qstr *qstr = &alias->d_name;
  1243. /*
  1244. * Don't need alias->d_lock here, because aliases with
  1245. * d_parent == entry->d_parent are not subject to name or
  1246. * parent changes, because the parent inode i_mutex is held.
  1247. */
  1248. if (qstr->hash != hash)
  1249. continue;
  1250. if (alias->d_parent != entry->d_parent)
  1251. continue;
  1252. if (dentry_cmp(qstr->name, qstr->len, name, len))
  1253. continue;
  1254. __dget(alias);
  1255. return alias;
  1256. }
  1257. __d_instantiate(entry, inode);
  1258. return NULL;
  1259. }
  1260. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  1261. {
  1262. struct dentry *result;
  1263. BUG_ON(!list_empty(&entry->d_alias));
  1264. if (inode)
  1265. spin_lock(&inode->i_lock);
  1266. result = __d_instantiate_unique(entry, inode);
  1267. if (inode)
  1268. spin_unlock(&inode->i_lock);
  1269. if (!result) {
  1270. security_d_instantiate(entry, inode);
  1271. return NULL;
  1272. }
  1273. BUG_ON(!d_unhashed(result));
  1274. iput(inode);
  1275. return result;
  1276. }
  1277. EXPORT_SYMBOL(d_instantiate_unique);
  1278. /**
  1279. * d_alloc_root - allocate root dentry
  1280. * @root_inode: inode to allocate the root for
  1281. *
  1282. * Allocate a root ("/") dentry for the inode given. The inode is
  1283. * instantiated and returned. %NULL is returned if there is insufficient
  1284. * memory or the inode passed is %NULL.
  1285. */
  1286. struct dentry * d_alloc_root(struct inode * root_inode)
  1287. {
  1288. struct dentry *res = NULL;
  1289. if (root_inode) {
  1290. static const struct qstr name = { .name = "/", .len = 1 };
  1291. res = __d_alloc(root_inode->i_sb, &name);
  1292. if (res)
  1293. d_instantiate(res, root_inode);
  1294. }
  1295. return res;
  1296. }
  1297. EXPORT_SYMBOL(d_alloc_root);
  1298. static struct dentry * __d_find_any_alias(struct inode *inode)
  1299. {
  1300. struct dentry *alias;
  1301. if (list_empty(&inode->i_dentry))
  1302. return NULL;
  1303. alias = list_first_entry(&inode->i_dentry, struct dentry, d_alias);
  1304. __dget(alias);
  1305. return alias;
  1306. }
  1307. static struct dentry * d_find_any_alias(struct inode *inode)
  1308. {
  1309. struct dentry *de;
  1310. spin_lock(&inode->i_lock);
  1311. de = __d_find_any_alias(inode);
  1312. spin_unlock(&inode->i_lock);
  1313. return de;
  1314. }
  1315. /**
  1316. * d_obtain_alias - find or allocate a dentry for a given inode
  1317. * @inode: inode to allocate the dentry for
  1318. *
  1319. * Obtain a dentry for an inode resulting from NFS filehandle conversion or
  1320. * similar open by handle operations. The returned dentry may be anonymous,
  1321. * or may have a full name (if the inode was already in the cache).
  1322. *
  1323. * When called on a directory inode, we must ensure that the inode only ever
  1324. * has one dentry. If a dentry is found, that is returned instead of
  1325. * allocating a new one.
  1326. *
  1327. * On successful return, the reference to the inode has been transferred
  1328. * to the dentry. In case of an error the reference on the inode is released.
  1329. * To make it easier to use in export operations a %NULL or IS_ERR inode may
  1330. * be passed in and will be the error will be propagate to the return value,
  1331. * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
  1332. */
  1333. struct dentry *d_obtain_alias(struct inode *inode)
  1334. {
  1335. static const struct qstr anonstring = { .name = "" };
  1336. struct dentry *tmp;
  1337. struct dentry *res;
  1338. if (!inode)
  1339. return ERR_PTR(-ESTALE);
  1340. if (IS_ERR(inode))
  1341. return ERR_CAST(inode);
  1342. res = d_find_any_alias(inode);
  1343. if (res)
  1344. goto out_iput;
  1345. tmp = __d_alloc(inode->i_sb, &anonstring);
  1346. if (!tmp) {
  1347. res = ERR_PTR(-ENOMEM);
  1348. goto out_iput;
  1349. }
  1350. spin_lock(&inode->i_lock);
  1351. res = __d_find_any_alias(inode);
  1352. if (res) {
  1353. spin_unlock(&inode->i_lock);
  1354. dput(tmp);
  1355. goto out_iput;
  1356. }
  1357. /* attach a disconnected dentry */
  1358. spin_lock(&tmp->d_lock);
  1359. tmp->d_inode = inode;
  1360. tmp->d_flags |= DCACHE_DISCONNECTED;
  1361. list_add(&tmp->d_alias, &inode->i_dentry);
  1362. hlist_bl_lock(&tmp->d_sb->s_anon);
  1363. hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
  1364. hlist_bl_unlock(&tmp->d_sb->s_anon);
  1365. spin_unlock(&tmp->d_lock);
  1366. spin_unlock(&inode->i_lock);
  1367. security_d_instantiate(tmp, inode);
  1368. return tmp;
  1369. out_iput:
  1370. if (res && !IS_ERR(res))
  1371. security_d_instantiate(res, inode);
  1372. iput(inode);
  1373. return res;
  1374. }
  1375. EXPORT_SYMBOL(d_obtain_alias);
  1376. /**
  1377. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  1378. * @inode: the inode which may have a disconnected dentry
  1379. * @dentry: a negative dentry which we want to point to the inode.
  1380. *
  1381. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1382. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1383. * and return it, else simply d_add the inode to the dentry and return NULL.
  1384. *
  1385. * This is needed in the lookup routine of any filesystem that is exportable
  1386. * (via knfsd) so that we can build dcache paths to directories effectively.
  1387. *
  1388. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1389. * is returned. This matches the expected return value of ->lookup.
  1390. *
  1391. */
  1392. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1393. {
  1394. struct dentry *new = NULL;
  1395. if (IS_ERR(inode))
  1396. return ERR_CAST(inode);
  1397. if (inode && S_ISDIR(inode->i_mode)) {
  1398. spin_lock(&inode->i_lock);
  1399. new = __d_find_alias(inode, 1);
  1400. if (new) {
  1401. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  1402. spin_unlock(&inode->i_lock);
  1403. security_d_instantiate(new, inode);
  1404. d_move(new, dentry);
  1405. iput(inode);
  1406. } else {
  1407. /* already taking inode->i_lock, so d_add() by hand */
  1408. __d_instantiate(dentry, inode);
  1409. spin_unlock(&inode->i_lock);
  1410. security_d_instantiate(dentry, inode);
  1411. d_rehash(dentry);
  1412. }
  1413. } else
  1414. d_add(dentry, inode);
  1415. return new;
  1416. }
  1417. EXPORT_SYMBOL(d_splice_alias);
  1418. /**
  1419. * d_add_ci - lookup or allocate new dentry with case-exact name
  1420. * @inode: the inode case-insensitive lookup has found
  1421. * @dentry: the negative dentry that was passed to the parent's lookup func
  1422. * @name: the case-exact name to be associated with the returned dentry
  1423. *
  1424. * This is to avoid filling the dcache with case-insensitive names to the
  1425. * same inode, only the actual correct case is stored in the dcache for
  1426. * case-insensitive filesystems.
  1427. *
  1428. * For a case-insensitive lookup match and if the the case-exact dentry
  1429. * already exists in in the dcache, use it and return it.
  1430. *
  1431. * If no entry exists with the exact case name, allocate new dentry with
  1432. * the exact case, and return the spliced entry.
  1433. */
  1434. struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
  1435. struct qstr *name)
  1436. {
  1437. int error;
  1438. struct dentry *found;
  1439. struct dentry *new;
  1440. /*
  1441. * First check if a dentry matching the name already exists,
  1442. * if not go ahead and create it now.
  1443. */
  1444. found = d_hash_and_lookup(dentry->d_parent, name);
  1445. if (!found) {
  1446. new = d_alloc(dentry->d_parent, name);
  1447. if (!new) {
  1448. error = -ENOMEM;
  1449. goto err_out;
  1450. }
  1451. found = d_splice_alias(inode, new);
  1452. if (found) {
  1453. dput(new);
  1454. return found;
  1455. }
  1456. return new;
  1457. }
  1458. /*
  1459. * If a matching dentry exists, and it's not negative use it.
  1460. *
  1461. * Decrement the reference count to balance the iget() done
  1462. * earlier on.
  1463. */
  1464. if (found->d_inode) {
  1465. if (unlikely(found->d_inode != inode)) {
  1466. /* This can't happen because bad inodes are unhashed. */
  1467. BUG_ON(!is_bad_inode(inode));
  1468. BUG_ON(!is_bad_inode(found->d_inode));
  1469. }
  1470. iput(inode);
  1471. return found;
  1472. }
  1473. /*
  1474. * We are going to instantiate this dentry, unhash it and clear the
  1475. * lookup flag so we can do that.
  1476. */
  1477. if (unlikely(d_need_lookup(found)))
  1478. d_clear_need_lookup(found);
  1479. /*
  1480. * Negative dentry: instantiate it unless the inode is a directory and
  1481. * already has a dentry.
  1482. */
  1483. new = d_splice_alias(inode, found);
  1484. if (new) {
  1485. dput(found);
  1486. found = new;
  1487. }
  1488. return found;
  1489. err_out:
  1490. iput(inode);
  1491. return ERR_PTR(error);
  1492. }
  1493. EXPORT_SYMBOL(d_add_ci);
  1494. /**
  1495. * __d_lookup_rcu - search for a dentry (racy, store-free)
  1496. * @parent: parent dentry
  1497. * @name: qstr of name we wish to find
  1498. * @seq: returns d_seq value at the point where the dentry was found
  1499. * @inode: returns dentry->d_inode when the inode was found valid.
  1500. * Returns: dentry, or NULL
  1501. *
  1502. * __d_lookup_rcu is the dcache lookup function for rcu-walk name
  1503. * resolution (store-free path walking) design described in
  1504. * Documentation/filesystems/path-lookup.txt.
  1505. *
  1506. * This is not to be used outside core vfs.
  1507. *
  1508. * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
  1509. * held, and rcu_read_lock held. The returned dentry must not be stored into
  1510. * without taking d_lock and checking d_seq sequence count against @seq
  1511. * returned here.
  1512. *
  1513. * A refcount may be taken on the found dentry with the __d_rcu_to_refcount
  1514. * function.
  1515. *
  1516. * Alternatively, __d_lookup_rcu may be called again to look up the child of
  1517. * the returned dentry, so long as its parent's seqlock is checked after the
  1518. * child is looked up. Thus, an interlocking stepping of sequence lock checks
  1519. * is formed, giving integrity down the path walk.
  1520. */
  1521. struct dentry *__d_lookup_rcu(struct dentry *parent, struct qstr *name,
  1522. unsigned *seq, struct inode **inode)
  1523. {
  1524. unsigned int len = name->len;
  1525. unsigned int hash = name->hash;
  1526. const unsigned char *str = name->name;
  1527. struct hlist_bl_head *b = d_hash(parent, hash);
  1528. struct hlist_bl_node *node;
  1529. struct dentry *dentry;
  1530. /*
  1531. * Note: There is significant duplication with __d_lookup_rcu which is
  1532. * required to prevent single threaded performance regressions
  1533. * especially on architectures where smp_rmb (in seqcounts) are costly.
  1534. * Keep the two functions in sync.
  1535. */
  1536. /*
  1537. * The hash list is protected using RCU.
  1538. *
  1539. * Carefully use d_seq when comparing a candidate dentry, to avoid
  1540. * races with d_move().
  1541. *
  1542. * It is possible that concurrent renames can mess up our list
  1543. * walk here and result in missing our dentry, resulting in the
  1544. * false-negative result. d_lookup() protects against concurrent
  1545. * renames using rename_lock seqlock.
  1546. *
  1547. * See Documentation/filesystems/path-lookup.txt for more details.
  1548. */
  1549. hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
  1550. struct inode *i;
  1551. const char *tname;
  1552. int tlen;
  1553. if (dentry->d_name.hash != hash)
  1554. continue;
  1555. seqretry:
  1556. *seq = read_seqcount_begin(&dentry->d_seq);
  1557. if (dentry->d_parent != parent)
  1558. continue;
  1559. if (d_unhashed(dentry))
  1560. continue;
  1561. tlen = dentry->d_name.len;
  1562. tname = dentry->d_name.name;
  1563. i = dentry->d_inode;
  1564. prefetch(tname);
  1565. /*
  1566. * This seqcount check is required to ensure name and
  1567. * len are loaded atomically, so as not to walk off the
  1568. * edge of memory when walking. If we could load this
  1569. * atomically some other way, we could drop this check.
  1570. */
  1571. if (read_seqcount_retry(&dentry->d_seq, *seq))
  1572. goto seqretry;
  1573. if (parent->d_flags & DCACHE_OP_COMPARE) {
  1574. if (parent->d_op->d_compare(parent, *inode,
  1575. dentry, i,
  1576. tlen, tname, name))
  1577. continue;
  1578. } else {
  1579. if (dentry_cmp(tname, tlen, str, len))
  1580. continue;
  1581. }
  1582. /*
  1583. * No extra seqcount check is required after the name
  1584. * compare. The caller must perform a seqcount check in
  1585. * order to do anything useful with the returned dentry
  1586. * anyway.
  1587. */
  1588. *inode = i;
  1589. return dentry;
  1590. }
  1591. return NULL;
  1592. }
  1593. /**
  1594. * d_lookup - search for a dentry
  1595. * @parent: parent dentry
  1596. * @name: qstr of name we wish to find
  1597. * Returns: dentry, or NULL
  1598. *
  1599. * d_lookup searches the children of the parent dentry for the name in
  1600. * question. If the dentry is found its reference count is incremented and the
  1601. * dentry is returned. The caller must use dput to free the entry when it has
  1602. * finished using it. %NULL is returned if the dentry does not exist.
  1603. */
  1604. struct dentry *d_lookup(struct dentry *parent, struct qstr *name)
  1605. {
  1606. struct dentry *dentry;
  1607. unsigned seq;
  1608. do {
  1609. seq = read_seqbegin(&rename_lock);
  1610. dentry = __d_lookup(parent, name);
  1611. if (dentry)
  1612. break;
  1613. } while (read_seqretry(&rename_lock, seq));
  1614. return dentry;
  1615. }
  1616. EXPORT_SYMBOL(d_lookup);
  1617. /**
  1618. * __d_lookup - search for a dentry (racy)
  1619. * @parent: parent dentry
  1620. * @name: qstr of name we wish to find
  1621. * Returns: dentry, or NULL
  1622. *
  1623. * __d_lookup is like d_lookup, however it may (rarely) return a
  1624. * false-negative result due to unrelated rename activity.
  1625. *
  1626. * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
  1627. * however it must be used carefully, eg. with a following d_lookup in
  1628. * the case of failure.
  1629. *
  1630. * __d_lookup callers must be commented.
  1631. */
  1632. struct dentry *__d_lookup(struct dentry *parent, struct qstr *name)
  1633. {
  1634. unsigned int len = name->len;
  1635. unsigned int hash = name->hash;
  1636. const unsigned char *str = name->name;
  1637. struct hlist_bl_head *b = d_hash(parent, hash);
  1638. struct hlist_bl_node *node;
  1639. struct dentry *found = NULL;
  1640. struct dentry *dentry;
  1641. /*
  1642. * Note: There is significant duplication with __d_lookup_rcu which is
  1643. * required to prevent single threaded performance regressions
  1644. * especially on architectures where smp_rmb (in seqcounts) are costly.
  1645. * Keep the two functions in sync.
  1646. */
  1647. /*
  1648. * The hash list is protected using RCU.
  1649. *
  1650. * Take d_lock when comparing a candidate dentry, to avoid races
  1651. * with d_move().
  1652. *
  1653. * It is possible that concurrent renames can mess up our list
  1654. * walk here and result in missing our dentry, resulting in the
  1655. * false-negative result. d_lookup() protects against concurrent
  1656. * renames using rename_lock seqlock.
  1657. *
  1658. * See Documentation/filesystems/path-lookup.txt for more details.
  1659. */
  1660. rcu_read_lock();
  1661. hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
  1662. const char *tname;
  1663. int tlen;
  1664. if (dentry->d_name.hash != hash)
  1665. continue;
  1666. spin_lock(&dentry->d_lock);
  1667. if (dentry->d_parent != parent)
  1668. goto next;
  1669. if (d_unhashed(dentry))
  1670. goto next;
  1671. /*
  1672. * It is safe to compare names since d_move() cannot
  1673. * change the qstr (protected by d_lock).
  1674. */
  1675. tlen = dentry->d_name.len;
  1676. tname = dentry->d_name.name;
  1677. if (parent->d_flags & DCACHE_OP_COMPARE) {
  1678. if (parent->d_op->d_compare(parent, parent->d_inode,
  1679. dentry, dentry->d_inode,
  1680. tlen, tname, name))
  1681. goto next;
  1682. } else {
  1683. if (dentry_cmp(tname, tlen, str, len))
  1684. goto next;
  1685. }
  1686. dentry->d_count++;
  1687. found = dentry;
  1688. spin_unlock(&dentry->d_lock);
  1689. break;
  1690. next:
  1691. spin_unlock(&dentry->d_lock);
  1692. }
  1693. rcu_read_unlock();
  1694. return found;
  1695. }
  1696. /**
  1697. * d_hash_and_lookup - hash the qstr then search for a dentry
  1698. * @dir: Directory to search in
  1699. * @name: qstr of name we wish to find
  1700. *
  1701. * On hash failure or on lookup failure NULL is returned.
  1702. */
  1703. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1704. {
  1705. struct dentry *dentry = NULL;
  1706. /*
  1707. * Check for a fs-specific hash function. Note that we must
  1708. * calculate the standard hash first, as the d_op->d_hash()
  1709. * routine may choose to leave the hash value unchanged.
  1710. */
  1711. name->hash = full_name_hash(name->name, name->len);
  1712. if (dir->d_flags & DCACHE_OP_HASH) {
  1713. if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
  1714. goto out;
  1715. }
  1716. dentry = d_lookup(dir, name);
  1717. out:
  1718. return dentry;
  1719. }
  1720. /**
  1721. * d_validate - verify dentry provided from insecure source (deprecated)
  1722. * @dentry: The dentry alleged to be valid child of @dparent
  1723. * @dparent: The parent dentry (known to be valid)
  1724. *
  1725. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1726. * This is used by ncpfs in its readdir implementation.
  1727. * Zero is returned in the dentry is invalid.
  1728. *
  1729. * This function is slow for big directories, and deprecated, do not use it.
  1730. */
  1731. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1732. {
  1733. struct dentry *child;
  1734. spin_lock(&dparent->d_lock);
  1735. list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
  1736. if (dentry == child) {
  1737. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1738. __dget_dlock(dentry);
  1739. spin_unlock(&dentry->d_lock);
  1740. spin_unlock(&dparent->d_lock);
  1741. return 1;
  1742. }
  1743. }
  1744. spin_unlock(&dparent->d_lock);
  1745. return 0;
  1746. }
  1747. EXPORT_SYMBOL(d_validate);
  1748. /*
  1749. * When a file is deleted, we have two options:
  1750. * - turn this dentry into a negative dentry
  1751. * - unhash this dentry and free it.
  1752. *
  1753. * Usually, we want to just turn this into
  1754. * a negative dentry, but if anybody else is
  1755. * currently using the dentry or the inode
  1756. * we can't do that and we fall back on removing
  1757. * it from the hash queues and waiting for
  1758. * it to be deleted later when it has no users
  1759. */
  1760. /**
  1761. * d_delete - delete a dentry
  1762. * @dentry: The dentry to delete
  1763. *
  1764. * Turn the dentry into a negative dentry if possible, otherwise
  1765. * remove it from the hash queues so it can be deleted later
  1766. */
  1767. void d_delete(struct dentry * dentry)
  1768. {
  1769. struct inode *inode;
  1770. int isdir = 0;
  1771. /*
  1772. * Are we the only user?
  1773. */
  1774. again:
  1775. spin_lock(&dentry->d_lock);
  1776. inode = dentry->d_inode;
  1777. isdir = S_ISDIR(inode->i_mode);
  1778. if (dentry->d_count == 1) {
  1779. if (inode && !spin_trylock(&inode->i_lock)) {
  1780. spin_unlock(&dentry->d_lock);
  1781. cpu_relax();
  1782. goto again;
  1783. }
  1784. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  1785. dentry_unlink_inode(dentry);
  1786. fsnotify_nameremove(dentry, isdir);
  1787. return;
  1788. }
  1789. if (!d_unhashed(dentry))
  1790. __d_drop(dentry);
  1791. spin_unlock(&dentry->d_lock);
  1792. fsnotify_nameremove(dentry, isdir);
  1793. }
  1794. EXPORT_SYMBOL(d_delete);
  1795. static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
  1796. {
  1797. BUG_ON(!d_unhashed(entry));
  1798. hlist_bl_lock(b);
  1799. entry->d_flags |= DCACHE_RCUACCESS;
  1800. hlist_bl_add_head_rcu(&entry->d_hash, b);
  1801. hlist_bl_unlock(b);
  1802. }
  1803. static void _d_rehash(struct dentry * entry)
  1804. {
  1805. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1806. }
  1807. /**
  1808. * d_rehash - add an entry back to the hash
  1809. * @entry: dentry to add to the hash
  1810. *
  1811. * Adds a dentry to the hash according to its name.
  1812. */
  1813. void d_rehash(struct dentry * entry)
  1814. {
  1815. spin_lock(&entry->d_lock);
  1816. _d_rehash(entry);
  1817. spin_unlock(&entry->d_lock);
  1818. }
  1819. EXPORT_SYMBOL(d_rehash);
  1820. /**
  1821. * dentry_update_name_case - update case insensitive dentry with a new name
  1822. * @dentry: dentry to be updated
  1823. * @name: new name
  1824. *
  1825. * Update a case insensitive dentry with new case of name.
  1826. *
  1827. * dentry must have been returned by d_lookup with name @name. Old and new
  1828. * name lengths must match (ie. no d_compare which allows mismatched name
  1829. * lengths).
  1830. *
  1831. * Parent inode i_mutex must be held over d_lookup and into this call (to
  1832. * keep renames and concurrent inserts, and readdir(2) away).
  1833. */
  1834. void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
  1835. {
  1836. BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
  1837. BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
  1838. spin_lock(&dentry->d_lock);
  1839. write_seqcount_begin(&dentry->d_seq);
  1840. memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
  1841. write_seqcount_end(&dentry->d_seq);
  1842. spin_unlock(&dentry->d_lock);
  1843. }
  1844. EXPORT_SYMBOL(dentry_update_name_case);
  1845. static void switch_names(struct dentry *dentry, struct dentry *target)
  1846. {
  1847. if (dname_external(target)) {
  1848. if (dname_external(dentry)) {
  1849. /*
  1850. * Both external: swap the pointers
  1851. */
  1852. swap(target->d_name.name, dentry->d_name.name);
  1853. } else {
  1854. /*
  1855. * dentry:internal, target:external. Steal target's
  1856. * storage and make target internal.
  1857. */
  1858. memcpy(target->d_iname, dentry->d_name.name,
  1859. dentry->d_name.len + 1);
  1860. dentry->d_name.name = target->d_name.name;
  1861. target->d_name.name = target->d_iname;
  1862. }
  1863. } else {
  1864. if (dname_external(dentry)) {
  1865. /*
  1866. * dentry:external, target:internal. Give dentry's
  1867. * storage to target and make dentry internal
  1868. */
  1869. memcpy(dentry->d_iname, target->d_name.name,
  1870. target->d_name.len + 1);
  1871. target->d_name.name = dentry->d_name.name;
  1872. dentry->d_name.name = dentry->d_iname;
  1873. } else {
  1874. /*
  1875. * Both are internal. Just copy target to dentry
  1876. */
  1877. memcpy(dentry->d_iname, target->d_name.name,
  1878. target->d_name.len + 1);
  1879. dentry->d_name.len = target->d_name.len;
  1880. return;
  1881. }
  1882. }
  1883. swap(dentry->d_name.len, target->d_name.len);
  1884. }
  1885. static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
  1886. {
  1887. /*
  1888. * XXXX: do we really need to take target->d_lock?
  1889. */
  1890. if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
  1891. spin_lock(&target->d_parent->d_lock);
  1892. else {
  1893. if (d_ancestor(dentry->d_parent, target->d_parent)) {
  1894. spin_lock(&dentry->d_parent->d_lock);
  1895. spin_lock_nested(&target->d_parent->d_lock,
  1896. DENTRY_D_LOCK_NESTED);
  1897. } else {
  1898. spin_lock(&target->d_parent->d_lock);
  1899. spin_lock_nested(&dentry->d_parent->d_lock,
  1900. DENTRY_D_LOCK_NESTED);
  1901. }
  1902. }
  1903. if (target < dentry) {
  1904. spin_lock_nested(&target->d_lock, 2);
  1905. spin_lock_nested(&dentry->d_lock, 3);
  1906. } else {
  1907. spin_lock_nested(&dentry->d_lock, 2);
  1908. spin_lock_nested(&target->d_lock, 3);
  1909. }
  1910. }
  1911. static void dentry_unlock_parents_for_move(struct dentry *dentry,
  1912. struct dentry *target)
  1913. {
  1914. if (target->d_parent != dentry->d_parent)
  1915. spin_unlock(&dentry->d_parent->d_lock);
  1916. if (target->d_parent != target)
  1917. spin_unlock(&target->d_parent->d_lock);
  1918. }
  1919. /*
  1920. * When switching names, the actual string doesn't strictly have to
  1921. * be preserved in the target - because we're dropping the target
  1922. * anyway. As such, we can just do a simple memcpy() to copy over
  1923. * the new name before we switch.
  1924. *
  1925. * Note that we have to be a lot more careful about getting the hash
  1926. * switched - we have to switch the hash value properly even if it
  1927. * then no longer matches the actual (corrupted) string of the target.
  1928. * The hash value has to match the hash queue that the dentry is on..
  1929. */
  1930. /*
  1931. * __d_move - move a dentry
  1932. * @dentry: entry to move
  1933. * @target: new dentry
  1934. *
  1935. * Update the dcache to reflect the move of a file name. Negative
  1936. * dcache entries should not be moved in this way. Caller must hold
  1937. * rename_lock, the i_mutex of the source and target directories,
  1938. * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
  1939. */
  1940. static void __d_move(struct dentry * dentry, struct dentry * target)
  1941. {
  1942. if (!dentry->d_inode)
  1943. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  1944. BUG_ON(d_ancestor(dentry, target));
  1945. BUG_ON(d_ancestor(target, dentry));
  1946. dentry_lock_for_move(dentry, target);
  1947. write_seqcount_begin(&dentry->d_seq);
  1948. write_seqcount_begin(&target->d_seq);
  1949. /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
  1950. /*
  1951. * Move the dentry to the target hash queue. Don't bother checking
  1952. * for the same hash queue because of how unlikely it is.
  1953. */
  1954. __d_drop(dentry);
  1955. __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
  1956. /* Unhash the target: dput() will then get rid of it */
  1957. __d_drop(target);
  1958. list_del(&dentry->d_u.d_child);
  1959. list_del(&target->d_u.d_child);
  1960. /* Switch the names.. */
  1961. switch_names(dentry, target);
  1962. swap(dentry->d_name.hash, target->d_name.hash);
  1963. /* ... and switch the parents */
  1964. if (IS_ROOT(dentry)) {
  1965. dentry->d_parent = target->d_parent;
  1966. target->d_parent = target;
  1967. INIT_LIST_HEAD(&target->d_u.d_child);
  1968. } else {
  1969. swap(dentry->d_parent, target->d_parent);
  1970. /* And add them back to the (new) parent lists */
  1971. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  1972. }
  1973. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1974. write_seqcount_end(&target->d_seq);
  1975. write_seqcount_end(&dentry->d_seq);
  1976. dentry_unlock_parents_for_move(dentry, target);
  1977. spin_unlock(&target->d_lock);
  1978. fsnotify_d_move(dentry);
  1979. spin_unlock(&dentry->d_lock);
  1980. }
  1981. /*
  1982. * d_move - move a dentry
  1983. * @dentry: entry to move
  1984. * @target: new dentry
  1985. *
  1986. * Update the dcache to reflect the move of a file name. Negative
  1987. * dcache entries should not be moved in this way. See the locking
  1988. * requirements for __d_move.
  1989. */
  1990. void d_move(struct dentry *dentry, struct dentry *target)
  1991. {
  1992. write_seqlock(&rename_lock);
  1993. __d_move(dentry, target);
  1994. write_sequnlock(&rename_lock);
  1995. }
  1996. EXPORT_SYMBOL(d_move);
  1997. /**
  1998. * d_ancestor - search for an ancestor
  1999. * @p1: ancestor dentry
  2000. * @p2: child dentry
  2001. *
  2002. * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
  2003. * an ancestor of p2, else NULL.
  2004. */
  2005. struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
  2006. {
  2007. struct dentry *p;
  2008. for (p = p2; !IS_ROOT(p); p = p->d_parent) {
  2009. if (p->d_parent == p1)
  2010. return p;
  2011. }
  2012. return NULL;
  2013. }
  2014. /*
  2015. * This helper attempts to cope with remotely renamed directories
  2016. *
  2017. * It assumes that the caller is already holding
  2018. * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
  2019. *
  2020. * Note: If ever the locking in lock_rename() changes, then please
  2021. * remember to update this too...
  2022. */
  2023. static struct dentry *__d_unalias(struct inode *inode,
  2024. struct dentry *dentry, struct dentry *alias)
  2025. {
  2026. struct mutex *m1 = NULL, *m2 = NULL;
  2027. struct dentry *ret;
  2028. /* If alias and dentry share a parent, then no extra locks required */
  2029. if (alias->d_parent == dentry->d_parent)
  2030. goto out_unalias;
  2031. /* See lock_rename() */
  2032. ret = ERR_PTR(-EBUSY);
  2033. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  2034. goto out_err;
  2035. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  2036. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  2037. goto out_err;
  2038. m2 = &alias->d_parent->d_inode->i_mutex;
  2039. out_unalias:
  2040. __d_move(alias, dentry);
  2041. ret = alias;
  2042. out_err:
  2043. spin_unlock(&inode->i_lock);
  2044. if (m2)
  2045. mutex_unlock(m2);
  2046. if (m1)
  2047. mutex_unlock(m1);
  2048. return ret;
  2049. }
  2050. /*
  2051. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  2052. * named dentry in place of the dentry to be replaced.
  2053. * returns with anon->d_lock held!
  2054. */
  2055. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  2056. {
  2057. struct dentry *dparent, *aparent;
  2058. dentry_lock_for_move(anon, dentry);
  2059. write_seqcount_begin(&dentry->d_seq);
  2060. write_seqcount_begin(&anon->d_seq);
  2061. dparent = dentry->d_parent;
  2062. aparent = anon->d_parent;
  2063. switch_names(dentry, anon);
  2064. swap(dentry->d_name.hash, anon->d_name.hash);
  2065. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  2066. list_del(&dentry->d_u.d_child);
  2067. if (!IS_ROOT(dentry))
  2068. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  2069. else
  2070. INIT_LIST_HEAD(&dentry->d_u.d_child);
  2071. anon->d_parent = (dparent == dentry) ? anon : dparent;
  2072. list_del(&anon->d_u.d_child);
  2073. if (!IS_ROOT(anon))
  2074. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  2075. else
  2076. INIT_LIST_HEAD(&anon->d_u.d_child);
  2077. write_seqcount_end(&dentry->d_seq);
  2078. write_seqcount_end(&anon->d_seq);
  2079. dentry_unlock_parents_for_move(anon, dentry);
  2080. spin_unlock(&dentry->d_lock);
  2081. /* anon->d_lock still locked, returns locked */
  2082. anon->d_flags &= ~DCACHE_DISCONNECTED;
  2083. }
  2084. /**
  2085. * d_materialise_unique - introduce an inode into the tree
  2086. * @dentry: candidate dentry
  2087. * @inode: inode to bind to the dentry, to which aliases may be attached
  2088. *
  2089. * Introduces an dentry into the tree, substituting an extant disconnected
  2090. * root directory alias in its place if there is one. Caller must hold the
  2091. * i_mutex of the parent directory.
  2092. */
  2093. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  2094. {
  2095. struct dentry *actual;
  2096. BUG_ON(!d_unhashed(dentry));
  2097. if (!inode) {
  2098. actual = dentry;
  2099. __d_instantiate(dentry, NULL);
  2100. d_rehash(actual);
  2101. goto out_nolock;
  2102. }
  2103. spin_lock(&inode->i_lock);
  2104. if (S_ISDIR(inode->i_mode)) {
  2105. struct dentry *alias;
  2106. /* Does an aliased dentry already exist? */
  2107. alias = __d_find_alias(inode, 0);
  2108. if (alias) {
  2109. actual = alias;
  2110. write_seqlock(&rename_lock);
  2111. if (d_ancestor(alias, dentry)) {
  2112. /* Check for loops */
  2113. actual = ERR_PTR(-ELOOP);
  2114. } else if (IS_ROOT(alias)) {
  2115. /* Is this an anonymous mountpoint that we
  2116. * could splice into our tree? */
  2117. __d_materialise_dentry(dentry, alias);
  2118. write_sequnlock(&rename_lock);
  2119. __d_drop(alias);
  2120. goto found;
  2121. } else {
  2122. /* Nope, but we must(!) avoid directory
  2123. * aliasing */
  2124. actual = __d_unalias(inode, dentry, alias);
  2125. }
  2126. write_sequnlock(&rename_lock);
  2127. if (IS_ERR(actual))
  2128. dput(alias);
  2129. goto out_nolock;
  2130. }
  2131. }
  2132. /* Add a unique reference */
  2133. actual = __d_instantiate_unique(dentry, inode);
  2134. if (!actual)
  2135. actual = dentry;
  2136. else
  2137. BUG_ON(!d_unhashed(actual));
  2138. spin_lock(&actual->d_lock);
  2139. found:
  2140. _d_rehash(actual);
  2141. spin_unlock(&actual->d_lock);
  2142. spin_unlock(&inode->i_lock);
  2143. out_nolock:
  2144. if (actual == dentry) {
  2145. security_d_instantiate(dentry, inode);
  2146. return NULL;
  2147. }
  2148. iput(inode);
  2149. return actual;
  2150. }
  2151. EXPORT_SYMBOL_GPL(d_materialise_unique);
  2152. static int prepend(char **buffer, int *buflen, const char *str, int namelen)
  2153. {
  2154. *buflen -= namelen;
  2155. if (*buflen < 0)
  2156. return -ENAMETOOLONG;
  2157. *buffer -= namelen;
  2158. memcpy(*buffer, str, namelen);
  2159. return 0;
  2160. }
  2161. static int prepend_name(char **buffer, int *buflen, struct qstr *name)
  2162. {
  2163. return prepend(buffer, buflen, name->name, name->len);
  2164. }
  2165. /**
  2166. * prepend_path - Prepend path string to a buffer
  2167. * @path: the dentry/vfsmount to report
  2168. * @root: root vfsmnt/dentry (may be modified by this function)
  2169. * @buffer: pointer to the end of the buffer
  2170. * @buflen: pointer to buffer length
  2171. *
  2172. * Caller holds the rename_lock.
  2173. *
  2174. * If path is not reachable from the supplied root, then the value of
  2175. * root is changed (without modifying refcounts).
  2176. */
  2177. static int prepend_path(const struct path *path, struct path *root,
  2178. char **buffer, int *buflen)
  2179. {
  2180. struct dentry *dentry = path->dentry;
  2181. struct vfsmount *vfsmnt = path->mnt;
  2182. bool slash = false;
  2183. int error = 0;
  2184. br_read_lock(vfsmount_lock);
  2185. while (dentry != root->dentry || vfsmnt != root->mnt) {
  2186. struct dentry * parent;
  2187. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  2188. /* Global root? */
  2189. if (vfsmnt->mnt_parent == vfsmnt) {
  2190. goto global_root;
  2191. }
  2192. dentry = vfsmnt->mnt_mountpoint;
  2193. vfsmnt = vfsmnt->mnt_parent;
  2194. continue;
  2195. }
  2196. parent = dentry->d_parent;
  2197. prefetch(parent);
  2198. spin_lock(&dentry->d_lock);
  2199. error = prepend_name(buffer, buflen, &dentry->d_name);
  2200. spin_unlock(&dentry->d_lock);
  2201. if (!error)
  2202. error = prepend(buffer, buflen, "/", 1);
  2203. if (error)
  2204. break;
  2205. slash = true;
  2206. dentry = parent;
  2207. }
  2208. out:
  2209. if (!error && !slash)
  2210. error = prepend(buffer, buflen, "/", 1);
  2211. br_read_unlock(vfsmount_lock);
  2212. return error;
  2213. global_root:
  2214. /*
  2215. * Filesystems needing to implement special "root names"
  2216. * should do so with ->d_dname()
  2217. */
  2218. if (IS_ROOT(dentry) &&
  2219. (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
  2220. WARN(1, "Root dentry has weird name <%.*s>\n",
  2221. (int) dentry->d_name.len, dentry->d_name.name);
  2222. }
  2223. root->mnt = vfsmnt;
  2224. root->dentry = dentry;
  2225. goto out;
  2226. }
  2227. /**
  2228. * __d_path - return the path of a dentry
  2229. * @path: the dentry/vfsmount to report
  2230. * @root: root vfsmnt/dentry (may be modified by this function)
  2231. * @buf: buffer to return value in
  2232. * @buflen: buffer length
  2233. *
  2234. * Convert a dentry into an ASCII path name.
  2235. *
  2236. * Returns a pointer into the buffer or an error code if the
  2237. * path was too long.
  2238. *
  2239. * "buflen" should be positive.
  2240. *
  2241. * If path is not reachable from the supplied root, then the value of
  2242. * root is changed (without modifying refcounts).
  2243. */
  2244. char *__d_path(const struct path *path, struct path *root,
  2245. char *buf, int buflen)
  2246. {
  2247. char *res = buf + buflen;
  2248. int error;
  2249. prepend(&res, &buflen, "\0", 1);
  2250. write_seqlock(&rename_lock);
  2251. error = prepend_path(path, root, &res, &buflen);
  2252. write_sequnlock(&rename_lock);
  2253. if (error)
  2254. return ERR_PTR(error);
  2255. return res;
  2256. }
  2257. /*
  2258. * same as __d_path but appends "(deleted)" for unlinked files.
  2259. */
  2260. static int path_with_deleted(const struct path *path, struct path *root,
  2261. char **buf, int *buflen)
  2262. {
  2263. prepend(buf, buflen, "\0", 1);
  2264. if (d_unlinked(path->dentry)) {
  2265. int error = prepend(buf, buflen, " (deleted)", 10);
  2266. if (error)
  2267. return error;
  2268. }
  2269. return prepend_path(path, root, buf, buflen);
  2270. }
  2271. static int prepend_unreachable(char **buffer, int *buflen)
  2272. {
  2273. return prepend(buffer, buflen, "(unreachable)", 13);
  2274. }
  2275. /**
  2276. * d_path - return the path of a dentry
  2277. * @path: path to report
  2278. * @buf: buffer to return value in
  2279. * @buflen: buffer length
  2280. *
  2281. * Convert a dentry into an ASCII path name. If the entry has been deleted
  2282. * the string " (deleted)" is appended. Note that this is ambiguous.
  2283. *
  2284. * Returns a pointer into the buffer or an error code if the path was
  2285. * too long. Note: Callers should use the returned pointer, not the passed
  2286. * in buffer, to use the name! The implementation often starts at an offset
  2287. * into the buffer, and may leave 0 bytes at the start.
  2288. *
  2289. * "buflen" should be positive.
  2290. */
  2291. char *d_path(const struct path *path, char *buf, int buflen)
  2292. {
  2293. char *res = buf + buflen;
  2294. struct path root;
  2295. struct path tmp;
  2296. int error;
  2297. /*
  2298. * We have various synthetic filesystems that never get mounted. On
  2299. * these filesystems dentries are never used for lookup purposes, and
  2300. * thus don't need to be hashed. They also don't need a name until a
  2301. * user wants to identify the object in /proc/pid/fd/. The little hack
  2302. * below allows us to generate a name for these objects on demand:
  2303. */
  2304. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  2305. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  2306. get_fs_root(current->fs, &root);
  2307. write_seqlock(&rename_lock);
  2308. tmp = root;
  2309. error = path_with_deleted(path, &tmp, &res, &buflen);
  2310. if (error)
  2311. res = ERR_PTR(error);
  2312. write_sequnlock(&rename_lock);
  2313. path_put(&root);
  2314. return res;
  2315. }
  2316. EXPORT_SYMBOL(d_path);
  2317. /**
  2318. * d_path_with_unreachable - return the path of a dentry
  2319. * @path: path to report
  2320. * @buf: buffer to return value in
  2321. * @buflen: buffer length
  2322. *
  2323. * The difference from d_path() is that this prepends "(unreachable)"
  2324. * to paths which are unreachable from the current process' root.
  2325. */
  2326. char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
  2327. {
  2328. char *res = buf + buflen;
  2329. struct path root;
  2330. struct path tmp;
  2331. int error;
  2332. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  2333. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  2334. get_fs_root(current->fs, &root);
  2335. write_seqlock(&rename_lock);
  2336. tmp = root;
  2337. error = path_with_deleted(path, &tmp, &res, &buflen);
  2338. if (!error && !path_equal(&tmp, &root))
  2339. error = prepend_unreachable(&res, &buflen);
  2340. write_sequnlock(&rename_lock);
  2341. path_put(&root);
  2342. if (error)
  2343. res = ERR_PTR(error);
  2344. return res;
  2345. }
  2346. /*
  2347. * Helper function for dentry_operations.d_dname() members
  2348. */
  2349. char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
  2350. const char *fmt, ...)
  2351. {
  2352. va_list args;
  2353. char temp[64];
  2354. int sz;
  2355. va_start(args, fmt);
  2356. sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
  2357. va_end(args);
  2358. if (sz > sizeof(temp) || sz > buflen)
  2359. return ERR_PTR(-ENAMETOOLONG);
  2360. buffer += buflen - sz;
  2361. return memcpy(buffer, temp, sz);
  2362. }
  2363. /*
  2364. * Write full pathname from the root of the filesystem into the buffer.
  2365. */
  2366. static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
  2367. {
  2368. char *end = buf + buflen;
  2369. char *retval;
  2370. prepend(&end, &buflen, "\0", 1);
  2371. if (buflen < 1)
  2372. goto Elong;
  2373. /* Get '/' right */
  2374. retval = end-1;
  2375. *retval = '/';
  2376. while (!IS_ROOT(dentry)) {
  2377. struct dentry *parent = dentry->d_parent;
  2378. int error;
  2379. prefetch(parent);
  2380. spin_lock(&dentry->d_lock);
  2381. error = prepend_name(&end, &buflen, &dentry->d_name);
  2382. spin_unlock(&dentry->d_lock);
  2383. if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
  2384. goto Elong;
  2385. retval = end;
  2386. dentry = parent;
  2387. }
  2388. return retval;
  2389. Elong:
  2390. return ERR_PTR(-ENAMETOOLONG);
  2391. }
  2392. char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
  2393. {
  2394. char *retval;
  2395. write_seqlock(&rename_lock);
  2396. retval = __dentry_path(dentry, buf, buflen);
  2397. write_sequnlock(&rename_lock);
  2398. return retval;
  2399. }
  2400. EXPORT_SYMBOL(dentry_path_raw);
  2401. char *dentry_path(struct dentry *dentry, char *buf, int buflen)
  2402. {
  2403. char *p = NULL;
  2404. char *retval;
  2405. write_seqlock(&rename_lock);
  2406. if (d_unlinked(dentry)) {
  2407. p = buf + buflen;
  2408. if (prepend(&p, &buflen, "//deleted", 10) != 0)
  2409. goto Elong;
  2410. buflen++;
  2411. }
  2412. retval = __dentry_path(dentry, buf, buflen);
  2413. write_sequnlock(&rename_lock);
  2414. if (!IS_ERR(retval) && p)
  2415. *p = '/'; /* restore '/' overriden with '\0' */
  2416. return retval;
  2417. Elong:
  2418. return ERR_PTR(-ENAMETOOLONG);
  2419. }
  2420. /*
  2421. * NOTE! The user-level library version returns a
  2422. * character pointer. The kernel system call just
  2423. * returns the length of the buffer filled (which
  2424. * includes the ending '\0' character), or a negative
  2425. * error value. So libc would do something like
  2426. *
  2427. * char *getcwd(char * buf, size_t size)
  2428. * {
  2429. * int retval;
  2430. *
  2431. * retval = sys_getcwd(buf, size);
  2432. * if (retval >= 0)
  2433. * return buf;
  2434. * errno = -retval;
  2435. * return NULL;
  2436. * }
  2437. */
  2438. SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
  2439. {
  2440. int error;
  2441. struct path pwd, root;
  2442. char *page = (char *) __get_free_page(GFP_USER);
  2443. if (!page)
  2444. return -ENOMEM;
  2445. get_fs_root_and_pwd(current->fs, &root, &pwd);
  2446. error = -ENOENT;
  2447. write_seqlock(&rename_lock);
  2448. if (!d_unlinked(pwd.dentry)) {
  2449. unsigned long len;
  2450. struct path tmp = root;
  2451. char *cwd = page + PAGE_SIZE;
  2452. int buflen = PAGE_SIZE;
  2453. prepend(&cwd, &buflen, "\0", 1);
  2454. error = prepend_path(&pwd, &tmp, &cwd, &buflen);
  2455. write_sequnlock(&rename_lock);
  2456. if (error)
  2457. goto out;
  2458. /* Unreachable from current root */
  2459. if (!path_equal(&tmp, &root)) {
  2460. error = prepend_unreachable(&cwd, &buflen);
  2461. if (error)
  2462. goto out;
  2463. }
  2464. error = -ERANGE;
  2465. len = PAGE_SIZE + page - cwd;
  2466. if (len <= size) {
  2467. error = len;
  2468. if (copy_to_user(buf, cwd, len))
  2469. error = -EFAULT;
  2470. }
  2471. } else {
  2472. write_sequnlock(&rename_lock);
  2473. }
  2474. out:
  2475. path_put(&pwd);
  2476. path_put(&root);
  2477. free_page((unsigned long) page);
  2478. return error;
  2479. }
  2480. /*
  2481. * Test whether new_dentry is a subdirectory of old_dentry.
  2482. *
  2483. * Trivially implemented using the dcache structure
  2484. */
  2485. /**
  2486. * is_subdir - is new dentry a subdirectory of old_dentry
  2487. * @new_dentry: new dentry
  2488. * @old_dentry: old dentry
  2489. *
  2490. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  2491. * Returns 0 otherwise.
  2492. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  2493. */
  2494. int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
  2495. {
  2496. int result;
  2497. unsigned seq;
  2498. if (new_dentry == old_dentry)
  2499. return 1;
  2500. do {
  2501. /* for restarting inner loop in case of seq retry */
  2502. seq = read_seqbegin(&rename_lock);
  2503. /*
  2504. * Need rcu_readlock to protect against the d_parent trashing
  2505. * due to d_move
  2506. */
  2507. rcu_read_lock();
  2508. if (d_ancestor(old_dentry, new_dentry))
  2509. result = 1;
  2510. else
  2511. result = 0;
  2512. rcu_read_unlock();
  2513. } while (read_seqretry(&rename_lock, seq));
  2514. return result;
  2515. }
  2516. int path_is_under(struct path *path1, struct path *path2)
  2517. {
  2518. struct vfsmount *mnt = path1->mnt;
  2519. struct dentry *dentry = path1->dentry;
  2520. int res;
  2521. br_read_lock(vfsmount_lock);
  2522. if (mnt != path2->mnt) {
  2523. for (;;) {
  2524. if (mnt->mnt_parent == mnt) {
  2525. br_read_unlock(vfsmount_lock);
  2526. return 0;
  2527. }
  2528. if (mnt->mnt_parent == path2->mnt)
  2529. break;
  2530. mnt = mnt->mnt_parent;
  2531. }
  2532. dentry = mnt->mnt_mountpoint;
  2533. }
  2534. res = is_subdir(dentry, path2->dentry);
  2535. br_read_unlock(vfsmount_lock);
  2536. return res;
  2537. }
  2538. EXPORT_SYMBOL(path_is_under);
  2539. void d_genocide(struct dentry *root)
  2540. {
  2541. struct dentry *this_parent;
  2542. struct list_head *next;
  2543. unsigned seq;
  2544. int locked = 0;
  2545. seq = read_seqbegin(&rename_lock);
  2546. again:
  2547. this_parent = root;
  2548. spin_lock(&this_parent->d_lock);
  2549. repeat:
  2550. next = this_parent->d_subdirs.next;
  2551. resume:
  2552. while (next != &this_parent->d_subdirs) {
  2553. struct list_head *tmp = next;
  2554. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  2555. next = tmp->next;
  2556. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  2557. if (d_unhashed(dentry) || !dentry->d_inode) {
  2558. spin_unlock(&dentry->d_lock);
  2559. continue;
  2560. }
  2561. if (!list_empty(&dentry->d_subdirs)) {
  2562. spin_unlock(&this_parent->d_lock);
  2563. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  2564. this_parent = dentry;
  2565. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  2566. goto repeat;
  2567. }
  2568. if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
  2569. dentry->d_flags |= DCACHE_GENOCIDE;
  2570. dentry->d_count--;
  2571. }
  2572. spin_unlock(&dentry->d_lock);
  2573. }
  2574. if (this_parent != root) {
  2575. struct dentry *child = this_parent;
  2576. if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
  2577. this_parent->d_flags |= DCACHE_GENOCIDE;
  2578. this_parent->d_count--;
  2579. }
  2580. this_parent = try_to_ascend(this_parent, locked, seq);
  2581. if (!this_parent)
  2582. goto rename_retry;
  2583. next = child->d_u.d_child.next;
  2584. goto resume;
  2585. }
  2586. spin_unlock(&this_parent->d_lock);
  2587. if (!locked && read_seqretry(&rename_lock, seq))
  2588. goto rename_retry;
  2589. if (locked)
  2590. write_sequnlock(&rename_lock);
  2591. return;
  2592. rename_retry:
  2593. locked = 1;
  2594. write_seqlock(&rename_lock);
  2595. goto again;
  2596. }
  2597. /**
  2598. * find_inode_number - check for dentry with name
  2599. * @dir: directory to check
  2600. * @name: Name to find.
  2601. *
  2602. * Check whether a dentry already exists for the given name,
  2603. * and return the inode number if it has an inode. Otherwise
  2604. * 0 is returned.
  2605. *
  2606. * This routine is used to post-process directory listings for
  2607. * filesystems using synthetic inode numbers, and is necessary
  2608. * to keep getcwd() working.
  2609. */
  2610. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  2611. {
  2612. struct dentry * dentry;
  2613. ino_t ino = 0;
  2614. dentry = d_hash_and_lookup(dir, name);
  2615. if (dentry) {
  2616. if (dentry->d_inode)
  2617. ino = dentry->d_inode->i_ino;
  2618. dput(dentry);
  2619. }
  2620. return ino;
  2621. }
  2622. EXPORT_SYMBOL(find_inode_number);
  2623. static __initdata unsigned long dhash_entries;
  2624. static int __init set_dhash_entries(char *str)
  2625. {
  2626. if (!str)
  2627. return 0;
  2628. dhash_entries = simple_strtoul(str, &str, 0);
  2629. return 1;
  2630. }
  2631. __setup("dhash_entries=", set_dhash_entries);
  2632. static void __init dcache_init_early(void)
  2633. {
  2634. int loop;
  2635. /* If hashes are distributed across NUMA nodes, defer
  2636. * hash allocation until vmalloc space is available.
  2637. */
  2638. if (hashdist)
  2639. return;
  2640. dentry_hashtable =
  2641. alloc_large_system_hash("Dentry cache",
  2642. sizeof(struct hlist_bl_head),
  2643. dhash_entries,
  2644. 13,
  2645. HASH_EARLY,
  2646. &d_hash_shift,
  2647. &d_hash_mask,
  2648. 0);
  2649. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2650. INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
  2651. }
  2652. static void __init dcache_init(void)
  2653. {
  2654. int loop;
  2655. /*
  2656. * A constructor could be added for stable state like the lists,
  2657. * but it is probably not worth it because of the cache nature
  2658. * of the dcache.
  2659. */
  2660. dentry_cache = KMEM_CACHE(dentry,
  2661. SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
  2662. /* Hash may have been set up in dcache_init_early */
  2663. if (!hashdist)
  2664. return;
  2665. dentry_hashtable =
  2666. alloc_large_system_hash("Dentry cache",
  2667. sizeof(struct hlist_bl_head),
  2668. dhash_entries,
  2669. 13,
  2670. 0,
  2671. &d_hash_shift,
  2672. &d_hash_mask,
  2673. 0);
  2674. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2675. INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
  2676. }
  2677. /* SLAB cache for __getname() consumers */
  2678. struct kmem_cache *names_cachep __read_mostly;
  2679. EXPORT_SYMBOL(names_cachep);
  2680. EXPORT_SYMBOL(d_genocide);
  2681. void __init vfs_caches_init_early(void)
  2682. {
  2683. dcache_init_early();
  2684. inode_init_early();
  2685. }
  2686. void __init vfs_caches_init(unsigned long mempages)
  2687. {
  2688. unsigned long reserve;
  2689. /* Base hash sizes on available memory, with a reserve equal to
  2690. 150% of current kernel size */
  2691. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  2692. mempages -= reserve;
  2693. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  2694. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2695. dcache_init();
  2696. inode_init();
  2697. files_init(mempages);
  2698. mnt_init();
  2699. bdev_cache_init();
  2700. chrdev_init();
  2701. }