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