dir.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/nfs/dir.c
  4. *
  5. * Copyright (C) 1992 Rick Sladkey
  6. *
  7. * nfs directory handling functions
  8. *
  9. * 10 Apr 1996 Added silly rename for unlink --okir
  10. * 28 Sep 1996 Improved directory cache --okir
  11. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  12. * Re-implemented silly rename for unlink, newly implemented
  13. * silly rename for nfs_rename() following the suggestions
  14. * of Olaf Kirch (okir) found in this file.
  15. * Following Linus comments on my original hack, this version
  16. * depends only on the dcache stuff and doesn't touch the inode
  17. * layer (iput() and friends).
  18. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  19. */
  20. #include <linux/compat.h>
  21. #include <linux/module.h>
  22. #include <linux/time.h>
  23. #include <linux/errno.h>
  24. #include <linux/stat.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/string.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/mm.h>
  30. #include <linux/sunrpc/clnt.h>
  31. #include <linux/nfs_fs.h>
  32. #include <linux/nfs_mount.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/pagevec.h>
  35. #include <linux/namei.h>
  36. #include <linux/mount.h>
  37. #include <linux/swap.h>
  38. #include <linux/sched.h>
  39. #include <linux/kmemleak.h>
  40. #include <linux/xattr.h>
  41. #include <linux/hash.h>
  42. #include "delegation.h"
  43. #include "iostat.h"
  44. #include "internal.h"
  45. #include "fscache.h"
  46. #include "nfstrace.h"
  47. /* #define NFS_DEBUG_VERBOSE 1 */
  48. static int nfs_opendir(struct inode *, struct file *);
  49. static int nfs_closedir(struct inode *, struct file *);
  50. static int nfs_readdir(struct file *, struct dir_context *);
  51. static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
  52. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  53. static void nfs_readdir_clear_array(struct folio *);
  54. static int nfs_do_create(struct inode *dir, struct dentry *dentry,
  55. umode_t mode, int open_flags);
  56. const struct file_operations nfs_dir_operations = {
  57. .llseek = nfs_llseek_dir,
  58. .read = generic_read_dir,
  59. .iterate_shared = nfs_readdir,
  60. .open = nfs_opendir,
  61. .release = nfs_closedir,
  62. .fsync = nfs_fsync_dir,
  63. };
  64. const struct address_space_operations nfs_dir_aops = {
  65. .free_folio = nfs_readdir_clear_array,
  66. };
  67. #define NFS_INIT_DTSIZE PAGE_SIZE
  68. static struct nfs_open_dir_context *
  69. alloc_nfs_open_dir_context(struct inode *dir)
  70. {
  71. struct nfs_inode *nfsi = NFS_I(dir);
  72. struct nfs_open_dir_context *ctx;
  73. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
  74. if (ctx != NULL) {
  75. ctx->attr_gencount = nfsi->attr_gencount;
  76. ctx->dtsize = NFS_INIT_DTSIZE;
  77. spin_lock(&dir->i_lock);
  78. if (list_empty(&nfsi->open_files) &&
  79. (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
  80. nfs_set_cache_invalid(dir,
  81. NFS_INO_INVALID_DATA |
  82. NFS_INO_REVAL_FORCED);
  83. list_add_tail_rcu(&ctx->list, &nfsi->open_files);
  84. memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf));
  85. spin_unlock(&dir->i_lock);
  86. return ctx;
  87. }
  88. return ERR_PTR(-ENOMEM);
  89. }
  90. static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
  91. {
  92. spin_lock(&dir->i_lock);
  93. list_del_rcu(&ctx->list);
  94. spin_unlock(&dir->i_lock);
  95. kfree_rcu(ctx, rcu_head);
  96. }
  97. /*
  98. * Open file
  99. */
  100. static int
  101. nfs_opendir(struct inode *inode, struct file *filp)
  102. {
  103. int res = 0;
  104. struct nfs_open_dir_context *ctx;
  105. dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
  106. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  107. ctx = alloc_nfs_open_dir_context(inode);
  108. if (IS_ERR(ctx)) {
  109. res = PTR_ERR(ctx);
  110. goto out;
  111. }
  112. filp->private_data = ctx;
  113. out:
  114. return res;
  115. }
  116. static int
  117. nfs_closedir(struct inode *inode, struct file *filp)
  118. {
  119. put_nfs_open_dir_context(file_inode(filp), filp->private_data);
  120. return 0;
  121. }
  122. struct nfs_cache_array_entry {
  123. u64 cookie;
  124. u64 ino;
  125. const char *name;
  126. unsigned int name_len;
  127. unsigned char d_type;
  128. };
  129. struct nfs_cache_array {
  130. u64 change_attr;
  131. u64 last_cookie;
  132. unsigned int size;
  133. unsigned char folio_full : 1,
  134. folio_is_eof : 1,
  135. cookies_are_ordered : 1;
  136. struct nfs_cache_array_entry array[] __counted_by(size);
  137. };
  138. struct nfs_readdir_descriptor {
  139. struct file *file;
  140. struct folio *folio;
  141. struct dir_context *ctx;
  142. pgoff_t folio_index;
  143. pgoff_t folio_index_max;
  144. u64 dir_cookie;
  145. u64 last_cookie;
  146. loff_t current_index;
  147. __be32 verf[NFS_DIR_VERIFIER_SIZE];
  148. unsigned long dir_verifier;
  149. unsigned long timestamp;
  150. unsigned long gencount;
  151. unsigned long attr_gencount;
  152. unsigned int cache_entry_index;
  153. unsigned int buffer_fills;
  154. unsigned int dtsize;
  155. bool clear_cache;
  156. bool plus;
  157. bool eob;
  158. bool eof;
  159. };
  160. static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz)
  161. {
  162. struct nfs_server *server = NFS_SERVER(file_inode(desc->file));
  163. unsigned int maxsize = server->dtsize;
  164. if (sz > maxsize)
  165. sz = maxsize;
  166. if (sz < NFS_MIN_FILE_IO_SIZE)
  167. sz = NFS_MIN_FILE_IO_SIZE;
  168. desc->dtsize = sz;
  169. }
  170. static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc)
  171. {
  172. nfs_set_dtsize(desc, desc->dtsize >> 1);
  173. }
  174. static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc)
  175. {
  176. nfs_set_dtsize(desc, desc->dtsize << 1);
  177. }
  178. static void nfs_readdir_folio_init_array(struct folio *folio, u64 last_cookie,
  179. u64 change_attr)
  180. {
  181. struct nfs_cache_array *array;
  182. array = kmap_local_folio(folio, 0);
  183. array->change_attr = change_attr;
  184. array->last_cookie = last_cookie;
  185. array->size = 0;
  186. array->folio_full = 0;
  187. array->folio_is_eof = 0;
  188. array->cookies_are_ordered = 1;
  189. kunmap_local(array);
  190. }
  191. /*
  192. * we are freeing strings created by nfs_add_to_readdir_array()
  193. */
  194. static void nfs_readdir_clear_array(struct folio *folio)
  195. {
  196. struct nfs_cache_array *array;
  197. unsigned int i;
  198. array = kmap_local_folio(folio, 0);
  199. for (i = 0; i < array->size; i++)
  200. kfree(array->array[i].name);
  201. array->size = 0;
  202. kunmap_local(array);
  203. }
  204. static void nfs_readdir_folio_reinit_array(struct folio *folio, u64 last_cookie,
  205. u64 change_attr)
  206. {
  207. nfs_readdir_clear_array(folio);
  208. nfs_readdir_folio_init_array(folio, last_cookie, change_attr);
  209. }
  210. static struct folio *
  211. nfs_readdir_folio_array_alloc(u64 last_cookie, gfp_t gfp_flags)
  212. {
  213. struct folio *folio = folio_alloc(gfp_flags, 0);
  214. if (folio)
  215. nfs_readdir_folio_init_array(folio, last_cookie, 0);
  216. return folio;
  217. }
  218. static void nfs_readdir_folio_array_free(struct folio *folio)
  219. {
  220. if (folio) {
  221. nfs_readdir_clear_array(folio);
  222. folio_put(folio);
  223. }
  224. }
  225. static u64 nfs_readdir_array_index_cookie(struct nfs_cache_array *array)
  226. {
  227. return array->size == 0 ? array->last_cookie : array->array[0].cookie;
  228. }
  229. static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
  230. {
  231. array->folio_is_eof = 1;
  232. array->folio_full = 1;
  233. }
  234. static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
  235. {
  236. return array->folio_full;
  237. }
  238. /*
  239. * the caller is responsible for freeing qstr.name
  240. * when called by nfs_readdir_add_to_array, the strings will be freed in
  241. * nfs_clear_readdir_array()
  242. */
  243. static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
  244. {
  245. const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
  246. /*
  247. * Avoid a kmemleak false positive. The pointer to the name is stored
  248. * in a page cache page which kmemleak does not scan.
  249. */
  250. if (ret != NULL)
  251. kmemleak_not_leak(ret);
  252. return ret;
  253. }
  254. static size_t nfs_readdir_array_maxentries(void)
  255. {
  256. return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
  257. sizeof(struct nfs_cache_array_entry);
  258. }
  259. /*
  260. * Check that the next array entry lies entirely within the page bounds
  261. */
  262. static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
  263. {
  264. if (array->folio_full)
  265. return -ENOSPC;
  266. if (array->size == nfs_readdir_array_maxentries()) {
  267. array->folio_full = 1;
  268. return -ENOSPC;
  269. }
  270. return 0;
  271. }
  272. static int nfs_readdir_folio_array_append(struct folio *folio,
  273. const struct nfs_entry *entry,
  274. u64 *cookie)
  275. {
  276. struct nfs_cache_array *array;
  277. struct nfs_cache_array_entry *cache_entry;
  278. const char *name;
  279. int ret = -ENOMEM;
  280. name = nfs_readdir_copy_name(entry->name, entry->len);
  281. array = kmap_local_folio(folio, 0);
  282. if (!name)
  283. goto out;
  284. ret = nfs_readdir_array_can_expand(array);
  285. if (ret) {
  286. kfree(name);
  287. goto out;
  288. }
  289. array->size++;
  290. cache_entry = &array->array[array->size - 1];
  291. cache_entry->cookie = array->last_cookie;
  292. cache_entry->ino = entry->ino;
  293. cache_entry->d_type = entry->d_type;
  294. cache_entry->name_len = entry->len;
  295. cache_entry->name = name;
  296. array->last_cookie = entry->cookie;
  297. if (array->last_cookie <= cache_entry->cookie)
  298. array->cookies_are_ordered = 0;
  299. if (entry->eof != 0)
  300. nfs_readdir_array_set_eof(array);
  301. out:
  302. *cookie = array->last_cookie;
  303. kunmap_local(array);
  304. return ret;
  305. }
  306. #define NFS_READDIR_COOKIE_MASK (U32_MAX >> 14)
  307. /*
  308. * Hash algorithm allowing content addressible access to sequences
  309. * of directory cookies. Content is addressed by the value of the
  310. * cookie index of the first readdir entry in a page.
  311. *
  312. * We select only the first 18 bits to avoid issues with excessive
  313. * memory use for the page cache XArray. 18 bits should allow the caching
  314. * of 262144 pages of sequences of readdir entries. Since each page holds
  315. * 127 readdir entries for a typical 64-bit system, that works out to a
  316. * cache of ~ 33 million entries per directory.
  317. */
  318. static pgoff_t nfs_readdir_folio_cookie_hash(u64 cookie)
  319. {
  320. if (cookie == 0)
  321. return 0;
  322. return hash_64(cookie, 18);
  323. }
  324. static bool nfs_readdir_folio_validate(struct folio *folio, u64 last_cookie,
  325. u64 change_attr)
  326. {
  327. struct nfs_cache_array *array = kmap_local_folio(folio, 0);
  328. int ret = true;
  329. if (array->change_attr != change_attr)
  330. ret = false;
  331. if (nfs_readdir_array_index_cookie(array) != last_cookie)
  332. ret = false;
  333. kunmap_local(array);
  334. return ret;
  335. }
  336. static void nfs_readdir_folio_unlock_and_put(struct folio *folio)
  337. {
  338. folio_unlock(folio);
  339. folio_put(folio);
  340. }
  341. static void nfs_readdir_folio_init_and_validate(struct folio *folio, u64 cookie,
  342. u64 change_attr)
  343. {
  344. if (folio_test_uptodate(folio)) {
  345. if (nfs_readdir_folio_validate(folio, cookie, change_attr))
  346. return;
  347. nfs_readdir_clear_array(folio);
  348. }
  349. nfs_readdir_folio_init_array(folio, cookie, change_attr);
  350. folio_mark_uptodate(folio);
  351. }
  352. static struct folio *nfs_readdir_folio_get_locked(struct address_space *mapping,
  353. u64 cookie, u64 change_attr)
  354. {
  355. pgoff_t index = nfs_readdir_folio_cookie_hash(cookie);
  356. struct folio *folio;
  357. folio = filemap_grab_folio(mapping, index);
  358. if (IS_ERR(folio))
  359. return NULL;
  360. nfs_readdir_folio_init_and_validate(folio, cookie, change_attr);
  361. return folio;
  362. }
  363. static u64 nfs_readdir_folio_last_cookie(struct folio *folio)
  364. {
  365. struct nfs_cache_array *array;
  366. u64 ret;
  367. array = kmap_local_folio(folio, 0);
  368. ret = array->last_cookie;
  369. kunmap_local(array);
  370. return ret;
  371. }
  372. static bool nfs_readdir_folio_needs_filling(struct folio *folio)
  373. {
  374. struct nfs_cache_array *array;
  375. bool ret;
  376. array = kmap_local_folio(folio, 0);
  377. ret = !nfs_readdir_array_is_full(array);
  378. kunmap_local(array);
  379. return ret;
  380. }
  381. static void nfs_readdir_folio_set_eof(struct folio *folio)
  382. {
  383. struct nfs_cache_array *array;
  384. array = kmap_local_folio(folio, 0);
  385. nfs_readdir_array_set_eof(array);
  386. kunmap_local(array);
  387. }
  388. static struct folio *nfs_readdir_folio_get_next(struct address_space *mapping,
  389. u64 cookie, u64 change_attr)
  390. {
  391. pgoff_t index = nfs_readdir_folio_cookie_hash(cookie);
  392. struct folio *folio;
  393. folio = __filemap_get_folio(mapping, index,
  394. FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
  395. mapping_gfp_mask(mapping));
  396. if (IS_ERR(folio))
  397. return NULL;
  398. nfs_readdir_folio_init_and_validate(folio, cookie, change_attr);
  399. if (nfs_readdir_folio_last_cookie(folio) != cookie)
  400. nfs_readdir_folio_reinit_array(folio, cookie, change_attr);
  401. return folio;
  402. }
  403. static inline
  404. int is_32bit_api(void)
  405. {
  406. #ifdef CONFIG_COMPAT
  407. return in_compat_syscall();
  408. #else
  409. return (BITS_PER_LONG == 32);
  410. #endif
  411. }
  412. static
  413. bool nfs_readdir_use_cookie(const struct file *filp)
  414. {
  415. if ((filp->f_mode & FMODE_32BITHASH) ||
  416. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  417. return false;
  418. return true;
  419. }
  420. static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
  421. struct nfs_readdir_descriptor *desc)
  422. {
  423. if (array->folio_full) {
  424. desc->last_cookie = array->last_cookie;
  425. desc->current_index += array->size;
  426. desc->cache_entry_index = 0;
  427. desc->folio_index++;
  428. } else
  429. desc->last_cookie = nfs_readdir_array_index_cookie(array);
  430. }
  431. static void nfs_readdir_rewind_search(struct nfs_readdir_descriptor *desc)
  432. {
  433. desc->current_index = 0;
  434. desc->last_cookie = 0;
  435. desc->folio_index = 0;
  436. }
  437. static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
  438. struct nfs_readdir_descriptor *desc)
  439. {
  440. loff_t diff = desc->ctx->pos - desc->current_index;
  441. unsigned int index;
  442. if (diff < 0)
  443. goto out_eof;
  444. if (diff >= array->size) {
  445. if (array->folio_is_eof)
  446. goto out_eof;
  447. nfs_readdir_seek_next_array(array, desc);
  448. return -EAGAIN;
  449. }
  450. index = (unsigned int)diff;
  451. desc->dir_cookie = array->array[index].cookie;
  452. desc->cache_entry_index = index;
  453. return 0;
  454. out_eof:
  455. desc->eof = true;
  456. return -EBADCOOKIE;
  457. }
  458. static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
  459. u64 cookie)
  460. {
  461. if (!array->cookies_are_ordered)
  462. return true;
  463. /* Optimisation for monotonically increasing cookies */
  464. if (cookie >= array->last_cookie)
  465. return false;
  466. if (array->size && cookie < array->array[0].cookie)
  467. return false;
  468. return true;
  469. }
  470. static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
  471. struct nfs_readdir_descriptor *desc)
  472. {
  473. unsigned int i;
  474. int status = -EAGAIN;
  475. if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
  476. goto check_eof;
  477. for (i = 0; i < array->size; i++) {
  478. if (array->array[i].cookie == desc->dir_cookie) {
  479. if (nfs_readdir_use_cookie(desc->file))
  480. desc->ctx->pos = desc->dir_cookie;
  481. else
  482. desc->ctx->pos = desc->current_index + i;
  483. desc->cache_entry_index = i;
  484. return 0;
  485. }
  486. }
  487. check_eof:
  488. if (array->folio_is_eof) {
  489. status = -EBADCOOKIE;
  490. if (desc->dir_cookie == array->last_cookie)
  491. desc->eof = true;
  492. } else
  493. nfs_readdir_seek_next_array(array, desc);
  494. return status;
  495. }
  496. static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
  497. {
  498. struct nfs_cache_array *array;
  499. int status;
  500. array = kmap_local_folio(desc->folio, 0);
  501. if (desc->dir_cookie == 0)
  502. status = nfs_readdir_search_for_pos(array, desc);
  503. else
  504. status = nfs_readdir_search_for_cookie(array, desc);
  505. kunmap_local(array);
  506. return status;
  507. }
  508. /* Fill a page with xdr information before transferring to the cache page */
  509. static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
  510. __be32 *verf, u64 cookie,
  511. struct page **pages, size_t bufsize,
  512. __be32 *verf_res)
  513. {
  514. struct inode *inode = file_inode(desc->file);
  515. struct nfs_readdir_arg arg = {
  516. .dentry = file_dentry(desc->file),
  517. .cred = desc->file->f_cred,
  518. .verf = verf,
  519. .cookie = cookie,
  520. .pages = pages,
  521. .page_len = bufsize,
  522. .plus = desc->plus,
  523. };
  524. struct nfs_readdir_res res = {
  525. .verf = verf_res,
  526. };
  527. unsigned long timestamp, gencount;
  528. int error;
  529. again:
  530. timestamp = jiffies;
  531. gencount = nfs_inc_attr_generation_counter();
  532. desc->dir_verifier = nfs_save_change_attribute(inode);
  533. error = NFS_PROTO(inode)->readdir(&arg, &res);
  534. if (error < 0) {
  535. /* We requested READDIRPLUS, but the server doesn't grok it */
  536. if (error == -ENOTSUPP && desc->plus) {
  537. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  538. desc->plus = arg.plus = false;
  539. goto again;
  540. }
  541. goto error;
  542. }
  543. desc->timestamp = timestamp;
  544. desc->gencount = gencount;
  545. error:
  546. return error;
  547. }
  548. static int xdr_decode(struct nfs_readdir_descriptor *desc,
  549. struct nfs_entry *entry, struct xdr_stream *xdr)
  550. {
  551. struct inode *inode = file_inode(desc->file);
  552. int error;
  553. error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
  554. if (error)
  555. return error;
  556. entry->fattr->time_start = desc->timestamp;
  557. entry->fattr->gencount = desc->gencount;
  558. return 0;
  559. }
  560. /* Match file and dirent using either filehandle or fileid
  561. * Note: caller is responsible for checking the fsid
  562. */
  563. static
  564. int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  565. {
  566. struct inode *inode;
  567. struct nfs_inode *nfsi;
  568. if (d_really_is_negative(dentry))
  569. return 0;
  570. inode = d_inode(dentry);
  571. if (is_bad_inode(inode) || NFS_STALE(inode))
  572. return 0;
  573. nfsi = NFS_I(inode);
  574. if (entry->fattr->fileid != nfsi->fileid)
  575. return 0;
  576. if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
  577. return 0;
  578. return 1;
  579. }
  580. #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL)
  581. static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx,
  582. unsigned int cache_hits,
  583. unsigned int cache_misses)
  584. {
  585. if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
  586. return false;
  587. if (ctx->pos == 0 ||
  588. cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD)
  589. return true;
  590. return false;
  591. }
  592. /*
  593. * This function is called by the getattr code to request the
  594. * use of readdirplus to accelerate any future lookups in the same
  595. * directory.
  596. */
  597. void nfs_readdir_record_entry_cache_hit(struct inode *dir)
  598. {
  599. struct nfs_inode *nfsi = NFS_I(dir);
  600. struct nfs_open_dir_context *ctx;
  601. if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
  602. S_ISDIR(dir->i_mode)) {
  603. rcu_read_lock();
  604. list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
  605. atomic_inc(&ctx->cache_hits);
  606. rcu_read_unlock();
  607. }
  608. }
  609. /*
  610. * This function is mainly for use by nfs_getattr().
  611. *
  612. * If this is an 'ls -l', we want to force use of readdirplus.
  613. */
  614. void nfs_readdir_record_entry_cache_miss(struct inode *dir)
  615. {
  616. struct nfs_inode *nfsi = NFS_I(dir);
  617. struct nfs_open_dir_context *ctx;
  618. if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
  619. S_ISDIR(dir->i_mode)) {
  620. rcu_read_lock();
  621. list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
  622. atomic_inc(&ctx->cache_misses);
  623. rcu_read_unlock();
  624. }
  625. }
  626. static void nfs_lookup_advise_force_readdirplus(struct inode *dir,
  627. unsigned int flags)
  628. {
  629. if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
  630. return;
  631. if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL))
  632. return;
  633. nfs_readdir_record_entry_cache_miss(dir);
  634. }
  635. static
  636. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
  637. unsigned long dir_verifier)
  638. {
  639. struct qstr filename = QSTR_INIT(entry->name, entry->len);
  640. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  641. struct dentry *dentry;
  642. struct dentry *alias;
  643. struct inode *inode;
  644. int status;
  645. if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
  646. return;
  647. if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
  648. return;
  649. if (filename.len == 0)
  650. return;
  651. /* Validate that the name doesn't contain any illegal '\0' */
  652. if (strnlen(filename.name, filename.len) != filename.len)
  653. return;
  654. /* ...or '/' */
  655. if (strnchr(filename.name, filename.len, '/'))
  656. return;
  657. if (filename.name[0] == '.') {
  658. if (filename.len == 1)
  659. return;
  660. if (filename.len == 2 && filename.name[1] == '.')
  661. return;
  662. }
  663. filename.hash = full_name_hash(parent, filename.name, filename.len);
  664. dentry = d_lookup(parent, &filename);
  665. again:
  666. if (!dentry) {
  667. dentry = d_alloc_parallel(parent, &filename, &wq);
  668. if (IS_ERR(dentry))
  669. return;
  670. }
  671. if (!d_in_lookup(dentry)) {
  672. /* Is there a mountpoint here? If so, just exit */
  673. if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
  674. &entry->fattr->fsid))
  675. goto out;
  676. if (nfs_same_file(dentry, entry)) {
  677. if (!entry->fh->size)
  678. goto out;
  679. nfs_set_verifier(dentry, dir_verifier);
  680. status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
  681. if (!status)
  682. nfs_setsecurity(d_inode(dentry), entry->fattr);
  683. trace_nfs_readdir_lookup_revalidate(d_inode(parent),
  684. dentry, 0, status);
  685. goto out;
  686. } else {
  687. trace_nfs_readdir_lookup_revalidate_failed(
  688. d_inode(parent), dentry, 0);
  689. d_invalidate(dentry);
  690. dput(dentry);
  691. dentry = NULL;
  692. goto again;
  693. }
  694. }
  695. if (!entry->fh->size) {
  696. d_lookup_done(dentry);
  697. goto out;
  698. }
  699. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  700. alias = d_splice_alias(inode, dentry);
  701. d_lookup_done(dentry);
  702. if (alias) {
  703. if (IS_ERR(alias))
  704. goto out;
  705. dput(dentry);
  706. dentry = alias;
  707. }
  708. nfs_set_verifier(dentry, dir_verifier);
  709. trace_nfs_readdir_lookup(d_inode(parent), dentry, 0);
  710. out:
  711. dput(dentry);
  712. }
  713. static int nfs_readdir_entry_decode(struct nfs_readdir_descriptor *desc,
  714. struct nfs_entry *entry,
  715. struct xdr_stream *stream)
  716. {
  717. int ret;
  718. if (entry->fattr->label)
  719. entry->fattr->label->len = NFS4_MAXLABELLEN;
  720. ret = xdr_decode(desc, entry, stream);
  721. if (ret || !desc->plus)
  722. return ret;
  723. nfs_prime_dcache(file_dentry(desc->file), entry, desc->dir_verifier);
  724. return 0;
  725. }
  726. /* Perform conversion from xdr to cache array */
  727. static int nfs_readdir_folio_filler(struct nfs_readdir_descriptor *desc,
  728. struct nfs_entry *entry,
  729. struct page **xdr_pages, unsigned int buflen,
  730. struct folio **arrays, size_t narrays,
  731. u64 change_attr)
  732. {
  733. struct address_space *mapping = desc->file->f_mapping;
  734. struct folio *new, *folio = *arrays;
  735. struct xdr_stream stream;
  736. struct page *scratch;
  737. struct xdr_buf buf;
  738. u64 cookie;
  739. int status;
  740. scratch = alloc_page(GFP_KERNEL);
  741. if (scratch == NULL)
  742. return -ENOMEM;
  743. xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
  744. xdr_set_scratch_page(&stream, scratch);
  745. do {
  746. status = nfs_readdir_entry_decode(desc, entry, &stream);
  747. if (status != 0)
  748. break;
  749. status = nfs_readdir_folio_array_append(folio, entry, &cookie);
  750. if (status != -ENOSPC)
  751. continue;
  752. if (folio->mapping != mapping) {
  753. if (!--narrays)
  754. break;
  755. new = nfs_readdir_folio_array_alloc(cookie, GFP_KERNEL);
  756. if (!new)
  757. break;
  758. arrays++;
  759. *arrays = folio = new;
  760. } else {
  761. new = nfs_readdir_folio_get_next(mapping, cookie,
  762. change_attr);
  763. if (!new)
  764. break;
  765. if (folio != *arrays)
  766. nfs_readdir_folio_unlock_and_put(folio);
  767. folio = new;
  768. }
  769. desc->folio_index_max++;
  770. status = nfs_readdir_folio_array_append(folio, entry, &cookie);
  771. } while (!status && !entry->eof);
  772. switch (status) {
  773. case -EBADCOOKIE:
  774. if (!entry->eof)
  775. break;
  776. nfs_readdir_folio_set_eof(folio);
  777. fallthrough;
  778. case -EAGAIN:
  779. status = 0;
  780. break;
  781. case -ENOSPC:
  782. status = 0;
  783. if (!desc->plus)
  784. break;
  785. while (!nfs_readdir_entry_decode(desc, entry, &stream))
  786. ;
  787. }
  788. if (folio != *arrays)
  789. nfs_readdir_folio_unlock_and_put(folio);
  790. put_page(scratch);
  791. return status;
  792. }
  793. static void nfs_readdir_free_pages(struct page **pages, size_t npages)
  794. {
  795. while (npages--)
  796. put_page(pages[npages]);
  797. kfree(pages);
  798. }
  799. /*
  800. * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
  801. * to nfs_readdir_free_pages()
  802. */
  803. static struct page **nfs_readdir_alloc_pages(size_t npages)
  804. {
  805. struct page **pages;
  806. size_t i;
  807. pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
  808. if (!pages)
  809. return NULL;
  810. for (i = 0; i < npages; i++) {
  811. struct page *page = alloc_page(GFP_KERNEL);
  812. if (page == NULL)
  813. goto out_freepages;
  814. pages[i] = page;
  815. }
  816. return pages;
  817. out_freepages:
  818. nfs_readdir_free_pages(pages, i);
  819. return NULL;
  820. }
  821. static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
  822. __be32 *verf_arg, __be32 *verf_res,
  823. struct folio **arrays, size_t narrays)
  824. {
  825. u64 change_attr;
  826. struct page **pages;
  827. struct folio *folio = *arrays;
  828. struct nfs_entry *entry;
  829. size_t array_size;
  830. struct inode *inode = file_inode(desc->file);
  831. unsigned int dtsize = desc->dtsize;
  832. unsigned int pglen;
  833. int status = -ENOMEM;
  834. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  835. if (!entry)
  836. return -ENOMEM;
  837. entry->cookie = nfs_readdir_folio_last_cookie(folio);
  838. entry->fh = nfs_alloc_fhandle();
  839. entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
  840. entry->server = NFS_SERVER(inode);
  841. if (entry->fh == NULL || entry->fattr == NULL)
  842. goto out;
  843. array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  844. pages = nfs_readdir_alloc_pages(array_size);
  845. if (!pages)
  846. goto out;
  847. change_attr = inode_peek_iversion_raw(inode);
  848. status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages,
  849. dtsize, verf_res);
  850. if (status < 0)
  851. goto free_pages;
  852. pglen = status;
  853. if (pglen != 0)
  854. status = nfs_readdir_folio_filler(desc, entry, pages, pglen,
  855. arrays, narrays, change_attr);
  856. else
  857. nfs_readdir_folio_set_eof(folio);
  858. desc->buffer_fills++;
  859. free_pages:
  860. nfs_readdir_free_pages(pages, array_size);
  861. out:
  862. nfs_free_fattr(entry->fattr);
  863. nfs_free_fhandle(entry->fh);
  864. kfree(entry);
  865. return status;
  866. }
  867. static void nfs_readdir_folio_put(struct nfs_readdir_descriptor *desc)
  868. {
  869. folio_put(desc->folio);
  870. desc->folio = NULL;
  871. }
  872. static void
  873. nfs_readdir_folio_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
  874. {
  875. folio_unlock(desc->folio);
  876. nfs_readdir_folio_put(desc);
  877. }
  878. static struct folio *
  879. nfs_readdir_folio_get_cached(struct nfs_readdir_descriptor *desc)
  880. {
  881. struct address_space *mapping = desc->file->f_mapping;
  882. u64 change_attr = inode_peek_iversion_raw(mapping->host);
  883. u64 cookie = desc->last_cookie;
  884. struct folio *folio;
  885. folio = nfs_readdir_folio_get_locked(mapping, cookie, change_attr);
  886. if (!folio)
  887. return NULL;
  888. if (desc->clear_cache && !nfs_readdir_folio_needs_filling(folio))
  889. nfs_readdir_folio_reinit_array(folio, cookie, change_attr);
  890. return folio;
  891. }
  892. /*
  893. * Returns 0 if desc->dir_cookie was found on page desc->page_index
  894. * and locks the page to prevent removal from the page cache.
  895. */
  896. static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
  897. {
  898. struct inode *inode = file_inode(desc->file);
  899. struct nfs_inode *nfsi = NFS_I(inode);
  900. __be32 verf[NFS_DIR_VERIFIER_SIZE];
  901. int res;
  902. desc->folio = nfs_readdir_folio_get_cached(desc);
  903. if (!desc->folio)
  904. return -ENOMEM;
  905. if (nfs_readdir_folio_needs_filling(desc->folio)) {
  906. /* Grow the dtsize if we had to go back for more pages */
  907. if (desc->folio_index == desc->folio_index_max)
  908. nfs_grow_dtsize(desc);
  909. desc->folio_index_max = desc->folio_index;
  910. trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf,
  911. desc->last_cookie,
  912. desc->folio->index, desc->dtsize);
  913. res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
  914. &desc->folio, 1);
  915. if (res < 0) {
  916. nfs_readdir_folio_unlock_and_put_cached(desc);
  917. trace_nfs_readdir_cache_fill_done(inode, res);
  918. if (res == -EBADCOOKIE || res == -ENOTSYNC) {
  919. invalidate_inode_pages2(desc->file->f_mapping);
  920. nfs_readdir_rewind_search(desc);
  921. trace_nfs_readdir_invalidate_cache_range(
  922. inode, 0, MAX_LFS_FILESIZE);
  923. return -EAGAIN;
  924. }
  925. return res;
  926. }
  927. /*
  928. * Set the cookie verifier if the page cache was empty
  929. */
  930. if (desc->last_cookie == 0 &&
  931. memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
  932. memcpy(nfsi->cookieverf, verf,
  933. sizeof(nfsi->cookieverf));
  934. invalidate_inode_pages2_range(desc->file->f_mapping, 1,
  935. -1);
  936. trace_nfs_readdir_invalidate_cache_range(
  937. inode, 1, MAX_LFS_FILESIZE);
  938. }
  939. desc->clear_cache = false;
  940. }
  941. res = nfs_readdir_search_array(desc);
  942. if (res == 0)
  943. return 0;
  944. nfs_readdir_folio_unlock_and_put_cached(desc);
  945. return res;
  946. }
  947. /* Search for desc->dir_cookie from the beginning of the page cache */
  948. static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
  949. {
  950. int res;
  951. do {
  952. res = find_and_lock_cache_page(desc);
  953. } while (res == -EAGAIN);
  954. return res;
  955. }
  956. #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL)
  957. /*
  958. * Once we've found the start of the dirent within a page: fill 'er up...
  959. */
  960. static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
  961. const __be32 *verf)
  962. {
  963. struct file *file = desc->file;
  964. struct nfs_cache_array *array;
  965. unsigned int i;
  966. bool first_emit = !desc->dir_cookie;
  967. array = kmap_local_folio(desc->folio, 0);
  968. for (i = desc->cache_entry_index; i < array->size; i++) {
  969. struct nfs_cache_array_entry *ent;
  970. /*
  971. * nfs_readdir_handle_cache_misses return force clear at
  972. * (cache_misses > NFS_READDIR_CACHE_MISS_THRESHOLD) for
  973. * readdir heuristic, NFS_READDIR_CACHE_MISS_THRESHOLD + 1
  974. * entries need be emitted here.
  975. */
  976. if (first_emit && i > NFS_READDIR_CACHE_MISS_THRESHOLD + 2) {
  977. desc->eob = true;
  978. break;
  979. }
  980. ent = &array->array[i];
  981. if (!dir_emit(desc->ctx, ent->name, ent->name_len,
  982. nfs_compat_user_ino64(ent->ino), ent->d_type)) {
  983. desc->eob = true;
  984. break;
  985. }
  986. memcpy(desc->verf, verf, sizeof(desc->verf));
  987. if (i == array->size - 1) {
  988. desc->dir_cookie = array->last_cookie;
  989. nfs_readdir_seek_next_array(array, desc);
  990. } else {
  991. desc->dir_cookie = array->array[i + 1].cookie;
  992. desc->last_cookie = array->array[0].cookie;
  993. }
  994. if (nfs_readdir_use_cookie(file))
  995. desc->ctx->pos = desc->dir_cookie;
  996. else
  997. desc->ctx->pos++;
  998. }
  999. if (array->folio_is_eof)
  1000. desc->eof = !desc->eob;
  1001. kunmap_local(array);
  1002. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
  1003. (unsigned long long)desc->dir_cookie);
  1004. }
  1005. /*
  1006. * If we cannot find a cookie in our cache, we suspect that this is
  1007. * because it points to a deleted file, so we ask the server to return
  1008. * whatever it thinks is the next entry. We then feed this to filldir.
  1009. * If all goes well, we should then be able to find our way round the
  1010. * cache on the next call to readdir_search_pagecache();
  1011. *
  1012. * NOTE: we cannot add the anonymous page to the pagecache because
  1013. * the data it contains might not be page aligned. Besides,
  1014. * we should already have a complete representation of the
  1015. * directory in the page cache by the time we get here.
  1016. */
  1017. static int uncached_readdir(struct nfs_readdir_descriptor *desc)
  1018. {
  1019. struct folio **arrays;
  1020. size_t i, sz = 512;
  1021. __be32 verf[NFS_DIR_VERIFIER_SIZE];
  1022. int status = -ENOMEM;
  1023. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
  1024. (unsigned long long)desc->dir_cookie);
  1025. arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
  1026. if (!arrays)
  1027. goto out;
  1028. arrays[0] = nfs_readdir_folio_array_alloc(desc->dir_cookie, GFP_KERNEL);
  1029. if (!arrays[0])
  1030. goto out;
  1031. desc->folio_index = 0;
  1032. desc->cache_entry_index = 0;
  1033. desc->last_cookie = desc->dir_cookie;
  1034. desc->folio_index_max = 0;
  1035. trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie,
  1036. -1, desc->dtsize);
  1037. status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
  1038. if (status < 0) {
  1039. trace_nfs_readdir_uncached_done(file_inode(desc->file), status);
  1040. goto out_free;
  1041. }
  1042. for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
  1043. desc->folio = arrays[i];
  1044. nfs_do_filldir(desc, verf);
  1045. }
  1046. desc->folio = NULL;
  1047. /*
  1048. * Grow the dtsize if we have to go back for more pages,
  1049. * or shrink it if we're reading too many.
  1050. */
  1051. if (!desc->eof) {
  1052. if (!desc->eob)
  1053. nfs_grow_dtsize(desc);
  1054. else if (desc->buffer_fills == 1 &&
  1055. i < (desc->folio_index_max >> 1))
  1056. nfs_shrink_dtsize(desc);
  1057. }
  1058. out_free:
  1059. for (i = 0; i < sz && arrays[i]; i++)
  1060. nfs_readdir_folio_array_free(arrays[i]);
  1061. out:
  1062. if (!nfs_readdir_use_cookie(desc->file))
  1063. nfs_readdir_rewind_search(desc);
  1064. desc->folio_index_max = -1;
  1065. kfree(arrays);
  1066. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
  1067. return status;
  1068. }
  1069. static bool nfs_readdir_handle_cache_misses(struct inode *inode,
  1070. struct nfs_readdir_descriptor *desc,
  1071. unsigned int cache_misses,
  1072. bool force_clear)
  1073. {
  1074. if (desc->ctx->pos == 0 || !desc->plus)
  1075. return false;
  1076. if (cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD && !force_clear)
  1077. return false;
  1078. trace_nfs_readdir_force_readdirplus(inode);
  1079. return true;
  1080. }
  1081. /* The file offset position represents the dirent entry number. A
  1082. last cookie cache takes care of the common case of reading the
  1083. whole directory.
  1084. */
  1085. static int nfs_readdir(struct file *file, struct dir_context *ctx)
  1086. {
  1087. struct dentry *dentry = file_dentry(file);
  1088. struct inode *inode = d_inode(dentry);
  1089. struct nfs_inode *nfsi = NFS_I(inode);
  1090. struct nfs_open_dir_context *dir_ctx = file->private_data;
  1091. struct nfs_readdir_descriptor *desc;
  1092. unsigned int cache_hits, cache_misses;
  1093. bool force_clear;
  1094. int res;
  1095. dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
  1096. file, (long long)ctx->pos);
  1097. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  1098. /*
  1099. * ctx->pos points to the dirent entry number.
  1100. * *desc->dir_cookie has the cookie for the next entry. We have
  1101. * to either find the entry with the appropriate number or
  1102. * revalidate the cookie.
  1103. */
  1104. nfs_revalidate_mapping(inode, file->f_mapping);
  1105. res = -ENOMEM;
  1106. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  1107. if (!desc)
  1108. goto out;
  1109. desc->file = file;
  1110. desc->ctx = ctx;
  1111. desc->folio_index_max = -1;
  1112. spin_lock(&file->f_lock);
  1113. desc->dir_cookie = dir_ctx->dir_cookie;
  1114. desc->folio_index = dir_ctx->page_index;
  1115. desc->last_cookie = dir_ctx->last_cookie;
  1116. desc->attr_gencount = dir_ctx->attr_gencount;
  1117. desc->eof = dir_ctx->eof;
  1118. nfs_set_dtsize(desc, dir_ctx->dtsize);
  1119. memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
  1120. cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0);
  1121. cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0);
  1122. force_clear = dir_ctx->force_clear;
  1123. spin_unlock(&file->f_lock);
  1124. if (desc->eof) {
  1125. res = 0;
  1126. goto out_free;
  1127. }
  1128. desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses);
  1129. force_clear = nfs_readdir_handle_cache_misses(inode, desc, cache_misses,
  1130. force_clear);
  1131. desc->clear_cache = force_clear;
  1132. do {
  1133. res = readdir_search_pagecache(desc);
  1134. if (res == -EBADCOOKIE) {
  1135. res = 0;
  1136. /* This means either end of directory */
  1137. if (desc->dir_cookie && !desc->eof) {
  1138. /* Or that the server has 'lost' a cookie */
  1139. res = uncached_readdir(desc);
  1140. if (res == 0)
  1141. continue;
  1142. if (res == -EBADCOOKIE || res == -ENOTSYNC)
  1143. res = 0;
  1144. }
  1145. break;
  1146. }
  1147. if (res == -ETOOSMALL && desc->plus) {
  1148. nfs_zap_caches(inode);
  1149. desc->plus = false;
  1150. desc->eof = false;
  1151. continue;
  1152. }
  1153. if (res < 0)
  1154. break;
  1155. nfs_do_filldir(desc, nfsi->cookieverf);
  1156. nfs_readdir_folio_unlock_and_put_cached(desc);
  1157. if (desc->folio_index == desc->folio_index_max)
  1158. desc->clear_cache = force_clear;
  1159. } while (!desc->eob && !desc->eof);
  1160. spin_lock(&file->f_lock);
  1161. dir_ctx->dir_cookie = desc->dir_cookie;
  1162. dir_ctx->last_cookie = desc->last_cookie;
  1163. dir_ctx->attr_gencount = desc->attr_gencount;
  1164. dir_ctx->page_index = desc->folio_index;
  1165. dir_ctx->force_clear = force_clear;
  1166. dir_ctx->eof = desc->eof;
  1167. dir_ctx->dtsize = desc->dtsize;
  1168. memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
  1169. spin_unlock(&file->f_lock);
  1170. out_free:
  1171. kfree(desc);
  1172. out:
  1173. dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
  1174. return res;
  1175. }
  1176. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
  1177. {
  1178. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  1179. dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
  1180. filp, offset, whence);
  1181. switch (whence) {
  1182. default:
  1183. return -EINVAL;
  1184. case SEEK_SET:
  1185. if (offset < 0)
  1186. return -EINVAL;
  1187. spin_lock(&filp->f_lock);
  1188. break;
  1189. case SEEK_CUR:
  1190. if (offset == 0)
  1191. return filp->f_pos;
  1192. spin_lock(&filp->f_lock);
  1193. offset += filp->f_pos;
  1194. if (offset < 0) {
  1195. spin_unlock(&filp->f_lock);
  1196. return -EINVAL;
  1197. }
  1198. }
  1199. if (offset != filp->f_pos) {
  1200. filp->f_pos = offset;
  1201. dir_ctx->page_index = 0;
  1202. if (!nfs_readdir_use_cookie(filp)) {
  1203. dir_ctx->dir_cookie = 0;
  1204. dir_ctx->last_cookie = 0;
  1205. } else {
  1206. dir_ctx->dir_cookie = offset;
  1207. dir_ctx->last_cookie = offset;
  1208. }
  1209. dir_ctx->eof = false;
  1210. }
  1211. spin_unlock(&filp->f_lock);
  1212. return offset;
  1213. }
  1214. /*
  1215. * All directory operations under NFS are synchronous, so fsync()
  1216. * is a dummy operation.
  1217. */
  1218. static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
  1219. int datasync)
  1220. {
  1221. dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
  1222. nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
  1223. return 0;
  1224. }
  1225. /**
  1226. * nfs_force_lookup_revalidate - Mark the directory as having changed
  1227. * @dir: pointer to directory inode
  1228. *
  1229. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  1230. * full lookup on all child dentries of 'dir' whenever a change occurs
  1231. * on the server that might have invalidated our dcache.
  1232. *
  1233. * Note that we reserve bit '0' as a tag to let us know when a dentry
  1234. * was revalidated while holding a delegation on its inode.
  1235. *
  1236. * The caller should be holding dir->i_lock
  1237. */
  1238. void nfs_force_lookup_revalidate(struct inode *dir)
  1239. {
  1240. NFS_I(dir)->cache_change_attribute += 2;
  1241. }
  1242. EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
  1243. /**
  1244. * nfs_verify_change_attribute - Detects NFS remote directory changes
  1245. * @dir: pointer to parent directory inode
  1246. * @verf: previously saved change attribute
  1247. *
  1248. * Return "false" if the verifiers doesn't match the change attribute.
  1249. * This would usually indicate that the directory contents have changed on
  1250. * the server, and that any dentries need revalidating.
  1251. */
  1252. static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
  1253. {
  1254. return (verf & ~1UL) == nfs_save_change_attribute(dir);
  1255. }
  1256. static void nfs_set_verifier_delegated(unsigned long *verf)
  1257. {
  1258. *verf |= 1UL;
  1259. }
  1260. #if IS_ENABLED(CONFIG_NFS_V4)
  1261. static void nfs_unset_verifier_delegated(unsigned long *verf)
  1262. {
  1263. *verf &= ~1UL;
  1264. }
  1265. #endif /* IS_ENABLED(CONFIG_NFS_V4) */
  1266. static bool nfs_test_verifier_delegated(unsigned long verf)
  1267. {
  1268. return verf & 1;
  1269. }
  1270. static bool nfs_verifier_is_delegated(struct dentry *dentry)
  1271. {
  1272. return nfs_test_verifier_delegated(dentry->d_time);
  1273. }
  1274. static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
  1275. {
  1276. struct inode *inode = d_inode(dentry);
  1277. struct inode *dir = d_inode_rcu(dentry->d_parent);
  1278. if (!dir || !nfs_verify_change_attribute(dir, verf))
  1279. return;
  1280. if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ, 0))
  1281. nfs_set_verifier_delegated(&verf);
  1282. dentry->d_time = verf;
  1283. }
  1284. /**
  1285. * nfs_set_verifier - save a parent directory verifier in the dentry
  1286. * @dentry: pointer to dentry
  1287. * @verf: verifier to save
  1288. *
  1289. * Saves the parent directory verifier in @dentry. If the inode has
  1290. * a delegation, we also tag the dentry as having been revalidated
  1291. * while holding a delegation so that we know we don't have to
  1292. * look it up again after a directory change.
  1293. */
  1294. void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
  1295. {
  1296. spin_lock(&dentry->d_lock);
  1297. nfs_set_verifier_locked(dentry, verf);
  1298. spin_unlock(&dentry->d_lock);
  1299. }
  1300. EXPORT_SYMBOL_GPL(nfs_set_verifier);
  1301. #if IS_ENABLED(CONFIG_NFS_V4)
  1302. /**
  1303. * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
  1304. * @inode: pointer to inode
  1305. *
  1306. * Iterates through the dentries in the inode alias list and clears
  1307. * the tag used to indicate that the dentry has been revalidated
  1308. * while holding a delegation.
  1309. * This function is intended for use when the delegation is being
  1310. * returned or revoked.
  1311. */
  1312. void nfs_clear_verifier_delegated(struct inode *inode)
  1313. {
  1314. struct dentry *alias;
  1315. if (!inode)
  1316. return;
  1317. spin_lock(&inode->i_lock);
  1318. hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
  1319. spin_lock(&alias->d_lock);
  1320. nfs_unset_verifier_delegated(&alias->d_time);
  1321. spin_unlock(&alias->d_lock);
  1322. }
  1323. spin_unlock(&inode->i_lock);
  1324. }
  1325. EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
  1326. #endif /* IS_ENABLED(CONFIG_NFS_V4) */
  1327. static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
  1328. {
  1329. if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
  1330. d_really_is_negative(dentry))
  1331. return dentry->d_time == inode_peek_iversion_raw(dir);
  1332. return nfs_verify_change_attribute(dir, dentry->d_time);
  1333. }
  1334. /*
  1335. * A check for whether or not the parent directory has changed.
  1336. * In the case it has, we assume that the dentries are untrustworthy
  1337. * and may need to be looked up again.
  1338. * If rcu_walk prevents us from performing a full check, return 0.
  1339. */
  1340. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
  1341. int rcu_walk)
  1342. {
  1343. if (IS_ROOT(dentry))
  1344. return 1;
  1345. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  1346. return 0;
  1347. if (!nfs_dentry_verify_change(dir, dentry))
  1348. return 0;
  1349. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  1350. if (nfs_mapping_need_revalidate_inode(dir)) {
  1351. if (rcu_walk)
  1352. return 0;
  1353. if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  1354. return 0;
  1355. }
  1356. if (!nfs_dentry_verify_change(dir, dentry))
  1357. return 0;
  1358. return 1;
  1359. }
  1360. /*
  1361. * Use intent information to check whether or not we're going to do
  1362. * an O_EXCL create using this path component.
  1363. */
  1364. static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
  1365. {
  1366. if (NFS_PROTO(dir)->version == 2)
  1367. return 0;
  1368. return flags & LOOKUP_EXCL;
  1369. }
  1370. /*
  1371. * Inode and filehandle revalidation for lookups.
  1372. *
  1373. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  1374. * or if the intent information indicates that we're about to open this
  1375. * particular file and the "nocto" mount flag is not set.
  1376. *
  1377. */
  1378. static
  1379. int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
  1380. {
  1381. struct nfs_server *server = NFS_SERVER(inode);
  1382. int ret;
  1383. if (IS_AUTOMOUNT(inode))
  1384. return 0;
  1385. if (flags & LOOKUP_OPEN) {
  1386. switch (inode->i_mode & S_IFMT) {
  1387. case S_IFREG:
  1388. /* A NFSv4 OPEN will revalidate later */
  1389. if (server->caps & NFS_CAP_ATOMIC_OPEN)
  1390. goto out;
  1391. fallthrough;
  1392. case S_IFDIR:
  1393. if (server->flags & NFS_MOUNT_NOCTO)
  1394. break;
  1395. /* NFS close-to-open cache consistency validation */
  1396. goto out_force;
  1397. }
  1398. }
  1399. /* VFS wants an on-the-wire revalidation */
  1400. if (flags & LOOKUP_REVAL)
  1401. goto out_force;
  1402. out:
  1403. if (inode->i_nlink > 0 ||
  1404. (inode->i_nlink == 0 &&
  1405. test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
  1406. return 0;
  1407. else
  1408. return -ESTALE;
  1409. out_force:
  1410. if (flags & LOOKUP_RCU)
  1411. return -ECHILD;
  1412. ret = __nfs_revalidate_inode(server, inode);
  1413. if (ret != 0)
  1414. return ret;
  1415. goto out;
  1416. }
  1417. static void nfs_mark_dir_for_revalidate(struct inode *inode)
  1418. {
  1419. spin_lock(&inode->i_lock);
  1420. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
  1421. spin_unlock(&inode->i_lock);
  1422. }
  1423. /*
  1424. * We judge how long we want to trust negative
  1425. * dentries by looking at the parent inode mtime.
  1426. *
  1427. * If parent mtime has changed, we revalidate, else we wait for a
  1428. * period corresponding to the parent's attribute cache timeout value.
  1429. *
  1430. * If LOOKUP_RCU prevents us from performing a full check, return 1
  1431. * suggesting a reval is needed.
  1432. *
  1433. * Note that when creating a new file, or looking up a rename target,
  1434. * then it shouldn't be necessary to revalidate a negative dentry.
  1435. */
  1436. static inline
  1437. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  1438. unsigned int flags)
  1439. {
  1440. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  1441. return 0;
  1442. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  1443. return 1;
  1444. /* Case insensitive server? Revalidate negative dentries */
  1445. if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
  1446. return 1;
  1447. return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
  1448. }
  1449. static int
  1450. nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
  1451. struct inode *inode, int error)
  1452. {
  1453. switch (error) {
  1454. case 1:
  1455. break;
  1456. case -ETIMEDOUT:
  1457. if (inode && (IS_ROOT(dentry) ||
  1458. NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL))
  1459. error = 1;
  1460. break;
  1461. case -ESTALE:
  1462. case -ENOENT:
  1463. error = 0;
  1464. fallthrough;
  1465. default:
  1466. /*
  1467. * We can't d_drop the root of a disconnected tree:
  1468. * its d_hash is on the s_anon list and d_drop() would hide
  1469. * it from shrink_dcache_for_unmount(), leading to busy
  1470. * inodes on unmount and further oopses.
  1471. */
  1472. if (inode && IS_ROOT(dentry))
  1473. error = 1;
  1474. break;
  1475. }
  1476. trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
  1477. return error;
  1478. }
  1479. static int
  1480. nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
  1481. unsigned int flags)
  1482. {
  1483. int ret = 1;
  1484. if (nfs_neg_need_reval(dir, dentry, flags)) {
  1485. if (flags & LOOKUP_RCU)
  1486. return -ECHILD;
  1487. ret = 0;
  1488. }
  1489. return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
  1490. }
  1491. static int
  1492. nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
  1493. struct inode *inode)
  1494. {
  1495. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1496. return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
  1497. }
  1498. static int nfs_lookup_revalidate_dentry(struct inode *dir,
  1499. struct dentry *dentry,
  1500. struct inode *inode, unsigned int flags)
  1501. {
  1502. struct nfs_fh *fhandle;
  1503. struct nfs_fattr *fattr;
  1504. unsigned long dir_verifier;
  1505. int ret;
  1506. trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
  1507. ret = -ENOMEM;
  1508. fhandle = nfs_alloc_fhandle();
  1509. fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
  1510. if (fhandle == NULL || fattr == NULL)
  1511. goto out;
  1512. dir_verifier = nfs_save_change_attribute(dir);
  1513. ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
  1514. if (ret < 0)
  1515. goto out;
  1516. /* Request help from readdirplus */
  1517. nfs_lookup_advise_force_readdirplus(dir, flags);
  1518. ret = 0;
  1519. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  1520. goto out;
  1521. if (nfs_refresh_inode(inode, fattr) < 0)
  1522. goto out;
  1523. nfs_setsecurity(inode, fattr);
  1524. nfs_set_verifier(dentry, dir_verifier);
  1525. ret = 1;
  1526. out:
  1527. nfs_free_fattr(fattr);
  1528. nfs_free_fhandle(fhandle);
  1529. /*
  1530. * If the lookup failed despite the dentry change attribute being
  1531. * a match, then we should revalidate the directory cache.
  1532. */
  1533. if (!ret && nfs_dentry_verify_change(dir, dentry))
  1534. nfs_mark_dir_for_revalidate(dir);
  1535. return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
  1536. }
  1537. /*
  1538. * This is called every time the dcache has a lookup hit,
  1539. * and we should check whether we can really trust that
  1540. * lookup.
  1541. *
  1542. * NOTE! The hit can be a negative hit too, don't assume
  1543. * we have an inode!
  1544. *
  1545. * If the parent directory is seen to have changed, we throw out the
  1546. * cached dentry and do a new lookup.
  1547. */
  1548. static int
  1549. nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
  1550. unsigned int flags)
  1551. {
  1552. struct inode *inode;
  1553. int error = 0;
  1554. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  1555. inode = d_inode(dentry);
  1556. if (!inode)
  1557. return nfs_lookup_revalidate_negative(dir, dentry, flags);
  1558. if (is_bad_inode(inode)) {
  1559. dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
  1560. __func__, dentry);
  1561. goto out_bad;
  1562. }
  1563. if ((flags & LOOKUP_RENAME_TARGET) && d_count(dentry) < 2 &&
  1564. nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
  1565. goto out_bad;
  1566. if (nfs_verifier_is_delegated(dentry))
  1567. return nfs_lookup_revalidate_delegated(dir, dentry, inode);
  1568. /* Force a full look up iff the parent directory has changed */
  1569. if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
  1570. nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
  1571. error = nfs_lookup_verify_inode(inode, flags);
  1572. if (error) {
  1573. if (error == -ESTALE)
  1574. nfs_mark_dir_for_revalidate(dir);
  1575. goto out_bad;
  1576. }
  1577. goto out_valid;
  1578. }
  1579. if (flags & LOOKUP_RCU)
  1580. return -ECHILD;
  1581. if (NFS_STALE(inode))
  1582. goto out_bad;
  1583. return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
  1584. out_valid:
  1585. return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
  1586. out_bad:
  1587. if (flags & LOOKUP_RCU)
  1588. return -ECHILD;
  1589. return nfs_lookup_revalidate_done(dir, dentry, inode, error);
  1590. }
  1591. static int
  1592. __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
  1593. int (*reval)(struct inode *, struct dentry *, unsigned int))
  1594. {
  1595. struct dentry *parent;
  1596. struct inode *dir;
  1597. int ret;
  1598. if (flags & LOOKUP_RCU) {
  1599. if (dentry->d_fsdata == NFS_FSDATA_BLOCKED)
  1600. return -ECHILD;
  1601. parent = READ_ONCE(dentry->d_parent);
  1602. dir = d_inode_rcu(parent);
  1603. if (!dir)
  1604. return -ECHILD;
  1605. ret = reval(dir, dentry, flags);
  1606. if (parent != READ_ONCE(dentry->d_parent))
  1607. return -ECHILD;
  1608. } else {
  1609. /* Wait for unlink to complete - see unblock_revalidate() */
  1610. wait_var_event(&dentry->d_fsdata,
  1611. smp_load_acquire(&dentry->d_fsdata)
  1612. != NFS_FSDATA_BLOCKED);
  1613. parent = dget_parent(dentry);
  1614. ret = reval(d_inode(parent), dentry, flags);
  1615. dput(parent);
  1616. }
  1617. return ret;
  1618. }
  1619. static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
  1620. {
  1621. return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
  1622. }
  1623. static void block_revalidate(struct dentry *dentry)
  1624. {
  1625. /* old devname - just in case */
  1626. kfree(dentry->d_fsdata);
  1627. /* Any new reference that could lead to an open
  1628. * will take ->d_lock in lookup_open() -> d_lookup().
  1629. * Holding this lock ensures we cannot race with
  1630. * __nfs_lookup_revalidate() and removes and need
  1631. * for further barriers.
  1632. */
  1633. lockdep_assert_held(&dentry->d_lock);
  1634. dentry->d_fsdata = NFS_FSDATA_BLOCKED;
  1635. }
  1636. static void unblock_revalidate(struct dentry *dentry)
  1637. {
  1638. store_release_wake_up(&dentry->d_fsdata, NULL);
  1639. }
  1640. /*
  1641. * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
  1642. * when we don't really care about the dentry name. This is called when a
  1643. * pathwalk ends on a dentry that was not found via a normal lookup in the
  1644. * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
  1645. *
  1646. * In this situation, we just want to verify that the inode itself is OK
  1647. * since the dentry might have changed on the server.
  1648. */
  1649. static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
  1650. {
  1651. struct inode *inode = d_inode(dentry);
  1652. int error = 0;
  1653. /*
  1654. * I believe we can only get a negative dentry here in the case of a
  1655. * procfs-style symlink. Just assume it's correct for now, but we may
  1656. * eventually need to do something more here.
  1657. */
  1658. if (!inode) {
  1659. dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
  1660. __func__, dentry);
  1661. return 1;
  1662. }
  1663. if (is_bad_inode(inode)) {
  1664. dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
  1665. __func__, dentry);
  1666. return 0;
  1667. }
  1668. error = nfs_lookup_verify_inode(inode, flags);
  1669. dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
  1670. __func__, inode->i_ino, error ? "invalid" : "valid");
  1671. return !error;
  1672. }
  1673. /*
  1674. * This is called from dput() when d_count is going to 0.
  1675. */
  1676. static int nfs_dentry_delete(const struct dentry *dentry)
  1677. {
  1678. dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
  1679. dentry, dentry->d_flags);
  1680. /* Unhash any dentry with a stale inode */
  1681. if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
  1682. return 1;
  1683. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1684. /* Unhash it, so that ->d_iput() would be called */
  1685. return 1;
  1686. }
  1687. if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
  1688. /* Unhash it, so that ancestors of killed async unlink
  1689. * files will be cleaned up during umount */
  1690. return 1;
  1691. }
  1692. return 0;
  1693. }
  1694. /* Ensure that we revalidate inode->i_nlink */
  1695. static void nfs_drop_nlink(struct inode *inode)
  1696. {
  1697. spin_lock(&inode->i_lock);
  1698. /* drop the inode if we're reasonably sure this is the last link */
  1699. if (inode->i_nlink > 0)
  1700. drop_nlink(inode);
  1701. NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
  1702. nfs_set_cache_invalid(
  1703. inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
  1704. NFS_INO_INVALID_NLINK);
  1705. spin_unlock(&inode->i_lock);
  1706. }
  1707. /*
  1708. * Called when the dentry loses inode.
  1709. * We use it to clean up silly-renamed files.
  1710. */
  1711. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  1712. {
  1713. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1714. nfs_complete_unlink(dentry, inode);
  1715. nfs_drop_nlink(inode);
  1716. }
  1717. iput(inode);
  1718. }
  1719. static void nfs_d_release(struct dentry *dentry)
  1720. {
  1721. /* free cached devname value, if it survived that far */
  1722. if (unlikely(dentry->d_fsdata)) {
  1723. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1724. WARN_ON(1);
  1725. else
  1726. kfree(dentry->d_fsdata);
  1727. }
  1728. }
  1729. const struct dentry_operations nfs_dentry_operations = {
  1730. .d_revalidate = nfs_lookup_revalidate,
  1731. .d_weak_revalidate = nfs_weak_revalidate,
  1732. .d_delete = nfs_dentry_delete,
  1733. .d_iput = nfs_dentry_iput,
  1734. .d_automount = nfs_d_automount,
  1735. .d_release = nfs_d_release,
  1736. };
  1737. EXPORT_SYMBOL_GPL(nfs_dentry_operations);
  1738. struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
  1739. {
  1740. struct dentry *res;
  1741. struct inode *inode = NULL;
  1742. struct nfs_fh *fhandle = NULL;
  1743. struct nfs_fattr *fattr = NULL;
  1744. unsigned long dir_verifier;
  1745. int error;
  1746. dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
  1747. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  1748. if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
  1749. return ERR_PTR(-ENAMETOOLONG);
  1750. /*
  1751. * If we're doing an exclusive create, optimize away the lookup
  1752. * but don't hash the dentry.
  1753. */
  1754. if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
  1755. return NULL;
  1756. res = ERR_PTR(-ENOMEM);
  1757. fhandle = nfs_alloc_fhandle();
  1758. fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
  1759. if (fhandle == NULL || fattr == NULL)
  1760. goto out;
  1761. dir_verifier = nfs_save_change_attribute(dir);
  1762. trace_nfs_lookup_enter(dir, dentry, flags);
  1763. error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
  1764. if (error == -ENOENT) {
  1765. if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
  1766. dir_verifier = inode_peek_iversion_raw(dir);
  1767. goto no_entry;
  1768. }
  1769. if (error < 0) {
  1770. res = ERR_PTR(error);
  1771. goto out;
  1772. }
  1773. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1774. res = ERR_CAST(inode);
  1775. if (IS_ERR(res))
  1776. goto out;
  1777. /* Notify readdir to use READDIRPLUS */
  1778. nfs_lookup_advise_force_readdirplus(dir, flags);
  1779. no_entry:
  1780. res = d_splice_alias(inode, dentry);
  1781. if (res != NULL) {
  1782. if (IS_ERR(res))
  1783. goto out;
  1784. dentry = res;
  1785. }
  1786. nfs_set_verifier(dentry, dir_verifier);
  1787. out:
  1788. trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
  1789. nfs_free_fattr(fattr);
  1790. nfs_free_fhandle(fhandle);
  1791. return res;
  1792. }
  1793. EXPORT_SYMBOL_GPL(nfs_lookup);
  1794. void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
  1795. {
  1796. /* Case insensitive server? Revalidate dentries */
  1797. if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
  1798. d_prune_aliases(inode);
  1799. }
  1800. EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
  1801. #if IS_ENABLED(CONFIG_NFS_V4)
  1802. static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
  1803. const struct dentry_operations nfs4_dentry_operations = {
  1804. .d_revalidate = nfs4_lookup_revalidate,
  1805. .d_weak_revalidate = nfs_weak_revalidate,
  1806. .d_delete = nfs_dentry_delete,
  1807. .d_iput = nfs_dentry_iput,
  1808. .d_automount = nfs_d_automount,
  1809. .d_release = nfs_d_release,
  1810. };
  1811. EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
  1812. static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
  1813. {
  1814. return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
  1815. }
  1816. static int do_open(struct inode *inode, struct file *filp)
  1817. {
  1818. nfs_fscache_open_file(inode, filp);
  1819. return 0;
  1820. }
  1821. static int nfs_finish_open(struct nfs_open_context *ctx,
  1822. struct dentry *dentry,
  1823. struct file *file, unsigned open_flags)
  1824. {
  1825. int err;
  1826. err = finish_open(file, dentry, do_open);
  1827. if (err)
  1828. goto out;
  1829. if (S_ISREG(file_inode(file)->i_mode))
  1830. nfs_file_set_open_context(file, ctx);
  1831. else
  1832. err = -EOPENSTALE;
  1833. out:
  1834. return err;
  1835. }
  1836. int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
  1837. struct file *file, unsigned open_flags,
  1838. umode_t mode)
  1839. {
  1840. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  1841. struct nfs_open_context *ctx;
  1842. struct dentry *res;
  1843. struct iattr attr = { .ia_valid = ATTR_OPEN };
  1844. struct inode *inode;
  1845. unsigned int lookup_flags = 0;
  1846. unsigned long dir_verifier;
  1847. bool switched = false;
  1848. int created = 0;
  1849. int err;
  1850. /* Expect a negative dentry */
  1851. BUG_ON(d_inode(dentry));
  1852. dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
  1853. dir->i_sb->s_id, dir->i_ino, dentry);
  1854. err = nfs_check_flags(open_flags);
  1855. if (err)
  1856. return err;
  1857. /* NFS only supports OPEN on regular files */
  1858. if ((open_flags & O_DIRECTORY)) {
  1859. if (!d_in_lookup(dentry)) {
  1860. /*
  1861. * Hashed negative dentry with O_DIRECTORY: dentry was
  1862. * revalidated and is fine, no need to perform lookup
  1863. * again
  1864. */
  1865. return -ENOENT;
  1866. }
  1867. lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
  1868. goto no_open;
  1869. }
  1870. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1871. return -ENAMETOOLONG;
  1872. if (open_flags & O_CREAT) {
  1873. struct nfs_server *server = NFS_SERVER(dir);
  1874. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  1875. mode &= ~current_umask();
  1876. attr.ia_valid |= ATTR_MODE;
  1877. attr.ia_mode = mode;
  1878. }
  1879. if (open_flags & O_TRUNC) {
  1880. attr.ia_valid |= ATTR_SIZE;
  1881. attr.ia_size = 0;
  1882. }
  1883. if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
  1884. d_drop(dentry);
  1885. switched = true;
  1886. dentry = d_alloc_parallel(dentry->d_parent,
  1887. &dentry->d_name, &wq);
  1888. if (IS_ERR(dentry))
  1889. return PTR_ERR(dentry);
  1890. if (unlikely(!d_in_lookup(dentry)))
  1891. return finish_no_open(file, dentry);
  1892. }
  1893. ctx = create_nfs_open_context(dentry, open_flags, file);
  1894. err = PTR_ERR(ctx);
  1895. if (IS_ERR(ctx))
  1896. goto out;
  1897. trace_nfs_atomic_open_enter(dir, ctx, open_flags);
  1898. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
  1899. if (created)
  1900. file->f_mode |= FMODE_CREATED;
  1901. if (IS_ERR(inode)) {
  1902. err = PTR_ERR(inode);
  1903. trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
  1904. put_nfs_open_context(ctx);
  1905. d_drop(dentry);
  1906. switch (err) {
  1907. case -ENOENT:
  1908. d_splice_alias(NULL, dentry);
  1909. if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
  1910. dir_verifier = inode_peek_iversion_raw(dir);
  1911. else
  1912. dir_verifier = nfs_save_change_attribute(dir);
  1913. nfs_set_verifier(dentry, dir_verifier);
  1914. break;
  1915. case -EISDIR:
  1916. case -ENOTDIR:
  1917. goto no_open;
  1918. case -ELOOP:
  1919. if (!(open_flags & O_NOFOLLOW))
  1920. goto no_open;
  1921. break;
  1922. /* case -EINVAL: */
  1923. default:
  1924. break;
  1925. }
  1926. goto out;
  1927. }
  1928. file->f_mode |= FMODE_CAN_ODIRECT;
  1929. err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
  1930. trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
  1931. put_nfs_open_context(ctx);
  1932. out:
  1933. if (unlikely(switched)) {
  1934. d_lookup_done(dentry);
  1935. dput(dentry);
  1936. }
  1937. return err;
  1938. no_open:
  1939. res = nfs_lookup(dir, dentry, lookup_flags);
  1940. if (!res) {
  1941. inode = d_inode(dentry);
  1942. if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
  1943. !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
  1944. res = ERR_PTR(-ENOTDIR);
  1945. else if (inode && S_ISREG(inode->i_mode))
  1946. res = ERR_PTR(-EOPENSTALE);
  1947. } else if (!IS_ERR(res)) {
  1948. inode = d_inode(res);
  1949. if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
  1950. !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
  1951. dput(res);
  1952. res = ERR_PTR(-ENOTDIR);
  1953. } else if (inode && S_ISREG(inode->i_mode)) {
  1954. dput(res);
  1955. res = ERR_PTR(-EOPENSTALE);
  1956. }
  1957. }
  1958. if (switched) {
  1959. d_lookup_done(dentry);
  1960. if (!res)
  1961. res = dentry;
  1962. else
  1963. dput(dentry);
  1964. }
  1965. if (IS_ERR(res))
  1966. return PTR_ERR(res);
  1967. return finish_no_open(file, res);
  1968. }
  1969. EXPORT_SYMBOL_GPL(nfs_atomic_open);
  1970. static int
  1971. nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
  1972. unsigned int flags)
  1973. {
  1974. struct inode *inode;
  1975. trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
  1976. if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
  1977. goto full_reval;
  1978. if (d_mountpoint(dentry))
  1979. goto full_reval;
  1980. inode = d_inode(dentry);
  1981. /* We can't create new files in nfs_open_revalidate(), so we
  1982. * optimize away revalidation of negative dentries.
  1983. */
  1984. if (inode == NULL)
  1985. goto full_reval;
  1986. if (nfs_verifier_is_delegated(dentry))
  1987. return nfs_lookup_revalidate_delegated(dir, dentry, inode);
  1988. /* NFS only supports OPEN on regular files */
  1989. if (!S_ISREG(inode->i_mode))
  1990. goto full_reval;
  1991. /* We cannot do exclusive creation on a positive dentry */
  1992. if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
  1993. goto reval_dentry;
  1994. /* Check if the directory changed */
  1995. if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
  1996. goto reval_dentry;
  1997. /* Let f_op->open() actually open (and revalidate) the file */
  1998. return 1;
  1999. reval_dentry:
  2000. if (flags & LOOKUP_RCU)
  2001. return -ECHILD;
  2002. return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
  2003. full_reval:
  2004. return nfs_do_lookup_revalidate(dir, dentry, flags);
  2005. }
  2006. static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
  2007. {
  2008. return __nfs_lookup_revalidate(dentry, flags,
  2009. nfs4_do_lookup_revalidate);
  2010. }
  2011. #endif /* CONFIG_NFSV4 */
  2012. int nfs_atomic_open_v23(struct inode *dir, struct dentry *dentry,
  2013. struct file *file, unsigned int open_flags,
  2014. umode_t mode)
  2015. {
  2016. /* Same as look+open from lookup_open(), but with different O_TRUNC
  2017. * handling.
  2018. */
  2019. int error = 0;
  2020. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  2021. return -ENAMETOOLONG;
  2022. if (open_flags & O_CREAT) {
  2023. file->f_mode |= FMODE_CREATED;
  2024. error = nfs_do_create(dir, dentry, mode, open_flags);
  2025. if (error)
  2026. return error;
  2027. return finish_open(file, dentry, NULL);
  2028. } else if (d_in_lookup(dentry)) {
  2029. /* The only flags nfs_lookup considers are
  2030. * LOOKUP_EXCL and LOOKUP_RENAME_TARGET, and
  2031. * we want those to be zero so the lookup isn't skipped.
  2032. */
  2033. struct dentry *res = nfs_lookup(dir, dentry, 0);
  2034. d_lookup_done(dentry);
  2035. if (unlikely(res)) {
  2036. if (IS_ERR(res))
  2037. return PTR_ERR(res);
  2038. return finish_no_open(file, res);
  2039. }
  2040. }
  2041. return finish_no_open(file, NULL);
  2042. }
  2043. EXPORT_SYMBOL_GPL(nfs_atomic_open_v23);
  2044. struct dentry *
  2045. nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
  2046. struct nfs_fattr *fattr)
  2047. {
  2048. struct dentry *parent = dget_parent(dentry);
  2049. struct inode *dir = d_inode(parent);
  2050. struct inode *inode;
  2051. struct dentry *d;
  2052. int error;
  2053. d_drop(dentry);
  2054. if (fhandle->size == 0) {
  2055. error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
  2056. if (error)
  2057. goto out_error;
  2058. }
  2059. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2060. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  2061. struct nfs_server *server = NFS_SB(dentry->d_sb);
  2062. error = server->nfs_client->rpc_ops->getattr(server, fhandle,
  2063. fattr, NULL);
  2064. if (error < 0)
  2065. goto out_error;
  2066. }
  2067. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  2068. d = d_splice_alias(inode, dentry);
  2069. out:
  2070. dput(parent);
  2071. return d;
  2072. out_error:
  2073. d = ERR_PTR(error);
  2074. goto out;
  2075. }
  2076. EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
  2077. /*
  2078. * Code common to create, mkdir, and mknod.
  2079. */
  2080. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  2081. struct nfs_fattr *fattr)
  2082. {
  2083. struct dentry *d;
  2084. d = nfs_add_or_obtain(dentry, fhandle, fattr);
  2085. if (IS_ERR(d))
  2086. return PTR_ERR(d);
  2087. /* Callers don't care */
  2088. dput(d);
  2089. return 0;
  2090. }
  2091. EXPORT_SYMBOL_GPL(nfs_instantiate);
  2092. /*
  2093. * Following a failed create operation, we drop the dentry rather
  2094. * than retain a negative dentry. This avoids a problem in the event
  2095. * that the operation succeeded on the server, but an error in the
  2096. * reply path made it appear to have failed.
  2097. */
  2098. static int nfs_do_create(struct inode *dir, struct dentry *dentry,
  2099. umode_t mode, int open_flags)
  2100. {
  2101. struct iattr attr;
  2102. int error;
  2103. open_flags |= O_CREAT;
  2104. dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
  2105. dir->i_sb->s_id, dir->i_ino, dentry);
  2106. attr.ia_mode = mode;
  2107. attr.ia_valid = ATTR_MODE;
  2108. if (open_flags & O_TRUNC) {
  2109. attr.ia_size = 0;
  2110. attr.ia_valid |= ATTR_SIZE;
  2111. }
  2112. trace_nfs_create_enter(dir, dentry, open_flags);
  2113. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
  2114. trace_nfs_create_exit(dir, dentry, open_flags, error);
  2115. if (error != 0)
  2116. goto out_err;
  2117. return 0;
  2118. out_err:
  2119. d_drop(dentry);
  2120. return error;
  2121. }
  2122. int nfs_create(struct mnt_idmap *idmap, struct inode *dir,
  2123. struct dentry *dentry, umode_t mode, bool excl)
  2124. {
  2125. return nfs_do_create(dir, dentry, mode, excl ? O_EXCL : 0);
  2126. }
  2127. EXPORT_SYMBOL_GPL(nfs_create);
  2128. /*
  2129. * See comments for nfs_proc_create regarding failed operations.
  2130. */
  2131. int
  2132. nfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
  2133. struct dentry *dentry, umode_t mode, dev_t rdev)
  2134. {
  2135. struct iattr attr;
  2136. int status;
  2137. dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
  2138. dir->i_sb->s_id, dir->i_ino, dentry);
  2139. attr.ia_mode = mode;
  2140. attr.ia_valid = ATTR_MODE;
  2141. trace_nfs_mknod_enter(dir, dentry);
  2142. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  2143. trace_nfs_mknod_exit(dir, dentry, status);
  2144. if (status != 0)
  2145. goto out_err;
  2146. return 0;
  2147. out_err:
  2148. d_drop(dentry);
  2149. return status;
  2150. }
  2151. EXPORT_SYMBOL_GPL(nfs_mknod);
  2152. /*
  2153. * See comments for nfs_proc_create regarding failed operations.
  2154. */
  2155. int nfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  2156. struct dentry *dentry, umode_t mode)
  2157. {
  2158. struct iattr attr;
  2159. int error;
  2160. dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
  2161. dir->i_sb->s_id, dir->i_ino, dentry);
  2162. attr.ia_valid = ATTR_MODE;
  2163. attr.ia_mode = mode | S_IFDIR;
  2164. trace_nfs_mkdir_enter(dir, dentry);
  2165. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  2166. trace_nfs_mkdir_exit(dir, dentry, error);
  2167. if (error != 0)
  2168. goto out_err;
  2169. return 0;
  2170. out_err:
  2171. d_drop(dentry);
  2172. return error;
  2173. }
  2174. EXPORT_SYMBOL_GPL(nfs_mkdir);
  2175. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  2176. {
  2177. if (simple_positive(dentry))
  2178. d_delete(dentry);
  2179. }
  2180. static void nfs_dentry_remove_handle_error(struct inode *dir,
  2181. struct dentry *dentry, int error)
  2182. {
  2183. switch (error) {
  2184. case -ENOENT:
  2185. if (d_really_is_positive(dentry))
  2186. d_delete(dentry);
  2187. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2188. break;
  2189. case 0:
  2190. nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
  2191. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2192. }
  2193. }
  2194. int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  2195. {
  2196. int error;
  2197. dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
  2198. dir->i_sb->s_id, dir->i_ino, dentry);
  2199. trace_nfs_rmdir_enter(dir, dentry);
  2200. if (d_really_is_positive(dentry)) {
  2201. down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  2202. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  2203. /* Ensure the VFS deletes this inode */
  2204. switch (error) {
  2205. case 0:
  2206. clear_nlink(d_inode(dentry));
  2207. break;
  2208. case -ENOENT:
  2209. nfs_dentry_handle_enoent(dentry);
  2210. }
  2211. up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  2212. } else
  2213. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  2214. nfs_dentry_remove_handle_error(dir, dentry, error);
  2215. trace_nfs_rmdir_exit(dir, dentry, error);
  2216. return error;
  2217. }
  2218. EXPORT_SYMBOL_GPL(nfs_rmdir);
  2219. /*
  2220. * Remove a file after making sure there are no pending writes,
  2221. * and after checking that the file has only one user.
  2222. *
  2223. * We invalidate the attribute cache and free the inode prior to the operation
  2224. * to avoid possible races if the server reuses the inode.
  2225. */
  2226. static int nfs_safe_remove(struct dentry *dentry)
  2227. {
  2228. struct inode *dir = d_inode(dentry->d_parent);
  2229. struct inode *inode = d_inode(dentry);
  2230. int error = -EBUSY;
  2231. dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
  2232. /* If the dentry was sillyrenamed, we simply call d_delete() */
  2233. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  2234. error = 0;
  2235. goto out;
  2236. }
  2237. trace_nfs_remove_enter(dir, dentry);
  2238. if (inode != NULL) {
  2239. error = NFS_PROTO(dir)->remove(dir, dentry);
  2240. if (error == 0)
  2241. nfs_drop_nlink(inode);
  2242. } else
  2243. error = NFS_PROTO(dir)->remove(dir, dentry);
  2244. if (error == -ENOENT)
  2245. nfs_dentry_handle_enoent(dentry);
  2246. trace_nfs_remove_exit(dir, dentry, error);
  2247. out:
  2248. return error;
  2249. }
  2250. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  2251. * belongs to an active ".nfs..." file and we return -EBUSY.
  2252. *
  2253. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  2254. */
  2255. int nfs_unlink(struct inode *dir, struct dentry *dentry)
  2256. {
  2257. int error;
  2258. dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
  2259. dir->i_ino, dentry);
  2260. trace_nfs_unlink_enter(dir, dentry);
  2261. spin_lock(&dentry->d_lock);
  2262. if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
  2263. &NFS_I(d_inode(dentry))->flags)) {
  2264. spin_unlock(&dentry->d_lock);
  2265. /* Start asynchronous writeout of the inode */
  2266. write_inode_now(d_inode(dentry), 0);
  2267. error = nfs_sillyrename(dir, dentry);
  2268. goto out;
  2269. }
  2270. /* We must prevent any concurrent open until the unlink
  2271. * completes. ->d_revalidate will wait for ->d_fsdata
  2272. * to clear. We set it here to ensure no lookup succeeds until
  2273. * the unlink is complete on the server.
  2274. */
  2275. error = -ETXTBSY;
  2276. if (WARN_ON(dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
  2277. WARN_ON(dentry->d_fsdata == NFS_FSDATA_BLOCKED)) {
  2278. spin_unlock(&dentry->d_lock);
  2279. goto out;
  2280. }
  2281. block_revalidate(dentry);
  2282. spin_unlock(&dentry->d_lock);
  2283. error = nfs_safe_remove(dentry);
  2284. nfs_dentry_remove_handle_error(dir, dentry, error);
  2285. unblock_revalidate(dentry);
  2286. out:
  2287. trace_nfs_unlink_exit(dir, dentry, error);
  2288. return error;
  2289. }
  2290. EXPORT_SYMBOL_GPL(nfs_unlink);
  2291. /*
  2292. * To create a symbolic link, most file systems instantiate a new inode,
  2293. * add a page to it containing the path, then write it out to the disk
  2294. * using prepare_write/commit_write.
  2295. *
  2296. * Unfortunately the NFS client can't create the in-core inode first
  2297. * because it needs a file handle to create an in-core inode (see
  2298. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  2299. * symlink request has completed on the server.
  2300. *
  2301. * So instead we allocate a raw page, copy the symname into it, then do
  2302. * the SYMLINK request with the page as the buffer. If it succeeds, we
  2303. * now have a new file handle and can instantiate an in-core NFS inode
  2304. * and move the raw page into its mapping.
  2305. */
  2306. int nfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  2307. struct dentry *dentry, const char *symname)
  2308. {
  2309. struct folio *folio;
  2310. char *kaddr;
  2311. struct iattr attr;
  2312. unsigned int pathlen = strlen(symname);
  2313. int error;
  2314. dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
  2315. dir->i_ino, dentry, symname);
  2316. if (pathlen > PAGE_SIZE)
  2317. return -ENAMETOOLONG;
  2318. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  2319. attr.ia_valid = ATTR_MODE;
  2320. folio = folio_alloc(GFP_USER, 0);
  2321. if (!folio)
  2322. return -ENOMEM;
  2323. kaddr = folio_address(folio);
  2324. memcpy(kaddr, symname, pathlen);
  2325. if (pathlen < PAGE_SIZE)
  2326. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  2327. trace_nfs_symlink_enter(dir, dentry);
  2328. error = NFS_PROTO(dir)->symlink(dir, dentry, folio, pathlen, &attr);
  2329. trace_nfs_symlink_exit(dir, dentry, error);
  2330. if (error != 0) {
  2331. dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
  2332. dir->i_sb->s_id, dir->i_ino,
  2333. dentry, symname, error);
  2334. d_drop(dentry);
  2335. folio_put(folio);
  2336. return error;
  2337. }
  2338. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2339. /*
  2340. * No big deal if we can't add this page to the page cache here.
  2341. * READLINK will get the missing page from the server if needed.
  2342. */
  2343. if (filemap_add_folio(d_inode(dentry)->i_mapping, folio, 0,
  2344. GFP_KERNEL) == 0) {
  2345. folio_mark_uptodate(folio);
  2346. folio_unlock(folio);
  2347. }
  2348. folio_put(folio);
  2349. return 0;
  2350. }
  2351. EXPORT_SYMBOL_GPL(nfs_symlink);
  2352. int
  2353. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  2354. {
  2355. struct inode *inode = d_inode(old_dentry);
  2356. int error;
  2357. dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
  2358. old_dentry, dentry);
  2359. trace_nfs_link_enter(inode, dir, dentry);
  2360. d_drop(dentry);
  2361. if (S_ISREG(inode->i_mode))
  2362. nfs_sync_inode(inode);
  2363. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  2364. if (error == 0) {
  2365. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2366. ihold(inode);
  2367. d_add(dentry, inode);
  2368. }
  2369. trace_nfs_link_exit(inode, dir, dentry, error);
  2370. return error;
  2371. }
  2372. EXPORT_SYMBOL_GPL(nfs_link);
  2373. static void
  2374. nfs_unblock_rename(struct rpc_task *task, struct nfs_renamedata *data)
  2375. {
  2376. struct dentry *new_dentry = data->new_dentry;
  2377. unblock_revalidate(new_dentry);
  2378. }
  2379. static bool nfs_rename_is_unsafe_cross_dir(struct dentry *old_dentry,
  2380. struct dentry *new_dentry)
  2381. {
  2382. struct nfs_server *server = NFS_SB(old_dentry->d_sb);
  2383. if (old_dentry->d_parent != new_dentry->d_parent)
  2384. return false;
  2385. if (server->fh_expire_type & NFS_FH_RENAME_UNSAFE)
  2386. return !(server->fh_expire_type & NFS_FH_NOEXPIRE_WITH_OPEN);
  2387. return true;
  2388. }
  2389. /*
  2390. * RENAME
  2391. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  2392. * different file handle for the same inode after a rename (e.g. when
  2393. * moving to a different directory). A fail-safe method to do so would
  2394. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  2395. * rename the old file using the sillyrename stuff. This way, the original
  2396. * file in old_dir will go away when the last process iput()s the inode.
  2397. *
  2398. * FIXED.
  2399. *
  2400. * It actually works quite well. One needs to have the possibility for
  2401. * at least one ".nfs..." file in each directory the file ever gets
  2402. * moved or linked to which happens automagically with the new
  2403. * implementation that only depends on the dcache stuff instead of
  2404. * using the inode layer
  2405. *
  2406. * Unfortunately, things are a little more complicated than indicated
  2407. * above. For a cross-directory move, we want to make sure we can get
  2408. * rid of the old inode after the operation. This means there must be
  2409. * no pending writes (if it's a file), and the use count must be 1.
  2410. * If these conditions are met, we can drop the dentries before doing
  2411. * the rename.
  2412. */
  2413. int nfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
  2414. struct dentry *old_dentry, struct inode *new_dir,
  2415. struct dentry *new_dentry, unsigned int flags)
  2416. {
  2417. struct inode *old_inode = d_inode(old_dentry);
  2418. struct inode *new_inode = d_inode(new_dentry);
  2419. struct dentry *dentry = NULL;
  2420. struct rpc_task *task;
  2421. bool must_unblock = false;
  2422. int error = -EBUSY;
  2423. if (flags)
  2424. return -EINVAL;
  2425. dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
  2426. old_dentry, new_dentry,
  2427. d_count(new_dentry));
  2428. trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
  2429. /*
  2430. * For non-directories, check whether the target is busy and if so,
  2431. * make a copy of the dentry and then do a silly-rename. If the
  2432. * silly-rename succeeds, the copied dentry is hashed and becomes
  2433. * the new target.
  2434. */
  2435. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  2436. /* We must prevent any concurrent open until the unlink
  2437. * completes. ->d_revalidate will wait for ->d_fsdata
  2438. * to clear. We set it here to ensure no lookup succeeds until
  2439. * the unlink is complete on the server.
  2440. */
  2441. error = -ETXTBSY;
  2442. if (WARN_ON(new_dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
  2443. WARN_ON(new_dentry->d_fsdata == NFS_FSDATA_BLOCKED))
  2444. goto out;
  2445. spin_lock(&new_dentry->d_lock);
  2446. if (d_count(new_dentry) > 2) {
  2447. int err;
  2448. spin_unlock(&new_dentry->d_lock);
  2449. /* copy the target dentry's name */
  2450. dentry = d_alloc(new_dentry->d_parent,
  2451. &new_dentry->d_name);
  2452. if (!dentry)
  2453. goto out;
  2454. /* silly-rename the existing target ... */
  2455. err = nfs_sillyrename(new_dir, new_dentry);
  2456. if (err)
  2457. goto out;
  2458. new_dentry = dentry;
  2459. new_inode = NULL;
  2460. } else {
  2461. block_revalidate(new_dentry);
  2462. must_unblock = true;
  2463. spin_unlock(&new_dentry->d_lock);
  2464. }
  2465. }
  2466. if (S_ISREG(old_inode->i_mode) &&
  2467. nfs_rename_is_unsafe_cross_dir(old_dentry, new_dentry))
  2468. nfs_sync_inode(old_inode);
  2469. task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry,
  2470. must_unblock ? nfs_unblock_rename : NULL);
  2471. if (IS_ERR(task)) {
  2472. if (must_unblock)
  2473. unblock_revalidate(new_dentry);
  2474. error = PTR_ERR(task);
  2475. goto out;
  2476. }
  2477. error = rpc_wait_for_completion_task(task);
  2478. if (error != 0) {
  2479. ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
  2480. /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
  2481. smp_wmb();
  2482. } else
  2483. error = task->tk_status;
  2484. rpc_put_task(task);
  2485. /* Ensure the inode attributes are revalidated */
  2486. if (error == 0) {
  2487. spin_lock(&old_inode->i_lock);
  2488. NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
  2489. nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
  2490. NFS_INO_INVALID_CTIME |
  2491. NFS_INO_REVAL_FORCED);
  2492. spin_unlock(&old_inode->i_lock);
  2493. }
  2494. out:
  2495. trace_nfs_rename_exit(old_dir, old_dentry,
  2496. new_dir, new_dentry, error);
  2497. if (!error) {
  2498. if (new_inode != NULL)
  2499. nfs_drop_nlink(new_inode);
  2500. /*
  2501. * The d_move() should be here instead of in an async RPC completion
  2502. * handler because we need the proper locks to move the dentry. If
  2503. * we're interrupted by a signal, the async RPC completion handler
  2504. * should mark the directories for revalidation.
  2505. */
  2506. d_move(old_dentry, new_dentry);
  2507. nfs_set_verifier(old_dentry,
  2508. nfs_save_change_attribute(new_dir));
  2509. } else if (error == -ENOENT)
  2510. nfs_dentry_handle_enoent(old_dentry);
  2511. /* new dentry created? */
  2512. if (dentry)
  2513. dput(dentry);
  2514. return error;
  2515. }
  2516. EXPORT_SYMBOL_GPL(nfs_rename);
  2517. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  2518. static LIST_HEAD(nfs_access_lru_list);
  2519. static atomic_long_t nfs_access_nr_entries;
  2520. static unsigned long nfs_access_max_cachesize = 4*1024*1024;
  2521. module_param(nfs_access_max_cachesize, ulong, 0644);
  2522. MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
  2523. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  2524. {
  2525. put_group_info(entry->group_info);
  2526. kfree_rcu(entry, rcu_head);
  2527. smp_mb__before_atomic();
  2528. atomic_long_dec(&nfs_access_nr_entries);
  2529. smp_mb__after_atomic();
  2530. }
  2531. static void nfs_access_free_list(struct list_head *head)
  2532. {
  2533. struct nfs_access_entry *cache;
  2534. while (!list_empty(head)) {
  2535. cache = list_entry(head->next, struct nfs_access_entry, lru);
  2536. list_del(&cache->lru);
  2537. nfs_access_free_entry(cache);
  2538. }
  2539. }
  2540. static unsigned long
  2541. nfs_do_access_cache_scan(unsigned int nr_to_scan)
  2542. {
  2543. LIST_HEAD(head);
  2544. struct nfs_inode *nfsi, *next;
  2545. struct nfs_access_entry *cache;
  2546. long freed = 0;
  2547. spin_lock(&nfs_access_lru_lock);
  2548. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  2549. struct inode *inode;
  2550. if (nr_to_scan-- == 0)
  2551. break;
  2552. inode = &nfsi->vfs_inode;
  2553. spin_lock(&inode->i_lock);
  2554. if (list_empty(&nfsi->access_cache_entry_lru))
  2555. goto remove_lru_entry;
  2556. cache = list_entry(nfsi->access_cache_entry_lru.next,
  2557. struct nfs_access_entry, lru);
  2558. list_move(&cache->lru, &head);
  2559. rb_erase(&cache->rb_node, &nfsi->access_cache);
  2560. freed++;
  2561. if (!list_empty(&nfsi->access_cache_entry_lru))
  2562. list_move_tail(&nfsi->access_cache_inode_lru,
  2563. &nfs_access_lru_list);
  2564. else {
  2565. remove_lru_entry:
  2566. list_del_init(&nfsi->access_cache_inode_lru);
  2567. smp_mb__before_atomic();
  2568. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  2569. smp_mb__after_atomic();
  2570. }
  2571. spin_unlock(&inode->i_lock);
  2572. }
  2573. spin_unlock(&nfs_access_lru_lock);
  2574. nfs_access_free_list(&head);
  2575. return freed;
  2576. }
  2577. unsigned long
  2578. nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
  2579. {
  2580. int nr_to_scan = sc->nr_to_scan;
  2581. gfp_t gfp_mask = sc->gfp_mask;
  2582. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  2583. return SHRINK_STOP;
  2584. return nfs_do_access_cache_scan(nr_to_scan);
  2585. }
  2586. unsigned long
  2587. nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
  2588. {
  2589. return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
  2590. }
  2591. static void
  2592. nfs_access_cache_enforce_limit(void)
  2593. {
  2594. long nr_entries = atomic_long_read(&nfs_access_nr_entries);
  2595. unsigned long diff;
  2596. unsigned int nr_to_scan;
  2597. if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
  2598. return;
  2599. nr_to_scan = 100;
  2600. diff = nr_entries - nfs_access_max_cachesize;
  2601. if (diff < nr_to_scan)
  2602. nr_to_scan = diff;
  2603. nfs_do_access_cache_scan(nr_to_scan);
  2604. }
  2605. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  2606. {
  2607. struct rb_root *root_node = &nfsi->access_cache;
  2608. struct rb_node *n;
  2609. struct nfs_access_entry *entry;
  2610. /* Unhook entries from the cache */
  2611. while ((n = rb_first(root_node)) != NULL) {
  2612. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  2613. rb_erase(n, root_node);
  2614. list_move(&entry->lru, head);
  2615. }
  2616. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  2617. }
  2618. void nfs_access_zap_cache(struct inode *inode)
  2619. {
  2620. LIST_HEAD(head);
  2621. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  2622. return;
  2623. /* Remove from global LRU init */
  2624. spin_lock(&nfs_access_lru_lock);
  2625. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  2626. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  2627. spin_lock(&inode->i_lock);
  2628. __nfs_access_zap_cache(NFS_I(inode), &head);
  2629. spin_unlock(&inode->i_lock);
  2630. spin_unlock(&nfs_access_lru_lock);
  2631. nfs_access_free_list(&head);
  2632. }
  2633. EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
  2634. static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
  2635. {
  2636. struct group_info *ga, *gb;
  2637. int g;
  2638. if (uid_lt(a->fsuid, b->fsuid))
  2639. return -1;
  2640. if (uid_gt(a->fsuid, b->fsuid))
  2641. return 1;
  2642. if (gid_lt(a->fsgid, b->fsgid))
  2643. return -1;
  2644. if (gid_gt(a->fsgid, b->fsgid))
  2645. return 1;
  2646. ga = a->group_info;
  2647. gb = b->group_info;
  2648. if (ga == gb)
  2649. return 0;
  2650. if (ga == NULL)
  2651. return -1;
  2652. if (gb == NULL)
  2653. return 1;
  2654. if (ga->ngroups < gb->ngroups)
  2655. return -1;
  2656. if (ga->ngroups > gb->ngroups)
  2657. return 1;
  2658. for (g = 0; g < ga->ngroups; g++) {
  2659. if (gid_lt(ga->gid[g], gb->gid[g]))
  2660. return -1;
  2661. if (gid_gt(ga->gid[g], gb->gid[g]))
  2662. return 1;
  2663. }
  2664. return 0;
  2665. }
  2666. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
  2667. {
  2668. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  2669. while (n != NULL) {
  2670. struct nfs_access_entry *entry =
  2671. rb_entry(n, struct nfs_access_entry, rb_node);
  2672. int cmp = access_cmp(cred, entry);
  2673. if (cmp < 0)
  2674. n = n->rb_left;
  2675. else if (cmp > 0)
  2676. n = n->rb_right;
  2677. else
  2678. return entry;
  2679. }
  2680. return NULL;
  2681. }
  2682. static u64 nfs_access_login_time(const struct task_struct *task,
  2683. const struct cred *cred)
  2684. {
  2685. const struct task_struct *parent;
  2686. const struct cred *pcred;
  2687. u64 ret;
  2688. rcu_read_lock();
  2689. for (;;) {
  2690. parent = rcu_dereference(task->real_parent);
  2691. pcred = __task_cred(parent);
  2692. if (parent == task || cred_fscmp(pcred, cred) != 0)
  2693. break;
  2694. task = parent;
  2695. }
  2696. ret = task->start_time;
  2697. rcu_read_unlock();
  2698. return ret;
  2699. }
  2700. static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
  2701. {
  2702. struct nfs_inode *nfsi = NFS_I(inode);
  2703. u64 login_time = nfs_access_login_time(current, cred);
  2704. struct nfs_access_entry *cache;
  2705. bool retry = true;
  2706. int err;
  2707. spin_lock(&inode->i_lock);
  2708. for(;;) {
  2709. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  2710. goto out_zap;
  2711. cache = nfs_access_search_rbtree(inode, cred);
  2712. err = -ENOENT;
  2713. if (cache == NULL)
  2714. goto out;
  2715. /* Found an entry, is our attribute cache valid? */
  2716. if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
  2717. break;
  2718. if (!retry)
  2719. break;
  2720. err = -ECHILD;
  2721. if (!may_block)
  2722. goto out;
  2723. spin_unlock(&inode->i_lock);
  2724. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2725. if (err)
  2726. return err;
  2727. spin_lock(&inode->i_lock);
  2728. retry = false;
  2729. }
  2730. err = -ENOENT;
  2731. if ((s64)(login_time - cache->timestamp) > 0)
  2732. goto out;
  2733. *mask = cache->mask;
  2734. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  2735. err = 0;
  2736. out:
  2737. spin_unlock(&inode->i_lock);
  2738. return err;
  2739. out_zap:
  2740. spin_unlock(&inode->i_lock);
  2741. nfs_access_zap_cache(inode);
  2742. return -ENOENT;
  2743. }
  2744. static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
  2745. {
  2746. /* Only check the most recently returned cache entry,
  2747. * but do it without locking.
  2748. */
  2749. struct nfs_inode *nfsi = NFS_I(inode);
  2750. u64 login_time = nfs_access_login_time(current, cred);
  2751. struct nfs_access_entry *cache;
  2752. int err = -ECHILD;
  2753. struct list_head *lh;
  2754. rcu_read_lock();
  2755. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  2756. goto out;
  2757. lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
  2758. cache = list_entry(lh, struct nfs_access_entry, lru);
  2759. if (lh == &nfsi->access_cache_entry_lru ||
  2760. access_cmp(cred, cache) != 0)
  2761. cache = NULL;
  2762. if (cache == NULL)
  2763. goto out;
  2764. if ((s64)(login_time - cache->timestamp) > 0)
  2765. goto out;
  2766. if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
  2767. goto out;
  2768. *mask = cache->mask;
  2769. err = 0;
  2770. out:
  2771. rcu_read_unlock();
  2772. return err;
  2773. }
  2774. int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
  2775. u32 *mask, bool may_block)
  2776. {
  2777. int status;
  2778. status = nfs_access_get_cached_rcu(inode, cred, mask);
  2779. if (status != 0)
  2780. status = nfs_access_get_cached_locked(inode, cred, mask,
  2781. may_block);
  2782. return status;
  2783. }
  2784. EXPORT_SYMBOL_GPL(nfs_access_get_cached);
  2785. static void nfs_access_add_rbtree(struct inode *inode,
  2786. struct nfs_access_entry *set,
  2787. const struct cred *cred)
  2788. {
  2789. struct nfs_inode *nfsi = NFS_I(inode);
  2790. struct rb_root *root_node = &nfsi->access_cache;
  2791. struct rb_node **p = &root_node->rb_node;
  2792. struct rb_node *parent = NULL;
  2793. struct nfs_access_entry *entry;
  2794. int cmp;
  2795. spin_lock(&inode->i_lock);
  2796. while (*p != NULL) {
  2797. parent = *p;
  2798. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  2799. cmp = access_cmp(cred, entry);
  2800. if (cmp < 0)
  2801. p = &parent->rb_left;
  2802. else if (cmp > 0)
  2803. p = &parent->rb_right;
  2804. else
  2805. goto found;
  2806. }
  2807. rb_link_node(&set->rb_node, parent, p);
  2808. rb_insert_color(&set->rb_node, root_node);
  2809. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  2810. spin_unlock(&inode->i_lock);
  2811. return;
  2812. found:
  2813. rb_replace_node(parent, &set->rb_node, root_node);
  2814. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  2815. list_del(&entry->lru);
  2816. spin_unlock(&inode->i_lock);
  2817. nfs_access_free_entry(entry);
  2818. }
  2819. void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
  2820. const struct cred *cred)
  2821. {
  2822. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  2823. if (cache == NULL)
  2824. return;
  2825. RB_CLEAR_NODE(&cache->rb_node);
  2826. cache->fsuid = cred->fsuid;
  2827. cache->fsgid = cred->fsgid;
  2828. cache->group_info = get_group_info(cred->group_info);
  2829. cache->mask = set->mask;
  2830. cache->timestamp = ktime_get_ns();
  2831. /* The above field assignments must be visible
  2832. * before this item appears on the lru. We cannot easily
  2833. * use rcu_assign_pointer, so just force the memory barrier.
  2834. */
  2835. smp_wmb();
  2836. nfs_access_add_rbtree(inode, cache, cred);
  2837. /* Update accounting */
  2838. smp_mb__before_atomic();
  2839. atomic_long_inc(&nfs_access_nr_entries);
  2840. smp_mb__after_atomic();
  2841. /* Add inode to global LRU list */
  2842. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  2843. spin_lock(&nfs_access_lru_lock);
  2844. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  2845. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  2846. &nfs_access_lru_list);
  2847. spin_unlock(&nfs_access_lru_lock);
  2848. }
  2849. nfs_access_cache_enforce_limit();
  2850. }
  2851. EXPORT_SYMBOL_GPL(nfs_access_add_cache);
  2852. #define NFS_MAY_READ (NFS_ACCESS_READ)
  2853. #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
  2854. NFS_ACCESS_EXTEND | \
  2855. NFS_ACCESS_DELETE)
  2856. #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
  2857. NFS_ACCESS_EXTEND)
  2858. #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
  2859. #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
  2860. #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
  2861. static int
  2862. nfs_access_calc_mask(u32 access_result, umode_t umode)
  2863. {
  2864. int mask = 0;
  2865. if (access_result & NFS_MAY_READ)
  2866. mask |= MAY_READ;
  2867. if (S_ISDIR(umode)) {
  2868. if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
  2869. mask |= MAY_WRITE;
  2870. if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
  2871. mask |= MAY_EXEC;
  2872. } else if (S_ISREG(umode)) {
  2873. if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
  2874. mask |= MAY_WRITE;
  2875. if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
  2876. mask |= MAY_EXEC;
  2877. } else if (access_result & NFS_MAY_WRITE)
  2878. mask |= MAY_WRITE;
  2879. return mask;
  2880. }
  2881. void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
  2882. {
  2883. entry->mask = access_result;
  2884. }
  2885. EXPORT_SYMBOL_GPL(nfs_access_set_mask);
  2886. static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
  2887. {
  2888. struct nfs_access_entry cache;
  2889. bool may_block = (mask & MAY_NOT_BLOCK) == 0;
  2890. int cache_mask = -1;
  2891. int status;
  2892. trace_nfs_access_enter(inode);
  2893. status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
  2894. if (status == 0)
  2895. goto out_cached;
  2896. status = -ECHILD;
  2897. if (!may_block)
  2898. goto out;
  2899. /*
  2900. * Determine which access bits we want to ask for...
  2901. */
  2902. cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
  2903. nfs_access_xattr_mask(NFS_SERVER(inode));
  2904. if (S_ISDIR(inode->i_mode))
  2905. cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
  2906. else
  2907. cache.mask |= NFS_ACCESS_EXECUTE;
  2908. status = NFS_PROTO(inode)->access(inode, &cache, cred);
  2909. if (status != 0) {
  2910. if (status == -ESTALE) {
  2911. if (!S_ISDIR(inode->i_mode))
  2912. nfs_set_inode_stale(inode);
  2913. else
  2914. nfs_zap_caches(inode);
  2915. }
  2916. goto out;
  2917. }
  2918. nfs_access_add_cache(inode, &cache, cred);
  2919. out_cached:
  2920. cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
  2921. if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
  2922. status = -EACCES;
  2923. out:
  2924. trace_nfs_access_exit(inode, mask, cache_mask, status);
  2925. return status;
  2926. }
  2927. static int nfs_open_permission_mask(int openflags)
  2928. {
  2929. int mask = 0;
  2930. if (openflags & __FMODE_EXEC) {
  2931. /* ONLY check exec rights */
  2932. mask = MAY_EXEC;
  2933. } else {
  2934. if ((openflags & O_ACCMODE) != O_WRONLY)
  2935. mask |= MAY_READ;
  2936. if ((openflags & O_ACCMODE) != O_RDONLY)
  2937. mask |= MAY_WRITE;
  2938. }
  2939. return mask;
  2940. }
  2941. int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
  2942. {
  2943. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  2944. }
  2945. EXPORT_SYMBOL_GPL(nfs_may_open);
  2946. static int nfs_execute_ok(struct inode *inode, int mask)
  2947. {
  2948. struct nfs_server *server = NFS_SERVER(inode);
  2949. int ret = 0;
  2950. if (S_ISDIR(inode->i_mode))
  2951. return 0;
  2952. if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
  2953. if (mask & MAY_NOT_BLOCK)
  2954. return -ECHILD;
  2955. ret = __nfs_revalidate_inode(server, inode);
  2956. }
  2957. if (ret == 0 && !execute_ok(inode))
  2958. ret = -EACCES;
  2959. return ret;
  2960. }
  2961. int nfs_permission(struct mnt_idmap *idmap,
  2962. struct inode *inode,
  2963. int mask)
  2964. {
  2965. const struct cred *cred = current_cred();
  2966. int res = 0;
  2967. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  2968. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  2969. goto out;
  2970. /* Is this sys_access() ? */
  2971. if (mask & (MAY_ACCESS | MAY_CHDIR))
  2972. goto force_lookup;
  2973. switch (inode->i_mode & S_IFMT) {
  2974. case S_IFLNK:
  2975. goto out;
  2976. case S_IFREG:
  2977. if ((mask & MAY_OPEN) &&
  2978. nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
  2979. return 0;
  2980. break;
  2981. case S_IFDIR:
  2982. /*
  2983. * Optimize away all write operations, since the server
  2984. * will check permissions when we perform the op.
  2985. */
  2986. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  2987. goto out;
  2988. }
  2989. force_lookup:
  2990. if (!NFS_PROTO(inode)->access)
  2991. goto out_notsup;
  2992. res = nfs_do_access(inode, cred, mask);
  2993. out:
  2994. if (!res && (mask & MAY_EXEC))
  2995. res = nfs_execute_ok(inode, mask);
  2996. dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
  2997. inode->i_sb->s_id, inode->i_ino, mask, res);
  2998. return res;
  2999. out_notsup:
  3000. if (mask & MAY_NOT_BLOCK)
  3001. return -ECHILD;
  3002. res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
  3003. NFS_INO_INVALID_OTHER);
  3004. if (res == 0)
  3005. res = generic_permission(&nop_mnt_idmap, inode, mask);
  3006. goto out;
  3007. }
  3008. EXPORT_SYMBOL_GPL(nfs_permission);