inode.c 45 KB

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