dcache.c 77 KB

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