inode.c 44 KB

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