inode.c 44 KB

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