Skip to content
Snippets Groups Projects
inode.c 52.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * nfs_revalidate_mapping - Revalidate the pagecache
     * @inode - pointer to host inode
     * @mapping - pointer to mapping
     */
    int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
    {
    	struct nfs_inode *nfsi = NFS_I(inode);
    
    	unsigned long *bitlock = &nfsi->flags;
    
    	/* swapfiles are not supposed to be shared. */
    	if (IS_SWAPFILE(inode))
    		goto out;
    
    
    	if (nfs_mapping_need_revalidate_inode(inode)) {
    
    		ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
    		if (ret < 0)
    			goto out;
    
    
    	/*
    	 * We must clear NFS_INO_INVALID_DATA first to ensure that
    	 * invalidations that come in while we're shooting down the mappings
    	 * are respected. But, that leaves a race window where one revalidator
    	 * can clear the flag, and then another checks it before the mapping
    	 * gets invalidated. Fix that by serializing access to this part of
    	 * the function.
    	 *
    	 * At the same time, we need to allow other tasks to see whether we
    	 * might be in the middle of invalidating the pages, so we only set
    	 * the bit lock here if it looks like we're going to be doing that.
    	 */
    	for (;;) {
    		ret = wait_on_bit(bitlock, NFS_INO_INVALIDATING,
    				  nfs_wait_bit_killable, TASK_KILLABLE);
    		if (ret)
    			goto out;
    		if (!(nfsi->cache_validity & NFS_INO_INVALID_DATA))
    			goto out;
    		if (!test_and_set_bit_lock(NFS_INO_INVALIDATING, bitlock))
    			break;
    	}
    
    	spin_lock(&inode->i_lock);
    
    	if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
    
    		nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
    		spin_unlock(&inode->i_lock);
    
    		trace_nfs_invalidate_mapping_enter(inode);
    
    		ret = nfs_invalidate_mapping(inode, mapping);
    
    		trace_nfs_invalidate_mapping_exit(inode, ret);
    
    	} else {
    		/* something raced in and cleared the flag */
    		spin_unlock(&inode->i_lock);
    
    	clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
    	smp_mb__after_clear_bit();
    	wake_up_bit(bitlock, NFS_INO_INVALIDATING);
    
    static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
    
    {
    	struct nfs_inode *nfsi = NFS_I(inode);
    
    	if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
    			&& (fattr->valid & NFS_ATTR_FATTR_CHANGE)
    
    			&& inode->i_version == fattr->pre_change_attr) {
    		inode->i_version = fattr->change_attr;
    
    		if (S_ISDIR(inode->i_mode))
    			nfsi->cache_validity |= NFS_INO_INVALID_DATA;
    
    		ret |= NFS_INO_INVALID_ATTR;
    
    	/* If we have atomic WCC data, we may update some attributes */
    
    	if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
    			&& (fattr->valid & NFS_ATTR_FATTR_CTIME)
    
    			&& timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
    		memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
    		ret |= NFS_INO_INVALID_ATTR;
    	}
    
    
    	if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
    			&& (fattr->valid & NFS_ATTR_FATTR_MTIME)
    			&& timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
    
    		memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
    		if (S_ISDIR(inode->i_mode))
    			nfsi->cache_validity |= NFS_INO_INVALID_DATA;
    		ret |= NFS_INO_INVALID_ATTR;
    
    	if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
    			&& (fattr->valid & NFS_ATTR_FATTR_SIZE)
    			&& i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
    
    			&& nfsi->npages == 0) {
    		i_size_write(inode, nfs_size_to_loff_t(fattr->size));
    		ret |= NFS_INO_INVALID_ATTR;
    	}
    
    
    	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
    		nfs_fscache_invalidate(inode);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
    
     * nfs_check_inode_attributes - verify consistency of the inode attribute cache
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * @inode - pointer to inode
     * @fattr - updated attributes
     *
     * Verifies the attribute cache. If we have just changed the attributes,
     * so that fattr carries weak cache consistency data, then it may
     * also update the ctime/mtime/change_attribute.
     */
    
    static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nfs_inode *nfsi = NFS_I(inode);
    	loff_t cur_size, new_isize;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (nfs_have_delegated_attributes(inode))
    		return 0;
    
    	/* Has the inode gone and changed behind our back? */
    
    	if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
    		return -EIO;
    	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
    
    	if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
    
    			inode->i_version != fattr->change_attr)
    
    		invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Verify a few of the more important attributes */
    
    	if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
    
    		invalid |= NFS_INO_INVALID_ATTR;
    
    	if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
    		cur_size = i_size_read(inode);
    		new_isize = nfs_size_to_loff_t(fattr->size);
    		if (cur_size != new_isize && nfsi->npages == 0)
    			invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Have any file permissions changed? */
    
    	if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
    		invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
    
    	if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
    
    		invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
    
    	if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
    
    		invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Has the link count changed? */
    
    	if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
    
    		invalid |= NFS_INO_INVALID_ATIME;
    
    	if (invalid != 0)
    		nfsi->cache_validity |= invalid;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	nfsi->read_cache_jiffies = fattr->time_start;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return 0;
    }
    
    
    static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
    
    	if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
    		return 0;
    
    	return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
    }
    
    static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
    {
    
    	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
    		return 0;
    
    	return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
    }
    
    static atomic_long_t nfs_attr_generation_counter;
    
    
    static unsigned long nfs_read_attr_generation_counter(void)
    {
    
    	return atomic_long_read(&nfs_attr_generation_counter);
    
    }
    
    unsigned long nfs_inc_attr_generation_counter(void)
    {
    
    	return atomic_long_inc_return(&nfs_attr_generation_counter);
    
    }
    
    void nfs_fattr_init(struct nfs_fattr *fattr)
    {
    	fattr->valid = 0;
    	fattr->time_start = jiffies;
    	fattr->gencount = nfs_inc_attr_generation_counter();
    
    	fattr->owner_name = NULL;
    	fattr->group_name = NULL;
    
    EXPORT_SYMBOL_GPL(nfs_fattr_init);
    
    struct nfs_fattr *nfs_alloc_fattr(void)
    {
    	struct nfs_fattr *fattr;
    
    	fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
    	if (fattr != NULL)
    		nfs_fattr_init(fattr);
    	return fattr;
    }
    
    EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
    
    
    struct nfs_fh *nfs_alloc_fhandle(void)
    {
    	struct nfs_fh *fh;
    
    	fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
    	if (fh != NULL)
    		fh->size = 0;
    	return fh;
    }
    
    EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
    
    /*
     * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
     *                             in the same way that wireshark does
     *
     * @fh: file handle
     *
     * For debugging only.
     */
    u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
    {
    	/* wireshark uses 32-bit AUTODIN crc and does a bitwise
    	 * not on the result */
    
    EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
    
     * _nfs_display_fhandle - display an NFS file handle on the console
     *
     * @fh: file handle to display
     * @caption: display caption
     *
     * For debugging only.
     */
    void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
    {
    	unsigned short i;
    
    
    	if (fh == NULL || fh->size == 0) {
    
    		printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
    		return;
    	}
    
    
    	printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
    	       caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
    
    	for (i = 0; i < fh->size; i += 16) {
    		__be32 *pos = (__be32 *)&fh->data[i];
    
    		switch ((fh->size - i - 1) >> 2) {
    		case 0:
    			printk(KERN_DEFAULT " %08x\n",
    				be32_to_cpup(pos));
    			break;
    		case 1:
    			printk(KERN_DEFAULT " %08x %08x\n",
    				be32_to_cpup(pos), be32_to_cpup(pos + 1));
    			break;
    		case 2:
    			printk(KERN_DEFAULT " %08x %08x %08x\n",
    				be32_to_cpup(pos), be32_to_cpup(pos + 1),
    				be32_to_cpup(pos + 2));
    			break;
    		default:
    			printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
    				be32_to_cpup(pos), be32_to_cpup(pos + 1),
    				be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
    		}
    	}
    }
    
    EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
    
    /**
     * nfs_inode_attrs_need_update - check if the inode attributes need updating
     * @inode - pointer to inode
     * @fattr - attributes
     *
     * Attempt to divine whether or not an RPC call reply carrying stale
     * attributes got scheduled after another call carrying updated ones.
     *
     * To do so, the function first assumes that a more recent ctime means
     * that the attributes in fattr are newer, however it also attempt to
     * catch the case where ctime either didn't change, or went backwards
     * (if someone reset the clock on the server) by looking at whether
     * or not this RPC call was started after the inode was last updated.
    
     * Note also the check for wraparound of 'attr_gencount'
    
     *
     * The function returns 'true' if it thinks the attributes in 'fattr' are
     * more recent than the ones cached in the inode.
     *
     */
    static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
    {
    	const struct nfs_inode *nfsi = NFS_I(inode);
    
    
    	return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
    
    		nfs_ctime_need_update(inode, fattr) ||
    		nfs_size_need_update(inode, fattr) ||
    
    		((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
    
    /*
     * Don't trust the change_attribute, mtime, ctime or size if
     * a pnfs LAYOUTCOMMIT is outstanding
     */
    static void nfs_inode_attrs_handle_layoutcommit(struct inode *inode,
    		struct nfs_fattr *fattr)
    {
    	if (pnfs_layoutcommit_outstanding(inode))
    		fattr->valid &= ~(NFS_ATTR_FATTR_CHANGE |
    				NFS_ATTR_FATTR_MTIME |
    				NFS_ATTR_FATTR_CTIME |
    				NFS_ATTR_FATTR_SIZE);
    }
    
    
    static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
    {
    
    	int ret;
    
    	trace_nfs_refresh_inode_enter(inode);
    
    
    	nfs_inode_attrs_handle_layoutcommit(inode, fattr);
    
    
    	if (nfs_inode_attrs_need_update(inode, fattr))
    
    		ret = nfs_update_inode(inode, fattr);
    	else
    		ret = nfs_check_inode_attributes(inode, fattr);
    
    	trace_nfs_refresh_inode_exit(inode, ret);
    	return ret;
    
    /**
     * nfs_refresh_inode - try to update the inode attribute cache
     * @inode - pointer to inode
     * @fattr - updated attributes
     *
     * Check that an RPC call that returned attributes has not overlapped with
     * other recent updates of the inode metadata, then decide whether it is
     * safe to do a full update of the inode attributes, or whether just to
     * call nfs_check_inode_attributes.
     */
    int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
    {
    	int status;
    
    	if ((fattr->valid & NFS_ATTR_FATTR) == 0)
    		return 0;
    	spin_lock(&inode->i_lock);
    
    	status = nfs_refresh_inode_locked(inode, fattr);
    
    	spin_unlock(&inode->i_lock);
    
    EXPORT_SYMBOL_GPL(nfs_refresh_inode);
    
    static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
    {
    	struct nfs_inode *nfsi = NFS_I(inode);
    
    	nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
    
    	if (S_ISDIR(inode->i_mode)) {
    
    		nfsi->cache_validity |= NFS_INO_INVALID_DATA;
    
    		nfs_fscache_invalidate(inode);
    	}
    
    	if ((fattr->valid & NFS_ATTR_FATTR) == 0)
    		return 0;
    	return nfs_refresh_inode_locked(inode, fattr);
    }
    
    
    /**
     * nfs_post_op_update_inode - try to update the inode attribute cache
     * @inode - pointer to inode
     * @fattr - updated attributes
     *
     * After an operation that has changed the inode metadata, mark the
     * attribute cache as being invalid, then try to update it.
    
     *
     * NB: if the server didn't return any post op attributes, this
     * function will force the retrieval of attributes before the next
     * NFS request.  Thus it should be used only for operations that
     * are expected to change one or more attributes, to avoid
     * unnecessary NFS requests and trips through nfs_update_inode().
    
     */
    int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
    {
    
    	status = nfs_post_op_update_inode_locked(inode, fattr);
    
    EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
    
    /**
     * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
     * @inode - pointer to inode
     * @fattr - updated attributes
     *
     * After an operation that has changed the inode metadata, mark the
     * attribute cache as being invalid, then try to update it. Fake up
     * weak cache consistency data, if none exist.
     *
     * This function is mainly designed to be used by the ->write_done() functions.
     */
    int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
    {
    
    	int status;
    
    	spin_lock(&inode->i_lock);
    	/* Don't do a WCC update if these attributes are already stale */
    	if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
    			!nfs_inode_attrs_need_update(inode, fattr)) {
    
    		fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
    				| NFS_ATTR_FATTR_PRESIZE
    				| NFS_ATTR_FATTR_PREMTIME
    				| NFS_ATTR_FATTR_PRECTIME);
    
    	if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
    			(fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
    
    		fattr->pre_change_attr = inode->i_version;
    
    		fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
    
    	if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
    			(fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
    
    		memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
    
    		fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
    	}
    	if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
    			(fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
    
    		memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
    
    		fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
    	}
    	if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
    			(fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
    
    		fattr->pre_size = i_size_read(inode);
    
    		fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
    
    out_noforce:
    	status = nfs_post_op_update_inode_locked(inode, fattr);
    	spin_unlock(&inode->i_lock);
    	return status;
    
    EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * Many nfs protocol calls return the new file attributes after
     * an operation.  Here we update the inode to reflect the state
     * of the server's inode.
     *
     * This is a bit tricky because we have to make sure all dirty pages
     * have been sent off to the server before calling invalidate_inode_pages.
     * To make sure no other process adds more write requests while we try
     * our best to flush them, we make them sleep during the attribute refresh.
     *
     * A very similar scenario holds for the dir cache.
     */
    
    static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct nfs_inode *nfsi = NFS_I(inode);
    
    	loff_t cur_isize, new_isize;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
    
    			__func__, inode->i_sb->s_id, inode->i_ino,
    
    			nfs_display_fhandle_hash(NFS_FH(inode)),
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			atomic_read(&inode->i_count), fattr->valid);
    
    
    	if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid) {
    		printk(KERN_ERR "NFS: server %s error: fileid changed\n"
    			"fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
    			NFS_SERVER(inode)->nfs_client->cl_hostname,
    			inode->i_sb->s_id, (long long)nfsi->fileid,
    			(long long)fattr->fileid);
    		goto out_err;
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/*
    	 * Make sure the inode's type hasn't changed.
    	 */
    
    	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
    		/*
    		* Big trouble! The inode has become a different object.
    		*/
    
    		printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
    
    				__func__, inode->i_ino, inode->i_mode, fattr->mode);
    		goto out_err;
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Update the fsid? */
    
    	if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
    
    			!nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/*
    	 * Update the read time so we don't revalidate too often.
    	 */
    
    	nfsi->read_cache_jiffies = fattr->time_start;
    
    	save_cache_validity = nfsi->cache_validity;
    	nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
    			| NFS_INO_INVALID_ATIME
    			| NFS_INO_REVAL_FORCED
    			| NFS_INO_REVAL_PAGECACHE);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Do atomic weak cache consistency updates */
    
    	invalid |= nfs_wcc_update_inode(inode, fattr);
    
    	if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
    
    		if (inode->i_version != fattr->change_attr) {
    
    			dprintk("NFS: change_attr change on server for file %s/%ld\n",
    					inode->i_sb->s_id, inode->i_ino);
    
    			invalid |= NFS_INO_INVALID_ATTR
    				| NFS_INO_INVALID_DATA
    				| NFS_INO_INVALID_ACCESS
    				| NFS_INO_INVALID_ACL
    				| NFS_INO_REVAL_PAGECACHE;
    
    			if (S_ISDIR(inode->i_mode))
    				nfs_force_lookup_revalidate(inode);
    
    			inode->i_version = fattr->change_attr;
    
    	} else if (server->caps & NFS_CAP_CHANGE_ATTR)
    		invalid |= save_cache_validity;
    
    
    	if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
    
    		memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
    
    	} else if (server->caps & NFS_CAP_MTIME)
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
    				| NFS_INO_REVAL_FORCED);
    
    
    	if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
    
    		memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
    
    	} else if (server->caps & NFS_CAP_CTIME)
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
    				| NFS_INO_REVAL_FORCED);
    
    	/* Check if our cached file size is stale */
    
    	if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
    		new_isize = nfs_size_to_loff_t(fattr->size);
    		cur_isize = i_size_read(inode);
    		if (new_isize != cur_isize) {
    			/* Do we perhaps have any outstanding writes, or has
    			 * the file grown beyond our last write? */
    
    			if ((nfsi->npages == 0) || new_isize > cur_isize) {
    
    				i_size_write(inode, new_isize);
    				invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
    			}
    
    			dprintk("NFS: isize change on server for file %s/%ld "
    					"(%Ld to %Ld)\n",
    					inode->i_sb->s_id,
    					inode->i_ino,
    					(long long)cur_isize,
    					(long long)new_isize);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    	} else
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
    				| NFS_INO_REVAL_PAGECACHE
    				| NFS_INO_REVAL_FORCED);
    
    	if (fattr->valid & NFS_ATTR_FATTR_ATIME)
    		memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
    
    	else if (server->caps & NFS_CAP_ATIME)
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
    				| NFS_INO_REVAL_FORCED);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (fattr->valid & NFS_ATTR_FATTR_MODE) {
    		if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
    
    			umode_t newmode = inode->i_mode & S_IFMT;
    			newmode |= fattr->mode & S_IALLUGO;
    			inode->i_mode = newmode;
    
    			invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
    		}
    
    	} else if (server->caps & NFS_CAP_MODE)
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
    				| NFS_INO_INVALID_ACCESS
    				| NFS_INO_INVALID_ACL
    				| NFS_INO_REVAL_FORCED);
    
    
    	if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
    
    		if (!uid_eq(inode->i_uid, fattr->uid)) {
    
    			invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
    			inode->i_uid = fattr->uid;
    		}
    
    	} else if (server->caps & NFS_CAP_OWNER)
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
    				| NFS_INO_INVALID_ACCESS
    				| NFS_INO_INVALID_ACL
    				| NFS_INO_REVAL_FORCED);
    
    
    	if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
    
    		if (!gid_eq(inode->i_gid, fattr->gid)) {
    
    			invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
    			inode->i_gid = fattr->gid;
    		}
    
    	} else if (server->caps & NFS_CAP_OWNER_GROUP)
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
    				| NFS_INO_INVALID_ACCESS
    				| NFS_INO_INVALID_ACL
    				| NFS_INO_REVAL_FORCED);
    
    	if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
    		if (inode->i_nlink != fattr->nlink) {
    			invalid |= NFS_INO_INVALID_ATTR;
    			if (S_ISDIR(inode->i_mode))
    				invalid |= NFS_INO_INVALID_DATA;
    
    			set_nlink(inode, fattr->nlink);
    
    	} else if (server->caps & NFS_CAP_NLINK)
    		invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
    				| NFS_INO_REVAL_FORCED);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		/*
    		 * report the blocks in 512byte units
    		 */
    		inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
     	}
    
    	if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
    		inode->i_blocks = fattr->du.nfs2.blocks;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Update attrtimeo value if we're out of the unstable period */
    
    	if (invalid & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL)) {
    
    		nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
    
    		nfsi->attrtimeo_timestamp = now;
    
    		nfsi->attr_gencount = nfs_inc_attr_generation_counter();
    
    		if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
    
    			if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
    				nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
    			nfsi->attrtimeo_timestamp = now;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	invalid &= ~NFS_INO_INVALID_LABEL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* Don't invalidate the data if we were to blame */
    	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
    				|| S_ISLNK(inode->i_mode)))
    		invalid &= ~NFS_INO_INVALID_DATA;
    
    	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) ||
    
    			(save_cache_validity & NFS_INO_REVAL_FORCED))
    
    		nfsi->cache_validity |= invalid;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (invalid & NFS_INO_INVALID_DATA)
    		nfs_fscache_invalidate(inode);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/*
    	 * No need to worry about unhashing the dentry, as the
    	 * lookup validation will know that the inode is bad.
    	 * (But we fall through to invalidate the caches.)
    	 */
    	nfs_invalidate_inode(inode);
    	return -ESTALE;
    }
    
    
    David Howells's avatar
    David Howells committed
    struct inode *nfs_alloc_inode(struct super_block *sb)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nfs_inode *nfsi;
    
    	nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!nfsi)
    		return NULL;
    
    	nfsi->flags = 0UL;
    	nfsi->cache_validity = 0UL;
    
    #ifdef CONFIG_NFS_V3_ACL
    	nfsi->acl_access = ERR_PTR(-EAGAIN);
    	nfsi->acl_default = ERR_PTR(-EAGAIN);
    #endif
    
    #if IS_ENABLED(CONFIG_NFS_V4)
    
    	nfsi->nfs4_acl = NULL;
    #endif /* CONFIG_NFS_V4 */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return &nfsi->vfs_inode;
    }
    
    EXPORT_SYMBOL_GPL(nfs_alloc_inode);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    static void nfs_i_callback(struct rcu_head *head)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct inode *inode = container_of(head, struct inode, i_rcu);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
    }
    
    
    void nfs_destroy_inode(struct inode *inode)
    {
    	call_rcu(&inode->i_rcu, nfs_i_callback);
    }
    
    EXPORT_SYMBOL_GPL(nfs_destroy_inode);
    
    Andrew Morton's avatar
    Andrew Morton committed
    static inline void nfs4_init_once(struct nfs_inode *nfsi)
    {
    
    #if IS_ENABLED(CONFIG_NFS_V4)
    
    Andrew Morton's avatar
    Andrew Morton committed
    	INIT_LIST_HEAD(&nfsi->open_states);
    	nfsi->delegation = NULL;
    	nfsi->delegation_state = 0;
    	init_rwsem(&nfsi->rwsem);
    
    	nfsi->layout = NULL;
    
    Andrew Morton's avatar
    Andrew Morton committed
    #endif
    }
    
    static void init_once(void *foo)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nfs_inode *nfsi = (struct nfs_inode *) foo;
    
    
    	inode_init_once(&nfsi->vfs_inode);
    	INIT_LIST_HEAD(&nfsi->open_files);
    	INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
    	INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
    
    	INIT_LIST_HEAD(&nfsi->commit_info.list);
    
    	nfsi->npages = 0;
    
    	nfsi->commit_info.ncommit = 0;
    
    	atomic_set(&nfsi->commit_info.rpcs_out, 0);
    
    	atomic_set(&nfsi->silly_count, 1);
    	INIT_HLIST_HEAD(&nfsi->silly_list);
    	init_waitqueue_head(&nfsi->waitqueue);
    
    	nfs4_init_once(nfsi);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    David Howells's avatar
    David Howells committed
    static int __init nfs_init_inodecache(void)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
    					     sizeof(struct nfs_inode),
    
    					     0, (SLAB_RECLAIM_ACCOUNT|
    						SLAB_MEM_SPREAD),
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (nfs_inode_cachep == NULL)
    		return -ENOMEM;
    
    	return 0;
    }
    
    
    static void nfs_destroy_inodecache(void)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	/*
    	 * Make sure all delayed rcu free inodes are flushed before we
    	 * destroy cache.
    	 */
    	rcu_barrier();
    
    	kmem_cache_destroy(nfs_inode_cachep);
    
    struct workqueue_struct *nfsiod_workqueue;
    
    EXPORT_SYMBOL_GPL(nfsiod_workqueue);
    
    
    /*
     * start up the nfsiod workqueue
     */
    static int nfsiod_start(void)
    {
    	struct workqueue_struct *wq;
    	dprintk("RPC:       creating workqueue nfsiod\n");
    
    	wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
    
    	if (wq == NULL)
    		return -ENOMEM;
    	nfsiod_workqueue = wq;
    	return 0;
    }
    
    /*
     * Destroy the nfsiod workqueue
     */
    static void nfsiod_stop(void)
    {
    	struct workqueue_struct *wq;
    
    	wq = nfsiod_workqueue;
    	if (wq == NULL)
    		return;
    	nfsiod_workqueue = NULL;
    	destroy_workqueue(wq);
    }
    
    
    	nfs_clients_init(net);
    
    	nfs_cleanup_cb_ident_idr(net);
    
    }
    
    static struct pernet_operations nfs_net_ops = {
    	.init = nfs_net_init,
    	.exit = nfs_net_exit,
    	.id   = &nfs_net_id,
    	.size = sizeof(struct nfs_net),
    };
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * Initialize NFS
     */
    static int __init init_nfs_fs(void)
    {
    	int err;
    
    
    	err = register_pernet_subsys(&nfs_net_ops);
    
    		goto out9;
    
    	err = nfs_fscache_register();
    	if (err < 0)
    
    		goto out8;
    
    	err = nfsiod_start();
    	if (err)
    
    		goto out7;
    
    	err = nfs_fs_proc_init();
    	if (err)
    
    		goto out6;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = nfs_init_nfspagecache();
    	if (err)
    
    		goto out5;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	err = nfs_init_inodecache();
    	if (err)
    
    		goto out4;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	err = nfs_init_readpagecache();
    	if (err)
    
    		goto out3;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	err = nfs_init_writepagecache();
    	if (err)
    
    		goto out2;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	err = nfs_init_directcache();
    	if (err)
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    #ifdef CONFIG_PROC_FS
    
    	rpc_proc_register(&init_net, &nfs_rpcstat);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    
    David Howells's avatar
    David Howells committed
    	if ((err = register_nfs_fs()) != 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #ifdef CONFIG_PROC_FS
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    	nfs_destroy_directcache();
    
    	nfs_destroy_writepagecache();
    
    	nfs_destroy_readpagecache();
    
    	nfs_destroy_inodecache();
    
    	nfs_destroy_nfspagecache();
    
    	nfs_fs_proc_exit();
    
    	nfsiod_stop();
    
    	nfs_fscache_unregister();
    
    	unregister_pernet_subsys(&nfs_net_ops);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    static void __exit exit_nfs_fs(void)
    {
    	nfs_destroy_directcache();
    	nfs_destroy_writepagecache();
    	nfs_destroy_readpagecache();
    	nfs_destroy_inodecache();
    	nfs_destroy_nfspagecache();
    
    	unregister_pernet_subsys(&nfs_net_ops);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #ifdef CONFIG_PROC_FS
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    
    David Howells's avatar
    David Howells committed
    	unregister_nfs_fs();
    
    	nfs_fs_proc_exit();
    
    	nfsiod_stop();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /* Not quite true; I just maintain it */
    MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
    MODULE_LICENSE("GPL");
    
    module_param(enable_ino64, bool, 0644);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    module_init(init_nfs_fs)
    module_exit(exit_nfs_fs)