dcache.c 72 KB

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