inode.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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/nfs_idmap.h>
  35. #include <linux/vfs.h>
  36. #include <linux/inet.h>
  37. #include <linux/nfs_xdr.h>
  38. #include <asm/system.h>
  39. #include <asm/uaccess.h>
  40. #include "nfs4_fs.h"
  41. #include "callback.h"
  42. #include "delegation.h"
  43. #include "iostat.h"
  44. #include "internal.h"
  45. #include "fscache.h"
  46. #include "dns_resolve.h"
  47. #define NFSDBG_FACILITY NFSDBG_VFS
  48. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  49. /* Default is to see 64-bit inode numbers */
  50. static int enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  51. static void nfs_invalidate_inode(struct inode *);
  52. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  53. static struct kmem_cache * nfs_inode_cachep;
  54. static inline unsigned long
  55. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  56. {
  57. return nfs_fileid_to_ino_t(fattr->fileid);
  58. }
  59. /**
  60. * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
  61. * @word: long word containing the bit lock
  62. */
  63. int nfs_wait_bit_killable(void *word)
  64. {
  65. if (fatal_signal_pending(current))
  66. return -ERESTARTSYS;
  67. schedule();
  68. return 0;
  69. }
  70. /**
  71. * nfs_compat_user_ino64 - returns the user-visible inode number
  72. * @fileid: 64-bit fileid
  73. *
  74. * This function returns a 32-bit inode number if the boot parameter
  75. * nfs.enable_ino64 is zero.
  76. */
  77. u64 nfs_compat_user_ino64(u64 fileid)
  78. {
  79. int ino;
  80. if (enable_ino64)
  81. return fileid;
  82. ino = fileid;
  83. if (sizeof(ino) < sizeof(fileid))
  84. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  85. return ino;
  86. }
  87. int nfs_write_inode(struct inode *inode, int sync)
  88. {
  89. int ret;
  90. if (sync) {
  91. ret = filemap_fdatawait(inode->i_mapping);
  92. if (ret == 0)
  93. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  94. } else
  95. ret = nfs_commit_inode(inode, 0);
  96. if (ret >= 0)
  97. return 0;
  98. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  99. return ret;
  100. }
  101. void nfs_clear_inode(struct inode *inode)
  102. {
  103. /*
  104. * The following should never happen...
  105. */
  106. BUG_ON(nfs_have_writebacks(inode));
  107. BUG_ON(!list_empty(&NFS_I(inode)->open_files));
  108. nfs_zap_acl_cache(inode);
  109. nfs_access_zap_cache(inode);
  110. nfs_fscache_release_inode_cookie(inode);
  111. }
  112. /**
  113. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  114. */
  115. int nfs_sync_mapping(struct address_space *mapping)
  116. {
  117. int ret;
  118. if (mapping->nrpages == 0)
  119. return 0;
  120. unmap_mapping_range(mapping, 0, 0, 0);
  121. ret = filemap_write_and_wait(mapping);
  122. if (ret != 0)
  123. goto out;
  124. ret = nfs_wb_all(mapping->host);
  125. out:
  126. return ret;
  127. }
  128. /*
  129. * Invalidate the local caches
  130. */
  131. static void nfs_zap_caches_locked(struct inode *inode)
  132. {
  133. struct nfs_inode *nfsi = NFS_I(inode);
  134. int mode = inode->i_mode;
  135. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  136. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  137. nfsi->attrtimeo_timestamp = jiffies;
  138. memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
  139. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  140. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  141. else
  142. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  143. }
  144. void nfs_zap_caches(struct inode *inode)
  145. {
  146. spin_lock(&inode->i_lock);
  147. nfs_zap_caches_locked(inode);
  148. spin_unlock(&inode->i_lock);
  149. }
  150. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  151. {
  152. if (mapping->nrpages != 0) {
  153. spin_lock(&inode->i_lock);
  154. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  155. spin_unlock(&inode->i_lock);
  156. }
  157. }
  158. void nfs_zap_acl_cache(struct inode *inode)
  159. {
  160. void (*clear_acl_cache)(struct inode *);
  161. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  162. if (clear_acl_cache != NULL)
  163. clear_acl_cache(inode);
  164. spin_lock(&inode->i_lock);
  165. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  166. spin_unlock(&inode->i_lock);
  167. }
  168. void nfs_invalidate_atime(struct inode *inode)
  169. {
  170. spin_lock(&inode->i_lock);
  171. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  172. spin_unlock(&inode->i_lock);
  173. }
  174. /*
  175. * Invalidate, but do not unhash, the inode.
  176. * NB: must be called with inode->i_lock held!
  177. */
  178. static void nfs_invalidate_inode(struct inode *inode)
  179. {
  180. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  181. nfs_zap_caches_locked(inode);
  182. }
  183. struct nfs_find_desc {
  184. struct nfs_fh *fh;
  185. struct nfs_fattr *fattr;
  186. };
  187. /*
  188. * In NFSv3 we can have 64bit inode numbers. In order to support
  189. * this, and re-exported directories (also seen in NFSv2)
  190. * we are forced to allow 2 different inodes to have the same
  191. * i_ino.
  192. */
  193. static int
  194. nfs_find_actor(struct inode *inode, void *opaque)
  195. {
  196. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  197. struct nfs_fh *fh = desc->fh;
  198. struct nfs_fattr *fattr = desc->fattr;
  199. if (NFS_FILEID(inode) != fattr->fileid)
  200. return 0;
  201. if (nfs_compare_fh(NFS_FH(inode), fh))
  202. return 0;
  203. if (is_bad_inode(inode) || NFS_STALE(inode))
  204. return 0;
  205. return 1;
  206. }
  207. static int
  208. nfs_init_locked(struct inode *inode, void *opaque)
  209. {
  210. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  211. struct nfs_fattr *fattr = desc->fattr;
  212. set_nfs_fileid(inode, fattr->fileid);
  213. nfs_copy_fh(NFS_FH(inode), desc->fh);
  214. return 0;
  215. }
  216. /* Don't use READDIRPLUS on directories that we believe are too large */
  217. #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
  218. /*
  219. * This is our front-end to iget that looks up inodes by file handle
  220. * instead of inode number.
  221. */
  222. struct inode *
  223. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  224. {
  225. struct nfs_find_desc desc = {
  226. .fh = fh,
  227. .fattr = fattr
  228. };
  229. struct inode *inode = ERR_PTR(-ENOENT);
  230. unsigned long hash;
  231. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
  232. goto out_no_inode;
  233. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  234. goto out_no_inode;
  235. hash = nfs_fattr_to_ino_t(fattr);
  236. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  237. if (inode == NULL) {
  238. inode = ERR_PTR(-ENOMEM);
  239. goto out_no_inode;
  240. }
  241. if (inode->i_state & I_NEW) {
  242. struct nfs_inode *nfsi = NFS_I(inode);
  243. unsigned long now = jiffies;
  244. /* We set i_ino for the few things that still rely on it,
  245. * such as stat(2) */
  246. inode->i_ino = hash;
  247. /* We can't support update_atime(), since the server will reset it */
  248. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  249. inode->i_mode = fattr->mode;
  250. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  251. && nfs_server_capable(inode, NFS_CAP_MODE))
  252. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  253. | NFS_INO_INVALID_ACCESS
  254. | NFS_INO_INVALID_ACL;
  255. /* Why so? Because we want revalidate for devices/FIFOs, and
  256. * that's precisely what we have in nfs_file_inode_operations.
  257. */
  258. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  259. if (S_ISREG(inode->i_mode)) {
  260. inode->i_fop = &nfs_file_operations;
  261. inode->i_data.a_ops = &nfs_file_aops;
  262. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  263. } else if (S_ISDIR(inode->i_mode)) {
  264. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  265. inode->i_fop = &nfs_dir_operations;
  266. if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
  267. && fattr->size <= NFS_LIMIT_READDIRPLUS)
  268. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  269. /* Deal with crossing mountpoints */
  270. if ((fattr->valid & NFS_ATTR_FATTR_FSID)
  271. && !nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
  272. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  273. inode->i_op = &nfs_referral_inode_operations;
  274. else
  275. inode->i_op = &nfs_mountpoint_inode_operations;
  276. inode->i_fop = NULL;
  277. set_bit(NFS_INO_MOUNTPOINT, &nfsi->flags);
  278. }
  279. } else if (S_ISLNK(inode->i_mode))
  280. inode->i_op = &nfs_symlink_inode_operations;
  281. else
  282. init_special_inode(inode, inode->i_mode, fattr->rdev);
  283. memset(&inode->i_atime, 0, sizeof(inode->i_atime));
  284. memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
  285. memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
  286. nfsi->change_attr = 0;
  287. inode->i_size = 0;
  288. inode->i_nlink = 0;
  289. inode->i_uid = -2;
  290. inode->i_gid = -2;
  291. inode->i_blocks = 0;
  292. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  293. nfsi->read_cache_jiffies = fattr->time_start;
  294. nfsi->attr_gencount = fattr->gencount;
  295. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  296. inode->i_atime = fattr->atime;
  297. else if (nfs_server_capable(inode, NFS_CAP_ATIME))
  298. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  299. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  300. inode->i_mtime = fattr->mtime;
  301. else if (nfs_server_capable(inode, NFS_CAP_MTIME))
  302. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  303. | NFS_INO_INVALID_DATA;
  304. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  305. inode->i_ctime = fattr->ctime;
  306. else if (nfs_server_capable(inode, NFS_CAP_CTIME))
  307. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  308. | NFS_INO_INVALID_ACCESS
  309. | NFS_INO_INVALID_ACL;
  310. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  311. nfsi->change_attr = fattr->change_attr;
  312. else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
  313. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  314. | NFS_INO_INVALID_DATA;
  315. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  316. inode->i_size = nfs_size_to_loff_t(fattr->size);
  317. else
  318. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  319. | NFS_INO_INVALID_DATA
  320. | NFS_INO_REVAL_PAGECACHE;
  321. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  322. inode->i_nlink = fattr->nlink;
  323. else if (nfs_server_capable(inode, NFS_CAP_NLINK))
  324. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  325. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  326. inode->i_uid = fattr->uid;
  327. else if (nfs_server_capable(inode, NFS_CAP_OWNER))
  328. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  329. | NFS_INO_INVALID_ACCESS
  330. | NFS_INO_INVALID_ACL;
  331. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  332. inode->i_gid = fattr->gid;
  333. else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
  334. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  335. | NFS_INO_INVALID_ACCESS
  336. | NFS_INO_INVALID_ACL;
  337. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  338. inode->i_blocks = fattr->du.nfs2.blocks;
  339. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  340. /*
  341. * report the blocks in 512byte units
  342. */
  343. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  344. }
  345. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  346. nfsi->attrtimeo_timestamp = now;
  347. nfsi->access_cache = RB_ROOT;
  348. nfs_fscache_init_inode_cookie(inode);
  349. unlock_new_inode(inode);
  350. } else
  351. nfs_refresh_inode(inode, fattr);
  352. dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
  353. inode->i_sb->s_id,
  354. (long long)NFS_FILEID(inode),
  355. atomic_read(&inode->i_count));
  356. out:
  357. return inode;
  358. out_no_inode:
  359. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  360. goto out;
  361. }
  362. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE)
  363. int
  364. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  365. {
  366. struct inode *inode = dentry->d_inode;
  367. struct nfs_fattr fattr;
  368. int error;
  369. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  370. /* skip mode change if it's just for clearing setuid/setgid */
  371. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  372. attr->ia_valid &= ~ATTR_MODE;
  373. if (attr->ia_valid & ATTR_SIZE) {
  374. if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
  375. attr->ia_valid &= ~ATTR_SIZE;
  376. }
  377. /* Optimization: if the end result is no change, don't RPC */
  378. attr->ia_valid &= NFS_VALID_ATTRS;
  379. if ((attr->ia_valid & ~ATTR_FILE) == 0)
  380. return 0;
  381. /* Write all dirty data */
  382. if (S_ISREG(inode->i_mode)) {
  383. filemap_write_and_wait(inode->i_mapping);
  384. nfs_wb_all(inode);
  385. }
  386. /*
  387. * Return any delegations if we're going to change ACLs
  388. */
  389. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  390. nfs_inode_return_delegation(inode);
  391. error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
  392. if (error == 0)
  393. nfs_refresh_inode(inode, &fattr);
  394. return error;
  395. }
  396. /**
  397. * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
  398. * @inode: inode of the file used
  399. * @offset: file offset to start truncating
  400. *
  401. * This is a copy of the common vmtruncate, but with the locking
  402. * corrected to take into account the fact that NFS requires
  403. * inode->i_size to be updated under the inode->i_lock.
  404. */
  405. static int nfs_vmtruncate(struct inode * inode, loff_t offset)
  406. {
  407. loff_t oldsize;
  408. int err;
  409. err = inode_newsize_ok(inode, offset);
  410. if (err)
  411. goto out;
  412. spin_lock(&inode->i_lock);
  413. oldsize = inode->i_size;
  414. i_size_write(inode, offset);
  415. spin_unlock(&inode->i_lock);
  416. truncate_pagecache(inode, oldsize, offset);
  417. out:
  418. return err;
  419. }
  420. /**
  421. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  422. * @inode: pointer to struct inode
  423. * @attr: pointer to struct iattr
  424. *
  425. * Note: we do this in the *proc.c in order to ensure that
  426. * it works for things like exclusive creates too.
  427. */
  428. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
  429. {
  430. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  431. spin_lock(&inode->i_lock);
  432. if ((attr->ia_valid & ATTR_MODE) != 0) {
  433. int mode = attr->ia_mode & S_IALLUGO;
  434. mode |= inode->i_mode & ~S_IALLUGO;
  435. inode->i_mode = mode;
  436. }
  437. if ((attr->ia_valid & ATTR_UID) != 0)
  438. inode->i_uid = attr->ia_uid;
  439. if ((attr->ia_valid & ATTR_GID) != 0)
  440. inode->i_gid = attr->ia_gid;
  441. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  442. spin_unlock(&inode->i_lock);
  443. }
  444. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  445. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  446. nfs_vmtruncate(inode, attr->ia_size);
  447. }
  448. }
  449. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  450. {
  451. struct inode *inode = dentry->d_inode;
  452. int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
  453. int err;
  454. /*
  455. * Flush out writes to the server in order to update c/mtime.
  456. *
  457. * Hold the i_mutex to suspend application writes temporarily;
  458. * this prevents long-running writing applications from blocking
  459. * nfs_wb_nocommit.
  460. */
  461. if (S_ISREG(inode->i_mode)) {
  462. mutex_lock(&inode->i_mutex);
  463. nfs_wb_nocommit(inode);
  464. mutex_unlock(&inode->i_mutex);
  465. }
  466. /*
  467. * We may force a getattr if the user cares about atime.
  468. *
  469. * Note that we only have to check the vfsmount flags here:
  470. * - NFS always sets S_NOATIME by so checking it would give a
  471. * bogus result
  472. * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
  473. * no point in checking those.
  474. */
  475. if ((mnt->mnt_flags & MNT_NOATIME) ||
  476. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  477. need_atime = 0;
  478. if (need_atime)
  479. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  480. else
  481. err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  482. if (!err) {
  483. generic_fillattr(inode, stat);
  484. stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
  485. }
  486. return err;
  487. }
  488. /**
  489. * nfs_close_context - Common close_context() routine NFSv2/v3
  490. * @ctx: pointer to context
  491. * @is_sync: is this a synchronous close
  492. *
  493. * always ensure that the attributes are up to date if we're mounted
  494. * with close-to-open semantics
  495. */
  496. void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
  497. {
  498. struct inode *inode;
  499. struct nfs_server *server;
  500. if (!(ctx->mode & FMODE_WRITE))
  501. return;
  502. if (!is_sync)
  503. return;
  504. inode = ctx->path.dentry->d_inode;
  505. if (!list_empty(&NFS_I(inode)->open_files))
  506. return;
  507. server = NFS_SERVER(inode);
  508. if (server->flags & NFS_MOUNT_NOCTO)
  509. return;
  510. nfs_revalidate_inode(server, inode);
  511. }
  512. static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
  513. {
  514. struct nfs_open_context *ctx;
  515. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  516. if (ctx != NULL) {
  517. ctx->path.dentry = dget(dentry);
  518. ctx->path.mnt = mntget(mnt);
  519. ctx->cred = get_rpccred(cred);
  520. ctx->state = NULL;
  521. ctx->lockowner = current->files;
  522. ctx->flags = 0;
  523. ctx->error = 0;
  524. ctx->dir_cookie = 0;
  525. atomic_set(&ctx->count, 1);
  526. }
  527. return ctx;
  528. }
  529. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  530. {
  531. if (ctx != NULL)
  532. atomic_inc(&ctx->count);
  533. return ctx;
  534. }
  535. static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
  536. {
  537. struct inode *inode = ctx->path.dentry->d_inode;
  538. if (!atomic_dec_and_lock(&ctx->count, &inode->i_lock))
  539. return;
  540. list_del(&ctx->list);
  541. spin_unlock(&inode->i_lock);
  542. NFS_PROTO(inode)->close_context(ctx, is_sync);
  543. if (ctx->cred != NULL)
  544. put_rpccred(ctx->cred);
  545. path_put(&ctx->path);
  546. kfree(ctx);
  547. }
  548. void put_nfs_open_context(struct nfs_open_context *ctx)
  549. {
  550. __put_nfs_open_context(ctx, 0);
  551. }
  552. static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
  553. {
  554. __put_nfs_open_context(ctx, 1);
  555. }
  556. /*
  557. * Ensure that mmap has a recent RPC credential for use when writing out
  558. * shared pages
  559. */
  560. static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  561. {
  562. struct inode *inode = filp->f_path.dentry->d_inode;
  563. struct nfs_inode *nfsi = NFS_I(inode);
  564. filp->private_data = get_nfs_open_context(ctx);
  565. spin_lock(&inode->i_lock);
  566. list_add(&ctx->list, &nfsi->open_files);
  567. spin_unlock(&inode->i_lock);
  568. }
  569. /*
  570. * Given an inode, search for an open context with the desired characteristics
  571. */
  572. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  573. {
  574. struct nfs_inode *nfsi = NFS_I(inode);
  575. struct nfs_open_context *pos, *ctx = NULL;
  576. spin_lock(&inode->i_lock);
  577. list_for_each_entry(pos, &nfsi->open_files, list) {
  578. if (cred != NULL && pos->cred != cred)
  579. continue;
  580. if ((pos->mode & mode) == mode) {
  581. ctx = get_nfs_open_context(pos);
  582. break;
  583. }
  584. }
  585. spin_unlock(&inode->i_lock);
  586. return ctx;
  587. }
  588. static void nfs_file_clear_open_context(struct file *filp)
  589. {
  590. struct inode *inode = filp->f_path.dentry->d_inode;
  591. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  592. if (ctx) {
  593. filp->private_data = NULL;
  594. spin_lock(&inode->i_lock);
  595. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  596. spin_unlock(&inode->i_lock);
  597. put_nfs_open_context_sync(ctx);
  598. }
  599. }
  600. /*
  601. * These allocate and release file read/write context information.
  602. */
  603. int nfs_open(struct inode *inode, struct file *filp)
  604. {
  605. struct nfs_open_context *ctx;
  606. struct rpc_cred *cred;
  607. cred = rpc_lookup_cred();
  608. if (IS_ERR(cred))
  609. return PTR_ERR(cred);
  610. ctx = alloc_nfs_open_context(filp->f_path.mnt, filp->f_path.dentry, cred);
  611. put_rpccred(cred);
  612. if (ctx == NULL)
  613. return -ENOMEM;
  614. ctx->mode = filp->f_mode;
  615. nfs_file_set_open_context(filp, ctx);
  616. put_nfs_open_context(ctx);
  617. nfs_fscache_set_inode_cookie(inode, filp);
  618. return 0;
  619. }
  620. int nfs_release(struct inode *inode, struct file *filp)
  621. {
  622. nfs_file_clear_open_context(filp);
  623. return 0;
  624. }
  625. /*
  626. * This function is called whenever some part of NFS notices that
  627. * the cached attributes have to be refreshed.
  628. */
  629. int
  630. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  631. {
  632. int status = -ESTALE;
  633. struct nfs_fattr fattr;
  634. struct nfs_inode *nfsi = NFS_I(inode);
  635. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  636. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  637. if (is_bad_inode(inode))
  638. goto out;
  639. if (NFS_STALE(inode))
  640. goto out;
  641. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  642. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
  643. if (status != 0) {
  644. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  645. inode->i_sb->s_id,
  646. (long long)NFS_FILEID(inode), status);
  647. if (status == -ESTALE) {
  648. nfs_zap_caches(inode);
  649. if (!S_ISDIR(inode->i_mode))
  650. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  651. }
  652. goto out;
  653. }
  654. status = nfs_refresh_inode(inode, &fattr);
  655. if (status) {
  656. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  657. inode->i_sb->s_id,
  658. (long long)NFS_FILEID(inode), status);
  659. goto out;
  660. }
  661. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  662. nfs_zap_acl_cache(inode);
  663. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  664. inode->i_sb->s_id,
  665. (long long)NFS_FILEID(inode));
  666. out:
  667. return status;
  668. }
  669. int nfs_attribute_timeout(struct inode *inode)
  670. {
  671. struct nfs_inode *nfsi = NFS_I(inode);
  672. if (nfs_have_delegation(inode, FMODE_READ))
  673. return 0;
  674. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  675. }
  676. /**
  677. * nfs_revalidate_inode - Revalidate the inode attributes
  678. * @server - pointer to nfs_server struct
  679. * @inode - pointer to inode struct
  680. *
  681. * Updates inode attribute information by retrieving the data from the server.
  682. */
  683. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  684. {
  685. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  686. && !nfs_attribute_timeout(inode))
  687. return NFS_STALE(inode) ? -ESTALE : 0;
  688. return __nfs_revalidate_inode(server, inode);
  689. }
  690. static int nfs_invalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
  691. {
  692. struct nfs_inode *nfsi = NFS_I(inode);
  693. if (mapping->nrpages != 0) {
  694. int ret = invalidate_inode_pages2(mapping);
  695. if (ret < 0)
  696. return ret;
  697. }
  698. spin_lock(&inode->i_lock);
  699. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  700. if (S_ISDIR(inode->i_mode))
  701. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  702. spin_unlock(&inode->i_lock);
  703. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  704. nfs_fscache_reset_inode_cookie(inode);
  705. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  706. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  707. return 0;
  708. }
  709. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  710. {
  711. int ret = 0;
  712. mutex_lock(&inode->i_mutex);
  713. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_DATA) {
  714. ret = nfs_sync_mapping(mapping);
  715. if (ret == 0)
  716. ret = nfs_invalidate_mapping_nolock(inode, mapping);
  717. }
  718. mutex_unlock(&inode->i_mutex);
  719. return ret;
  720. }
  721. /**
  722. * nfs_revalidate_mapping_nolock - Revalidate the pagecache
  723. * @inode - pointer to host inode
  724. * @mapping - pointer to mapping
  725. */
  726. int nfs_revalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
  727. {
  728. struct nfs_inode *nfsi = NFS_I(inode);
  729. int ret = 0;
  730. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  731. || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
  732. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  733. if (ret < 0)
  734. goto out;
  735. }
  736. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  737. ret = nfs_invalidate_mapping_nolock(inode, mapping);
  738. out:
  739. return ret;
  740. }
  741. /**
  742. * nfs_revalidate_mapping - Revalidate the pagecache
  743. * @inode - pointer to host inode
  744. * @mapping - pointer to mapping
  745. *
  746. * This version of the function will take the inode->i_mutex and attempt to
  747. * flush out all dirty data if it needs to invalidate the page cache.
  748. */
  749. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  750. {
  751. struct nfs_inode *nfsi = NFS_I(inode);
  752. int ret = 0;
  753. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  754. || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
  755. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  756. if (ret < 0)
  757. goto out;
  758. }
  759. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  760. ret = nfs_invalidate_mapping(inode, mapping);
  761. out:
  762. return ret;
  763. }
  764. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  765. {
  766. struct nfs_inode *nfsi = NFS_I(inode);
  767. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  768. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  769. && nfsi->change_attr == fattr->pre_change_attr) {
  770. nfsi->change_attr = fattr->change_attr;
  771. if (S_ISDIR(inode->i_mode))
  772. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  773. }
  774. /* If we have atomic WCC data, we may update some attributes */
  775. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  776. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  777. && timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
  778. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  779. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  780. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  781. && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  782. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  783. if (S_ISDIR(inode->i_mode))
  784. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  785. }
  786. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  787. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  788. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  789. && nfsi->npages == 0)
  790. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  791. }
  792. /**
  793. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  794. * @inode - pointer to inode
  795. * @fattr - updated attributes
  796. *
  797. * Verifies the attribute cache. If we have just changed the attributes,
  798. * so that fattr carries weak cache consistency data, then it may
  799. * also update the ctime/mtime/change_attribute.
  800. */
  801. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  802. {
  803. struct nfs_inode *nfsi = NFS_I(inode);
  804. loff_t cur_size, new_isize;
  805. unsigned long invalid = 0;
  806. /* Has the inode gone and changed behind our back? */
  807. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  808. return -EIO;
  809. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  810. return -EIO;
  811. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  812. nfsi->change_attr != fattr->change_attr)
  813. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  814. /* Verify a few of the more important attributes */
  815. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
  816. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  817. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  818. cur_size = i_size_read(inode);
  819. new_isize = nfs_size_to_loff_t(fattr->size);
  820. if (cur_size != new_isize && nfsi->npages == 0)
  821. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  822. }
  823. /* Have any file permissions changed? */
  824. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  825. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  826. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
  827. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  828. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
  829. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  830. /* Has the link count changed? */
  831. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  832. invalid |= NFS_INO_INVALID_ATTR;
  833. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
  834. invalid |= NFS_INO_INVALID_ATIME;
  835. if (invalid != 0)
  836. nfsi->cache_validity |= invalid;
  837. nfsi->read_cache_jiffies = fattr->time_start;
  838. return 0;
  839. }
  840. static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  841. {
  842. if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
  843. return 0;
  844. return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
  845. }
  846. static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  847. {
  848. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  849. return 0;
  850. return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
  851. }
  852. static atomic_long_t nfs_attr_generation_counter;
  853. static unsigned long nfs_read_attr_generation_counter(void)
  854. {
  855. return atomic_long_read(&nfs_attr_generation_counter);
  856. }
  857. unsigned long nfs_inc_attr_generation_counter(void)
  858. {
  859. return atomic_long_inc_return(&nfs_attr_generation_counter);
  860. }
  861. void nfs_fattr_init(struct nfs_fattr *fattr)
  862. {
  863. fattr->valid = 0;
  864. fattr->time_start = jiffies;
  865. fattr->gencount = nfs_inc_attr_generation_counter();
  866. }
  867. /**
  868. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  869. * @inode - pointer to inode
  870. * @fattr - attributes
  871. *
  872. * Attempt to divine whether or not an RPC call reply carrying stale
  873. * attributes got scheduled after another call carrying updated ones.
  874. *
  875. * To do so, the function first assumes that a more recent ctime means
  876. * that the attributes in fattr are newer, however it also attempt to
  877. * catch the case where ctime either didn't change, or went backwards
  878. * (if someone reset the clock on the server) by looking at whether
  879. * or not this RPC call was started after the inode was last updated.
  880. * Note also the check for wraparound of 'attr_gencount'
  881. *
  882. * The function returns 'true' if it thinks the attributes in 'fattr' are
  883. * more recent than the ones cached in the inode.
  884. *
  885. */
  886. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  887. {
  888. const struct nfs_inode *nfsi = NFS_I(inode);
  889. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  890. nfs_ctime_need_update(inode, fattr) ||
  891. nfs_size_need_update(inode, fattr) ||
  892. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  893. }
  894. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  895. {
  896. if (nfs_inode_attrs_need_update(inode, fattr))
  897. return nfs_update_inode(inode, fattr);
  898. return nfs_check_inode_attributes(inode, fattr);
  899. }
  900. /**
  901. * nfs_refresh_inode - try to update the inode attribute cache
  902. * @inode - pointer to inode
  903. * @fattr - updated attributes
  904. *
  905. * Check that an RPC call that returned attributes has not overlapped with
  906. * other recent updates of the inode metadata, then decide whether it is
  907. * safe to do a full update of the inode attributes, or whether just to
  908. * call nfs_check_inode_attributes.
  909. */
  910. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  911. {
  912. int status;
  913. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  914. return 0;
  915. spin_lock(&inode->i_lock);
  916. status = nfs_refresh_inode_locked(inode, fattr);
  917. spin_unlock(&inode->i_lock);
  918. return status;
  919. }
  920. static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  921. {
  922. struct nfs_inode *nfsi = NFS_I(inode);
  923. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  924. if (S_ISDIR(inode->i_mode))
  925. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  926. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  927. return 0;
  928. return nfs_refresh_inode_locked(inode, fattr);
  929. }
  930. /**
  931. * nfs_post_op_update_inode - try to update the inode attribute cache
  932. * @inode - pointer to inode
  933. * @fattr - updated attributes
  934. *
  935. * After an operation that has changed the inode metadata, mark the
  936. * attribute cache as being invalid, then try to update it.
  937. *
  938. * NB: if the server didn't return any post op attributes, this
  939. * function will force the retrieval of attributes before the next
  940. * NFS request. Thus it should be used only for operations that
  941. * are expected to change one or more attributes, to avoid
  942. * unnecessary NFS requests and trips through nfs_update_inode().
  943. */
  944. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  945. {
  946. int status;
  947. spin_lock(&inode->i_lock);
  948. status = nfs_post_op_update_inode_locked(inode, fattr);
  949. spin_unlock(&inode->i_lock);
  950. return status;
  951. }
  952. /**
  953. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  954. * @inode - pointer to inode
  955. * @fattr - updated attributes
  956. *
  957. * After an operation that has changed the inode metadata, mark the
  958. * attribute cache as being invalid, then try to update it. Fake up
  959. * weak cache consistency data, if none exist.
  960. *
  961. * This function is mainly designed to be used by the ->write_done() functions.
  962. */
  963. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  964. {
  965. int status;
  966. spin_lock(&inode->i_lock);
  967. /* Don't do a WCC update if these attributes are already stale */
  968. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  969. !nfs_inode_attrs_need_update(inode, fattr)) {
  970. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  971. | NFS_ATTR_FATTR_PRESIZE
  972. | NFS_ATTR_FATTR_PREMTIME
  973. | NFS_ATTR_FATTR_PRECTIME);
  974. goto out_noforce;
  975. }
  976. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  977. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  978. fattr->pre_change_attr = NFS_I(inode)->change_attr;
  979. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  980. }
  981. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  982. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  983. memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
  984. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  985. }
  986. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  987. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  988. memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
  989. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  990. }
  991. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  992. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  993. fattr->pre_size = i_size_read(inode);
  994. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  995. }
  996. out_noforce:
  997. status = nfs_post_op_update_inode_locked(inode, fattr);
  998. spin_unlock(&inode->i_lock);
  999. return status;
  1000. }
  1001. /*
  1002. * Many nfs protocol calls return the new file attributes after
  1003. * an operation. Here we update the inode to reflect the state
  1004. * of the server's inode.
  1005. *
  1006. * This is a bit tricky because we have to make sure all dirty pages
  1007. * have been sent off to the server before calling invalidate_inode_pages.
  1008. * To make sure no other process adds more write requests while we try
  1009. * our best to flush them, we make them sleep during the attribute refresh.
  1010. *
  1011. * A very similar scenario holds for the dir cache.
  1012. */
  1013. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1014. {
  1015. struct nfs_server *server;
  1016. struct nfs_inode *nfsi = NFS_I(inode);
  1017. loff_t cur_isize, new_isize;
  1018. unsigned long invalid = 0;
  1019. unsigned long now = jiffies;
  1020. unsigned long save_cache_validity;
  1021. dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
  1022. __func__, inode->i_sb->s_id, inode->i_ino,
  1023. atomic_read(&inode->i_count), fattr->valid);
  1024. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  1025. goto out_fileid;
  1026. /*
  1027. * Make sure the inode's type hasn't changed.
  1028. */
  1029. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1030. goto out_changed;
  1031. server = NFS_SERVER(inode);
  1032. /* Update the fsid? */
  1033. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1034. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1035. !test_bit(NFS_INO_MOUNTPOINT, &nfsi->flags))
  1036. server->fsid = fattr->fsid;
  1037. /*
  1038. * Update the read time so we don't revalidate too often.
  1039. */
  1040. nfsi->read_cache_jiffies = fattr->time_start;
  1041. save_cache_validity = nfsi->cache_validity;
  1042. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1043. | NFS_INO_INVALID_ATIME
  1044. | NFS_INO_REVAL_FORCED
  1045. | NFS_INO_REVAL_PAGECACHE);
  1046. /* Do atomic weak cache consistency updates */
  1047. nfs_wcc_update_inode(inode, fattr);
  1048. /* More cache consistency checks */
  1049. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1050. if (nfsi->change_attr != fattr->change_attr) {
  1051. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1052. inode->i_sb->s_id, inode->i_ino);
  1053. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1054. if (S_ISDIR(inode->i_mode))
  1055. nfs_force_lookup_revalidate(inode);
  1056. nfsi->change_attr = fattr->change_attr;
  1057. }
  1058. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1059. invalid |= save_cache_validity;
  1060. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1061. /* NFSv2/v3: Check if the mtime agrees */
  1062. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  1063. dprintk("NFS: mtime change on server for file %s/%ld\n",
  1064. inode->i_sb->s_id, inode->i_ino);
  1065. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1066. if (S_ISDIR(inode->i_mode))
  1067. nfs_force_lookup_revalidate(inode);
  1068. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1069. }
  1070. } else if (server->caps & NFS_CAP_MTIME)
  1071. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1072. | NFS_INO_INVALID_DATA
  1073. | NFS_INO_REVAL_PAGECACHE
  1074. | NFS_INO_REVAL_FORCED);
  1075. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1076. /* If ctime has changed we should definitely clear access+acl caches */
  1077. if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
  1078. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1079. /* and probably clear data for a directory too as utimes can cause
  1080. * havoc with our cache.
  1081. */
  1082. if (S_ISDIR(inode->i_mode)) {
  1083. invalid |= NFS_INO_INVALID_DATA;
  1084. nfs_force_lookup_revalidate(inode);
  1085. }
  1086. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1087. }
  1088. } else if (server->caps & NFS_CAP_CTIME)
  1089. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1090. | NFS_INO_INVALID_ACCESS
  1091. | NFS_INO_INVALID_ACL
  1092. | NFS_INO_REVAL_FORCED);
  1093. /* Check if our cached file size is stale */
  1094. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1095. new_isize = nfs_size_to_loff_t(fattr->size);
  1096. cur_isize = i_size_read(inode);
  1097. if (new_isize != cur_isize) {
  1098. /* Do we perhaps have any outstanding writes, or has
  1099. * the file grown beyond our last write? */
  1100. if (nfsi->npages == 0 || new_isize > cur_isize) {
  1101. i_size_write(inode, new_isize);
  1102. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1103. }
  1104. dprintk("NFS: isize change on server for file %s/%ld\n",
  1105. inode->i_sb->s_id, inode->i_ino);
  1106. }
  1107. } else
  1108. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1109. | NFS_INO_REVAL_PAGECACHE
  1110. | NFS_INO_REVAL_FORCED);
  1111. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1112. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1113. else if (server->caps & NFS_CAP_ATIME)
  1114. invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
  1115. | NFS_INO_REVAL_FORCED);
  1116. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1117. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1118. umode_t newmode = inode->i_mode & S_IFMT;
  1119. newmode |= fattr->mode & S_IALLUGO;
  1120. inode->i_mode = newmode;
  1121. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1122. }
  1123. } else if (server->caps & NFS_CAP_MODE)
  1124. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1125. | NFS_INO_INVALID_ACCESS
  1126. | NFS_INO_INVALID_ACL
  1127. | NFS_INO_REVAL_FORCED);
  1128. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1129. if (inode->i_uid != fattr->uid) {
  1130. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1131. inode->i_uid = fattr->uid;
  1132. }
  1133. } else if (server->caps & NFS_CAP_OWNER)
  1134. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1135. | NFS_INO_INVALID_ACCESS
  1136. | NFS_INO_INVALID_ACL
  1137. | NFS_INO_REVAL_FORCED);
  1138. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1139. if (inode->i_gid != fattr->gid) {
  1140. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1141. inode->i_gid = fattr->gid;
  1142. }
  1143. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1144. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1145. | NFS_INO_INVALID_ACCESS
  1146. | NFS_INO_INVALID_ACL
  1147. | NFS_INO_REVAL_FORCED);
  1148. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1149. if (inode->i_nlink != fattr->nlink) {
  1150. invalid |= NFS_INO_INVALID_ATTR;
  1151. if (S_ISDIR(inode->i_mode))
  1152. invalid |= NFS_INO_INVALID_DATA;
  1153. inode->i_nlink = fattr->nlink;
  1154. }
  1155. } else if (server->caps & NFS_CAP_NLINK)
  1156. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1157. | NFS_INO_REVAL_FORCED);
  1158. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1159. /*
  1160. * report the blocks in 512byte units
  1161. */
  1162. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1163. }
  1164. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1165. inode->i_blocks = fattr->du.nfs2.blocks;
  1166. /* Update attrtimeo value if we're out of the unstable period */
  1167. if (invalid & NFS_INO_INVALID_ATTR) {
  1168. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1169. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1170. nfsi->attrtimeo_timestamp = now;
  1171. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1172. } else {
  1173. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1174. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1175. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1176. nfsi->attrtimeo_timestamp = now;
  1177. }
  1178. }
  1179. invalid &= ~NFS_INO_INVALID_ATTR;
  1180. /* Don't invalidate the data if we were to blame */
  1181. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1182. || S_ISLNK(inode->i_mode)))
  1183. invalid &= ~NFS_INO_INVALID_DATA;
  1184. if (!nfs_have_delegation(inode, FMODE_READ) ||
  1185. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1186. nfsi->cache_validity |= invalid;
  1187. return 0;
  1188. out_changed:
  1189. /*
  1190. * Big trouble! The inode has become a different object.
  1191. */
  1192. printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
  1193. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1194. out_err:
  1195. /*
  1196. * No need to worry about unhashing the dentry, as the
  1197. * lookup validation will know that the inode is bad.
  1198. * (But we fall through to invalidate the caches.)
  1199. */
  1200. nfs_invalidate_inode(inode);
  1201. return -ESTALE;
  1202. out_fileid:
  1203. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1204. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1205. NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
  1206. (long long)nfsi->fileid, (long long)fattr->fileid);
  1207. goto out_err;
  1208. }
  1209. #ifdef CONFIG_NFS_V4
  1210. /*
  1211. * Clean out any remaining NFSv4 state that might be left over due
  1212. * to open() calls that passed nfs_atomic_lookup, but failed to call
  1213. * nfs_open().
  1214. */
  1215. void nfs4_clear_inode(struct inode *inode)
  1216. {
  1217. /* If we are holding a delegation, return it! */
  1218. nfs_inode_return_delegation_noreclaim(inode);
  1219. /* First call standard NFS clear_inode() code */
  1220. nfs_clear_inode(inode);
  1221. }
  1222. #endif
  1223. struct inode *nfs_alloc_inode(struct super_block *sb)
  1224. {
  1225. struct nfs_inode *nfsi;
  1226. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1227. if (!nfsi)
  1228. return NULL;
  1229. nfsi->flags = 0UL;
  1230. nfsi->cache_validity = 0UL;
  1231. #ifdef CONFIG_NFS_V3_ACL
  1232. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1233. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1234. #endif
  1235. #ifdef CONFIG_NFS_V4
  1236. nfsi->nfs4_acl = NULL;
  1237. #endif /* CONFIG_NFS_V4 */
  1238. return &nfsi->vfs_inode;
  1239. }
  1240. void nfs_destroy_inode(struct inode *inode)
  1241. {
  1242. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1243. }
  1244. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1245. {
  1246. #ifdef CONFIG_NFS_V4
  1247. INIT_LIST_HEAD(&nfsi->open_states);
  1248. nfsi->delegation = NULL;
  1249. nfsi->delegation_state = 0;
  1250. init_rwsem(&nfsi->rwsem);
  1251. #endif
  1252. }
  1253. static void init_once(void *foo)
  1254. {
  1255. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1256. inode_init_once(&nfsi->vfs_inode);
  1257. INIT_LIST_HEAD(&nfsi->open_files);
  1258. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1259. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1260. INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
  1261. nfsi->npages = 0;
  1262. atomic_set(&nfsi->silly_count, 1);
  1263. INIT_HLIST_HEAD(&nfsi->silly_list);
  1264. init_waitqueue_head(&nfsi->waitqueue);
  1265. nfs4_init_once(nfsi);
  1266. }
  1267. static int __init nfs_init_inodecache(void)
  1268. {
  1269. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1270. sizeof(struct nfs_inode),
  1271. 0, (SLAB_RECLAIM_ACCOUNT|
  1272. SLAB_MEM_SPREAD),
  1273. init_once);
  1274. if (nfs_inode_cachep == NULL)
  1275. return -ENOMEM;
  1276. return 0;
  1277. }
  1278. static void nfs_destroy_inodecache(void)
  1279. {
  1280. kmem_cache_destroy(nfs_inode_cachep);
  1281. }
  1282. struct workqueue_struct *nfsiod_workqueue;
  1283. /*
  1284. * start up the nfsiod workqueue
  1285. */
  1286. static int nfsiod_start(void)
  1287. {
  1288. struct workqueue_struct *wq;
  1289. dprintk("RPC: creating workqueue nfsiod\n");
  1290. wq = create_singlethread_workqueue("nfsiod");
  1291. if (wq == NULL)
  1292. return -ENOMEM;
  1293. nfsiod_workqueue = wq;
  1294. return 0;
  1295. }
  1296. /*
  1297. * Destroy the nfsiod workqueue
  1298. */
  1299. static void nfsiod_stop(void)
  1300. {
  1301. struct workqueue_struct *wq;
  1302. wq = nfsiod_workqueue;
  1303. if (wq == NULL)
  1304. return;
  1305. nfsiod_workqueue = NULL;
  1306. destroy_workqueue(wq);
  1307. }
  1308. /*
  1309. * Initialize NFS
  1310. */
  1311. static int __init init_nfs_fs(void)
  1312. {
  1313. int err;
  1314. err = nfs_dns_resolver_init();
  1315. if (err < 0)
  1316. goto out8;
  1317. err = nfs_fscache_register();
  1318. if (err < 0)
  1319. goto out7;
  1320. err = nfsiod_start();
  1321. if (err)
  1322. goto out6;
  1323. err = nfs_fs_proc_init();
  1324. if (err)
  1325. goto out5;
  1326. err = nfs_init_nfspagecache();
  1327. if (err)
  1328. goto out4;
  1329. err = nfs_init_inodecache();
  1330. if (err)
  1331. goto out3;
  1332. err = nfs_init_readpagecache();
  1333. if (err)
  1334. goto out2;
  1335. err = nfs_init_writepagecache();
  1336. if (err)
  1337. goto out1;
  1338. err = nfs_init_directcache();
  1339. if (err)
  1340. goto out0;
  1341. #ifdef CONFIG_PROC_FS
  1342. rpc_proc_register(&nfs_rpcstat);
  1343. #endif
  1344. if ((err = register_nfs_fs()) != 0)
  1345. goto out;
  1346. return 0;
  1347. out:
  1348. #ifdef CONFIG_PROC_FS
  1349. rpc_proc_unregister("nfs");
  1350. #endif
  1351. nfs_destroy_directcache();
  1352. out0:
  1353. nfs_destroy_writepagecache();
  1354. out1:
  1355. nfs_destroy_readpagecache();
  1356. out2:
  1357. nfs_destroy_inodecache();
  1358. out3:
  1359. nfs_destroy_nfspagecache();
  1360. out4:
  1361. nfs_fs_proc_exit();
  1362. out5:
  1363. nfsiod_stop();
  1364. out6:
  1365. nfs_fscache_unregister();
  1366. out7:
  1367. nfs_dns_resolver_destroy();
  1368. out8:
  1369. return err;
  1370. }
  1371. static void __exit exit_nfs_fs(void)
  1372. {
  1373. nfs_destroy_directcache();
  1374. nfs_destroy_writepagecache();
  1375. nfs_destroy_readpagecache();
  1376. nfs_destroy_inodecache();
  1377. nfs_destroy_nfspagecache();
  1378. nfs_fscache_unregister();
  1379. nfs_dns_resolver_destroy();
  1380. #ifdef CONFIG_PROC_FS
  1381. rpc_proc_unregister("nfs");
  1382. #endif
  1383. unregister_nfs_fs();
  1384. nfs_fs_proc_exit();
  1385. nfsiod_stop();
  1386. }
  1387. /* Not quite true; I just maintain it */
  1388. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1389. MODULE_LICENSE("GPL");
  1390. module_param(enable_ino64, bool, 0644);
  1391. module_init(init_nfs_fs)
  1392. module_exit(exit_nfs_fs)