dcache.c 89 KB

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