dcache.c 76 KB

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