dcache.c 88 KB

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