dcache.c 79 KB

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