• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_inode_item.h"
32 #include "xfs_bmap.h"
33 #include "xfs_error.h"
34 #include "xfs_vnodeops.h"
35 #include "xfs_da_btree.h"
36 #include "xfs_ioctl.h"
37 #include "xfs_trace.h"
38 
39 #include <linux/dcache.h>
40 #include <linux/falloc.h>
41 
42 static const struct vm_operations_struct xfs_file_vm_ops;
43 
44 /*
45  * Locking primitives for read and write IO paths to ensure we consistently use
46  * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
47  */
48 static inline void
xfs_rw_ilock(struct xfs_inode * ip,int type)49 xfs_rw_ilock(
50 	struct xfs_inode	*ip,
51 	int			type)
52 {
53 	if (type & XFS_IOLOCK_EXCL)
54 		mutex_lock(&VFS_I(ip)->i_mutex);
55 	xfs_ilock(ip, type);
56 }
57 
58 static inline void
xfs_rw_iunlock(struct xfs_inode * ip,int type)59 xfs_rw_iunlock(
60 	struct xfs_inode	*ip,
61 	int			type)
62 {
63 	xfs_iunlock(ip, type);
64 	if (type & XFS_IOLOCK_EXCL)
65 		mutex_unlock(&VFS_I(ip)->i_mutex);
66 }
67 
68 static inline void
xfs_rw_ilock_demote(struct xfs_inode * ip,int type)69 xfs_rw_ilock_demote(
70 	struct xfs_inode	*ip,
71 	int			type)
72 {
73 	xfs_ilock_demote(ip, type);
74 	if (type & XFS_IOLOCK_EXCL)
75 		mutex_unlock(&VFS_I(ip)->i_mutex);
76 }
77 
78 /*
79  *	xfs_iozero
80  *
81  *	xfs_iozero clears the specified range of buffer supplied,
82  *	and marks all the affected blocks as valid and modified.  If
83  *	an affected block is not allocated, it will be allocated.  If
84  *	an affected block is not completely overwritten, and is not
85  *	valid before the operation, it will be read from disk before
86  *	being partially zeroed.
87  */
88 STATIC int
xfs_iozero(struct xfs_inode * ip,loff_t pos,size_t count)89 xfs_iozero(
90 	struct xfs_inode	*ip,	/* inode			*/
91 	loff_t			pos,	/* offset in file		*/
92 	size_t			count)	/* size of data to zero		*/
93 {
94 	struct page		*page;
95 	struct address_space	*mapping;
96 	int			status;
97 
98 	mapping = VFS_I(ip)->i_mapping;
99 	do {
100 		unsigned offset, bytes;
101 		void *fsdata;
102 
103 		offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
104 		bytes = PAGE_CACHE_SIZE - offset;
105 		if (bytes > count)
106 			bytes = count;
107 
108 		status = pagecache_write_begin(NULL, mapping, pos, bytes,
109 					AOP_FLAG_UNINTERRUPTIBLE,
110 					&page, &fsdata);
111 		if (status)
112 			break;
113 
114 		zero_user(page, offset, bytes);
115 
116 		status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
117 					page, fsdata);
118 		WARN_ON(status <= 0); /* can't return less than zero! */
119 		pos += bytes;
120 		count -= bytes;
121 		status = 0;
122 	} while (count);
123 
124 	return (-status);
125 }
126 
127 /*
128  * Fsync operations on directories are much simpler than on regular files,
129  * as there is no file data to flush, and thus also no need for explicit
130  * cache flush operations, and there are no non-transaction metadata updates
131  * on directories either.
132  */
133 STATIC int
xfs_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)134 xfs_dir_fsync(
135 	struct file		*file,
136 	loff_t			start,
137 	loff_t			end,
138 	int			datasync)
139 {
140 	struct xfs_inode	*ip = XFS_I(file->f_mapping->host);
141 	struct xfs_mount	*mp = ip->i_mount;
142 	xfs_lsn_t		lsn = 0;
143 
144 	trace_xfs_dir_fsync(ip);
145 
146 	xfs_ilock(ip, XFS_ILOCK_SHARED);
147 	if (xfs_ipincount(ip))
148 		lsn = ip->i_itemp->ili_last_lsn;
149 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
150 
151 	if (!lsn)
152 		return 0;
153 	return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
154 }
155 
156 STATIC int
xfs_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)157 xfs_file_fsync(
158 	struct file		*file,
159 	loff_t			start,
160 	loff_t			end,
161 	int			datasync)
162 {
163 	struct inode		*inode = file->f_mapping->host;
164 	struct xfs_inode	*ip = XFS_I(inode);
165 	struct xfs_mount	*mp = ip->i_mount;
166 	int			error = 0;
167 	int			log_flushed = 0;
168 	xfs_lsn_t		lsn = 0;
169 
170 	trace_xfs_file_fsync(ip);
171 
172 	error = filemap_write_and_wait_range(inode->i_mapping, start, end);
173 	if (error)
174 		return error;
175 
176 	if (XFS_FORCED_SHUTDOWN(mp))
177 		return -XFS_ERROR(EIO);
178 
179 	xfs_iflags_clear(ip, XFS_ITRUNCATED);
180 
181 	if (mp->m_flags & XFS_MOUNT_BARRIER) {
182 		/*
183 		 * If we have an RT and/or log subvolume we need to make sure
184 		 * to flush the write cache the device used for file data
185 		 * first.  This is to ensure newly written file data make
186 		 * it to disk before logging the new inode size in case of
187 		 * an extending write.
188 		 */
189 		if (XFS_IS_REALTIME_INODE(ip))
190 			xfs_blkdev_issue_flush(mp->m_rtdev_targp);
191 		else if (mp->m_logdev_targp != mp->m_ddev_targp)
192 			xfs_blkdev_issue_flush(mp->m_ddev_targp);
193 	}
194 
195 	/*
196 	 * All metadata updates are logged, which means that we just have
197 	 * to flush the log up to the latest LSN that touched the inode.
198 	 */
199 	xfs_ilock(ip, XFS_ILOCK_SHARED);
200 	if (xfs_ipincount(ip)) {
201 		if (!datasync ||
202 		    (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
203 			lsn = ip->i_itemp->ili_last_lsn;
204 	}
205 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
206 
207 	if (lsn)
208 		error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
209 
210 	/*
211 	 * If we only have a single device, and the log force about was
212 	 * a no-op we might have to flush the data device cache here.
213 	 * This can only happen for fdatasync/O_DSYNC if we were overwriting
214 	 * an already allocated file and thus do not have any metadata to
215 	 * commit.
216 	 */
217 	if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
218 	    mp->m_logdev_targp == mp->m_ddev_targp &&
219 	    !XFS_IS_REALTIME_INODE(ip) &&
220 	    !log_flushed)
221 		xfs_blkdev_issue_flush(mp->m_ddev_targp);
222 
223 	return -error;
224 }
225 
226 STATIC ssize_t
xfs_file_aio_read(struct kiocb * iocb,const struct iovec * iovp,unsigned long nr_segs,loff_t pos)227 xfs_file_aio_read(
228 	struct kiocb		*iocb,
229 	const struct iovec	*iovp,
230 	unsigned long		nr_segs,
231 	loff_t			pos)
232 {
233 	struct file		*file = iocb->ki_filp;
234 	struct inode		*inode = file->f_mapping->host;
235 	struct xfs_inode	*ip = XFS_I(inode);
236 	struct xfs_mount	*mp = ip->i_mount;
237 	size_t			size = 0;
238 	ssize_t			ret = 0;
239 	int			ioflags = 0;
240 	xfs_fsize_t		n;
241 	unsigned long		seg;
242 
243 	XFS_STATS_INC(xs_read_calls);
244 
245 	BUG_ON(iocb->ki_pos != pos);
246 
247 	if (unlikely(file->f_flags & O_DIRECT))
248 		ioflags |= IO_ISDIRECT;
249 	if (file->f_mode & FMODE_NOCMTIME)
250 		ioflags |= IO_INVIS;
251 
252 	/* START copy & waste from filemap.c */
253 	for (seg = 0; seg < nr_segs; seg++) {
254 		const struct iovec *iv = &iovp[seg];
255 
256 		/*
257 		 * If any segment has a negative length, or the cumulative
258 		 * length ever wraps negative then return -EINVAL.
259 		 */
260 		size += iv->iov_len;
261 		if (unlikely((ssize_t)(size|iv->iov_len) < 0))
262 			return XFS_ERROR(-EINVAL);
263 	}
264 	/* END copy & waste from filemap.c */
265 
266 	if (unlikely(ioflags & IO_ISDIRECT)) {
267 		xfs_buftarg_t	*target =
268 			XFS_IS_REALTIME_INODE(ip) ?
269 				mp->m_rtdev_targp : mp->m_ddev_targp;
270 		if ((iocb->ki_pos & target->bt_smask) ||
271 		    (size & target->bt_smask)) {
272 			if (iocb->ki_pos == i_size_read(inode))
273 				return 0;
274 			return -XFS_ERROR(EINVAL);
275 		}
276 	}
277 
278 	n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
279 	if (n <= 0 || size == 0)
280 		return 0;
281 
282 	if (n < size)
283 		size = n;
284 
285 	if (XFS_FORCED_SHUTDOWN(mp))
286 		return -EIO;
287 
288 	/*
289 	 * Locking is a bit tricky here. If we take an exclusive lock
290 	 * for direct IO, we effectively serialise all new concurrent
291 	 * read IO to this file and block it behind IO that is currently in
292 	 * progress because IO in progress holds the IO lock shared. We only
293 	 * need to hold the lock exclusive to blow away the page cache, so
294 	 * only take lock exclusively if the page cache needs invalidation.
295 	 * This allows the normal direct IO case of no page cache pages to
296 	 * proceeed concurrently without serialisation.
297 	 */
298 	xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
299 	if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
300 		xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
301 		xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
302 
303 		if (inode->i_mapping->nrpages) {
304 			ret = -xfs_flushinval_pages(ip,
305 					(iocb->ki_pos & PAGE_CACHE_MASK),
306 					-1, FI_REMAPF_LOCKED);
307 			if (ret) {
308 				xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
309 				return ret;
310 			}
311 		}
312 		xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
313 	}
314 
315 	trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
316 
317 	ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
318 	if (ret > 0)
319 		XFS_STATS_ADD(xs_read_bytes, ret);
320 
321 	xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
322 	return ret;
323 }
324 
325 STATIC ssize_t
xfs_file_splice_read(struct file * infilp,loff_t * ppos,struct pipe_inode_info * pipe,size_t count,unsigned int flags)326 xfs_file_splice_read(
327 	struct file		*infilp,
328 	loff_t			*ppos,
329 	struct pipe_inode_info	*pipe,
330 	size_t			count,
331 	unsigned int		flags)
332 {
333 	struct xfs_inode	*ip = XFS_I(infilp->f_mapping->host);
334 	int			ioflags = 0;
335 	ssize_t			ret;
336 
337 	XFS_STATS_INC(xs_read_calls);
338 
339 	if (infilp->f_mode & FMODE_NOCMTIME)
340 		ioflags |= IO_INVIS;
341 
342 	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
343 		return -EIO;
344 
345 	xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
346 
347 	trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
348 
349 	ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
350 	if (ret > 0)
351 		XFS_STATS_ADD(xs_read_bytes, ret);
352 
353 	xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
354 	return ret;
355 }
356 
357 /*
358  * xfs_file_splice_write() does not use xfs_rw_ilock() because
359  * generic_file_splice_write() takes the i_mutex itself. This, in theory,
360  * couuld cause lock inversions between the aio_write path and the splice path
361  * if someone is doing concurrent splice(2) based writes and write(2) based
362  * writes to the same inode. The only real way to fix this is to re-implement
363  * the generic code here with correct locking orders.
364  */
365 STATIC ssize_t
xfs_file_splice_write(struct pipe_inode_info * pipe,struct file * outfilp,loff_t * ppos,size_t count,unsigned int flags)366 xfs_file_splice_write(
367 	struct pipe_inode_info	*pipe,
368 	struct file		*outfilp,
369 	loff_t			*ppos,
370 	size_t			count,
371 	unsigned int		flags)
372 {
373 	struct inode		*inode = outfilp->f_mapping->host;
374 	struct xfs_inode	*ip = XFS_I(inode);
375 	int			ioflags = 0;
376 	ssize_t			ret;
377 
378 	XFS_STATS_INC(xs_write_calls);
379 
380 	if (outfilp->f_mode & FMODE_NOCMTIME)
381 		ioflags |= IO_INVIS;
382 
383 	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
384 		return -EIO;
385 
386 	xfs_ilock(ip, XFS_IOLOCK_EXCL);
387 
388 	trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
389 
390 	ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
391 	if (ret > 0)
392 		XFS_STATS_ADD(xs_write_bytes, ret);
393 
394 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
395 	return ret;
396 }
397 
398 /*
399  * This routine is called to handle zeroing any space in the last
400  * block of the file that is beyond the EOF.  We do this since the
401  * size is being increased without writing anything to that block
402  * and we don't want anyone to read the garbage on the disk.
403  */
404 STATIC int				/* error (positive) */
xfs_zero_last_block(xfs_inode_t * ip,xfs_fsize_t offset,xfs_fsize_t isize)405 xfs_zero_last_block(
406 	xfs_inode_t	*ip,
407 	xfs_fsize_t	offset,
408 	xfs_fsize_t	isize)
409 {
410 	xfs_fileoff_t	last_fsb;
411 	xfs_mount_t	*mp = ip->i_mount;
412 	int		nimaps;
413 	int		zero_offset;
414 	int		zero_len;
415 	int		error = 0;
416 	xfs_bmbt_irec_t	imap;
417 
418 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
419 
420 	zero_offset = XFS_B_FSB_OFFSET(mp, isize);
421 	if (zero_offset == 0) {
422 		/*
423 		 * There are no extra bytes in the last block on disk to
424 		 * zero, so return.
425 		 */
426 		return 0;
427 	}
428 
429 	last_fsb = XFS_B_TO_FSBT(mp, isize);
430 	nimaps = 1;
431 	error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
432 	if (error)
433 		return error;
434 	ASSERT(nimaps > 0);
435 	/*
436 	 * If the block underlying isize is just a hole, then there
437 	 * is nothing to zero.
438 	 */
439 	if (imap.br_startblock == HOLESTARTBLOCK) {
440 		return 0;
441 	}
442 	/*
443 	 * Zero the part of the last block beyond the EOF, and write it
444 	 * out sync.  We need to drop the ilock while we do this so we
445 	 * don't deadlock when the buffer cache calls back to us.
446 	 */
447 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
448 
449 	zero_len = mp->m_sb.sb_blocksize - zero_offset;
450 	if (isize + zero_len > offset)
451 		zero_len = offset - isize;
452 	error = xfs_iozero(ip, isize, zero_len);
453 
454 	xfs_ilock(ip, XFS_ILOCK_EXCL);
455 	ASSERT(error >= 0);
456 	return error;
457 }
458 
459 /*
460  * Zero any on disk space between the current EOF and the new,
461  * larger EOF.  This handles the normal case of zeroing the remainder
462  * of the last block in the file and the unusual case of zeroing blocks
463  * out beyond the size of the file.  This second case only happens
464  * with fixed size extents and when the system crashes before the inode
465  * size was updated but after blocks were allocated.  If fill is set,
466  * then any holes in the range are filled and zeroed.  If not, the holes
467  * are left alone as holes.
468  */
469 
470 int					/* error (positive) */
xfs_zero_eof(xfs_inode_t * ip,xfs_off_t offset,xfs_fsize_t isize)471 xfs_zero_eof(
472 	xfs_inode_t	*ip,
473 	xfs_off_t	offset,		/* starting I/O offset */
474 	xfs_fsize_t	isize)		/* current inode size */
475 {
476 	xfs_mount_t	*mp = ip->i_mount;
477 	xfs_fileoff_t	start_zero_fsb;
478 	xfs_fileoff_t	end_zero_fsb;
479 	xfs_fileoff_t	zero_count_fsb;
480 	xfs_fileoff_t	last_fsb;
481 	xfs_fileoff_t	zero_off;
482 	xfs_fsize_t	zero_len;
483 	int		nimaps;
484 	int		error = 0;
485 	xfs_bmbt_irec_t	imap;
486 
487 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
488 	ASSERT(offset > isize);
489 
490 	/*
491 	 * First handle zeroing the block on which isize resides.
492 	 * We only zero a part of that block so it is handled specially.
493 	 */
494 	error = xfs_zero_last_block(ip, offset, isize);
495 	if (error) {
496 		ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
497 		return error;
498 	}
499 
500 	/*
501 	 * Calculate the range between the new size and the old
502 	 * where blocks needing to be zeroed may exist.  To get the
503 	 * block where the last byte in the file currently resides,
504 	 * we need to subtract one from the size and truncate back
505 	 * to a block boundary.  We subtract 1 in case the size is
506 	 * exactly on a block boundary.
507 	 */
508 	last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
509 	start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
510 	end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
511 	ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
512 	if (last_fsb == end_zero_fsb) {
513 		/*
514 		 * The size was only incremented on its last block.
515 		 * We took care of that above, so just return.
516 		 */
517 		return 0;
518 	}
519 
520 	ASSERT(start_zero_fsb <= end_zero_fsb);
521 	while (start_zero_fsb <= end_zero_fsb) {
522 		nimaps = 1;
523 		zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
524 		error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
525 					  &imap, &nimaps, 0);
526 		if (error) {
527 			ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
528 			return error;
529 		}
530 		ASSERT(nimaps > 0);
531 
532 		if (imap.br_state == XFS_EXT_UNWRITTEN ||
533 		    imap.br_startblock == HOLESTARTBLOCK) {
534 			/*
535 			 * This loop handles initializing pages that were
536 			 * partially initialized by the code below this
537 			 * loop. It basically zeroes the part of the page
538 			 * that sits on a hole and sets the page as P_HOLE
539 			 * and calls remapf if it is a mapped file.
540 			 */
541 			start_zero_fsb = imap.br_startoff + imap.br_blockcount;
542 			ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
543 			continue;
544 		}
545 
546 		/*
547 		 * There are blocks we need to zero.
548 		 * Drop the inode lock while we're doing the I/O.
549 		 * We'll still have the iolock to protect us.
550 		 */
551 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
552 
553 		zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
554 		zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
555 
556 		if ((zero_off + zero_len) > offset)
557 			zero_len = offset - zero_off;
558 
559 		error = xfs_iozero(ip, zero_off, zero_len);
560 		if (error) {
561 			goto out_lock;
562 		}
563 
564 		start_zero_fsb = imap.br_startoff + imap.br_blockcount;
565 		ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
566 
567 		xfs_ilock(ip, XFS_ILOCK_EXCL);
568 	}
569 
570 	return 0;
571 
572 out_lock:
573 	xfs_ilock(ip, XFS_ILOCK_EXCL);
574 	ASSERT(error >= 0);
575 	return error;
576 }
577 
578 /*
579  * Common pre-write limit and setup checks.
580  *
581  * Called with the iolocked held either shared and exclusive according to
582  * @iolock, and returns with it held.  Might upgrade the iolock to exclusive
583  * if called for a direct write beyond i_size.
584  */
585 STATIC ssize_t
xfs_file_aio_write_checks(struct file * file,loff_t * pos,size_t * count,int * iolock)586 xfs_file_aio_write_checks(
587 	struct file		*file,
588 	loff_t			*pos,
589 	size_t			*count,
590 	int			*iolock)
591 {
592 	struct inode		*inode = file->f_mapping->host;
593 	struct xfs_inode	*ip = XFS_I(inode);
594 	int			error = 0;
595 
596 	xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
597 restart:
598 	error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
599 	if (error) {
600 		xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
601 		return error;
602 	}
603 
604 	/*
605 	 * If the offset is beyond the size of the file, we need to zero any
606 	 * blocks that fall between the existing EOF and the start of this
607 	 * write.  If zeroing is needed and we are currently holding the
608 	 * iolock shared, we need to update it to exclusive which involves
609 	 * dropping all locks and relocking to maintain correct locking order.
610 	 * If we do this, restart the function to ensure all checks and values
611 	 * are still valid.
612 	 */
613 	if (*pos > i_size_read(inode)) {
614 		if (*iolock == XFS_IOLOCK_SHARED) {
615 			xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
616 			*iolock = XFS_IOLOCK_EXCL;
617 			xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
618 			goto restart;
619 		}
620 		error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
621 	}
622 	xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
623 	if (error)
624 		return error;
625 
626 	/*
627 	 * Updating the timestamps will grab the ilock again from
628 	 * xfs_fs_dirty_inode, so we have to call it after dropping the
629 	 * lock above.  Eventually we should look into a way to avoid
630 	 * the pointless lock roundtrip.
631 	 */
632 	if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
633 		error = file_update_time(file);
634 		if (error)
635 			return error;
636 	}
637 
638 	/*
639 	 * If we're writing the file then make sure to clear the setuid and
640 	 * setgid bits if the process is not being run by root.  This keeps
641 	 * people from modifying setuid and setgid binaries.
642 	 */
643 	return file_remove_suid(file);
644 
645 }
646 
647 /*
648  * xfs_file_dio_aio_write - handle direct IO writes
649  *
650  * Lock the inode appropriately to prepare for and issue a direct IO write.
651  * By separating it from the buffered write path we remove all the tricky to
652  * follow locking changes and looping.
653  *
654  * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
655  * until we're sure the bytes at the new EOF have been zeroed and/or the cached
656  * pages are flushed out.
657  *
658  * In most cases the direct IO writes will be done holding IOLOCK_SHARED
659  * allowing them to be done in parallel with reads and other direct IO writes.
660  * However, if the IO is not aligned to filesystem blocks, the direct IO layer
661  * needs to do sub-block zeroing and that requires serialisation against other
662  * direct IOs to the same block. In this case we need to serialise the
663  * submission of the unaligned IOs so that we don't get racing block zeroing in
664  * the dio layer.  To avoid the problem with aio, we also need to wait for
665  * outstanding IOs to complete so that unwritten extent conversion is completed
666  * before we try to map the overlapping block. This is currently implemented by
667  * hitting it with a big hammer (i.e. inode_dio_wait()).
668  *
669  * Returns with locks held indicated by @iolock and errors indicated by
670  * negative return values.
671  */
672 STATIC ssize_t
xfs_file_dio_aio_write(struct kiocb * iocb,const struct iovec * iovp,unsigned long nr_segs,loff_t pos,size_t ocount)673 xfs_file_dio_aio_write(
674 	struct kiocb		*iocb,
675 	const struct iovec	*iovp,
676 	unsigned long		nr_segs,
677 	loff_t			pos,
678 	size_t			ocount)
679 {
680 	struct file		*file = iocb->ki_filp;
681 	struct address_space	*mapping = file->f_mapping;
682 	struct inode		*inode = mapping->host;
683 	struct xfs_inode	*ip = XFS_I(inode);
684 	struct xfs_mount	*mp = ip->i_mount;
685 	ssize_t			ret = 0;
686 	size_t			count = ocount;
687 	int			unaligned_io = 0;
688 	int			iolock;
689 	struct xfs_buftarg	*target = XFS_IS_REALTIME_INODE(ip) ?
690 					mp->m_rtdev_targp : mp->m_ddev_targp;
691 
692 	if ((pos & target->bt_smask) || (count & target->bt_smask))
693 		return -XFS_ERROR(EINVAL);
694 
695 	if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
696 		unaligned_io = 1;
697 
698 	/*
699 	 * We don't need to take an exclusive lock unless there page cache needs
700 	 * to be invalidated or unaligned IO is being executed. We don't need to
701 	 * consider the EOF extension case here because
702 	 * xfs_file_aio_write_checks() will relock the inode as necessary for
703 	 * EOF zeroing cases and fill out the new inode size as appropriate.
704 	 */
705 	if (unaligned_io || mapping->nrpages)
706 		iolock = XFS_IOLOCK_EXCL;
707 	else
708 		iolock = XFS_IOLOCK_SHARED;
709 	xfs_rw_ilock(ip, iolock);
710 
711 	/*
712 	 * Recheck if there are cached pages that need invalidate after we got
713 	 * the iolock to protect against other threads adding new pages while
714 	 * we were waiting for the iolock.
715 	 */
716 	if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
717 		xfs_rw_iunlock(ip, iolock);
718 		iolock = XFS_IOLOCK_EXCL;
719 		xfs_rw_ilock(ip, iolock);
720 	}
721 
722 	ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
723 	if (ret)
724 		goto out;
725 
726 	if (mapping->nrpages) {
727 		ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
728 							FI_REMAPF_LOCKED);
729 		if (ret)
730 			goto out;
731 	}
732 
733 	/*
734 	 * If we are doing unaligned IO, wait for all other IO to drain,
735 	 * otherwise demote the lock if we had to flush cached pages
736 	 */
737 	if (unaligned_io)
738 		inode_dio_wait(inode);
739 	else if (iolock == XFS_IOLOCK_EXCL) {
740 		xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
741 		iolock = XFS_IOLOCK_SHARED;
742 	}
743 
744 	trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
745 	ret = generic_file_direct_write(iocb, iovp,
746 			&nr_segs, pos, &iocb->ki_pos, count, ocount);
747 
748 out:
749 	xfs_rw_iunlock(ip, iolock);
750 
751 	/* No fallback to buffered IO on errors for XFS. */
752 	ASSERT(ret < 0 || ret == count);
753 	return ret;
754 }
755 
756 STATIC ssize_t
xfs_file_buffered_aio_write(struct kiocb * iocb,const struct iovec * iovp,unsigned long nr_segs,loff_t pos,size_t ocount)757 xfs_file_buffered_aio_write(
758 	struct kiocb		*iocb,
759 	const struct iovec	*iovp,
760 	unsigned long		nr_segs,
761 	loff_t			pos,
762 	size_t			ocount)
763 {
764 	struct file		*file = iocb->ki_filp;
765 	struct address_space	*mapping = file->f_mapping;
766 	struct inode		*inode = mapping->host;
767 	struct xfs_inode	*ip = XFS_I(inode);
768 	ssize_t			ret;
769 	int			enospc = 0;
770 	int			iolock = XFS_IOLOCK_EXCL;
771 	size_t			count = ocount;
772 
773 	xfs_rw_ilock(ip, iolock);
774 
775 	ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
776 	if (ret)
777 		goto out;
778 
779 	/* We can write back this queue in page reclaim */
780 	current->backing_dev_info = mapping->backing_dev_info;
781 
782 write_retry:
783 	trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
784 	ret = generic_file_buffered_write(iocb, iovp, nr_segs,
785 			pos, &iocb->ki_pos, count, ret);
786 	/*
787 	 * if we just got an ENOSPC, flush the inode now we aren't holding any
788 	 * page locks and retry *once*
789 	 */
790 	if (ret == -ENOSPC && !enospc) {
791 		enospc = 1;
792 		ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
793 		if (!ret)
794 			goto write_retry;
795 	}
796 
797 	current->backing_dev_info = NULL;
798 out:
799 	xfs_rw_iunlock(ip, iolock);
800 	return ret;
801 }
802 
803 STATIC ssize_t
xfs_file_aio_write(struct kiocb * iocb,const struct iovec * iovp,unsigned long nr_segs,loff_t pos)804 xfs_file_aio_write(
805 	struct kiocb		*iocb,
806 	const struct iovec	*iovp,
807 	unsigned long		nr_segs,
808 	loff_t			pos)
809 {
810 	struct file		*file = iocb->ki_filp;
811 	struct address_space	*mapping = file->f_mapping;
812 	struct inode		*inode = mapping->host;
813 	struct xfs_inode	*ip = XFS_I(inode);
814 	ssize_t			ret;
815 	size_t			ocount = 0;
816 
817 	XFS_STATS_INC(xs_write_calls);
818 
819 	BUG_ON(iocb->ki_pos != pos);
820 
821 	ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
822 	if (ret)
823 		return ret;
824 
825 	if (ocount == 0)
826 		return 0;
827 
828 	xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
829 
830 	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
831 		return -EIO;
832 
833 	if (unlikely(file->f_flags & O_DIRECT))
834 		ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
835 	else
836 		ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
837 						  ocount);
838 
839 	if (ret > 0) {
840 		ssize_t err;
841 
842 		XFS_STATS_ADD(xs_write_bytes, ret);
843 
844 		/* Handle various SYNC-type writes */
845 		err = generic_write_sync(file, pos, ret);
846 		if (err < 0)
847 			ret = err;
848 	}
849 
850 	return ret;
851 }
852 
853 STATIC long
xfs_file_fallocate(struct file * file,int mode,loff_t offset,loff_t len)854 xfs_file_fallocate(
855 	struct file	*file,
856 	int		mode,
857 	loff_t		offset,
858 	loff_t		len)
859 {
860 	struct inode	*inode = file->f_path.dentry->d_inode;
861 	long		error;
862 	loff_t		new_size = 0;
863 	xfs_flock64_t	bf;
864 	xfs_inode_t	*ip = XFS_I(inode);
865 	int		cmd = XFS_IOC_RESVSP;
866 	int		attr_flags = XFS_ATTR_NOLOCK;
867 
868 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
869 		return -EOPNOTSUPP;
870 
871 	bf.l_whence = 0;
872 	bf.l_start = offset;
873 	bf.l_len = len;
874 
875 	xfs_ilock(ip, XFS_IOLOCK_EXCL);
876 
877 	if (mode & FALLOC_FL_PUNCH_HOLE)
878 		cmd = XFS_IOC_UNRESVSP;
879 
880 	/* check the new inode size is valid before allocating */
881 	if (!(mode & FALLOC_FL_KEEP_SIZE) &&
882 	    offset + len > i_size_read(inode)) {
883 		new_size = offset + len;
884 		error = inode_newsize_ok(inode, new_size);
885 		if (error)
886 			goto out_unlock;
887 	}
888 
889 	if (file->f_flags & O_DSYNC)
890 		attr_flags |= XFS_ATTR_SYNC;
891 
892 	error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
893 	if (error)
894 		goto out_unlock;
895 
896 	/* Change file size if needed */
897 	if (new_size) {
898 		struct iattr iattr;
899 
900 		iattr.ia_valid = ATTR_SIZE;
901 		iattr.ia_size = new_size;
902 		error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
903 	}
904 
905 out_unlock:
906 	xfs_iunlock(ip, XFS_IOLOCK_EXCL);
907 	return error;
908 }
909 
910 
911 STATIC int
xfs_file_open(struct inode * inode,struct file * file)912 xfs_file_open(
913 	struct inode	*inode,
914 	struct file	*file)
915 {
916 	if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
917 		return -EFBIG;
918 	if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
919 		return -EIO;
920 	return 0;
921 }
922 
923 STATIC int
xfs_dir_open(struct inode * inode,struct file * file)924 xfs_dir_open(
925 	struct inode	*inode,
926 	struct file	*file)
927 {
928 	struct xfs_inode *ip = XFS_I(inode);
929 	int		mode;
930 	int		error;
931 
932 	error = xfs_file_open(inode, file);
933 	if (error)
934 		return error;
935 
936 	/*
937 	 * If there are any blocks, read-ahead block 0 as we're almost
938 	 * certain to have the next operation be a read there.
939 	 */
940 	mode = xfs_ilock_map_shared(ip);
941 	if (ip->i_d.di_nextents > 0)
942 		xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
943 	xfs_iunlock(ip, mode);
944 	return 0;
945 }
946 
947 STATIC int
xfs_file_release(struct inode * inode,struct file * filp)948 xfs_file_release(
949 	struct inode	*inode,
950 	struct file	*filp)
951 {
952 	return -xfs_release(XFS_I(inode));
953 }
954 
955 STATIC int
xfs_file_readdir(struct file * filp,void * dirent,filldir_t filldir)956 xfs_file_readdir(
957 	struct file	*filp,
958 	void		*dirent,
959 	filldir_t	filldir)
960 {
961 	struct inode	*inode = filp->f_path.dentry->d_inode;
962 	xfs_inode_t	*ip = XFS_I(inode);
963 	int		error;
964 	size_t		bufsize;
965 
966 	/*
967 	 * The Linux API doesn't pass down the total size of the buffer
968 	 * we read into down to the filesystem.  With the filldir concept
969 	 * it's not needed for correct information, but the XFS dir2 leaf
970 	 * code wants an estimate of the buffer size to calculate it's
971 	 * readahead window and size the buffers used for mapping to
972 	 * physical blocks.
973 	 *
974 	 * Try to give it an estimate that's good enough, maybe at some
975 	 * point we can change the ->readdir prototype to include the
976 	 * buffer size.  For now we use the current glibc buffer size.
977 	 */
978 	bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
979 
980 	error = xfs_readdir(ip, dirent, bufsize,
981 				(xfs_off_t *)&filp->f_pos, filldir);
982 	if (error)
983 		return -error;
984 	return 0;
985 }
986 
987 STATIC int
xfs_file_mmap(struct file * filp,struct vm_area_struct * vma)988 xfs_file_mmap(
989 	struct file	*filp,
990 	struct vm_area_struct *vma)
991 {
992 	vma->vm_ops = &xfs_file_vm_ops;
993 	vma->vm_flags |= VM_CAN_NONLINEAR;
994 
995 	file_accessed(filp);
996 	return 0;
997 }
998 
999 /*
1000  * mmap()d file has taken write protection fault and is being made
1001  * writable. We can set the page state up correctly for a writable
1002  * page, which means we can do correct delalloc accounting (ENOSPC
1003  * checking!) and unwritten extent mapping.
1004  */
1005 STATIC int
xfs_vm_page_mkwrite(struct vm_area_struct * vma,struct vm_fault * vmf)1006 xfs_vm_page_mkwrite(
1007 	struct vm_area_struct	*vma,
1008 	struct vm_fault		*vmf)
1009 {
1010 	return block_page_mkwrite(vma, vmf, xfs_get_blocks);
1011 }
1012 
1013 const struct file_operations xfs_file_operations = {
1014 	.llseek		= generic_file_llseek,
1015 	.read		= do_sync_read,
1016 	.write		= do_sync_write,
1017 	.aio_read	= xfs_file_aio_read,
1018 	.aio_write	= xfs_file_aio_write,
1019 	.splice_read	= xfs_file_splice_read,
1020 	.splice_write	= xfs_file_splice_write,
1021 	.unlocked_ioctl	= xfs_file_ioctl,
1022 #ifdef CONFIG_COMPAT
1023 	.compat_ioctl	= xfs_file_compat_ioctl,
1024 #endif
1025 	.mmap		= xfs_file_mmap,
1026 	.open		= xfs_file_open,
1027 	.release	= xfs_file_release,
1028 	.fsync		= xfs_file_fsync,
1029 	.fallocate	= xfs_file_fallocate,
1030 };
1031 
1032 const struct file_operations xfs_dir_file_operations = {
1033 	.open		= xfs_dir_open,
1034 	.read		= generic_read_dir,
1035 	.readdir	= xfs_file_readdir,
1036 	.llseek		= generic_file_llseek,
1037 	.unlocked_ioctl	= xfs_file_ioctl,
1038 #ifdef CONFIG_COMPAT
1039 	.compat_ioctl	= xfs_file_compat_ioctl,
1040 #endif
1041 	.fsync		= xfs_dir_fsync,
1042 };
1043 
1044 static const struct vm_operations_struct xfs_file_vm_ops = {
1045 	.fault		= filemap_fault,
1046 	.page_mkwrite	= xfs_vm_page_mkwrite,
1047 };
1048