dcache.c 88 KB

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