dcache.c 88 KB

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