dcache.c 80 KB

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