inode.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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.Cox@linux.org>, 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/time.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/string.h>
  21. #include <linux/stat.h>
  22. #include <linux/errno.h>
  23. #include <linux/unistd.h>
  24. #include <linux/sunrpc/clnt.h>
  25. #include <linux/sunrpc/stats.h>
  26. #include <linux/sunrpc/metrics.h>
  27. #include <linux/nfs_fs.h>
  28. #include <linux/nfs_mount.h>
  29. #include <linux/nfs4_mount.h>
  30. #include <linux/lockd/bind.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/mount.h>
  34. #include <linux/nfs_idmap.h>
  35. #include <linux/vfs.h>
  36. #include <linux/inet.h>
  37. #include <linux/nfs_xdr.h>
  38. #include <asm/system.h>
  39. #include <asm/uaccess.h>
  40. #include "nfs4_fs.h"
  41. #include "callback.h"
  42. #include "delegation.h"
  43. #include "iostat.h"
  44. #include "internal.h"
  45. #define NFSDBG_FACILITY NFSDBG_VFS
  46. #define NFS_PARANOIA 1
  47. static void nfs_invalidate_inode(struct inode *);
  48. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  49. static void nfs_zap_acl_cache(struct inode *);
  50. static kmem_cache_t * nfs_inode_cachep;
  51. static inline unsigned long
  52. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  53. {
  54. return nfs_fileid_to_ino_t(fattr->fileid);
  55. }
  56. int nfs_write_inode(struct inode *inode, int sync)
  57. {
  58. int flags = sync ? FLUSH_SYNC : 0;
  59. int ret;
  60. ret = nfs_commit_inode(inode, flags);
  61. if (ret < 0)
  62. return ret;
  63. return 0;
  64. }
  65. void nfs_clear_inode(struct inode *inode)
  66. {
  67. /*
  68. * The following should never happen...
  69. */
  70. BUG_ON(nfs_have_writebacks(inode));
  71. BUG_ON(!list_empty(&NFS_I(inode)->open_files));
  72. BUG_ON(atomic_read(&NFS_I(inode)->data_updates) != 0);
  73. nfs_zap_acl_cache(inode);
  74. nfs_access_zap_cache(inode);
  75. }
  76. /**
  77. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  78. */
  79. int nfs_sync_mapping(struct address_space *mapping)
  80. {
  81. int ret;
  82. if (mapping->nrpages == 0)
  83. return 0;
  84. unmap_mapping_range(mapping, 0, 0, 0);
  85. ret = filemap_write_and_wait(mapping);
  86. if (ret != 0)
  87. goto out;
  88. ret = nfs_wb_all(mapping->host);
  89. out:
  90. return ret;
  91. }
  92. /*
  93. * Invalidate the local caches
  94. */
  95. static void nfs_zap_caches_locked(struct inode *inode)
  96. {
  97. struct nfs_inode *nfsi = NFS_I(inode);
  98. int mode = inode->i_mode;
  99. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  100. NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
  101. NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
  102. memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
  103. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  104. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  105. else
  106. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  107. }
  108. void nfs_zap_caches(struct inode *inode)
  109. {
  110. spin_lock(&inode->i_lock);
  111. nfs_zap_caches_locked(inode);
  112. spin_unlock(&inode->i_lock);
  113. }
  114. static void nfs_zap_acl_cache(struct inode *inode)
  115. {
  116. void (*clear_acl_cache)(struct inode *);
  117. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  118. if (clear_acl_cache != NULL)
  119. clear_acl_cache(inode);
  120. spin_lock(&inode->i_lock);
  121. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  122. spin_unlock(&inode->i_lock);
  123. }
  124. /*
  125. * Invalidate, but do not unhash, the inode.
  126. * NB: must be called with inode->i_lock held!
  127. */
  128. static void nfs_invalidate_inode(struct inode *inode)
  129. {
  130. set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
  131. nfs_zap_caches_locked(inode);
  132. }
  133. struct nfs_find_desc {
  134. struct nfs_fh *fh;
  135. struct nfs_fattr *fattr;
  136. };
  137. /*
  138. * In NFSv3 we can have 64bit inode numbers. In order to support
  139. * this, and re-exported directories (also seen in NFSv2)
  140. * we are forced to allow 2 different inodes to have the same
  141. * i_ino.
  142. */
  143. static int
  144. nfs_find_actor(struct inode *inode, void *opaque)
  145. {
  146. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  147. struct nfs_fh *fh = desc->fh;
  148. struct nfs_fattr *fattr = desc->fattr;
  149. if (NFS_FILEID(inode) != fattr->fileid)
  150. return 0;
  151. if (nfs_compare_fh(NFS_FH(inode), fh))
  152. return 0;
  153. if (is_bad_inode(inode) || NFS_STALE(inode))
  154. return 0;
  155. return 1;
  156. }
  157. static int
  158. nfs_init_locked(struct inode *inode, void *opaque)
  159. {
  160. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  161. struct nfs_fattr *fattr = desc->fattr;
  162. NFS_FILEID(inode) = fattr->fileid;
  163. nfs_copy_fh(NFS_FH(inode), desc->fh);
  164. return 0;
  165. }
  166. /* Don't use READDIRPLUS on directories that we believe are too large */
  167. #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
  168. /*
  169. * This is our front-end to iget that looks up inodes by file handle
  170. * instead of inode number.
  171. */
  172. struct inode *
  173. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  174. {
  175. struct nfs_find_desc desc = {
  176. .fh = fh,
  177. .fattr = fattr
  178. };
  179. struct inode *inode = ERR_PTR(-ENOENT);
  180. unsigned long hash;
  181. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  182. goto out_no_inode;
  183. if (!fattr->nlink) {
  184. printk("NFS: Buggy server - nlink == 0!\n");
  185. goto out_no_inode;
  186. }
  187. hash = nfs_fattr_to_ino_t(fattr);
  188. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  189. if (inode == NULL) {
  190. inode = ERR_PTR(-ENOMEM);
  191. goto out_no_inode;
  192. }
  193. if (inode->i_state & I_NEW) {
  194. struct nfs_inode *nfsi = NFS_I(inode);
  195. /* We set i_ino for the few things that still rely on it,
  196. * such as stat(2) */
  197. inode->i_ino = hash;
  198. /* We can't support update_atime(), since the server will reset it */
  199. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  200. inode->i_mode = fattr->mode;
  201. /* Why so? Because we want revalidate for devices/FIFOs, and
  202. * that's precisely what we have in nfs_file_inode_operations.
  203. */
  204. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  205. if (S_ISREG(inode->i_mode)) {
  206. inode->i_fop = &nfs_file_operations;
  207. inode->i_data.a_ops = &nfs_file_aops;
  208. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  209. } else if (S_ISDIR(inode->i_mode)) {
  210. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  211. inode->i_fop = &nfs_dir_operations;
  212. if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
  213. && fattr->size <= NFS_LIMIT_READDIRPLUS)
  214. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
  215. /* Deal with crossing mountpoints */
  216. if (!nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
  217. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  218. inode->i_op = &nfs_referral_inode_operations;
  219. else
  220. inode->i_op = &nfs_mountpoint_inode_operations;
  221. inode->i_fop = NULL;
  222. }
  223. } else if (S_ISLNK(inode->i_mode))
  224. inode->i_op = &nfs_symlink_inode_operations;
  225. else
  226. init_special_inode(inode, inode->i_mode, fattr->rdev);
  227. nfsi->read_cache_jiffies = fattr->time_start;
  228. nfsi->last_updated = jiffies;
  229. inode->i_atime = fattr->atime;
  230. inode->i_mtime = fattr->mtime;
  231. inode->i_ctime = fattr->ctime;
  232. if (fattr->valid & NFS_ATTR_FATTR_V4)
  233. nfsi->change_attr = fattr->change_attr;
  234. inode->i_size = nfs_size_to_loff_t(fattr->size);
  235. inode->i_nlink = fattr->nlink;
  236. inode->i_uid = fattr->uid;
  237. inode->i_gid = fattr->gid;
  238. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  239. /*
  240. * report the blocks in 512byte units
  241. */
  242. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  243. inode->i_blksize = inode->i_sb->s_blocksize;
  244. } else {
  245. inode->i_blocks = fattr->du.nfs2.blocks;
  246. inode->i_blksize = fattr->du.nfs2.blocksize;
  247. }
  248. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  249. nfsi->attrtimeo_timestamp = jiffies;
  250. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  251. nfsi->access_cache = RB_ROOT;
  252. unlock_new_inode(inode);
  253. } else
  254. nfs_refresh_inode(inode, fattr);
  255. dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
  256. inode->i_sb->s_id,
  257. (long long)NFS_FILEID(inode),
  258. atomic_read(&inode->i_count));
  259. out:
  260. return inode;
  261. out_no_inode:
  262. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  263. goto out;
  264. }
  265. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
  266. int
  267. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  268. {
  269. struct inode *inode = dentry->d_inode;
  270. struct nfs_fattr fattr;
  271. int error;
  272. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  273. if (attr->ia_valid & ATTR_SIZE) {
  274. if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
  275. attr->ia_valid &= ~ATTR_SIZE;
  276. }
  277. /* Optimization: if the end result is no change, don't RPC */
  278. attr->ia_valid &= NFS_VALID_ATTRS;
  279. if (attr->ia_valid == 0)
  280. return 0;
  281. lock_kernel();
  282. nfs_begin_data_update(inode);
  283. /* Write all dirty data */
  284. filemap_write_and_wait(inode->i_mapping);
  285. nfs_wb_all(inode);
  286. /*
  287. * Return any delegations if we're going to change ACLs
  288. */
  289. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  290. nfs_inode_return_delegation(inode);
  291. error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
  292. if (error == 0)
  293. nfs_refresh_inode(inode, &fattr);
  294. nfs_end_data_update(inode);
  295. unlock_kernel();
  296. return error;
  297. }
  298. /**
  299. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  300. * @inode: pointer to struct inode
  301. * @attr: pointer to struct iattr
  302. *
  303. * Note: we do this in the *proc.c in order to ensure that
  304. * it works for things like exclusive creates too.
  305. */
  306. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
  307. {
  308. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  309. if ((attr->ia_valid & ATTR_MODE) != 0) {
  310. int mode = attr->ia_mode & S_IALLUGO;
  311. mode |= inode->i_mode & ~S_IALLUGO;
  312. inode->i_mode = mode;
  313. }
  314. if ((attr->ia_valid & ATTR_UID) != 0)
  315. inode->i_uid = attr->ia_uid;
  316. if ((attr->ia_valid & ATTR_GID) != 0)
  317. inode->i_gid = attr->ia_gid;
  318. spin_lock(&inode->i_lock);
  319. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  320. spin_unlock(&inode->i_lock);
  321. }
  322. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  323. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  324. inode->i_size = attr->ia_size;
  325. vmtruncate(inode, attr->ia_size);
  326. }
  327. }
  328. static int nfs_wait_schedule(void *word)
  329. {
  330. if (signal_pending(current))
  331. return -ERESTARTSYS;
  332. schedule();
  333. return 0;
  334. }
  335. /*
  336. * Wait for the inode to get unlocked.
  337. */
  338. static int nfs_wait_on_inode(struct inode *inode)
  339. {
  340. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  341. struct nfs_inode *nfsi = NFS_I(inode);
  342. sigset_t oldmask;
  343. int error;
  344. rpc_clnt_sigmask(clnt, &oldmask);
  345. error = wait_on_bit_lock(&nfsi->flags, NFS_INO_REVALIDATING,
  346. nfs_wait_schedule, TASK_INTERRUPTIBLE);
  347. rpc_clnt_sigunmask(clnt, &oldmask);
  348. return error;
  349. }
  350. static void nfs_wake_up_inode(struct inode *inode)
  351. {
  352. struct nfs_inode *nfsi = NFS_I(inode);
  353. clear_bit(NFS_INO_REVALIDATING, &nfsi->flags);
  354. smp_mb__after_clear_bit();
  355. wake_up_bit(&nfsi->flags, NFS_INO_REVALIDATING);
  356. }
  357. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  358. {
  359. struct inode *inode = dentry->d_inode;
  360. int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
  361. int err;
  362. /* Flush out writes to the server in order to update c/mtime */
  363. nfs_sync_inode_wait(inode, 0, 0, FLUSH_NOCOMMIT);
  364. /*
  365. * We may force a getattr if the user cares about atime.
  366. *
  367. * Note that we only have to check the vfsmount flags here:
  368. * - NFS always sets S_NOATIME by so checking it would give a
  369. * bogus result
  370. * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
  371. * no point in checking those.
  372. */
  373. if ((mnt->mnt_flags & MNT_NOATIME) ||
  374. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  375. need_atime = 0;
  376. if (need_atime)
  377. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  378. else
  379. err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  380. if (!err)
  381. generic_fillattr(inode, stat);
  382. return err;
  383. }
  384. static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
  385. {
  386. struct nfs_open_context *ctx;
  387. ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
  388. if (ctx != NULL) {
  389. atomic_set(&ctx->count, 1);
  390. ctx->dentry = dget(dentry);
  391. ctx->vfsmnt = mntget(mnt);
  392. ctx->cred = get_rpccred(cred);
  393. ctx->state = NULL;
  394. ctx->lockowner = current->files;
  395. ctx->error = 0;
  396. ctx->dir_cookie = 0;
  397. }
  398. return ctx;
  399. }
  400. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  401. {
  402. if (ctx != NULL)
  403. atomic_inc(&ctx->count);
  404. return ctx;
  405. }
  406. void put_nfs_open_context(struct nfs_open_context *ctx)
  407. {
  408. if (atomic_dec_and_test(&ctx->count)) {
  409. if (!list_empty(&ctx->list)) {
  410. struct inode *inode = ctx->dentry->d_inode;
  411. spin_lock(&inode->i_lock);
  412. list_del(&ctx->list);
  413. spin_unlock(&inode->i_lock);
  414. }
  415. if (ctx->state != NULL)
  416. nfs4_close_state(ctx->state, ctx->mode);
  417. if (ctx->cred != NULL)
  418. put_rpccred(ctx->cred);
  419. dput(ctx->dentry);
  420. mntput(ctx->vfsmnt);
  421. kfree(ctx);
  422. }
  423. }
  424. /*
  425. * Ensure that mmap has a recent RPC credential for use when writing out
  426. * shared pages
  427. */
  428. static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  429. {
  430. struct inode *inode = filp->f_dentry->d_inode;
  431. struct nfs_inode *nfsi = NFS_I(inode);
  432. filp->private_data = get_nfs_open_context(ctx);
  433. spin_lock(&inode->i_lock);
  434. list_add(&ctx->list, &nfsi->open_files);
  435. spin_unlock(&inode->i_lock);
  436. }
  437. /*
  438. * Given an inode, search for an open context with the desired characteristics
  439. */
  440. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, int mode)
  441. {
  442. struct nfs_inode *nfsi = NFS_I(inode);
  443. struct nfs_open_context *pos, *ctx = NULL;
  444. spin_lock(&inode->i_lock);
  445. list_for_each_entry(pos, &nfsi->open_files, list) {
  446. if (cred != NULL && pos->cred != cred)
  447. continue;
  448. if ((pos->mode & mode) == mode) {
  449. ctx = get_nfs_open_context(pos);
  450. break;
  451. }
  452. }
  453. spin_unlock(&inode->i_lock);
  454. return ctx;
  455. }
  456. static void nfs_file_clear_open_context(struct file *filp)
  457. {
  458. struct inode *inode = filp->f_dentry->d_inode;
  459. struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
  460. if (ctx) {
  461. filp->private_data = NULL;
  462. spin_lock(&inode->i_lock);
  463. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  464. spin_unlock(&inode->i_lock);
  465. put_nfs_open_context(ctx);
  466. }
  467. }
  468. /*
  469. * These allocate and release file read/write context information.
  470. */
  471. int nfs_open(struct inode *inode, struct file *filp)
  472. {
  473. struct nfs_open_context *ctx;
  474. struct rpc_cred *cred;
  475. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  476. if (IS_ERR(cred))
  477. return PTR_ERR(cred);
  478. ctx = alloc_nfs_open_context(filp->f_vfsmnt, filp->f_dentry, cred);
  479. put_rpccred(cred);
  480. if (ctx == NULL)
  481. return -ENOMEM;
  482. ctx->mode = filp->f_mode;
  483. nfs_file_set_open_context(filp, ctx);
  484. put_nfs_open_context(ctx);
  485. return 0;
  486. }
  487. int nfs_release(struct inode *inode, struct file *filp)
  488. {
  489. nfs_file_clear_open_context(filp);
  490. return 0;
  491. }
  492. /*
  493. * This function is called whenever some part of NFS notices that
  494. * the cached attributes have to be refreshed.
  495. */
  496. int
  497. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  498. {
  499. int status = -ESTALE;
  500. struct nfs_fattr fattr;
  501. struct nfs_inode *nfsi = NFS_I(inode);
  502. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  503. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  504. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  505. lock_kernel();
  506. if (!inode || is_bad_inode(inode))
  507. goto out_nowait;
  508. if (NFS_STALE(inode))
  509. goto out_nowait;
  510. status = nfs_wait_on_inode(inode);
  511. if (status < 0)
  512. goto out;
  513. if (NFS_STALE(inode)) {
  514. status = -ESTALE;
  515. /* Do we trust the cached ESTALE? */
  516. if (NFS_ATTRTIMEO(inode) != 0) {
  517. if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME)) {
  518. /* no */
  519. } else
  520. goto out;
  521. }
  522. }
  523. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
  524. if (status != 0) {
  525. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  526. inode->i_sb->s_id,
  527. (long long)NFS_FILEID(inode), status);
  528. if (status == -ESTALE) {
  529. nfs_zap_caches(inode);
  530. if (!S_ISDIR(inode->i_mode))
  531. set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
  532. }
  533. goto out;
  534. }
  535. spin_lock(&inode->i_lock);
  536. status = nfs_update_inode(inode, &fattr);
  537. if (status) {
  538. spin_unlock(&inode->i_lock);
  539. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  540. inode->i_sb->s_id,
  541. (long long)NFS_FILEID(inode), status);
  542. goto out;
  543. }
  544. spin_unlock(&inode->i_lock);
  545. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  546. nfs_zap_acl_cache(inode);
  547. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  548. inode->i_sb->s_id,
  549. (long long)NFS_FILEID(inode));
  550. out:
  551. nfs_wake_up_inode(inode);
  552. out_nowait:
  553. unlock_kernel();
  554. return status;
  555. }
  556. int nfs_attribute_timeout(struct inode *inode)
  557. {
  558. struct nfs_inode *nfsi = NFS_I(inode);
  559. if (nfs_have_delegation(inode, FMODE_READ))
  560. return 0;
  561. return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
  562. }
  563. /**
  564. * nfs_revalidate_inode - Revalidate the inode attributes
  565. * @server - pointer to nfs_server struct
  566. * @inode - pointer to inode struct
  567. *
  568. * Updates inode attribute information by retrieving the data from the server.
  569. */
  570. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  571. {
  572. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  573. && !nfs_attribute_timeout(inode))
  574. return NFS_STALE(inode) ? -ESTALE : 0;
  575. return __nfs_revalidate_inode(server, inode);
  576. }
  577. /**
  578. * nfs_revalidate_mapping - Revalidate the pagecache
  579. * @inode - pointer to host inode
  580. * @mapping - pointer to mapping
  581. */
  582. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  583. {
  584. struct nfs_inode *nfsi = NFS_I(inode);
  585. int ret = 0;
  586. if (NFS_STALE(inode))
  587. ret = -ESTALE;
  588. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  589. || nfs_attribute_timeout(inode))
  590. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  591. if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
  592. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  593. if (S_ISREG(inode->i_mode))
  594. nfs_sync_mapping(mapping);
  595. invalidate_inode_pages2(mapping);
  596. spin_lock(&inode->i_lock);
  597. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  598. if (S_ISDIR(inode->i_mode)) {
  599. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  600. /* This ensures we revalidate child dentries */
  601. nfsi->cache_change_attribute = jiffies;
  602. }
  603. spin_unlock(&inode->i_lock);
  604. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  605. inode->i_sb->s_id,
  606. (long long)NFS_FILEID(inode));
  607. }
  608. return ret;
  609. }
  610. /**
  611. * nfs_begin_data_update
  612. * @inode - pointer to inode
  613. * Declare that a set of operations will update file data on the server
  614. */
  615. void nfs_begin_data_update(struct inode *inode)
  616. {
  617. atomic_inc(&NFS_I(inode)->data_updates);
  618. }
  619. /**
  620. * nfs_end_data_update
  621. * @inode - pointer to inode
  622. * Declare end of the operations that will update file data
  623. * This will mark the inode as immediately needing revalidation
  624. * of its attribute cache.
  625. */
  626. void nfs_end_data_update(struct inode *inode)
  627. {
  628. struct nfs_inode *nfsi = NFS_I(inode);
  629. /* Directories: invalidate page cache */
  630. if (S_ISDIR(inode->i_mode)) {
  631. spin_lock(&inode->i_lock);
  632. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  633. spin_unlock(&inode->i_lock);
  634. }
  635. nfsi->cache_change_attribute = jiffies;
  636. atomic_dec(&nfsi->data_updates);
  637. }
  638. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  639. {
  640. struct nfs_inode *nfsi = NFS_I(inode);
  641. /* If we have atomic WCC data, we may update some attributes */
  642. if ((fattr->valid & NFS_ATTR_WCC) != 0) {
  643. if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
  644. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  645. nfsi->cache_change_attribute = jiffies;
  646. }
  647. if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  648. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  649. nfsi->cache_change_attribute = jiffies;
  650. }
  651. if (inode->i_size == fattr->pre_size && nfsi->npages == 0) {
  652. inode->i_size = fattr->size;
  653. nfsi->cache_change_attribute = jiffies;
  654. }
  655. }
  656. }
  657. /**
  658. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  659. * @inode - pointer to inode
  660. * @fattr - updated attributes
  661. *
  662. * Verifies the attribute cache. If we have just changed the attributes,
  663. * so that fattr carries weak cache consistency data, then it may
  664. * also update the ctime/mtime/change_attribute.
  665. */
  666. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  667. {
  668. struct nfs_inode *nfsi = NFS_I(inode);
  669. loff_t cur_size, new_isize;
  670. int data_unstable;
  671. /* Has the inode gone and changed behind our back? */
  672. if (nfsi->fileid != fattr->fileid
  673. || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  674. return -EIO;
  675. }
  676. /* Are we in the process of updating data on the server? */
  677. data_unstable = nfs_caches_unstable(inode);
  678. /* Do atomic weak cache consistency updates */
  679. nfs_wcc_update_inode(inode, fattr);
  680. if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
  681. nfsi->change_attr != fattr->change_attr)
  682. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  683. /* Verify a few of the more important attributes */
  684. if (!timespec_equal(&inode->i_mtime, &fattr->mtime))
  685. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  686. cur_size = i_size_read(inode);
  687. new_isize = nfs_size_to_loff_t(fattr->size);
  688. if (cur_size != new_isize && nfsi->npages == 0)
  689. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  690. /* Have any file permissions changed? */
  691. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
  692. || inode->i_uid != fattr->uid
  693. || inode->i_gid != fattr->gid)
  694. nfsi->cache_validity |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  695. /* Has the link count changed? */
  696. if (inode->i_nlink != fattr->nlink)
  697. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  698. if (!timespec_equal(&inode->i_atime, &fattr->atime))
  699. nfsi->cache_validity |= NFS_INO_INVALID_ATIME;
  700. nfsi->read_cache_jiffies = fattr->time_start;
  701. return 0;
  702. }
  703. /**
  704. * nfs_refresh_inode - try to update the inode attribute cache
  705. * @inode - pointer to inode
  706. * @fattr - updated attributes
  707. *
  708. * Check that an RPC call that returned attributes has not overlapped with
  709. * other recent updates of the inode metadata, then decide whether it is
  710. * safe to do a full update of the inode attributes, or whether just to
  711. * call nfs_check_inode_attributes.
  712. */
  713. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  714. {
  715. struct nfs_inode *nfsi = NFS_I(inode);
  716. int status;
  717. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  718. return 0;
  719. spin_lock(&inode->i_lock);
  720. if (time_after(fattr->time_start, nfsi->last_updated))
  721. status = nfs_update_inode(inode, fattr);
  722. else
  723. status = nfs_check_inode_attributes(inode, fattr);
  724. spin_unlock(&inode->i_lock);
  725. return status;
  726. }
  727. /**
  728. * nfs_post_op_update_inode - try to update the inode attribute cache
  729. * @inode - pointer to inode
  730. * @fattr - updated attributes
  731. *
  732. * After an operation that has changed the inode metadata, mark the
  733. * attribute cache as being invalid, then try to update it.
  734. *
  735. * NB: if the server didn't return any post op attributes, this
  736. * function will force the retrieval of attributes before the next
  737. * NFS request. Thus it should be used only for operations that
  738. * are expected to change one or more attributes, to avoid
  739. * unnecessary NFS requests and trips through nfs_update_inode().
  740. */
  741. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  742. {
  743. struct nfs_inode *nfsi = NFS_I(inode);
  744. int status = 0;
  745. spin_lock(&inode->i_lock);
  746. if (unlikely((fattr->valid & NFS_ATTR_FATTR) == 0)) {
  747. nfsi->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  748. goto out;
  749. }
  750. status = nfs_update_inode(inode, fattr);
  751. out:
  752. spin_unlock(&inode->i_lock);
  753. return status;
  754. }
  755. /*
  756. * Many nfs protocol calls return the new file attributes after
  757. * an operation. Here we update the inode to reflect the state
  758. * of the server's inode.
  759. *
  760. * This is a bit tricky because we have to make sure all dirty pages
  761. * have been sent off to the server before calling invalidate_inode_pages.
  762. * To make sure no other process adds more write requests while we try
  763. * our best to flush them, we make them sleep during the attribute refresh.
  764. *
  765. * A very similar scenario holds for the dir cache.
  766. */
  767. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  768. {
  769. struct nfs_server *server;
  770. struct nfs_inode *nfsi = NFS_I(inode);
  771. loff_t cur_isize, new_isize;
  772. unsigned int invalid = 0;
  773. int data_stable;
  774. dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
  775. __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
  776. atomic_read(&inode->i_count), fattr->valid);
  777. if (nfsi->fileid != fattr->fileid)
  778. goto out_fileid;
  779. /*
  780. * Make sure the inode's type hasn't changed.
  781. */
  782. if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  783. goto out_changed;
  784. server = NFS_SERVER(inode);
  785. /* Update the fsid if and only if this is the root directory */
  786. if (inode == inode->i_sb->s_root->d_inode
  787. && !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  788. server->fsid = fattr->fsid;
  789. /*
  790. * Update the read time so we don't revalidate too often.
  791. */
  792. nfsi->read_cache_jiffies = fattr->time_start;
  793. nfsi->last_updated = jiffies;
  794. /* Are we racing with known updates of the metadata on the server? */
  795. data_stable = nfs_verify_change_attribute(inode, fattr->time_start);
  796. if (data_stable)
  797. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATIME);
  798. /* Do atomic weak cache consistency updates */
  799. nfs_wcc_update_inode(inode, fattr);
  800. /* Check if our cached file size is stale */
  801. new_isize = nfs_size_to_loff_t(fattr->size);
  802. cur_isize = i_size_read(inode);
  803. if (new_isize != cur_isize) {
  804. /* Do we perhaps have any outstanding writes? */
  805. if (nfsi->npages == 0) {
  806. /* No, but did we race with nfs_end_data_update()? */
  807. if (data_stable) {
  808. inode->i_size = new_isize;
  809. invalid |= NFS_INO_INVALID_DATA;
  810. }
  811. invalid |= NFS_INO_INVALID_ATTR;
  812. } else if (new_isize > cur_isize) {
  813. inode->i_size = new_isize;
  814. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  815. }
  816. nfsi->cache_change_attribute = jiffies;
  817. dprintk("NFS: isize change on server for file %s/%ld\n",
  818. inode->i_sb->s_id, inode->i_ino);
  819. }
  820. /* Check if the mtime agrees */
  821. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  822. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  823. dprintk("NFS: mtime change on server for file %s/%ld\n",
  824. inode->i_sb->s_id, inode->i_ino);
  825. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  826. nfsi->cache_change_attribute = jiffies;
  827. }
  828. /* If ctime has changed we should definitely clear access+acl caches */
  829. if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
  830. invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  831. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  832. nfsi->cache_change_attribute = jiffies;
  833. }
  834. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  835. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
  836. inode->i_uid != fattr->uid ||
  837. inode->i_gid != fattr->gid)
  838. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  839. inode->i_mode = fattr->mode;
  840. inode->i_nlink = fattr->nlink;
  841. inode->i_uid = fattr->uid;
  842. inode->i_gid = fattr->gid;
  843. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  844. /*
  845. * report the blocks in 512byte units
  846. */
  847. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  848. inode->i_blksize = inode->i_sb->s_blocksize;
  849. } else {
  850. inode->i_blocks = fattr->du.nfs2.blocks;
  851. inode->i_blksize = fattr->du.nfs2.blocksize;
  852. }
  853. if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
  854. nfsi->change_attr != fattr->change_attr) {
  855. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  856. inode->i_sb->s_id, inode->i_ino);
  857. nfsi->change_attr = fattr->change_attr;
  858. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  859. nfsi->cache_change_attribute = jiffies;
  860. }
  861. /* Update attrtimeo value if we're out of the unstable period */
  862. if (invalid & NFS_INO_INVALID_ATTR) {
  863. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  864. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  865. nfsi->attrtimeo_timestamp = jiffies;
  866. } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
  867. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  868. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  869. nfsi->attrtimeo_timestamp = jiffies;
  870. }
  871. /* Don't invalidate the data if we were to blame */
  872. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  873. || S_ISLNK(inode->i_mode)))
  874. invalid &= ~NFS_INO_INVALID_DATA;
  875. if (data_stable)
  876. invalid &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
  877. if (!nfs_have_delegation(inode, FMODE_READ))
  878. nfsi->cache_validity |= invalid;
  879. return 0;
  880. out_changed:
  881. /*
  882. * Big trouble! The inode has become a different object.
  883. */
  884. #ifdef NFS_PARANOIA
  885. printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
  886. __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
  887. #endif
  888. out_err:
  889. /*
  890. * No need to worry about unhashing the dentry, as the
  891. * lookup validation will know that the inode is bad.
  892. * (But we fall through to invalidate the caches.)
  893. */
  894. nfs_invalidate_inode(inode);
  895. return -ESTALE;
  896. out_fileid:
  897. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  898. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  899. NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
  900. (long long)nfsi->fileid, (long long)fattr->fileid);
  901. goto out_err;
  902. }
  903. #ifdef CONFIG_NFS_V4
  904. /*
  905. * Clean out any remaining NFSv4 state that might be left over due
  906. * to open() calls that passed nfs_atomic_lookup, but failed to call
  907. * nfs_open().
  908. */
  909. void nfs4_clear_inode(struct inode *inode)
  910. {
  911. struct nfs_inode *nfsi = NFS_I(inode);
  912. /* If we are holding a delegation, return it! */
  913. nfs_inode_return_delegation(inode);
  914. /* First call standard NFS clear_inode() code */
  915. nfs_clear_inode(inode);
  916. /* Now clear out any remaining state */
  917. while (!list_empty(&nfsi->open_states)) {
  918. struct nfs4_state *state;
  919. state = list_entry(nfsi->open_states.next,
  920. struct nfs4_state,
  921. inode_states);
  922. dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
  923. __FUNCTION__,
  924. inode->i_sb->s_id,
  925. (long long)NFS_FILEID(inode),
  926. state);
  927. BUG_ON(atomic_read(&state->count) != 1);
  928. nfs4_close_state(state, state->state);
  929. }
  930. }
  931. #endif
  932. struct inode *nfs_alloc_inode(struct super_block *sb)
  933. {
  934. struct nfs_inode *nfsi;
  935. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
  936. if (!nfsi)
  937. return NULL;
  938. nfsi->flags = 0UL;
  939. nfsi->cache_validity = 0UL;
  940. nfsi->cache_change_attribute = jiffies;
  941. #ifdef CONFIG_NFS_V3_ACL
  942. nfsi->acl_access = ERR_PTR(-EAGAIN);
  943. nfsi->acl_default = ERR_PTR(-EAGAIN);
  944. #endif
  945. #ifdef CONFIG_NFS_V4
  946. nfsi->nfs4_acl = NULL;
  947. #endif /* CONFIG_NFS_V4 */
  948. return &nfsi->vfs_inode;
  949. }
  950. void nfs_destroy_inode(struct inode *inode)
  951. {
  952. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  953. }
  954. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  955. {
  956. #ifdef CONFIG_NFS_V4
  957. INIT_LIST_HEAD(&nfsi->open_states);
  958. nfsi->delegation = NULL;
  959. nfsi->delegation_state = 0;
  960. init_rwsem(&nfsi->rwsem);
  961. #endif
  962. }
  963. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  964. {
  965. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  966. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  967. SLAB_CTOR_CONSTRUCTOR) {
  968. inode_init_once(&nfsi->vfs_inode);
  969. spin_lock_init(&nfsi->req_lock);
  970. INIT_LIST_HEAD(&nfsi->dirty);
  971. INIT_LIST_HEAD(&nfsi->commit);
  972. INIT_LIST_HEAD(&nfsi->open_files);
  973. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  974. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  975. INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
  976. atomic_set(&nfsi->data_updates, 0);
  977. nfsi->ndirty = 0;
  978. nfsi->ncommit = 0;
  979. nfsi->npages = 0;
  980. nfs4_init_once(nfsi);
  981. }
  982. }
  983. static int __init nfs_init_inodecache(void)
  984. {
  985. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  986. sizeof(struct nfs_inode),
  987. 0, (SLAB_RECLAIM_ACCOUNT|
  988. SLAB_MEM_SPREAD),
  989. init_once, NULL);
  990. if (nfs_inode_cachep == NULL)
  991. return -ENOMEM;
  992. return 0;
  993. }
  994. static void nfs_destroy_inodecache(void)
  995. {
  996. if (kmem_cache_destroy(nfs_inode_cachep))
  997. printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
  998. }
  999. /*
  1000. * Initialize NFS
  1001. */
  1002. static int __init init_nfs_fs(void)
  1003. {
  1004. int err;
  1005. err = nfs_fs_proc_init();
  1006. if (err)
  1007. goto out5;
  1008. err = nfs_init_nfspagecache();
  1009. if (err)
  1010. goto out4;
  1011. err = nfs_init_inodecache();
  1012. if (err)
  1013. goto out3;
  1014. err = nfs_init_readpagecache();
  1015. if (err)
  1016. goto out2;
  1017. err = nfs_init_writepagecache();
  1018. if (err)
  1019. goto out1;
  1020. err = nfs_init_directcache();
  1021. if (err)
  1022. goto out0;
  1023. #ifdef CONFIG_PROC_FS
  1024. rpc_proc_register(&nfs_rpcstat);
  1025. #endif
  1026. if ((err = register_nfs_fs()) != 0)
  1027. goto out;
  1028. return 0;
  1029. out:
  1030. #ifdef CONFIG_PROC_FS
  1031. rpc_proc_unregister("nfs");
  1032. #endif
  1033. nfs_destroy_directcache();
  1034. out0:
  1035. nfs_destroy_writepagecache();
  1036. out1:
  1037. nfs_destroy_readpagecache();
  1038. out2:
  1039. nfs_destroy_inodecache();
  1040. out3:
  1041. nfs_destroy_nfspagecache();
  1042. out4:
  1043. nfs_fs_proc_exit();
  1044. out5:
  1045. return err;
  1046. }
  1047. static void __exit exit_nfs_fs(void)
  1048. {
  1049. nfs_destroy_directcache();
  1050. nfs_destroy_writepagecache();
  1051. nfs_destroy_readpagecache();
  1052. nfs_destroy_inodecache();
  1053. nfs_destroy_nfspagecache();
  1054. #ifdef CONFIG_PROC_FS
  1055. rpc_proc_unregister("nfs");
  1056. #endif
  1057. unregister_nfs_fs();
  1058. nfs_fs_proc_exit();
  1059. }
  1060. /* Not quite true; I just maintain it */
  1061. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1062. MODULE_LICENSE("GPL");
  1063. module_init(init_nfs_fs)
  1064. module_exit(exit_nfs_fs)