⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xfs_lrw.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	/*	 * First handle zeroing the block on which isize resides.	 * We only zero a part of that block so it is handled specially.	 */	error = xfs_zero_last_block(ip, io, offset, isize);	if (error) {		ASSERT(ismrlocked(io->io_lock, MR_UPDATE));		ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));		return error;	}	/*	 * Calculate the range between the new size and the old	 * where blocks needing to be zeroed may exist.  To get the	 * block where the last byte in the file currently resides,	 * we need to subtract one from the size and truncate back	 * to a block boundary.  We subtract 1 in case the size is	 * exactly on a block boundary.	 */	last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;	start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);	end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);	ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);	if (last_fsb == end_zero_fsb) {		/*		 * The size was only incremented on its last block.		 * We took care of that above, so just return.		 */		return 0;	}	ASSERT(start_zero_fsb <= end_zero_fsb);	while (start_zero_fsb <= end_zero_fsb) {		nimaps = 1;		zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;		error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,				  0, NULL, 0, &imap, &nimaps, NULL, NULL);		if (error) {			ASSERT(ismrlocked(io->io_lock, MR_UPDATE));			ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));			return error;		}		ASSERT(nimaps > 0);		if (imap.br_state == XFS_EXT_UNWRITTEN ||		    imap.br_startblock == HOLESTARTBLOCK) {			/*			 * This loop handles initializing pages that were			 * partially initialized by the code below this			 * loop. It basically zeroes the part of the page			 * that sits on a hole and sets the page as P_HOLE			 * and calls remapf if it is a mapped file.			 */			start_zero_fsb = imap.br_startoff + imap.br_blockcount;			ASSERT(start_zero_fsb <= (end_zero_fsb + 1));			continue;		}		/*		 * There are blocks we need to zero.		 * Drop the inode lock while we're doing the I/O.		 * We'll still have the iolock to protect us.		 */		XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);		zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);		zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);		if ((zero_off + zero_len) > offset)			zero_len = offset - zero_off;		error = xfs_iozero(ip, zero_off, zero_len);		if (error) {			goto out_lock;		}		start_zero_fsb = imap.br_startoff + imap.br_blockcount;		ASSERT(start_zero_fsb <= (end_zero_fsb + 1));		XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);	}	return 0;out_lock:	XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);	ASSERT(error >= 0);	return error;}ssize_t				/* bytes written, or (-) error */xfs_write(	struct xfs_inode	*xip,	struct kiocb		*iocb,	const struct iovec	*iovp,	unsigned int		nsegs,	loff_t			*offset,	int			ioflags){	struct file		*file = iocb->ki_filp;	struct address_space	*mapping = file->f_mapping;	struct inode		*inode = mapping->host;	bhv_vnode_t		*vp = XFS_ITOV(xip);	unsigned long		segs = nsegs;	xfs_mount_t		*mp;	ssize_t			ret = 0, error = 0;	xfs_fsize_t		isize, new_size;	xfs_iocore_t		*io;	int			iolock;	int			eventsent = 0;	bhv_vrwlock_t		locktype;	size_t			ocount = 0, count;	loff_t			pos;	int			need_i_mutex;	XFS_STATS_INC(xs_write_calls);	error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);	if (error)		return error;	count = ocount;	pos = *offset;	if (count == 0)		return 0;	io = &xip->i_iocore;	mp = io->io_mount;	xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);	if (XFS_FORCED_SHUTDOWN(mp))		return -EIO;relock:	if (ioflags & IO_ISDIRECT) {		iolock = XFS_IOLOCK_SHARED;		locktype = VRWLOCK_WRITE_DIRECT;		need_i_mutex = 0;	} else {		iolock = XFS_IOLOCK_EXCL;		locktype = VRWLOCK_WRITE;		need_i_mutex = 1;		mutex_lock(&inode->i_mutex);	}	xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);start:	error = -generic_write_checks(file, &pos, &count,					S_ISBLK(inode->i_mode));	if (error) {		xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);		goto out_unlock_mutex;	}	if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&	    !(ioflags & IO_INVIS) && !eventsent)) {		int		dmflags = FILP_DELAY_FLAG(file);		if (need_i_mutex)			dmflags |= DM_FLAGS_IMUX;		xfs_iunlock(xip, XFS_ILOCK_EXCL);		error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,				      pos, count,				      dmflags, &locktype);		if (error) {			goto out_unlock_internal;		}		xfs_ilock(xip, XFS_ILOCK_EXCL);		eventsent = 1;		/*		 * The iolock was dropped and reacquired in XFS_SEND_DATA		 * so we have to recheck the size when appending.		 * We will only "goto start;" once, since having sent the		 * event prevents another call to XFS_SEND_DATA, which is		 * what allows the size to change in the first place.		 */		if ((file->f_flags & O_APPEND) && pos != xip->i_size)			goto start;	}	if (ioflags & IO_ISDIRECT) {		xfs_buftarg_t	*target =			(xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?				mp->m_rtdev_targp : mp->m_ddev_targp;		if ((pos & target->bt_smask) || (count & target->bt_smask)) {			xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);			return XFS_ERROR(-EINVAL);		}		if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {			xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);			iolock = XFS_IOLOCK_EXCL;			locktype = VRWLOCK_WRITE;			need_i_mutex = 1;			mutex_lock(&inode->i_mutex);			xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);			goto start;		}	}	new_size = pos + count;	if (new_size > xip->i_size)		io->io_new_size = new_size;	if (likely(!(ioflags & IO_INVIS))) {		file_update_time(file);		xfs_ichgtime_fast(xip, inode,				  XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);	}	/*	 * If the offset is beyond the size of the file, we have a couple	 * of things to do. First, if there is already space allocated	 * we need to either create holes or zero the disk or ...	 *	 * If there is a page where the previous size lands, we need	 * to zero it out up to the new size.	 */	if (pos > xip->i_size) {		error = xfs_zero_eof(vp, io, pos, xip->i_size);		if (error) {			xfs_iunlock(xip, XFS_ILOCK_EXCL);			goto out_unlock_internal;		}	}	xfs_iunlock(xip, XFS_ILOCK_EXCL);	/*	 * If we're writing the file then make sure to clear the	 * setuid and setgid bits if the process is not being run	 * by root.  This keeps people from modifying setuid and	 * setgid binaries.	 */	if (((xip->i_d.di_mode & S_ISUID) ||	    ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==		(S_ISGID | S_IXGRP))) &&	     !capable(CAP_FSETID)) {		error = xfs_write_clear_setuid(xip);		if (likely(!error))			error = -remove_suid(file->f_path.dentry);		if (unlikely(error)) {			goto out_unlock_internal;		}	}retry:	/* We can write back this queue in page reclaim */	current->backing_dev_info = mapping->backing_dev_info;	if ((ioflags & IO_ISDIRECT)) {		if (VN_CACHED(vp)) {			WARN_ON(need_i_mutex == 0);			xfs_inval_cached_trace(io, pos, -1,					ctooff(offtoct(pos)), -1);			error = xfs_flushinval_pages(xip,					ctooff(offtoct(pos)),					-1, FI_REMAPF_LOCKED);			if (error)				goto out_unlock_internal;		}		if (need_i_mutex) {			/* demote the lock now the cached pages are gone */			XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);			mutex_unlock(&inode->i_mutex);			iolock = XFS_IOLOCK_SHARED;			locktype = VRWLOCK_WRITE_DIRECT;			need_i_mutex = 0;		} 		xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,				*offset, ioflags);		ret = generic_file_direct_write(iocb, iovp,				&segs, pos, offset, count, ocount);		/*		 * direct-io write to a hole: fall through to buffered I/O		 * for completing the rest of the request.		 */		if (ret >= 0 && ret != count) {			XFS_STATS_ADD(xs_write_bytes, ret);			pos += ret;			count -= ret;			ioflags &= ~IO_ISDIRECT;			xfs_iunlock(xip, iolock);			goto relock;		}	} else {		xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,				*offset, ioflags);		ret = generic_file_buffered_write(iocb, iovp, segs,				pos, offset, count, ret);	}	current->backing_dev_info = NULL;	if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))		ret = wait_on_sync_kiocb(iocb);	if (ret == -ENOSPC &&	    DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {		xfs_rwunlock(xip, locktype);		if (need_i_mutex)			mutex_unlock(&inode->i_mutex);		error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,				DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,				0, 0, 0); /* Delay flag intentionally  unused */		if (need_i_mutex)			mutex_lock(&inode->i_mutex);		xfs_rwlock(xip, locktype);		if (error)			goto out_unlock_internal;		pos = xip->i_size;		ret = 0;		goto retry;	}	isize = i_size_read(inode);	if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))		*offset = isize;	if (*offset > xip->i_size) {		xfs_ilock(xip, XFS_ILOCK_EXCL);		if (*offset > xip->i_size)			xip->i_size = *offset;		xfs_iunlock(xip, XFS_ILOCK_EXCL);	}	error = -ret;	if (ret <= 0)		goto out_unlock_internal;	XFS_STATS_ADD(xs_write_bytes, ret);	/* Handle various SYNC-type writes */	if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {		int error2;		xfs_rwunlock(xip, locktype);		if (need_i_mutex)			mutex_unlock(&inode->i_mutex);		error2 = sync_page_range(inode, mapping, pos, ret);		if (!error)			error = error2;		if (need_i_mutex)			mutex_lock(&inode->i_mutex);		xfs_rwlock(xip, locktype);		error2 = xfs_write_sync_logforce(mp, xip);		if (!error)			error = error2;	} out_unlock_internal:	if (io->io_new_size) {		xfs_ilock(xip, XFS_ILOCK_EXCL);		io->io_new_size = 0;		/*		 * If this was a direct or synchronous I/O that failed (such		 * as ENOSPC) then part of the I/O may have been written to		 * disk before the error occured.  In this case the on-disk		 * file size may have been adjusted beyond the in-memory file		 * size and now needs to be truncated back.		 */		if (xip->i_d.di_size > xip->i_size)			xip->i_d.di_size = xip->i_size;		xfs_iunlock(xip, XFS_ILOCK_EXCL);	}	xfs_rwunlock(xip, locktype); out_unlock_mutex:	if (need_i_mutex)		mutex_unlock(&inode->i_mutex);	return -error;}/* * All xfs metadata buffers except log state machine buffers * get this attached as their b_bdstrat callback function. * This is so that we can catch a buffer * after prematurely unpinning it to forcibly shutdown the filesystem. */intxfs_bdstrat_cb(struct xfs_buf *bp){	xfs_mount_t	*mp;	mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);	if (!XFS_FORCED_SHUTDOWN(mp)) {		xfs_buf_iorequest(bp);		return 0;	} else {		xfs_buftrace("XFS__BDSTRAT IOERROR", bp);		/*		 * Metadata write that didn't get logged but		 * written delayed anyway. These aren't associated		 * with a transaction, and can be ignored.		 */		if (XFS_BUF_IODONE_FUNC(bp) == NULL &&		    (XFS_BUF_ISREAD(bp)) == 0)			return (xfs_bioerror_relse(bp));		else			return (xfs_bioerror(bp));	}}intxfs_bmap(	xfs_inode_t	*ip,	xfs_off_t	offset,	ssize_t		count,	int		flags,	xfs_iomap_t	*iomapp,	int		*niomaps){	xfs_iocore_t	*io = &ip->i_iocore;	ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);	ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==	       ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));	return xfs_iomap(io, offset, count, flags, iomapp, niomaps);}/* * Wrapper around bdstrat so that we can stop data * from going to disk in case we are shutting down the filesystem. * Typically user data goes thru this path; one of the exceptions * is the superblock. */intxfsbdstrat(	struct xfs_mount	*mp,	struct xfs_buf		*bp){	ASSERT(mp);	if (!XFS_FORCED_SHUTDOWN(mp)) {		/* Grio redirection would go here		 * if (XFS_BUF_IS_GRIO(bp)) {		 */		xfs_buf_iorequest(bp);		return 0;	}	xfs_buftrace("XFSBDSTRAT IOERROR", bp);	return (xfs_bioerror_relse(bp));}/* * If the underlying (data/log/rt) device is readonly, there are some * operations that cannot proceed. */intxfs_dev_is_read_only(	xfs_mount_t		*mp,	char			*message){	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||	    xfs_readonly_buftarg(mp->m_logdev_targp) ||	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {		cmn_err(CE_NOTE,			"XFS: %s required on read-only device.", message);		cmn_err(CE_NOTE,			"XFS: write access unavailable, cannot proceed.");		return EROFS;	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -