inode.c 45 KB

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