dcache.c 76 KB

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