dcache.c 82 KB

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