xref: /openbmc/linux/fs/xfs/xfs_pnfs.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
252785112SChristoph Hellwig /*
352785112SChristoph Hellwig  * Copyright (c) 2014 Christoph Hellwig.
452785112SChristoph Hellwig  */
552785112SChristoph Hellwig #include "xfs.h"
65467b34bSDarrick J. Wong #include "xfs_shared.h"
752785112SChristoph Hellwig #include "xfs_format.h"
852785112SChristoph Hellwig #include "xfs_log_format.h"
952785112SChristoph Hellwig #include "xfs_trans_resv.h"
1052785112SChristoph Hellwig #include "xfs_mount.h"
1152785112SChristoph Hellwig #include "xfs_inode.h"
1252785112SChristoph Hellwig #include "xfs_trans.h"
1352785112SChristoph Hellwig #include "xfs_bmap.h"
1452785112SChristoph Hellwig #include "xfs_iomap.h"
151aa63006SBen Dooks (Codethink) #include "xfs_pnfs.h"
1652785112SChristoph Hellwig 
1752785112SChristoph Hellwig /*
18781355c6SChristoph Hellwig  * Ensure that we do not have any outstanding pNFS layouts that can be used by
19781355c6SChristoph Hellwig  * clients to directly read from or write to this inode.  This must be called
20781355c6SChristoph Hellwig  * before every operation that can remove blocks from the extent map.
21781355c6SChristoph Hellwig  * Additionally we call it during the write operation, where aren't concerned
22781355c6SChristoph Hellwig  * about exposing unallocated blocks but just want to provide basic
23781355c6SChristoph Hellwig  * synchronization between a local writer and pNFS clients.  mmap writes would
24781355c6SChristoph Hellwig  * also benefit from this sort of synchronization, but due to the tricky locking
25781355c6SChristoph Hellwig  * rules in the page fault path we don't bother.
26781355c6SChristoph Hellwig  */
27781355c6SChristoph Hellwig int
xfs_break_leased_layouts(struct inode * inode,uint * iolock,bool * did_unlock)2869eb5fa1SDan Williams xfs_break_leased_layouts(
29781355c6SChristoph Hellwig 	struct inode		*inode,
3069eb5fa1SDan Williams 	uint			*iolock,
3169eb5fa1SDan Williams 	bool			*did_unlock)
32781355c6SChristoph Hellwig {
33781355c6SChristoph Hellwig 	struct xfs_inode	*ip = XFS_I(inode);
34781355c6SChristoph Hellwig 	int			error;
35781355c6SChristoph Hellwig 
36b6827160SIra Weiny 	while ((error = break_layout(inode, false)) == -EWOULDBLOCK) {
37781355c6SChristoph Hellwig 		xfs_iunlock(ip, *iolock);
3869eb5fa1SDan Williams 		*did_unlock = true;
39781355c6SChristoph Hellwig 		error = break_layout(inode, true);
40c63a8eaeSDan Williams 		*iolock &= ~XFS_IOLOCK_SHARED;
41c63a8eaeSDan Williams 		*iolock |= XFS_IOLOCK_EXCL;
42781355c6SChristoph Hellwig 		xfs_ilock(ip, *iolock);
43781355c6SChristoph Hellwig 	}
44781355c6SChristoph Hellwig 
45781355c6SChristoph Hellwig 	return error;
46781355c6SChristoph Hellwig }
47781355c6SChristoph Hellwig 
48781355c6SChristoph Hellwig /*
4952785112SChristoph Hellwig  * Get a unique ID including its location so that the client can identify
5052785112SChristoph Hellwig  * the exported device.
5152785112SChristoph Hellwig  */
5252785112SChristoph Hellwig int
xfs_fs_get_uuid(struct super_block * sb,u8 * buf,u32 * len,u64 * offset)5352785112SChristoph Hellwig xfs_fs_get_uuid(
5452785112SChristoph Hellwig 	struct super_block	*sb,
5552785112SChristoph Hellwig 	u8			*buf,
5652785112SChristoph Hellwig 	u32			*len,
5752785112SChristoph Hellwig 	u64			*offset)
5852785112SChristoph Hellwig {
5952785112SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(sb);
6052785112SChristoph Hellwig 
61ec43f6daSEric Sandeen 	xfs_notice_once(mp,
62ec43f6daSEric Sandeen "Using experimental pNFS feature, use at your own risk!");
6352785112SChristoph Hellwig 
6452785112SChristoph Hellwig 	if (*len < sizeof(uuid_t))
6552785112SChristoph Hellwig 		return -EINVAL;
6652785112SChristoph Hellwig 
6752785112SChristoph Hellwig 	memcpy(buf, &mp->m_sb.sb_uuid, sizeof(uuid_t));
6852785112SChristoph Hellwig 	*len = sizeof(uuid_t);
6952785112SChristoph Hellwig 	*offset = offsetof(struct xfs_dsb, sb_uuid);
7052785112SChristoph Hellwig 	return 0;
7152785112SChristoph Hellwig }
7252785112SChristoph Hellwig 
7352785112SChristoph Hellwig /*
74b39a0463SDave Chinner  * We cannot use file based VFS helpers such as file_modified() to update
75b39a0463SDave Chinner  * inode state as we modify the data/metadata in the inode here. Hence we have
76b39a0463SDave Chinner  * to open code the timestamp updates and SUID/SGID stripping. We also need
77b39a0463SDave Chinner  * to set the inode prealloc flag to ensure that the extents we allocate are not
78b39a0463SDave Chinner  * removed if the inode is reclaimed from memory before xfs_fs_block_commit()
79b39a0463SDave Chinner  * is from the client to indicate that data has been written and the file size
80b39a0463SDave Chinner  * can be extended.
81b39a0463SDave Chinner  */
82b39a0463SDave Chinner static int
xfs_fs_map_update_inode(struct xfs_inode * ip)83b39a0463SDave Chinner xfs_fs_map_update_inode(
84b39a0463SDave Chinner 	struct xfs_inode	*ip)
85b39a0463SDave Chinner {
86b39a0463SDave Chinner 	struct xfs_trans	*tp;
87b39a0463SDave Chinner 	int			error;
88b39a0463SDave Chinner 
89b39a0463SDave Chinner 	error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
90b39a0463SDave Chinner 			0, 0, 0, &tp);
91b39a0463SDave Chinner 	if (error)
92b39a0463SDave Chinner 		return error;
93b39a0463SDave Chinner 
94b39a0463SDave Chinner 	xfs_ilock(ip, XFS_ILOCK_EXCL);
95b39a0463SDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
96b39a0463SDave Chinner 
97b39a0463SDave Chinner 	VFS_I(ip)->i_mode &= ~S_ISUID;
98b39a0463SDave Chinner 	if (VFS_I(ip)->i_mode & S_IXGRP)
99b39a0463SDave Chinner 		VFS_I(ip)->i_mode &= ~S_ISGID;
100b39a0463SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
101b39a0463SDave Chinner 	ip->i_diflags |= XFS_DIFLAG_PREALLOC;
102b39a0463SDave Chinner 
103b39a0463SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
104b39a0463SDave Chinner 	return xfs_trans_commit(tp);
105b39a0463SDave Chinner }
106b39a0463SDave Chinner 
107b39a0463SDave Chinner /*
10852785112SChristoph Hellwig  * Get a layout for the pNFS client.
10952785112SChristoph Hellwig  */
11052785112SChristoph Hellwig int
xfs_fs_map_blocks(struct inode * inode,loff_t offset,u64 length,struct iomap * iomap,bool write,u32 * device_generation)11152785112SChristoph Hellwig xfs_fs_map_blocks(
11252785112SChristoph Hellwig 	struct inode		*inode,
11352785112SChristoph Hellwig 	loff_t			offset,
11452785112SChristoph Hellwig 	u64			length,
11552785112SChristoph Hellwig 	struct iomap		*iomap,
11652785112SChristoph Hellwig 	bool			write,
11752785112SChristoph Hellwig 	u32			*device_generation)
11852785112SChristoph Hellwig {
11952785112SChristoph Hellwig 	struct xfs_inode	*ip = XFS_I(inode);
12052785112SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
12152785112SChristoph Hellwig 	struct xfs_bmbt_irec	imap;
12252785112SChristoph Hellwig 	xfs_fileoff_t		offset_fsb, end_fsb;
12352785112SChristoph Hellwig 	loff_t			limit;
12452785112SChristoph Hellwig 	int			bmapi_flags = XFS_BMAPI_ENTIRE;
12552785112SChristoph Hellwig 	int			nimaps = 1;
12652785112SChristoph Hellwig 	uint			lock_flags;
12752785112SChristoph Hellwig 	int			error = 0;
128304a68b9SDave Chinner 	u64			seq;
12952785112SChristoph Hellwig 
13075c8c50fSDave Chinner 	if (xfs_is_shutdown(mp))
13152785112SChristoph Hellwig 		return -EIO;
13252785112SChristoph Hellwig 
13352785112SChristoph Hellwig 	/*
13452785112SChristoph Hellwig 	 * We can't export inodes residing on the realtime device.  The realtime
13552785112SChristoph Hellwig 	 * device doesn't have a UUID to identify it, so the client has no way
13652785112SChristoph Hellwig 	 * to find it.
13752785112SChristoph Hellwig 	 */
13852785112SChristoph Hellwig 	if (XFS_IS_REALTIME_INODE(ip))
13952785112SChristoph Hellwig 		return -ENXIO;
14052785112SChristoph Hellwig 
14152785112SChristoph Hellwig 	/*
14246eeb521SDarrick J. Wong 	 * The pNFS block layout spec actually supports reflink like
14346eeb521SDarrick J. Wong 	 * functionality, but the Linux pNFS server doesn't implement it yet.
14446eeb521SDarrick J. Wong 	 */
14546eeb521SDarrick J. Wong 	if (xfs_is_reflink_inode(ip))
14646eeb521SDarrick J. Wong 		return -ENXIO;
14746eeb521SDarrick J. Wong 
14846eeb521SDarrick J. Wong 	/*
14952785112SChristoph Hellwig 	 * Lock out any other I/O before we flush and invalidate the pagecache,
15052785112SChristoph Hellwig 	 * and then hand out a layout to the remote system.  This is very
15152785112SChristoph Hellwig 	 * similar to direct I/O, except that the synchronization is much more
15269eb5fa1SDan Williams 	 * complicated.  See the comment near xfs_break_leased_layouts
15369eb5fa1SDan Williams 	 * for a detailed explanation.
15452785112SChristoph Hellwig 	 */
15552785112SChristoph Hellwig 	xfs_ilock(ip, XFS_IOLOCK_EXCL);
15652785112SChristoph Hellwig 
15752785112SChristoph Hellwig 	error = -EINVAL;
15852785112SChristoph Hellwig 	limit = mp->m_super->s_maxbytes;
15952785112SChristoph Hellwig 	if (!write)
16052785112SChristoph Hellwig 		limit = max(limit, round_up(i_size_read(inode),
16152785112SChristoph Hellwig 				     inode->i_sb->s_blocksize));
16252785112SChristoph Hellwig 	if (offset > limit)
16352785112SChristoph Hellwig 		goto out_unlock;
16452785112SChristoph Hellwig 	if (offset > limit - length)
16552785112SChristoph Hellwig 		length = limit - offset;
16652785112SChristoph Hellwig 
16752785112SChristoph Hellwig 	error = filemap_write_and_wait(inode->i_mapping);
16852785112SChristoph Hellwig 	if (error)
16952785112SChristoph Hellwig 		goto out_unlock;
17052785112SChristoph Hellwig 	error = invalidate_inode_pages2(inode->i_mapping);
17152785112SChristoph Hellwig 	if (WARN_ON_ONCE(error))
1722bd3fa79SChristoph Hellwig 		goto out_unlock;
17352785112SChristoph Hellwig 
17452785112SChristoph Hellwig 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
17552785112SChristoph Hellwig 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
17652785112SChristoph Hellwig 
17752785112SChristoph Hellwig 	lock_flags = xfs_ilock_data_map_shared(ip);
17852785112SChristoph Hellwig 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
17952785112SChristoph Hellwig 				&imap, &nimaps, bmapi_flags);
180304a68b9SDave Chinner 	seq = xfs_iomap_inode_sequence(ip, 0);
18152785112SChristoph Hellwig 
18288cdb714SChristoph Hellwig 	ASSERT(!nimaps || imap.br_startblock != DELAYSTARTBLOCK);
18388cdb714SChristoph Hellwig 
184e696663aSChristoph Hellwig 	if (!error && write &&
185e696663aSChristoph Hellwig 	    (!nimaps || imap.br_startblock == HOLESTARTBLOCK)) {
186e696663aSChristoph Hellwig 		if (offset + length > XFS_ISIZE(ip))
187e696663aSChristoph Hellwig 			end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
188e696663aSChristoph Hellwig 		else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
189e696663aSChristoph Hellwig 			end_fsb = min(end_fsb, imap.br_startoff +
190e696663aSChristoph Hellwig 					       imap.br_blockcount);
191e696663aSChristoph Hellwig 		xfs_iunlock(ip, lock_flags);
192e696663aSChristoph Hellwig 
193e696663aSChristoph Hellwig 		error = xfs_iomap_write_direct(ip, offset_fsb,
194304a68b9SDave Chinner 				end_fsb - offset_fsb, 0, &imap, &seq);
19552785112SChristoph Hellwig 		if (error)
19652785112SChristoph Hellwig 			goto out_unlock;
19752785112SChristoph Hellwig 
19852785112SChristoph Hellwig 		/*
199307cdb54SChristoph Hellwig 		 * Ensure the next transaction is committed synchronously so
200307cdb54SChristoph Hellwig 		 * that the blocks allocated and handed out to the client are
201307cdb54SChristoph Hellwig 		 * guaranteed to be present even after a server crash.
20252785112SChristoph Hellwig 		 */
203b39a0463SDave Chinner 		error = xfs_fs_map_update_inode(ip);
204472c6e46SDave Chinner 		if (!error)
205472c6e46SDave Chinner 			error = xfs_log_force_inode(ip);
20652785112SChristoph Hellwig 		if (error)
20752785112SChristoph Hellwig 			goto out_unlock;
208472c6e46SDave Chinner 
209e696663aSChristoph Hellwig 	} else {
210e696663aSChristoph Hellwig 		xfs_iunlock(ip, lock_flags);
21152785112SChristoph Hellwig 	}
21252785112SChristoph Hellwig 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
21352785112SChristoph Hellwig 
214304a68b9SDave Chinner 	error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0, 0, seq);
21552785112SChristoph Hellwig 	*device_generation = mp->m_generation;
21652785112SChristoph Hellwig 	return error;
21752785112SChristoph Hellwig out_unlock:
21852785112SChristoph Hellwig 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
21952785112SChristoph Hellwig 	return error;
22052785112SChristoph Hellwig }
22152785112SChristoph Hellwig 
22252785112SChristoph Hellwig /*
22352785112SChristoph Hellwig  * Ensure the size update falls into a valid allocated block.
22452785112SChristoph Hellwig  */
22552785112SChristoph Hellwig static int
xfs_pnfs_validate_isize(struct xfs_inode * ip,xfs_off_t isize)22652785112SChristoph Hellwig xfs_pnfs_validate_isize(
22752785112SChristoph Hellwig 	struct xfs_inode	*ip,
22852785112SChristoph Hellwig 	xfs_off_t		isize)
22952785112SChristoph Hellwig {
23052785112SChristoph Hellwig 	struct xfs_bmbt_irec	imap;
23152785112SChristoph Hellwig 	int			nimaps = 1;
23252785112SChristoph Hellwig 	int			error = 0;
23352785112SChristoph Hellwig 
23452785112SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_SHARED);
23552785112SChristoph Hellwig 	error = xfs_bmapi_read(ip, XFS_B_TO_FSBT(ip->i_mount, isize - 1), 1,
23652785112SChristoph Hellwig 				&imap, &nimaps, 0);
23752785112SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
23852785112SChristoph Hellwig 	if (error)
23952785112SChristoph Hellwig 		return error;
24052785112SChristoph Hellwig 
24152785112SChristoph Hellwig 	if (imap.br_startblock == HOLESTARTBLOCK ||
24252785112SChristoph Hellwig 	    imap.br_startblock == DELAYSTARTBLOCK ||
24352785112SChristoph Hellwig 	    imap.br_state == XFS_EXT_UNWRITTEN)
24452785112SChristoph Hellwig 		return -EIO;
24552785112SChristoph Hellwig 	return 0;
24652785112SChristoph Hellwig }
24752785112SChristoph Hellwig 
24852785112SChristoph Hellwig /*
24952785112SChristoph Hellwig  * Make sure the blocks described by maps are stable on disk.  This includes
25052785112SChristoph Hellwig  * converting any unwritten extents, flushing the disk cache and updating the
25152785112SChristoph Hellwig  * time stamps.
25252785112SChristoph Hellwig  *
25352785112SChristoph Hellwig  * Note that we rely on the caller to always send us a timestamp update so that
25452785112SChristoph Hellwig  * we always commit a transaction here.  If that stops being true we will have
25552785112SChristoph Hellwig  * to manually flush the cache here similar to what the fsync code path does
25652785112SChristoph Hellwig  * for datasyncs on files that have no dirty metadata.
25752785112SChristoph Hellwig  */
25852785112SChristoph Hellwig int
xfs_fs_commit_blocks(struct inode * inode,struct iomap * maps,int nr_maps,struct iattr * iattr)25952785112SChristoph Hellwig xfs_fs_commit_blocks(
26052785112SChristoph Hellwig 	struct inode		*inode,
26152785112SChristoph Hellwig 	struct iomap		*maps,
26252785112SChristoph Hellwig 	int			nr_maps,
26352785112SChristoph Hellwig 	struct iattr		*iattr)
26452785112SChristoph Hellwig {
26552785112SChristoph Hellwig 	struct xfs_inode	*ip = XFS_I(inode);
26652785112SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
26752785112SChristoph Hellwig 	struct xfs_trans	*tp;
26852785112SChristoph Hellwig 	bool			update_isize = false;
26952785112SChristoph Hellwig 	int			error, i;
27052785112SChristoph Hellwig 	loff_t			size;
27152785112SChristoph Hellwig 
27252785112SChristoph Hellwig 	ASSERT(iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME));
27352785112SChristoph Hellwig 
27452785112SChristoph Hellwig 	xfs_ilock(ip, XFS_IOLOCK_EXCL);
27552785112SChristoph Hellwig 
27652785112SChristoph Hellwig 	size = i_size_read(inode);
27752785112SChristoph Hellwig 	if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size > size) {
27852785112SChristoph Hellwig 		update_isize = true;
27952785112SChristoph Hellwig 		size = iattr->ia_size;
28052785112SChristoph Hellwig 	}
28152785112SChristoph Hellwig 
28252785112SChristoph Hellwig 	for (i = 0; i < nr_maps; i++) {
28352785112SChristoph Hellwig 		u64 start, length, end;
28452785112SChristoph Hellwig 
28552785112SChristoph Hellwig 		start = maps[i].offset;
28652785112SChristoph Hellwig 		if (start > size)
28752785112SChristoph Hellwig 			continue;
28852785112SChristoph Hellwig 
28952785112SChristoph Hellwig 		end = start + maps[i].length;
29052785112SChristoph Hellwig 		if (end > size)
29152785112SChristoph Hellwig 			end = size;
29252785112SChristoph Hellwig 
29352785112SChristoph Hellwig 		length = end - start;
29452785112SChristoph Hellwig 		if (!length)
29552785112SChristoph Hellwig 			continue;
29652785112SChristoph Hellwig 
29752785112SChristoph Hellwig 		/*
29852785112SChristoph Hellwig 		 * Make sure reads through the pagecache see the new data.
29952785112SChristoph Hellwig 		 */
30052785112SChristoph Hellwig 		error = invalidate_inode_pages2_range(inode->i_mapping,
30109cbfeafSKirill A. Shutemov 					start >> PAGE_SHIFT,
30209cbfeafSKirill A. Shutemov 					(end - 1) >> PAGE_SHIFT);
30352785112SChristoph Hellwig 		WARN_ON_ONCE(error);
30452785112SChristoph Hellwig 
305ee70daabSEryu Guan 		error = xfs_iomap_write_unwritten(ip, start, length, false);
30652785112SChristoph Hellwig 		if (error)
30752785112SChristoph Hellwig 			goto out_drop_iolock;
30852785112SChristoph Hellwig 	}
30952785112SChristoph Hellwig 
31052785112SChristoph Hellwig 	if (update_isize) {
31152785112SChristoph Hellwig 		error = xfs_pnfs_validate_isize(ip, size);
31252785112SChristoph Hellwig 		if (error)
31352785112SChristoph Hellwig 			goto out_drop_iolock;
31452785112SChristoph Hellwig 	}
31552785112SChristoph Hellwig 
316253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
317253f4911SChristoph Hellwig 	if (error)
31852785112SChristoph Hellwig 		goto out_drop_iolock;
31952785112SChristoph Hellwig 
32052785112SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
32152785112SChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
32252785112SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
32352785112SChristoph Hellwig 
324e014f37dSDarrick J. Wong 	ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
325*c1632a0fSChristian Brauner 	setattr_copy(&nop_mnt_idmap, inode, iattr);
32652785112SChristoph Hellwig 	if (update_isize) {
32752785112SChristoph Hellwig 		i_size_write(inode, iattr->ia_size);
32813d2c10bSChristoph Hellwig 		ip->i_disk_size = iattr->ia_size;
32952785112SChristoph Hellwig 	}
33052785112SChristoph Hellwig 
33152785112SChristoph Hellwig 	xfs_trans_set_sync(tp);
33270393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
33352785112SChristoph Hellwig 
33452785112SChristoph Hellwig out_drop_iolock:
33552785112SChristoph Hellwig 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
33652785112SChristoph Hellwig 	return error;
33752785112SChristoph Hellwig }
338