dcache.c 91 KB

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