inode.c 53.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7
/*
 *  linux/fs/nfs/inode.c
 *
 *  Copyright (C) 1992  Rick Sladkey
 *
 *  nfs inode and superblock handling functions
 *
Alan Cox's avatar
Alan Cox committed
8
 *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17
 *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
 *
 *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
 *  J.S.Peatfield@damtp.cam.ac.uk
 *
 */

#include <linux/module.h>
#include <linux/init.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
18
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
19 20 21 22 23 24 25 26 27
#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/stats.h>
28
#include <linux/sunrpc/metrics.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35
#include <linux/nfs_fs.h>
#include <linux/nfs_mount.h>
#include <linux/nfs4_mount.h>
#include <linux/lockd/bind.h>
#include <linux/seq_file.h>
#include <linux/mount.h>
#include <linux/vfs.h>
36 37
#include <linux/inet.h>
#include <linux/nfs_xdr.h>
38
#include <linux/slab.h>
39
#include <linux/compat.h>
40
#include <linux/freezer.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43

#include <asm/uaccess.h>

44
#include "nfs4_fs.h"
45
#include "callback.h"
Linus Torvalds's avatar
Linus Torvalds committed
46
#include "delegation.h"
47
#include "iostat.h"
David Howells's avatar
David Howells committed
48
#include "internal.h"
49
#include "fscache.h"
50
#include "pnfs.h"
51
#include "nfs.h"
52
#include "netns.h"
Linus Torvalds's avatar
Linus Torvalds committed
53

54 55
#include "nfstrace.h"

Linus Torvalds's avatar
Linus Torvalds committed
56 57
#define NFSDBG_FACILITY		NFSDBG_VFS

58 59 60
#define NFS_64_BIT_INODE_NUMBERS_ENABLED	1

/* Default is to see 64-bit inode numbers */
61
static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
62

Linus Torvalds's avatar
Linus Torvalds committed
63
static void nfs_invalidate_inode(struct inode *);
64
static int nfs_update_inode(struct inode *, struct nfs_fattr *);
Linus Torvalds's avatar
Linus Torvalds committed
65

66
static struct kmem_cache * nfs_inode_cachep;
67

Linus Torvalds's avatar
Linus Torvalds committed
68 69 70 71 72 73
static inline unsigned long
nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
{
	return nfs_fileid_to_ino_t(fattr->fileid);
}

74 75 76 77
/**
 * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
 * @word: long word containing the bit lock
 */
78
int nfs_wait_bit_killable(struct wait_bit_key *key)
79 80 81
{
	if (fatal_signal_pending(current))
		return -ERESTARTSYS;
82
	freezable_schedule_unsafe();
83 84
	return 0;
}
85
EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
86

87 88 89 90 91 92 93 94 95
/**
 * nfs_compat_user_ino64 - returns the user-visible inode number
 * @fileid: 64-bit fileid
 *
 * This function returns a 32-bit inode number if the boot parameter
 * nfs.enable_ino64 is zero.
 */
u64 nfs_compat_user_ino64(u64 fileid)
{
96 97 98 99 100
#ifdef CONFIG_COMPAT
	compat_ulong_t ino;
#else	
	unsigned long ino;
#endif
101 102 103 104 105 106 107 108 109

	if (enable_ino64)
		return fileid;
	ino = fileid;
	if (sizeof(ino) < sizeof(fileid))
		ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
	return ino;
}

110 111 112 113 114 115
int nfs_drop_inode(struct inode *inode)
{
	return NFS_STALE(inode) || generic_drop_inode(inode);
}
EXPORT_SYMBOL_GPL(nfs_drop_inode);

116
void nfs_clear_inode(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
117
{
118 119 120
	/*
	 * The following should never happen...
	 */
121 122
	WARN_ON_ONCE(nfs_have_writebacks(inode));
	WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
123
	nfs_zap_acl_cache(inode);
124
	nfs_access_zap_cache(inode);
125
	nfs_fscache_clear_inode(inode);
Linus Torvalds's avatar
Linus Torvalds committed
126
}
127
EXPORT_SYMBOL_GPL(nfs_clear_inode);
Linus Torvalds's avatar
Linus Torvalds committed
128

129 130
void nfs_evict_inode(struct inode *inode)
{
131
	truncate_inode_pages_final(&inode->i_data);
132
	clear_inode(inode);
133 134 135
	nfs_clear_inode(inode);
}

136 137 138 139 140
/**
 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
 */
int nfs_sync_mapping(struct address_space *mapping)
{
141
	int ret = 0;
142

143 144 145 146
	if (mapping->nrpages != 0) {
		unmap_mapping_range(mapping, 0, 0, 0);
		ret = nfs_wb_all(mapping->host);
	}
147 148 149
	return ret;
}

150 151 152 153 154 155 156 157 158 159 160
static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
{
	struct nfs_inode *nfsi = NFS_I(inode);

	if (inode->i_mapping->nrpages == 0)
		flags &= ~NFS_INO_INVALID_DATA;
	nfsi->cache_validity |= flags;
	if (flags & NFS_INO_INVALID_DATA)
		nfs_fscache_invalidate(inode);
}

Linus Torvalds's avatar
Linus Torvalds committed
161 162 163
/*
 * Invalidate the local caches
 */
164
static void nfs_zap_caches_locked(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168
{
	struct nfs_inode *nfsi = NFS_I(inode);
	int mode = inode->i_mode;

169 170
	nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);

171 172
	nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
	nfsi->attrtimeo_timestamp = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
173

174
	memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
David Howells's avatar
David Howells committed
175
	if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
176
		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
177 178 179
					| NFS_INO_INVALID_DATA
					| NFS_INO_INVALID_ACCESS
					| NFS_INO_INVALID_ACL
180
					| NFS_INO_REVAL_PAGECACHE);
181
	} else
182
		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
183 184
					| NFS_INO_INVALID_ACCESS
					| NFS_INO_INVALID_ACL
185
					| NFS_INO_REVAL_PAGECACHE);
186
	nfs_zap_label_cache_locked(nfsi);
187
}
188

189 190 191 192
void nfs_zap_caches(struct inode *inode)
{
	spin_lock(&inode->i_lock);
	nfs_zap_caches_locked(inode);
193
	spin_unlock(&inode->i_lock);
194
}
Anna Schumaker's avatar
Anna Schumaker committed
195
EXPORT_SYMBOL_GPL(nfs_zap_caches);
196

197 198 199 200
void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
{
	if (mapping->nrpages != 0) {
		spin_lock(&inode->i_lock);
201
		nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
202 203 204 205
		spin_unlock(&inode->i_lock);
	}
}

206
void nfs_zap_acl_cache(struct inode *inode)
207 208 209 210 211 212
{
	void (*clear_acl_cache)(struct inode *);

	clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
	if (clear_acl_cache != NULL)
		clear_acl_cache(inode);
213
	spin_lock(&inode->i_lock);
214
	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
215
	spin_unlock(&inode->i_lock);
Linus Torvalds's avatar
Linus Torvalds committed
216
}
217
EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
Linus Torvalds's avatar
Linus Torvalds committed
218

219 220 221
void nfs_invalidate_atime(struct inode *inode)
{
	spin_lock(&inode->i_lock);
222
	nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
223 224
	spin_unlock(&inode->i_lock);
}
225
EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
226

Linus Torvalds's avatar
Linus Torvalds committed
227
/*
228 229
 * Invalidate, but do not unhash, the inode.
 * NB: must be called with inode->i_lock held!
Linus Torvalds's avatar
Linus Torvalds committed
230
 */
231
static void nfs_invalidate_inode(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
232
{
Benny Halevy's avatar
Benny Halevy committed
233
	set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
234
	nfs_zap_caches_locked(inode);
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
}

struct nfs_find_desc {
	struct nfs_fh		*fh;
	struct nfs_fattr	*fattr;
};

/*
 * In NFSv3 we can have 64bit inode numbers. In order to support
 * this, and re-exported directories (also seen in NFSv2)
 * we are forced to allow 2 different inodes to have the same
 * i_ino.
 */
static int
nfs_find_actor(struct inode *inode, void *opaque)
{
	struct nfs_find_desc	*desc = (struct nfs_find_desc *)opaque;
	struct nfs_fh		*fh = desc->fh;
	struct nfs_fattr	*fattr = desc->fattr;

	if (NFS_FILEID(inode) != fattr->fileid)
		return 0;
257 258
	if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
259 260 261 262 263 264 265 266 267 268 269 270 271
	if (nfs_compare_fh(NFS_FH(inode), fh))
		return 0;
	if (is_bad_inode(inode) || NFS_STALE(inode))
		return 0;
	return 1;
}

static int
nfs_init_locked(struct inode *inode, void *opaque)
{
	struct nfs_find_desc	*desc = (struct nfs_find_desc *)opaque;
	struct nfs_fattr	*fattr = desc->fattr;

272
	set_nfs_fileid(inode, fattr->fileid);
Linus Torvalds's avatar
Linus Torvalds committed
273 274 275 276
	nfs_copy_fh(NFS_FH(inode), desc->fh);
	return 0;
}

277
#ifdef CONFIG_NFS_V4_SECURITY_LABEL
278 279 280 281 282 283 284
static void nfs_clear_label_invalid(struct inode *inode)
{
	spin_lock(&inode->i_lock);
	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
	spin_unlock(&inode->i_lock);
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
					struct nfs4_label *label)
{
	int error;

	if (label == NULL)
		return;

	if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
		error = security_inode_notifysecctx(inode, label->label,
				label->len);
		if (error)
			printk(KERN_ERR "%s() %s %d "
					"security_inode_notifysecctx() %d\n",
					__func__,
					(char *)label->label,
					label->len, error);
302
		nfs_clear_label_invalid(inode);
303 304 305
	}
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
{
	struct nfs4_label *label = NULL;
	int minor_version = server->nfs_client->cl_minorversion;

	if (minor_version < 2)
		return label;

	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
		return label;

	label = kzalloc(sizeof(struct nfs4_label), flags);
	if (label == NULL)
		return ERR_PTR(-ENOMEM);

	label->label = kzalloc(NFS4_MAXLABELLEN, flags);
	if (label->label == NULL) {
		kfree(label);
		return ERR_PTR(-ENOMEM);
	}
	label->len = NFS4_MAXLABELLEN;

	return label;
}
EXPORT_SYMBOL_GPL(nfs4_label_alloc);
331
#else
332
void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
333 334 335
					struct nfs4_label *label)
{
}
336
#endif
337
EXPORT_SYMBOL_GPL(nfs_setsecurity);
338

Linus Torvalds's avatar
Linus Torvalds committed
339 340 341 342 343
/*
 * This is our front-end to iget that looks up inodes by file handle
 * instead of inode number.
 */
struct inode *
344
nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds's avatar
Linus Torvalds committed
345 346 347 348 349
{
	struct nfs_find_desc desc = {
		.fh	= fh,
		.fattr	= fattr
	};
350
	struct inode *inode = ERR_PTR(-ENOENT);
Linus Torvalds's avatar
Linus Torvalds committed
351 352
	unsigned long hash;

353 354
	nfs_attr_check_mountpoint(sb, fattr);

355 356
	if (((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) &&
	    !nfs_attr_use_mounted_on_fileid(fattr))
Linus Torvalds's avatar
Linus Torvalds committed
357
		goto out_no_inode;
358
	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
359 360 361 362
		goto out_no_inode;

	hash = nfs_fattr_to_ino_t(fattr);

363 364 365
	inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
	if (inode == NULL) {
		inode = ERR_PTR(-ENOMEM);
Linus Torvalds's avatar
Linus Torvalds committed
366
		goto out_no_inode;
367
	}
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370

	if (inode->i_state & I_NEW) {
		struct nfs_inode *nfsi = NFS_I(inode);
371
		unsigned long now = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378 379

		/* We set i_ino for the few things that still rely on it,
		 * such as stat(2) */
		inode->i_ino = hash;

		/* We can't support update_atime(), since the server will reset it */
		inode->i_flags |= S_NOATIME|S_NOCMTIME;
		inode->i_mode = fattr->mode;
380 381
		if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
				&& nfs_server_capable(inode, NFS_CAP_MODE))
382
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
Linus Torvalds's avatar
Linus Torvalds committed
383 384 385
		/* Why so? Because we want revalidate for devices/FIFOs, and
		 * that's precisely what we have in nfs_file_inode_operations.
		 */
386
		inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
Linus Torvalds's avatar
Linus Torvalds committed
387
		if (S_ISREG(inode->i_mode)) {
388
			inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
Linus Torvalds's avatar
Linus Torvalds committed
389 390
			inode->i_data.a_ops = &nfs_file_aops;
		} else if (S_ISDIR(inode->i_mode)) {
391
			inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
Linus Torvalds's avatar
Linus Torvalds committed
392
			inode->i_fop = &nfs_dir_operations;
393
			inode->i_data.a_ops = &nfs_dir_aops;
394
			/* Deal with crossing mountpoints */
395 396
			if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
					fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
Manoj Naik's avatar
Manoj Naik committed
397 398 399 400
				if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
					inode->i_op = &nfs_referral_inode_operations;
				else
					inode->i_op = &nfs_mountpoint_inode_operations;
401
				inode->i_fop = NULL;
402
				inode->i_flags |= S_AUTOMOUNT;
403
			}
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407 408
		} else if (S_ISLNK(inode->i_mode))
			inode->i_op = &nfs_symlink_inode_operations;
		else
			init_special_inode(inode, inode->i_mode, fattr->rdev);

409 410 411
		memset(&inode->i_atime, 0, sizeof(inode->i_atime));
		memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
		memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
412
		inode->i_version = 0;
413
		inode->i_size = 0;
414
		clear_nlink(inode);
415 416
		inode->i_uid = make_kuid(&init_user_ns, -2);
		inode->i_gid = make_kgid(&init_user_ns, -2);
417 418
		inode->i_blocks = 0;
		memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
419 420
		nfsi->write_io = 0;
		nfsi->read_io = 0;
421

422
		nfsi->read_cache_jiffies = fattr->time_start;
423
		nfsi->attr_gencount = fattr->gencount;
424 425
		if (fattr->valid & NFS_ATTR_FATTR_ATIME)
			inode->i_atime = fattr->atime;
426
		else if (nfs_server_capable(inode, NFS_CAP_ATIME))
427
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
428 429
		if (fattr->valid & NFS_ATTR_FATTR_MTIME)
			inode->i_mtime = fattr->mtime;
430
		else if (nfs_server_capable(inode, NFS_CAP_MTIME))
431
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
432 433
		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
			inode->i_ctime = fattr->ctime;
434
		else if (nfs_server_capable(inode, NFS_CAP_CTIME))
435
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
436
		if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
437
			inode->i_version = fattr->change_attr;
438
		else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
439
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
440 441
		if (fattr->valid & NFS_ATTR_FATTR_SIZE)
			inode->i_size = nfs_size_to_loff_t(fattr->size);
442
		else
443 444
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
				| NFS_INO_REVAL_PAGECACHE);
445
		if (fattr->valid & NFS_ATTR_FATTR_NLINK)
Miklos Szeredi's avatar
Miklos Szeredi committed
446
			set_nlink(inode, fattr->nlink);
447
		else if (nfs_server_capable(inode, NFS_CAP_NLINK))
448
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
449 450
		if (fattr->valid & NFS_ATTR_FATTR_OWNER)
			inode->i_uid = fattr->uid;
451
		else if (nfs_server_capable(inode, NFS_CAP_OWNER))
452
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
453 454
		if (fattr->valid & NFS_ATTR_FATTR_GROUP)
			inode->i_gid = fattr->gid;
455
		else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
456
			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
457 458 459
		if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
			inode->i_blocks = fattr->du.nfs2.blocks;
		if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464
			/*
			 * report the blocks in 512byte units
			 */
			inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
		}
465 466 467

		nfs_setsecurity(inode, fattr, label);

Linus Torvalds's avatar
Linus Torvalds committed
468
		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
469
		nfsi->attrtimeo_timestamp = now;
470
		nfsi->access_cache = RB_ROOT;
Linus Torvalds's avatar
Linus Torvalds committed
471

472
		nfs_fscache_init_inode(inode);
473

Linus Torvalds's avatar
Linus Torvalds committed
474 475 476
		unlock_new_inode(inode);
	} else
		nfs_refresh_inode(inode, fattr);
477
	dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
Linus Torvalds's avatar
Linus Torvalds committed
478
		inode->i_sb->s_id,
479
		(unsigned long long)NFS_FILEID(inode),
480
		nfs_display_fhandle_hash(fh),
Linus Torvalds's avatar
Linus Torvalds committed
481 482 483 484 485 486
		atomic_read(&inode->i_count));

out:
	return inode;

out_no_inode:
487
	dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
Linus Torvalds's avatar
Linus Torvalds committed
488 489
	goto out;
}
490
EXPORT_SYMBOL_GPL(nfs_fhget);
Linus Torvalds's avatar
Linus Torvalds committed
491

492
#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)
Linus Torvalds's avatar
Linus Torvalds committed
493 494 495 496 497

int
nfs_setattr(struct dentry *dentry, struct iattr *attr)
{
	struct inode *inode = dentry->d_inode;
498 499
	struct nfs_fattr *fattr;
	int error = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
500

501 502
	nfs_inc_stats(inode, NFSIOS_VFSSETATTR);

503 504 505 506
	/* skip mode change if it's just for clearing setuid/setgid */
	if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
		attr->ia_valid &= ~ATTR_MODE;

Linus Torvalds's avatar
Linus Torvalds committed
507
	if (attr->ia_valid & ATTR_SIZE) {
508 509 510
		BUG_ON(!S_ISREG(inode->i_mode));

		if (attr->ia_size == i_size_read(inode))
Linus Torvalds's avatar
Linus Torvalds committed
511 512 513 514 515
			attr->ia_valid &= ~ATTR_SIZE;
	}

	/* Optimization: if the end result is no change, don't RPC */
	attr->ia_valid &= NFS_VALID_ATTRS;
516
	if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
517 518
		return 0;

519 520
	trace_nfs_setattr_enter(inode);

521
	/* Write all dirty data */
522 523
	if (S_ISREG(inode->i_mode)) {
		nfs_inode_dio_wait(inode);
524
		nfs_wb_all(inode);
525
	}
526 527 528 529

	fattr = nfs_alloc_fattr();
	if (fattr == NULL)
		goto out;
530 531 532 533
	/*
	 * Return any delegations if we're going to change ACLs
	 */
	if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
534
		NFS_PROTO(inode)->return_delegation(inode);
535
	error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
536
	if (error == 0)
537
		error = nfs_refresh_inode(inode, fattr);
538 539
	nfs_free_fattr(fattr);
out:
540
	trace_nfs_setattr_exit(inode, error);
541 542
	return error;
}
543
EXPORT_SYMBOL_GPL(nfs_setattr);
544

545 546 547 548 549 550 551 552 553 554 555
/**
 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
 * @inode: inode of the file used
 * @offset: file offset to start truncating
 *
 * This is a copy of the common vmtruncate, but with the locking
 * corrected to take into account the fact that NFS requires
 * inode->i_size to be updated under the inode->i_lock.
 */
static int nfs_vmtruncate(struct inode * inode, loff_t offset)
{
npiggin@suse.de's avatar
npiggin@suse.de committed
556
	int err;
557

npiggin@suse.de's avatar
npiggin@suse.de committed
558 559 560
	err = inode_newsize_ok(inode, offset);
	if (err)
		goto out;
561

npiggin@suse.de's avatar
npiggin@suse.de committed
562 563
	spin_lock(&inode->i_lock);
	i_size_write(inode, offset);
564 565 566
	/* Optimisation */
	if (offset == 0)
		NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
npiggin@suse.de's avatar
npiggin@suse.de committed
567 568
	spin_unlock(&inode->i_lock);

569
	truncate_pagecache(inode, offset);
npiggin@suse.de's avatar
npiggin@suse.de committed
570 571
out:
	return err;
572 573
}

574 575 576 577 578 579 580 581 582 583 584
/**
 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
 * @inode: pointer to struct inode
 * @attr: pointer to struct iattr
 *
 * Note: we do this in the *proc.c in order to ensure that
 *       it works for things like exclusive creates too.
 */
void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
{
	if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
585
		spin_lock(&inode->i_lock);
Linus Torvalds's avatar
Linus Torvalds committed
586
		if ((attr->ia_valid & ATTR_MODE) != 0) {
587 588
			int mode = attr->ia_mode & S_IALLUGO;
			mode |= inode->i_mode & ~S_IALLUGO;
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593 594
			inode->i_mode = mode;
		}
		if ((attr->ia_valid & ATTR_UID) != 0)
			inode->i_uid = attr->ia_uid;
		if ((attr->ia_valid & ATTR_GID) != 0)
			inode->i_gid = attr->ia_gid;
595 596
		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
				| NFS_INO_INVALID_ACL);
597
		spin_unlock(&inode->i_lock);
598 599
	}
	if ((attr->ia_valid & ATTR_SIZE) != 0) {
600
		nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
601
		nfs_vmtruncate(inode, attr->ia_size);
602
	}
Linus Torvalds's avatar
Linus Torvalds committed
603
}
604
EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
Linus Torvalds's avatar
Linus Torvalds committed
605

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
static void nfs_request_parent_use_readdirplus(struct dentry *dentry)
{
	struct dentry *parent;

	parent = dget_parent(dentry);
	nfs_force_use_readdirplus(parent->d_inode);
	dput(parent);
}

static bool nfs_need_revalidate_inode(struct inode *inode)
{
	if (NFS_I(inode)->cache_validity &
			(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
		return true;
	if (nfs_attribute_cache_expired(inode))
		return true;
	return false;
}

Linus Torvalds's avatar
Linus Torvalds committed
625 626 627
int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
{
	struct inode *inode = dentry->d_inode;
628
	int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
629
	int err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
630

631
	trace_nfs_getattr_enter(inode);
632
	/* Flush out writes to the server in order to update c/mtime.  */
633
	if (S_ISREG(inode->i_mode)) {
634
		nfs_inode_dio_wait(inode);
635 636 637
		err = filemap_write_and_wait(inode->i_mapping);
		if (err)
			goto out;
638
	}
639 640 641 642 643 644 645 646 647 648 649 650

	/*
	 * We may force a getattr if the user cares about atime.
	 *
	 * Note that we only have to check the vfsmount flags here:
	 *  - NFS always sets S_NOATIME by so checking it would give a
	 *    bogus result
	 *  - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
	 *    no point in checking those.
	 */
 	if ((mnt->mnt_flags & MNT_NOATIME) ||
 	    ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
Linus Torvalds's avatar
Linus Torvalds committed
651
		need_atime = 0;
652

653 654 655 656 657 658 659
	if (need_atime || nfs_need_revalidate_inode(inode)) {
		struct nfs_server *server = NFS_SERVER(inode);

		if (server->caps & NFS_CAP_READDIRPLUS)
			nfs_request_parent_use_readdirplus(dentry);
		err = __nfs_revalidate_inode(server, inode);
	}
660
	if (!err) {
Linus Torvalds's avatar
Linus Torvalds committed
661
		generic_fillattr(inode, stat);
662
		stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
663
	}
664
out:
665
	trace_nfs_getattr_exit(inode, err);
Linus Torvalds's avatar
Linus Torvalds committed
666 667
	return err;
}
668
EXPORT_SYMBOL_GPL(nfs_getattr);
Linus Torvalds's avatar
Linus Torvalds committed
669

670 671 672
static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
{
	atomic_set(&l_ctx->count, 1);
673 674
	l_ctx->lockowner.l_owner = current->files;
	l_ctx->lockowner.l_pid = current->tgid;
675
	INIT_LIST_HEAD(&l_ctx->list);
676
	nfs_iocounter_init(&l_ctx->io_count);
677 678 679 680
}

static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
{
681 682
	struct nfs_lock_context *head = &ctx->lock_context;
	struct nfs_lock_context *pos = head;
683

684
	do {
685
		if (pos->lockowner.l_owner != current->files)
686
			continue;
687
		if (pos->lockowner.l_pid != current->tgid)
688 689 690
			continue;
		atomic_inc(&pos->count);
		return pos;
691
	} while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head);
692 693 694 695 696 697
	return NULL;
}

struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
{
	struct nfs_lock_context *res, *new = NULL;
698
	struct inode *inode = ctx->dentry->d_inode;
699 700 701 702 703 704 705

	spin_lock(&inode->i_lock);
	res = __nfs_find_lock_context(ctx);
	if (res == NULL) {
		spin_unlock(&inode->i_lock);
		new = kmalloc(sizeof(*new), GFP_KERNEL);
		if (new == NULL)
706
			return ERR_PTR(-ENOMEM);
707 708 709 710 711 712 713 714 715 716 717 718 719 720
		nfs_init_lock_context(new);
		spin_lock(&inode->i_lock);
		res = __nfs_find_lock_context(ctx);
		if (res == NULL) {
			list_add_tail(&new->list, &ctx->lock_context.list);
			new->open_context = ctx;
			res = new;
			new = NULL;
		}
	}
	spin_unlock(&inode->i_lock);
	kfree(new);
	return res;
}
Anna Schumaker's avatar
Anna Schumaker committed
721
EXPORT_SYMBOL_GPL(nfs_get_lock_context);
722 723 724 725

void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
{
	struct nfs_open_context *ctx = l_ctx->open_context;
726
	struct inode *inode = ctx->dentry->d_inode;
727 728 729 730 731 732 733

	if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock))
		return;
	list_del(&l_ctx->list);
	spin_unlock(&inode->i_lock);
	kfree(l_ctx);
}
Anna Schumaker's avatar
Anna Schumaker committed
734
EXPORT_SYMBOL_GPL(nfs_put_lock_context);
735

Trond Myklebust's avatar
Trond Myklebust committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
/**
 * nfs_close_context - Common close_context() routine NFSv2/v3
 * @ctx: pointer to context
 * @is_sync: is this a synchronous close
 *
 * always ensure that the attributes are up to date if we're mounted
 * with close-to-open semantics
 */
void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
{
	struct inode *inode;
	struct nfs_server *server;

	if (!(ctx->mode & FMODE_WRITE))
		return;
	if (!is_sync)
		return;
753
	inode = ctx->dentry->d_inode;
Trond Myklebust's avatar
Trond Myklebust committed
754 755 756 757 758 759 760
	if (!list_empty(&NFS_I(inode)->open_files))
		return;
	server = NFS_SERVER(inode);
	if (server->flags & NFS_MOUNT_NOCTO)
		return;
	nfs_revalidate_inode(server, inode);
}
761
EXPORT_SYMBOL_GPL(nfs_close_context);
Trond Myklebust's avatar
Trond Myklebust committed
762

763
struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, fmode_t f_mode)
Linus Torvalds's avatar
Linus Torvalds committed
764 765
{
	struct nfs_open_context *ctx;
766 767 768
	struct rpc_cred *cred = rpc_lookup_cred();
	if (IS_ERR(cred))
		return ERR_CAST(cred);
Linus Torvalds's avatar
Linus Torvalds committed
769

770
	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
771 772 773
	if (!ctx) {
		put_rpccred(cred);
		return ERR_PTR(-ENOMEM);
Linus Torvalds's avatar
Linus Torvalds committed
774
	}
775 776 777 778 779 780 781 782 783 784
	nfs_sb_active(dentry->d_sb);
	ctx->dentry = dget(dentry);
	ctx->cred = cred;
	ctx->state = NULL;
	ctx->mode = f_mode;
	ctx->flags = 0;
	ctx->error = 0;
	nfs_init_lock_context(&ctx->lock_context);
	ctx->lock_context.open_context = ctx;
	INIT_LIST_HEAD(&ctx->list);
785
	ctx->mdsthreshold = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
786 787
	return ctx;
}
788
EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791 792

struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
{
	if (ctx != NULL)
793
		atomic_inc(&ctx->lock_context.count);
Linus Torvalds's avatar
Linus Torvalds committed
794 795
	return ctx;
}
796
EXPORT_SYMBOL_GPL(get_nfs_open_context);
Linus Torvalds's avatar
Linus Torvalds committed
797

Trond Myklebust's avatar
Trond Myklebust committed
798
static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
Linus Torvalds's avatar
Linus Torvalds committed
799
{
800 801
	struct inode *inode = ctx->dentry->d_inode;
	struct super_block *sb = ctx->dentry->d_sb;
802

803
	if (!list_empty(&ctx->list)) {
804 805 806 807 808
		if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
			return;
		list_del(&ctx->list);
		spin_unlock(&inode->i_lock);
	} else if (!atomic_dec_and_test(&ctx->lock_context.count))
809
		return;
810 811
	if (inode != NULL)
		NFS_PROTO(inode)->close_context(ctx, is_sync);
812 813
	if (ctx->cred != NULL)
		put_rpccred(ctx->cred);
814
	dput(ctx->dentry);
815
	nfs_sb_deactive(sb);
816
	kfree(ctx->mdsthreshold);
817 818 819
	kfree(ctx);
}

820 821 822 823
void put_nfs_open_context(struct nfs_open_context *ctx)
{
	__put_nfs_open_context(ctx, 0);
}
824
EXPORT_SYMBOL_GPL(put_nfs_open_context);
825

Linus Torvalds's avatar
Linus Torvalds committed
826 827 828 829
/*
 * Ensure that mmap has a recent RPC credential for use when writing out
 * shared pages
 */
830
void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
Linus Torvalds's avatar
Linus Torvalds committed
831
{
832
	struct inode *inode = ctx->dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
833 834 835 836 837 838
	struct nfs_inode *nfsi = NFS_I(inode);

	spin_lock(&inode->i_lock);
	list_add(&ctx->list, &nfsi->open_files);
	spin_unlock(&inode->i_lock);
}
839 840 841 842 843 844 845 846
EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);

void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
{
	filp->private_data = get_nfs_open_context(ctx);
	if (list_empty(&ctx->list))
		nfs_inode_attach_open_context(ctx);
}
847
EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
Linus Torvalds's avatar
Linus Torvalds committed
848

849 850 851
/*
 * Given an inode, search for an open context with the desired characteristics
 */
852
struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
Linus Torvalds's avatar
Linus Torvalds committed
853 854 855 856 857 858
{
	struct nfs_inode *nfsi = NFS_I(inode);
	struct nfs_open_context *pos, *ctx = NULL;

	spin_lock(&inode->i_lock);
	list_for_each_entry(pos, &nfsi->open_files, list) {
859 860
		if (cred != NULL && pos->cred != cred)
			continue;
861 862 863 864
		if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
			continue;
		ctx = get_nfs_open_context(pos);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
865 866 867 868 869
	}
	spin_unlock(&inode->i_lock);
	return ctx;
}

870
static void nfs_file_clear_open_context(struct file *filp)
Linus Torvalds's avatar
Linus Torvalds committed
871
{
872
	struct nfs_open_context *ctx = nfs_file_open_context(filp);
Linus Torvalds's avatar
Linus Torvalds committed
873 874

	if (ctx) {
875 876
		struct inode *inode = ctx->dentry->d_inode;

Linus Torvalds's avatar
Linus Torvalds committed
877 878 879 880
		filp->private_data = NULL;
		spin_lock(&inode->i_lock);
		list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
		spin_unlock(&inode->i_lock);
881
		__put_nfs_open_context(ctx, filp->f_flags & O_DIRECT ? 0 : 1);
Linus Torvalds's avatar
Linus Torvalds committed
882 883 884 885 886 887 888 889 890 891
	}
}

/*
 * These allocate and release file read/write context information.
 */
int nfs_open(struct inode *inode, struct file *filp)
{
	struct nfs_open_context *ctx;

892 893 894
	ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
Linus Torvalds's avatar
Linus Torvalds committed
895 896
	nfs_file_set_open_context(filp, ctx);
	put_nfs_open_context(ctx);
897
	nfs_fscache_open_file(inode, filp);
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	return 0;
}

int nfs_release(struct inode *inode, struct file *filp)
{
	nfs_file_clear_open_context(filp);
	return 0;
}

/*
 * This function is called whenever some part of NFS notices that
 * the cached attributes have to be refreshed.
 */
int
__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
{
	int		 status = -ESTALE;
915
	struct nfs4_label *label = NULL;
916
	struct nfs_fattr *fattr = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
917 918
	struct nfs_inode *nfsi = NFS_I(inode);

919 920
	dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
		inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
Linus Torvalds's avatar
Linus Torvalds committed
921

922 923
	trace_nfs_revalidate_inode_enter(inode);

924
	if (is_bad_inode(inode))
925
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
926
	if (NFS_STALE(inode))
927
		go