dcache.c 76 KB

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