inode.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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/vfs.h>
  35. #include <linux/inet.h>
  36. #include <linux/nfs_xdr.h>
  37. #include <linux/slab.h>
  38. #include <linux/compat.h>
  39. #include <linux/freezer.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 "pnfs.h"
  48. #include "nfs.h"
  49. #include "netns.h"
  50. #include "nfstrace.h"
  51. #define NFSDBG_FACILITY NFSDBG_VFS
  52. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  53. /* Default is to see 64-bit inode numbers */
  54. static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  55. static void nfs_invalidate_inode(struct inode *);
  56. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  57. static struct kmem_cache * nfs_inode_cachep;
  58. static inline unsigned long
  59. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  60. {
  61. return nfs_fileid_to_ino_t(fattr->fileid);
  62. }
  63. /**
  64. * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
  65. * @word: long word containing the bit lock
  66. */
  67. int nfs_wait_bit_killable(struct wait_bit_key *key)
  68. {
  69. if (fatal_signal_pending(current))
  70. return -ERESTARTSYS;
  71. freezable_schedule_unsafe();
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
  75. /**
  76. * nfs_compat_user_ino64 - returns the user-visible inode number
  77. * @fileid: 64-bit fileid
  78. *
  79. * This function returns a 32-bit inode number if the boot parameter
  80. * nfs.enable_ino64 is zero.
  81. */
  82. u64 nfs_compat_user_ino64(u64 fileid)
  83. {
  84. #ifdef CONFIG_COMPAT
  85. compat_ulong_t ino;
  86. #else
  87. unsigned long ino;
  88. #endif
  89. if (enable_ino64)
  90. return fileid;
  91. ino = fileid;
  92. if (sizeof(ino) < sizeof(fileid))
  93. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  94. return ino;
  95. }
  96. int nfs_drop_inode(struct inode *inode)
  97. {
  98. return NFS_STALE(inode) || generic_drop_inode(inode);
  99. }
  100. EXPORT_SYMBOL_GPL(nfs_drop_inode);
  101. void nfs_clear_inode(struct inode *inode)
  102. {
  103. /*
  104. * The following should never happen...
  105. */
  106. WARN_ON_ONCE(nfs_have_writebacks(inode));
  107. WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
  108. nfs_zap_acl_cache(inode);
  109. nfs_access_zap_cache(inode);
  110. nfs_fscache_clear_inode(inode);
  111. }
  112. EXPORT_SYMBOL_GPL(nfs_clear_inode);
  113. void nfs_evict_inode(struct inode *inode)
  114. {
  115. truncate_inode_pages_final(&inode->i_data);
  116. clear_inode(inode);
  117. nfs_clear_inode(inode);
  118. }
  119. int nfs_sync_inode(struct inode *inode)
  120. {
  121. nfs_inode_dio_wait(inode);
  122. return nfs_wb_all(inode);
  123. }
  124. EXPORT_SYMBOL_GPL(nfs_sync_inode);
  125. /**
  126. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  127. */
  128. int nfs_sync_mapping(struct address_space *mapping)
  129. {
  130. int ret = 0;
  131. if (mapping->nrpages != 0) {
  132. unmap_mapping_range(mapping, 0, 0, 0);
  133. ret = nfs_wb_all(mapping->host);
  134. }
  135. return ret;
  136. }
  137. static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
  138. {
  139. struct nfs_inode *nfsi = NFS_I(inode);
  140. if (inode->i_mapping->nrpages == 0)
  141. flags &= ~NFS_INO_INVALID_DATA;
  142. nfsi->cache_validity |= flags;
  143. if (flags & NFS_INO_INVALID_DATA)
  144. nfs_fscache_invalidate(inode);
  145. }
  146. /*
  147. * Invalidate the local caches
  148. */
  149. static void nfs_zap_caches_locked(struct inode *inode)
  150. {
  151. struct nfs_inode *nfsi = NFS_I(inode);
  152. int mode = inode->i_mode;
  153. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  154. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  155. nfsi->attrtimeo_timestamp = jiffies;
  156. memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
  157. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  158. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
  159. | NFS_INO_INVALID_DATA
  160. | NFS_INO_INVALID_ACCESS
  161. | NFS_INO_INVALID_ACL
  162. | NFS_INO_REVAL_PAGECACHE);
  163. } else
  164. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
  165. | NFS_INO_INVALID_ACCESS
  166. | NFS_INO_INVALID_ACL
  167. | NFS_INO_REVAL_PAGECACHE);
  168. nfs_zap_label_cache_locked(nfsi);
  169. }
  170. void nfs_zap_caches(struct inode *inode)
  171. {
  172. spin_lock(&inode->i_lock);
  173. nfs_zap_caches_locked(inode);
  174. spin_unlock(&inode->i_lock);
  175. }
  176. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  177. {
  178. if (mapping->nrpages != 0) {
  179. spin_lock(&inode->i_lock);
  180. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  181. spin_unlock(&inode->i_lock);
  182. }
  183. }
  184. void nfs_zap_acl_cache(struct inode *inode)
  185. {
  186. void (*clear_acl_cache)(struct inode *);
  187. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  188. if (clear_acl_cache != NULL)
  189. clear_acl_cache(inode);
  190. spin_lock(&inode->i_lock);
  191. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  192. spin_unlock(&inode->i_lock);
  193. }
  194. EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
  195. void nfs_invalidate_atime(struct inode *inode)
  196. {
  197. spin_lock(&inode->i_lock);
  198. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  199. spin_unlock(&inode->i_lock);
  200. }
  201. EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
  202. /*
  203. * Invalidate, but do not unhash, the inode.
  204. * NB: must be called with inode->i_lock held!
  205. */
  206. static void nfs_invalidate_inode(struct inode *inode)
  207. {
  208. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  209. nfs_zap_caches_locked(inode);
  210. }
  211. struct nfs_find_desc {
  212. struct nfs_fh *fh;
  213. struct nfs_fattr *fattr;
  214. };
  215. /*
  216. * In NFSv3 we can have 64bit inode numbers. In order to support
  217. * this, and re-exported directories (also seen in NFSv2)
  218. * we are forced to allow 2 different inodes to have the same
  219. * i_ino.
  220. */
  221. static int
  222. nfs_find_actor(struct inode *inode, void *opaque)
  223. {
  224. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  225. struct nfs_fh *fh = desc->fh;
  226. struct nfs_fattr *fattr = desc->fattr;
  227. if (NFS_FILEID(inode) != fattr->fileid)
  228. return 0;
  229. if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
  230. return 0;
  231. if (nfs_compare_fh(NFS_FH(inode), fh))
  232. return 0;
  233. if (is_bad_inode(inode) || NFS_STALE(inode))
  234. return 0;
  235. return 1;
  236. }
  237. static int
  238. nfs_init_locked(struct inode *inode, void *opaque)
  239. {
  240. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  241. struct nfs_fattr *fattr = desc->fattr;
  242. set_nfs_fileid(inode, fattr->fileid);
  243. nfs_copy_fh(NFS_FH(inode), desc->fh);
  244. return 0;
  245. }
  246. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  247. static void nfs_clear_label_invalid(struct inode *inode)
  248. {
  249. spin_lock(&inode->i_lock);
  250. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
  251. spin_unlock(&inode->i_lock);
  252. }
  253. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
  254. struct nfs4_label *label)
  255. {
  256. int error;
  257. if (label == NULL)
  258. return;
  259. if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
  260. error = security_inode_notifysecctx(inode, label->label,
  261. label->len);
  262. if (error)
  263. printk(KERN_ERR "%s() %s %d "
  264. "security_inode_notifysecctx() %d\n",
  265. __func__,
  266. (char *)label->label,
  267. label->len, error);
  268. nfs_clear_label_invalid(inode);
  269. }
  270. }
  271. struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
  272. {
  273. struct nfs4_label *label = NULL;
  274. int minor_version = server->nfs_client->cl_minorversion;
  275. if (minor_version < 2)
  276. return label;
  277. if (!(server->caps & NFS_CAP_SECURITY_LABEL))
  278. return label;
  279. label = kzalloc(sizeof(struct nfs4_label), flags);
  280. if (label == NULL)
  281. return ERR_PTR(-ENOMEM);
  282. label->label = kzalloc(NFS4_MAXLABELLEN, flags);
  283. if (label->label == NULL) {
  284. kfree(label);
  285. return ERR_PTR(-ENOMEM);
  286. }
  287. label->len = NFS4_MAXLABELLEN;
  288. return label;
  289. }
  290. EXPORT_SYMBOL_GPL(nfs4_label_alloc);
  291. #else
  292. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
  293. struct nfs4_label *label)
  294. {
  295. }
  296. #endif
  297. EXPORT_SYMBOL_GPL(nfs_setsecurity);
  298. /*
  299. * This is our front-end to iget that looks up inodes by file handle
  300. * instead of inode number.
  301. */
  302. struct inode *
  303. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
  304. {
  305. struct nfs_find_desc desc = {
  306. .fh = fh,
  307. .fattr = fattr
  308. };
  309. struct inode *inode = ERR_PTR(-ENOENT);
  310. unsigned long hash;
  311. nfs_attr_check_mountpoint(sb, fattr);
  312. if (nfs_attr_use_mounted_on_fileid(fattr))
  313. fattr->fileid = fattr->mounted_on_fileid;
  314. else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
  315. goto out_no_inode;
  316. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  317. goto out_no_inode;
  318. hash = nfs_fattr_to_ino_t(fattr);
  319. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  320. if (inode == NULL) {
  321. inode = ERR_PTR(-ENOMEM);
  322. goto out_no_inode;
  323. }
  324. if (inode->i_state & I_NEW) {
  325. struct nfs_inode *nfsi = NFS_I(inode);
  326. unsigned long now = jiffies;
  327. /* We set i_ino for the few things that still rely on it,
  328. * such as stat(2) */
  329. inode->i_ino = hash;
  330. /* We can't support update_atime(), since the server will reset it */
  331. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  332. inode->i_mode = fattr->mode;
  333. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  334. && nfs_server_capable(inode, NFS_CAP_MODE))
  335. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  336. /* Why so? Because we want revalidate for devices/FIFOs, and
  337. * that's precisely what we have in nfs_file_inode_operations.
  338. */
  339. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  340. if (S_ISREG(inode->i_mode)) {
  341. inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
  342. inode->i_data.a_ops = &nfs_file_aops;
  343. } else if (S_ISDIR(inode->i_mode)) {
  344. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  345. inode->i_fop = &nfs_dir_operations;
  346. inode->i_data.a_ops = &nfs_dir_aops;
  347. /* Deal with crossing mountpoints */
  348. if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
  349. fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  350. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  351. inode->i_op = &nfs_referral_inode_operations;
  352. else
  353. inode->i_op = &nfs_mountpoint_inode_operations;
  354. inode->i_fop = NULL;
  355. inode->i_flags |= S_AUTOMOUNT;
  356. }
  357. } else if (S_ISLNK(inode->i_mode))
  358. inode->i_op = &nfs_symlink_inode_operations;
  359. else
  360. init_special_inode(inode, inode->i_mode, fattr->rdev);
  361. memset(&inode->i_atime, 0, sizeof(inode->i_atime));
  362. memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
  363. memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
  364. inode->i_version = 0;
  365. inode->i_size = 0;
  366. clear_nlink(inode);
  367. inode->i_uid = make_kuid(&init_user_ns, -2);
  368. inode->i_gid = make_kgid(&init_user_ns, -2);
  369. inode->i_blocks = 0;
  370. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  371. nfsi->write_io = 0;
  372. nfsi->read_io = 0;
  373. nfsi->read_cache_jiffies = fattr->time_start;
  374. nfsi->attr_gencount = fattr->gencount;
  375. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  376. inode->i_atime = fattr->atime;
  377. else if (nfs_server_capable(inode, NFS_CAP_ATIME))
  378. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  379. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  380. inode->i_mtime = fattr->mtime;
  381. else if (nfs_server_capable(inode, NFS_CAP_MTIME))
  382. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  383. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  384. inode->i_ctime = fattr->ctime;
  385. else if (nfs_server_capable(inode, NFS_CAP_CTIME))
  386. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  387. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  388. inode->i_version = fattr->change_attr;
  389. else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
  390. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  391. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  392. inode->i_size = nfs_size_to_loff_t(fattr->size);
  393. else
  394. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
  395. | NFS_INO_REVAL_PAGECACHE);
  396. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  397. set_nlink(inode, fattr->nlink);
  398. else if (nfs_server_capable(inode, NFS_CAP_NLINK))
  399. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  400. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  401. inode->i_uid = fattr->uid;
  402. else if (nfs_server_capable(inode, NFS_CAP_OWNER))
  403. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  404. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  405. inode->i_gid = fattr->gid;
  406. else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
  407. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
  408. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  409. inode->i_blocks = fattr->du.nfs2.blocks;
  410. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  411. /*
  412. * report the blocks in 512byte units
  413. */
  414. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  415. }
  416. nfs_setsecurity(inode, fattr, label);
  417. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  418. nfsi->attrtimeo_timestamp = now;
  419. nfsi->access_cache = RB_ROOT;
  420. nfs_fscache_init_inode(inode);
  421. unlock_new_inode(inode);
  422. } else
  423. nfs_refresh_inode(inode, fattr);
  424. dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
  425. inode->i_sb->s_id,
  426. (unsigned long long)NFS_FILEID(inode),
  427. nfs_display_fhandle_hash(fh),
  428. atomic_read(&inode->i_count));
  429. out:
  430. return inode;
  431. out_no_inode:
  432. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  433. goto out;
  434. }
  435. EXPORT_SYMBOL_GPL(nfs_fhget);
  436. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN)
  437. int
  438. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  439. {
  440. struct inode *inode = d_inode(dentry);
  441. struct nfs_fattr *fattr;
  442. int error = -ENOMEM;
  443. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  444. /* skip mode change if it's just for clearing setuid/setgid */
  445. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  446. attr->ia_valid &= ~ATTR_MODE;
  447. if (attr->ia_valid & ATTR_SIZE) {
  448. loff_t i_size;
  449. BUG_ON(!S_ISREG(inode->i_mode));
  450. i_size = i_size_read(inode);
  451. if (attr->ia_size == i_size)
  452. attr->ia_valid &= ~ATTR_SIZE;
  453. else if (attr->ia_size < i_size && IS_SWAPFILE(inode))
  454. return -ETXTBSY;
  455. }
  456. /* Optimization: if the end result is no change, don't RPC */
  457. attr->ia_valid &= NFS_VALID_ATTRS;
  458. if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  459. return 0;
  460. trace_nfs_setattr_enter(inode);
  461. /* Write all dirty data */
  462. if (S_ISREG(inode->i_mode))
  463. nfs_sync_inode(inode);
  464. fattr = nfs_alloc_fattr();
  465. if (fattr == NULL)
  466. goto out;
  467. /*
  468. * Return any delegations if we're going to change ACLs
  469. */
  470. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  471. NFS_PROTO(inode)->return_delegation(inode);
  472. error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
  473. if (error == 0)
  474. error = nfs_refresh_inode(inode, fattr);
  475. nfs_free_fattr(fattr);
  476. out:
  477. trace_nfs_setattr_exit(inode, error);
  478. return error;
  479. }
  480. EXPORT_SYMBOL_GPL(nfs_setattr);
  481. /**
  482. * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
  483. * @inode: inode of the file used
  484. * @offset: file offset to start truncating
  485. *
  486. * This is a copy of the common vmtruncate, but with the locking
  487. * corrected to take into account the fact that NFS requires
  488. * inode->i_size to be updated under the inode->i_lock.
  489. * Note: must be called with inode->i_lock held!
  490. */
  491. static int nfs_vmtruncate(struct inode * inode, loff_t offset)
  492. {
  493. int err;
  494. err = inode_newsize_ok(inode, offset);
  495. if (err)
  496. goto out;
  497. i_size_write(inode, offset);
  498. /* Optimisation */
  499. if (offset == 0)
  500. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
  501. spin_unlock(&inode->i_lock);
  502. truncate_pagecache(inode, offset);
  503. spin_lock(&inode->i_lock);
  504. out:
  505. return err;
  506. }
  507. /**
  508. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  509. * @inode: pointer to struct inode
  510. * @attr: pointer to struct iattr
  511. *
  512. * Note: we do this in the *proc.c in order to ensure that
  513. * it works for things like exclusive creates too.
  514. */
  515. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
  516. struct nfs_fattr *fattr)
  517. {
  518. /* Barrier: bump the attribute generation count. */
  519. nfs_fattr_set_barrier(fattr);
  520. spin_lock(&inode->i_lock);
  521. NFS_I(inode)->attr_gencount = fattr->gencount;
  522. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  523. if ((attr->ia_valid & ATTR_MODE) != 0) {
  524. int mode = attr->ia_mode & S_IALLUGO;
  525. mode |= inode->i_mode & ~S_IALLUGO;
  526. inode->i_mode = mode;
  527. }
  528. if ((attr->ia_valid & ATTR_UID) != 0)
  529. inode->i_uid = attr->ia_uid;
  530. if ((attr->ia_valid & ATTR_GID) != 0)
  531. inode->i_gid = attr->ia_gid;
  532. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
  533. | NFS_INO_INVALID_ACL);
  534. }
  535. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  536. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  537. nfs_vmtruncate(inode, attr->ia_size);
  538. }
  539. nfs_update_inode(inode, fattr);
  540. spin_unlock(&inode->i_lock);
  541. }
  542. EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
  543. static void nfs_request_parent_use_readdirplus(struct dentry *dentry)
  544. {
  545. struct dentry *parent;
  546. parent = dget_parent(dentry);
  547. nfs_force_use_readdirplus(d_inode(parent));
  548. dput(parent);
  549. }
  550. static bool nfs_need_revalidate_inode(struct inode *inode)
  551. {
  552. if (NFS_I(inode)->cache_validity &
  553. (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
  554. return true;
  555. if (nfs_attribute_cache_expired(inode))
  556. return true;
  557. return false;
  558. }
  559. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  560. {
  561. struct inode *inode = d_inode(dentry);
  562. int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
  563. int err = 0;
  564. trace_nfs_getattr_enter(inode);
  565. /* Flush out writes to the server in order to update c/mtime. */
  566. if (S_ISREG(inode->i_mode)) {
  567. mutex_lock(&inode->i_mutex);
  568. err = nfs_sync_inode(inode);
  569. mutex_unlock(&inode->i_mutex);
  570. if (err)
  571. goto out;
  572. }
  573. /*
  574. * We may force a getattr if the user cares about atime.
  575. *
  576. * Note that we only have to check the vfsmount flags here:
  577. * - NFS always sets S_NOATIME by so checking it would give a
  578. * bogus result
  579. * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
  580. * no point in checking those.
  581. */
  582. if ((mnt->mnt_flags & MNT_NOATIME) ||
  583. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  584. need_atime = 0;
  585. if (need_atime || nfs_need_revalidate_inode(inode)) {
  586. struct nfs_server *server = NFS_SERVER(inode);
  587. if (server->caps & NFS_CAP_READDIRPLUS)
  588. nfs_request_parent_use_readdirplus(dentry);
  589. err = __nfs_revalidate_inode(server, inode);
  590. }
  591. if (!err) {
  592. generic_fillattr(inode, stat);
  593. stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
  594. if (S_ISDIR(inode->i_mode))
  595. stat->blksize = NFS_SERVER(inode)->dtsize;
  596. }
  597. out:
  598. trace_nfs_getattr_exit(inode, err);
  599. return err;
  600. }
  601. EXPORT_SYMBOL_GPL(nfs_getattr);
  602. static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
  603. {
  604. atomic_set(&l_ctx->count, 1);
  605. l_ctx->lockowner.l_owner = current->files;
  606. l_ctx->lockowner.l_pid = current->tgid;
  607. INIT_LIST_HEAD(&l_ctx->list);
  608. nfs_iocounter_init(&l_ctx->io_count);
  609. }
  610. static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
  611. {
  612. struct nfs_lock_context *head = &ctx->lock_context;
  613. struct nfs_lock_context *pos = head;
  614. do {
  615. if (pos->lockowner.l_owner != current->files)
  616. continue;
  617. if (pos->lockowner.l_pid != current->tgid)
  618. continue;
  619. atomic_inc(&pos->count);
  620. return pos;
  621. } while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head);
  622. return NULL;
  623. }
  624. struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
  625. {
  626. struct nfs_lock_context *res, *new = NULL;
  627. struct inode *inode = d_inode(ctx->dentry);
  628. spin_lock(&inode->i_lock);
  629. res = __nfs_find_lock_context(ctx);
  630. if (res == NULL) {
  631. spin_unlock(&inode->i_lock);
  632. new = kmalloc(sizeof(*new), GFP_KERNEL);
  633. if (new == NULL)
  634. return ERR_PTR(-ENOMEM);
  635. nfs_init_lock_context(new);
  636. spin_lock(&inode->i_lock);
  637. res = __nfs_find_lock_context(ctx);
  638. if (res == NULL) {
  639. list_add_tail(&new->list, &ctx->lock_context.list);
  640. new->open_context = ctx;
  641. res = new;
  642. new = NULL;
  643. }
  644. }
  645. spin_unlock(&inode->i_lock);
  646. kfree(new);
  647. return res;
  648. }
  649. EXPORT_SYMBOL_GPL(nfs_get_lock_context);
  650. void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
  651. {
  652. struct nfs_open_context *ctx = l_ctx->open_context;
  653. struct inode *inode = d_inode(ctx->dentry);
  654. if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock))
  655. return;
  656. list_del(&l_ctx->list);
  657. spin_unlock(&inode->i_lock);
  658. kfree(l_ctx);
  659. }
  660. EXPORT_SYMBOL_GPL(nfs_put_lock_context);
  661. /**
  662. * nfs_close_context - Common close_context() routine NFSv2/v3
  663. * @ctx: pointer to context
  664. * @is_sync: is this a synchronous close
  665. *
  666. * always ensure that the attributes are up to date if we're mounted
  667. * with close-to-open semantics
  668. */
  669. void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
  670. {
  671. struct inode *inode;
  672. struct nfs_server *server;
  673. if (!(ctx->mode & FMODE_WRITE))
  674. return;
  675. if (!is_sync)
  676. return;
  677. inode = d_inode(ctx->dentry);
  678. if (!list_empty(&NFS_I(inode)->open_files))
  679. return;
  680. server = NFS_SERVER(inode);
  681. if (server->flags & NFS_MOUNT_NOCTO)
  682. return;
  683. nfs_revalidate_inode(server, inode);
  684. }
  685. EXPORT_SYMBOL_GPL(nfs_close_context);
  686. struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, fmode_t f_mode)
  687. {
  688. struct nfs_open_context *ctx;
  689. struct rpc_cred *cred = rpc_lookup_cred();
  690. if (IS_ERR(cred))
  691. return ERR_CAST(cred);
  692. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  693. if (!ctx) {
  694. put_rpccred(cred);
  695. return ERR_PTR(-ENOMEM);
  696. }
  697. nfs_sb_active(dentry->d_sb);
  698. ctx->dentry = dget(dentry);
  699. ctx->cred = cred;
  700. ctx->state = NULL;
  701. ctx->mode = f_mode;
  702. ctx->flags = 0;
  703. ctx->error = 0;
  704. nfs_init_lock_context(&ctx->lock_context);
  705. ctx->lock_context.open_context = ctx;
  706. INIT_LIST_HEAD(&ctx->list);
  707. ctx->mdsthreshold = NULL;
  708. return ctx;
  709. }
  710. EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
  711. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  712. {
  713. if (ctx != NULL)
  714. atomic_inc(&ctx->lock_context.count);
  715. return ctx;
  716. }
  717. EXPORT_SYMBOL_GPL(get_nfs_open_context);
  718. static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
  719. {
  720. struct inode *inode = d_inode(ctx->dentry);
  721. struct super_block *sb = ctx->dentry->d_sb;
  722. if (!list_empty(&ctx->list)) {
  723. if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
  724. return;
  725. list_del(&ctx->list);
  726. spin_unlock(&inode->i_lock);
  727. } else if (!atomic_dec_and_test(&ctx->lock_context.count))
  728. return;
  729. if (inode != NULL)
  730. NFS_PROTO(inode)->close_context(ctx, is_sync);
  731. if (ctx->cred != NULL)
  732. put_rpccred(ctx->cred);
  733. dput(ctx->dentry);
  734. nfs_sb_deactive(sb);
  735. kfree(ctx->mdsthreshold);
  736. kfree(ctx);
  737. }
  738. void put_nfs_open_context(struct nfs_open_context *ctx)
  739. {
  740. __put_nfs_open_context(ctx, 0);
  741. }
  742. EXPORT_SYMBOL_GPL(put_nfs_open_context);
  743. /*
  744. * Ensure that mmap has a recent RPC credential for use when writing out
  745. * shared pages
  746. */
  747. void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
  748. {
  749. struct inode *inode = d_inode(ctx->dentry);
  750. struct nfs_inode *nfsi = NFS_I(inode);
  751. spin_lock(&inode->i_lock);
  752. list_add(&ctx->list, &nfsi->open_files);
  753. spin_unlock(&inode->i_lock);
  754. }
  755. EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
  756. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  757. {
  758. filp->private_data = get_nfs_open_context(ctx);
  759. if (list_empty(&ctx->list))
  760. nfs_inode_attach_open_context(ctx);
  761. }
  762. EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
  763. /*
  764. * Given an inode, search for an open context with the desired characteristics
  765. */
  766. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  767. {
  768. struct nfs_inode *nfsi = NFS_I(inode);
  769. struct nfs_open_context *pos, *ctx = NULL;
  770. spin_lock(&inode->i_lock);
  771. list_for_each_entry(pos, &nfsi->open_files, list) {
  772. if (cred != NULL && pos->cred != cred)
  773. continue;
  774. if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
  775. continue;
  776. ctx = get_nfs_open_context(pos);
  777. break;
  778. }
  779. spin_unlock(&inode->i_lock);
  780. return ctx;
  781. }
  782. static void nfs_file_clear_open_context(struct file *filp)
  783. {
  784. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  785. if (ctx) {
  786. struct inode *inode = d_inode(ctx->dentry);
  787. filp->private_data = NULL;
  788. spin_lock(&inode->i_lock);
  789. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  790. spin_unlock(&inode->i_lock);
  791. __put_nfs_open_context(ctx, filp->f_flags & O_DIRECT ? 0 : 1);
  792. }
  793. }
  794. /*
  795. * These allocate and release file read/write context information.
  796. */
  797. int nfs_open(struct inode *inode, struct file *filp)
  798. {
  799. struct nfs_open_context *ctx;
  800. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  801. if (IS_ERR(ctx))
  802. return PTR_ERR(ctx);
  803. nfs_file_set_open_context(filp, ctx);
  804. put_nfs_open_context(ctx);
  805. nfs_fscache_open_file(inode, filp);
  806. return 0;
  807. }
  808. int nfs_release(struct inode *inode, struct file *filp)
  809. {
  810. nfs_file_clear_open_context(filp);
  811. return 0;
  812. }
  813. /*
  814. * This function is called whenever some part of NFS notices that
  815. * the cached attributes have to be refreshed.
  816. */
  817. int
  818. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  819. {
  820. int status = -ESTALE;
  821. struct nfs4_label *label = NULL;
  822. struct nfs_fattr *fattr = NULL;
  823. struct nfs_inode *nfsi = NFS_I(inode);
  824. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
  825. inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
  826. trace_nfs_revalidate_inode_enter(inode);
  827. if (is_bad_inode(inode))
  828. goto out;
  829. if (NFS_STALE(inode))
  830. goto out;
  831. status = -ENOMEM;
  832. fattr = nfs_alloc_fattr();
  833. if (fattr == NULL)
  834. goto out;
  835. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  836. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  837. if (IS_ERR(label)) {
  838. status = PTR_ERR(label);
  839. goto out;
  840. }
  841. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, label);
  842. if (status != 0) {
  843. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
  844. inode->i_sb->s_id,
  845. (unsigned long long)NFS_FILEID(inode), status);
  846. if (status == -ESTALE) {
  847. nfs_zap_caches(inode);
  848. if (!S_ISDIR(inode->i_mode))
  849. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  850. }
  851. goto err_out;
  852. }
  853. status = nfs_refresh_inode(inode, fattr);
  854. if (status) {
  855. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
  856. inode->i_sb->s_id,
  857. (unsigned long long)NFS_FILEID(inode), status);
  858. goto err_out;
  859. }
  860. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  861. nfs_zap_acl_cache(inode);
  862. nfs_setsecurity(inode, fattr, label);
  863. dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
  864. inode->i_sb->s_id,
  865. (unsigned long long)NFS_FILEID(inode));
  866. err_out:
  867. nfs4_label_free(label);
  868. out:
  869. nfs_free_fattr(fattr);
  870. trace_nfs_revalidate_inode_exit(inode, status);
  871. return status;
  872. }
  873. int nfs_attribute_timeout(struct inode *inode)
  874. {
  875. struct nfs_inode *nfsi = NFS_I(inode);
  876. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  877. }
  878. int nfs_attribute_cache_expired(struct inode *inode)
  879. {
  880. if (nfs_have_delegated_attributes(inode))
  881. return 0;
  882. return nfs_attribute_timeout(inode);
  883. }
  884. /**
  885. * nfs_revalidate_inode - Revalidate the inode attributes
  886. * @server - pointer to nfs_server struct
  887. * @inode - pointer to inode struct
  888. *
  889. * Updates inode attribute information by retrieving the data from the server.
  890. */
  891. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  892. {
  893. if (!nfs_need_revalidate_inode(inode))
  894. return NFS_STALE(inode) ? -ESTALE : 0;
  895. return __nfs_revalidate_inode(server, inode);
  896. }
  897. EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
  898. int nfs_revalidate_inode_rcu(struct nfs_server *server, struct inode *inode)
  899. {
  900. if (!(NFS_I(inode)->cache_validity &
  901. (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
  902. && !nfs_attribute_cache_expired(inode))
  903. return NFS_STALE(inode) ? -ESTALE : 0;
  904. return -ECHILD;
  905. }
  906. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  907. {
  908. struct nfs_inode *nfsi = NFS_I(inode);
  909. int ret;
  910. if (mapping->nrpages != 0) {
  911. if (S_ISREG(inode->i_mode)) {
  912. unmap_mapping_range(mapping, 0, 0, 0);
  913. ret = nfs_sync_mapping(mapping);
  914. if (ret < 0)
  915. return ret;
  916. }
  917. ret = invalidate_inode_pages2(mapping);
  918. if (ret < 0)
  919. return ret;
  920. }
  921. if (S_ISDIR(inode->i_mode)) {
  922. spin_lock(&inode->i_lock);
  923. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  924. spin_unlock(&inode->i_lock);
  925. }
  926. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  927. nfs_fscache_wait_on_invalidate(inode);
  928. dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
  929. inode->i_sb->s_id,
  930. (unsigned long long)NFS_FILEID(inode));
  931. return 0;
  932. }
  933. static bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  934. {
  935. if (nfs_have_delegated_attributes(inode))
  936. return false;
  937. return (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
  938. || nfs_attribute_timeout(inode)
  939. || NFS_STALE(inode);
  940. }
  941. /**
  942. * __nfs_revalidate_mapping - Revalidate the pagecache
  943. * @inode - pointer to host inode
  944. * @mapping - pointer to mapping
  945. * @may_lock - take inode->i_mutex?
  946. */
  947. static int __nfs_revalidate_mapping(struct inode *inode,
  948. struct address_space *mapping,
  949. bool may_lock)
  950. {
  951. struct nfs_inode *nfsi = NFS_I(inode);
  952. unsigned long *bitlock = &nfsi->flags;
  953. int ret = 0;
  954. /* swapfiles are not supposed to be shared. */
  955. if (IS_SWAPFILE(inode))
  956. goto out;
  957. if (nfs_mapping_need_revalidate_inode(inode)) {
  958. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  959. if (ret < 0)
  960. goto out;
  961. }
  962. /*
  963. * We must clear NFS_INO_INVALID_DATA first to ensure that
  964. * invalidations that come in while we're shooting down the mappings
  965. * are respected. But, that leaves a race window where one revalidator
  966. * can clear the flag, and then another checks it before the mapping
  967. * gets invalidated. Fix that by serializing access to this part of
  968. * the function.
  969. *
  970. * At the same time, we need to allow other tasks to see whether we
  971. * might be in the middle of invalidating the pages, so we only set
  972. * the bit lock here if it looks like we're going to be doing that.
  973. */
  974. for (;;) {
  975. ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
  976. nfs_wait_bit_killable, TASK_KILLABLE);
  977. if (ret)
  978. goto out;
  979. spin_lock(&inode->i_lock);
  980. if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
  981. spin_unlock(&inode->i_lock);
  982. continue;
  983. }
  984. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  985. break;
  986. spin_unlock(&inode->i_lock);
  987. goto out;
  988. }
  989. set_bit(NFS_INO_INVALIDATING, bitlock);
  990. smp_wmb();
  991. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  992. spin_unlock(&inode->i_lock);
  993. trace_nfs_invalidate_mapping_enter(inode);
  994. if (may_lock) {
  995. mutex_lock(&inode->i_mutex);
  996. ret = nfs_invalidate_mapping(inode, mapping);
  997. mutex_unlock(&inode->i_mutex);
  998. } else
  999. ret = nfs_invalidate_mapping(inode, mapping);
  1000. trace_nfs_invalidate_mapping_exit(inode, ret);
  1001. clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
  1002. smp_mb__after_atomic();
  1003. wake_up_bit(bitlock, NFS_INO_INVALIDATING);
  1004. out:
  1005. return ret;
  1006. }
  1007. /**
  1008. * nfs_revalidate_mapping - Revalidate the pagecache
  1009. * @inode - pointer to host inode
  1010. * @mapping - pointer to mapping
  1011. */
  1012. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  1013. {
  1014. return __nfs_revalidate_mapping(inode, mapping, false);
  1015. }
  1016. /**
  1017. * nfs_revalidate_mapping_protected - Revalidate the pagecache
  1018. * @inode - pointer to host inode
  1019. * @mapping - pointer to mapping
  1020. *
  1021. * Differs from nfs_revalidate_mapping() in that it grabs the inode->i_mutex
  1022. * while invalidating the mapping.
  1023. */
  1024. int nfs_revalidate_mapping_protected(struct inode *inode, struct address_space *mapping)
  1025. {
  1026. return __nfs_revalidate_mapping(inode, mapping, true);
  1027. }
  1028. static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1029. {
  1030. struct nfs_inode *nfsi = NFS_I(inode);
  1031. unsigned long ret = 0;
  1032. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  1033. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  1034. && inode->i_version == fattr->pre_change_attr) {
  1035. inode->i_version = fattr->change_attr;
  1036. if (S_ISDIR(inode->i_mode))
  1037. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  1038. ret |= NFS_INO_INVALID_ATTR;
  1039. }
  1040. /* If we have atomic WCC data, we may update some attributes */
  1041. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  1042. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  1043. && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
  1044. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1045. ret |= NFS_INO_INVALID_ATTR;
  1046. }
  1047. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  1048. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  1049. && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  1050. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1051. if (S_ISDIR(inode->i_mode))
  1052. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  1053. ret |= NFS_INO_INVALID_ATTR;
  1054. }
  1055. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  1056. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  1057. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  1058. && nfsi->nrequests == 0) {
  1059. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  1060. ret |= NFS_INO_INVALID_ATTR;
  1061. }
  1062. return ret;
  1063. }
  1064. /**
  1065. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  1066. * @inode - pointer to inode
  1067. * @fattr - updated attributes
  1068. *
  1069. * Verifies the attribute cache. If we have just changed the attributes,
  1070. * so that fattr carries weak cache consistency data, then it may
  1071. * also update the ctime/mtime/change_attribute.
  1072. */
  1073. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  1074. {
  1075. struct nfs_inode *nfsi = NFS_I(inode);
  1076. loff_t cur_size, new_isize;
  1077. unsigned long invalid = 0;
  1078. if (nfs_have_delegated_attributes(inode))
  1079. return 0;
  1080. /* Has the inode gone and changed behind our back? */
  1081. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  1082. return -EIO;
  1083. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1084. return -EIO;
  1085. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1086. inode->i_version != fattr->change_attr)
  1087. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1088. /* Verify a few of the more important attributes */
  1089. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
  1090. invalid |= NFS_INO_INVALID_ATTR;
  1091. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1092. cur_size = i_size_read(inode);
  1093. new_isize = nfs_size_to_loff_t(fattr->size);
  1094. if (cur_size != new_isize && nfsi->nrequests == 0)
  1095. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1096. }
  1097. /* Have any file permissions changed? */
  1098. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  1099. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  1100. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
  1101. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  1102. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
  1103. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  1104. /* Has the link count changed? */
  1105. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  1106. invalid |= NFS_INO_INVALID_ATTR;
  1107. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
  1108. invalid |= NFS_INO_INVALID_ATIME;
  1109. if (invalid != 0)
  1110. nfs_set_cache_invalid(inode, invalid);
  1111. nfsi->read_cache_jiffies = fattr->time_start;
  1112. return 0;
  1113. }
  1114. static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  1115. {
  1116. if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
  1117. return 0;
  1118. return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
  1119. }
  1120. static atomic_long_t nfs_attr_generation_counter;
  1121. static unsigned long nfs_read_attr_generation_counter(void)
  1122. {
  1123. return atomic_long_read(&nfs_attr_generation_counter);
  1124. }
  1125. unsigned long nfs_inc_attr_generation_counter(void)
  1126. {
  1127. return atomic_long_inc_return(&nfs_attr_generation_counter);
  1128. }
  1129. EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
  1130. void nfs_fattr_init(struct nfs_fattr *fattr)
  1131. {
  1132. fattr->valid = 0;
  1133. fattr->time_start = jiffies;
  1134. fattr->gencount = nfs_inc_attr_generation_counter();
  1135. fattr->owner_name = NULL;
  1136. fattr->group_name = NULL;
  1137. }
  1138. EXPORT_SYMBOL_GPL(nfs_fattr_init);
  1139. /**
  1140. * nfs_fattr_set_barrier
  1141. * @fattr: attributes
  1142. *
  1143. * Used to set a barrier after an attribute was updated. This
  1144. * barrier ensures that older attributes from RPC calls that may
  1145. * have raced with our update cannot clobber these new values.
  1146. * Note that you are still responsible for ensuring that other
  1147. * operations which change the attribute on the server do not
  1148. * collide.
  1149. */
  1150. void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
  1151. {
  1152. fattr->gencount = nfs_inc_attr_generation_counter();
  1153. }
  1154. struct nfs_fattr *nfs_alloc_fattr(void)
  1155. {
  1156. struct nfs_fattr *fattr;
  1157. fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
  1158. if (fattr != NULL)
  1159. nfs_fattr_init(fattr);
  1160. return fattr;
  1161. }
  1162. EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
  1163. struct nfs_fh *nfs_alloc_fhandle(void)
  1164. {
  1165. struct nfs_fh *fh;
  1166. fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
  1167. if (fh != NULL)
  1168. fh->size = 0;
  1169. return fh;
  1170. }
  1171. EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
  1172. #ifdef NFS_DEBUG
  1173. /*
  1174. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  1175. * in the same way that wireshark does
  1176. *
  1177. * @fh: file handle
  1178. *
  1179. * For debugging only.
  1180. */
  1181. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  1182. {
  1183. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  1184. * not on the result */
  1185. return nfs_fhandle_hash(fh);
  1186. }
  1187. EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
  1188. /*
  1189. * _nfs_display_fhandle - display an NFS file handle on the console
  1190. *
  1191. * @fh: file handle to display
  1192. * @caption: display caption
  1193. *
  1194. * For debugging only.
  1195. */
  1196. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  1197. {
  1198. unsigned short i;
  1199. if (fh == NULL || fh->size == 0) {
  1200. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  1201. return;
  1202. }
  1203. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  1204. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  1205. for (i = 0; i < fh->size; i += 16) {
  1206. __be32 *pos = (__be32 *)&fh->data[i];
  1207. switch ((fh->size - i - 1) >> 2) {
  1208. case 0:
  1209. printk(KERN_DEFAULT " %08x\n",
  1210. be32_to_cpup(pos));
  1211. break;
  1212. case 1:
  1213. printk(KERN_DEFAULT " %08x %08x\n",
  1214. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  1215. break;
  1216. case 2:
  1217. printk(KERN_DEFAULT " %08x %08x %08x\n",
  1218. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1219. be32_to_cpup(pos + 2));
  1220. break;
  1221. default:
  1222. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  1223. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1224. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  1225. }
  1226. }
  1227. }
  1228. EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
  1229. #endif
  1230. /**
  1231. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  1232. * @inode - pointer to inode
  1233. * @fattr - attributes
  1234. *
  1235. * Attempt to divine whether or not an RPC call reply carrying stale
  1236. * attributes got scheduled after another call carrying updated ones.
  1237. *
  1238. * To do so, the function first assumes that a more recent ctime means
  1239. * that the attributes in fattr are newer, however it also attempt to
  1240. * catch the case where ctime either didn't change, or went backwards
  1241. * (if someone reset the clock on the server) by looking at whether
  1242. * or not this RPC call was started after the inode was last updated.
  1243. * Note also the check for wraparound of 'attr_gencount'
  1244. *
  1245. * The function returns 'true' if it thinks the attributes in 'fattr' are
  1246. * more recent than the ones cached in the inode.
  1247. *
  1248. */
  1249. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  1250. {
  1251. const struct nfs_inode *nfsi = NFS_I(inode);
  1252. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  1253. nfs_ctime_need_update(inode, fattr) ||
  1254. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  1255. }
  1256. /*
  1257. * Don't trust the change_attribute, mtime, ctime or size if
  1258. * a pnfs LAYOUTCOMMIT is outstanding
  1259. */
  1260. static void nfs_inode_attrs_handle_layoutcommit(struct inode *inode,
  1261. struct nfs_fattr *fattr)
  1262. {
  1263. if (pnfs_layoutcommit_outstanding(inode))
  1264. fattr->valid &= ~(NFS_ATTR_FATTR_CHANGE |
  1265. NFS_ATTR_FATTR_MTIME |
  1266. NFS_ATTR_FATTR_CTIME |
  1267. NFS_ATTR_FATTR_SIZE);
  1268. }
  1269. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1270. {
  1271. int ret;
  1272. trace_nfs_refresh_inode_enter(inode);
  1273. nfs_inode_attrs_handle_layoutcommit(inode, fattr);
  1274. if (nfs_inode_attrs_need_update(inode, fattr))
  1275. ret = nfs_update_inode(inode, fattr);
  1276. else
  1277. ret = nfs_check_inode_attributes(inode, fattr);
  1278. trace_nfs_refresh_inode_exit(inode, ret);
  1279. return ret;
  1280. }
  1281. /**
  1282. * nfs_refresh_inode - try to update the inode attribute cache
  1283. * @inode - pointer to inode
  1284. * @fattr - updated attributes
  1285. *
  1286. * Check that an RPC call that returned attributes has not overlapped with
  1287. * other recent updates of the inode metadata, then decide whether it is
  1288. * safe to do a full update of the inode attributes, or whether just to
  1289. * call nfs_check_inode_attributes.
  1290. */
  1291. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1292. {
  1293. int status;
  1294. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1295. return 0;
  1296. spin_lock(&inode->i_lock);
  1297. status = nfs_refresh_inode_locked(inode, fattr);
  1298. spin_unlock(&inode->i_lock);
  1299. return status;
  1300. }
  1301. EXPORT_SYMBOL_GPL(nfs_refresh_inode);
  1302. static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1303. {
  1304. unsigned long invalid = NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1305. if (S_ISDIR(inode->i_mode))
  1306. invalid |= NFS_INO_INVALID_DATA;
  1307. nfs_set_cache_invalid(inode, invalid);
  1308. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1309. return 0;
  1310. return nfs_refresh_inode_locked(inode, fattr);
  1311. }
  1312. /**
  1313. * nfs_post_op_update_inode - try to update the inode attribute cache
  1314. * @inode - pointer to inode
  1315. * @fattr - updated attributes
  1316. *
  1317. * After an operation that has changed the inode metadata, mark the
  1318. * attribute cache as being invalid, then try to update it.
  1319. *
  1320. * NB: if the server didn't return any post op attributes, this
  1321. * function will force the retrieval of attributes before the next
  1322. * NFS request. Thus it should be used only for operations that
  1323. * are expected to change one or more attributes, to avoid
  1324. * unnecessary NFS requests and trips through nfs_update_inode().
  1325. */
  1326. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1327. {
  1328. int status;
  1329. spin_lock(&inode->i_lock);
  1330. nfs_fattr_set_barrier(fattr);
  1331. status = nfs_post_op_update_inode_locked(inode, fattr);
  1332. spin_unlock(&inode->i_lock);
  1333. return status;
  1334. }
  1335. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
  1336. /**
  1337. * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
  1338. * @inode - pointer to inode
  1339. * @fattr - updated attributes
  1340. *
  1341. * After an operation that has changed the inode metadata, mark the
  1342. * attribute cache as being invalid, then try to update it. Fake up
  1343. * weak cache consistency data, if none exist.
  1344. *
  1345. * This function is mainly designed to be used by the ->write_done() functions.
  1346. */
  1347. int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
  1348. {
  1349. int status;
  1350. /* Don't do a WCC update if these attributes are already stale */
  1351. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  1352. !nfs_inode_attrs_need_update(inode, fattr)) {
  1353. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1354. | NFS_ATTR_FATTR_PRESIZE
  1355. | NFS_ATTR_FATTR_PREMTIME
  1356. | NFS_ATTR_FATTR_PRECTIME);
  1357. goto out_noforce;
  1358. }
  1359. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1360. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1361. fattr->pre_change_attr = inode->i_version;
  1362. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1363. }
  1364. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1365. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1366. memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
  1367. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1368. }
  1369. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1370. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1371. memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
  1372. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1373. }
  1374. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1375. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1376. fattr->pre_size = i_size_read(inode);
  1377. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1378. }
  1379. out_noforce:
  1380. status = nfs_post_op_update_inode_locked(inode, fattr);
  1381. return status;
  1382. }
  1383. /**
  1384. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1385. * @inode - pointer to inode
  1386. * @fattr - updated attributes
  1387. *
  1388. * After an operation that has changed the inode metadata, mark the
  1389. * attribute cache as being invalid, then try to update it. Fake up
  1390. * weak cache consistency data, if none exist.
  1391. *
  1392. * This function is mainly designed to be used by the ->write_done() functions.
  1393. */
  1394. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1395. {
  1396. int status;
  1397. spin_lock(&inode->i_lock);
  1398. nfs_fattr_set_barrier(fattr);
  1399. status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1400. spin_unlock(&inode->i_lock);
  1401. return status;
  1402. }
  1403. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
  1404. static inline bool nfs_fileid_valid(struct nfs_inode *nfsi,
  1405. struct nfs_fattr *fattr)
  1406. {
  1407. bool ret1 = true, ret2 = true;
  1408. if (fattr->valid & NFS_ATTR_FATTR_FILEID)
  1409. ret1 = (nfsi->fileid == fattr->fileid);
  1410. if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
  1411. ret2 = (nfsi->fileid == fattr->mounted_on_fileid);
  1412. return ret1 || ret2;
  1413. }
  1414. /*
  1415. * Many nfs protocol calls return the new file attributes after
  1416. * an operation. Here we update the inode to reflect the state
  1417. * of the server's inode.
  1418. *
  1419. * This is a bit tricky because we have to make sure all dirty pages
  1420. * have been sent off to the server before calling invalidate_inode_pages.
  1421. * To make sure no other process adds more write requests while we try
  1422. * our best to flush them, we make them sleep during the attribute refresh.
  1423. *
  1424. * A very similar scenario holds for the dir cache.
  1425. */
  1426. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1427. {
  1428. struct nfs_server *server;
  1429. struct nfs_inode *nfsi = NFS_I(inode);
  1430. loff_t cur_isize, new_isize;
  1431. unsigned long invalid = 0;
  1432. unsigned long now = jiffies;
  1433. unsigned long save_cache_validity;
  1434. dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
  1435. __func__, inode->i_sb->s_id, inode->i_ino,
  1436. nfs_display_fhandle_hash(NFS_FH(inode)),
  1437. atomic_read(&inode->i_count), fattr->valid);
  1438. if (!nfs_fileid_valid(nfsi, fattr)) {
  1439. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1440. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1441. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1442. inode->i_sb->s_id, (long long)nfsi->fileid,
  1443. (long long)fattr->fileid);
  1444. goto out_err;
  1445. }
  1446. /*
  1447. * Make sure the inode's type hasn't changed.
  1448. */
  1449. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  1450. /*
  1451. * Big trouble! The inode has become a different object.
  1452. */
  1453. printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
  1454. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1455. goto out_err;
  1456. }
  1457. server = NFS_SERVER(inode);
  1458. /* Update the fsid? */
  1459. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1460. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1461. !IS_AUTOMOUNT(inode))
  1462. server->fsid = fattr->fsid;
  1463. /*
  1464. * Update the read time so we don't revalidate too often.
  1465. */
  1466. nfsi->read_cache_jiffies = fattr->time_start;
  1467. save_cache_validity = nfsi->cache_validity;
  1468. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1469. | NFS_INO_INVALID_ATIME
  1470. | NFS_INO_REVAL_FORCED
  1471. | NFS_INO_REVAL_PAGECACHE);
  1472. /* Do atomic weak cache consistency updates */
  1473. invalid |= nfs_wcc_update_inode(inode, fattr);
  1474. /* More cache consistency checks */
  1475. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1476. if (inode->i_version != fattr->change_attr) {
  1477. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1478. inode->i_sb->s_id, inode->i_ino);
  1479. invalid |= NFS_INO_INVALID_ATTR
  1480. | NFS_INO_INVALID_DATA
  1481. | NFS_INO_INVALID_ACCESS
  1482. | NFS_INO_INVALID_ACL
  1483. | NFS_INO_REVAL_PAGECACHE;
  1484. if (S_ISDIR(inode->i_mode))
  1485. nfs_force_lookup_revalidate(inode);
  1486. inode->i_version = fattr->change_attr;
  1487. }
  1488. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1489. nfsi->cache_validity |= save_cache_validity;
  1490. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1491. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1492. } else if (server->caps & NFS_CAP_MTIME)
  1493. nfsi->cache_validity |= save_cache_validity &
  1494. (NFS_INO_INVALID_ATTR
  1495. | NFS_INO_REVAL_FORCED);
  1496. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1497. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1498. } else if (server->caps & NFS_CAP_CTIME)
  1499. nfsi->cache_validity |= save_cache_validity &
  1500. (NFS_INO_INVALID_ATTR
  1501. | NFS_INO_REVAL_FORCED);
  1502. /* Check if our cached file size is stale */
  1503. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1504. new_isize = nfs_size_to_loff_t(fattr->size);
  1505. cur_isize = i_size_read(inode);
  1506. if (new_isize != cur_isize) {
  1507. /* Do we perhaps have any outstanding writes, or has
  1508. * the file grown beyond our last write? */
  1509. if ((nfsi->nrequests == 0) || new_isize > cur_isize) {
  1510. i_size_write(inode, new_isize);
  1511. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1512. invalid &= ~NFS_INO_REVAL_PAGECACHE;
  1513. }
  1514. dprintk("NFS: isize change on server for file %s/%ld "
  1515. "(%Ld to %Ld)\n",
  1516. inode->i_sb->s_id,
  1517. inode->i_ino,
  1518. (long long)cur_isize,
  1519. (long long)new_isize);
  1520. }
  1521. } else
  1522. nfsi->cache_validity |= save_cache_validity &
  1523. (NFS_INO_INVALID_ATTR
  1524. | NFS_INO_REVAL_PAGECACHE
  1525. | NFS_INO_REVAL_FORCED);
  1526. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1527. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1528. else if (server->caps & NFS_CAP_ATIME)
  1529. nfsi->cache_validity |= save_cache_validity &
  1530. (NFS_INO_INVALID_ATIME
  1531. | NFS_INO_REVAL_FORCED);
  1532. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1533. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1534. umode_t newmode = inode->i_mode & S_IFMT;
  1535. newmode |= fattr->mode & S_IALLUGO;
  1536. inode->i_mode = newmode;
  1537. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1538. }
  1539. } else if (server->caps & NFS_CAP_MODE)
  1540. nfsi->cache_validity |= save_cache_validity &
  1541. (NFS_INO_INVALID_ATTR
  1542. | NFS_INO_INVALID_ACCESS
  1543. | NFS_INO_INVALID_ACL
  1544. | NFS_INO_REVAL_FORCED);
  1545. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1546. if (!uid_eq(inode->i_uid, fattr->uid)) {
  1547. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1548. inode->i_uid = fattr->uid;
  1549. }
  1550. } else if (server->caps & NFS_CAP_OWNER)
  1551. nfsi->cache_validity |= save_cache_validity &
  1552. (NFS_INO_INVALID_ATTR
  1553. | NFS_INO_INVALID_ACCESS
  1554. | NFS_INO_INVALID_ACL
  1555. | NFS_INO_REVAL_FORCED);
  1556. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1557. if (!gid_eq(inode->i_gid, fattr->gid)) {
  1558. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1559. inode->i_gid = fattr->gid;
  1560. }
  1561. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1562. nfsi->cache_validity |= save_cache_validity &
  1563. (NFS_INO_INVALID_ATTR
  1564. | NFS_INO_INVALID_ACCESS
  1565. | NFS_INO_INVALID_ACL
  1566. | NFS_INO_REVAL_FORCED);
  1567. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1568. if (inode->i_nlink != fattr->nlink) {
  1569. invalid |= NFS_INO_INVALID_ATTR;
  1570. if (S_ISDIR(inode->i_mode))
  1571. invalid |= NFS_INO_INVALID_DATA;
  1572. set_nlink(inode, fattr->nlink);
  1573. }
  1574. } else if (server->caps & NFS_CAP_NLINK)
  1575. nfsi->cache_validity |= save_cache_validity &
  1576. (NFS_INO_INVALID_ATTR
  1577. | NFS_INO_REVAL_FORCED);
  1578. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1579. /*
  1580. * report the blocks in 512byte units
  1581. */
  1582. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1583. }
  1584. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1585. inode->i_blocks = fattr->du.nfs2.blocks;
  1586. /* Update attrtimeo value if we're out of the unstable period */
  1587. if (invalid & NFS_INO_INVALID_ATTR) {
  1588. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1589. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1590. nfsi->attrtimeo_timestamp = now;
  1591. /* Set barrier to be more recent than all outstanding updates */
  1592. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1593. } else {
  1594. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1595. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1596. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1597. nfsi->attrtimeo_timestamp = now;
  1598. }
  1599. /* Set the barrier to be more recent than this fattr */
  1600. if ((long)fattr->gencount - (long)nfsi->attr_gencount > 0)
  1601. nfsi->attr_gencount = fattr->gencount;
  1602. }
  1603. invalid &= ~NFS_INO_INVALID_ATTR;
  1604. /* Don't invalidate the data if we were to blame */
  1605. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1606. || S_ISLNK(inode->i_mode)))
  1607. invalid &= ~NFS_INO_INVALID_DATA;
  1608. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) ||
  1609. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1610. nfs_set_cache_invalid(inode, invalid);
  1611. return 0;
  1612. out_err:
  1613. /*
  1614. * No need to worry about unhashing the dentry, as the
  1615. * lookup validation will know that the inode is bad.
  1616. * (But we fall through to invalidate the caches.)
  1617. */
  1618. nfs_invalidate_inode(inode);
  1619. return -ESTALE;
  1620. }
  1621. struct inode *nfs_alloc_inode(struct super_block *sb)
  1622. {
  1623. struct nfs_inode *nfsi;
  1624. nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1625. if (!nfsi)
  1626. return NULL;
  1627. nfsi->flags = 0UL;
  1628. nfsi->cache_validity = 0UL;
  1629. #if IS_ENABLED(CONFIG_NFS_V4)
  1630. nfsi->nfs4_acl = NULL;
  1631. #endif /* CONFIG_NFS_V4 */
  1632. return &nfsi->vfs_inode;
  1633. }
  1634. EXPORT_SYMBOL_GPL(nfs_alloc_inode);
  1635. static void nfs_i_callback(struct rcu_head *head)
  1636. {
  1637. struct inode *inode = container_of(head, struct inode, i_rcu);
  1638. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1639. }
  1640. void nfs_destroy_inode(struct inode *inode)
  1641. {
  1642. call_rcu(&inode->i_rcu, nfs_i_callback);
  1643. }
  1644. EXPORT_SYMBOL_GPL(nfs_destroy_inode);
  1645. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1646. {
  1647. #if IS_ENABLED(CONFIG_NFS_V4)
  1648. INIT_LIST_HEAD(&nfsi->open_states);
  1649. nfsi->delegation = NULL;
  1650. init_rwsem(&nfsi->rwsem);
  1651. nfsi->layout = NULL;
  1652. #endif
  1653. }
  1654. static void init_once(void *foo)
  1655. {
  1656. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1657. inode_init_once(&nfsi->vfs_inode);
  1658. INIT_LIST_HEAD(&nfsi->open_files);
  1659. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1660. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1661. INIT_LIST_HEAD(&nfsi->commit_info.list);
  1662. nfsi->nrequests = 0;
  1663. nfsi->commit_info.ncommit = 0;
  1664. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  1665. atomic_set(&nfsi->silly_count, 1);
  1666. INIT_HLIST_HEAD(&nfsi->silly_list);
  1667. init_waitqueue_head(&nfsi->waitqueue);
  1668. nfs4_init_once(nfsi);
  1669. }
  1670. static int __init nfs_init_inodecache(void)
  1671. {
  1672. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1673. sizeof(struct nfs_inode),
  1674. 0, (SLAB_RECLAIM_ACCOUNT|
  1675. SLAB_MEM_SPREAD),
  1676. init_once);
  1677. if (nfs_inode_cachep == NULL)
  1678. return -ENOMEM;
  1679. return 0;
  1680. }
  1681. static void nfs_destroy_inodecache(void)
  1682. {
  1683. /*
  1684. * Make sure all delayed rcu free inodes are flushed before we
  1685. * destroy cache.
  1686. */
  1687. rcu_barrier();
  1688. kmem_cache_destroy(nfs_inode_cachep);
  1689. }
  1690. struct workqueue_struct *nfsiod_workqueue;
  1691. EXPORT_SYMBOL_GPL(nfsiod_workqueue);
  1692. /*
  1693. * start up the nfsiod workqueue
  1694. */
  1695. static int nfsiod_start(void)
  1696. {
  1697. struct workqueue_struct *wq;
  1698. dprintk("RPC: creating workqueue nfsiod\n");
  1699. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1700. if (wq == NULL)
  1701. return -ENOMEM;
  1702. nfsiod_workqueue = wq;
  1703. return 0;
  1704. }
  1705. /*
  1706. * Destroy the nfsiod workqueue
  1707. */
  1708. static void nfsiod_stop(void)
  1709. {
  1710. struct workqueue_struct *wq;
  1711. wq = nfsiod_workqueue;
  1712. if (wq == NULL)
  1713. return;
  1714. nfsiod_workqueue = NULL;
  1715. destroy_workqueue(wq);
  1716. }
  1717. int nfs_net_id;
  1718. EXPORT_SYMBOL_GPL(nfs_net_id);
  1719. static int nfs_net_init(struct net *net)
  1720. {
  1721. nfs_clients_init(net);
  1722. return nfs_fs_proc_net_init(net);
  1723. }
  1724. static void nfs_net_exit(struct net *net)
  1725. {
  1726. nfs_fs_proc_net_exit(net);
  1727. nfs_cleanup_cb_ident_idr(net);
  1728. }
  1729. static struct pernet_operations nfs_net_ops = {
  1730. .init = nfs_net_init,
  1731. .exit = nfs_net_exit,
  1732. .id = &nfs_net_id,
  1733. .size = sizeof(struct nfs_net),
  1734. };
  1735. /*
  1736. * Initialize NFS
  1737. */
  1738. static int __init init_nfs_fs(void)
  1739. {
  1740. int err;
  1741. err = register_pernet_subsys(&nfs_net_ops);
  1742. if (err < 0)
  1743. goto out9;
  1744. err = nfs_fscache_register();
  1745. if (err < 0)
  1746. goto out8;
  1747. err = nfsiod_start();
  1748. if (err)
  1749. goto out7;
  1750. err = nfs_fs_proc_init();
  1751. if (err)
  1752. goto out6;
  1753. err = nfs_init_nfspagecache();
  1754. if (err)
  1755. goto out5;
  1756. err = nfs_init_inodecache();
  1757. if (err)
  1758. goto out4;
  1759. err = nfs_init_readpagecache();
  1760. if (err)
  1761. goto out3;
  1762. err = nfs_init_writepagecache();
  1763. if (err)
  1764. goto out2;
  1765. err = nfs_init_directcache();
  1766. if (err)
  1767. goto out1;
  1768. rpc_proc_register(&init_net, &nfs_rpcstat);
  1769. err = register_nfs_fs();
  1770. if (err)
  1771. goto out0;
  1772. return 0;
  1773. out0:
  1774. rpc_proc_unregister(&init_net, "nfs");
  1775. nfs_destroy_directcache();
  1776. out1:
  1777. nfs_destroy_writepagecache();
  1778. out2:
  1779. nfs_destroy_readpagecache();
  1780. out3:
  1781. nfs_destroy_inodecache();
  1782. out4:
  1783. nfs_destroy_nfspagecache();
  1784. out5:
  1785. nfs_fs_proc_exit();
  1786. out6:
  1787. nfsiod_stop();
  1788. out7:
  1789. nfs_fscache_unregister();
  1790. out8:
  1791. unregister_pernet_subsys(&nfs_net_ops);
  1792. out9:
  1793. return err;
  1794. }
  1795. static void __exit exit_nfs_fs(void)
  1796. {
  1797. nfs_destroy_directcache();
  1798. nfs_destroy_writepagecache();
  1799. nfs_destroy_readpagecache();
  1800. nfs_destroy_inodecache();
  1801. nfs_destroy_nfspagecache();
  1802. nfs_fscache_unregister();
  1803. unregister_pernet_subsys(&nfs_net_ops);
  1804. rpc_proc_unregister(&init_net, "nfs");
  1805. unregister_nfs_fs();
  1806. nfs_fs_proc_exit();
  1807. nfsiod_stop();
  1808. }
  1809. /* Not quite true; I just maintain it */
  1810. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1811. MODULE_LICENSE("GPL");
  1812. module_param(enable_ino64, bool, 0644);
  1813. module_init(init_nfs_fs)
  1814. module_exit(exit_nfs_fs)