dcache.c 78 KB

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