inode.c 44 KB

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