dcache.c 77 KB

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