inode.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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. static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
  553. {
  554. __put_nfs_open_context(ctx, 1);
  555. }
  556. /*
  557. * Ensure that mmap has a recent RPC credential for use when writing out
  558. * shared pages
  559. */
  560. static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  561. {
  562. struct inode *inode = filp->f_path.dentry->d_inode;
  563. struct nfs_inode *nfsi = NFS_I(inode);
  564. filp->private_data = get_nfs_open_context(ctx);
  565. spin_lock(&inode->i_lock);
  566. list_add(&ctx->list, &nfsi->open_files);
  567. spin_unlock(&inode->i_lock);
  568. }
  569. /*
  570. * Given an inode, search for an open context with the desired characteristics
  571. */
  572. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  573. {
  574. struct nfs_inode *nfsi = NFS_I(inode);
  575. struct nfs_open_context *pos, *ctx = NULL;
  576. spin_lock(&inode->i_lock);
  577. list_for_each_entry(pos, &nfsi->open_files, list) {
  578. if (cred != NULL && pos->cred != cred)
  579. continue;
  580. if ((pos->mode & mode) == mode) {
  581. ctx = get_nfs_open_context(pos);
  582. break;
  583. }
  584. }
  585. spin_unlock(&inode->i_lock);
  586. return ctx;
  587. }
  588. static void nfs_file_clear_open_context(struct file *filp)
  589. {
  590. struct inode *inode = filp->f_path.dentry->d_inode;
  591. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  592. if (ctx) {
  593. filp->private_data = NULL;
  594. spin_lock(&inode->i_lock);
  595. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  596. spin_unlock(&inode->i_lock);
  597. put_nfs_open_context_sync(ctx);
  598. }
  599. }
  600. /*
  601. * These allocate and release file read/write context information.
  602. */
  603. int nfs_open(struct inode *inode, struct file *filp)
  604. {
  605. struct nfs_open_context *ctx;
  606. struct rpc_cred *cred;
  607. cred = rpc_lookup_cred();
  608. if (IS_ERR(cred))
  609. return PTR_ERR(cred);
  610. ctx = alloc_nfs_open_context(filp->f_path.mnt, filp->f_path.dentry, cred);
  611. put_rpccred(cred);
  612. if (ctx == NULL)
  613. return -ENOMEM;
  614. ctx->mode = filp->f_mode;
  615. nfs_file_set_open_context(filp, ctx);
  616. put_nfs_open_context(ctx);
  617. nfs_fscache_set_inode_cookie(inode, filp);
  618. return 0;
  619. }
  620. int nfs_release(struct inode *inode, struct file *filp)
  621. {
  622. nfs_file_clear_open_context(filp);
  623. return 0;
  624. }
  625. /*
  626. * This function is called whenever some part of NFS notices that
  627. * the cached attributes have to be refreshed.
  628. */
  629. int
  630. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  631. {
  632. int status = -ESTALE;
  633. struct nfs_fattr fattr;
  634. struct nfs_inode *nfsi = NFS_I(inode);
  635. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  636. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  637. if (is_bad_inode(inode))
  638. goto out;
  639. if (NFS_STALE(inode))
  640. goto out;
  641. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  642. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
  643. if (status != 0) {
  644. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  645. inode->i_sb->s_id,
  646. (long long)NFS_FILEID(inode), status);
  647. if (status == -ESTALE) {
  648. nfs_zap_caches(inode);
  649. if (!S_ISDIR(inode->i_mode))
  650. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  651. }
  652. goto out;
  653. }
  654. status = nfs_refresh_inode(inode, &fattr);
  655. if (status) {
  656. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  657. inode->i_sb->s_id,
  658. (long long)NFS_FILEID(inode), status);
  659. goto out;
  660. }
  661. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  662. nfs_zap_acl_cache(inode);
  663. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  664. inode->i_sb->s_id,
  665. (long long)NFS_FILEID(inode));
  666. out:
  667. return status;
  668. }
  669. int nfs_attribute_timeout(struct inode *inode)
  670. {
  671. struct nfs_inode *nfsi = NFS_I(inode);
  672. if (nfs_have_delegation(inode, FMODE_READ))
  673. return 0;
  674. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  675. }
  676. /**
  677. * nfs_revalidate_inode - Revalidate the inode attributes
  678. * @server - pointer to nfs_server struct
  679. * @inode - pointer to inode struct
  680. *
  681. * Updates inode attribute information by retrieving the data from the server.
  682. */
  683. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  684. {
  685. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  686. && !nfs_attribute_timeout(inode))
  687. return NFS_STALE(inode) ? -ESTALE : 0;
  688. return __nfs_revalidate_inode(server, inode);
  689. }
  690. static int nfs_invalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
  691. {
  692. struct nfs_inode *nfsi = NFS_I(inode);
  693. if (mapping->nrpages != 0) {
  694. int ret = invalidate_inode_pages2(mapping);
  695. if (ret < 0)
  696. return ret;
  697. }
  698. spin_lock(&inode->i_lock);
  699. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  700. if (S_ISDIR(inode->i_mode))
  701. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  702. spin_unlock(&inode->i_lock);
  703. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  704. nfs_fscache_reset_inode_cookie(inode);
  705. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  706. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  707. return 0;
  708. }
  709. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  710. {
  711. int ret = 0;
  712. mutex_lock(&inode->i_mutex);
  713. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_DATA) {
  714. ret = nfs_sync_mapping(mapping);
  715. if (ret == 0)
  716. ret = nfs_invalidate_mapping_nolock(inode, mapping);
  717. }
  718. mutex_unlock(&inode->i_mutex);
  719. return ret;
  720. }
  721. /**
  722. * nfs_revalidate_mapping_nolock - Revalidate the pagecache
  723. * @inode - pointer to host inode
  724. * @mapping - pointer to mapping
  725. */
  726. int nfs_revalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
  727. {
  728. struct nfs_inode *nfsi = NFS_I(inode);
  729. int ret = 0;
  730. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  731. || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
  732. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  733. if (ret < 0)
  734. goto out;
  735. }
  736. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  737. ret = nfs_invalidate_mapping_nolock(inode, mapping);
  738. out:
  739. return ret;
  740. }
  741. /**
  742. * nfs_revalidate_mapping - Revalidate the pagecache
  743. * @inode - pointer to host inode
  744. * @mapping - pointer to mapping
  745. *
  746. * This version of the function will take the inode->i_mutex and attempt to
  747. * flush out all dirty data if it needs to invalidate the page cache.
  748. */
  749. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  750. {
  751. struct nfs_inode *nfsi = NFS_I(inode);
  752. int ret = 0;
  753. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  754. || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
  755. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  756. if (ret < 0)
  757. goto out;
  758. }
  759. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  760. ret = nfs_invalidate_mapping(inode, mapping);
  761. out:
  762. return ret;
  763. }
  764. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  765. {
  766. struct nfs_inode *nfsi = NFS_I(inode);
  767. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  768. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  769. && nfsi->change_attr == fattr->pre_change_attr) {
  770. nfsi->change_attr = fattr->change_attr;
  771. if (S_ISDIR(inode->i_mode))
  772. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  773. }
  774. /* If we have atomic WCC data, we may update some attributes */
  775. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  776. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  777. && timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
  778. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  779. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  780. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  781. && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  782. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  783. if (S_ISDIR(inode->i_mode))
  784. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  785. }
  786. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  787. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  788. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  789. && nfsi->npages == 0)
  790. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  791. }
  792. /**
  793. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  794. * @inode - pointer to inode
  795. * @fattr - updated attributes
  796. *
  797. * Verifies the attribute cache. If we have just changed the attributes,
  798. * so that fattr carries weak cache consistency data, then it may
  799. * also update the ctime/mtime/change_attribute.
  800. */
  801. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  802. {
  803. struct nfs_inode *nfsi = NFS_I(inode);
  804. loff_t cur_size, new_isize;
  805. unsigned long invalid = 0;
  806. /* Has the inode gone and changed behind our back? */
  807. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  808. return -EIO;
  809. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  810. return -EIO;
  811. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  812. nfsi->change_attr != fattr->change_attr)
  813. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  814. /* Verify a few of the more important attributes */
  815. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
  816. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  817. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  818. cur_size = i_size_read(inode);
  819. new_isize = nfs_size_to_loff_t(fattr->size);
  820. if (cur_size != new_isize && nfsi->npages == 0)
  821. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  822. }
  823. /* Have any file permissions changed? */
  824. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  825. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  826. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
  827. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  828. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
  829. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  830. /* Has the link count changed? */
  831. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  832. invalid |= NFS_INO_INVALID_ATTR;
  833. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
  834. invalid |= NFS_INO_INVALID_ATIME;
  835. if (invalid != 0)
  836. nfsi->cache_validity |= invalid;
  837. nfsi->read_cache_jiffies = fattr->time_start;
  838. return 0;
  839. }
  840. static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  841. {
  842. if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
  843. return 0;
  844. return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
  845. }
  846. static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  847. {
  848. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  849. return 0;
  850. return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
  851. }
  852. static atomic_long_t nfs_attr_generation_counter;
  853. static unsigned long nfs_read_attr_generation_counter(void)
  854. {
  855. return atomic_long_read(&nfs_attr_generation_counter);
  856. }
  857. unsigned long nfs_inc_attr_generation_counter(void)
  858. {
  859. return atomic_long_inc_return(&nfs_attr_generation_counter);
  860. }
  861. void nfs_fattr_init(struct nfs_fattr *fattr)
  862. {
  863. fattr->valid = 0;
  864. fattr->time_start = jiffies;
  865. fattr->gencount = nfs_inc_attr_generation_counter();
  866. }
  867. /**
  868. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  869. * @inode - pointer to inode
  870. * @fattr - attributes
  871. *
  872. * Attempt to divine whether or not an RPC call reply carrying stale
  873. * attributes got scheduled after another call carrying updated ones.
  874. *
  875. * To do so, the function first assumes that a more recent ctime means
  876. * that the attributes in fattr are newer, however it also attempt to
  877. * catch the case where ctime either didn't change, or went backwards
  878. * (if someone reset the clock on the server) by looking at whether
  879. * or not this RPC call was started after the inode was last updated.
  880. * Note also the check for wraparound of 'attr_gencount'
  881. *
  882. * The function returns 'true' if it thinks the attributes in 'fattr' are
  883. * more recent than the ones cached in the inode.
  884. *
  885. */
  886. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  887. {
  888. const struct nfs_inode *nfsi = NFS_I(inode);
  889. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  890. nfs_ctime_need_update(inode, fattr) ||
  891. nfs_size_need_update(inode, fattr) ||
  892. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  893. }
  894. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  895. {
  896. if (nfs_inode_attrs_need_update(inode, fattr))
  897. return nfs_update_inode(inode, fattr);
  898. return nfs_check_inode_attributes(inode, fattr);
  899. }
  900. /**
  901. * nfs_refresh_inode - try to update the inode attribute cache
  902. * @inode - pointer to inode
  903. * @fattr - updated attributes
  904. *
  905. * Check that an RPC call that returned attributes has not overlapped with
  906. * other recent updates of the inode metadata, then decide whether it is
  907. * safe to do a full update of the inode attributes, or whether just to
  908. * call nfs_check_inode_attributes.
  909. */
  910. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  911. {
  912. int status;
  913. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  914. return 0;
  915. spin_lock(&inode->i_lock);
  916. status = nfs_refresh_inode_locked(inode, fattr);
  917. spin_unlock(&inode->i_lock);
  918. return status;
  919. }
  920. static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  921. {
  922. struct nfs_inode *nfsi = NFS_I(inode);
  923. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  924. if (S_ISDIR(inode->i_mode))
  925. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  926. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  927. return 0;
  928. return nfs_refresh_inode_locked(inode, fattr);
  929. }
  930. /**
  931. * nfs_post_op_update_inode - try to update the inode attribute cache
  932. * @inode - pointer to inode
  933. * @fattr - updated attributes
  934. *
  935. * After an operation that has changed the inode metadata, mark the
  936. * attribute cache as being invalid, then try to update it.
  937. *
  938. * NB: if the server didn't return any post op attributes, this
  939. * function will force the retrieval of attributes before the next
  940. * NFS request. Thus it should be used only for operations that
  941. * are expected to change one or more attributes, to avoid
  942. * unnecessary NFS requests and trips through nfs_update_inode().
  943. */
  944. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  945. {
  946. int status;
  947. spin_lock(&inode->i_lock);
  948. status = nfs_post_op_update_inode_locked(inode, fattr);
  949. spin_unlock(&inode->i_lock);
  950. return status;
  951. }
  952. /**
  953. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  954. * @inode - pointer to inode
  955. * @fattr - updated attributes
  956. *
  957. * After an operation that has changed the inode metadata, mark the
  958. * attribute cache as being invalid, then try to update it. Fake up
  959. * weak cache consistency data, if none exist.
  960. *
  961. * This function is mainly designed to be used by the ->write_done() functions.
  962. */
  963. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  964. {
  965. int status;
  966. spin_lock(&inode->i_lock);
  967. /* Don't do a WCC update if these attributes are already stale */
  968. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  969. !nfs_inode_attrs_need_update(inode, fattr)) {
  970. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  971. | NFS_ATTR_FATTR_PRESIZE
  972. | NFS_ATTR_FATTR_PREMTIME
  973. | NFS_ATTR_FATTR_PRECTIME);
  974. goto out_noforce;
  975. }
  976. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  977. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  978. fattr->pre_change_attr = NFS_I(inode)->change_attr;
  979. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  980. }
  981. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  982. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  983. memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
  984. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  985. }
  986. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  987. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  988. memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
  989. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  990. }
  991. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  992. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  993. fattr->pre_size = i_size_read(inode);
  994. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  995. }
  996. out_noforce:
  997. status = nfs_post_op_update_inode_locked(inode, fattr);
  998. spin_unlock(&inode->i_lock);
  999. return status;
  1000. }
  1001. /*
  1002. * Many nfs protocol calls return the new file attributes after
  1003. * an operation. Here we update the inode to reflect the state
  1004. * of the server's inode.
  1005. *
  1006. * This is a bit tricky because we have to make sure all dirty pages
  1007. * have been sent off to the server before calling invalidate_inode_pages.
  1008. * To make sure no other process adds more write requests while we try
  1009. * our best to flush them, we make them sleep during the attribute refresh.
  1010. *
  1011. * A very similar scenario holds for the dir cache.
  1012. */
  1013. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1014. {
  1015. struct nfs_server *server;
  1016. struct nfs_inode *nfsi = NFS_I(inode);
  1017. loff_t cur_isize, new_isize;
  1018. unsigned long invalid = 0;
  1019. unsigned long now = jiffies;
  1020. unsigned long save_cache_validity;
  1021. dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
  1022. __func__, inode->i_sb->s_id, inode->i_ino,
  1023. atomic_read(&inode->i_count), fattr->valid);
  1024. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  1025. goto out_fileid;
  1026. /*
  1027. * Make sure the inode's type hasn't changed.
  1028. */
  1029. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1030. goto out_changed;
  1031. server = NFS_SERVER(inode);
  1032. /* Update the fsid? */
  1033. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1034. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1035. !test_bit(NFS_INO_MOUNTPOINT, &nfsi->flags))
  1036. server->fsid = fattr->fsid;
  1037. /*
  1038. * Update the read time so we don't revalidate too often.
  1039. */
  1040. nfsi->read_cache_jiffies = fattr->time_start;
  1041. save_cache_validity = nfsi->cache_validity;
  1042. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1043. | NFS_INO_INVALID_ATIME
  1044. | NFS_INO_REVAL_FORCED
  1045. | NFS_INO_REVAL_PAGECACHE);
  1046. /* Do atomic weak cache consistency updates */
  1047. nfs_wcc_update_inode(inode, fattr);
  1048. /* More cache consistency checks */
  1049. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1050. if (nfsi->change_attr != fattr->change_attr) {
  1051. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1052. inode->i_sb->s_id, inode->i_ino);
  1053. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1054. if (S_ISDIR(inode->i_mode))
  1055. nfs_force_lookup_revalidate(inode);
  1056. nfsi->change_attr = fattr->change_attr;
  1057. }
  1058. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1059. invalid |= save_cache_validity;
  1060. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1061. /* NFSv2/v3: Check if the mtime agrees */
  1062. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  1063. dprintk("NFS: mtime change on server for file %s/%ld\n",
  1064. inode->i_sb->s_id, inode->i_ino);
  1065. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1066. if (S_ISDIR(inode->i_mode))
  1067. nfs_force_lookup_revalidate(inode);
  1068. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1069. }
  1070. } else if (server->caps & NFS_CAP_MTIME)
  1071. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1072. | NFS_INO_INVALID_DATA
  1073. | NFS_INO_REVAL_PAGECACHE
  1074. | NFS_INO_REVAL_FORCED);
  1075. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1076. /* If ctime has changed we should definitely clear access+acl caches */
  1077. if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
  1078. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1079. /* and probably clear data for a directory too as utimes can cause
  1080. * havoc with our cache.
  1081. */
  1082. if (S_ISDIR(inode->i_mode)) {
  1083. invalid |= NFS_INO_INVALID_DATA;
  1084. nfs_force_lookup_revalidate(inode);
  1085. }
  1086. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1087. }
  1088. } else if (server->caps & NFS_CAP_CTIME)
  1089. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1090. | NFS_INO_INVALID_ACCESS
  1091. | NFS_INO_INVALID_ACL
  1092. | NFS_INO_REVAL_FORCED);
  1093. /* Check if our cached file size is stale */
  1094. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1095. new_isize = nfs_size_to_loff_t(fattr->size);
  1096. cur_isize = i_size_read(inode);
  1097. if (new_isize != cur_isize) {
  1098. /* Do we perhaps have any outstanding writes, or has
  1099. * the file grown beyond our last write? */
  1100. if (nfsi->npages == 0 || new_isize > cur_isize) {
  1101. i_size_write(inode, new_isize);
  1102. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1103. }
  1104. dprintk("NFS: isize change on server for file %s/%ld\n",
  1105. inode->i_sb->s_id, inode->i_ino);
  1106. }
  1107. } else
  1108. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1109. | NFS_INO_REVAL_PAGECACHE
  1110. | NFS_INO_REVAL_FORCED);
  1111. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1112. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1113. else if (server->caps & NFS_CAP_ATIME)
  1114. invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
  1115. | NFS_INO_REVAL_FORCED);
  1116. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1117. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1118. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1119. inode->i_mode = fattr->mode;
  1120. }
  1121. } else if (server->caps & NFS_CAP_MODE)
  1122. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1123. | NFS_INO_INVALID_ACCESS
  1124. | NFS_INO_INVALID_ACL
  1125. | NFS_INO_REVAL_FORCED);
  1126. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1127. if (inode->i_uid != fattr->uid) {
  1128. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1129. inode->i_uid = fattr->uid;
  1130. }
  1131. } else if (server->caps & NFS_CAP_OWNER)
  1132. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1133. | NFS_INO_INVALID_ACCESS
  1134. | NFS_INO_INVALID_ACL
  1135. | NFS_INO_REVAL_FORCED);
  1136. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1137. if (inode->i_gid != fattr->gid) {
  1138. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1139. inode->i_gid = fattr->gid;
  1140. }
  1141. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1142. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1143. | NFS_INO_INVALID_ACCESS
  1144. | NFS_INO_INVALID_ACL
  1145. | NFS_INO_REVAL_FORCED);
  1146. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1147. if (inode->i_nlink != fattr->nlink) {
  1148. invalid |= NFS_INO_INVALID_ATTR;
  1149. if (S_ISDIR(inode->i_mode))
  1150. invalid |= NFS_INO_INVALID_DATA;
  1151. inode->i_nlink = fattr->nlink;
  1152. }
  1153. } else if (server->caps & NFS_CAP_NLINK)
  1154. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1155. | NFS_INO_REVAL_FORCED);
  1156. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1157. /*
  1158. * report the blocks in 512byte units
  1159. */
  1160. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1161. }
  1162. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1163. inode->i_blocks = fattr->du.nfs2.blocks;
  1164. /* Update attrtimeo value if we're out of the unstable period */
  1165. if (invalid & NFS_INO_INVALID_ATTR) {
  1166. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1167. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1168. nfsi->attrtimeo_timestamp = now;
  1169. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1170. } else {
  1171. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1172. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1173. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1174. nfsi->attrtimeo_timestamp = now;
  1175. }
  1176. }
  1177. invalid &= ~NFS_INO_INVALID_ATTR;
  1178. /* Don't invalidate the data if we were to blame */
  1179. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1180. || S_ISLNK(inode->i_mode)))
  1181. invalid &= ~NFS_INO_INVALID_DATA;
  1182. if (!nfs_have_delegation(inode, FMODE_READ) ||
  1183. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1184. nfsi->cache_validity |= invalid;
  1185. return 0;
  1186. out_changed:
  1187. /*
  1188. * Big trouble! The inode has become a different object.
  1189. */
  1190. printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
  1191. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1192. out_err:
  1193. /*
  1194. * No need to worry about unhashing the dentry, as the
  1195. * lookup validation will know that the inode is bad.
  1196. * (But we fall through to invalidate the caches.)
  1197. */
  1198. nfs_invalidate_inode(inode);
  1199. return -ESTALE;
  1200. out_fileid:
  1201. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1202. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1203. NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
  1204. (long long)nfsi->fileid, (long long)fattr->fileid);
  1205. goto out_err;
  1206. }
  1207. #ifdef CONFIG_NFS_V4
  1208. /*
  1209. * Clean out any remaining NFSv4 state that might be left over due
  1210. * to open() calls that passed nfs_atomic_lookup, but failed to call
  1211. * nfs_open().
  1212. */
  1213. void nfs4_clear_inode(struct inode *inode)
  1214. {
  1215. /* If we are holding a delegation, return it! */
  1216. nfs_inode_return_delegation_noreclaim(inode);
  1217. /* First call standard NFS clear_inode() code */
  1218. nfs_clear_inode(inode);
  1219. }
  1220. #endif
  1221. struct inode *nfs_alloc_inode(struct super_block *sb)
  1222. {
  1223. struct nfs_inode *nfsi;
  1224. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1225. if (!nfsi)
  1226. return NULL;
  1227. nfsi->flags = 0UL;
  1228. nfsi->cache_validity = 0UL;
  1229. #ifdef CONFIG_NFS_V3_ACL
  1230. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1231. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1232. #endif
  1233. #ifdef CONFIG_NFS_V4
  1234. nfsi->nfs4_acl = NULL;
  1235. #endif /* CONFIG_NFS_V4 */
  1236. return &nfsi->vfs_inode;
  1237. }
  1238. void nfs_destroy_inode(struct inode *inode)
  1239. {
  1240. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1241. }
  1242. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1243. {
  1244. #ifdef CONFIG_NFS_V4
  1245. INIT_LIST_HEAD(&nfsi->open_states);
  1246. nfsi->delegation = NULL;
  1247. nfsi->delegation_state = 0;
  1248. init_rwsem(&nfsi->rwsem);
  1249. #endif
  1250. }
  1251. static void init_once(void *foo)
  1252. {
  1253. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1254. inode_init_once(&nfsi->vfs_inode);
  1255. INIT_LIST_HEAD(&nfsi->open_files);
  1256. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1257. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1258. INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
  1259. nfsi->npages = 0;
  1260. atomic_set(&nfsi->silly_count, 1);
  1261. INIT_HLIST_HEAD(&nfsi->silly_list);
  1262. init_waitqueue_head(&nfsi->waitqueue);
  1263. nfs4_init_once(nfsi);
  1264. }
  1265. static int __init nfs_init_inodecache(void)
  1266. {
  1267. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1268. sizeof(struct nfs_inode),
  1269. 0, (SLAB_RECLAIM_ACCOUNT|
  1270. SLAB_MEM_SPREAD),
  1271. init_once);
  1272. if (nfs_inode_cachep == NULL)
  1273. return -ENOMEM;
  1274. return 0;
  1275. }
  1276. static void nfs_destroy_inodecache(void)
  1277. {
  1278. kmem_cache_destroy(nfs_inode_cachep);
  1279. }
  1280. struct workqueue_struct *nfsiod_workqueue;
  1281. /*
  1282. * start up the nfsiod workqueue
  1283. */
  1284. static int nfsiod_start(void)
  1285. {
  1286. struct workqueue_struct *wq;
  1287. dprintk("RPC: creating workqueue nfsiod\n");
  1288. wq = create_singlethread_workqueue("nfsiod");
  1289. if (wq == NULL)
  1290. return -ENOMEM;
  1291. nfsiod_workqueue = wq;
  1292. return 0;
  1293. }
  1294. /*
  1295. * Destroy the nfsiod workqueue
  1296. */
  1297. static void nfsiod_stop(void)
  1298. {
  1299. struct workqueue_struct *wq;
  1300. wq = nfsiod_workqueue;
  1301. if (wq == NULL)
  1302. return;
  1303. nfsiod_workqueue = NULL;
  1304. destroy_workqueue(wq);
  1305. }
  1306. /*
  1307. * Initialize NFS
  1308. */
  1309. static int __init init_nfs_fs(void)
  1310. {
  1311. int err;
  1312. err = nfs_dns_resolver_init();
  1313. if (err < 0)
  1314. goto out8;
  1315. err = nfs_fscache_register();
  1316. if (err < 0)
  1317. goto out7;
  1318. err = nfsiod_start();
  1319. if (err)
  1320. goto out6;
  1321. err = nfs_fs_proc_init();
  1322. if (err)
  1323. goto out5;
  1324. err = nfs_init_nfspagecache();
  1325. if (err)
  1326. goto out4;
  1327. err = nfs_init_inodecache();
  1328. if (err)
  1329. goto out3;
  1330. err = nfs_init_readpagecache();
  1331. if (err)
  1332. goto out2;
  1333. err = nfs_init_writepagecache();
  1334. if (err)
  1335. goto out1;
  1336. err = nfs_init_directcache();
  1337. if (err)
  1338. goto out0;
  1339. #ifdef CONFIG_PROC_FS
  1340. rpc_proc_register(&nfs_rpcstat);
  1341. #endif
  1342. if ((err = register_nfs_fs()) != 0)
  1343. goto out;
  1344. return 0;
  1345. out:
  1346. #ifdef CONFIG_PROC_FS
  1347. rpc_proc_unregister("nfs");
  1348. #endif
  1349. nfs_destroy_directcache();
  1350. out0:
  1351. nfs_destroy_writepagecache();
  1352. out1:
  1353. nfs_destroy_readpagecache();
  1354. out2:
  1355. nfs_destroy_inodecache();
  1356. out3:
  1357. nfs_destroy_nfspagecache();
  1358. out4:
  1359. nfs_fs_proc_exit();
  1360. out5:
  1361. nfsiod_stop();
  1362. out6:
  1363. nfs_fscache_unregister();
  1364. out7:
  1365. nfs_dns_resolver_destroy();
  1366. out8:
  1367. return err;
  1368. }
  1369. static void __exit exit_nfs_fs(void)
  1370. {
  1371. nfs_destroy_directcache();
  1372. nfs_destroy_writepagecache();
  1373. nfs_destroy_readpagecache();
  1374. nfs_destroy_inodecache();
  1375. nfs_destroy_nfspagecache();
  1376. nfs_fscache_unregister();
  1377. nfs_dns_resolver_destroy();
  1378. #ifdef CONFIG_PROC_FS
  1379. rpc_proc_unregister("nfs");
  1380. #endif
  1381. unregister_nfs_fs();
  1382. nfs_fs_proc_exit();
  1383. nfsiod_stop();
  1384. }
  1385. /* Not quite true; I just maintain it */
  1386. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1387. MODULE_LICENSE("GPL");
  1388. module_param(enable_ino64, bool, 0644);
  1389. module_init(init_nfs_fs)
  1390. module_exit(exit_nfs_fs)