dcache.c 80 KB

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