dcache.c 95 KB

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