inode.c 56 KB

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