inode.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. /*
  2. * linux/fs/nfs/inode.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs inode and superblock handling functions
  7. *
  8. * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
  9. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10. *
  11. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12. * J.S.Peatfield@damtp.cam.ac.uk
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/sched/signal.h>
  18. #include <linux/time.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/string.h>
  22. #include <linux/stat.h>
  23. #include <linux/errno.h>
  24. #include <linux/unistd.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #include <linux/sunrpc/stats.h>
  27. #include <linux/sunrpc/metrics.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/nfs4_mount.h>
  31. #include <linux/lockd/bind.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/mount.h>
  34. #include <linux/vfs.h>
  35. #include <linux/inet.h>
  36. #include <linux/nfs_xdr.h>
  37. #include <linux/slab.h>
  38. #include <linux/compat.h>
  39. #include <linux/freezer.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/iversion.h>
  42. #include "nfs4_fs.h"
  43. #include "callback.h"
  44. #include "delegation.h"
  45. #include "iostat.h"
  46. #include "internal.h"
  47. #include "fscache.h"
  48. #include "pnfs.h"
  49. #include "nfs.h"
  50. #include "netns.h"
  51. #include "nfstrace.h"
  52. #define NFSDBG_FACILITY NFSDBG_VFS
  53. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  54. /* Default is to see 64-bit inode numbers */
  55. static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  56. static void nfs_invalidate_inode(struct inode *);
  57. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  58. static struct kmem_cache * nfs_inode_cachep;
  59. static inline unsigned long
  60. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  61. {
  62. return nfs_fileid_to_ino_t(fattr->fileid);
  63. }
  64. static int nfs_wait_killable(int mode)
  65. {
  66. freezable_schedule_unsafe();
  67. if (signal_pending_state(mode, current))
  68. return -ERESTARTSYS;
  69. return 0;
  70. }
  71. int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
  72. {
  73. return nfs_wait_killable(mode);
  74. }
  75. EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
  76. /**
  77. * nfs_compat_user_ino64 - returns the user-visible inode number
  78. * @fileid: 64-bit fileid
  79. *
  80. * This function returns a 32-bit inode number if the boot parameter
  81. * nfs.enable_ino64 is zero.
  82. */
  83. u64 nfs_compat_user_ino64(u64 fileid)
  84. {
  85. #ifdef CONFIG_COMPAT
  86. compat_ulong_t ino;
  87. #else
  88. unsigned long ino;
  89. #endif
  90. if (enable_ino64)
  91. return fileid;
  92. ino = fileid;
  93. if (sizeof(ino) < sizeof(fileid))
  94. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  95. return ino;
  96. }
  97. int nfs_drop_inode(struct inode *inode)
  98. {
  99. return NFS_STALE(inode) || generic_drop_inode(inode);
  100. }
  101. EXPORT_SYMBOL_GPL(nfs_drop_inode);
  102. void nfs_clear_inode(struct inode *inode)
  103. {
  104. /*
  105. * The following should never happen...
  106. */
  107. WARN_ON_ONCE(nfs_have_writebacks(inode));
  108. WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
  109. nfs_zap_acl_cache(inode);
  110. nfs_access_zap_cache(inode);
  111. nfs_fscache_clear_inode(inode);
  112. }
  113. EXPORT_SYMBOL_GPL(nfs_clear_inode);
  114. void nfs_evict_inode(struct inode *inode)
  115. {
  116. truncate_inode_pages_final(&inode->i_data);
  117. clear_inode(inode);
  118. nfs_clear_inode(inode);
  119. }
  120. int nfs_sync_inode(struct inode *inode)
  121. {
  122. inode_dio_wait(inode);
  123. return nfs_wb_all(inode);
  124. }
  125. EXPORT_SYMBOL_GPL(nfs_sync_inode);
  126. /**
  127. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  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_invalid_delegated(struct inode *inode, unsigned long flags)
  144. {
  145. unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  146. /* Special case for the pagecache or access cache */
  147. if (flags == NFS_INO_REVAL_PAGECACHE &&
  148. !(cache_validity & NFS_INO_REVAL_FORCED))
  149. return false;
  150. return (cache_validity & flags) != 0;
  151. }
  152. static bool nfs_check_cache_invalid_not_delegated(struct inode *inode, unsigned long flags)
  153. {
  154. unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  155. if ((cache_validity & flags) != 0)
  156. return true;
  157. if (nfs_attribute_timeout(inode))
  158. return true;
  159. return false;
  160. }
  161. bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
  162. {
  163. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  164. return nfs_check_cache_invalid_delegated(inode, flags);
  165. return nfs_check_cache_invalid_not_delegated(inode, flags);
  166. }
  167. static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
  168. {
  169. struct nfs_inode *nfsi = NFS_I(inode);
  170. bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
  171. if (have_delegation) {
  172. if (!(flags & NFS_INO_REVAL_FORCED))
  173. flags &= ~NFS_INO_INVALID_OTHER;
  174. flags &= ~(NFS_INO_INVALID_CHANGE
  175. | NFS_INO_INVALID_SIZE
  176. | NFS_INO_REVAL_PAGECACHE);
  177. }
  178. if (inode->i_mapping->nrpages == 0)
  179. flags &= ~NFS_INO_INVALID_DATA;
  180. nfsi->cache_validity |= flags;
  181. if (flags & NFS_INO_INVALID_DATA)
  182. nfs_fscache_invalidate(inode);
  183. }
  184. /*
  185. * Invalidate the local caches
  186. */
  187. static void nfs_zap_caches_locked(struct inode *inode)
  188. {
  189. struct nfs_inode *nfsi = NFS_I(inode);
  190. int mode = inode->i_mode;
  191. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  192. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  193. nfsi->attrtimeo_timestamp = jiffies;
  194. memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
  195. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  196. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
  197. | NFS_INO_INVALID_DATA
  198. | NFS_INO_INVALID_ACCESS
  199. | NFS_INO_INVALID_ACL
  200. | NFS_INO_REVAL_PAGECACHE);
  201. } else
  202. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
  203. | NFS_INO_INVALID_ACCESS
  204. | NFS_INO_INVALID_ACL
  205. | NFS_INO_REVAL_PAGECACHE);
  206. nfs_zap_label_cache_locked(nfsi);
  207. }
  208. void nfs_zap_caches(struct inode *inode)
  209. {
  210. spin_lock(&inode->i_lock);
  211. nfs_zap_caches_locked(inode);
  212. spin_unlock(&inode->i_lock);
  213. }
  214. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  215. {
  216. if (mapping->nrpages != 0) {
  217. spin_lock(&inode->i_lock);
  218. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  219. spin_unlock(&inode->i_lock);
  220. }
  221. }
  222. void nfs_zap_acl_cache(struct inode *inode)
  223. {
  224. void (*clear_acl_cache)(struct inode *);
  225. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  226. if (clear_acl_cache != NULL)
  227. clear_acl_cache(inode);
  228. spin_lock(&inode->i_lock);
  229. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  230. spin_unlock(&inode->i_lock);
  231. }
  232. EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
  233. void nfs_invalidate_atime(struct inode *inode)
  234. {
  235. spin_lock(&inode->i_lock);
  236. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  237. spin_unlock(&inode->i_lock);
  238. }
  239. EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
  240. /*
  241. * Invalidate, but do not unhash, the inode.
  242. * NB: must be called with inode->i_lock held!
  243. */
  244. static void nfs_invalidate_inode(struct inode *inode)
  245. {
  246. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  247. nfs_zap_caches_locked(inode);
  248. }
  249. struct nfs_find_desc {
  250. struct nfs_fh *fh;
  251. struct nfs_fattr *fattr;
  252. };
  253. /*
  254. * In NFSv3 we can have 64bit inode numbers. In order to support
  255. * this, and re-exported directories (also seen in NFSv2)
  256. * we are forced to allow 2 different inodes to have the same
  257. * i_ino.
  258. */
  259. static int
  260. nfs_find_actor(struct inode *inode, void *opaque)
  261. {
  262. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  263. struct nfs_fh *fh = desc->fh;
  264. struct nfs_fattr *fattr = desc->fattr;
  265. if (NFS_FILEID(inode) != fattr->fileid)
  266. return 0;
  267. if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
  268. return 0;
  269. if (nfs_compare_fh(NFS_FH(inode), fh))
  270. return 0;
  271. if (is_bad_inode(inode) || NFS_STALE(inode))
  272. return 0;
  273. return 1;
  274. }
  275. static int
  276. nfs_init_locked(struct inode *inode, void *opaque)
  277. {
  278. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  279. struct nfs_fattr *fattr = desc->fattr;
  280. set_nfs_fileid(inode, fattr->fileid);
  281. inode->i_mode = fattr->mode;
  282. nfs_copy_fh(NFS_FH(inode), desc->fh);
  283. return 0;
  284. }
  285. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  286. static void nfs_clear_label_invalid(struct inode *inode)
  287. {
  288. spin_lock(&inode->i_lock);
  289. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
  290. spin_unlock(&inode->i_lock);
  291. }
  292. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
  293. struct nfs4_label *label)
  294. {
  295. int error;
  296. if (label == NULL)
  297. return;
  298. if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
  299. error = security_inode_notifysecctx(inode, label->label,
  300. label->len);
  301. if (error)
  302. printk(KERN_ERR "%s() %s %d "
  303. "security_inode_notifysecctx() %d\n",
  304. __func__,
  305. (char *)label->label,
  306. label->len, error);
  307. nfs_clear_label_invalid(inode);
  308. }
  309. }
  310. struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
  311. {
  312. struct nfs4_label *label = NULL;
  313. int minor_version = server->nfs_client->cl_minorversion;
  314. if (minor_version < 2)
  315. return label;
  316. if (!(server->caps & NFS_CAP_SECURITY_LABEL))
  317. return label;
  318. label = kzalloc(sizeof(struct nfs4_label), flags);
  319. if (label == NULL)
  320. return ERR_PTR(-ENOMEM);
  321. label->label = kzalloc(NFS4_MAXLABELLEN, flags);
  322. if (label->label == NULL) {
  323. kfree(label);
  324. return ERR_PTR(-ENOMEM);
  325. }
  326. label->len = NFS4_MAXLABELLEN;
  327. return label;
  328. }
  329. EXPORT_SYMBOL_GPL(nfs4_label_alloc);
  330. #else
  331. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
  332. struct nfs4_label *label)
  333. {
  334. }
  335. #endif
  336. EXPORT_SYMBOL_GPL(nfs_setsecurity);
  337. /* Search for inode identified by fh, fileid and i_mode in inode cache. */
  338. struct inode *
  339. nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
  340. {
  341. struct nfs_find_desc desc = {
  342. .fh = fh,
  343. .fattr = fattr,
  344. };
  345. struct inode *inode;
  346. unsigned long hash;
  347. if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
  348. !(fattr->valid & NFS_ATTR_FATTR_TYPE))
  349. return NULL;
  350. hash = nfs_fattr_to_ino_t(fattr);
  351. inode = ilookup5(sb, hash, nfs_find_actor, &desc);
  352. dprintk("%s: returning %p\n", __func__, inode);
  353. return inode;
  354. }
  355. /*
  356. * This is our front-end to iget that looks up inodes by file handle
  357. * instead of inode number.
  358. */
  359. struct inode *
  360. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
  361. {
  362. struct nfs_find_desc desc = {
  363. .fh = fh,
  364. .fattr = fattr
  365. };
  366. struct inode *inode = ERR_PTR(-ENOENT);
  367. unsigned long hash;
  368. nfs_attr_check_mountpoint(sb, fattr);
  369. if (nfs_attr_use_mounted_on_fileid(fattr))
  370. fattr->fileid = fattr->mounted_on_fileid;
  371. else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
  372. goto out_no_inode;
  373. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  374. goto out_no_inode;
  375. hash = nfs_fattr_to_ino_t(fattr);
  376. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  377. if (inode == NULL) {
  378. inode = ERR_PTR(-ENOMEM);
  379. goto out_no_inode;
  380. }
  381. if (inode->i_state & I_NEW) {
  382. struct nfs_inode *nfsi = NFS_I(inode);
  383. unsigned long now = jiffies;
  384. /* We set i_ino for the few things that still rely on it,
  385. * such as stat(2) */
  386. inode->i_ino = hash;
  387. /* We can't support update_atime(), since the server will reset it */
  388. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  389. inode->i_mode = fattr->mode;
  390. nfsi->cache_validity = 0;
  391. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  392. && nfs_server_capable(inode, NFS_CAP_MODE))
  393. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  394. /* Why so? Because we want revalidate for devices/FIFOs, and
  395. * that's precisely what we have in nfs_file_inode_operations.
  396. */
  397. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  398. if (S_ISREG(inode->i_mode)) {
  399. inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
  400. inode->i_data.a_ops = &nfs_file_aops;
  401. } else if (S_ISDIR(inode->i_mode)) {
  402. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  403. inode->i_fop = &nfs_dir_operations;
  404. inode->i_data.a_ops = &nfs_dir_aops;
  405. /* Deal with crossing mountpoints */
  406. if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
  407. fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  408. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  409. inode->i_op = &nfs_referral_inode_operations;
  410. else
  411. inode->i_op = &nfs_mountpoint_inode_operations;
  412. inode->i_fop = NULL;
  413. inode->i_flags |= S_AUTOMOUNT;
  414. }
  415. } else if (S_ISLNK(inode->i_mode)) {
  416. inode->i_op = &nfs_symlink_inode_operations;
  417. inode_nohighmem(inode);
  418. } else
  419. init_special_inode(inode, inode->i_mode, fattr->rdev);
  420. memset(&inode->i_atime, 0, sizeof(inode->i_atime));
  421. memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
  422. memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
  423. inode_set_iversion_raw(inode, 0);
  424. inode->i_size = 0;
  425. clear_nlink(inode);
  426. inode->i_uid = make_kuid(&init_user_ns, -2);
  427. inode->i_gid = make_kgid(&init_user_ns, -2);
  428. inode->i_blocks = 0;
  429. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  430. nfsi->write_io = 0;
  431. nfsi->read_io = 0;
  432. nfsi->read_cache_jiffies = fattr->time_start;
  433. nfsi->attr_gencount = fattr->gencount;
  434. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  435. inode->i_atime = timespec_to_timespec64(fattr->atime);
  436. else if (nfs_server_capable(inode, NFS_CAP_ATIME))
  437. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  438. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  439. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  440. else if (nfs_server_capable(inode, NFS_CAP_MTIME))
  441. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  442. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  443. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  444. else if (nfs_server_capable(inode, NFS_CAP_CTIME))
  445. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
  446. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  447. inode_set_iversion_raw(inode, fattr->change_attr);
  448. else
  449. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
  450. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  451. inode->i_size = nfs_size_to_loff_t(fattr->size);
  452. else
  453. nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
  454. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  455. set_nlink(inode, fattr->nlink);
  456. else if (nfs_server_capable(inode, NFS_CAP_NLINK))
  457. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  458. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  459. inode->i_uid = fattr->uid;
  460. else if (nfs_server_capable(inode, NFS_CAP_OWNER))
  461. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  462. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  463. inode->i_gid = fattr->gid;
  464. else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
  465. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  466. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  467. inode->i_blocks = fattr->du.nfs2.blocks;
  468. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  469. /*
  470. * report the blocks in 512byte units
  471. */
  472. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  473. }
  474. if (nfsi->cache_validity != 0)
  475. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  476. nfs_setsecurity(inode, fattr, label);
  477. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  478. nfsi->attrtimeo_timestamp = now;
  479. nfsi->access_cache = RB_ROOT;
  480. nfs_fscache_init_inode(inode);
  481. unlock_new_inode(inode);
  482. } else {
  483. int err = nfs_refresh_inode(inode, fattr);
  484. if (err < 0) {
  485. iput(inode);
  486. inode = ERR_PTR(err);
  487. goto out_no_inode;
  488. }
  489. }
  490. dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
  491. inode->i_sb->s_id,
  492. (unsigned long long)NFS_FILEID(inode),
  493. nfs_display_fhandle_hash(fh),
  494. atomic_read(&inode->i_count));
  495. out:
  496. return inode;
  497. out_no_inode:
  498. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  499. goto out;
  500. }
  501. EXPORT_SYMBOL_GPL(nfs_fhget);
  502. #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)
  503. int
  504. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  505. {
  506. struct inode *inode = d_inode(dentry);
  507. struct nfs_fattr *fattr;
  508. int error = 0;
  509. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  510. /* skip mode change if it's just for clearing setuid/setgid */
  511. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  512. attr->ia_valid &= ~ATTR_MODE;
  513. if (attr->ia_valid & ATTR_SIZE) {
  514. BUG_ON(!S_ISREG(inode->i_mode));
  515. error = inode_newsize_ok(inode, attr->ia_size);
  516. if (error)
  517. return error;
  518. if (attr->ia_size == i_size_read(inode))
  519. attr->ia_valid &= ~ATTR_SIZE;
  520. }
  521. /* Optimization: if the end result is no change, don't RPC */
  522. attr->ia_valid &= NFS_VALID_ATTRS;
  523. if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  524. return 0;
  525. trace_nfs_setattr_enter(inode);
  526. /* Write all dirty data */
  527. if (S_ISREG(inode->i_mode))
  528. nfs_sync_inode(inode);
  529. fattr = nfs_alloc_fattr();
  530. if (fattr == NULL) {
  531. error = -ENOMEM;
  532. goto out;
  533. }
  534. error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
  535. if (error == 0)
  536. error = nfs_refresh_inode(inode, fattr);
  537. nfs_free_fattr(fattr);
  538. out:
  539. trace_nfs_setattr_exit(inode, error);
  540. return error;
  541. }
  542. EXPORT_SYMBOL_GPL(nfs_setattr);
  543. /**
  544. * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
  545. * @inode: inode of the file used
  546. * @offset: file offset to start truncating
  547. *
  548. * This is a copy of the common vmtruncate, but with the locking
  549. * corrected to take into account the fact that NFS requires
  550. * inode->i_size to be updated under the inode->i_lock.
  551. * Note: must be called with inode->i_lock held!
  552. */
  553. static int nfs_vmtruncate(struct inode * inode, loff_t offset)
  554. {
  555. int err;
  556. err = inode_newsize_ok(inode, offset);
  557. if (err)
  558. goto out;
  559. i_size_write(inode, offset);
  560. /* Optimisation */
  561. if (offset == 0)
  562. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
  563. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
  564. spin_unlock(&inode->i_lock);
  565. truncate_pagecache(inode, offset);
  566. spin_lock(&inode->i_lock);
  567. out:
  568. return err;
  569. }
  570. /**
  571. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  572. * @inode: pointer to struct inode
  573. * @attr: pointer to struct iattr
  574. * @fattr: pointer to struct nfs_fattr
  575. *
  576. * Note: we do this in the *proc.c in order to ensure that
  577. * it works for things like exclusive creates too.
  578. */
  579. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
  580. struct nfs_fattr *fattr)
  581. {
  582. /* Barrier: bump the attribute generation count. */
  583. nfs_fattr_set_barrier(fattr);
  584. spin_lock(&inode->i_lock);
  585. NFS_I(inode)->attr_gencount = fattr->gencount;
  586. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  587. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  588. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  589. nfs_vmtruncate(inode, attr->ia_size);
  590. }
  591. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  592. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
  593. if ((attr->ia_valid & ATTR_MODE) != 0) {
  594. int mode = attr->ia_mode & S_IALLUGO;
  595. mode |= inode->i_mode & ~S_IALLUGO;
  596. inode->i_mode = mode;
  597. }
  598. if ((attr->ia_valid & ATTR_UID) != 0)
  599. inode->i_uid = attr->ia_uid;
  600. if ((attr->ia_valid & ATTR_GID) != 0)
  601. inode->i_gid = attr->ia_gid;
  602. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  603. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  604. else
  605. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  606. | NFS_INO_INVALID_CTIME);
  607. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
  608. | NFS_INO_INVALID_ACL);
  609. }
  610. if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
  611. NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
  612. | NFS_INO_INVALID_CTIME);
  613. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  614. inode->i_atime = timespec_to_timespec64(fattr->atime);
  615. else if (attr->ia_valid & ATTR_ATIME_SET)
  616. inode->i_atime = attr->ia_atime;
  617. else
  618. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  619. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  620. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  621. else
  622. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  623. | NFS_INO_INVALID_CTIME);
  624. }
  625. if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
  626. NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
  627. | NFS_INO_INVALID_CTIME);
  628. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  629. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  630. else if (attr->ia_valid & ATTR_MTIME_SET)
  631. inode->i_mtime = attr->ia_mtime;
  632. else
  633. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  634. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  635. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  636. else
  637. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  638. | NFS_INO_INVALID_CTIME);
  639. }
  640. if (fattr->valid)
  641. nfs_update_inode(inode, fattr);
  642. spin_unlock(&inode->i_lock);
  643. }
  644. EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
  645. static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
  646. {
  647. struct dentry *parent;
  648. if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
  649. return;
  650. parent = dget_parent(dentry);
  651. nfs_force_use_readdirplus(d_inode(parent));
  652. dput(parent);
  653. }
  654. static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
  655. {
  656. struct dentry *parent;
  657. if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
  658. return;
  659. parent = dget_parent(dentry);
  660. nfs_advise_use_readdirplus(d_inode(parent));
  661. dput(parent);
  662. }
  663. static bool nfs_need_revalidate_inode(struct inode *inode)
  664. {
  665. if (NFS_I(inode)->cache_validity &
  666. (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
  667. return true;
  668. if (nfs_attribute_cache_expired(inode))
  669. return true;
  670. return false;
  671. }
  672. int nfs_getattr(const struct path *path, struct kstat *stat,
  673. u32 request_mask, unsigned int query_flags)
  674. {
  675. struct inode *inode = d_inode(path->dentry);
  676. struct nfs_server *server = NFS_SERVER(inode);
  677. unsigned long cache_validity;
  678. int err = 0;
  679. bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
  680. bool do_update = false;
  681. trace_nfs_getattr_enter(inode);
  682. if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync)
  683. goto out_no_update;
  684. /* Flush out writes to the server in order to update c/mtime. */
  685. if ((request_mask & (STATX_CTIME|STATX_MTIME)) &&
  686. S_ISREG(inode->i_mode)) {
  687. err = filemap_write_and_wait(inode->i_mapping);
  688. if (err)
  689. goto out;
  690. }
  691. /*
  692. * We may force a getattr if the user cares about atime.
  693. *
  694. * Note that we only have to check the vfsmount flags here:
  695. * - NFS always sets S_NOATIME by so checking it would give a
  696. * bogus result
  697. * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
  698. * no point in checking those.
  699. */
  700. if ((path->mnt->mnt_flags & MNT_NOATIME) ||
  701. ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  702. request_mask &= ~STATX_ATIME;
  703. /* Is the user requesting attributes that might need revalidation? */
  704. if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
  705. STATX_MTIME|STATX_UID|STATX_GID|
  706. STATX_SIZE|STATX_BLOCKS)))
  707. goto out_no_revalidate;
  708. /* Check whether the cached attributes are stale */
  709. do_update |= force_sync || nfs_attribute_cache_expired(inode);
  710. cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  711. do_update |= cache_validity &
  712. (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL);
  713. if (request_mask & STATX_ATIME)
  714. do_update |= cache_validity & NFS_INO_INVALID_ATIME;
  715. if (request_mask & (STATX_CTIME|STATX_MTIME))
  716. do_update |= cache_validity & NFS_INO_REVAL_PAGECACHE;
  717. if (do_update) {
  718. /* Update the attribute cache */
  719. if (!(server->flags & NFS_MOUNT_NOAC))
  720. nfs_readdirplus_parent_cache_miss(path->dentry);
  721. else
  722. nfs_readdirplus_parent_cache_hit(path->dentry);
  723. err = __nfs_revalidate_inode(server, inode);
  724. if (err)
  725. goto out;
  726. } else
  727. nfs_readdirplus_parent_cache_hit(path->dentry);
  728. out_no_revalidate:
  729. /* Only return attributes that were revalidated. */
  730. stat->result_mask &= request_mask;
  731. out_no_update:
  732. generic_fillattr(inode, stat);
  733. stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
  734. if (S_ISDIR(inode->i_mode))
  735. stat->blksize = NFS_SERVER(inode)->dtsize;
  736. out:
  737. trace_nfs_getattr_exit(inode, err);
  738. return err;
  739. }
  740. EXPORT_SYMBOL_GPL(nfs_getattr);
  741. static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
  742. {
  743. refcount_set(&l_ctx->count, 1);
  744. l_ctx->lockowner = current->files;
  745. INIT_LIST_HEAD(&l_ctx->list);
  746. atomic_set(&l_ctx->io_count, 0);
  747. }
  748. static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
  749. {
  750. struct nfs_lock_context *head = &ctx->lock_context;
  751. struct nfs_lock_context *pos = head;
  752. do {
  753. if (pos->lockowner != current->files)
  754. continue;
  755. refcount_inc(&pos->count);
  756. return pos;
  757. } while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head);
  758. return NULL;
  759. }
  760. struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
  761. {
  762. struct nfs_lock_context *res, *new = NULL;
  763. struct inode *inode = d_inode(ctx->dentry);
  764. spin_lock(&inode->i_lock);
  765. res = __nfs_find_lock_context(ctx);
  766. if (res == NULL) {
  767. spin_unlock(&inode->i_lock);
  768. new = kmalloc(sizeof(*new), GFP_KERNEL);
  769. if (new == NULL)
  770. return ERR_PTR(-ENOMEM);
  771. nfs_init_lock_context(new);
  772. spin_lock(&inode->i_lock);
  773. res = __nfs_find_lock_context(ctx);
  774. if (res == NULL) {
  775. list_add_tail(&new->list, &ctx->lock_context.list);
  776. new->open_context = ctx;
  777. res = new;
  778. new = NULL;
  779. }
  780. }
  781. spin_unlock(&inode->i_lock);
  782. kfree(new);
  783. return res;
  784. }
  785. EXPORT_SYMBOL_GPL(nfs_get_lock_context);
  786. void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
  787. {
  788. struct nfs_open_context *ctx = l_ctx->open_context;
  789. struct inode *inode = d_inode(ctx->dentry);
  790. if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
  791. return;
  792. list_del(&l_ctx->list);
  793. spin_unlock(&inode->i_lock);
  794. kfree(l_ctx);
  795. }
  796. EXPORT_SYMBOL_GPL(nfs_put_lock_context);
  797. /**
  798. * nfs_close_context - Common close_context() routine NFSv2/v3
  799. * @ctx: pointer to context
  800. * @is_sync: is this a synchronous close
  801. *
  802. * Ensure that the attributes are up to date if we're mounted
  803. * with close-to-open semantics and we have cached data that will
  804. * need to be revalidated on open.
  805. */
  806. void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
  807. {
  808. struct nfs_inode *nfsi;
  809. struct inode *inode;
  810. struct nfs_server *server;
  811. if (!(ctx->mode & FMODE_WRITE))
  812. return;
  813. if (!is_sync)
  814. return;
  815. inode = d_inode(ctx->dentry);
  816. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  817. return;
  818. nfsi = NFS_I(inode);
  819. if (inode->i_mapping->nrpages == 0)
  820. return;
  821. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  822. return;
  823. if (!list_empty(&nfsi->open_files))
  824. return;
  825. server = NFS_SERVER(inode);
  826. if (server->flags & NFS_MOUNT_NOCTO)
  827. return;
  828. nfs_revalidate_inode(server, inode);
  829. }
  830. EXPORT_SYMBOL_GPL(nfs_close_context);
  831. struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
  832. fmode_t f_mode,
  833. struct file *filp)
  834. {
  835. struct nfs_open_context *ctx;
  836. struct rpc_cred *cred = rpc_lookup_cred();
  837. if (IS_ERR(cred))
  838. return ERR_CAST(cred);
  839. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  840. if (!ctx) {
  841. put_rpccred(cred);
  842. return ERR_PTR(-ENOMEM);
  843. }
  844. nfs_sb_active(dentry->d_sb);
  845. ctx->dentry = dget(dentry);
  846. ctx->cred = cred;
  847. ctx->state = NULL;
  848. ctx->mode = f_mode;
  849. ctx->flags = 0;
  850. ctx->error = 0;
  851. ctx->flock_owner = (fl_owner_t)filp;
  852. nfs_init_lock_context(&ctx->lock_context);
  853. ctx->lock_context.open_context = ctx;
  854. INIT_LIST_HEAD(&ctx->list);
  855. ctx->mdsthreshold = NULL;
  856. return ctx;
  857. }
  858. EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
  859. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  860. {
  861. if (ctx != NULL)
  862. refcount_inc(&ctx->lock_context.count);
  863. return ctx;
  864. }
  865. EXPORT_SYMBOL_GPL(get_nfs_open_context);
  866. static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
  867. {
  868. struct inode *inode = d_inode(ctx->dentry);
  869. struct super_block *sb = ctx->dentry->d_sb;
  870. if (!list_empty(&ctx->list)) {
  871. if (!refcount_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
  872. return;
  873. list_del(&ctx->list);
  874. spin_unlock(&inode->i_lock);
  875. } else if (!refcount_dec_and_test(&ctx->lock_context.count))
  876. return;
  877. if (inode != NULL)
  878. NFS_PROTO(inode)->close_context(ctx, is_sync);
  879. if (ctx->cred != NULL)
  880. put_rpccred(ctx->cred);
  881. dput(ctx->dentry);
  882. nfs_sb_deactive(sb);
  883. kfree(ctx->mdsthreshold);
  884. kfree(ctx);
  885. }
  886. void put_nfs_open_context(struct nfs_open_context *ctx)
  887. {
  888. __put_nfs_open_context(ctx, 0);
  889. }
  890. EXPORT_SYMBOL_GPL(put_nfs_open_context);
  891. static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
  892. {
  893. __put_nfs_open_context(ctx, 1);
  894. }
  895. /*
  896. * Ensure that mmap has a recent RPC credential for use when writing out
  897. * shared pages
  898. */
  899. void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
  900. {
  901. struct inode *inode = d_inode(ctx->dentry);
  902. struct nfs_inode *nfsi = NFS_I(inode);
  903. spin_lock(&inode->i_lock);
  904. if (ctx->mode & FMODE_WRITE)
  905. list_add(&ctx->list, &nfsi->open_files);
  906. else
  907. list_add_tail(&ctx->list, &nfsi->open_files);
  908. spin_unlock(&inode->i_lock);
  909. }
  910. EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
  911. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  912. {
  913. filp->private_data = get_nfs_open_context(ctx);
  914. if (list_empty(&ctx->list))
  915. nfs_inode_attach_open_context(ctx);
  916. }
  917. EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
  918. /*
  919. * Given an inode, search for an open context with the desired characteristics
  920. */
  921. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  922. {
  923. struct nfs_inode *nfsi = NFS_I(inode);
  924. struct nfs_open_context *pos, *ctx = NULL;
  925. spin_lock(&inode->i_lock);
  926. list_for_each_entry(pos, &nfsi->open_files, list) {
  927. if (cred != NULL && pos->cred != cred)
  928. continue;
  929. if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
  930. continue;
  931. ctx = get_nfs_open_context(pos);
  932. break;
  933. }
  934. spin_unlock(&inode->i_lock);
  935. return ctx;
  936. }
  937. void nfs_file_clear_open_context(struct file *filp)
  938. {
  939. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  940. if (ctx) {
  941. struct inode *inode = d_inode(ctx->dentry);
  942. /*
  943. * We fatal error on write before. Try to writeback
  944. * every page again.
  945. */
  946. if (ctx->error < 0)
  947. invalidate_inode_pages2(inode->i_mapping);
  948. filp->private_data = NULL;
  949. spin_lock(&inode->i_lock);
  950. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  951. spin_unlock(&inode->i_lock);
  952. put_nfs_open_context_sync(ctx);
  953. }
  954. }
  955. /*
  956. * These allocate and release file read/write context information.
  957. */
  958. int nfs_open(struct inode *inode, struct file *filp)
  959. {
  960. struct nfs_open_context *ctx;
  961. ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
  962. if (IS_ERR(ctx))
  963. return PTR_ERR(ctx);
  964. nfs_file_set_open_context(filp, ctx);
  965. put_nfs_open_context(ctx);
  966. nfs_fscache_open_file(inode, filp);
  967. return 0;
  968. }
  969. /*
  970. * This function is called whenever some part of NFS notices that
  971. * the cached attributes have to be refreshed.
  972. */
  973. int
  974. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  975. {
  976. int status = -ESTALE;
  977. struct nfs4_label *label = NULL;
  978. struct nfs_fattr *fattr = NULL;
  979. struct nfs_inode *nfsi = NFS_I(inode);
  980. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
  981. inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
  982. trace_nfs_revalidate_inode_enter(inode);
  983. if (is_bad_inode(inode))
  984. goto out;
  985. if (NFS_STALE(inode))
  986. goto out;
  987. /* pNFS: Attributes aren't updated until we layoutcommit */
  988. if (S_ISREG(inode->i_mode)) {
  989. status = pnfs_sync_inode(inode, false);
  990. if (status)
  991. goto out;
  992. }
  993. status = -ENOMEM;
  994. fattr = nfs_alloc_fattr();
  995. if (fattr == NULL)
  996. goto out;
  997. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  998. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  999. if (IS_ERR(label)) {
  1000. status = PTR_ERR(label);
  1001. goto out;
  1002. }
  1003. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr,
  1004. label, inode);
  1005. if (status != 0) {
  1006. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
  1007. inode->i_sb->s_id,
  1008. (unsigned long long)NFS_FILEID(inode), status);
  1009. if (status == -ESTALE) {
  1010. nfs_zap_caches(inode);
  1011. if (!S_ISDIR(inode->i_mode))
  1012. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1013. }
  1014. goto err_out;
  1015. }
  1016. status = nfs_refresh_inode(inode, fattr);
  1017. if (status) {
  1018. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
  1019. inode->i_sb->s_id,
  1020. (unsigned long long)NFS_FILEID(inode), status);
  1021. goto err_out;
  1022. }
  1023. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  1024. nfs_zap_acl_cache(inode);
  1025. nfs_setsecurity(inode, fattr, label);
  1026. dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
  1027. inode->i_sb->s_id,
  1028. (unsigned long long)NFS_FILEID(inode));
  1029. err_out:
  1030. nfs4_label_free(label);
  1031. out:
  1032. nfs_free_fattr(fattr);
  1033. trace_nfs_revalidate_inode_exit(inode, status);
  1034. return status;
  1035. }
  1036. int nfs_attribute_cache_expired(struct inode *inode)
  1037. {
  1038. if (nfs_have_delegated_attributes(inode))
  1039. return 0;
  1040. return nfs_attribute_timeout(inode);
  1041. }
  1042. /**
  1043. * nfs_revalidate_inode - Revalidate the inode attributes
  1044. * @server - pointer to nfs_server struct
  1045. * @inode - pointer to inode struct
  1046. *
  1047. * Updates inode attribute information by retrieving the data from the server.
  1048. */
  1049. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  1050. {
  1051. if (!nfs_need_revalidate_inode(inode))
  1052. return NFS_STALE(inode) ? -ESTALE : 0;
  1053. return __nfs_revalidate_inode(server, inode);
  1054. }
  1055. EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
  1056. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  1057. {
  1058. struct nfs_inode *nfsi = NFS_I(inode);
  1059. int ret;
  1060. if (mapping->nrpages != 0) {
  1061. if (S_ISREG(inode->i_mode)) {
  1062. ret = nfs_sync_mapping(mapping);
  1063. if (ret < 0)
  1064. return ret;
  1065. }
  1066. ret = invalidate_inode_pages2(mapping);
  1067. if (ret < 0)
  1068. return ret;
  1069. }
  1070. if (S_ISDIR(inode->i_mode)) {
  1071. spin_lock(&inode->i_lock);
  1072. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  1073. spin_unlock(&inode->i_lock);
  1074. }
  1075. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  1076. nfs_fscache_wait_on_invalidate(inode);
  1077. dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
  1078. inode->i_sb->s_id,
  1079. (unsigned long long)NFS_FILEID(inode));
  1080. return 0;
  1081. }
  1082. bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  1083. {
  1084. return nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE) ||
  1085. NFS_STALE(inode);
  1086. }
  1087. int nfs_revalidate_mapping_rcu(struct inode *inode)
  1088. {
  1089. struct nfs_inode *nfsi = NFS_I(inode);
  1090. unsigned long *bitlock = &nfsi->flags;
  1091. int ret = 0;
  1092. if (IS_SWAPFILE(inode))
  1093. goto out;
  1094. if (nfs_mapping_need_revalidate_inode(inode)) {
  1095. ret = -ECHILD;
  1096. goto out;
  1097. }
  1098. spin_lock(&inode->i_lock);
  1099. if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
  1100. (nfsi->cache_validity & NFS_INO_INVALID_DATA))
  1101. ret = -ECHILD;
  1102. spin_unlock(&inode->i_lock);
  1103. out:
  1104. return ret;
  1105. }
  1106. /**
  1107. * nfs_revalidate_mapping - Revalidate the pagecache
  1108. * @inode - pointer to host inode
  1109. * @mapping - pointer to mapping
  1110. */
  1111. int nfs_revalidate_mapping(struct inode *inode,
  1112. struct address_space *mapping)
  1113. {
  1114. struct nfs_inode *nfsi = NFS_I(inode);
  1115. unsigned long *bitlock = &nfsi->flags;
  1116. int ret = 0;
  1117. /* swapfiles are not supposed to be shared. */
  1118. if (IS_SWAPFILE(inode))
  1119. goto out;
  1120. if (nfs_mapping_need_revalidate_inode(inode)) {
  1121. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1122. if (ret < 0)
  1123. goto out;
  1124. }
  1125. /*
  1126. * We must clear NFS_INO_INVALID_DATA first to ensure that
  1127. * invalidations that come in while we're shooting down the mappings
  1128. * are respected. But, that leaves a race window where one revalidator
  1129. * can clear the flag, and then another checks it before the mapping
  1130. * gets invalidated. Fix that by serializing access to this part of
  1131. * the function.
  1132. *
  1133. * At the same time, we need to allow other tasks to see whether we
  1134. * might be in the middle of invalidating the pages, so we only set
  1135. * the bit lock here if it looks like we're going to be doing that.
  1136. */
  1137. for (;;) {
  1138. ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
  1139. nfs_wait_bit_killable, TASK_KILLABLE);
  1140. if (ret)
  1141. goto out;
  1142. spin_lock(&inode->i_lock);
  1143. if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
  1144. spin_unlock(&inode->i_lock);
  1145. continue;
  1146. }
  1147. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1148. break;
  1149. spin_unlock(&inode->i_lock);
  1150. goto out;
  1151. }
  1152. set_bit(NFS_INO_INVALIDATING, bitlock);
  1153. smp_wmb();
  1154. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  1155. spin_unlock(&inode->i_lock);
  1156. trace_nfs_invalidate_mapping_enter(inode);
  1157. ret = nfs_invalidate_mapping(inode, mapping);
  1158. trace_nfs_invalidate_mapping_exit(inode, ret);
  1159. clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
  1160. smp_mb__after_atomic();
  1161. wake_up_bit(bitlock, NFS_INO_INVALIDATING);
  1162. out:
  1163. return ret;
  1164. }
  1165. static bool nfs_file_has_writers(struct nfs_inode *nfsi)
  1166. {
  1167. struct inode *inode = &nfsi->vfs_inode;
  1168. assert_spin_locked(&inode->i_lock);
  1169. if (!S_ISREG(inode->i_mode))
  1170. return false;
  1171. if (list_empty(&nfsi->open_files))
  1172. return false;
  1173. /* Note: This relies on nfsi->open_files being ordered with writers
  1174. * being placed at the head of the list.
  1175. * See nfs_inode_attach_open_context()
  1176. */
  1177. return (list_first_entry(&nfsi->open_files,
  1178. struct nfs_open_context,
  1179. list)->mode & FMODE_WRITE) == FMODE_WRITE;
  1180. }
  1181. static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
  1182. {
  1183. return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
  1184. }
  1185. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1186. {
  1187. struct timespec ts;
  1188. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  1189. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  1190. && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
  1191. inode_set_iversion_raw(inode, fattr->change_attr);
  1192. if (S_ISDIR(inode->i_mode))
  1193. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  1194. }
  1195. /* If we have atomic WCC data, we may update some attributes */
  1196. ts = timespec64_to_timespec(inode->i_ctime);
  1197. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  1198. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  1199. && timespec_equal(&ts, &fattr->pre_ctime)) {
  1200. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  1201. }
  1202. ts = timespec64_to_timespec(inode->i_mtime);
  1203. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  1204. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  1205. && timespec_equal(&ts, &fattr->pre_mtime)) {
  1206. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  1207. if (S_ISDIR(inode->i_mode))
  1208. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  1209. }
  1210. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  1211. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  1212. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  1213. && !nfs_have_writebacks(inode)) {
  1214. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  1215. }
  1216. }
  1217. /**
  1218. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  1219. * @inode - pointer to inode
  1220. * @fattr - updated attributes
  1221. *
  1222. * Verifies the attribute cache. If we have just changed the attributes,
  1223. * so that fattr carries weak cache consistency data, then it may
  1224. * also update the ctime/mtime/change_attribute.
  1225. */
  1226. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  1227. {
  1228. struct nfs_inode *nfsi = NFS_I(inode);
  1229. loff_t cur_size, new_isize;
  1230. unsigned long invalid = 0;
  1231. struct timespec ts;
  1232. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  1233. return 0;
  1234. /* Has the inode gone and changed behind our back? */
  1235. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  1236. return -ESTALE;
  1237. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1238. return -ESTALE;
  1239. if (!nfs_file_has_buffered_writers(nfsi)) {
  1240. /* Verify a few of the more important attributes */
  1241. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
  1242. invalid |= NFS_INO_INVALID_CHANGE
  1243. | NFS_INO_REVAL_PAGECACHE;
  1244. ts = timespec64_to_timespec(inode->i_mtime);
  1245. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&ts, &fattr->mtime))
  1246. invalid |= NFS_INO_INVALID_MTIME;
  1247. ts = timespec64_to_timespec(inode->i_ctime);
  1248. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec_equal(&ts, &fattr->ctime))
  1249. invalid |= NFS_INO_INVALID_CTIME;
  1250. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1251. cur_size = i_size_read(inode);
  1252. new_isize = nfs_size_to_loff_t(fattr->size);
  1253. if (cur_size != new_isize)
  1254. invalid |= NFS_INO_INVALID_SIZE
  1255. | NFS_INO_REVAL_PAGECACHE;
  1256. }
  1257. }
  1258. /* Have any file permissions changed? */
  1259. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  1260. invalid |= NFS_INO_INVALID_ACCESS
  1261. | NFS_INO_INVALID_ACL
  1262. | NFS_INO_INVALID_OTHER;
  1263. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
  1264. invalid |= NFS_INO_INVALID_ACCESS
  1265. | NFS_INO_INVALID_ACL
  1266. | NFS_INO_INVALID_OTHER;
  1267. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
  1268. invalid |= NFS_INO_INVALID_ACCESS
  1269. | NFS_INO_INVALID_ACL
  1270. | NFS_INO_INVALID_OTHER;
  1271. /* Has the link count changed? */
  1272. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  1273. invalid |= NFS_INO_INVALID_OTHER;
  1274. ts = timespec64_to_timespec(inode->i_atime);
  1275. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&ts, &fattr->atime))
  1276. invalid |= NFS_INO_INVALID_ATIME;
  1277. if (invalid != 0)
  1278. nfs_set_cache_invalid(inode, invalid);
  1279. nfsi->read_cache_jiffies = fattr->time_start;
  1280. return 0;
  1281. }
  1282. static atomic_long_t nfs_attr_generation_counter;
  1283. static unsigned long nfs_read_attr_generation_counter(void)
  1284. {
  1285. return atomic_long_read(&nfs_attr_generation_counter);
  1286. }
  1287. unsigned long nfs_inc_attr_generation_counter(void)
  1288. {
  1289. return atomic_long_inc_return(&nfs_attr_generation_counter);
  1290. }
  1291. EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
  1292. void nfs_fattr_init(struct nfs_fattr *fattr)
  1293. {
  1294. fattr->valid = 0;
  1295. fattr->time_start = jiffies;
  1296. fattr->gencount = nfs_inc_attr_generation_counter();
  1297. fattr->owner_name = NULL;
  1298. fattr->group_name = NULL;
  1299. }
  1300. EXPORT_SYMBOL_GPL(nfs_fattr_init);
  1301. /**
  1302. * nfs_fattr_set_barrier
  1303. * @fattr: attributes
  1304. *
  1305. * Used to set a barrier after an attribute was updated. This
  1306. * barrier ensures that older attributes from RPC calls that may
  1307. * have raced with our update cannot clobber these new values.
  1308. * Note that you are still responsible for ensuring that other
  1309. * operations which change the attribute on the server do not
  1310. * collide.
  1311. */
  1312. void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
  1313. {
  1314. fattr->gencount = nfs_inc_attr_generation_counter();
  1315. }
  1316. struct nfs_fattr *nfs_alloc_fattr(void)
  1317. {
  1318. struct nfs_fattr *fattr;
  1319. fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
  1320. if (fattr != NULL)
  1321. nfs_fattr_init(fattr);
  1322. return fattr;
  1323. }
  1324. EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
  1325. struct nfs_fh *nfs_alloc_fhandle(void)
  1326. {
  1327. struct nfs_fh *fh;
  1328. fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
  1329. if (fh != NULL)
  1330. fh->size = 0;
  1331. return fh;
  1332. }
  1333. EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
  1334. #ifdef NFS_DEBUG
  1335. /*
  1336. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  1337. * in the same way that wireshark does
  1338. *
  1339. * @fh: file handle
  1340. *
  1341. * For debugging only.
  1342. */
  1343. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  1344. {
  1345. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  1346. * not on the result */
  1347. return nfs_fhandle_hash(fh);
  1348. }
  1349. EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
  1350. /*
  1351. * _nfs_display_fhandle - display an NFS file handle on the console
  1352. *
  1353. * @fh: file handle to display
  1354. * @caption: display caption
  1355. *
  1356. * For debugging only.
  1357. */
  1358. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  1359. {
  1360. unsigned short i;
  1361. if (fh == NULL || fh->size == 0) {
  1362. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  1363. return;
  1364. }
  1365. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  1366. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  1367. for (i = 0; i < fh->size; i += 16) {
  1368. __be32 *pos = (__be32 *)&fh->data[i];
  1369. switch ((fh->size - i - 1) >> 2) {
  1370. case 0:
  1371. printk(KERN_DEFAULT " %08x\n",
  1372. be32_to_cpup(pos));
  1373. break;
  1374. case 1:
  1375. printk(KERN_DEFAULT " %08x %08x\n",
  1376. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  1377. break;
  1378. case 2:
  1379. printk(KERN_DEFAULT " %08x %08x %08x\n",
  1380. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1381. be32_to_cpup(pos + 2));
  1382. break;
  1383. default:
  1384. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  1385. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1386. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  1387. }
  1388. }
  1389. }
  1390. EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
  1391. #endif
  1392. /**
  1393. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  1394. * @inode - pointer to inode
  1395. * @fattr - attributes
  1396. *
  1397. * Attempt to divine whether or not an RPC call reply carrying stale
  1398. * attributes got scheduled after another call carrying updated ones.
  1399. *
  1400. * To do so, the function first assumes that a more recent ctime means
  1401. * that the attributes in fattr are newer, however it also attempt to
  1402. * catch the case where ctime either didn't change, or went backwards
  1403. * (if someone reset the clock on the server) by looking at whether
  1404. * or not this RPC call was started after the inode was last updated.
  1405. * Note also the check for wraparound of 'attr_gencount'
  1406. *
  1407. * The function returns 'true' if it thinks the attributes in 'fattr' are
  1408. * more recent than the ones cached in the inode.
  1409. *
  1410. */
  1411. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  1412. {
  1413. const struct nfs_inode *nfsi = NFS_I(inode);
  1414. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  1415. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  1416. }
  1417. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1418. {
  1419. int ret;
  1420. trace_nfs_refresh_inode_enter(inode);
  1421. if (nfs_inode_attrs_need_update(inode, fattr))
  1422. ret = nfs_update_inode(inode, fattr);
  1423. else
  1424. ret = nfs_check_inode_attributes(inode, fattr);
  1425. trace_nfs_refresh_inode_exit(inode, ret);
  1426. return ret;
  1427. }
  1428. /**
  1429. * nfs_refresh_inode - try to update the inode attribute cache
  1430. * @inode - pointer to inode
  1431. * @fattr - updated attributes
  1432. *
  1433. * Check that an RPC call that returned attributes has not overlapped with
  1434. * other recent updates of the inode metadata, then decide whether it is
  1435. * safe to do a full update of the inode attributes, or whether just to
  1436. * call nfs_check_inode_attributes.
  1437. */
  1438. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1439. {
  1440. int status;
  1441. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1442. return 0;
  1443. spin_lock(&inode->i_lock);
  1444. status = nfs_refresh_inode_locked(inode, fattr);
  1445. spin_unlock(&inode->i_lock);
  1446. return status;
  1447. }
  1448. EXPORT_SYMBOL_GPL(nfs_refresh_inode);
  1449. static int nfs_post_op_update_inode_locked(struct inode *inode,
  1450. struct nfs_fattr *fattr, unsigned int invalid)
  1451. {
  1452. if (S_ISDIR(inode->i_mode))
  1453. invalid |= NFS_INO_INVALID_DATA;
  1454. nfs_set_cache_invalid(inode, invalid);
  1455. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1456. return 0;
  1457. return nfs_refresh_inode_locked(inode, fattr);
  1458. }
  1459. /**
  1460. * nfs_post_op_update_inode - try to update the inode attribute cache
  1461. * @inode - pointer to inode
  1462. * @fattr - updated attributes
  1463. *
  1464. * After an operation that has changed the inode metadata, mark the
  1465. * attribute cache as being invalid, then try to update it.
  1466. *
  1467. * NB: if the server didn't return any post op attributes, this
  1468. * function will force the retrieval of attributes before the next
  1469. * NFS request. Thus it should be used only for operations that
  1470. * are expected to change one or more attributes, to avoid
  1471. * unnecessary NFS requests and trips through nfs_update_inode().
  1472. */
  1473. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1474. {
  1475. int status;
  1476. spin_lock(&inode->i_lock);
  1477. nfs_fattr_set_barrier(fattr);
  1478. status = nfs_post_op_update_inode_locked(inode, fattr,
  1479. NFS_INO_INVALID_CHANGE
  1480. | NFS_INO_INVALID_CTIME
  1481. | NFS_INO_REVAL_FORCED);
  1482. spin_unlock(&inode->i_lock);
  1483. return status;
  1484. }
  1485. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
  1486. /**
  1487. * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
  1488. * @inode - pointer to inode
  1489. * @fattr - updated attributes
  1490. *
  1491. * After an operation that has changed the inode metadata, mark the
  1492. * attribute cache as being invalid, then try to update it. Fake up
  1493. * weak cache consistency data, if none exist.
  1494. *
  1495. * This function is mainly designed to be used by the ->write_done() functions.
  1496. */
  1497. int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
  1498. {
  1499. int status;
  1500. /* Don't do a WCC update if these attributes are already stale */
  1501. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  1502. !nfs_inode_attrs_need_update(inode, fattr)) {
  1503. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1504. | NFS_ATTR_FATTR_PRESIZE
  1505. | NFS_ATTR_FATTR_PREMTIME
  1506. | NFS_ATTR_FATTR_PRECTIME);
  1507. goto out_noforce;
  1508. }
  1509. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1510. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1511. fattr->pre_change_attr = inode_peek_iversion_raw(inode);
  1512. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1513. }
  1514. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1515. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1516. fattr->pre_ctime = timespec64_to_timespec(inode->i_ctime);
  1517. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1518. }
  1519. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1520. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1521. fattr->pre_mtime = timespec64_to_timespec(inode->i_mtime);
  1522. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1523. }
  1524. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1525. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1526. fattr->pre_size = i_size_read(inode);
  1527. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1528. }
  1529. out_noforce:
  1530. status = nfs_post_op_update_inode_locked(inode, fattr,
  1531. NFS_INO_INVALID_CHANGE
  1532. | NFS_INO_INVALID_CTIME
  1533. | NFS_INO_INVALID_MTIME);
  1534. return status;
  1535. }
  1536. /**
  1537. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1538. * @inode - pointer to inode
  1539. * @fattr - updated attributes
  1540. *
  1541. * After an operation that has changed the inode metadata, mark the
  1542. * attribute cache as being invalid, then try to update it. Fake up
  1543. * weak cache consistency data, if none exist.
  1544. *
  1545. * This function is mainly designed to be used by the ->write_done() functions.
  1546. */
  1547. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1548. {
  1549. int status;
  1550. spin_lock(&inode->i_lock);
  1551. nfs_fattr_set_barrier(fattr);
  1552. status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1553. spin_unlock(&inode->i_lock);
  1554. return status;
  1555. }
  1556. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
  1557. static inline bool nfs_fileid_valid(struct nfs_inode *nfsi,
  1558. struct nfs_fattr *fattr)
  1559. {
  1560. bool ret1 = true, ret2 = true;
  1561. if (fattr->valid & NFS_ATTR_FATTR_FILEID)
  1562. ret1 = (nfsi->fileid == fattr->fileid);
  1563. if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
  1564. ret2 = (nfsi->fileid == fattr->mounted_on_fileid);
  1565. return ret1 || ret2;
  1566. }
  1567. /*
  1568. * Many nfs protocol calls return the new file attributes after
  1569. * an operation. Here we update the inode to reflect the state
  1570. * of the server's inode.
  1571. *
  1572. * This is a bit tricky because we have to make sure all dirty pages
  1573. * have been sent off to the server before calling invalidate_inode_pages.
  1574. * To make sure no other process adds more write requests while we try
  1575. * our best to flush them, we make them sleep during the attribute refresh.
  1576. *
  1577. * A very similar scenario holds for the dir cache.
  1578. */
  1579. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1580. {
  1581. struct nfs_server *server;
  1582. struct nfs_inode *nfsi = NFS_I(inode);
  1583. loff_t cur_isize, new_isize;
  1584. unsigned long invalid = 0;
  1585. unsigned long now = jiffies;
  1586. unsigned long save_cache_validity;
  1587. bool have_writers = nfs_file_has_buffered_writers(nfsi);
  1588. bool cache_revalidated = true;
  1589. bool attr_changed = false;
  1590. bool have_delegation;
  1591. dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
  1592. __func__, inode->i_sb->s_id, inode->i_ino,
  1593. nfs_display_fhandle_hash(NFS_FH(inode)),
  1594. atomic_read(&inode->i_count), fattr->valid);
  1595. if (!nfs_fileid_valid(nfsi, fattr)) {
  1596. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1597. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1598. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1599. inode->i_sb->s_id, (long long)nfsi->fileid,
  1600. (long long)fattr->fileid);
  1601. goto out_err;
  1602. }
  1603. /*
  1604. * Make sure the inode's type hasn't changed.
  1605. */
  1606. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  1607. /*
  1608. * Big trouble! The inode has become a different object.
  1609. */
  1610. printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
  1611. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1612. goto out_err;
  1613. }
  1614. server = NFS_SERVER(inode);
  1615. /* Update the fsid? */
  1616. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1617. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1618. !IS_AUTOMOUNT(inode))
  1619. server->fsid = fattr->fsid;
  1620. /* Save the delegation state before clearing cache_validity */
  1621. have_delegation = nfs_have_delegated_attributes(inode);
  1622. /*
  1623. * Update the read time so we don't revalidate too often.
  1624. */
  1625. nfsi->read_cache_jiffies = fattr->time_start;
  1626. save_cache_validity = nfsi->cache_validity;
  1627. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1628. | NFS_INO_INVALID_ATIME
  1629. | NFS_INO_REVAL_FORCED
  1630. | NFS_INO_REVAL_PAGECACHE);
  1631. /* Do atomic weak cache consistency updates */
  1632. nfs_wcc_update_inode(inode, fattr);
  1633. if (pnfs_layoutcommit_outstanding(inode)) {
  1634. nfsi->cache_validity |= save_cache_validity & NFS_INO_INVALID_ATTR;
  1635. cache_revalidated = false;
  1636. }
  1637. /* More cache consistency checks */
  1638. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1639. if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
  1640. /* Could it be a race with writeback? */
  1641. if (!(have_writers || have_delegation)) {
  1642. invalid |= NFS_INO_INVALID_DATA
  1643. | NFS_INO_INVALID_ACCESS
  1644. | NFS_INO_INVALID_ACL;
  1645. /* Force revalidate of all attributes */
  1646. save_cache_validity |= NFS_INO_INVALID_CTIME
  1647. | NFS_INO_INVALID_MTIME
  1648. | NFS_INO_INVALID_SIZE
  1649. | NFS_INO_INVALID_OTHER;
  1650. if (S_ISDIR(inode->i_mode))
  1651. nfs_force_lookup_revalidate(inode);
  1652. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1653. inode->i_sb->s_id,
  1654. inode->i_ino);
  1655. }
  1656. inode_set_iversion_raw(inode, fattr->change_attr);
  1657. attr_changed = true;
  1658. }
  1659. } else {
  1660. nfsi->cache_validity |= save_cache_validity &
  1661. (NFS_INO_INVALID_CHANGE
  1662. | NFS_INO_REVAL_PAGECACHE
  1663. | NFS_INO_REVAL_FORCED);
  1664. cache_revalidated = false;
  1665. }
  1666. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1667. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  1668. } else if (server->caps & NFS_CAP_MTIME) {
  1669. nfsi->cache_validity |= save_cache_validity &
  1670. (NFS_INO_INVALID_MTIME
  1671. | NFS_INO_REVAL_FORCED);
  1672. cache_revalidated = false;
  1673. }
  1674. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1675. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  1676. } else if (server->caps & NFS_CAP_CTIME) {
  1677. nfsi->cache_validity |= save_cache_validity &
  1678. (NFS_INO_INVALID_CTIME
  1679. | NFS_INO_REVAL_FORCED);
  1680. cache_revalidated = false;
  1681. }
  1682. /* Check if our cached file size is stale */
  1683. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1684. new_isize = nfs_size_to_loff_t(fattr->size);
  1685. cur_isize = i_size_read(inode);
  1686. if (new_isize != cur_isize && !have_delegation) {
  1687. /* Do we perhaps have any outstanding writes, or has
  1688. * the file grown beyond our last write? */
  1689. if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
  1690. i_size_write(inode, new_isize);
  1691. if (!have_writers)
  1692. invalid |= NFS_INO_INVALID_DATA;
  1693. attr_changed = true;
  1694. }
  1695. dprintk("NFS: isize change on server for file %s/%ld "
  1696. "(%Ld to %Ld)\n",
  1697. inode->i_sb->s_id,
  1698. inode->i_ino,
  1699. (long long)cur_isize,
  1700. (long long)new_isize);
  1701. }
  1702. } else {
  1703. nfsi->cache_validity |= save_cache_validity &
  1704. (NFS_INO_INVALID_SIZE
  1705. | NFS_INO_REVAL_PAGECACHE
  1706. | NFS_INO_REVAL_FORCED);
  1707. cache_revalidated = false;
  1708. }
  1709. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1710. inode->i_atime = timespec_to_timespec64(fattr->atime);
  1711. else if (server->caps & NFS_CAP_ATIME) {
  1712. nfsi->cache_validity |= save_cache_validity &
  1713. (NFS_INO_INVALID_ATIME
  1714. | NFS_INO_REVAL_FORCED);
  1715. cache_revalidated = false;
  1716. }
  1717. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1718. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1719. umode_t newmode = inode->i_mode & S_IFMT;
  1720. newmode |= fattr->mode & S_IALLUGO;
  1721. inode->i_mode = newmode;
  1722. invalid |= NFS_INO_INVALID_ACCESS
  1723. | NFS_INO_INVALID_ACL;
  1724. attr_changed = true;
  1725. }
  1726. } else if (server->caps & NFS_CAP_MODE) {
  1727. nfsi->cache_validity |= save_cache_validity &
  1728. (NFS_INO_INVALID_OTHER
  1729. | NFS_INO_REVAL_FORCED);
  1730. cache_revalidated = false;
  1731. }
  1732. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1733. if (!uid_eq(inode->i_uid, fattr->uid)) {
  1734. invalid |= NFS_INO_INVALID_ACCESS
  1735. | NFS_INO_INVALID_ACL;
  1736. inode->i_uid = fattr->uid;
  1737. attr_changed = true;
  1738. }
  1739. } else if (server->caps & NFS_CAP_OWNER) {
  1740. nfsi->cache_validity |= save_cache_validity &
  1741. (NFS_INO_INVALID_OTHER
  1742. | NFS_INO_REVAL_FORCED);
  1743. cache_revalidated = false;
  1744. }
  1745. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1746. if (!gid_eq(inode->i_gid, fattr->gid)) {
  1747. invalid |= NFS_INO_INVALID_ACCESS
  1748. | NFS_INO_INVALID_ACL;
  1749. inode->i_gid = fattr->gid;
  1750. attr_changed = true;
  1751. }
  1752. } else if (server->caps & NFS_CAP_OWNER_GROUP) {
  1753. nfsi->cache_validity |= save_cache_validity &
  1754. (NFS_INO_INVALID_OTHER
  1755. | NFS_INO_REVAL_FORCED);
  1756. cache_revalidated = false;
  1757. }
  1758. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1759. if (inode->i_nlink != fattr->nlink) {
  1760. if (S_ISDIR(inode->i_mode))
  1761. invalid |= NFS_INO_INVALID_DATA;
  1762. set_nlink(inode, fattr->nlink);
  1763. attr_changed = true;
  1764. }
  1765. } else if (server->caps & NFS_CAP_NLINK) {
  1766. nfsi->cache_validity |= save_cache_validity &
  1767. (NFS_INO_INVALID_OTHER
  1768. | NFS_INO_REVAL_FORCED);
  1769. cache_revalidated = false;
  1770. }
  1771. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1772. /*
  1773. * report the blocks in 512byte units
  1774. */
  1775. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1776. } else if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1777. inode->i_blocks = fattr->du.nfs2.blocks;
  1778. else
  1779. cache_revalidated = false;
  1780. /* Update attrtimeo value if we're out of the unstable period */
  1781. if (attr_changed) {
  1782. invalid &= ~NFS_INO_INVALID_ATTR;
  1783. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1784. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1785. nfsi->attrtimeo_timestamp = now;
  1786. /* Set barrier to be more recent than all outstanding updates */
  1787. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1788. } else {
  1789. if (cache_revalidated) {
  1790. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
  1791. nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1792. nfsi->attrtimeo <<= 1;
  1793. if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
  1794. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1795. }
  1796. nfsi->attrtimeo_timestamp = now;
  1797. }
  1798. /* Set the barrier to be more recent than this fattr */
  1799. if ((long)fattr->gencount - (long)nfsi->attr_gencount > 0)
  1800. nfsi->attr_gencount = fattr->gencount;
  1801. }
  1802. /* Don't invalidate the data if we were to blame */
  1803. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1804. || S_ISLNK(inode->i_mode)))
  1805. invalid &= ~NFS_INO_INVALID_DATA;
  1806. nfs_set_cache_invalid(inode, invalid);
  1807. return 0;
  1808. out_err:
  1809. /*
  1810. * No need to worry about unhashing the dentry, as the
  1811. * lookup validation will know that the inode is bad.
  1812. * (But we fall through to invalidate the caches.)
  1813. */
  1814. nfs_invalidate_inode(inode);
  1815. return -ESTALE;
  1816. }
  1817. struct inode *nfs_alloc_inode(struct super_block *sb)
  1818. {
  1819. struct nfs_inode *nfsi;
  1820. nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1821. if (!nfsi)
  1822. return NULL;
  1823. nfsi->flags = 0UL;
  1824. nfsi->cache_validity = 0UL;
  1825. #if IS_ENABLED(CONFIG_NFS_V4)
  1826. nfsi->nfs4_acl = NULL;
  1827. #endif /* CONFIG_NFS_V4 */
  1828. return &nfsi->vfs_inode;
  1829. }
  1830. EXPORT_SYMBOL_GPL(nfs_alloc_inode);
  1831. static void nfs_i_callback(struct rcu_head *head)
  1832. {
  1833. struct inode *inode = container_of(head, struct inode, i_rcu);
  1834. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1835. }
  1836. void nfs_destroy_inode(struct inode *inode)
  1837. {
  1838. call_rcu(&inode->i_rcu, nfs_i_callback);
  1839. }
  1840. EXPORT_SYMBOL_GPL(nfs_destroy_inode);
  1841. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1842. {
  1843. #if IS_ENABLED(CONFIG_NFS_V4)
  1844. INIT_LIST_HEAD(&nfsi->open_states);
  1845. nfsi->delegation = NULL;
  1846. init_rwsem(&nfsi->rwsem);
  1847. nfsi->layout = NULL;
  1848. #endif
  1849. }
  1850. static void init_once(void *foo)
  1851. {
  1852. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1853. inode_init_once(&nfsi->vfs_inode);
  1854. INIT_LIST_HEAD(&nfsi->open_files);
  1855. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1856. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1857. INIT_LIST_HEAD(&nfsi->commit_info.list);
  1858. atomic_long_set(&nfsi->nrequests, 0);
  1859. atomic_long_set(&nfsi->commit_info.ncommit, 0);
  1860. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  1861. init_rwsem(&nfsi->rmdir_sem);
  1862. mutex_init(&nfsi->commit_mutex);
  1863. nfs4_init_once(nfsi);
  1864. }
  1865. static int __init nfs_init_inodecache(void)
  1866. {
  1867. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1868. sizeof(struct nfs_inode),
  1869. 0, (SLAB_RECLAIM_ACCOUNT|
  1870. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1871. init_once);
  1872. if (nfs_inode_cachep == NULL)
  1873. return -ENOMEM;
  1874. return 0;
  1875. }
  1876. static void nfs_destroy_inodecache(void)
  1877. {
  1878. /*
  1879. * Make sure all delayed rcu free inodes are flushed before we
  1880. * destroy cache.
  1881. */
  1882. rcu_barrier();
  1883. kmem_cache_destroy(nfs_inode_cachep);
  1884. }
  1885. struct workqueue_struct *nfsiod_workqueue;
  1886. EXPORT_SYMBOL_GPL(nfsiod_workqueue);
  1887. /*
  1888. * start up the nfsiod workqueue
  1889. */
  1890. static int nfsiod_start(void)
  1891. {
  1892. struct workqueue_struct *wq;
  1893. dprintk("RPC: creating workqueue nfsiod\n");
  1894. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1895. if (wq == NULL)
  1896. return -ENOMEM;
  1897. nfsiod_workqueue = wq;
  1898. return 0;
  1899. }
  1900. /*
  1901. * Destroy the nfsiod workqueue
  1902. */
  1903. static void nfsiod_stop(void)
  1904. {
  1905. struct workqueue_struct *wq;
  1906. wq = nfsiod_workqueue;
  1907. if (wq == NULL)
  1908. return;
  1909. nfsiod_workqueue = NULL;
  1910. destroy_workqueue(wq);
  1911. }
  1912. unsigned int nfs_net_id;
  1913. EXPORT_SYMBOL_GPL(nfs_net_id);
  1914. static int nfs_net_init(struct net *net)
  1915. {
  1916. nfs_clients_init(net);
  1917. return nfs_fs_proc_net_init(net);
  1918. }
  1919. static void nfs_net_exit(struct net *net)
  1920. {
  1921. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1922. nfs_fs_proc_net_exit(net);
  1923. nfs_cleanup_cb_ident_idr(net);
  1924. WARN_ON_ONCE(!list_empty(&nn->nfs_client_list));
  1925. WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list));
  1926. }
  1927. static struct pernet_operations nfs_net_ops = {
  1928. .init = nfs_net_init,
  1929. .exit = nfs_net_exit,
  1930. .id = &nfs_net_id,
  1931. .size = sizeof(struct nfs_net),
  1932. };
  1933. /*
  1934. * Initialize NFS
  1935. */
  1936. static int __init init_nfs_fs(void)
  1937. {
  1938. int err;
  1939. err = register_pernet_subsys(&nfs_net_ops);
  1940. if (err < 0)
  1941. goto out9;
  1942. err = nfs_fscache_register();
  1943. if (err < 0)
  1944. goto out8;
  1945. err = nfsiod_start();
  1946. if (err)
  1947. goto out7;
  1948. err = nfs_fs_proc_init();
  1949. if (err)
  1950. goto out6;
  1951. err = nfs_init_nfspagecache();
  1952. if (err)
  1953. goto out5;
  1954. err = nfs_init_inodecache();
  1955. if (err)
  1956. goto out4;
  1957. err = nfs_init_readpagecache();
  1958. if (err)
  1959. goto out3;
  1960. err = nfs_init_writepagecache();
  1961. if (err)
  1962. goto out2;
  1963. err = nfs_init_directcache();
  1964. if (err)
  1965. goto out1;
  1966. rpc_proc_register(&init_net, &nfs_rpcstat);
  1967. err = register_nfs_fs();
  1968. if (err)
  1969. goto out0;
  1970. return 0;
  1971. out0:
  1972. rpc_proc_unregister(&init_net, "nfs");
  1973. nfs_destroy_directcache();
  1974. out1:
  1975. nfs_destroy_writepagecache();
  1976. out2:
  1977. nfs_destroy_readpagecache();
  1978. out3:
  1979. nfs_destroy_inodecache();
  1980. out4:
  1981. nfs_destroy_nfspagecache();
  1982. out5:
  1983. nfs_fs_proc_exit();
  1984. out6:
  1985. nfsiod_stop();
  1986. out7:
  1987. nfs_fscache_unregister();
  1988. out8:
  1989. unregister_pernet_subsys(&nfs_net_ops);
  1990. out9:
  1991. return err;
  1992. }
  1993. static void __exit exit_nfs_fs(void)
  1994. {
  1995. nfs_destroy_directcache();
  1996. nfs_destroy_writepagecache();
  1997. nfs_destroy_readpagecache();
  1998. nfs_destroy_inodecache();
  1999. nfs_destroy_nfspagecache();
  2000. nfs_fscache_unregister();
  2001. unregister_pernet_subsys(&nfs_net_ops);
  2002. rpc_proc_unregister(&init_net, "nfs");
  2003. unregister_nfs_fs();
  2004. nfs_fs_proc_exit();
  2005. nfsiod_stop();
  2006. }
  2007. /* Not quite true; I just maintain it */
  2008. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  2009. MODULE_LICENSE("GPL");
  2010. module_param(enable_ino64, bool, 0644);
  2011. module_init(init_nfs_fs)
  2012. module_exit(exit_nfs_fs)