inode.c 48 KB

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