dcache.c 87 KB

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