blob: 42dd3bcfba6b89e6afe21c02b6bac98f9544d0cb [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_dir2.h"
26#include "xfs_trans.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_alloc_btree.h"
31#include "xfs_ialloc_btree.h"
32#include "xfs_alloc.h"
33#include "xfs_btree.h"
34#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000038#include "xfs_inode_item.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000039#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_error.h"
41#include "xfs_rw.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100042#include "xfs_vnodeops.h"
Christoph Hellwigf999a5b2008-11-28 14:23:32 +110043#include "xfs_da_btree.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050044#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000045#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040049static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Hellwigdda35b82010-02-15 09:44:46 +000051/*
52 * xfs_iozero
53 *
54 * xfs_iozero clears the specified range of buffer supplied,
55 * and marks all the affected blocks as valid and modified. If
56 * an affected block is not allocated, it will be allocated. If
57 * an affected block is not completely overwritten, and is not
58 * valid before the operation, it will be read from disk before
59 * being partially zeroed.
60 */
61STATIC int
62xfs_iozero(
63 struct xfs_inode *ip, /* inode */
64 loff_t pos, /* offset in file */
65 size_t count) /* size of data to zero */
66{
67 struct page *page;
68 struct address_space *mapping;
69 int status;
70
71 mapping = VFS_I(ip)->i_mapping;
72 do {
73 unsigned offset, bytes;
74 void *fsdata;
75
76 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
77 bytes = PAGE_CACHE_SIZE - offset;
78 if (bytes > count)
79 bytes = count;
80
81 status = pagecache_write_begin(NULL, mapping, pos, bytes,
82 AOP_FLAG_UNINTERRUPTIBLE,
83 &page, &fsdata);
84 if (status)
85 break;
86
87 zero_user(page, offset, bytes);
88
89 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
90 page, fsdata);
91 WARN_ON(status <= 0); /* can't return less than zero! */
92 pos += bytes;
93 count -= bytes;
94 status = 0;
95 } while (count);
96
97 return (-status);
98}
99
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000100STATIC int
101xfs_file_fsync(
102 struct file *file,
103 struct dentry *dentry,
104 int datasync)
105{
106 struct xfs_inode *ip = XFS_I(dentry->d_inode);
107 struct xfs_trans *tp;
108 int error = 0;
109 int log_flushed = 0;
110
111 xfs_itrace_entry(ip);
112
113 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
114 return -XFS_ERROR(EIO);
115
116 xfs_iflags_clear(ip, XFS_ITRUNCATED);
117
118 /*
119 * We always need to make sure that the required inode state is safe on
120 * disk. The inode might be clean but we still might need to force the
121 * log because of committed transactions that haven't hit the disk yet.
122 * Likewise, there could be unflushed non-transactional changes to the
123 * inode core that have to go to disk and this requires us to issue
124 * a synchronous transaction to capture these changes correctly.
125 *
126 * This code relies on the assumption that if the i_update_core field
127 * of the inode is clear and the inode is unpinned then it is clean
128 * and no action is required.
129 */
130 xfs_ilock(ip, XFS_ILOCK_SHARED);
131
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000132 /*
133 * First check if the VFS inode is marked dirty. All the dirtying
134 * of non-transactional updates no goes through mark_inode_dirty*,
135 * which allows us to distinguish beteeen pure timestamp updates
136 * and i_size updates which need to be caught for fdatasync.
137 * After that also theck for the dirty state in the XFS inode, which
138 * might gets cleared when the inode gets written out via the AIL
139 * or xfs_iflush_cluster.
140 */
141 if (((dentry->d_inode->i_state & I_DIRTY_DATASYNC) ||
142 ((dentry->d_inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
143 ip->i_update_core) {
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000144 /*
145 * Kick off a transaction to log the inode core to get the
146 * updates. The sync transaction will also force the log.
147 */
148 xfs_iunlock(ip, XFS_ILOCK_SHARED);
149 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
150 error = xfs_trans_reserve(tp, 0,
151 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
152 if (error) {
153 xfs_trans_cancel(tp, 0);
154 return -error;
155 }
156 xfs_ilock(ip, XFS_ILOCK_EXCL);
157
158 /*
159 * Note - it's possible that we might have pushed ourselves out
160 * of the way during trans_reserve which would flush the inode.
161 * But there's no guarantee that the inode buffer has actually
162 * gone out yet (it's delwri). Plus the buffer could be pinned
163 * anyway if it's part of an inode in another recent
164 * transaction. So we play it safe and fire off the
165 * transaction anyway.
166 */
167 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
168 xfs_trans_ihold(tp, ip);
169 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
170 xfs_trans_set_sync(tp);
171 error = _xfs_trans_commit(tp, 0, &log_flushed);
172
173 xfs_iunlock(ip, XFS_ILOCK_EXCL);
174 } else {
175 /*
176 * Timestamps/size haven't changed since last inode flush or
177 * inode transaction commit. That means either nothing got
178 * written or a transaction committed which caught the updates.
179 * If the latter happened and the transaction hasn't hit the
180 * disk yet, the inode will be still be pinned. If it is,
181 * force the log.
182 */
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000183 if (xfs_ipincount(ip)) {
Christoph Hellwig024910c2010-02-17 19:34:57 +0000184 error = _xfs_log_force_lsn(ip->i_mount,
185 ip->i_itemp->ili_last_lsn,
186 XFS_LOG_SYNC, &log_flushed);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000187 }
Christoph Hellwig024910c2010-02-17 19:34:57 +0000188 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000189 }
190
191 if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
192 /*
193 * If the log write didn't issue an ordered tag we need
194 * to flush the disk cache for the data device now.
195 */
196 if (!log_flushed)
197 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
198
199 /*
200 * If this inode is on the RT dev we need to flush that
201 * cache as well.
202 */
203 if (XFS_IS_REALTIME_INODE(ip))
204 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
205 }
206
207 return -error;
208}
209
Christoph Hellwig00258e32010-02-15 09:44:47 +0000210STATIC ssize_t
211xfs_file_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000212 struct kiocb *iocb,
213 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000214 unsigned long nr_segs,
215 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000216{
217 struct file *file = iocb->ki_filp;
218 struct inode *inode = file->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000219 struct xfs_inode *ip = XFS_I(inode);
220 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000221 size_t size = 0;
222 ssize_t ret = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000223 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000224 xfs_fsize_t n;
225 unsigned long seg;
226
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000227 XFS_STATS_INC(xs_read_calls);
228
Christoph Hellwig00258e32010-02-15 09:44:47 +0000229 BUG_ON(iocb->ki_pos != pos);
230
231 if (unlikely(file->f_flags & O_DIRECT))
232 ioflags |= IO_ISDIRECT;
233 if (file->f_mode & FMODE_NOCMTIME)
234 ioflags |= IO_INVIS;
235
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000236 /* START copy & waste from filemap.c */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000237 for (seg = 0; seg < nr_segs; seg++) {
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000238 const struct iovec *iv = &iovp[seg];
239
240 /*
241 * If any segment has a negative length, or the cumulative
242 * length ever wraps negative then return -EINVAL.
243 */
244 size += iv->iov_len;
245 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
246 return XFS_ERROR(-EINVAL);
247 }
248 /* END copy & waste from filemap.c */
249
250 if (unlikely(ioflags & IO_ISDIRECT)) {
251 xfs_buftarg_t *target =
252 XFS_IS_REALTIME_INODE(ip) ?
253 mp->m_rtdev_targp : mp->m_ddev_targp;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000254 if ((iocb->ki_pos & target->bt_smask) ||
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000255 (size & target->bt_smask)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000256 if (iocb->ki_pos == ip->i_size)
257 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000258 return -XFS_ERROR(EINVAL);
259 }
260 }
261
Christoph Hellwig00258e32010-02-15 09:44:47 +0000262 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
263 if (n <= 0 || size == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000264 return 0;
265
266 if (n < size)
267 size = n;
268
269 if (XFS_FORCED_SHUTDOWN(mp))
270 return -EIO;
271
272 if (unlikely(ioflags & IO_ISDIRECT))
273 mutex_lock(&inode->i_mutex);
274 xfs_ilock(ip, XFS_IOLOCK_SHARED);
275
276 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
277 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
278 int iolock = XFS_IOLOCK_SHARED;
279
Christoph Hellwig00258e32010-02-15 09:44:47 +0000280 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, iocb->ki_pos, size,
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000281 dmflags, &iolock);
282 if (ret) {
283 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
284 if (unlikely(ioflags & IO_ISDIRECT))
285 mutex_unlock(&inode->i_mutex);
286 return ret;
287 }
288 }
289
290 if (unlikely(ioflags & IO_ISDIRECT)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000291 if (inode->i_mapping->nrpages) {
292 ret = -xfs_flushinval_pages(ip,
293 (iocb->ki_pos & PAGE_CACHE_MASK),
294 -1, FI_REMAPF_LOCKED);
295 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000296 mutex_unlock(&inode->i_mutex);
297 if (ret) {
298 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
299 return ret;
300 }
301 }
302
Christoph Hellwig00258e32010-02-15 09:44:47 +0000303 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000304
Christoph Hellwig00258e32010-02-15 09:44:47 +0000305 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000306 if (ret > 0)
307 XFS_STATS_ADD(xs_read_bytes, ret);
308
309 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
310 return ret;
311}
312
Christoph Hellwig00258e32010-02-15 09:44:47 +0000313STATIC ssize_t
314xfs_file_splice_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000315 struct file *infilp,
316 loff_t *ppos,
317 struct pipe_inode_info *pipe,
318 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000319 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000320{
Christoph Hellwig00258e32010-02-15 09:44:47 +0000321 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
322 struct xfs_mount *mp = ip->i_mount;
323 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000324 ssize_t ret;
325
326 XFS_STATS_INC(xs_read_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000327
328 if (infilp->f_mode & FMODE_NOCMTIME)
329 ioflags |= IO_INVIS;
330
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000331 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
332 return -EIO;
333
334 xfs_ilock(ip, XFS_IOLOCK_SHARED);
335
336 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
337 int iolock = XFS_IOLOCK_SHARED;
338 int error;
339
340 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
341 FILP_DELAY_FLAG(infilp), &iolock);
342 if (error) {
343 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
344 return -error;
345 }
346 }
347
348 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
349
350 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
351 if (ret > 0)
352 XFS_STATS_ADD(xs_read_bytes, ret);
353
354 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
355 return ret;
356}
357
Christoph Hellwig00258e32010-02-15 09:44:47 +0000358STATIC ssize_t
359xfs_file_splice_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000360 struct pipe_inode_info *pipe,
361 struct file *outfilp,
362 loff_t *ppos,
363 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000364 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000365{
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000366 struct inode *inode = outfilp->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000367 struct xfs_inode *ip = XFS_I(inode);
368 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000369 xfs_fsize_t isize, new_size;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000370 int ioflags = 0;
371 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000372
373 XFS_STATS_INC(xs_write_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000374
375 if (outfilp->f_mode & FMODE_NOCMTIME)
376 ioflags |= IO_INVIS;
377
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000378 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
379 return -EIO;
380
381 xfs_ilock(ip, XFS_IOLOCK_EXCL);
382
383 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
384 int iolock = XFS_IOLOCK_EXCL;
385 int error;
386
387 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
388 FILP_DELAY_FLAG(outfilp), &iolock);
389 if (error) {
390 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
391 return -error;
392 }
393 }
394
395 new_size = *ppos + count;
396
397 xfs_ilock(ip, XFS_ILOCK_EXCL);
398 if (new_size > ip->i_size)
399 ip->i_new_size = new_size;
400 xfs_iunlock(ip, XFS_ILOCK_EXCL);
401
402 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
403
404 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
405 if (ret > 0)
406 XFS_STATS_ADD(xs_write_bytes, ret);
407
408 isize = i_size_read(inode);
409 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
410 *ppos = isize;
411
412 if (*ppos > ip->i_size) {
413 xfs_ilock(ip, XFS_ILOCK_EXCL);
414 if (*ppos > ip->i_size)
415 ip->i_size = *ppos;
416 xfs_iunlock(ip, XFS_ILOCK_EXCL);
417 }
418
419 if (ip->i_new_size) {
420 xfs_ilock(ip, XFS_ILOCK_EXCL);
421 ip->i_new_size = 0;
422 if (ip->i_d.di_size > ip->i_size)
423 ip->i_d.di_size = ip->i_size;
424 xfs_iunlock(ip, XFS_ILOCK_EXCL);
425 }
426 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
427 return ret;
428}
429
430/*
431 * This routine is called to handle zeroing any space in the last
432 * block of the file that is beyond the EOF. We do this since the
433 * size is being increased without writing anything to that block
434 * and we don't want anyone to read the garbage on the disk.
435 */
436STATIC int /* error (positive) */
437xfs_zero_last_block(
438 xfs_inode_t *ip,
439 xfs_fsize_t offset,
440 xfs_fsize_t isize)
441{
442 xfs_fileoff_t last_fsb;
443 xfs_mount_t *mp = ip->i_mount;
444 int nimaps;
445 int zero_offset;
446 int zero_len;
447 int error = 0;
448 xfs_bmbt_irec_t imap;
449
450 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
451
452 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
453 if (zero_offset == 0) {
454 /*
455 * There are no extra bytes in the last block on disk to
456 * zero, so return.
457 */
458 return 0;
459 }
460
461 last_fsb = XFS_B_TO_FSBT(mp, isize);
462 nimaps = 1;
463 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
464 &nimaps, NULL, NULL);
465 if (error) {
466 return error;
467 }
468 ASSERT(nimaps > 0);
469 /*
470 * If the block underlying isize is just a hole, then there
471 * is nothing to zero.
472 */
473 if (imap.br_startblock == HOLESTARTBLOCK) {
474 return 0;
475 }
476 /*
477 * Zero the part of the last block beyond the EOF, and write it
478 * out sync. We need to drop the ilock while we do this so we
479 * don't deadlock when the buffer cache calls back to us.
480 */
481 xfs_iunlock(ip, XFS_ILOCK_EXCL);
482
483 zero_len = mp->m_sb.sb_blocksize - zero_offset;
484 if (isize + zero_len > offset)
485 zero_len = offset - isize;
486 error = xfs_iozero(ip, isize, zero_len);
487
488 xfs_ilock(ip, XFS_ILOCK_EXCL);
489 ASSERT(error >= 0);
490 return error;
491}
492
493/*
494 * Zero any on disk space between the current EOF and the new,
495 * larger EOF. This handles the normal case of zeroing the remainder
496 * of the last block in the file and the unusual case of zeroing blocks
497 * out beyond the size of the file. This second case only happens
498 * with fixed size extents and when the system crashes before the inode
499 * size was updated but after blocks were allocated. If fill is set,
500 * then any holes in the range are filled and zeroed. If not, the holes
501 * are left alone as holes.
502 */
503
504int /* error (positive) */
505xfs_zero_eof(
506 xfs_inode_t *ip,
507 xfs_off_t offset, /* starting I/O offset */
508 xfs_fsize_t isize) /* current inode size */
509{
510 xfs_mount_t *mp = ip->i_mount;
511 xfs_fileoff_t start_zero_fsb;
512 xfs_fileoff_t end_zero_fsb;
513 xfs_fileoff_t zero_count_fsb;
514 xfs_fileoff_t last_fsb;
515 xfs_fileoff_t zero_off;
516 xfs_fsize_t zero_len;
517 int nimaps;
518 int error = 0;
519 xfs_bmbt_irec_t imap;
520
521 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
522 ASSERT(offset > isize);
523
524 /*
525 * First handle zeroing the block on which isize resides.
526 * We only zero a part of that block so it is handled specially.
527 */
528 error = xfs_zero_last_block(ip, offset, isize);
529 if (error) {
530 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
531 return error;
532 }
533
534 /*
535 * Calculate the range between the new size and the old
536 * where blocks needing to be zeroed may exist. To get the
537 * block where the last byte in the file currently resides,
538 * we need to subtract one from the size and truncate back
539 * to a block boundary. We subtract 1 in case the size is
540 * exactly on a block boundary.
541 */
542 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
543 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
544 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
545 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
546 if (last_fsb == end_zero_fsb) {
547 /*
548 * The size was only incremented on its last block.
549 * We took care of that above, so just return.
550 */
551 return 0;
552 }
553
554 ASSERT(start_zero_fsb <= end_zero_fsb);
555 while (start_zero_fsb <= end_zero_fsb) {
556 nimaps = 1;
557 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
558 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
559 0, NULL, 0, &imap, &nimaps, NULL, NULL);
560 if (error) {
561 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
562 return error;
563 }
564 ASSERT(nimaps > 0);
565
566 if (imap.br_state == XFS_EXT_UNWRITTEN ||
567 imap.br_startblock == HOLESTARTBLOCK) {
568 /*
569 * This loop handles initializing pages that were
570 * partially initialized by the code below this
571 * loop. It basically zeroes the part of the page
572 * that sits on a hole and sets the page as P_HOLE
573 * and calls remapf if it is a mapped file.
574 */
575 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
576 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
577 continue;
578 }
579
580 /*
581 * There are blocks we need to zero.
582 * Drop the inode lock while we're doing the I/O.
583 * We'll still have the iolock to protect us.
584 */
585 xfs_iunlock(ip, XFS_ILOCK_EXCL);
586
587 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
588 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
589
590 if ((zero_off + zero_len) > offset)
591 zero_len = offset - zero_off;
592
593 error = xfs_iozero(ip, zero_off, zero_len);
594 if (error) {
595 goto out_lock;
596 }
597
598 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
599 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
600
601 xfs_ilock(ip, XFS_ILOCK_EXCL);
602 }
603
604 return 0;
605
606out_lock:
607 xfs_ilock(ip, XFS_ILOCK_EXCL);
608 ASSERT(error >= 0);
609 return error;
610}
611
Christoph Hellwig00258e32010-02-15 09:44:47 +0000612STATIC ssize_t
613xfs_file_aio_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000614 struct kiocb *iocb,
615 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000616 unsigned long nr_segs,
617 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000618{
619 struct file *file = iocb->ki_filp;
620 struct address_space *mapping = file->f_mapping;
621 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000622 struct xfs_inode *ip = XFS_I(inode);
623 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000624 ssize_t ret = 0, error = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000625 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000626 xfs_fsize_t isize, new_size;
627 int iolock;
628 int eventsent = 0;
629 size_t ocount = 0, count;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000630 int need_i_mutex;
631
632 XFS_STATS_INC(xs_write_calls);
633
Christoph Hellwig00258e32010-02-15 09:44:47 +0000634 BUG_ON(iocb->ki_pos != pos);
635
636 if (unlikely(file->f_flags & O_DIRECT))
637 ioflags |= IO_ISDIRECT;
638 if (file->f_mode & FMODE_NOCMTIME)
639 ioflags |= IO_INVIS;
640
641 error = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000642 if (error)
643 return error;
644
645 count = ocount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000646 if (count == 0)
647 return 0;
648
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000649 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
650
651 if (XFS_FORCED_SHUTDOWN(mp))
652 return -EIO;
653
654relock:
655 if (ioflags & IO_ISDIRECT) {
656 iolock = XFS_IOLOCK_SHARED;
657 need_i_mutex = 0;
658 } else {
659 iolock = XFS_IOLOCK_EXCL;
660 need_i_mutex = 1;
661 mutex_lock(&inode->i_mutex);
662 }
663
Christoph Hellwig00258e32010-02-15 09:44:47 +0000664 xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000665
666start:
667 error = -generic_write_checks(file, &pos, &count,
668 S_ISBLK(inode->i_mode));
669 if (error) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000670 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000671 goto out_unlock_mutex;
672 }
673
Christoph Hellwig00258e32010-02-15 09:44:47 +0000674 if ((DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) &&
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000675 !(ioflags & IO_INVIS) && !eventsent)) {
676 int dmflags = FILP_DELAY_FLAG(file);
677
678 if (need_i_mutex)
679 dmflags |= DM_FLAGS_IMUX;
680
Christoph Hellwig00258e32010-02-15 09:44:47 +0000681 xfs_iunlock(ip, XFS_ILOCK_EXCL);
682 error = XFS_SEND_DATA(ip->i_mount, DM_EVENT_WRITE, ip,
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000683 pos, count, dmflags, &iolock);
684 if (error) {
685 goto out_unlock_internal;
686 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000687 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000688 eventsent = 1;
689
690 /*
691 * The iolock was dropped and reacquired in XFS_SEND_DATA
692 * so we have to recheck the size when appending.
693 * We will only "goto start;" once, since having sent the
694 * event prevents another call to XFS_SEND_DATA, which is
695 * what allows the size to change in the first place.
696 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000697 if ((file->f_flags & O_APPEND) && pos != ip->i_size)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000698 goto start;
699 }
700
701 if (ioflags & IO_ISDIRECT) {
702 xfs_buftarg_t *target =
Christoph Hellwig00258e32010-02-15 09:44:47 +0000703 XFS_IS_REALTIME_INODE(ip) ?
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000704 mp->m_rtdev_targp : mp->m_ddev_targp;
705
706 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000707 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000708 return XFS_ERROR(-EINVAL);
709 }
710
Christoph Hellwig00258e32010-02-15 09:44:47 +0000711 if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) {
712 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000713 iolock = XFS_IOLOCK_EXCL;
714 need_i_mutex = 1;
715 mutex_lock(&inode->i_mutex);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000716 xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000717 goto start;
718 }
719 }
720
721 new_size = pos + count;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000722 if (new_size > ip->i_size)
723 ip->i_new_size = new_size;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000724
725 if (likely(!(ioflags & IO_INVIS)))
726 file_update_time(file);
727
728 /*
729 * If the offset is beyond the size of the file, we have a couple
730 * of things to do. First, if there is already space allocated
731 * we need to either create holes or zero the disk or ...
732 *
733 * If there is a page where the previous size lands, we need
734 * to zero it out up to the new size.
735 */
736
Christoph Hellwig00258e32010-02-15 09:44:47 +0000737 if (pos > ip->i_size) {
738 error = xfs_zero_eof(ip, pos, ip->i_size);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000739 if (error) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000740 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000741 goto out_unlock_internal;
742 }
743 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000744 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000745
746 /*
747 * If we're writing the file then make sure to clear the
748 * setuid and setgid bits if the process is not being run
749 * by root. This keeps people from modifying setuid and
750 * setgid binaries.
751 */
752 error = -file_remove_suid(file);
753 if (unlikely(error))
754 goto out_unlock_internal;
755
756 /* We can write back this queue in page reclaim */
757 current->backing_dev_info = mapping->backing_dev_info;
758
759 if ((ioflags & IO_ISDIRECT)) {
760 if (mapping->nrpages) {
761 WARN_ON(need_i_mutex == 0);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000762 error = xfs_flushinval_pages(ip,
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000763 (pos & PAGE_CACHE_MASK),
764 -1, FI_REMAPF_LOCKED);
765 if (error)
766 goto out_unlock_internal;
767 }
768
769 if (need_i_mutex) {
770 /* demote the lock now the cached pages are gone */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000771 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000772 mutex_unlock(&inode->i_mutex);
773
774 iolock = XFS_IOLOCK_SHARED;
775 need_i_mutex = 0;
776 }
777
Christoph Hellwig00258e32010-02-15 09:44:47 +0000778 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000779 ret = generic_file_direct_write(iocb, iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000780 &nr_segs, pos, &iocb->ki_pos, count, ocount);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000781
782 /*
783 * direct-io write to a hole: fall through to buffered I/O
784 * for completing the rest of the request.
785 */
786 if (ret >= 0 && ret != count) {
787 XFS_STATS_ADD(xs_write_bytes, ret);
788
789 pos += ret;
790 count -= ret;
791
792 ioflags &= ~IO_ISDIRECT;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000793 xfs_iunlock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000794 goto relock;
795 }
796 } else {
797 int enospc = 0;
798 ssize_t ret2 = 0;
799
800write_retry:
Christoph Hellwig00258e32010-02-15 09:44:47 +0000801 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, ioflags);
802 ret2 = generic_file_buffered_write(iocb, iovp, nr_segs,
803 pos, &iocb->ki_pos, count, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000804 /*
805 * if we just got an ENOSPC, flush the inode now we
806 * aren't holding any page locks and retry *once*
807 */
808 if (ret2 == -ENOSPC && !enospc) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000809 error = xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000810 if (error)
811 goto out_unlock_internal;
812 enospc = 1;
813 goto write_retry;
814 }
815 ret = ret2;
816 }
817
818 current->backing_dev_info = NULL;
819
820 isize = i_size_read(inode);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000821 if (unlikely(ret < 0 && ret != -EFAULT && iocb->ki_pos > isize))
822 iocb->ki_pos = isize;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000823
Christoph Hellwig00258e32010-02-15 09:44:47 +0000824 if (iocb->ki_pos > ip->i_size) {
825 xfs_ilock(ip, XFS_ILOCK_EXCL);
826 if (iocb->ki_pos > ip->i_size)
827 ip->i_size = iocb->ki_pos;
828 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000829 }
830
831 if (ret == -ENOSPC &&
Christoph Hellwig00258e32010-02-15 09:44:47 +0000832 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
833 xfs_iunlock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000834 if (need_i_mutex)
835 mutex_unlock(&inode->i_mutex);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000836 error = XFS_SEND_NAMESP(ip->i_mount, DM_EVENT_NOSPACE, ip,
837 DM_RIGHT_NULL, ip, DM_RIGHT_NULL, NULL, NULL,
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000838 0, 0, 0); /* Delay flag intentionally unused */
839 if (need_i_mutex)
840 mutex_lock(&inode->i_mutex);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000841 xfs_ilock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000842 if (error)
843 goto out_unlock_internal;
844 goto start;
845 }
846
847 error = -ret;
848 if (ret <= 0)
849 goto out_unlock_internal;
850
851 XFS_STATS_ADD(xs_write_bytes, ret);
852
853 /* Handle various SYNC-type writes */
854 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
855 loff_t end = pos + ret - 1;
856 int error2;
857
Christoph Hellwig00258e32010-02-15 09:44:47 +0000858 xfs_iunlock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000859 if (need_i_mutex)
860 mutex_unlock(&inode->i_mutex);
861
862 error2 = filemap_write_and_wait_range(mapping, pos, end);
863 if (!error)
864 error = error2;
865 if (need_i_mutex)
866 mutex_lock(&inode->i_mutex);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000867 xfs_ilock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000868
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000869 error2 = -xfs_file_fsync(file, file->f_path.dentry,
870 (file->f_flags & __O_SYNC) ? 0 : 1);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000871 if (!error)
872 error = error2;
873 }
874
875 out_unlock_internal:
Christoph Hellwig00258e32010-02-15 09:44:47 +0000876 if (ip->i_new_size) {
877 xfs_ilock(ip, XFS_ILOCK_EXCL);
878 ip->i_new_size = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000879 /*
880 * If this was a direct or synchronous I/O that failed (such
881 * as ENOSPC) then part of the I/O may have been written to
882 * disk before the error occured. In this case the on-disk
883 * file size may have been adjusted beyond the in-memory file
884 * size and now needs to be truncated back.
885 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000886 if (ip->i_d.di_size > ip->i_size)
887 ip->i_d.di_size = ip->i_size;
888 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000889 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000890 xfs_iunlock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000891 out_unlock_mutex:
892 if (need_i_mutex)
893 mutex_unlock(&inode->i_mutex);
894 return -error;
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100898xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100900 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100902 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100904 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
905 return -EIO;
906 return 0;
907}
908
909STATIC int
910xfs_dir_open(
911 struct inode *inode,
912 struct file *file)
913{
914 struct xfs_inode *ip = XFS_I(inode);
915 int mode;
916 int error;
917
918 error = xfs_file_open(inode, file);
919 if (error)
920 return error;
921
922 /*
923 * If there are any blocks, read-ahead block 0 as we're almost
924 * certain to have the next operation be a read there.
925 */
926 mode = xfs_ilock_map_shared(ip);
927 if (ip->i_d.di_nextents > 0)
928 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
929 xfs_iunlock(ip, mode);
930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100934xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct inode *inode,
936 struct file *filp)
937{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000938 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100942xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct file *filp,
944 void *dirent,
945 filldir_t filldir)
946{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000947 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000948 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000949 int error;
950 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000952 /*
953 * The Linux API doesn't pass down the total size of the buffer
954 * we read into down to the filesystem. With the filldir concept
955 * it's not needed for correct information, but the XFS dir2 leaf
956 * code wants an estimate of the buffer size to calculate it's
957 * readahead window and size the buffers used for mapping to
958 * physical blocks.
959 *
960 * Try to give it an estimate that's good enough, maybe at some
961 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +0000962 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000963 */
Eric Sandeena9cc7992010-02-03 17:50:13 +0000964 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000966 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000967 (xfs_off_t *)&filp->f_pos, filldir);
968 if (error)
969 return -error;
970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100974xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct file *filp,
976 struct vm_area_struct *vma)
977{
Nathan Scott3562fd42006-03-14 14:00:35 +1100978 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -0700979 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000980
Nathan Scottfbc14622006-06-09 14:52:13 +1000981 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return 0;
983}
984
David Chinner4f57dbc2007-07-19 16:28:17 +1000985/*
986 * mmap()d file has taken write protection fault and is being made
987 * writable. We can set the page state up correctly for a writable
988 * page, which means we can do correct delalloc accounting (ENOSPC
989 * checking!) and unwritten extent mapping.
990 */
991STATIC int
992xfs_vm_page_mkwrite(
993 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -0700994 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +1000995{
Nick Pigginc2ec1752009-03-31 15:23:21 -0700996 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +1000997}
998
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800999const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 .llseek = generic_file_llseek,
1001 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +10001002 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001003 .aio_read = xfs_file_aio_read,
1004 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +10001005 .splice_read = xfs_file_splice_read,
1006 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001007 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001009 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001011 .mmap = xfs_file_mmap,
1012 .open = xfs_file_open,
1013 .release = xfs_file_release,
1014 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015#ifdef HAVE_FOP_OPEN_EXEC
Nathan Scott3562fd42006-03-14 14:00:35 +11001016 .open_exec = xfs_file_open_exec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017#endif
1018};
1019
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001020const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001021 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +11001023 .readdir = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001024 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001025 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001026#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001027 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001028#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001029 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030};
1031
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001032static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001033 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +10001034 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001035};