dcache.c 78 KB

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