dcache.c 85 KB

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