dcache.c 79 KB

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