dcache.c 72 KB

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