dcache.c 78 KB

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