inode.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/nfs/inode.c
  4. *
  5. * Copyright (C) 1992 Rick Sladkey
  6. *
  7. * nfs inode and superblock handling functions
  8. *
  9. * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
  10. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  11. *
  12. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  13. * J.S.Peatfield@damtp.cam.ac.uk
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/time.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/string.h>
  23. #include <linux/stat.h>
  24. #include <linux/errno.h>
  25. #include <linux/unistd.h>
  26. #include <linux/sunrpc/clnt.h>
  27. #include <linux/sunrpc/stats.h>
  28. #include <linux/sunrpc/metrics.h>
  29. #include <linux/nfs_fs.h>
  30. #include <linux/nfs_mount.h>
  31. #include <linux/nfs4_mount.h>
  32. #include <linux/lockd/bind.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/mount.h>
  35. #include <linux/vfs.h>
  36. #include <linux/inet.h>
  37. #include <linux/nfs_xdr.h>
  38. #include <linux/slab.h>
  39. #include <linux/compat.h>
  40. #include <linux/freezer.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/iversion.h>
  43. #include "nfs4_fs.h"
  44. #include "callback.h"
  45. #include "delegation.h"
  46. #include "iostat.h"
  47. #include "internal.h"
  48. #include "fscache.h"
  49. #include "pnfs.h"
  50. #include "nfs.h"
  51. #include "netns.h"
  52. #include "sysfs.h"
  53. #include "nfstrace.h"
  54. #define NFSDBG_FACILITY NFSDBG_VFS
  55. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  56. /* Default is to see 64-bit inode numbers */
  57. static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  58. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  59. static struct kmem_cache * nfs_inode_cachep;
  60. static inline unsigned long
  61. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  62. {
  63. return nfs_fileid_to_ino_t(fattr->fileid);
  64. }
  65. int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
  66. {
  67. if (unlikely(nfs_current_task_exiting()))
  68. return -EINTR;
  69. schedule();
  70. if (signal_pending_state(mode, current))
  71. return -ERESTARTSYS;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
  75. /**
  76. * nfs_compat_user_ino64 - returns the user-visible inode number
  77. * @fileid: 64-bit fileid
  78. *
  79. * This function returns a 32-bit inode number if the boot parameter
  80. * nfs.enable_ino64 is zero.
  81. */
  82. u64 nfs_compat_user_ino64(u64 fileid)
  83. {
  84. #ifdef CONFIG_COMPAT
  85. compat_ulong_t ino;
  86. #else
  87. unsigned long ino;
  88. #endif
  89. if (enable_ino64)
  90. return fileid;
  91. ino = fileid;
  92. if (sizeof(ino) < sizeof(fileid))
  93. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  94. return ino;
  95. }
  96. int nfs_drop_inode(struct inode *inode)
  97. {
  98. return NFS_STALE(inode) || inode_generic_drop(inode);
  99. }
  100. EXPORT_SYMBOL_GPL(nfs_drop_inode);
  101. void nfs_clear_inode(struct inode *inode)
  102. {
  103. /*
  104. * The following should never happen...
  105. */
  106. WARN_ON_ONCE(nfs_have_writebacks(inode));
  107. WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
  108. nfs_zap_acl_cache(inode);
  109. nfs_access_zap_cache(inode);
  110. nfs_fscache_clear_inode(inode);
  111. }
  112. EXPORT_SYMBOL_GPL(nfs_clear_inode);
  113. void nfs_evict_inode(struct inode *inode)
  114. {
  115. truncate_inode_pages_final(&inode->i_data);
  116. clear_inode(inode);
  117. nfs_clear_inode(inode);
  118. }
  119. int nfs_sync_inode(struct inode *inode)
  120. {
  121. inode_dio_wait(inode);
  122. return nfs_wb_all(inode);
  123. }
  124. EXPORT_SYMBOL_GPL(nfs_sync_inode);
  125. /**
  126. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  127. * @mapping: pointer to struct address_space
  128. */
  129. int nfs_sync_mapping(struct address_space *mapping)
  130. {
  131. int ret = 0;
  132. if (mapping->nrpages != 0) {
  133. unmap_mapping_range(mapping, 0, 0, 0);
  134. ret = nfs_wb_all(mapping->host);
  135. }
  136. return ret;
  137. }
  138. static int nfs_attribute_timeout(struct inode *inode)
  139. {
  140. struct nfs_inode *nfsi = NFS_I(inode);
  141. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  142. }
  143. static bool nfs_check_cache_flags_invalid(struct inode *inode,
  144. unsigned long flags)
  145. {
  146. unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  147. return (cache_validity & flags) != 0;
  148. }
  149. bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
  150. {
  151. if (nfs_check_cache_flags_invalid(inode, flags))
  152. return true;
  153. return nfs_attribute_cache_expired(inode);
  154. }
  155. EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
  156. #ifdef CONFIG_NFS_V4_2
  157. static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
  158. {
  159. return nfsi->xattr_cache != NULL;
  160. }
  161. #else
  162. static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
  163. {
  164. return false;
  165. }
  166. #endif
  167. void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
  168. {
  169. struct nfs_inode *nfsi = NFS_I(inode);
  170. if (nfs_have_delegated_attributes(inode)) {
  171. if (!(flags & NFS_INO_REVAL_FORCED))
  172. flags &= ~(NFS_INO_INVALID_MODE |
  173. NFS_INO_INVALID_OTHER |
  174. NFS_INO_INVALID_BTIME |
  175. NFS_INO_INVALID_XATTR);
  176. flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
  177. }
  178. if (!nfs_has_xattr_cache(nfsi))
  179. flags &= ~NFS_INO_INVALID_XATTR;
  180. if (flags & NFS_INO_INVALID_DATA)
  181. nfs_fscache_invalidate(inode, 0);
  182. flags &= ~NFS_INO_REVAL_FORCED;
  183. flags |= nfsi->cache_validity;
  184. if (inode->i_mapping->nrpages == 0)
  185. flags &= ~NFS_INO_INVALID_DATA;
  186. /* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
  187. smp_store_release(&nfsi->cache_validity, flags);
  188. if (inode->i_mapping->nrpages == 0 ||
  189. nfsi->cache_validity & NFS_INO_INVALID_DATA) {
  190. nfs_ooo_clear(nfsi);
  191. }
  192. trace_nfs_set_cache_invalid(inode, 0);
  193. }
  194. EXPORT_SYMBOL_GPL(nfs_set_cache_invalid);
  195. /*
  196. * Invalidate the local caches
  197. */
  198. static void nfs_zap_caches_locked(struct inode *inode)
  199. {
  200. struct nfs_inode *nfsi = NFS_I(inode);
  201. int mode = inode->i_mode;
  202. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  203. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  204. nfsi->attrtimeo_timestamp = jiffies;
  205. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  206. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
  207. NFS_INO_INVALID_DATA |
  208. NFS_INO_INVALID_ACCESS |
  209. NFS_INO_INVALID_ACL |
  210. NFS_INO_INVALID_XATTR);
  211. else
  212. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
  213. NFS_INO_INVALID_ACCESS |
  214. NFS_INO_INVALID_ACL |
  215. NFS_INO_INVALID_XATTR);
  216. nfs_zap_label_cache_locked(nfsi);
  217. }
  218. void nfs_zap_caches(struct inode *inode)
  219. {
  220. spin_lock(&inode->i_lock);
  221. nfs_zap_caches_locked(inode);
  222. spin_unlock(&inode->i_lock);
  223. }
  224. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  225. {
  226. if (mapping->nrpages != 0) {
  227. spin_lock(&inode->i_lock);
  228. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  229. spin_unlock(&inode->i_lock);
  230. }
  231. }
  232. void nfs_zap_acl_cache(struct inode *inode)
  233. {
  234. void (*clear_acl_cache)(struct inode *);
  235. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  236. if (clear_acl_cache != NULL)
  237. clear_acl_cache(inode);
  238. spin_lock(&inode->i_lock);
  239. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  240. spin_unlock(&inode->i_lock);
  241. }
  242. EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
  243. void nfs_invalidate_atime(struct inode *inode)
  244. {
  245. if (nfs_have_delegated_atime(inode))
  246. return;
  247. spin_lock(&inode->i_lock);
  248. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  249. spin_unlock(&inode->i_lock);
  250. }
  251. EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
  252. /*
  253. * Invalidate, but do not unhash, the inode.
  254. * NB: must be called with inode->i_lock held!
  255. */
  256. static void nfs_set_inode_stale_locked(struct inode *inode)
  257. {
  258. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  259. nfs_zap_caches_locked(inode);
  260. trace_nfs_set_inode_stale(inode);
  261. }
  262. void nfs_set_inode_stale(struct inode *inode)
  263. {
  264. spin_lock(&inode->i_lock);
  265. nfs_set_inode_stale_locked(inode);
  266. spin_unlock(&inode->i_lock);
  267. }
  268. struct nfs_find_desc {
  269. struct nfs_fh *fh;
  270. struct nfs_fattr *fattr;
  271. };
  272. /*
  273. * In NFSv3 we can have 64bit inode numbers. In order to support
  274. * this, and re-exported directories (also seen in NFSv2)
  275. * we are forced to allow 2 different inodes to have the same
  276. * i_ino.
  277. */
  278. static int
  279. nfs_find_actor(struct inode *inode, void *opaque)
  280. {
  281. struct nfs_find_desc *desc = opaque;
  282. struct nfs_fh *fh = desc->fh;
  283. struct nfs_fattr *fattr = desc->fattr;
  284. if (NFS_FILEID(inode) != fattr->fileid)
  285. return 0;
  286. if (inode_wrong_type(inode, fattr->mode))
  287. return 0;
  288. if (nfs_compare_fh(NFS_FH(inode), fh))
  289. return 0;
  290. if (is_bad_inode(inode) || NFS_STALE(inode))
  291. return 0;
  292. return 1;
  293. }
  294. static int
  295. nfs_init_locked(struct inode *inode, void *opaque)
  296. {
  297. struct nfs_find_desc *desc = opaque;
  298. struct nfs_fattr *fattr = desc->fattr;
  299. set_nfs_fileid(inode, fattr->fileid);
  300. inode->i_mode = fattr->mode;
  301. nfs_copy_fh(NFS_FH(inode), desc->fh);
  302. return 0;
  303. }
  304. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  305. static void nfs_clear_label_invalid(struct inode *inode)
  306. {
  307. spin_lock(&inode->i_lock);
  308. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
  309. spin_unlock(&inode->i_lock);
  310. }
  311. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
  312. {
  313. int error;
  314. if (fattr->label == NULL)
  315. return;
  316. if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
  317. error = security_inode_notifysecctx(inode, fattr->label->label,
  318. fattr->label->len);
  319. if (error)
  320. printk(KERN_ERR "%s() %s %d "
  321. "security_inode_notifysecctx() %d\n",
  322. __func__,
  323. (char *)fattr->label->label,
  324. fattr->label->len, error);
  325. nfs_clear_label_invalid(inode);
  326. }
  327. }
  328. struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
  329. {
  330. struct nfs4_label *label;
  331. if (!(server->caps & NFS_CAP_SECURITY_LABEL))
  332. return NULL;
  333. label = kzalloc_obj(struct nfs4_label, flags);
  334. if (label == NULL)
  335. return ERR_PTR(-ENOMEM);
  336. label->label = kzalloc(NFS4_MAXLABELLEN, flags);
  337. if (label->label == NULL) {
  338. kfree(label);
  339. return ERR_PTR(-ENOMEM);
  340. }
  341. label->len = NFS4_MAXLABELLEN;
  342. return label;
  343. }
  344. EXPORT_SYMBOL_GPL(nfs4_label_alloc);
  345. #else
  346. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
  347. {
  348. }
  349. #endif
  350. EXPORT_SYMBOL_GPL(nfs_setsecurity);
  351. /* Search for inode identified by fh, fileid and i_mode in inode cache. */
  352. struct inode *
  353. nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
  354. {
  355. struct nfs_find_desc desc = {
  356. .fh = fh,
  357. .fattr = fattr,
  358. };
  359. struct inode *inode;
  360. unsigned long hash;
  361. if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
  362. !(fattr->valid & NFS_ATTR_FATTR_TYPE))
  363. return NULL;
  364. hash = nfs_fattr_to_ino_t(fattr);
  365. inode = ilookup5(sb, hash, nfs_find_actor, &desc);
  366. dprintk("%s: returning %p\n", __func__, inode);
  367. return inode;
  368. }
  369. static void nfs_inode_init_regular(struct nfs_inode *nfsi)
  370. {
  371. atomic_long_set(&nfsi->nrequests, 0);
  372. atomic_long_set(&nfsi->redirtied_pages, 0);
  373. INIT_LIST_HEAD(&nfsi->commit_info.list);
  374. atomic_long_set(&nfsi->commit_info.ncommit, 0);
  375. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  376. mutex_init(&nfsi->commit_mutex);
  377. }
  378. static void nfs_inode_init_dir(struct nfs_inode *nfsi)
  379. {
  380. nfsi->cache_change_attribute = 0;
  381. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  382. init_rwsem(&nfsi->rmdir_sem);
  383. }
  384. /*
  385. * This is our front-end to iget that looks up inodes by file handle
  386. * instead of inode number.
  387. */
  388. struct inode *
  389. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  390. {
  391. struct nfs_find_desc desc = {
  392. .fh = fh,
  393. .fattr = fattr
  394. };
  395. struct inode *inode = ERR_PTR(-ENOENT);
  396. u64 fattr_supported = NFS_SB(sb)->fattr_valid;
  397. unsigned long hash;
  398. nfs_attr_check_mountpoint(sb, fattr);
  399. if (nfs_attr_use_mounted_on_fileid(fattr))
  400. fattr->fileid = fattr->mounted_on_fileid;
  401. else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
  402. goto out_no_inode;
  403. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  404. goto out_no_inode;
  405. hash = nfs_fattr_to_ino_t(fattr);
  406. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  407. if (inode == NULL) {
  408. inode = ERR_PTR(-ENOMEM);
  409. goto out_no_inode;
  410. }
  411. if (inode_state_read_once(inode) & I_NEW) {
  412. struct nfs_inode *nfsi = NFS_I(inode);
  413. unsigned long now = jiffies;
  414. /* We set i_ino for the few things that still rely on it,
  415. * such as stat(2) */
  416. inode->i_ino = hash;
  417. /* We can't support update_atime(), since the server will reset it */
  418. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  419. inode->i_mode = fattr->mode;
  420. nfsi->cache_validity = 0;
  421. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  422. && (fattr_supported & NFS_ATTR_FATTR_MODE))
  423. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
  424. /* Why so? Because we want revalidate for devices/FIFOs, and
  425. * that's precisely what we have in nfs_file_inode_operations.
  426. */
  427. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  428. if (S_ISREG(inode->i_mode)) {
  429. inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
  430. inode->i_data.a_ops = &nfs_file_aops;
  431. nfs_inode_init_regular(nfsi);
  432. mapping_set_large_folios(inode->i_mapping);
  433. } else if (S_ISDIR(inode->i_mode)) {
  434. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  435. inode->i_fop = &nfs_dir_operations;
  436. inode->i_data.a_ops = &nfs_dir_aops;
  437. nfs_inode_init_dir(nfsi);
  438. /* Deal with crossing mountpoints */
  439. if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
  440. fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  441. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  442. inode->i_op = &nfs_referral_inode_operations;
  443. else
  444. inode->i_op = &nfs_mountpoint_inode_operations;
  445. inode->i_fop = NULL;
  446. inode->i_flags |= S_AUTOMOUNT;
  447. }
  448. } else if (S_ISLNK(inode->i_mode)) {
  449. inode->i_op = &nfs_symlink_inode_operations;
  450. inode_nohighmem(inode);
  451. } else
  452. init_special_inode(inode, inode->i_mode, fattr->rdev);
  453. inode_set_atime(inode, 0, 0);
  454. inode_set_mtime(inode, 0, 0);
  455. inode_set_ctime(inode, 0, 0);
  456. memset(&nfsi->btime, 0, sizeof(nfsi->btime));
  457. inode_set_iversion_raw(inode, 0);
  458. inode->i_size = 0;
  459. clear_nlink(inode);
  460. inode->i_uid = make_kuid(&init_user_ns, -2);
  461. inode->i_gid = make_kgid(&init_user_ns, -2);
  462. inode->i_blocks = 0;
  463. nfsi->write_io = 0;
  464. nfsi->read_io = 0;
  465. nfsi->read_cache_jiffies = fattr->time_start;
  466. nfsi->attr_gencount = fattr->gencount;
  467. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  468. inode_set_atime_to_ts(inode, fattr->atime);
  469. else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
  470. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  471. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  472. inode_set_mtime_to_ts(inode, fattr->mtime);
  473. else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
  474. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  475. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  476. inode_set_ctime_to_ts(inode, fattr->ctime);
  477. else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
  478. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
  479. if (fattr->valid & NFS_ATTR_FATTR_BTIME)
  480. nfsi->btime = fattr->btime;
  481. else if (fattr_supported & NFS_ATTR_FATTR_BTIME)
  482. nfs_set_cache_invalid(inode, NFS_INO_INVALID_BTIME);
  483. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  484. inode_set_iversion_raw(inode, fattr->change_attr);
  485. else
  486. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
  487. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  488. inode->i_size = nfs_size_to_loff_t(fattr->size);
  489. else
  490. nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
  491. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  492. set_nlink(inode, fattr->nlink);
  493. else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
  494. nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
  495. else
  496. set_nlink(inode, 1);
  497. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  498. inode->i_uid = fattr->uid;
  499. else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
  500. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  501. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  502. inode->i_gid = fattr->gid;
  503. else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
  504. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  505. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  506. inode->i_blocks = fattr->du.nfs2.blocks;
  507. else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED &&
  508. fattr->size != 0)
  509. nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
  510. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  511. /*
  512. * report the blocks in 512byte units
  513. */
  514. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  515. } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED &&
  516. fattr->size != 0)
  517. nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
  518. nfs_setsecurity(inode, fattr);
  519. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  520. nfsi->attrtimeo_timestamp = now;
  521. nfsi->access_cache = RB_ROOT;
  522. nfs_fscache_init_inode(inode);
  523. unlock_new_inode(inode);
  524. } else {
  525. int err = nfs_refresh_inode(inode, fattr);
  526. if (err < 0) {
  527. iput(inode);
  528. inode = ERR_PTR(err);
  529. goto out_no_inode;
  530. }
  531. }
  532. dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
  533. inode->i_sb->s_id,
  534. (unsigned long long)NFS_FILEID(inode),
  535. nfs_display_fhandle_hash(fh),
  536. icount_read(inode));
  537. out:
  538. return inode;
  539. out_no_inode:
  540. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  541. goto out;
  542. }
  543. EXPORT_SYMBOL_GPL(nfs_fhget);
  544. static void
  545. nfs_fattr_fixup_delegated(struct inode *inode, struct nfs_fattr *fattr)
  546. {
  547. unsigned long cache_validity = NFS_I(inode)->cache_validity;
  548. if (nfs_have_delegated_mtime(inode)) {
  549. if (!(cache_validity & NFS_INO_INVALID_CTIME))
  550. fattr->valid &= ~(NFS_ATTR_FATTR_PRECTIME |
  551. NFS_ATTR_FATTR_CTIME);
  552. if (!(cache_validity & NFS_INO_INVALID_MTIME))
  553. fattr->valid &= ~(NFS_ATTR_FATTR_PREMTIME |
  554. NFS_ATTR_FATTR_MTIME);
  555. if (!(cache_validity & NFS_INO_INVALID_ATIME))
  556. fattr->valid &= ~NFS_ATTR_FATTR_ATIME;
  557. } else if (nfs_have_delegated_atime(inode)) {
  558. if (!(cache_validity & NFS_INO_INVALID_ATIME))
  559. fattr->valid &= ~NFS_ATTR_FATTR_ATIME;
  560. }
  561. }
  562. static void nfs_set_timestamps_to_ts(struct inode *inode, struct iattr *attr)
  563. {
  564. unsigned int cache_flags = 0;
  565. if (attr->ia_valid & ATTR_MTIME_SET) {
  566. struct timespec64 ctime = inode_get_ctime(inode);
  567. struct timespec64 mtime = inode_get_mtime(inode);
  568. struct timespec64 now;
  569. bool updated = false;
  570. now = inode_set_ctime_current(inode);
  571. if (!timespec64_equal(&now, &ctime))
  572. updated = true;
  573. inode_set_mtime_to_ts(inode, attr->ia_mtime);
  574. if (!timespec64_equal(&now, &mtime))
  575. updated = true;
  576. inode_maybe_inc_iversion(inode, updated);
  577. cache_flags |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
  578. }
  579. if (attr->ia_valid & ATTR_ATIME_SET) {
  580. inode_set_atime_to_ts(inode, attr->ia_atime);
  581. cache_flags |= NFS_INO_INVALID_ATIME;
  582. }
  583. NFS_I(inode)->cache_validity &= ~cache_flags;
  584. }
  585. static void nfs_update_atime(struct inode *inode)
  586. {
  587. inode_update_time(inode, FS_UPD_ATIME, 0);
  588. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ATIME;
  589. }
  590. static void nfs_update_mtime(struct inode *inode)
  591. {
  592. inode_update_time(inode, FS_UPD_CMTIME, 0);
  593. NFS_I(inode)->cache_validity &=
  594. ~(NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME);
  595. }
  596. void nfs_update_delegated_atime(struct inode *inode)
  597. {
  598. spin_lock(&inode->i_lock);
  599. if (nfs_have_delegated_atime(inode))
  600. nfs_update_atime(inode);
  601. spin_unlock(&inode->i_lock);
  602. }
  603. void nfs_update_delegated_mtime_locked(struct inode *inode)
  604. {
  605. if (nfs_have_delegated_mtime(inode))
  606. nfs_update_mtime(inode);
  607. }
  608. void nfs_update_delegated_mtime(struct inode *inode)
  609. {
  610. spin_lock(&inode->i_lock);
  611. nfs_update_delegated_mtime_locked(inode);
  612. spin_unlock(&inode->i_lock);
  613. }
  614. EXPORT_SYMBOL_GPL(nfs_update_delegated_mtime);
  615. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN)
  616. int
  617. nfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  618. struct iattr *attr)
  619. {
  620. struct inode *inode = d_inode(dentry);
  621. struct nfs_fattr *fattr;
  622. loff_t oldsize;
  623. int error = 0;
  624. kuid_t task_uid = current_fsuid();
  625. kuid_t owner_uid = inode->i_uid;
  626. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  627. /* skip mode change if it's just for clearing setuid/setgid */
  628. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  629. attr->ia_valid &= ~ATTR_MODE;
  630. if (S_ISREG(inode->i_mode))
  631. nfs_file_block_o_direct(NFS_I(inode));
  632. oldsize = i_size_read(inode);
  633. if (attr->ia_valid & ATTR_SIZE) {
  634. BUG_ON(!S_ISREG(inode->i_mode));
  635. error = inode_newsize_ok(inode, attr->ia_size);
  636. if (error)
  637. return error;
  638. if (attr->ia_size == oldsize)
  639. attr->ia_valid &= ~ATTR_SIZE;
  640. }
  641. if (nfs_have_delegated_mtime(inode) && attr->ia_valid & ATTR_MTIME) {
  642. spin_lock(&inode->i_lock);
  643. if (attr->ia_valid & ATTR_MTIME_SET) {
  644. if (uid_eq(task_uid, owner_uid)) {
  645. nfs_set_timestamps_to_ts(inode, attr);
  646. attr->ia_valid &= ~(ATTR_MTIME|ATTR_MTIME_SET|
  647. ATTR_ATIME|ATTR_ATIME_SET);
  648. }
  649. } else {
  650. if (attr->ia_valid & ATTR_MTIME)
  651. nfs_update_mtime(inode);
  652. if (attr->ia_valid & ATTR_ATIME)
  653. nfs_update_atime(inode);
  654. attr->ia_valid &= ~(ATTR_MTIME|ATTR_ATIME);
  655. }
  656. spin_unlock(&inode->i_lock);
  657. } else if (nfs_have_delegated_atime(inode) &&
  658. attr->ia_valid & ATTR_ATIME &&
  659. !(attr->ia_valid & ATTR_MTIME)) {
  660. if (attr->ia_valid & ATTR_ATIME_SET) {
  661. if (uid_eq(task_uid, owner_uid)) {
  662. spin_lock(&inode->i_lock);
  663. nfs_set_timestamps_to_ts(inode, attr);
  664. spin_unlock(&inode->i_lock);
  665. attr->ia_valid &= ~(ATTR_ATIME|ATTR_ATIME_SET);
  666. }
  667. } else {
  668. nfs_update_delegated_atime(inode);
  669. attr->ia_valid &= ~ATTR_ATIME;
  670. }
  671. }
  672. /* Optimization: if the end result is no change, don't RPC */
  673. if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  674. return 0;
  675. trace_nfs_setattr_enter(inode);
  676. /* Write all dirty data */
  677. if (S_ISREG(inode->i_mode))
  678. nfs_sync_inode(inode);
  679. fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
  680. if (fattr == NULL) {
  681. error = -ENOMEM;
  682. goto out;
  683. }
  684. error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
  685. if (error == 0) {
  686. if (attr->ia_valid & ATTR_SIZE)
  687. nfs_truncate_last_folio(inode->i_mapping, oldsize,
  688. attr->ia_size);
  689. error = nfs_refresh_inode(inode, fattr);
  690. }
  691. nfs_free_fattr(fattr);
  692. out:
  693. trace_nfs_setattr_exit(inode, error);
  694. return error;
  695. }
  696. EXPORT_SYMBOL_GPL(nfs_setattr);
  697. /**
  698. * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
  699. * @inode: inode of the file used
  700. * @offset: file offset to start truncating
  701. *
  702. * This is a copy of the common vmtruncate, but with the locking
  703. * corrected to take into account the fact that NFS requires
  704. * inode->i_size to be updated under the inode->i_lock.
  705. * Note: must be called with inode->i_lock held!
  706. */
  707. static int nfs_vmtruncate(struct inode * inode, loff_t offset)
  708. {
  709. int err;
  710. err = inode_newsize_ok(inode, offset);
  711. if (err)
  712. goto out;
  713. trace_nfs_size_truncate(inode, offset);
  714. i_size_write(inode, offset);
  715. /* Optimisation */
  716. if (offset == 0) {
  717. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
  718. nfs_ooo_clear(NFS_I(inode));
  719. }
  720. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
  721. spin_unlock(&inode->i_lock);
  722. truncate_pagecache(inode, offset);
  723. nfs_update_delegated_mtime_locked(inode);
  724. spin_lock(&inode->i_lock);
  725. out:
  726. return err;
  727. }
  728. /**
  729. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  730. * @inode: pointer to struct inode
  731. * @attr: pointer to struct iattr
  732. * @fattr: pointer to struct nfs_fattr
  733. *
  734. * Note: we do this in the *proc.c in order to ensure that
  735. * it works for things like exclusive creates too.
  736. */
  737. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
  738. struct nfs_fattr *fattr)
  739. {
  740. /* Barrier: bump the attribute generation count. */
  741. nfs_fattr_set_barrier(fattr);
  742. spin_lock(&inode->i_lock);
  743. NFS_I(inode)->attr_gencount = fattr->gencount;
  744. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  745. if (!nfs_have_delegated_mtime(inode))
  746. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  747. nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
  748. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  749. nfs_vmtruncate(inode, attr->ia_size);
  750. }
  751. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  752. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
  753. if ((attr->ia_valid & ATTR_KILL_SUID) != 0 &&
  754. inode->i_mode & S_ISUID)
  755. inode->i_mode &= ~S_ISUID;
  756. if (setattr_should_drop_sgid(&nop_mnt_idmap, inode))
  757. inode->i_mode &= ~S_ISGID;
  758. if ((attr->ia_valid & ATTR_MODE) != 0) {
  759. int mode = attr->ia_mode & S_IALLUGO;
  760. mode |= inode->i_mode & ~S_IALLUGO;
  761. inode->i_mode = mode;
  762. }
  763. if ((attr->ia_valid & ATTR_UID) != 0)
  764. inode->i_uid = attr->ia_uid;
  765. if ((attr->ia_valid & ATTR_GID) != 0)
  766. inode->i_gid = attr->ia_gid;
  767. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  768. inode_set_ctime_to_ts(inode, fattr->ctime);
  769. else
  770. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  771. | NFS_INO_INVALID_CTIME);
  772. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
  773. | NFS_INO_INVALID_ACL);
  774. }
  775. if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
  776. NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
  777. | NFS_INO_INVALID_CTIME);
  778. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  779. inode_set_atime_to_ts(inode, fattr->atime);
  780. else if (attr->ia_valid & ATTR_ATIME_SET)
  781. inode_set_atime_to_ts(inode, attr->ia_atime);
  782. else
  783. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  784. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  785. inode_set_ctime_to_ts(inode, fattr->ctime);
  786. else
  787. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  788. | NFS_INO_INVALID_CTIME);
  789. }
  790. if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
  791. NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
  792. | NFS_INO_INVALID_CTIME);
  793. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  794. inode_set_mtime_to_ts(inode, fattr->mtime);
  795. else if (attr->ia_valid & ATTR_MTIME_SET)
  796. inode_set_mtime_to_ts(inode, attr->ia_mtime);
  797. else
  798. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  799. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  800. inode_set_ctime_to_ts(inode, fattr->ctime);
  801. else
  802. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  803. | NFS_INO_INVALID_CTIME);
  804. }
  805. if (fattr->valid)
  806. nfs_update_inode(inode, fattr);
  807. spin_unlock(&inode->i_lock);
  808. }
  809. EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
  810. /*
  811. * Don't request help from readdirplus if the file is being written to,
  812. * or if attribute caching is turned off
  813. */
  814. static bool nfs_getattr_readdirplus_enable(const struct inode *inode)
  815. {
  816. return nfs_server_capable(inode, NFS_CAP_READDIRPLUS) &&
  817. !nfs_have_writebacks(inode) && NFS_MAXATTRTIMEO(inode) > 5 * HZ;
  818. }
  819. static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
  820. {
  821. if (!IS_ROOT(dentry)) {
  822. struct dentry *parent = dget_parent(dentry);
  823. nfs_readdir_record_entry_cache_miss(d_inode(parent));
  824. dput(parent);
  825. }
  826. }
  827. static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
  828. {
  829. if (!IS_ROOT(dentry)) {
  830. struct dentry *parent = dget_parent(dentry);
  831. nfs_readdir_record_entry_cache_hit(d_inode(parent));
  832. dput(parent);
  833. }
  834. }
  835. static u32 nfs_get_valid_attrmask(struct inode *inode)
  836. {
  837. u64 fattr_valid = NFS_SERVER(inode)->fattr_valid;
  838. unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  839. u32 reply_mask = STATX_INO | STATX_TYPE;
  840. if (!(cache_validity & NFS_INO_INVALID_ATIME))
  841. reply_mask |= STATX_ATIME;
  842. if (!(cache_validity & NFS_INO_INVALID_CTIME))
  843. reply_mask |= STATX_CTIME;
  844. if (!(cache_validity & NFS_INO_INVALID_MTIME))
  845. reply_mask |= STATX_MTIME;
  846. if (!(cache_validity & NFS_INO_INVALID_SIZE))
  847. reply_mask |= STATX_SIZE;
  848. if (!(cache_validity & NFS_INO_INVALID_NLINK))
  849. reply_mask |= STATX_NLINK;
  850. if (!(cache_validity & NFS_INO_INVALID_MODE))
  851. reply_mask |= STATX_MODE;
  852. if (!(cache_validity & NFS_INO_INVALID_OTHER))
  853. reply_mask |= STATX_UID | STATX_GID;
  854. if (!(cache_validity & NFS_INO_INVALID_BLOCKS))
  855. reply_mask |= STATX_BLOCKS;
  856. if (!(cache_validity & NFS_INO_INVALID_BTIME) &&
  857. (fattr_valid & NFS_ATTR_FATTR_BTIME))
  858. reply_mask |= STATX_BTIME;
  859. if (!(cache_validity & NFS_INO_INVALID_CHANGE))
  860. reply_mask |= STATX_CHANGE_COOKIE;
  861. return reply_mask;
  862. }
  863. int nfs_getattr(struct mnt_idmap *idmap, const struct path *path,
  864. struct kstat *stat, u32 request_mask, unsigned int query_flags)
  865. {
  866. struct inode *inode = d_inode(path->dentry);
  867. struct nfs_server *server = NFS_SERVER(inode);
  868. u64 fattr_valid = server->fattr_valid;
  869. unsigned long cache_validity;
  870. int err = 0;
  871. bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
  872. bool do_update = false;
  873. bool readdirplus_enabled = nfs_getattr_readdirplus_enable(inode);
  874. trace_nfs_getattr_enter(inode);
  875. request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID |
  876. STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME |
  877. STATX_INO | STATX_SIZE | STATX_BLOCKS | STATX_BTIME |
  878. STATX_CHANGE_COOKIE;
  879. if (!(fattr_valid & NFS_ATTR_FATTR_BTIME))
  880. request_mask &= ~STATX_BTIME;
  881. if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
  882. if (readdirplus_enabled)
  883. nfs_readdirplus_parent_cache_hit(path->dentry);
  884. goto out_no_revalidate;
  885. }
  886. /* Flush out writes to the server in order to update c/mtime/version. */
  887. if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_CHANGE_COOKIE)) &&
  888. S_ISREG(inode->i_mode)) {
  889. if (nfs_have_delegated_mtime(inode))
  890. filemap_fdatawrite(inode->i_mapping);
  891. else
  892. filemap_write_and_wait(inode->i_mapping);
  893. }
  894. /*
  895. * We may force a getattr if the user cares about atime.
  896. *
  897. * Note that we only have to check the vfsmount flags here:
  898. * - NFS always sets S_NOATIME by so checking it would give a
  899. * bogus result
  900. * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
  901. * no point in checking those.
  902. */
  903. if ((path->mnt->mnt_flags & MNT_NOATIME) ||
  904. ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  905. request_mask &= ~STATX_ATIME;
  906. /* Is the user requesting attributes that might need revalidation? */
  907. if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
  908. STATX_MTIME|STATX_UID|STATX_GID|
  909. STATX_SIZE|STATX_BLOCKS|STATX_BTIME|
  910. STATX_CHANGE_COOKIE)))
  911. goto out_no_revalidate;
  912. /* Check whether the cached attributes are stale */
  913. do_update |= force_sync || nfs_attribute_cache_expired(inode);
  914. cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  915. do_update |= cache_validity & NFS_INO_INVALID_CHANGE;
  916. if (request_mask & STATX_ATIME)
  917. do_update |= cache_validity & NFS_INO_INVALID_ATIME;
  918. if (request_mask & STATX_CTIME)
  919. do_update |= cache_validity & NFS_INO_INVALID_CTIME;
  920. if (request_mask & STATX_MTIME)
  921. do_update |= cache_validity & NFS_INO_INVALID_MTIME;
  922. if (request_mask & STATX_SIZE)
  923. do_update |= cache_validity & NFS_INO_INVALID_SIZE;
  924. if (request_mask & STATX_NLINK)
  925. do_update |= cache_validity & NFS_INO_INVALID_NLINK;
  926. if (request_mask & STATX_MODE)
  927. do_update |= cache_validity & NFS_INO_INVALID_MODE;
  928. if (request_mask & (STATX_UID | STATX_GID))
  929. do_update |= cache_validity & NFS_INO_INVALID_OTHER;
  930. if (request_mask & STATX_BLOCKS)
  931. do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
  932. if (request_mask & STATX_BTIME)
  933. do_update |= cache_validity & NFS_INO_INVALID_BTIME;
  934. if (do_update) {
  935. if (readdirplus_enabled)
  936. nfs_readdirplus_parent_cache_miss(path->dentry);
  937. err = __nfs_revalidate_inode(server, inode);
  938. if (err)
  939. goto out;
  940. } else if (readdirplus_enabled)
  941. nfs_readdirplus_parent_cache_hit(path->dentry);
  942. out_no_revalidate:
  943. /* Only return attributes that were revalidated. */
  944. stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask;
  945. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  946. stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
  947. stat->change_cookie = inode_peek_iversion_raw(inode);
  948. stat->attributes_mask |= STATX_ATTR_CHANGE_MONOTONIC;
  949. if (server->change_attr_type != NFS4_CHANGE_TYPE_IS_UNDEFINED)
  950. stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
  951. if (S_ISDIR(inode->i_mode))
  952. stat->blksize = NFS_SERVER(inode)->dtsize;
  953. stat->btime = NFS_I(inode)->btime;
  954. /* Special handling for STATX_DIOALIGN and STATX_DIO_READ_ALIGN
  955. * - NFS doesn't have DIO alignment constraints, avoid getting
  956. * these DIO attrs from remote and just respond with most
  957. * accommodating limits (so client will issue supported DIO).
  958. * - this is unintuitive, but the most coarse-grained
  959. * dio_offset_align is the most accommodating.
  960. */
  961. if ((request_mask & (STATX_DIOALIGN | STATX_DIO_READ_ALIGN)) &&
  962. S_ISREG(inode->i_mode)) {
  963. stat->result_mask |= STATX_DIOALIGN | STATX_DIO_READ_ALIGN;
  964. stat->dio_mem_align = 4; /* 4-byte alignment */
  965. stat->dio_offset_align = PAGE_SIZE;
  966. stat->dio_read_offset_align = stat->dio_offset_align;
  967. }
  968. out:
  969. trace_nfs_getattr_exit(inode, err);
  970. return err;
  971. }
  972. EXPORT_SYMBOL_GPL(nfs_getattr);
  973. static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
  974. {
  975. refcount_set(&l_ctx->count, 1);
  976. l_ctx->lockowner = current->files;
  977. INIT_LIST_HEAD(&l_ctx->list);
  978. atomic_set(&l_ctx->io_count, 0);
  979. }
  980. static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
  981. {
  982. struct nfs_lock_context *pos;
  983. list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
  984. if (pos->lockowner != current->files)
  985. continue;
  986. if (refcount_inc_not_zero(&pos->count))
  987. return pos;
  988. }
  989. return NULL;
  990. }
  991. struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
  992. {
  993. struct nfs_lock_context *res, *new = NULL;
  994. struct inode *inode = d_inode(ctx->dentry);
  995. rcu_read_lock();
  996. res = __nfs_find_lock_context(ctx);
  997. rcu_read_unlock();
  998. if (res == NULL) {
  999. new = kmalloc_obj(*new, GFP_KERNEL_ACCOUNT);
  1000. if (new == NULL)
  1001. return ERR_PTR(-ENOMEM);
  1002. nfs_init_lock_context(new);
  1003. spin_lock(&inode->i_lock);
  1004. res = __nfs_find_lock_context(ctx);
  1005. if (res == NULL) {
  1006. new->open_context = get_nfs_open_context(ctx);
  1007. if (new->open_context) {
  1008. list_add_tail_rcu(&new->list,
  1009. &ctx->lock_context.list);
  1010. res = new;
  1011. new = NULL;
  1012. } else
  1013. res = ERR_PTR(-EBADF);
  1014. }
  1015. spin_unlock(&inode->i_lock);
  1016. kfree(new);
  1017. }
  1018. return res;
  1019. }
  1020. EXPORT_SYMBOL_GPL(nfs_get_lock_context);
  1021. void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
  1022. {
  1023. struct nfs_open_context *ctx = l_ctx->open_context;
  1024. struct inode *inode = d_inode(ctx->dentry);
  1025. if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
  1026. return;
  1027. list_del_rcu(&l_ctx->list);
  1028. spin_unlock(&inode->i_lock);
  1029. put_nfs_open_context(ctx);
  1030. kfree_rcu(l_ctx, rcu_head);
  1031. }
  1032. EXPORT_SYMBOL_GPL(nfs_put_lock_context);
  1033. /**
  1034. * nfs_close_context - Common close_context() routine NFSv2/v3
  1035. * @ctx: pointer to context
  1036. * @is_sync: is this a synchronous close
  1037. *
  1038. * Ensure that the attributes are up to date if we're mounted
  1039. * with close-to-open semantics and we have cached data that will
  1040. * need to be revalidated on open.
  1041. */
  1042. void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
  1043. {
  1044. struct nfs_inode *nfsi;
  1045. struct inode *inode;
  1046. if (!(ctx->mode & FMODE_WRITE))
  1047. return;
  1048. if (!is_sync)
  1049. return;
  1050. inode = d_inode(ctx->dentry);
  1051. if (nfs_have_read_or_write_delegation(inode))
  1052. return;
  1053. nfsi = NFS_I(inode);
  1054. if (inode->i_mapping->nrpages == 0)
  1055. return;
  1056. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1057. return;
  1058. if (!list_empty(&nfsi->open_files))
  1059. return;
  1060. if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO)
  1061. return;
  1062. nfs_revalidate_inode(inode,
  1063. NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
  1064. }
  1065. EXPORT_SYMBOL_GPL(nfs_close_context);
  1066. struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
  1067. fmode_t f_mode,
  1068. struct file *filp)
  1069. {
  1070. struct nfs_open_context *ctx;
  1071. ctx = kmalloc_obj(*ctx, GFP_KERNEL_ACCOUNT);
  1072. if (!ctx)
  1073. return ERR_PTR(-ENOMEM);
  1074. nfs_sb_active(dentry->d_sb);
  1075. ctx->dentry = dget(dentry);
  1076. if (filp)
  1077. ctx->cred = get_cred(filp->f_cred);
  1078. else
  1079. ctx->cred = get_current_cred();
  1080. rcu_assign_pointer(ctx->ll_cred, NULL);
  1081. ctx->state = NULL;
  1082. ctx->mode = f_mode;
  1083. ctx->flags = 0;
  1084. ctx->error = 0;
  1085. ctx->flock_owner = (fl_owner_t)filp;
  1086. nfs_init_lock_context(&ctx->lock_context);
  1087. ctx->lock_context.open_context = ctx;
  1088. INIT_LIST_HEAD(&ctx->list);
  1089. ctx->mdsthreshold = NULL;
  1090. nfs_localio_file_init(&ctx->nfl);
  1091. return ctx;
  1092. }
  1093. EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
  1094. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  1095. {
  1096. if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
  1097. return ctx;
  1098. return NULL;
  1099. }
  1100. EXPORT_SYMBOL_GPL(get_nfs_open_context);
  1101. static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
  1102. {
  1103. struct inode *inode = d_inode(ctx->dentry);
  1104. struct super_block *sb = ctx->dentry->d_sb;
  1105. if (!refcount_dec_and_test(&ctx->lock_context.count))
  1106. return;
  1107. if (!list_empty(&ctx->list)) {
  1108. spin_lock(&inode->i_lock);
  1109. list_del_rcu(&ctx->list);
  1110. spin_unlock(&inode->i_lock);
  1111. }
  1112. if (inode != NULL)
  1113. NFS_PROTO(inode)->close_context(ctx, is_sync);
  1114. put_cred(ctx->cred);
  1115. dput(ctx->dentry);
  1116. nfs_sb_deactive(sb);
  1117. put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1));
  1118. kfree(ctx->mdsthreshold);
  1119. nfs_close_local_fh(&ctx->nfl);
  1120. kfree_rcu(ctx, rcu_head);
  1121. }
  1122. void put_nfs_open_context(struct nfs_open_context *ctx)
  1123. {
  1124. __put_nfs_open_context(ctx, 0);
  1125. }
  1126. EXPORT_SYMBOL_GPL(put_nfs_open_context);
  1127. static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
  1128. {
  1129. __put_nfs_open_context(ctx, 1);
  1130. }
  1131. /*
  1132. * Ensure that mmap has a recent RPC credential for use when writing out
  1133. * shared pages
  1134. */
  1135. void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
  1136. {
  1137. struct inode *inode = d_inode(ctx->dentry);
  1138. struct nfs_inode *nfsi = NFS_I(inode);
  1139. spin_lock(&inode->i_lock);
  1140. if (list_empty(&nfsi->open_files) &&
  1141. nfs_ooo_test(nfsi))
  1142. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA |
  1143. NFS_INO_REVAL_FORCED);
  1144. list_add_tail_rcu(&ctx->list, &nfsi->open_files);
  1145. spin_unlock(&inode->i_lock);
  1146. }
  1147. EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
  1148. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  1149. {
  1150. filp->private_data = get_nfs_open_context(ctx);
  1151. set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
  1152. if (list_empty(&ctx->list))
  1153. nfs_inode_attach_open_context(ctx);
  1154. }
  1155. EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
  1156. /*
  1157. * Given an inode, search for an open context with the desired characteristics
  1158. */
  1159. struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
  1160. {
  1161. struct nfs_inode *nfsi = NFS_I(inode);
  1162. struct nfs_open_context *pos, *ctx = NULL;
  1163. rcu_read_lock();
  1164. list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
  1165. if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
  1166. continue;
  1167. if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
  1168. continue;
  1169. if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags))
  1170. continue;
  1171. ctx = get_nfs_open_context(pos);
  1172. if (ctx)
  1173. break;
  1174. }
  1175. rcu_read_unlock();
  1176. return ctx;
  1177. }
  1178. void nfs_file_clear_open_context(struct file *filp)
  1179. {
  1180. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  1181. if (ctx) {
  1182. struct inode *inode = d_inode(ctx->dentry);
  1183. clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
  1184. /*
  1185. * We fatal error on write before. Try to writeback
  1186. * every page again.
  1187. */
  1188. if (ctx->error < 0)
  1189. invalidate_inode_pages2(inode->i_mapping);
  1190. filp->private_data = NULL;
  1191. put_nfs_open_context_sync(ctx);
  1192. }
  1193. }
  1194. /*
  1195. * These allocate and release file read/write context information.
  1196. */
  1197. int nfs_open(struct inode *inode, struct file *filp)
  1198. {
  1199. struct nfs_open_context *ctx;
  1200. ctx = alloc_nfs_open_context(file_dentry(filp),
  1201. flags_to_mode(filp->f_flags), filp);
  1202. if (IS_ERR(ctx))
  1203. return PTR_ERR(ctx);
  1204. nfs_file_set_open_context(filp, ctx);
  1205. put_nfs_open_context(ctx);
  1206. nfs_fscache_open_file(inode, filp);
  1207. return 0;
  1208. }
  1209. /*
  1210. * This function is called whenever some part of NFS notices that
  1211. * the cached attributes have to be refreshed.
  1212. */
  1213. int
  1214. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  1215. {
  1216. int status = -ESTALE;
  1217. struct nfs_fattr *fattr = NULL;
  1218. struct nfs_inode *nfsi = NFS_I(inode);
  1219. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
  1220. inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
  1221. trace_nfs_revalidate_inode_enter(inode);
  1222. if (is_bad_inode(inode))
  1223. goto out;
  1224. if (NFS_STALE(inode))
  1225. goto out;
  1226. /* pNFS: Attributes aren't updated until we layoutcommit */
  1227. if (S_ISREG(inode->i_mode)) {
  1228. status = pnfs_sync_inode(inode, false);
  1229. if (status)
  1230. goto out;
  1231. } else if (nfs_have_directory_delegation(inode)) {
  1232. status = 0;
  1233. goto out;
  1234. }
  1235. status = -ENOMEM;
  1236. fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
  1237. if (fattr == NULL)
  1238. goto out;
  1239. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  1240. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, inode);
  1241. if (status != 0) {
  1242. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
  1243. inode->i_sb->s_id,
  1244. (unsigned long long)NFS_FILEID(inode), status);
  1245. switch (status) {
  1246. case -ETIMEDOUT:
  1247. /* A soft timeout occurred. Use cached information? */
  1248. if (server->flags & NFS_MOUNT_SOFTREVAL)
  1249. status = 0;
  1250. break;
  1251. case -ESTALE:
  1252. if (!S_ISDIR(inode->i_mode))
  1253. nfs_set_inode_stale(inode);
  1254. else
  1255. nfs_zap_caches(inode);
  1256. }
  1257. goto out;
  1258. }
  1259. status = nfs_refresh_inode(inode, fattr);
  1260. if (status) {
  1261. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
  1262. inode->i_sb->s_id,
  1263. (unsigned long long)NFS_FILEID(inode), status);
  1264. goto out;
  1265. }
  1266. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  1267. nfs_zap_acl_cache(inode);
  1268. nfs_setsecurity(inode, fattr);
  1269. dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
  1270. inode->i_sb->s_id,
  1271. (unsigned long long)NFS_FILEID(inode));
  1272. out:
  1273. nfs_free_fattr(fattr);
  1274. trace_nfs_revalidate_inode_exit(inode, status);
  1275. return status;
  1276. }
  1277. int nfs_attribute_cache_expired(struct inode *inode)
  1278. {
  1279. if (nfs_have_delegated_attributes(inode))
  1280. return 0;
  1281. return nfs_attribute_timeout(inode);
  1282. }
  1283. /**
  1284. * nfs_revalidate_inode - Revalidate the inode attributes
  1285. * @inode: pointer to inode struct
  1286. * @flags: cache flags to check
  1287. *
  1288. * Updates inode attribute information by retrieving the data from the server.
  1289. */
  1290. int nfs_revalidate_inode(struct inode *inode, unsigned long flags)
  1291. {
  1292. if (!nfs_check_cache_invalid(inode, flags))
  1293. return NFS_STALE(inode) ? -ESTALE : 0;
  1294. return __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1295. }
  1296. EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
  1297. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  1298. {
  1299. int ret;
  1300. nfs_fscache_invalidate(inode, 0);
  1301. if (mapping->nrpages != 0) {
  1302. if (S_ISREG(inode->i_mode)) {
  1303. ret = nfs_sync_mapping(mapping);
  1304. if (ret < 0)
  1305. return ret;
  1306. }
  1307. ret = invalidate_inode_pages2(mapping);
  1308. if (ret < 0)
  1309. return ret;
  1310. }
  1311. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  1312. dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
  1313. inode->i_sb->s_id,
  1314. (unsigned long long)NFS_FILEID(inode));
  1315. return 0;
  1316. }
  1317. /**
  1318. * nfs_clear_invalid_mapping - Conditionally clear a mapping
  1319. * @mapping: pointer to mapping
  1320. *
  1321. * If the NFS_INO_INVALID_DATA inode flag is set, clear the mapping.
  1322. */
  1323. int nfs_clear_invalid_mapping(struct address_space *mapping)
  1324. {
  1325. struct inode *inode = mapping->host;
  1326. struct nfs_inode *nfsi = NFS_I(inode);
  1327. unsigned long *bitlock = &nfsi->flags;
  1328. int ret = 0;
  1329. /*
  1330. * We must clear NFS_INO_INVALID_DATA first to ensure that
  1331. * invalidations that come in while we're shooting down the mappings
  1332. * are respected. But, that leaves a race window where one revalidator
  1333. * can clear the flag, and then another checks it before the mapping
  1334. * gets invalidated. Fix that by serializing access to this part of
  1335. * the function.
  1336. *
  1337. * At the same time, we need to allow other tasks to see whether we
  1338. * might be in the middle of invalidating the pages, so we only set
  1339. * the bit lock here if it looks like we're going to be doing that.
  1340. */
  1341. for (;;) {
  1342. ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
  1343. nfs_wait_bit_killable,
  1344. TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  1345. if (ret)
  1346. goto out;
  1347. smp_rmb(); /* pairs with smp_wmb() below */
  1348. if (test_bit(NFS_INO_INVALIDATING, bitlock))
  1349. continue;
  1350. /* pairs with nfs_set_cache_invalid()'s smp_store_release() */
  1351. if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
  1352. goto out;
  1353. /* Slow-path that double-checks with spinlock held */
  1354. spin_lock(&inode->i_lock);
  1355. if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
  1356. spin_unlock(&inode->i_lock);
  1357. continue;
  1358. }
  1359. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1360. break;
  1361. spin_unlock(&inode->i_lock);
  1362. goto out;
  1363. }
  1364. set_bit(NFS_INO_INVALIDATING, bitlock);
  1365. smp_wmb();
  1366. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  1367. nfs_ooo_clear(nfsi);
  1368. spin_unlock(&inode->i_lock);
  1369. trace_nfs_invalidate_mapping_enter(inode);
  1370. ret = nfs_invalidate_mapping(inode, mapping);
  1371. trace_nfs_invalidate_mapping_exit(inode, ret);
  1372. clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
  1373. smp_mb__after_atomic();
  1374. wake_up_bit(bitlock, NFS_INO_INVALIDATING);
  1375. out:
  1376. return ret;
  1377. }
  1378. bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  1379. {
  1380. return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) ||
  1381. NFS_STALE(inode);
  1382. }
  1383. int nfs_revalidate_mapping_rcu(struct inode *inode)
  1384. {
  1385. struct nfs_inode *nfsi = NFS_I(inode);
  1386. unsigned long *bitlock = &nfsi->flags;
  1387. int ret = 0;
  1388. if (IS_SWAPFILE(inode))
  1389. goto out;
  1390. if (nfs_mapping_need_revalidate_inode(inode)) {
  1391. ret = -ECHILD;
  1392. goto out;
  1393. }
  1394. spin_lock(&inode->i_lock);
  1395. if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
  1396. (nfsi->cache_validity & NFS_INO_INVALID_DATA))
  1397. ret = -ECHILD;
  1398. spin_unlock(&inode->i_lock);
  1399. out:
  1400. return ret;
  1401. }
  1402. /**
  1403. * nfs_revalidate_mapping - Revalidate the pagecache
  1404. * @inode: pointer to host inode
  1405. * @mapping: pointer to mapping
  1406. */
  1407. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  1408. {
  1409. /* swapfiles are not supposed to be shared. */
  1410. if (IS_SWAPFILE(inode))
  1411. return 0;
  1412. if (nfs_mapping_need_revalidate_inode(inode)) {
  1413. int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1414. if (ret < 0)
  1415. return ret;
  1416. }
  1417. return nfs_clear_invalid_mapping(mapping);
  1418. }
  1419. static bool nfs_file_has_writers(struct nfs_inode *nfsi)
  1420. {
  1421. struct inode *inode = &nfsi->vfs_inode;
  1422. if (!S_ISREG(inode->i_mode))
  1423. return false;
  1424. if (list_empty(&nfsi->open_files))
  1425. return false;
  1426. return inode_is_open_for_write(inode);
  1427. }
  1428. static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
  1429. {
  1430. return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
  1431. }
  1432. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1433. {
  1434. struct timespec64 ts;
  1435. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  1436. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  1437. && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
  1438. inode_set_iversion_raw(inode, fattr->change_attr);
  1439. if (S_ISDIR(inode->i_mode))
  1440. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  1441. else if (nfs_server_capable(inode, NFS_CAP_XATTR))
  1442. nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
  1443. }
  1444. /* If we have atomic WCC data, we may update some attributes */
  1445. ts = inode_get_ctime(inode);
  1446. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  1447. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  1448. && timespec64_equal(&ts, &fattr->pre_ctime)) {
  1449. inode_set_ctime_to_ts(inode, fattr->ctime);
  1450. }
  1451. ts = inode_get_mtime(inode);
  1452. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  1453. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  1454. && timespec64_equal(&ts, &fattr->pre_mtime)) {
  1455. inode_set_mtime_to_ts(inode, fattr->mtime);
  1456. }
  1457. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  1458. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  1459. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  1460. && !nfs_have_writebacks(inode)) {
  1461. trace_nfs_size_wcc(inode, fattr->size);
  1462. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  1463. }
  1464. }
  1465. /**
  1466. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  1467. * @inode: pointer to inode
  1468. * @fattr: updated attributes
  1469. *
  1470. * Verifies the attribute cache. If we have just changed the attributes,
  1471. * so that fattr carries weak cache consistency data, then it may
  1472. * also update the ctime/mtime/change_attribute.
  1473. */
  1474. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  1475. {
  1476. struct nfs_inode *nfsi = NFS_I(inode);
  1477. loff_t cur_size, new_isize;
  1478. unsigned long invalid = 0;
  1479. struct timespec64 ts;
  1480. if (nfs_have_delegated_attributes(inode))
  1481. return 0;
  1482. if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
  1483. /* Only a mounted-on-fileid? Just exit */
  1484. if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
  1485. return 0;
  1486. /* Has the inode gone and changed behind our back? */
  1487. } else if (nfsi->fileid != fattr->fileid) {
  1488. /* Is this perhaps the mounted-on fileid? */
  1489. if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
  1490. nfsi->fileid == fattr->mounted_on_fileid)
  1491. return 0;
  1492. return -ESTALE;
  1493. }
  1494. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
  1495. return -ESTALE;
  1496. if (!nfs_file_has_buffered_writers(nfsi)) {
  1497. /* Verify a few of the more important attributes */
  1498. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
  1499. invalid |= NFS_INO_INVALID_CHANGE;
  1500. ts = inode_get_mtime(inode);
  1501. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
  1502. invalid |= NFS_INO_INVALID_MTIME;
  1503. ts = inode_get_ctime(inode);
  1504. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
  1505. invalid |= NFS_INO_INVALID_CTIME;
  1506. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1507. cur_size = i_size_read(inode);
  1508. new_isize = nfs_size_to_loff_t(fattr->size);
  1509. if (cur_size != new_isize)
  1510. invalid |= NFS_INO_INVALID_SIZE;
  1511. }
  1512. }
  1513. /* Have any file permissions changed? */
  1514. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  1515. invalid |= NFS_INO_INVALID_MODE;
  1516. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
  1517. invalid |= NFS_INO_INVALID_OTHER;
  1518. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
  1519. invalid |= NFS_INO_INVALID_OTHER;
  1520. /* Has the link count changed? */
  1521. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  1522. invalid |= NFS_INO_INVALID_NLINK;
  1523. ts = inode_get_atime(inode);
  1524. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
  1525. invalid |= NFS_INO_INVALID_ATIME;
  1526. if (invalid != 0)
  1527. nfs_set_cache_invalid(inode, invalid);
  1528. nfsi->read_cache_jiffies = fattr->time_start;
  1529. return 0;
  1530. }
  1531. static atomic_long_t nfs_attr_generation_counter;
  1532. static unsigned long nfs_read_attr_generation_counter(void)
  1533. {
  1534. return atomic_long_read(&nfs_attr_generation_counter);
  1535. }
  1536. unsigned long nfs_inc_attr_generation_counter(void)
  1537. {
  1538. return atomic_long_inc_return(&nfs_attr_generation_counter);
  1539. }
  1540. EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
  1541. void nfs_fattr_init(struct nfs_fattr *fattr)
  1542. {
  1543. fattr->valid = 0;
  1544. fattr->time_start = jiffies;
  1545. fattr->gencount = nfs_inc_attr_generation_counter();
  1546. fattr->owner_name = NULL;
  1547. fattr->group_name = NULL;
  1548. fattr->mdsthreshold = NULL;
  1549. }
  1550. EXPORT_SYMBOL_GPL(nfs_fattr_init);
  1551. /**
  1552. * nfs_fattr_set_barrier
  1553. * @fattr: attributes
  1554. *
  1555. * Used to set a barrier after an attribute was updated. This
  1556. * barrier ensures that older attributes from RPC calls that may
  1557. * have raced with our update cannot clobber these new values.
  1558. * Note that you are still responsible for ensuring that other
  1559. * operations which change the attribute on the server do not
  1560. * collide.
  1561. */
  1562. void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
  1563. {
  1564. fattr->gencount = nfs_inc_attr_generation_counter();
  1565. }
  1566. struct nfs_fattr *nfs_alloc_fattr(void)
  1567. {
  1568. struct nfs_fattr *fattr;
  1569. fattr = kmalloc_obj(*fattr);
  1570. if (fattr != NULL) {
  1571. nfs_fattr_init(fattr);
  1572. fattr->label = NULL;
  1573. }
  1574. return fattr;
  1575. }
  1576. EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
  1577. struct nfs_fattr *nfs_alloc_fattr_with_label(struct nfs_server *server)
  1578. {
  1579. struct nfs_fattr *fattr = nfs_alloc_fattr();
  1580. if (!fattr)
  1581. return NULL;
  1582. fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
  1583. if (IS_ERR(fattr->label)) {
  1584. kfree(fattr);
  1585. return NULL;
  1586. }
  1587. return fattr;
  1588. }
  1589. EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label);
  1590. struct nfs_fh *nfs_alloc_fhandle(void)
  1591. {
  1592. struct nfs_fh *fh;
  1593. fh = kmalloc_obj(struct nfs_fh);
  1594. if (fh != NULL)
  1595. fh->size = 0;
  1596. return fh;
  1597. }
  1598. EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
  1599. #ifdef NFS_DEBUG
  1600. /*
  1601. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  1602. * in the same way that wireshark does
  1603. *
  1604. * @fh: file handle
  1605. *
  1606. * For debugging only.
  1607. */
  1608. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  1609. {
  1610. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  1611. * not on the result */
  1612. return nfs_fhandle_hash(fh);
  1613. }
  1614. EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
  1615. /*
  1616. * _nfs_display_fhandle - display an NFS file handle on the console
  1617. *
  1618. * @fh: file handle to display
  1619. * @caption: display caption
  1620. *
  1621. * For debugging only.
  1622. */
  1623. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  1624. {
  1625. unsigned short i;
  1626. if (fh == NULL || fh->size == 0) {
  1627. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  1628. return;
  1629. }
  1630. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  1631. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  1632. for (i = 0; i < fh->size; i += 16) {
  1633. __be32 *pos = (__be32 *)&fh->data[i];
  1634. switch ((fh->size - i - 1) >> 2) {
  1635. case 0:
  1636. printk(KERN_DEFAULT " %08x\n",
  1637. be32_to_cpup(pos));
  1638. break;
  1639. case 1:
  1640. printk(KERN_DEFAULT " %08x %08x\n",
  1641. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  1642. break;
  1643. case 2:
  1644. printk(KERN_DEFAULT " %08x %08x %08x\n",
  1645. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1646. be32_to_cpup(pos + 2));
  1647. break;
  1648. default:
  1649. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  1650. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1651. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  1652. }
  1653. }
  1654. }
  1655. EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
  1656. #endif
  1657. /**
  1658. * nfs_inode_attrs_cmp_generic - compare attributes
  1659. * @fattr: attributes
  1660. * @inode: pointer to inode
  1661. *
  1662. * Attempt to divine whether or not an RPC call reply carrying stale
  1663. * attributes got scheduled after another call carrying updated ones.
  1664. * Note also the check for wraparound of 'attr_gencount'
  1665. *
  1666. * The function returns '1' if it thinks the attributes in @fattr are
  1667. * more recent than the ones cached in @inode. Otherwise it returns
  1668. * the value '0'.
  1669. */
  1670. static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr,
  1671. const struct inode *inode)
  1672. {
  1673. unsigned long attr_gencount = NFS_I(inode)->attr_gencount;
  1674. return (long)(fattr->gencount - attr_gencount) > 0 ||
  1675. (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0;
  1676. }
  1677. /**
  1678. * nfs_inode_attrs_cmp_monotonic - compare attributes
  1679. * @fattr: attributes
  1680. * @inode: pointer to inode
  1681. *
  1682. * Attempt to divine whether or not an RPC call reply carrying stale
  1683. * attributes got scheduled after another call carrying updated ones.
  1684. *
  1685. * We assume that the server observes monotonic semantics for
  1686. * the change attribute, so a larger value means that the attributes in
  1687. * @fattr are more recent, in which case the function returns the
  1688. * value '1'.
  1689. * A return value of '0' indicates no measurable change
  1690. * A return value of '-1' means that the attributes in @inode are
  1691. * more recent.
  1692. */
  1693. static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr,
  1694. const struct inode *inode)
  1695. {
  1696. s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode);
  1697. if (diff > 0)
  1698. return 1;
  1699. return diff == 0 ? 0 : -1;
  1700. }
  1701. /**
  1702. * nfs_inode_attrs_cmp_strict_monotonic - compare attributes
  1703. * @fattr: attributes
  1704. * @inode: pointer to inode
  1705. *
  1706. * Attempt to divine whether or not an RPC call reply carrying stale
  1707. * attributes got scheduled after another call carrying updated ones.
  1708. *
  1709. * We assume that the server observes strictly monotonic semantics for
  1710. * the change attribute, so a larger value means that the attributes in
  1711. * @fattr are more recent, in which case the function returns the
  1712. * value '1'.
  1713. * A return value of '-1' means that the attributes in @inode are
  1714. * more recent or unchanged.
  1715. */
  1716. static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr,
  1717. const struct inode *inode)
  1718. {
  1719. return nfs_inode_attrs_cmp_monotonic(fattr, inode) > 0 ? 1 : -1;
  1720. }
  1721. /**
  1722. * nfs_inode_attrs_cmp - compare attributes
  1723. * @fattr: attributes
  1724. * @inode: pointer to inode
  1725. *
  1726. * This function returns '1' if it thinks the attributes in @fattr are
  1727. * more recent than the ones cached in @inode. It returns '-1' if
  1728. * the attributes in @inode are more recent than the ones in @fattr,
  1729. * and it returns 0 if not sure.
  1730. */
  1731. static int nfs_inode_attrs_cmp(const struct nfs_fattr *fattr,
  1732. const struct inode *inode)
  1733. {
  1734. if (nfs_inode_attrs_cmp_generic(fattr, inode) > 0)
  1735. return 1;
  1736. switch (NFS_SERVER(inode)->change_attr_type) {
  1737. case NFS4_CHANGE_TYPE_IS_UNDEFINED:
  1738. break;
  1739. case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
  1740. if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
  1741. break;
  1742. return nfs_inode_attrs_cmp_monotonic(fattr, inode);
  1743. default:
  1744. if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
  1745. break;
  1746. return nfs_inode_attrs_cmp_strict_monotonic(fattr, inode);
  1747. }
  1748. return 0;
  1749. }
  1750. /**
  1751. * nfs_inode_finish_partial_attr_update - complete a previous inode update
  1752. * @fattr: attributes
  1753. * @inode: pointer to inode
  1754. *
  1755. * Returns '1' if the last attribute update left the inode cached
  1756. * attributes in a partially unrevalidated state, and @fattr
  1757. * matches the change attribute of that partial update.
  1758. * Otherwise returns '0'.
  1759. */
  1760. static int nfs_inode_finish_partial_attr_update(const struct nfs_fattr *fattr,
  1761. const struct inode *inode)
  1762. {
  1763. const unsigned long check_valid =
  1764. NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
  1765. NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
  1766. NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_OTHER |
  1767. NFS_INO_INVALID_NLINK | NFS_INO_INVALID_BTIME;
  1768. unsigned long cache_validity = NFS_I(inode)->cache_validity;
  1769. enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type;
  1770. if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED &&
  1771. !(cache_validity & NFS_INO_INVALID_CHANGE) &&
  1772. (cache_validity & check_valid) != 0 &&
  1773. (fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1774. nfs_inode_attrs_cmp_monotonic(fattr, inode) == 0)
  1775. return 1;
  1776. return 0;
  1777. }
  1778. static void nfs_ooo_merge(struct nfs_inode *nfsi,
  1779. u64 start, u64 end)
  1780. {
  1781. int i, cnt;
  1782. if (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)
  1783. /* No point merging anything */
  1784. return;
  1785. if (!nfsi->ooo) {
  1786. nfsi->ooo = kmalloc_obj(*nfsi->ooo, GFP_ATOMIC);
  1787. if (!nfsi->ooo) {
  1788. nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
  1789. return;
  1790. }
  1791. nfsi->ooo->cnt = 0;
  1792. }
  1793. /* add this range, merging if possible */
  1794. cnt = nfsi->ooo->cnt;
  1795. for (i = 0; i < cnt; i++) {
  1796. if (end == nfsi->ooo->gap[i].start)
  1797. end = nfsi->ooo->gap[i].end;
  1798. else if (start == nfsi->ooo->gap[i].end)
  1799. start = nfsi->ooo->gap[i].start;
  1800. else
  1801. continue;
  1802. /* Remove 'i' from table and loop to insert the new range */
  1803. cnt -= 1;
  1804. nfsi->ooo->gap[i] = nfsi->ooo->gap[cnt];
  1805. i = -1;
  1806. }
  1807. if (start != end) {
  1808. if (cnt >= ARRAY_SIZE(nfsi->ooo->gap)) {
  1809. nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
  1810. kfree(nfsi->ooo);
  1811. nfsi->ooo = NULL;
  1812. return;
  1813. }
  1814. nfsi->ooo->gap[cnt].start = start;
  1815. nfsi->ooo->gap[cnt].end = end;
  1816. cnt += 1;
  1817. }
  1818. nfsi->ooo->cnt = cnt;
  1819. }
  1820. static void nfs_ooo_record(struct nfs_inode *nfsi,
  1821. struct nfs_fattr *fattr)
  1822. {
  1823. /* This reply was out-of-order, so record in the
  1824. * pre/post change id, possibly cancelling
  1825. * gaps created when iversion was jumpped forward.
  1826. */
  1827. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) &&
  1828. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE))
  1829. nfs_ooo_merge(nfsi,
  1830. fattr->change_attr,
  1831. fattr->pre_change_attr);
  1832. }
  1833. static int nfs_refresh_inode_locked(struct inode *inode,
  1834. struct nfs_fattr *fattr)
  1835. {
  1836. int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
  1837. int ret = 0;
  1838. trace_nfs_refresh_inode_enter(inode);
  1839. if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode))
  1840. ret = nfs_update_inode(inode, fattr);
  1841. else {
  1842. nfs_ooo_record(NFS_I(inode), fattr);
  1843. if (attr_cmp == 0)
  1844. ret = nfs_check_inode_attributes(inode, fattr);
  1845. }
  1846. trace_nfs_refresh_inode_exit(inode, ret);
  1847. return ret;
  1848. }
  1849. /**
  1850. * nfs_refresh_inode - try to update the inode attribute cache
  1851. * @inode: pointer to inode
  1852. * @fattr: updated attributes
  1853. *
  1854. * Check that an RPC call that returned attributes has not overlapped with
  1855. * other recent updates of the inode metadata, then decide whether it is
  1856. * safe to do a full update of the inode attributes, or whether just to
  1857. * call nfs_check_inode_attributes.
  1858. */
  1859. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1860. {
  1861. int status;
  1862. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1863. return 0;
  1864. spin_lock(&inode->i_lock);
  1865. status = nfs_refresh_inode_locked(inode, fattr);
  1866. spin_unlock(&inode->i_lock);
  1867. return status;
  1868. }
  1869. EXPORT_SYMBOL_GPL(nfs_refresh_inode);
  1870. static int nfs_post_op_update_inode_locked(struct inode *inode,
  1871. struct nfs_fattr *fattr, unsigned int invalid)
  1872. {
  1873. if (S_ISDIR(inode->i_mode))
  1874. invalid |= NFS_INO_INVALID_DATA;
  1875. nfs_set_cache_invalid(inode, invalid);
  1876. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1877. return 0;
  1878. return nfs_refresh_inode_locked(inode, fattr);
  1879. }
  1880. /**
  1881. * nfs_post_op_update_inode - try to update the inode attribute cache
  1882. * @inode: pointer to inode
  1883. * @fattr: updated attributes
  1884. *
  1885. * After an operation that has changed the inode metadata, mark the
  1886. * attribute cache as being invalid, then try to update it.
  1887. *
  1888. * NB: if the server didn't return any post op attributes, this
  1889. * function will force the retrieval of attributes before the next
  1890. * NFS request. Thus it should be used only for operations that
  1891. * are expected to change one or more attributes, to avoid
  1892. * unnecessary NFS requests and trips through nfs_update_inode().
  1893. */
  1894. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1895. {
  1896. int status;
  1897. spin_lock(&inode->i_lock);
  1898. nfs_fattr_set_barrier(fattr);
  1899. status = nfs_post_op_update_inode_locked(inode, fattr,
  1900. NFS_INO_INVALID_CHANGE
  1901. | NFS_INO_INVALID_CTIME
  1902. | NFS_INO_REVAL_FORCED);
  1903. spin_unlock(&inode->i_lock);
  1904. return status;
  1905. }
  1906. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
  1907. /**
  1908. * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
  1909. * @inode: pointer to inode
  1910. * @fattr: updated attributes
  1911. *
  1912. * After an operation that has changed the inode metadata, mark the
  1913. * attribute cache as being invalid, then try to update it. Fake up
  1914. * weak cache consistency data, if none exist.
  1915. *
  1916. * This function is mainly designed to be used by the ->write_done() functions.
  1917. */
  1918. int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
  1919. {
  1920. int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
  1921. int status;
  1922. /* Don't do a WCC update if these attributes are already stale */
  1923. if (attr_cmp < 0)
  1924. return 0;
  1925. if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) {
  1926. /* Record the pre/post change info before clearing PRECHANGE */
  1927. nfs_ooo_record(NFS_I(inode), fattr);
  1928. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1929. | NFS_ATTR_FATTR_PRESIZE
  1930. | NFS_ATTR_FATTR_PREMTIME
  1931. | NFS_ATTR_FATTR_PRECTIME);
  1932. goto out_noforce;
  1933. }
  1934. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1935. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1936. fattr->pre_change_attr = inode_peek_iversion_raw(inode);
  1937. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1938. }
  1939. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1940. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1941. fattr->pre_ctime = inode_get_ctime(inode);
  1942. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1943. }
  1944. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1945. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1946. fattr->pre_mtime = inode_get_mtime(inode);
  1947. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1948. }
  1949. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1950. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1951. fattr->pre_size = i_size_read(inode);
  1952. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1953. }
  1954. out_noforce:
  1955. status = nfs_post_op_update_inode_locked(inode, fattr,
  1956. NFS_INO_INVALID_CHANGE
  1957. | NFS_INO_INVALID_CTIME
  1958. | NFS_INO_INVALID_MTIME
  1959. | NFS_INO_INVALID_BLOCKS);
  1960. return status;
  1961. }
  1962. /**
  1963. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1964. * @inode: pointer to inode
  1965. * @fattr: updated attributes
  1966. *
  1967. * After an operation that has changed the inode metadata, mark the
  1968. * attribute cache as being invalid, then try to update it. Fake up
  1969. * weak cache consistency data, if none exist.
  1970. *
  1971. * This function is mainly designed to be used by the ->write_done() functions.
  1972. */
  1973. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1974. {
  1975. int status;
  1976. spin_lock(&inode->i_lock);
  1977. nfs_fattr_set_barrier(fattr);
  1978. status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1979. spin_unlock(&inode->i_lock);
  1980. return status;
  1981. }
  1982. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
  1983. /*
  1984. * Many nfs protocol calls return the new file attributes after
  1985. * an operation. Here we update the inode to reflect the state
  1986. * of the server's inode.
  1987. *
  1988. * This is a bit tricky because we have to make sure all dirty pages
  1989. * have been sent off to the server before calling invalidate_inode_pages.
  1990. * To make sure no other process adds more write requests while we try
  1991. * our best to flush them, we make them sleep during the attribute refresh.
  1992. *
  1993. * A very similar scenario holds for the dir cache.
  1994. */
  1995. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1996. {
  1997. struct nfs_server *server = NFS_SERVER(inode);
  1998. struct nfs_inode *nfsi = NFS_I(inode);
  1999. loff_t cur_isize, new_isize;
  2000. u64 fattr_supported = server->fattr_valid;
  2001. unsigned long invalid = 0;
  2002. unsigned long now = jiffies;
  2003. unsigned long save_cache_validity;
  2004. bool have_writers = nfs_file_has_buffered_writers(nfsi);
  2005. bool cache_revalidated = true;
  2006. bool attr_changed = false;
  2007. bool have_delegation;
  2008. dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%llx)\n",
  2009. __func__, inode->i_sb->s_id, inode->i_ino,
  2010. nfs_display_fhandle_hash(NFS_FH(inode)),
  2011. icount_read(inode), fattr->valid);
  2012. if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
  2013. /* Only a mounted-on-fileid? Just exit */
  2014. if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
  2015. return 0;
  2016. /* Has the inode gone and changed behind our back? */
  2017. } else if (nfsi->fileid != fattr->fileid) {
  2018. /* Is this perhaps the mounted-on fileid? */
  2019. if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
  2020. nfsi->fileid == fattr->mounted_on_fileid)
  2021. return 0;
  2022. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  2023. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  2024. NFS_SERVER(inode)->nfs_client->cl_hostname,
  2025. inode->i_sb->s_id, (long long)nfsi->fileid,
  2026. (long long)fattr->fileid);
  2027. goto out_err;
  2028. }
  2029. /*
  2030. * Make sure the inode's type hasn't changed.
  2031. */
  2032. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
  2033. /*
  2034. * Big trouble! The inode has become a different object.
  2035. */
  2036. printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
  2037. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  2038. goto out_err;
  2039. }
  2040. /* Update the fsid? */
  2041. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  2042. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  2043. !IS_AUTOMOUNT(inode))
  2044. server->fsid = fattr->fsid;
  2045. /* Save the delegation state before clearing cache_validity */
  2046. have_delegation = nfs_have_delegated_attributes(inode);
  2047. /*
  2048. * Update the read time so we don't revalidate too often.
  2049. */
  2050. nfsi->read_cache_jiffies = fattr->time_start;
  2051. /* Fix up any delegated attributes in the struct nfs_fattr */
  2052. nfs_fattr_fixup_delegated(inode, fattr);
  2053. save_cache_validity = nfsi->cache_validity;
  2054. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  2055. | NFS_INO_INVALID_ATIME
  2056. | NFS_INO_REVAL_FORCED
  2057. | NFS_INO_INVALID_BLOCKS);
  2058. /* Do atomic weak cache consistency updates */
  2059. nfs_wcc_update_inode(inode, fattr);
  2060. if (pnfs_layoutcommit_outstanding(inode)) {
  2061. nfsi->cache_validity |=
  2062. save_cache_validity &
  2063. (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
  2064. NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
  2065. NFS_INO_INVALID_BLOCKS);
  2066. cache_revalidated = false;
  2067. }
  2068. /* More cache consistency checks */
  2069. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  2070. if (!have_writers && nfsi->ooo && nfsi->ooo->cnt == 1 &&
  2071. nfsi->ooo->gap[0].end == inode_peek_iversion_raw(inode)) {
  2072. /* There is one remaining gap that hasn't been
  2073. * merged into iversion - do that now.
  2074. */
  2075. inode_set_iversion_raw(inode, nfsi->ooo->gap[0].start);
  2076. kfree(nfsi->ooo);
  2077. nfsi->ooo = NULL;
  2078. }
  2079. if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
  2080. /* Could it be a race with writeback? */
  2081. if (!(have_writers || have_delegation)) {
  2082. invalid |= NFS_INO_INVALID_DATA
  2083. | NFS_INO_INVALID_ACCESS
  2084. | NFS_INO_INVALID_ACL
  2085. | NFS_INO_INVALID_XATTR;
  2086. /* Force revalidate of all attributes */
  2087. save_cache_validity |= NFS_INO_INVALID_CTIME
  2088. | NFS_INO_INVALID_MTIME
  2089. | NFS_INO_INVALID_SIZE
  2090. | NFS_INO_INVALID_BLOCKS
  2091. | NFS_INO_INVALID_NLINK
  2092. | NFS_INO_INVALID_MODE
  2093. | NFS_INO_INVALID_OTHER
  2094. | NFS_INO_INVALID_BTIME;
  2095. if (S_ISDIR(inode->i_mode))
  2096. nfs_force_lookup_revalidate(inode);
  2097. attr_changed = true;
  2098. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  2099. inode->i_sb->s_id,
  2100. inode->i_ino);
  2101. } else if (!have_delegation) {
  2102. nfs_ooo_record(nfsi, fattr);
  2103. nfs_ooo_merge(nfsi, inode_peek_iversion_raw(inode),
  2104. fattr->change_attr);
  2105. }
  2106. inode_set_iversion_raw(inode, fattr->change_attr);
  2107. }
  2108. } else {
  2109. nfsi->cache_validity |=
  2110. save_cache_validity & NFS_INO_INVALID_CHANGE;
  2111. if (!have_delegation ||
  2112. (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0)
  2113. cache_revalidated = false;
  2114. }
  2115. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  2116. inode_set_mtime_to_ts(inode, fattr->mtime);
  2117. else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
  2118. nfsi->cache_validity |=
  2119. save_cache_validity & NFS_INO_INVALID_MTIME;
  2120. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  2121. inode_set_ctime_to_ts(inode, fattr->ctime);
  2122. else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
  2123. nfsi->cache_validity |=
  2124. save_cache_validity & NFS_INO_INVALID_CTIME;
  2125. if (fattr->valid & NFS_ATTR_FATTR_BTIME)
  2126. nfsi->btime = fattr->btime;
  2127. else if (fattr_supported & NFS_ATTR_FATTR_BTIME)
  2128. nfsi->cache_validity |=
  2129. save_cache_validity & NFS_INO_INVALID_BTIME;
  2130. /* Check if our cached file size is stale */
  2131. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  2132. new_isize = nfs_size_to_loff_t(fattr->size);
  2133. cur_isize = i_size_read(inode);
  2134. if (new_isize != cur_isize && !have_delegation) {
  2135. /* Do we perhaps have any outstanding writes, or has
  2136. * the file grown beyond our last write? */
  2137. if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
  2138. trace_nfs_size_update(inode, new_isize);
  2139. i_size_write(inode, new_isize);
  2140. if (!have_writers)
  2141. invalid |= NFS_INO_INVALID_DATA;
  2142. }
  2143. }
  2144. if (new_isize == 0 &&
  2145. !(fattr->valid & (NFS_ATTR_FATTR_SPACE_USED |
  2146. NFS_ATTR_FATTR_BLOCKS_USED))) {
  2147. fattr->du.nfs3.used = 0;
  2148. fattr->valid |= NFS_ATTR_FATTR_SPACE_USED;
  2149. }
  2150. } else
  2151. nfsi->cache_validity |=
  2152. save_cache_validity & NFS_INO_INVALID_SIZE;
  2153. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  2154. inode_set_atime_to_ts(inode, fattr->atime);
  2155. else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
  2156. nfsi->cache_validity |=
  2157. save_cache_validity & NFS_INO_INVALID_ATIME;
  2158. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  2159. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  2160. umode_t newmode = inode->i_mode & S_IFMT;
  2161. newmode |= fattr->mode & S_IALLUGO;
  2162. inode->i_mode = newmode;
  2163. invalid |= NFS_INO_INVALID_ACCESS
  2164. | NFS_INO_INVALID_ACL;
  2165. }
  2166. } else if (fattr_supported & NFS_ATTR_FATTR_MODE)
  2167. nfsi->cache_validity |=
  2168. save_cache_validity & NFS_INO_INVALID_MODE;
  2169. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  2170. if (!uid_eq(inode->i_uid, fattr->uid)) {
  2171. invalid |= NFS_INO_INVALID_ACCESS
  2172. | NFS_INO_INVALID_ACL;
  2173. inode->i_uid = fattr->uid;
  2174. }
  2175. } else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
  2176. nfsi->cache_validity |=
  2177. save_cache_validity & NFS_INO_INVALID_OTHER;
  2178. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  2179. if (!gid_eq(inode->i_gid, fattr->gid)) {
  2180. invalid |= NFS_INO_INVALID_ACCESS
  2181. | NFS_INO_INVALID_ACL;
  2182. inode->i_gid = fattr->gid;
  2183. }
  2184. } else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
  2185. nfsi->cache_validity |=
  2186. save_cache_validity & NFS_INO_INVALID_OTHER;
  2187. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  2188. if (inode->i_nlink != fattr->nlink)
  2189. set_nlink(inode, fattr->nlink);
  2190. } else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
  2191. nfsi->cache_validity |=
  2192. save_cache_validity & NFS_INO_INVALID_NLINK;
  2193. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  2194. /*
  2195. * report the blocks in 512byte units
  2196. */
  2197. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  2198. } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED)
  2199. nfsi->cache_validity |=
  2200. save_cache_validity & NFS_INO_INVALID_BLOCKS;
  2201. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  2202. inode->i_blocks = fattr->du.nfs2.blocks;
  2203. else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED)
  2204. nfsi->cache_validity |=
  2205. save_cache_validity & NFS_INO_INVALID_BLOCKS;
  2206. /* Update attrtimeo value if we're out of the unstable period */
  2207. if (attr_changed) {
  2208. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  2209. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  2210. nfsi->attrtimeo_timestamp = now;
  2211. /* Set barrier to be more recent than all outstanding updates */
  2212. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  2213. } else {
  2214. if (cache_revalidated) {
  2215. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
  2216. nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  2217. nfsi->attrtimeo <<= 1;
  2218. if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
  2219. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  2220. }
  2221. nfsi->attrtimeo_timestamp = now;
  2222. }
  2223. /* Set the barrier to be more recent than this fattr */
  2224. if ((long)(fattr->gencount - nfsi->attr_gencount) > 0)
  2225. nfsi->attr_gencount = fattr->gencount;
  2226. }
  2227. /* Don't invalidate the data if we were to blame */
  2228. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  2229. || S_ISLNK(inode->i_mode)))
  2230. invalid &= ~NFS_INO_INVALID_DATA;
  2231. nfs_set_cache_invalid(inode, invalid);
  2232. return 0;
  2233. out_err:
  2234. /*
  2235. * No need to worry about unhashing the dentry, as the
  2236. * lookup validation will know that the inode is bad.
  2237. * (But we fall through to invalidate the caches.)
  2238. */
  2239. nfs_set_inode_stale_locked(inode);
  2240. return -ESTALE;
  2241. }
  2242. struct inode *nfs_alloc_inode(struct super_block *sb)
  2243. {
  2244. struct nfs_inode *nfsi;
  2245. nfsi = alloc_inode_sb(sb, nfs_inode_cachep, GFP_KERNEL);
  2246. if (!nfsi)
  2247. return NULL;
  2248. nfsi->flags = 0UL;
  2249. nfsi->cache_validity = 0UL;
  2250. nfsi->ooo = NULL;
  2251. #if IS_ENABLED(CONFIG_NFS_V4)
  2252. nfsi->nfs4_acl = NULL;
  2253. #endif /* CONFIG_NFS_V4 */
  2254. #ifdef CONFIG_NFS_V4_2
  2255. nfsi->xattr_cache = NULL;
  2256. #endif
  2257. nfs_netfs_inode_init(nfsi);
  2258. return &nfsi->vfs_inode;
  2259. }
  2260. EXPORT_SYMBOL_GPL(nfs_alloc_inode);
  2261. void nfs_free_inode(struct inode *inode)
  2262. {
  2263. kfree(NFS_I(inode)->ooo);
  2264. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  2265. }
  2266. EXPORT_SYMBOL_GPL(nfs_free_inode);
  2267. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  2268. {
  2269. #if IS_ENABLED(CONFIG_NFS_V4)
  2270. INIT_LIST_HEAD(&nfsi->open_states);
  2271. nfsi->delegation = NULL;
  2272. init_rwsem(&nfsi->rwsem);
  2273. nfsi->layout = NULL;
  2274. #endif
  2275. }
  2276. static void init_once(void *foo)
  2277. {
  2278. struct nfs_inode *nfsi = foo;
  2279. inode_init_once(&nfsi->vfs_inode);
  2280. INIT_LIST_HEAD(&nfsi->open_files);
  2281. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  2282. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  2283. nfs4_init_once(nfsi);
  2284. }
  2285. static int __init nfs_init_inodecache(void)
  2286. {
  2287. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  2288. sizeof(struct nfs_inode),
  2289. 0, (SLAB_RECLAIM_ACCOUNT|
  2290. SLAB_ACCOUNT),
  2291. init_once);
  2292. if (nfs_inode_cachep == NULL)
  2293. return -ENOMEM;
  2294. return 0;
  2295. }
  2296. static void nfs_destroy_inodecache(void)
  2297. {
  2298. /*
  2299. * Make sure all delayed rcu free inodes are flushed before we
  2300. * destroy cache.
  2301. */
  2302. rcu_barrier();
  2303. kmem_cache_destroy(nfs_inode_cachep);
  2304. }
  2305. struct workqueue_struct *nfslocaliod_workqueue;
  2306. struct workqueue_struct *nfsiod_workqueue;
  2307. EXPORT_SYMBOL_GPL(nfsiod_workqueue);
  2308. /*
  2309. * Destroy the nfsiod workqueues
  2310. */
  2311. static void nfsiod_stop(void)
  2312. {
  2313. struct workqueue_struct *wq;
  2314. wq = nfsiod_workqueue;
  2315. if (wq != NULL) {
  2316. nfsiod_workqueue = NULL;
  2317. destroy_workqueue(wq);
  2318. }
  2319. #if IS_ENABLED(CONFIG_NFS_LOCALIO)
  2320. wq = nfslocaliod_workqueue;
  2321. if (wq != NULL) {
  2322. nfslocaliod_workqueue = NULL;
  2323. destroy_workqueue(wq);
  2324. }
  2325. #endif /* CONFIG_NFS_LOCALIO */
  2326. }
  2327. /*
  2328. * Start the nfsiod workqueues
  2329. */
  2330. static int nfsiod_start(void)
  2331. {
  2332. dprintk("RPC: creating workqueue nfsiod\n");
  2333. nfsiod_workqueue = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  2334. if (nfsiod_workqueue == NULL)
  2335. return -ENOMEM;
  2336. #if IS_ENABLED(CONFIG_NFS_LOCALIO)
  2337. /*
  2338. * localio writes need to use a normal (non-memreclaim) workqueue.
  2339. * When we start getting low on space, XFS goes and calls flush_work() on
  2340. * a non-memreclaim work queue, which causes a priority inversion problem.
  2341. */
  2342. dprintk("RPC: creating workqueue nfslocaliod\n");
  2343. nfslocaliod_workqueue = alloc_workqueue("nfslocaliod", WQ_UNBOUND, 0);
  2344. if (unlikely(nfslocaliod_workqueue == NULL)) {
  2345. nfsiod_stop();
  2346. return -ENOMEM;
  2347. }
  2348. #endif /* CONFIG_NFS_LOCALIO */
  2349. return 0;
  2350. }
  2351. unsigned int nfs_net_id;
  2352. EXPORT_SYMBOL_GPL(nfs_net_id);
  2353. static int nfs_net_init(struct net *net)
  2354. {
  2355. struct nfs_net *nn = net_generic(net, nfs_net_id);
  2356. int err;
  2357. nfs_clients_init(net);
  2358. if (!rpc_proc_register(net, &nn->rpcstats)) {
  2359. err = -ENOMEM;
  2360. goto err_proc_rpc;
  2361. }
  2362. err = nfs_fs_proc_net_init(net);
  2363. if (err)
  2364. goto err_proc_nfs;
  2365. return 0;
  2366. err_proc_nfs:
  2367. rpc_proc_unregister(net, "nfs");
  2368. err_proc_rpc:
  2369. nfs_clients_exit(net);
  2370. return err;
  2371. }
  2372. static void nfs_net_exit(struct net *net)
  2373. {
  2374. rpc_proc_unregister(net, "nfs");
  2375. nfs_fs_proc_net_exit(net);
  2376. nfs_clients_exit(net);
  2377. }
  2378. static struct pernet_operations nfs_net_ops = {
  2379. .init = nfs_net_init,
  2380. .exit = nfs_net_exit,
  2381. .id = &nfs_net_id,
  2382. .size = sizeof(struct nfs_net),
  2383. };
  2384. #ifdef CONFIG_KEYS
  2385. static struct key *nfs_keyring;
  2386. static int __init nfs_init_keyring(void)
  2387. {
  2388. nfs_keyring = keyring_alloc(".nfs",
  2389. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
  2390. current_cred(),
  2391. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  2392. (KEY_USR_ALL & ~KEY_USR_SETATTR),
  2393. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  2394. return PTR_ERR_OR_ZERO(nfs_keyring);
  2395. }
  2396. static void nfs_exit_keyring(void)
  2397. {
  2398. key_put(nfs_keyring);
  2399. }
  2400. #else
  2401. static inline int nfs_init_keyring(void)
  2402. {
  2403. return 0;
  2404. }
  2405. static inline void nfs_exit_keyring(void)
  2406. {
  2407. }
  2408. #endif /* CONFIG_KEYS */
  2409. /*
  2410. * Initialize NFS
  2411. */
  2412. static int __init init_nfs_fs(void)
  2413. {
  2414. int err;
  2415. err = nfs_init_keyring();
  2416. if (err)
  2417. return err;
  2418. err = nfs_sysfs_init();
  2419. if (err < 0)
  2420. goto out10;
  2421. err = register_pernet_subsys(&nfs_net_ops);
  2422. if (err < 0)
  2423. goto out9;
  2424. err = nfsiod_start();
  2425. if (err)
  2426. goto out7;
  2427. err = nfs_fs_proc_init();
  2428. if (err)
  2429. goto out6;
  2430. err = nfs_init_nfspagecache();
  2431. if (err)
  2432. goto out5;
  2433. err = nfs_init_inodecache();
  2434. if (err)
  2435. goto out4;
  2436. err = nfs_init_readpagecache();
  2437. if (err)
  2438. goto out3;
  2439. err = nfs_init_writepagecache();
  2440. if (err)
  2441. goto out2;
  2442. err = nfs_init_directcache();
  2443. if (err)
  2444. goto out1;
  2445. err = register_nfs_fs();
  2446. if (err)
  2447. goto out0;
  2448. return 0;
  2449. out0:
  2450. nfs_destroy_directcache();
  2451. out1:
  2452. nfs_destroy_writepagecache();
  2453. out2:
  2454. nfs_destroy_readpagecache();
  2455. out3:
  2456. nfs_destroy_inodecache();
  2457. out4:
  2458. nfs_destroy_nfspagecache();
  2459. out5:
  2460. nfs_fs_proc_exit();
  2461. out6:
  2462. nfsiod_stop();
  2463. out7:
  2464. unregister_pernet_subsys(&nfs_net_ops);
  2465. out9:
  2466. nfs_sysfs_exit();
  2467. out10:
  2468. nfs_exit_keyring();
  2469. return err;
  2470. }
  2471. static void __exit exit_nfs_fs(void)
  2472. {
  2473. nfs_destroy_directcache();
  2474. nfs_destroy_writepagecache();
  2475. nfs_destroy_readpagecache();
  2476. nfs_destroy_inodecache();
  2477. nfs_destroy_nfspagecache();
  2478. unregister_pernet_subsys(&nfs_net_ops);
  2479. unregister_nfs_fs();
  2480. nfs_fs_proc_exit();
  2481. nfsiod_stop();
  2482. nfs_sysfs_exit();
  2483. nfs_exit_keyring();
  2484. }
  2485. /* Not quite true; I just maintain it */
  2486. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  2487. MODULE_DESCRIPTION("NFS client support");
  2488. MODULE_LICENSE("GPL");
  2489. module_param(enable_ino64, bool, 0644);
  2490. module_init(init_nfs_fs)
  2491. module_exit(exit_nfs_fs)