dcache.c 78 KB

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