dir.c 89 KB

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