inode.c 33 KB

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