inode.c 58 KB

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