blob: 8d26d93648b4aaaba94c8429fc6a12e1b07a67b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
27#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000031#include "xfs_inode_item.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000032#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100034#include "xfs_vnodeops.h"
Christoph Hellwigf999a5b2008-11-28 14:23:32 +110035#include "xfs_da_btree.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050036#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000037#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040041static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Christoph Hellwigdda35b82010-02-15 09:44:46 +000043/*
44 * xfs_iozero
45 *
46 * xfs_iozero clears the specified range of buffer supplied,
47 * and marks all the affected blocks as valid and modified. If
48 * an affected block is not allocated, it will be allocated. If
49 * an affected block is not completely overwritten, and is not
50 * valid before the operation, it will be read from disk before
51 * being partially zeroed.
52 */
53STATIC int
54xfs_iozero(
55 struct xfs_inode *ip, /* inode */
56 loff_t pos, /* offset in file */
57 size_t count) /* size of data to zero */
58{
59 struct page *page;
60 struct address_space *mapping;
61 int status;
62
63 mapping = VFS_I(ip)->i_mapping;
64 do {
65 unsigned offset, bytes;
66 void *fsdata;
67
68 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
69 bytes = PAGE_CACHE_SIZE - offset;
70 if (bytes > count)
71 bytes = count;
72
73 status = pagecache_write_begin(NULL, mapping, pos, bytes,
74 AOP_FLAG_UNINTERRUPTIBLE,
75 &page, &fsdata);
76 if (status)
77 break;
78
79 zero_user(page, offset, bytes);
80
81 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
82 page, fsdata);
83 WARN_ON(status <= 0); /* can't return less than zero! */
84 pos += bytes;
85 count -= bytes;
86 status = 0;
87 } while (count);
88
89 return (-status);
90}
91
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000092STATIC int
93xfs_file_fsync(
94 struct file *file,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000095 int datasync)
96{
Christoph Hellwig7ea80852010-05-26 17:53:25 +020097 struct inode *inode = file->f_mapping->host;
98 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000099 struct xfs_trans *tp;
100 int error = 0;
101 int log_flushed = 0;
102
103 xfs_itrace_entry(ip);
104
105 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
106 return -XFS_ERROR(EIO);
107
108 xfs_iflags_clear(ip, XFS_ITRUNCATED);
109
Christoph Hellwig37bc5742010-04-20 17:00:59 +1000110 xfs_ioend_wait(ip);
111
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000112 /*
113 * We always need to make sure that the required inode state is safe on
114 * disk. The inode might be clean but we still might need to force the
115 * log because of committed transactions that haven't hit the disk yet.
116 * Likewise, there could be unflushed non-transactional changes to the
117 * inode core that have to go to disk and this requires us to issue
118 * a synchronous transaction to capture these changes correctly.
119 *
120 * This code relies on the assumption that if the i_update_core field
121 * of the inode is clear and the inode is unpinned then it is clean
122 * and no action is required.
123 */
124 xfs_ilock(ip, XFS_ILOCK_SHARED);
125
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000126 /*
127 * First check if the VFS inode is marked dirty. All the dirtying
128 * of non-transactional updates no goes through mark_inode_dirty*,
129 * which allows us to distinguish beteeen pure timestamp updates
130 * and i_size updates which need to be caught for fdatasync.
131 * After that also theck for the dirty state in the XFS inode, which
132 * might gets cleared when the inode gets written out via the AIL
133 * or xfs_iflush_cluster.
134 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200135 if (((inode->i_state & I_DIRTY_DATASYNC) ||
136 ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000137 ip->i_update_core) {
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000138 /*
139 * Kick off a transaction to log the inode core to get the
140 * updates. The sync transaction will also force the log.
141 */
142 xfs_iunlock(ip, XFS_ILOCK_SHARED);
143 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
144 error = xfs_trans_reserve(tp, 0,
145 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
146 if (error) {
147 xfs_trans_cancel(tp, 0);
148 return -error;
149 }
150 xfs_ilock(ip, XFS_ILOCK_EXCL);
151
152 /*
153 * Note - it's possible that we might have pushed ourselves out
154 * of the way during trans_reserve which would flush the inode.
155 * But there's no guarantee that the inode buffer has actually
156 * gone out yet (it's delwri). Plus the buffer could be pinned
157 * anyway if it's part of an inode in another recent
158 * transaction. So we play it safe and fire off the
159 * transaction anyway.
160 */
161 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
162 xfs_trans_ihold(tp, ip);
163 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
164 xfs_trans_set_sync(tp);
165 error = _xfs_trans_commit(tp, 0, &log_flushed);
166
167 xfs_iunlock(ip, XFS_ILOCK_EXCL);
168 } else {
169 /*
170 * Timestamps/size haven't changed since last inode flush or
171 * inode transaction commit. That means either nothing got
172 * written or a transaction committed which caught the updates.
173 * If the latter happened and the transaction hasn't hit the
174 * disk yet, the inode will be still be pinned. If it is,
175 * force the log.
176 */
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000177 if (xfs_ipincount(ip)) {
Christoph Hellwig024910c2010-02-17 19:34:57 +0000178 error = _xfs_log_force_lsn(ip->i_mount,
179 ip->i_itemp->ili_last_lsn,
180 XFS_LOG_SYNC, &log_flushed);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000181 }
Christoph Hellwig024910c2010-02-17 19:34:57 +0000182 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000183 }
184
185 if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
186 /*
187 * If the log write didn't issue an ordered tag we need
188 * to flush the disk cache for the data device now.
189 */
190 if (!log_flushed)
191 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
192
193 /*
194 * If this inode is on the RT dev we need to flush that
195 * cache as well.
196 */
197 if (XFS_IS_REALTIME_INODE(ip))
198 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
199 }
200
201 return -error;
202}
203
Christoph Hellwig00258e32010-02-15 09:44:47 +0000204STATIC ssize_t
205xfs_file_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000206 struct kiocb *iocb,
207 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000208 unsigned long nr_segs,
209 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000210{
211 struct file *file = iocb->ki_filp;
212 struct inode *inode = file->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000213 struct xfs_inode *ip = XFS_I(inode);
214 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000215 size_t size = 0;
216 ssize_t ret = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000217 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000218 xfs_fsize_t n;
219 unsigned long seg;
220
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000221 XFS_STATS_INC(xs_read_calls);
222
Christoph Hellwig00258e32010-02-15 09:44:47 +0000223 BUG_ON(iocb->ki_pos != pos);
224
225 if (unlikely(file->f_flags & O_DIRECT))
226 ioflags |= IO_ISDIRECT;
227 if (file->f_mode & FMODE_NOCMTIME)
228 ioflags |= IO_INVIS;
229
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000230 /* START copy & waste from filemap.c */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000231 for (seg = 0; seg < nr_segs; seg++) {
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000232 const struct iovec *iv = &iovp[seg];
233
234 /*
235 * If any segment has a negative length, or the cumulative
236 * length ever wraps negative then return -EINVAL.
237 */
238 size += iv->iov_len;
239 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
240 return XFS_ERROR(-EINVAL);
241 }
242 /* END copy & waste from filemap.c */
243
244 if (unlikely(ioflags & IO_ISDIRECT)) {
245 xfs_buftarg_t *target =
246 XFS_IS_REALTIME_INODE(ip) ?
247 mp->m_rtdev_targp : mp->m_ddev_targp;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000248 if ((iocb->ki_pos & target->bt_smask) ||
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000249 (size & target->bt_smask)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000250 if (iocb->ki_pos == ip->i_size)
251 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000252 return -XFS_ERROR(EINVAL);
253 }
254 }
255
Christoph Hellwig00258e32010-02-15 09:44:47 +0000256 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
257 if (n <= 0 || size == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000258 return 0;
259
260 if (n < size)
261 size = n;
262
263 if (XFS_FORCED_SHUTDOWN(mp))
264 return -EIO;
265
266 if (unlikely(ioflags & IO_ISDIRECT))
267 mutex_lock(&inode->i_mutex);
268 xfs_ilock(ip, XFS_IOLOCK_SHARED);
269
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000270 if (unlikely(ioflags & IO_ISDIRECT)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000271 if (inode->i_mapping->nrpages) {
272 ret = -xfs_flushinval_pages(ip,
273 (iocb->ki_pos & PAGE_CACHE_MASK),
274 -1, FI_REMAPF_LOCKED);
275 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000276 mutex_unlock(&inode->i_mutex);
277 if (ret) {
278 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
279 return ret;
280 }
281 }
282
Christoph Hellwig00258e32010-02-15 09:44:47 +0000283 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000284
Christoph Hellwig00258e32010-02-15 09:44:47 +0000285 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000286 if (ret > 0)
287 XFS_STATS_ADD(xs_read_bytes, ret);
288
289 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
290 return ret;
291}
292
Christoph Hellwig00258e32010-02-15 09:44:47 +0000293STATIC ssize_t
294xfs_file_splice_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000295 struct file *infilp,
296 loff_t *ppos,
297 struct pipe_inode_info *pipe,
298 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000299 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000300{
Christoph Hellwig00258e32010-02-15 09:44:47 +0000301 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000302 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000303 ssize_t ret;
304
305 XFS_STATS_INC(xs_read_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000306
307 if (infilp->f_mode & FMODE_NOCMTIME)
308 ioflags |= IO_INVIS;
309
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000310 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
311 return -EIO;
312
313 xfs_ilock(ip, XFS_IOLOCK_SHARED);
314
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000315 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
316
317 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
318 if (ret > 0)
319 XFS_STATS_ADD(xs_read_bytes, ret);
320
321 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
322 return ret;
323}
324
Christoph Hellwig00258e32010-02-15 09:44:47 +0000325STATIC ssize_t
326xfs_file_splice_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000327 struct pipe_inode_info *pipe,
328 struct file *outfilp,
329 loff_t *ppos,
330 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000331 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000332{
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000333 struct inode *inode = outfilp->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000334 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000335 xfs_fsize_t isize, new_size;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000336 int ioflags = 0;
337 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000338
339 XFS_STATS_INC(xs_write_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000340
341 if (outfilp->f_mode & FMODE_NOCMTIME)
342 ioflags |= IO_INVIS;
343
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000344 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
345 return -EIO;
346
347 xfs_ilock(ip, XFS_IOLOCK_EXCL);
348
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000349 new_size = *ppos + count;
350
351 xfs_ilock(ip, XFS_ILOCK_EXCL);
352 if (new_size > ip->i_size)
353 ip->i_new_size = new_size;
354 xfs_iunlock(ip, XFS_ILOCK_EXCL);
355
356 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
357
358 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
359 if (ret > 0)
360 XFS_STATS_ADD(xs_write_bytes, ret);
361
362 isize = i_size_read(inode);
363 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
364 *ppos = isize;
365
366 if (*ppos > ip->i_size) {
367 xfs_ilock(ip, XFS_ILOCK_EXCL);
368 if (*ppos > ip->i_size)
369 ip->i_size = *ppos;
370 xfs_iunlock(ip, XFS_ILOCK_EXCL);
371 }
372
373 if (ip->i_new_size) {
374 xfs_ilock(ip, XFS_ILOCK_EXCL);
375 ip->i_new_size = 0;
376 if (ip->i_d.di_size > ip->i_size)
377 ip->i_d.di_size = ip->i_size;
378 xfs_iunlock(ip, XFS_ILOCK_EXCL);
379 }
380 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
381 return ret;
382}
383
384/*
385 * This routine is called to handle zeroing any space in the last
386 * block of the file that is beyond the EOF. We do this since the
387 * size is being increased without writing anything to that block
388 * and we don't want anyone to read the garbage on the disk.
389 */
390STATIC int /* error (positive) */
391xfs_zero_last_block(
392 xfs_inode_t *ip,
393 xfs_fsize_t offset,
394 xfs_fsize_t isize)
395{
396 xfs_fileoff_t last_fsb;
397 xfs_mount_t *mp = ip->i_mount;
398 int nimaps;
399 int zero_offset;
400 int zero_len;
401 int error = 0;
402 xfs_bmbt_irec_t imap;
403
404 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
405
406 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
407 if (zero_offset == 0) {
408 /*
409 * There are no extra bytes in the last block on disk to
410 * zero, so return.
411 */
412 return 0;
413 }
414
415 last_fsb = XFS_B_TO_FSBT(mp, isize);
416 nimaps = 1;
417 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
418 &nimaps, NULL, NULL);
419 if (error) {
420 return error;
421 }
422 ASSERT(nimaps > 0);
423 /*
424 * If the block underlying isize is just a hole, then there
425 * is nothing to zero.
426 */
427 if (imap.br_startblock == HOLESTARTBLOCK) {
428 return 0;
429 }
430 /*
431 * Zero the part of the last block beyond the EOF, and write it
432 * out sync. We need to drop the ilock while we do this so we
433 * don't deadlock when the buffer cache calls back to us.
434 */
435 xfs_iunlock(ip, XFS_ILOCK_EXCL);
436
437 zero_len = mp->m_sb.sb_blocksize - zero_offset;
438 if (isize + zero_len > offset)
439 zero_len = offset - isize;
440 error = xfs_iozero(ip, isize, zero_len);
441
442 xfs_ilock(ip, XFS_ILOCK_EXCL);
443 ASSERT(error >= 0);
444 return error;
445}
446
447/*
448 * Zero any on disk space between the current EOF and the new,
449 * larger EOF. This handles the normal case of zeroing the remainder
450 * of the last block in the file and the unusual case of zeroing blocks
451 * out beyond the size of the file. This second case only happens
452 * with fixed size extents and when the system crashes before the inode
453 * size was updated but after blocks were allocated. If fill is set,
454 * then any holes in the range are filled and zeroed. If not, the holes
455 * are left alone as holes.
456 */
457
458int /* error (positive) */
459xfs_zero_eof(
460 xfs_inode_t *ip,
461 xfs_off_t offset, /* starting I/O offset */
462 xfs_fsize_t isize) /* current inode size */
463{
464 xfs_mount_t *mp = ip->i_mount;
465 xfs_fileoff_t start_zero_fsb;
466 xfs_fileoff_t end_zero_fsb;
467 xfs_fileoff_t zero_count_fsb;
468 xfs_fileoff_t last_fsb;
469 xfs_fileoff_t zero_off;
470 xfs_fsize_t zero_len;
471 int nimaps;
472 int error = 0;
473 xfs_bmbt_irec_t imap;
474
475 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
476 ASSERT(offset > isize);
477
478 /*
479 * First handle zeroing the block on which isize resides.
480 * We only zero a part of that block so it is handled specially.
481 */
482 error = xfs_zero_last_block(ip, offset, isize);
483 if (error) {
484 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
485 return error;
486 }
487
488 /*
489 * Calculate the range between the new size and the old
490 * where blocks needing to be zeroed may exist. To get the
491 * block where the last byte in the file currently resides,
492 * we need to subtract one from the size and truncate back
493 * to a block boundary. We subtract 1 in case the size is
494 * exactly on a block boundary.
495 */
496 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
497 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
498 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
499 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
500 if (last_fsb == end_zero_fsb) {
501 /*
502 * The size was only incremented on its last block.
503 * We took care of that above, so just return.
504 */
505 return 0;
506 }
507
508 ASSERT(start_zero_fsb <= end_zero_fsb);
509 while (start_zero_fsb <= end_zero_fsb) {
510 nimaps = 1;
511 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
512 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
513 0, NULL, 0, &imap, &nimaps, NULL, NULL);
514 if (error) {
515 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
516 return error;
517 }
518 ASSERT(nimaps > 0);
519
520 if (imap.br_state == XFS_EXT_UNWRITTEN ||
521 imap.br_startblock == HOLESTARTBLOCK) {
522 /*
523 * This loop handles initializing pages that were
524 * partially initialized by the code below this
525 * loop. It basically zeroes the part of the page
526 * that sits on a hole and sets the page as P_HOLE
527 * and calls remapf if it is a mapped file.
528 */
529 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
530 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
531 continue;
532 }
533
534 /*
535 * There are blocks we need to zero.
536 * Drop the inode lock while we're doing the I/O.
537 * We'll still have the iolock to protect us.
538 */
539 xfs_iunlock(ip, XFS_ILOCK_EXCL);
540
541 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
542 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
543
544 if ((zero_off + zero_len) > offset)
545 zero_len = offset - zero_off;
546
547 error = xfs_iozero(ip, zero_off, zero_len);
548 if (error) {
549 goto out_lock;
550 }
551
552 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
553 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
554
555 xfs_ilock(ip, XFS_ILOCK_EXCL);
556 }
557
558 return 0;
559
560out_lock:
561 xfs_ilock(ip, XFS_ILOCK_EXCL);
562 ASSERT(error >= 0);
563 return error;
564}
565
Christoph Hellwig00258e32010-02-15 09:44:47 +0000566STATIC ssize_t
567xfs_file_aio_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000568 struct kiocb *iocb,
569 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000570 unsigned long nr_segs,
571 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000572{
573 struct file *file = iocb->ki_filp;
574 struct address_space *mapping = file->f_mapping;
575 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000576 struct xfs_inode *ip = XFS_I(inode);
577 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000578 ssize_t ret = 0, error = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000579 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000580 xfs_fsize_t isize, new_size;
581 int iolock;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000582 size_t ocount = 0, count;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000583 int need_i_mutex;
584
585 XFS_STATS_INC(xs_write_calls);
586
Christoph Hellwig00258e32010-02-15 09:44:47 +0000587 BUG_ON(iocb->ki_pos != pos);
588
589 if (unlikely(file->f_flags & O_DIRECT))
590 ioflags |= IO_ISDIRECT;
591 if (file->f_mode & FMODE_NOCMTIME)
592 ioflags |= IO_INVIS;
593
594 error = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000595 if (error)
596 return error;
597
598 count = ocount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000599 if (count == 0)
600 return 0;
601
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000602 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
603
604 if (XFS_FORCED_SHUTDOWN(mp))
605 return -EIO;
606
607relock:
608 if (ioflags & IO_ISDIRECT) {
609 iolock = XFS_IOLOCK_SHARED;
610 need_i_mutex = 0;
611 } else {
612 iolock = XFS_IOLOCK_EXCL;
613 need_i_mutex = 1;
614 mutex_lock(&inode->i_mutex);
615 }
616
Christoph Hellwig00258e32010-02-15 09:44:47 +0000617 xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000618
619start:
620 error = -generic_write_checks(file, &pos, &count,
621 S_ISBLK(inode->i_mode));
622 if (error) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000623 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000624 goto out_unlock_mutex;
625 }
626
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000627 if (ioflags & IO_ISDIRECT) {
628 xfs_buftarg_t *target =
Christoph Hellwig00258e32010-02-15 09:44:47 +0000629 XFS_IS_REALTIME_INODE(ip) ?
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000630 mp->m_rtdev_targp : mp->m_ddev_targp;
631
632 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000633 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000634 return XFS_ERROR(-EINVAL);
635 }
636
Christoph Hellwig00258e32010-02-15 09:44:47 +0000637 if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) {
638 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000639 iolock = XFS_IOLOCK_EXCL;
640 need_i_mutex = 1;
641 mutex_lock(&inode->i_mutex);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000642 xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000643 goto start;
644 }
645 }
646
647 new_size = pos + count;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000648 if (new_size > ip->i_size)
649 ip->i_new_size = new_size;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000650
651 if (likely(!(ioflags & IO_INVIS)))
652 file_update_time(file);
653
654 /*
655 * If the offset is beyond the size of the file, we have a couple
656 * of things to do. First, if there is already space allocated
657 * we need to either create holes or zero the disk or ...
658 *
659 * If there is a page where the previous size lands, we need
660 * to zero it out up to the new size.
661 */
662
Christoph Hellwig00258e32010-02-15 09:44:47 +0000663 if (pos > ip->i_size) {
664 error = xfs_zero_eof(ip, pos, ip->i_size);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000665 if (error) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000666 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000667 goto out_unlock_internal;
668 }
669 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000670 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000671
672 /*
673 * If we're writing the file then make sure to clear the
674 * setuid and setgid bits if the process is not being run
675 * by root. This keeps people from modifying setuid and
676 * setgid binaries.
677 */
678 error = -file_remove_suid(file);
679 if (unlikely(error))
680 goto out_unlock_internal;
681
682 /* We can write back this queue in page reclaim */
683 current->backing_dev_info = mapping->backing_dev_info;
684
685 if ((ioflags & IO_ISDIRECT)) {
686 if (mapping->nrpages) {
687 WARN_ON(need_i_mutex == 0);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000688 error = xfs_flushinval_pages(ip,
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000689 (pos & PAGE_CACHE_MASK),
690 -1, FI_REMAPF_LOCKED);
691 if (error)
692 goto out_unlock_internal;
693 }
694
695 if (need_i_mutex) {
696 /* demote the lock now the cached pages are gone */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000697 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000698 mutex_unlock(&inode->i_mutex);
699
700 iolock = XFS_IOLOCK_SHARED;
701 need_i_mutex = 0;
702 }
703
Christoph Hellwig00258e32010-02-15 09:44:47 +0000704 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000705 ret = generic_file_direct_write(iocb, iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000706 &nr_segs, pos, &iocb->ki_pos, count, ocount);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000707
708 /*
709 * direct-io write to a hole: fall through to buffered I/O
710 * for completing the rest of the request.
711 */
712 if (ret >= 0 && ret != count) {
713 XFS_STATS_ADD(xs_write_bytes, ret);
714
715 pos += ret;
716 count -= ret;
717
718 ioflags &= ~IO_ISDIRECT;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000719 xfs_iunlock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000720 goto relock;
721 }
722 } else {
723 int enospc = 0;
724 ssize_t ret2 = 0;
725
726write_retry:
Christoph Hellwig00258e32010-02-15 09:44:47 +0000727 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, ioflags);
728 ret2 = generic_file_buffered_write(iocb, iovp, nr_segs,
729 pos, &iocb->ki_pos, count, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000730 /*
731 * if we just got an ENOSPC, flush the inode now we
732 * aren't holding any page locks and retry *once*
733 */
734 if (ret2 == -ENOSPC && !enospc) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000735 error = xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000736 if (error)
737 goto out_unlock_internal;
738 enospc = 1;
739 goto write_retry;
740 }
741 ret = ret2;
742 }
743
744 current->backing_dev_info = NULL;
745
746 isize = i_size_read(inode);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000747 if (unlikely(ret < 0 && ret != -EFAULT && iocb->ki_pos > isize))
748 iocb->ki_pos = isize;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000749
Christoph Hellwig00258e32010-02-15 09:44:47 +0000750 if (iocb->ki_pos > ip->i_size) {
751 xfs_ilock(ip, XFS_ILOCK_EXCL);
752 if (iocb->ki_pos > ip->i_size)
753 ip->i_size = iocb->ki_pos;
754 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000755 }
756
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000757 error = -ret;
758 if (ret <= 0)
759 goto out_unlock_internal;
760
761 XFS_STATS_ADD(xs_write_bytes, ret);
762
763 /* Handle various SYNC-type writes */
764 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
765 loff_t end = pos + ret - 1;
766 int error2;
767
Christoph Hellwig00258e32010-02-15 09:44:47 +0000768 xfs_iunlock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000769 if (need_i_mutex)
770 mutex_unlock(&inode->i_mutex);
771
772 error2 = filemap_write_and_wait_range(mapping, pos, end);
773 if (!error)
774 error = error2;
775 if (need_i_mutex)
776 mutex_lock(&inode->i_mutex);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000777 xfs_ilock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000778
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200779 error2 = -xfs_file_fsync(file,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000780 (file->f_flags & __O_SYNC) ? 0 : 1);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000781 if (!error)
782 error = error2;
783 }
784
785 out_unlock_internal:
Christoph Hellwig00258e32010-02-15 09:44:47 +0000786 if (ip->i_new_size) {
787 xfs_ilock(ip, XFS_ILOCK_EXCL);
788 ip->i_new_size = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000789 /*
790 * If this was a direct or synchronous I/O that failed (such
791 * as ENOSPC) then part of the I/O may have been written to
792 * disk before the error occured. In this case the on-disk
793 * file size may have been adjusted beyond the in-memory file
794 * size and now needs to be truncated back.
795 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000796 if (ip->i_d.di_size > ip->i_size)
797 ip->i_d.di_size = ip->i_size;
798 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000799 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000800 xfs_iunlock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000801 out_unlock_mutex:
802 if (need_i_mutex)
803 mutex_unlock(&inode->i_mutex);
804 return -error;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100808xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100810 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100812 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100814 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
815 return -EIO;
816 return 0;
817}
818
819STATIC int
820xfs_dir_open(
821 struct inode *inode,
822 struct file *file)
823{
824 struct xfs_inode *ip = XFS_I(inode);
825 int mode;
826 int error;
827
828 error = xfs_file_open(inode, file);
829 if (error)
830 return error;
831
832 /*
833 * If there are any blocks, read-ahead block 0 as we're almost
834 * certain to have the next operation be a read there.
835 */
836 mode = xfs_ilock_map_shared(ip);
837 if (ip->i_d.di_nextents > 0)
838 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
839 xfs_iunlock(ip, mode);
840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100844xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct inode *inode,
846 struct file *filp)
847{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000848 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100852xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct file *filp,
854 void *dirent,
855 filldir_t filldir)
856{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000857 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000858 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000859 int error;
860 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000862 /*
863 * The Linux API doesn't pass down the total size of the buffer
864 * we read into down to the filesystem. With the filldir concept
865 * it's not needed for correct information, but the XFS dir2 leaf
866 * code wants an estimate of the buffer size to calculate it's
867 * readahead window and size the buffers used for mapping to
868 * physical blocks.
869 *
870 * Try to give it an estimate that's good enough, maybe at some
871 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +0000872 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000873 */
Eric Sandeena9cc7992010-02-03 17:50:13 +0000874 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000876 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000877 (xfs_off_t *)&filp->f_pos, filldir);
878 if (error)
879 return -error;
880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100884xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct file *filp,
886 struct vm_area_struct *vma)
887{
Nathan Scott3562fd42006-03-14 14:00:35 +1100888 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -0700889 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000890
Nathan Scottfbc14622006-06-09 14:52:13 +1000891 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return 0;
893}
894
David Chinner4f57dbc2007-07-19 16:28:17 +1000895/*
896 * mmap()d file has taken write protection fault and is being made
897 * writable. We can set the page state up correctly for a writable
898 * page, which means we can do correct delalloc accounting (ENOSPC
899 * checking!) and unwritten extent mapping.
900 */
901STATIC int
902xfs_vm_page_mkwrite(
903 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -0700904 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +1000905{
Nick Pigginc2ec1752009-03-31 15:23:21 -0700906 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +1000907}
908
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800909const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 .llseek = generic_file_llseek,
911 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000912 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +1100913 .aio_read = xfs_file_aio_read,
914 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +1000915 .splice_read = xfs_file_splice_read,
916 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +1100917 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +1100919 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920#endif
Nathan Scott3562fd42006-03-14 14:00:35 +1100921 .mmap = xfs_file_mmap,
922 .open = xfs_file_open,
923 .release = xfs_file_release,
924 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925#ifdef HAVE_FOP_OPEN_EXEC
Nathan Scott3562fd42006-03-14 14:00:35 +1100926 .open_exec = xfs_file_open_exec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927#endif
928};
929
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800930const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100931 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +1100933 .readdir = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -0400934 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +1100935 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -0700936#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +1100937 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -0700938#endif
Nathan Scott3562fd42006-03-14 14:00:35 +1100939 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940};
941
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400942static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -0700943 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +1000944 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000945};