dcache.c 88 KB

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