inode.c 42 KB

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