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