blob: 0abc7d0586c925c7e12c8ef9db4573dc71f75603 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_inode_item.h"
48#include "xfs_buf_item.h"
49#include "xfs_utils.h"
50#include "xfs_iomap.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100051#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include <linux/capability.h>
54#include <linux/writeback.h>
55
56
57#if defined(XFS_RW_TRACE)
58void
59xfs_rw_enter_trace(
60 int tag,
61 xfs_iocore_t *io,
62 void *data,
63 size_t segs,
64 loff_t offset,
65 int ioflags)
66{
67 xfs_inode_t *ip = XFS_IO_INODE(io);
68
69 if (ip->i_rwtrace == NULL)
70 return;
71 ktrace_enter(ip->i_rwtrace,
72 (void *)(unsigned long)tag,
73 (void *)ip,
74 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
75 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
76 (void *)data,
77 (void *)((unsigned long)segs),
78 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
79 (void *)((unsigned long)(offset & 0xffffffff)),
80 (void *)((unsigned long)ioflags),
81 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
82 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110083 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 (void *)NULL,
85 (void *)NULL,
86 (void *)NULL,
87 (void *)NULL);
88}
89
90void
91xfs_inval_cached_trace(
92 xfs_iocore_t *io,
93 xfs_off_t offset,
94 xfs_off_t len,
95 xfs_off_t first,
96 xfs_off_t last)
97{
98 xfs_inode_t *ip = XFS_IO_INODE(io);
99
100 if (ip->i_rwtrace == NULL)
101 return;
102 ktrace_enter(ip->i_rwtrace,
103 (void *)(__psint_t)XFS_INVAL_CACHED,
104 (void *)ip,
105 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
106 (void *)((unsigned long)(offset & 0xffffffff)),
107 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
108 (void *)((unsigned long)(len & 0xffffffff)),
109 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
110 (void *)((unsigned long)(first & 0xffffffff)),
111 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
112 (void *)((unsigned long)(last & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +1100113 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 (void *)NULL,
115 (void *)NULL,
116 (void *)NULL,
117 (void *)NULL,
118 (void *)NULL);
119}
120#endif
121
122/*
123 * xfs_iozero
124 *
125 * xfs_iozero clears the specified range of buffer supplied,
126 * and marks all the affected blocks as valid and modified. If
127 * an affected block is not allocated, it will be allocated. If
128 * an affected block is not completely overwritten, and is not
129 * valid before the operation, it will be read from disk before
130 * being partially zeroed.
131 */
132STATIC int
133xfs_iozero(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000134 struct xfs_inode *ip, /* inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 loff_t pos, /* offset in file */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100136 size_t count) /* size of data to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct page *page;
139 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int status;
141
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000142 mapping = ip->i_vnode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 do {
Nick Piggind79689c2007-10-16 01:25:06 -0700144 unsigned offset, bytes;
145 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 bytes = PAGE_CACHE_SIZE - offset;
149 if (bytes > count)
150 bytes = count;
151
Nick Piggind79689c2007-10-16 01:25:06 -0700152 status = pagecache_write_begin(NULL, mapping, pos, bytes,
153 AOP_FLAG_UNINTERRUPTIBLE,
154 &page, &fsdata);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100155 if (status)
Nick Piggind79689c2007-10-16 01:25:06 -0700156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Christoph Hellwig700716c2007-05-24 15:27:17 +1000158 zero_user_page(page, offset, bytes, KM_USER0);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100159
Nick Piggind79689c2007-10-16 01:25:06 -0700160 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
161 page, fsdata);
162 WARN_ON(status <= 0); /* can't return less than zero! */
163 pos += bytes;
164 count -= bytes;
165 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 } while (count);
167
168 return (-status);
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171ssize_t /* bytes read, or (-) error */
172xfs_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000173 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct kiocb *iocb,
175 const struct iovec *iovp,
176 unsigned int segs,
177 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000178 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct file *file = iocb->ki_filp;
181 struct inode *inode = file->f_mapping->host;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000182 bhv_vnode_t *vp = XFS_ITOV(ip);
183 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 size_t size = 0;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000185 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 xfs_fsize_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned long seg;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 XFS_STATS_INC(xs_read_calls);
191
192 /* START copy & waste from filemap.c */
193 for (seg = 0; seg < segs; seg++) {
194 const struct iovec *iv = &iovp[seg];
195
196 /*
197 * If any segment has a negative length, or the cumulative
198 * length ever wraps negative then return -EINVAL.
199 */
200 size += iv->iov_len;
201 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
202 return XFS_ERROR(-EINVAL);
203 }
204 /* END copy & waste from filemap.c */
205
206 if (unlikely(ioflags & IO_ISDIRECT)) {
207 xfs_buftarg_t *target =
208 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
209 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100210 if ((*offset & target->bt_smask) ||
211 (size & target->bt_smask)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000212 if (*offset == ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return (0);
214 }
215 return -XFS_ERROR(EINVAL);
216 }
217 }
218
219 n = XFS_MAXIOFFSET(mp) - *offset;
220 if ((n <= 0) || (size == 0))
221 return 0;
222
223 if (n < size)
224 size = n;
225
Nathan Scotta13828b2006-03-17 17:26:14 +1100226 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800230 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 xfs_ilock(ip, XFS_IOLOCK_SHARED);
232
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000233 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000234 bhv_vrwlock_t locktype = VRWLOCK_READ;
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000235 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Christoph Hellwig993386c2007-08-28 16:12:30 +1000237 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *offset, size,
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000238 dmflags, &locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (ret) {
240 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000241 if (unlikely(ioflags & IO_ISDIRECT))
242 mutex_unlock(&inode->i_mutex);
243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 }
246
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000247 if (unlikely(ioflags & IO_ISDIRECT)) {
248 if (VN_CACHED(vp))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000249 ret = xfs_flushinval_pages(ip,
250 ctooff(offtoct(*offset)),
251 -1, FI_REMAPF_LOCKED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000252 mutex_unlock(&inode->i_mutex);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000253 if (ret) {
254 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
255 return ret;
256 }
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000257 }
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
260 (void *)iovp, segs, *offset, ioflags);
Badari Pulavarty543ade12006-09-30 23:28:48 -0700261
262 iocb->ki_pos = *offset;
263 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
265 ret = wait_on_sync_kiocb(iocb);
266 if (ret > 0)
267 XFS_STATS_ADD(xs_read_bytes, ret);
268
269 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return ret;
271}
272
273ssize_t
Nathan Scott1b895842006-03-31 13:08:59 +1000274xfs_splice_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000275 xfs_inode_t *ip,
Nathan Scott1b895842006-03-31 13:08:59 +1000276 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200277 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200278 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000279 size_t count,
280 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000281 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000282{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000283 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott1b895842006-03-31 13:08:59 +1000284 xfs_mount_t *mp = ip->i_mount;
285 ssize_t ret;
286
287 XFS_STATS_INC(xs_read_calls);
288 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
289 return -EIO;
290
291 xfs_ilock(ip, XFS_IOLOCK_SHARED);
292
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000293 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000294 bhv_vrwlock_t locktype = VRWLOCK_READ;
Nathan Scott1b895842006-03-31 13:08:59 +1000295 int error;
296
Christoph Hellwig993386c2007-08-28 16:12:30 +1000297 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000298 FILP_DELAY_FLAG(infilp), &locktype);
299 if (error) {
300 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
301 return -error;
302 }
303 }
304 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200305 pipe, count, *ppos, ioflags);
306 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000307 if (ret > 0)
308 XFS_STATS_ADD(xs_read_bytes, ret);
309
310 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
311 return ret;
312}
313
314ssize_t
315xfs_splice_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000316 xfs_inode_t *ip,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200317 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000318 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200319 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000320 size_t count,
321 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000322 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000323{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000324 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott1b895842006-03-31 13:08:59 +1000325 xfs_mount_t *mp = ip->i_mount;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000326 xfs_iocore_t *io = &ip->i_iocore;
Nathan Scott1b895842006-03-31 13:08:59 +1000327 ssize_t ret;
David Chinner0a8d17d2006-09-07 14:27:15 +1000328 struct inode *inode = outfilp->f_mapping->host;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000329 xfs_fsize_t isize, new_size;
Nathan Scott1b895842006-03-31 13:08:59 +1000330
331 XFS_STATS_INC(xs_write_calls);
332 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
333 return -EIO;
334
335 xfs_ilock(ip, XFS_IOLOCK_EXCL);
336
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000337 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000338 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
Nathan Scott1b895842006-03-31 13:08:59 +1000339 int error;
340
Christoph Hellwig993386c2007-08-28 16:12:30 +1000341 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp, *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000342 FILP_DELAY_FLAG(outfilp), &locktype);
343 if (error) {
344 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
345 return -error;
346 }
347 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000348
349 new_size = *ppos + count;
350
351 xfs_ilock(ip, XFS_ILOCK_EXCL);
352 if (new_size > ip->i_size)
353 io->io_new_size = new_size;
354 xfs_iunlock(ip, XFS_ILOCK_EXCL);
355
Nathan Scott1b895842006-03-31 13:08:59 +1000356 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200357 pipe, count, *ppos, ioflags);
358 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000359 if (ret > 0)
360 XFS_STATS_ADD(xs_write_bytes, ret);
361
David Chinner0a8d17d2006-09-07 14:27:15 +1000362 isize = i_size_read(inode);
363 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
364 *ppos = isize;
365
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000366 if (*ppos > ip->i_size) {
David Chinner0a8d17d2006-09-07 14:27:15 +1000367 xfs_ilock(ip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000368 if (*ppos > ip->i_size)
369 ip->i_size = *ppos;
370 xfs_iunlock(ip, XFS_ILOCK_EXCL);
371 }
372
373 if (io->io_new_size) {
374 xfs_ilock(ip, XFS_ILOCK_EXCL);
375 io->io_new_size = 0;
376 if (ip->i_d.di_size > ip->i_size)
377 ip->i_d.di_size = ip->i_size;
David Chinner0a8d17d2006-09-07 14:27:15 +1000378 xfs_iunlock(ip, XFS_ILOCK_EXCL);
379 }
Nathan Scott1b895842006-03-31 13:08:59 +1000380 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 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(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000392 xfs_inode_t *ip,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100393 xfs_fsize_t offset,
394 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 xfs_fileoff_t last_fsb;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000397 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int nimaps;
399 int zero_offset;
400 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int error = 0;
402 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000404 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Eric Sandeen24ee8082006-01-11 15:34:32 +1100406 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
407 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /*
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;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000417 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000418 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 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 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000435 xfs_iunlock(ip, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
Nathan Scott1b895842006-03-31 13:08:59 +1000436
Eric Sandeen24ee8082006-01-11 15:34:32 +1100437 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100438 if (isize + zero_len > offset)
439 zero_len = offset - isize;
440 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000442 xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 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(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000460 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100462 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000464 xfs_iocore_t *io = &ip->i_iocore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 xfs_fileoff_t start_zero_fsb;
466 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 xfs_fileoff_t zero_count_fsb;
468 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100469 xfs_fileoff_t zero_off;
470 xfs_fsize_t zero_len;
Nathan Scott1b895842006-03-31 13:08:59 +1000471 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int nimaps;
473 int error = 0;
474 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
477 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100478 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
481 * First handle zeroing the block on which isize resides.
482 * We only zero a part of that block so it is handled specially.
483 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000484 error = xfs_zero_last_block(ip, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (error) {
486 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
487 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
488 return error;
489 }
490
491 /*
492 * Calculate the range between the new size and the old
493 * where blocks needing to be zeroed may exist. To get the
494 * block where the last byte in the file currently resides,
495 * we need to subtract one from the size and truncate back
496 * to a block boundary. We subtract 1 in case the size is
497 * exactly on a block boundary.
498 */
499 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
500 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
501 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
502 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
503 if (last_fsb == end_zero_fsb) {
504 /*
505 * The size was only incremented on its last block.
506 * We took care of that above, so just return.
507 */
508 return 0;
509 }
510
511 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 while (start_zero_fsb <= end_zero_fsb) {
513 nimaps = 1;
514 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000515 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000516 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (error) {
518 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
519 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
520 return error;
521 }
522 ASSERT(nimaps > 0);
523
524 if (imap.br_state == XFS_EXT_UNWRITTEN ||
525 imap.br_startblock == HOLESTARTBLOCK) {
526 /*
527 * This loop handles initializing pages that were
528 * partially initialized by the code below this
529 * loop. It basically zeroes the part of the page
530 * that sits on a hole and sets the page as P_HOLE
531 * and calls remapf if it is a mapped file.
532 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100533 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
535 continue;
536 }
537
538 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000539 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * Drop the inode lock while we're doing the I/O.
541 * We'll still have the iolock to protect us.
542 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000543 xfs_iunlock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Lachlan McIlroy68160162007-02-10 18:36:47 +1100545 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
546 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
547
548 if ((zero_off + zero_len) > offset)
549 zero_len = offset - zero_off;
550
551 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (error) {
553 goto out_lock;
554 }
555
Nathan Scott3d80ede2006-06-09 14:57:30 +1000556 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
558
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000559 xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 return 0;
563
564out_lock:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000565 xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 ASSERT(error >= 0);
567 return error;
568}
569
570ssize_t /* bytes written, or (-) error */
571xfs_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000572 struct xfs_inode *xip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct kiocb *iocb,
574 const struct iovec *iovp,
575 unsigned int nsegs,
576 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000577 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct file *file = iocb->ki_filp;
580 struct address_space *mapping = file->f_mapping;
581 struct inode *inode = mapping->host;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000582 bhv_vnode_t *vp = XFS_ITOV(xip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned long segs = nsegs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 xfs_mount_t *mp;
585 ssize_t ret = 0, error = 0;
586 xfs_fsize_t isize, new_size;
587 xfs_iocore_t *io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int iolock;
589 int eventsent = 0;
Nathan Scott8285fb52006-06-09 17:07:12 +1000590 bhv_vrwlock_t locktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 size_t ocount = 0, count;
592 loff_t pos;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000593 int need_i_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 XFS_STATS_INC(xs_write_calls);
596
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -0700597 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
598 if (error)
599 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 count = ocount;
602 pos = *offset;
603
604 if (count == 0)
605 return 0;
606
607 io = &xip->i_iocore;
608 mp = io->io_mount;
609
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000610 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
Nathan Scott34327e12006-06-09 17:11:55 +1000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (XFS_FORCED_SHUTDOWN(mp))
613 return -EIO;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615relock:
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000616 if (ioflags & IO_ISDIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 iolock = XFS_IOLOCK_SHARED;
618 locktype = VRWLOCK_WRITE_DIRECT;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000619 need_i_mutex = 0;
620 } else {
621 iolock = XFS_IOLOCK_EXCL;
622 locktype = VRWLOCK_WRITE;
623 need_i_mutex = 1;
624 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629start:
630 error = -generic_write_checks(file, &pos, &count,
631 S_ISBLK(inode->i_mode));
632 if (error) {
633 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100634 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000637 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 !(ioflags & IO_INVIS) && !eventsent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int dmflags = FILP_DELAY_FLAG(file);
640
Nathan Scotta13828b2006-03-17 17:26:14 +1100641 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800642 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 xfs_iunlock(xip, XFS_ILOCK_EXCL);
645 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
646 pos, count,
647 dmflags, &locktype);
648 if (error) {
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000649 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 xfs_ilock(xip, XFS_ILOCK_EXCL);
652 eventsent = 1;
653
654 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000655 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * so we have to recheck the size when appending.
657 * We will only "goto start;" once, since having sent the
658 * event prevents another call to XFS_SEND_DATA, which is
659 * what allows the size to change in the first place.
660 */
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000661 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
662 goto start;
663 }
664
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000665 if (ioflags & IO_ISDIRECT) {
666 xfs_buftarg_t *target =
667 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
668 mp->m_rtdev_targp : mp->m_ddev_targp;
669
670 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
671 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
672 return XFS_ERROR(-EINVAL);
673 }
674
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000675 if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000676 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
677 iolock = XFS_IOLOCK_EXCL;
678 locktype = VRWLOCK_WRITE;
679 need_i_mutex = 1;
680 mutex_lock(&inode->i_mutex);
681 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto start;
683 }
684 }
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 new_size = pos + count;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000687 if (new_size > xip->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 io->io_new_size = new_size;
689
Nathan Scott4aeb6642005-11-02 11:43:58 +1100690 if (likely(!(ioflags & IO_INVIS))) {
Christoph Hellwig870f4812006-01-09 20:52:01 -0800691 file_update_time(file);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100692 xfs_ichgtime_fast(xip, inode,
693 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
696 /*
697 * If the offset is beyond the size of the file, we have a couple
698 * of things to do. First, if there is already space allocated
699 * we need to either create holes or zero the disk or ...
700 *
701 * If there is a page where the previous size lands, we need
702 * to zero it out up to the new size.
703 */
704
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000705 if (pos > xip->i_size) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000706 error = xfs_zero_eof(xip, pos, xip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (error) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000708 xfs_iunlock(xip, XFS_ILOCK_EXCL);
709 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 }
712 xfs_iunlock(xip, XFS_ILOCK_EXCL);
713
714 /*
715 * If we're writing the file then make sure to clear the
716 * setuid and setgid bits if the process is not being run
717 * by root. This keeps people from modifying setuid and
718 * setgid binaries.
719 */
720
721 if (((xip->i_d.di_mode & S_ISUID) ||
722 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
723 (S_ISGID | S_IXGRP))) &&
724 !capable(CAP_FSETID)) {
725 error = xfs_write_clear_setuid(xip);
726 if (likely(!error))
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800727 error = -remove_suid(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (unlikely(error)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000729 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 }
732
733retry:
734 /* We can write back this queue in page reclaim */
735 current->backing_dev_info = mapping->backing_dev_info;
736
737 if ((ioflags & IO_ISDIRECT)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000738 if (VN_CACHED(vp)) {
739 WARN_ON(need_i_mutex == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 xfs_inval_cached_trace(io, pos, -1,
741 ctooff(offtoct(pos)), -1);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000742 error = xfs_flushinval_pages(xip,
743 ctooff(offtoct(pos)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000745 if (error)
746 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Nathan Scotta13828b2006-03-17 17:26:14 +1100749 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* demote the lock now the cached pages are gone */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000751 xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800752 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 iolock = XFS_IOLOCK_SHARED;
755 locktype = VRWLOCK_WRITE_DIRECT;
Nathan Scotta13828b2006-03-17 17:26:14 +1100756 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
760 *offset, ioflags);
761 ret = generic_file_direct_write(iocb, iovp,
762 &segs, pos, offset, count, ocount);
763
764 /*
765 * direct-io write to a hole: fall through to buffered I/O
766 * for completing the rest of the request.
767 */
768 if (ret >= 0 && ret != count) {
769 XFS_STATS_ADD(xs_write_bytes, ret);
770
771 pos += ret;
772 count -= ret;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 ioflags &= ~IO_ISDIRECT;
775 xfs_iunlock(xip, iolock);
776 goto relock;
777 }
778 } else {
779 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
780 *offset, ioflags);
781 ret = generic_file_buffered_write(iocb, iovp, segs,
782 pos, offset, count, ret);
783 }
784
785 current->backing_dev_info = NULL;
786
787 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
788 ret = wait_on_sync_kiocb(iocb);
789
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000790 if (ret == -ENOSPC &&
791 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
Christoph Hellwig993386c2007-08-28 16:12:30 +1000792 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100793 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800794 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
796 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
797 0, 0, 0); /* Delay flag intentionally unused */
Nathan Scotta13828b2006-03-17 17:26:14 +1100798 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800799 mutex_lock(&inode->i_mutex);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000800 xfs_rwlock(xip, locktype);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000801 if (error)
802 goto out_unlock_internal;
803 pos = xip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ret = 0;
805 goto retry;
806 }
807
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100808 isize = i_size_read(inode);
809 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
810 *offset = isize;
811
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000812 if (*offset > xip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 xfs_ilock(xip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000814 if (*offset > xip->i_size)
815 xip->i_size = *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 xfs_iunlock(xip, XFS_ILOCK_EXCL);
817 }
818
819 error = -ret;
820 if (ret <= 0)
821 goto out_unlock_internal;
822
823 XFS_STATS_ADD(xs_write_bytes, ret);
824
825 /* Handle various SYNC-type writes */
826 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000827 int error2;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000828 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100829 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800830 mutex_unlock(&inode->i_mutex);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000831 error2 = sync_page_range(inode, mapping, pos, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!error)
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000833 error = error2;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000834 if (need_i_mutex)
835 mutex_lock(&inode->i_mutex);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000836 xfs_rwlock(xip, locktype);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000837 error2 = xfs_write_sync_logforce(mp, xip);
838 if (!error)
839 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 out_unlock_internal:
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000843 if (io->io_new_size) {
844 xfs_ilock(xip, XFS_ILOCK_EXCL);
845 io->io_new_size = 0;
846 /*
847 * If this was a direct or synchronous I/O that failed (such
848 * as ENOSPC) then part of the I/O may have been written to
849 * disk before the error occured. In this case the on-disk
850 * file size may have been adjusted beyond the in-memory file
851 * size and now needs to be truncated back.
852 */
853 if (xip->i_d.di_size > xip->i_size)
854 xip->i_d.di_size = xip->i_size;
855 xfs_iunlock(xip, XFS_ILOCK_EXCL);
856 }
Christoph Hellwig993386c2007-08-28 16:12:30 +1000857 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100858 out_unlock_mutex:
859 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800860 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -error;
862}
863
864/*
865 * All xfs metadata buffers except log state machine buffers
866 * get this attached as their b_bdstrat callback function.
867 * This is so that we can catch a buffer
868 * after prematurely unpinning it to forcibly shutdown the filesystem.
869 */
870int
871xfs_bdstrat_cb(struct xfs_buf *bp)
872{
873 xfs_mount_t *mp;
874
875 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
876 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100877 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879 } else {
880 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
881 /*
882 * Metadata write that didn't get logged but
883 * written delayed anyway. These aren't associated
884 * with a transaction, and can be ignored.
885 */
886 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
887 (XFS_BUF_ISREAD(bp)) == 0)
888 return (xfs_bioerror_relse(bp));
889 else
890 return (xfs_bioerror(bp));
891 }
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/*
895 * Wrapper around bdstrat so that we can stop data
896 * from going to disk in case we are shutting down the filesystem.
897 * Typically user data goes thru this path; one of the exceptions
898 * is the superblock.
899 */
900int
901xfsbdstrat(
902 struct xfs_mount *mp,
903 struct xfs_buf *bp)
904{
905 ASSERT(mp);
906 if (!XFS_FORCED_SHUTDOWN(mp)) {
907 /* Grio redirection would go here
908 * if (XFS_BUF_IS_GRIO(bp)) {
909 */
910
Nathan Scottce8e9222006-01-11 15:39:08 +1100911 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913 }
914
915 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
916 return (xfs_bioerror_relse(bp));
917}
918
919/*
920 * If the underlying (data/log/rt) device is readonly, there are some
921 * operations that cannot proceed.
922 */
923int
924xfs_dev_is_read_only(
925 xfs_mount_t *mp,
926 char *message)
927{
928 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
929 xfs_readonly_buftarg(mp->m_logdev_targp) ||
930 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
931 cmn_err(CE_NOTE,
932 "XFS: %s required on read-only device.", message);
933 cmn_err(CE_NOTE,
934 "XFS: write access unavailable, cannot proceed.");
935 return EROFS;
936 }
937 return 0;
938}