inode.c 43 KB

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