dcache.c 76 KB

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