dcache.c 85 KB

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