dcache.c 80 KB

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