dcache.c 95 KB

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