dcache.c 78 KB

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