inode.c 56 KB

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