dcache.c 88 KB

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