dcache.c 83 KB

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