xref: /OK3568_Linux_fs/kernel/fs/xfs/xfs_pnfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2014 Christoph Hellwig.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #include "xfs.h"
6*4882a593Smuzhiyun #include "xfs_shared.h"
7*4882a593Smuzhiyun #include "xfs_format.h"
8*4882a593Smuzhiyun #include "xfs_log_format.h"
9*4882a593Smuzhiyun #include "xfs_trans_resv.h"
10*4882a593Smuzhiyun #include "xfs_mount.h"
11*4882a593Smuzhiyun #include "xfs_inode.h"
12*4882a593Smuzhiyun #include "xfs_trans.h"
13*4882a593Smuzhiyun #include "xfs_bmap.h"
14*4882a593Smuzhiyun #include "xfs_iomap.h"
15*4882a593Smuzhiyun #include "xfs_pnfs.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun  * Ensure that we do not have any outstanding pNFS layouts that can be used by
19*4882a593Smuzhiyun  * clients to directly read from or write to this inode.  This must be called
20*4882a593Smuzhiyun  * before every operation that can remove blocks from the extent map.
21*4882a593Smuzhiyun  * Additionally we call it during the write operation, where aren't concerned
22*4882a593Smuzhiyun  * about exposing unallocated blocks but just want to provide basic
23*4882a593Smuzhiyun  * synchronization between a local writer and pNFS clients.  mmap writes would
24*4882a593Smuzhiyun  * also benefit from this sort of synchronization, but due to the tricky locking
25*4882a593Smuzhiyun  * rules in the page fault path we don't bother.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun int
xfs_break_leased_layouts(struct inode * inode,uint * iolock,bool * did_unlock)28*4882a593Smuzhiyun xfs_break_leased_layouts(
29*4882a593Smuzhiyun 	struct inode		*inode,
30*4882a593Smuzhiyun 	uint			*iolock,
31*4882a593Smuzhiyun 	bool			*did_unlock)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	struct xfs_inode	*ip = XFS_I(inode);
34*4882a593Smuzhiyun 	int			error;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	while ((error = break_layout(inode, false)) == -EWOULDBLOCK) {
37*4882a593Smuzhiyun 		xfs_iunlock(ip, *iolock);
38*4882a593Smuzhiyun 		*did_unlock = true;
39*4882a593Smuzhiyun 		error = break_layout(inode, true);
40*4882a593Smuzhiyun 		*iolock &= ~XFS_IOLOCK_SHARED;
41*4882a593Smuzhiyun 		*iolock |= XFS_IOLOCK_EXCL;
42*4882a593Smuzhiyun 		xfs_ilock(ip, *iolock);
43*4882a593Smuzhiyun 	}
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	return error;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun  * Get a unique ID including its location so that the client can identify
50*4882a593Smuzhiyun  * the exported device.
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun int
xfs_fs_get_uuid(struct super_block * sb,u8 * buf,u32 * len,u64 * offset)53*4882a593Smuzhiyun xfs_fs_get_uuid(
54*4882a593Smuzhiyun 	struct super_block	*sb,
55*4882a593Smuzhiyun 	u8			*buf,
56*4882a593Smuzhiyun 	u32			*len,
57*4882a593Smuzhiyun 	u64			*offset)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	struct xfs_mount	*mp = XFS_M(sb);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	xfs_notice_once(mp,
62*4882a593Smuzhiyun "Using experimental pNFS feature, use at your own risk!");
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	if (*len < sizeof(uuid_t))
65*4882a593Smuzhiyun 		return -EINVAL;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	memcpy(buf, &mp->m_sb.sb_uuid, sizeof(uuid_t));
68*4882a593Smuzhiyun 	*len = sizeof(uuid_t);
69*4882a593Smuzhiyun 	*offset = offsetof(struct xfs_dsb, sb_uuid);
70*4882a593Smuzhiyun 	return 0;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * Get a layout for the pNFS client.
75*4882a593Smuzhiyun  */
76*4882a593Smuzhiyun int
xfs_fs_map_blocks(struct inode * inode,loff_t offset,u64 length,struct iomap * iomap,bool write,u32 * device_generation)77*4882a593Smuzhiyun xfs_fs_map_blocks(
78*4882a593Smuzhiyun 	struct inode		*inode,
79*4882a593Smuzhiyun 	loff_t			offset,
80*4882a593Smuzhiyun 	u64			length,
81*4882a593Smuzhiyun 	struct iomap		*iomap,
82*4882a593Smuzhiyun 	bool			write,
83*4882a593Smuzhiyun 	u32			*device_generation)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	struct xfs_inode	*ip = XFS_I(inode);
86*4882a593Smuzhiyun 	struct xfs_mount	*mp = ip->i_mount;
87*4882a593Smuzhiyun 	struct xfs_bmbt_irec	imap;
88*4882a593Smuzhiyun 	xfs_fileoff_t		offset_fsb, end_fsb;
89*4882a593Smuzhiyun 	loff_t			limit;
90*4882a593Smuzhiyun 	int			bmapi_flags = XFS_BMAPI_ENTIRE;
91*4882a593Smuzhiyun 	int			nimaps = 1;
92*4882a593Smuzhiyun 	uint			lock_flags;
93*4882a593Smuzhiyun 	int			error = 0;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (XFS_FORCED_SHUTDOWN(mp))
96*4882a593Smuzhiyun 		return -EIO;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/*
99*4882a593Smuzhiyun 	 * We can't export inodes residing on the realtime device.  The realtime
100*4882a593Smuzhiyun 	 * device doesn't have a UUID to identify it, so the client has no way
101*4882a593Smuzhiyun 	 * to find it.
102*4882a593Smuzhiyun 	 */
103*4882a593Smuzhiyun 	if (XFS_IS_REALTIME_INODE(ip))
104*4882a593Smuzhiyun 		return -ENXIO;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/*
107*4882a593Smuzhiyun 	 * The pNFS block layout spec actually supports reflink like
108*4882a593Smuzhiyun 	 * functionality, but the Linux pNFS server doesn't implement it yet.
109*4882a593Smuzhiyun 	 */
110*4882a593Smuzhiyun 	if (xfs_is_reflink_inode(ip))
111*4882a593Smuzhiyun 		return -ENXIO;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/*
114*4882a593Smuzhiyun 	 * Lock out any other I/O before we flush and invalidate the pagecache,
115*4882a593Smuzhiyun 	 * and then hand out a layout to the remote system.  This is very
116*4882a593Smuzhiyun 	 * similar to direct I/O, except that the synchronization is much more
117*4882a593Smuzhiyun 	 * complicated.  See the comment near xfs_break_leased_layouts
118*4882a593Smuzhiyun 	 * for a detailed explanation.
119*4882a593Smuzhiyun 	 */
120*4882a593Smuzhiyun 	xfs_ilock(ip, XFS_IOLOCK_EXCL);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	error = -EINVAL;
123*4882a593Smuzhiyun 	limit = mp->m_super->s_maxbytes;
124*4882a593Smuzhiyun 	if (!write)
125*4882a593Smuzhiyun 		limit = max(limit, round_up(i_size_read(inode),
126*4882a593Smuzhiyun 				     inode->i_sb->s_blocksize));
127*4882a593Smuzhiyun 	if (offset > limit)
128*4882a593Smuzhiyun 		goto out_unlock;
129*4882a593Smuzhiyun 	if (offset > limit - length)
130*4882a593Smuzhiyun 		length = limit - offset;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	error = filemap_write_and_wait(inode->i_mapping);
133*4882a593Smuzhiyun 	if (error)
134*4882a593Smuzhiyun 		goto out_unlock;
135*4882a593Smuzhiyun 	error = invalidate_inode_pages2(inode->i_mapping);
136*4882a593Smuzhiyun 	if (WARN_ON_ONCE(error))
137*4882a593Smuzhiyun 		goto out_unlock;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
140*4882a593Smuzhiyun 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	lock_flags = xfs_ilock_data_map_shared(ip);
143*4882a593Smuzhiyun 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
144*4882a593Smuzhiyun 				&imap, &nimaps, bmapi_flags);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	ASSERT(!nimaps || imap.br_startblock != DELAYSTARTBLOCK);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	if (!error && write &&
149*4882a593Smuzhiyun 	    (!nimaps || imap.br_startblock == HOLESTARTBLOCK)) {
150*4882a593Smuzhiyun 		if (offset + length > XFS_ISIZE(ip))
151*4882a593Smuzhiyun 			end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
152*4882a593Smuzhiyun 		else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
153*4882a593Smuzhiyun 			end_fsb = min(end_fsb, imap.br_startoff +
154*4882a593Smuzhiyun 					       imap.br_blockcount);
155*4882a593Smuzhiyun 		xfs_iunlock(ip, lock_flags);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		error = xfs_iomap_write_direct(ip, offset_fsb,
158*4882a593Smuzhiyun 				end_fsb - offset_fsb, &imap);
159*4882a593Smuzhiyun 		if (error)
160*4882a593Smuzhiyun 			goto out_unlock;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 		/*
163*4882a593Smuzhiyun 		 * Ensure the next transaction is committed synchronously so
164*4882a593Smuzhiyun 		 * that the blocks allocated and handed out to the client are
165*4882a593Smuzhiyun 		 * guaranteed to be present even after a server crash.
166*4882a593Smuzhiyun 		 */
167*4882a593Smuzhiyun 		error = xfs_update_prealloc_flags(ip,
168*4882a593Smuzhiyun 				XFS_PREALLOC_SET | XFS_PREALLOC_SYNC);
169*4882a593Smuzhiyun 		if (error)
170*4882a593Smuzhiyun 			goto out_unlock;
171*4882a593Smuzhiyun 	} else {
172*4882a593Smuzhiyun 		xfs_iunlock(ip, lock_flags);
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
177*4882a593Smuzhiyun 	*device_generation = mp->m_generation;
178*4882a593Smuzhiyun 	return error;
179*4882a593Smuzhiyun out_unlock:
180*4882a593Smuzhiyun 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
181*4882a593Smuzhiyun 	return error;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun  * Ensure the size update falls into a valid allocated block.
186*4882a593Smuzhiyun  */
187*4882a593Smuzhiyun static int
xfs_pnfs_validate_isize(struct xfs_inode * ip,xfs_off_t isize)188*4882a593Smuzhiyun xfs_pnfs_validate_isize(
189*4882a593Smuzhiyun 	struct xfs_inode	*ip,
190*4882a593Smuzhiyun 	xfs_off_t		isize)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct xfs_bmbt_irec	imap;
193*4882a593Smuzhiyun 	int			nimaps = 1;
194*4882a593Smuzhiyun 	int			error = 0;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	xfs_ilock(ip, XFS_ILOCK_SHARED);
197*4882a593Smuzhiyun 	error = xfs_bmapi_read(ip, XFS_B_TO_FSBT(ip->i_mount, isize - 1), 1,
198*4882a593Smuzhiyun 				&imap, &nimaps, 0);
199*4882a593Smuzhiyun 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
200*4882a593Smuzhiyun 	if (error)
201*4882a593Smuzhiyun 		return error;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	if (imap.br_startblock == HOLESTARTBLOCK ||
204*4882a593Smuzhiyun 	    imap.br_startblock == DELAYSTARTBLOCK ||
205*4882a593Smuzhiyun 	    imap.br_state == XFS_EXT_UNWRITTEN)
206*4882a593Smuzhiyun 		return -EIO;
207*4882a593Smuzhiyun 	return 0;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun  * Make sure the blocks described by maps are stable on disk.  This includes
212*4882a593Smuzhiyun  * converting any unwritten extents, flushing the disk cache and updating the
213*4882a593Smuzhiyun  * time stamps.
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * Note that we rely on the caller to always send us a timestamp update so that
216*4882a593Smuzhiyun  * we always commit a transaction here.  If that stops being true we will have
217*4882a593Smuzhiyun  * to manually flush the cache here similar to what the fsync code path does
218*4882a593Smuzhiyun  * for datasyncs on files that have no dirty metadata.
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun int
xfs_fs_commit_blocks(struct inode * inode,struct iomap * maps,int nr_maps,struct iattr * iattr)221*4882a593Smuzhiyun xfs_fs_commit_blocks(
222*4882a593Smuzhiyun 	struct inode		*inode,
223*4882a593Smuzhiyun 	struct iomap		*maps,
224*4882a593Smuzhiyun 	int			nr_maps,
225*4882a593Smuzhiyun 	struct iattr		*iattr)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	struct xfs_inode	*ip = XFS_I(inode);
228*4882a593Smuzhiyun 	struct xfs_mount	*mp = ip->i_mount;
229*4882a593Smuzhiyun 	struct xfs_trans	*tp;
230*4882a593Smuzhiyun 	bool			update_isize = false;
231*4882a593Smuzhiyun 	int			error, i;
232*4882a593Smuzhiyun 	loff_t			size;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	ASSERT(iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME));
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	xfs_ilock(ip, XFS_IOLOCK_EXCL);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	size = i_size_read(inode);
239*4882a593Smuzhiyun 	if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size > size) {
240*4882a593Smuzhiyun 		update_isize = true;
241*4882a593Smuzhiyun 		size = iattr->ia_size;
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	for (i = 0; i < nr_maps; i++) {
245*4882a593Smuzhiyun 		u64 start, length, end;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		start = maps[i].offset;
248*4882a593Smuzhiyun 		if (start > size)
249*4882a593Smuzhiyun 			continue;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 		end = start + maps[i].length;
252*4882a593Smuzhiyun 		if (end > size)
253*4882a593Smuzhiyun 			end = size;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 		length = end - start;
256*4882a593Smuzhiyun 		if (!length)
257*4882a593Smuzhiyun 			continue;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 		/*
260*4882a593Smuzhiyun 		 * Make sure reads through the pagecache see the new data.
261*4882a593Smuzhiyun 		 */
262*4882a593Smuzhiyun 		error = invalidate_inode_pages2_range(inode->i_mapping,
263*4882a593Smuzhiyun 					start >> PAGE_SHIFT,
264*4882a593Smuzhiyun 					(end - 1) >> PAGE_SHIFT);
265*4882a593Smuzhiyun 		WARN_ON_ONCE(error);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 		error = xfs_iomap_write_unwritten(ip, start, length, false);
268*4882a593Smuzhiyun 		if (error)
269*4882a593Smuzhiyun 			goto out_drop_iolock;
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	if (update_isize) {
273*4882a593Smuzhiyun 		error = xfs_pnfs_validate_isize(ip, size);
274*4882a593Smuzhiyun 		if (error)
275*4882a593Smuzhiyun 			goto out_drop_iolock;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
279*4882a593Smuzhiyun 	if (error)
280*4882a593Smuzhiyun 		goto out_drop_iolock;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	xfs_ilock(ip, XFS_ILOCK_EXCL);
283*4882a593Smuzhiyun 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
284*4882a593Smuzhiyun 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	xfs_setattr_time(ip, iattr);
287*4882a593Smuzhiyun 	if (update_isize) {
288*4882a593Smuzhiyun 		i_size_write(inode, iattr->ia_size);
289*4882a593Smuzhiyun 		ip->i_d.di_size = iattr->ia_size;
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	xfs_trans_set_sync(tp);
293*4882a593Smuzhiyun 	error = xfs_trans_commit(tp);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun out_drop_iolock:
296*4882a593Smuzhiyun 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
297*4882a593Smuzhiyun 	return error;
298*4882a593Smuzhiyun }
299