dcache.c 87 KB

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