dcache.c 77 KB

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