dcache.c 88 KB

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