dcache.c 78 KB

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