dcache.c 71 KB

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