dcache.c 71 KB

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