dcache.c 78 KB

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