inode.c 43 KB

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